← Back to Pull requests

[18.0][ADD] contract_minimum_term, product_contract_minimum_term #1528

Open
wpichler opened this pull request with 4527 lines diff across 52 files
View on GitHub
wpichler wpichler commented on Sep 18, 2026 06:42 UTC
Author

This PR adds two modules for contracts with a minimum term, as used for example by internet service providers and telecom operators: the customer commits to 24 months, after that the contract goes on until it is terminated.

  • contract_minimum_term: minimum term handling on contracts. Depends on contract_termination.

  • product_contract_minimum_term: glue module with product_contract, so the minimum term can be defined on the product and sold through sale orders. It is auto_install.
  • Why

    In the contract modules, a commitment can only be modelled with a fixed end date (date_end), optionally renewed automatically (is_auto_renew in contract_line_successor, "Duration" in product_contract). That is a different model than a minimum term:

    | | Fixed duration + auto renew | Minimum term |
    |---|---|---|
    | Contract line | ends at date_end, a new period starts at each renewal | open-ended, no date_end |
    | Meaning of "24 months" | the line ends after 24 months | the line can't be terminated before 24 months |
    | Termination | the line is not renewed | the termination date must respect the minimum term and the termination notice |

    Modelling a minimum term with a fixed end date + auto renewal makes the contract lines end and renew for no business reason, and the termination rules are not enforced anywhere. With a minimum term, the contract stays open-ended and only the termination is restricted. contract_termination already has the right place to enforce it.

    contract_minimum_term

    Configuration: the only new fields are the minimum term and its unit (min_term_interval / min_term_rule_type, e.g. 24 months, counted from the start date; 0 disables it). Like the recurrence fields, they are defined on the contract, or on each line when line_recurrence is set, and taken from the contract template and template lines.

    The renewal and the notice reuse the existing fields of contract_line_successor:

  • Auto renew (is_auto_renew + auto_renew_interval / auto_renew_rule_type), e.g. 12 months: on a line with a minimum term it renews the minimum term if the contract is not terminated in time, not an end date. The line stays open-ended. Without auto renew, the contract can be terminated at any time once the minimum term is over, respecting the notice. This is for example what the EU Electronic Communications Code (art. 105) requires for consumer telecom contracts after the initial commitment.

  • Termination notice (termination_notice_interval / termination_notice_rule_type), e.g. 3 months: the termination has to be received at least this long before the end of the minimum term.
  • Reusing these fields needs two small adaptations, both limited to lines with a minimum term: _check_allowed no longer requires an end date on them when they auto renew (also in the line form), and _compute_allowed treats them like open-ended lines, which get_allowed() has no entry for.

    Computed dates on the lines (stored, the contract shows the latest one):

  • min_term_date_end: last day of the running minimum term.

  • min_term_notice_date: last day on which a termination has to be received to end the contract at min_term_date_end.
  • A daily cron extends the terms whose notice deadline is over and logs it in the chatter. The terms are always computed from the start date: no drift on month ends, and a contract that started years ago gets the right current term straight away.

    Termination (contract_termination wizard):

  • New field Termination Received On (default: today), used for the notice check.

  • The wizard proposes the earliest possible termination date and warns when the chosen date is earlier.

  • _terminate_contract refuses an earlier date, except for the new group Contract: Terminate Before Minimum Term (implies Can Terminate Contracts). An early termination is flagged on the contract and logged in the chatter.

  • The check computes the earliest date from the configuration and the reception date, not from the stored fields. It doesn't depend on the cron having run, and a termination letter received days before it is entered in Odoo is handled correctly.
  • Single lines: the minimum term also applies when a line is ended on its own, so a line can't be used to get around the commitment:

  • stop() and a direct change of date_end (form, list, import, RPC) refuse an end date before the earliest possible termination date, with the same exception for the group and a chatter entry. Only an earlier end date is checked: moving it later or removing it doesn't shorten the commitment.

  • cancel() is only possible as long as the line has not started. A user of the group may still cancel a started line that was never invoiced, to correct a mistake.

  • The line stop wizard and the plan successor wizard show the reception date and propose the earliest possible date.

  • The context key min_term_skip_checks lets a module replacing a line by a successor bypass these checks (used by the glue module for upsell / downsell).
  • A line with a minimum term can't have an end date that is renewed automatically (constraint): both renewals would act on the same line.

    product_contract_minimum_term

    On a contract product, a radio button chooses how the contract duration is defined. The two options are exclusive:

  • Fixed Duration: standard product_contract behavior (duration → date_end, optional auto renewal). This is the default, so existing products are not affected.

  • Minimum Term: the fixed duration is hidden and the minimum term is shown instead. Auto renew and the termination notice stay available: they renew the minimum term and define the notice to respect.
  • The choice and the values go to the sale order line and the contract configurator (same radio button, JS patch of the configurator like product_contract_variable_quantity). They are then passed to the contract lines created at confirmation, without date_end. Lines with a fixed duration explicitly get no minimum term, even if the contract template defines one.

    This covers contracts created from a sale order without a contract template.

    Tests

  • contract_minimum_term: 32 tests. Contract and line level values, template, computation and renewal (compute and cron), month ends, termination refused / allowed / allowed with the group, notice deadline, wizards, stopping, cancelling and directly changing the end date of a single line, saving the line form without an end date.

  • product_contract_minimum_term: 4 tests. Sale order line with a minimum term and with a fixed duration, configurator, contract creation from a sale order with a minimum term line and a fixed duration line.

  • The existing tests of contract, contract_line_successor, contract_termination and product_contract pass with both modules installed.
  • The modules are in two separate commits. I'm happy to split them into two PRs if you prefer, the glue module depending on the first one.

    AI disclosure (per the OCA AI policy): this PR was written with the help of Claude Code. I reviewed the code and take responsibility for it. Both commits carry the trailers:

    Assisted-by: Claude Code:claude-opus-5
    Assisted-by: Claude Code:claude-opus-5-5
    

    Diff: 4527 lines (+4527 / -0) across 52 files Reviewer: tobiaszehntner Review duration: 111018m
    All Vouch review confidence checks passed
    Residual risk 0.09 = Change Risk (0.79) × (1 − Review Confidence (0.89)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.79 · 4527 lines diff across 52 files
    0.79
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.89 · 5 substantive comment(s)
    0.89
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 50%
    88%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.400
    Mass File Touch mass_file_touch
    0.080
    Real PR Conversation Timeline
    38 items
    tobiaszehntner tobiaszehntner commented on code on Sep 21, 2026 09:57 UTC
    Code Review
    contract_minimum_term/models/contract_contract.py:1
    @@ -0,0 +1,127 @@
    +# Copyright 2026 Callino - Wolfgang Pichler
    +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
    +
    +from markupsafe import Markup
    +
    +from odoo import api, fields, models
    +from odoo.exceptions import UserError
    +
    +
    +class ContractContract(models.Model):
    +    _inherit = "contract.contract"
    +
    +    min_term_date_end = fields.Date(
    +        string="Minimum Term End",
    +        compute="_compute_min_term_dates",
    +        store=True,
    +        help="Last day of the currently running minimum term (the latest one "
    +        "of all contract lines).",
    +    )
    +    min_term_notice_date = fields.Date(
    +        string="Termination Notice Deadline",
    +        compute="_compute_min_term_dates",
    +        store=True,
    +        help="Termination notice deadline of the contract line with the "
    +        "latest minimum term end.",
    +    )
    +    terminate_request_date = fields.Date(
    +        string="Termination Received On",
    +        readonly=True,
    +        copy=False,
    +        tracking=True,
    +    )
    +    is_terminated_before_min_term = fields.Boolean(
    +        string="Terminated Before Minimum Term",
    +        readonly=True,
    +        copy=False,
    +        tracking=True,
    +    )
    +
    +    @api.depends(
    +        "contract_line_ids.min_term_date_end",
    +        "contract_line_ids.min_term_notice_date",
    +        "contract_line_ids.is_canceled",
    +    )
    +    def _compute_min_term_dates(self):
    +        for rec in self:
    +            lines = rec._get_min_term_lines().filtered("min_term_date_end")
    +            last_line = lines.sorted("min_term_date_end")[-1:]
    +            rec.min_term_date_end = last_line.min_term_date_end
    +            rec.min_term_notice_date = last_line.min_term_notice_date
    +
    +    def _get_min_term_lines(self):
    +        self.ensure_one()
    +        return self.contract_line_ids.filtered(
    +            lambda line: not line.display_type and not line.is_canceled

    I wonder if this could only concern lines with min_term_date_end, ie. actively adding the ones you want instead of excluding (you filter on those in the compute but you seem to need all of them in _get_min_term_contract_termination_date). My thinking is to make it work with other modules without needing a bridge. I have for example a module that introduces 'is_deferred' on the line which would have to be excluded. Of course it's an easy bridge with this hook, but if it could always concern only contract lines with a minimum term it wouldn't need anything. Not entirely sure you can make it work, so leaving that up to you.

    tobiaszehntner tobiaszehntner commented on code on Sep 21, 2026 09:58 UTC
    Code Review
    contract_minimum_term/models/contract_line.py:1
    @@ -0,0 +1,81 @@
    +# Copyright 2026 Callino - Wolfgang Pichler
    +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
    +
    +from markupsafe import Markup
    +
    +from odoo import api, fields, models
    +
    +from .contract_minimum_term_mixin import MINIMUM_TERM_FIELDS
    +
    +
    +class ContractLine(models.Model):
    +    _inherit = "contract.line"
    +
    +    min_term_date_end = fields.Date(
    +        string="Minimum Term End",
    +        compute="_compute_min_term_dates",
    +        store=True,
    +        copy=False,
    +        help="Last day of the currently running minimum term. The contract "
    +        "line cannot be terminated before this date.",
    +    )
    +    min_term_notice_date = fields.Date(
    +        string="Termination Notice Deadline",
    +        compute="_compute_min_term_dates",
    +        store=True,
    +        copy=False,
    +        help="A termination has to be received at the latest on this date, "
    +        "otherwise the minimum term is extended by the renewal term.",
    +    )
    +
    +    @api.depends("date_start", *MINIMUM_TERM_FIELDS)
    +    def _compute_min_term_dates(self):
    +        today = fields.Date.context_today(self)
    +        for rec in self:
    +            date_end = rec._get_min_term_date_end(rec.date_start, today)
    +            rec.min_term_date_end = date_end
    +            rec.min_term_notice_date = (
    +                date_end - rec._get_min_term_notice_delta() if date_end else False
    +            )
    +
    +    def _get_min_term_earliest_termination_date(self, date_start, request_date):
    +        earliest = super()._get_min_term_earliest_termination_date(
    +            date_start, request_date
    +        )
    +        # A line which ends anyway is not restricted beyond its end date
    +        if earliest and self.date_end:
    +            earliest = min(earliest, self.date_end)
    +        return earliest
    +
    +    @api.model
    +    def _get_min_term_renewal_domain(self):
    +        return [
    +            ("min_term_notice_date", "<", fields.Date.context_today(self)),
    +            ("min_term_renewal_interval", ">", 0),
    +            ("is_canceled", "=", False),
    +            ("contract_id.is_terminated", "=", False),
    +        ]
    +
    +    @api.model
    +    def cron_renew_minimum_term(self):

        def _cron_renew_minimum_term(self):
    

    tobiaszehntner tobiaszehntner commented on code on Sep 21, 2026 10:09 UTC
    Code Review
    contract_minimum_term/models/contract_line.py:1
    @@ -0,0 +1,81 @@
    +# Copyright 2026 Callino - Wolfgang Pichler
    +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
    +
    +from markupsafe import Markup
    +
    +from odoo import api, fields, models
    +
    +from .contract_minimum_term_mixin import MINIMUM_TERM_FIELDS
    +
    +
    +class ContractLine(models.Model):
    +    _inherit = "contract.line"
    +
    +    min_term_date_end = fields.Date(
    +        string="Minimum Term End",
    +        compute="_compute_min_term_dates",
    +        store=True,
    +        copy=False,
    +        help="Last day of the currently running minimum term. The contract "
    +        "line cannot be terminated before this date.",
    +    )
    +    min_term_notice_date = fields.Date(
    +        string="Termination Notice Deadline",
    +        compute="_compute_min_term_dates",
    +        store=True,
    +        copy=False,
    +        help="A termination has to be received at the latest on this date, "
    +        "otherwise the minimum term is extended by the renewal term.",
    +    )
    +
    +    @api.depends("date_start", *MINIMUM_TERM_FIELDS)
    +    def _compute_min_term_dates(self):
    +        today = fields.Date.context_today(self)
    +        for rec in self:
    +            date_end = rec._get_min_term_date_end(rec.date_start, today)
    +            rec.min_term_date_end = date_end
    +            rec.min_term_notice_date = (
    +                date_end - rec._get_min_term_notice_delta() if date_end else False
    +            )
    +
    +    def _get_min_term_earliest_termination_date(self, date_start, request_date):
    +        earliest = super()._get_min_term_earliest_termination_date(
    +            date_start, request_date
    +        )
    +        # A line which ends anyway is not restricted beyond its end date
    +        if earliest and self.date_end:
    +            earliest = min(earliest, self.date_end)
    +        return earliest
    +
    +    @api.model
    +    def _get_min_term_renewal_domain(self):
    +        return [
    +            ("min_term_notice_date", "<", fields.Date.context_today(self)),
    +            ("min_term_renewal_interval", ">", 0),
    +            ("is_canceled", "=", False),
    +            ("contract_id.is_terminated", "=", False),
    +        ]
    +
    +    @api.model
    +    def cron_renew_minimum_term(self):
    +        lines = self.search(self._get_min_term_renewal_domain())
    +        old_dates = {line: line.min_term_date_end for line in lines}
    +        # The dates depend on the current date: force their recomputation and
    +        # propagate the new values to the dependent fields (contract dates)
    +        fnames = ["min_term_date_end", "min_term_notice_date"]

    maybe separate that out into a hook?

    tobiaszehntner tobiaszehntner commented on code on Sep 21, 2026 10:16 UTC
    Code Review
    contract_minimum_term/models/contract_minimum_term_mixin.py:1
    @@ -0,0 +1,115 @@
    +# Copyright 2026 Callino - Wolfgang Pichler
    +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
    +
    +from dateutil.relativedelta import relativedelta
    +
    +from odoo import api, fields, models
    +
    +MINIMUM_TERM_FIELDS = [
    +    "min_term_interval",
    +    "min_term_rule_type",
    +    "min_term_renewal_interval",
    +    "min_term_renewal_rule_type",
    +    "min_term_notice_interval",
    +    "min_term_notice_rule_type",
    +]
    +
    +RULE_TYPES = [
    +    ("daily", "Day(s)"),
    +    ("weekly", "Week(s)"),
    +    ("monthly", "Month(s)"),
    +    ("yearly", "Year(s)"),
    +]
    +
    +
    +class ContractMinimumTermMixin(models.AbstractModel):
    +    _name = "contract.minimum.term.mixin"

    product_contract and contract_line_successor both already have renewal periods. Could the contract not go to that after the minimum term is fulfilled, in order to not introduce yet another interval?

    tobiaszehntner tobiaszehntner commented on code on Sep 21, 2026 10:30 UTC
    Code Review
    product_contract_minimum_term/models/product_template.py:1
    @@ -0,0 +1,49 @@
    +# Copyright 2026 Callino - Wolfgang Pichler
    +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
    +
    +from odoo import api, fields, models
    +from odoo.exceptions import ValidationError
    +
    +CONTRACT_DURATION_TYPES = [
    +    ("fixed", "Fixed Duration"),
    +    ("minimum_term", "Minimum Term"),
    +]
    +
    +
    +class ProductTemplate(models.Model):
    +    _name = "product.template"
    +    _inherit = ["product.template", "contract.minimum.term.mixin"]
    +
    +    contract_duration_type = fields.Selection(
    +        CONTRACT_DURATION_TYPES,
    +        string="Contract Duration",
    +        default="fixed",
    +        required=True,
    +        help="Fixed Duration: the contract line ends after the duration, it can "
    +        "be renewed automatically.\n"
    +        "Minimum Term: the contract line is open-ended, it cannot be "
    +        "terminated before the end of the minimum term, which is extended by "
    +        "the renewal term if the contract is not terminated in time.",
    +    )
    +    min_term_rule_type = fields.Selection(default="monthly")
    +    min_term_renewal_rule_type = fields.Selection(default="monthly")

    to only have one source of truth instead of declaring the same string multiple times, I like to declare the selection values as static (in the mixin) and import it here and other places. But not required.

    tobiaszehntner tobiaszehntner commented on code on Sep 21, 2026 10:47 UTC
    Code Review
    product_contract_minimum_term/models/sale_order_line_contract_mixin.py:1
    @@ -0,0 +1,88 @@
    +# Copyright 2026 Callino - Wolfgang Pichler
    +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
    +
    +from odoo import api, fields, models
    +
    +from odoo.addons.contract_minimum_term.models.contract_minimum_term_mixin import (
    +    MINIMUM_TERM_FIELDS,
    +)
    +
    +from .product_template import CONTRACT_DURATION_TYPES
    +
    +
    +class SaleOrderLineContractMixin(models.AbstractModel):
    +    _name = "sale.order.line.contract.mixin"
    +    _inherit = ["sale.order.line.contract.mixin", "contract.minimum.term.mixin"]
    +
    +    contract_duration_type = fields.Selection(
    +        CONTRACT_DURATION_TYPES,
    +        string="Contract Duration",
    +        compute="_compute_product_contract_minimum_term",
    +        precompute=True,
    +        store=True,
    +        readonly=False,
    +    )
    +    min_term_interval = fields.Integer(
    +        compute="_compute_product_contract_minimum_term",
    +        precompute=True,
    +        store=True,
    +        readonly=False,
    +    )
    +    min_term_rule_type = fields.Selection(
    +        compute="_compute_product_contract_minimum_term",
    +        precompute=True,
    +        store=True,
    +        readonly=False,
    +    )
    +    min_term_renewal_interval = fields.Integer(
    +        compute="_compute_product_contract_minimum_term",
    +        precompute=True,
    +        store=True,
    +        readonly=False,
    +    )
    +    min_term_renewal_rule_type = fields.Selection(

    sale.order.line.contract.mixin has already

    auto_renew_rule_type = fields.Selection(
            [
                ("daily", "Day(s)"),
                ("weekly", "Week(s)"),
                ("monthly", "Month(s)"),
                ("yearly", "Year(s)"),
            ],
    

    so it seems weird to have a separate renewal field..?

    tobiaszehntner tobiaszehntner requested changes on Sep 21, 2026 10:51 UTC
    Changes Requested Reviewer

    Main concern is about introducing new renewal fields and not using existing ones. In my opinion, after a contract's minimum term has ended, it should go over to standard (or other contract module's renewal) functions for the continuation. So these modules only handle the minimum term, not the period afterwards.

    wpichler wpichler commented on Sep 21, 2026 12:48 UTC
    Author

    Thanks a lot for the thorough review!

    Renewal fields. You are right that the renewal term should not be a new
    concept, and I will reuse the existing fields:

  • is_auto_renew + auto_renew_interval / auto_renew_rule_type for the

  • renewal of the minimum term
  • termination_notice_interval / termination_notice_rule_type (also already

  • there) instead of min_term_notice_

    That leaves min_term_interval / min_term_rule_type as the only new fields,
    on the contract (line) and, through the glue module, on the product and the
    sale order line. The current constraint "a minimum term excludes automatic
    renewal" then becomes "a minimum term excludes a fixed end date", which is the
    real restriction: with date_end set, both mechanisms would fight over the
    same line.

    Handing over to the standard renewal after the minimum term. I looked into
    that, but I do not think it can work, because the existing renewal is built
    around date_end:

  • _compute_termination_notice_date returns False without a date_end

  • _contract_line_to_renew_domain filters on termination_notice_date <= today,

  • so an open-ended line is never picked up
  • _get_renewal_new_date_end starts from self.date_end
  • A line with a minimum term has no date_end at all -- that is the point of the
    module: the contract stays open-ended, only the commitment is renewed. So there
    is nothing for renew() to extend, and if it ran it would give the line an end
    date, which is exactly what we want to avoid. What these modules add is
    therefore only "until when can this line not be terminated", never an end date,
    and everything after the minimum term is plain open-ended contract behaviour.

    If you would rather have no renewal of the commitment at all in this module,
    that is possible too, but it would leave out the common case in telecom/ISP
    contracts: 24 months, then 12 more unless terminated 3 months in advance.

    _get_min_term_lines. Good point, I will turn it around and select the
    lines that actually have a minimum term (min_term_date_end set, and not
    cancelled) instead of excluding the others. Lines that keep their
    min_term_date_end in the past are still covered, so the notice period after
    the end of the minimum term keeps working, and your is_deferred lines need no
    bridge. Note that this also means a line without a minimum term is no longer
    restricted by a termination notice alone -- which fits the reuse of
    termination_notice_
    above, since every product carries a default notice of
    one month.

    _cron_renew_minimum_term and the hook: both done. The recomputation
    moves into _renew_minimum_term() on the lines, and the cron only searches and
    calls it. (I had followed cron_renew_contract_line from
    contract_line_successor for the name; happy to use the private one.)

    Selection values. CONTRACT_DURATION_TYPES is already a module-level
    constant that the sale order line mixin imports, and the rule types live in
    contract_minimum_term as RULE_TYPES; the lines you pointed at only set a
    different default. If you meant something else there, let me know.

    wpichler wpichler commented on Sep 21, 2026 13:09 UTC
    Author

    Rework pushed (force-push, still two commits, one per module).

    Renewal and notice now reuse the existing fields. min_term_renewal_ and
    min_term_notice_
    are gone. What is left as new configuration is
    min_term_interval / min_term_rule_type on the contract, the template, the
    product and the sale order line.

  • is_auto_renew + auto_renew_interval / auto_renew_rule_type: on a line

  • with a minimum term these renew the minimum term, not an end date.
  • termination_notice_interval / termination_notice_rule_type: the notice

  • to respect, as before.

    That reuse needed two small adaptations in contract_minimum_term, both
    scoped to lines that actually have a minimum term:

  • _check_allowed of contract_line_successor requires an end date on an

  • auto-renewing line. A minimum term line is open-ended on purpose, so the
    check now only applies to the other lines (the "successor" part still
    applies to all).
  • get_allowed() has no entry for has_date_end=False, is_auto_renew=True,

  • which until now could not exist, so such a line was allowed nothing at all
    (no stop, no cancel, and therefore no termination). _compute_allowed
    evaluates those lines as open-ended lines without auto renewal, which is
    what they are as far as the end date is concerned.

    The constraint is now "a minimum term cannot have an end date that is
    renewed automatically
    " rather than "cannot have an end date": stopping or
    terminating a line sets date_end and switches is_auto_renew off, and that
    has to stay possible.

    _get_min_term_lines now selects the lines that have a minimum term
    (min_term_date_end set, not cancelled) instead of excluding lines. As
    announced, a line without a minimum term is no longer restricted by a
    termination notice alone.

    _cron_renew_minimum_term renamed, and the forced recomputation moved
    into _renew_minimum_term() on the lines; the cron only searches and calls
    it.

    Views: the minimum term of a line sits on the existing Renewal page next
    to is_auto_renew and the notice, and that group is now also shown when there
    is a minimum term without auto renewal. On the product, Renewal and
    Termination Notice stay visible in both duration types, only the fixed
    Duration is hidden for a minimum term.

    Tests: both test classes are now @tagged("post_install", "-at_install").
    Thanks for that catch, it also fixes a failure in a database where the glue
    module is installed. 18 + 6 tests, and the tests of contract,
    contract_line_successor, contract_termination and product_contract pass
    with both modules installed (168 in total).

    I left the selection constants as they are, as discussed; let me know if you
    meant something else there.

    🤖 Generated with Claude Code

    wpichler wpichler commented on Sep 21, 2026 13:57 UTC
    Author

    One more commit (no force-push this time): the minimum term now also applies to
    a single contract line. Until now only contract.contract._terminate_contract
    was checked, so stopping or cancelling a line was a way around the commitment —
    all the more since _compute_allowed has to allow those actions on an
    open-ended auto renewing line for the termination to work at all.

  • stop() refuses an end date before the earliest possible termination date,

  • computed exactly like in the wizard (_get_min_term_earliest_termination_date),
    with the same exception for the group Terminate Before Minimum Term and a
    chatter entry. The termination of the whole contract passes its reception date
    down, so both levels agree and only one message is logged.
  • cancel() is only possible while the line has not started (no start date, or

  • a start date in the future). A running line is stopped, not taken back. A user
    of that group may still cancel a started line that was never invoiced, to
    correct a mistake; is_cancel_allowed itself stays user-independent and only
    carries the "not started yet" rule.
  • New context key min_term_skip_checks for modules that replace a line by a

  • successor instead of ending it. product_contract_minimum_term uses it for
    the upsell/downsell of product_contract, where the replacement line carries
    the commitment on.

    24 + 6 tests, everything green together with contract, contract_line_successor,
    contract_termination and product_contract (174).

    🤖 Generated with Claude Code

    tobiaszehntner tobiaszehntner commented on code on Sep 22, 2026 08:32 UTC
    Code Review
    contract_minimum_term/models/contract_line.py:1
    @@ -0,0 +1,272 @@
    +# Copyright 2026 Callino - Wolfgang Pichler
    +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
    +
    +from markupsafe import Markup
    +
    +from odoo import api, fields, models
    +from odoo.exceptions import UserError, ValidationError
    +
    +from odoo.addons.contract_line_successor.models.contract_line_constraints import (
    +    get_allowed,
    +)
    +
    +MINIMUM_TERM_DATE_FIELDS = ["min_term_date_end", "min_term_notice_date"]
    +EARLY_TERMINATION_GROUP = "contract_minimum_term.group_early_termination"
    +
    +
    +class ContractLine(models.Model):
    +    _inherit = "contract.line"
    +
    +    min_term_date_end = fields.Date(
    +        string="Minimum Term End",
    +        compute="_compute_min_term_dates",
    +        store=True,
    +        copy=False,
    +        help="Last day of the currently running minimum term. The contract "
    +        "line cannot be terminated before this date.",
    +    )
    +    min_term_notice_date = fields.Date(
    +        string="Termination Notice Deadline",
    +        compute="_compute_min_term_dates",
    +        store=True,
    +        copy=False,
    +        help="A termination has to be received at the latest on this date, "
    +        "otherwise the minimum term is extended by the renewal term.",
    +    )
    +
    +    @api.depends(
    +        "date_start",
    +        "min_term_interval",
    +        "min_term_rule_type",
    +        "is_auto_renew",
    +        "auto_renew_interval",
    +        "auto_renew_rule_type",
    +        "termination_notice_interval",
    +        "termination_notice_rule_type",
    +    )
    +    def _compute_min_term_dates(self):
    +        today = fields.Date.context_today(self)
    +        for rec in self:
    +            date_end = rec._get_min_term_date_end(rec.date_start, today)
    +            rec.min_term_date_end = date_end
    +            rec.min_term_notice_date = (
    +                date_end - rec._get_min_term_notice_delta() if date_end else False
    +            )
    +
    +    def _get_min_term_earliest_termination_date(self, date_start, request_date):
    +        earliest = super()._get_min_term_earliest_termination_date(
    +            date_start, request_date
    +        )
    +        # A line which ends anyway is not restricted beyond its end date. A
    +        # successor starting after it has a term of its own.
    +        if earliest and self.date_end and self.date_end >= date_start:
    +            earliest = min(earliest, self.date_end)
    +        return earliest
    +
    +    @api.constrains("is_auto_renew", "successor_contract_line_id", "date_end")
    +    def _check_allowed(self):
    +        """A line with a minimum term renews the term, not an end date.
    +
    +        ``contract_line_successor`` requires an end date on an auto-renewing
    +        line. That check only applies to lines without a minimum term, the
    +        others stay open-ended on purpose.
    +        """
    +        min_term_lines = self.filtered(lambda rec: rec.min_term_interval > 0)
    +        res = super(ContractLine, self - min_term_lines)._check_allowed()
    +        for rec in min_term_lines:
    +            if rec.is_auto_renew and rec.successor_contract_line_id:
    +                raise ValidationError(
    +                    self.env._(
    +                        "A contract line with a successor can't be set to " "auto-renew"

    please review what you push manually

                            "A contract line with a successor can't be set to auto-renew"
    

    tobiaszehntner tobiaszehntner approved these changes on Sep 22, 2026 08:36 UTC
    Approved Reviewer

    I don't mind if you write your code with AI, but it would be great to have a human writing and responding to comments.

    wpichler wpichler commented on code on Sep 22, 2026 08:54 UTC
    Code Review
    contract_minimum_term/models/contract_line.py:1
    @@ -0,0 +1,272 @@
    +# Copyright 2026 Callino - Wolfgang Pichler
    +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
    +
    +from markupsafe import Markup
    +
    +from odoo import api, fields, models
    +from odoo.exceptions import UserError, ValidationError
    +
    +from odoo.addons.contract_line_successor.models.contract_line_constraints import (
    +    get_allowed,
    +)
    +
    +MINIMUM_TERM_DATE_FIELDS = ["min_term_date_end", "min_term_notice_date"]
    +EARLY_TERMINATION_GROUP = "contract_minimum_term.group_early_termination"
    +
    +
    +class ContractLine(models.Model):
    +    _inherit = "contract.line"
    +
    +    min_term_date_end = fields.Date(
    +        string="Minimum Term End",
    +        compute="_compute_min_term_dates",
    +        store=True,
    +        copy=False,
    +        help="Last day of the currently running minimum term. The contract "
    +        "line cannot be terminated before this date.",
    +    )
    +    min_term_notice_date = fields.Date(
    +        string="Termination Notice Deadline",
    +        compute="_compute_min_term_dates",
    +        store=True,
    +        copy=False,
    +        help="A termination has to be received at the latest on this date, "
    +        "otherwise the minimum term is extended by the renewal term.",
    +    )
    +
    +    @api.depends(
    +        "date_start",
    +        "min_term_interval",
    +        "min_term_rule_type",
    +        "is_auto_renew",
    +        "auto_renew_interval",
    +        "auto_renew_rule_type",
    +        "termination_notice_interval",
    +        "termination_notice_rule_type",
    +    )
    +    def _compute_min_term_dates(self):
    +        today = fields.Date.context_today(self)
    +        for rec in self:
    +            date_end = rec._get_min_term_date_end(rec.date_start, today)
    +            rec.min_term_date_end = date_end
    +            rec.min_term_notice_date = (
    +                date_end - rec._get_min_term_notice_delta() if date_end else False
    +            )
    +
    +    def _get_min_term_earliest_termination_date(self, date_start, request_date):
    +        earliest = super()._get_min_term_earliest_termination_date(
    +            date_start, request_date
    +        )
    +        # A line which ends anyway is not restricted beyond its end date. A
    +        # successor starting after it has a term of its own.
    +        if earliest and self.date_end and self.date_end >= date_start:
    +            earliest = min(earliest, self.date_end)
    +        return earliest
    +
    +    @api.constrains("is_auto_renew", "successor_contract_line_id", "date_end")
    +    def _check_allowed(self):
    +        """A line with a minimum term renews the term, not an end date.
    +
    +        ``contract_line_successor`` requires an end date on an auto-renewing
    +        line. That check only applies to lines without a minimum term, the
    +        others stay open-ended on purpose.
    +        """
    +        min_term_lines = self.filtered(lambda rec: rec.min_term_interval > 0)
    +        res = super(ContractLine, self - min_term_lines)._check_allowed()
    +        for rec in min_term_lines:
    +            if rec.is_auto_renew and rec.successor_contract_line_id:
    +                raise ValidationError(
    +                    self.env._(
    +                        "A contract line with a successor can't be set to " "auto-renew"

    Hi Tobias,

    thanks for your response - you are right - i will take a closer look at what i push. Next Force Push is on the way...

    Thanks for your support...

    I do also have an other module which could be interesting - contract_periods - this module does force contracts to align to invoicing periods (first of month for example) - and does implement aliquot calculation, and also periods in the invoice (so if you create a new contract which started some months ago, and you selected monthly billing, it would create one invoice - with invoice lines for each period). Is there already such functionality in the repo (i havn't seen something like that) - should i also create a PR for it ?

    bosd bosd commented on Sep 22, 2026 10:24 UTC
    Contributor

    @wpichler Can you please have a look at PR #1374
    It has a overlap in use case. Altough being on odoo 19 and a more simple implementation.
    Let's see if we can have some common ground.

    IMO, the Termination notice deadline is a function that should be in it's own module. As it is not strictly related to the minimum duration.
    (Also in my use case, we want silent renewal. While UX is already disastrous, on these modules, not willing to have another field added by default, which will never be used. )

    This PR is more advanced and supports minimum duration on the line level.

    I tried to test this module on the runboat. But was not able to find the button "Terminate Contract" according to the readme. Tried both line level and header contracts.

    For now, focused mainly on the contract_minimum_term, Is the product_contract_minimum_term adding any additional functionality, which is not already covered by setting these fields in the contract template?

    bosd bosd commented on Sep 22, 2026 10:30 UTC
    Contributor

    Also might want to consider using the widgets on the fields. e.g. 3 days left before min termination date.

    bosd bosd commented on code on Oct 01, 2026 16:17 UTC
    Code Review
    contract_minimum_term/views/contract_contract.xml:1
    @@ -0,0 +1,73 @@
    +<?xml version="1.0" encoding="utf-8" ?>
    +<!-- Copyright 2026 Callino - Wolfgang Pichler
    +     License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). -->
    +<odoo>
    +    <record model="ir.ui.view" id="contract_contract_form_view">
    +        <field name="model">contract.contract</field>
    +        <field name="inherit_id" ref="contract.contract_contract_form_view" />
    +        <field name="arch" type="xml">
    +            <xpath expr="//page[@name='invoicing']" position="after">
    +                <page string="Minimum Term" name="minimum_term">
    +                    <group name="minimum_term">
    +                        <group invisible="line_recurrence">
    +                            <label for="min_term_interval" />
    +                            <div class="o_row">
    +                                <field
    +                                    name="min_term_interval"
    +                                    class="oe_inline"
    +                                    readonly="is_terminated"
    +                                />
    +                                <field
    +                                    name="min_term_rule_type"
    +                                    class="oe_inline"
    +                                    readonly="is_terminated"
    +                                    required="min_term_interval"
    +                                />
    +                            </div>
    +                        </group>
    +                        <group>
    +                            <field name="min_term_date_end" />
    +                            <field name="min_term_notice_date" />

                                <field name="min_term_date_end" widget="remaining_days" />
                                <field name="min_term_notice_date" widget="remaining_days" />
    

    bosd bosd commented on code on Oct 01, 2026 16:17 UTC
    Code Review
    contract_minimum_term/views/contract_line.xml:1
    @@ -0,0 +1,15 @@
    +<?xml version="1.0" encoding="utf-8" ?>
    +<!-- Copyright 2026 Callino - Wolfgang Pichler
    +     License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). -->
    +<odoo>
    +    <record model="ir.ui.view" id="contract_line_form_view">
    +        <field name="model">contract.line</field>
    +        <field name="inherit_id" ref="contract.contract_line_form_view" />
    +        <field name="arch" type="xml">
    +            <xpath expr="//group[@name='minimum_term']" position="inside">
    +                <field name="min_term_date_end" />
    +                <field name="min_term_notice_date" />

                    <field name="min_term_date_end" widget="remaining_days" />
                    <field name="min_term_notice_date" widget="remaining_days" />
    

    bosd bosd commented on code on Oct 01, 2026 16:18 UTC
    Code Review
    contract_minimum_term/views/contract_contract.xml:1
    @@ -0,0 +1,73 @@
    +<?xml version="1.0" encoding="utf-8" ?>
    +<!-- Copyright 2026 Callino - Wolfgang Pichler
    +     License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). -->
    +<odoo>
    +    <record model="ir.ui.view" id="contract_contract_form_view">
    +        <field name="model">contract.contract</field>
    +        <field name="inherit_id" ref="contract.contract_contract_form_view" />
    +        <field name="arch" type="xml">
    +            <xpath expr="//page[@name='invoicing']" position="after">
    +                <page string="Minimum Term" name="minimum_term">
    +                    <group name="minimum_term">
    +                        <group invisible="line_recurrence">
    +                            <label for="min_term_interval" />
    +                            <div class="o_row">
    +                                <field
    +                                    name="min_term_interval"
    +                                    class="oe_inline"
    +                                    readonly="is_terminated"
    +                                />
    +                                <field
    +                                    name="min_term_rule_type"
    +                                    class="oe_inline"
    +                                    readonly="is_terminated"
    +                                    required="min_term_interval"
    +                                />
    +                            </div>
    +                        </group>
    +                        <group>
    +                            <field name="min_term_date_end" />
    +                            <field name="min_term_notice_date" />
    +                            <field
    +                                name="terminate_request_date"
    +                                invisible="not is_terminated"
    +                            />
    +                            <field
    +                                name="is_terminated_before_min_term"
    +                                invisible="not is_terminated"
    +                            />
    +                        </group>
    +                    </group>
    +                </page>
    +            </xpath>
    +            <xpath
    +                expr="//field[@name='contract_line_fixed_ids']//list/field[@name='date_end']"
    +                position="after"
    +            >
    +                <field name="min_term_date_end" optional="show" />
    +                <field name="min_term_notice_date" optional="hide" />

                    <field name="min_term_date_end" widget="remaining_days" optional="show" />
                    <field name="min_term_notice_date" widget="remaining_days" optional="hide" />
    

    bosd bosd commented on code on Oct 01, 2026 16:18 UTC
    Code Review
    contract_minimum_term/views/contract_contract.xml:1
    @@ -0,0 +1,73 @@
    +<?xml version="1.0" encoding="utf-8" ?>
    +<!-- Copyright 2026 Callino - Wolfgang Pichler
    +     License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). -->
    +<odoo>
    +    <record model="ir.ui.view" id="contract_contract_form_view">
    +        <field name="model">contract.contract</field>
    +        <field name="inherit_id" ref="contract.contract_contract_form_view" />
    +        <field name="arch" type="xml">
    +            <xpath expr="//page[@name='invoicing']" position="after">
    +                <page string="Minimum Term" name="minimum_term">
    +                    <group name="minimum_term">
    +                        <group invisible="line_recurrence">
    +                            <label for="min_term_interval" />
    +                            <div class="o_row">
    +                                <field
    +                                    name="min_term_interval"
    +                                    class="oe_inline"
    +                                    readonly="is_terminated"
    +                                />
    +                                <field
    +                                    name="min_term_rule_type"
    +                                    class="oe_inline"
    +                                    readonly="is_terminated"
    +                                    required="min_term_interval"
    +                                />
    +                            </div>
    +                        </group>
    +                        <group>
    +                            <field name="min_term_date_end" />
    +                            <field name="min_term_notice_date" />
    +                            <field
    +                                name="terminate_request_date"
    +                                invisible="not is_terminated"
    +                            />
    +                            <field
    +                                name="is_terminated_before_min_term"
    +                                invisible="not is_terminated"
    +                            />
    +                        </group>
    +                    </group>
    +                </page>
    +            </xpath>
    +            <xpath
    +                expr="//field[@name='contract_line_fixed_ids']//list/field[@name='date_end']"
    +                position="after"
    +            >
    +                <field name="min_term_date_end" optional="show" />
    +                <field name="min_term_notice_date" optional="hide" />
    +            </xpath>
    +            <xpath
    +                expr="//field[@name='contract_line_ids']//list/field[@name='date_end']"
    +                position="after"
    +            >
    +                <field name="min_term_date_end" optional="show" />
    +                <field name="min_term_notice_date" optional="hide" />

                    <field name="min_term_date_end" widget="remaining_days" optional="show" />
                    <field name="min_term_notice_date" widget="remaining_days" optional="hide" />
    

    bosd bosd commented on code on Oct 01, 2026 16:21 UTC
    Code Review
    contract_minimum_term/views/contract_contract.xml:1
    @@ -0,0 +1,73 @@
    +<?xml version="1.0" encoding="utf-8" ?>
    +<!-- Copyright 2026 Callino - Wolfgang Pichler
    +     License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). -->
    +<odoo>
    +    <record model="ir.ui.view" id="contract_contract_form_view">
    +        <field name="model">contract.contract</field>
    +        <field name="inherit_id" ref="contract.contract_contract_form_view" />
    +        <field name="arch" type="xml">
    +            <xpath expr="//page[@name='invoicing']" position="after">
    +                <page string="Minimum Term" name="minimum_term">
    +                    <group name="minimum_term">
    +                        <group invisible="line_recurrence">
    +                            <label for="min_term_interval" />
    +                            <div class="o_row">
    +                                <field
    +                                    name="min_term_interval"
    +                                    class="oe_inline"
    +                                    readonly="is_terminated"
    +                                />
    +                                <field
    +                                    name="min_term_rule_type"
    +                                    class="oe_inline"
    +                                    readonly="is_terminated"
    +                                    required="min_term_interval"
    +                                />
    +                            </div>
    +                        </group>
    +                        <group>
    +                            <field name="min_term_date_end" />
    +                            <field name="min_term_notice_date" />
    +                            <field
    +                                name="terminate_request_date"
    +                                invisible="not is_terminated"
    +                            />
    +                            <field
    +                                name="is_terminated_before_min_term"
    +                                invisible="not is_terminated"
    +                            />

                              <div class="alert alert-warning" role="alert"
                                   invisible="not is_terminated_before_min_term">
                                  This contract was terminated before the end of its minimum term.
                              </div>
    

    bosd bosd commented on code on Oct 01, 2026 16:24 UTC
    Code Review
    contract_minimum_term/models/contract_minimum_term_mixin.py:1
    @@ -0,0 +1,113 @@
    +# Copyright 2026 Callino - Wolfgang Pichler
    +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
    +
    +from dateutil.relativedelta import relativedelta
    +
    +from odoo import api, fields, models
    +
    +MINIMUM_TERM_FIELDS = [
    +    "min_term_interval",
    +    "min_term_rule_type",
    +]
    +
    +RULE_TYPES = [
    +    ("daily", "Day(s)"),
    +    ("weekly", "Week(s)"),
    +    ("monthly", "Month(s)"),
    +    ("yearly", "Year(s)"),
    +]
    +
    +
    +class ContractMinimumTermMixin(models.AbstractModel):
    +    """Minimum term of a contract (line).
    +
    +    The renewal of the minimum term and the termination notice reuse the
    +    fields of ``contract_line_successor``: ``is_auto_renew`` together with
    +    ``auto_renew_interval`` / ``auto_renew_rule_type`` renews the minimum
    +    term instead of an end date, and ``termination_notice_interval`` /
    +    ``termination_notice_rule_type`` is the notice to respect.
    +    """
    +
    +    _name = "contract.minimum.term.mixin"
    +    _description = "Contract Minimum Term Mixin"
    +
    +    min_term_interval = fields.Integer(
    +        string="Minimum Term",
    +        help="Initial minimum term, counted from the start date. The contract "
    +        "cannot be terminated before the end of this term. Leave 0 to disable "
    +        "the minimum term.",
    +    )
    +    min_term_rule_type = fields.Selection(
    +        RULE_TYPES,
    +        string="Minimum Term Type",
    +    )

        min_term_rule_type = fields.Selection(
            RULE_TYPES,
            string="Minimum Term Type",
            default="monthly"
        )
    

    wpichler wpichler commented on code on Oct 29, 2026 08:06 UTC
    Code Review
    contract_minimum_term/models/contract_contract.py:1
    @@ -0,0 +1,127 @@
    +# Copyright 2026 Callino - Wolfgang Pichler
    +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
    +
    +from markupsafe import Markup
    +
    +from odoo import api, fields, models
    +from odoo.exceptions import UserError
    +
    +
    +class ContractContract(models.Model):
    +    _inherit = "contract.contract"
    +
    +    min_term_date_end = fields.Date(
    +        string="Minimum Term End",
    +        compute="_compute_min_term_dates",
    +        store=True,
    +        help="Last day of the currently running minimum term (the latest one "
    +        "of all contract lines).",
    +    )
    +    min_term_notice_date = fields.Date(
    +        string="Termination Notice Deadline",
    +        compute="_compute_min_term_dates",
    +        store=True,
    +        help="Termination notice deadline of the contract line with the "
    +        "latest minimum term end.",
    +    )
    +    terminate_request_date = fields.Date(
    +        string="Termination Received On",
    +        readonly=True,
    +        copy=False,
    +        tracking=True,
    +    )
    +    is_terminated_before_min_term = fields.Boolean(
    +        string="Terminated Before Minimum Term",
    +        readonly=True,
    +        copy=False,
    +        tracking=True,
    +    )
    +
    +    @api.depends(
    +        "contract_line_ids.min_term_date_end",
    +        "contract_line_ids.min_term_notice_date",
    +        "contract_line_ids.is_canceled",
    +    )
    +    def _compute_min_term_dates(self):
    +        for rec in self:
    +            lines = rec._get_min_term_lines().filtered("min_term_date_end")
    +            last_line = lines.sorted("min_term_date_end")[-1:]
    +            rec.min_term_date_end = last_line.min_term_date_end
    +            rec.min_term_notice_date = last_line.min_term_notice_date
    +
    +    def _get_min_term_lines(self):
    +        self.ensure_one()
    +        return self.contract_line_ids.filtered(
    +            lambda line: not line.display_type and not line.is_canceled

    done

    wpichler wpichler commented on code on Oct 29, 2026 08:07 UTC
    Code Review
    contract_minimum_term/models/contract_line.py:1
    @@ -0,0 +1,81 @@
    +# Copyright 2026 Callino - Wolfgang Pichler
    +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
    +
    +from markupsafe import Markup
    +
    +from odoo import api, fields, models
    +
    +from .contract_minimum_term_mixin import MINIMUM_TERM_FIELDS
    +
    +
    +class ContractLine(models.Model):
    +    _inherit = "contract.line"
    +
    +    min_term_date_end = fields.Date(
    +        string="Minimum Term End",
    +        compute="_compute_min_term_dates",
    +        store=True,
    +        copy=False,
    +        help="Last day of the currently running minimum term. The contract "
    +        "line cannot be terminated before this date.",
    +    )
    +    min_term_notice_date = fields.Date(
    +        string="Termination Notice Deadline",
    +        compute="_compute_min_term_dates",
    +        store=True,
    +        copy=False,
    +        help="A termination has to be received at the latest on this date, "
    +        "otherwise the minimum term is extended by the renewal term.",
    +    )
    +
    +    @api.depends("date_start", *MINIMUM_TERM_FIELDS)
    +    def _compute_min_term_dates(self):
    +        today = fields.Date.context_today(self)
    +        for rec in self:
    +            date_end = rec._get_min_term_date_end(rec.date_start, today)
    +            rec.min_term_date_end = date_end
    +            rec.min_term_notice_date = (
    +                date_end - rec._get_min_term_notice_delta() if date_end else False
    +            )
    +
    +    def _get_min_term_earliest_termination_date(self, date_start, request_date):
    +        earliest = super()._get_min_term_earliest_termination_date(
    +            date_start, request_date
    +        )
    +        # A line which ends anyway is not restricted beyond its end date
    +        if earliest and self.date_end:
    +            earliest = min(earliest, self.date_end)
    +        return earliest
    +
    +    @api.model
    +    def _get_min_term_renewal_domain(self):
    +        return [
    +            ("min_term_notice_date", "<", fields.Date.context_today(self)),
    +            ("min_term_renewal_interval", ">", 0),
    +            ("is_canceled", "=", False),
    +            ("contract_id.is_terminated", "=", False),
    +        ]
    +
    +    @api.model
    +    def cron_renew_minimum_term(self):

    done

    wpichler wpichler commented on code on Oct 29, 2026 08:08 UTC
    Code Review
    contract_minimum_term/models/contract_line.py:1
    @@ -0,0 +1,81 @@
    +# Copyright 2026 Callino - Wolfgang Pichler
    +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
    +
    +from markupsafe import Markup
    +
    +from odoo import api, fields, models
    +
    +from .contract_minimum_term_mixin import MINIMUM_TERM_FIELDS
    +
    +
    +class ContractLine(models.Model):
    +    _inherit = "contract.line"
    +
    +    min_term_date_end = fields.Date(
    +        string="Minimum Term End",
    +        compute="_compute_min_term_dates",
    +        store=True,
    +        copy=False,
    +        help="Last day of the currently running minimum term. The contract "
    +        "line cannot be terminated before this date.",
    +    )
    +    min_term_notice_date = fields.Date(
    +        string="Termination Notice Deadline",
    +        compute="_compute_min_term_dates",
    +        store=True,
    +        copy=False,
    +        help="A termination has to be received at the latest on this date, "
    +        "otherwise the minimum term is extended by the renewal term.",
    +    )
    +
    +    @api.depends("date_start", *MINIMUM_TERM_FIELDS)
    +    def _compute_min_term_dates(self):
    +        today = fields.Date.context_today(self)
    +        for rec in self:
    +            date_end = rec._get_min_term_date_end(rec.date_start, today)
    +            rec.min_term_date_end = date_end
    +            rec.min_term_notice_date = (
    +                date_end - rec._get_min_term_notice_delta() if date_end else False
    +            )
    +
    +    def _get_min_term_earliest_termination_date(self, date_start, request_date):
    +        earliest = super()._get_min_term_earliest_termination_date(
    +            date_start, request_date
    +        )
    +        # A line which ends anyway is not restricted beyond its end date
    +        if earliest and self.date_end:
    +            earliest = min(earliest, self.date_end)
    +        return earliest
    +
    +    @api.model
    +    def _get_min_term_renewal_domain(self):
    +        return [
    +            ("min_term_notice_date", "<", fields.Date.context_today(self)),
    +            ("min_term_renewal_interval", ">", 0),
    +            ("is_canceled", "=", False),
    +            ("contract_id.is_terminated", "=", False),
    +        ]
    +
    +    @api.model
    +    def cron_renew_minimum_term(self):
    +        lines = self.search(self._get_min_term_renewal_domain())
    +        old_dates = {line: line.min_term_date_end for line in lines}
    +        # The dates depend on the current date: force their recomputation and
    +        # propagate the new values to the dependent fields (contract dates)
    +        fnames = ["min_term_date_end", "min_term_notice_date"]

    used MINIMUM_TERM_DATE_FIELDS

    wpichler wpichler commented on code on Oct 29, 2026 08:11 UTC
    Code Review
    contract_minimum_term/models/contract_minimum_term_mixin.py:1
    @@ -0,0 +1,115 @@
    +# Copyright 2026 Callino - Wolfgang Pichler
    +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
    +
    +from dateutil.relativedelta import relativedelta
    +
    +from odoo import api, fields, models
    +
    +MINIMUM_TERM_FIELDS = [
    +    "min_term_interval",
    +    "min_term_rule_type",
    +    "min_term_renewal_interval",
    +    "min_term_renewal_rule_type",
    +    "min_term_notice_interval",
    +    "min_term_notice_rule_type",
    +]
    +
    +RULE_TYPES = [
    +    ("daily", "Day(s)"),
    +    ("weekly", "Week(s)"),
    +    ("monthly", "Month(s)"),
    +    ("yearly", "Year(s)"),
    +]
    +
    +
    +class ContractMinimumTermMixin(models.AbstractModel):
    +    _name = "contract.minimum.term.mixin"

    we removed the extra fields

    wpichler wpichler commented on code on Oct 29, 2026 08:12 UTC
    Code Review
    product_contract_minimum_term/models/product_template.py:1
    @@ -0,0 +1,49 @@
    +# Copyright 2026 Callino - Wolfgang Pichler
    +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
    +
    +from odoo import api, fields, models
    +from odoo.exceptions import ValidationError
    +
    +CONTRACT_DURATION_TYPES = [
    +    ("fixed", "Fixed Duration"),
    +    ("minimum_term", "Minimum Term"),
    +]
    +
    +
    +class ProductTemplate(models.Model):
    +    _name = "product.template"
    +    _inherit = ["product.template", "contract.minimum.term.mixin"]
    +
    +    contract_duration_type = fields.Selection(
    +        CONTRACT_DURATION_TYPES,
    +        string="Contract Duration",
    +        default="fixed",
    +        required=True,
    +        help="Fixed Duration: the contract line ends after the duration, it can "
    +        "be renewed automatically.\n"
    +        "Minimum Term: the contract line is open-ended, it cannot be "
    +        "terminated before the end of the minimum term, which is extended by "
    +        "the renewal term if the contract is not terminated in time.",
    +    )
    +    min_term_rule_type = fields.Selection(default="monthly")
    +    min_term_renewal_rule_type = fields.Selection(default="monthly")

    done

    wpichler wpichler commented on code on Oct 29, 2026 08:14 UTC
    Code Review
    contract_minimum_term/views/contract_contract.xml:1
    @@ -0,0 +1,73 @@
    +<?xml version="1.0" encoding="utf-8" ?>
    +<!-- Copyright 2026 Callino - Wolfgang Pichler
    +     License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). -->
    +<odoo>
    +    <record model="ir.ui.view" id="contract_contract_form_view">
    +        <field name="model">contract.contract</field>
    +        <field name="inherit_id" ref="contract.contract_contract_form_view" />
    +        <field name="arch" type="xml">
    +            <xpath expr="//page[@name='invoicing']" position="after">
    +                <page string="Minimum Term" name="minimum_term">
    +                    <group name="minimum_term">
    +                        <group invisible="line_recurrence">
    +                            <label for="min_term_interval" />
    +                            <div class="o_row">
    +                                <field
    +                                    name="min_term_interval"
    +                                    class="oe_inline"
    +                                    readonly="is_terminated"
    +                                />
    +                                <field
    +                                    name="min_term_rule_type"
    +                                    class="oe_inline"
    +                                    readonly="is_terminated"
    +                                    required="min_term_interval"
    +                                />
    +                            </div>
    +                        </group>
    +                        <group>
    +                            <field name="min_term_date_end" />
    +                            <field name="min_term_notice_date" />

    done

    wpichler wpichler commented on code on Oct 29, 2026 08:16 UTC
    Code Review
    contract_minimum_term/models/contract_minimum_term_mixin.py:1
    @@ -0,0 +1,113 @@
    +# Copyright 2026 Callino - Wolfgang Pichler
    +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
    +
    +from dateutil.relativedelta import relativedelta
    +
    +from odoo import api, fields, models
    +
    +MINIMUM_TERM_FIELDS = [
    +    "min_term_interval",
    +    "min_term_rule_type",
    +]
    +
    +RULE_TYPES = [
    +    ("daily", "Day(s)"),
    +    ("weekly", "Week(s)"),
    +    ("monthly", "Month(s)"),
    +    ("yearly", "Year(s)"),
    +]
    +
    +
    +class ContractMinimumTermMixin(models.AbstractModel):
    +    """Minimum term of a contract (line).
    +
    +    The renewal of the minimum term and the termination notice reuse the
    +    fields of ``contract_line_successor``: ``is_auto_renew`` together with
    +    ``auto_renew_interval`` / ``auto_renew_rule_type`` renews the minimum
    +    term instead of an end date, and ``termination_notice_interval`` /
    +    ``termination_notice_rule_type`` is the notice to respect.
    +    """
    +
    +    _name = "contract.minimum.term.mixin"
    +    _description = "Contract Minimum Term Mixin"
    +
    +    min_term_interval = fields.Integer(
    +        string="Minimum Term",
    +        help="Initial minimum term, counted from the start date. The contract "
    +        "cannot be terminated before the end of this term. Leave 0 to disable "
    +        "the minimum term.",
    +    )
    +    min_term_rule_type = fields.Selection(
    +        RULE_TYPES,
    +        string="Minimum Term Type",
    +    )

    in next commit done

    wpichler wpichler commented on Oct 29, 2026 08:47 UTC
    Author

    @bosd i've looked at #1374 - yes - we should bundle effort here. I do plan to upgrade this module to v19 and v20 as soon as it is accepted here, and i would be happy if you continue to share your thoughts about it.

    About the Termination notice deadline - could get split into a separate module - but i am not a friend of having many small modules, so i left this functionality in here. And they share some common ground in date computation. Also i think that most of the users who need the termination minimum term will also need the notice deadline.

    About the great UI - i do have an other module for my customers in which i do make all the not needed fields invisible. I do agree with you - the UI should get polished. Maybe we should try to get this done in 20.0 Branch...

    About product_contract_minimum_term - this is for the case where a user does create a contract using a sale.order - not based on a template.

    wpichler wpichler commented on Oct 29, 2026 09:03 UTC
    Author

    @bosd About the missing button - i'ved tested it now again locally - here a screenshot
    <img width="2444" height="1170" alt="image" src="https://github.com/user-attachments/assets/d6486259-876a-4727-ae23-c56c1b0cca9b" />
    i will test it on runboat after the next commit - i've tested it now - you have to grant the terminate contract right to see the butto
    <img width="864" height="120" alt="image" src="https://github.com/user-attachments/assets/040289e3-b767-4809-b7ac-4ec34c70890e" />

    bosd bosd commented on Oct 29, 2026 10:29 UTC
    Reviewer

    Thanks for the rework, the reuse of is_auto_renew / termination_notice_* is much cleaner.

    Our use case for context: the client never wants a date_end on their contracts. They commit to a minimum term, then the contract continues silently until it is terminated, with no reminders or alerts when the term or notice deadline comes up. I checked the current head against that, and the model fits: the term renews without an end date and only leaves an internal note in the chatter, nothing is sent out. Two things get in the way though:

    1. The line form still requires an end date.
    contract_line_successor sets required="is_auto_renew" on date_end in the line form (contract_line_successor/views/contract_line.xml). This PR relaxes _check_allowed in Python but not that view attribute. So a line with a minimum term and auto renewal can't be saved from the form without entering an end date, which is exactly what the module is meant to avoid. The tests don't hit it because they work in Python, not through the form. Could the view make it required="is_auto_renew and not min_term_interval"?

    2. Editing date_end directly bypasses the minimum term.
    The check runs in the termination wizard, stop() and cancel(), but typing an earlier date_end on a line (in the form, the list, an import or over RPC) isn't checked at all. In our case date_end should stay empty, so any earlier value set there is a way around the commitment. A check in write (or a constraint) on contract.line.date_end, with the same exception for the early-termination group, would close it. #1374 hooks write/create for exactly this reason.

    With these two addressed I'm happy to approve. Once this lands I'll close #1374 in favour of a 19.0 port of this one.

    wpichler wpichler commented on Oct 29, 2026 10:43 UTC
    Author

    @bosd Thanks for checking it against your use case! Both are fixed:

    1. The line form now makes date_end required only for auto-renewing lines without a minimum term (is_auto_renew and not min_term_interval).

    2. write() on contract.line now runs the same check as stop() when date_end is set or moved earlier, with the same exception and chatter note for the early-termination group. Moving it later or clearing it isn't restricted. I left create() out on purpose: there is no commitment yet at creation, and planning successor lines creates lines with an end date.

    Tests added for both, including the form.

    bosd bosd commented on Oct 29, 2026 13:01 UTC
    Contributor

    Almost good to go,
    Please change your commit messages:

    Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
    

    to
    Assisted-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
    

    wpichler wpichler commented on Oct 29, 2026 14:59 UTC
    Author

    @bosd I've updated the commit messages

    bosd bosd approved these changes on Oct 29, 2026 15:01 UTC
    Approved Reviewer

    Approved these changes.

    wpichler wpichler commented on Oct 29, 2026 18:01 UTC
    Author

    Fixed the test failures with product_contract_section: product_contract_line_defer and product_contract_variable_quantity name the second argument of _prepare_contract_line_values differently, so it is passed positionally now (a2461c97).

    OCA-git-bot OCA-git-bot commented on Nov 28, 2026 19:00 UTC
    Bot

    This PR has the approved label and has been created more than 5 days ago. It should therefore be ready to merge by a maintainer (or a PSC member if the concerned addon has no declared maintainer). 🤖

    Assignees
    wpichler wpichler
    Labels
    risk: low confidence: 88.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 78.5% (HIGH) · Higher = larger blast radius
    78.5%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 88.5% · Lower = rushed or shallow review
    88.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 9% · Safe to merge
    9%
    Repository