Skip to content

Commit

Permalink
code refactor and simplification
Browse files Browse the repository at this point in the history
  • Loading branch information
chafique-delli committed Dec 6, 2023
1 parent f6af44a commit 83a13ad
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 143 deletions.
32 changes: 5 additions & 27 deletions sale_certificat_typology/i18n/fr.po
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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)"
Expand All @@ -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"

Expand All @@ -227,10 +205,10 @@ msgstr "Durée de stockage"
#. module: sale_certificat_typology
#: code:addons/sale_certificat_typology/models/sale_order.py:0
#, python-format
msgid "Warning: in this order, some products require certificates !"
msgid "Warning: in this order, some products require valid certificates !"
msgstr ""
"Alerte: dans cette commande, il y a des produits qui nécessitent des "
"justificatifs !"
"justificatifs valides !"

#. module: sale_certificat_typology
#: code:addons/sale_certificat_typology/models/sale_order.py:0
Expand Down
29 changes: 1 addition & 28 deletions sale_certificat_typology/models/product.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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",
)
119 changes: 36 additions & 83 deletions sale_certificat_typology/models/sale_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,29 @@ 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",
store=True,
)
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):
Expand All @@ -36,89 +45,38 @@ def _compute_sale_warn_msg(self):
lambda ci: not ci.certificat_is_ok
):
sale_warn_msg = _(
"Warning: in this order, some products require certificates !"
"Warning: in this order, some products require valid certificates !"
)
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
)
Expand All @@ -143,8 +101,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)]
15 changes: 10 additions & 5 deletions sale_certificat_typology/views/sale_order.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,6 @@
delete="false"
create="false"
>
<field
name="product_ids"
widget="many2many_tags"
readonly="True"
/>
<field name="certificat_typology_id" readonly="True" />
<field name="certificat" />
<field
Expand All @@ -40,6 +35,16 @@
</field>
</page>
</xpath>
<xpath
expr="//notebook/page[@name='order_lines']/field[@name='order_line']/tree/field[@name='company_id']"
position="after"
>
<field
name="required_certificat_ids"
widget="many2many_tags"
optional="hide"
/>
</xpath>
<header position="after">
<div
class="alert alert-warning"
Expand Down

0 comments on commit 83a13ad

Please sign in to comment.