From 825da2a746bad6962164e0db25ab7c3c29f01dcb Mon Sep 17 00:00:00 2001 From: remihb Date: Wed, 26 Apr 2023 14:59:08 +0200 Subject: [PATCH] [MIG] stock_secondary_unit: Migration to 16.0 --- .../odoo/addons/stock_secondary_unit | 1 + setup/stock_secondary_unit/setup.py | 6 +++++ stock_secondary_unit/__manifest__.py | 2 +- stock_secondary_unit/models/__init__.py | 5 +++- .../models/product_product.py | 17 ++++++++++++ .../models/product_template.py | 14 ++++++++++ stock_secondary_unit/models/stock_move.py | 15 ++++++----- ... => stock_product_secondary_unit_mixin.py} | 26 +++---------------- .../tests/test_stock_secondary_unit.py | 26 ++++++++++--------- stock_secondary_unit/views/product_views.xml | 21 +++++++++------ .../views/stock_move_views.xml | 20 ++++++++------ .../views/stock_picking_views.xml | 13 +++++++--- 12 files changed, 103 insertions(+), 63 deletions(-) create mode 120000 setup/stock_secondary_unit/odoo/addons/stock_secondary_unit create mode 100644 setup/stock_secondary_unit/setup.py create mode 100644 stock_secondary_unit/models/product_product.py create mode 100644 stock_secondary_unit/models/product_template.py rename stock_secondary_unit/models/{product.py => stock_product_secondary_unit_mixin.py} (55%) diff --git a/setup/stock_secondary_unit/odoo/addons/stock_secondary_unit b/setup/stock_secondary_unit/odoo/addons/stock_secondary_unit new file mode 120000 index 000000000000..4aaeec1bcb15 --- /dev/null +++ b/setup/stock_secondary_unit/odoo/addons/stock_secondary_unit @@ -0,0 +1 @@ +../../../../stock_secondary_unit \ No newline at end of file diff --git a/setup/stock_secondary_unit/setup.py b/setup/stock_secondary_unit/setup.py new file mode 100644 index 000000000000..28c57bb64031 --- /dev/null +++ b/setup/stock_secondary_unit/setup.py @@ -0,0 +1,6 @@ +import setuptools + +setuptools.setup( + setup_requires=['setuptools-odoo'], + odoo_addon=True, +) diff --git a/stock_secondary_unit/__manifest__.py b/stock_secondary_unit/__manifest__.py index 7569e81a3fcb..7ac653761475 100644 --- a/stock_secondary_unit/__manifest__.py +++ b/stock_secondary_unit/__manifest__.py @@ -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", diff --git a/stock_secondary_unit/models/__init__.py b/stock_secondary_unit/models/__init__.py index 960afa99e789..d342115d5a6d 100644 --- a/stock_secondary_unit/models/__init__.py +++ b/stock_secondary_unit/models/__init__.py @@ -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 diff --git a/stock_secondary_unit/models/product_product.py b/stock_secondary_unit/models/product_product.py new file mode 100644 index 000000000000..6794b98e0d66 --- /dev/null +++ b/stock_secondary_unit/models/product_product.py @@ -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)]", + ) diff --git a/stock_secondary_unit/models/product_template.py b/stock_secondary_unit/models/product_template.py new file mode 100644 index 000000000000..c8b1845aa6e4 --- /dev/null +++ b/stock_secondary_unit/models/product_template.py @@ -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", + ) diff --git a/stock_secondary_unit/models/stock_move.py b/stock_secondary_unit/models/stock_move.py index 22c1580e1584..ff419c2617e5 100644 --- a/stock_secondary_unit/models/stock_move.py +++ b/stock_secondary_unit/models/stock_move.py @@ -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") @@ -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""" @@ -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): diff --git a/stock_secondary_unit/models/product.py b/stock_secondary_unit/models/stock_product_secondary_unit_mixin.py similarity index 55% rename from stock_secondary_unit/models/product.py rename to stock_secondary_unit/models/stock_product_secondary_unit_mixin.py index a57cef0c7cf1..99796f4c1fd1 100644 --- a/stock_secondary_unit/models/product.py +++ b/stock_secondary_unit/models/stock_product_secondary_unit_mixin.py @@ -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)", @@ -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", - ) diff --git a/stock_secondary_unit/tests/test_stock_secondary_unit.py b/stock_secondary_unit/tests/test_stock_secondary_unit.py index 7a300ec05ff1..34ea83e6c5ca 100644 --- a/stock_secondary_unit/tests/test_stock_secondary_unit.py +++ b/stock_secondary_unit/tests/test_stock_secondary_unit.py @@ -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( { @@ -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( @@ -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, @@ -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) @@ -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) @@ -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) @@ -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) diff --git a/stock_secondary_unit/views/product_views.xml b/stock_secondary_unit/views/product_views.xml index 98c89b539b44..a8d80bb6c3f5 100644 --- a/stock_secondary_unit/views/product_views.xml +++ b/stock_secondary_unit/views/product_views.xml @@ -6,13 +6,11 @@ Product template Secondary Unit product.template - - + @@ -34,6 +32,7 @@ attrs="{'invisible':[('type', '!=', 'product')]}" class="oe_stat_button" icon="fa-building-o" + groups="stock.group_stock_user" >
@@ -54,7 +53,6 @@ product.product -