[imp] sale_order_batch: make interface more robust

This commit is contained in:
Niels Göttsch 2025-03-18 20:30:34 +01:00 committed by madmooose
parent 705881328f
commit 105e3e252f
4 changed files with 37 additions and 11 deletions

View file

@ -16,10 +16,7 @@ class SaleOrderBatchProduct(models.Model):
copy=False, copy=False,
readonly=True, readonly=True,
) )
# TODO: compute and store
product_id = fields.Many2one(comodel_name="product.product", required=True, readonly=True) product_id = fields.Many2one(comodel_name="product.product", required=True, readonly=True)
product_template_id = fields.Many2one( product_template_id = fields.Many2one(
"product.template", related="product_id.product_tmpl_id", string="Product Template" "product.template", related="product_id.product_tmpl_id", string="Product Template"
) )
@ -27,14 +24,40 @@ class SaleOrderBatchProduct(models.Model):
product_uom_category_id = fields.Many2one(related="product_id.uom_id.category_id", depends=["product_id"]) product_uom_category_id = fields.Many2one(related="product_id.uom_id.category_id", depends=["product_id"])
product_uom_qty = fields.Float(compute="_compute_uom_qty", string="Quantity") product_uom_qty = fields.Float(compute="_compute_uom_qty", string="Quantity")
product_uom = fields.Many2one(related="product_id.uom_id") product_uom = fields.Many2one(related="product_id.uom_id")
product_packaging_id = fields.Many2one("product.packaging") product_packaging_id = fields.Many2one(
comodel_name="product.packaging",
string="Packaging",
compute="_compute_product_packaging_id",
store=True,
readonly=False,
precompute=True,
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")
@api.constrains("product_id", "sale_order_line_ids")
def _check_sale_order_line_products(self):
for product in self:
if any(line.product_id != product.product_id for line in product.sale_order_line_ids):
raise ValueError("Product must be the same for all Sale Order Lines")
@api.depends("sale_order_line_ids.product_uom_qty") @api.depends("sale_order_line_ids.product_uom_qty")
def _compute_uom_qty(self): def _compute_uom_qty(self):
for product in self: for product in self:
product.product_uom_qty = sum(product.sale_order_line_ids.mapped("product_uom_qty")) product.product_uom_qty = sum(product.sale_order_line_ids.mapped("product_uom_qty"))
@api.depends("product_id", "product_uom_qty", "product_uom")
def _compute_product_packaging_id(self):
for line in self:
# remove packaging if not match the product
if line.product_packaging_id.product_id != line.product_id:
line.product_packaging_id = False
# suggest always the first packaging
if line.product_id and line.product_uom_qty and line.product_uom:
suggested_packaging = line.product_id.packaging_ids[0] if line.product_id.packaging_ids else False
line.product_packaging_id = suggested_packaging or line.product_packaging_id
@api.depends("product_packaging_id") @api.depends("product_packaging_id")
def _compute_product_packaging_qty(self): def _compute_product_packaging_qty(self):
for product in self: for product in self:

View file

@ -5,7 +5,7 @@ class SaleOrderLine(models.Model):
_inherit = "sale.order.line" _inherit = "sale.order.line"
batch_id = fields.Many2one(related="order_id.batch_id") batch_id = fields.Many2one(related="order_id.batch_id")
batch_product_id = fields.Many2one("sale.order.batch.product") batch_product_id = fields.Many2one("sale.order.batch.product", ondelete="restrict")
def _link_batch_product(self): def _link_batch_product(self):
lines_with_batch = self.filtered(lambda l: l.batch_id) lines_with_batch = self.filtered(lambda l: l.batch_id)

View file

@ -27,8 +27,10 @@
<tree editable="bottom" create="0"> <tree editable="bottom" create="0">
<field name="sequence" widget="handle"/> <field name="sequence" widget="handle"/>
<field name="order_id"/> <field name="order_id"/>
<field name="name"/>
<field name="order_partner_id"/> <field name="order_partner_id"/>
<field name="product_template_id" optional="hide"/>
<field name="product_id" optional="hide"/>
<field name="name"/>
<field name="product_uom_qty"/> <field name="product_uom_qty"/>
<field name="price_unit"/> <field name="price_unit"/>
<field name="price_subtotal"/> <field name="price_subtotal"/>

View file

@ -98,10 +98,10 @@
<notebook> <notebook>
<page string="Sale Orders" name="orders"> <page string="Sale Orders" name="orders">
<field name="sale_order_ids"> <field name="sale_order_ids">
<tree delete="0"> <tree delete="0" editable="0" create="0">
<field name="name"/> <field name="name"/>
<field name="partner_id"/> <field name="partner_id" readonly="1"/>
<field name="date_order"/> <field name="date_order" readonly="1"/>
<field name="amount_untaxed"/> <field name="amount_untaxed"/>
<field name="amount_total"/> <field name="amount_total"/>
<field name="company_id" invisible="1"/> <field name="company_id" invisible="1"/>
@ -137,8 +137,9 @@
</page> </page>
<page string="Products" name="products"> <page string="Products" name="products">
<field name="product_ids"> <field name="product_ids">
<tree create="0"> <tree create="0" delete="0">
<field name="product_template_id"/> <field name="product_template_id" optional="hide"/>
<field name="product_id"/>
<field name="product_uom_qty"/> <field name="product_uom_qty"/>
<field name="product_packaging_qty" groups="product.group_stock_packaging"/> <field name="product_packaging_qty" groups="product.group_stock_packaging"/>
<field name="product_packaging_id" groups="product.group_stock_packaging"/> <field name="product_packaging_id" groups="product.group_stock_packaging"/>