Skip to content

[IMP] subscription_oca: centralize recurring date computation#1440

Open
alvaro-domatix wants to merge 1 commit into
OCA:19.0from
alvaro-domatix:19.0-imp-subscription_oca-recurrence-dates
Open

[IMP] subscription_oca: centralize recurring date computation#1440
alvaro-domatix wants to merge 1 commit into
OCA:19.0from
alvaro-domatix:19.0-imp-subscription_oca-recurrence-dates

Conversation

@alvaro-domatix
Copy link
Copy Markdown

Problem

The logic for recurring_next_date is scattered across onchange, write, create, generate_invoice, manual_invoice and the cron, with the increment based on the value stored in the record rather than on the date that was actually invoiced. This dispersion makes the behavior hard to review and easy to break, especially when reasoning about contract end dates and first-invoice dates.

Solution

Add five small, side-effect-free helpers on sale.subscription:

  • _get_recurrence_delta(): relativedelta for the template recurrence (days/weeks/months/years), honoring recurring_interval.
  • _get_first_invoice_date(): date_start when set, otherwise today.
  • _get_next_invoice_date(previous_date): previous_date + delta, with datetime/string normalization.
  • _set_next_invoice_date_after_invoice(invoice_date=None): advances recurring_next_date from the date that was actually invoiced.
  • _get_contract_end_date(): end date derived from the template, or False when the template is unlimited.

generate_invoice, manual_invoice and calculate_recurring_next_date are updated to use these helpers. The external behavior of the model is preserved: existing tests continue to pass and the new test module covers the helpers in isolation.

How to test

Run:

odoo-bin -d <db> --init=subscription_oca --stop-after-init
odoo-bin -d <db> -u subscription_oca --test-enable --test-tags=/subscription_oca --stop-after-init

The new tests/test_subscription_recurrence_dates.py covers:

  • Delta for daily / weekly / monthly / yearly templates, plus recurring_interval > 1.
  • First invoice date defaults to date_start when set.
  • Next invoice date advances by exactly one recurrence unit from a given previous date.
  • _set_next_invoice_date_after_invoice advances recurring_next_date by one recurrence unit.
  • Contract end date returns False for unlimited templates and date_start + duration for limited ones.

The recurring_next_date logic was duplicated across onchange, write,
create, generate_invoice, manual_invoice and the cron, with the
increment based on the recurring_next_date stored in the record rather
than on the date that was actually invoiced. This made changes hard to
review and easy to break.

Add five small, side-effect-free helpers on sale.subscription:

* _get_recurrence_delta: relativedelta for the template recurrence
  (days/weeks/months/years), honoring recurring_interval.
* _get_first_invoice_date: date_start when set, otherwise today.
* _get_next_invoice_date(previous_date): previous_date + delta, with
  datetime/string normalization.
* _set_next_invoice_date_after_invoice(invoice_date=None): advances
  recurring_next_date from the date that was actually invoiced.
* _get_contract_end_date: end date derived from the template, or False
  when the template is unlimited.

generate_invoice, manual_invoice and calculate_recurring_next_date are
updated to use these helpers. The external behavior of the model is
preserved: existing tests continue to pass and the new test module
covers the helpers in isolation.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants