Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] [l10n_it_intrastat_statement] Bug sulla compensazione delle note di credito emesse/ricevute #4450

Open
wants to merge 1 commit into
base: 16.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions l10n_it_intrastat_statement/models/intrastat_statement.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -451,7 +451,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
Expand Down Expand Up @@ -815,11 +815,21 @@ 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 should
# happen
refund_date = line.invoice_id.reversed_entry_id.invoice_date
if (
refund_date
and period_date_start <= refund_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
Expand Down
56 changes: 51 additions & 5 deletions l10n_it_intrastat_statement/tests/test_intrastat_statement.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def setUp(self):
)

def _get_intrastat_computed_bill(
self, product=None, currency=None, price_unit=100.0
self, product=None, currency=None, price_unit=100.0, date=None
):
if product is None:
product = self.product01
Expand All @@ -99,20 +99,26 @@ def _get_intrastat_computed_bill(
product=product,
taxes=self.tax22_purchase,
amount=price_unit,
date=date,
)
if currency:
invoice.currency_id = currency
invoice.action_post()
return invoice

def _get_intrastat_computed_invoice(self, price_unit=100.0):
def _get_intrastat_computed_invoice(
self,
price_unit=100.0,
date=None,
):
invoice = self._create_move(
"out_invoice",
partner=self.partner01,
product=self.product01,
taxes=self.tax22_sale,
amount=price_unit,
post=True,
date=date,
)
return invoice

Expand Down Expand Up @@ -221,6 +227,11 @@ def test_statement_purchase_refund(self):
statement.generate_file_export()

statement.compute_statement()

# Check compensation, we expect only purchase section 1 having one line
self.assertEqual(len(statement.purchase_section1_ids), 1)
self.assertEqual(len(statement.purchase_section2_ids), 0)

file_content = statement.with_context(purchase=True).generate_file_export()
self.assertIn(bill_refund.partner_id.vat[2:], file_content)

Expand All @@ -232,6 +243,38 @@ def test_statement_purchase_refund(self):
self.assertEqual(len(file_lines), 3)
self.assertSetEqual({len(line) for line in file_lines}, {75, 130, 119})

def test_statement_purchase_refund_no_compensation(self):
bill = self._get_intrastat_computed_bill(date="2018-12-15")
self._get_intrastat_computed_bill(date="2019-01-05")

bill_refund = self._create_move_refund(bill, date="2019-01-10")
# This refund will be subtracted from bill
bill_refund.update(
{
"intrastat": True,
}
)
bill_refund.action_post()
bill_refund.compute_intrastat_lines()

statement = self.statement_model.create(
{
"period_number": bill_refund.invoice_date.month,
"fiscalyear": bill_refund.invoice_date.year,
}
)

# We have created 2 bills, one in 2018 and another in 2019, we make a
# refund in 2019 of the bill made in 2018. We expect that the refund,
# in the generated statement, is not compensated because the original
# bill referenced by the refund belongs to a different period
statement.compute_statement()

# No compensation should happen, we expect both purchase section 1 and 2
# to have 1 entry
self.assertEqual(len(statement.purchase_section1_ids), 1)
self.assertEqual(len(statement.purchase_section2_ids), 1)

def test_statement_purchase_refund_no_subtract(self):
bill = self._get_intrastat_computed_bill()

Expand Down Expand Up @@ -507,11 +550,14 @@ def _create_move(
product=None,
amount=None,
taxes=None,
date=None,
):
move_form = Form(
self.env["account.move"].with_context(default_move_type=move_type)
)
move_form.invoice_date = invoice_date or fields.Date.from_string("2019-01-01")
move_form.invoice_date = invoice_date or fields.Date.from_string(
date or "2019-01-01"
)
move_form.partner_id = partner or self.partner_a
move_form.intrastat = True

Expand All @@ -529,13 +575,13 @@ def _create_move(

return rslt

def _create_move_refund(self, move):
def _create_move_refund(self, move, date=None):
move_reversal = (
self.env["account.move.reversal"]
.with_context(active_model="account.move", active_ids=move.ids)
.create(
{
"date": fields.Date.from_string("2019-01-01"),
"date": fields.Date.from_string(date or "2019-01-01"),
"reason": "no reason",
"refund_method": "refund",
"journal_id": move.journal_id.id,
Expand Down
39 changes: 20 additions & 19 deletions l10n_it_intrastat_statement/views/intrastat.xml
Original file line number Diff line number Diff line change
Expand Up @@ -282,15 +282,15 @@
<field name="model">account.intrastat.statement.sale.section1</field>
<field name="arch" type="xml">
<tree>
<field name="sequence" />
<field name="invoice_id" />
<field name="partner_id" string="Buyer" />
<field name="country_partner_id" string="Buyer State" />
<field name="intrastat_code_id" string="Goods Code" />
<field name="weight_kg" />
<field name="country_destination_id" />
<field name="province_origin_id" />
<field name="amount_euro" sum="Total" />
<field name="sequence" />
<field name="invoice_id" />
<field name="partner_id" string="Buyer" />
<field name="country_partner_id" string="Buyer State" />
<field name="intrastat_code_id" string="Goods Code" />
<field name="weight_kg" />
<field name="country_destination_id" />
<field name="province_origin_id" />
<field name="amount_euro" sum="Total" />
</tree>
</field>
</record>
Expand Down Expand Up @@ -355,16 +355,17 @@
<field name="model">account.intrastat.statement.sale.section2</field>
<field name="arch" type="xml">
<tree>
<field name="sequence" />
<field name="year_id" />
<field name="quarterly" />
<field name="month" />
<field name="country_partner_id" string="Buyer State" />
<field name="partner_id" string="Buyer" />
<field name="vat_code" string="Buyer VAT Code" />
<field name="intrastat_code_id" string="Goods Code" />
<field name="sign_variation" />
<field name="amount_euro" sum="Total" />
<field name="sequence" />
<field name="invoice_id" />
<field name="year_id" />
<field name="quarterly" />
<field name="month" />
<field name="country_partner_id" string="Buyer State" />
<field name="partner_id" string="Buyer" />
<field name="vat_code" string="Buyer VAT Code" />
<field name="intrastat_code_id" string="Goods Code" />
<field name="sign_variation" />
<field name="amount_euro" sum="Total" />
</tree>
</field>
</record>
Expand Down
Loading