Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[16.0][IMP] contract: Big changes in recurrency fields #1080

Open
wants to merge 1 commit into
base: 16.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions contract/models/abstract_contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,20 @@
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 = (

Check warning on line 97 in contract/models/abstract_contract.py

View check run for this annotation

Codecov / codecov/patch

contract/models/abstract_contract.py#L93-L97

Added lines #L93 - L97 were not covered by tests
self.recurring_invoicing_type
)
14 changes: 9 additions & 5 deletions contract/models/abstract_contract_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ class ContractAbstractContractLine(models.AbstractModel):
readonly=False,
copy=True,
)
date_end = fields.Date(
compute="_compute_date_end",
store=True,
readonly=False,
copy=True,
)
last_date_invoiced = fields.Date()
is_canceled = fields.Boolean(string="Canceled", default=False)
is_auto_renew = fields.Boolean(string="Auto Renew", default=False)
Expand Down Expand Up @@ -166,11 +172,9 @@ def _compute_recurring_interval(self):
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):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are you removing this?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @pedrobaeza. Sorry for the delay for the response.

I removed this because it's redundant. Since we are inheriting from contract.recurrency.mixin.basic in the contract.line, it's redundant having the same on the abstract.contract.line.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But this override was needed AFAIK.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mmmm i'm going to check it now, but one of the problems is that the contract never ends. This happens because this inherit.
On a first step, I created the _compute_date_end function below. This, trigger a chain of computes (next_period_date_start compute that triggers the recurring_next_date compute).
Then, I checked this function and I concluded that it was redundant and not necessary.

I've been working with this changed since I created the MR and it's working very well.

If u want, I can let function as it was initially since is not having any impact on the code.

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
9 changes: 0 additions & 9 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,14 +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",
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: 5 additions & 1 deletion contract/models/contract_recurrency_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ 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 +79,10 @@ 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",
store=True,
readonly=False,
copy=True,
)
date_end = fields.Date(index=True)
next_period_date_start = fields.Date(
Expand Down
1 change: 1 addition & 0 deletions contract/tests/test_contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ def test_automatic_price(self):
self.assertEqual(self.acct_line.price_unit, 10)

def test_contract(self):
self.assertEqual(self.acct_line.recurring_next_date, to_date("2018-01-15"))
self.assertEqual(self.contract.recurring_next_date, to_date("2018-01-15"))
self.assertAlmostEqual(self.acct_line.price_subtotal, 50.0)
self.acct_line.price_unit = 100.0
Expand Down
5 changes: 5 additions & 0 deletions contract/views/contract.xml
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,11 @@
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
Loading