mirror of
https://github.com/OCA/contract.git
synced 2025-02-13 17:57:24 +02:00
[REF+FIX+IMP] contract: Several refactorings + fixes + imps
- REF: Refactor _update_recurring_next_date Reuse the logic that is now fully located in _get_recurring_next_date. - REF: re-add _compute_first_recurring_next_date for backward compatibility - FIX: add missing dependency in computed field - REF: remove one monthlylastday special case get_relative_delta now works the same for all recurring rules. Move the special case handling to _init_last_date_invoiced which is used only for migration. - IMP: support pre-paid for monthlylastday monthlylastday is (almost) not a special case anymore \o/. montlylastday is simply a montly period where the periods are aligned on month boundaries. The last bit of special casing is that postpaid generates invoice the day after the last dasy of the period, except for monthlylastday where the invoice is generated on the last day of the period. This last exception will disappear when we put the offset under user control. This is a breaking change because the post-paid/pre-paid mode becomes relevant for monthlylastday invoicing. The field becomes visible in the UI. Code that generate monthlylastday contract lines must now correctly set the pre-paid/post-paid mode too. Some tests have had to be adapted to reflect that. - REF: make recurring_invoicing_offset a computed field In preparation to making it user modifiable. - REF: make get_next_period_date_end public Make it public because it is the core logic of the module. Also, clarify that recurring_invoicing_type and recurring_invoicing_offset are needed only when we want the next period to be computed from a user chosen next invoice date. - REF: rename _get_recurring_next_date as get_next_invoice_date It is easier to understand. Also make it public.
This commit is contained in:
committed by
Pedro M. Baeza
parent
04bd78f16d
commit
3afddeec07
@@ -70,9 +70,20 @@ class ContractAbstractContractLine(models.AbstractModel):
|
||||
[('pre-paid', 'Pre-paid'), ('post-paid', 'Post-paid')],
|
||||
default='pre-paid',
|
||||
string='Invoicing type',
|
||||
help="Specify if process date is 'from' or 'to' invoicing date",
|
||||
help=(
|
||||
"Specify if the invoice must be generated at the beginning "
|
||||
"(pre-paid) or end (post-paid) of the period."
|
||||
),
|
||||
required=True,
|
||||
)
|
||||
recurring_invoicing_offset = fields.Integer(
|
||||
compute="_compute_recurring_invoicing_offset",
|
||||
string="Invoicing offset",
|
||||
help=(
|
||||
"Number of days to offset the invoice from the period end "
|
||||
"date (in post-paid mode) or beginning date (in pre-paid mode)."
|
||||
)
|
||||
)
|
||||
recurring_interval = fields.Integer(
|
||||
default=1,
|
||||
string='Invoice Every',
|
||||
@@ -115,6 +126,27 @@ class ContractAbstractContractLine(models.AbstractModel):
|
||||
ondelete='cascade',
|
||||
)
|
||||
|
||||
@api.model
|
||||
def _get_default_recurring_invoicing_offset(
|
||||
self, recurring_invoicing_type, recurring_rule_type
|
||||
):
|
||||
if (
|
||||
recurring_invoicing_type == 'pre-paid'
|
||||
or recurring_rule_type == 'monthlylastday'
|
||||
):
|
||||
return 0
|
||||
else:
|
||||
return 1
|
||||
|
||||
@api.depends('recurring_invoicing_type', 'recurring_rule_type')
|
||||
def _compute_recurring_invoicing_offset(self):
|
||||
for rec in self:
|
||||
rec.recurring_invoicing_offset = (
|
||||
self._get_default_recurring_invoicing_offset(
|
||||
rec.recurring_invoicing_type, rec.recurring_rule_type
|
||||
)
|
||||
)
|
||||
|
||||
@api.depends(
|
||||
'automatic_price',
|
||||
'specific_price',
|
||||
|
||||
@@ -364,42 +364,74 @@ class ContractLine(models.Model):
|
||||
)
|
||||
|
||||
@api.model
|
||||
def _get_recurring_next_date(
|
||||
def _compute_first_recurring_next_date(
|
||||
self,
|
||||
date_start,
|
||||
recurring_invoicing_type,
|
||||
recurring_rule_type,
|
||||
recurring_interval
|
||||
):
|
||||
# deprecated method for backward compatibility
|
||||
return self.get_next_invoice_date(
|
||||
date_start,
|
||||
recurring_invoicing_type,
|
||||
self._get_default_recurring_invoicing_offset(
|
||||
recurring_invoicing_type, recurring_rule_type
|
||||
),
|
||||
recurring_rule_type,
|
||||
recurring_interval,
|
||||
max_date_end=False,
|
||||
)
|
||||
|
||||
@api.model
|
||||
def get_next_invoice_date(
|
||||
self,
|
||||
next_period_date_start,
|
||||
recurring_invoicing_type,
|
||||
recurring_invoicing_offset,
|
||||
recurring_rule_type,
|
||||
recurring_interval,
|
||||
max_date_end,
|
||||
):
|
||||
next_period_date_end = self._get_next_period_date_end(
|
||||
next_period_date_end = self.get_next_period_date_end(
|
||||
next_period_date_start,
|
||||
recurring_invoicing_type,
|
||||
recurring_rule_type,
|
||||
recurring_interval,
|
||||
max_date_end=max_date_end,
|
||||
)
|
||||
if not next_period_date_end:
|
||||
return False
|
||||
if recurring_rule_type == 'monthlylastday':
|
||||
recurring_next_date = next_period_date_end
|
||||
elif recurring_invoicing_type == 'pre-paid':
|
||||
recurring_next_date = next_period_date_start
|
||||
if recurring_invoicing_type == 'pre-paid':
|
||||
recurring_next_date = (
|
||||
next_period_date_start
|
||||
+ relativedelta(days=recurring_invoicing_offset)
|
||||
)
|
||||
else: # post-paid
|
||||
recurring_next_date = next_period_date_end + relativedelta(days=1)
|
||||
recurring_next_date = (
|
||||
next_period_date_end
|
||||
+ relativedelta(days=recurring_invoicing_offset)
|
||||
)
|
||||
return recurring_next_date
|
||||
|
||||
@api.model
|
||||
def _get_next_period_date_end(
|
||||
def get_next_period_date_end(
|
||||
self,
|
||||
next_period_date_start,
|
||||
recurring_invoicing_type,
|
||||
recurring_rule_type,
|
||||
recurring_interval,
|
||||
max_date_end,
|
||||
next_invoice_date=False,
|
||||
recurring_invoicing_type=False,
|
||||
recurring_invoicing_offset=False,
|
||||
):
|
||||
"""Compute the end date for the next period"""
|
||||
"""Compute the end date for the next period.
|
||||
|
||||
The next period normally depends on recurrence options only.
|
||||
It is however possible to provide it a next invoice date, in
|
||||
which case this method can adjust the next period based on that
|
||||
too. In that scenario it required the invoicing type and offset
|
||||
arguments.
|
||||
"""
|
||||
if not next_period_date_start:
|
||||
return False
|
||||
if max_date_end and next_period_date_start > max_date_end:
|
||||
@@ -407,36 +439,28 @@ class ContractLine(models.Model):
|
||||
return False
|
||||
if not next_invoice_date:
|
||||
# regular algorithm
|
||||
if recurring_rule_type == 'monthlylastday':
|
||||
next_period_date_end = (
|
||||
next_period_date_start
|
||||
+ self.get_relative_delta(
|
||||
recurring_rule_type, recurring_interval - 1
|
||||
)
|
||||
)
|
||||
else:
|
||||
next_period_date_end = (
|
||||
next_period_date_start
|
||||
+ self.get_relative_delta(
|
||||
recurring_rule_type, recurring_interval
|
||||
)
|
||||
- relativedelta(days=1)
|
||||
next_period_date_end = (
|
||||
next_period_date_start
|
||||
+ self.get_relative_delta(
|
||||
recurring_rule_type, recurring_interval
|
||||
)
|
||||
- relativedelta(days=1)
|
||||
)
|
||||
else:
|
||||
# special algorithm when the next invoice date is forced
|
||||
if recurring_rule_type == 'monthlylastday':
|
||||
next_period_date_end = next_invoice_date
|
||||
elif recurring_invoicing_type == 'pre-paid':
|
||||
if recurring_invoicing_type == 'pre-paid':
|
||||
next_period_date_end = (
|
||||
next_invoice_date
|
||||
- relativedelta(days=recurring_invoicing_offset)
|
||||
+ self.get_relative_delta(
|
||||
recurring_rule_type, recurring_interval
|
||||
)
|
||||
- relativedelta(days=1)
|
||||
)
|
||||
else: # post-paid
|
||||
next_period_date_end = next_invoice_date - relativedelta(
|
||||
days=1
|
||||
next_period_date_end = (
|
||||
next_invoice_date
|
||||
- relativedelta(days=recurring_invoicing_offset)
|
||||
)
|
||||
if max_date_end and next_period_date_end > max_date_end:
|
||||
# end date is past max_date_end: trim it
|
||||
@@ -459,19 +483,22 @@ class ContractLine(models.Model):
|
||||
@api.depends(
|
||||
'next_period_date_start',
|
||||
'recurring_invoicing_type',
|
||||
'recurring_invoicing_offset',
|
||||
'recurring_rule_type',
|
||||
'recurring_interval',
|
||||
'date_end',
|
||||
'recurring_next_date',
|
||||
)
|
||||
def _compute_next_period_date_end(self):
|
||||
for rec in self:
|
||||
rec.next_period_date_end = self._get_next_period_date_end(
|
||||
rec.next_period_date_end = self.get_next_period_date_end(
|
||||
rec.next_period_date_start,
|
||||
rec.recurring_invoicing_type,
|
||||
rec.recurring_rule_type,
|
||||
rec.recurring_interval,
|
||||
max_date_end=rec.date_end,
|
||||
next_invoice_date=rec.recurring_next_date,
|
||||
recurring_invoicing_type=rec.recurring_invoicing_type,
|
||||
recurring_invoicing_offset=rec.recurring_invoicing_offset,
|
||||
)
|
||||
|
||||
@api.model
|
||||
@@ -512,9 +539,10 @@ class ContractLine(models.Model):
|
||||
)
|
||||
def _onchange_date_start(self):
|
||||
for rec in self.filtered('date_start'):
|
||||
rec.recurring_next_date = self._get_recurring_next_date(
|
||||
rec.recurring_next_date = self.get_next_invoice_date(
|
||||
rec.date_start,
|
||||
rec.recurring_invoicing_type,
|
||||
rec.recurring_invoicing_offset,
|
||||
rec.recurring_rule_type,
|
||||
rec.recurring_interval,
|
||||
max_date_end=rec.date_end,
|
||||
@@ -647,13 +675,14 @@ class ContractLine(models.Model):
|
||||
if last_date_invoiced
|
||||
else self.date_start
|
||||
)
|
||||
last_date_invoiced = self._get_next_period_date_end(
|
||||
last_date_invoiced = self.get_next_period_date_end(
|
||||
first_date_invoiced,
|
||||
self.recurring_invoicing_type,
|
||||
self.recurring_rule_type,
|
||||
self.recurring_interval,
|
||||
max_date_end=(self.date_end if stop_at_date_end else False),
|
||||
next_invoice_date=recurring_next_date,
|
||||
recurring_invoicing_type=self.recurring_invoicing_type,
|
||||
recurring_invoicing_offset=self.recurring_invoicing_offset,
|
||||
)
|
||||
return first_date_invoiced, last_date_invoiced, recurring_next_date
|
||||
|
||||
@@ -675,23 +704,19 @@ class ContractLine(models.Model):
|
||||
@api.multi
|
||||
def _update_recurring_next_date(self):
|
||||
for rec in self:
|
||||
old_date = rec.recurring_next_date
|
||||
new_date = old_date + self.get_relative_delta(
|
||||
rec.recurring_rule_type, rec.recurring_interval
|
||||
last_date_invoiced = rec.next_period_date_end
|
||||
recurring_next_date = rec.get_next_invoice_date(
|
||||
last_date_invoiced + relativedelta(days=1),
|
||||
rec.recurring_invoicing_type,
|
||||
rec.recurring_invoicing_offset,
|
||||
rec.recurring_rule_type,
|
||||
rec.recurring_interval,
|
||||
max_date_end=rec.date_end,
|
||||
)
|
||||
if rec.recurring_rule_type == 'monthlylastday':
|
||||
last_date_invoiced = old_date
|
||||
elif rec.recurring_invoicing_type == 'post-paid':
|
||||
last_date_invoiced = old_date - relativedelta(days=1)
|
||||
elif rec.recurring_invoicing_type == 'pre-paid':
|
||||
last_date_invoiced = new_date - relativedelta(days=1)
|
||||
|
||||
if rec.date_end and last_date_invoiced >= rec.date_end:
|
||||
rec.last_date_invoiced = rec.date_end
|
||||
rec.recurring_next_date = False
|
||||
else:
|
||||
rec.last_date_invoiced = last_date_invoiced
|
||||
rec.recurring_next_date = new_date
|
||||
rec.write({
|
||||
"recurring_next_date": recurring_next_date,
|
||||
"last_date_invoiced": last_date_invoiced,
|
||||
})
|
||||
|
||||
@api.multi
|
||||
def _init_last_date_invoiced(self):
|
||||
@@ -704,8 +729,9 @@ class ContractLine(models.Model):
|
||||
last_date_invoiced = (
|
||||
rec.recurring_next_date
|
||||
- self.get_relative_delta(
|
||||
rec.recurring_rule_type, rec.recurring_interval
|
||||
rec.recurring_rule_type, rec.recurring_interval - 1
|
||||
)
|
||||
- relativedelta(days=1)
|
||||
)
|
||||
elif rec.recurring_invoicing_type == 'post-paid':
|
||||
last_date_invoiced = (
|
||||
@@ -713,12 +739,18 @@ class ContractLine(models.Model):
|
||||
- self.get_relative_delta(
|
||||
rec.recurring_rule_type, rec.recurring_interval
|
||||
)
|
||||
) - relativedelta(days=1)
|
||||
- relativedelta(days=1)
|
||||
)
|
||||
if last_date_invoiced > rec.date_start:
|
||||
rec.last_date_invoiced = last_date_invoiced
|
||||
|
||||
@api.model
|
||||
def get_relative_delta(self, recurring_rule_type, interval):
|
||||
"""Return a relativedelta for one period.
|
||||
|
||||
When added to the first day of the period,
|
||||
it gives the first day of the next period.
|
||||
"""
|
||||
if recurring_rule_type == 'daily':
|
||||
return relativedelta(days=interval)
|
||||
elif recurring_rule_type == 'weekly':
|
||||
@@ -726,7 +758,7 @@ class ContractLine(models.Model):
|
||||
elif recurring_rule_type == 'monthly':
|
||||
return relativedelta(months=interval)
|
||||
elif recurring_rule_type == 'monthlylastday':
|
||||
return relativedelta(months=interval, day=31)
|
||||
return relativedelta(months=interval, day=1)
|
||||
else:
|
||||
return relativedelta(years=interval)
|
||||
|
||||
@@ -750,9 +782,10 @@ class ContractLine(models.Model):
|
||||
new_date_end = rec.date_end + delay_delta
|
||||
else:
|
||||
new_date_end = False
|
||||
new_recurring_next_date = self._get_recurring_next_date(
|
||||
new_recurring_next_date = self.get_next_invoice_date(
|
||||
new_date_start,
|
||||
rec.recurring_invoicing_type,
|
||||
rec.recurring_invoicing_offset,
|
||||
rec.recurring_rule_type,
|
||||
rec.recurring_interval,
|
||||
max_date_end=new_date_end
|
||||
@@ -814,9 +847,10 @@ class ContractLine(models.Model):
|
||||
):
|
||||
self.ensure_one()
|
||||
if not recurring_next_date:
|
||||
recurring_next_date = self._get_recurring_next_date(
|
||||
recurring_next_date = self.get_next_invoice_date(
|
||||
date_start,
|
||||
self.recurring_invoicing_type,
|
||||
self.recurring_invoicing_offset,
|
||||
self.recurring_rule_type,
|
||||
self.recurring_interval,
|
||||
max_date_end=date_end,
|
||||
|
||||
Reference in New Issue
Block a user