Skip to content

Commit

Permalink
[MIG] sale_order_secondary_unit: Migration to 16.0
Browse files Browse the repository at this point in the history
  • Loading branch information
remihb committed Apr 18, 2023
1 parent bba7a4b commit 39a1fe5
Show file tree
Hide file tree
Showing 10 changed files with 136 additions and 43 deletions.
2 changes: 1 addition & 1 deletion sale_order_secondary_unit/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{
"name": "Sale Order Secondary Unit",
"summary": "Sale product in a secondary unit",
"version": "15.0.1.2.0",
"version": "16.0.1.0.0",
"development_status": "Production/Stable",
"category": "Sale",
"website": "https://github.com/OCA/sale-workflow",
Expand Down
1 change: 1 addition & 0 deletions sale_order_secondary_unit/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
from . import product_product
from . import product_template
from . import sale_order
17 changes: 17 additions & 0 deletions sale_order_secondary_unit/models/product_product.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Copyright 2022 Tecnativa - Sergio Teruel
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
from odoo import fields, models


class ProductProduct(models.Model):
_inherit = "product.product"

sale_secondary_uom_id = fields.Many2one(
comodel_name="product.secondary.unit",
string="Default secondary unit for sales",
help="In order to set a value, please first add at least one record"
" in 'Secondary Unit of Measure'",
domain="['|', ('product_id', '=', id),"
"'&', ('product_tmpl_id', '=', product_tmpl_id),"
" ('product_id', '=', False)]",
)
60 changes: 59 additions & 1 deletion sale_order_secondary_unit/models/product_template.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Copyright 2018-2020 Tecnativa - Sergio Teruel
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
from odoo import fields, models
from odoo import _, api, fields, models


class ProductTemplate(models.Model):
Expand All @@ -9,6 +9,64 @@ class ProductTemplate(models.Model):
sale_secondary_uom_id = fields.Many2one(
comodel_name="product.secondary.unit",
string="Default secondary unit for sales",
compute="_compute_sale_secondary_uom_id",
inverse="_inverse_sale_secondary_uom_id",
help="In order to set a value, please first add at least one record"
" in 'Secondary Unit of Measure'",
domain="[('product_tmpl_id', '=', id), ('product_id', '=', False)]",
store=True,
)

@api.depends("product_variant_ids", "product_variant_ids.sale_secondary_uom_id")
def _compute_sale_secondary_uom_id(self):
unique_variants = self.filtered(lambda tmpl: tmpl.product_variant_count == 1)
for template in unique_variants:
template.sale_secondary_uom_id = (
template.product_variant_ids.sale_secondary_uom_id
)
for template in self - unique_variants:
if len(template.product_variant_ids.sale_secondary_uom_id) == 1:
template.sale_secondary_uom_id = (
template.product_variant_ids.sale_secondary_uom_id
)
else:
template.sale_secondary_uom_id = False

def _inverse_sale_secondary_uom_id(self):
for template in self:
# if template.product_variant_count == 1:
template.product_variant_ids.sale_secondary_uom_id = (
template.sale_secondary_uom_id
)

@api.onchange("sale_secondary_uom_id")
def onchange_sale_secondary_uom_id(self):
if len(self.product_variant_ids.sale_secondary_uom_id) > 1:
return {
"warning": {
"title": _("Warning"),
"message": _(
"Product variants have distinct sale secondary uom:"
"\n{secondary_uom}\n"
"All variants will be written with new secondary uom"
).format(
secondary_uom="\n".join(
self.product_variant_ids.mapped(
"sale_secondary_uom_id.name"
)
)
),
}
}

@api.model_create_multi
def create(self, vals_list):
templates = super(ProductTemplate, self).create(vals_list)
# This is needed to set given values to first variant after creation
for template, vals in zip(templates, vals_list):
related_vals = {}
if vals.get("sale_secondary_uom_id"):
related_vals["sale_secondary_uom_id"] = vals["sale_secondary_uom_id"]
if related_vals:
template.write(related_vals)
return templates
24 changes: 5 additions & 19 deletions sale_order_secondary_unit/models/sale_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,9 @@ class SaleOrderLine(models.Model):
"uom_field": "product_uom",
}

secondary_uom_qty = fields.Float(string="2nd Qty", digits="Product Unit of Measure")
secondary_uom_id = fields.Many2one(
comodel_name="product.secondary.unit",
string="2nd uom",
ondelete="restrict",
)

secondary_uom_unit_price = fields.Float(
string="2nd unit price",
digits="Product Unit of Measure",
store=False,
readonly=True,
digits="Product Price",
compute="_compute_secondary_uom_unit_price",
)

Expand All @@ -48,17 +39,12 @@ def product_id_change(self):
"""
# Determine if we compute the sale line with one unit of secondary uom as default.
# Based on method of sale order line _update_taxes
default_secondary_qty = 0.0
if (
not self.product_uom or (self.product_id.uom_id.id != self.product_uom.id)
) and not self.product_uom_qty:
default_secondary_qty = 1.0
res = super().product_id_change()
line_uom_qty = self.product_uom_qty
self.secondary_uom_id = self.product_id.sale_secondary_uom_id
if self.product_id.sale_secondary_uom_id:
line_uom_qty = self.product_uom_qty
self.secondary_uom_id = self.product_id.sale_secondary_uom_id
if default_secondary_qty:
self.secondary_uom_qty = default_secondary_qty
if line_uom_qty == 1.0:
self.secondary_uom_qty = 1.0
self.onchange_product_uom_for_secondary()
else:
self.product_uom_qty = line_uom_qty
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ def setUpClass(cls):
"name": "test",
"uom_id": cls.product_uom_kg.id,
"uom_po_id": cls.product_uom_kg.id,
}
)
# Set secondary uom on product template
cls.product.product_tmpl_id.write(
{
"secondary_uom_ids": [
(
0,
Expand Down
7 changes: 1 addition & 6 deletions sale_order_secondary_unit/views/product_views.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,9 @@
<field name="name">Product template Secondary Unit</field>
<field name="model">product.template</field>
<field name="inherit_id" ref="product.product_template_form_view" />
<field name="groups_id" eval="[(4, ref('uom.group_uom'))]" />
<field name="arch" type="xml">
<field name="uom_id" position="after">
<field
name="sale_secondary_uom_id"
domain="[('product_tmpl_id', '=', id)]"
options="{'no_create': True}"
/>
<field name="sale_secondary_uom_id" options="{'no_create': True}" groups="uom.group_uom" />
</field>
</field>
</record>
Expand Down
56 changes: 40 additions & 16 deletions sale_order_secondary_unit/views/sale_order_views.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,30 @@
<field name="name">Sale Order Secondary Unit</field>
<field name="model">sale.order</field>
<field name="inherit_id" ref="sale.view_order_form" />
<field name="groups_id" eval="[(4, ref('uom.group_uom'))]" />
<field name="arch" type="xml">
<xpath
expr="//field[@name='order_line']/form//field[@name='price_subtotal']"
position="after"
>
<field
name="secondary_uom_qty"
class="oe_inline oe_no_button"
attrs="{'readonly': [('state', 'in', ('done', 'cancel'))]}"
optional="show"
/>
<field
name="secondary_uom_id"
class="oe_inline"
domain="[('product_tmpl_id.product_variant_ids', 'in', [product_id])]"
attrs="{'readonly': [('state', 'in', ('done', 'cancel'))]}"
optional="show"
/>
<field name="secondary_uom_unit_price" readonly="1" optional="show" />
<label for="secondary_uom_qty" groups="uom.group_uom" />
<div class="o_row" name="secondary_uom_qty" groups="uom.group_uom">
<field
name="secondary_uom_qty"
class="oe_inline oe_no_button"
widget="numeric_step"
options="{'auto_select': True}"
attrs="{'readonly': [('state', 'in', ('done', 'cancel'))]}"
/>
<field
name="secondary_uom_id"
class="oe_inline oe_no_button"
domain="['|', ('product_id', '=', product_id),
'&amp;', ('product_tmpl_id.product_variant_ids', 'in', [product_id]),
('product_id', '=', False)]"
options="{'no_create': True, 'no_open': True}"
attrs="{'readonly': [('state', 'in', ('done', 'cancel'))]}"
/>
</div>
</xpath>
<xpath
expr="//field[@name='order_line']/tree//field[@name='product_uom_qty']"
Expand All @@ -34,14 +38,34 @@
<field
name="secondary_uom_qty"
attrs="{'readonly': [('parent.state', 'in', ('done', 'cancel'))]}"
groups="uom.group_uom"
/>
<field
name="secondary_uom_id"
domain="[('product_tmpl_id.product_variant_ids', 'in', [product_id])]"
domain="['|', ('product_id', '=', product_id),
'&amp;', ('product_tmpl_id.product_variant_ids', 'in', [product_id]),
('product_id', '=', False)]"
options="{'no_create': True}"
attrs="{'readonly': [('parent.state', 'in', ('done', 'cancel'))]}"
groups="uom.group_uom"
/>
</xpath>
<xpath
expr="//field[@name='order_line']/kanban//field[@name='company_id']"
position="after"
>
<field name="secondary_uom_id" invisible="1" />
<field name="secondary_uom_qty" invisible="1" />
</xpath>
<xpath
expr="//field[@name='order_line']/kanban//t[@t-out='record.product_uom_qty.value']"
position="before"
>
<t t-if="record.secondary_uom_id.value">
<t t-esc="record.secondary_uom_qty.value" groups="uom.group_uom" />
<t t-esc="record.secondary_uom_id.value" groups="uom.group_uom" />
</t>
</xpath>
</field>
</record>
</odoo>
6 changes: 6 additions & 0 deletions setup/sale_order_secondary_unit/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import setuptools

setuptools.setup(
setup_requires=['setuptools-odoo'],
odoo_addon=True,
)

0 comments on commit 39a1fe5

Please sign in to comment.