[18.0][ADD] contract_minimum_term, product_contract_minimum_term #1528
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.
def _cron_renew_minimum_term(self):
maybe separate that out into a hook?
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?
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.
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..?
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.
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 therenewal of the minimum term
termination_notice_interval / termination_notice_rule_type (also alreadythere) 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_endA 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 theirmin_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 oftermination_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 fromcontract_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 incontract_minimum_term as RULE_TYPES; the lines you pointed at only set a
different default. If you meant something else there, let me know.
Rework pushed (force-push, still two commits, one per module).
Renewal and notice now reuse the existing fields. min_term_renewal_ andmin_term_notice_ are gone. What is left as new configuration ismin_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 linewith a minimum term these renew the minimum term, not an end date.
termination_notice_interval / termination_notice_rule_type: the noticeto 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 anauto-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_allowedevaluates 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
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, ora 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 onlycarries the "not started yet" rule.
min_term_skip_checks for modules that replace a line by asuccessor instead of ending it.
product_contract_minimum_term uses it forthe upsell/downsell of
product_contract, where the replacement line carriesthe commitment on.
24 + 6 tests, everything green together with contract, contract_line_successor,contract_termination and product_contract (174).
🤖 Generated with Claude Code
please review what you push manually
"A contract line with a successor can't be set to auto-renew"
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.
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 ?
@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?
Also might want to consider using the widgets on the fields. e.g. 3 days left before min termination date.
<field name="min_term_date_end" widget="remaining_days" />
<field name="min_term_notice_date" widget="remaining_days" />
<field name="min_term_date_end" widget="remaining_days" />
<field name="min_term_notice_date" widget="remaining_days" />
<field name="min_term_date_end" widget="remaining_days" optional="show" />
<field name="min_term_notice_date" widget="remaining_days" optional="hide" />
<field name="min_term_date_end" widget="remaining_days" optional="show" />
<field name="min_term_notice_date" widget="remaining_days" optional="hide" />
<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>
min_term_rule_type = fields.Selection(
RULE_TYPES,
string="Minimum Term Type",
default="monthly"
)
done
done
used MINIMUM_TERM_DATE_FIELDS
we removed the extra fields
done
done
in next commit done
@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.
@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" />
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.
@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.
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>
@bosd I've updated the commit messages
Approved these changes.
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).
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). 🤖
tobiaszehntner
wpichler
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 oncontract_termination.product_contract_minimum_term: glue module withproduct_contract, so the minimum term can be defined on the product and sold through sale orders. It isauto_install.Why
In the contract modules, a commitment can only be modelled with a fixed end date (
date_end), optionally renewed automatically (is_auto_renewincontract_line_successor, "Duration" inproduct_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, nodate_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_terminationalready has the right place to enforce it.contract_minimum_termConfiguration: 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 whenline_recurrenceis set, and taken from the contract template and template lines.The renewal and the notice reuse the existing fields of
contract_line_successor: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_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_allowedno longer requires an end date on them when they auto renew (also in the line form), and_compute_allowedtreats them like open-ended lines, whichget_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 atmin_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_terminationwizard):_terminate_contractrefuses 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.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 ofdate_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.min_term_skip_checkslets 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_termOn a contract product, a radio button chooses how the contract duration is defined. The two options are exclusive:
product_contractbehavior (duration →date_end, optional auto renewal). This is the default, so existing products are not affected.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, withoutdate_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.contract,contract_line_successor,contract_terminationandproduct_contractpass 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: