Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Rounded #s for Debt Payments #6

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 60 additions & 1 deletion src/Calculators/DebtAmortizator.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ class DebtAmortizator extends CalculatorAbstract
"days" => "debtLengthInDays"
],
"debtSingleRepayment",
"debtRepayments" => "debtRepaymentsAsArrays"
"debtSingleRepaymentRounded",
"debtRepayments" => "debtRepaymentsAsArrays",
"debtRepaymentsRounded" => "debtRepaymentsAsArraysRounded"
];

/**
Expand Down Expand Up @@ -288,6 +290,27 @@ public function getDebtSingleRepayment()
)
);
}

/**
* @return string [Value of a single debt repayment instance as a string]
*/
public function getDebtSingleRepaymentRounded() {
// single repayment 'K = PV/((1-v^n)/i)'
return MathFuncs::roundUp(
MathFuncs::div(
$this->debtPrincipal,
MathFuncs::div(
MathFuncs::sub(
1,
MathFuncs::pow(
$this->getDebtDiscountFactor(),
$this->debtNoOfCompoundingPeriods
)
),
$this->debtInterest
)
), 2);
}

/**
* @return RepaymentInstance[] [Array of individual debt repayments (RepaymentInstances)]
Expand All @@ -314,7 +337,43 @@ public function getDebtRepaymentsAsArrays()

return $repayments;
}

/**
* @return array
*/
public function getDebtRepaymentsAsArraysRounded()
{
$repayments = array();
$i = 1;
$balance = $this->debtPrincipal;
foreach ($this->debtRepayments as $repayment) {
if($balance > $repayment->getTotalAmount()) {
$totalAmount = MathFuncs::roundUp($repayment->getTotalAmount(), 2);
$interestAmount = MathFuncs::round($repayment->getInterestAmount(), 2);
$principalAmount = MathFuncs::round($totalAmount - $interestAmount, 2);
}
else {
$interestAmount = MathFuncs::round($repayment->getInterestAmount(), 2);
$totalAmount = MathFuncs::round(MathFuncs::add($balance, $interestAmount), 2);
$principalAmount = MathFuncs::round($balance, 2);
}

$beginningBalance = $balance;
$endingBalance = MathFuncs::sub($balance, $principalAmount);
$balance = MathFuncs::sub($balance, $principalAmount);

$repayments[$i++] = [
"beginningBalance" => MathFuncs::round($beginningBalance, 2),
"totalAmount" => $totalAmount,
"principalAmount" => $principalAmount,
"interestAmount" => $interestAmount,
"endingBalance" => MathFuncs::round($endingBalance, 2)
];
}

return $repayments;
}

}

/**
Expand Down
20 changes: 20 additions & 0 deletions src/Utils/MathFuncs.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,25 @@ public static function round($roundedNumber, $precision = 2)
{
return (string)number_format((float)$roundedNumber, $precision);
}

/**
* @param $roundedNumber
* @param $precision
* @return string
*/
public static function roundUp($roundedNumber, $precision = 2)
{
return (string)ceil((float)$roundedNumber * pow(10, $precision)) / pow(10, $precision);
}

/**
* @param $roundedNumber
* @param $precision
* @return string
*/
public static function roundDown($roundedNumber, $precision = 2)
{
return (string)floor((float)$roundedNumber * pow(10, $precision)) / pow(10, $precision);
}
}
}
27 changes: 27 additions & 0 deletions tests/DebtAmortizatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,33 @@ private function processArray(array $resultArray)
$this->assertEquals("8686.63", round($repayments[6]["principalAmount"], 2));
$this->assertEquals("1042.4", round($repayments[6]["interestAmount"], 2));
$this->assertEquals($INDIVIDUAL_REPAYMENT, round($repayments[6]["totalAmount"], 2));

$repaymentsRounded = $resultArray["debtRepaymentsRounded"];

$INDIVIDUAL_REPAYMENT = "9729.03";
$this->assertEquals("4929.03", round($repaymentsRounded[1]["principalAmount"], 2));
$this->assertEquals("4800.00", round($repaymentsRounded[1]["interestAmount"], 2));
$this->assertEquals($INDIVIDUAL_REPAYMENT, round($repaymentsRounded[1]["totalAmount"], 2));

$this->assertEquals("5520.51", round($repaymentsRounded[2]["principalAmount"], 2));
$this->assertEquals("4208.52", round($repaymentsRounded[2]["interestAmount"], 2));
$this->assertEquals($INDIVIDUAL_REPAYMENT, round($repaymentsRounded[2]["totalAmount"], 2));

$this->assertEquals("6182.97", round($repaymentsRounded[3]["principalAmount"], 2));
$this->assertEquals("3546.06", round($repaymentsRounded[3]["interestAmount"], 2));
$this->assertEquals($INDIVIDUAL_REPAYMENT, round($repaymentsRounded[3]["totalAmount"], 2));

$this->assertEquals("6924.93", round($repaymentsRounded[4]["principalAmount"], 2));
$this->assertEquals("2804.10", round($repaymentsRounded[4]["interestAmount"], 2));
$this->assertEquals($INDIVIDUAL_REPAYMENT, round($repaymentsRounded[4]["totalAmount"], 2));

$this->assertEquals("7755.92", round($repaymentsRounded[5]["principalAmount"], 2));
$this->assertEquals("1973.11", round($repaymentsRounded[5]["interestAmount"], 2));
$this->assertEquals($INDIVIDUAL_REPAYMENT, round($repaymentsRounded[5]["totalAmount"], 2));

$this->assertEquals("8686.63", round($repaymentsRounded[6]["principalAmount"], 2));
$this->assertEquals("1042.4", round($repaymentsRounded[6]["interestAmount"], 2));
$this->assertEquals($INDIVIDUAL_REPAYMENT, round($repaymentsRounded[6]["totalAmount"], 2));
}

/**
Expand Down
10 changes: 10 additions & 0 deletions tests/MathFuncsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,14 @@ public function testRound()
{
$this->assertEquals("3.68", MathFuncs::round(3.675624));
}

public function testRoundUp()
{
$this->assertEquals("3.69", MathFuncs::round(3.675624));
}

public function testRoundDown()
{
$this->assertEquals("3.67", MathFuncs::round(3.675624));
}
}