From 10e5e6d6235dd4fdd39224f940f981dcb60aed06 Mon Sep 17 00:00:00 2001 From: Frederik Rommel <15031079+rommelfreddy@users.noreply.github.com> Date: Mon, 5 Sep 2022 11:33:31 +0200 Subject: [PATCH] fix: installment: fix calculation of getAllowedMoth on ConfigurationRequest (#29) getAllowedMoth has a legacy calculation of the monthly rate, so the results are not correctly on threshold values. It has been replace with the offline-calculation-request, so it is the same calculation. --- src/Model/Response/ConfigurationRequest.php | 33 +++++++++++-------- .../Response/ConfigurationRequestTest.php | 2 ++ 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/src/Model/Response/ConfigurationRequest.php b/src/Model/Response/ConfigurationRequest.php index 7cc1c59..5e1bba0 100644 --- a/src/Model/Response/ConfigurationRequest.php +++ b/src/Model/Response/ConfigurationRequest.php @@ -9,6 +9,9 @@ namespace RatePAY\Model\Response; + use RatePAY\ModelBuilder; + use RatePAY\Service\OfflineInstallmentCalculation; + class ConfigurationRequest extends AbstractResponse { /** @@ -44,30 +47,34 @@ public function validateResponse() * Returns allowed months. If order amount is entered it returns only admitted month for specific order amount. * * @param float $orderAmount + * @param float $paymentFirstDay * * @return array */ - public function getAllowedMonths($orderAmount = 0) + public function getAllowedMonths($orderAmount = 0, $paymentFirstDay = null) { if ($orderAmount == 0) { return $this->result['monthAllowed']; } - $allowedMonths = $this->result['monthAllowed']; $possibleMonths = []; - $rateMinNormal = $this->result['rateMinNormal']; - $interestRate = $this->result['interestRateDefault'] / 100; - $interestRateMonth = $interestRate / 12; + foreach ($this->result['monthAllowed'] as $runtime) { + $mbContent = new ModelBuilder('Content'); + $mbContent->setArray([ + 'InstallmentCalculation' => [ + 'Amount' => $orderAmount, + 'PaymentFirstday' => $paymentFirstDay ? $paymentFirstDay : $this->result['paymentFirstday'], + 'InterestRate' => $this->result['interestRateDefault'], + 'ServiceCharge' => $this->result['serviceCharge'], + 'CalculationTime' => [ + 'Month' => $runtime, + ], + ], + ]); - foreach ($allowedMonths as $runtime) { - if ($interestRate > 0) { - $rateAmount = $orderAmount * (($interestRateMonth * pow((1 + $interestRateMonth), $runtime)) / (pow((1 + $interestRateMonth), $runtime) - 1)); - } else { - $rateAmount = $orderAmount / $runtime; - } - $rateAmount = ceil($rateAmount); - if ($rateAmount >= $rateMinNormal) { + $monthlyRate = (new OfflineInstallmentCalculation())->callOfflineCalculation($mbContent)->subtype('calculation-by-time'); + if ($monthlyRate >= $this->result['rateMinNormal']) { $possibleMonths[] = $runtime; } } diff --git a/tests/Unit/Model/Response/ConfigurationRequestTest.php b/tests/Unit/Model/Response/ConfigurationRequestTest.php index 4e17474..3cea17b 100644 --- a/tests/Unit/Model/Response/ConfigurationRequestTest.php +++ b/tests/Unit/Model/Response/ConfigurationRequestTest.php @@ -21,6 +21,8 @@ public function testGetAllowedMonths($amount, $expectedAllowedMonths) 'monthAllowed' => [3, 6, 9, 12, 18, 24, 36], 'rateMinNormal' => 20.0, 'interestRateDefault' => 13.7, + 'paymentFirstday' => 2, + 'serviceCharge' => 0 ]); $allowedMonths = $response->getAllowedMonths($amount);