Skip to content

Commit

Permalink
[MIG] stock_secondary_unit: Migration to 17.0
Browse files Browse the repository at this point in the history
  • Loading branch information
mmrondon authored and rov-adhoc committed Oct 17, 2024
1 parent 4123cba commit 984cec6
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 84 deletions.
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.2.2.0",
"version": "17.0.1.0.0",
"development_status": "Production/Stable",
"category": "stock",
"website": "https://github.com/OCA/stock-logistics-warehouse",
Expand Down
42 changes: 27 additions & 15 deletions stock_secondary_unit/models/stock_move.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,29 @@ 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")
def _compute_product_uom_qty(self):
self._compute_helper_target_field_qty()

@api.onchange("product_uom")
def onchange_product_uom_for_secondary(self):
self._onchange_helper_product_uom_for_secondary()
@api.model_create_multi
def create(self, vals_list):
for vals in vals_list:
product = self.env["product.product"].browse(vals.get("product_id", False))
if product:
vals.update(
{
"secondary_uom_id": product.stock_secondary_uom_id.id
or product.product_tmpl_id.stock_secondary_uom_id.id,
}
)
return super().create(vals_list)

@api.model
def _prepare_merge_moves_distinct_fields(self):
Expand All @@ -40,17 +53,16 @@ def _prepare_extra_move_vals(self, qty):
class StockMoveLine(models.Model):
_inherit = ["stock.move.line", "product.secondary.unit.mixin"]
_name = "stock.move.line"
_secondary_unit_fields = {"qty_field": "qty_done", "uom_field": "product_uom_id"}
_secondary_unit_fields = {"qty_field": "quantity", "uom_field": "product_uom_id"}

qty_done = fields.Float(store=True, readonly=False, compute="_compute_qty_done")
@api.model_create_multi
def create(self, vals_list):
for vals in vals_list:
move = self.env["stock.move"].browse(vals.get("move_id", False))
if move.secondary_uom_id:
vals["secondary_uom_id"] = move.secondary_uom_id.id
return super().create(vals_list)

@api.model
def create(self, vals):
move = self.env["stock.move"].browse(vals.get("move_id", False))
if move.secondary_uom_id:
vals["secondary_uom_id"] = move.secondary_uom_id.id
return super().create(vals)

@api.depends("secondary_uom_id", "secondary_uom_qty")
def _compute_qty_done(self):
@api.depends("secondary_uom_id", "secondary_uom_qty", "quant_id")
def _compute_quantity(self):
self._compute_helper_target_field_qty()

Check warning on line 68 in stock_secondary_unit/models/stock_move.py

View check run for this annotation

Codecov / codecov/patch

stock_secondary_unit/models/stock_move.py#L68

Added line #L68 was not covered by tests
13 changes: 12 additions & 1 deletion stock_secondary_unit/report/report_deliveryslip.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
</td>
</xpath>
<xpath
expr='//table[@t-if="o.move_line_ids and o.state==&apos;done&apos;"]/thead/tr/th'
expr='//table[@t-elif="o.move_line_ids and o.state==&apos;done&apos;"]/thead/tr/th'
position="after"
>
<th>
Expand All @@ -41,4 +41,15 @@
</td>
</xpath>
</template>
<template
id="stock_report_delivery_aggregated_move_lines"
inherit_id="stock.stock_report_delivery_aggregated_move_lines"
>
<xpath expr="//tr[@t-foreach='aggregated_lines']/td[1]" position="after">
<td>
<span t-field="aggregated_lines[line]['move'].secondary_uom_qty" />
<span t-field="aggregated_lines[line]['move'].secondary_uom_id" />
</td>
</xpath>
</template>
</odoo>
45 changes: 19 additions & 26 deletions stock_secondary_unit/tests/test_stock_secondary_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,15 +148,14 @@ def test_03_stock_picking_secondary_unit(self):
delivery_order = StockPicking.create(do_vals)
delivery_order.action_confirm()
# Move is merged into 1 line for both stock.move and stock.move.line
self.assertEqual(len(delivery_order.move_lines), 1)
self.assertEqual(len(delivery_order.move_line_ids), 1)
# Qty merged to 20, and secondary unit qty is 40line
uom_qty = sum(delivery_order.move_lines.mapped("product_uom_qty"))
# Qty merged to 20, and secondary unit qty is 40 line
uom_qty = sum(delivery_order.move_ids.mapped("product_uom_qty"))
secondary_uom_qty = sum(
delivery_order.move_line_ids.mapped("secondary_uom_qty")
)
self.assertEqual(uom_qty, 20.0)
self.assertEqual(secondary_uom_qty, 0.0)
self.assertEqual(secondary_uom_qty, 40.0)

def test_picking_secondary_unit(self):
product = self.product_template.product_variant_ids[0]
Expand All @@ -182,23 +181,9 @@ def test_picking_secondary_unit(self):
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)

self.assertAlmostEqual(move.secondary_uom_qty, 1, 2)
picking = picking_form.save()
picking.action_confirm()
with Form(picking) as picking_form:
# Test detail operations
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.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.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)

def test_secondary_unit_merge_move_diff_uom(self):
product = self.product_template.product_variant_ids[0]
Expand All @@ -211,14 +196,18 @@ 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.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.product_tmpl_id.secondary_uom_ids[1]
picking = picking_form.save()
picking.move_ids[
0
].secondary_uom_id = product.product_tmpl_id.secondary_uom_ids[0]
picking.move_ids[
1
].secondary_uom_id = product.product_tmpl_id.secondary_uom_ids[1]
picking.action_confirm()
self.assertEqual(len(picking.move_lines), 2)
self.assertEqual(len(picking.move_ids), 2)

def test_secondary_unit_merge_move_same_uom(self):
product = self.product_template.product_variant_ids[0]
Expand All @@ -231,12 +220,16 @@ 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.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.product_tmpl_id.secondary_uom_ids[0]
picking = picking_form.save()
picking.move_ids[
0
].secondary_uom_id = product.product_tmpl_id.secondary_uom_ids[0]
picking.move_ids[
1
].secondary_uom_id = product.product_tmpl_id.secondary_uom_ids[0]
picking.action_confirm()
self.assertEqual(len(picking.move_lines), 1)
self.assertEqual(picking.move_lines.secondary_uom_qty, 2)
self.assertEqual(len(picking.move_ids), 1)
self.assertEqual(picking.move_ids.secondary_uom_qty, 2)
38 changes: 25 additions & 13 deletions stock_secondary_unit/views/product_views.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
<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">
<field
name="stock_secondary_uom_id"
options="{'no_create': True}"
groups="stock.group_stock_user"
/>
</group>
</xpath>
Expand All @@ -24,15 +24,18 @@
name="inherit_id"
ref="stock.product_template_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">
<xpath
expr="//button[@name='action_update_quantity_on_hand']"
position="after"
>
<button
type="object"
name="action_open_quants"
attrs="{'invisible':[('type', '!=', 'product')]}"
name="action_update_quantity_on_hand"
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 @@ -53,15 +56,18 @@
<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">
<xpath
expr="//button[@name='action_update_quantity_on_hand']"
position="after"
>
<button
type="object"
name="action_open_quants"
attrs="{'invisible':[('type', '!=', 'product')]}"
name="action_update_quantity_on_hand"
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 @@ -82,27 +88,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
18 changes: 10 additions & 8 deletions stock_secondary_unit/views/stock_move_views.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,23 @@
<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="quantity" position="before">
<field
name="secondary_uom_qty"
attrs="{'readonly': [('state', 'in', ('done', 'cancel')), ('is_locked', '=', True)]}"
readonly="state in ['done', 'cancel'] and is_locked"
force_save="1"
groups="uom.group_uom"
optional="show"
/>
<field
name="secondary_uom_id"
domain="['|', ('product_id', '=', product_id),
'&amp;', ('product_tmpl_id.product_variant_ids', 'in', [product_id]),
('product_id', '=', False)]"
attrs="{'readonly': [('state', '!=', 'draft'), ('id', '!=', False)]}"
readonly="state != 'draft' and id"
options="{'no_create': True}"
groups="uom.group_uom"
optional="show"
/>
</field>
Expand All @@ -34,22 +35,23 @@
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="quantity" position="before">
<field
name="secondary_uom_qty"
attrs="{'readonly': [('state', 'in', ('done', 'cancel')), ('is_locked', '=', True)]}"
readonly="state in ['done', 'cancel'] and is_locked"
force_save="1"
groups="uom.group_uom"
optional="show"
/>
<field
name="secondary_uom_id"
domain="['|', ('product_id', '=', product_id),
'&amp;', ('product_tmpl_id.product_variant_ids', 'in', [product_id]),
('product_id', '=', False)]"
attrs="{'readonly': [('state', '!=', 'draft'), ('id', '!=', False)]}"
readonly="state != 'draft' and id"
options="{'no_create': True}"
groups="uom.group_uom"
optional="show"
/>
</field>
Expand Down
26 changes: 6 additions & 20 deletions stock_secondary_unit/views/stock_picking_views.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@
<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']"
position="before"
>
<field name="is_locked" invisible="1" />
<field name="show_operations" invisible="1" />
<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"
readonly="(not is_initial_demand_editable) or (show_operations and is_locked and not is_initial_demand_editable)"
optional="show"
/>
<field
Expand All @@ -23,27 +25,11 @@
'&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"
readonly="(not is_initial_demand_editable) or (show_operations and is_locked and not is_initial_demand_editable)"
optional="show"
/>
</xpath>
<xpath
expr="//field[@name='move_ids_without_package']/form//field[@name='product_uom_qty']"
position="before"
>
<field
name="secondary_uom_qty"
attrs="{'invisible': [('parent.immediate_transfer', '=', True)], 'readonly': [('is_initial_demand_editable', '=', False)]}"
/>
<field
name="secondary_uom_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)]}"
/>
</xpath>
</field>
</record>
</odoo>

0 comments on commit 984cec6

Please sign in to comment.