diff --git a/stock_move_auto_assign/README.rst b/stock_move_auto_assign/README.rst new file mode 100644 index 000000000..bbdc20f85 --- /dev/null +++ b/stock_move_auto_assign/README.rst @@ -0,0 +1,90 @@ +====================== +Stock Move Auto Assign +====================== + +.. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! 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--warehouse-lightgray.png?logo=github + :target: https://github.com/OCA/stock-logistics-warehouse/tree/13.0/stock_move_auto_assign + :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-13-0/stock-logistics-warehouse-13-0-stock_move_auto_assign + :alt: Translate me on Weblate +.. |badge5| image:: https://img.shields.io/badge/runbot-Try%20me-875A7B.png + :target: https://runbot.odoo-community.org/runbot/153/13.0 + :alt: Try me on Runbot + +|badge1| |badge2| |badge3| |badge4| |badge5| + +Automatically check availability of stock moves when a move is set to "done". + +It uses queue jobs to verify the availability in order to have a minimal impact +on the user operations. + +The conditions to trigger the check are: + +* A move is marked as done +* The destination locations of the move lines are internal +* The move doesn't have successors in a chain of moves + +At this point, jobs are generated: + +* One job per product +* Any move waiting for stock in a parent (or same) location of the internal + destination locations from the done move has its availability checked + +Only one job is generated for an identical set of (product, locations). + +**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 smashing it by providing a detailed and welcomed +`feedback `_. + +Do not contact contributors directly about support or help with technical issues. + +Credits +======= + +Authors +~~~~~~~ + +* Camptocamp + +Contributors +~~~~~~~~~~~~ + +* Guewen Baconnier + +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_auto_assign/__init__.py b/stock_move_auto_assign/__init__.py new file mode 100644 index 000000000..0650744f6 --- /dev/null +++ b/stock_move_auto_assign/__init__.py @@ -0,0 +1 @@ +from . import models diff --git a/stock_move_auto_assign/__manifest__.py b/stock_move_auto_assign/__manifest__.py new file mode 100644 index 000000000..15804e3df --- /dev/null +++ b/stock_move_auto_assign/__manifest__.py @@ -0,0 +1,20 @@ +# Copyright 2020 Camptocamp SA +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). + +{ + "name": "Stock Move Auto Assign", + "summary": "Try to reserve moves when goods enter in a location", + "version": "13.0.1.0.1", + "author": "Camptocamp, Odoo Community Association (OCA)", + "website": "https://github.com/OCA/stock-logistics-warehouse", + "category": "Stock Management", + "depends": [ + "stock", + # OCA/queue + "queue_job", + ], + "data": [], + "installable": True, + "development_status": "Beta", + "license": "AGPL-3", +} diff --git a/stock_move_auto_assign/i18n/stock_move_auto_assign.pot b/stock_move_auto_assign/i18n/stock_move_auto_assign.pot new file mode 100644 index 000000000..583aa2719 --- /dev/null +++ b/stock_move_auto_assign/i18n/stock_move_auto_assign.pot @@ -0,0 +1,30 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * stock_move_auto_assign +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 13.0\n" +"Report-Msgid-Bugs-To: \n" +"Last-Translator: \n" +"Language-Team: \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: \n" +"Plural-Forms: \n" + +#. module: stock_move_auto_assign +#: model:ir.model,name:stock_move_auto_assign.model_product_product +msgid "Product" +msgstr "" + +#. module: stock_move_auto_assign +#: model:ir.model,name:stock_move_auto_assign.model_stock_move +msgid "Stock Move" +msgstr "" + +#. module: stock_move_auto_assign +#: code:addons/stock_move_auto_assign/models/stock_move.py:0 +#, python-format +msgid "Try reserving \"{}\" for quantities added in: {}" +msgstr "" diff --git a/stock_move_auto_assign/models/__init__.py b/stock_move_auto_assign/models/__init__.py new file mode 100644 index 000000000..1f887626e --- /dev/null +++ b/stock_move_auto_assign/models/__init__.py @@ -0,0 +1,2 @@ +from . import stock_move +from . import product_product diff --git a/stock_move_auto_assign/models/product_product.py b/stock_move_auto_assign/models/product_product.py new file mode 100644 index 000000000..5310950a5 --- /dev/null +++ b/stock_move_auto_assign/models/product_product.py @@ -0,0 +1,41 @@ +# Copyright 2020 Camptocamp SA +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). + +from odoo import models + +from odoo.addons.queue_job.job import job + + +class ProductProduct(models.Model): + _inherit = "product.product" + + def _moves_auto_assign_domain(self, locations): + return [ + ("product_id", "=", self.id), + ("location_id", "parent_of", locations.ids), + ("state", "in", ("confirmed", "partially_available")), + # useless to try reserving a move that waits on another move + # or is MTO + ("move_orig_ids", "=", False), + ("procure_method", "=", "make_to_stock"), + # Do not filter on product_id.type by default because it uses an + # additional query on product_product and product_template. + # StockMove._prepare_auto_assign() already filtered out + # non-stockable products and # `_action_assign()` would filter them + # out anyway. + ] + + @job(default_channel="root.stock_auto_assign") + def moves_auto_assign(self, locations): + """Try to reserve moves based on product and locations + + When a product has been added to a location, it searches all* + the moves with a source equal or above this location and try + to reserve them. + + * all the moves that would make sense to reserve, so no chained + moves, no MTO, ... + """ + self.ensure_one() + moves = self.env["stock.move"].search(self._moves_auto_assign_domain(locations)) + moves._action_assign() diff --git a/stock_move_auto_assign/models/stock_move.py b/stock_move_auto_assign/models/stock_move.py new file mode 100644 index 000000000..1979d8da5 --- /dev/null +++ b/stock_move_auto_assign/models/stock_move.py @@ -0,0 +1,57 @@ +# Copyright 2020 Camptocamp SA +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). + +from collections import defaultdict + +from odoo import _, models + +from odoo.addons.queue_job.job import identity_exact + + +class StockMove(models.Model): + _inherit = "stock.move" + + def _action_done(self, cancel_backorder=False): + done_moves = super()._action_done(cancel_backorder=cancel_backorder) + done_moves._prepare_auto_assign() + return done_moves + + def _prepare_auto_assign(self): + product_locs = defaultdict(set) + for move in self: + # select internal locations where we moved goods not for the + # purpose of another move (so no further destination move) + if move.move_dest_ids: + continue + product = move.product_id + if product.type != "product": + continue + locations = move.mapped("move_line_ids.location_dest_id").filtered( + lambda l: l.usage == "internal" + ) + product_locs[product.id].update(locations.ids) + + for product_id, location_ids in product_locs.items(): + if not location_ids: + continue + self._enqueue_auto_assign( + self.env["product.product"].browse(product_id), + self.env["stock.location"].browse(location_ids), + ) + + def _enqueue_auto_assign(self, product, locations, **job_options): + """Enqueue a job ProductProduct.moves_auto_assign() + + Can be extended to pass different options to the job (priority, ...). + The usage of `.setdefault` allows to override the options set by default. + """ + job_options = job_options.copy() + job_options.setdefault( + "description", + _('Try reserving "{}" for quantities added in: {}').format( + product.display_name, ", ".join(locations.mapped("name")) + ), + ) + # do not enqueue 2 jobs for the same product and locations set + job_options.setdefault("identity_key", identity_exact) + product.with_delay(**job_options).moves_auto_assign(locations) diff --git a/stock_move_auto_assign/readme/CONTRIBUTORS.rst b/stock_move_auto_assign/readme/CONTRIBUTORS.rst new file mode 100644 index 000000000..48286263c --- /dev/null +++ b/stock_move_auto_assign/readme/CONTRIBUTORS.rst @@ -0,0 +1 @@ +* Guewen Baconnier diff --git a/stock_move_auto_assign/readme/DESCRIPTION.rst b/stock_move_auto_assign/readme/DESCRIPTION.rst new file mode 100644 index 000000000..611da0e11 --- /dev/null +++ b/stock_move_auto_assign/readme/DESCRIPTION.rst @@ -0,0 +1,18 @@ +Automatically check availability of stock moves when a move is set to "done". + +It uses queue jobs to verify the availability in order to have a minimal impact +on the user operations. + +The conditions to trigger the check are: + +* A move is marked as done +* The destination locations of the move lines are internal +* The move doesn't have successors in a chain of moves + +At this point, jobs are generated: + +* One job per product +* Any move waiting for stock in a parent (or same) location of the internal + destination locations from the done move has its availability checked + +Only one job is generated for an identical set of (product, locations). diff --git a/stock_move_auto_assign/static/description/icon.png b/stock_move_auto_assign/static/description/icon.png new file mode 100644 index 000000000..3a0328b51 Binary files /dev/null and b/stock_move_auto_assign/static/description/icon.png differ diff --git a/stock_move_auto_assign/static/description/index.html b/stock_move_auto_assign/static/description/index.html new file mode 100644 index 000000000..72ef17756 --- /dev/null +++ b/stock_move_auto_assign/static/description/index.html @@ -0,0 +1,434 @@ + + + + + + +Stock Move Auto Assign + + + +
+

Stock Move Auto Assign

+ + +

Beta License: AGPL-3 OCA/stock-logistics-warehouse Translate me on Weblate Try me on Runbot

+

Automatically check availability of stock moves when a move is set to “done”.

+

It uses queue jobs to verify the availability in order to have a minimal impact +on the user operations.

+

The conditions to trigger the check are:

+
    +
  • A move is marked as done
  • +
  • The destination locations of the move lines are internal
  • +
  • The move doesn’t have successors in a chain of moves
  • +
+

At this point, jobs are generated:

+
    +
  • One job per product
  • +
  • Any move waiting for stock in a parent (or same) location of the internal +destination locations from the done move has its availability checked
  • +
+

Only one job is generated for an identical set of (product, locations).

+

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

+

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

+
+
+

Credits

+
+

Authors

+
    +
  • Camptocamp
  • +
+
+
+

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-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_auto_assign/tests/__init__.py b/stock_move_auto_assign/tests/__init__.py new file mode 100644 index 000000000..4b002ea91 --- /dev/null +++ b/stock_move_auto_assign/tests/__init__.py @@ -0,0 +1 @@ +from . import test_auto_assign diff --git a/stock_move_auto_assign/tests/test_auto_assign.py b/stock_move_auto_assign/tests/test_auto_assign.py new file mode 100644 index 000000000..2d389ec74 --- /dev/null +++ b/stock_move_auto_assign/tests/test_auto_assign.py @@ -0,0 +1,129 @@ +# Copyright 2020 Camptocamp SA +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). + +from odoo.tests import SavepointCase + +from odoo.addons.queue_job.job import identity_exact +from odoo.addons.queue_job.tests.common import mock_with_delay + + +class TestStockMoveAutoAssign(SavepointCase): + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.env = cls.env(context=dict(cls.env.context, tracking_disable=True)) + + cls.wh = cls.env.ref("stock.warehouse0") + cls.out_type = cls.wh.out_type_id + cls.in_type = cls.wh.in_type_id + cls.int_type = cls.wh.int_type_id + + cls.customer_loc = cls.env.ref("stock.stock_location_customers") + cls.supplier_loc = cls.env.ref("stock.stock_location_suppliers") + cls.shelf1_loc = cls.env.ref("stock.stock_location_components") + cls.shelf2_loc = cls.env.ref("stock.stock_location_14") + + cls.product = cls.env["product.product"].create( + {"name": "Product", "type": "product"} + ) + + def _create_move( + self, + product, + picking_type, + qty=1.0, + state="confirmed", + procure_method="make_to_stock", + move_dest=None, + ): + source = picking_type.default_location_src_id or self.supplier_loc + dest = picking_type.default_location_dest_id or self.customer_loc + move_vals = { + "name": product.name, + "product_id": product.id, + "product_uom_qty": qty, + "product_uom": product.uom_id.id, + "picking_type_id": picking_type.id, + "location_id": source.id, + "location_dest_id": dest.id, + "state": state, + "procure_method": procure_method, + } + if move_dest: + move_vals["move_dest_ids"] = [(4, move_dest.id, False)] + return self.env["stock.move"].create(move_vals) + + def _update_qty_in_location(self, location, product, quantity): + self.env["stock.quant"]._update_available_quantity(product, location, quantity) + + def test_job_assign_confirmed_move(self): + """Test job method, assign moves matching product and location""" + move1 = self._create_move(self.product, self.out_type) + move2 = self._create_move(self.product, self.out_type) + # put stock in Stock/Shelf 1, the move has a source location in Stock + self._update_qty_in_location(self.shelf1_loc, self.product, 100) + self.product.moves_auto_assign(self.shelf1_loc) + self.assertEqual(move1.state, "assigned") + self.assertEqual(move2.state, "assigned") + + def test_move_done_enqueue_job(self): + """A move done enqueue a new job to assign other moves""" + move = self._create_move(self.product, self.in_type, qty=100) + move._action_assign() + move.move_line_ids.qty_done = 50 + move.move_line_ids.location_dest_id = self.shelf1_loc.id + move.move_line_ids.copy( + default={"qty_done": 50, "location_dest_id": self.shelf2_loc.id} + ) + with mock_with_delay() as (delayable_cls, delayable): + move._action_done() + # .with_delay() has been called once + self.assertEqual(delayable_cls.call_count, 1) + delay_args, delay_kwargs = delayable_cls.call_args + # .with_delay() is called on self.product + self.assertEqual(delay_args, (self.product,)) + # .with_delay() with the following options + self.assertEqual(delay_kwargs.get("identity_key"), identity_exact) + # check what's passed to the job method 'moves_auto_assign' + self.assertEqual(delayable.moves_auto_assign.call_count, 1) + delay_args, delay_kwargs = delayable.moves_auto_assign.call_args + self.assertEqual(delay_args, (self.shelf1_loc | self.shelf2_loc,)) + self.assertDictEqual(delay_kwargs, {}) + + def test_move_done_service_no_job(self): + """Service products do not enqueue job""" + self.product.type = "service" + move = self._create_move(self.product, self.in_type, qty=1) + move._action_assign() + move.move_line_ids.qty_done = 1 + move.move_line_ids.location_dest_id = self.shelf1_loc.id + with mock_with_delay() as (delayable_cls, delayable): + move._action_done() + # .with_delay() has not been called + self.assertEqual(delayable_cls.call_count, 0) + + def test_move_done_chained_no_job(self): + """A move chained to another does not enqueue job""" + move_out = self._create_move( + self.product, self.out_type, qty=1, state="waiting" + ) + move = self._create_move(self.product, self.in_type, qty=1, move_dest=move_out) + move._action_assign() + move.move_line_ids.qty_done = 1 + move.move_line_ids.location_dest_id = self.shelf1_loc.id + with mock_with_delay() as (delayable_cls, delayable): + move._action_done() + # .with_delay() has not been called + self.assertEqual(delayable_cls.call_count, 0) + + def test_move_done_customer_no_job(self): + """A move with other destination than internal does not enqueue job""" + move = self._create_move(self.product, self.out_type, qty=1) + self._update_qty_in_location(self.shelf1_loc, self.product, 1) + move._action_assign() + move.move_line_ids.qty_done = 1 + move.move_line_ids.location_dest_id = self.customer_loc + with mock_with_delay() as (delayable_cls, delayable): + move._action_done() + # .with_delay() has not been called + self.assertEqual(delayable_cls.call_count, 0)