diff --git a/contract_sale_generation_merge/README.rst b/contract_sale_generation_merge/README.rst new file mode 100644 index 0000000000..8fad67cc64 --- /dev/null +++ b/contract_sale_generation_merge/README.rst @@ -0,0 +1,97 @@ +============================================ +Contracts Management - Recurring Sales Merge +============================================ + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:f10acd5ec9b53b9daf0b48c0d0ac856355aa3ceb797516528bffde97af0a61de + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |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/15.0/contract_sale_generation_merge + :alt: OCA/contract +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/contract-15-0/contract-15-0-contract_sale_generation_merge + :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/contract&target_branch=15.0 + :alt: Try me on Runboat + +|badge1| |badge2| |badge3| |badge4| |badge5| + +This module extends contract sale generation by merging contract lines into existing sales orders within the same commitment (delivery) date. + +**Table of contents** + +.. contents:: + :local: + +Usage +===== + +To use this module, you need to: + +1. Go to **Sales -> Orders -> Contracts** and select or create a new contract. +2. Add contract lines with the desired product and quantity. +3. Set the **Generation Type** field to **Sale** +4. Check the **Merge Existing Orders** field. This will merge the contract lines into an existing sale order within the same commitment (delivery) date as the **Date of Next Invoice** field on the contract. If no matching sale order is found, a new one will be created. + +To force the sale order generation, enable the **Debug Mode** and click on the **CREATE SALES** button. + +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 +~~~~~~~ + +* APSL-Nagarro + +Contributors +~~~~~~~~~~~~ + +* `APSL-Nagarro `_: + * Patryk Pyczko + +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-ppyczko| image:: https://github.com/ppyczko.png?size=40px + :target: https://github.com/ppyczko + :alt: ppyczko + +Current `maintainer `__: + +|maintainer-ppyczko| + +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_sale_generation_merge/__init__.py b/contract_sale_generation_merge/__init__.py new file mode 100644 index 0000000000..31660d6a96 --- /dev/null +++ b/contract_sale_generation_merge/__init__.py @@ -0,0 +1,3 @@ +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from . import models diff --git a/contract_sale_generation_merge/__manifest__.py b/contract_sale_generation_merge/__manifest__.py new file mode 100644 index 0000000000..6d263da9c0 --- /dev/null +++ b/contract_sale_generation_merge/__manifest__.py @@ -0,0 +1,20 @@ +# Copyright 2025 Patryk Pyczko (APSL-Nagarro) +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +{ + "name": "Contracts Management - Recurring Sales Merge", + "version": "15.0.1.0.0", + "summary": "Merges contract lines into existing sale orders " + "with the same commitment (delivery) date.", + "category": "Contract Management", + "website": "https://github.com/OCA/contract", + "author": "APSL-Nagarro, Odoo Community Association (OCA)", + "maintainers": ["ppyczko"], + "license": "AGPL-3", + "application": False, + "installable": True, + "depends": ["contract_sale_generation"], + "data": [ + "views/contract.xml", + ], +} diff --git a/contract_sale_generation_merge/i18n/ca.po b/contract_sale_generation_merge/i18n/ca.po new file mode 100644 index 0000000000..69bddacf16 --- /dev/null +++ b/contract_sale_generation_merge/i18n/ca.po @@ -0,0 +1,34 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * contract_sale_generation_merge +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 15.0\n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2025-02-17 08:59+0000\n" +"PO-Revision-Date: 2025-02-17 08:59+0000\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_sale_generation_merge +#: model:ir.model,name:contract_sale_generation_merge.model_contract_contract +msgid "Contract" +msgstr "Contracte" + +#. module: contract_sale_generation_merge +#: model:ir.model.fields,help:contract_sale_generation_merge.field_contract_contract__merge_sales_orders +msgid "" +"If enabled, contract lines will be added to an existing sales order with the" +" same commitment (delivery) date instead of creating a new one." +msgstr "" +"Si està habilitat, les línies del contracte s'afegiran a una comanda de venda existent amb la mateixa data de compromís (lliurament) en lloc de crear-ne una de nova." + +#. module: contract_sale_generation_merge +#: model:ir.model.fields,field_description:contract_sale_generation_merge.field_contract_contract__merge_sales_orders +msgid "Merge Existing Orders" +msgstr "Fusionar comandes existents" diff --git a/contract_sale_generation_merge/i18n/contract_sale_generation_merge.pot b/contract_sale_generation_merge/i18n/contract_sale_generation_merge.pot new file mode 100644 index 0000000000..1264a97f60 --- /dev/null +++ b/contract_sale_generation_merge/i18n/contract_sale_generation_merge.pot @@ -0,0 +1,33 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * contract_sale_generation_merge +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 15.0\n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2025-02-17 08:59+0000\n" +"PO-Revision-Date: 2025-02-17 08:59+0000\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_sale_generation_merge +#: model:ir.model,name:contract_sale_generation_merge.model_contract_contract +msgid "Contract" +msgstr "" + +#. module: contract_sale_generation_merge +#: model:ir.model.fields,help:contract_sale_generation_merge.field_contract_contract__merge_sales_orders +msgid "" +"If enabled, contract lines will be added to an existing sales order with the" +" same commitment (delivery) date instead of creating a new one." +msgstr "" + +#. module: contract_sale_generation_merge +#: model:ir.model.fields,field_description:contract_sale_generation_merge.field_contract_contract__merge_sales_orders +msgid "Merge Existing Orders" +msgstr "" diff --git a/contract_sale_generation_merge/i18n/es.po b/contract_sale_generation_merge/i18n/es.po new file mode 100644 index 0000000000..3611011efc --- /dev/null +++ b/contract_sale_generation_merge/i18n/es.po @@ -0,0 +1,34 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * contract_sale_generation_merge +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 15.0\n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2025-02-17 08:59+0000\n" +"PO-Revision-Date: 2025-02-17 08:59+0000\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_sale_generation_merge +#: model:ir.model,name:contract_sale_generation_merge.model_contract_contract +msgid "Contract" +msgstr "Contrato" + +#. module: contract_sale_generation_merge +#: model:ir.model.fields,help:contract_sale_generation_merge.field_contract_contract__merge_sales_orders +msgid "" +"If enabled, contract lines will be added to an existing sales order with the" +" same commitment (delivery) date instead of creating a new one." +msgstr "" +"Si está habilitado, las líneas del contrato se agregarán a un pedido de venta existente con la misma fecha de compromiso (entrega) en lugar de crear uno nuevo." + +#. module: contract_sale_generation_merge +#: model:ir.model.fields,field_description:contract_sale_generation_merge.field_contract_contract__merge_sales_orders +msgid "Merge Existing Orders" +msgstr "Fusionar Pedidos Existentes" diff --git a/contract_sale_generation_merge/models/__init__.py b/contract_sale_generation_merge/models/__init__.py new file mode 100644 index 0000000000..a51c4d1fba --- /dev/null +++ b/contract_sale_generation_merge/models/__init__.py @@ -0,0 +1,3 @@ +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from . import contract diff --git a/contract_sale_generation_merge/models/contract.py b/contract_sale_generation_merge/models/contract.py new file mode 100644 index 0000000000..43bc861bf7 --- /dev/null +++ b/contract_sale_generation_merge/models/contract.py @@ -0,0 +1,64 @@ +# Copyright 2025 Patryk Pyczko (APSL-Nagarro) +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from datetime import datetime, timedelta + +from odoo import fields, models + + +class ContractContract(models.Model): + _inherit = "contract.contract" + + merge_sales_orders = fields.Boolean( + string="Merge Existing Orders", + help="If enabled, contract lines will be added to an existing sales order " + "with the same commitment (delivery) date instead of creating a new one.", + ) + + def _recurring_create_sale(self, date_ref=False): + sales_values = self._prepare_recurring_sales_values(date_ref) + sale_orders = self.env["sale.order"] + new_sale_values = [] + + for sale_values in sales_values: + existing_sale = ( + self._get_existing_sale(sale_values) + if self.merge_sales_orders + else None + ) + + if existing_sale: + self._add_contract_lines(existing_sale) + sale_orders |= existing_sale + else: + new_sale_values.append(sale_values) + + if new_sale_values: + sale_orders |= self.env["sale.order"].create(new_sale_values) + + sale_orders.filtered(lambda sale: sale.contract_auto_confirm).action_confirm() + self._compute_recurring_next_date() + return sale_orders + + def _get_existing_sale(self, sale_values): + """Find an existing active sale order for the same partner + within the same day (commitment date).""" + date_only = sale_values["date_order"].date() + date_start = datetime.combine(date_only, datetime.min.time()) + date_end = datetime.combine(date_only + timedelta(days=1), datetime.min.time()) + + return self.env["sale.order"].search( + [ + ("partner_id", "=", sale_values["partner_id"]), + ("commitment_date", ">=", date_start), + ("commitment_date", "<", date_end), + ("state", "!=", "cancel"), + ], + limit=1, + ) + + def _add_contract_lines(self, existing_sale): + """Add contract lines to an existing sale order.""" + for contract_line in self.contract_line_ids: + sale_line_vals = contract_line._prepare_sale_line(order_id=existing_sale) + existing_sale.write({"order_line": [(0, 0, sale_line_vals)]}) diff --git a/contract_sale_generation_merge/readme/CONTRIBUTORS.rst b/contract_sale_generation_merge/readme/CONTRIBUTORS.rst new file mode 100644 index 0000000000..d9d047c2d6 --- /dev/null +++ b/contract_sale_generation_merge/readme/CONTRIBUTORS.rst @@ -0,0 +1,2 @@ +* `APSL-Nagarro `_: + * Patryk Pyczko diff --git a/contract_sale_generation_merge/readme/DESCRIPTION.rst b/contract_sale_generation_merge/readme/DESCRIPTION.rst new file mode 100644 index 0000000000..ac31a36234 --- /dev/null +++ b/contract_sale_generation_merge/readme/DESCRIPTION.rst @@ -0,0 +1 @@ +This module extends contract sale generation by merging contract lines into existing sales orders within the same commitment (delivery) date. diff --git a/contract_sale_generation_merge/readme/USAGE.rst b/contract_sale_generation_merge/readme/USAGE.rst new file mode 100644 index 0000000000..5e647d6ac1 --- /dev/null +++ b/contract_sale_generation_merge/readme/USAGE.rst @@ -0,0 +1,8 @@ +To use this module, you need to: + +1. Go to **Sales -> Orders -> Contracts** and select or create a new contract. +2. Add contract lines with the desired product and quantity. +3. Set the **Generation Type** field to **Sale** +4. Check the **Merge Existing Orders** field. This will merge the contract lines into an existing sale order within the same commitment (delivery) date as the **Date of Next Invoice** field on the contract. If no matching sale order is found, a new one will be created. + +To force the sale order generation, enable the **Debug Mode** and click on the **CREATE SALES** button. diff --git a/contract_sale_generation_merge/static/description/icon.png b/contract_sale_generation_merge/static/description/icon.png new file mode 100644 index 0000000000..3a0328b516 Binary files /dev/null and b/contract_sale_generation_merge/static/description/icon.png differ diff --git a/contract_sale_generation_merge/static/description/index.html b/contract_sale_generation_merge/static/description/index.html new file mode 100644 index 0000000000..9ba95bc50a --- /dev/null +++ b/contract_sale_generation_merge/static/description/index.html @@ -0,0 +1,438 @@ + + + + + +Contracts Management - Recurring Sales Merge + + + +
+

Contracts Management - Recurring Sales Merge

+ + +

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

+

This module extends contract sale generation by merging contract lines into existing sales orders within the same commitment (delivery) date.

+

Table of contents

+ +
+

Usage

+

To use this module, you need to:

+
    +
  1. Go to Sales -> Orders -> Contracts and select or create a new contract.
  2. +
  3. Add contract lines with the desired product and quantity.
  4. +
  5. Set the Generation Type field to Sale
  6. +
  7. Check the Merge Existing Orders field. This will merge the contract lines into an existing sale order within the same commitment (delivery) date as the Date of Next Invoice field on the contract. If no matching sale order is found, a new one will be created.
  8. +
+

To force the sale order generation, enable the Debug Mode and click on the CREATE SALES button.

+
+
+

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

+
    +
  • APSL-Nagarro
  • +
+
+
+

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:

+

ppyczko

+

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_sale_generation_merge/tests/__init__.py b/contract_sale_generation_merge/tests/__init__.py new file mode 100644 index 0000000000..73c26e171e --- /dev/null +++ b/contract_sale_generation_merge/tests/__init__.py @@ -0,0 +1,3 @@ +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from . import test_contact_sale_generation_merge diff --git a/contract_sale_generation_merge/tests/test_contact_sale_generation_merge.py b/contract_sale_generation_merge/tests/test_contact_sale_generation_merge.py new file mode 100644 index 0000000000..ef3ce8ecca --- /dev/null +++ b/contract_sale_generation_merge/tests/test_contact_sale_generation_merge.py @@ -0,0 +1,123 @@ +# Copyright 2025 Patryk Pyczko (APSL-Nagarro) +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from datetime import datetime + +from odoo.tests.common import TransactionCase + + +class TestContractSaleGenerationMerge(TransactionCase): + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.contract_model = cls.env["contract.contract"] + cls.sale_order_model = cls.env["sale.order"] + cls.sale_order_line_model = cls.env["sale.order.line"] + cls.contract_line_model = cls.env["contract.line"] + cls.partner = cls.env["res.partner"].create({"name": "Test Partner"}) + + cls.product = cls.env["product.product"].create( + { + "name": "Test Product", + "type": "service", + } + ) + + cls.contract = cls.contract_model.create( + { + "name": "Test Contract", + "partner_id": cls.partner.id, + "merge_sales_orders": True, + } + ) + + cls.contract_line = cls.contract_line_model.create( + { + "contract_id": cls.contract.id, + "name": "Test Contract Line", + "product_id": cls.product.id, + "price_unit": 100.0, + "uom_id": cls.product.uom_id.id, + "recurring_next_date": datetime.today(), + } + ) + + cls.commitment_date = datetime.today() + cls.existing_order = cls.sale_order_model.create( + { + "partner_id": cls.partner.id, + "state": "draft", + "commitment_date": cls.commitment_date, + } + ) + + def test_create_new_sale_order(self): + """Should create a new sale order if no existing order is found.""" + self.contract._recurring_create_sale() + + sale_orders = self.sale_order_model.search( + [("partner_id", "=", self.partner.id)] + ) + self.assertEqual( + len(sale_orders), 1, "A new sale order should have been created." + ) + + def test_merge_existing_sale_order(self): + """Should merge with an existing sale order if merge_sales_orders is enabled.""" + self.contract._recurring_create_sale() + + sale_orders = self.sale_order_model.search( + [("partner_id", "=", self.partner.id)] + ) + self.assertEqual( + len(sale_orders), + 1, + "Contract lines should have merged into the existing order.", + ) + self.assertEqual( + len(self.existing_order.order_line), + 1, + "A contract line should have been added.", + ) + + def test_no_merge_when_disabled(self): + """Should create a new sale order if merge_sales_orders is disabled.""" + self.contract.merge_sales_orders = False + + self.contract._recurring_create_sale() + + sale_orders = self.sale_order_model.search( + [("partner_id", "=", self.partner.id)] + ) + self.assertEqual( + len(sale_orders), 2, "A new sale order should have been created." + ) + + def test_ignore_canceled_orders(self): + """Should not merge with an order that has a 'cancel' state.""" + self.sale_order_model.create( + { + "partner_id": self.partner.id, + "state": "cancel", + "commitment_date": self.commitment_date, + } + ) + + self.contract._recurring_create_sale() + + sale_orders = self.sale_order_model.search( + [("partner_id", "=", self.partner.id), ("state", "!=", "cancel")] + ) + self.assertEqual( + len(sale_orders), 1, "A new sale order should have been created." + ) + + def test_contract_lines_added_correctly(self): + """Should add contract lines correctly to an existing order.""" + self.contract._add_contract_lines(self.existing_order) + + self.assertEqual( + len(self.existing_order.order_line), + 1, + "A contract line should have been added.", + ) diff --git a/contract_sale_generation_merge/views/contract.xml b/contract_sale_generation_merge/views/contract.xml new file mode 100644 index 0000000000..08b59aa269 --- /dev/null +++ b/contract_sale_generation_merge/views/contract.xml @@ -0,0 +1,23 @@ + + + + + + + contract.contract.form.recurring.sale.merge + contract.contract + + + + + + + + diff --git a/setup/contract_sale_generation_merge/odoo/addons/contract_sale_generation_merge b/setup/contract_sale_generation_merge/odoo/addons/contract_sale_generation_merge new file mode 120000 index 0000000000..696e1126a1 --- /dev/null +++ b/setup/contract_sale_generation_merge/odoo/addons/contract_sale_generation_merge @@ -0,0 +1 @@ +../../../../contract_sale_generation_merge \ No newline at end of file diff --git a/setup/contract_sale_generation_merge/setup.py b/setup/contract_sale_generation_merge/setup.py new file mode 100644 index 0000000000..28c57bb640 --- /dev/null +++ b/setup/contract_sale_generation_merge/setup.py @@ -0,0 +1,6 @@ +import setuptools + +setuptools.setup( + setup_requires=['setuptools-odoo'], + odoo_addon=True, +)