From 96ee09cba36b2ee19dea140ef658fa5a96a49f76 Mon Sep 17 00:00:00 2001 From: "chafique.delli" Date: Thu, 30 Nov 2023 20:10:34 +0100 Subject: [PATCH] code refactor and simplification --- sale_certificat_typology/i18n/fr.po | 28 +---- sale_certificat_typology/models/product.py | 29 +---- sale_certificat_typology/models/sale_order.py | 116 +++++------------- sale_certificat_typology/views/sale_order.xml | 15 ++- 4 files changed, 48 insertions(+), 140 deletions(-) diff --git a/sale_certificat_typology/i18n/fr.po b/sale_certificat_typology/i18n/fr.po index 64e99185..53154273 100644 --- a/sale_certificat_typology/i18n/fr.po +++ b/sale_certificat_typology/i18n/fr.po @@ -6,8 +6,8 @@ msgid "" msgstr "" "Project-Id-Version: Odoo Server 14.0\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2023-10-05 08:44+0000\n" -"PO-Revision-Date: 2023-10-05 08:44+0000\n" +"POT-Creation-Date: 2023-12-01 18:22+0000\n" +"PO-Revision-Date: 2023-12-01 18:22+0000\n" "Last-Translator: \n" "Language-Team: \n" "MIME-Version: 1.0\n" @@ -83,11 +83,6 @@ msgstr "Justificatifs: Supprimer les documents stockés" msgid "Company" msgstr "Société" -#. module: sale_certificat_typology -#: model:ir.model.fields,field_description:sale_certificat_typology.field_sale_order_certificat_item__product_ids -msgid "Concerned products" -msgstr "Produits concernés" - #. module: sale_certificat_typology #: model:ir.model.fields,field_description:sale_certificat_typology.field_certificat_typology__create_uid #: model:ir.model.fields,field_description:sale_certificat_typology.field_sale_order_certificat_item__create_uid @@ -112,7 +107,6 @@ msgstr "" #. module: sale_certificat_typology #: model:ir.model.fields,field_description:sale_certificat_typology.field_certificat_typology__display_name -#: model:ir.model.fields,field_description:sale_certificat_typology.field_product_product__display_name #: model:ir.model.fields,field_description:sale_certificat_typology.field_product_template__display_name #: model:ir.model.fields,field_description:sale_certificat_typology.field_sale_order__display_name #: model:ir.model.fields,field_description:sale_certificat_typology.field_sale_order_certificat_item__display_name @@ -127,7 +121,6 @@ msgstr "Saisir le nombre de jours durant lesquels on doit stocker le document" #. module: sale_certificat_typology #: model:ir.model.fields,field_description:sale_certificat_typology.field_certificat_typology__id -#: model:ir.model.fields,field_description:sale_certificat_typology.field_product_product__id #: model:ir.model.fields,field_description:sale_certificat_typology.field_product_template__id #: model:ir.model.fields,field_description:sale_certificat_typology.field_sale_order__id #: model:ir.model.fields,field_description:sale_certificat_typology.field_sale_order_certificat_item__id @@ -142,7 +135,6 @@ msgstr "Est valide ?" #. module: sale_certificat_typology #: model:ir.model.fields,field_description:sale_certificat_typology.field_certificat_typology____last_update -#: model:ir.model.fields,field_description:sale_certificat_typology.field_product_product____last_update #: model:ir.model.fields,field_description:sale_certificat_typology.field_product_template____last_update #: model:ir.model.fields,field_description:sale_certificat_typology.field_sale_order____last_update #: model:ir.model.fields,field_description:sale_certificat_typology.field_sale_order_certificat_item____last_update @@ -162,21 +154,11 @@ msgstr "Dernière mise à jour par" msgid "Last Updated on" msgstr "Dernière mise à jour le" -#. module: sale_certificat_typology -#: model:ir.model.fields,field_description:sale_certificat_typology.field_sale_order_line__certificat_item_ids -msgid "Linked certificat items" -msgstr "Justificatifs liés" - #. module: sale_certificat_typology #: model:ir.model.fields,field_description:sale_certificat_typology.field_sale_order_certificat_item__order_id msgid "Linked order" msgstr "Commande liée" -#. module: sale_certificat_typology -#: model:ir.model.fields,field_description:sale_certificat_typology.field_sale_order_certificat_item__order_line_ids -msgid "Linked order lines" -msgstr "Lignes de commande liées" - #. module: sale_certificat_typology #: model:ir.model.fields,field_description:sale_certificat_typology.field_certificat_typology__name #: model_terms:ir.ui.view,arch_db:sale_certificat_typology.view_certificat_typology_form @@ -188,11 +170,6 @@ msgstr "Nom" msgid "None" msgstr "Non" -#. module: sale_certificat_typology -#: model:ir.model,name:sale_certificat_typology.model_product_product -msgid "Product" -msgstr "Article" - #. module: sale_certificat_typology #: model:ir.model,name:sale_certificat_typology.model_product_template msgid "Product Template (Multi-Company)" @@ -201,6 +178,7 @@ msgstr "Modèle de produit (Multi-Sociétés)" #. module: sale_certificat_typology #: model:ir.model.fields,field_description:sale_certificat_typology.field_product_product__required_certificat_ids #: model:ir.model.fields,field_description:sale_certificat_typology.field_product_template__required_certificat_ids +#: model:ir.model.fields,field_description:sale_certificat_typology.field_sale_order_line__required_certificat_ids msgid "Requested certificats" msgstr "Justificatifs demandés" diff --git a/sale_certificat_typology/models/product.py b/sale_certificat_typology/models/product.py index 517995ab..ea3c6cbe 100644 --- a/sale_certificat_typology/models/product.py +++ b/sale_certificat_typology/models/product.py @@ -1,7 +1,7 @@ # Copyright 2023 Akretion # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). -from odoo import api, fields, models +from odoo import fields, models class ProductTemplate(models.Model): @@ -11,30 +11,3 @@ class ProductTemplate(models.Model): comodel_name="certificat.typology", string="Requested certificats", ) - - @api.model_create_multi - def create(self, vals_list): - templates = super().create(vals_list) - for template, vals in zip(templates, vals_list): - if "required_certificat_ids" in vals: - template.product_variant_ids.write( - {"required_certificat_ids": vals["required_certificat_ids"]} - ) - return templates - - def write(self, vals): - res = super().write(vals) - if "required_certificat_ids" in vals: - self.product_variant_ids.write( - {"required_certificat_ids": vals["required_certificat_ids"]} - ) - return res - - -class ProductProduct(models.Model): - _inherit = "product.product" - - required_certificat_ids = fields.Many2many( - comodel_name="certificat.typology", - string="Requested certificats", - ) diff --git a/sale_certificat_typology/models/sale_order.py b/sale_certificat_typology/models/sale_order.py index 8a2d1dca..c4c1dc3e 100644 --- a/sale_certificat_typology/models/sale_order.py +++ b/sale_certificat_typology/models/sale_order.py @@ -13,20 +13,28 @@ class SaleOrder(models.Model): certificat_item_ids = fields.One2many( comodel_name="sale.order.certificat.item", inverse_name="order_id", + compute="_compute_certificat_item_ids", string="Certificat items", ) sale_warn_msg = fields.Text(compute="_compute_sale_warn_msg") - def action_confirm(self): - for sale in self: - if sale.certificat_item_ids.filtered(lambda ci: not ci.certificat_is_ok): - raise ValidationError( - _( - "You cannot validate your order because some products " - "require certificates !" + @api.depends("order_line.product_id") + def _compute_certificat_item_ids(self): + for record in self: + required_certificats = record.order_line.product_id.required_certificat_ids + items = record.certificat_item_ids.filtered( + lambda s: s.certificat_typology_id in required_certificats + ) + for certificat in required_certificats: + if certificat not in items.certificat_typology_id: + items |= self.env["sale.order.certificat.item"].create( + { + "order_id": record.id, + "certificat_typology_id": certificat.id, + "company_id": record.company_id.id, + } ) - ) - return super().action_confirm() + record.certificat_item_ids = [(6, 0, items.ids)] @api.depends("certificat_item_ids") def _compute_sale_warn_msg(self): @@ -40,85 +48,34 @@ def _compute_sale_warn_msg(self): ) sale.sale_warn_msg = sale_warn_msg + def action_confirm(self): + for sale in self: + if sale.certificat_item_ids.filtered(lambda ci: not ci.certificat_is_ok): + raise ValidationError( + _( + "You cannot validate your order because some products " + "require certificates !" + ) + ) + return super().action_confirm() + class SaleOrderLine(models.Model): _inherit = "sale.order.line" - certificat_item_ids = fields.Many2many( - comodel_name="sale.order.certificat.item", string="Linked certificat items" + required_certificat_ids = fields.Many2many( + comodel_name="certificat.typology", + related="product_id.required_certificat_ids", + string="Requested certificats", + readonly=True, ) - @api.model_create_multi - def create(self, vals_list): - lines = super().create(vals_list) - for line, vals in zip(lines, vals_list): - if "order_id" in vals and "product_id" in vals: - self._add_certificat_items(line, vals.get("product_id")) - return lines - - def write(self, values): - for line in self: - if "product_id" in values: - # remove certificat item for old product - self._remove_certificat_items(line) - # add certificat item for new product - self._add_certificat_items(line, values.get("product_id")) - return super().write(values) - - def unlink(self): - for line in self: - self._remove_certificat_items(line) - return super().unlink() - - def _add_certificat_items(self, line, product_id): - product = self.env["product.product"].browse(product_id) - for certificat in product.required_certificat_ids: - certificat_item = self.env["sale.order.certificat.item"].search( - [ - ("order_id", "=", line.order_id.id), - ("certificat_typology_id", "=", certificat.id), - ], - limit=1, - ) - if certificat_item: - certificat_item.write({"order_line_ids": [(4, line.id, 0)]}) - else: - self.env["sale.order.certificat.item"].create( - { - "order_id": line.order_id.id, - "order_line_ids": [(6, 0, line.ids)], - "certificat_typology_id": certificat.id, - "company_id": line.company_id.id, - } - ) - - def _remove_certificat_items(self, line): - certificat_items = self.env["sale.order.certificat.item"].search( - [("order_id", "=", line.order_id.id), ("order_line_ids", "in", line.id)] - ) - for item in certificat_items: - if len(item.order_line_ids) == 1: - item.unlink() - else: - item.write({"order_line_ids": [(3, line.id, 0)]}) - class SaleOrderCertificatItem(models.Model): _name = "sale.order.certificat.item" _description = "Certificat Items in Sale Order" - order_id = fields.Many2one( - comodel_name="sale.order", string="Linked order", required=True - ) - order_line_ids = fields.Many2many( - comodel_name="sale.order.line", string="Linked order lines", required=True - ) - product_ids = fields.Many2many( - comodel_name="product.product", - string="Concerned products", - compute="_compute_product_ids", - readonly=False, - ) + order_id = fields.Many2one(comodel_name="sale.order", string="Linked order") certificat_typology_id = fields.Many2one( comodel_name="certificat.typology", string="Certificat typology", required=True ) @@ -143,8 +100,3 @@ def _delete_documents_stored(self): date.today() - item.order_id.date_order.date() ).days >= item.certificat_typology_id.storage_duration: item.write({"certificat": False}) - - @api.depends("order_line_ids") - def _compute_product_ids(self): - for rec in self: - rec.product_ids = [(6, 0, rec.order_line_ids.mapped("product_id").ids)] diff --git a/sale_certificat_typology/views/sale_order.xml b/sale_certificat_typology/views/sale_order.xml index c7723ef5..ccc0c27d 100644 --- a/sale_certificat_typology/views/sale_order.xml +++ b/sale_certificat_typology/views/sale_order.xml @@ -21,11 +21,6 @@ delete="false" create="false" > - + + +