diff --git a/models/sale_intervention.py b/models/sale_intervention.py index 9443152f956fa32704942405da9a4a3e4f9817b7..b899ca3a346e88aad395a5dc637bc47e14f0f65e 100644 --- a/models/sale_intervention.py +++ b/models/sale_intervention.py @@ -757,7 +757,7 @@ class SaleIntervention(models.Model): % rec.project_subvention_id.name ) total_price += rec.price - rec._update_order_lines() + rec.project_id.update_order_lines() return total_price def _get_plants_qties(self): @@ -936,51 +936,13 @@ class SaleIntervention(models.Model): # ------------------------------------------------------ # ------------------------------------------------------ - # Business methods + # CRUD methods (ORM overrides) # ------------------------------------------------------ - def _update_order_lines(self): - # 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, - ) - 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"] - ) - 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 - ) - 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) + def unlink(self): + project_id = self.project_id + res = super().unlink() + project_id.update_order_lines() + return res class SaleInterventionStock(models.Model): diff --git a/models/sale_order.py b/models/sale_order.py index 2cc1d453a1f3365dd8e16e3c00b772dedecc2dc2..4b91826fedae9c90d22e1eabdacd8da19c8b01bb 100644 --- a/models/sale_order.py +++ b/models/sale_order.py @@ -22,3 +22,17 @@ class SaleOrder(models.Model): inverse_name="sale_order_id", string="Articles d'interventions'", ) + + # ------------------------------------------------------ + # Fields declaration + # ------------------------------------------------------ + def clean_sale_order(self): + current_inter = list() + for project in self.sale_project_ids: + for inter in project.intervention_ids: + current_inter.append( + (project.id, inter.intervention_type_id.id)) + for so in self.order_line: + if so.product_id and (so.sale_project_id.id, so.product_id.id)\ + not in current_inter: + so.unlink() diff --git a/models/sale_project.py b/models/sale_project.py index 8e19983758c56c15971f08014e665853898a10b8..5c4dd0592daeb68652c0202d947600293358e672 100644 --- a/models/sale_project.py +++ b/models/sale_project.py @@ -151,7 +151,6 @@ class SaleProject(models.Model): # ------------------------------------------------------ # CRUD methods (ORM overrides) # ------------------------------------------------------ - @api.model_create_multi def create(self, vals_list): res = super().create(vals_list) @@ -206,7 +205,50 @@ class SaleProject(models.Model): # ------------------------------------------------------ # Business methods # ------------------------------------------------------ - + def update_order_lines(self): + # group by intervention_type for current project + lines = self.env['sale.intervention'].read_group( + (("project_id", "=", self.id),), + ("price", "intervention_length", "plant_qty"), + "intervention_type_id", + lazy=False, + ) + 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"] + ) + 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.id + ) + 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.name + + " - " + + product_tmpl.name, + "product_id": product_tmpl.product_variant_id.id, + "sale_project_id": self.id, + } + ) + self.env["sale.order.line"].create(data) + self.sale_order_id.clean_sale_order() class SaleFinancialHelp(models.Model): _name = "sale.financial.help"