Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions lighting/i18n/es.po
Original file line number Diff line number Diff line change
Expand Up @@ -2568,6 +2568,11 @@ msgstr "Es max. potencia"
msgid "Is recommended accessory"
msgstr ""

#. module: lighting
#: model:ir.model.fields,field_description:lighting.field_lighting_product__is_spare_part
msgid "Is spare part"
msgstr "Es recambio"

#. module: lighting
#: model:ir.model.constraint,message:lighting.constraint_lighting_product_voltage_voltage_uniq
msgid "It already exists another voltage with the same parameters"
Expand Down Expand Up @@ -3442,6 +3447,11 @@ msgstr ""
msgid "P. rec. acc."
msgstr ""

#. module: lighting
#: model_terms:ir.ui.view,arch_db:lighting.product_form_view
msgid "P. sp. parts"
msgstr "P. recambios"

#. module: lighting
#: model:ir.model.fields,field_description:lighting.field_lighting_product_category__parent_id
#: model:ir.model.fields,field_description:lighting.field_lighting_product_group__parent_id
Expand Down Expand Up @@ -4637,6 +4647,13 @@ msgstr "Fuentes"
msgid "Spanish"
msgstr ""

#. module: lighting
#: model:ir.model.fields,field_description:lighting.field_lighting_product__spare_part_ids
#: model_terms:ir.ui.view,arch_db:lighting.product_form_view
#: model_terms:ir.ui.view,arch_db:lighting.lighting_product_view
msgid "Spare parts"
msgstr "Recambios"

#. module: lighting
#: model:ir.model,name:lighting.model_lighting_product_special_spectrum
msgid "Special Spectrum"
Expand Down
17 changes: 17 additions & 0 deletions lighting/i18n/fr.po
Original file line number Diff line number Diff line change
Expand Up @@ -2561,6 +2561,11 @@ msgstr "Est max. En watts"
msgid "Is recommended accessory"
msgstr ""

#. module: lighting
#: model:ir.model.fields,field_description:lighting.field_lighting_product__is_spare_part
msgid "Is spare part"
msgstr "Est pièce de rechange"

#. module: lighting
#: model:ir.model.constraint,message:lighting.constraint_lighting_product_voltage_voltage_uniq
msgid "It already exists another voltage with the same parameters"
Expand Down Expand Up @@ -3440,6 +3445,11 @@ msgstr ""
msgid "P. rec. acc."
msgstr ""

#. module: lighting
#: model_terms:ir.ui.view,arch_db:lighting.product_form_view
msgid "P. sp. parts"
msgstr "P. pièces"

#. module: lighting
#: model:ir.model.fields,field_description:lighting.field_lighting_product_category__parent_id
#: model:ir.model.fields,field_description:lighting.field_lighting_product_group__parent_id
Expand Down Expand Up @@ -4635,6 +4645,13 @@ msgstr "Sources"
msgid "Spanish"
msgstr ""

#. module: lighting
#: model:ir.model.fields,field_description:lighting.field_lighting_product__spare_part_ids
#: model_terms:ir.ui.view,arch_db:lighting.product_form_view
#: model_terms:ir.ui.view,arch_db:lighting.lighting_product_view
msgid "Spare parts"
msgstr "Pièces de rechange"

#. module: lighting
#: model:ir.model,name:lighting.model_lighting_product_special_spectrum
msgid "Special Spectrum"
Expand Down
17 changes: 17 additions & 0 deletions lighting/i18n/pt.po
Original file line number Diff line number Diff line change
Expand Up @@ -2444,6 +2444,23 @@ msgstr "Controle remoto"
msgid "Recommended accessories"
msgstr "Acessórios recomendados"

#. module: lighting
#: model:ir.model.fields,field_description:lighting.field_lighting_product__spare_part_ids
#: model_terms:ir.ui.view,arch_db:lighting.product_form_view
#: model_terms:ir.ui.view,arch_db:lighting.lighting_product_view
msgid "Spare parts"
msgstr "Peças sobressalentes"

#. module: lighting
#: model:ir.model.fields,field_description:lighting.field_lighting_product__is_spare_part
msgid "Is spare part"
msgstr "É peça sobressalente"

#. module: lighting
#: model_terms:ir.ui.view,arch_db:lighting.product_form_view
msgid "P. sp. parts"
msgstr "P. peças"

#. module: lighting
#: model:ir.model.fields,field_description:lighting.field_lighting_product_required_ids
#: model:ir.ui.view,arch_db:lighting.product_form_view
Expand Down
66 changes: 65 additions & 1 deletion lighting/models/product.py
Original file line number Diff line number Diff line change
Expand Up @@ -1357,6 +1357,66 @@ def _search_is_required_accessory(self, operator, value):
tracking=True,
)

# Spare parts tab
spare_part_ids = fields.Many2many(
comodel_name="lighting.product",
relation="lighting_product_spare_part_rel",
column1="product_id",
column2="spare_part_id",
string="Spare parts",
tracking=True,
)
parent_spare_part_product_count = fields.Integer(
compute="_compute_parent_spare_part_product_count"
)

@api.depends("spare_part_ids")
def _compute_parent_spare_part_product_count(self):
if not self:
return
# Raw SQL for performance: the ORM approach triggers one query per
# record which is too slow on large recordsets
self.env.cr.execute(
"SELECT spare_part_id, COUNT(*)"
" FROM lighting_product_spare_part_rel"
" WHERE spare_part_id IN %s"
" GROUP BY spare_part_id",
(tuple(self.ids),),
)
counts = dict(self.env.cr.fetchall())
for rec in self:
rec.parent_spare_part_product_count = counts.get(rec.id, 0)

is_spare_part = fields.Boolean(
string="Is spare part",
compute="_compute_is_spare_part",
search="_search_is_spare_part",
)

@api.depends("spare_part_ids")
def _compute_is_spare_part(self):
if not self:
return
# Raw SQL for performance: the ORM approach triggers one query per
# record which is too slow on large recordsets
self.env.cr.execute(
"SELECT DISTINCT spare_part_id"
" FROM lighting_product_spare_part_rel"
" WHERE spare_part_id IN %s",
(tuple(self.ids),),
)
spare_ids = {row[0] for row in self.env.cr.fetchall()}
for rec in self:
rec.is_spare_part = rec.id in spare_ids

def _search_is_spare_part(self, operator, value):
ids = (
self.env["lighting.product"]
.search([("spare_part_ids", "!=", False)])
.mapped("spare_part_ids.id")
)
return [("id", "in", ids)]

# logistics tab
tariff_item = fields.Char(
tracking=True,
Expand Down Expand Up @@ -1553,7 +1613,7 @@ def _check_composite_product(self):
)
)

@api.constrains("optional_ids", "required_ids")
@api.constrains("optional_ids", "required_ids", "spare_part_ids")
def _check_product_dependency(self):
for rec in self:
if rec in rec.required_ids:
Expand All @@ -1566,6 +1626,10 @@ def _check_product_dependency(self):
"The current reference cannot be defined as a recomended accessory"
)
)
if rec in rec.spare_part_ids:
raise ValidationError(
_("The current reference cannot be defined as a spare part")
)

# TODO: REVIEW: Self ensure
@api.constrains("product_group_id")
Expand Down
36 changes: 36 additions & 0 deletions lighting/views/product_views.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@
<p class="oe_view_nocontent_create">Create the first product</p>
</field>
</record>
<record id="product_action_parent_spare_part_product" model="ir.actions.act_window">
<field name="name">Parent products as spare parts</field>
<field name="res_model">lighting.product</field>
<field name="view_mode">tree,kanban,form</field>
<field name="domain">[('spare_part_ids', '=', active_id)]</field>
<field
name="context"
>{'default_spare_part_ids': [(4, active_id, False)]}</field>
<field name="help" type="html">
<p class="oe_view_nocontent_create">Create the first product</p>
</field>
</record>
<record model="ir.ui.view" id="product_form_view">
<field name="name">product.form</field>
<field name="model">lighting.product</field>
Expand Down Expand Up @@ -73,6 +85,20 @@
widget="statinfo"
/>
</button>
<button
type="action"
class="oe_stat_button"
icon="fa-filter"
name="%(product_action_parent_spare_part_product)d"
attrs="{'invisible': [('parent_spare_part_product_count', '=', 0)]}"
>
<field
string="P. sp. parts"
name="parent_spare_part_product_count"
help="Products where one of its spare parts is the current product"
widget="statinfo"
/>
</button>
<button
type="action"
class="oe_stat_button"
Expand Down Expand Up @@ -510,6 +536,11 @@
<field name="substitute_ids" nolabel="1" colspan="2" />
</group>
</page>
<page string="Spare parts">
<group>
<field name="spare_part_ids" nolabel="1" colspan="2" />
</group>
</page>
</notebook>
</sheet>
<div class="oe_chatter">
Expand Down Expand Up @@ -788,6 +819,11 @@
name="recommended_accessories"
domain="[('is_optional_accessory', '=', True)]"
/>
<filter
string="Spare parts"
name="spare_parts"
domain="[('is_spare_part', '=', True)]"
/>
<filter
string="Mandatory accessories"
name="mandatory_accessories"
Expand Down
Loading