diff --git a/account_asset_compute_batch_job/README.rst b/account_asset_compute_batch_job/README.rst new file mode 100644 index 000000000000..6aa79e0581ad --- /dev/null +++ b/account_asset_compute_batch_job/README.rst @@ -0,0 +1,83 @@ +=============================================== +Assets - Compute Depre. with Job Queue in Batch +=============================================== + +.. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! 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%2Faccount--financial--tools-lightgray.png?logo=github + :target: https://github.com/OCA/account-financial-tools/tree/14.0/account_asset_compute_batch_job + :alt: OCA/account-financial-tools +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/account-financial-tools-14-0/account-financial-tools-14-0-account_asset_compute_batch_job + :alt: Translate me on Weblate +.. |badge5| image:: https://img.shields.io/badge/runbot-Try%20me-875A7B.png + :target: https://runbot.odoo-community.org/runbot/92/14.0 + :alt: Try me on Runbot + +|badge1| |badge2| |badge3| |badge4| |badge5| + +Add the possibility to compute assets batch with job queue. +This module adds a new button for compute in order to execute this process in job queue. + +**Table of contents** + +.. contents:: + :local: + +Usage +===== + +To use this module, you need to: + +#. Configured job queue +#. Create Compute Asset Batch directly +#. Click "Compute as Background" button to compute asset with job. + +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 +~~~~~~~ + +* Ecosoft + +Contributors +~~~~~~~~~~~~ + +* Saran Lim. + +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/account-financial-tools `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/account_asset_compute_batch_job/__init__.py b/account_asset_compute_batch_job/__init__.py new file mode 100644 index 000000000000..69f7babdfb1a --- /dev/null +++ b/account_asset_compute_batch_job/__init__.py @@ -0,0 +1,3 @@ +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from . import models diff --git a/account_asset_compute_batch_job/__manifest__.py b/account_asset_compute_batch_job/__manifest__.py new file mode 100644 index 000000000000..05585d6b0961 --- /dev/null +++ b/account_asset_compute_batch_job/__manifest__.py @@ -0,0 +1,17 @@ +# Copyright 2022 Ecosoft Co., Ltd. (http://ecosoft.co.th) +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +{ + "name": "Assets - Compute Depre. with Job Queue in Batch", + "version": "14.0.1.0.0", + "license": "AGPL-3", + "depends": ["account_asset_compute_batch", "queue_job"], + "author": "Ecosoft, Odoo Community Association (OCA)", + "website": "https://github.com/OCA/account-financial-tools", + "category": "Accounting & Finance", + "data": [ + "security/ir.model.access.csv", + "data/queue_data.xml", + "views/account_asset_compute_batch.xml", + ], +} diff --git a/account_asset_compute_batch_job/data/queue_data.xml b/account_asset_compute_batch_job/data/queue_data.xml new file mode 100644 index 000000000000..a51a3818ebc0 --- /dev/null +++ b/account_asset_compute_batch_job/data/queue_data.xml @@ -0,0 +1,18 @@ + + + + account_asset_compute_batch + + + + + action_compute + + + diff --git a/account_asset_compute_batch_job/models/__init__.py b/account_asset_compute_batch_job/models/__init__.py new file mode 100644 index 000000000000..1d2eb410b861 --- /dev/null +++ b/account_asset_compute_batch_job/models/__init__.py @@ -0,0 +1,3 @@ +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from . import account_asset_compute_batch diff --git a/account_asset_compute_batch_job/models/account_asset_compute_batch.py b/account_asset_compute_batch_job/models/account_asset_compute_batch.py new file mode 100644 index 000000000000..fe8a8d35fb7e --- /dev/null +++ b/account_asset_compute_batch_job/models/account_asset_compute_batch.py @@ -0,0 +1,46 @@ +# Copyright 2022 Ecosoft Co., Ltd. (http://ecosoft.co.th) +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from odoo import _, fields, models + + +class AssetComputeBatch(models.Model): + _inherit = "account.asset.compute.batch" + + job_ids = fields.Many2many( + comodel_name="queue.job", + column1="batch_id", + column2="job_id", + relation="asset_batch_job_rel", + copy=False, + ) + job_current = fields.Many2one(comodel_name="queue.job", readonly=True) + job_state = fields.Selection(string="Job State", related="job_current.state") + + def action_compute_job(self): + self.ensure_one() + queue_obj = self.env["queue.job"] + if not self.env.context.get("job_uuid") and not self.env.context.get( + "test_queue_job_no_delay" + ): + description = _("Creating jobs to create moves for assets to %s") % ( + self.date_end, + ) + new_delay = self.with_delay(description=description).action_compute() + job = queue_obj.search([("uuid", "=", new_delay.uuid)]) + self.job_ids = [(4, job.id)] + self.job_current = job.id + return "Job created with uuid {}".format(new_delay.uuid) + return super().action_compute() + + def open_queue_job(self): + self.ensure_one() + action = { + "name": _("Jobs"), + "view_type": "tree", + "view_mode": "list,form", + "res_model": "queue.job", + "type": "ir.actions.act_window", + "domain": [("id", "in", self.job_ids.ids)], + } + return action diff --git a/account_asset_compute_batch_job/readme/CONTRIBUTORS.rst b/account_asset_compute_batch_job/readme/CONTRIBUTORS.rst new file mode 100644 index 000000000000..cc6b23102b9d --- /dev/null +++ b/account_asset_compute_batch_job/readme/CONTRIBUTORS.rst @@ -0,0 +1 @@ +* Saran Lim. diff --git a/account_asset_compute_batch_job/readme/DESCRIPTION.rst b/account_asset_compute_batch_job/readme/DESCRIPTION.rst new file mode 100644 index 000000000000..fe679283e301 --- /dev/null +++ b/account_asset_compute_batch_job/readme/DESCRIPTION.rst @@ -0,0 +1,2 @@ +Add the possibility to compute assets batch with job queue. +This module adds a new button for compute in order to execute this process in job queue. diff --git a/account_asset_compute_batch_job/readme/USAGE.rst b/account_asset_compute_batch_job/readme/USAGE.rst new file mode 100644 index 000000000000..13867cd04af3 --- /dev/null +++ b/account_asset_compute_batch_job/readme/USAGE.rst @@ -0,0 +1,5 @@ +To use this module, you need to: + +#. Configured job queue +#. Create Compute Asset Batch directly +#. Click "Compute as Background" button to compute asset with job. diff --git a/account_asset_compute_batch_job/security/ir.model.access.csv b/account_asset_compute_batch_job/security/ir.model.access.csv new file mode 100644 index 000000000000..f363c9e29312 --- /dev/null +++ b/account_asset_compute_batch_job/security/ir.model.access.csv @@ -0,0 +1,2 @@ +id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink +access_job_queue_account_readonly,access_job_queue_account_readonly,queue_job.model_queue_job,account.group_account_invoice,1,0,0,0 diff --git a/account_asset_compute_batch_job/static/description/index.html b/account_asset_compute_batch_job/static/description/index.html new file mode 100644 index 000000000000..d369fb65d2de --- /dev/null +++ b/account_asset_compute_batch_job/static/description/index.html @@ -0,0 +1,430 @@ + + + + + + +Assets - Compute Depre. with Job Queue in Batch + + + +
+

Assets - Compute Depre. with Job Queue in Batch

+ + +

Beta License: AGPL-3 OCA/account-financial-tools Translate me on Weblate Try me on Runbot

+

Add the possibility to compute assets batch with job queue. +This module adds a new button for compute in order to execute this process in job queue.

+

Table of contents

+ +
+

Usage

+

To use this module, you need to:

+
    +
  1. Configured job queue
  2. +
  3. Create Compute Asset Batch directly
  4. +
  5. Click “Compute as Background” button to compute asset with job.
  6. +
+
+
+

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

+
    +
  • Ecosoft
  • +
+
+
+

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/account-financial-tools project on GitHub.

+

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

+
+
+
+ + diff --git a/account_asset_compute_batch_job/tests/__init__.py b/account_asset_compute_batch_job/tests/__init__.py new file mode 100644 index 000000000000..8b7febdc591a --- /dev/null +++ b/account_asset_compute_batch_job/tests/__init__.py @@ -0,0 +1,3 @@ +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from . import test_account_asset_compute_batch_job diff --git a/account_asset_compute_batch_job/tests/test_account_asset_compute_batch_job.py b/account_asset_compute_batch_job/tests/test_account_asset_compute_batch_job.py new file mode 100644 index 000000000000..caf2c06cea08 --- /dev/null +++ b/account_asset_compute_batch_job/tests/test_account_asset_compute_batch_job.py @@ -0,0 +1,93 @@ +# Copyright 2021 Ecosoft Co., Ltd. (http://ecosoft.co.th) +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +import time + +from freezegun import freeze_time + +from odoo.tests import tagged +from odoo.tests.common import Form + +from odoo.addons.account_asset_management.tests.test_account_asset_management import ( + TestAssetManagement, +) + + +@tagged("post_install", "-at_install") +class TestAssetComputeBatch(TestAssetManagement): + @classmethod + @freeze_time("2000-01-01") + def setUpClass(cls): + super().setUpClass() + # Create 3 assets from 2 profiles + cls.ict0 = cls.asset_model.create( + { + "state": "draft", + "method_time": "year", + "method_number": 3, + "method_period": "year", + "name": "Laptop", + "code": "PI00101", + "purchase_value": 1500.0, + "profile_id": cls.ict3Y.id, + "date_start": time.strftime("2000-01-01"), + } + ) + cls.ict1 = cls.asset_model.create( + { + "state": "draft", + "method_time": "year", + "method_number": 3, + "method_period": "year", + "name": "Monitor", + "code": "PI00102", + "purchase_value": 2100.0, + "profile_id": cls.ict3Y.id, + "date_start": time.strftime("2000-01-01"), + } + ) + # 2nd asset + cls.vehicle0 = cls.asset_model.create( + { + "state": "draft", + "method_time": "year", + "method_number": 5, + "method_period": "year", + "name": "CEO's Car", + "purchase_value": 12000.0, + "salvage_value": 2000.0, + "profile_id": cls.car5y.id, + "date_start": time.strftime("2000-01-01"), + } + ) + + def _create_compute_wizard(self, use_batch=False, delay_compute=False): + with Form(self.env["account.asset.compute"]) as f: + f.batch_name = "Test Batch" + f.description = "Compute asset with 2 profiles" + f.profile_ids.add(self.ict3Y) + f.profile_ids.add(self.car5y) + f.use_batch = use_batch + f.delay_compute = delay_compute + wiz = f.save() + return wiz + + @freeze_time("2000-12-31") + def test_01_asset_compute_batch_job(self): + # Confirm 2 assets + self.ict0.validate() + self.assertEqual(self.ict0.depreciation_line_ids[1].amount, 500) + self.vehicle0.validate() + self.assertEqual(self.vehicle0.depreciation_line_ids[1].amount, 2000) + # Compute Asset, with delay + wiz = self._create_compute_wizard(use_batch=True, delay_compute=True) + res = wiz.asset_compute() + batch = self.env["account.asset.compute.batch"].browse(res["res_id"]) + self.assertFalse(batch.run_job) + self.assertEqual(batch.state, "draft") + self.assertEqual(batch.depre_amount, 0) + # Batch will be posted by cron job + batch.with_context(compute_batch_job=1).action_compute_job() + self.assertFalse(batch.run_job) + self.assertEqual(batch.state, "computed") + self.assertEqual(batch.depre_amount, 2500) diff --git a/account_asset_compute_batch_job/views/account_asset_compute_batch.xml b/account_asset_compute_batch_job/views/account_asset_compute_batch.xml new file mode 100644 index 000000000000..f30a278fac50 --- /dev/null +++ b/account_asset_compute_batch_job/views/account_asset_compute_batch.xml @@ -0,0 +1,90 @@ + + + + account.asset.compute.batch.form + account.asset.compute.batch + + + + + {'invisible': [('job_current', '!=', False), '|', ('job_state', 'not in', ['failed', 'cancelled']), ('state', '!=', 'draft')]} + + + + + +