Skip to content

Commit

Permalink
[MIG] stock_secondary_unit: Migration to 16.0
Browse files Browse the repository at this point in the history
  • Loading branch information
remihb committed Apr 26, 2023
1 parent fb76e39 commit 825da2a
Show file tree
Hide file tree
Showing 12 changed files with 103 additions and 63 deletions.
6 changes: 6 additions & 0 deletions setup/stock_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,
)
2 changes: 1 addition & 1 deletion stock_secondary_unit/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{
"name": "Stock Secondary Unit",
"summary": "Get product quantities in a secondary unit",
"version": "15.0.1.0.0",
"version": "16.0.1.0.0",
"development_status": "Production/Stable",
"category": "stock",
"website": "https://github.com/OCA/stock-logistics-warehouse",
Expand Down
5 changes: 4 additions & 1 deletion stock_secondary_unit/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
from . import product
# Keep order
from . import stock_product_secondary_unit_mixin
from . import product_product
from . import product_template
from . import stock_move
17 changes: 17 additions & 0 deletions stock_secondary_unit/models/product_product.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Copyright 2018 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", "stock.product.secondary.unit.mixin"]
_name = "product.product"

stock_secondary_uom_id = fields.Many2one(
comodel_name="product.secondary.unit",
string="Second unit for inventory",
readonly=False,
domain="['|', ('product_id', '=', id),"
"'&', ('product_tmpl_id', '=', product_tmpl_id),"
" ('product_id', '=', False)]",
)
14 changes: 14 additions & 0 deletions stock_secondary_unit/models/product_template.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Copyright 2018 Tecnativa - Sergio Teruel
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
from odoo import fields, models


class ProductTemplate(models.Model):
_inherit = ["product.template", "stock.product.secondary.unit.mixin"]
_name = "product.template"

stock_secondary_uom_id = fields.Many2one(
comodel_name="product.secondary.unit",
domain="[('product_tmpl_id', '=', id), ('product_id', '=', False)]",
string="Second unit for inventory",
)
15 changes: 8 additions & 7 deletions stock_secondary_unit/models/stock_move.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ class StockMove(models.Model):
}

product_uom_qty = fields.Float(
store=True, readonly=False, compute="_compute_product_uom_qty", copy=True
store=True,
readonly=False,
compute="_compute_product_uom_qty",
copy=True,
precompute=True,
)

@api.depends("secondary_uom_qty", "secondary_uom_id")
Expand All @@ -24,11 +28,6 @@ def _compute_product_uom_qty(self):
def onchange_product_uom_for_secondary(self):
self._onchange_helper_product_uom_for_secondary()

def _merge_moves_fields(self):
res = super()._merge_moves_fields()
res["secondary_uom_qty"] = sum(self.mapped("secondary_uom_qty"))
return res

@api.model
def _prepare_merge_moves_distinct_fields(self):
"""Don't merge moves with distinct secondary units"""
Expand All @@ -42,7 +41,9 @@ class StockMoveLine(models.Model):
_name = "stock.move.line"
_secondary_unit_fields = {"qty_field": "qty_done", "uom_field": "product_uom_id"}

qty_done = fields.Float(store=True, readonly=False, compute="_compute_qty_done")
qty_done = fields.Float(
store=True, readonly=False, compute="_compute_qty_done", precompute=True
)

@api.model
def create(self, vals):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
from odoo.tools.float_utils import float_round


class StockProductSecondaryUnit(models.AbstractModel):
_name = "stock.product.secondary.unit"
_description = "Stock Product Secondary Unit"
class StockProductSecondaryUnitMixin(models.AbstractModel):
_name = "stock.product.secondary.unit.mixin"
_description = "Stock Product Secondary Unit Mixin"

secondary_unit_qty_available = fields.Float(
string="Quantity On Hand (2Unit)",
Expand All @@ -26,23 +26,3 @@ def _compute_secondary_unit_qty_available(self):
product.secondary_unit_qty_available = float_round(
qty, precision_rounding=product.uom_id.rounding
)


class ProductTemplate(models.Model):
_inherit = ["product.template", "stock.product.secondary.unit"]
_name = "product.template"

stock_secondary_uom_id = fields.Many2one(
comodel_name="product.secondary.unit", string="Second unit for inventory"
)


class ProductProduct(models.Model):
_inherit = ["product.product", "stock.product.secondary.unit"]
_name = "product.product"

stock_secondary_uom_id = fields.Many2one(
comodel_name="product.secondary.unit",
string="Second unit for inventory",
related="product_tmpl_id.stock_secondary_uom_id",
)
26 changes: 14 additions & 12 deletions stock_secondary_unit/tests/test_stock_secondary_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@ def setUpClass(cls):
secondary_unit = cls.env["product.secondary.unit"].search(
[("product_tmpl_id", "=", cls.product_template.id)], limit=1
)
cls.product_template.write({"stock_secondary_uom_id": secondary_unit.id})
cls.product_template.product_variant_ids.write(
{"stock_secondary_uom_id": secondary_unit.id}
)
StockQuant = cls.env["stock.quant"]
cls.quant_white = StockQuant.create(
{
Expand All @@ -104,7 +106,7 @@ def setUpClass(cls):
)

def test_01_stock_secondary_unit_template(self):
self.assertEqual(self.product_template.secondary_unit_qty_available, 40.0)
self.assertEqual(self.product_template.secondary_unit_qty_available, 0)

def test_02_stock_secondary_unit_variant(self):
for variant in self.product_template.product_variant_ids.filtered(
Expand All @@ -118,7 +120,7 @@ def test_03_stock_picking_secondary_unit(self):
move_vals = {
"product_id": product1.id,
"name": product1.display_name,
"secondary_uom_id": product1.secondary_uom_ids[0].id,
"secondary_uom_id": product1.product_tmpl_id.secondary_uom_ids[0].id,
"product_uom": product1.uom_id.id,
"product_uom_qty": 10.0,
"location_id": self.location_supplier.id,
Expand Down Expand Up @@ -157,17 +159,17 @@ def test_picking_secondary_unit(self):
with picking_form.move_ids_without_package.new() as move:
move.product_id = product
move.secondary_uom_qty = 1
move.secondary_uom_id = product.secondary_uom_ids[0]
move.secondary_uom_id = product.product_tmpl_id.secondary_uom_ids[0]
self.assertEqual(move.product_uom_qty, 0.5)
move.secondary_uom_qty = 2
self.assertEqual(move.product_uom_qty, 1)
move.secondary_uom_id = product.secondary_uom_ids[1]
move.secondary_uom_id = product.product_tmpl_id.secondary_uom_ids[1]
self.assertEqual(move.product_uom_qty, 1.8)
move.product_uom_qty = 5
self.assertAlmostEqual(move.secondary_uom_qty, 5.56, 2)
# Change uom from stock move line
move.secondary_uom_qty = 1
move.secondary_uom_id = product.secondary_uom_ids[2]
move.secondary_uom_id = product.product_tmpl_id.secondary_uom_ids[2]
self.assertEqual(move.product_uom_qty, 10)
move.product_uom = self.product_uom_ton
self.assertAlmostEqual(move.secondary_uom_qty, 1000, 2)
Expand All @@ -179,11 +181,11 @@ def test_picking_secondary_unit(self):
with picking_form.move_line_ids_without_package.new() as move:
move.product_id = product
move.secondary_uom_qty = 1
move.secondary_uom_id = product.secondary_uom_ids[0]
move.secondary_uom_id = product.product_tmpl_id.secondary_uom_ids[0]
self.assertEqual(move.qty_done, 0.5)
move.secondary_uom_qty = 2
self.assertEqual(move.qty_done, 1)
move.secondary_uom_id = product.secondary_uom_ids[1]
move.secondary_uom_id = product.product_tmpl_id.secondary_uom_ids[1]
self.assertEqual(move.qty_done, 1.8)
move.qty_done = 5
self.assertAlmostEqual(move.secondary_uom_qty, 5.56, 2)
Expand All @@ -199,11 +201,11 @@ def test_secondary_unit_merge_move_diff_uom(self):
with picking_form.move_ids_without_package.new() as move:
move.product_id = product
move.secondary_uom_qty = 1
move.secondary_uom_id = product.secondary_uom_ids[0]
move.secondary_uom_id = product.product_tmpl_id.secondary_uom_ids[0]
with picking_form.move_ids_without_package.new() as move:
move.product_id = product
move.secondary_uom_qty = 1
move.secondary_uom_id = product.secondary_uom_ids[1]
move.secondary_uom_id = product.product_tmpl_id.secondary_uom_ids[1]
picking = picking_form.save()
picking.action_confirm()
self.assertEqual(len(picking.move_lines), 2)
Expand All @@ -219,11 +221,11 @@ def test_secondary_unit_merge_move_same_uom(self):
with picking_form.move_ids_without_package.new() as move:
move.product_id = product
move.secondary_uom_qty = 1
move.secondary_uom_id = product.secondary_uom_ids[0]
move.secondary_uom_id = product.product_tmpl_id.secondary_uom_ids[0]
with picking_form.move_ids_without_package.new() as move:
move.product_id = product
move.secondary_uom_qty = 1
move.secondary_uom_id = product.secondary_uom_ids[0]
move.secondary_uom_id = product.product_tmpl_id.secondary_uom_ids[0]
picking = picking_form.save()
picking.action_confirm()
self.assertEqual(len(picking.move_lines), 1)
Expand Down
21 changes: 13 additions & 8 deletions stock_secondary_unit/views/product_views.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@
<field name="name">Product template Secondary Unit</field>
<field name="model">product.template</field>
<field name="inherit_id" ref="stock.view_template_property_form" />
<field name="groups_id" eval="[(4, ref('stock.group_stock_user'))]" />
<field name="arch" type="xml">
<xpath expr="//group[@name='inventory']" position="inside">
<group string="Secondary unit">
<group string="Secondary unit" groups="stock.group_stock_user">
<field
name="stock_secondary_uom_id"
domain="[('product_tmpl_id', '=', id)]"
options="{'no_create': True}"
/>
</group>
Expand All @@ -34,6 +32,7 @@
attrs="{'invisible':[('type', '!=', 'product')]}"
class="oe_stat_button"
icon="fa-building-o"
groups="stock.group_stock_user"
>
<div class="o_form_field o_stat_info">
<span class="o_stat_value">
Expand All @@ -54,7 +53,6 @@
<record id="product_form_view_procurement_button" model="ir.ui.view">
<field name="model">product.product</field>
<field name="inherit_id" ref="stock.product_form_view_procurement_button" />
<field name="groups_id" eval="[(4, ref('stock.group_stock_user'))]" />
<field name="arch" type="xml">
<xpath expr="//button[@name='action_open_quants']" position="after">
<button
Expand All @@ -63,6 +61,7 @@
attrs="{'invisible':[('type', '!=', 'product')]}"
class="oe_stat_button"
icon="fa-building-o"
groups="stock.group_stock_user"
>
<div class="o_form_field o_stat_info">
<span class="o_stat_value">
Expand All @@ -83,27 +82,33 @@
<record id="product_template_tree_view" model="ir.ui.view">
<field name="model">product.template</field>
<field name="inherit_id" ref="product.product_template_tree_view" />
<field name="groups_id" eval="[(4, ref('stock.group_stock_user'))]" />
<field name="arch" type="xml">
<xpath expr="//field[@name='uom_id']" position="after">
<field name="secondary_unit_qty_available" />
<field
name="secondary_unit_qty_available"
groups="stock.group_stock_user"
/>
<field
name="stock_secondary_uom_id"
options="{'no_open': True, 'no_create': True}"
groups="stock.group_stock_user"
/>
</xpath>
</field>
</record>
<record id="product_product_tree_view" model="ir.ui.view">
<field name="model">product.product</field>
<field name="inherit_id" ref="product.product_product_tree_view" />
<field name="groups_id" eval="[(4, ref('stock.group_stock_user'))]" />
<field name="arch" type="xml">
<xpath expr="//field[@name='uom_id']" position="after">
<field name="secondary_unit_qty_available" />
<field
name="secondary_unit_qty_available"
groups="stock.group_stock_user"
/>
<field
name="stock_secondary_uom_id"
options="{'no_open': True, 'no_create': True}"
groups="stock.group_stock_user"
/>
</xpath>
</field>
Expand Down
20 changes: 12 additions & 8 deletions stock_secondary_unit/views/stock_move_views.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@
<field name="name">Stock Move Secondary Unit</field>
<field name="model">stock.move.line</field>
<field name="inherit_id" ref="stock.view_stock_move_line_operation_tree" />
<field name="groups_id" eval="[(4, ref('uom.group_uom'))]" />
<field name="arch" type="xml">
<field name="product_uom_qty" position="before">
<field name="secondary_uom_qty" />
<field name="reserved_uom_qty" position="before">
<field name="secondary_uom_qty" 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}"
groups="uom.group_uom"
/>
</field>
</field>
Expand All @@ -25,14 +27,16 @@
name="inherit_id"
ref="stock.view_stock_move_line_detailed_operation_tree"
/>
<field name="groups_id" eval="[(4, ref('uom.group_uom'))]" />
<field name="arch" type="xml">
<field name="product_uom_qty" position="before">
<field name="secondary_uom_qty" />
<field name="reserved_uom_qty" position="before">
<field name="secondary_uom_qty" 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}"
groups="uom.group_uom"
/>
</field>
</field>
Expand Down
13 changes: 10 additions & 3 deletions stock_secondary_unit/views/stock_picking_views.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
<field name="name">Stock Picking Secondary Unit</field>
<field name="model">stock.picking</field>
<field name="inherit_id" ref="stock.view_picking_form" />
<field name="groups_id" eval="[(4, ref('uom.group_uom'))]" />
<field name="arch" type="xml">
<xpath
expr="//field[@name='move_ids_without_package']/tree/field[@name='product_uom_qty']"
Expand All @@ -15,12 +14,16 @@
<field
name="secondary_uom_qty"
attrs="{'column_invisible': [('parent.immediate_transfer', '=', True)], 'readonly': ['|', ('is_initial_demand_editable', '=', False), '&amp;', '&amp;', ('show_operations', '=', True), ('is_locked', '=', True), ('is_initial_demand_editable', '=', False)]}"
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="{'column_invisible': [('parent.immediate_transfer', '=', True)], 'readonly': ['|', ('is_initial_demand_editable', '=', False), '&amp;', '&amp;', ('show_operations', '=', True), ('is_locked', '=', True), ('is_initial_demand_editable', '=', False)]}"
groups="uom.group_uom"
/>
</xpath>
<xpath
Expand All @@ -30,12 +33,16 @@
<field
name="secondary_uom_qty"
attrs="{'invisible': [('parent.immediate_transfer', '=', True)], 'readonly': [('is_initial_demand_editable', '=', False)]}"
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="{'invisible': [('parent.immediate_transfer', '=', True)], 'readonly': [('is_initial_demand_editable', '=', False)]}"
groups="uom.group_uom"
/>
</xpath>
</field>
Expand Down

0 comments on commit 825da2a

Please sign in to comment.