From 59d3992ad4ba2f6dc88979fb59a2c6b2020c9d70 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20-=20Le=20Filament?= <remi@le-filament.com> Date: Wed, 17 Nov 2021 23:22:09 +0100 Subject: [PATCH] [FIX] group intervention_type on sol --- models/sale_intervention.py | 66 ++++++++++++++++++++++--------------- 1 file changed, 39 insertions(+), 27 deletions(-) diff --git a/models/sale_intervention.py b/models/sale_intervention.py index 393fbae..9443152 100644 --- a/models/sale_intervention.py +++ b/models/sale_intervention.py @@ -939,36 +939,48 @@ class SaleIntervention(models.Model): # Business methods # ------------------------------------------------------ def _update_order_lines(self): - product_tmpl = self.intervention_type_id - quantity = ( - self.intervention_length if product_tmpl.uom_name == "m" else self.plant_qty + # group by intervention_type for current project + lines = self.read_group( + (("project_id", "=", self.project_id.id),), + ("price", "intervention_length", "plant_qty"), + "intervention_type_id", + lazy=False, ) - if quantity != 0: - data = { - "price_unit": self.price / quantity, - "product_uom_qty": quantity, - } - existing_lines = self.sale_order_id.order_line.filtered( - lambda r: r.product_id.id == product_tmpl.product_variant_id.id - and r.sale_project_id.id == self.project_id.id + for line in lines: + product_tmpl_id = line["intervention_type_id"][0] + product_tmpl = self.env["product.template"].browse(product_tmpl_id) + quantity = ( + line["intervention_length"] + if product_tmpl.uom_name == "m" + else line["plant_qty"] ) - if existing_lines: - # Update these lines - existing_lines.write(data) - else: - # Add missing fields and create new lines - data.update( - { - "order_id": self.sale_order_id.id, - "name": "Projet " - + self.project_id.name - + " - " - + product_tmpl.name, - "product_id": product_tmpl.product_variant_id.id, - "sale_project_id": self.project_id.id, - } + data = {} + if quantity != 0: + data = { + "price_unit": line["price"] / quantity, + "product_uom_qty": quantity, + } + existing_lines = self.sale_order_id.order_line.filtered( + lambda r: r.product_id.id == product_tmpl.product_variant_id.id + and r.sale_project_id.id == self.project_id.id ) - self.env["sale.order.line"].create(data) + if existing_lines: + # Update these lines + existing_lines.write(data) + else: + # Add missing fields and create new lines + data.update( + { + "order_id": self.sale_order_id.id, + "name": "Projet " + + self.project_id.name + + " - " + + product_tmpl.name, + "product_id": product_tmpl.product_variant_id.id, + "sale_project_id": self.project_id.id, + } + ) + self.env["sale.order.line"].create(data) class SaleInterventionStock(models.Model): -- GitLab