Skip to content

Commit 0adf792

Browse files
committed
[FIX] account_vat_period_end_statement: Select previous Statement by Account
1 parent 03aeafe commit 0adf792

File tree

3 files changed

+153
-4
lines changed

3 files changed

+153
-4
lines changed

account_vat_period_end_statement/models/account.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Copyright 2011-2012 Domsense s.r.l. (<http://www.domsense.com>).
22
# Copyright 2012-15 Agile Business Group sagl (<http://www.agilebg.com>)
33
# Copyright 2015 Associazione Odoo Italia (<http://www.odoo-italia.org>)
4+
# Copyright 2022 Simone Rubino - TAKOBI
45
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
56

67
import math
@@ -536,16 +537,28 @@ def create_move(self):
536537

537538
return True
538539

540+
def _get_previous_statements(self):
541+
self.ensure_one()
542+
prev_statements = self.search(
543+
[
544+
('date', '<', self.date),
545+
('annual', '=', False),
546+
],
547+
order='date desc',
548+
)
549+
prev_statements_same_accounts = prev_statements.filtered(
550+
lambda s: s.account_ids == self.account_ids
551+
)
552+
return prev_statements_same_accounts
553+
539554
@api.multi
540555
def compute_amounts(self):
541556
decimal_precision_obj = self.env['decimal.precision']
542557
debit_line_model = self.env['statement.debit.account.line']
543558
credit_line_model = self.env['statement.credit.account.line']
544559
for statement in self:
545560
statement.previous_debit_vat_amount = 0.0
546-
prev_statements = self.search(
547-
[('date', '<', statement.date), ('annual', '=', False)],
548-
order='date desc')
561+
prev_statements = statement._get_previous_statements()
549562
if prev_statements and not statement.annual:
550563
prev_statement = prev_statements[0]
551564
if (

account_vat_period_end_statement/readme/CONTRIBUTORS.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,6 @@
99
* Giacomo Grasso <[email protected]>
1010
* Lara Baggio <http://linkgroup.it/>
1111
* Gianmarco Conte <[email protected]>
12+
* `TAKOBI <https://takobi.online>`_:
1213

14+
* Simone Rubino <[email protected]>

account_vat_period_end_statement/tests/test_vat_statement.py

Lines changed: 135 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
# Copyright 2015 Agile Business Group <http://www.agilebg.com>
2+
# Copyright 2022 Simone Rubino - TAKOBI
23
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
34

4-
from odoo.tests.common import TransactionCase
5+
from odoo import fields
6+
from odoo.tests.common import TransactionCase, Form
57
from datetime import datetime, date
68
from dateutil.rrule import MONTHLY
79

@@ -227,3 +229,135 @@ def test_vat_statement(self):
227229
self.assertTrue(self.vat_statement.move_id)
228230
self.assertEqual(self.vat_statement.move_id.amount, 122)
229231
# TODO payment
232+
233+
def _create_vendor_bill(self, partner, date_invoice, price_unit, tax):
234+
"""
235+
Create an open Vendor Bill for `partner` having date `date_invoice`.
236+
The Bill will also have a Line having Price `price_unit` and Tax `tax`.
237+
"""
238+
bill_model = self.invoice_model.with_context(
239+
type='in_invoice',
240+
)
241+
bill_form = Form(bill_model)
242+
bill_form.partner_id = partner
243+
bill_form.date_invoice = date_invoice
244+
with bill_form.invoice_line_ids.new() as line:
245+
line.invoice_line_tax_ids.clear()
246+
line.invoice_line_tax_ids.add(tax)
247+
line.name = "Test Invoice Line"
248+
line.price_unit = price_unit
249+
bill = bill_form.save()
250+
bill.action_invoice_open()
251+
return bill
252+
253+
def _get_statement(self, period, statement_date, accounts):
254+
"""
255+
Create a VAT Statement in date `statement_date`
256+
for Period `period` and Accounts `accounts`.
257+
"""
258+
# Create statement
259+
statement_form = Form(self.vat_statement_model)
260+
statement_form.journal_id = self.general_journal
261+
statement_form.authority_vat_account_id = self.vat_authority
262+
statement_form.payment_term_id = self.account_payment_term
263+
statement_form.date = statement_date
264+
statement_form.account_ids.clear()
265+
for account in accounts:
266+
statement_form.account_ids.add(account)
267+
statement = statement_form.save()
268+
269+
# Add period
270+
add_period_model = self.env['add.period.to.vat.statement']
271+
add_period_model = add_period_model \
272+
.with_context(
273+
active_id=statement.id,
274+
active_model=statement._name,
275+
)
276+
add_period_form = Form(add_period_model)
277+
add_period_form.period_id = period
278+
add_period = add_period_form.save()
279+
add_period.add_period()
280+
return statement
281+
282+
def test_different_previous_vat_statements(self):
283+
"""
284+
Statements for different Accounts
285+
only count previous Amounts from Statements with the same Accounts.
286+
"""
287+
# Arrange: Create two different VAT Statements
288+
# selecting two different Accounts
289+
partner = self.env.ref('base.res_partner_4')
290+
tax = self.account_tax_22_credit
291+
tax_statement_account = tax.vat_statement_account_id
292+
last_year_bill = self._create_vendor_bill(
293+
partner, self.last_year_recent_date,
294+
10, tax,
295+
)
296+
self.assertEqual(last_year_bill.state, 'open')
297+
last_year_period = self.last_year_period
298+
last_year_statement = self._get_statement(
299+
last_year_period,
300+
self.last_year_date,
301+
tax_statement_account,
302+
)
303+
self.assertTrue(last_year_statement)
304+
305+
# Create another Bill and Statement
306+
other_tax_statement_account = tax_statement_account.copy()
307+
other_tax = tax.copy(
308+
default={
309+
'vat_statement_account_id': other_tax_statement_account.id,
310+
},
311+
)
312+
other_last_year_bill = self._create_vendor_bill(
313+
partner, self.last_year_recent_date,
314+
20, other_tax,
315+
)
316+
self.assertEqual(other_last_year_bill.state, 'open')
317+
last_year_period.type_id.allow_overlap = True
318+
other_last_year_period = last_year_period.copy(
319+
default={
320+
'name': "Test Other last Year Period",
321+
'vat_statement_id': False,
322+
},
323+
)
324+
other_last_year_statement = self._get_statement(
325+
other_last_year_period,
326+
self.last_year_date,
327+
other_tax_statement_account,
328+
)
329+
self.assertTrue(other_last_year_statement)
330+
331+
# Act: Create this Year's Statements,
332+
# one for each Account used in previous Statements
333+
today = fields.Date.today()
334+
current_period = self.current_period
335+
statement = self._get_statement(
336+
current_period,
337+
today,
338+
tax_statement_account,
339+
)
340+
341+
current_period.type_id.allow_overlap = True
342+
other_current_period = current_period.copy(
343+
default={
344+
'name': "Test Other current Period",
345+
'vat_statement_id': False,
346+
},
347+
)
348+
other_statement = self._get_statement(
349+
other_current_period,
350+
today,
351+
other_tax_statement_account,
352+
)
353+
354+
# Assert: Each one of this Year's Statements counts as previous Amount
355+
# only the corresponding last Year's Statement
356+
self.assertEqual(
357+
statement.previous_credit_vat_amount,
358+
-last_year_statement.authority_vat_amount,
359+
)
360+
self.assertEqual(
361+
other_statement.previous_credit_vat_amount,
362+
-other_last_year_statement.authority_vat_amount,
363+
)

0 commit comments

Comments
 (0)