From de1d1c147b5c4c39d4c97cfcdca0bf7d8a70e620 Mon Sep 17 00:00:00 2001 From: Alexis de Lattre Date: Thu, 6 Feb 2025 12:05:43 +0100 Subject: [PATCH 1/2] [IMP] intrastat_base: add field intrastat_type on product.template --- intrastat_base/models/product_template.py | 38 +++++++++++++++++------ intrastat_base/views/product_template.xml | 5 ++- 2 files changed, 32 insertions(+), 11 deletions(-) diff --git a/intrastat_base/models/product_template.py b/intrastat_base/models/product_template.py index f4c3ae8db..3229f9980 100644 --- a/intrastat_base/models/product_template.py +++ b/intrastat_base/models/product_template.py @@ -9,27 +9,45 @@ class ProductTemplate(models.Model): _inherit = "product.template" + intrastat_type = fields.Selection( + [ + ("product", "Product"), + ("service", "Service"), + ], + compute="_compute_intrastat_type", + store=True, + help="Type of product used for the intrastat declarations.", + ) is_accessory_cost = fields.Boolean( help="Activate this option for shipping costs, packaging " "costs and all services related to the sale of products. " "This option is used for Intrastat reports.", ) - @api.constrains("type", "is_accessory_cost") + @api.depends("type", "combo_ids.combo_item_ids.product_id.type") + def _compute_intrastat_type(self): + for this in self: + intrastat_type = "service" + if this.type == "consu": + intrastat_type = "product" + elif this.type == "combo": + for combo in this.combo_ids: + for item in combo.combo_item_ids: + if item.product_id.type == "consu": + intrastat_type = "product" + break + this.intrastat_type = intrastat_type + + @api.constrains("intrastat_type", "is_accessory_cost") def _check_accessory_cost(self): for this in self: - if this.is_accessory_cost and this.type != "service": + if this.is_accessory_cost and this.intrastat_type != "service": raise ValidationError( _( - "The option 'Is accessory cost?' should only be " + "The option 'Is accessory cost?' can only be " "activated on 'Service' products. You have activated " "this option for the product '%(product_name)s' which is " - "configured with type '%(product_type)s'." + "not a service product.", + product_name=this.display_name, ) - % { - "product_name": this.display_name, - "product_type": this._fields["type"].convert_to_export( - this.type, this - ), - } ) diff --git a/intrastat_base/views/product_template.xml b/intrastat_base/views/product_template.xml index be100a5c0..7ad057735 100644 --- a/intrastat_base/views/product_template.xml +++ b/intrastat_base/views/product_template.xml @@ -10,10 +10,13 @@ product.template + + + + localisation module to set invisible="intrastat_type != 'service'" --> From aa4607734150d605cebb48a755efa0b5059e49ea Mon Sep 17 00:00:00 2001 From: Alexis de Lattre Date: Thu, 6 Feb 2025 19:26:33 +0100 Subject: [PATCH 2/2] [IMP] intrastat_base: is_accessory_cost is now a computed field --- intrastat_base/models/product_template.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/intrastat_base/models/product_template.py b/intrastat_base/models/product_template.py index 3229f9980..a09791942 100644 --- a/intrastat_base/models/product_template.py +++ b/intrastat_base/models/product_template.py @@ -16,9 +16,14 @@ class ProductTemplate(models.Model): ], compute="_compute_intrastat_type", store=True, + precompute=True, help="Type of product used for the intrastat declarations.", ) is_accessory_cost = fields.Boolean( + compute="_compute_is_accessory_cost", + store=True, + precompute=True, + readonly=False, help="Activate this option for shipping costs, packaging " "costs and all services related to the sale of products. " "This option is used for Intrastat reports.", @@ -38,6 +43,12 @@ def _compute_intrastat_type(self): break this.intrastat_type = intrastat_type + @api.depends("intrastat_type") + def _compute_is_accessory_cost(self): + for this in self: + if this.intrastat_type != "service": + this.is_accessory_cost = False + @api.constrains("intrastat_type", "is_accessory_cost") def _check_accessory_cost(self): for this in self: