From 4725b2a3b633bf8f67a15127e7fdfee0156d871c Mon Sep 17 00:00:00 2001
From: Juliana <juliana@le-filament.com>
Date: Wed, 14 Sep 2022 18:40:29 +0200
Subject: [PATCH] [UPD]Update arborescence documents

---
 services/operation_services.py | 132 ++++++++++++---------------------
 1 file changed, 48 insertions(+), 84 deletions(-)

diff --git a/services/operation_services.py b/services/operation_services.py
index 3449ff8..f3f1e6e 100644
--- a/services/operation_services.py
+++ b/services/operation_services.py
@@ -192,11 +192,47 @@ class OperationsService(Component):
             "id": operation.id,
             "name": operation.name,
         }
-        if type == "invoice":
-            # Récupération de toutes les factures liées à l'opération spécifiée
-            acc_account_ids = self.env["acc.account"].search(
-                [("acc_operation_id", "=", _id), ("state", "=", "published")]
-            )
+
+        # Récupération de toutes les factures liées à l'opération spécifiée
+        acc_account_ids = self.env["acc.account"].search(
+            [("acc_operation_id", "=", _id), ("state", "=", "published")]
+        )
+
+        # Récupération de toutes les factures externes
+        # liées à l'opération spécifiée
+        domain = [("acc_operation_id", "=", _id)]
+        domain += [
+            ("type", "=", "facture"),
+        ]
+        acc_contract_ids_f = self.env["acc.contract"].search(domain)
+        invoice_exist = False
+        if acc_contract_ids_f or acc_account_ids:
+            invoice_exist = True
+
+        # Récupération de tous les contrats liés à l'opération spécifiée
+        domain_c = [("acc_operation_id", "=", _id)]
+        domain_pmo = [("acc_operation_id", "=", _id)]
+        role = self._get_role(operation)
+        if role.get("isConsumer") or role.get("isProductor"):
+            domain_c += [
+                ("type", "!=", "convention"),
+                ("type", "!=", "all"),
+                ("type", "!=", False),
+            ]
+        if role.get("isPmo"):
+            domain_pmo += [("type", "!=", False)]
+            acc_contract_ids = self.env["acc.contract"].sudo().search(domain_pmo)
+        else:
+            acc_contract_all = self.env["acc.contract"].sudo().search([
+                ("acc_operation_id", "=", _id), ("type", "=", "all"),
+            ])
+            acc_contract_ids = self.env["acc.contract"].search(domain_c)
+            acc_contract_ids += acc_contract_all
+        contract_exist = False
+        if acc_contract_ids:
+            contract_exist = True
+        if type == "invoice" and invoice_exist:
+
             datas["documents"] = (
                 acc_account_ids.mapped(
                     lambda n: {
@@ -213,50 +249,8 @@ class OperationsService(Component):
                 else []
             )
 
-            # Récupération de toutes les factures externes
-            # liées à l'opération spécifiée
-            domain = [("acc_operation_id", "=", _id)]
-
-            domain += [
-                ("type", "=", "facture"),
-            ]
-            acc_contract_ids = self.env["acc.contract"].search(domain)
             datas["factures_externes"] = (
-                acc_contract_ids.mapped(
-                    lambda n: {
-                        "id": n.id,
-                        "name": n.name,
-                        "start_date": n.start_date,
-                        "end_date": n.end_date,
-                        "type": n.type,
-                        "url": base_url + n.get_portal_url(),
-                    }
-                )
-                if acc_contract_ids
-                else []
-            )
-        elif type == "other":
-            # Récupération de tous les contrats liés à l'opération spécifiée
-            domain = [("acc_operation_id", "=", _id)]
-            role = self._get_role(operation)
-            # if role.get("isConsumer") or role.get("isProductor"):
-            #     domain += [
-            #         ("type", "!=", "convention"),
-            #         ("type", "!=", "all"),
-            #         ("type", "!=", False),
-            #         ]
-            # if role.get("isPmo"):
-            #     domain_pmo += [("type", "!=", False)]
-            #     acc_contract_ids = self.env["acc.contract"].sudo().search(domain_pmo)
-            # else:
-            acc_contract_ids = self.env["acc.contract"].sudo().search([
-                ("acc_operation_id", "=", _id), ("type", "=", "all"),
-            ])
-                # acc_contract_ids = self.env["acc.contract"].search(domain)
-                # acc_contract_ids += acc_contract_all
-
-            datas["other"] = (
-                acc_contract_ids.mapped(
+                acc_contract_ids_f.mapped(
                     lambda n: {
                         "id": n.id,
                         "name": n.name,
@@ -266,29 +260,10 @@ class OperationsService(Component):
                         "url": base_url + n.get_portal_url(),
                     }
                 )
-                if acc_contract_ids
+                if acc_contract_ids_f
                 else []
             )
-        else:
-            # Récupération de tous les contrats liés à l'opération spécifiée
-            domain = [("acc_operation_id", "=", _id)]
-            domain_pmo = [("acc_operation_id", "=", _id)]
-            role = self._get_role(operation)
-            if role.get("isConsumer") or role.get("isProductor"):
-                domain += [
-                    ("type", "!=", "convention"),
-                    ("type", "!=", "all"),
-                    ("type", "!=", False),
-                    ]
-            if role.get("isPmo"):
-                domain_pmo += [("type", "!=", False)]
-                acc_contract_ids = self.env["acc.contract"].sudo().search(domain_pmo)
-            else:
-                # acc_contract_all = self.env["acc.contract"].sudo().search([
-                #     ("acc_operation_id", "=", _id), ("type", "=", "all"),
-                # ])
-                acc_contract_ids = self.env["acc.contract"].search(domain)
-                # acc_contract_ids += acc_contract_all
+        elif type == "contract" or (not invoice_exist and contract_exist):
 
             datas["contracts"] = (
                 acc_contract_ids.mapped(
@@ -304,6 +279,8 @@ class OperationsService(Component):
                 if acc_contract_ids
                 else []
             )
+        datas["contract_exist"] = contract_exist
+        datas["invoice_exist"] = invoice_exist
         return datas
 
     @restapi.method(
@@ -751,6 +728,8 @@ class OperationsService(Component):
                 },
             },
             "id": {"type": "integer"},
+            "invoice_exist": {"type": "boolean"},
+            "contract_exist": {"type": "boolean"},
             "name": {"type": "string"},
             "documents": {
                 "nullable": True,
@@ -798,21 +777,6 @@ class OperationsService(Component):
                     },
                 },
             },
-            "other": {
-                "nullable": True,
-                "type": "list",
-                "schema": {
-                    "type": "dict",
-                    "schema": {
-                        "id": {"type": "integer"},
-                        "name": {"type": "string"},
-                        "type": {"type": "string", "nullable": True},
-                        "url": {"type": "string", "nullable": True},
-                        "seller": {"type": "string", "nullable": True},
-                        "buyer": {"type": "string", "nullable": True},
-                    },
-                },
-            },
         }
 
     def _validator_get_pmo(self):
-- 
GitLab