[WIP] sale_order_batch: restructure batch product

This commit is contained in:
NIels Göttsch 2025-03-04 22:25:11 +01:00 committed by Niels Göttsch
parent 77b74b8fea
commit 9bab583996
6 changed files with 30 additions and 43 deletions

View file

@ -1,4 +1,3 @@
from . import product_product
from . import sale_order from . import sale_order
from . import sale_order_batch from . import sale_order_batch
from . import sale_order_batch_product from . import sale_order_batch_product

View file

@ -1,7 +0,0 @@
from odoo import fields, models
class ProductProduct(models.Model):
_inherit = "product.product"
batch_ids = fields.Many2many("sale.order.batch", "product_ids")

View file

@ -11,14 +11,15 @@ class SaleOrder(models.Model):
""" """
TODO: Add Wizard to choose between batches if more than one existis. Picking the first one for now. TODO: Add Wizard to choose between batches if more than one existis. Picking the first one for now.
""" """
self = self.with_company(self.company_id)
batch = self.env["sale.order.batch"].search(
[("state", "=", "open"), ("company_id", "=", self.company_id.id)], limit=1
)
if not batch:
batch = self.env["sale.order.batch"].create({})
for sale_order in self: for sale_order in self:
company = sale_order.company_id
sale_order = sale_order.with_company(company)
batch = self.env["sale.order.batch"].search(
[("state", "=", "open"), ("company_id", "=", company.id)], limit=1
)
if not sale_order.batch_id and sale_order.state in ["draft", "sent"]: if not sale_order.batch_id and sale_order.state in ["draft", "sent"]:
if not batch:
batch = sale_order.env["sale.order.batch"].create({})
sale_order.batch_id = batch sale_order.batch_id = batch
def action_view_sale_order_batch(self): def action_view_sale_order_batch(self):
@ -29,7 +30,6 @@ class SaleOrder(models.Model):
"view_mode": "form", "view_mode": "form",
"res_model": "sale.order.batch", "res_model": "sale.order.batch",
"res_id": self.batch_id.id, "res_id": self.batch_id.id,
# 'target': '',
} }
def action_confirm(self): def action_confirm(self):
@ -41,9 +41,3 @@ class SaleOrder(models.Model):
if invalid_orders: if invalid_orders:
raise UserError(_(f"Sale Order belongs to a Batch: {', '.join(invalid_orders)}")) raise UserError(_(f"Sale Order belongs to a Batch: {', '.join(invalid_orders)}"))
return super().action_confirm() return super().action_confirm()
def write(self, vals):
res = super().write(vals)
if vals.get("batch_id"):
self.order_line._update_batch_product()
return res

View file

@ -46,7 +46,7 @@ class SaleOrderBatch(models.Model):
) )
sale_order_ids = fields.One2many("sale.order", "batch_id") sale_order_ids = fields.One2many("sale.order", "batch_id")
sale_order_count = fields.Integer(compute="_compute_sale_order_count") sale_order_count = fields.Integer(compute="_compute_sale_order_count")
sale_order_line_ids = fields.Many2many("sale.order.line", compute="_compute_sale_order_line_ids", store=True) sale_order_line_ids = fields.One2many("sale.order.line", "batch_id")
invoice_ids = fields.Many2many("account.move", compute="_compute_invoice_ids") invoice_ids = fields.Many2many("account.move", compute="_compute_invoice_ids")
invoice_count = fields.Integer(compute="_compute_invoice_ids") invoice_count = fields.Integer(compute="_compute_invoice_ids")
amount_total = fields.Float(compute="_compute_amount_total", string="Total") amount_total = fields.Float(compute="_compute_amount_total", string="Total")
@ -54,12 +54,6 @@ class SaleOrderBatch(models.Model):
product_count = fields.Integer(compute="_compute_product_count") product_count = fields.Integer(compute="_compute_product_count")
partner_credit_warning = fields.Text(compute="_compute_partner_credit_warning") partner_credit_warning = fields.Text(compute="_compute_partner_credit_warning")
@api.depends("sale_order_ids.order_line")
def _compute_sale_order_line_ids(self):
for batch in self:
order_lines = self.env["sale.order.line"].search([("order_id", "in", batch.sale_order_ids.ids)])
batch.sale_order_line_ids = order_lines
@api.depends("sale_order_ids") @api.depends("sale_order_ids")
def _compute_sale_order_count(self): def _compute_sale_order_count(self):
for batch in self: for batch in self:

View file

@ -17,26 +17,24 @@ class SaleOrderBatchProduct(models.Model):
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"
) )
sale_order_line_ids = fields.Many2many( sale_order_line_ids = fields.One2many("sale.order.line", "batch_product_id")
"sale.order.line", compute="_compute_sale_order_line_ids", store=True, readonly=False
)
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("product.packaging")
product_packaging_qty = fields.Float(compute="_compute_product_packaging_qty") product_packaging_qty = fields.Float(compute="_compute_product_packaging_qty")
@api.depends("batch_id.sale_order_line_ids") @api.depends("sale_order_line_ids")
def _compute_sale_order_line_ids(self): def _update_batch_products(self):
for product in self: for product in self:
product.sale_order_line_ids = product.batch_id.sale_order_line_ids.filtered( if not product.sale_order_line_ids:
lambda o: o.product_id == product.product_id product.unlink()
)
@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):

View file

@ -5,19 +5,28 @@ 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")
# TODO:
# - recompute values on batchproduct
# - check for last item on unlink or removal ob batch_id and remove batch_product_id
@api.depends("batch_id", "batch_product_id")
def _update_batch_product(self): def _update_batch_product(self):
for order_line in self: for line in self:
batch_id = order_line.order_id.batch_id batch_id = line.batch_id
if batch_id: if batch_id:
registered_products = batch_id.mapped("product_ids").mapped("product_id") batch_product = line.env["sale.order.batch.product"].search(
if order_line.product_id not in registered_products: [("product_id", "=", line.product_id.id), ("batch_id", "=", batch_id.id)]
self.env["sale.order.batch.product"].create( )
{"batch_id": batch_id.id, "product_id": order_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}
) )
@api.model_create_multi @api.model_create_multi
def create(self, vals_list): def create(self, vals_list):
res = super().create(vals_list) res = super().create(vals_list)
for line in res:
if line.batch_id:
res._update_batch_product() res._update_batch_product()
return res return res