From 179386d8e1201438603a622adb24994d70893581 Mon Sep 17 00:00:00 2001 From: gianmarco Date: Mon, 2 Aug 2021 14:54:25 +0200 Subject: [PATCH 1/2] [IMP] Add new field to filter the taxes to be processed, so the account configured in the taxes "Account used for VAT settlement" must be indicated. --- account_vat_period_end_statement/README.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/account_vat_period_end_statement/README.rst b/account_vat_period_end_statement/README.rst index c13565a2eb32..5946913e298f 100644 --- a/account_vat_period_end_statement/README.rst +++ b/account_vat_period_end_statement/README.rst @@ -2,7 +2,7 @@ ITA - Liquidazione IVA ====================== -.. +.. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !! This file is generated by oca-gen-addon-readme !! !! changes will be overwritten. !! From 77959343015d9d242b137ae51fde564fd8791a61 Mon Sep 17 00:00:00 2001 From: Marco Colombo Date: Fri, 16 Sep 2022 18:40:19 +0200 Subject: [PATCH 2/2] [FIX] account_vat_period_end_statement: Select previous Statement by Account Co-Authored-by: SirTakobi --- .../models/account.py | 22 ++- .../readme/CONTRIBUTORS.rst | 3 + .../tests/test_vat_statement.py | 139 ++++++++++++++++++ 3 files changed, 159 insertions(+), 5 deletions(-) diff --git a/account_vat_period_end_statement/models/account.py b/account_vat_period_end_statement/models/account.py index 94a57265f5a8..8942de4a5c41 100644 --- a/account_vat_period_end_statement/models/account.py +++ b/account_vat_period_end_statement/models/account.py @@ -1,7 +1,8 @@ # Copyright 2011-2012 Domsense s.r.l. (). # Copyright 2012-15 Agile Business Group sagl () # Copyright 2015 Associazione Odoo Italia () -# Copyright 2021 Gianmarco Conte - Dinamiche Aziendali Srl () +# Copyright 2021 Gianmarco Conte - Dinamiche Aziendali Srl () +# Copyright 2022 Simone Rubino - TAKOBI # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). import math @@ -620,16 +621,27 @@ def _add_debit_vat_data(self, lines_to_create, move, statement, statement_date): debit_vat_data["credit"] = math.fabs(debit_line.amount) lines_to_create.append((0, 0, debit_vat_data)) + def _get_previous_statements(self): + self.ensure_one() + prev_statements = self.search( + [ + ("date", "<", self.date), + ("annual", "=", False), + ], + order="date desc", + ) + prev_statements_same_accounts = prev_statements.filtered( + lambda s: s.account_ids == self.account_ids + ) + return prev_statements_same_accounts + def compute_amounts(self): decimal_precision_obj = self.env["decimal.precision"] debit_line_model = self.env["statement.debit.account.line"] credit_line_model = self.env["statement.credit.account.line"] for statement in self: statement.previous_debit_vat_amount = 0.0 - prev_statements = self.search( - [("date", "<", statement.date), ("annual", "=", False)], - order="date desc", - ) + prev_statements = statement._get_previous_statements() if prev_statements and not statement.annual: prev_statement = prev_statements[0] if ( diff --git a/account_vat_period_end_statement/readme/CONTRIBUTORS.rst b/account_vat_period_end_statement/readme/CONTRIBUTORS.rst index 8efaaef9c5c7..b93e0e06618f 100644 --- a/account_vat_period_end_statement/readme/CONTRIBUTORS.rst +++ b/account_vat_period_end_statement/readme/CONTRIBUTORS.rst @@ -9,3 +9,6 @@ * Giacomo Grasso * Lara Baggio * Gianmarco Conte +* `TAKOBI `_: + + * Simone Rubino diff --git a/account_vat_period_end_statement/tests/test_vat_statement.py b/account_vat_period_end_statement/tests/test_vat_statement.py index 785ff9a21588..b99e108601a5 100644 --- a/account_vat_period_end_statement/tests/test_vat_statement.py +++ b/account_vat_period_end_statement/tests/test_vat_statement.py @@ -1,11 +1,14 @@ # Copyright 2015 Agile Business Group +# Copyright 2022 Simone Rubino - TAKOBI # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). from datetime import date, datetime from dateutil.rrule import MONTHLY +from odoo import fields from odoo.tests import tagged +from odoo.tests.common import Form from odoo.addons.account.tests.common import AccountTestInvoicingCommon @@ -277,3 +280,139 @@ def test_vat_statement(self): self.assertEqual(line.debit, 100) self.assertTrue(vat_auth_found) # TODO payment + + def _create_vendor_bill(self, partner, invoice_date, price_unit, tax): + """ + Create an open Vendor Bill for `partner` having date `invoice_date`. + The Bill will also have a Line having Price `price_unit` and Tax `tax`. + """ + bill_model = self.invoice_model.with_context( + invoice_type="in_invoice", + ) + bill_form = Form(bill_model) + bill_form.partner_id = partner + bill_form.invoice_date = invoice_date + with bill_form.invoice_line_ids.new() as line: + line.tax_ids.clear() + line.tax_ids.add(tax) + line.name = "Test Invoice Line" + line.account_id = self.company_data["default_account_expense"] + line.price_unit = price_unit + bill = bill_form.save() + bill.action_post() + return bill + + def _get_statement(self, period, statement_date, accounts): + """ + Create a VAT Statement in date `statement_date` + for Period `period` and Accounts `accounts`. + """ + # Create statement + statement_form = Form(self.vat_statement_model) + statement_form.journal_id = self.general_journal + statement_form.authority_vat_account_id = self.vat_authority + statement_form.payment_term_id = self.account_payment_term + statement_form.date = statement_date + statement_form.account_ids.clear() + for account in accounts: + statement_form.account_ids.add(account) + statement = statement_form.save() + + # Add period + add_period_model = self.env["add.period.to.vat.statement"] + add_period_model = add_period_model.with_context( + active_id=statement.id, + active_model=statement._name, + ) + add_period_form = Form(add_period_model) + add_period_form.period_id = period + add_period = add_period_form.save() + add_period.add_period() + return statement + + def test_different_previous_vat_statements(self): + """ + Statements for different Accounts + only count previous Amounts from Statements with the same Accounts. + """ + # Arrange: Create two different VAT Statements + # selecting two different Accounts + partner = self.env.ref("base.res_partner_4") + tax = self.account_tax_22_credit + tax_statement_account = tax.vat_statement_account_id + last_year_bill = self._create_vendor_bill( + partner, + self.last_year_recent_date, + 10, + tax, + ) + self.assertEqual(last_year_bill.state, "posted") + last_year_period = self.last_year_period + last_year_statement = self._get_statement( + last_year_period, + self.last_year_date, + tax_statement_account, + ) + self.assertTrue(last_year_statement) + + # Create another Bill and Statement + other_tax_statement_account = tax_statement_account.copy() + other_tax = tax.copy( + default={ + "vat_statement_account_id": other_tax_statement_account.id, + }, + ) + other_last_year_bill = self._create_vendor_bill( + partner, + self.last_year_recent_date, + 20, + other_tax, + ) + self.assertEqual(other_last_year_bill.state, "posted") + last_year_period.type_id.allow_overlap = True + other_last_year_period = last_year_period.copy( + default={ + "name": "Test Other last Year Period", + "vat_statement_id": False, + }, + ) + other_last_year_statement = self._get_statement( + other_last_year_period, + self.last_year_date, + other_tax_statement_account, + ) + self.assertTrue(other_last_year_statement) + + # Act: Create this Year's Statements, + # one for each Account used in previous Statements + today = fields.Date.today() + current_period = self.current_period + statement = self._get_statement( + current_period, + today, + tax_statement_account, + ) + + current_period.type_id.allow_overlap = True + other_current_period = current_period.copy( + default={ + "name": "Test Other current Period", + "vat_statement_id": False, + }, + ) + other_statement = self._get_statement( + other_current_period, + today, + other_tax_statement_account, + ) + + # Assert: Each one of this Year's Statements counts as previous Amount + # only the corresponding last Year's Statement + self.assertEqual( + statement.previous_credit_vat_amount, + -last_year_statement.authority_vat_amount, + ) + self.assertEqual( + other_statement.previous_credit_vat_amount, + -other_last_year_statement.authority_vat_amount, + )