diff --git a/htdocs/compta/loan/calculator.php b/htdocs/compta/loan/calculator.php new file mode 100644 index 00000000000..562ce2c27d5 --- /dev/null +++ b/htdocs/compta/loan/calculator.php @@ -0,0 +1,223 @@ + + * + * 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 . + */ + +/** + * \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 ''; + +print '' . "\n"; +print ''; +print ''; + +// Capital +print ''; + +// Length +print ''; + +// Interest +print ''; + +print '
'.$langs->trans("Capital").'
'.$langs->trans("LengthByYears").'
'.$langs->trans("Interest").' %
'; + +print '
'; + +print ''; + +if ($action == 'calculate') +{ +/* + + + + + + {amortization_table_rows} + + + +
PeriodInterest PaidPrincipal PaidRemaining Balance
Totals:{total_interest}${total_principal}$ 
+ + + + + {period} + {interest}$ + {principal}$ + {balance}$ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Loan Summary
Loan amount:{loan_amount}$
Loan length:{loan_length} years
Annual interest:{annual_interest}%
Pay periodicity:{periodicity}
{periodicity} payment:{period_payment}$
Total paid:{total_paid}$
Total interest:{total_interest}$
Total periods:{total_periods}
+ + */ +} +else +{ + +} + +$db->close(); + +llxFooter(); diff --git a/htdocs/compta/loan/calculator.tpl b/htdocs/compta/loan/calculator.tpl new file mode 100644 index 00000000000..7efbca9924b --- /dev/null +++ b/htdocs/compta/loan/calculator.tpl @@ -0,0 +1,150 @@ + + + + Loan Calculator + + + + + + + + + + +
+ {error} + + + + + +
+ {loan_summary} + {loan_parameters_form} + + {amortization_table} +
+
+ + + +
+ + + + + + + + + + +
Loan Parameters
+ + + + + + + + + + + + + + + + + + + + +
Loan amount: $
Loan length: years
Annual interest: %
Pay periodicity: + +
+
Powered by PC Calculators
+
+ + + + + + + + + + + + {amortization_table_rows} + + + +
PeriodInterest PaidPrincipal PaidRemaining Balance
Totals:{total_interest}${total_principal}$ 
+ + + + + {period} + {interest}$ + {principal}$ + {balance}$ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Loan Summary
Loan amount:{loan_amount}$
Loan length:{loan_length} years
Annual interest:{annual_interest}%
Pay periodicity:{periodicity}
{periodicity} payment:{period_payment}$
Total paid:{total_paid}$
Total interest:{total_interest}$
Total periods:{total_periods}
+ + + + + + diff --git a/htdocs/core/menus/standard/eldy.lib.php b/htdocs/core/menus/standard/eldy.lib.php index c3df5b82fff..bea92481d12 100644 --- a/htdocs/core/menus/standard/eldy.lib.php +++ b/htdocs/core/menus/standard/eldy.lib.php @@ -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 diff --git a/htdocs/langs/en_US/loan.lang b/htdocs/langs/en_US/loan.lang index 6be2f13a101..37523b7408b 100644 --- a/htdocs/langs/en_US/loan.lang +++ b/htdocs/langs/en_US/loan.lang @@ -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=Loan amount has to be numeric and greater than zero. +ErrorLoanLength=Loan length has to be numeric and greater than zero. +ErrorLoanInterest=Annual interest 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 diff --git a/htdocs/langs/fr_FR/loan.lang b/htdocs/langs/fr_FR/loan.lang index d230f7f0582..c699388e0c0 100644 --- a/htdocs/langs/fr_FR/loan.lang +++ b/htdocs/langs/fr_FR/loan.lang @@ -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=Le capital de l'emprunt doit être au format numérique et supérieur à zéro. +ErrorLoanLength=La durée de l'emprunt doit être au format numérique et supérieur à zéro. +ErrorLoanInterest=Les intérêts d'emprunt 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