Skip to content

Commit

Permalink
fix: installment: fix calculation of getAllowedMoth on ConfigurationR…
Browse files Browse the repository at this point in the history
…equest (#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.
  • Loading branch information
rommelfreddy authored Sep 5, 2022
1 parent 35d4fc5 commit 10e5e6d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
33 changes: 20 additions & 13 deletions src/Model/Response/ConfigurationRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@

namespace RatePAY\Model\Response;

use RatePAY\ModelBuilder;
use RatePAY\Service\OfflineInstallmentCalculation;

class ConfigurationRequest extends AbstractResponse
{
/**
Expand Down Expand Up @@ -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;
}
}
Expand Down
2 changes: 2 additions & 0 deletions tests/Unit/Model/Response/ConfigurationRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 10e5e6d

Please sign in to comment.