-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/develop'
- Loading branch information
Showing
25 changed files
with
2,670 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
src/Helper/PaymentPlanHelper/CampaignTypeCalculator/EffectiveInterestRateCalculator.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
<?php | ||
|
||
|
||
namespace Svea\WebPay\Helper\PaymentPlanHelper\CampaignTypeCalculator; | ||
|
||
use Svea\WebPay\BuildOrder\Validator\ValidationException; | ||
use Svea\WebPay\Helper\Helper; | ||
|
||
class EffectiveInterestRateCalculator | ||
{ | ||
private $oneMonth = 0.08327627652; // Factor of month average length divided by average year length (leap years etc) | ||
private $tolerance = 0.00000001; | ||
private $initialLowerBound = 0; | ||
private $initialUpperBound = 10000000; | ||
private $payments = array(); | ||
private $sizeOfLoan; | ||
|
||
public function __construct($sizeOfLoan) | ||
{ | ||
$this->sizeOfLoan = $sizeOfLoan; | ||
} | ||
|
||
/** | ||
* @param $sizeOfLoan | ||
* @param $firstPayment | ||
* @param $monthlyPayment | ||
* @param $contactLengthInMonths | ||
* @param $deferralPeriodInMonths | ||
* @return float|int | ||
* @throws ValidationException | ||
*/ | ||
public function calculate($sizeOfLoan, $firstPayment, $monthlyPayment, $contactLengthInMonths, $deferralPeriodInMonths) | ||
{ | ||
if($monthlyPayment < 0) | ||
{ | ||
throw new ValidationException("Monthly payment can not be below 0"); | ||
} | ||
if($contactLengthInMonths < 1) | ||
{ | ||
throw new ValidationException("Contract length must be at least 1 month"); | ||
} | ||
|
||
$firstPaymentMonth = min($contactLengthInMonths, $deferralPeriodInMonths + 1); | ||
$this->addPayment($this->oneMonth * $firstPaymentMonth, $firstPayment); | ||
|
||
for($month = $firstPaymentMonth + 1; $month <= $contactLengthInMonths; $month++) | ||
{ | ||
$this->addPayment($this->oneMonth * $month, $monthlyPayment); | ||
} | ||
|
||
if(array_sum(array_column($this->payments, 'amount')) - $sizeOfLoan < 0.1) | ||
{ | ||
return 0; | ||
} | ||
|
||
return Helper::bround($this->solveUsingBisection() * 100, 2); | ||
} | ||
|
||
/** | ||
* @return float|int | ||
* @throws ValidationException | ||
*/ | ||
public function solveUsingBisection() | ||
{ | ||
$lowerBound = $this->initialLowerBound; | ||
$upperBound = $this->initialUpperBound; | ||
|
||
while($upperBound - $lowerBound > $this->tolerance) | ||
{ | ||
$newPoint = $lowerBound + ($upperBound - $lowerBound) / 2; | ||
if($this->sign($this->evaluate($lowerBound)) == $this->sign($this->evaluate($newPoint))) | ||
{ | ||
$lowerBound = $newPoint; | ||
} | ||
else | ||
{ | ||
$upperBound = $newPoint; | ||
} | ||
} | ||
|
||
if(abs($lowerBound - $this->initialLowerBound) < 2 * $this->tolerance || abs($upperBound - $this->initialUpperBound) < 2 * $this->tolerance) | ||
{ | ||
throw new ValidationException("No solution found"); | ||
} | ||
return $lowerBound; | ||
} | ||
|
||
private function sign( $number ) { | ||
return ( $number > 0 ) ? 1 : ( ( $number < 0 ) ? -1 : 0 ); | ||
} | ||
|
||
private function evaluate($val) | ||
{ | ||
$sum = 0; | ||
foreach($this->payments as $value) | ||
{ | ||
$sum += $value['amount'] / pow(1 + $val, $value['timeToPaymentInYears']); | ||
} | ||
return $sum - $this->sizeOfLoan; | ||
} | ||
|
||
public function addPayment($timeToPaymentInYears, $amount) | ||
{ | ||
array_push($this->payments, array('timeToPaymentInYears' => $timeToPaymentInYears, 'amount' => $amount)); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/Helper/PaymentPlanHelper/CampaignTypeCalculator/IPaymentPlanCalculator.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
|
||
namespace Svea\WebPay\Helper\PaymentPlanHelper\CampaignTypeCalculator; | ||
|
||
|
||
interface IPaymentPlanCalculator | ||
{ | ||
static public function calculateTotalAmountToPay($totalPrice, $campaign, $decimals = 0); | ||
static public function calculateMonthlyAmountToPay($totalPrice, $campaign, $decimals = 0); | ||
static public function calculateEffectiveInterestRate($totalPrice, $campaign, $decimals = 0); | ||
static public function calculatePaymentFactor($numberOfPayments, $yearlyInterestRate, $paymentFrequencyPerYear = 12); | ||
static public function calculateMonthlyAnnuityAmount($totalPrice, $campaign); | ||
} |
38 changes: 38 additions & 0 deletions
38
...entPlanHelper/CampaignTypeCalculator/InterestAndAmortizationFreePaymentPlanCalculator.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
|
||
namespace Svea\WebPay\Helper\PaymentPlanHelper\CampaignTypeCalculator; | ||
|
||
use Svea\WebPay\Helper\Helper; | ||
|
||
class InterestAndAmortizationFreePaymentPlanCalculator implements IPaymentPlanCalculator | ||
{ | ||
public static function calculateTotalAmountToPay($totalPrice, $campaign, $decimals = 0) | ||
{ | ||
return $totalPrice + $campaign['notificationFee'] + $campaign['initialFee']; | ||
} | ||
|
||
public static function calculateMonthlyAmountToPay($totalPrice, $campaign, $decimals = 0) | ||
{ | ||
return $totalPrice + $campaign['notificationFee'] + $campaign['initialFee']; | ||
} | ||
|
||
public static function calculateEffectiveInterestRate($totalPrice, $campaign, $decimals = 0) | ||
{ | ||
$effectiveCalculator = new EffectiveInterestRateCalculator($totalPrice); | ||
$firstPayment = $campaign['initialFee'] + $campaign['notificationFee'] + Helper::bround(self::calculateMonthlyAnnuityAmount($totalPrice, $campaign), $decimals); | ||
$monthlyAmount = Helper::bround(self::calculateMonthlyAnnuityAmount($totalPrice, $campaign), $decimals) + $campaign['notificationFee']; | ||
return $effectiveCalculator->calculate($totalPrice, $firstPayment, $monthlyAmount, $campaign['contractLengthInMonths'], $campaign['numberOfPaymentFreeMonths']); | ||
} | ||
|
||
public static function calculatePaymentFactor($numberOfPayments, $yearlyInterestRate, $paymentFrequencyPerYear = 12) | ||
{ | ||
return 1; | ||
} | ||
|
||
public static function calculateMonthlyAnnuityAmount($totalPrice, $campaign) | ||
{ | ||
return $totalPrice; | ||
} | ||
|
||
} |
39 changes: 39 additions & 0 deletions
39
src/Helper/PaymentPlanHelper/CampaignTypeCalculator/InterestFreePaymentPlanCalculator.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
|
||
namespace Svea\WebPay\Helper\PaymentPlanHelper\CampaignTypeCalculator; | ||
|
||
use Svea\WebPay\Helper\Helper; | ||
|
||
class InterestFreePaymentPlanCalculator implements IPaymentPlanCalculator | ||
{ | ||
public static function calculateTotalAmountToPay($totalPrice, $campaign, $decimals = 0) | ||
{ | ||
return $totalPrice + $campaign['initialFee'] + $campaign['notificationFee'] * max(1, $campaign['contractLengthInMonths'] - $campaign['numberOfPaymentFreeMonths']); | ||
} | ||
|
||
public static function calculateMonthlyAmountToPay($totalPrice, $campaign, $decimals = 0) | ||
{ | ||
return Helper::bround(self::calculateTotalAmountToPay($totalPrice, $campaign) / max(1, $campaign['contractLengthInMonths'] - $campaign['numberOfPaymentFreeMonths']), $decimals); | ||
} | ||
|
||
public static function calculateEffectiveInterestRate($totalPrice, $campaign, $decimals = 0) | ||
{ | ||
$effectiveCalculator = new EffectiveInterestRateCalculator($totalPrice); | ||
$firstPayment = $campaign['initialFee'] + $campaign['notificationFee'] + Helper::bround(self::calculateMonthlyAnnuityAmount($totalPrice, $campaign), $decimals); | ||
$monthlyAmount = Helper::bround(self::calculateMonthlyAnnuityAmount($totalPrice, $campaign), $decimals) + $campaign['notificationFee']; | ||
return $effectiveCalculator->calculate($totalPrice, $firstPayment, $monthlyAmount, $campaign['contractLengthInMonths'], $campaign['numberOfPaymentFreeMonths']); | ||
} | ||
|
||
public static function calculatePaymentFactor($numberOfPayments, $yearlyInterestRate, $paymentFrequencyPerYear = 12) | ||
{ | ||
return 1 / $numberOfPayments; | ||
} | ||
|
||
public static function calculateMonthlyAnnuityAmount($totalPrice, $campaign) | ||
{ | ||
$paymentFactor = self::calculatePaymentFactor($campaign['contractLengthInMonths'] - $campaign['numberOfPaymentFreeMonths'], | ||
$campaign['interestRatePercent']); | ||
return $totalPrice * $paymentFactor; | ||
} | ||
} |
Oops, something went wrong.