diff --git a/setup/stock_procurement_group_hook/odoo/addons/stock_procurement_group_hook b/setup/stock_procurement_group_hook/odoo/addons/stock_procurement_group_hook new file mode 120000 index 000000000..1f861e53c --- /dev/null +++ b/setup/stock_procurement_group_hook/odoo/addons/stock_procurement_group_hook @@ -0,0 +1 @@ +../../../../stock_procurement_group_hook \ No newline at end of file diff --git a/setup/stock_procurement_group_hook/setup.py b/setup/stock_procurement_group_hook/setup.py new file mode 100644 index 000000000..28c57bb64 --- /dev/null +++ b/setup/stock_procurement_group_hook/setup.py @@ -0,0 +1,6 @@ +import setuptools + +setuptools.setup( + setup_requires=['setuptools-odoo'], + odoo_addon=True, +) diff --git a/stock_procurement_group_hook/README.rst b/stock_procurement_group_hook/README.rst new file mode 100644 index 000000000..d12c04c87 --- /dev/null +++ b/stock_procurement_group_hook/README.rst @@ -0,0 +1,75 @@ +============================ +Stock Procurement Group Hook +============================ + +.. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! 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-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/14.0/stock_procurement_group_hook + :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-14-0/stock-logistics-warehouse-14-0-stock_procurement_group_hook + :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/14.0 + :alt: Try me on Runbot + +|badge1| |badge2| |badge3| |badge4| |badge5| + +This module adds a Hook to Procurement Group run method. + +This is a technical module and does not provide any functionally by itself. + +**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 +~~~~~~~ + +* ForgeFlow + +Contributors +~~~~~~~~~~~~ + +* Lois Rilo Antelo + +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_procurement_group_hook/__init__.py b/stock_procurement_group_hook/__init__.py new file mode 100644 index 000000000..5bf67492b --- /dev/null +++ b/stock_procurement_group_hook/__init__.py @@ -0,0 +1,2 @@ +from . import models +from .hooks import post_load_hook diff --git a/stock_procurement_group_hook/__manifest__.py b/stock_procurement_group_hook/__manifest__.py new file mode 100644 index 000000000..3853e9d86 --- /dev/null +++ b/stock_procurement_group_hook/__manifest__.py @@ -0,0 +1,15 @@ +# Copyright 2023 ForgeFlow S.L. +# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl). +{ + "name": "Stock Procurement Group Hook", + "summary": "Adds Hook to Procurement Group run method.", + "version": "14.0.1.0.0", + "category": "Inventory", + "website": "https://github.com/OCA/stock-logistics-warehouse", + "author": "ForgeFlow, Odoo Community Association (OCA)", + "license": "LGPL-3", + "application": False, + "installable": True, + "depends": ["stock"], + "post_load": "post_load_hook", +} diff --git a/stock_procurement_group_hook/hooks.py b/stock_procurement_group_hook/hooks.py new file mode 100644 index 000000000..fd633e4df --- /dev/null +++ b/stock_procurement_group_hook/hooks.py @@ -0,0 +1,76 @@ +# Copyright 2023 ForgeFlow S.L. +# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl). + +import logging +from collections import defaultdict + +from odoo import _, api, fields +from odoo.exceptions import UserError + +from odoo.addons.stock.models.stock_rule import ProcurementException, ProcurementGroup + +_logger = logging.getLogger(__name__) + + +def post_load_hook(): + + # Changes done to the original method are highlighted with the comments + # "START/END OF CHANGES" + @api.model + def run_new(self, procurements, raise_user_error=True): + def raise_exception(procurement_errors): + if raise_user_error: + dummy, errors = zip(*procurement_errors) + raise UserError("\n".join(errors)) + else: + raise ProcurementException(procurement_errors) + + actions_to_run = defaultdict(list) + procurement_errors = [] + for procurement in procurements: + procurement.values.setdefault( + "company_id", procurement.location_id.company_id + ) + procurement.values.setdefault("priority", "0") + procurement.values.setdefault("date_planned", fields.Datetime.now()) + # START OF CHANGES + if self._skip_procurement(procurement): + # END OF CHANGES + continue + rule = self._get_rule( + procurement.product_id, procurement.location_id, procurement.values + ) + if not rule: + error = _( + 'No rule has been found to replenish "%s" in "%s".\n' + "Verify the routes configuration on the product." + ) % ( + procurement.product_id.display_name, + procurement.location_id.display_name, + ) + procurement_errors.append((procurement, error)) + else: + action = "pull" if rule.action == "pull_push" else rule.action + actions_to_run[action].append((procurement, rule)) + + if procurement_errors: + raise_exception(procurement_errors) + + for action, procurements in actions_to_run.items(): + if hasattr(self.env["stock.rule"], "_run_%s" % action): + try: + getattr(self.env["stock.rule"], "_run_%s" % action)(procurements) + except ProcurementException as e: + procurement_errors += e.procurement_exceptions + else: + _logger.error( + "The method _run_%s doesn't exist on the procurement rules" % action + ) + + if procurement_errors: + raise_exception(procurement_errors) + return True + + if not hasattr(ProcurementGroup, "run_original"): + ProcurementGroup.run_original = ProcurementGroup.run + ProcurementGroup.run = run_new diff --git a/stock_procurement_group_hook/models/__init__.py b/stock_procurement_group_hook/models/__init__.py new file mode 100644 index 000000000..898acee04 --- /dev/null +++ b/stock_procurement_group_hook/models/__init__.py @@ -0,0 +1 @@ +from . import procurement_group diff --git a/stock_procurement_group_hook/models/procurement_group.py b/stock_procurement_group_hook/models/procurement_group.py new file mode 100644 index 000000000..db29ba13a --- /dev/null +++ b/stock_procurement_group_hook/models/procurement_group.py @@ -0,0 +1,15 @@ +# Copyright 2023 ForgeFlow S.L. +# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl). + +from odoo import api, models +from odoo.tools import float_is_zero + + +class ProcurementGroup(models.Model): + _inherit = "procurement.group" + + @api.model + def _skip_procurement(self, procurement): + return procurement.product_id.type not in ("consu", "product") or float_is_zero( + procurement.product_qty, precision_rounding=procurement.product_uom.rounding + ) diff --git a/stock_procurement_group_hook/readme/CONTRIBUTORS.rst b/stock_procurement_group_hook/readme/CONTRIBUTORS.rst new file mode 100644 index 000000000..611afb02f --- /dev/null +++ b/stock_procurement_group_hook/readme/CONTRIBUTORS.rst @@ -0,0 +1 @@ +* Lois Rilo Antelo diff --git a/stock_procurement_group_hook/readme/DESCRIPTION.rst b/stock_procurement_group_hook/readme/DESCRIPTION.rst new file mode 100644 index 000000000..5795c2c51 --- /dev/null +++ b/stock_procurement_group_hook/readme/DESCRIPTION.rst @@ -0,0 +1,3 @@ +This module adds a Hook to Procurement Group run method. + +This is a technical module and does not provide any functionally by itself. diff --git a/stock_procurement_group_hook/static/description/icon.png b/stock_procurement_group_hook/static/description/icon.png new file mode 100644 index 000000000..3a0328b51 Binary files /dev/null and b/stock_procurement_group_hook/static/description/icon.png differ diff --git a/stock_procurement_group_hook/static/description/index.html b/stock_procurement_group_hook/static/description/index.html new file mode 100644 index 000000000..f952901a4 --- /dev/null +++ b/stock_procurement_group_hook/static/description/index.html @@ -0,0 +1,420 @@ + + + + + + +Stock Procurement Group Hook + + + +
+

Stock Procurement Group Hook

+ + +

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

+

This module adds a Hook to Procurement Group run method.

+

This is a technical module and does not provide any functionally by itself.

+

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

+
    +
  • ForgeFlow
  • +
+
+
+

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.

+
+
+
+ +