Skip to content

Commit 74abccd

Browse files
committed
[FIX] l10n_it_fatturapa_in: Let the move line compute its account
In `16.0` the field `account.move.line.account_id` is computed based on many factors, for instance the most used account by the supplier. This also allows other modules like `account_invoice_line_default_account` to work as expected.
1 parent 9f6c1ad commit 74abccd

File tree

2 files changed

+65
-84
lines changed

2 files changed

+65
-84
lines changed

l10n_it_fatturapa_in/tests/test_import_fatturapa_xml.py

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -208,31 +208,15 @@ def test_08_xml_import_no_account(self):
208208
journal_account = journal.default_account_id
209209
journal.default_account_id = False
210210

211-
expense_default_property = self.env["ir.property"]._get_property(
212-
"property_account_expense_categ_id",
213-
"product.category",
214-
res_id=False,
215-
)
216-
# Setting res_id disables the property from acting as default value
217-
expense_default_property.res_id = 1
218-
with self.assertRaises(UserError) as ue:
211+
with self.assertRaises(UserError) as ue, mute_logger("odoo.sql_db"):
219212
res = self.run_wizard("test8_no_account", "IT05979361218_005.xml")
220213
# allow following code to reuse the same XML file
221214
invoice_id = res.get("domain")[0][2][0]
222215
invoice = self.invoice_model.browse(invoice_id)
223216
invoice.ref = invoice.payment_reference = "14082"
224217
self.assertIn(journal.display_name, ue.exception.args[0])
225-
self.assertIn(company.display_name, ue.exception.args[0])
226-
227-
# Restore the property and import the invoice
228-
expense_default_property.res_id = False
229-
res = self.run_wizard("test8_with_property", "IT05979361218_005.xml")
230-
invoice_id = res.get("domain")[0][2][0]
231-
invoice = self.invoice_model.browse(invoice_id)
232-
# allow following code to reuse the same XML file
233-
invoice.ref = invoice.payment_reference = "14083"
234218

235-
# Restore the property and import the invoice
219+
# Restore the journal account and import the invoice
236220
journal.default_account_id = journal_account
237221
res = self.run_wizard("test8_with_journal", "IT05979361218_005.xml")
238222
invoice_id = res.get("domain")[0][2][0]

l10n_it_fatturapa_in/wizard/wizard_import_fatturapa.py

Lines changed: 63 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@
44

55
import logging
66
import re
7+
import warnings
78
from datetime import datetime
89

10+
import psycopg2
11+
912
from odoo import api, fields, models
1013
from odoo.exceptions import UserError
1114
from odoo.fields import first
@@ -614,14 +617,9 @@ def get_line_product(self, line, partner):
614617
return product
615618

616619
def adjust_accounting_data(self, product, line_vals):
617-
account = self.get_credit_account(product)
618-
line_vals["account_id"] = account.id
619-
620620
new_tax = None
621621
if len(product.product_tmpl_id.supplier_taxes_id) == 1:
622622
new_tax = product.product_tmpl_id.supplier_taxes_id[0]
623-
elif len(account.tax_ids) == 1:
624-
new_tax = account.tax_ids[0]
625623
line_tax = self.env["account.tax"]
626624
if (
627625
line_vals.get("tax_ids")
@@ -651,10 +649,16 @@ def adjust_accounting_data(self, product, line_vals):
651649
# move_line.tax_ids
652650
# move_line.name
653651
# move_line.sequence
654-
# move_line.account_id
655652
# move_line.price_unit
656653
# move_line.quantity
657654
def _prepareInvoiceLineAliquota(self, credit_account_id, line, nline):
655+
if credit_account_id:
656+
warnings.warn(
657+
"The `credit_account_id` argument is deprecated "
658+
"because the account is automatically computed from the move line.",
659+
DeprecationWarning,
660+
stacklevel=2,
661+
)
658662
retLine = {}
659663
account_taxes = self.get_account_taxes(line.AliquotaIVA, line.Natura)
660664
if account_taxes:
@@ -666,27 +670,31 @@ def _prepareInvoiceLineAliquota(self, credit_account_id, line, nline):
666670
{
667671
"name": f"Riepilogo Aliquota {line.AliquotaIVA}",
668672
"sequence": nline,
669-
"account_id": credit_account_id,
670673
"price_unit": float(abs(line.ImponibileImporto)),
671674
}
672675
)
673676
return retLine
674677

675678
# move_line.name
676679
# move_line.sequence
677-
# move_line.account_id
678680
# move_line.price_unit
679681
# move_line.quantity
680682
# move_line.discount
681683
# move_line.admin_ref
682684
# move_line.invoice_line_tax_wt_ids
683685
def _prepareInvoiceLine(self, credit_account_id, line, wt_founds=False):
686+
if credit_account_id:
687+
warnings.warn(
688+
"The `credit_account_id` argument is deprecated "
689+
"because the account is automatically computed from the move line.",
690+
DeprecationWarning,
691+
stacklevel=2,
692+
)
684693
retLine = self._prepare_generic_line_data(line)
685694
retLine.update(
686695
{
687696
"name": line.Descrizione,
688697
"sequence": int(line.NumeroLinea),
689-
"account_id": credit_account_id,
690698
"price_unit": float(line.PrezzoUnitario),
691699
"display_type": "product",
692700
}
@@ -1029,52 +1037,13 @@ def create_e_invoice_line(self, line):
10291037
return einvoiceline
10301038

10311039
def get_credit_account(self, product=None):
1032-
"""
1033-
Try to get default credit account for invoice line looking in
1034-
1035-
1) product (if provided)
1036-
2) journal
1037-
3) company default.
1038-
1039-
:param product: Product whose expense account will be used
1040-
:return: The account found
1041-
"""
1042-
credit_account = self.env["account.account"].browse()
1043-
1044-
# If there is a product, get its default expense account
1045-
if product:
1046-
template = product.product_tmpl_id
1047-
accounts_dict = template.get_product_accounts()
1048-
credit_account = accounts_dict["expense"]
1049-
1050-
company = self.env.company
1051-
# Search in journal
1052-
journal = self.get_journal(company)
1053-
if not credit_account:
1054-
credit_account = journal.default_account_id
1055-
1056-
# Search in company defaults
1057-
if not credit_account:
1058-
credit_account = (
1059-
self.env["ir.property"]
1060-
.with_company(company)
1061-
._get("property_account_expense_categ_id", "product.category")
1062-
)
1063-
1064-
if not credit_account:
1065-
raise UserError(
1066-
_(
1067-
"Please configure Default Credit Account "
1068-
"in Journal '{journal}' "
1069-
"or check default expense account "
1070-
"for company '{company}'."
1071-
).format(
1072-
journal=journal.display_name,
1073-
company=company.display_name,
1074-
)
1075-
)
1076-
1077-
return credit_account
1040+
warnings.warn(
1041+
"The `get_credit_account` method is deprecated "
1042+
"because the account is automatically computed from the move line.",
1043+
DeprecationWarning,
1044+
stacklevel=2,
1045+
)
1046+
return self.env["account.account"].browse()
10781047

10791048
def _get_currency(self, FatturaBody):
10801049
# currency 2.1.1.2
@@ -1207,14 +1176,13 @@ def invoiceCreate(self, fatt, fatturapa_attachment, FatturaBody, partner_id):
12071176
found_withholding_taxes = self.set_withholding_tax(FatturaBody, invoice_data)
12081177

12091178
invoice = self.env["account.move"].create(invoice_data)
1210-
credit_account = self.get_credit_account()
12111179

12121180
invoice_lines = []
12131181
# 2.2.1
12141182
invoice_lines.extend(
12151183
self.set_invoice_line_ids(
12161184
FatturaBody,
1217-
credit_account.id,
1185+
False,
12181186
partner,
12191187
found_withholding_taxes,
12201188
invoice,
@@ -1223,9 +1191,7 @@ def invoiceCreate(self, fatt, fatturapa_attachment, FatturaBody, partner_id):
12231191

12241192
# 2.1.1.7
12251193
invoice_lines.extend(
1226-
self.set_welfares_fund(
1227-
FatturaBody, credit_account.id, invoice, found_withholding_taxes
1228-
)
1194+
self.set_welfares_fund(FatturaBody, False, invoice, found_withholding_taxes)
12291195
)
12301196

12311197
# 2.1.1.10
@@ -1600,6 +1566,13 @@ def set_withholding_tax(self, FatturaBody, invoice_data):
16001566
return found_withholding_taxes
16011567

16021568
def set_welfares_fund(self, FatturaBody, credit_account_id, invoice, wt_founds):
1569+
if credit_account_id:
1570+
warnings.warn(
1571+
"The `credit_account_id` argument is deprecated "
1572+
"because the account is automatically computed from the move line.",
1573+
DeprecationWarning,
1574+
stacklevel=2,
1575+
)
16031576
invoice_line_model = self.env["account.move.line"]
16041577
invoice_line_ids = []
16051578
if self.e_invoice_detail_level == "2":
@@ -1621,7 +1594,6 @@ def set_welfares_fund(self, FatturaBody, credit_account_id, invoice, wt_founds):
16211594
"name": _("Welfare Fund: %s") % welfareLine.TipoCassa,
16221595
"price_unit": float(welfareLine.ImportoContributoCassa),
16231596
"move_id": invoice.id,
1624-
"account_id": credit_account_id,
16251597
"quantity": 1,
16261598
}
16271599
)
@@ -1735,6 +1707,13 @@ def _set_invoice_lines(
17351707
def set_invoice_line_ids(
17361708
self, FatturaBody, credit_account_id, partner, wt_founds, invoice
17371709
):
1710+
if credit_account_id:
1711+
warnings.warn(
1712+
"The `credit_account_id` argument is deprecated "
1713+
"because the account is automatically computed from the move line.",
1714+
DeprecationWarning,
1715+
stacklevel=2,
1716+
)
17381717
invoice_lines = []
17391718
invoice_line_model = self.env["account.move.line"]
17401719
if self.e_invoice_detail_level == "1":
@@ -1779,11 +1758,29 @@ def check_invoice_amount(self, invoice, FatturaElettronicaBody):
17791758
)
17801759

17811760
def create_and_get_line_id(self, invoice_line_ids, invoice_line_model, upd_vals):
1782-
invoice_line_id = (
1783-
invoice_line_model.with_context(check_move_validity=False)
1784-
.create(upd_vals)
1785-
.id
1786-
)
1761+
try:
1762+
with self.env.cr.savepoint():
1763+
invoice_line_id = (
1764+
invoice_line_model.with_context(check_move_validity=False)
1765+
.create(upd_vals)
1766+
.id
1767+
)
1768+
except psycopg2.errors.CheckViolation as cv:
1769+
if (
1770+
cv.diag.constraint_name
1771+
== "account_move_line_check_accountable_required_fields"
1772+
):
1773+
move = self.env["account.move"].browse(upd_vals["move_id"])
1774+
journal = move.journal_id
1775+
if journal:
1776+
raise UserError(
1777+
_(
1778+
"Please configure Default Credit Account "
1779+
"in Journal %(journal)s'.",
1780+
journal=journal.display_name,
1781+
)
1782+
) from cv
1783+
raise cv
17871784
invoice_line_ids.append(invoice_line_id)
17881785

17891786
def _set_decimal_precision(self, precision_name, field_name, attachments):

0 commit comments

Comments
 (0)