[wip] nfu_sale_order_batch_packaging: fix calculation

This commit is contained in:
Niels Göttsch 2025-03-25 16:12:28 +01:00 committed by madmooose
parent 06a114bffc
commit d31e45814a
5 changed files with 39 additions and 41 deletions

View file

@ -1,5 +1,4 @@
from . import product_product from . import product_product
from . import product_template from . import product_template
from . import sale_order_batch
from . import sale_order_line
from . import sale_order_batch_product from . import sale_order_batch_product
from . import sale_order_line

View file

@ -9,7 +9,7 @@ class SaleOrderBatchProduct(models.Model):
product_uom_max_qty = fields.Float("Max Qty", compute="_compute_product_uom_max_qty") product_uom_max_qty = fields.Float("Max Qty", compute="_compute_product_uom_max_qty")
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") @api.depends("sale_order_line_ids.product_uom_max_qty")
def _compute_product_uom_max_qty(self): def _compute_product_uom_max_qty(self):
@ -29,15 +29,15 @@ 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("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:
if product.open_packaging_qty == 0: # if product.open_packaging_qty == 0:
product.open_packaging_state = "full" # product.open_packaging_state = "full"
elif product.open_packaging_qty < product.product_packaging_qty: # elif product.open_packaging_qty < product.product_packaging_qty:
product.open_packaging_state = "open" # product.open_packaging_state = "open"
else: # else:
product.open_packaging_state = "last_open" # product.open_packaging_state = "last_open"
@api.model_create_multi @api.model_create_multi
def create(self, vals_list): def create(self, vals_list):
@ -46,5 +46,6 @@ class SaleOrderBatchProduct(models.Model):
packaging = ( packaging = (
self.env["product.product"].search([("id", "=", vals.get("product_id"))])._get_nfu_packaging() self.env["product.product"].search([("id", "=", vals.get("product_id"))])._get_nfu_packaging()
) )
vals["product_packaging_id"] = packaging.id if packaging:
vals["product_packaging_id"] = packaging.id
return super().create(vals_list) return super().create(vals_list)

View file

@ -1,6 +1,5 @@
from odoo import _, api, fields, models from odoo import _, api, fields, models
from odoo.exceptions import UserError from odoo.exceptions import UserError
from odoo.tools import float_round
class SaleOrderLine(models.Model): class SaleOrderLine(models.Model):
@ -8,8 +7,8 @@ class SaleOrderLine(models.Model):
product_uom_ordered_qty = fields.Float(string="Ordered Qty", digits="Product Unit of Measure", default=1.0) product_uom_ordered_qty = fields.Float(string="Ordered Qty", digits="Product Unit of Measure", default=1.0)
product_uom_max_qty = fields.Float(string="Max Qty", digits="Product Unit of Measure") product_uom_max_qty = fields.Float(string="Max Qty", digits="Product Unit of Measure")
batch_uom_qty = fields.Float(compute="_compute_batch_uom_qty") # batch_uom_qty = fields.Float(compute="_compute_batch_uom_qty")
batch_uom_max_qty = fields.Float(compute="_compute_batch_uom_qty") # batch_uom_max_qty = fields.Float(compute="_compute_batch_uom_qty")
@api.constrains("product_uom_qty", "product_uom_max_qty") @api.constrains("product_uom_qty", "product_uom_max_qty")
def _check_product_uom_qty(self): def _check_product_uom_qty(self):
@ -22,29 +21,29 @@ 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.batch_product_id: # if line.batch_product_id:
line.batch_uom_qty = line.batch_product_id.product_uom_qty # line.batch_uom_qty = line.batch_product_id.product_uom_qty
line.batch_uom_max_qty = line.batch_product_id.product_uom_max_qty # line.batch_uom_max_qty = line.batch_product_id.product_uom_max_qty
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 not line.product_packaging_id: # if not line.product_packaging_id:
line.product_packaging_qty = False # line.product_packaging_qty = False
elif line.batch_id: # 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(
batch_uom_qty / line.product_packaging_id.qty, precision_rounding=packaging_uom.rounding # batch_uom_qty / line.product_packaging_id.qty, precision_rounding=packaging_uom.rounding
) # )
else: # else:
super()._compute_product_packaging_qty() # super()._compute_product_packaging_qty()
return True # return True
@api.model_create_multi @api.model_create_multi
def create(self, vals_list): def create(self, vals_list):

View file

@ -18,10 +18,10 @@
<field name="product_uom_max_qty"/> <field name="product_uom_max_qty"/>
</xpath> </xpath>
<xpath expr="//field[@name='product_ids']/tree" position="inside"> <xpath expr="//field[@name='product_ids']/tree" position="inside">
<field name="open_packaging_qty" invisiable="True"/> <field name="open_packaging_qty" invisiable="1"/>
</xpath> </xpath>
<xpath expr="//field[@name='product_ids']/tree/field[@name='product_uom_qty']" position="after"> <xpath expr="//field[@name='product_ids']/tree/field[@name='product_uom_qty']" position="after">
<field name="product_uom_max_qty" invisiable="True"/> <field name="product_uom_max_qty"/>
</xpath> </xpath>
<xpath <xpath
expr="//field[@name='product_ids']/tree/field[@name='product_template_id']" expr="//field[@name='product_ids']/tree/field[@name='product_template_id']"

View file

@ -10,7 +10,6 @@
</xpath> </xpath>
<xpath expr="//field[@name='order_line']/tree/field[@name='product_uom_qty']" position="after"> <xpath expr="//field[@name='order_line']/tree/field[@name='product_uom_qty']" position="after">
<field name="product_uom_max_qty"/> <field name="product_uom_max_qty"/>
<field name="batch_uom_qty"/>
</xpath> </xpath>
</field> </field>
</record> </record>