From c40cbab2dcd57c969adf622db0107d63795e1d4f Mon Sep 17 00:00:00 2001 From: sbejaoui Date: Wed, 16 Apr 2025 14:47:38 +0200 Subject: [PATCH 01/18] [REF] split contract module into: contract, contract_line_successor, contract_termination [REF] contract: remove abstract conract model simplify contract definition [IMP] contract: clean UI and improve UX [FIX] contract: fix tour test [IMP] contract: several imp - use Command - mark _update_recurring_next_date deprecated - use list instead of tree [FIX] contract: recurring_next_date is required only if next_period_date_end is set [IMP] contract: contract template inherit from mail thread [IMP] index contract.template product_id [IMP] contract_line_successor: Auto renew interval should be different then 0 [FIX] contract_line_successor: Adding markup to messages [IMP] contract: simplify multi-company ir.rule domain [IMP] contract: remove invisible fields [IMP] contract: use Command new sytnax [FIX] contract: remove useless odoo-modules declaration in js [FIX] contract: use text-end in report --- contract_line_successor/README.rst | 166 ++++ contract_line_successor/__init__.py | 2 + contract_line_successor/__manifest__.py | 24 + .../data/contract_renew_cron.xml | 12 + contract_line_successor/models/__init__.py | 5 + .../models/contract_contract.py | 25 + .../models/contract_line.py | 835 +++++++++++++++++ .../models/contract_line_constraints.py | 429 +++++++++ .../models/contract_template_line.py | 35 + contract_line_successor/models/res_company.py | 15 + .../models/res_config_settings.py | 18 + contract_line_successor/pyproject.toml | 3 + contract_line_successor/readme/CONFIGURE.md | 10 + .../readme/CONTRIBUTORS.md | 1 + contract_line_successor/readme/DESCRIPTION.md | 40 + contract_line_successor/readme/USAGE.md | 6 + .../security/ir.model.access.csv | 2 + .../static/description/icon.png | Bin 0 -> 9455 bytes .../static/description/index.html | 519 +++++++++++ contract_line_successor/tests/__init__.py | 1 + .../tests/test_contract.py | 847 ++++++++++++++++++ .../views/contract_contract.xml | 73 ++ .../views/contract_line.xml | 134 +++ .../views/contract_template.xml | 19 + .../views/contract_template_line.xml | 50 ++ .../views/res_config_settings.xml | 16 + contract_line_successor/wizards/__init__.py | 1 + .../wizards/contract_line_wizard.py | 53 ++ .../wizards/contract_line_wizard.xml | 110 +++ 29 files changed, 3451 insertions(+) create mode 100644 contract_line_successor/README.rst create mode 100644 contract_line_successor/__init__.py create mode 100644 contract_line_successor/__manifest__.py create mode 100644 contract_line_successor/data/contract_renew_cron.xml create mode 100644 contract_line_successor/models/__init__.py create mode 100644 contract_line_successor/models/contract_contract.py create mode 100644 contract_line_successor/models/contract_line.py create mode 100644 contract_line_successor/models/contract_line_constraints.py create mode 100644 contract_line_successor/models/contract_template_line.py create mode 100644 contract_line_successor/models/res_company.py create mode 100644 contract_line_successor/models/res_config_settings.py create mode 100644 contract_line_successor/pyproject.toml create mode 100644 contract_line_successor/readme/CONFIGURE.md create mode 100644 contract_line_successor/readme/CONTRIBUTORS.md create mode 100644 contract_line_successor/readme/DESCRIPTION.md create mode 100644 contract_line_successor/readme/USAGE.md create mode 100644 contract_line_successor/security/ir.model.access.csv create mode 100644 contract_line_successor/static/description/icon.png create mode 100644 contract_line_successor/static/description/index.html create mode 100644 contract_line_successor/tests/__init__.py create mode 100644 contract_line_successor/tests/test_contract.py create mode 100644 contract_line_successor/views/contract_contract.xml create mode 100644 contract_line_successor/views/contract_line.xml create mode 100644 contract_line_successor/views/contract_template.xml create mode 100644 contract_line_successor/views/contract_template_line.xml create mode 100644 contract_line_successor/views/res_config_settings.xml create mode 100644 contract_line_successor/wizards/__init__.py create mode 100644 contract_line_successor/wizards/contract_line_wizard.py create mode 100644 contract_line_successor/wizards/contract_line_wizard.xml diff --git a/contract_line_successor/README.rst b/contract_line_successor/README.rst new file mode 100644 index 0000000000..58f012ad4e --- /dev/null +++ b/contract_line_successor/README.rst @@ -0,0 +1,166 @@ +======================= +Contract Line Successor +======================= + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:f263db27174e72407a77645bfcb7854c8395d8d649863283f9ed304b7ea5f072 + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |badge1| image:: https://img.shields.io/badge/maturity-Production%2FStable-green.png + :target: https://odoo-community.org/page/development-status + :alt: Production/Stable +.. |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/18.0/contract_line_successor + :alt: OCA/contract +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/contract-18-0/contract-18-0-contract_line_successor + :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=18.0 + :alt: Try me on Runboat + +|badge1| |badge2| |badge3| |badge4| |badge5| + +| **Contract Line Successor** extends ``contract.line`` model to support + advanced contract lifecycle management, including suspension, + successor planning, cancellation, and renewal. +| It provides a flexible and robust framework for managing complex + contract line scenarios in a clean and structured way. + +Features +-------- + +- **Successor and Predecessor Management** + + - Link contract lines with successor and predecessor lines. + - Plan successors automatically or manually after a stop or + suspension. + +- **Contract Line Lifecycle States** + + - Manage contract lines with the following computed states: + + - ``Upcoming`` + - ``In-Progress`` + - ``To Renew`` + - ``Upcoming Close`` + - ``Closed`` + - ``Canceled`` + +- **Lifecycle Operations** + + - Stop a contract line. + - Plan a successor for a contract line. + - Stop and plan a successor in one operation (useful for + suspensions). + - Cancel and un-cancel contract lines. + - Renew contract lines automatically (new line or extension). + +- **Auto-Renewal Handling** + + - Auto-renewal based on company settings (extend existing line or + create a new one). + - Cron job to automate renewal of eligible contract lines. + +- **Data Integrity and Validation** + + - Prevent invalid successor or predecessor configurations. + - Validate state transitions and date overlaps. + - Ensure clean renewal and cancellation workflows. + +- **Audit Trail** + + - Automatic posting of chatter messages for lifecycle events like + stops, renewals, suspensions, cancellations, etc. + +**Table of contents** + +.. contents:: + :local: + +Configuration +============= + +- | **Auto-Renewal Strategy** + | In the company settings, define whether renewing a contract line: + + - Extends the current line (updates ``date_end``), + - or creates a new successor contract line. + + | Field: + | ``Company > Configuration > Contracts > Create new contract line at renewal`` + +- | **Scheduled Actions** + | Ensure the scheduled action ``Contract Line: Auto Renew`` is + activated if you want automatic renewal without manual + intervention. + +Usage +===== + +1. **Select a contract** and enable the option **"Recurrence at line + level?"**. +2. Once enabled, you will have access to several actions at the contract + line level: + + - **Stop** a contract line and optionally **plan a successor**. + - **Handle temporary suspensions** and **resume** the contract line + after the suspension period. + - **Cancel** and **un-cancel** contract lines if necessary. + - **Renew** contract lines either by **extending** the current line + or by **creating a new successor line** automatically. + +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 +------- + +* ACSONE SA/NV + +Contributors +------------ + +- Souheil Bejaoui souheil.bejaoui@acsone.eu (ACSONE SA/NV) + +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 + +Current `maintainer `__: + +|maintainer-sbejaoui| + +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_line_successor/__init__.py b/contract_line_successor/__init__.py new file mode 100644 index 0000000000..aee8895e7a --- /dev/null +++ b/contract_line_successor/__init__.py @@ -0,0 +1,2 @@ +from . import models +from . import wizards diff --git a/contract_line_successor/__manifest__.py b/contract_line_successor/__manifest__.py new file mode 100644 index 0000000000..4cb2274842 --- /dev/null +++ b/contract_line_successor/__manifest__.py @@ -0,0 +1,24 @@ +# Copyright 2018 ACSONE SA/NV +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +{ + "name": "Contract Line Successor", + "version": "18.0.1.0.0", + "license": "AGPL-3", + "author": "ACSONE SA/NV,Odoo Community Association (OCA)", + "website": "https://github.com/OCA/contract", + "depends": ["contract"], + "development_status": "Production/Stable", + "maintainers": ["sbejaoui"], + "data": [ + "data/contract_renew_cron.xml", + "security/ir.model.access.csv", + "views/contract_template.xml", + "views/contract_contract.xml", + "views/contract_template_line.xml", + "views/contract_line.xml", + "wizards/contract_line_wizard.xml", + "views/res_config_settings.xml", + ], + "demo": [], +} diff --git a/contract_line_successor/data/contract_renew_cron.xml b/contract_line_successor/data/contract_renew_cron.xml new file mode 100644 index 0000000000..aea0e67fb7 --- /dev/null +++ b/contract_line_successor/data/contract_renew_cron.xml @@ -0,0 +1,12 @@ + + + + Renew Contract lines + + code + model.cron_renew_contract_line() + + 1 + days + + diff --git a/contract_line_successor/models/__init__.py b/contract_line_successor/models/__init__.py new file mode 100644 index 0000000000..e7a0fab28a --- /dev/null +++ b/contract_line_successor/models/__init__.py @@ -0,0 +1,5 @@ +from . import contract_contract +from . import contract_line +from . import contract_template_line +from . import res_company +from . import res_config_settings diff --git a/contract_line_successor/models/contract_contract.py b/contract_line_successor/models/contract_contract.py new file mode 100644 index 0000000000..d14ec8ec25 --- /dev/null +++ b/contract_line_successor/models/contract_contract.py @@ -0,0 +1,25 @@ +# Copyright 2025 ACSONE SA/NV +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo import api, fields, models + + +class ContractContract(models.Model): + _inherit = "contract.contract" + + line_recurrence = fields.Boolean(default=True) + + @api.depends("contract_line_ids.date_end", "contract_line_ids.is_canceled") + def _compute_date_end(self): + for contract in self: + contract.date_end = False + date_end = contract.contract_line_ids.filtered( + lambda line: not line.is_canceled + ).mapped("date_end") + if date_end and all(date_end): + contract.date_end = max(date_end) + + def _convert_contract_lines(self, contract): + new_lines = super()._convert_contract_lines(contract) + new_lines._onchange_is_auto_renew() + return new_lines diff --git a/contract_line_successor/models/contract_line.py b/contract_line_successor/models/contract_line.py new file mode 100644 index 0000000000..51fcb04771 --- /dev/null +++ b/contract_line_successor/models/contract_line.py @@ -0,0 +1,835 @@ +# Copyright 2025 ACSONE SA/NV +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from datetime import timedelta + +from dateutil.relativedelta import relativedelta +from markupsafe import Markup + +from odoo import _, api, fields, models +from odoo.exceptions import ValidationError + +from .contract_line_constraints import get_allowed + + +class ContractLine(models.Model): + _inherit = "contract.line" + + termination_notice_date = fields.Date( + compute="_compute_termination_notice_date", + store=True, + copy=False, + ) + successor_contract_line_id = fields.Many2one( + comodel_name="contract.line", + string="Successor Contract Line", + required=False, + readonly=True, + index=True, + copy=False, + help="In case of restart after suspension, this field contain the new " + "contract line created.", + ) + predecessor_contract_line_id = fields.Many2one( + comodel_name="contract.line", + string="Predecessor Contract Line", + required=False, + readonly=True, + index=True, + copy=False, + help="Contract Line origin of this one.", + ) + manual_renew_needed = fields.Boolean( + default=False, + help="This flag is used to make a difference between a definitive stop" + "and temporary one for which a user is not able to plan a" + "successor in advance", + ) + is_plan_successor_allowed = fields.Boolean( + string="Plan successor allowed?", compute="_compute_allowed" + ) + is_stop_plan_successor_allowed = fields.Boolean( + string="Stop/Plan successor allowed?", compute="_compute_allowed" + ) + is_stop_allowed = fields.Boolean(string="Stop allowed?", compute="_compute_allowed") + is_cancel_allowed = fields.Boolean( + string="Cancel allowed?", compute="_compute_allowed" + ) + is_un_cancel_allowed = fields.Boolean( + string="Un-Cancel allowed?", compute="_compute_allowed" + ) + state = fields.Selection( + selection=[ + ("upcoming", "Upcoming"), + ("in-progress", "In-progress"), + ("to-renew", "To renew"), + ("upcoming-close", "Upcoming Close"), + ("closed", "Closed"), + ("canceled", "Canceled"), + ], + compute="_compute_state", + search="_search_state", + ) + + @api.depends( + "date_end", + "termination_notice_rule_type", + "termination_notice_interval", + ) + def _compute_termination_notice_date(self): + for rec in self: + if rec.date_end: + rec.termination_notice_date = rec.date_end - self.get_relative_delta( + rec.termination_notice_rule_type, + rec.termination_notice_interval, + ) + else: + rec.termination_notice_date = False + + @api.depends( + "date_start", + "date_end", + "last_date_invoiced", + "is_auto_renew", + "successor_contract_line_id", + "predecessor_contract_line_id", + "is_canceled", + ) + def _compute_allowed(self): + for rec in self: + rec.update( + { + "is_plan_successor_allowed": False, + "is_stop_plan_successor_allowed": False, + "is_stop_allowed": False, + "is_cancel_allowed": False, + "is_un_cancel_allowed": False, + } + ) + if rec.date_start: + allowed = get_allowed( + rec.date_start, + rec.date_end, + rec.last_date_invoiced, + rec.is_auto_renew, + rec.successor_contract_line_id, + rec.predecessor_contract_line_id, + rec.is_canceled, + ) + if allowed: + rec.update( + { + "is_plan_successor_allowed": allowed.plan_successor, + "is_stop_plan_successor_allowed": ( + allowed.stop_plan_successor + ), + "is_stop_allowed": allowed.stop, + "is_cancel_allowed": allowed.cancel, + "is_un_cancel_allowed": allowed.uncancel, + } + ) + + @api.constrains("is_auto_renew", "successor_contract_line_id", "date_end") + def _check_allowed(self): + """ + logical impossible combination: + * a line with is_auto_renew True should have date_end and + couldn't have successor_contract_line_id + * a line without date_end can't have successor_contract_line_id + + """ + for rec in self: + if rec.is_auto_renew: + if rec.successor_contract_line_id: + raise ValidationError( + _( + "A contract line with a successor " + "can't be set to auto-renew" + ) + ) + if not rec.date_end: + raise ValidationError(_("An auto-renew line must have a end date")) + else: + if not rec.date_end and rec.successor_contract_line_id: + raise ValidationError( + _("A contract line with a successor " "must have a end date") + ) + + @api.constrains("successor_contract_line_id", "date_end") + def _check_overlap_successor(self): + for rec in self: + if rec.date_end and rec.successor_contract_line_id: + if rec.date_end >= rec.successor_contract_line_id.date_start: + raise ValidationError( + _("Contract line and its successor overlapped") + ) + + @api.constrains("predecessor_contract_line_id", "date_start") + def _check_overlap_predecessor(self): + for rec in self: + if ( + rec.predecessor_contract_line_id + and rec.predecessor_contract_line_id.date_end + ): + if rec.date_start <= rec.predecessor_contract_line_id.date_end: + raise ValidationError( + _("Contract line and its predecessor overlapped") + ) + + @api.depends( + "is_canceled", + "date_start", + "date_end", + "is_auto_renew", + "manual_renew_needed", + "termination_notice_date", + "successor_contract_line_id", + ) + def _compute_state(self): + today = fields.Date.context_today(self) + for rec in self: + rec.state = False + if rec.display_type: + continue + if rec.is_canceled: + rec.state = "canceled" + continue + + if rec.date_start and rec.date_start > today: + # Before period + rec.state = "upcoming" + continue + if ( + rec.date_start + and rec.date_start <= today + and (not rec.date_end or rec.date_end >= today) + ): + # In period + if ( + rec.termination_notice_date + and rec.termination_notice_date < today + and not rec.is_auto_renew + and not rec.manual_renew_needed + ): + rec.state = "upcoming-close" + else: + rec.state = "in-progress" + continue + if rec.date_end and rec.date_end < today: + # After + if ( + rec.manual_renew_needed + and not rec.successor_contract_line_id + or rec.is_auto_renew + ): + rec.state = "to-renew" + else: + rec.state = "closed" + + @api.model + def _get_state_domain(self, state): + today = fields.Date.context_today(self) + if state == "upcoming": + return [ + "&", + ("date_start", ">", today), + ("is_canceled", "=", False), + ] + if state == "in-progress": + return [ + "&", + "&", + "&", + ("date_start", "<=", today), + ("is_canceled", "=", False), + "|", + ("date_end", ">=", today), + ("date_end", "=", False), + "|", + ("is_auto_renew", "=", True), + "&", + ("is_auto_renew", "=", False), + ("termination_notice_date", ">", today), + ] + if state == "to-renew": + return [ + "&", + "&", + ("is_canceled", "=", False), + ("date_end", "<", today), + "|", + "&", + ("manual_renew_needed", "=", True), + ("successor_contract_line_id", "=", False), + ("is_auto_renew", "=", True), + ] + if state == "upcoming-close": + return [ + "&", + "&", + "&", + "&", + "&", + ("date_start", "<=", today), + ("is_auto_renew", "=", False), + ("manual_renew_needed", "=", False), + ("is_canceled", "=", False), + ("termination_notice_date", "<", today), + ("date_end", ">=", today), + ] + if state == "closed": + return [ + "&", + "&", + "&", + ("is_canceled", "=", False), + ("date_end", "<", today), + ("is_auto_renew", "=", False), + "|", + "&", + ("manual_renew_needed", "=", True), + ("successor_contract_line_id", "!=", False), + ("manual_renew_needed", "=", False), + ] + if state == "canceled": + return [("is_canceled", "=", True)] + if not state: + return [("display_type", "!=", False)] + + @api.model + def _search_state(self, operator, value): + states = [ + "upcoming", + "in-progress", + "to-renew", + "upcoming-close", + "closed", + "canceled", + False, + ] + if operator == "=": + return self._get_state_domain(value) + if operator == "!=": + domain = [] + for state in states: + if state != value: + if domain: + domain.insert(0, "|") + domain.extend(self._get_state_domain(state)) + return domain + if operator == "in": + domain = [] + for state in value: + if domain: + domain.insert(0, "|") + domain.extend(self._get_state_domain(state)) + return domain + + if operator == "not in": + if set(value) == set(states): + return [("id", "=", False)] + return self._search_state( + "in", [state for state in states if state not in value] + ) + + @api.model + def _get_first_date_end( + self, date_start, auto_renew_rule_type, auto_renew_interval + ): + return ( + date_start + + self.get_relative_delta(auto_renew_rule_type, auto_renew_interval) + - relativedelta(days=1) + ) + + @api.onchange( + "date_start", + "is_auto_renew", + "auto_renew_rule_type", + "auto_renew_interval", + ) + def _onchange_is_auto_renew(self): + """Date end should be auto-computed if a contract line is set to + auto_renew""" + for rec in self.filtered("is_auto_renew"): + if rec.date_start: + rec.date_end = self._get_first_date_end( + rec.date_start, + rec.auto_renew_rule_type, + rec.auto_renew_interval, + ) + + @api.constrains("is_canceled", "is_auto_renew") + def _check_auto_renew_canceled_lines(self): + for rec in self: + if rec.is_canceled and rec.is_auto_renew: + raise ValidationError( + _("A canceled contract line can't be set to auto-renew") + ) + + def _delay(self, delay_delta): + """ + Delay a contract line + :param delay_delta: delay relative delta + :return: delayed contract line + """ + for rec in self: + if rec.last_date_invoiced: + raise ValidationError( + _("You can't delay a contract line " "invoiced at least one time.") + ) + new_date_start = rec.date_start + delay_delta + if rec.date_end: + new_date_end = rec.date_end + delay_delta + else: + new_date_end = False + new_recurring_next_date = self.get_next_invoice_date( + new_date_start, + rec.recurring_invoicing_type, + rec.recurring_invoicing_offset, + rec.recurring_rule_type, + rec.recurring_interval, + max_date_end=new_date_end, + ) + rec.write( + { + "date_start": new_date_start, + "date_end": new_date_end, + "recurring_next_date": new_recurring_next_date, + } + ) + + def _prepare_value_for_stop(self, date_end, manual_renew_needed): + self.ensure_one() + return { + "date_end": date_end, + "is_auto_renew": False, + "manual_renew_needed": manual_renew_needed, + "recurring_next_date": self.get_next_invoice_date( + self.next_period_date_start, + self.recurring_invoicing_type, + self.recurring_invoicing_offset, + self.recurring_rule_type, + self.recurring_interval, + max_date_end=date_end, + ), + } + + def stop(self, date_end, manual_renew_needed=False, post_message=True): + """ + Put date_end on contract line + We don't consider contract lines that end's before the new end date + :param date_end: new date end for contract line + :return: True + """ + if not all(self.mapped("is_stop_allowed")): + raise ValidationError(_("Stop not allowed for this line")) + for rec in self: + if date_end < rec.date_start: + rec.cancel() + else: + if not rec.date_end or rec.date_end > date_end: + old_date_end = rec.date_end + rec.write( + rec._prepare_value_for_stop(date_end, manual_renew_needed) + ) + if post_message: + msg = Markup( + _( + """Contract line for %(product)s + stopped:
+ - End: %(old_end)s -- %(new_end)s + """ + ) + ) % { + "product": rec.name, + "old_end": old_date_end, + "new_end": rec.date_end, + } + rec.contract_id.message_post(body=msg) + else: + rec.write( + { + "is_auto_renew": False, + "manual_renew_needed": manual_renew_needed, + } + ) + return True + + def _prepare_value_for_plan_successor( + self, date_start, date_end, is_auto_renew, recurring_next_date=False + ): + self.ensure_one() + if not recurring_next_date: + recurring_next_date = self.get_next_invoice_date( + date_start, + self.recurring_invoicing_type, + self.recurring_invoicing_offset, + self.recurring_rule_type, + self.recurring_interval, + max_date_end=date_end, + ) + new_vals = self.read()[0] + new_vals.pop("id", None) + new_vals.pop("last_date_invoiced", None) + values = self._convert_to_write(new_vals) + values["date_start"] = date_start + values["date_end"] = date_end + values["recurring_next_date"] = recurring_next_date + values["is_auto_renew"] = is_auto_renew + values["predecessor_contract_line_id"] = self.id + return values + + def plan_successor( + self, + date_start, + date_end, + is_auto_renew, + recurring_next_date=False, + post_message=True, + ): + """ + Create a copy of a contract line in a new interval + :param date_start: date_start for the successor_contract_line + :param date_end: date_end for the successor_contract_line + :param is_auto_renew: is_auto_renew option for successor_contract_line + :param recurring_next_date: recurring_next_date for the + successor_contract_line + :return: successor_contract_line + """ + contract_line = self.env["contract.line"] + for rec in self: + if not rec.is_plan_successor_allowed: + raise ValidationError(_("Plan successor not allowed for this line")) + rec.is_auto_renew = False + new_line = self.create( + rec._prepare_value_for_plan_successor( + date_start, date_end, is_auto_renew, recurring_next_date + ) + ) + rec.successor_contract_line_id = new_line + contract_line |= new_line + if post_message: + msg = Markup( + _( + """Contract line for %(product)s + planned a successor:
+ - Start: %(new_date_start)s +
+ - End: %(new_date_end)s + """ + ) + ) % { + "product": rec.name, + "new_date_start": new_line.date_start, + "new_date_end": new_line.date_end, + } + rec.contract_id.message_post(body=msg) + return contract_line + + def stop_plan_successor(self, date_start, date_end, is_auto_renew): + """ + Stop a contract line for a defined period and start it later + Cases to consider: + * contract line end's before the suspension period: + -> apply stop + * contract line start before the suspension period and end in it + -> apply stop at suspension start date + -> apply plan successor: + - date_start: suspension.date_end + - date_end: date_end + (contract_line.date_end + - suspension.date_start) + * contract line start before the suspension period and end after it + -> apply stop at suspension start date + -> apply plan successor: + - date_start: suspension.date_end + - date_end: date_end + (suspension.date_end + - suspension.date_start) + * contract line start and end's in the suspension period + -> apply delay + - delay: suspension.date_end - contract_line.date_start + * contract line start in the suspension period and end after it + -> apply delay + - delay: suspension.date_end - contract_line.date_start + * contract line start and end after the suspension period + -> apply delay + - delay: suspension.date_end - suspension.start_date + :param date_start: suspension start date + :param date_end: suspension end date + :param is_auto_renew: is the new line is set to auto_renew + :return: created contract line + """ + if not all(self.mapped("is_stop_plan_successor_allowed")): + raise ValidationError(_("Stop/Plan successor not allowed for this line")) + contract_line = self.env["contract.line"] + for rec in self: + if rec.date_start >= date_start: + if rec.date_start < date_end: + delay = (date_end - rec.date_start) + timedelta(days=1) + else: + delay = (date_end - date_start) + timedelta(days=1) + rec._delay(delay) + contract_line |= rec + else: + if rec.date_end and rec.date_end < date_start: + rec.stop(date_start, post_message=False) + elif ( + rec.date_end + and rec.date_end > date_start + and rec.date_end < date_end + ): + new_date_start = date_end + relativedelta(days=1) + new_date_end = ( + date_end + (rec.date_end - date_start) + relativedelta(days=1) + ) + rec.stop( + date_start - relativedelta(days=1), + manual_renew_needed=True, + post_message=False, + ) + contract_line |= rec.plan_successor( + new_date_start, + new_date_end, + is_auto_renew, + post_message=False, + ) + else: + new_date_start = date_end + relativedelta(days=1) + if rec.date_end: + new_date_end = ( + rec.date_end + + (date_end - date_start) + + relativedelta(days=1) + ) + else: + new_date_end = rec.date_end + + rec.stop( + date_start - relativedelta(days=1), + manual_renew_needed=True, + post_message=False, + ) + contract_line |= rec.plan_successor( + new_date_start, + new_date_end, + is_auto_renew, + post_message=False, + ) + msg = Markup( + _( + """Contract line for %(product)s + suspended:
+ - Suspension Start: %(new_date_start)s +
+ - Suspension End: %(new_date_end)s + """ + ) + ) % { + "product": rec.name, + "new_date_start": date_start, + "new_date_end": date_end, + } + rec.contract_id.message_post(body=msg) + return contract_line + + def cancel(self): + if not all(self.mapped("is_cancel_allowed")): + raise ValidationError(_("Cancel not allowed for this line")) + for contract in self.mapped("contract_id"): + lines = self.filtered(lambda line, c=contract: line.contract_id == c) + msg = Markup( + _( + "Contract line canceled: %s", + "
- ".join( + [f"{name}" for name in lines.mapped("name")] + ), + ) + ) + contract.message_post(body=msg) + self.mapped("predecessor_contract_line_id").write( + {"successor_contract_line_id": False} + ) + return self.write({"is_canceled": True, "is_auto_renew": False}) + + def uncancel(self, recurring_next_date): + if not all(self.mapped("is_un_cancel_allowed")): + raise ValidationError(_("Un-cancel not allowed for this line")) + for contract in self.mapped("contract_id"): + lines = self.filtered(lambda line, c=contract: line.contract_id == c) + msg = Markup( + _( + "Contract line Un-canceled: %s", + "
- ".join( + [f"{name}" for name in lines.mapped("name")] + ), + ) + ) + contract.message_post(body=msg) + for rec in self: + if rec.predecessor_contract_line_id: + predecessor_contract_line = rec.predecessor_contract_line_id + assert not predecessor_contract_line.successor_contract_line_id + predecessor_contract_line.successor_contract_line_id = rec + rec.is_canceled = False + rec.recurring_next_date = recurring_next_date + return True + + def action_uncancel(self): + self.ensure_one() + context = { + "default_contract_line_id": self.id, + "default_recurring_next_date": fields.Date.context_today(self), + } + context.update(self.env.context) + view_id = self.env.ref( + "contract_line_successor.contract_line_wizard_uncancel_form_view" + ).id + return { + "type": "ir.actions.act_window", + "name": "Un-Cancel Contract Line", + "res_model": "contract.line.wizard", + "view_mode": "form", + "views": [(view_id, "form")], + "target": "new", + "context": context, + } + + def action_plan_successor(self): + self.ensure_one() + context = { + "default_contract_line_id": self.id, + "default_is_auto_renew": self.is_auto_renew, + } + context.update(self.env.context) + view_id = self.env.ref( + "contract_line_successor.contract_line_wizard_plan_successor_form_view" + ).id + return { + "type": "ir.actions.act_window", + "name": "Plan contract line successor", + "res_model": "contract.line.wizard", + "view_mode": "form", + "views": [(view_id, "form")], + "target": "new", + "context": context, + } + + def action_stop(self): + self.ensure_one() + context = { + "default_contract_line_id": self.id, + "default_date_end": self.date_end, + } + context.update(self.env.context) + view_id = self.env.ref( + "contract_line_successor.contract_line_wizard_stop_form_view" + ).id + return { + "type": "ir.actions.act_window", + "name": "Terminate contract line", + "res_model": "contract.line.wizard", + "view_mode": "form", + "views": [(view_id, "form")], + "target": "new", + "context": context, + } + + def action_stop_plan_successor(self): + self.ensure_one() + context = { + "default_contract_line_id": self.id, + "default_is_auto_renew": self.is_auto_renew, + } + context.update(self.env.context) + view_id = self.env.ref( + "contract_line_successor.contract_line_wizard_stop_plan_successor_form_view" + ).id + return { + "type": "ir.actions.act_window", + "name": "Suspend contract line", + "res_model": "contract.line.wizard", + "view_mode": "form", + "views": [(view_id, "form")], + "target": "new", + "context": context, + } + + def _get_renewal_new_date_end(self): + self.ensure_one() + date_start = self.date_end + relativedelta(days=1) + date_end = self._get_first_date_end( + date_start, self.auto_renew_rule_type, self.auto_renew_interval + ) + return date_end + + def _renew_create_line(self, date_end): + self.ensure_one() + date_start = self.date_end + relativedelta(days=1) + is_auto_renew = self.is_auto_renew + self.stop(self.date_end, post_message=False) + new_line = self.plan_successor( + date_start, date_end, is_auto_renew, post_message=False + ) + return new_line + + def _renew_extend_line(self, date_end): + self.ensure_one() + self.date_end = date_end + return self + + def renew(self): + res = self.env["contract.line"] + for rec in self: + company = rec.contract_id.company_id + date_end = rec._get_renewal_new_date_end() + date_start = rec.date_end + relativedelta(days=1) + if company.create_new_line_at_contract_line_renew: + new_line = rec._renew_create_line(date_end) + else: + new_line = rec._renew_extend_line(date_end) + res |= new_line + msg = Markup( + _( + """Contract line for %(product)s + renewed:
+ - Start: %(new_date_start)s +
+ - End: %(new_date_end)s + """ + ) + ) % { + "product": rec.name, + "new_date_start": date_start, + "new_date_end": date_end, + } + rec.contract_id.message_post(body=msg) + return res + + @api.model + def _contract_line_to_renew_domain(self): + return [ + ("is_auto_renew", "=", True), + ("is_canceled", "=", False), + ("termination_notice_date", "<=", fields.Date.context_today(self)), + ] + + @api.model + def cron_renew_contract_line(self): + domain = self._contract_line_to_renew_domain() + to_renew = self.search(domain) + to_renew.renew() + + def unlink(self): + """stop unlink uncnacled lines""" + for record in self: + if not (record.is_canceled or record.display_type): + raise ValidationError(_("Contract line must be canceled before delete")) + return super().unlink() + + @api.constrains("is_auto_renew", "auto_renew_interval") + def _check_auto_renew_interval(self): + for rec in self: + if rec.is_auto_renew and not rec.auto_renew_interval: + raise ValidationError( + _("Auto renew interval should be different then 0") + ) diff --git a/contract_line_successor/models/contract_line_constraints.py b/contract_line_successor/models/contract_line_constraints.py new file mode 100644 index 0000000000..eeaba307bb --- /dev/null +++ b/contract_line_successor/models/contract_line_constraints.py @@ -0,0 +1,429 @@ +# Copyright 2018 ACSONE SA/NV. +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +import itertools +from collections import namedtuple + +from odoo.fields import Date + +Criteria = namedtuple( + "Criteria", + [ + "when", # Contract line relatively to today (BEFORE, IN, AFTER) + "has_date_end", # Is date_end set on contract line (bool) + "has_last_date_invoiced", # Is last_date_invoiced set on contract line + "is_auto_renew", # Is is_auto_renew set on contract line (bool) + "has_successor", # Is contract line has_successor (bool) + "predecessor_has_successor", + # Is contract line predecessor has successor (bool) + # In almost of the cases + # contract_line.predecessor.successor == contract_line + # But at cancel action, + # contract_line.predecessor.successor == False + # This is to permit plan_successor on predecessor + # If contract_line.predecessor.successor != False + # and contract_line is canceled, we don't allow uncancel + # else we re-link contract_line and its predecessor + "canceled", # Is contract line canceled (bool) + ], +) +Allowed = namedtuple( + "Allowed", + ["plan_successor", "stop_plan_successor", "stop", "cancel", "uncancel"], +) + + +def _expand_none(criteria): + variations = [] + for attribute, value in criteria._asdict().items(): + if value is None: + if attribute == "when": + variations.append(["BEFORE", "IN", "AFTER"]) + else: + variations.append([True, False]) + else: + variations.append([value]) + return itertools.product(*variations) + + +def _add(matrix, criteria, allowed): + """Expand None values to True/False combination""" + for c in _expand_none(criteria): + matrix[c] = allowed + + +CRITERIA_ALLOWED_DICT = { + Criteria( + when="BEFORE", + has_date_end=True, + has_last_date_invoiced=False, + is_auto_renew=True, + has_successor=False, + predecessor_has_successor=None, + canceled=False, + ): Allowed( + plan_successor=False, + stop_plan_successor=True, + stop=True, + cancel=True, + uncancel=False, + ), + Criteria( + when="BEFORE", + has_date_end=True, + has_last_date_invoiced=False, + is_auto_renew=False, + has_successor=True, + predecessor_has_successor=None, + canceled=False, + ): Allowed( + plan_successor=False, + stop_plan_successor=False, + stop=True, + cancel=True, + uncancel=False, + ), + Criteria( + when="BEFORE", + has_date_end=True, + has_last_date_invoiced=False, + is_auto_renew=False, + has_successor=False, + predecessor_has_successor=None, + canceled=False, + ): Allowed( + plan_successor=True, + stop_plan_successor=True, + stop=True, + cancel=True, + uncancel=False, + ), + Criteria( + when="BEFORE", + has_date_end=False, + has_last_date_invoiced=False, + is_auto_renew=False, + has_successor=False, + predecessor_has_successor=None, + canceled=False, + ): Allowed( + plan_successor=False, + stop_plan_successor=True, + stop=True, + cancel=True, + uncancel=False, + ), + Criteria( + when="IN", + has_date_end=True, + has_last_date_invoiced=False, + is_auto_renew=True, + has_successor=False, + predecessor_has_successor=None, + canceled=False, + ): Allowed( + plan_successor=False, + stop_plan_successor=True, + stop=True, + cancel=True, + uncancel=False, + ), + Criteria( + when="IN", + has_date_end=True, + has_last_date_invoiced=False, + is_auto_renew=False, + has_successor=True, + predecessor_has_successor=None, + canceled=False, + ): Allowed( + plan_successor=False, + stop_plan_successor=False, + stop=True, + cancel=True, + uncancel=False, + ), + Criteria( + when="IN", + has_date_end=True, + has_last_date_invoiced=False, + is_auto_renew=False, + has_successor=False, + predecessor_has_successor=None, + canceled=False, + ): Allowed( + plan_successor=True, + stop_plan_successor=True, + stop=True, + cancel=True, + uncancel=False, + ), + Criteria( + when="IN", + has_date_end=False, + has_last_date_invoiced=False, + is_auto_renew=False, + has_successor=False, + predecessor_has_successor=None, + canceled=False, + ): Allowed( + plan_successor=False, + stop_plan_successor=True, + stop=True, + cancel=True, + uncancel=False, + ), + Criteria( + when="BEFORE", + has_date_end=True, + has_last_date_invoiced=True, + is_auto_renew=True, + has_successor=False, + predecessor_has_successor=None, + canceled=False, + ): Allowed( + plan_successor=False, + stop_plan_successor=True, + stop=True, + cancel=False, + uncancel=False, + ), + Criteria( + when="BEFORE", + has_date_end=True, + has_last_date_invoiced=True, + is_auto_renew=False, + has_successor=True, + predecessor_has_successor=None, + canceled=False, + ): Allowed( + plan_successor=False, + stop_plan_successor=False, + stop=True, + cancel=False, + uncancel=False, + ), + Criteria( + when="BEFORE", + has_date_end=True, + has_last_date_invoiced=True, + is_auto_renew=False, + has_successor=False, + predecessor_has_successor=None, + canceled=False, + ): Allowed( + plan_successor=True, + stop_plan_successor=True, + stop=True, + cancel=False, + uncancel=False, + ), + Criteria( + when="BEFORE", + has_date_end=False, + has_last_date_invoiced=True, + is_auto_renew=False, + has_successor=False, + predecessor_has_successor=None, + canceled=False, + ): Allowed( + plan_successor=False, + stop_plan_successor=True, + stop=True, + cancel=False, + uncancel=False, + ), + Criteria( + when="IN", + has_date_end=True, + has_last_date_invoiced=True, + is_auto_renew=True, + has_successor=False, + predecessor_has_successor=None, + canceled=False, + ): Allowed( + plan_successor=False, + stop_plan_successor=True, + stop=True, + cancel=False, + uncancel=False, + ), + Criteria( + when="IN", + has_date_end=True, + has_last_date_invoiced=True, + is_auto_renew=False, + has_successor=True, + predecessor_has_successor=None, + canceled=False, + ): Allowed( + plan_successor=False, + stop_plan_successor=False, + stop=True, + cancel=False, + uncancel=False, + ), + Criteria( + when="IN", + has_date_end=True, + has_last_date_invoiced=True, + is_auto_renew=False, + has_successor=False, + predecessor_has_successor=None, + canceled=False, + ): Allowed( + plan_successor=True, + stop_plan_successor=True, + stop=True, + cancel=False, + uncancel=False, + ), + Criteria( + when="IN", + has_date_end=False, + has_last_date_invoiced=True, + is_auto_renew=False, + has_successor=False, + predecessor_has_successor=None, + canceled=False, + ): Allowed( + plan_successor=False, + stop_plan_successor=True, + stop=True, + cancel=False, + uncancel=False, + ), + Criteria( + when="AFTER", + has_date_end=True, + has_last_date_invoiced=None, + is_auto_renew=True, + has_successor=False, + predecessor_has_successor=None, + canceled=False, + ): Allowed( + plan_successor=False, + stop_plan_successor=False, + stop=True, + cancel=False, + uncancel=False, + ), + Criteria( + when="AFTER", + has_date_end=True, + has_last_date_invoiced=None, + is_auto_renew=False, + has_successor=True, + predecessor_has_successor=None, + canceled=False, + ): Allowed( + plan_successor=False, + stop_plan_successor=False, + stop=False, + cancel=False, + uncancel=False, + ), + Criteria( + when="AFTER", + has_date_end=True, + has_last_date_invoiced=None, + is_auto_renew=False, + has_successor=False, + predecessor_has_successor=None, + canceled=False, + ): Allowed( + plan_successor=True, + stop_plan_successor=False, + stop=True, + cancel=False, + uncancel=False, + ), + Criteria( + when=None, + has_date_end=None, + has_last_date_invoiced=None, + is_auto_renew=None, + has_successor=None, + predecessor_has_successor=False, + canceled=True, + ): Allowed( + plan_successor=False, + stop_plan_successor=False, + stop=False, + cancel=False, + uncancel=True, + ), + Criteria( + when=None, + has_date_end=None, + has_last_date_invoiced=None, + is_auto_renew=None, + has_successor=None, + predecessor_has_successor=True, + canceled=True, + ): Allowed( + plan_successor=False, + stop_plan_successor=False, + stop=False, + cancel=False, + uncancel=False, + ), +} +criteria_allowed_dict = {} + +for c in CRITERIA_ALLOWED_DICT: + _add(criteria_allowed_dict, c, CRITERIA_ALLOWED_DICT[c]) + + +def compute_when(date_start, date_end): + today = Date.today() + if today < date_start: + return "BEFORE" + if date_end and today > date_end: + return "AFTER" + return "IN" + + +def compute_criteria( + date_start, + date_end, + has_last_date_invoiced, + is_auto_renew, + successor_contract_line_id, + predecessor_contract_line_id, + is_canceled, +): + return Criteria( + when=compute_when(date_start, date_end), + has_date_end=bool(date_end), + has_last_date_invoiced=bool(has_last_date_invoiced), + is_auto_renew=is_auto_renew, + has_successor=bool(successor_contract_line_id), + predecessor_has_successor=bool( + predecessor_contract_line_id.successor_contract_line_id + ), + canceled=is_canceled, + ) + + +def get_allowed( + date_start, + date_end, + has_last_date_invoiced, + is_auto_renew, + successor_contract_line_id, + predecessor_contract_line_id, + is_canceled, +): + criteria = compute_criteria( + date_start, + date_end, + has_last_date_invoiced, + is_auto_renew, + successor_contract_line_id, + predecessor_contract_line_id, + is_canceled, + ) + if criteria in criteria_allowed_dict: + return criteria_allowed_dict[criteria] + return False diff --git a/contract_line_successor/models/contract_template_line.py b/contract_line_successor/models/contract_template_line.py new file mode 100644 index 0000000000..3d15f06023 --- /dev/null +++ b/contract_line_successor/models/contract_template_line.py @@ -0,0 +1,35 @@ +# Copyright 2025 ACSONE SA/NV +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo import fields, models + + +class ContractTemplateLine(models.Model): + _inherit = "contract.template.line" + + is_auto_renew = fields.Boolean(string="Auto Renew", default=False) + auto_renew_interval = fields.Integer( + string="Renew Every", + default=1, + help="Renew every (Days/Weeks/Months/Years)", + ) + auto_renew_rule_type = fields.Selection( + [ + ("daily", "Day(s)"), + ("weekly", "Week(s)"), + ("monthly", "Month(s)"), + ("yearly", "Year(s)"), + ], + default="yearly", + string="Renewal type", + help="Specify interval for automatic renewal.", + ) + termination_notice_interval = fields.Integer( + default=1, + string="Termination Notice Before", + ) + termination_notice_rule_type = fields.Selection( + [("daily", "Day(s)"), ("weekly", "Week(s)"), ("monthly", "Month(s)")], + default="monthly", + string="Termination Notice type", + ) diff --git a/contract_line_successor/models/res_company.py b/contract_line_successor/models/res_company.py new file mode 100644 index 0000000000..36079fc811 --- /dev/null +++ b/contract_line_successor/models/res_company.py @@ -0,0 +1,15 @@ +# Copyright 2019 ACSONE SA/NV +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from odoo import fields, models + + +class ResCompany(models.Model): + _inherit = "res.company" + + create_new_line_at_contract_line_renew = fields.Boolean( + help="If checked, a new line will be generated at contract line renew " + "and linked to the original one as successor. The default " + "behavior is to extend the end date of the contract by a new " + "subscription period", + ) diff --git a/contract_line_successor/models/res_config_settings.py b/contract_line_successor/models/res_config_settings.py new file mode 100644 index 0000000000..e5f628a2a6 --- /dev/null +++ b/contract_line_successor/models/res_config_settings.py @@ -0,0 +1,18 @@ +# Copyright 2019 ACSONE SA/NV +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from odoo import fields, models + + +class ResConfigSettings(models.TransientModel): + _inherit = "res.config.settings" + + create_new_line_at_contract_line_renew = fields.Boolean( + related="company_id.create_new_line_at_contract_line_renew", + readonly=False, + string="Create New Line At Contract Line Renew", + help="If checked, a new line will be generated at contract line renew " + "and linked to the original one as successor. The default " + "behavior is to extend the end date of the contract by a new " + "subscription period", + ) diff --git a/contract_line_successor/pyproject.toml b/contract_line_successor/pyproject.toml new file mode 100644 index 0000000000..4231d0cccb --- /dev/null +++ b/contract_line_successor/pyproject.toml @@ -0,0 +1,3 @@ +[build-system] +requires = ["whool"] +build-backend = "whool.buildapi" diff --git a/contract_line_successor/readme/CONFIGURE.md b/contract_line_successor/readme/CONFIGURE.md new file mode 100644 index 0000000000..a1a9bab2ff --- /dev/null +++ b/contract_line_successor/readme/CONFIGURE.md @@ -0,0 +1,10 @@ +- **Auto-Renewal Strategy** + In the company settings, define whether renewing a contract line: + - Extends the current line (updates `date_end`), + - or creates a new successor contract line. + + Field: + `Company > Configuration > Contracts > Create new contract line at renewal` +- **Scheduled Actions** + Ensure the scheduled action `Contract Line: Auto Renew` is activated if you + want automatic renewal without manual intervention. diff --git a/contract_line_successor/readme/CONTRIBUTORS.md b/contract_line_successor/readme/CONTRIBUTORS.md new file mode 100644 index 0000000000..ac775f4d05 --- /dev/null +++ b/contract_line_successor/readme/CONTRIBUTORS.md @@ -0,0 +1 @@ +- Souheil Bejaoui (ACSONE SA/NV) diff --git a/contract_line_successor/readme/DESCRIPTION.md b/contract_line_successor/readme/DESCRIPTION.md new file mode 100644 index 0000000000..f6a68a92b9 --- /dev/null +++ b/contract_line_successor/readme/DESCRIPTION.md @@ -0,0 +1,40 @@ +**Contract Line Successor** extends `contract.line` model to support advanced +contract lifecycle management, including suspension, successor planning, +cancellation, and renewal. +It provides a flexible and robust framework for managing complex contract +line scenarios in a clean and structured way. + +## Features + +- **Successor and Predecessor Management** + - Link contract lines with successor and predecessor lines. + - Plan successors automatically or manually after a stop or suspension. + +- **Contract Line Lifecycle States** + - Manage contract lines with the following computed states: + - `Upcoming` + - `In-Progress` + - `To Renew` + - `Upcoming Close` + - `Closed` + - `Canceled` + +- **Lifecycle Operations** + - Stop a contract line. + - Plan a successor for a contract line. + - Stop and plan a successor in one operation (useful for suspensions). + - Cancel and un-cancel contract lines. + - Renew contract lines automatically (new line or extension). + +- **Auto-Renewal Handling** + - Auto-renewal based on company settings (extend existing line or create a new one). + - Cron job to automate renewal of eligible contract lines. + +- **Data Integrity and Validation** + - Prevent invalid successor or predecessor configurations. + - Validate state transitions and date overlaps. + - Ensure clean renewal and cancellation workflows. + +- **Audit Trail** + - Automatic posting of chatter messages for lifecycle events like stops, + renewals, suspensions, cancellations, etc. diff --git a/contract_line_successor/readme/USAGE.md b/contract_line_successor/readme/USAGE.md new file mode 100644 index 0000000000..be99092bd6 --- /dev/null +++ b/contract_line_successor/readme/USAGE.md @@ -0,0 +1,6 @@ +1. **Select a contract** and enable the option **"Recurrence at line level?"**. +2. Once enabled, you will have access to several actions at the contract line level: + - **Stop** a contract line and optionally **plan a successor**. + - **Handle temporary suspensions** and **resume** the contract line after the suspension period. + - **Cancel** and **un-cancel** contract lines if necessary. + - **Renew** contract lines either by **extending** the current line or by **creating a new successor line** automatically. diff --git a/contract_line_successor/security/ir.model.access.csv b/contract_line_successor/security/ir.model.access.csv new file mode 100644 index 0000000000..aae8a4dc7c --- /dev/null +++ b/contract_line_successor/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" +"contract_line_wizard","contract_line_wizard","model_contract_line_wizard","account.group_account_manager",1,1,1,1 diff --git a/contract_line_successor/static/description/icon.png b/contract_line_successor/static/description/icon.png new file mode 100644 index 0000000000000000000000000000000000000000..3a0328b516c4980e8e44cdb63fd945757ddd132d GIT binary patch literal 9455 zcmW++2RxMjAAjx~&dlBk9S+%}OXg)AGE&Cb*&}d0jUxM@u(PQx^-s)697TX`ehR4?GS^qbkof1cslKgkU)h65qZ9Oc=ml_0temigYLJfnz{IDzUf>bGs4N!v3=Z3jMq&A#7%rM5eQ#dc?k~! zVpnB`o+K7|Al`Q_U;eD$B zfJtP*jH`siUq~{KE)`jP2|#TUEFGRryE2`i0**z#*^6~AI|YzIWy$Cu#CSLW3q=GA z6`?GZymC;dCPk~rBS%eCb`5OLr;RUZ;D`}um=H)BfVIq%7VhiMr)_#G0N#zrNH|__ zc+blN2UAB0=617@>_u;MPHN;P;N#YoE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aw0qtTRbx} z({Pg?#{2`sc9)M5N$*N|4;^t$+QP?#mov zGVC@I*lBVrOU-%2y!7%)fAKjpEFsgQc4{amtiHb95KQEwvf<(3T<9-Zm$xIew#P22 zc2Ix|App^>v6(3L_MCU0d3W##AB0M~3D00EWoKZqsJYT(#@w$Y_H7G22M~ApVFTRHMI_3be)Lkn#0F*V8Pq zc}`Cjy$bE;FJ6H7p=0y#R>`}-m4(0F>%@P|?7fx{=R^uFdISRnZ2W_xQhD{YuR3t< z{6yxu=4~JkeA;|(J6_nv#>Nvs&FuLA&PW^he@t(UwFFE8)|a!R{`E`K`i^ZnyE4$k z;(749Ix|oi$c3QbEJ3b~D_kQsPz~fIUKym($a_7dJ?o+40*OLl^{=&oq$<#Q(yyrp z{J-FAniyAw9tPbe&IhQ|a`DqFTVQGQ&Gq3!C2==4x{6EJwiPZ8zub-iXoUtkJiG{} zPaR&}_fn8_z~(=;5lD-aPWD3z8PZS@AaUiomF!G8I}Mf>e~0g#BelA-5#`cj;O5>N Xviia!U7SGha1wx#SCgwmn*{w2TRX*I literal 0 HcmV?d00001 diff --git a/contract_line_successor/static/description/index.html b/contract_line_successor/static/description/index.html new file mode 100644 index 0000000000..b4131545fc --- /dev/null +++ b/contract_line_successor/static/description/index.html @@ -0,0 +1,519 @@ + + + + + +Contract Line Successor + + + +
+

Contract Line Successor

+ + +

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

+
+
Contract Line Successor extends contract.line model to support +advanced contract lifecycle management, including suspension, +successor planning, cancellation, and renewal.
+
It provides a flexible and robust framework for managing complex +contract line scenarios in a clean and structured way.
+
+
+

Features

+
    +
  • Successor and Predecessor Management
      +
    • Link contract lines with successor and predecessor lines.
    • +
    • Plan successors automatically or manually after a stop or +suspension.
    • +
    +
  • +
  • Contract Line Lifecycle States
      +
    • Manage contract lines with the following computed states:
        +
      • Upcoming
      • +
      • In-Progress
      • +
      • To Renew
      • +
      • Upcoming Close
      • +
      • Closed
      • +
      • Canceled
      • +
      +
    • +
    +
  • +
  • Lifecycle Operations
      +
    • Stop a contract line.
    • +
    • Plan a successor for a contract line.
    • +
    • Stop and plan a successor in one operation (useful for +suspensions).
    • +
    • Cancel and un-cancel contract lines.
    • +
    • Renew contract lines automatically (new line or extension).
    • +
    +
  • +
  • Auto-Renewal Handling
      +
    • Auto-renewal based on company settings (extend existing line or +create a new one).
    • +
    • Cron job to automate renewal of eligible contract lines.
    • +
    +
  • +
  • Data Integrity and Validation
      +
    • Prevent invalid successor or predecessor configurations.
    • +
    • Validate state transitions and date overlaps.
    • +
    • Ensure clean renewal and cancellation workflows.
    • +
    +
  • +
  • Audit Trail
      +
    • Automatic posting of chatter messages for lifecycle events like +stops, renewals, suspensions, cancellations, etc.
    • +
    +
  • +
+

Table of contents

+ +
+

Configuration

+
    +
  • +
    Auto-Renewal Strategy
    +
    In the company settings, define whether renewing a contract line:
    +
    +
      +
    • Extends the current line (updates date_end),
    • +
    • or creates a new successor contract line.
    • +
    +
    +
    Field:
    +
    Company > Configuration > Contracts > Create new contract line at renewal
    +
    +
  • +
  • +
    Scheduled Actions
    +
    Ensure the scheduled action Contract Line: Auto Renew is +activated if you want automatic renewal without manual +intervention.
    +
    +
  • +
+
+
+

Usage

+
    +
  1. Select a contract and enable the option “Recurrence at line +level?”.
  2. +
  3. Once enabled, you will have access to several actions at the contract +line level:
      +
    • Stop a contract line and optionally plan a successor.
    • +
    • Handle temporary suspensions and resume the contract line +after the suspension period.
    • +
    • Cancel and un-cancel contract lines if necessary.
    • +
    • Renew contract lines either by extending the current line +or by creating a new successor line automatically.
    • +
    +
  4. +
+
+
+

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.

+
+ +
+
+

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 maintainer:

+

sbejaoui

+

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_line_successor/tests/__init__.py b/contract_line_successor/tests/__init__.py new file mode 100644 index 0000000000..3c02082d69 --- /dev/null +++ b/contract_line_successor/tests/__init__.py @@ -0,0 +1 @@ +from . import test_contract diff --git a/contract_line_successor/tests/test_contract.py b/contract_line_successor/tests/test_contract.py new file mode 100644 index 0000000000..c0bda42acb --- /dev/null +++ b/contract_line_successor/tests/test_contract.py @@ -0,0 +1,847 @@ +# Copyright 2018 ACSONE SA/NV +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from datetime import timedelta + +from dateutil.relativedelta import relativedelta + +from odoo.exceptions import ValidationError + +from odoo.addons.contract.tests.test_contract import ( + TestContract, + to_date, +) + + +class TestContractSuccessor(TestContract): + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.contract.company_id.create_new_line_at_contract_line_renew = True + cls.line_vals.update({"is_auto_renew": False}) + + def test_date_end(self): + """recurring next date for a contract is the min for all lines""" + self.acct_line.date_end = "2018-01-01" + self.acct_line.copy() + self.acct_line.write({"date_end": False, "is_auto_renew": False}) + self.assertFalse(self.contract.date_end) + + def test_cancel_contract_line(self): + """It should raise a validation error""" + self.acct_line.cancel() + with self.assertRaises(ValidationError): + self.acct_line.stop(self.today) + + def test_stop_contract_line(self): + """It should put end to the contract line""" + self.acct_line.write( + { + "date_start": self.today, + "recurring_next_date": self.today, + "date_end": self.today + relativedelta(months=7), + "is_auto_renew": True, + } + ) + self.acct_line.stop(self.today + relativedelta(months=5)) + self.assertEqual(self.acct_line.date_end, self.today + relativedelta(months=5)) + + def test_stop_upcoming_contract_line(self): + """It should put end to the contract line""" + self.acct_line.write( + { + "date_start": self.today + relativedelta(months=3), + "recurring_next_date": self.today + relativedelta(months=3), + "date_end": self.today + relativedelta(months=7), + "is_auto_renew": True, + } + ) + self.acct_line.stop(self.today) + self.assertEqual(self.acct_line.date_end, self.today + relativedelta(months=7)) + self.assertTrue(self.acct_line.is_canceled) + + def test_stop_past_contract_line(self): + """Past contract line are ignored on stop""" + self.acct_line.write( + {"date_end": self.today + relativedelta(months=5), "is_auto_renew": True} + ) + self.acct_line.stop(self.today + relativedelta(months=7)) + self.assertEqual(self.acct_line.date_end, self.today + relativedelta(months=5)) + + def test_stop_contract_line_without_date_end(self): + """Past contract line are ignored on stop""" + self.acct_line.write({"date_end": False, "is_auto_renew": False}) + self.acct_line.stop(self.today + relativedelta(months=7)) + self.assertEqual(self.acct_line.date_end, self.today + relativedelta(months=7)) + + def test_stop_wizard(self): + self.acct_line.write( + { + "date_start": self.today, + "recurring_next_date": self.today, + "date_end": self.today + relativedelta(months=5), + "is_auto_renew": True, + } + ) + wizard = self.env["contract.line.wizard"].create( + { + "date_end": self.today + relativedelta(months=3), + "contract_line_id": self.acct_line.id, + } + ) + wizard.stop() + self.assertEqual(self.acct_line.date_end, self.today + relativedelta(months=3)) + self.assertFalse(self.acct_line.is_auto_renew) + + def test_stop_plan_successor_contract_line_0(self): + successor_contract_line = self.acct_line.copy( + { + "date_start": self.today + relativedelta(months=5), + "recurring_next_date": self.today + relativedelta(months=5), + } + ) + self.acct_line.write( + { + "successor_contract_line_id": successor_contract_line.id, + "is_auto_renew": False, + "date_end": self.today, + } + ) + suspension_start = self.today + relativedelta(months=5) + suspension_end = self.today + relativedelta(months=6) + with self.assertRaises(ValidationError): + self.acct_line.stop_plan_successor(suspension_start, suspension_end, True) + + def test_stop_plan_successor_contract_line_1(self): + """ + * contract line end's before the suspension period: + -> apply stop + """ + suspension_start = self.today + relativedelta(months=5) + suspension_end = self.today + relativedelta(months=6) + start_date = self.today + end_date = self.today + relativedelta(months=4) + self.acct_line.write( + { + "date_start": start_date, + "recurring_next_date": start_date, + "date_end": end_date, + } + ) + self.acct_line.stop_plan_successor(suspension_start, suspension_end, True) + self.assertEqual(self.acct_line.date_end, end_date) + new_line = self.env["contract.line"].search( + [("predecessor_contract_line_id", "=", self.acct_line.id)] + ) + self.assertFalse(new_line) + + def test_stop_plan_successor_contract_line_2(self): + """ + * contract line start before the suspension period and end in it + -> apply stop at suspension start date + -> apply plan successor: + - date_start: suspension.date_end + - date_end: suspension.date_end + (contract_line.date_end + - suspension.date_start) + """ + suspension_start = self.today + relativedelta(months=3) + suspension_end = self.today + relativedelta(months=5) + start_date = self.today + end_date = self.today + relativedelta(months=4) + self.acct_line.write( + { + "date_start": start_date, + "recurring_next_date": start_date, + "date_end": end_date, + } + ) + self.acct_line.stop_plan_successor(suspension_start, suspension_end, True) + self.assertEqual( + self.acct_line.date_end, suspension_start - relativedelta(days=1) + ) + new_line = self.env["contract.line"].search( + [("predecessor_contract_line_id", "=", self.acct_line.id)] + ) + self.assertTrue(new_line) + new_date_end = ( + suspension_end + (end_date - suspension_start) + relativedelta(days=1) + ) + self.assertEqual(new_line.date_start, suspension_end + relativedelta(days=1)) + self.assertEqual(new_line.date_end, new_date_end) + self.assertTrue(self.acct_line.manual_renew_needed) + + def test_stop_plan_successor_contract_line_3(self): + """ + * contract line start before the suspension period and end after it + -> apply stop at suspension start date + -> apply plan successor: + - date_start: suspension.date_end + - date_end: suspension.date_end + (suspension.date_end + - suspension.date_start) + """ + suspension_start = self.today + relativedelta(months=3) + suspension_end = self.today + relativedelta(months=5) + start_date = self.today + end_date = self.today + relativedelta(months=6) + self.acct_line.write( + { + "date_start": start_date, + "recurring_next_date": start_date, + "date_end": end_date, + } + ) + self.acct_line.stop_plan_successor(suspension_start, suspension_end, True) + self.assertEqual( + self.acct_line.date_end, suspension_start - relativedelta(days=1) + ) + new_line = self.env["contract.line"].search( + [("predecessor_contract_line_id", "=", self.acct_line.id)] + ) + self.assertTrue(new_line) + new_date_end = ( + end_date + (suspension_end - suspension_start) + relativedelta(days=1) + ) + self.assertEqual(new_line.date_start, suspension_end + relativedelta(days=1)) + self.assertEqual(new_line.date_end, new_date_end) + self.assertTrue(self.acct_line.manual_renew_needed) + + def test_stop_plan_successor_contract_line_3_without_end_date(self): + """ + * contract line start before the suspension period and end after it + -> apply stop at suspension start date + -> apply plan successor: + - date_start: suspension.date_end + - date_end: suspension.date_end + (suspension.date_end + - suspension.date_start) + """ + suspension_start = self.today + relativedelta(months=3) + suspension_end = self.today + relativedelta(months=5) + start_date = self.today + end_date = False + self.acct_line.write( + { + "date_start": start_date, + "recurring_next_date": start_date, + "date_end": end_date, + "is_auto_renew": False, + } + ) + self.acct_line.stop_plan_successor(suspension_start, suspension_end, False) + self.assertEqual( + self.acct_line.date_end, suspension_start - relativedelta(days=1) + ) + new_line = self.env["contract.line"].search( + [("predecessor_contract_line_id", "=", self.acct_line.id)] + ) + self.assertTrue(new_line) + self.assertEqual(new_line.date_start, suspension_end + relativedelta(days=1)) + self.assertFalse(new_line.date_end) + self.assertTrue(self.acct_line.manual_renew_needed) + + def test_stop_plan_successor_contract_line_4(self): + """ + * contract line start and end's in the suspension period + -> apply delay + - delay: suspension.date_end - contract_line.end_date + """ + suspension_start = self.today + relativedelta(months=2) + suspension_end = self.today + relativedelta(months=5) + start_date = self.today + relativedelta(months=3) + end_date = self.today + relativedelta(months=4) + self.acct_line.write( + { + "date_start": start_date, + "recurring_next_date": start_date, + "date_end": end_date, + } + ) + self.acct_line.stop_plan_successor(suspension_start, suspension_end, True) + self.assertEqual( + self.acct_line.date_start, + start_date + (suspension_end - start_date) + timedelta(days=1), + ) + self.assertEqual( + self.acct_line.date_end, + end_date + (suspension_end - start_date) + timedelta(days=1), + ) + new_line = self.env["contract.line"].search( + [("predecessor_contract_line_id", "=", self.acct_line.id)] + ) + self.assertFalse(new_line) + + def test_stop_plan_successor_contract_line_5(self): + """ + * contract line start in the suspension period and end after it + -> apply delay + - delay: suspension.date_end - contract_line.date_start + """ + suspension_start = self.today + relativedelta(months=2) + suspension_end = self.today + relativedelta(months=5) + start_date = self.today + relativedelta(months=3) + end_date = self.today + relativedelta(months=6) + self.acct_line.write( + { + "date_start": start_date, + "recurring_next_date": start_date, + "date_end": end_date, + } + ) + self.acct_line.stop_plan_successor(suspension_start, suspension_end, True) + self.assertEqual( + self.acct_line.date_start, + start_date + (suspension_end - start_date) + timedelta(days=1), + ) + self.assertEqual( + self.acct_line.date_end, + end_date + (suspension_end - start_date) + timedelta(days=1), + ) + new_line = self.env["contract.line"].search( + [("predecessor_contract_line_id", "=", self.acct_line.id)] + ) + self.assertFalse(new_line) + + def test_stop_plan_successor_contract_line_5_without_date_end(self): + """ + * contract line start in the suspension period and end after it + -> apply delay + - delay: suspension.date_end - contract_line.date_start + """ + suspension_start = self.today + relativedelta(months=2) + suspension_end = self.today + relativedelta(months=5) + start_date = self.today + relativedelta(months=3) + end_date = False + self.acct_line.write( + { + "date_start": start_date, + "recurring_next_date": start_date, + "date_end": end_date, + "is_auto_renew": False, + } + ) + self.acct_line.stop_plan_successor(suspension_start, suspension_end, True) + self.assertEqual( + self.acct_line.date_start, + start_date + (suspension_end - start_date) + timedelta(days=1), + ) + self.assertFalse(self.acct_line.date_end) + new_line = self.env["contract.line"].search( + [("predecessor_contract_line_id", "=", self.acct_line.id)] + ) + self.assertFalse(new_line) + + def test_stop_plan_successor_contract_line_6(self): + """ + * contract line start and end after the suspension period + -> apply delay + - delay: suspension.date_end - suspension.start_date + """ + suspension_start = self.today + relativedelta(months=2) + suspension_end = self.today + relativedelta(months=3) + start_date = self.today + relativedelta(months=4) + end_date = self.today + relativedelta(months=6) + self.acct_line.write( + { + "date_start": start_date, + "recurring_next_date": start_date, + "date_end": end_date, + } + ) + self.acct_line.stop_plan_successor(suspension_start, suspension_end, True) + self.assertEqual( + self.acct_line.date_start, + start_date + (suspension_end - suspension_start) + timedelta(days=1), + ) + self.assertEqual( + self.acct_line.date_end, + end_date + (suspension_end - suspension_start) + timedelta(days=1), + ) + new_line = self.env["contract.line"].search( + [("predecessor_contract_line_id", "=", self.acct_line.id)] + ) + self.assertFalse(new_line) + + def test_stop_plan_successor_contract_line_6_without_date_end(self): + """ + * contract line start and end after the suspension period + -> apply delay + - delay: suspension.date_end - suspension.start_date + """ + suspension_start = self.today + relativedelta(months=2) + suspension_end = self.today + relativedelta(months=3) + start_date = self.today + relativedelta(months=4) + end_date = False + self.acct_line.write( + { + "date_start": start_date, + "recurring_next_date": start_date, + "date_end": end_date, + "is_auto_renew": False, + } + ) + self.acct_line.stop_plan_successor(suspension_start, suspension_end, True) + self.assertEqual( + self.acct_line.date_start, + start_date + (suspension_end - suspension_start) + timedelta(days=1), + ) + self.assertFalse(self.acct_line.date_end) + new_line = self.env["contract.line"].search( + [("predecessor_contract_line_id", "=", self.acct_line.id)] + ) + self.assertFalse(new_line) + + def test_stop_plan_successor_wizard(self): + suspension_start = self.today + relativedelta(months=2) + suspension_end = self.today + relativedelta(months=3) + start_date = self.today + relativedelta(months=4) + end_date = self.today + relativedelta(months=6) + self.acct_line.write( + { + "date_start": start_date, + "recurring_next_date": start_date, + "date_end": end_date, + } + ) + wizard = self.env["contract.line.wizard"].create( + { + "date_start": suspension_start, + "date_end": suspension_end, + "is_auto_renew": False, + "contract_line_id": self.acct_line.id, + } + ) + wizard.stop_plan_successor() + self.assertEqual( + self.acct_line.date_start, + start_date + (suspension_end - suspension_start) + timedelta(days=1), + ) + self.assertEqual( + self.acct_line.date_end, + end_date + (suspension_end - suspension_start) + timedelta(days=1), + ) + new_line = self.env["contract.line"].search( + [("predecessor_contract_line_id", "=", self.acct_line.id)] + ) + self.assertFalse(new_line) + + def test_plan_successor_contract_line(self): + self.acct_line.write( + { + "date_start": self.today, + "recurring_next_date": self.today, + "date_end": self.today + relativedelta(months=3), + "is_auto_renew": False, + } + ) + self.acct_line.plan_successor( + self.today + relativedelta(months=5), + self.today + relativedelta(months=7), + True, + ) + new_line = self.env["contract.line"].search( + [("predecessor_contract_line_id", "=", self.acct_line.id)] + ) + self.assertFalse(self.acct_line.is_auto_renew) + self.assertTrue(new_line.is_auto_renew) + self.assertTrue(new_line, "should create a new contract line") + self.assertEqual(new_line.date_start, self.today + relativedelta(months=5)) + self.assertEqual(new_line.date_end, self.today + relativedelta(months=7)) + + def test_overlap(self): + self.acct_line.write( + { + "date_start": self.today, + "recurring_next_date": self.today, + "date_end": self.today + relativedelta(months=3), + "is_auto_renew": False, + } + ) + self.acct_line.plan_successor( + self.today + relativedelta(months=5), + self.today + relativedelta(months=7), + True, + ) + new_line = self.env["contract.line"].search( + [("predecessor_contract_line_id", "=", self.acct_line.id)] + ) + with self.assertRaises(ValidationError): + new_line.date_start = self.today + relativedelta(months=2) + with self.assertRaises(ValidationError): + self.acct_line.date_end = self.today + relativedelta(months=6) + + def test_plan_successor_wizard(self): + self.acct_line.write( + { + "date_start": self.today, + "recurring_next_date": self.today, + "date_end": self.today + relativedelta(months=2), + "is_auto_renew": False, + } + ) + wizard = self.env["contract.line.wizard"].create( + { + "date_start": self.today + relativedelta(months=3), + "date_end": self.today + relativedelta(months=5), + "is_auto_renew": True, + "contract_line_id": self.acct_line.id, + } + ) + wizard.plan_successor() + new_line = self.env["contract.line"].search( + [("predecessor_contract_line_id", "=", self.acct_line.id)] + ) + self.assertFalse(self.acct_line.is_auto_renew) + self.assertTrue(new_line.is_auto_renew) + self.assertTrue(new_line, "should create a new contract line") + self.assertEqual(new_line.date_start, self.today + relativedelta(months=3)) + self.assertEqual(new_line.date_end, self.today + relativedelta(months=5)) + + def test_cancel(self): + self.acct_line.write( + {"date_end": self.today + relativedelta(months=5), "is_auto_renew": True} + ) + self.acct_line.cancel() + self.assertTrue(self.acct_line.is_canceled) + self.assertFalse(self.acct_line.is_auto_renew) + with self.assertRaises(ValidationError): + self.acct_line.is_auto_renew = True + self.acct_line.uncancel(self.today) + self.assertFalse(self.acct_line.is_canceled) + + def test_uncancel_wizard(self): + self.acct_line.cancel() + self.assertTrue(self.acct_line.is_canceled) + wizard = self.env["contract.line.wizard"].create( + {"recurring_next_date": self.today, "contract_line_id": self.acct_line.id} + ) + wizard.uncancel() + self.assertFalse(self.acct_line.is_canceled) + + def test_cancel_uncancel_with_predecessor(self): + suspension_start = self.today + relativedelta(months=3) + suspension_end = self.today + relativedelta(months=5) + start_date = self.today + end_date = self.today + relativedelta(months=4) + self.acct_line.write( + { + "date_start": start_date, + "recurring_next_date": start_date, + "date_end": end_date, + } + ) + self.acct_line.stop_plan_successor(suspension_start, suspension_end, True) + self.assertEqual( + self.acct_line.date_end, suspension_start - relativedelta(days=1) + ) + new_line = self.env["contract.line"].search( + [("predecessor_contract_line_id", "=", self.acct_line.id)] + ) + self.assertEqual(self.acct_line.successor_contract_line_id, new_line) + new_line.cancel() + self.assertTrue(new_line.is_canceled) + self.assertFalse(self.acct_line.successor_contract_line_id) + self.assertEqual(new_line.predecessor_contract_line_id, self.acct_line) + new_line.uncancel(suspension_end + relativedelta(days=1)) + self.assertFalse(new_line.is_canceled) + self.assertEqual(self.acct_line.successor_contract_line_id, new_line) + self.assertEqual( + new_line.recurring_next_date, + suspension_end + relativedelta(days=1), + ) + + def test_cancel_uncancel_with_predecessor_has_successor(self): + suspension_start = self.today + relativedelta(months=6) + suspension_end = self.today + relativedelta(months=7) + start_date = self.today + end_date = self.today + relativedelta(months=8) + self.acct_line.write( + { + "date_start": start_date, + "recurring_next_date": start_date, + "date_end": end_date, + } + ) + self.acct_line.stop_plan_successor(suspension_start, suspension_end, True) + new_line = self.env["contract.line"].search( + [("predecessor_contract_line_id", "=", self.acct_line.id)] + ) + new_line.cancel() + suspension_start = self.today + relativedelta(months=4) + suspension_end = self.today + relativedelta(months=5) + self.acct_line.stop_plan_successor(suspension_start, suspension_end, True) + with self.assertRaises(ValidationError): + new_line.uncancel(suspension_end) + + def test_check_has_not_date_end_has_successor(self): + self.acct_line.write({"date_end": False, "is_auto_renew": False}) + with self.assertRaises(ValidationError): + self.acct_line.plan_successor( + to_date("2016-03-01"), to_date("2016-09-01"), False + ) + + def test_check_has_not_date_end_is_auto_renew(self): + with self.assertRaises(ValidationError): + self.acct_line.write({"date_end": False, "is_auto_renew": True}) + + def test_check_has_successor_is_auto_renew(self): + with self.assertRaises(ValidationError): + self.acct_line.plan_successor( + to_date("2016-03-01"), to_date("2018-09-01"), False + ) + + def test_search_contract_line_to_renew(self): + self.acct_line.write({"date_end": self.today, "is_auto_renew": True}) + line_1 = self.acct_line.copy({"date_end": self.today + relativedelta(months=1)}) + line_2 = self.acct_line.copy({"date_end": self.today - relativedelta(months=1)}) + line_3 = self.acct_line.copy({"date_end": self.today - relativedelta(months=2)}) + line_4 = self.acct_line.copy({"date_end": self.today + relativedelta(months=2)}) + to_renew = self.acct_line.search( + self.acct_line._contract_line_to_renew_domain() + ) + self.assertEqual(set(to_renew), {self.acct_line, line_1, line_2, line_3}) + self.acct_line.cron_renew_contract_line() + self.assertTrue(self.acct_line.successor_contract_line_id) + self.assertTrue(line_1.successor_contract_line_id) + self.assertTrue(line_2.successor_contract_line_id) + self.assertTrue(line_3.successor_contract_line_id) + self.assertFalse(line_4.successor_contract_line_id) + + def test_renew_create_new_line(self): + date_start = self.today - relativedelta(months=9) + date_end = date_start + relativedelta(months=12) - relativedelta(days=1) + self.acct_line.write( + { + "is_auto_renew": True, + "date_start": date_start, + "recurring_next_date": date_start, + "date_end": self.today, + } + ) + self.acct_line._onchange_is_auto_renew() + self.assertEqual(self.acct_line.date_end, date_end) + new_line = self.acct_line.renew() + self.assertFalse(self.acct_line.is_auto_renew) + self.assertTrue(new_line.is_auto_renew) + self.assertEqual(new_line.date_start, date_start + relativedelta(months=12)) + self.assertEqual(new_line.date_end, date_end + relativedelta(months=12)) + + def test_renew_extend_original_line(self): + self.contract.company_id.create_new_line_at_contract_line_renew = False + date_start = self.today - relativedelta(months=9) + date_end = date_start + relativedelta(months=12) - relativedelta(days=1) + self.acct_line.write( + { + "is_auto_renew": True, + "date_start": date_start, + "recurring_next_date": date_start, + "date_end": self.today, + } + ) + self.acct_line._onchange_is_auto_renew() + self.assertEqual(self.acct_line.date_end, date_end) + self.acct_line.renew() + self.assertTrue(self.acct_line.is_auto_renew) + self.assertEqual(self.acct_line.date_start, date_start) + self.assertEqual(self.acct_line.date_end, date_end + relativedelta(months=12)) + + def test_unlink(self): + with self.assertRaises(ValidationError): + self.acct_line.unlink() + + def test_contract_line_state(self): + lines = self.env["contract.line"] + # upcoming + lines |= self.acct_line.copy( + { + "date_start": self.today + relativedelta(months=3), + "recurring_next_date": self.today + relativedelta(months=3), + "date_end": self.today + relativedelta(months=5), + } + ) + # in-progress + lines |= self.acct_line.copy( + { + "date_start": self.today, + "recurring_next_date": self.today, + "date_end": self.today + relativedelta(months=5), + } + ) + # in-progress + lines |= self.acct_line.copy( + { + "date_start": self.today, + "recurring_next_date": self.today, + "date_end": self.today + relativedelta(months=5), + "manual_renew_needed": True, + } + ) + # to-renew + lines |= self.acct_line.copy( + { + "date_start": self.today - relativedelta(months=5), + "recurring_next_date": self.today - relativedelta(months=5), + "date_end": self.today - relativedelta(months=2), + "manual_renew_needed": True, + } + ) + # upcoming-close + lines |= self.acct_line.copy( + { + "date_start": self.today - relativedelta(months=5), + "recurring_next_date": self.today - relativedelta(months=5), + "date_end": self.today + relativedelta(days=20), + "is_auto_renew": False, + } + ) + # closed + lines |= self.acct_line.copy( + { + "date_start": self.today - relativedelta(months=5), + "recurring_next_date": self.today - relativedelta(months=5), + "date_end": self.today - relativedelta(months=2), + "is_auto_renew": False, + } + ) + # canceled + lines |= self.acct_line.copy( + { + "date_start": self.today - relativedelta(months=5), + "recurring_next_date": self.today - relativedelta(months=5), + "date_end": self.today - relativedelta(months=2), + "is_canceled": True, + } + ) + # section + lines |= self.env["contract.line"].create( + { + "contract_id": self.contract.id, + "display_type": "line_section", + "name": "Test section", + } + ) + states = [ + "upcoming", + "in-progress", + "to-renew", + "upcoming-close", + "closed", + "canceled", + False, + ] + self.assertEqual(set(lines.mapped("state")), set(states)) + # Test search method + lines.flush_recordset() # Needed for computed stored fields + # like termination_notice_date + for state in states: + lines = self.env["contract.line"].search([("state", "=", state)]) + self.assertTrue(lines, state) + self.assertTrue(state in lines.mapped("state"), state) + lines = self.env["contract.line"].search([("state", "!=", state)]) + self.assertFalse(state in lines.mapped("state"), state) + lines = self.env["contract.line"].search([("state", "in", states)]) + self.assertEqual(set(lines.mapped("state")), set(states)) + lines = self.env["contract.line"].search([("state", "in", [])]) + self.assertFalse(lines.mapped("state")) + with self.assertRaises(TypeError): + self.env["contract.line"].search([("state", "in", "upcoming")]) + lines = self.env["contract.line"].search([("state", "not in", [])]) + self.assertEqual(set(lines.mapped("state")), set(states)) + lines = self.env["contract.line"].search([("state", "not in", states)]) + self.assertFalse(lines.mapped("state")) + state2 = ["upcoming", "in-progress"] + lines = self.env["contract.line"].search([("state", "not in", state2)]) + self.assertEqual(set(lines.mapped("state")), set(states) - set(state2)) + + def test_check_auto_renew_contract_line_with_successor(self): + """ + A contract line with a successor can't be set to auto-renew + """ + successor_contract_line = self.acct_line.copy() + with self.assertRaises(ValidationError): + self.acct_line.write( + { + "is_auto_renew": True, + "successor_contract_line_id": successor_contract_line.id, + } + ) + + def test_check_no_date_end_contract_line_with_successor(self): + """ + A contract line with a successor must have a end date + """ + successor_contract_line = self.acct_line.copy() + with self.assertRaises(ValidationError): + self.acct_line.write( + { + "date_end": False, + "successor_contract_line_id": successor_contract_line.id, + } + ) + + def test_delay_invoiced_contract_line(self): + self.acct_line.write( + {"last_date_invoiced": self.acct_line.date_start + relativedelta(days=1)} + ) + with self.assertRaises(ValidationError): + self.acct_line._delay(relativedelta(months=1)) + + def test_cancel_invoiced_contract_line(self): + self.acct_line.write( + {"last_date_invoiced": self.acct_line.date_start + relativedelta(days=1)} + ) + with self.assertRaises(ValidationError): + self.acct_line.cancel() + + def test_action_uncancel(self): + action = self.acct_line.action_uncancel() + self.assertEqual( + action["context"]["default_contract_line_id"], self.acct_line.id + ) + + def test_action_plan_successor(self): + action = self.acct_line.action_plan_successor() + self.assertEqual( + action["context"]["default_contract_line_id"], self.acct_line.id + ) + + def test_action_stop(self): + action = self.acct_line.action_stop() + self.assertEqual( + action["context"]["default_contract_line_id"], self.acct_line.id + ) + + def test_action_stop_plan_successor(self): + action = self.acct_line.action_stop_plan_successor() + self.assertEqual( + action["context"]["default_contract_line_id"], self.acct_line.id + ) + + def test_stop_at_last_date_invoiced(self): + self.contract.recurring_create_invoice() + self.assertTrue(self.acct_line.recurring_next_date) + self.acct_line.stop(self.acct_line.last_date_invoiced) + self.assertFalse(self.acct_line.recurring_next_date) + + def test_check_last_date_invoiced_before_next_invoice_date(self): + with self.assertRaises(ValidationError): + self.acct_line.write( + { + "date_start": "2019-01-01", + "date_end": "2019-12-01", + "recurring_next_date": "2019-01-01", + "last_date_invoiced": "2019-06-01", + } + ) + + def test_stop_and_update_recurring_invoice_date(self): + self.acct_line.write( + { + "date_start": "2019-01-01", + "date_end": "2019-12-31", + "recurring_next_date": "2020-01-01", + "recurring_invoicing_type": "post-paid", + "recurring_rule_type": "yearly", + } + ) + self.acct_line.stop(to_date("2019-05-31")) + self.assertEqual(self.acct_line.date_end, to_date("2019-05-31")) + self.assertEqual(self.acct_line.recurring_next_date, to_date("2019-06-01")) diff --git a/contract_line_successor/views/contract_contract.xml b/contract_line_successor/views/contract_contract.xml new file mode 100644 index 0000000000..3ac792370d --- /dev/null +++ b/contract_line_successor/views/contract_contract.xml @@ -0,0 +1,73 @@ + + + + + contract.contract + + + + + + + + + + +