Skip to content

Commit

Permalink
[ADD][14.0] Add check for invoice change compatibility to account_vat…
Browse files Browse the repository at this point in the history
…_period_end_statement
  • Loading branch information
sergiocorato committed Aug 28, 2024
1 parent 0eaeb44 commit 6ddc9c4
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 0 deletions.
2 changes: 2 additions & 0 deletions account_vat_period_end_statement/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@

from . import config
from . import account
from . import account_move
from . import account_move_line
11 changes: 11 additions & 0 deletions account_vat_period_end_statement/models/account_move.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from odoo import models


class AccountMove(models.Model):
_inherit = "account.move"

def write(self, vals):
for move in self:
if "date" in vals and move.date != vals["date"]:
move.line_ids._check_tax_statement()
return super().write(vals)
71 changes: 71 additions & 0 deletions account_vat_period_end_statement/models/account_move_line.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
from odoo import _, models
from odoo.exceptions import UserError
from odoo.tools.misc import format_date


class AccountMoveLine(models.Model):
_inherit = "account.move.line"

def _check_tax_statement(self):
for line in self.filtered(lambda _l: _l.move_id.posted_before):
move = line.move_id
if not move.is_invoice():
continue

Check warning on line 13 in account_vat_period_end_statement/models/account_move_line.py

View check run for this annotation

Codecov / codecov/patch

account_vat_period_end_statement/models/account_move_line.py#L13

Added line #L13 was not covered by tests
invoice_account_vat_ids = line.filtered(lambda x: x.tax_line_id).mapped(
"tax_line_id.vat_statement_account_id"
)
if not invoice_account_vat_ids:
continue
invoice_date_range_ids = self.env["date.range"].search(
[
("date_start", "<=", move.date),
("date_end", ">=", move.date),
]
)
if not invoice_date_range_ids:
continue

Check warning on line 26 in account_vat_period_end_statement/models/account_move_line.py

View check run for this annotation

Codecov / codecov/patch

account_vat_period_end_statement/models/account_move_line.py#L26

Added line #L26 was not covered by tests
vat_statement_obj = self.env["account.vat.period.end.statement"]
vat_statements = vat_statement_obj.search(
[
("date_range_ids", "in", invoice_date_range_ids.ids),
("state", "!=", "draft"),
"|",
(
"credit_vat_account_line_ids.account_id",
"in",
invoice_account_vat_ids.ids,
),
(
"debit_vat_account_line_ids.account_id",
"in",
invoice_account_vat_ids.ids,
),
]
)
if vat_statements:
raise UserError(
_(
"The operation is refused as it would impact already issued "
"tax statements on %s.\n"
"Please restore the journal entry date or reset VAT statement "
"to draft to proceed."
)
% (
" - ".join(
format_date(self.env, x.date) for x in vat_statements
)
)
)

def write(self, vals):
for line in self:
if (
"account_id" in vals
and line.account_id.id != vals["account_id"]
or "credit" in vals
and line.credit != vals["credit"]
or "debit" in vals
and line.debit != vals["debit"]
):
line._check_tax_statement()
return super().write(vals)
65 changes: 65 additions & 0 deletions account_vat_period_end_statement/tests/test_vat_statement.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@
from dateutil.rrule import MONTHLY

from odoo import fields
from odoo.exceptions import UserError
from odoo.tests import tagged
from odoo.tests.common import Form
from odoo.tools.date_utils import relativedelta
from odoo.tools.misc import format_date

from odoo.addons.account.tests.common import AccountTestInvoicingCommon

Expand Down Expand Up @@ -164,6 +167,16 @@ def test_vat_statement(self):
)
.id
)
expenses_invoice_line_account = self.env["account.account"].search(
[
(
"user_type_id",
"=",
self.env.ref("account.data_account_type_expenses").id,
)
],
limit=1,
)

out_invoice = self.invoice_model.create(
{
Expand Down Expand Up @@ -280,6 +293,58 @@ def test_vat_statement(self):
self.assertEqual(line.debit, 100)
self.assertTrue(vat_auth_found)
# TODO payment
# test account for an invoice line is writable
out_invoice.button_draft()
invoice_line = out_invoice.line_ids.filtered(
lambda x: x.account_id.id == in_invoice_line_account
)[0]
invoice_line.account_id = expenses_invoice_line_account
# test account for a tax line is not writable
tax_line = out_invoice.line_ids.filtered(lambda x: x.tax_line_id)[0]
with self.assertRaises(UserError) as ue:
tax_line.account_id = expenses_invoice_line_account
error_message = (
"The operation is refused as it would impact already issued "
"tax statements on %s.\n"
"Please restore the journal entry date or reset VAT statement "
"to draft to proceed."
) % format_date(self.env, self.vat_statement.date)
self.assertEqual(ue.exception.args[0], error_message)
# test invoice date cannot be changed
with self.assertRaises(UserError) as ue:
out_invoice.date = out_invoice.date + relativedelta(days=1)
self.assertEqual(ue.exception.args[0], error_message)
# test an invoice in a date range without VAT statement can be modified
out_invoice = self.invoice_model.create(
{
"invoice_date": self.current_period.date_end + relativedelta(days=1),
"journal_id": self.sale_journal.id,
"partner_id": self.env.ref("base.res_partner_3").id,
"move_type": "out_invoice",
"invoice_line_ids": [
(
0,
0,
{
"name": "service",
"price_unit": 100,
"quantity": 1,
"tax_ids": [(6, 0, [self.account_tax_22.id])],
},
)
],
}
)
out_invoice._recompute_tax_lines()
out_invoice.action_post()
out_invoice.button_draft()
invoice_line = out_invoice.line_ids.filtered(
lambda x: x.account_id.id == in_invoice_line_account
)[0]
invoice_line.account_id = expenses_invoice_line_account
tax_line = out_invoice.line_ids.filtered(lambda x: x.tax_line_id)[0]
tax_line.account_id = expenses_invoice_line_account
out_invoice.date = out_invoice.date + relativedelta(days=1)

def _create_vendor_bill(self, partner, invoice_date, price_unit, tax):
"""
Expand Down

0 comments on commit 6ddc9c4

Please sign in to comment.