From 029eca47dc79aab22acf3f594382e17bce076792 Mon Sep 17 00:00:00 2001 From: Carolina Fernandez Date: Mon, 8 Jul 2024 15:59:04 +0200 Subject: [PATCH] [IMP] account_invoice_margin_sale: Add post_install hook Update margin fields in invoice lines where there are values from sales order when installing module. TT49994 --- account_invoice_margin_sale/README.rst | 1 + account_invoice_margin_sale/__init__.py | 2 +- account_invoice_margin_sale/__manifest__.py | 1 + account_invoice_margin_sale/hooks.py | 39 +++++++++++++++++++ .../readme/CONTRIBUTORS.rst | 1 + .../static/description/index.html | 1 + 6 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 account_invoice_margin_sale/hooks.py diff --git a/account_invoice_margin_sale/README.rst b/account_invoice_margin_sale/README.rst index ca957d31..32e2cbdf 100644 --- a/account_invoice_margin_sale/README.rst +++ b/account_invoice_margin_sale/README.rst @@ -75,6 +75,7 @@ Contributors * Sergio Teruel * Carlos Dauden * Víctor Martínez + * Carolina Fernandez * `Open Source Integrators `__: diff --git a/account_invoice_margin_sale/__init__.py b/account_invoice_margin_sale/__init__.py index 69f7babd..21e9576d 100644 --- a/account_invoice_margin_sale/__init__.py +++ b/account_invoice_margin_sale/__init__.py @@ -1,3 +1,3 @@ # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). - +from .hooks import post_init_hook from . import models diff --git a/account_invoice_margin_sale/__manifest__.py b/account_invoice_margin_sale/__manifest__.py index e701b5d1..aac043e8 100644 --- a/account_invoice_margin_sale/__manifest__.py +++ b/account_invoice_margin_sale/__manifest__.py @@ -15,5 +15,6 @@ "application": False, "installable": True, "auto_install": True, + "post_init_hook": "post_init_hook", "depends": ["sale_margin", "account_invoice_margin"], } diff --git a/account_invoice_margin_sale/hooks.py b/account_invoice_margin_sale/hooks.py new file mode 100644 index 00000000..258297bf --- /dev/null +++ b/account_invoice_margin_sale/hooks.py @@ -0,0 +1,39 @@ +def post_init_hook(cr, registry): + # update purchase_price for invoices from sale_order + cr.execute( + """ + UPDATE account_move_line AS aml + SET + purchase_price = sol.purchase_price, + margin = sol.margin, + margin_signed = CASE + WHEN am.move_type = 'out_refund' THEN sol.margin * -1 + ELSE sol.margin + END, + margin_percent = sol.margin_percent * 100 + FROM sale_order_line sol, account_move am, sale_order_line_invoice_rel rel + WHERE am.id = aml.move_id + AND rel.order_line_id = sol.id + AND rel.invoice_line_id = aml.id; + """ + ) + # recalculate margin for invoices from sale_order + cr.execute( + """ + UPDATE account_move AS am + SET + margin = aml.sum_margin, + margin_signed = aml.sum_margin_signed, + margin_percent = aml.sum_margin / am.amount_untaxed * 100 + FROM ( + SELECT + aml.move_id, + SUM(aml.margin) AS sum_margin, + SUM(aml.margin_signed) AS sum_margin_signed + FROM account_move_line AS aml + GROUP BY aml.move_id + ) AS aml + WHERE am.id = aml.move_id + AND am.amount_untaxed > 0.0; + """ + ) diff --git a/account_invoice_margin_sale/readme/CONTRIBUTORS.rst b/account_invoice_margin_sale/readme/CONTRIBUTORS.rst index 79af01f4..ac76f9ea 100644 --- a/account_invoice_margin_sale/readme/CONTRIBUTORS.rst +++ b/account_invoice_margin_sale/readme/CONTRIBUTORS.rst @@ -3,6 +3,7 @@ * Sergio Teruel * Carlos Dauden * Víctor Martínez + * Carolina Fernandez * `Open Source Integrators `__: diff --git a/account_invoice_margin_sale/static/description/index.html b/account_invoice_margin_sale/static/description/index.html index abb6d18a..f92de031 100644 --- a/account_invoice_margin_sale/static/description/index.html +++ b/account_invoice_margin_sale/static/description/index.html @@ -420,6 +420,7 @@

Contributors

  • Sergio Teruel
  • Carlos Dauden
  • Víctor Martínez
  • +
  • Carolina Fernandez
  • Open Source Integrators: