From 59d8b1e9f0f13b9ab7730f30ae0eb0d47fcac198 Mon Sep 17 00:00:00 2001
From: jordan <jordan@le-filament.com>
Date: Thu, 18 Nov 2021 12:42:11 +0100
Subject: [PATCH] [update] refactor code to put update_order_lines in project +
 add clean_sale_order function

---
 models/sale_intervention.py | 52 +++++--------------------------------
 models/sale_order.py        | 14 ++++++++++
 models/sale_project.py      | 46 ++++++++++++++++++++++++++++++--
 3 files changed, 65 insertions(+), 47 deletions(-)

diff --git a/models/sale_intervention.py b/models/sale_intervention.py
index 9443152..b899ca3 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 2cc1d45..4b91826 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 8e19983..5c4dd05 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"
-- 
GitLab