diff --git a/contract_min_duration/README.rst b/contract_min_duration/README.rst new file mode 100644 index 0000000000..00fba49147 --- /dev/null +++ b/contract_min_duration/README.rst @@ -0,0 +1,100 @@ +.. image:: https://odoo-community.org/readme-banner-image + :target: https://odoo-community.org/get-involved?utm_source=readme + :alt: Odoo Community Association + +========================= +Contract Minimum Duration +========================= + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:204d88cac556f56f9ea4f02945e1d0197b6507d0996b25137a9c4cda58a69c80 + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |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/license-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/19.0/contract_min_duration + :alt: OCA/contract +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/contract-19-0/contract-19-0-contract_min_duration + :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=19.0 + :alt: Try me on Runboat + +|badge1| |badge2| |badge3| |badge4| |badge5| + +This module extends the **Contract** module to enforce a minimum +contract duration. + +It adds a ``min_contract_end_date`` field to contracts. If a user +attempts to set an end date (e.g., via termination) that is earlier than +this minimum date, the system automatically extends the end date to the +minimum required date, ensuring the customer is invoiced for the full +minimum period. + +**Table of contents** + +.. contents:: + :local: + +Usage +===== + +To use this module: + +1. Go to **Accounting > Customers > Contracts**. +2. Open or create a contract. +3. In the **Dates** section (or wherever the field is placed), set the + **Minimum End Date**. +4. If you try to terminate the contract or set a **Date End** that is + **before** this minimum date, the system will automatically correct + the **Date End** to match the **Minimum End Date**. + +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 +------- + +* bosd + +Contributors +------------ + +- bosd emiel.vanbokhoven@obs-solutions.com + +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/contract `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/contract_min_duration/__init__.py b/contract_min_duration/__init__.py new file mode 100644 index 0000000000..0650744f6b --- /dev/null +++ b/contract_min_duration/__init__.py @@ -0,0 +1 @@ +from . import models diff --git a/contract_min_duration/__manifest__.py b/contract_min_duration/__manifest__.py new file mode 100644 index 0000000000..5583d4f86b --- /dev/null +++ b/contract_min_duration/__manifest__.py @@ -0,0 +1,16 @@ +{ + "name": "Contract Minimum Duration", + "summary": "Enforce minimum contract duration", + "version": "19.0.1.0.0", + "category": "Accounting", + "website": "https://github.com/OCA/contract", + "author": "bosd, Odoo Community Association (OCA)", + "license": "AGPL-3", + "depends": [ + "contract", + ], + "data": [ + "views/contract_view.xml", + ], + "installable": True, +} diff --git a/contract_min_duration/models/__init__.py b/contract_min_duration/models/__init__.py new file mode 100644 index 0000000000..99a5468ac8 --- /dev/null +++ b/contract_min_duration/models/__init__.py @@ -0,0 +1 @@ +from . import contract diff --git a/contract_min_duration/models/contract.py b/contract_min_duration/models/contract.py new file mode 100644 index 0000000000..9f58f98abb --- /dev/null +++ b/contract_min_duration/models/contract.py @@ -0,0 +1,50 @@ +# Copyright 2025 bosd +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from odoo import api, fields, models + + +class ContractContract(models.Model): + _inherit = "contract.contract" + + min_contract_end_date = fields.Date( + string="Minimum End Date", + help="The contract cannot end before this date. " + "If a termination or end date is set earlier, " + "it will be automatically extended to this date.", + ) + + def write(self, vals): + if "date_end" not in vals and "min_contract_end_date" not in vals: + return super().write(vals) + + for contract in self: + _vals = vals.copy() + min_date_str = ( + _vals["min_contract_end_date"] + if "min_contract_end_date" in _vals + else contract.min_contract_end_date + ) + end_date_str = ( + _vals["date_end"] if "date_end" in _vals else contract.date_end + ) + + if min_date_str and end_date_str: + min_date = fields.Date.to_date(min_date_str) + end_date = fields.Date.to_date(end_date_str) + if end_date < min_date: + _vals["date_end"] = min_date_str + super(ContractContract, contract).write(_vals) + return True + + @api.model_create_multi + def create(self, vals_list): + for vals in vals_list: + min_date_str = vals.get("min_contract_end_date") + end_date_str = vals.get("date_end") + if min_date_str and end_date_str: + min_date = fields.Date.to_date(min_date_str) + end_date = fields.Date.to_date(end_date_str) + if min_date and end_date and end_date < min_date: + vals["date_end"] = min_date_str + return super().create(vals_list) diff --git a/contract_min_duration/pyproject.toml b/contract_min_duration/pyproject.toml new file mode 100644 index 0000000000..4231d0cccb --- /dev/null +++ b/contract_min_duration/pyproject.toml @@ -0,0 +1,3 @@ +[build-system] +requires = ["whool"] +build-backend = "whool.buildapi" diff --git a/contract_min_duration/readme/CONTRIBUTORS.md b/contract_min_duration/readme/CONTRIBUTORS.md new file mode 100644 index 0000000000..4697864203 --- /dev/null +++ b/contract_min_duration/readme/CONTRIBUTORS.md @@ -0,0 +1 @@ +* bosd diff --git a/contract_min_duration/readme/DESCRIPTION.md b/contract_min_duration/readme/DESCRIPTION.md new file mode 100644 index 0000000000..7bc54cea5c --- /dev/null +++ b/contract_min_duration/readme/DESCRIPTION.md @@ -0,0 +1,3 @@ +This module extends the **Contract** module to enforce a minimum contract duration. + +It adds a `min_contract_end_date` field to contracts. If a user attempts to set an end date (e.g., via termination) that is earlier than this minimum date, the system automatically extends the end date to the minimum required date, ensuring the customer is invoiced for the full minimum period. diff --git a/contract_min_duration/readme/USAGE.md b/contract_min_duration/readme/USAGE.md new file mode 100644 index 0000000000..93e91c1250 --- /dev/null +++ b/contract_min_duration/readme/USAGE.md @@ -0,0 +1,6 @@ +To use this module: + +1. Go to **Accounting > Customers > Contracts**. +2. Open or create a contract. +3. In the **Dates** section (or wherever the field is placed), set the **Minimum End Date**. +4. If you try to terminate the contract or set a **Date End** that is **before** this minimum date, the system will automatically correct the **Date End** to match the **Minimum End Date**. diff --git a/contract_min_duration/static/description/icon.png b/contract_min_duration/static/description/icon.png new file mode 100644 index 0000000000..1dcc49c24f Binary files /dev/null and b/contract_min_duration/static/description/icon.png differ diff --git a/contract_min_duration/static/description/index.html b/contract_min_duration/static/description/index.html new file mode 100644 index 0000000000..2b3b8ecfa8 --- /dev/null +++ b/contract_min_duration/static/description/index.html @@ -0,0 +1,449 @@ + + + + + +README.rst + + + +
+ + + +Odoo Community Association + +
+

Contract Minimum Duration

+ +

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

+

This module extends the Contract module to enforce a minimum +contract duration.

+

It adds a min_contract_end_date field to contracts. If a user +attempts to set an end date (e.g., via termination) that is earlier than +this minimum date, the system automatically extends the end date to the +minimum required date, ensuring the customer is invoiced for the full +minimum period.

+

Table of contents

+ +
+

Usage

+

To use this module:

+
    +
  1. Go to Accounting > Customers > Contracts.
  2. +
  3. Open or create a contract.
  4. +
  5. In the Dates section (or wherever the field is placed), set the +Minimum End Date.
  6. +
  7. If you try to terminate the contract or set a Date End that is +before this minimum date, the system will automatically correct +the Date End to match the Minimum End Date.
  8. +
+
+
+

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

+
    +
  • bosd
  • +
+
+ +
+

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/contract project on GitHub.

+

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

+
+
+
+
+ + diff --git a/contract_min_duration/tests/__init__.py b/contract_min_duration/tests/__init__.py new file mode 100644 index 0000000000..b67a2d102e --- /dev/null +++ b/contract_min_duration/tests/__init__.py @@ -0,0 +1 @@ +from . import test_contract_min_duration diff --git a/contract_min_duration/tests/test_contract_min_duration.py b/contract_min_duration/tests/test_contract_min_duration.py new file mode 100644 index 0000000000..980245723b --- /dev/null +++ b/contract_min_duration/tests/test_contract_min_duration.py @@ -0,0 +1,97 @@ +# Copyright 2025 bosd +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from odoo import fields +from odoo.tests import common, tagged + + +@tagged("post_install", "-at_install") +class TestContractMinDuration(common.TransactionCase): + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.partner = cls.env["res.partner"].create({"name": "Test Partner"}) + cls.product = cls.env["product.product"].create( + {"name": "Test Service", "type": "service"} + ) + cls.contract = cls.env["contract.contract"].create( + { + "name": "Test Min Duration Contract", + "partner_id": cls.partner.id, + "recurring_interval": 1, + "recurring_rule_type": "monthly", + "date_start": "2025-01-01", + "min_contract_end_date": "2025-06-30", + "contract_line_ids": [ + ( + 0, + 0, + { + "product_id": cls.product.id, + "name": "Service", + "quantity": 1, + "price_unit": 100, + "recurring_interval": 1, + "recurring_rule_type": "monthly", + "date_start": "2025-01-01", + }, + ) + ], + } + ) + + def test_end_date_enforcement_on_write(self): + """Test that setting date_end earlier than min_contract_end_date + forces it to min date.""" + # Try to terminate on Feb 28th (early) + self.contract.write({"date_end": "2025-02-28"}) + + # Expectation: date_end should be auto-extended to 2025-06-30 + self.assertEqual( + self.contract.date_end, + fields.Date.to_date("2025-06-30"), + "Contract end date should be extended to minimum end date", + ) + + def test_end_date_later_than_min(self): + """Test that setting date_end later than min_contract_end_date + works as normal.""" + # Terminate on Dec 31st (allowed) + self.contract.write({"date_end": "2025-12-31"}) + + self.assertEqual( + self.contract.date_end, + fields.Date.to_date("2025-12-31"), + "Contract end date should be respected if after minimum", + ) + + def test_change_min_date_triggers_check(self): + """Test that increasing min_contract_end_date updates existing date_end.""" + # Set an end date that is valid FOR NOW + self.contract.write({"date_end": "2025-08-31"}) # Initial min is June + + # Now extend the minimum requirement to September + self.contract.write({"min_contract_end_date": "2025-09-30"}) + + self.assertEqual( + self.contract.date_end, + fields.Date.to_date("2025-09-30"), + "Existing end date should be updated when minimum constraint increases", + ) + + def test_end_date_enforcement_on_create(self): + """Test that creating a contract with date_end < min_date is corrected.""" + contract = self.env["contract.contract"].create( + { + "name": "Test Create Contract", + "partner_id": self.partner.id, + "date_start": "2025-01-01", + "min_contract_end_date": "2025-06-30", + "date_end": "2025-03-31", # Invalid end date + } + ) + self.assertEqual( + contract.date_end, + fields.Date.to_date("2025-06-30"), + "Contract end date should be extended to minimum on creation", + ) diff --git a/contract_min_duration/views/contract_view.xml b/contract_min_duration/views/contract_view.xml new file mode 100644 index 0000000000..7f66f107eb --- /dev/null +++ b/contract_min_duration/views/contract_view.xml @@ -0,0 +1,13 @@ + + + + contract.contract.form.inherit.min.duration + contract.contract + + + + + + + + diff --git a/test-requirements.txt b/test-requirements.txt new file mode 100644 index 0000000000..b4db471c5f --- /dev/null +++ b/test-requirements.txt @@ -0,0 +1 @@ +odoo-addon-contract @ git+https://github.com/OCA/contract@refs/pull/1312/head#subdirectory=contract