From 516d1a45b446442b66531450c44bbcce7304bc1a Mon Sep 17 00:00:00 2001
From: chakib <chakib@le-filament.com>
Date: Wed, 24 Apr 2024 16:05:54 +0200
Subject: [PATCH] [UPD] reintroduce fields on sale and invoice

---
 __init__.py             |  1 +
 models/__init__.py      |  3 +++
 models/account.py       | 51 +++++++++++++++++++++++++++++++++++
 models/res_users.py     | 26 ++++++++++++++++++
 models/sale.py          | 60 +++++++++++++++++++++++++++++++++++++++++
 views/account_views.xml | 20 ++++++++++++++
 views/sale_views.xml    | 26 ++++++++++++++++++
 7 files changed, 187 insertions(+)
 create mode 100644 models/__init__.py
 create mode 100644 models/account.py
 create mode 100644 models/res_users.py
 create mode 100644 models/sale.py
 create mode 100644 views/account_views.xml
 create mode 100644 views/sale_views.xml

diff --git a/__init__.py b/__init__.py
index e69de29..0650744 100644
--- a/__init__.py
+++ b/__init__.py
@@ -0,0 +1 @@
+from . import models
diff --git a/models/__init__.py b/models/__init__.py
new file mode 100644
index 0000000..cba8f31
--- /dev/null
+++ b/models/__init__.py
@@ -0,0 +1,3 @@
+from . import account
+from . import res_users
+from . import sale
diff --git a/models/account.py b/models/account.py
new file mode 100644
index 0000000..9cb8e11
--- /dev/null
+++ b/models/account.py
@@ -0,0 +1,51 @@
+# Copyright 2021- Le Filament (https://le-filament.com)
+# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
+
+from odoo import fields, models
+
+
+class AccountInvoice(models.Model):
+    _inherit = "account.move"
+
+    # ------------------------------------------------------
+    # Fields declaration
+    # ------------------------------------------------------
+    user_second_ids = fields.Many2many(
+        comodel_name="res.users",
+        column1="invoice_id",
+        column2="user_id",
+        string="Spécialistes",
+        track_visibility="onchange",
+        copy=False,
+    )
+    user_provider_id = fields.Many2one(
+        "res.partner", string="Apporteur", track_visibility="onchange", copy=False
+    )
+
+    # ------------------------------------------------------
+    # SQL Constraints
+    # ------------------------------------------------------
+
+    # ------------------------------------------------------
+    # Default methods
+    # ------------------------------------------------------
+
+    # ------------------------------------------------------
+    # Computed fields / Search Fields
+    # ------------------------------------------------------
+
+    # ------------------------------------------------------
+    # Onchange / Constraints
+    # ------------------------------------------------------
+
+    # ------------------------------------------------------
+    # CRUD methods (ORM overrides)
+    # ------------------------------------------------------
+
+    # ------------------------------------------------------
+    # Actions
+    # ------------------------------------------------------
+
+    # ------------------------------------------------------
+    # Business methods
+    # ------------------------------------------------------
diff --git a/models/res_users.py b/models/res_users.py
new file mode 100644
index 0000000..e4e9a9e
--- /dev/null
+++ b/models/res_users.py
@@ -0,0 +1,26 @@
+# Copyright 2021- Le Filament (https://le-filament.com>)
+# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
+
+from odoo import fields, models
+
+
+class ResUsers(models.Model):
+    _inherit = "res.users"
+
+    # ------------------------------------------------------
+    # Fields declaration
+    # ------------------------------------------------------
+    order_second_ids = fields.Many2many(
+        comodel_name="sale.order",
+        column1="user_id",
+        column2="order_id",
+        string="Commandes",
+        copy=False,
+    )
+    invoice_second_ids = fields.Many2many(
+        comodel_name="account.move",
+        column1="user_id",
+        column2="invoice_id",
+        string="Factures",
+        copy=False,
+    )
diff --git a/models/sale.py b/models/sale.py
new file mode 100644
index 0000000..d098e48
--- /dev/null
+++ b/models/sale.py
@@ -0,0 +1,60 @@
+# Copyright 2021- Le Filament (https://le-filament.com>)
+# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
+
+from odoo import fields, models
+
+
+class SaleOrder(models.Model):
+    _inherit = "sale.order"
+
+    # ------------------------------------------------------
+    # Fields declaration
+    # ------------------------------------------------------
+    user_second_ids = fields.Many2many(
+        comodel_name="res.users",
+        column1="order_id",
+        column2="user_id",
+        string="Spécialistes",
+        track_visibility="onchange",
+        copy=False,
+    )
+    user_provider_id = fields.Many2one(
+        "res.partner", string="Apporteur", track_visibility="onchange", copy=False
+    )
+
+    # ------------------------------------------------------
+    # SQL Constraints
+    # ------------------------------------------------------
+
+    # ------------------------------------------------------
+    # Default methods
+    # ------------------------------------------------------
+
+    # ------------------------------------------------------
+    # Computed fields / Search Fields
+    # ------------------------------------------------------
+
+    # ------------------------------------------------------
+    # Onchange / Constraints
+    # ------------------------------------------------------
+
+    # ------------------------------------------------------
+    # CRUD methods (ORM overrides)
+    # ------------------------------------------------------
+
+    # ------------------------------------------------------
+    # Actions
+    # ------------------------------------------------------
+
+    # ------------------------------------------------------
+    # Business methods
+    # ------------------------------------------------------
+    def _prepare_invoice(self):
+        values = super()._prepare_invoice()
+        values.update(
+            {
+                "user_second_ids": fields.Command.create(self.user_second_ids.ids),
+                "user_provider_id": self.user_provider_id.id or False,
+            }
+        )
+        return values
diff --git a/views/account_views.xml b/views/account_views.xml
new file mode 100644
index 0000000..d8d679c
--- /dev/null
+++ b/views/account_views.xml
@@ -0,0 +1,20 @@
+<?xml version="1.0" encoding="utf-8" ?>
+<!-- Copyright 2019- Le Filament (https://le-filament.com)
+     License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). -->
+<odoo>
+	<record id="account_move_3ad_form" model="ir.ui.view">
+	    <field name="name">account.3a.move.form</field>
+	    <field name="model">account.move</field>
+	    <field name="inherit_id" ref="account.view_move_form" />
+	    <field name="arch" type="xml">
+		<field name="invoice_user_id" position="after">
+		    <field
+                    name="user_second_ids"
+                    options="{'no_create_edit': 1}"
+                    widget="many2many_tags"
+                />
+		    <field name="user_provider_id" options="{'no_create_edit': 1}" />
+		</field>
+	    </field>
+	</record>
+</odoo>
diff --git a/views/sale_views.xml b/views/sale_views.xml
new file mode 100644
index 0000000..b4ba204
--- /dev/null
+++ b/views/sale_views.xml
@@ -0,0 +1,26 @@
+<?xml version="1.0" encoding="utf-8" ?>
+<!-- Copyright 2019- Le Filament (https://le-filament.com)
+     License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). -->
+<odoo>
+        <record id="view_order_form" model="ir.ui.view">
+            <field name="name">sale.3a.order.form</field>
+            <field name="model">sale.order</field>
+            <field name="inherit_id" ref="sale.view_order_form" />
+            <field name="arch" type="xml">
+                <xpath
+                expr="//notebook//page[@name='other_information']//group//group//field[@name='user_id']"
+                position="after"
+            >
+                    <field
+                    name="user_second_ids"
+                    options="{'no_create_edit': 1}"
+                    widget="many2many_tags"
+                />
+                    <field
+                    name="user_provider_id"
+                    options="{'no_create_edit': 1}"
+                />
+                </xpath>
+            </field>
+        </record>
+</odoo>
-- 
GitLab