From 574c7e2fc987ed08ea71e81c7d3fcdaaee67aa02 Mon Sep 17 00:00:00 2001
From: Julien - Le Filament <julien@le-filament.com>
Date: Thu, 7 Nov 2024 14:40:45 +0100
Subject: [PATCH] 16.0 auto price conf

---
 models/acc_operation.py | 59 +++++++++++++++++++++++++++++++++++++----
 1 file changed, 54 insertions(+), 5 deletions(-)

diff --git a/models/acc_operation.py b/models/acc_operation.py
index 576b8bd..cbea187 100644
--- a/models/acc_operation.py
+++ b/models/acc_operation.py
@@ -249,7 +249,6 @@ class AccOperation(models.Model):
                             "name": usage_point["usage_point_id"],
                         }
                     )
-
                     counter_used.append(counter_id.name)
                     self.env["acc.counter.period"].create(
                         {
@@ -258,13 +257,18 @@ class AccOperation(models.Model):
                             "acc_operation_id": self.id,
                             "start_date": usage_point_start,
                             "end_date": usage_point_end,
+                            "sale_price": self.sale_price_by_default,
                         }
                     )
 
-                    if self.check_priority_groups(counter=counter_id):
-                        message += (
-                            "Ajout du nouveau PRM au premier groupe de priorité<br/>"
-                        )
+                    self.check_sale_price_conf(
+                        counter_id=counter_id, periode_start_date=usage_point_start
+                    )
+
+                    # If delivery counter add to first priority group if exist
+                    if usage_point_prm_type == "delivery":
+                        if self.check_priority_groups(counter=counter_id):
+                            message += "Ajout du nouveau PRM au premier groupe de priorité<br/>"
 
                     message += "Fin de la création du PRM<br/>"
 
@@ -329,3 +333,48 @@ class AccOperation(models.Model):
             first_prio.add_counter(counter_id=counter)
             return True
         return False
+
+    def check_sale_price_conf(self, counter_id, periode_start_date):
+        """
+        create sale price conf on new counter
+        """
+        if counter_id.type in ["del", "del_inj"]:
+            inj_periods = self.env["acc.counter.period"].search(
+                [("acc_operation_id", "=", self.id), ("prm_type", "=", "injection")]
+            )
+
+            for inj_period in inj_periods:
+                if counter_id.type == "del_inj":
+                    price = self.sale_price_by_default
+                else:
+                    price = inj_period.sale_price
+
+                if self.use_default_sale_price and price > 0.0:
+                    self.env["acc.price.conf"].create(
+                        {
+                            "start_date": inj_period.start_date,
+                            "acc_operation_id": self.id,
+                            "acc_injection_counter_id": inj_period.acc_counter_id.id,
+                            "acc_delivery_counter_id": counter_id.id,
+                            "price": price,
+                            "type": "sale",
+                        }
+                    )
+
+        elif counter_id.type in ["inj"]:
+            del_periods = self.env["acc.counter.period"].search(
+                [("acc_operation_id", "=", self.id), ("prm_type", "=", "delivery")]
+            )
+
+            for del_period in del_periods:
+                if self.use_default_sale_price and self.sale_price_by_default > 0.0:
+                    self.env["acc.price.conf"].create(
+                        {
+                            "start_date": periode_start_date,
+                            "acc_operation_id": self.id,
+                            "acc_injection_counter_id": counter_id.id,
+                            "acc_delivery_counter_id": del_period.acc_counter_id.id,
+                            "price": self.sale_price_by_default,
+                            "type": "sale",
+                        }
+                    )
-- 
GitLab