mirror of
https://github.com/OCA/contract.git
synced 2025-02-13 17:57:24 +02:00
[REF] - code refactoring + improve unit tests
This commit is contained in:
@@ -7,39 +7,52 @@ from odoo import api, models
|
|||||||
class ContractLine(models.Model):
|
class ContractLine(models.Model):
|
||||||
_inherit = "contract.line"
|
_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
|
@api.model
|
||||||
def _compute_prorated(
|
def _compute_prorated(
|
||||||
self, real_next_date, real_last_date, theoretical_next_date,
|
self,
|
||||||
theoretical_last_date):
|
period_first_date,
|
||||||
|
period_last_date,
|
||||||
|
invoice_date,
|
||||||
|
recurring_rule_type,
|
||||||
|
recurring_interval,
|
||||||
|
recurring_invoicing_type,
|
||||||
|
):
|
||||||
def _invoiced_days(next_date, last_date):
|
def _invoiced_days(next_date, last_date):
|
||||||
return (next_date - last_date).days + 1
|
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(
|
relative_delta = self.get_relative_delta(
|
||||||
self.recurring_rule_type, self.recurring_interval
|
recurring_rule_type, recurring_interval
|
||||||
)
|
)
|
||||||
theoretical_next_date = invoice_date
|
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 (
|
if (
|
||||||
self.recurring_invoicing_type == "pre-paid"
|
recurring_rule_type == "monthlylastday"
|
||||||
and self.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_next_date += relative_delta
|
||||||
theoretical_last_date = theoretical_next_date - relative_delta
|
theoretical_last_date = theoretical_next_date - relative_delta
|
||||||
theoretical_next_date -= self.get_relative_delta("daily", 1)
|
theoretical_next_date -= self.get_relative_delta("daily", 1)
|
||||||
real_last_date = period_first_date
|
real_last_date = period_first_date
|
||||||
real_next_date = period_last_date
|
real_next_date = period_last_date
|
||||||
return self._compute_prorated(
|
return _invoiced_days(real_next_date, real_last_date) / _invoiced_days(
|
||||||
real_next_date, real_last_date, theoretical_next_date,
|
theoretical_next_date, theoretical_last_date
|
||||||
theoretical_last_date
|
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
# Copyright 2018 ACSONE SA/NV.
|
# Copyright 2018 ACSONE SA/NV.
|
||||||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
|
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
|
||||||
|
|
||||||
|
|
||||||
from odoo import fields
|
from odoo import fields
|
||||||
from odoo.tests.common import TransactionCase
|
from odoo.tests.common import TransactionCase
|
||||||
|
|
||||||
@@ -306,7 +307,7 @@ class TestProductTemplate(TransactionCase):
|
|||||||
),
|
),
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
0.032,
|
1.0,
|
||||||
(
|
(
|
||||||
"Case 18",
|
"Case 18",
|
||||||
"monthly",
|
"monthly",
|
||||||
@@ -315,7 +316,7 @@ class TestProductTemplate(TransactionCase):
|
|||||||
to_date("2017-02-15"),
|
to_date("2017-02-15"),
|
||||||
to_date("2018-01-01"),
|
to_date("2018-01-01"),
|
||||||
False,
|
False,
|
||||||
to_date("2018-01-30"),
|
to_date("2017-12-31"),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
@@ -331,19 +332,6 @@ class TestProductTemplate(TransactionCase):
|
|||||||
to_date("2018-01-30"),
|
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,
|
1.566,
|
||||||
(
|
(
|
||||||
@@ -380,6 +368,30 @@ class TestProductTemplate(TransactionCase):
|
|||||||
False,
|
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:
|
for result, combination in combinations:
|
||||||
update_contract_line(*combination)
|
update_contract_line(*combination)
|
||||||
|
|||||||
Reference in New Issue
Block a user