diff --git a/budget_control_queue/README.rst b/budget_control_queue/README.rst new file mode 100644 index 00000000..c7099f77 --- /dev/null +++ b/budget_control_queue/README.rst @@ -0,0 +1,78 @@ +====================== +Budget Control - Queue +====================== + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:170b7aa450e2ccdfa27c0d5840cf49a8511a46198988caa073e23f03a6689384 + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |badge1| image:: https://img.shields.io/badge/maturity-Alpha-red.png + :target: https://odoo-community.org/page/development-status + :alt: Alpha +.. |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-ecosoft--odoo%2Fbudgeting-lightgray.png?logo=github + :target: https://github.com/ecosoft-odoo/budgeting/tree/15.0/budget_control_queue + :alt: ecosoft-odoo/budgeting + +|badge1| |badge2| |badge3| + +# TODO + +.. IMPORTANT:: + This is an alpha version, the data model and design can change at any time without warning. + Only for development or testing purpose, do not use in production. + `More details on development status `_ + +**Table of contents** + +.. contents:: + :local: + +Usage +===== + +# TODO + +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 +~~~~~~~ + +* Ecosoft + +Contributors +~~~~~~~~~~~~ + +* Saran Lim. + +Maintainers +~~~~~~~~~~~ + +.. |maintainer-Saran440| image:: https://github.com/Saran440.png?size=40px + :target: https://github.com/Saran440 + :alt: Saran440 + +Current maintainer: + +|maintainer-Saran440| + +This module is part of the `ecosoft-odoo/budgeting `_ project on GitHub. + +You are welcome to contribute. diff --git a/budget_control_queue/__init__.py b/budget_control_queue/__init__.py new file mode 100644 index 00000000..69f7babd --- /dev/null +++ b/budget_control_queue/__init__.py @@ -0,0 +1,3 @@ +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from . import models diff --git a/budget_control_queue/__manifest__.py b/budget_control_queue/__manifest__.py new file mode 100644 index 00000000..88a45452 --- /dev/null +++ b/budget_control_queue/__manifest__.py @@ -0,0 +1,24 @@ +# Copyright 2024 Ecosoft Co., Ltd. (http://ecosoft.co.th) +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +{ + "name": "Budget Control - Queue", + "version": "15.0.1.0.0", + "category": "Accounting", + "license": "AGPL-3", + "author": "Ecosoft, Odoo Community Association (OCA)", + "website": "https://github.com/ecosoft-odoo/budgeting", + "depends": [ + "budget_control", + "queue_job", + ], + "data": [ + "data/ir_config_parameter_data.xml", + "data/queue_data.xml", + "views/budget_balance_forward_view.xml", + "views/budget_commit_forward_view.xml", + ], + "installable": True, + "maintainers": ["Saran440"], + "development_status": "Alpha", +} diff --git a/budget_control_queue/data/ir_config_parameter_data.xml b/budget_control_queue/data/ir_config_parameter_data.xml new file mode 100644 index 00000000..27f746e9 --- /dev/null +++ b/budget_control_queue/data/ir_config_parameter_data.xml @@ -0,0 +1,10 @@ + + + budget_control.forward_create_analytic + 100 + + + budget_control.carry_forward + 10 + + diff --git a/budget_control_queue/data/queue_data.xml b/budget_control_queue/data/queue_data.xml new file mode 100644 index 00000000..dd0b1948 --- /dev/null +++ b/budget_control_queue/data/queue_data.xml @@ -0,0 +1,7 @@ + + + + budget_forward + + + diff --git a/budget_control_queue/models/__init__.py b/budget_control_queue/models/__init__.py new file mode 100644 index 00000000..93db3d34 --- /dev/null +++ b/budget_control_queue/models/__init__.py @@ -0,0 +1,4 @@ +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from . import budget_balance_forward +from . import budget_commit_forward diff --git a/budget_control_queue/models/budget_balance_forward.py b/budget_control_queue/models/budget_balance_forward.py new file mode 100644 index 00000000..5708dc9f --- /dev/null +++ b/budget_control_queue/models/budget_balance_forward.py @@ -0,0 +1,80 @@ +# Copyright 2024 Ecosoft Co., Ltd. (http://ecosoft.co.th) +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from odoo import fields, models + +from odoo.addons.queue_job.delay import group + + +class BudgetBalanceForward(models.Model): + _inherit = "budget.balance.forward" + + use_queue_job = fields.Boolean() + still_running = fields.Boolean(copy=False) + + def _create_missing_analytic_job(self, lines): + self.ensure_one() + for line in lines: + line.to_analytic_account_id = line.analytic_account_id.next_year_analytic() + + def create_missing_analytic(self): + if not self.use_queue_job: + return super().create_missing_analytic() + + ICP = self.env["ir.config_parameter"] + chunk_size_create_analytic = ( + int(ICP.sudo().get_param("budget_control.forward_create_analytic")) or 100 + ) + + for rec in self: + lines = rec.forward_line_ids.filtered_domain( + [("to_analytic_account_id", "=", False)] + ) + job_create_missing = [] + # Create analytic with chunk + for i in range(0, len(lines), chunk_size_create_analytic): + chunk_lines = lines[i : i + chunk_size_create_analytic] + job1 = rec.delayable( + channel="root.budget_forward" + )._create_missing_analytic_job(chunk_lines) + job_create_missing.append(job1) + # Change state and still running flag + rec.write({"still_running": True}) + # Job delay, change state back to done + job_run_done = rec.delayable(channel="root.budget_forward").write( + {"still_running": False} + ) + + # Run queue job + group_job_create_missing = group(*job_create_missing) + group_job_create_missing.on_done(job_run_done) + group_job_create_missing.delay() + + def _action_budget_balance_forward(self): + for rec in self: + for line in rec.forward_line_ids: + if line.method_type == "extend": + line.to_analytic_account_id.bm_date_to = ( + rec.to_budget_period_id.bm_date_to + ) + # -- + self.write({"state": "done"}) + self._do_update_initial_avaliable() + + def action_budget_balance_forward(self): + if not self.use_queue_job: + return super().action_budget_balance_forward() + + job_balance_forward = self.delayable( + channel="root.budget_forward" + )._action_budget_balance_forward() + # Change state and still running flag + self.write({"still_running": True}) + # Job delay, change state back to done + job_run_done = self.delayable(channel="root.budget_forward").write( + {"still_running": False} + ) + + # Run queue job + job_balance_forward.on_done(job_run_done) + job_balance_forward.delay() diff --git a/budget_control_queue/models/budget_commit_forward.py b/budget_control_queue/models/budget_commit_forward.py new file mode 100644 index 00000000..327b261d --- /dev/null +++ b/budget_control_queue/models/budget_commit_forward.py @@ -0,0 +1,153 @@ +# Copyright 2024 Ecosoft Co., Ltd. (http://ecosoft.co.th) +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from odoo import fields, models + +from odoo.addons.queue_job.delay import chain, group + + +class BudgetCommitForward(models.Model): + _inherit = "budget.commit.forward" + + use_queue_job = fields.Boolean() + still_running = fields.Boolean(copy=False) + + def _create_missing_analytic_job(self, lines): + self.ensure_one() + for line in lines: + line.to_analytic_account_id = line.analytic_account_id.next_year_analytic() + + def create_missing_analytic(self): + if not self.use_queue_job: + return super().create_missing_analytic() + + ICP = self.env["ir.config_parameter"] + chunk_size_create_analytic = ( + int(ICP.sudo().get_param("budget_control.carry_forward_create_analytic")) + or 100 + ) + + for rec in self: + lines = rec.forward_line_ids.filtered_domain( + [("to_analytic_account_id", "=", False)] + ) + job_create_missing = [] + # Create analytic with chunk + for i in range(0, len(lines), chunk_size_create_analytic): + chunk_lines = lines[i : i + chunk_size_create_analytic] + job1 = rec.delayable( + channel="root.budget_forward" + )._create_missing_analytic_job(chunk_lines) + job_create_missing.append(job1) + # Change state and still running flag + rec.write({"still_running": True}) + # Job delay, change state back to done + job_run_done = rec.delayable(channel="root.budget_forward").write( + {"still_running": False} + ) + + # Run queue job + group_job_create_missing = group(*job_create_missing) + group_job_create_missing.on_done(job_run_done) + group_job_create_missing.delay() + + def _job_do_forward_commit(self): + self._do_forward_commit() + self.write({"state": "done"}) + self._do_update_initial_commit() + + def _job_recompute(self, chunk_lines): + for line in chunk_lines: + doc = line.document_number + doc.recompute_budget_move() + + def action_budget_commit_forward(self): + """Use queue job will split function into multiple jobs""" + if not self.use_queue_job: + return super().action_budget_commit_forward() + + ICP = self.env["ir.config_parameter"] + chunk_size_carry_forward = ( + int(ICP.sudo().get_param("budget_control.carry_forward")) or 10 + ) + + # Job1: Do Forward Commit + job_carry_commit = self.delayable( + channel="root.budget_forward" + )._job_do_forward_commit() + + # Recompute budget on document number + job_recompute = [] + lines = self.forward_line_ids + for i in range(0, len(lines), chunk_size_carry_forward): + chunk_lines = lines[i : i + chunk_size_carry_forward] + + # Job2: Recompute after carry forward and update line is done + job2 = self.delayable(channel="root.budget_forward")._job_recompute( + chunk_lines + ) + job_recompute.append(job2) + self.write({"still_running": True}) + + group_job_recompute = group(*job_recompute) + + # Job3: Update header is done + job_run_done = self.delayable(channel="root.budget_forward").write( + {"still_running": False} + ) + + # Run queue job + chain_process = chain( + job_carry_commit, + group_job_recompute, + job_run_done, + ) + chain_process.delay() + + def _job_reverse_forward_commit(self): + self.filtered(lambda l: l.state == "done")._do_forward_commit(reverse=True) + self.write({"state": "cancel"}) + self._do_update_initial_commit(reverse=True) + + def _action_cancel(self): + """Use queue job will split function into multiple jobs""" + if not self.use_queue_job: + return super().action_cancel() + + ICP = self.env["ir.config_parameter"] + chunk_size_carry_forward_cancel = ( + int(ICP.sudo().get_param("budget_control.carry_forward")) or 10 + ) + + # Job1: Reverse Forward Commit + job_carry_commit = self.delayable( + channel="root.budget_forward" + )._job_reverse_forward_commit() + + # Recompute budget on document number + job_recompute = [] + lines = self.forward_line_ids + for i in range(0, len(lines), chunk_size_carry_forward_cancel): + chunk_lines = lines[i : i + chunk_size_carry_forward_cancel] + + # Job2: Recompute after carry forward and update line is done + job2 = self.delayable(channel="root.budget_forward")._job_recompute( + chunk_lines + ) + job_recompute.append(job2) + self.write({"still_running": True}) + + group_job_recompute = group(*job_recompute) + + # Job3: Update header is done + job_run_done = self.delayable(channel="root.budget_forward").write( + {"still_running": False} + ) + + # Run queue job + chain_process = chain( + job_carry_commit, + group_job_recompute, + job_run_done, + ) + chain_process.delay() diff --git a/budget_control_queue/readme/CONTRIBUTORS.rst b/budget_control_queue/readme/CONTRIBUTORS.rst new file mode 100644 index 00000000..cc6b2310 --- /dev/null +++ b/budget_control_queue/readme/CONTRIBUTORS.rst @@ -0,0 +1 @@ +* Saran Lim. diff --git a/budget_control_queue/readme/DESCRIPTION.rst b/budget_control_queue/readme/DESCRIPTION.rst new file mode 100644 index 00000000..46409041 --- /dev/null +++ b/budget_control_queue/readme/DESCRIPTION.rst @@ -0,0 +1 @@ +# TODO diff --git a/budget_control_queue/readme/USAGE.rst b/budget_control_queue/readme/USAGE.rst new file mode 100644 index 00000000..46409041 --- /dev/null +++ b/budget_control_queue/readme/USAGE.rst @@ -0,0 +1 @@ +# TODO diff --git a/budget_control_queue/static/description/index.html b/budget_control_queue/static/description/index.html new file mode 100644 index 00000000..2c78ecaf --- /dev/null +++ b/budget_control_queue/static/description/index.html @@ -0,0 +1,428 @@ + + + + + +Budget Control - Queue + + + +
+

Budget Control - Queue

+ + +

Alpha License: AGPL-3 ecosoft-odoo/budgeting

+

# TODO

+
+

Important

+

This is an alpha version, the data model and design can change at any time without warning. +Only for development or testing purpose, do not use in production. +More details on development status

+
+

Table of contents

+ +
+

Usage

+

# TODO

+
+
+

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

+
    +
  • Ecosoft
  • +
+
+
+

Contributors

+ +
+
+

Maintainers

+

Current maintainer:

+

Saran440

+

This module is part of the ecosoft-odoo/budgeting project on GitHub.

+

You are welcome to contribute.

+
+
+
+ + diff --git a/budget_control_queue/views/budget_balance_forward_view.xml b/budget_control_queue/views/budget_balance_forward_view.xml new file mode 100644 index 00000000..49d2c14e --- /dev/null +++ b/budget_control_queue/views/budget_balance_forward_view.xml @@ -0,0 +1,27 @@ + + + + view.budget.balance.forward.form + budget.balance.forward + + + + + + + + + + + + diff --git a/budget_control_queue/views/budget_commit_forward_view.xml b/budget_control_queue/views/budget_commit_forward_view.xml new file mode 100644 index 00000000..cc3e8bce --- /dev/null +++ b/budget_control_queue/views/budget_commit_forward_view.xml @@ -0,0 +1,24 @@ + + + + view.budget.commit.forward.form + budget.commit.forward + + + + + + + + + + + + diff --git a/setup/budget_control_queue/odoo/addons/budget_control_queue b/setup/budget_control_queue/odoo/addons/budget_control_queue new file mode 120000 index 00000000..64d4cc91 --- /dev/null +++ b/setup/budget_control_queue/odoo/addons/budget_control_queue @@ -0,0 +1 @@ +../../../../budget_control_queue \ No newline at end of file diff --git a/setup/budget_control_queue/setup.py b/setup/budget_control_queue/setup.py new file mode 100644 index 00000000..28c57bb6 --- /dev/null +++ b/setup/budget_control_queue/setup.py @@ -0,0 +1,6 @@ +import setuptools + +setuptools.setup( + setup_requires=['setuptools-odoo'], + odoo_addon=True, +)