diff --git a/l10n_it_intrastat/__manifest__.py b/l10n_it_intrastat/__manifest__.py index 96033eee518b..2a4ce62fba5b 100644 --- a/l10n_it_intrastat/__manifest__.py +++ b/l10n_it_intrastat/__manifest__.py @@ -4,7 +4,7 @@ { "name": "ITA - Intrastat", - "version": "14.0.1.4.2", + "version": "14.0.1.4.3", "category": "Account", "summary": "Riclassificazione merci e servizi per dichiarazioni Intrastat", "author": "Openforce, Link IT srl, Agile Business Group, " diff --git a/l10n_it_intrastat/migrations/14.0.1.4.3/pre-migrate.py b/l10n_it_intrastat/migrations/14.0.1.4.3/pre-migrate.py new file mode 100644 index 000000000000..a6e92fe495fe --- /dev/null +++ b/l10n_it_intrastat/migrations/14.0.1.4.3/pre-migrate.py @@ -0,0 +1,34 @@ +from openupgradelib import openupgrade +from openupgradelib.openupgrade_tools import column_exists + + +@openupgrade.migrate() +def migrate(env, version): + for table, column in [ + ("account_fiscal_position", "intrastat_sale"), + ("account_fiscal_position", "intrastat_purchase"), + ]: + if not column_exists(env.cr, table, column): + openupgrade.add_fields( + env, + [ + ( + column, + ".".join(table.split("_")), + False, + "boolean", + False, + "l10n_it_intrastat", + ) + ], + ) + openupgrade.logged_query( + env.cr, + """ +UPDATE account_fiscal_position +SET + intrastat_sale = intrastat, + intrastat_purchase = intrastat +WHERE intrastat is true; + """, + ) diff --git a/l10n_it_intrastat/models/account.py b/l10n_it_intrastat/models/account.py index 8b25c7cebf21..2362eec4faed 100644 --- a/l10n_it_intrastat/models/account.py +++ b/l10n_it_intrastat/models/account.py @@ -9,7 +9,8 @@ class AccountFiscalPosition(models.Model): _inherit = "account.fiscal.position" - intrastat = fields.Boolean(string="Subject to Intrastat") + intrastat_purchase = fields.Boolean(string="Subject to Intrastat (purchases)") + intrastat_sale = fields.Boolean(string="Subject to Intrastat (sales)") class AccountMoveLine(models.Model): @@ -299,7 +300,12 @@ class AccountMove(models.Model): @api.onchange("fiscal_position_id") def change_fiscal_position(self): - self.intrastat = self.fiscal_position_id.intrastat + self.intrastat = ( + self.is_sale_document() + and self.fiscal_position_id.intrastat_sale + or self.is_purchase_document() + and self.fiscal_position_id.intrastat_purchase + ) @api.onchange("partner_id") def _onchange_partner_id(self): @@ -310,13 +316,24 @@ def _onchange_partner_id(self): @api.model_create_multi def create(self, vals_list): for val in vals_list: - if "intrastat" not in val and "fiscal_position_id" in val: - intrastat = ( - self.env["account.fiscal.position"] - .browse(val["fiscal_position_id"]) - .intrastat + if "fiscal_position_id" in val: + fiscal_position = self.env["account.fiscal.position"].browse( + val["fiscal_position_id"] ) - val.update({"intrastat": intrastat}) + if "intrastat" not in val: + intrastat = False + if "move_type" in val and val.get("move_type"): + if ( + val.get("move_type") + in self.env["account.move"].get_sale_types() + ): + intrastat = fiscal_position.intrastat_sale + elif ( + val.get("move_type") + in self.env["account.move"].get_purchase_types() + ): + intrastat = fiscal_position.intrastat_purchase + val.update({"intrastat": intrastat}) return super().create(vals_list) def action_post(self): diff --git a/l10n_it_intrastat/tests/test_intrastat.py b/l10n_it_intrastat/tests/test_intrastat.py index 763d3719e1cd..05572d79ddbb 100644 --- a/l10n_it_intrastat/tests/test_intrastat.py +++ b/l10n_it_intrastat/tests/test_intrastat.py @@ -65,15 +65,16 @@ def test_invoice_totals(self): ) self.assertEqual(total_intrastat_amount, invoice.amount_untaxed) - def test_invoice_fiscal_postion(self): + def _test_invoice_fiscal_position(self, intrastat_type): self.partner01.property_account_position_id = self.fp_model.create( { - "name": "F.P subjected to intrastat", - "intrastat": True, + "name": "F.P subjected to intrastat on %s" % intrastat_type, + "intrastat_sale": True if intrastat_type == "sale" else False, + "intrastat_purchase": True if intrastat_type == "purchase" else False, } ) invoice = self.init_invoice( - "out_invoice", + "out_invoice" if intrastat_type == "sale" else "in_invoice", partner=self.partner01, products=self.product01, taxes=self.tax22, @@ -82,3 +83,20 @@ def test_invoice_fiscal_postion(self): invoice.action_post() invoice.compute_intrastat_lines() self.assertEqual(invoice.intrastat, True) + + invoice_purchase = self.init_invoice( + "in_invoice" if intrastat_type == "sale" else "out_invoice", + partner=self.partner01, + products=self.product01, + taxes=self.tax22, + ) + # Compute intrastat lines + invoice_purchase.action_post() + invoice_purchase.compute_intrastat_lines() + self.assertEqual(invoice_purchase.intrastat, False) + + def test_invoice_fiscal_position_sale(self): + self._test_invoice_fiscal_position("sale") + + def test_invoice_fiscal_position_purchase(self): + self._test_invoice_fiscal_position("purchase") diff --git a/l10n_it_intrastat/views/account.xml b/l10n_it_intrastat/views/account.xml index fb58f0e6f838..c4b737c41fc0 100644 --- a/l10n_it_intrastat/views/account.xml +++ b/l10n_it_intrastat/views/account.xml @@ -11,7 +11,8 @@ - + +