Skip to content

Commit

Permalink
[IMP] Big changes in recurrency fields
Browse files Browse the repository at this point in the history
  • Loading branch information
flalexg committed Apr 24, 2024
1 parent 2b8ecce commit 5695352
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 74 deletions.
10 changes: 10 additions & 0 deletions contract/models/abstract_contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,13 @@ def _compute_journal_id(self):
contract.journal_id = journal.id
else:
contract.journal_id = None

@api.onchange('date_start', 'date_end', 'recurring_interval', 'recurring_rule_type', 'recurring_invoicing_type')
def _onchange_recurrency_fields(self):
if self.contract_line_fixed_ids:
self.contract_line_fixed_ids.date_start = self.date_start
self.contract_line_fixed_ids.date_end = self.date_end
self.contract_line_fixed_ids.recurring_interval = self.recurring_interval
self.contract_line_fixed_ids.recurring_rule_type = self.recurring_rule_type
self.contract_line_fixed_ids.recurring_invoicing_type = self.recurring_invoicing_type

21 changes: 15 additions & 6 deletions contract/models/abstract_contract_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,26 +64,37 @@ class ContractAbstractContractLine(models.AbstractModel):
readonly=False,
required=True,
copy=True,
precompute=True,
)
recurring_invoicing_type = fields.Selection(
compute="_compute_recurring_invoicing_type",
store=True,
readonly=False,
required=True,
copy=True,
precompute=True,
)
recurring_interval = fields.Integer(
compute="_compute_recurring_interval",
store=True,
readonly=False,
required=True,
copy=True,
precompute=True,
)
date_start = fields.Date(
compute="_compute_date_start",
store=True,
readonly=False,
copy=True,
precompute=True,
)
date_end = fields.Date(
compute="_compute_date_end",
store=True,
readonly=False,
copy=True,
precompute=True,
)
last_date_invoiced = fields.Date()
is_canceled = fields.Boolean(string="Canceled", default=False)
Expand Down Expand Up @@ -165,12 +176,10 @@ def _compute_recurring_interval(self):
@api.depends("contract_id.date_start", "contract_id.line_recurrence")
def _compute_date_start(self):
self._set_recurrence_field("date_start")

# pylint: disable=missing-return
@api.depends("contract_id.recurring_next_date", "contract_id.line_recurrence")
def _compute_recurring_next_date(self):
super()._compute_recurring_next_date()
self._set_recurrence_field("recurring_next_date")

@api.depends("contract_id.date_end", "contract_id.line_recurrence")
def _compute_date_end(self):
self._set_recurrence_field("date_end")

@api.depends("display_type", "note_invoicing_mode")
def _compute_is_recurring_note(self):
Expand Down
33 changes: 0 additions & 33 deletions contract/models/contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ class ContractContract(models.Model):
create_invoice_visibility = fields.Boolean(
compute="_compute_create_invoice_visibility"
)
date_end = fields.Date(compute="_compute_date_end", store=True, readonly=False)
payment_term_id = fields.Many2one(
comodel_name="account.payment.term", string="Payment Terms", index=True
)
Expand Down Expand Up @@ -302,38 +301,6 @@ def action_show_invoices(self):
action["views"] = [(tree_view.id, "tree"), (form_view.id, "form")]
return action

@api.depends("contract_line_ids.date_end")
def _compute_date_end(self):
for contract in self:
contract.date_end = False
date_end = contract.contract_line_ids.mapped("date_end")
if date_end and all(date_end):
contract.date_end = max(date_end)

@api.depends(
"contract_line_ids.recurring_next_date",
"contract_line_ids.is_canceled",
)
# pylint: disable=missing-return
def _compute_recurring_next_date(self):
for contract in self:
recurring_next_date = contract.contract_line_ids.filtered(
lambda l: (
l.recurring_next_date
and not l.is_canceled
and (not l.display_type or l.is_recurring_note)
)
).mapped("recurring_next_date")
# we give priority to computation from date_start if modified
if (
contract._origin
and contract._origin.date_start != contract.date_start
or not recurring_next_date
):
super(ContractContract, contract)._compute_recurring_next_date()
else:
contract.recurring_next_date = min(recurring_next_date)

@api.depends("contract_line_ids.create_invoice_visibility")
def _compute_create_invoice_visibility(self):
for contract in self:
Expand Down
31 changes: 0 additions & 31 deletions contract/models/contract_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ class ContractLine(models.Model):
)
currency_id = fields.Many2one(related="contract_id.currency_id")
date_start = fields.Date(required=True)
date_end = fields.Date(compute="_compute_date_end", store=True, readonly=False)
termination_notice_date = fields.Date(
compute="_compute_termination_notice_date",
store=True,
Expand Down Expand Up @@ -100,36 +99,6 @@ class ContractLine(models.Model):
readonly=True,
)

@api.depends(
"last_date_invoiced",
"date_start",
"date_end",
"contract_id.last_date_invoiced",
"contract_id.contract_line_ids.last_date_invoiced",
)
# pylint: disable=missing-return
def _compute_next_period_date_start(self):
"""Rectify next period date start if another line in the contract has been
already invoiced previously when the recurrence is by contract.
"""
rest = self.filtered(lambda x: x.contract_id.line_recurrence)
for rec in self - rest:
lines = rec.contract_id.contract_line_ids
if not rec.last_date_invoiced and any(lines.mapped("last_date_invoiced")):
next_period_date_start = max(
lines.filtered("last_date_invoiced").mapped("last_date_invoiced")
) + relativedelta(days=1)
if rec.date_end and next_period_date_start > rec.date_end:
next_period_date_start = False
rec.next_period_date_start = next_period_date_start
else:
rest |= rec
super(ContractLine, rest)._compute_next_period_date_start()

@api.depends("contract_id.date_end", "contract_id.line_recurrence")
def _compute_date_end(self):
self._set_recurrence_field("date_end")

@api.depends(
"date_end",
"termination_notice_rule_type",
Expand Down
6 changes: 4 additions & 2 deletions contract/models/contract_recurrency_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ class ContractRecurrencyBasicMixin(models.AbstractModel):
help="Invoice every (Days/Week/Month/Year)",
)
date_start = fields.Date()
date_end = fields.Date()

recurring_next_date = fields.Date(string="Date of Next Invoice")

@api.depends("recurring_invoicing_type", "recurring_rule_type")
Expand Down Expand Up @@ -78,7 +80,7 @@ class ContractRecurrencyMixin(models.AbstractModel):

date_start = fields.Date(default=lambda self: fields.Date.context_today(self))
recurring_next_date = fields.Date(
compute="_compute_recurring_next_date", store=True, readonly=False, copy=True
compute="_compute_recurring_next_date", precompute=True, store=True, readonly=False, copy=True
)
date_end = fields.Date(index=True)
next_period_date_start = fields.Date(
Expand All @@ -89,7 +91,7 @@ class ContractRecurrencyMixin(models.AbstractModel):
string="Next Period End",
compute="_compute_next_period_date_end",
)
last_date_invoiced = fields.Date(readonly=True, copy=False)
last_date_invoiced = fields.Date(copy=False)

@api.depends("next_period_date_start")
def _compute_recurring_next_date(self):
Expand Down
7 changes: 5 additions & 2 deletions contract/views/contract.xml
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@
<field name="currency_id" invisible="1" />
<field name="display_type" invisible="1" />
<field name="sequence" widget="handle" />
<field name="product_id" />
<field name="product_id" force_save="1" />
<field
name="product_uom_category_id"
invisible="1"
Expand Down Expand Up @@ -249,11 +249,14 @@
/>
<field name="recurring_next_date" invisible="1" />
<field name="date_start" invisible="1" />
<field name="date_end" />
<field name="date_end" invisible="1" />
<field
name="last_date_invoiced"
groups="base.group_no_one"
/>
<field name="next_period_date_start" invisible="1" />
<field name="next_period_date_end" invisible="1" />

<field
name="create_invoice_visibility"
invisible="1"
Expand Down

0 comments on commit 5695352

Please sign in to comment.