diff --git a/sale_order_batch/README.rst b/sale_order_batch/README.rst index 4c01e31..e370d9b 100644 --- a/sale_order_batch/README.rst +++ b/sale_order_batch/README.rst @@ -39,6 +39,7 @@ Changelog :1.0.1: rename product creation methode :1.1.0: Make validity_date persistent, add batches to SO views :1.1.1: Remove validity date +:2.0.0: Move product logic to sale order lines Bug Tracker =========== diff --git a/sale_order_batch/__manifest__.py b/sale_order_batch/__manifest__.py index e1e2142..59c437f 100644 --- a/sale_order_batch/__manifest__.py +++ b/sale_order_batch/__manifest__.py @@ -4,7 +4,7 @@ "author": "BAKEUP", "website": "https://www.bakeup.org", "category": "Sale", - "version": "16.0.1.1.1", + "version": "16.0.2.0.0", "depends": ["sale", "product"], "data": [ "data/ir_sequence_data.xml", diff --git a/sale_order_batch/models/sale_order_batch_product.py b/sale_order_batch/models/sale_order_batch_product.py index 3eaeaae..9809db7 100644 --- a/sale_order_batch/models/sale_order_batch_product.py +++ b/sale_order_batch/models/sale_order_batch_product.py @@ -39,9 +39,3 @@ 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 08b91d5..fdd0dae 100644 --- a/sale_order_batch/models/sale_order_line.py +++ b/sale_order_batch/models/sale_order_line.py @@ -7,31 +7,44 @@ class SaleOrderLine(models.Model): batch_id = fields.Many2one(related="order_id.batch_id") batch_product_id = fields.Many2one("sale.order.batch.product") - # TODO: - # - recompute values on batchproduct - # - check for last item on unlink or removal ob batch_id and remove batch_product_id - def _update_batch_product(self): - for line in self: + def _link_batch_product(self): + lines_with_batch = self.filtered(lambda l: l.batch_id) + for line in lines_with_batch: 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)], limit=1 + batch_product = self.env["sale.order.batch.product"].search( + [("product_id", "=", line.product_id.id), ("batch_id", "=", batch_id.id)], limit=1 + ) + if not batch_product: + batch_product = self.env["sale.order.batch.product"].create( + {"batch_id": batch_id.id, "product_id": line.product_id.id} ) - 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 + line.batch_product_id = batch_product + + def _unlink_batch_product(self): + lines_to_unlink = self.filtered(lambda l: l.batch_product_id) + batch_product_unlink = self.env["sale.order.batch.product"] + for line in lines_to_unlink: + if line.batch_product_id: + batch_product = line.batch_product_id + if len(batch_product.sale_order_line_ids) == 1: + batch_product_unlink += batch_product + else: + line.batch_product_id = False + batch_product_unlink.unlink() + + def _update_batch_product(self): + lines_with_batch = self.filtered(lambda l: l.batch_id) + lines_without_batch = self - lines_with_batch + lines_with_batch.filtered( + lambda l: l.batch_product_id and l.batch_product_id.product_id != l.product_id + )._unlink_batch_product() + lines_with_batch._link_batch_product() + lines_without_batch._unlink_batch_product() @api.model_create_multi def create(self, vals_list): res = super().create(vals_list) - for line in res: - if line.batch_id: - line._update_batch_product() + res._link_batch_product() return res def write(self, vals): @@ -41,7 +54,5 @@ class SaleOrderLine(models.Model): return res def unlink(self): - for line in self: - if line.batch_product_id: - line.batch_product_id.unlink() + self._unlink_batch_product() return super().unlink() diff --git a/sale_order_batch/readme/HISTORY.rst b/sale_order_batch/readme/HISTORY.rst index ba8435e..5b08b64 100644 --- a/sale_order_batch/readme/HISTORY.rst +++ b/sale_order_batch/readme/HISTORY.rst @@ -3,3 +3,4 @@ :1.0.1: rename product creation methode :1.1.0: Make validity_date persistent, add batches to SO views :1.1.1: Remove validity date +:2.0.0: Move product logic to sale order lines diff --git a/sale_order_batch/static/description/index.html b/sale_order_batch/static/description/index.html index 03f9c90..3bbaf3f 100644 --- a/sale_order_batch/static/description/index.html +++ b/sale_order_batch/static/description/index.html @@ -402,6 +402,8 @@ anymore as log as they are part of a batch. Only Sale Orders in state Qutotation 1.1.1:Remove validity date +2.0.0:Move product logic to sale order lines + diff --git a/sale_order_batch/views/sale_order_batch_product_views.xml b/sale_order_batch/views/sale_order_batch_product_views.xml index b6e6a46..8439332 100644 --- a/sale_order_batch/views/sale_order_batch_product_views.xml +++ b/sale_order_batch/views/sale_order_batch_product_views.xml @@ -26,6 +26,7 @@ +