diff --git a/l10n_it_intrastat_statement/models/intrastat_statement.py b/l10n_it_intrastat_statement/models/intrastat_statement.py
index 4e6cb7e39a9d..bb1af6ab78d4 100644
--- a/l10n_it_intrastat_statement/models/intrastat_statement.py
+++ b/l10n_it_intrastat_statement/models/intrastat_statement.py
@@ -1,7 +1,7 @@
# Copyright 2019 Simone Rubino - Agile Business Group
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
-from datetime import date, datetime, timedelta
+from datetime import date, timedelta
from dateutil.relativedelta import relativedelta
@@ -449,7 +449,7 @@ def get_dates_start_stop(self):
month = self.period_number
period_date_start = date(year, month, 1)
period_date_stop = (
- datetime(year, month, 1) + relativedelta(months=1) - timedelta(days=1)
+ period_date_start + relativedelta(months=1) - timedelta(days=1)
)
elif self.period_type == "T":
quarter = self.period_number
@@ -819,11 +819,23 @@ def compute_statement(self):
refund_section_details = (section_type, refund_section_number)
section_field = self.get_section_field_name(*section_details)
for line in self[section_field]:
- refund_section_model = self.get_section_model(
- *refund_section_details
- )
- to_refund_model = self.env[refund_section_model]
- self.refund_line(line, to_refund_model)
+ # Compensation can happen only if the credit note has been issued
+ # for invoices belonging the considered period. Here we check
+ # if the reversed entry of the Sale/Purchase section 2 line
+ # respect this constraint otherwise no compensation shouldn't
+ # happen
+ if (
+ line.invoice_id.reversed_entry_id
+ and line.invoice_id.reversed_entry_id.invoice_date
+ >= period_date_start
+ and line.invoice_id.reversed_entry_id.invoice_date
+ <= period_date_stop
+ ):
+ refund_section_model = self.get_section_model(
+ *refund_section_details
+ )
+ to_refund_model = self.env[refund_section_model]
+ self.refund_line(line, to_refund_model)
return True
@staticmethod
diff --git a/l10n_it_intrastat_statement/views/intrastat.xml b/l10n_it_intrastat_statement/views/intrastat.xml
index 5c1a9a57801e..f2d6ef7e7044 100644
--- a/l10n_it_intrastat_statement/views/intrastat.xml
+++ b/l10n_it_intrastat_statement/views/intrastat.xml
@@ -282,15 +282,15 @@
account.intrastat.statement.sale.section1
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
@@ -356,16 +356,17 @@
account.intrastat.statement.sale.section2
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+