function generateScheduleTable($ud, $ld, $displyall = 0, $disburseRate = 0) { global $database; $path = getEditablePath('loanstatn.php'); include FULL_PATH . "editables/" . $path; $schedule = $database->getSchedulefromDB($ud, $ld); $actualSchedule = $database->getRepaySchedulefromDB($ud, $ld); $gracePeriod = $database->gerGracePeriod($ld); $UserCurrency = $database->getUserCurrency($ud); if (!empty($disburseRate)) { $CurrencyRate = $disburseRate; } else { $CurrencyRate = $database->getCurrentRate($ud); } $rtnArray = array(); if (empty($schedule)) { $rtnArray['schedule'] = ''; $rtnArray['due'] = 0; $rtnArray['amtPaidTillShow'] = 0; } else { if ($displyall) { $tmpcurr = $UserCurrency; } else { $tmpcurr = 'USD'; } $text = "<table width = 100% class='zebra-striped'>\r\n\t\t\t<tr>\r\n\t\t\t\t<th>" . $lang['loanstatn']['date'] . "</th>\r\n\t\t\t\t<th>" . $lang['loanstatn']['due_amount'] . "</th>\r\n\t\t\t\t<th>" . $lang['loanstatn']['datepaid'] . "</th>\r\n\t\t\t\t<th>" . $lang['loanstatn']['paid_amount'] . " <a style='cursor:pointer' class='tt'><img src='library/tooltips/help.png' style='border-style:none;' /><span class='tooltip'><span class='top'></span><span class='middle'>" . $lang['loanstatn']['paidAmt_tooltip'] . "</span><span class='bottom'></span></span></a></th>\r\n\t\t\t</tr>"; $printSchedule = array(); $paidBalance = 0; $totalDueAmt = 0; $totalDueAmtUsd = 0; $totalPaidAmt = 0; $totalPaidAmtUsd = 0; $amtDueTill = 0; $amtPaidTill = 0; $amtDueTillUsd = 0; for ($i = 0, $j = 0; $i < count($schedule); $i++) { $totalDueAmt += $schedule[$i]['amount']; $printSchedule[$i]['dueAmt'] = $schedule[$i]['amount']; $printSchedule[$i]['dueDate'] = $schedule[$i]['duedate']; if ($schedule[$i]['duedate'] < time()) { $amtDueTill += $schedule[$i]['amount']; } $inst = 0; $inst = $schedule[$i]['amount']; while ($paidBalance > 0) { if ($inst > 0) { if ($inst <= $paidBalance) { $printSchedule[$i]['sub'][][$actualSchedule[$j - 1]['paiddate']] = $inst; $paidBalance = number_format($paidBalance - $inst, 6, '.', ''); $inst = 0; break; } else { $printSchedule[$i]['sub'][][$actualSchedule[$j - 1]['paiddate']] = $paidBalance; $inst = number_format($inst - $paidBalance, 6, '.', ''); $paidBalance = 0; } } else { break; } } if ($paidBalance == 0) { for ($k = 0; $j < count($actualSchedule); $j++) { if ($inst > 0) { if ($inst <= $actualSchedule[$j]['paidamt']) { $printSchedule[$i]['sub'][][$actualSchedule[$j]['paiddate']] = $inst; /*Pranjal Change 26 Jan When there are 2 small payments on the same date the schedule does not show correct data Example loain id 206*/ //$printSchedule[$i]['sub'][$actualSchedule[$j]['paiddate']]+=$inst; $paidBalance = number_format($actualSchedule[$j]['paidamt'] - $inst, 6, '.', ''); $j++; break; } else { $printSchedule[$i]['sub'][][$actualSchedule[$j]['paiddate']] = $actualSchedule[$j]['paidamt']; $inst = number_format($inst - $actualSchedule[$j]['paidamt'], 6, '.', ''); } } else { break; } } } if ($i == count($schedule) - 1 && $paidBalance > 0) { if (isset($printSchedule[$i]['sub'])) { $pos = count($printSchedule[$i]['sub']) - 1; $printSchedule[$i]['sub'][$pos][$actualSchedule[count($actualSchedule) - 1]['paiddate']] += $paidBalance; } else { $printSchedule[$i]['sub'][0][$actualSchedule[count($actualSchedule) - 1]['paiddate']] = $paidBalance; } } } $totalDueAmtUsd += convertToDollar($totalDueAmt, $CurrencyRate); $amtDueTillUsd += convertToDollar($amtDueTill, $CurrencyRate); for ($i = 0; $i < count($printSchedule); $i++) { if ($i < $gracePeriod) { continue; } $text = $text . "<tr> "; $text = $text . "<td style='text-align:left; width:20%'>" . date('M d, Y', $printSchedule[$i]['dueDate']) . "</td>"; if ($displyall) { $text = $text . "<td style='text-align:left; width:20%'>" . number_format(round_local($printSchedule[$i]['dueAmt']), 0, '.', ',') . "</td>"; } else { $text = $text . "<td style='text-align:left; width:20%'>" . number_format(convertToDollar($printSchedule[$i]['dueAmt'], $CurrencyRate), 2, '.', ',') . "</td>"; } if (isset($printSchedule[$i]['sub'])) { $j = 0; foreach ($printSchedule[$i]['sub'] as $sub) { foreach ($sub as $key => $value) { $totalPaidAmt += $value; if ($key < time()) { $amtPaidTill += $value; } if ($j > 0) { $text = $text . "<tr> "; $text = $text . "<td style='text-align:left; width:20%'> </td>"; $text = $text . "<td style='text-align:left; width:20%'> </td>"; } $text = $text . "<td style='text-align:left; width:20%'>" . date('M d, Y', $key) . "</td>"; if ($displyall) { $amtPaidShow = number_format(round_local($value), 0, '.', ','); $text = $text . "<td style='text-align:left; width:20%'>" . $amtPaidShow . "</td>"; } else { $amtPaidShow = convertToDollar($value, $CurrencyRate); } if (count($printSchedule[$i]['sub']) > 1) { if ($j % 2 == 0) { $amtPaidShow = round_up($amtPaidShow, 2); } else { $amtPaidShow = round_down($amtPaidShow, 2); } } if (!$displyall) { $text = $text . "<td style='text-align:left; width:20%'>" . number_format($amtPaidShow, 2, '.', ',') . "</td>"; } $text = $text . " </tr>"; if ($j > 0) { $text = $text . "</tr> "; } $j++; } } } else { $text = $text . "<td style='text-align:left; width:20%'> </td>"; $text = $text . "<td style='text-align:left; width:20%'> </td>"; $text = $text . " </tr>"; } } $totalPaidAmtUsd = convertToDollar($totalPaidAmt, $CurrencyRate); $amtPaidTillUsd = convertToDollar($amtPaidTill, $CurrencyRate); $text = $text . "<tfoot>\r\n\t\t\t\t\t<tr>\r\n\t\t\t\t\t<th>" . $lang['loanstatn']['tot_amount'] . "</th>"; if ($displyall) { $text = $text . "<th>" . number_format(round_local($totalDueAmt), 0, '.', ',') . "</th>"; } else { $text = $text . "<th>" . number_format($totalDueAmtUsd, 2, '.', ',') . "</th>"; } $text = $text . "<th>" . $lang['loanstatn']['tot_paid_amount'] . "</th>"; if ($displyall) { $text = $text . "<th>" . number_format($totalPaidAmt, 0, '.', ',') . "</th>"; // round_local($totalPaidAmt) } else { $text = $text . "<th>" . number_format($totalPaidAmtUsd, 2, '.', ',') . "</th>"; } $text = $text . "</tr></tfoot>"; $text = $text . "</table>"; $rtnArray['schedule'] = $text; if ($displyall) { $due = $amtDueTill - $amtPaidTill; $amtPaidTillShow = $amtPaidTill; $amtRemaining = $totalDueAmt - $totalPaidAmt; } else { $due = $amtDueTillUsd - $amtPaidTillUsd; $amtPaidTillShow = $amtPaidTillUsd; $amtRemaining = $totalDueAmtUsd - $totalPaidAmtUsd; } if ($due < 0) { $due = 0; } if ($amtRemaining < 0) { $amtRemaining = 0; } $rtnArray['amtPaidTillShow'] = $amtPaidTillShow; $rtnArray['amtRemaining'] = $amtRemaining; $rtnArray['due'] = $due; } return $rtnArray; }
/** * Smarty string format modifier plugin * * Type: modifier<br> * Name: round_down<br> * Purpose: Round a number down to nearest 10th, 100th, 1000th * @author James Baicoianu or google * @param string * @param string * @return string */ function smarty_modifier_round_down($number) { return round_down($number); }