[REF] - code refactoring + improve unit tests

This commit is contained in:
sbejaoui
2020-05-15 19:51:00 +02:00
parent ebd1ed9fa3
commit dd5775e7d4
2 changed files with 60 additions and 35 deletions

View File

@@ -7,39 +7,52 @@ from odoo import api, models
class ContractLine(models.Model):
_inherit = "contract.line"
@api.multi
def compute_prorated(
self, period_first_date, period_last_date, invoice_date
):
self.ensure_one()
return self._compute_prorated(
period_first_date,
period_last_date,
invoice_date,
self.recurring_rule_type,
self.recurring_interval,
self.recurring_invoicing_type,
)
@api.model
def _compute_prorated(
self, real_next_date, real_last_date, theoretical_next_date,
theoretical_last_date):
self,
period_first_date,
period_last_date,
invoice_date,
recurring_rule_type,
recurring_interval,
recurring_invoicing_type,
):
def _invoiced_days(next_date, last_date):
return (next_date - last_date).days + 1
return _invoiced_days(real_next_date, real_last_date) / _invoiced_days(
theoretical_next_date, theoretical_last_date
)
@api.multi
def compute_prorated(self, period_first_date, period_last_date,
invoice_date):
self.ensure_one()
relative_delta = self.get_relative_delta(
self.recurring_rule_type, self.recurring_interval
recurring_rule_type, recurring_interval
)
theoretical_next_date = invoice_date
if self.recurring_rule_type == "monthlylastday":
relative_delta = self.get_relative_delta("monthly",
self.recurring_interval)
theoretical_next_date += self.get_relative_delta("daily", 1)
if (
self.recurring_invoicing_type == "pre-paid"
and self.recurring_rule_type != "monthlylastday"
recurring_rule_type == "monthlylastday"
and recurring_invoicing_type == "post-paid"
):
relative_delta = self.get_relative_delta(
"monthly", recurring_interval
)
theoretical_next_date += self.get_relative_delta("daily", 1)
if recurring_invoicing_type == "pre-paid":
theoretical_next_date += relative_delta
theoretical_last_date = theoretical_next_date - relative_delta
theoretical_next_date -= self.get_relative_delta("daily", 1)
real_last_date = period_first_date
real_next_date = period_last_date
return self._compute_prorated(
real_next_date, real_last_date, theoretical_next_date,
theoretical_last_date
return _invoiced_days(real_next_date, real_last_date) / _invoiced_days(
theoretical_next_date, theoretical_last_date
)

View File

@@ -1,6 +1,7 @@
# Copyright 2018 ACSONE SA/NV.
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from odoo import fields
from odoo.tests.common import TransactionCase
@@ -306,7 +307,7 @@ class TestProductTemplate(TransactionCase):
),
),
(
0.032,
1.0,
(
"Case 18",
"monthly",
@@ -315,7 +316,7 @@ class TestProductTemplate(TransactionCase):
to_date("2017-02-15"),
to_date("2018-01-01"),
False,
to_date("2018-01-30"),
to_date("2017-12-31"),
),
),
(
@@ -331,19 +332,6 @@ class TestProductTemplate(TransactionCase):
to_date("2018-01-30"),
),
),
(
1.0,
(
"Case 18",
"monthlylastday",
1,
"pre-paid",
to_date("2017-02-15"),
to_date("2018-01-31"),
False,
to_date("2017-12-31"),
),
),
(
1.566,
(
@@ -380,6 +368,30 @@ class TestProductTemplate(TransactionCase):
False,
),
),
(
1,
(
"Case 22",
"monthlylastday",
1,
"pre-paid",
to_date("2018-03-01"),
to_date("2018-03-01"),
False,
),
),
(
0.5,
(
"Case 23",
"monthlylastday",
1,
"pre-paid",
to_date("2018-04-16"),
to_date("2018-04-16"),
False,
),
),
]
for result, combination in combinations:
update_contract_line(*combination)