[imp] nfu_sale_order_batch_packaging: remove product recreation and duplicated code

This commit is contained in:
Niels Göttsch 2025-03-18 20:31:46 +01:00 committed by madmooose
parent 105e3e252f
commit b8aa9edb80
8 changed files with 23 additions and 45 deletions

View file

@ -4,6 +4,7 @@ from odoo import models
class ProductTemplate(models.Model): class ProductTemplate(models.Model):
_inherit = "product.template" _inherit = "product.template"
# TODO: remove here and only use product.product
def _get_nfu_packaging(self): def _get_nfu_packaging(self):
self.ensure_one() self.ensure_one()
if self.packaging_ids: if self.packaging_ids:

View file

@ -1,11 +0,0 @@
from odoo import models
class SaleOrderBatch(models.Model):
_inherit = "sale.order.batch"
def action_recompute_products(self):
for batch in self:
batch.product_ids.unlink()
for line in batch.sale_order_line_ids:
line._update_batch_product()

View file

@ -11,6 +11,14 @@ class SaleOrderBatchProduct(models.Model):
open_packaging_qty = fields.Float(compute="_compute_open_packagin_qty", store=True) open_packaging_qty = fields.Float(compute="_compute_open_packagin_qty", store=True)
open_packaging_state = fields.Selection(selection=PACKAGING_STATES, compute="_compute_open_packagin_state") open_packaging_state = fields.Selection(selection=PACKAGING_STATES, compute="_compute_open_packagin_state")
@api.depends("sale_order_line_ids.product_uom_max_qty")
def _compute_product_uom_max_qty(self):
for product in self:
if product.sale_order_line_ids:
product.product_uom_max_qty = sum(product.sale_order_line_ids.mapped("product_uom_max_qty"))
else:
product.product_uom_max_qty = False
@api.depends("sale_order_line_ids.product_uom_qty") @api.depends("sale_order_line_ids.product_uom_qty")
def _compute_open_packagin_qty(self): def _compute_open_packagin_qty(self):
for product in self: for product in self:
@ -21,14 +29,6 @@ class SaleOrderBatchProduct(models.Model):
0 if open_packaging_qty == product.product_packaging_qty else open_packaging_qty 0 if open_packaging_qty == product.product_packaging_qty else open_packaging_qty
) )
@api.depends("sale_order_line_ids.product_uom_max_qty")
def _compute_product_uom_max_qty(self):
for product in self:
if product.sale_order_line_ids:
product.product_uom_max_qty = sum(product.sale_order_line_ids.mapped("product_uom_max_qty"))
else:
product.product_uom_max_qty = 0
@api.depends("open_packaging_qty", "product_packaging_qty") @api.depends("open_packaging_qty", "product_packaging_qty")
def _compute_open_packagin_state(self): def _compute_open_packagin_state(self):
for product in self: for product in self:

View file

@ -25,25 +25,18 @@ class SaleOrderLine(models.Model):
@api.depends("product_uom_qty", "product_uom_max_qty") @api.depends("product_uom_qty", "product_uom_max_qty")
def _compute_batch_uom_qty(self): def _compute_batch_uom_qty(self):
for line in self: for line in self:
if line.order_id.batch_id: if line.batch_product_id:
batch_id = line.batch_id line.batch_uom_qty = line.batch_product_id.product_uom_qty
product_id = line.product_id line.batch_uom_max_qty = line.batch_product_id.product_uom_max_qty
precision_rounding = line.product_id.uom_id.rounding
precision_digits = len(str(precision_rounding).split(".")[1])
batch_lines = (
self.env["sale.order.line"]
.sudo()
.search([("batch_id", "=", batch_id.id), ("product_id", "=", product_id.id)])
)
line.batch_uom_qty = sum(batch_lines.mapped("product_uom_qty"))
line.batch_uom_max_qty = round(sum(batch_lines.mapped("product_uom_max_qty")), precision_digits)
else: else:
line.batch_uom_max_qty = line.batch_uom_qty = 0 line.batch_uom_max_qty = line.batch_uom_qty = 0
@api.depends("product_packaging_id", "product_uom", "product_uom_qty", "batch_id") @api.depends("product_packaging_id", "product_uom", "product_uom_qty", "batch_id")
def _compute_product_packaging_qty(self): def _compute_product_packaging_qty(self):
for line in self: for line in self:
if line.batch_id and line.product_packaging_id: if not line.product_packaging_id:
line.product_packaging_qty = False
elif line.batch_id:
packaging_uom = line.product_packaging_id.product_uom_id packaging_uom = line.product_packaging_id.product_uom_id
batch_uom_qty = line.product_uom._compute_quantity(line.batch_uom_qty, packaging_uom) batch_uom_qty = line.product_uom._compute_quantity(line.batch_uom_qty, packaging_uom)
line.product_packaging_qty = float_round( line.product_packaging_qty = float_round(

View file

@ -1,15 +1,5 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<odoo> <odoo>
<record id="action_update_batch_products_binding" model="ir.actions.server">
<field name="name">Regenerate Products</field>
<field name="model_id" ref="model_sale_order_batch"/>
<field name="binding_model_id" ref="model_sale_order_batch"/>
<field name="state">code</field>
<field name="code">
for batch in records:
batch.action_recompute_products()
</field>
</record>
<record id="view_order_batch_form" model="ir.ui.view"> <record id="view_order_batch_form" model="ir.ui.view">
<field name="name">sale.order.batch.form.product.min_max_qty</field> <field name="name">sale.order.batch.form.product.min_max_qty</field>
<field name="model">sale.order.batch</field> <field name="model">sale.order.batch</field>

View file

@ -32,7 +32,6 @@ class SaleOrderBatchProduct(models.Model):
readonly=False, readonly=False,
precompute=True, precompute=True,
domain="[('sales', '=', True), ('product_id','=',product_id)]", domain="[('sales', '=', True), ('product_id','=',product_id)]",
check_company=True,
) )
product_packaging_qty = fields.Float(compute="_compute_product_packaging_qty") product_packaging_qty = fields.Float(compute="_compute_product_packaging_qty")

View file

@ -23,8 +23,13 @@
</group> </group>
<notebook> <notebook>
<page string="Sale Order Lines" name="order_line"> <page string="Sale Order Lines" name="order_line">
<field name="sale_order_line_ids" widget="section_and_note_one2many"> <field
name="sale_order_line_ids"
widget="section_and_note_one2many"
attrs="{'readonly': [('state', 'in', ('closed'))]}"
>
<tree editable="bottom" create="0"> <tree editable="bottom" create="0">
<field name="company_id" invisible="1"/>
<field name="sequence" widget="handle"/> <field name="sequence" widget="handle"/>
<field name="order_id"/> <field name="order_id"/>
<field name="order_partner_id"/> <field name="order_partner_id"/>

View file

@ -122,11 +122,12 @@
widget="section_and_note_one2many" widget="section_and_note_one2many"
attrs="{'readonly': [('state', 'in', ('closed'))]}" attrs="{'readonly': [('state', 'in', ('closed'))]}"
> >
<tree editable="bottom"> <tree create="0" editable="bottom">
<field name="sequence" widget="handle"/> <field name="sequence" widget="handle"/>
<field name="order_id"/> <field name="order_id"/>
<field name="order_partner_id"/> <field name="order_partner_id"/>
<field name="product_template_id" optional="hide"/> <field name="product_template_id" optional="hide"/>
<field name="product_id" optional="hide"/>
<field name="name"/> <field name="name"/>
<field name="product_uom_qty"/> <field name="product_uom_qty"/>
<field name="company_id" invisible="1"/> <field name="company_id" invisible="1"/>