diff --git a/l10n_it_declaration_of_intent/models/account_move.py b/l10n_it_declaration_of_intent/models/account_move.py
index 163f235e5d63..8eaa20e4c3f2 100644
--- a/l10n_it_declaration_of_intent/models/account_move.py
+++ b/l10n_it_declaration_of_intent/models/account_move.py
@@ -8,7 +8,6 @@
class AccountMove(models.Model):
-
_inherit = "account.move"
declaration_of_intent_ids = fields.Many2many(
@@ -258,6 +257,10 @@ def check_declarations_amounts(self, declarations):
def get_declaration_residual_amounts(self, declarations):
"""Get residual amount for every `declarations`."""
+ plafond = self.env.user.company_id.declaration_yearly_limit_ids.filtered(
+ lambda r: r.year == str(fields.first(declarations).date_start.year)
+ )
+ available_plafond = plafond.limit_amount - plafond.actual_used_amount
declarations_amounts = {}
# If the tax amount is 0, then there is no line representing the tax
# so there will be no line having tax_line_id.
@@ -273,7 +276,12 @@ def get_declaration_residual_amounts(self, declarations):
for declaration in declarations:
if declaration.id not in declarations_amounts:
- declarations_amounts[declaration.id] = declaration.available_amount
+ if declaration.available_amount > available_plafond:
+ declarations_amounts[declaration.id] = available_plafond
+ else:
+ declarations_amounts[
+ declaration.id
+ ] = declaration.available_amount
if any(tax in declaration.taxes_ids for tax in tax_line.tax_ids):
declarations_amounts[declaration.id] -= amount
for declaration in declarations:
@@ -295,7 +303,6 @@ def button_cancel(self):
class AccountMoveLine(models.Model):
-
_inherit = "account.move.line"
force_declaration_of_intent_id = fields.Many2one(
diff --git a/l10n_it_declaration_of_intent/models/declaration.py b/l10n_it_declaration_of_intent/models/declaration.py
index d5ce9e1294df..f31e00e885f9 100644
--- a/l10n_it_declaration_of_intent/models/declaration.py
+++ b/l10n_it_declaration_of_intent/models/declaration.py
@@ -8,7 +8,6 @@
class DeclarationOfIntentYearlyLimit(models.Model):
-
_name = "l10n_it_declaration_of_intent.yearly_limit"
_description = "Yearly limit for declarations"
_order = "company_id, year desc"
@@ -16,8 +15,14 @@ class DeclarationOfIntentYearlyLimit(models.Model):
company_id = fields.Many2one("res.company", string="Company")
year = fields.Char(required=True)
- limit_amount = fields.Float()
- used_amount = fields.Float(compute="_compute_used_amount")
+ limit_amount = fields.Float(string="Plafond")
+ # TODO align terms: used_amount > issued_declarations
+ used_amount = fields.Float(
+ string="Issued Declarations", compute="_compute_used_amount"
+ )
+ actual_used_amount = fields.Float(
+ string="Actual Used Amount", compute="_compute_used_amount"
+ )
def _compute_used_amount(self):
for record in self:
@@ -31,10 +36,10 @@ def _compute_used_amount(self):
]
)
record.used_amount = sum([d.limit_amount for d in declarations])
+ record.actual_used_amount = sum([d.used_amount for d in declarations])
class DeclarationOfIntent(models.Model):
-
_name = "l10n_it_declaration_of_intent.declaration"
_description = "Declaration of intent"
_order = "date_start desc,date_end desc"
@@ -120,7 +125,8 @@ def create(self, values):
sum([d.limit_amount for d in declarations]) + values["limit_amount"]
)
if actual_limit_total > plafond.limit_amount:
- raise UserError(_("Total of documents exceed yearly limit"))
+ if plafond.limit_amount < plafond.actual_used_amount:
+ raise UserError(_("Total of documents exceed yearly limit"))
# ----- Assign a number to declaration
if values and not values.get("number", ""):
values["number"] = self.env["ir.sequence"].next_by_code(
@@ -255,7 +261,6 @@ def get_all_for_partner(self, type_d=None, partner_id=False, ignore_state=False)
class DeclarationOfIntentLine(models.Model):
-
_name = "l10n_it_declaration_of_intent.declaration_line"
_description = "Details of declaration of intent"
diff --git a/l10n_it_declaration_of_intent/views/company_view.xml b/l10n_it_declaration_of_intent/views/company_view.xml
index eae75e5c27ba..97195a2bacfb 100644
--- a/l10n_it_declaration_of_intent/views/company_view.xml
+++ b/l10n_it_declaration_of_intent/views/company_view.xml
@@ -6,12 +6,13 @@
-
+
+
diff --git a/l10n_it_fatturapa_out_di/tests/test_fatturapa_xml_validation.py b/l10n_it_fatturapa_out_di/tests/test_fatturapa_xml_validation.py
index 294937eb1348..83ade3d48001 100644
--- a/l10n_it_fatturapa_out_di/tests/test_fatturapa_xml_validation.py
+++ b/l10n_it_fatturapa_out_di/tests/test_fatturapa_xml_validation.py
@@ -32,6 +32,9 @@ def setUp(self):
self.env.company.fatturapa_fiscal_position_id = self.env.ref(
"l10n_it_fatturapa.fatturapa_RF01"
).id
+ self.env.company.declaration_yearly_limit_ids = [
+ (0, 0, {"year": 2016, "limit_amount": 500})
+ ]
self.env.ref("product.decimal_product_uom").digits = 3
self.env.ref("uom.product_uom_unit").name = "Unit(s)"