From e18185e617e7affb4fe99d00a052cd7e381af22a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?NIels=20G=C3=B6ttsch?= Date: Mon, 10 Mar 2025 20:56:57 +0100 Subject: [PATCH] [WIP] sale_order_batch: restructure batch product --- sale_order_batch/models/sale_order.py | 8 +++++++ .../models/sale_order_batch_product.py | 12 +++++------ sale_order_batch/models/sale_order_line.py | 21 ++++++++++++++++--- 3 files changed, 32 insertions(+), 9 deletions(-) diff --git a/sale_order_batch/models/sale_order.py b/sale_order_batch/models/sale_order.py index c6fa7de..51b6235 100644 --- a/sale_order_batch/models/sale_order.py +++ b/sale_order_batch/models/sale_order.py @@ -41,3 +41,11 @@ class SaleOrder(models.Model): if invalid_orders: raise UserError(_(f"Sale Order belongs to a Batch: {', '.join(invalid_orders)}")) 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 diff --git a/sale_order_batch/models/sale_order_batch_product.py b/sale_order_batch/models/sale_order_batch_product.py index a6fae16..3eaeaae 100644 --- a/sale_order_batch/models/sale_order_batch_product.py +++ b/sale_order_batch/models/sale_order_batch_product.py @@ -30,12 +30,6 @@ class SaleOrderBatchProduct(models.Model): product_packaging_id = fields.Many2one("product.packaging") 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") def _compute_uom_qty(self): for product in self: @@ -45,3 +39,9 @@ class SaleOrderBatchProduct(models.Model): def _compute_product_packaging_qty(self): for product in self: 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 diff --git a/sale_order_batch/models/sale_order_line.py b/sale_order_batch/models/sale_order_line.py index c1740b0..08b91d5 100644 --- a/sale_order_batch/models/sale_order_line.py +++ b/sale_order_batch/models/sale_order_line.py @@ -10,23 +10,38 @@ class SaleOrderLine(models.Model): # TODO: # - recompute values on batchproduct # - 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): for line in self: batch_id = line.batch_id if batch_id: 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: batch_product = line.env["sale.order.batch.product"].create( {"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 def create(self, vals_list): res = super().create(vals_list) for line in res: if line.batch_id: - res._update_batch_product() + line._update_batch_product() 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()