[IMP] sale_order_batch: restructure batch product

This commit is contained in:
Niels Göttsch 2025-03-10 22:20:30 +01:00 committed by Niels Göttsch
parent 0297c66ce2
commit c1d3d4fa28
7 changed files with 39 additions and 29 deletions

View file

@ -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
===========

View file

@ -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",

View file

@ -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

View file

@ -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(
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 = line.env["sale.order.batch.product"].create(
batch_product = self.env["sale.order.batch.product"].create(
{"batch_id": batch_id.id, "product_id": line.product_id.id}
)
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.unlink()
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()

View file

@ -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

View file

@ -402,6 +402,8 @@ anymore as log as they are part of a batch. Only Sale Orders in state Qutotation
</tr>
<tr class="field"><th class="field-name">1.1.1:</th><td class="field-body">Remove validity date</td>
</tr>
<tr class="field"><th class="field-name">2.0.0:</th><td class="field-body">Move product logic to sale order lines</td>
</tr>
</tbody>
</table>
</div>

View file

@ -26,6 +26,7 @@
<field name="sale_order_line_ids" widget="section_and_note_one2many">
<tree editable="bottom" create="0">
<field name="sequence" widget="handle"/>
<field name="order_id"/>
<field name="name"/>
<field name="order_partner_id"/>
<field name="product_uom_qty"/>