I have a basic object (Loan) and 2 secondary that must be iterated in the correct order. This 2 secondary objects are (Forced Payments) and (Deferral). All the objects came from DataBase, and this 2 secondary objects has field "startDate" of DateTime type.
I need to make calculations in a for loop with my main object Loan, and on each iteration to change a Loan $date variable by one month, and on each iteration to check if modified date is equal to Forced Payment or Deferral startDate, to start some calculations with secondary object, then return back in the main Loan for loop, and continue.
So, to continue solving this algorithm, I need to sort this 2 objects by a "startDate" field, and then from this objects to make a array ordered by date, which will be checked on each iteration, if modified Loan date is equal to any date of this array.
There how var_dumo of this 2 objects looks like :
Forced Payments :
array(1) { [0]=> array(5) { ["id"]=> int(101010109) ["startDate"]=> object(DateTime)#566 (3) { ["date"]=> string(26) "2000-04-30 00:00:00.000000" ["timezone_type"]=> int(3) ["timezone"]=> string(15) "Europe/Helsinki" } ["value"]=> float(343) ["rate"]=> float(3) ["duration"]=> int(10) } }
Deferrals :
array(1) { [0]=> array(4) { ["id"]=> int(1) ["type"]=> int(0) ["startDate"]=> object(DateTime)#572 (3) { ["date"]=> string(26) "2001-08-31 00:00:00.000000" ["timezone_type"]=> int(3) ["timezone"]=> string(15) "Europe/Helsinki" } ["endDate"]=> int(14) } }
And here is my loop, it will not give you nothing except some idea how it looks. I want to put the sorted Array Result somewhere in the loop. To check if modified date is equal to date from the Array Result (which I ask in this question). If yes, start some new calculations, and then come back and continue the algorithm.
for ($i = 0; $i < $duration; $i++){
$interest = $capital * ($rate / 100) / $frequency;
array_push($allInterests,$interest);
array_push($allPayments,$payment);
$amortization = $payment - $interest;
$remaining = $capital - $amortization;
//calculate totals for table headers output in Twig :
$interestTotal += $interest; $amortizationTotal += $amortization;
$inverseCapital = $capital * -1;
$paymentTotal = $amortizationTotal + $interestTotal;
$this->array[$i] = array(
'Date' => $date->format('d/m/Y'),
'Capital' => $capital,
'Rate' => $rate,
'Interest' => $interest,
'Payment' => $payment,
'Amortization' => $amortization,
'Remaining' => $remaining,
'InterestTotal' => $interestTotal,
'AmortizationTotal' => $amortizationTotal,
'PaymentTotal' => $paymentTotal,
'InverseCapital' => $inverseCapital,
);
$capital = $remaining;
$M = 12/$frequency;
$months = (12/$frequency). ' months';
$this->dateCalculator($date, $day, $M, $case);
}