[WIP] sale_order_batch: restructure batch product

This commit is contained in:
NIels Göttsch 2025-03-10 20:56:57 +01:00 committed by Niels Göttsch
parent 9bab583996
commit 0297c66ce2
3 changed files with 32 additions and 9 deletions

View file

@ -41,3 +41,11 @@ class SaleOrder(models.Model):
if invalid_orders: if invalid_orders:
raise UserError(_(f"Sale Order belongs to a Batch: {', '.join(invalid_orders)}")) raise UserError(_(f"Sale Order belongs to a Batch: {', '.join(invalid_orders)}"))
return super().action_confirm() return super().action_confirm()
# TODO: Unlink batch_product when no so lines anymore
def write(self, vals):
res = super().write(vals)
if "batch_id" in vals.keys():
for order in self:
order.order_line._update_batch_product()
return res

View file

@ -30,12 +30,6 @@ class SaleOrderBatchProduct(models.Model):
product_packaging_id = fields.Many2one("product.packaging") product_packaging_id = fields.Many2one("product.packaging")
product_packaging_qty = fields.Float(compute="_compute_product_packaging_qty") product_packaging_qty = fields.Float(compute="_compute_product_packaging_qty")
@api.depends("sale_order_line_ids")
def _update_batch_products(self):
for product in self:
if not product.sale_order_line_ids:
product.unlink()
@api.depends("sale_order_line_ids.product_uom_qty") @api.depends("sale_order_line_ids.product_uom_qty")
def _compute_uom_qty(self): def _compute_uom_qty(self):
for product in self: for product in self:
@ -45,3 +39,9 @@ class SaleOrderBatchProduct(models.Model):
def _compute_product_packaging_qty(self): def _compute_product_packaging_qty(self):
for product in self: for product in self:
product.product_packaging_qty = product.product_packaging_id.qty if product.product_packaging_id else 1 product.product_packaging_qty = product.product_packaging_id.qty if product.product_packaging_id else 1
def unlink(self):
for product in self:
if not product.sale_order_line_ids:
return super().unlink()
return True

View file

@ -10,23 +10,38 @@ class SaleOrderLine(models.Model):
# TODO: # TODO:
# - recompute values on batchproduct # - recompute values on batchproduct
# - check for last item on unlink or removal ob batch_id and remove batch_product_id # - check for last item on unlink or removal ob batch_id and remove batch_product_id
@api.depends("batch_id", "batch_product_id")
def _update_batch_product(self): def _update_batch_product(self):
for line in self: for line in self:
batch_id = line.batch_id batch_id = line.batch_id
if batch_id: if batch_id:
batch_product = line.env["sale.order.batch.product"].search( batch_product = line.env["sale.order.batch.product"].search(
[("product_id", "=", line.product_id.id), ("batch_id", "=", batch_id.id)] [("product_id", "=", line.product_id.id), ("batch_id", "=", batch_id.id)], limit=1
) )
if not batch_product: if not batch_product:
batch_product = line.env["sale.order.batch.product"].create( batch_product = line.env["sale.order.batch.product"].create(
{"batch_id": batch_id.id, "product_id": line.product_id.id} {"batch_id": batch_id.id, "product_id": line.product_id.id}
) )
line.batch_product_id = batch_product
else:
line.batch_product_id.unlink()
line.batch_product_id = False
@api.model_create_multi @api.model_create_multi
def create(self, vals_list): def create(self, vals_list):
res = super().create(vals_list) res = super().create(vals_list)
for line in res: for line in res:
if line.batch_id: if line.batch_id:
res._update_batch_product() line._update_batch_product()
return res return res
def write(self, vals):
res = super().write(vals)
if vals.get("product_id"):
self._update_batch_product()
return res
def unlink(self):
for line in self:
if line.batch_product_id:
line.batch_product_id.unlink()
return super().unlink()