From b87153cabdb1fba9d0a63d74a53b865afd722cf4 Mon Sep 17 00:00:00 2001 From: Telmo Santos Date: Fri, 20 Dec 2024 06:18:39 +0100 Subject: [PATCH] [ADD] stock_move_negative_stock_location --- .../addons/stock_move_negative_stock_location | 1 + .../setup.py | 6 + stock_move_negative_stock_location/README.rst | 85 ++++ .../__init__.py | 1 + .../__manifest__.py | 12 + .../models/__init__.py | 1 + .../models/stock_move.py | 43 ++ .../readme/CONTRIBUTORS.rst | 3 + .../readme/DESCRIPTION.rst | 3 + .../readme/USAGE.rst | 3 + .../static/description/index.html | 431 ++++++++++++++++++ .../tests/__init__.py | 1 + ...test_stock_move_negative_stock_location.py | 54 +++ 13 files changed, 644 insertions(+) create mode 120000 setup/stock_move_negative_stock_location/odoo/addons/stock_move_negative_stock_location create mode 100644 setup/stock_move_negative_stock_location/setup.py create mode 100644 stock_move_negative_stock_location/README.rst create mode 100644 stock_move_negative_stock_location/__init__.py create mode 100644 stock_move_negative_stock_location/__manifest__.py create mode 100644 stock_move_negative_stock_location/models/__init__.py create mode 100644 stock_move_negative_stock_location/models/stock_move.py create mode 100644 stock_move_negative_stock_location/readme/CONTRIBUTORS.rst create mode 100644 stock_move_negative_stock_location/readme/DESCRIPTION.rst create mode 100644 stock_move_negative_stock_location/readme/USAGE.rst create mode 100644 stock_move_negative_stock_location/static/description/index.html create mode 100644 stock_move_negative_stock_location/tests/__init__.py create mode 100644 stock_move_negative_stock_location/tests/test_stock_move_negative_stock_location.py diff --git a/setup/stock_move_negative_stock_location/odoo/addons/stock_move_negative_stock_location b/setup/stock_move_negative_stock_location/odoo/addons/stock_move_negative_stock_location new file mode 120000 index 000000000000..a3efbe47ad38 --- /dev/null +++ b/setup/stock_move_negative_stock_location/odoo/addons/stock_move_negative_stock_location @@ -0,0 +1 @@ +../../../../stock_move_negative_stock_location \ No newline at end of file diff --git a/setup/stock_move_negative_stock_location/setup.py b/setup/stock_move_negative_stock_location/setup.py new file mode 100644 index 000000000000..28c57bb64031 --- /dev/null +++ b/setup/stock_move_negative_stock_location/setup.py @@ -0,0 +1,6 @@ +import setuptools + +setuptools.setup( + setup_requires=['setuptools-odoo'], + odoo_addon=True, +) diff --git a/stock_move_negative_stock_location/README.rst b/stock_move_negative_stock_location/README.rst new file mode 100644 index 000000000000..8ffbee05c871 --- /dev/null +++ b/stock_move_negative_stock_location/README.rst @@ -0,0 +1,85 @@ +================================== +Stock Move Negative Stock Location +================================== + +.. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |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-AGPL--3-blue.png + :target: http://www.gnu.org/licenses/agpl-3.0-standalone.html + :alt: License: AGPL-3 +.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fstock--logistics--workflow-lightgray.png?logo=github + :target: https://github.com/OCA/stock-logistics-workflow/tree/16.0/stock_move_negative_stock_location + :alt: OCA/stock-logistics-workflow +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/stock-logistics-workflow-16-0/stock-logistics-workflow-16-0-stock_move_negative_stock_location + :alt: Translate me on Weblate +.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png + :target: https://runboat.odoo-community.org/webui/builds.html?repo=OCA/stock-logistics-workflow&target_branch=16.0 + :alt: Try me on Runboat + +|badge1| |badge2| |badge3| |badge4| |badge5| + +When making an stock move with a negative quantity, odoo convert it to a positive return move and the locations are inverted. +For stock moves with negative quantities, this module sets the source location with the return +picking type default destination location. Like that destination location is properly set on the return move. + +**Table of contents** + +.. contents:: + :local: + +Usage +===== + +In `Returns`` picking type set the default destination location as `Stock` +Create a stock move from Stock to Customer with a negative quantity and validate it. +That move will be converted into a stock move from Customer to Stock with a positive quantity. + +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 smashing it by providing a detailed and welcomed +`feedback `_. + +Do not contact contributors directly about support or help with technical issues. + +Credits +======= + +Authors +~~~~~~~ + +* Camptocamp +* BCIM + +Contributors +~~~~~~~~~~~~ + +* Telmo Santos +* Sébastien Alix +* Jacques-Etienne Baudoux (BCIM) + +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-workflow `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/stock_move_negative_stock_location/__init__.py b/stock_move_negative_stock_location/__init__.py new file mode 100644 index 000000000000..0650744f6bc6 --- /dev/null +++ b/stock_move_negative_stock_location/__init__.py @@ -0,0 +1 @@ +from . import models diff --git a/stock_move_negative_stock_location/__manifest__.py b/stock_move_negative_stock_location/__manifest__.py new file mode 100644 index 000000000000..9acd62403827 --- /dev/null +++ b/stock_move_negative_stock_location/__manifest__.py @@ -0,0 +1,12 @@ +# Copyright 2024 Camptocamp +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +{ + "name": "Stock Move Negative Stock Location", + "version": "16.0.1.0.0", + "category": "Stock", + "website": "https://github.com/OCA/stock-logistics-workflow", + "author": "Camptocamp, BCIM, Odoo Community Association (OCA)", + "license": "AGPL-3", + "depends": ["stock"], + "installable": True, +} diff --git a/stock_move_negative_stock_location/models/__init__.py b/stock_move_negative_stock_location/models/__init__.py new file mode 100644 index 000000000000..6bda2d2428e0 --- /dev/null +++ b/stock_move_negative_stock_location/models/__init__.py @@ -0,0 +1 @@ +from . import stock_move diff --git a/stock_move_negative_stock_location/models/stock_move.py b/stock_move_negative_stock_location/models/stock_move.py new file mode 100644 index 000000000000..65825077dc4d --- /dev/null +++ b/stock_move_negative_stock_location/models/stock_move.py @@ -0,0 +1,43 @@ +# Copyright 2024 Camptocamp SA +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl) + +from odoo import models +from odoo.tools.float_utils import float_compare + + +class StockMove(models.Model): + _inherit = "stock.move" + + def _action_confirm(self, merge=True, merge_into=False): + """Override to set the location_id of negative return moves to the default + location_dest_id of the return picking type.""" + neg_r_moves = self.filtered( + lambda move: float_compare( + move.product_uom_qty, 0, precision_rounding=move.product_uom.rounding + ) + < 0 + ) + self = self.with_context(neg_r_moves=neg_r_moves.ids) + return super()._action_confirm(merge=merge, merge_into=merge_into) + + def set_negative_return_moves_location(self): + """Set the location_id of negative return moves to the default location_dest_id + of the return picking type.""" + neg_r_moves = self.browse(self.env.context["neg_r_moves"]) + for move in neg_r_moves: + if move.picking_type_id.return_picking_type_id.default_location_dest_id: + move.location_id = ( + move.picking_type_id.return_picking_type_id.default_location_dest_id + ) + + def _check_company(self, fnames=None): + # Set the location_id of negative return moves to the default location_dest_id + # of the return picking type + # We hook into this method used in stock.move._action_confirm + # (see https://github.com/odoo/odoo/blob/ + # 3a63c90fff615b70881131e11d7375af2ae082a6/addons/stock/models/stock_move.py + # #L1381C14-L1381C27) + # to be able to invert location_id and location_dest_id + if self.env.context.get("neg_r_moves"): + self.set_negative_return_moves_location() + return super()._check_company(fnames=fnames) diff --git a/stock_move_negative_stock_location/readme/CONTRIBUTORS.rst b/stock_move_negative_stock_location/readme/CONTRIBUTORS.rst new file mode 100644 index 000000000000..95caab4ef400 --- /dev/null +++ b/stock_move_negative_stock_location/readme/CONTRIBUTORS.rst @@ -0,0 +1,3 @@ +* Telmo Santos +* Sébastien Alix +* Jacques-Etienne Baudoux (BCIM) diff --git a/stock_move_negative_stock_location/readme/DESCRIPTION.rst b/stock_move_negative_stock_location/readme/DESCRIPTION.rst new file mode 100644 index 000000000000..cfb751f8fdd7 --- /dev/null +++ b/stock_move_negative_stock_location/readme/DESCRIPTION.rst @@ -0,0 +1,3 @@ +When making an stock move with a negative quantity, odoo convert it to a positive return move and the locations are inverted. +For stock moves with negative quantities, this module sets the source location with the return +picking type default destination location. Like that destination location is properly set on the return move. \ No newline at end of file diff --git a/stock_move_negative_stock_location/readme/USAGE.rst b/stock_move_negative_stock_location/readme/USAGE.rst new file mode 100644 index 000000000000..f036b25e5e8d --- /dev/null +++ b/stock_move_negative_stock_location/readme/USAGE.rst @@ -0,0 +1,3 @@ +In `Returns`` picking type set the default destination location as `Stock` +Create a stock move from Stock to Customer with a negative quantity and validate it. +That move will be converted into a stock move from Customer to Stock with a positive quantity. \ No newline at end of file diff --git a/stock_move_negative_stock_location/static/description/index.html b/stock_move_negative_stock_location/static/description/index.html new file mode 100644 index 000000000000..88ab90bf16ed --- /dev/null +++ b/stock_move_negative_stock_location/static/description/index.html @@ -0,0 +1,431 @@ + + + + + + +Stock Move Negative Stock Location + + + +
+

Stock Move Negative Stock Location

+ + +

Beta License: AGPL-3 OCA/stock-logistics-workflow Translate me on Weblate Try me on Runboat

+

When making an stock move with a negative quantity, odoo convert it to a positive return move and the locations are inverted. +For stock moves with negative quantities, this module sets the source location with the return +picking type default destination location. Like that destination location is properly set on the return move.

+

Table of contents

+ +
+

Usage

+

In Returns` picking type set the default destination location as Stock +Create a stock move from Stock to Customer with a negative quantity and validate it. +That move will be converted into a stock move from Customer to Stock with a positive quantity.

+
+
+

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 smashing it by providing a detailed and welcomed +feedback.

+

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

+
+
+

Credits

+
+

Authors

+
    +
  • Camptocamp
  • +
  • BCIM
  • +
+
+
+

Contributors

+ +
+
+

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-workflow project on GitHub.

+

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

+
+
+
+ + diff --git a/stock_move_negative_stock_location/tests/__init__.py b/stock_move_negative_stock_location/tests/__init__.py new file mode 100644 index 000000000000..446db974abd8 --- /dev/null +++ b/stock_move_negative_stock_location/tests/__init__.py @@ -0,0 +1 @@ +from . import test_stock_move_negative_stock_location diff --git a/stock_move_negative_stock_location/tests/test_stock_move_negative_stock_location.py b/stock_move_negative_stock_location/tests/test_stock_move_negative_stock_location.py new file mode 100644 index 000000000000..a6d075df0af5 --- /dev/null +++ b/stock_move_negative_stock_location/tests/test_stock_move_negative_stock_location.py @@ -0,0 +1,54 @@ +# Copyright 2024 Camptocamp +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl) +from odoo.tests import common + +from odoo.addons.base.tests.common import DISABLED_MAIL_CONTEXT + + +class TestStockMoveNegativeStockLocation(common.TransactionCase): + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.env = cls.env["base"].with_context(**DISABLED_MAIL_CONTEXT).env + cls.product = cls.env.ref("product.product_product_25") + cls.return_stock_location = cls.env["stock.location"].create( + { + "name": "Return Stock Location", + "usage": "internal", + } + ) + cls.env.ref( + "stock.warehouse0" + ).return_type_id.default_location_dest_id = cls.return_stock_location + + def test_stock_negative_stock_location(self): + """Test that the location_id of negative return moves is set to the + default location_dest_id of the return picking type.""" + location_source = self.env.ref("stock.stock_location_stock") + location_dest = self.env.ref("stock.stock_location_customers") + + # WH: Stock → Customers + rule = self.env["stock.rule"].search( + [ + ("action", "=", "pull"), + ("picking_type_id", "=", self.env.ref("stock.picking_type_out").id), + ("location_src_id", "=", location_source.id), + ("location_dest_id", "=", location_dest.id), + ("procure_method", "=", "make_to_stock"), + ] + ) + + move = self.env["stock.move"].create( + { + "name": "Test Move", + "product_id": self.product.id, + "product_uom_qty": -5, + "product_uom": self.product.uom_id.id, + "location_id": location_source.id, + "location_dest_id": location_dest.id, + "picking_type_id": self.env.ref("stock.picking_type_out").id, + "rule_id": rule.id, + } + ) + move._action_confirm() + self.assertEqual(move.location_dest_id, self.return_stock_location)