Skip to content

Commit

Permalink
[IMP] account_invoice_margin_sale: Update purchase cost in invoice li…
Browse files Browse the repository at this point in the history
…nes from sale order when installing module
  • Loading branch information
carolinafernandez-tecnativa committed Jul 8, 2024
1 parent 012c35e commit 396845e
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 1 deletion.
1 change: 1 addition & 0 deletions account_invoice_margin_sale/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ Contributors
* Sergio Teruel
* Carlos Dauden
* Víctor Martínez
* Carolina Fernandez

* `Open Source Integrators <https://www.opensourceintegrators.com>`__:

Expand Down
2 changes: 1 addition & 1 deletion account_invoice_margin_sale/__init__.py
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions account_invoice_margin_sale/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@
"application": False,
"installable": True,
"auto_install": True,
"post_init_hook": "post_init_hook",
"depends": ["sale_margin", "account_invoice_margin"],
}
51 changes: 51 additions & 0 deletions account_invoice_margin_sale/hooks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
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 = (
SELECT COALESCE(SUM(order_line.purchase_price), 0.0)
FROM sale_order_line AS order_line
INNER JOIN sale_order AS so ON order_line.order_id = so.id
INNER JOIN sale_order_line_invoice_rel AS soi
ON soi.order_line_id = order_line.id
WHERE soi.invoice_line_id = aml.id
)
WHERE EXISTS (
SELECT 1
FROM sale_order_line AS order_line
INNER JOIN sale_order_line_invoice_rel AS soi
ON soi.order_line_id = order_line.id
WHERE soi.invoice_line_id = aml.id
);
"""
)
# recalculate margin for invoices from sale_order
cr.execute(
"""
WITH aml AS (
SELECT
account_move_line.move_id,
SUM(account_move_line.margin) AS sum_margin,
SUM(account_move_line.margin_signed) AS sum_margin_signed
FROM account_move_line
INNER JOIN account_move ON account_move.id = account_move_line.move_id
WHERE EXISTS (
SELECT 1
FROM sale_order_line AS order_line
INNER JOIN sale_order AS so ON order_line.order_id = so.id
INNER JOIN sale_order_line_invoice_rel AS soi
ON soi.order_line_id = order_line.id
WHERE soi.invoice_line_id = account_move_line.id
)
GROUP BY account_move_line.move_id
)
UPDATE account_move
SET margin = aml.sum_margin,
margin_signed = aml.sum_margin_signed,
margin_percent = aml.sum_margin_signed / amount_untaxed * 100
FROM aml
WHERE account_move.id = aml.move_id
AND account_move.amount_untaxed > 0.0;
"""
)
1 change: 1 addition & 0 deletions account_invoice_margin_sale/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* Sergio Teruel
* Carlos Dauden
* Víctor Martínez
* Carolina Fernandez

* `Open Source Integrators <https://www.opensourceintegrators.com>`__:

Expand Down
1 change: 1 addition & 0 deletions account_invoice_margin_sale/static/description/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,7 @@ <h2><a class="toc-backref" href="#toc-entry-6">Contributors</a></h2>
<li>Sergio Teruel</li>
<li>Carlos Dauden</li>
<li>Víctor Martínez</li>
<li>Carolina Fernandez</li>
</ul>
</li>
<li><a class="reference external" href="https://www.opensourceintegrators.com">Open Source Integrators</a>:<ul>
Expand Down

0 comments on commit 396845e

Please sign in to comment.