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

[IMP] Liquidazione IVA divisa per attività #4356

Open
wants to merge 5 commits 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
2 changes: 2 additions & 0 deletions account_vat_period_end_statement/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# Copyright 2015 Associazione Odoo Italia (<http://www.odoo-italia.org>)
# Copyright 2021 Gianmarco Conte
# - Dinamiche Aziendali Srl (<www.dinamicheaziendali.it>)
# Copyright 2024 Simone Rubino - Aion Tech
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

{
Expand All @@ -16,6 +17,7 @@
"website": "https://github.com/OCA/l10n-italy",
"depends": [
"account",
"account_journal_group_included",
"account_tax_balance",
"date_range",
"l10n_it_account",
Expand Down
37 changes: 37 additions & 0 deletions account_vat_period_end_statement/models/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

import math
from contextlib import contextmanager, nullcontext

from odoo import api, fields, models
from odoo.exceptions import UserError
Expand Down Expand Up @@ -347,13 +348,45 @@ def _get_default_interest_percent(self):
string="Accounts filter",
domain=lambda self: self._get_domain_account(),
)
journal_group_ids = fields.Many2many(
comodel_name="account.journal.group",
string="Journal groups",
help="The report will show amounts for the groups and journals selected.",
)

def _get_domain_account(self):
domain = [("vat_statement_account_id", "!=", False)]
tax_ids = self.env["account.tax"].search(domain)
account_ids = tax_ids.mapped("vat_statement_account_id")
return [("id", "in", account_ids.ids)]

@contextmanager
def _recompute_amounts_on_change(self, field_name):
"""Recompute statement amounts when `field_name` changes."""
statement_to_previous_value = {
statement: statement[field_name] for statement in self
}

yield

statements_to_recompute = self.browse()
for statement in self:
previous_value = statement_to_previous_value.get(statement)
if statement[field_name] != previous_value:
statements_to_recompute |= statement

if statements_to_recompute:
statements_to_recompute.compute_amounts()

def write(self, vals):
if "journal_group_ids" in vals:
recompute_context = self._recompute_amounts_on_change("journal_group_ids")
else:
recompute_context = nullcontext()

with recompute_context:
return super().write(vals)

def unlink(self):
for statement in self:
if statement.state == "confirmed" or statement.state == "paid":
Expand Down Expand Up @@ -740,12 +773,14 @@ def compute_amounts(self):

def _set_debit_lines(self, debit_tax, debit_line_ids, statement):
total = 0.0
selected_journals = statement.journal_group_ids.included_journal_ids
for period in statement.date_range_ids:
total += debit_tax._compute_totals_tax(
{
"from_date": period.date_start,
"to_date": period.date_end,
"registry_type": "customer",
"journal_ids": selected_journals.ids,
}
)[3] # position 3 is deductible part
debit_line_ids.append(
Expand All @@ -758,12 +793,14 @@ def _set_debit_lines(self, debit_tax, debit_line_ids, statement):

def _set_credit_lines(self, credit_tax, credit_line_ids, statement):
total = 0.0
selected_journals = statement.journal_group_ids.included_journal_ids
for period in statement.date_range_ids:
total += credit_tax._compute_totals_tax(
{
"from_date": period.date_start,
"to_date": period.date_end,
"registry_type": "supplier",
"journal_ids": selected_journals.ids,
}
)[3] # position 3 is deductible part
credit_line_ids.append(
Expand Down
30 changes: 29 additions & 1 deletion account_vat_period_end_statement/report/vat_statement.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# Copyright 2015 Associazione OpenERP Italia (<http://www.openerp-italia.org>)
# Copyright 2015 Openforce di Alessandro Camilli (<http://www.openforce.it>)
# Copyright 2015 Link It S.p.a. (<http://www.linkgroup.it/>)
# Copyright 2024 Simone Rubino - Aion Tech
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

import time
Expand All @@ -26,6 +27,8 @@ def _get_report_values(self, docids, data=None):
"account_vat_amounts": self._get_account_vat_amounts,
"formatLang": formatLang,
"env": self.env,
"sum_tax_code_amounts": self.sum_tax_code_amounts,
"all_journal_groups": self.env["account.journal.group"].search([]),
}
return vals

Expand All @@ -36,9 +39,32 @@ def _get_statement(self, statement_id):
statement = statement_obj.browse(statement_id)
return statement

def _get_taxes_amounts(self, period_id, tax_ids=None, registry_type="customer"):
@api.model
def sum_tax_code_amounts(self, tax_code_amounts, other_tax_code_amounts):
"""Sum two tax amounts dictionaries."""
sum_tax_code_amounts = tax_code_amounts.copy()
for code, amounts in other_tax_code_amounts.items():
sum_amounts = sum_tax_code_amounts.get(code)
if sum_amounts is None:
sum_tax_code_amounts[code] = amounts
else:
for amount_name in sum_amounts:
if amount_name in [
"vat",
"vat_deductible",
"vat_undeductible",
"base",
]:
sum_amounts[amount_name] += amounts[amount_name]
return sum_tax_code_amounts

def _get_taxes_amounts(
self, period_id, tax_ids=None, registry_type="customer", journal_ids=None
):
if tax_ids is None:
tax_ids = []
if journal_ids is None:
journal_ids = []
res = {}
date_range = self.env["date.range"].browse(period_id)
tax_model = self.env["account.tax"]
Expand All @@ -50,6 +76,7 @@ def _get_taxes_amounts(self, period_id, tax_ids=None, registry_type="customer"):
"from_date": date_range.date_start,
"to_date": date_range.date_end,
"registry_type": registry_type,
"journal_ids": journal_ids,
}
)

Expand All @@ -63,6 +90,7 @@ def _get_taxes_amounts(self, period_id, tax_ids=None, registry_type="customer"):
"from_date": date_range.date_start,
"to_date": date_range.date_end,
"registry_type": registry_type,
"journal_ids": journal_ids,
}
)
# return tax_name, base, tax_val, deductible, undeductible
Expand Down
101 changes: 100 additions & 1 deletion account_vat_period_end_statement/tests/test_vat_statement.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import re

from odoo import fields
from odoo import Command, fields
from odoo.tests import Form, tagged

from .common import TestVATStatementCommon
Expand Down Expand Up @@ -328,3 +328,102 @@ def test_report_multiple_payment_info(self):
report_content.decode(),
)
self.assertEqual(len(payment_infos), 1)

def test_report_journal_groups(self):
"""Journal groups are shown in the report."""
date_range = self.current_period
bill = self.init_invoice(
"in_invoice",
invoice_date=self.recent_date,
amounts=[500],
taxes=self.account_tax_22_credit,
post=True,
)
bill_journal = bill.journal_id
invoice = self.init_invoice(
"out_invoice",
invoice_date=self.recent_date,
amounts=[1000],
taxes=self.account_tax_22,
post=True,
)
invoice_journal = invoice.journal_id
journal_group = self.env["account.journal.group"].create(
{
"name": "Test group",
"included_journal_ids": [
Command.set((bill_journal + invoice_journal).ids),
],
}
)
statement = self._get_statement(
date_range,
self.last_year_date,
self.env["account.account"].browse(),
)
statement.journal_group_ids = journal_group
# pre-condition
self.assertEqual(
statement.journal_group_ids.included_journal_ids,
bill_journal + invoice_journal,
)

# Act
report_content, report_type = self.env["ir.actions.report"]._render_qweb_html(
"account_vat_period_end_statement.report_vat_statement", statement.ids
)

# Assert
report_content = report_content.decode()
self.assertIn(bill_journal.name, report_content)
self.assertIn(invoice_journal.name, report_content)
self.assertIn(journal_group.name, report_content)

def test_recompute_amounts_for_journal_groups(self):
"""Statement amounts are recomputed based on the selected groups."""
# Arrange
self.init_invoice(
"in_invoice",
invoice_date=self.recent_date,
amounts=[500],
taxes=self.account_tax_22_credit,
post=True,
)
self.init_invoice(
"out_invoice",
invoice_date=self.recent_date,
amounts=[1000],
taxes=self.account_tax_22,
post=True,
)

new_journal_form = Form(self.env["account.journal"])
new_journal_form.name = "Test Journal"
new_journal_form.code = "TJ"
new_journal_form.type = "general"
new_journal = new_journal_form.save()

journal_group = self.env["account.journal.group"].create(
{
"name": "Test group",
"included_journal_ids": [
Command.set(new_journal.ids),
],
}
)
statement = self._get_statement(
self.current_period,
self.last_year_date,
self.env["account.account"].browse(),
)
# pre-condition
self.assertFalse(statement.journal_group_ids)
self.assertTrue(sum(statement.credit_vat_account_line_ids.mapped("amount")))
self.assertTrue(sum(statement.debit_vat_account_line_ids.mapped("amount")))

# Act
statement.journal_group_ids = journal_group

# Assert
self.assertFalse(sum(statement.credit_vat_account_line_ids.mapped("amount")))
self.assertFalse(sum(statement.debit_vat_account_line_ids.mapped("amount")))
10 changes: 10 additions & 0 deletions account_vat_period_end_statement/views/account_view.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8" ?>
<!--
Copyright 2019 Simone Rubino - Agile Business Group
Copyright 2024 Simone Rubino - Aion Tech
License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
-->
<odoo>
Expand Down Expand Up @@ -63,6 +64,15 @@
<field name="company_id" groups="base.group_multi_company" />
<field name="interest" />
<field name="account_ids" widget="many2many_tags" />
<field name="journal_group_ids">
<tree create="false" delete="false" edit="false" no_open="true">
<field name="name" />
<field
name="included_journal_ids"
widget="many2many_tags"
/>
</tree>
</field>
<field
name="interest_percent"
attrs="{'invisible': [('interest', '=', False)], 'required': [('interest', '=', True)]}"
Expand Down
Loading
Loading