From dd5775e7d462e6d8bad2d4f727869ed5fb36238c Mon Sep 17 00:00:00 2001 From: sbejaoui Date: Fri, 15 May 2020 19:51:00 +0200 Subject: [PATCH] [REF] - code refactoring + improve unit tests --- .../models/contract_line.py | 53 ++++++++++++------- .../tests/test_compute_proprata.py | 42 +++++++++------ 2 files changed, 60 insertions(+), 35 deletions(-) diff --git a/contract_variable_qty_prorated/models/contract_line.py b/contract_variable_qty_prorated/models/contract_line.py index f653aab21..79e2e408d 100644 --- a/contract_variable_qty_prorated/models/contract_line.py +++ b/contract_variable_qty_prorated/models/contract_line.py @@ -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 ) diff --git a/contract_variable_qty_prorated/tests/test_compute_proprata.py b/contract_variable_qty_prorated/tests/test_compute_proprata.py index 7fd610c14..95833830c 100644 --- a/contract_variable_qty_prorated/tests/test_compute_proprata.py +++ b/contract_variable_qty_prorated/tests/test_compute_proprata.py @@ -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)