From f5e9150d2d682167f373366bf628efe3823b9fef Mon Sep 17 00:00:00 2001 From: Alexis de Lattre Date: Mon, 23 Jan 2023 18:57:14 +0100 Subject: [PATCH] intrastat_product: default intrastat transaction on fiscal position AND company Odoo uses, by order of priority: 1) intrastat transaction of the invoice 2) default intrastat transaction of the fiscal position of the invoice 3) default intrastat transaction of the company --- .../models/account_fiscal_position.py | 16 +++++++ .../models/intrastat_product_declaration.py | 43 +++++++++++-------- intrastat_product/models/res_company.py | 32 ++++++++++++++ .../models/res_config_settings.py | 12 ++++++ .../views/intrastat_product_declaration.xml | 10 +++-- .../views/res_config_settings.xml | 31 +++++++++++++ 6 files changed, 122 insertions(+), 22 deletions(-) diff --git a/intrastat_product/models/account_fiscal_position.py b/intrastat_product/models/account_fiscal_position.py index ad86b3f..cdadfb8 100644 --- a/intrastat_product/models/account_fiscal_position.py +++ b/intrastat_product/models/account_fiscal_position.py @@ -11,18 +11,34 @@ class AccountFiscalPosition(models.Model): intrastat_out_invoice_transaction_id = fields.Many2one( comodel_name="intrastat.transaction", string="Default Intrastat Transaction For Customer Invoice", + help="Odoo uses the intrastat transaction of the invoice. " + "If not set, Odoo uses the default intrastat transaction of " + "the fiscal position of the invoice. " + "If not set, Odoo uses the default intrastat transaction of the company.", ) intrastat_out_refund_transaction_id = fields.Many2one( comodel_name="intrastat.transaction", string="Default Intrastat Transaction for Customer Refunds", + help="Odoo uses the intrastat transaction of the invoice. " + "If not set, Odoo uses the default intrastat transaction of " + "the fiscal position of the invoice. " + "If not set, Odoo uses the default intrastat transaction of the company.", ) intrastat_in_invoice_transaction_id = fields.Many2one( comodel_name="intrastat.transaction", string="Default Intrastat Transaction For Supplier Invoices", + help="Odoo uses the intrastat transaction of the invoice. " + "If not set, Odoo uses the default intrastat transaction of " + "the fiscal position of the invoice. " + "If not set, Odoo uses the default intrastat transaction of the company.", ) intrastat_in_refund_transaction_id = fields.Many2one( comodel_name="intrastat.transaction", string="Default Intrastat Transaction For Supplier Refunds", + help="Odoo uses the intrastat transaction of the invoice. " + "If not set, Odoo uses the default intrastat transaction of " + "the fiscal position of the invoice. " + "If not set, Odoo uses the default intrastat transaction of the company.", ) # field used to show/hide fields in country-specific modules company_country_code = fields.Char(related="company_id.country_id.code") diff --git a/intrastat_product/models/intrastat_product_declaration.py b/intrastat_product/models/intrastat_product_declaration.py index dea6641..63b1397 100644 --- a/intrastat_product/models/intrastat_product_declaration.py +++ b/intrastat_product/models/intrastat_product_declaration.py @@ -200,16 +200,21 @@ class IntrastatProductDeclaration(models.Model): @api.depends("declaration_line_ids.amount_company_currency") def _compute_numbers(self): - rg_res = self.env['intrastat.product.declaration.line'].read_group( - [('parent_id', 'in', self.ids)], - ['parent_id', 'amount_company_currency:sum'], - ['parent_id']) - mapped_data = dict( - [(x['parent_id'][0], - {'num_decl_lines': x['parent_id_count'], 'total_amount': x['amount_company_currency']}) for x in rg_res]) + rg_res = self.env["intrastat.product.declaration.line"].read_group( + [("parent_id", "in", self.ids)], + ["parent_id", "amount_company_currency:sum"], + ["parent_id"], + ) + mapped_data = { + x["parent_id"][0]: { + "num_decl_lines": x["parent_id_count"], + "total_amount": x["amount_company_currency"], + } + for x in rg_res + } for this in self: - this.num_decl_lines = mapped_data.get(this.id, {}).get('num_decl_lines', 0) - this.total_amount = mapped_data.get(this.id, {}).get('total_amount', 0) + this.num_decl_lines = mapped_data.get(this.id, {}).get("num_decl_lines", 0) + this.total_amount = mapped_data.get(this.id, {}).get("total_amount", 0) @api.constrains("year") def _check_year(self): @@ -303,18 +308,18 @@ class IntrastatProductDeclaration(models.Model): def _get_intrastat_transaction(self, inv_line, notedict): invoice = inv_line.move_id transaction = invoice.intrastat_transaction_id - if not transaction: + fieldmap = { + "out_invoice": "intrastat_out_invoice_transaction_id", + "out_refund": "intrastat_out_refund_transaction_id", + "in_invoice": "intrastat_in_invoice_transaction_id", + "in_refund": "intrastat_in_refund_transaction_id", + } + if not transaction and invoice.move_type in fieldmap: # as we have searched with intrastat_fiscal_position = True # we should always have a fiscal position on the invoice - fp = invoice.fiscal_position_id - if invoice.move_type == "out_invoice": - transaction = fp.intrastat_out_invoice_transaction_id - elif invoice.move_type == "out_refund": - transaction = fp.intrastat_out_refund_transaction_id - elif invoice.move_type == "in_invoice": - transaction = fp.intrastat_in_invoice_transaction_id - elif invoice.move_type == "in_refund": - transaction = fp.intrastat_in_refund_transaction_id + transaction = invoice.fiscal_position_id[fieldmap[invoice.move_type]] + if not transaction and invoice.move_type in fieldmap: + transaction = invoice.company_id[fieldmap[invoice.move_type]] if not transaction: line_notes = [ _( diff --git a/intrastat_product/models/res_company.py b/intrastat_product/models/res_company.py index 78e7f8a..b0af3ec 100644 --- a/intrastat_product/models/res_company.py +++ b/intrastat_product/models/res_company.py @@ -35,6 +35,38 @@ class ResCompany(models.Model): intrastat_region_id = fields.Many2one( comodel_name="intrastat.region", string="Default Intrastat Region" ) + intrastat_out_invoice_transaction_id = fields.Many2one( + comodel_name="intrastat.transaction", + string="Default Intrastat Transaction For Customer Invoice", + help="Odoo uses the intrastat transaction of the invoice. " + "If not set, Odoo uses the default intrastat transaction of " + "the fiscal position of the invoice. " + "If not set, Odoo uses the default intrastat transaction of the company.", + ) + intrastat_out_refund_transaction_id = fields.Many2one( + comodel_name="intrastat.transaction", + string="Default Intrastat Transaction for Customer Refunds", + help="Odoo uses the intrastat transaction of the invoice. " + "If not set, Odoo uses the default intrastat transaction of " + "the fiscal position of the invoice. " + "If not set, Odoo uses the default intrastat transaction of the company.", + ) + intrastat_in_invoice_transaction_id = fields.Many2one( + comodel_name="intrastat.transaction", + string="Default Intrastat Transaction For Supplier Invoices", + help="Odoo uses the intrastat transaction of the invoice. " + "If not set, Odoo uses the default intrastat transaction of " + "the fiscal position of the invoice. " + "If not set, Odoo uses the default intrastat transaction of the company.", + ) + intrastat_in_refund_transaction_id = fields.Many2one( + comodel_name="intrastat.transaction", + string="Default Intrastat Transaction For Supplier Refunds", + help="Odoo uses the intrastat transaction of the invoice. " + "If not set, Odoo uses the default intrastat transaction of " + "the fiscal position of the invoice. " + "If not set, Odoo uses the default intrastat transaction of the company.", + ) intrastat_accessory_costs = fields.Boolean( string="Include Accessory Costs in Fiscal Value of Product" ) diff --git a/intrastat_product/models/res_config_settings.py b/intrastat_product/models/res_config_settings.py index f48d6a7..01a6688 100644 --- a/intrastat_product/models/res_config_settings.py +++ b/intrastat_product/models/res_config_settings.py @@ -21,6 +21,18 @@ class ResConfigSettings(models.TransientModel): intrastat_region_id = fields.Many2one( related="company_id.intrastat_region_id", readonly=False ) + intrastat_out_invoice_transaction_id = fields.Many2one( + related="company_id.intrastat_out_invoice_transaction_id", readonly=False + ) + intrastat_out_refund_transaction_id = fields.Many2one( + related="company_id.intrastat_out_refund_transaction_id", readonly=False + ) + intrastat_in_invoice_transaction_id = fields.Many2one( + related="company_id.intrastat_in_invoice_transaction_id", readonly=False + ) + intrastat_in_refund_transaction_id = fields.Many2one( + related="company_id.intrastat_in_refund_transaction_id", readonly=False + ) intrastat_accessory_costs = fields.Boolean( related="company_id.intrastat_accessory_costs", readonly=False ) diff --git a/intrastat_product/views/intrastat_product_declaration.xml b/intrastat_product/views/intrastat_product_declaration.xml index 0d5e309..413ddd5 100644 --- a/intrastat_product/views/intrastat_product_declaration.xml +++ b/intrastat_product/views/intrastat_product_declaration.xml @@ -311,8 +311,12 @@ optional="show" attrs="{'column_invisible': [('parent.reporting_level', '!=', 'extended')]}" /> - - + + - + +
+
+
+
+
+
+
+