Skip to content

Commit 6e431b4

Browse files
[16.0][FIX] account_move_line_purchase_info: do not consider stock entry lines on revaluations
Up to now, stock entries were considered in the revaluation process as they were stored on field invoice_lines. To avoid that we introduced a dedicated field to manage stock entries and avoid taking them into account in the revaluation odoo core process.
1 parent a8c69ea commit 6e431b4

File tree

11 files changed

+129
-25
lines changed

11 files changed

+129
-25
lines changed

account_move_line_purchase_info/README.rst

+7
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@ This module will add the purchase order line to journal items.
3333
The ultimate goal is to establish the purchase order line as one of the key
3434
fields to reconcile the Goods Received Not Invoiced accrual account.
3535

36+
Field oca_purchase_line_id it's necessary. In Odoo >=16 automatic
37+
revaluation for a product with FIFO costing method only works if invoice
38+
lines related to a purchase order line do not include stock journal items.
39+
To avoid that oca_purchase_line_id includes invoice and stock journal items,
40+
and we keep Odoo field invoice_lines just with bill lines.
41+
- Check issue https://github.com/OCA/account-financial-tools/issues/2017
42+
3643
**Table of contents**
3744

3845
.. contents::

account_move_line_purchase_info/__manifest__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
{
66
"name": "Account Move Line Purchase Info",
77
"summary": "Introduces the purchase order line to the journal items",
8-
"version": "16.0.1.0.0",
8+
"version": "16.0.2.0.0",
99
"author": "ForgeFlow, Odoo Community Association (OCA)",
1010
"website": "https://github.com/OCA/account-financial-tools",
1111
"category": "Generic",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import logging
2+
3+
from openupgradelib import openupgrade
4+
5+
_logger = logging.getLogger(__name__)
6+
7+
8+
@openupgrade.migrate()
9+
def migrate(env, version):
10+
purchase_orders = env["purchase.order"].search([("state", "=", "purchase")])
11+
for order in purchase_orders:
12+
try:
13+
_logger.info(f"Processing Purchase Order: {order.name} (ID: {order.id})")
14+
valued_lines = order.order_line.invoice_lines.filtered(
15+
lambda l: l.product_id
16+
and l.product_id.cost_method != "standard"
17+
and (
18+
not l.company_id.tax_lock_date
19+
or l.date > l.company_id.tax_lock_date
20+
)
21+
)
22+
svls, _amls = valued_lines._apply_price_difference()
23+
24+
if svls:
25+
svls._validate_accounting_entries()
26+
27+
bills = order.invoice_ids.filtered(lambda bill: bill.state == "posted")
28+
bills._stock_account_anglo_saxon_reconcile_valuation()
29+
30+
except Exception as e:
31+
_logger.error(
32+
f"Error processing Purchase Order {order.name} (ID: {order.id}): {str(e)}",
33+
exc_info=True,
34+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from openupgradelib import openupgrade
2+
3+
4+
@openupgrade.migrate()
5+
def migrate(env, version):
6+
openupgrade.logged_query(
7+
env.cr,
8+
"""
9+
ALTER TABLE account_move_line
10+
ADD COLUMN IF NOT EXISTS oca_purchase_line_id INTEGER;
11+
""",
12+
)
13+
14+
openupgrade.logged_query(
15+
env.cr,
16+
"""
17+
UPDATE account_move_line
18+
SET oca_purchase_line_id = purchase_line_id;
19+
""",
20+
)
21+
22+
openupgrade.logged_query(
23+
env.cr,
24+
"""
25+
UPDATE account_move_line
26+
SET purchase_line_id = NULL
27+
FROM account_move
28+
WHERE account_move_line.move_id = account_move.id
29+
AND account_move.move_type = 'entry';
30+
""",
31+
)

account_move_line_purchase_info/models/account_move.py

+25-1
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,38 @@
22
# (https://www.forgeflow.com)
33
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
44

5-
from odoo import fields, models
5+
from odoo import api, fields, models
66

77

88
class AccountMoveLine(models.Model):
99
_inherit = "account.move.line"
1010

11+
# Set related None, to make it compute and avoid base related to purchase_line_id
1112
purchase_order_id = fields.Many2one(
1213
comodel_name="purchase.order",
14+
related=None,
1315
store=True,
1416
index=True,
17+
compute="_compute_purchase_id",
1518
)
19+
20+
oca_purchase_line_id = fields.Many2one(
21+
comodel_name="purchase.order.line",
22+
string="OCA Purchase Line",
23+
store=True,
24+
index=True,
25+
compute="_compute_oca_purchase_line_id",
26+
)
27+
28+
@api.depends("purchase_line_id")
29+
def _compute_oca_purchase_line_id(self):
30+
for rec in self:
31+
if rec.purchase_line_id:
32+
rec.oca_purchase_line_id = rec.purchase_line_id
33+
34+
@api.depends("purchase_line_id", "oca_purchase_line_id")
35+
def _compute_purchase_id(self):
36+
for rec in self:
37+
rec.purchase_order_id = (
38+
rec.purchase_line_id.order_id.id or rec.oca_purchase_line_id.order_id.id
39+
)

account_move_line_purchase_info/models/purchase_order.py

+4-15
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
class PurchaseOrder(models.Model):
55
_inherit = "purchase.order"
66

7-
@api.depends("order_line.invoice_lines.move_id")
7+
@api.depends("order_line.stock_invoice_lines.move_id")
88
def _compute_journal_entries(self):
99
for order in self:
10-
journal_entries = order.mapped("order_line.invoice_lines.move_id").filtered(
11-
lambda r: r.move_type == "entry"
12-
)
10+
journal_entries = order.mapped(
11+
"order_line.stock_invoice_lines.move_id"
12+
).filtered(lambda r: r.move_type == "entry")
1313
order.journal_entry_ids = journal_entries
1414
order.journal_entries_count = len(journal_entries)
1515

@@ -21,17 +21,6 @@ def _compute_journal_entries(self):
2121
string="Journal Entries",
2222
)
2323

24-
@api.depends("order_line.invoice_lines.move_id")
25-
def _compute_invoice(self):
26-
"""Overwritten compute to avoid show all Journal Entries with
27-
purchase_order_line as invoice_lines One2many would take them into account."""
28-
for order in self:
29-
invoices = order.order_line.invoice_lines.move_id.filtered(
30-
lambda m: m.is_invoice(include_receipts=True)
31-
)
32-
order.invoice_ids = invoices
33-
order.invoice_count = len(invoices)
34-
3524
def action_view_journal_entries(self, invoices=False):
3625
"""This function returns an action that display existing journal entries of
3726
given purchase order ids. When only one found, show the journal entry

account_move_line_purchase_info/models/purchase_order_line.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
# Copyright 2019-2020 ForgeFlow S.L.
22
# (https://www.forgeflow.com)
33
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
4-
from odoo import models
4+
from odoo import fields, models
55

66

77
class PurchaseOrderLine(models.Model):
88
_inherit = "purchase.order.line"
99

10+
stock_invoice_lines = fields.One2many(
11+
"account.move.line", "oca_purchase_line_id", readonly=True, copy=False
12+
)
13+
1014
def name_get(self):
1115
result = []
1216
orig_name = dict(super(PurchaseOrderLine, self).name_get())

account_move_line_purchase_info/models/stock_move.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@ def _prepare_account_move_line(
1515
qty, cost, credit_account_id, debit_account_id, svl_id, description
1616
)
1717
for line in res:
18-
line[2]["purchase_line_id"] = self.purchase_line_id.id
18+
line[2]["oca_purchase_line_id"] = self.purchase_line_id.id
1919
return res

account_move_line_purchase_info/readme/DESCRIPTION.rst

+7
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,10 @@ This module will add the purchase order line to journal items.
22

33
The ultimate goal is to establish the purchase order line as one of the key
44
fields to reconcile the Goods Received Not Invoiced accrual account.
5+
6+
Field oca_purchase_line_id it's necessary. In Odoo >=16 automatic
7+
revaluation for a product with FIFO costing method only works if invoice
8+
lines related to a purchase order line do not include stock journal items.
9+
To avoid that oca_purchase_line_id includes invoice and stock journal items,
10+
and we keep Odoo field invoice_lines just with bill lines.
11+
- Check issue https://github.com/OCA/account-financial-tools/issues/2017

account_move_line_purchase_info/static/description/index.html

+13-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
<?xml version="1.0" encoding="utf-8"?>
21
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
32
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
43
<head>
@@ -9,10 +8,11 @@
98

109
/*
1110
:Author: David Goodger (goodger@python.org)
12-
:Id: $Id: html4css1.css 8954 2022-01-20 10:10:25Z milde $
11+
:Id: $Id: html4css1.css 9511 2024-01-13 09:50:07Z milde $
1312
:Copyright: This stylesheet has been placed in the public domain.
1413
1514
Default cascading style sheet for the HTML output of Docutils.
15+
Despite the name, some widely supported CSS2 features are used.
1616
1717
See https://docutils.sourceforge.io/docs/howto/html-stylesheets.html for how to
1818
customize this style sheet.
@@ -275,7 +275,7 @@
275275
margin-left: 2em ;
276276
margin-right: 2em }
277277

278-
pre.code .ln { color: grey; } /* line numbers */
278+
pre.code .ln { color: gray; } /* line numbers */
279279
pre.code, code { background-color: #eeeeee }
280280
pre.code .comment, code .comment { color: #5C6576 }
281281
pre.code .keyword, code .keyword { color: #3B0D06; font-weight: bold }
@@ -301,7 +301,7 @@
301301
span.pre {
302302
white-space: pre }
303303

304-
span.problematic {
304+
span.problematic, pre.problematic {
305305
color: red }
306306

307307
span.section-subtitle {
@@ -373,6 +373,12 @@ <h1 class="title">Account Move Line Purchase Info</h1>
373373
<p>This module will add the purchase order line to journal items.</p>
374374
<p>The ultimate goal is to establish the purchase order line as one of the key
375375
fields to reconcile the Goods Received Not Invoiced accrual account.</p>
376+
<p>Field oca_purchase_line_id it’s necessary. In Odoo &gt;=16 automatic
377+
revaluation for a product with FIFO costing method only works if invoice
378+
lines related to a purchase order line do not include stock journal items.
379+
To avoid that oca_purchase_line_id includes invoice and stock journal items,
380+
and we keep Odoo field invoice_lines just with bill lines.
381+
- Check issue <a class="reference external" href="https://github.com/OCA/account-financial-tools/issues/2017">https://github.com/OCA/account-financial-tools/issues/2017</a></p>
376382
<p><strong>Table of contents</strong></p>
377383
<div class="contents local topic" id="contents">
378384
<ul class="simple">
@@ -423,7 +429,9 @@ <h2><a class="toc-backref" href="#toc-entry-5">Contributors</a></h2>
423429
<div class="section" id="maintainers">
424430
<h2><a class="toc-backref" href="#toc-entry-6">Maintainers</a></h2>
425431
<p>This module is maintained by the OCA.</p>
426-
<a class="reference external image-reference" href="https://odoo-community.org"><img alt="Odoo Community Association" src="https://odoo-community.org/logo.png" /></a>
432+
<a class="reference external image-reference" href="https://odoo-community.org">
433+
<img alt="Odoo Community Association" src="https://odoo-community.org/logo.png" />
434+
</a>
427435
<p>OCA, or the Odoo Community Association, is a nonprofit organization whose
428436
mission is to support the collaborative development of Odoo features and
429437
promote its widespread use.</p>

account_move_line_purchase_info/tests/test_account_move_line_purchase_info.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ def _check_account_balance(
153153
"""
154154
domain = [("account_id", "=", account_id)]
155155
if purchase_line:
156-
domain.extend([("purchase_line_id", "=", purchase_line.id)])
156+
domain.extend([("oca_purchase_line_id", "=", purchase_line.id)])
157157

158158
balance = self._get_balance(domain)
159159
if purchase_line:

0 commit comments

Comments
 (0)