Skip to content

Commit

Permalink
[IMP] l10n_it_fatturapa_in: Warn user if there is no default credit a…
Browse files Browse the repository at this point in the history
…ccount configured
  • Loading branch information
SimoRubi authored and TheMule71 committed Jun 15, 2021
1 parent cd1214c commit 6e75ad1
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 22 deletions.
50 changes: 50 additions & 0 deletions l10n_it_fatturapa_in/tests/test_import_fatturapa_xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,56 @@ def test_08_xml_import(self):
self.assertAlmostEqual(invoice.amount_total, 1288.61)
self.assertFalse(invoice.inconsistencies)

def test_08_xml_import_no_account(self):
"""Check that a useful error message is raised when
the credit account is missing in purchase journal."""
company = self.env.user.company_id
journal = self.wizard_model.get_purchase_journal(company)
journal_account = journal.default_account_id
journal.default_account_id = False

expense_default_property = self.env["ir.property"]._get_property(
"property_account_expense_categ_id",
"product.category",
res_id=False,
)
# Setting res_id disables the property from acting as default value
expense_default_property.res_id = 1
with self.assertRaises(UserError) as ue:
self.run_wizard("test8_no_account", "IT05979361218_005.xml")
self.assertIn(journal.display_name, ue.exception.args[0])
self.assertIn(company.display_name, ue.exception.args[0])

discount_amount = -143.18
# Restore the property and import the invoice
expense_default_property.res_id = False
res = self.run_wizard("test8_with_property", "IT05979361218_005.xml")
invoice_id = res.get("domain")[0][2][0]
invoice = self.invoice_model.browse(invoice_id)
invoice_lines = invoice.invoice_line_ids
discount_line = invoice_lines.filtered(
lambda line: line.price_unit == discount_amount
)
self.assertEqual(
discount_line.account_id,
expense_default_property.get_by_record(),
)

# Restore the property and import the invoice
journal.default_account_id = journal_account
res = self.run_wizard("test8_with_journal", "IT05979361218_005.xml")
invoice_id = res.get("domain")[0][2][0]
invoice = self.invoice_model.browse(invoice_id)
invoice_lines = invoice.invoice_line_ids
discount_line = invoice_lines.filtered(
lambda line: line.price_unit == discount_amount
)
self.assertEqual(
discount_line.account_id,
journal_account,
)
self.assertTrue(invoice)

def test_09_xml_import(self):
# using DatiGeneraliDocumento.ScontoMaggiorazione without
# ImportoTotaleDocumento
Expand Down
86 changes: 64 additions & 22 deletions l10n_it_fatturapa_in/wizard/wizard_import_fatturapa.py
Original file line number Diff line number Diff line change
Expand Up @@ -475,15 +475,9 @@ def get_line_product(self, line, partner):
return product

def adjust_accounting_data(self, product, line_vals):
if product.product_tmpl_id.property_account_expense_id:
line_vals[
"account_id"
] = product.product_tmpl_id.property_account_expense_id.id
elif product.product_tmpl_id.categ_id.property_account_expense_categ_id:
line_vals[
"account_id"
] = product.product_tmpl_id.categ_id.property_account_expense_categ_id.id
account = self.env["account.account"].browse(line_vals["account_id"])
account = self.get_credit_account(product)
line_vals["account_id"] = account.id

new_tax = None
if len(product.product_tmpl_id.supplier_taxes_id) == 1:
new_tax = product.product_tmpl_id.supplier_taxes_id[0]
Expand Down Expand Up @@ -696,22 +690,22 @@ def _addGlobalDiscount(self, invoice_id, DatiGeneraliDocumento):
discount -= float(DiscRise.Importo)
elif DiscRise.Tipo == "MG":
discount += float(DiscRise.Importo)
journal = self.get_purchase_journal(invoice.company_id)
credit_account_id = journal.default_account_id.id
company = invoice.company_id
global_discount_product = company.sconto_maggiorazione_product_id
credit_account = self.get_credit_account(
product=global_discount_product,
)
line_vals = {
"move_id": invoice_id,
"name": _("Global bill discount from document general data"),
"account_id": credit_account_id,
"account_id": credit_account.id,
"price_unit": discount,
"quantity": 1,
}
if self.env.company.sconto_maggiorazione_product_id:
sconto_maggiorazione_product = (
self.env.company.sconto_maggiorazione_product_id
)
line_vals["product_id"] = sconto_maggiorazione_product.id
line_vals["name"] = sconto_maggiorazione_product.name
self.adjust_accounting_data(sconto_maggiorazione_product, line_vals)
if global_discount_product:
line_vals["product_id"] = global_discount_product.id
line_vals["name"] = global_discount_product.name
self.adjust_accounting_data(global_discount_product, line_vals)
self.env["account.move.line"].with_context(
check_move_validity=False
).create(line_vals)
Expand Down Expand Up @@ -912,6 +906,54 @@ def create_e_invoice_line(self, line):
)
return einvoiceline

def get_credit_account(self, product=None):
"""
Try to get default credit account for invoice line looking in
1) product (if provided)
2) purchase journal
3) company default.
:param product: Product whose expense account will be used
:return: The account found
"""
credit_account = self.env["account.account"].browse()

# If there is a product, get its default expense account
if product:
template = product.product_tmpl_id
accounts_dict = template.get_product_accounts()
credit_account = accounts_dict["expense"]

company = self.env.user.company_id
# Search in purchase journal
journal = self.get_purchase_journal(company)
if not credit_account:
credit_account = journal.default_account_id

# Search in company defaults
if not credit_account:
credit_account = (
self.env["ir.property"]
.with_company(company)
._get("property_account_expense_categ_id", "product.category")
)

if not credit_account:
raise UserError(
_(
"Please configure Default Credit Account "
"in Journal '{journal}' "
"or check default expense account "
"for company '{company}'."
).format(
journal=journal.display_name,
company=company.display_name,
)
)

return credit_account

def invoiceCreate(self, fatt, fatturapa_attachment, FatturaBody, partner_id):
partner_model = self.env["res.partner"]
invoice_model = self.env["account.move"]
Expand All @@ -934,7 +976,7 @@ def invoiceCreate(self, fatt, fatturapa_attachment, FatturaBody, partner_id):
)
)
purchase_journal = self.get_purchase_journal(company)
credit_account_id = purchase_journal.default_account_id.id
credit_account = self.get_credit_account()
comment = ""
# 2.1.1
docType_id = False
Expand Down Expand Up @@ -998,13 +1040,13 @@ def invoiceCreate(self, fatt, fatturapa_attachment, FatturaBody, partner_id):
# 2.2.1
invoice_lines.extend(
self.set_invoice_line_ids(
FatturaBody, credit_account_id, partner, wt_founds, invoice
FatturaBody, credit_account.id, partner, wt_founds, invoice
)
)

# 2.1.1.7
invoice_lines.extend(
self.set_welfares_fund(FatturaBody, credit_account_id, invoice, wt_founds)
self.set_welfares_fund(FatturaBody, credit_account.id, invoice, wt_founds)
)

# 2.1.1.10
Expand Down

0 comments on commit 6e75ad1

Please sign in to comment.