forked from Wavyzz/dolibarr
Add calculator
This commit is contained in:
223
htdocs/compta/loan/calculator.php
Normal file
223
htdocs/compta/loan/calculator.php
Normal file
@@ -0,0 +1,223 @@
|
||||
<?php
|
||||
/* Copyright (C) 2014 Alexandre Spangaro <alexandre.spangaro@gmail.com>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
/**
|
||||
* \file htdocs/compta/loan/calculator.php
|
||||
* \ingroup loan
|
||||
* \brief Calculator for loan
|
||||
*/
|
||||
|
||||
require '../../main.inc.php';
|
||||
require_once DOL_DOCUMENT_ROOT.'/compta/loan/class/loan.class.php';
|
||||
|
||||
$langs->load("loan");
|
||||
$langs->load("compta");
|
||||
$langs->load("banks");
|
||||
$langs->load("bills");
|
||||
|
||||
// Security check
|
||||
$socid = isset($GETPOST["socid"])?$GETPOST["socid"]:'';
|
||||
if ($user->societe_id) $socid=$user->societe_id;
|
||||
$result = restrictedArea($user, 'loan', '', '', '');
|
||||
|
||||
$action=GETPOST("action");
|
||||
|
||||
/* Defining Variables */
|
||||
$periods = array(
|
||||
52 => $langs->trans("Weekly"),
|
||||
26 => $langs->trans("Bi-weekly"),
|
||||
12 => $langs->trans("Monthly"),
|
||||
6 => $langs->trans("Bi-monthly"),
|
||||
4 => $langs->trans("Quarterly"),
|
||||
2 => $langs->trans("Semi-annually"),
|
||||
1 => $langs->trans("Annually")
|
||||
);
|
||||
|
||||
$loan_capital = isset($GETPOST("loan_capital")) ? $GETPOST("loan_capital") : 0;
|
||||
$loan_length = isset($GETPOST("loan_length")) ? $GETPOST("loan_length") : 0;
|
||||
$loan_interest = isset(GETPOST("loan_interest")) ? $GETPOST("loan_interest") : 0;
|
||||
$pay_periodicity = isset(GETPOST("pay_periodicity")) ? $GETPOST("pay_periodicity") : 12;
|
||||
$periodicity = $periods[$pay_periodicity];
|
||||
|
||||
$pay_periods = '';
|
||||
foreach($periods as $value => $name)
|
||||
{
|
||||
$selected = ($pay_periodicity == $value) ? 'selected' : '';
|
||||
}
|
||||
|
||||
if ($action == 'calculate')
|
||||
{
|
||||
/* Checking Variables */
|
||||
$error = 0;
|
||||
|
||||
if(!is_numeric($loan_capital) || $loan_capital <= 0)
|
||||
{
|
||||
// $this->error="ErrorLoanCapital";
|
||||
return -1;
|
||||
}
|
||||
if(!is_numeric($loan_length) || $loan_length <= 0)
|
||||
{
|
||||
// $this->error="ErrorLoanLength";
|
||||
return -1;
|
||||
}
|
||||
if(!is_numeric($loan_interest) or $loan_interest <= 0)
|
||||
{
|
||||
// $this->error="ErrorLoanInterest";
|
||||
return -1;
|
||||
}
|
||||
|
||||
/*
|
||||
* Calculating
|
||||
*/
|
||||
if(isset($_GET['action']))
|
||||
{
|
||||
$c_balance = $loan_capital;
|
||||
$total_periods = $loan_length * $pay_periodicity;
|
||||
$interest_percent = $loan_interest / 100;
|
||||
$period_interest = $interest_percent / $pay_periodicity;
|
||||
$c_period_payment = $loan_capital * ($period_interest / (1 - pow((1 + $period_interest), -($total_periods))));
|
||||
$total_paid = number_format($c_period_payment * $total_periods, 2, '.', ' ');
|
||||
$total_interest = number_format($c_period_payment * $total_periods - $loan_capital, 2, '.', ' ');
|
||||
$total_principal = number_format($loan_capital, 2, '.', ' ');
|
||||
|
||||
$loan_capital = number_format($loan_capital, 2, '.', ' ');
|
||||
$loan_interest = number_format($loan_interest, 2, '.', ' ');
|
||||
$period_payment = number_format($c_period_payment, 2, '.', ' ');
|
||||
|
||||
for($period = 1; $period <= $total_periods; $period++)
|
||||
{
|
||||
$c_interest = $c_balance * $period_interest;
|
||||
$c_principal = $c_period_payment - $c_interest;
|
||||
$c_balance -= $c_principal;
|
||||
|
||||
$interest = number_format($c_interest, 2, '.', ' ');
|
||||
$principal = number_format($c_principal, 2, '.', ' ');
|
||||
$balance = number_format($c_balance, 2, '.', ' ');
|
||||
|
||||
$evenrow_row_modifier = ($period % 2) ? '' : 'class=evenrow';
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
$amortization_table = '';
|
||||
$loan_summary = '';
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* View
|
||||
*/
|
||||
|
||||
$help_url='EN:Module_Loan|FR:Module_Emprunt';
|
||||
llxHeader("",$langs->trans("Loan"),$help_url);
|
||||
|
||||
print_fiche_titre($langs->trans("CalculatorLoan"));
|
||||
|
||||
print '<table class="border" width="100%">';
|
||||
|
||||
print '<form name="calculate" action="' . $_SERVER["PHP_SELF"] . '" method="POST">' . "\n";
|
||||
print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
|
||||
print '<input type="hidden" name="action" value="calculate">';
|
||||
|
||||
// Capital
|
||||
print '<tr><td width="25%" class="fieldrequired">'.$langs->trans("Capital").'</td><td><input name="loan_capital" size="10" value="' . $loan_capital . '"></td></tr>';
|
||||
|
||||
// Length
|
||||
print '<tr><td class="fieldrequired">'.$langs->trans("LengthByYears").'</td><td><input name="loan_length" size="2" value="' . $loan_length . '"></td></tr>';
|
||||
|
||||
// Interest
|
||||
print '<tr><td class="fieldrequired">'.$langs->trans("Interest").'</td><td><input name="loan_interest" size="4" value="' . $loan_interest . '"> %</td></tr>';
|
||||
|
||||
print '</table>';
|
||||
|
||||
print '<br><center><input class="button" type="submit" value="'.$langs->trans("Calculate").'"></center>';
|
||||
|
||||
print '</form>';
|
||||
|
||||
if ($action == 'calculate')
|
||||
{
|
||||
/*
|
||||
<!-- BEGIN amortization_table -->
|
||||
<table class=bordered cellpadding=5>
|
||||
<tr>
|
||||
<th>Period</th><th>Interest Paid</th><th>Principal Paid</th><th>Remaining Balance</th>
|
||||
</tr>
|
||||
{amortization_table_rows}
|
||||
<tr>
|
||||
<th>Totals:</th><th>{total_interest}$</th><th>{total_principal}$</th><th> </th>
|
||||
</tr>
|
||||
</table>
|
||||
<!-- END amortization_table -->
|
||||
|
||||
<!-- BEGIN amortization_table_row -->
|
||||
<tr {evenrow_row_modifier}>
|
||||
<td align=center class=bordered>{period}</td>
|
||||
<td align=right class=bordered>{interest}$</td>
|
||||
<td align=right class=bordered>{principal}$</td>
|
||||
<td align=right class=bordered>{balance}$</td>
|
||||
</tr>
|
||||
<!-- END amortization_table_row -->
|
||||
|
||||
<!-- BEGIN loan_summary -->
|
||||
<table cellpadding=5 width=100% class=bordered bgcolor=#EFEFEF style="margin-bottom: 10px">
|
||||
<tr>
|
||||
<th colspan=4>Loan Summary</th>
|
||||
</tr>
|
||||
<tr>
|
||||
<td class=label>Loan amount:</td>
|
||||
<td><b>{loan_amount}$</b></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td class=label>Loan length:</td>
|
||||
<td><b>{loan_length} years</b></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td class=label>Annual interest:</td>
|
||||
<td><b>{annual_interest}%</b></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td class=label>Pay periodicity:</td>
|
||||
<td><b>{periodicity}</b></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td class=label style="border-top: 1px solid #D6D6D6">{periodicity} payment:</td>
|
||||
<td style="border-top: 1px solid #D6D6D6"><b>{period_payment}$</b></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td class=label>Total paid:</td>
|
||||
<td><b>{total_paid}$</b></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td class=label>Total interest:</td>
|
||||
<td><b>{total_interest}$</b></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td class=label>Total periods:</td>
|
||||
<td><b>{total_periods}</b></td>
|
||||
</tr>
|
||||
</table>
|
||||
<!-- END loan_summary -->
|
||||
*/
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
$db->close();
|
||||
|
||||
llxFooter();
|
||||
150
htdocs/compta/loan/calculator.tpl
Normal file
150
htdocs/compta/loan/calculator.tpl
Normal file
@@ -0,0 +1,150 @@
|
||||
<!-- BEGIN header -->
|
||||
<html>
|
||||
<head>
|
||||
<title>Loan Calculator</title>
|
||||
<style>
|
||||
BODY, TABLE, TH, TD, TR, FORM {font: 11px Tahoma,Arial,Helvetica,sans-serif; vertical-align: top}
|
||||
FORM {margin: 0px; padding: 0px}
|
||||
TEXTAREA, INPUT, SELECT, LABEL, BUTTON {font: 11px Tahoma,Arial,Helvetica,sans-serif}
|
||||
TD.label { text-align: right; vertical-align: middle}
|
||||
TABLE {border-collapse: collapse}
|
||||
TH {font: bold; background: #FFF3CB; border: 1px solid #D6D6D6; padding: 5px; text-align: center}
|
||||
.bordered {border: 1px solid #D6D6D6}
|
||||
.evenrow {background-color: #EFEFEF}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<!-- END header -->
|
||||
|
||||
<!-- BEGIN body -->
|
||||
<table cellpadding=5 width=100% height=100% style="text-align: center; vertical-align: middle">
|
||||
<tr>
|
||||
<td height=100% style="text-align: center; vertical-align: middle">
|
||||
{error}
|
||||
<table cellpadding=10>
|
||||
<tr>
|
||||
<td>
|
||||
{loan_summary}
|
||||
{loan_parameters_form}
|
||||
</td>
|
||||
<td>
|
||||
{amortization_table}
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<!-- END body -->
|
||||
|
||||
<!-- BEGIN loan_parameters_form -->
|
||||
<form>
|
||||
<table cellpadding=5 width=100%>
|
||||
<tr>
|
||||
<th>Loan Parameters</th>
|
||||
</tr>
|
||||
<tr>
|
||||
<td class=bordered bgcolor=#EFEFEF>
|
||||
<table cellpadding=4 width=100%>
|
||||
<tr>
|
||||
<td class=label>Loan amount:</td>
|
||||
<td><input type="text" name="loan_amount" value="{loan_amount}" size=7> $</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td class=label>Loan length:</td>
|
||||
<td><input type="text" name="loan_length" value="{loan_length}" size=2> years</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td class=label>Annual interest:</td>
|
||||
<td><input type="text" name="annual_interest" value="{annual_interest}" size=2> %</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td class=label>Pay periodicity:</td>
|
||||
<td>
|
||||
<select name=pay_periodicity>
|
||||
{pay_periods}
|
||||
</select>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td colspan=2 align=center><input name="action" type="submit" value="Calculate"></td>
|
||||
</tr>
|
||||
</table>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<!-- DO NOT REMOVE THIS LINE! Read Disclaimer in the loan-calculators.php file --><td align=center style="font: 9px; color: #AAAAAA">Powered by PC <a href="http://www.pc-calculators.com" style="font: 9px; color: #AAAAAA; text-decoration: none">Calculators</a></td>
|
||||
</tr>
|
||||
</table>
|
||||
</form>
|
||||
<!-- END loan_parameters_form -->
|
||||
|
||||
<!-- BEGIN pay_period_option -->
|
||||
<option value={value} {selected}>{name}</option>
|
||||
<!-- END pay_period_option -->
|
||||
|
||||
<!-- BEGIN amortization_table -->
|
||||
<table class=bordered cellpadding=5>
|
||||
<tr>
|
||||
<th>Period</th><th>Interest Paid</th><th>Principal Paid</th><th>Remaining Balance</th>
|
||||
</tr>
|
||||
{amortization_table_rows}
|
||||
<tr>
|
||||
<th>Totals:</th><th>{total_interest}$</th><th>{total_principal}$</th><th> </th>
|
||||
</tr>
|
||||
</table>
|
||||
<!-- END amortization_table -->
|
||||
|
||||
<!-- BEGIN amortization_table_row -->
|
||||
<tr {evenrow_row_modifier}>
|
||||
<td align=center class=bordered>{period}</td>
|
||||
<td align=right class=bordered>{interest}$</td>
|
||||
<td align=right class=bordered>{principal}$</td>
|
||||
<td align=right class=bordered>{balance}$</td>
|
||||
</tr>
|
||||
<!-- END amortization_table_row -->
|
||||
|
||||
<!-- BEGIN loan_summary -->
|
||||
<table cellpadding=5 width=100% class=bordered bgcolor=#EFEFEF style="margin-bottom: 10px">
|
||||
<tr>
|
||||
<th colspan=4>Loan Summary</th>
|
||||
</tr>
|
||||
<tr>
|
||||
<td class=label>Loan amount:</td>
|
||||
<td><b>{loan_amount}$</b></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td class=label>Loan length:</td>
|
||||
<td><b>{loan_length} years</b></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td class=label>Annual interest:</td>
|
||||
<td><b>{annual_interest}%</b></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td class=label>Pay periodicity:</td>
|
||||
<td><b>{periodicity}</b></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td class=label style="border-top: 1px solid #D6D6D6">{periodicity} payment:</td>
|
||||
<td style="border-top: 1px solid #D6D6D6"><b>{period_payment}$</b></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td class=label>Total paid:</td>
|
||||
<td><b>{total_paid}$</b></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td class=label>Total interest:</td>
|
||||
<td><b>{total_interest}$</b></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td class=label>Total periods:</td>
|
||||
<td><b>{total_periods}</b></td>
|
||||
</tr>
|
||||
</table>
|
||||
<!-- END loan_summary -->
|
||||
|
||||
<!-- BEGIN footer -->
|
||||
</body>
|
||||
</html>
|
||||
<!-- END footer -->
|
||||
@@ -825,6 +825,7 @@ function print_left_eldy_menu($db,$menu_array_before,$menu_array_after,&$tabMenu
|
||||
$newmenu->add("/compta/loan/index.php?leftmenu=tax_loan&mainmenu=accountancy",$langs->trans("Loans"),1,$user->rights->loan->read, '', $mainmenu, 'tax_loan');
|
||||
if (empty($leftmenu) || preg_match('/^tax_loan/i',$leftmenu)) $newmenu->add("/compta/loan/card.php?leftmenu=tax_loan&action=create",$langs->trans("NewLoan"),2,$user->rights->loan->write);
|
||||
if (empty($leftmenu) || preg_match('/^tax_loan/i',$leftmenu)) $newmenu->add("/compta/loan/index.php?leftmenu=tax_loan",$langs->trans("Payments"),2,$user->rights->loan->read);
|
||||
if (empty($leftmenu) || preg_match('/^tax_loan/i',$leftmenu)) $newmenu->add("/compta/loan/calculator.php?leftmenu=tax_loan",$langs->trans("Calculator"),2,$user->rights->loan->read);
|
||||
}
|
||||
|
||||
// Social contributions
|
||||
|
||||
@@ -9,6 +9,12 @@ Nbterms=Number of terms
|
||||
LoanAccountancyCapitalCode=Accountancy code capital
|
||||
LoanAccountancyInsuranceCode=Accountancy code insurance
|
||||
LoanAccountancyInterestCode=Accountancy code interest
|
||||
Calculator=Simulator
|
||||
CalculatorLoan=Loan simulator
|
||||
ErrorLoanCapital=<font color=red>Loan amount</font> has to be numeric and greater than zero.
|
||||
ErrorLoanLength=<font color=red>Loan length</font> has to be numeric and greater than zero.
|
||||
ErrorLoanInterest=<font color=red>Annual interest</font> has to be numeric and greater than zero.
|
||||
# Admin
|
||||
ConfigLoan=Configuration of the module loan
|
||||
LOAN_ACCOUNTING_ACCOUNT_CAPITAL=Accountancy code capital by default
|
||||
LOAN_ACCOUNTING_ACCOUNT_INTEREST=Accountancy code interest by default
|
||||
|
||||
@@ -9,6 +9,12 @@ Nbterms=Nombre d'échéances
|
||||
LoanAccountancyCapitalCode=Compte comptable capital
|
||||
LoanAccountancyInsuranceCode=Compte comptable assurance
|
||||
LoanAccountancyInterestCode=Compte comptable intérêts
|
||||
Calculator=Simulateur
|
||||
CalculatorLoan=Simulateur d'emprunt
|
||||
ErrorLoanCapital=<font color=red>Le capital de l'emprunt</font> doit être au format numérique et supérieur à zéro.
|
||||
ErrorLoanLength=<font color=red>La durée de l'emprunt</font> doit être au format numérique et supérieur à zéro.
|
||||
ErrorLoanInterest=<font color=red>Les intérêts d'emprunt</font> doivent être au format numérique et supérieur à zéro.
|
||||
# Admin
|
||||
ConfigLoan=Configuration du module emprunt
|
||||
LOAN_ACCOUNTING_ACCOUNT_CAPITAL=Compte comptable capital par défaut
|
||||
LOAN_ACCOUNTING_ACCOUNT_INTEREST=Compte comptable intérêts par défaut
|
||||
|
||||
Reference in New Issue
Block a user