diff --git a/contract_queue_job/README.rst b/contract_queue_job/README.rst new file mode 100644 index 000000000..3806c0e47 --- /dev/null +++ b/contract_queue_job/README.rst @@ -0,0 +1,90 @@ +================== +Contract Queue Job +================== + +.. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! 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%2Fcontract-lightgray.png?logo=github + :target: https://github.com/OCA/contract/tree/14.0/contract_queue_job + :alt: OCA/contract +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/contract-14-0/contract-14-0-contract_queue_job + :alt: Translate me on Weblate +.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png + :target: https://runboat.odoo-community.org/webui/builds.html?repo=OCA/contract&target_branch=14.0 + :alt: Try me on Runboat + +|badge1| |badge2| |badge3| |badge4| |badge5| + +This addon make contract invoicing cron plan each contract in a job instead of creating all invoices in one transaction + +**Table of contents** + +.. contents:: + :local: + +Usage +===== + +The feature can be enabled by setting the ir.config_parameter +"contract.queue.job" to True. + +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 +~~~~~~~ + +* ACSONE SA/NV + +Contributors +~~~~~~~~~~~~ + +* Souheil Bejaoui + +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-sbejaoui| image:: https://github.com/sbejaoui.png?size=40px + :target: https://github.com/sbejaoui + :alt: sbejaoui +.. |maintainer-BurkhalterY| image:: https://github.com/BurkhalterY.png?size=40px + :target: https://github.com/BurkhalterY + :alt: BurkhalterY + +Current `maintainers `__: + +|maintainer-sbejaoui| |maintainer-BurkhalterY| + +This module is part of the `OCA/contract `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/contract_queue_job/__init__.py b/contract_queue_job/__init__.py new file mode 100644 index 000000000..aee8895e7 --- /dev/null +++ b/contract_queue_job/__init__.py @@ -0,0 +1,2 @@ +from . import models +from . import wizards diff --git a/contract_queue_job/__manifest__.py b/contract_queue_job/__manifest__.py new file mode 100644 index 000000000..dca19647b --- /dev/null +++ b/contract_queue_job/__manifest__.py @@ -0,0 +1,19 @@ +# Copyright 2020 ACSONE SA/NV +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +{ + "name": "Contract Queue Job", + "summary": """ + This addon make contract invoicing cron plan each contract in a job + instead of creating all invoices in one transaction""", + "version": "14.0.1.0.0", + "license": "AGPL-3", + "author": "ACSONE SA/NV," "Odoo Community Association (OCA)", + "website": "https://github.com/OCA/contract", + "depends": ["contract", "queue_job"], + "data": [ + "data/ir_config_parameter.xml", + "wizards/contract_manually_create_invoice.xml", + ], + "maintainers": ["sbejaoui", "BurkhalterY"], +} diff --git a/contract_queue_job/data/ir_config_parameter.xml b/contract_queue_job/data/ir_config_parameter.xml new file mode 100644 index 000000000..fa02eaab9 --- /dev/null +++ b/contract_queue_job/data/ir_config_parameter.xml @@ -0,0 +1,8 @@ + + + + contract.queue.job + + False + + diff --git a/contract_queue_job/i18n/contract_queue_job.pot b/contract_queue_job/i18n/contract_queue_job.pot new file mode 100644 index 000000000..f39ed2c3d --- /dev/null +++ b/contract_queue_job/i18n/contract_queue_job.pot @@ -0,0 +1,30 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * contract_queue_job +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 12.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: contract_queue_job +#: model:ir.model,name:contract_queue_job.model_contract_contract +msgid "Contract" +msgstr "" + +#. module: contract_queue_job +#: model:ir.model,name:contract_queue_job.model_contract_line +msgid "Contract Line" +msgstr "" + +#. module: contract_queue_job +#: model:ir.model,name:contract_queue_job.model_contract_manually_create_invoice +msgid "Contract Manually Create Invoice Wizard" +msgstr "" + diff --git a/contract_queue_job/models/__init__.py b/contract_queue_job/models/__init__.py new file mode 100644 index 000000000..f810156f5 --- /dev/null +++ b/contract_queue_job/models/__init__.py @@ -0,0 +1,2 @@ +from . import contract_contract +from . import contract_line diff --git a/contract_queue_job/models/contract_contract.py b/contract_queue_job/models/contract_contract.py new file mode 100644 index 000000000..fc5b1db3c --- /dev/null +++ b/contract_queue_job/models/contract_contract.py @@ -0,0 +1,28 @@ +# Copyright 2020 ACSONE SA/NV +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). +import ast + +from odoo import models + + +class ContractContract(models.Model): + + _inherit = "contract.contract" + + def _recurring_create_invoice(self, date_ref=False): + as_job = ( + self.env["ir.config_parameter"] + .sudo() + .get_param("contract.queue.job", default=False) + ) + + try: + as_job = ast.literal_eval(as_job) if as_job else False + except ValueError: + as_job = False + + if as_job and len(self) > 1: + for rec in self: + rec.with_delay()._recurring_create_invoice(date_ref=date_ref) + return self.env["account.move"] + return super()._recurring_create_invoice(date_ref=date_ref) diff --git a/contract_queue_job/models/contract_line.py b/contract_queue_job/models/contract_line.py new file mode 100644 index 000000000..41850d55f --- /dev/null +++ b/contract_queue_job/models/contract_line.py @@ -0,0 +1,28 @@ +# Copyright 2021 ACSONE SA/NV +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). +import ast + +from odoo import models + + +class ContractLine(models.Model): + + _inherit = "contract.line" + + def renew(self): + as_job = ( + self.env["ir.config_parameter"] + .sudo() + .get_param("contract.queue.job", default=False) + ) + + try: + as_job = ast.literal_eval(as_job) if as_job else False + except ValueError: + as_job = False + + if as_job and len(self) > 1: + for rec in self: + rec.with_delay().renew() + return self.env["contract.line"] + return super().renew() diff --git a/contract_queue_job/readme/CONTRIBUTORS.rst b/contract_queue_job/readme/CONTRIBUTORS.rst new file mode 100644 index 000000000..50e6298db --- /dev/null +++ b/contract_queue_job/readme/CONTRIBUTORS.rst @@ -0,0 +1 @@ +* Souheil Bejaoui diff --git a/contract_queue_job/readme/DESCRIPTION.rst b/contract_queue_job/readme/DESCRIPTION.rst new file mode 100644 index 000000000..150a27af8 --- /dev/null +++ b/contract_queue_job/readme/DESCRIPTION.rst @@ -0,0 +1 @@ +This addon make contract invoicing cron plan each contract in a job instead of creating all invoices in one transaction diff --git a/contract_queue_job/readme/USAGE.rst b/contract_queue_job/readme/USAGE.rst new file mode 100644 index 000000000..334887b74 --- /dev/null +++ b/contract_queue_job/readme/USAGE.rst @@ -0,0 +1,2 @@ +The feature can be enabled by setting the ir.config_parameter +"contract.queue.job" to True. diff --git a/contract_queue_job/static/description/icon.png b/contract_queue_job/static/description/icon.png new file mode 100644 index 000000000..3a0328b51 Binary files /dev/null and b/contract_queue_job/static/description/icon.png differ diff --git a/contract_queue_job/static/description/index.html b/contract_queue_job/static/description/index.html new file mode 100644 index 000000000..58a6a3871 --- /dev/null +++ b/contract_queue_job/static/description/index.html @@ -0,0 +1,427 @@ + + + + + + +Contract Queue Job + + + +
+

Contract Queue Job

+ + +

Beta License: AGPL-3 OCA/contract Translate me on Weblate Try me on Runboat

+

This addon make contract invoicing cron plan each contract in a job instead of creating all invoices in one transaction

+

Table of contents

+ +
+

Usage

+

The feature can be enabled by setting the ir.config_parameter +“contract.queue.job” to True.

+
+
+

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

+
    +
  • 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 maintainers:

+

sbejaoui BurkhalterY

+

This module is part of the OCA/contract project on GitHub.

+

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

+
+
+
+ + diff --git a/contract_queue_job/tests/__init__.py b/contract_queue_job/tests/__init__.py new file mode 100644 index 000000000..f99a96d72 --- /dev/null +++ b/contract_queue_job/tests/__init__.py @@ -0,0 +1,2 @@ +from . import test_contract_queue_job +from . import test_contract_line_queue_job diff --git a/contract_queue_job/tests/test_contract_line_queue_job.py b/contract_queue_job/tests/test_contract_line_queue_job.py new file mode 100644 index 000000000..9acb66242 --- /dev/null +++ b/contract_queue_job/tests/test_contract_line_queue_job.py @@ -0,0 +1,50 @@ +# Copyright 2020 ACSONE SA/NV +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from dateutil.relativedelta import relativedelta + +from odoo import fields + +from odoo.addons.contract.tests.test_contract import TestContractBase +from odoo.addons.queue_job.tests.common import JobMixin + + +class TestContractLineQueueJob(TestContractBase, JobMixin): + @classmethod + def setUpClass(cls): + super(TestContractLineQueueJob, cls).setUpClass() + cls.env["ir.config_parameter"].sudo().set_param("contract.queue.job", True) + cls.contract3 = cls.contract2.copy() + + def test_contract_renew_queue_job_1(self): + """Only one line, task is run without delay""" + line = self.contract2.mapped("contract_line_ids") + line.date_end = fields.Date.today() + res = line.renew() + self.assertTrue(line.date_end < res.date_start) + + def test_contract_renew_queue_job_2(self): + """Two lines, two jobs are created.""" + contracts = self.contract2 | self.contract3 + lines = contracts.mapped("contract_line_ids") + job_counter = self.job_counter() + lines.renew() + self.assertEqual(job_counter.count_created(), len(lines)) + + def test_contract_renew_queue_job_3(self): + """wrong ir_config_parameter : no job""" + self.env["ir.config_parameter"].sudo().set_param( + "contract.queue.job", "wronginput" + ) + date_start = self.today - relativedelta(months=9) + self.acct_line.write( + { + "date_start": date_start, + "recurring_next_date": date_start, + "date_end": self.today, + } + ) + lines = self.acct_line | self.acct_line.copy() + job_counter = self.job_counter() + lines.renew() + self.assertEqual(job_counter.count_created(), 0) diff --git a/contract_queue_job/tests/test_contract_queue_job.py b/contract_queue_job/tests/test_contract_queue_job.py new file mode 100644 index 000000000..0b8414c33 --- /dev/null +++ b/contract_queue_job/tests/test_contract_queue_job.py @@ -0,0 +1,74 @@ +# Copyright 2020 ACSONE SA/NV +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from odoo.addons.contract.tests.test_contract import TestContractBase +from odoo.addons.queue_job.tests.common import JobMixin + + +class TestContractQueueJob(TestContractBase, JobMixin): + @classmethod + def setUpClass(cls): + super(TestContractQueueJob, cls).setUpClass() + cls.env["ir.config_parameter"].sudo().set_param("contract.queue.job", True) + cls.contract3 = cls.contract2.copy() + + def _get_related_invoices(self, contracts): + return ( + self.env["account.move.line"] + .search( + [ + ( + "contract_line_id", + "in", + contracts.mapped("contract_line_ids.id"), + ) + ] + ) + .mapped("move_id") + ) + + def test_contract_queue_job(self): + contracts = self.contract2 | self.contract3 + job_counter = self.job_counter() + invoices = contracts._recurring_create_invoice() + self.assertFalse(invoices) + invoices = self._get_related_invoices(contracts) + self.assertFalse(invoices) + self.assertEqual(job_counter.count_created(), 2) + self.perform_jobs(job_counter) + invoices = self._get_related_invoices(contracts) + self.assertEqual(len(invoices), 2) + + def test_contract_queue_job_1(self): + contracts = self.contract2 + job_counter = self.job_counter() + count_all_jobs = job_counter.count_all() + self.assertEqual(job_counter.count_all(), count_all_jobs) + invoices_res = contracts._recurring_create_invoice() + self.assertTrue(invoices_res) + invoices = self._get_related_invoices(contracts) + self.assertEqual(invoices_res, invoices) + + def test_contract_queue_job_2(self): + contracts = self.contract2 | self.contract3 + job_counter = self.job_counter() + wizard = self.env["contract.manually.create.invoice"].create( + {"invoice_date": self.today, "contract_type": "purchase"} + ) + wizard.create_invoice_queued() + invoices = self._get_related_invoices(contracts) + self.assertFalse(invoices) + self.assertEqual(job_counter.count_created(), 2) + self.perform_jobs(job_counter) + invoices = self._get_related_invoices(contracts) + self.assertEqual(len(invoices), 2) + + def test_contract_queue_job_3(self): + """wrong ir_config_parameter : no job""" + self.env["ir.config_parameter"].sudo().set_param( + "contract.queue.job", "wronginput" + ) + contracts = self.contract2 | self.contract3 + job_counter = self.job_counter() + contracts._recurring_create_invoice() + self.assertEqual(job_counter.count_created(), 0) diff --git a/contract_queue_job/wizards/__init__.py b/contract_queue_job/wizards/__init__.py new file mode 100644 index 000000000..2e56cd6d0 --- /dev/null +++ b/contract_queue_job/wizards/__init__.py @@ -0,0 +1 @@ +from . import contract_manually_create_invoice diff --git a/contract_queue_job/wizards/contract_manually_create_invoice.py b/contract_queue_job/wizards/contract_manually_create_invoice.py new file mode 100644 index 000000000..e5ed84711 --- /dev/null +++ b/contract_queue_job/wizards/contract_manually_create_invoice.py @@ -0,0 +1,14 @@ +# Copyright 2020 ACSONE SA/NV +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from odoo import models + + +class ContractManuallyCreateInvoice(models.TransientModel): + + _inherit = "contract.manually.create.invoice" + + def create_invoice_queued(self): + self.ensure_one() + self.contract_to_invoice_ids._recurring_create_invoice() + return {} diff --git a/contract_queue_job/wizards/contract_manually_create_invoice.xml b/contract_queue_job/wizards/contract_manually_create_invoice.xml new file mode 100644 index 000000000..a36cb1e1b --- /dev/null +++ b/contract_queue_job/wizards/contract_manually_create_invoice.xml @@ -0,0 +1,21 @@ + + + + contract.manually.create.invoice + + + +