Skip to content

Commit

Permalink
[FIX] product_contract: Compute date_start and date_end correctly. Ma…
Browse files Browse the repository at this point in the history
…ke fields computed
  • Loading branch information
sergio-teruel committed Jan 24, 2025
1 parent 891236a commit 90dcd82
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 42 deletions.
1 change: 1 addition & 0 deletions product_contract/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ Contributors
- Ernesto Tejeda
- Pedro M. Baeza
- Carlos Roca
- Sergio Teruel

- David Jaen <[email protected]>

Expand Down
95 changes: 59 additions & 36 deletions product_contract/models/sale_order_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,16 @@ class SaleOrderLine(models.Model):
recurring_invoicing_type = fields.Selection(
related="product_id.recurring_invoicing_type"
)
date_start = fields.Date()
date_end = fields.Date()

date_start = fields.Date(compute="_compute_date_start", readonly=False, store=True)
date_end = fields.Date(compute="_compute_date_end", readonly=False, store=True)
contract_line_id = fields.Many2one(
comodel_name="contract.line",
string="Contract Line to replace",
required=False,
copy=False,
)
is_auto_renew = fields.Boolean(
string="Auto Renew",
compute="_compute_auto_renew",
default=False,
store=True,
readonly=False,
)
Expand Down Expand Up @@ -93,47 +90,73 @@ def _compute_contract_template_id(self):
rec.order_id.company_id
).property_contract_template_id

@api.depends("product_id")
def _compute_date_start(self):
for sol in self:
if sol.contract_start_date_method == "start_this":
sol.date_start = sol.order_id.date_order.replace(day=1)

Check warning on line 97 in product_contract/models/sale_order_line.py

View check run for this annotation

Codecov / codecov/patch

product_contract/models/sale_order_line.py#L97

Added line #L97 was not covered by tests
elif sol.contract_start_date_method == "end_this":
sol.date_start = (

Check warning on line 99 in product_contract/models/sale_order_line.py

View check run for this annotation

Codecov / codecov/patch

product_contract/models/sale_order_line.py#L99

Added line #L99 was not covered by tests
sol.order_id.date_order
+ self.get_relative_delta(
sol.recurring_rule_type, sol.product_id.default_qty
)
).replace(day=1) - relativedelta(days=1)
elif sol.contract_start_date_method == "start_next":
# Dia 1 del siguiente recurring_rule_type
sol.date_start = (

Check warning on line 107 in product_contract/models/sale_order_line.py

View check run for this annotation

Codecov / codecov/patch

product_contract/models/sale_order_line.py#L107

Added line #L107 was not covered by tests
sol.order_id.date_order
+ self.get_relative_delta(
sol.recurring_rule_type, sol.product_id.default_qty
)
).replace(day=1)
elif sol.contract_start_date_method == "end_next":
# Last day of next recurring period
sol.date_start = (

Check warning on line 115 in product_contract/models/sale_order_line.py

View check run for this annotation

Codecov / codecov/patch

product_contract/models/sale_order_line.py#L115

Added line #L115 was not covered by tests
sol.order_id.date_order
+ self.get_relative_delta(
sol.recurring_rule_type, sol.product_id.default_qty + 1
)
).replace(day=1) - relativedelta(days=1)
else:
# Manual method
sol.date_start = False

@api.depends(
"is_auto_renew",
"date_start",
"auto_renew_interval",
"auto_renew_rule_type",
)
def _compute_date_end(self):
for sol in self:
if sol.is_auto_renew and sol.date_start:
sol.date_end = self.env["contract.line"]._get_first_date_end(
sol.date_start, sol.auto_renew_rule_type, sol.auto_renew_interval
)
else:
sol.date_end = False

@api.model
def get_relative_delta(self, recurring_rule_type, interval):
return self.env["contract.recurrency.mixin"].get_relative_delta(

Check warning on line 142 in product_contract/models/sale_order_line.py

View check run for this annotation

Codecov / codecov/patch

product_contract/models/sale_order_line.py#L142

Added line #L142 was not covered by tests
recurring_rule_type, interval
)

def _get_auto_renew_rule_type(self):
"""monthly last day don't make sense for auto_renew_rule_type"""
self.ensure_one()
if self.recurring_rule_type == "monthlylastday":
return "monthly"
return self.recurring_rule_type

def _get_date_end(self):
self.ensure_one()
contract_start_date_method = self.product_id.contract_start_date_method
date_end = False
if contract_start_date_method == "manual":
contract_line_model = self.env["contract.line"]
date_end = (
self.date_start
+ contract_line_model.get_relative_delta(
self._get_auto_renew_rule_type(),
int(self.product_uom_qty),
)
- relativedelta(days=1)
)
return date_end

@api.depends("product_id")
def _compute_auto_renew(self):
for rec in self:
if rec.product_id.is_contract:
rec.product_uom_qty = rec.product_id.default_qty
contract_start_date_method = rec.product_id.contract_start_date_method
if contract_start_date_method == "manual":
rec.date_start = rec.date_start or fields.Date.today()
rec.date_end = rec._get_date_end()
rec.is_auto_renew = rec.product_id.is_auto_renew
if rec.is_auto_renew:
rec.auto_renew_interval = rec.product_id.auto_renew_interval
rec.auto_renew_rule_type = rec.product_id.auto_renew_rule_type

@api.onchange("date_start", "product_uom_qty")
def onchange_date_start(self):
for rec in self.filtered("product_id.is_contract"):
rec.date_end = rec._get_date_end() if rec.date_start else False
rec.product_uom_qty = rec.product_id.default_qty
rec.is_auto_renew = rec.product_id.is_auto_renew
rec.auto_renew_interval = rec.product_id.auto_renew_interval
rec.auto_renew_rule_type = rec.product_id.auto_renew_rule_type

def _get_contract_line_qty(self):
"""Returns the amount that will be placed in new contract lines."""
Expand Down
1 change: 1 addition & 0 deletions product_contract/readme/CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
- Ernesto Tejeda
- Pedro M. Baeza
- Carlos Roca
- Sergio Teruel
- David Jaen \<<[email protected]>\>
1 change: 1 addition & 0 deletions product_contract/static/description/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,7 @@ <h2><a class="toc-backref" href="#toc-entry-6">Contributors</a></h2>
<li>Ernesto Tejeda</li>
<li>Pedro M. Baeza</li>
<li>Carlos Roca</li>
<li>Sergio Teruel</li>
</ul>
</li>
<li>David Jaen &lt;<a class="reference external" href="mailto:david.jaen.revert&#64;gmail.com">david.jaen.revert&#64;gmail.com</a>&gt;</li>
Expand Down
8 changes: 4 additions & 4 deletions product_contract/tests/test_sale_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def test_compute_is_contract(self):
def test_action_confirm(self):
"""It should create a contract for each contract template used in
order_line"""
self.order_line1._compute_auto_renew()
self.order_line1.is_auto_renew = True
self.sale.action_confirm()
contracts = self.sale.order_line.mapped("contract_id")
self.assertEqual(len(contracts), 2)
Expand Down Expand Up @@ -155,7 +155,7 @@ def test_action_confirm_without_contract_creation(self):
"""It should create a contract for each contract template used in
order_line"""
self.sale.company_id.create_contract_at_sale_order_confirmation = False
self.order_line1._compute_auto_renew()
self.order_line1.is_auto_renew = True
self.sale.action_confirm()
self.assertEqual(len(self.sale.order_line.mapped("contract_id")), 0)
self.assertTrue(self.sale.need_contract_creation)
Expand All @@ -174,14 +174,14 @@ def test_action_confirm_without_contract_creation(self):
def test_sale_contract_count(self):
"""It should count contracts as many different contract template used
in order_line"""
self.order_line1._compute_auto_renew()
self.order_line1.is_auto_renew = True
self.sale.action_confirm()
self.assertEqual(self.sale.contract_count, 2)

def test_onchange_product(self):
"""It should get recurrence invoicing info to the sale line from
its product"""
self.order_line1._compute_auto_renew()
self.order_line1.is_auto_renew = True
self.assertEqual(
self.order_line1.recurring_rule_type,
self.product1.recurring_rule_type,
Expand Down
4 changes: 2 additions & 2 deletions product_contract/wizards/product_contract_configurator.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@ def _compute_auto_renew(self):
def _get_auto_renew_rule_type(self):
"""monthly last day don't make sense for auto_renew_rule_type"""
self.ensure_one()
if self.recurring_rule_type == "monthlylastday":
if self.auto_renew_rule_type == "monthlylastday":
return "monthly"
return self.recurring_rule_type
return self.auto_renew_rule_type

Check warning on line 93 in product_contract/wizards/product_contract_configurator.py

View check run for this annotation

Codecov / codecov/patch

product_contract/wizards/product_contract_configurator.py#L93

Added line #L93 was not covered by tests

def _get_date_end(self):
self.ensure_one()
Expand Down

0 comments on commit 90dcd82

Please sign in to comment.