diff --git a/setup/stock_quant_task_deferred/odoo/addons/stock_quant_task_deferred b/setup/stock_quant_task_deferred/odoo/addons/stock_quant_task_deferred new file mode 120000 index 00000000000..4697e7f68c2 --- /dev/null +++ b/setup/stock_quant_task_deferred/odoo/addons/stock_quant_task_deferred @@ -0,0 +1 @@ +../../../../stock_quant_task_deferred \ No newline at end of file diff --git a/setup/stock_quant_task_deferred/setup.py b/setup/stock_quant_task_deferred/setup.py new file mode 100644 index 00000000000..28c57bb6403 --- /dev/null +++ b/setup/stock_quant_task_deferred/setup.py @@ -0,0 +1,6 @@ +import setuptools + +setuptools.setup( + setup_requires=['setuptools-odoo'], + odoo_addon=True, +) diff --git a/stock_quant_task_deferred/README.rst b/stock_quant_task_deferred/README.rst new file mode 100644 index 00000000000..1d212053fd7 --- /dev/null +++ b/stock_quant_task_deferred/README.rst @@ -0,0 +1,116 @@ +========================= +Stock Quant Task Deferred +========================= + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:d91c5b8474a86c4cfbb6bc0018af2553324d948cb619a9e1535f06ad76b49988 + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |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/16.0/stock_quant_task_deferred + :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-16-0/stock-logistics-warehouse-16-0-stock_quant_task_deferred + :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=16.0 + :alt: Try me on Runboat + +|badge1| |badge2| |badge3| |badge4| |badge5| + +This module allows to deactivate synchronuous quant tasks and use a cron +job instead. + +The cron job will trigger queue jobs to do the tasks. + +**Table of contents** + +.. contents:: + :local: + +Use Cases / Context +=================== + +- In Odoo, product quantities are regularily reorganized: + + - when opening the product stock from product form + - when unpacking products + +This implies a lot of stock quants are deleted if you have a lot of +stock operations and users in parallel. + +That can causes some DB locks. + +Configuration +============= + +- Go to Inventory > Configuration > Settings > Warehouse. +- Check the box 'Defer Quant Tasks' if you want to deactivate + synchronuous quant tasks. +- Go To Settings > Technical > Scheduled Actions > 'Launch quant tasks'. +- Set the desired cron job configuration. + +Usage +===== + +- After having configured the scheduled task, go to Queue Job and look + for "Executing Quant Tasks" queue job descriptions. + +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 +------- + +* ACSONE SA/NV + +Contributors +------------ + +- Denis Roussel denis.roussel@acsone.eu +- Jacques-Étienne Baudoux je@bcim.be + +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. + +.. |maintainer-rousseldenis| image:: https://github.com/rousseldenis.png?size=40px + :target: https://github.com/rousseldenis + :alt: rousseldenis + +Current `maintainer `__: + +|maintainer-rousseldenis| + +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_quant_task_deferred/__init__.py b/stock_quant_task_deferred/__init__.py new file mode 100644 index 00000000000..0650744f6bc --- /dev/null +++ b/stock_quant_task_deferred/__init__.py @@ -0,0 +1 @@ +from . import models diff --git a/stock_quant_task_deferred/__manifest__.py b/stock_quant_task_deferred/__manifest__.py new file mode 100644 index 00000000000..2c7004917b0 --- /dev/null +++ b/stock_quant_task_deferred/__manifest__.py @@ -0,0 +1,14 @@ +# Copyright 2024 ACSONE SA/NV +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +{ + "name": "Stock Quant Task Deferred", + "summary": """This module allows to defer quant tasks (merge, delete)""", + "version": "16.0.1.0.0", + "license": "AGPL-3", + "author": "ACSONE SA/NV,Odoo Community Association (OCA)", + "website": "https://github.com/OCA/stock-logistics-warehouse", + "maintainers": ["rousseldenis"], + "depends": ["stock", "queue_job"], + "data": ["views/res_config_settings.xml", "data/ir_cron.xml"], +} diff --git a/stock_quant_task_deferred/data/ir_cron.xml b/stock_quant_task_deferred/data/ir_cron.xml new file mode 100644 index 00000000000..5990ca564ab --- /dev/null +++ b/stock_quant_task_deferred/data/ir_cron.xml @@ -0,0 +1,19 @@ + + + + + Launch quant tasks + 1 + days + -1 + + + + model._run_quant_tasks_deferred() + code + + diff --git a/stock_quant_task_deferred/models/__init__.py b/stock_quant_task_deferred/models/__init__.py new file mode 100644 index 00000000000..326c55ea2fe --- /dev/null +++ b/stock_quant_task_deferred/models/__init__.py @@ -0,0 +1,3 @@ +from . import stock_quant +from . import res_company +from . import res_config_settings diff --git a/stock_quant_task_deferred/models/res_company.py b/stock_quant_task_deferred/models/res_company.py new file mode 100644 index 00000000000..7b2c536ebef --- /dev/null +++ b/stock_quant_task_deferred/models/res_company.py @@ -0,0 +1,13 @@ +# Copyright 2024 ACSONE SA/NV +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo import fields, models + + +class ResCompany(models.Model): + + _inherit = "res.company" + + defer_quant_tasks = fields.Boolean( + help="Check this if you want to defer stock quant deletions and merges." + ) diff --git a/stock_quant_task_deferred/models/res_config_settings.py b/stock_quant_task_deferred/models/res_config_settings.py new file mode 100644 index 00000000000..368dac58a69 --- /dev/null +++ b/stock_quant_task_deferred/models/res_config_settings.py @@ -0,0 +1,14 @@ +# Copyright 2024 ACSONE SA/NV +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo import fields, models + + +class ResConfigSettings(models.TransientModel): + + _inherit = "res.config.settings" + + defer_quant_tasks = fields.Boolean( + related="company_id.defer_quant_tasks", + readonly=False, + ) diff --git a/stock_quant_task_deferred/models/stock_quant.py b/stock_quant_task_deferred/models/stock_quant.py new file mode 100644 index 00000000000..05405edf29c --- /dev/null +++ b/stock_quant_task_deferred/models/stock_quant.py @@ -0,0 +1,29 @@ +# Copyright 2024 ACSONE SA/NV +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +from odoo import api, models + +from odoo.addons.queue_job.job import identity_exact + + +class StockQuant(models.Model): + + _inherit = "stock.quant" + + def _quant_tasks(self): + # Introduce a parameter that enable to run the + # function even if disabled in company settings + if self.env.company.defer_quant_tasks and not self.env.context.get( + "run_defer_quant_tasks" + ): + return + return super()._quant_tasks() + + @api.model + def _quant_tasks_deferred(self): + self.with_context(run_defer_quant_tasks=True)._quant_tasks() + + @api.model + def _run_quant_tasks_deferred(self): + self.with_delay( + description="Executing Quant Tasks", identity_key=identity_exact + )._quant_tasks_deferred() diff --git a/stock_quant_task_deferred/readme/CONFIGURE.md b/stock_quant_task_deferred/readme/CONFIGURE.md new file mode 100644 index 00000000000..d0c2e4e34be --- /dev/null +++ b/stock_quant_task_deferred/readme/CONFIGURE.md @@ -0,0 +1,5 @@ +- Go to Inventory > Configuration > Settings > Warehouse. +- Check the box 'Defer Quant Tasks' if you want to deactivate + synchronuous quant tasks. +- Go To Settings > Technical > Scheduled Actions > 'Launch quant tasks'. +- Set the desired cron job configuration. diff --git a/stock_quant_task_deferred/readme/CONTEXT.md b/stock_quant_task_deferred/readme/CONTEXT.md new file mode 100644 index 00000000000..291c8e2daac --- /dev/null +++ b/stock_quant_task_deferred/readme/CONTEXT.md @@ -0,0 +1,8 @@ +- In Odoo, product quantities are regularily reorganized: + - when opening the product stock from product form + - when unpacking products + +This implies a lot of stock quants are deleted if you have a lot +of stock operations and users in parallel. + +That can causes some DB locks. diff --git a/stock_quant_task_deferred/readme/CONTRIBUTORS.md b/stock_quant_task_deferred/readme/CONTRIBUTORS.md new file mode 100644 index 00000000000..80157490f5e --- /dev/null +++ b/stock_quant_task_deferred/readme/CONTRIBUTORS.md @@ -0,0 +1,2 @@ +- Denis Roussel +- Jacques-Étienne Baudoux diff --git a/stock_quant_task_deferred/readme/DESCRIPTION.md b/stock_quant_task_deferred/readme/DESCRIPTION.md new file mode 100644 index 00000000000..c109a5a2ad9 --- /dev/null +++ b/stock_quant_task_deferred/readme/DESCRIPTION.md @@ -0,0 +1,4 @@ +This module allows to deactivate synchronuous quant tasks and +use a cron job instead. + +The cron job will trigger queue jobs to do the tasks. diff --git a/stock_quant_task_deferred/readme/USAGE.md b/stock_quant_task_deferred/readme/USAGE.md new file mode 100644 index 00000000000..2e9057daf82 --- /dev/null +++ b/stock_quant_task_deferred/readme/USAGE.md @@ -0,0 +1,3 @@ +- After having configured the scheduled task, go to Queue Job and look + for "Executing Quant Tasks" queue job descriptions. + \ No newline at end of file diff --git a/stock_quant_task_deferred/static/description/icon.png b/stock_quant_task_deferred/static/description/icon.png new file mode 100644 index 00000000000..3a0328b516c Binary files /dev/null and b/stock_quant_task_deferred/static/description/icon.png differ diff --git a/stock_quant_task_deferred/static/description/index.html b/stock_quant_task_deferred/static/description/index.html new file mode 100644 index 00000000000..838bc6cb864 --- /dev/null +++ b/stock_quant_task_deferred/static/description/index.html @@ -0,0 +1,461 @@ + + + + + +Stock Quant Task Deferred + + + +
+

Stock Quant Task Deferred

+ + +

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

+

This module allows to deactivate synchronuous quant tasks and use a cron +job instead.

+

The cron job will trigger queue jobs to do the tasks.

+

Table of contents

+ +
+

Use Cases / Context

+
    +
  • In Odoo, product quantities are regularily reorganized:
      +
    • when opening the product stock from product form
    • +
    • when unpacking products
    • +
    +
  • +
+

This implies a lot of stock quants are deleted if you have a lot of +stock operations and users in parallel.

+

That can causes some DB locks.

+
+
+

Configuration

+
    +
  • Go to Inventory > Configuration > Settings > Warehouse.
  • +
  • Check the box ‘Defer Quant Tasks’ if you want to deactivate +synchronuous quant tasks.
  • +
  • Go To Settings > Technical > Scheduled Actions > ‘Launch quant tasks’.
  • +
  • Set the desired cron job configuration.
  • +
+
+
+

Usage

+
    +
  • After having configured the scheduled task, go to Queue Job and look +for “Executing Quant Tasks” queue job descriptions.
  • +
+
+
+

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

+
    +
  • ACSONE SA/NV
  • +
+
+
+

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.

+

Current maintainer:

+

rousseldenis

+

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_quant_task_deferred/tests/__init__.py b/stock_quant_task_deferred/tests/__init__.py new file mode 100644 index 00000000000..c628e1203bf --- /dev/null +++ b/stock_quant_task_deferred/tests/__init__.py @@ -0,0 +1 @@ +from . import test_defer diff --git a/stock_quant_task_deferred/tests/test_defer.py b/stock_quant_task_deferred/tests/test_defer.py new file mode 100644 index 00000000000..37ead76d0ca --- /dev/null +++ b/stock_quant_task_deferred/tests/test_defer.py @@ -0,0 +1,89 @@ +# Copyright 2024 ACSONE SA/NV +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo.addons.base.tests.common import BaseCommon +from odoo.addons.queue_job.tests.common import trap_jobs + + +class TestDeferQuantTask(BaseCommon): + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.stock = cls.env.ref("stock.stock_location_stock") + cls.product = cls.env["product.product"].create( + { + "name": "Product Test", + "type": "product", + } + ) + + def test_defer_quant_tasks_not_deferred(self): + # Check the normal process + self.env["stock.quant"].with_context(inventory_mode=True).create( + { + "location_id": self.stock.id, + "product_id": self.product.id, + "inventory_quantity": 0.0, + } + )._apply_inventory() + quant = self.env["stock.quant"].search( + [("product_id", "=", self.product.id), ("location_id", "=", self.stock.id)] + ) + self.assertTrue(quant) + self.env["stock.quant"].action_view_inventory() + quant = self.env["stock.quant"].search( + [("product_id", "=", self.product.id), ("location_id", "=", self.stock.id)] + ) + self.assertFalse(quant) + + def test_defer_quant_tasks(self): + # No more quant tasks are done + self.env.company.defer_quant_tasks = True + self.env["stock.quant"].with_context(inventory_mode=True).create( + { + "location_id": self.stock.id, + "product_id": self.product.id, + "inventory_quantity": 0.0, + } + )._apply_inventory() + quant = self.env["stock.quant"].search( + [("product_id", "=", self.product.id), ("location_id", "=", self.stock.id)] + ) + self.assertTrue(quant) + self.env["stock.quant"].action_view_inventory() + quant = self.env["stock.quant"].search( + [("product_id", "=", self.product.id), ("location_id", "=", self.stock.id)] + ) + self.assertTrue(quant) + + # Call _quant_tasks() with run + self.env["stock.quant"].with_context(run_defer_quant_tasks=True)._quant_tasks() + self.assertFalse(quant.exists()) + + def test_defer_quant_tasks_cron(self): + # Quant tasks are done through a cron job + # that trigger a queue job. + self.env.company.defer_quant_tasks = True + self.env["stock.quant"].with_context(inventory_mode=True).create( + { + "location_id": self.stock.id, + "product_id": self.product.id, + "inventory_quantity": 0.0, + } + )._apply_inventory() + quant = self.env["stock.quant"].search( + [("product_id", "=", self.product.id), ("location_id", "=", self.stock.id)] + ) + self.assertTrue(quant) + self.env["stock.quant"].action_view_inventory() + quant = self.env["stock.quant"].search( + [("product_id", "=", self.product.id), ("location_id", "=", self.stock.id)] + ) + self.assertTrue(quant) + + # Call _quant_tasks() with run + with trap_jobs() as trap: + self.env["stock.quant"]._run_quant_tasks_deferred() + trap.assert_enqueued_job(self.env["stock.quant"]._quant_tasks_deferred) + trap.perform_enqueued_jobs() + self.assertFalse(quant.exists()) diff --git a/stock_quant_task_deferred/views/res_config_settings.xml b/stock_quant_task_deferred/views/res_config_settings.xml new file mode 100644 index 00000000000..ae6c5e1cf66 --- /dev/null +++ b/stock_quant_task_deferred/views/res_config_settings.xml @@ -0,0 +1,28 @@ + + + + + + res.config.settings + + +
+
+
+ +
+
+
+
+ +
+
+
+ +