From 07b8796e5864705e06c82dbec14188531526d9d6 Mon Sep 17 00:00:00 2001
From: PicchiSeba <picchiseba+git@gmail.com>
Date: Thu, 11 Jan 2024 09:47:45 +0100
Subject: [PATCH] [IMP] l10n_it_delivery_note: add SO number and customer
 reference to DN report

---
 l10n_it_delivery_note/README.rst              |  9 +++-
 l10n_it_delivery_note/models/__init__.py      |  1 +
 l10n_it_delivery_note/models/res_company.py   | 14 +++++
 .../models/res_config_settings.py             | 11 ++++
 .../models/stock_delivery_note.py             | 42 +++++++++++++++
 l10n_it_delivery_note/readme/CONFIGURE.rst    |  2 +
 l10n_it_delivery_note/readme/CONTRIBUTORS.rst |  5 +-
 .../report/report_delivery_note.xml           | 12 +++++
 .../static/description/index.html             | 51 ++++++++++---------
 .../views/res_config_settings.xml             | 32 ++++++++++++
 .../views/stock_delivery_note.xml             | 34 +++++++++++++
 11 files changed, 186 insertions(+), 27 deletions(-)
 create mode 100644 l10n_it_delivery_note/models/res_company.py

diff --git a/l10n_it_delivery_note/README.rst b/l10n_it_delivery_note/README.rst
index d607eedd283d..585c9d5e9765 100644
--- a/l10n_it_delivery_note/README.rst
+++ b/l10n_it_delivery_note/README.rst
@@ -7,7 +7,7 @@ ITA - Documento di trasporto
    !! This file is generated by oca-gen-addon-readme !!
    !! changes will be overwritten.                   !!
    !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
-   !! source digest: sha256:86cc264b131150d0e14a593b955879a5dfe4ecfe7db4069f8ede4f8a416c9e20
+   !! source digest: sha256:7e84ab9e7d8b95fa408369675f7b6f005af32f694db0a5699a9f793ea45eb8f3
    !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
 
 .. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
@@ -73,6 +73,8 @@ To configure this module, go to:
 
    Checking 'Use Advanced DN Features' allows you to manage more picking on one delivery note.
 
+   Checking 'Display Ref. Order in Delivery Note Report' or 'Display Ref. Customer in Delivery Note Report" enables in report fields relating DN line to SO (if applicable).
+
 2. *Inventory → Configuration → Warehouse Management → Delivery Note Types*
 
    In delivery note type you can specify if the product price have to be printed in the delivery note report/slip.
@@ -190,9 +192,12 @@ Contributors
 * Andrea Piovesana <andrea.m.piovesana@gmail.com>
 * Alex Comba <alex.comba@agilebg.com>
 * `Ooops <https://www.ooops404.com>`_:
-
    * Giovanni Serra <giovanni@gslab.it>
+   * Foresti Francesco <francesco.foresti@ooops404.com>
 * Nextev Srl <odoo@nextev.it>
+* `PyTech-SRL <info@pytech.it>`_:
+   * Alessandro Uffreduzzi <alessandro.uffreduzzi@pytech.it>
+   * Sebastiano Picchi <sebastiano.picchi@pytech.it>
 
 Maintainers
 ~~~~~~~~~~~
diff --git a/l10n_it_delivery_note/models/__init__.py b/l10n_it_delivery_note/models/__init__.py
index f5b6a5cd2593..96fc59a422fc 100644
--- a/l10n_it_delivery_note/models/__init__.py
+++ b/l10n_it_delivery_note/models/__init__.py
@@ -1,4 +1,5 @@
 from . import account_invoice
+from . import res_company
 from . import res_config_settings
 from . import res_partner
 from . import sale_order
diff --git a/l10n_it_delivery_note/models/res_company.py b/l10n_it_delivery_note/models/res_company.py
new file mode 100644
index 000000000000..3418b3064557
--- /dev/null
+++ b/l10n_it_delivery_note/models/res_company.py
@@ -0,0 +1,14 @@
+from odoo import fields, models
+
+
+class ResCompany(models.Model):
+    _inherit = "res.company"
+
+    display_ref_order_dn_report = fields.Boolean(
+        "Display Ref. Order in Delivery Note Report",
+        default=False,
+    )
+    display_ref_customer_dn_report = fields.Boolean(
+        "Display Ref. Customer in Delivery Note Report",
+        default=False,
+    )
diff --git a/l10n_it_delivery_note/models/res_config_settings.py b/l10n_it_delivery_note/models/res_config_settings.py
index 61b3fbe6971d..959b0c451650 100644
--- a/l10n_it_delivery_note/models/res_config_settings.py
+++ b/l10n_it_delivery_note/models/res_config_settings.py
@@ -23,3 +23,14 @@ def _default_virtual_locations_root(self):
         default=_default_virtual_locations_root,
         config_parameter="stock.location.virtual_root",
     )
+
+    display_ref_order_dn_report = fields.Boolean(
+        string="Display Ref. Order in Delivery Note Report",
+        related="company_id.display_ref_order_dn_report",
+        readonly=False,
+    )
+    display_ref_customer_dn_report = fields.Boolean(
+        string="Display Ref. Customer in Delivery Note Report",
+        related="company_id.display_ref_customer_dn_report",
+        readonly=False,
+    )
diff --git a/l10n_it_delivery_note/models/stock_delivery_note.py b/l10n_it_delivery_note/models/stock_delivery_note.py
index b99dccecf0b2..5b5fdc57395d 100644
--- a/l10n_it_delivery_note/models/stock_delivery_note.py
+++ b/l10n_it_delivery_note/models/stock_delivery_note.py
@@ -244,6 +244,12 @@ def _domain_weight_uom(self):
         store=True,
         copy=False,
     )
+    lines_have_so_number = fields.Boolean(
+        compute="_compute_lines_have_so_number",
+    )
+    lines_have_customer_ref = fields.Boolean(
+        compute="_compute_lines_have_customer_ref",
+    )
 
     picking_ids = fields.One2many(
         "stock.picking",
@@ -412,6 +418,20 @@ def _compute_access_url(self):
         for dn in self:
             dn.access_url = "/my/delivery-notes/%s" % (dn.id)
 
+    def _compute_lines_have_so_number(self):
+        for sdn in self:
+            sdn.lines_have_so_number = (
+                sdn.company_id.display_ref_order_dn_report
+                and any(line.sale_order_number for line in sdn.line_ids)
+            )
+
+    def _compute_lines_have_customer_ref(self):
+        for sdn in self:
+            sdn.lines_have_customer_ref = (
+                sdn.company_id.display_ref_customer_dn_report
+                and any(line.sale_order_client_ref for line in sdn.line_ids)
+            )
+
     @api.onchange("picking_type")
     def _onchange_picking_type(self):
         if self.picking_type:
@@ -847,6 +867,16 @@ def _default_unit_uom(self):
     sale_line_id = fields.Many2one(
         "sale.order.line", related="move_id.sale_line_id", store=True, copy=False
     )
+    sale_order_number = fields.Char(
+        "Sale Order Number",
+        compute="_compute_sale_order_number",
+        store=True,
+    )
+    sale_order_client_ref = fields.Char(
+        "Customer Reference",
+        compute="_compute_sale_order_client_ref",
+        store=True,
+    )
     invoice_status = fields.Selection(
         INVOICE_STATUSES,
         string="Invoice status",
@@ -868,6 +898,18 @@ def _default_unit_uom(self):
     def is_invoiceable(self):
         return self.invoice_status == DOMAIN_INVOICE_STATUSES[1]
 
+    @api.depends("sale_line_id.order_id.name")
+    def _compute_sale_order_number(self):
+        for sdnl in self:
+            sdnl.sale_order_number = sdnl.sale_line_id.order_id.name or ""
+
+    @api.depends("sale_line_id.order_id.client_order_ref")
+    def _compute_sale_order_client_ref(self):
+        for sdnl in self:
+            sdnl.sale_order_client_ref = (
+                sdnl.sale_line_id.order_id.client_order_ref or ""
+            )
+
     @api.onchange("product_id")
     def _onchange_product_id(self):
         if self.product_id:
diff --git a/l10n_it_delivery_note/readme/CONFIGURE.rst b/l10n_it_delivery_note/readme/CONFIGURE.rst
index b5a314532cde..3ce312185b99 100644
--- a/l10n_it_delivery_note/readme/CONFIGURE.rst
+++ b/l10n_it_delivery_note/readme/CONFIGURE.rst
@@ -4,6 +4,8 @@ To configure this module, go to:
 
    Checking 'Use Advanced DN Features' allows you to manage more picking on one delivery note.
 
+   Checking 'Display Ref. Order in Delivery Note Report' or 'Display Ref. Customer in Delivery Note Report" enables in report fields relating DN line to SO (if applicable).
+
 2. *Inventory → Configuration → Warehouse Management → Delivery Note Types*
 
    In delivery note type you can specify if the product price have to be printed in the delivery note report/slip.
diff --git a/l10n_it_delivery_note/readme/CONTRIBUTORS.rst b/l10n_it_delivery_note/readme/CONTRIBUTORS.rst
index de505deefa55..6447f31e0d37 100644
--- a/l10n_it_delivery_note/readme/CONTRIBUTORS.rst
+++ b/l10n_it_delivery_note/readme/CONTRIBUTORS.rst
@@ -8,6 +8,9 @@
 * Andrea Piovesana <andrea.m.piovesana@gmail.com>
 * Alex Comba <alex.comba@agilebg.com>
 * `Ooops <https://www.ooops404.com>`_:
-
    * Giovanni Serra <giovanni@gslab.it>
+   * Foresti Francesco <francesco.foresti@ooops404.com>
 * Nextev Srl <odoo@nextev.it>
+* `PyTech-SRL <info@pytech.it>`_:
+   * Alessandro Uffreduzzi <alessandro.uffreduzzi@pytech.it>
+   * Sebastiano Picchi <sebastiano.picchi@pytech.it>
diff --git a/l10n_it_delivery_note/report/report_delivery_note.xml b/l10n_it_delivery_note/report/report_delivery_note.xml
index fda0b9babc90..2e13d7f07bd4 100644
--- a/l10n_it_delivery_note/report/report_delivery_note.xml
+++ b/l10n_it_delivery_note/report/report_delivery_note.xml
@@ -189,6 +189,12 @@
                             <th t-if="doc.print_prices" class="text-right">
                                 Taxes
                             </th>
+                            <th t-if="doc.lines_have_so_number">
+                                Ref. Order
+                            </th>
+                            <th t-if="doc.lines_have_customer_ref">
+                                Ref. Customer
+                            </th>
                         </tr>
                     </thead>
 
@@ -234,6 +240,12 @@
                                             t-esc="', '.join(map(lambda t: (t.description or t.name), line.tax_ids))"
                                         />
                                     </td>
+                                    <td t-if="doc.lines_have_so_number">
+                                        <span t-esc="line.sale_order_number" />
+                                    </td>
+                                    <td t-if="doc.lines_have_customer_ref">
+                                        <span t-esc="line.sale_order_client_ref" />
+                                    </td>
                                 </t>
                                 <t t-else="">
                                     <td colspan="6">
diff --git a/l10n_it_delivery_note/static/description/index.html b/l10n_it_delivery_note/static/description/index.html
index 060cae4a5b50..95d049f18cca 100644
--- a/l10n_it_delivery_note/static/description/index.html
+++ b/l10n_it_delivery_note/static/description/index.html
@@ -1,3 +1,4 @@
+<?xml version="1.0" encoding="utf-8"?>
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
 <head>
@@ -366,7 +367,7 @@ <h1 class="title">ITA - Documento di trasporto</h1>
 !! This file is generated by oca-gen-addon-readme !!
 !! changes will be overwritten.                   !!
 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
-!! source digest: sha256:86cc264b131150d0e14a593b955879a5dfe4ecfe7db4069f8ede4f8a416c9e20
+!! source digest: sha256:7e84ab9e7d8b95fa408369675f7b6f005af32f694db0a5699a9f793ea45eb8f3
 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->
 <p><a class="reference external image-reference" href="https://odoo-community.org/page/development-status"><img alt="Beta" src="https://img.shields.io/badge/maturity-Beta-yellow.png" /></a> <a class="reference external image-reference" href="http://www.gnu.org/licenses/agpl-3.0-standalone.html"><img alt="License: AGPL-3" src="https://img.shields.io/badge/licence-AGPL--3-blue.png" /></a> <a class="reference external image-reference" href="https://github.com/OCA/l10n-italy/tree/14.0/l10n_it_delivery_note"><img alt="OCA/l10n-italy" src="https://img.shields.io/badge/github-OCA%2Fl10n--italy-lightgray.png?logo=github" /></a> <a class="reference external image-reference" href="https://translation.odoo-community.org/projects/l10n-italy-14-0/l10n-italy-14-0-l10n_it_delivery_note"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external image-reference" href="https://runboat.odoo-community.org/builds?repo=OCA/l10n-italy&amp;target_branch=14.0"><img alt="Try me on Runboat" src="https://img.shields.io/badge/runboat-Try%20me-875A7B.png" /></a></p>
 <p><strong>English</strong></p>
@@ -415,6 +416,7 @@ <h1><a class="toc-backref" href="#toc-entry-1">Configuration</a></h1>
 <ol class="arabic">
 <li><p class="first"><em>Inventory → Configuration → Settings - Delivery Notes</em></p>
 <p>Checking ‘Use Advanced DN Features’ allows you to manage more picking on one delivery note.</p>
+<p>Checking ‘Display Ref. Order in Delivery Note Report’ or ‘Display Ref. Customer in Delivery Note Report” enables in report fields relating DN line to SO (if applicable).</p>
 </li>
 <li><p class="first"><em>Inventory → Configuration → Warehouse Management → Delivery Note Types</em></p>
 <p>In delivery note type you can specify if the product price have to be printed in the delivery note report/slip.</p>
@@ -507,33 +509,34 @@ <h2><a class="toc-backref" href="#toc-entry-9">Authors</a></h2>
 </div>
 <div class="section" id="contributors">
 <h2><a class="toc-backref" href="#toc-entry-10">Contributors</a></h2>
-<ul>
-<li><p class="first">Riccardo Bellanova &lt;<a class="reference external" href="mailto:r.bellanova&#64;apuliasoftware.it">r.bellanova&#64;apuliasoftware.it</a>&gt;</p>
-</li>
-<li><p class="first">Matteo Bilotta &lt;<a class="reference external" href="mailto:mbilotta&#64;linkeurope.it">mbilotta&#64;linkeurope.it</a>&gt;</p>
-</li>
-<li><p class="first">Giuseppe Borruso &lt;<a class="reference external" href="mailto:gconte&#64;dinamicheaziendali.it">gconte&#64;dinamicheaziendali.it</a>&gt;</p>
-</li>
-<li><p class="first">Marco Calcagni &lt;<a class="reference external" href="mailto:mcalcagni&#64;dinamicheaziendali.it">mcalcagni&#64;dinamicheaziendali.it</a>&gt;</p>
-</li>
-<li><p class="first">Marco Colombo &lt;<a class="reference external" href="mailto:marco.colombo&#64;gmail.com">marco.colombo&#64;gmail.com</a>&gt;</p>
-</li>
-<li><p class="first">Gianmarco Conte &lt;<a class="reference external" href="mailto:gconte&#64;dinamicheaziendali.it">gconte&#64;dinamicheaziendali.it</a>&gt;</p>
-</li>
-<li><p class="first">Letizia Freda &lt;<a class="reference external" href="mailto:letizia.freda&#64;netfarm.it">letizia.freda&#64;netfarm.it</a>&gt;</p>
-</li>
-<li><p class="first">Andrea Piovesana &lt;<a class="reference external" href="mailto:andrea.m.piovesana&#64;gmail.com">andrea.m.piovesana&#64;gmail.com</a>&gt;</p>
-</li>
-<li><p class="first">Alex Comba &lt;<a class="reference external" href="mailto:alex.comba&#64;agilebg.com">alex.comba&#64;agilebg.com</a>&gt;</p>
-</li>
-<li><p class="first"><a class="reference external" href="https://www.ooops404.com">Ooops</a>:</p>
-<blockquote>
 <ul class="simple">
+<li>Riccardo Bellanova &lt;<a class="reference external" href="mailto:r.bellanova&#64;apuliasoftware.it">r.bellanova&#64;apuliasoftware.it</a>&gt;</li>
+<li>Matteo Bilotta &lt;<a class="reference external" href="mailto:mbilotta&#64;linkeurope.it">mbilotta&#64;linkeurope.it</a>&gt;</li>
+<li>Giuseppe Borruso &lt;<a class="reference external" href="mailto:gconte&#64;dinamicheaziendali.it">gconte&#64;dinamicheaziendali.it</a>&gt;</li>
+<li>Marco Calcagni &lt;<a class="reference external" href="mailto:mcalcagni&#64;dinamicheaziendali.it">mcalcagni&#64;dinamicheaziendali.it</a>&gt;</li>
+<li>Marco Colombo &lt;<a class="reference external" href="mailto:marco.colombo&#64;gmail.com">marco.colombo&#64;gmail.com</a>&gt;</li>
+<li>Gianmarco Conte &lt;<a class="reference external" href="mailto:gconte&#64;dinamicheaziendali.it">gconte&#64;dinamicheaziendali.it</a>&gt;</li>
+<li>Letizia Freda &lt;<a class="reference external" href="mailto:letizia.freda&#64;netfarm.it">letizia.freda&#64;netfarm.it</a>&gt;</li>
+<li>Andrea Piovesana &lt;<a class="reference external" href="mailto:andrea.m.piovesana&#64;gmail.com">andrea.m.piovesana&#64;gmail.com</a>&gt;</li>
+<li>Alex Comba &lt;<a class="reference external" href="mailto:alex.comba&#64;agilebg.com">alex.comba&#64;agilebg.com</a>&gt;</li>
+<li><dl class="first docutils">
+<dt><a class="reference external" href="https://www.ooops404.com">Ooops</a>:</dt>
+<dd><ul class="first last">
 <li>Giovanni Serra &lt;<a class="reference external" href="mailto:giovanni&#64;gslab.it">giovanni&#64;gslab.it</a>&gt;</li>
+<li>Foresti Francesco &lt;<a class="reference external" href="mailto:francesco.foresti&#64;ooops404.com">francesco.foresti&#64;ooops404.com</a>&gt;</li>
 </ul>
-</blockquote>
+</dd>
+</dl>
 </li>
-<li><p class="first">Nextev Srl &lt;<a class="reference external" href="mailto:odoo&#64;nextev.it">odoo&#64;nextev.it</a>&gt;</p>
+<li>Nextev Srl &lt;<a class="reference external" href="mailto:odoo&#64;nextev.it">odoo&#64;nextev.it</a>&gt;</li>
+<li><dl class="first docutils">
+<dt><a class="reference external" href="mailto:info&#64;pytech.it">PyTech-SRL</a>:</dt>
+<dd><ul class="first last">
+<li>Alessandro Uffreduzzi &lt;<a class="reference external" href="mailto:alessandro.uffreduzzi&#64;pytech.it">alessandro.uffreduzzi&#64;pytech.it</a>&gt;</li>
+<li>Sebastiano Picchi &lt;<a class="reference external" href="mailto:sebastiano.picchi&#64;pytech.it">sebastiano.picchi&#64;pytech.it</a>&gt;</li>
+</ul>
+</dd>
+</dl>
 </li>
 </ul>
 </div>
diff --git a/l10n_it_delivery_note/views/res_config_settings.xml b/l10n_it_delivery_note/views/res_config_settings.xml
index cb6bd5d3fc70..96ae9e755ab1 100644
--- a/l10n_it_delivery_note/views/res_config_settings.xml
+++ b/l10n_it_delivery_note/views/res_config_settings.xml
@@ -42,6 +42,38 @@
                             <field name="virtual_locations_root" />
                         </div>
                     </div>
+                    <div
+                        class="col-12 col-lg-6 o_setting_box"
+                        attrs="{'invisible': [('group_use_advanced_delivery_notes', '=', False)]}"
+                    >
+                        <div class="o_setting_left_pane">
+                            <field name="display_ref_order_dn_report" />
+                        </div>
+                        <div class="o_setting_right_pane">
+                            <label for="display_ref_order_dn_report" />
+                            <div class="text-muted">
+                                <p>
+                                    Prints Sale Order numbers on DN report (if there are any)
+                                </p>
+                            </div>
+                        </div>
+                    </div>
+                    <div
+                        class="col-12 col-lg-6 o_setting_box"
+                        attrs="{'invisible': [('group_use_advanced_delivery_notes', '=', False)]}"
+                    >
+                        <div class="o_setting_left_pane">
+                            <field name="display_ref_customer_dn_report" />
+                        </div>
+                        <div class="o_setting_right_pane">
+                            <label for="display_ref_customer_dn_report" />
+                            <div class="text-muted">
+                                <p>
+                                    Prints Customer References on DN report (if there are any)
+                                </p>
+                            </div>
+                        </div>
+                    </div>
                 </div>
             </xpath>
         </field>
diff --git a/l10n_it_delivery_note/views/stock_delivery_note.xml b/l10n_it_delivery_note/views/stock_delivery_note.xml
index af27e90b7318..261bbd79927f 100644
--- a/l10n_it_delivery_note/views/stock_delivery_note.xml
+++ b/l10n_it_delivery_note/views/stock_delivery_note.xml
@@ -277,6 +277,40 @@
                                                    'readonly': [('sale_line_id', '!=', False)]}"
                                         widget="many2many_tags"
                                     />
+                                    <field
+                                        name="sale_order_number"
+                                        attrs="
+                                        {
+                                            'column_invisible': [(
+                                                'parent.show_product_information',
+                                                '=',
+                                                False
+                                            )],
+                                            'readonly': [(
+                                                'sale_line_id',
+                                                '!=',
+                                                False
+                                            )]
+                                        }"
+                                        optional="hide"
+                                    />
+                                    <field
+                                        name="sale_order_client_ref"
+                                        attrs="
+                                        {
+                                            'column_invisible': [(
+                                                'parent.show_product_information',
+                                                '=',
+                                                False
+                                            )],
+                                            'readonly': [(
+                                                'sale_line_id',
+                                                '!=',
+                                                False
+                                            )]
+                                        }"
+                                        optional="hide"
+                                    />
                                     <field name="company_id" invisible="1" />
                                 </tree>
                                 <form>