diff --git a/setup/stock_move_purchase_uom/odoo/addons/stock_move_purchase_uom b/setup/stock_move_purchase_uom/odoo/addons/stock_move_purchase_uom new file mode 120000 index 000000000..5277887ff --- /dev/null +++ b/setup/stock_move_purchase_uom/odoo/addons/stock_move_purchase_uom @@ -0,0 +1 @@ +../../../../stock_move_purchase_uom \ No newline at end of file diff --git a/setup/stock_move_purchase_uom/setup.py b/setup/stock_move_purchase_uom/setup.py new file mode 100644 index 000000000..28c57bb64 --- /dev/null +++ b/setup/stock_move_purchase_uom/setup.py @@ -0,0 +1,6 @@ +import setuptools + +setuptools.setup( + setup_requires=['setuptools-odoo'], + odoo_addon=True, +) diff --git a/stock_move_purchase_uom/README.rst b/stock_move_purchase_uom/README.rst new file mode 100644 index 000000000..903696fd2 --- /dev/null +++ b/stock_move_purchase_uom/README.rst @@ -0,0 +1,77 @@ +======================= +Stock Move Purchase UoM +======================= + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:5474d629f67853c99209a7735a2cffe885088232cd8b42903e3ce753f4a1738f + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png + :target: https://odoo-community.org/page/development-status + :alt: Beta +.. |badge2| image:: https://img.shields.io/badge/licence-LGPL--3-blue.png + :target: http://www.gnu.org/licenses/lgpl-3.0-standalone.html + :alt: License: LGPL-3 +.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fstock--logistics--warehouse-lightgray.png?logo=github + :target: https://github.com/OCA/stock-logistics-warehouse/tree/15.0/stock_move_purchase_uom + :alt: OCA/stock-logistics-warehouse +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/stock-logistics-warehouse-15-0/stock-logistics-warehouse-15-0-stock_move_purchase_uom + :alt: Translate me on Weblate +.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png + :target: https://runboat.odoo-community.org/builds?repo=OCA/stock-logistics-warehouse&target_branch=15.0 + :alt: Try me on Runboat + +|badge1| |badge2| |badge3| |badge4| |badge5| + +This module adds a check in the operation types form that allows users to decide if they want to use the purchase UoM +for the moves of that operation type. + +**Table of contents** + +.. contents:: + :local: + +Bug Tracker +=========== + +Bugs are tracked on `GitHub Issues `_. +In case of trouble, please check there if your issue has already been reported. +If you spotted it first, help us to smash it by providing a detailed and welcomed +`feedback `_. + +Do not contact contributors directly about support or help with technical issues. + +Credits +======= + +Authors +~~~~~~~ + +* ForgeFlow + +Contributors +~~~~~~~~~~~~ + +* Marina Alapont + +Maintainers +~~~~~~~~~~~ + +This module is maintained by the OCA. + +.. image:: https://odoo-community.org/logo.png + :alt: Odoo Community Association + :target: https://odoo-community.org + +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. + +This module is part of the `OCA/stock-logistics-warehouse `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/stock_move_purchase_uom/__init__.py b/stock_move_purchase_uom/__init__.py new file mode 100644 index 000000000..0650744f6 --- /dev/null +++ b/stock_move_purchase_uom/__init__.py @@ -0,0 +1 @@ +from . import models diff --git a/stock_move_purchase_uom/__manifest__.py b/stock_move_purchase_uom/__manifest__.py new file mode 100644 index 000000000..d9364508c --- /dev/null +++ b/stock_move_purchase_uom/__manifest__.py @@ -0,0 +1,16 @@ +# Copyright 2024 ForgeFlow S.L. (https://www.forgeflow.com) +# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html). +{ + "name": "Stock Move Purchase UoM", + "summary": "Allow to use the purchase UoM in a stock move", + "version": "15.0.1.0.1", + "author": "ForgeFlow, Odoo Community Association (OCA)", + "website": "https://github.com/OCA/stock-logistics-warehouse", + "category": "Warehouse", + "depends": ["stock"], + "data": [ + "views/stock_picking_type_views.xml", + ], + "license": "LGPL-3", + "installable": True, +} diff --git a/stock_move_purchase_uom/models/__init__.py b/stock_move_purchase_uom/models/__init__.py new file mode 100644 index 000000000..75644b204 --- /dev/null +++ b/stock_move_purchase_uom/models/__init__.py @@ -0,0 +1,3 @@ +from . import stock_move +from . import stock_move_line +from . import stock_picking_type diff --git a/stock_move_purchase_uom/models/stock_move.py b/stock_move_purchase_uom/models/stock_move.py new file mode 100644 index 000000000..2b65850c5 --- /dev/null +++ b/stock_move_purchase_uom/models/stock_move.py @@ -0,0 +1,17 @@ +# Copyright 2024 ForgeFlow S.L. (https://www.forgeflow.com) +# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html). +from odoo import api, models + + +class StockMove(models.Model): + _inherit = "stock.move" + + @api.onchange("product_id", "picking_type_id") + def _onchange_product_id(self): + res = super()._onchange_product_id() + if self.product_id: + if self.picking_type_id and self.picking_type_id.use_purchase_uom: + self.product_uom = self.product_id.with_context( + lang=self._get_lang() + ).uom_po_id.id + return res diff --git a/stock_move_purchase_uom/models/stock_move_line.py b/stock_move_purchase_uom/models/stock_move_line.py new file mode 100644 index 000000000..7dec17b48 --- /dev/null +++ b/stock_move_purchase_uom/models/stock_move_line.py @@ -0,0 +1,23 @@ +# Copyright 2024 ForgeFlow S.L. (https://www.forgeflow.com) +# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html). +from odoo import api, models + + +class StockMoveLine(models.Model): + _inherit = "stock.move.line" + + @api.onchange("product_id", "product_uom_id") + def _onchange_product_id(self): + origin_product_uom = self.product_uom_id + res = super()._onchange_product_id() + # We impose the purchase uom if the operation type has the use_purchase_uom enabled + # and we are creating the line and the stock_move_line is not linked to a stock_move. + if ( + self.picking_type_id + and self.picking_type_id.use_purchase_uom + and not self.move_id + and not origin_product_uom + and self.product_id + ): + self.product_uom_id = self.product_id.uom_po_id.id + return res diff --git a/stock_move_purchase_uom/models/stock_picking_type.py b/stock_move_purchase_uom/models/stock_picking_type.py new file mode 100644 index 000000000..a687ee3a4 --- /dev/null +++ b/stock_move_purchase_uom/models/stock_picking_type.py @@ -0,0 +1,12 @@ +# Copyright 2024 ForgeFlow S.L. (https://www.forgeflow.com) +# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html). +from odoo import fields, models + + +class PickingType(models.Model): + _inherit = "stock.picking.type" + + use_purchase_uom = fields.Boolean( + help="Use the product purchase UoM instead of the default UoM " + "for the moves belonging to this operation type" + ) diff --git a/stock_move_purchase_uom/readme/CONTRIBUTORS.rst b/stock_move_purchase_uom/readme/CONTRIBUTORS.rst new file mode 100644 index 000000000..dbcbc9316 --- /dev/null +++ b/stock_move_purchase_uom/readme/CONTRIBUTORS.rst @@ -0,0 +1 @@ +* Marina Alapont diff --git a/stock_move_purchase_uom/readme/DESCRIPTION.rst b/stock_move_purchase_uom/readme/DESCRIPTION.rst new file mode 100644 index 000000000..60f11698d --- /dev/null +++ b/stock_move_purchase_uom/readme/DESCRIPTION.rst @@ -0,0 +1,2 @@ +This module adds a check in the operation types form that allows users to decide if they want to use the purchase UoM +for the moves of that operation type. diff --git a/stock_move_purchase_uom/static/description/index.html b/stock_move_purchase_uom/static/description/index.html new file mode 100644 index 000000000..f522dacc8 --- /dev/null +++ b/stock_move_purchase_uom/static/description/index.html @@ -0,0 +1,421 @@ + + + + + +Stock Move Purchase UoM + + + +
+

Stock Move Purchase UoM

+ + +

Beta License: LGPL-3 OCA/stock-logistics-warehouse Translate me on Weblate Try me on Runboat

+

This module adds a check in the operation types form that allows users to decide if they want to use the purchase UoM +for the moves of that operation type.

+

Table of contents

+ +
+

Bug Tracker

+

Bugs are tracked on GitHub Issues. +In case of trouble, please check there if your issue has already been reported. +If you spotted it first, help us to smash it by providing a detailed and welcomed +feedback.

+

Do not contact contributors directly about support or help with technical issues.

+
+
+

Credits

+
+

Authors

+
    +
  • ForgeFlow
  • +
+
+ +
+

Maintainers

+

This module is maintained by the OCA.

+Odoo Community Association +

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.

+

This module is part of the OCA/stock-logistics-warehouse project on GitHub.

+

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.

+
+
+
+ + diff --git a/stock_move_purchase_uom/tests/__init__.py b/stock_move_purchase_uom/tests/__init__.py new file mode 100644 index 000000000..f22f6f742 --- /dev/null +++ b/stock_move_purchase_uom/tests/__init__.py @@ -0,0 +1,4 @@ +from . import test_common +from . import test_stock_move +from . import test_stock_move_line +from . import test_stock_move_purchase_uom diff --git a/stock_move_purchase_uom/tests/test_common.py b/stock_move_purchase_uom/tests/test_common.py new file mode 100644 index 000000000..d11cd2141 --- /dev/null +++ b/stock_move_purchase_uom/tests/test_common.py @@ -0,0 +1,46 @@ +# Copyright 2024 ForgeFlow S.L. (https://www.forgeflow.com) +# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html). +from odoo.tests.common import TransactionCase + + +class TestCommon(TransactionCase): + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.partner = cls.env["res.partner"].create({"name": "Test Partner"}) + cls.location = cls.env.ref("stock.stock_location_stock") + cls.location_dest = cls.env["stock.location"].create( + { + "name": "Test dest location", + "usage": cls.env.ref("stock.stock_location_stock").usage, + } + ) + cls.meter_uom = cls.env.ref("uom.product_uom_meter") + cls.cm_uom = cls.env.ref("uom.product_uom_cm") + cls.product = cls.env["product.product"].create( + { + "name": "Test product", + "type": "product", + "categ_id": cls.env.ref("product.product_category_all").id, + "uom_id": cls.cm_uom.id, + "uom_po_id": cls.meter_uom.id, + } + ) + cls.stock_picking_type = cls.env["stock.picking.type"].create( + { + "name": "Internal transfer", + "code": "internal", + "sequence_code": "INT", + } + ) + cls.stock_move = cls.env["stock.move"].create( + { + "name": cls.product.display_name, + "location_id": cls.location.id, + "location_dest_id": cls.location_dest.id, + "product_id": cls.product.id, + "product_uom_qty": 1, + "picking_type_id": cls.stock_picking_type.id, + "product_uom": cls.cm_uom.id, + } + ) diff --git a/stock_move_purchase_uom/tests/test_stock_move.py b/stock_move_purchase_uom/tests/test_stock_move.py new file mode 100644 index 000000000..509320aa7 --- /dev/null +++ b/stock_move_purchase_uom/tests/test_stock_move.py @@ -0,0 +1,18 @@ +# Copyright 2024 ForgeFlow S.L. (https://www.forgeflow.com) +# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html). +from .test_common import TestCommon + + +class TestStockMove(TestCommon): + @classmethod + def setUpClass(cls): + super().setUpClass() + + def test_onchange_product_id(self): + self.stock_move._onchange_product_id() + self.assertEqual(self.stock_move.product_uom.id, self.product.uom_id.id) + + self.stock_picking_type.use_purchase_uom = True + + self.stock_move._onchange_product_id() + self.assertEqual(self.stock_move.product_uom.id, self.product.uom_po_id.id) diff --git a/stock_move_purchase_uom/tests/test_stock_move_line.py b/stock_move_purchase_uom/tests/test_stock_move_line.py new file mode 100644 index 000000000..12edf1020 --- /dev/null +++ b/stock_move_purchase_uom/tests/test_stock_move_line.py @@ -0,0 +1,32 @@ +# Copyright 2024 ForgeFlow S.L. (https://www.forgeflow.com) +# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html). +from odoo.odoo.tests import Form + +from .test_common import TestCommon + + +class TestStockMoveLine(TestCommon): + @classmethod + def setUpClass(cls): + super().setUpClass() + + cls.picking = cls.env["stock.picking"].create( + { + "picking_type_id": cls.stock_picking_type.id, + "location_id": cls.location.id, + "location_dest_id": cls.location_dest.id, + } + ) + + def test_onchange_product_id_use_purchase_uom(self): + self.stock_picking_type.use_purchase_uom = True + stock_move_line = Form(self.env["stock.move.line"]) + stock_move_line.picking_id = self.picking + stock_move_line.product_id = self.product + self.assertEqual(stock_move_line.product_uom_id.id, self.product.uom_po_id.id) + + def test_onchange_product_id_no_use_purchase_uom(self): + stock_move_line = Form(self.env["stock.move.line"]) + stock_move_line.picking_id = self.picking + stock_move_line.product_id = self.product + self.assertEqual(stock_move_line.product_uom_id.id, self.product.uom_id.id) diff --git a/stock_move_purchase_uom/tests/test_stock_move_purchase_uom.py b/stock_move_purchase_uom/tests/test_stock_move_purchase_uom.py new file mode 100644 index 000000000..763585889 --- /dev/null +++ b/stock_move_purchase_uom/tests/test_stock_move_purchase_uom.py @@ -0,0 +1,64 @@ +# Copyright 2024 ForgeFlow S.L. (https://www.forgeflow.com) +# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html). +from odoo.tests import Form + +from .test_common import TestCommon + + +class TestStockMovePurchaseUom(TestCommon): + @classmethod + def setUpClass(cls): + super().setUpClass() + + cls.stock_picking_type_2 = cls.env["stock.picking.type"].create( + { + "name": "Internal transfer 2", + "code": "internal", + "sequence_code": "INT", + "use_purchase_uom": True, + "show_operations": True, + } + ) + + def test_stock_move_purchase_uom_unlinked_move_line(self): + picking_form = Form(self.env["stock.picking"]) + picking_form.partner_id = self.partner + picking_form.picking_type_id = self.stock_picking_type_2 + picking_form.location_id = self.location + picking_form.location_dest_id = self.location_dest + with picking_form.move_ids_without_package.new() as move: + move.product_id = self.product + self.assertEqual(move.product_uom.id, self.product.uom_po_id.id) + picking = picking_form.save() + picking.action_confirm() + with Form(picking) as picking_form: + with picking_form.move_line_ids_without_package.new() as move_line: + move_line.product_id = self.product + # Since the line is not being linked to the move (move_id is false) + # the uom to set is the product purchase uom. + self.assertEqual(move_line.product_uom_id.id, self.product.uom_po_id.id) + + def test_stock_move_purchase_uom_linked_move_line(self): + self.stock_picking_type_2.show_operations = False + picking_form = Form(self.env["stock.picking"]) + picking_form.partner_id = self.partner + picking_form.picking_type_id = self.stock_picking_type_2 + picking_form.location_id = self.location + picking_form.location_dest_id = self.location_dest + with picking_form.move_ids_without_package.new() as move: + move.product_id = self.product + self.assertEqual(move.product_uom.id, self.product.uom_po_id.id) + move.product_uom = self.product.uom_id + self.assertEqual(move.product_uom.id, self.product.uom_id.id) + picking = picking_form.save() + picking.action_confirm() + move = picking.move_ids_without_package[0] + with Form( + move, view=self.env.ref("stock.view_stock_move_operations") + ) as move_form: + with move_form.move_line_ids.new() as move_line: + move_line.product_id = self.product + # Since the line is being linked to the move the uom used will be the move one. + self.assertEqual( + move_line.product_uom_id.id, move_line.move_id.product_uom.id + ) diff --git a/stock_move_purchase_uom/views/stock_picking_type_views.xml b/stock_move_purchase_uom/views/stock_picking_type_views.xml new file mode 100644 index 000000000..37fd6800e --- /dev/null +++ b/stock_move_purchase_uom/views/stock_picking_type_views.xml @@ -0,0 +1,13 @@ + + + + stock.picking.type.form.purchase.uom + stock.picking.type + + + + + + + +