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 Jul 30, 2024
1 parent 8d01b54 commit 489e38d
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 85 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
49 changes: 34 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,36 @@ 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.depends("product_id")
def _compute_product_uom(self):
res = super()._compute_product_uom()
for move in self:
move._onchange_helper_product_uom_for_secondary()
return res

@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 +60,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()
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>
11 changes: 7 additions & 4 deletions stock_secondary_unit/static/description/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@

/*
:Author: David Goodger (goodger@python.org)
:Id: $Id: html4css1.css 8954 2022-01-20 10:10:25Z milde $
:Id: $Id: html4css1.css 9511 2024-01-13 09:50:07Z milde $
:Copyright: This stylesheet has been placed in the public domain.
Default cascading style sheet for the HTML output of Docutils.
Despite the name, some widely supported CSS2 features are used.
See https://docutils.sourceforge.io/docs/howto/html-stylesheets.html for how to
customize this style sheet.
Expand Down Expand Up @@ -274,7 +275,7 @@
margin-left: 2em ;
margin-right: 2em }

pre.code .ln { color: grey; } /* line numbers */
pre.code .ln { color: gray; } /* line numbers */
pre.code, code { background-color: #eeeeee }
pre.code .comment, code .comment { color: #5C6576 }
pre.code .keyword, code .keyword { color: #3B0D06; font-weight: bold }
Expand All @@ -300,7 +301,7 @@
span.pre {
white-space: pre }

span.problematic {
span.problematic, pre.problematic {
color: red }

span.section-subtitle {
Expand Down Expand Up @@ -430,7 +431,9 @@ <h2><a class="toc-backref" href="#toc-entry-5">Contributors</a></h2>
<div class="section" id="maintainers">
<h2><a class="toc-backref" href="#toc-entry-6">Maintainers</a></h2>
<p>This module is maintained by the OCA.</p>
<a class="reference external image-reference" href="https://odoo-community.org"><img alt="Odoo Community Association" src="https://odoo-community.org/logo.png" /></a>
<a class="reference external image-reference" href="https://odoo-community.org">
<img alt="Odoo Community Association" src="https://odoo-community.org/logo.png" />
</a>
<p>OCA, or the Odoo Community Association, is a nonprofit organization whose
mission is to support the collaborative development of Odoo features and
promote its widespread use.</p>
Expand Down
32 changes: 15 additions & 17 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,8 +181,7 @@ 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:
Expand All @@ -192,12 +190,12 @@ def test_picking_secondary_unit(self):
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)
self.assertEqual(move.quantity, 0.5)
move.secondary_uom_qty = 2
self.assertEqual(move.qty_done, 1)
self.assertEqual(move.quantity, 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.assertEqual(move.quantity, 1.8)
move.quantity = 5
self.assertAlmostEqual(move.secondary_uom_qty, 5.56, 2)

def test_secondary_unit_merge_move_diff_uom(self):
Expand All @@ -211,14 +209,14 @@ 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 +229,12 @@ 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
22 changes: 10 additions & 12 deletions stock_secondary_unit/views/stock_move_views.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,22 @@
<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"
optional="show"
groups="uom.group_uom"
/>
<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}"
optional="show"
groups="uom.group_uom"
/>
</field>
</field>
Expand All @@ -34,23 +33,22 @@
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"
optional="show"
groups="uom.group_uom"
/>
<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}"
optional="show"
groups="uom.group_uom"
/>
</field>
</field>
Expand Down
Loading

0 comments on commit 489e38d

Please sign in to comment.