Skip to content

Commit

Permalink
[IMP] l10n_it_fatturapa_out: improve preventive checks on taxes
Browse files Browse the repository at this point in the history
  • Loading branch information
TheMule71 committed Sep 5, 2024
1 parent 4c19d38 commit 548cba0
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 6 deletions.
39 changes: 33 additions & 6 deletions l10n_it_fatturapa_out/models/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from odoo import api, fields, models
from odoo.exceptions import UserError
from odoo.tools.float_utils import float_is_zero
from odoo.tools.translate import _

fatturapa_attachment_state_mapping = {
Expand Down Expand Up @@ -48,6 +49,36 @@ def _compute_fatturapa_state(self):
)

def preventive_checks(self):
def _check_taxes(invoice):
if not all(
aml.tax_ids for aml in invoice.invoice_line_ids if aml.product_id
):
raise UserError(
_("Invoice %s contains product lines w/o taxes") % invoice.name
)

# see odoo/addons/account/models/account_tax.py
# amount is defined as fields.Float(digits=(16, 4), ...)
for tax in invoice.invoice_line_ids.tax_ids.filtered(
lambda t: float_is_zero(t.amount, precision_digits=4)
):
if not tax.kind_id:
raise UserError(
_(
"Invoice %(invoice)s: a tax exemption kind"
" must be specified for tax %(tax)s"
)
% {"invoice": invoice.name, "tax": tax.name}
)
if not tax.law_reference:
raise UserError(
_(
"Invoice %(invoice)s: the law reference"
" must be specified for tax %(tax)s"
)
% {"invoice": invoice.name, "tax": tax.name}
)

for invoice in self:
if not invoice.is_sale_document():
raise UserError(
Expand Down Expand Up @@ -81,12 +112,8 @@ def preventive_checks(self):
)
)

if not all(
aml.tax_ids for aml in invoice.invoice_line_ids if aml.product_id
):
raise UserError(
_("Invoice %s contains product lines w/o taxes") % invoice.name
)
_check_taxes(invoice)

company_id = invoice.company_id
if company_id.vat != company_id.partner_id.vat:
raise UserError(
Expand Down
1 change: 1 addition & 0 deletions l10n_it_fatturapa_out/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
from . import fatturapa_common
from . import test_fatturapa_xml_validation
from . import test_fatturapa_out_noteline
from . import test_fatturapa_preventive_checks
88 changes: 88 additions & 0 deletions l10n_it_fatturapa_out/tests/test_fatturapa_preventive_checks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# Copyright 2023 Marco Colombo <[email protected]>

from odoo.exceptions import UserError
from odoo.tests import tagged

from .fatturapa_common import FatturaPACommon


@tagged("post_install", "-at_install")
class TestFatturaPAPreventiveChecks(FatturaPACommon):
def setUp(self):
super().setUp()
self.company = self.env.company = self.sales_journal.company_id

# XXX - a company named "YourCompany" alread exists
# we move it out of the way but we should do better here
self.env.company.sudo().search([("name", "=", "YourCompany")]).write(
{"name": "YourCompany_"}
)
self.env.company.name = "YourCompany"
self.env.company.vat = "IT06363391001"

def test_1_missing_taxes(self):
self.env.company.fatturapa_pub_administration_ref = "F000000111"
self.set_sequences(13, "2016-01-07")
invoice = self.invoice_model.create(
{
"partner_id": self.res_partner_fatturapa_0.id,
"move_type": "out_invoice",
"invoice_line_ids": [
(
0,
0,
{
"account_id": self.a_sale.id,
"product_id": self.product_product_10.id,
"name": "Mouse\nOptical",
"quantity": 1,
"product_uom_id": self.product_uom_unit.id,
"price_unit": 10,
"tax_ids": [(5, 0, 0)],
},
),
],
}
)
invoice._post()
with self.assertRaises(UserError) as ue:
self.run_wizard(invoice.id)
error_message = "Invoice {} contains product lines w/o taxes".format(
invoice.name
)
self.assertEqual(ue.exception.args[0], error_message)

# test lack of exemption kind
tax_0 = self.tax_00_ns.copy()
tax_0.name = "Exempt (test)"
former_kind_id = tax_0.kind_id
tax_0.kind_id = False

invoice = invoice.copy()
invoice.invoice_line_ids[0].tax_ids = [(6, 0, [tax_0.id])]
invoice._post()
error_message = (
"Invoice {}: a tax exemption kind must be specified for tax {}".format(
invoice.name, tax_0.name
)
)
with self.assertRaises(UserError) as ue:
self.run_wizard(invoice.id)
self.assertEqual(ue.exception.args[0], error_message)

# test lack of law_reference
tax_0.kind_id = former_kind_id
tax_0.law_reference = False

invoice = invoice.copy()
invoice.invoice_line_ids[0].tax_ids = [(6, 0, [tax_0.id])]
invoice._post()

error_message = (
"Invoice {}: the law reference must be specified for tax {}".format(
invoice.name, tax_0.name
)
)
with self.assertRaises(UserError) as ue:
self.run_wizard(invoice.id)
self.assertEqual(ue.exception.args[0], error_message)

0 comments on commit 548cba0

Please sign in to comment.