From 31b90cf3834b48f4fda5ab2f5b41a56326935943 Mon Sep 17 00:00:00 2001 From: aspangaro Date: Mon, 17 Nov 2014 06:38:06 +0100 Subject: [PATCH] Add 1st version of calculator but need work --- htdocs/compta/loan/calc.php | 385 ++++++++++++++++++++++++ htdocs/core/menus/init_menu_auguria.sql | 1 + htdocs/core/menus/standard/eldy.lib.php | 1 + htdocs/core/modules/modLoan.class.php | 8 + htdocs/langs/en_US/admin.lang | 1 + htdocs/langs/en_US/loan.lang | 2 + 6 files changed, 398 insertions(+) create mode 100644 htdocs/compta/loan/calc.php diff --git a/htdocs/compta/loan/calc.php b/htdocs/compta/loan/calc.php new file mode 100644 index 00000000000..5cc2c830094 --- /dev/null +++ b/htdocs/compta/loan/calc.php @@ -0,0 +1,385 @@ + + * Copyright (C) 2014 Alexandre Spangaro + * + * 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 . + */ + +require '../../main.inc.php'; + +$langs->load("loan"); + +/* --------------------------------------------------- * + * Set Form DEFAULT values + * --------------------------------------------------- */ +$default_sale_price = "150000"; +$default_annual_interest_percent = 7.0; +$default_year_term = 30; +$default_down_percent = 10; +$default_show_progress = TRUE; + +/* --------------------------------------------------- * + * Initialize Variables + * --------------------------------------------------- */ +$sale_price = 0; +$annual_interest_percent = 0; +$year_term = 0; +$down_percent = 0; +$this_year_interest_paid = 0; +$this_year_principal_paid = 0; +$form_complete = false; +$show_progress = false; +$monthly_payment = false; +$show_progress = false; +$error = false; + +/* --------------------------------------------------- * + * Set the USER INPUT values + * --------------------------------------------------- */ +if (isset($_REQUEST['form_complete'])) { + $sale_price = $_REQUEST['sale_price']; + $annual_interest_percent = $_REQUEST['annual_interest_percent']; + $year_term = $_REQUEST['year_term']; + $down_percent = $_REQUEST['down_percent']; + $show_progress = (isset($_REQUEST['show_progress'])) ? $_REQUEST['show_progress'] : false; + $form_complete = $_REQUEST['form_complete']; +} + +// This function does the actual mortgage calculations +// by plotting a PVIFA (Present Value Interest Factor of Annuity) +// table... +function get_interest_factor($year_term, $monthly_interest_rate) { + global $base_rate; + + $factor = 0; + $base_rate = 1 + $monthly_interest_rate; + $denominator = $base_rate; + for ($i=0; $i < ($year_term * 12); $i++) { + $factor += (1 / $denominator); + $denominator *= $base_rate; + } + return $factor; +} + +// If the form is complete, we'll start the math +if ($form_complete) { + // We'll set all the numeric values to JUST + // numbers - this will delete any dollars signs, + // commas, spaces, and letters, without invalidating + // the value of the number + $sale_price = preg_replace( "[^0-9.]", "", $sale_price); + $annual_interest_percent = preg_replace( "[^0-9.]", "", $annual_interest_percent); + $year_term = preg_replace( "[^0-9.]", "", $year_term); + $down_percent = preg_replace( "[^0-9.]", "", $down_percent); + + if (((float) $year_term <= 0) || ((float) $sale_price <= 0) || ((float) $annual_interest_percent <= 0)) { + $error = "You must enter a Sale Price of Home, Length of Motgage and Annual Interest Rate"; + } + + if (!$error) { + $month_term = $year_term * 12; + $down_payment = $sale_price * ($down_percent / 100); + $annual_interest_rate = $annual_interest_percent / 100; + $monthly_interest_rate = $annual_interest_rate / 12; + $financing_price = $sale_price - $down_payment; + $monthly_factor = get_interest_factor($year_term, $monthly_interest_rate); + $monthly_payment = $financing_price / $monthly_factor; + } +} else { + if (!$sale_price) { $sale_price = $default_sale_price; } + if (!$annual_interest_percent) { $annual_interest_percent = $default_annual_interest_percent; } + if (!$year_term) { $year_term = $default_year_term; } + if (!$down_percent) { $down_percent = $default_down_percent; } + if (!$show_progress) { $show_progress = $default_show_progress; } +} + +if ($error) { + print("" . $error . "

\n"); + $form_complete = false; +} + +/* + * View + */ + +llxHeader(); + +echo "This mortgage calculator can be used to figure out monthly payments of a home mortgage loan, + based on the home's sale price, the term of the loan desired, buyer's down payment percentage, and the loan's interest rate. + This calculator factors in PMI (Private Mortgage Insurance) for loans where less than 20% is put as a down payment. Also taken + into consideration are the town property taxes, and their effect on the total monthly mortgage payment.
"; + +print '
'; +print ''; +print ''; +print ''; +print ''; +print ''; +print ''; +print ''; +print ''; +print ''; +print ''; +print ''; +print ''; +print ''; +print ''; +print ''; +print ''; +print ''; +print ''; +print ''; +print ''; +print ''; +print ''; +print ''; +print ''; +print ''; +print ''; +print ''; + +if (! empty($show_progress)) +{ + print ''; +} +else +{ + print ''; +} + +print ''; +print '
Purchase & Financing Information
Sale Price of Home: '.$langs->trans("Currency".$conf->currency).'
Percentage Down:%
Length of Mortgage:years
Annual Interest Rate:%
Explain Calculations:Show me the calculations and amortizationShow me the calculations and amortization
'; + +print '
    '; +print '
'; + +// If the form has already been calculated, the $down_payment +// and $monthly_payment variables will be figured out, so we can show them in this table +if ($form_complete && $monthly_payment) +{ + print '
'; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + + if ($down_percent < 20) + { + $pmi_per_month = 55 * ($financing_price / 100000); + + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + } + + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; +} + +print '
Mortgage Payment Information
Down Payment:' . number_format($down_payment, "2", ".", ",") . ' ' . $langs->trans("Currency".$conf->currency) . '
Amount Financed:' . number_format($financing_price, "2", ".", ",") . ' ' . $langs->trans("Currency".$conf->currency) . '
Monthly Payment:' . number_format($monthly_payment, "2", ".", ",") . ' ' . $langs->trans("Currency".$conf->currency) . '
(Principal & Interest ONLY)
 '; + print '
'; + echo 'Since you are putting LESS than 20% down, you will need to pay PMI + (Private Mortgage Insurance), which tends + to be about $55 per month for every $100,000 financed (until you have paid off 20% of your loan). This could add + '."\$" . number_format($pmi_per_month, "2", ".", ",").' to your monthly payment.'; + print '
Monthly Payment:' . number_format(($monthly_payment + $pmi_per_month), "2", ".", ",") . $langs->trans("Currency".$conf->currency) . '
'; + print '(Principal & Interest, and PMI)
 '; + print '
'; + + $assessed_price = ($sale_price * .85); + $residential_yearly_tax = ($assessed_price / 1000) * 14; + $residential_monthly_tax = $residential_yearly_tax / 12; + + if ($pmi_per_month) + { + $pmi_text = "PMI and "; + } + + echo "Residential (or Property) Taxes are a little harder to figure out... In Massachusetts, the average resedential tax rate seems + to be around $14 per year for every $1,000 of your property's assessed value."; + + print '

'; + print "Let's say that your property's assessed value is 85% of what you actually paid for it - "; + print number_format($assessed_price, "2", ".", ",") . ' ' . $langs->trans("Currency".$conf->currency) . 'This would mean that your yearly residential taxes will be around'; + print number_format($residential_yearly_tax, "2", ".", ",") . ' ' . $langs->trans("Currency".$conf->currency); + print 'This could add ' . number_format($residential_monthly_tax, "2", ".", ",") . ' ' . $langs->trans("Currency".$conf->currency) . 'to your monthly payment'; + print '
TOTAL Monthly Payment:' . number_format(($monthly_payment + $pmi_per_month + $residential_monthly_tax), "2", ".", ",") . ' ' . $langs->trans("Currency".$conf->currency) . '
'; + print '(including '.$pmi_text.' residential tax)
'; +print '
'; + +// This prints the calculation progress and +// the instructions of HOW everything is figured +// out +if ($form_complete && $show_progress) { + $step = 1; + + print '

'; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print '
'. $step++ .''; + echo 'The down payment + = The price of the home multiplied by the percentage down divided by 100 (for 5% down becomes 5/100 or 0.05)

+ ' . number_format($down_payment,"2",".",",") . ' ' . $langs->trans("Currency".$conf->currency) . ' = '.number_format($sale_price,"2",".",",") . ' ' . $langs->trans("Currency".$conf->currency) . ' X + ('.$down_percent.' / 100)'; + print '
' . $step++ . ''; + print 'The interest rate = The annual interest percentage divided by 100

'; + print $annual_interest_rate . ' = ' . $annual_interest_percent . '% / 100'; + print '
'; + print 'The monthly factor = The result of the following formula:'; + print '
' . $step++ . ''; + print 'The monthly interest rate = The annual interest rate divided by 12 (for the 12 months in a year)

'; + print $monthly_interest_rate . ' = ' . $annual_interest_rate . ' / 12'; + print '
' . $step++ . ''; + print 'The month term of the loan in months = The number of years you\'ve taken the loan out for times 12

'; + print $month_term . ' Months = ' . $year_term . ' Years X 12'; + print '
' . $step++ . ''; + print 'The montly payment is figured out using the following formula:
'; + print 'Monthly Payment = ' . number_format($financing_price, "2", "", "") . ' * '; + print number_format($monthly_interest_rate, "4", "", "") . ' / '; + print '(1 - ((1 + ' . number_format($monthly_interest_rate, "4", "", "") . ')'; + print '-(' . $month_term . '))))'; + print '

'; + print 'The amortization breaks down how much of your monthly payment goes towards the bank\'s interest,'; + print 'and how much goes into paying off the principal of your loan.'; + print '
'; + print '
'; + + + // Set some base variables + $principal = $financing_price; + $current_month = 1; + $current_year = 1; + + // This basically, re-figures out the monthly payment, again. + $power = -($month_term); + $denom = pow((1 + $monthly_interest_rate), $power); + $monthly_payment = $principal * ($monthly_interest_rate / (1 - $denom)); + + print("

Amortization For Monthly Payment: \$" . number_format($monthly_payment, "2", ".", ",") . " over " . $year_term . " years
\n"); + + print ''; + + // This LEGEND will get reprinted every 12 months + $legend = ''; + $legend.= ''; + $legend.= ''; + $legend.= ''; + $legend.= ''; + $legend.= ''; + + echo $legend; + + // Loop through and get the current month's payments for + // the length of the loan + while ($current_month <= $month_term) + { + $interest_paid = $principal * $monthly_interest_rate; + $principal_paid = $monthly_payment - $interest_paid; + $remaining_balance = $principal - $principal_paid; + + $this_year_interest_paid = $this_year_interest_paid + $interest_paid; + $this_year_principal_paid = $this_year_principal_paid + $principal_paid; + + $var = !$var; + print ""; + print ''; + print ''; + print ''; + print ''; + print ''; + + ($current_month % 12) ? $show_legend = FALSE : $show_legend = TRUE; + + if ($show_legend) { + print ''; + print ''; + print ''; + + $total_spent_this_year = $this_year_interest_paid + $this_year_principal_paid; + print ''; + print ''; + print ''; + print ''; + + print ''; + print ''; + print ''; + + $current_year++; + $this_year_interest_paid = 0; + $this_year_principal_paid = 0; + + if (($current_month + 6) < $month_term) + { + echo $legend; + } + } + $principal = $remaining_balance; + $current_month++; + } + print("
' . $langs->trans("Month") . '' . $langs->trans("Interest") . '' . $langs->trans("Capital") . '' . $langs->trans("Position") . '
' . $current_month . '' . number_format($interest_paid, "2", ".", ",") . ' ' . $langs->trans("Currency".$conf->currency) . '' . number_format($principal_paid, "2", ".", ",") . ' ' . $langs->trans("Currency".$conf->currency) . '' . number_format($remaining_balance, "2", ".", ",") . ' ' . $langs->trans("Currency".$conf->currency) . '
' . $langs->trans("Totalsforyear") . ' ' . $current_year . '
 '; + print 'You will spend ' . number_format($total_spent_this_year, "2", ".", ",") . ' ' . $langs->trans("Currency".$conf->currency) . ' on your house in year ' . $current_year . '
'; + print number_format($this_year_interest_paid, "2", ".", ",") . ' ' . $langs->trans("Currency".$conf->currency) . ' will go towards INTEREST
'; + print number_format($this_year_principal_paid, "2", ".", ",") . ' ' . $langs->trans("Currency".$conf->currency) . ' will go towards PRINCIPAL
'; + print '
 
\n"); +} + +llxFooter(); + +$db->close(); diff --git a/htdocs/core/menus/init_menu_auguria.sql b/htdocs/core/menus/init_menu_auguria.sql index bf35ac2806e..a0a91cab781 100644 --- a/htdocs/core/menus/init_menu_auguria.sql +++ b/htdocs/core/menus/init_menu_auguria.sql @@ -181,6 +181,7 @@ insert into llx_menu (module, enabled, menu_handler, type, rowid, mainmenu, left insert into llx_menu (module, enabled, menu_handler, type, rowid, mainmenu, leftmenu, fk_menu, url, titre, level, langs, perms, target, usertype, position, entity) values ('', '$conf->loan->enabled', __HANDLER__, 'left', 2220__+MAX_llx_menu__, 'accountancy', 'tax_loan', 2200__+MAX_llx_menu__, '/compta/loan/index.php?leftmenu=tax_loan&mainmenu=accountancy', 'Loans', 1, 'loan', '$user->rights->loan->read', '', 0, 1, __ENTITY__); insert into llx_menu (module, enabled, menu_handler, type, rowid, mainmenu, leftmenu, fk_menu, url, titre, level, langs, perms, target, usertype, position, entity) values ('', '$conf->loan->enabled && $leftmenu=="tax_loan"', __HANDLER__, 'left', 2221__+MAX_llx_menu__, 'accountancy', '', 2220__+MAX_llx_menu__, '/compta/loan/card.php?leftmenu=tax_loan&action=create', 'NewLoan', 2, 'loan', '$user->rights->loan->write', '', 0, 2, __ENTITY__); insert into llx_menu (module, enabled, menu_handler, type, rowid, mainmenu, leftmenu, fk_menu, url, titre, level, langs, perms, target, usertype, position, entity) values ('', '$conf->loan->enabled && $leftmenu=="tax_loan"', __HANDLER__, 'left', 2222__+MAX_llx_menu__, 'accountancy', '', 2220__+MAX_llx_menu__, '/compta/loan/index.php?leftmenu=tax_loan', 'Payments', 2, 'companies', '$user->rights->loan->read', '', 0, 3, __ENTITY__); +insert into llx_menu (module, enabled, menu_handler, type, rowid, mainmenu, leftmenu, fk_menu, url, titre, level, langs, perms, target, usertype, position, entity) values ('', '$conf->loan->enabled && $leftmenu=="tax_loan"', __HANDLER__, 'left', 2223__+MAX_llx_menu__, 'accountancy', '', 2220__+MAX_llx_menu__, '/compta/loan/calc.php?leftmenu=tax_loan', 'Calculator', 2, 'companies', '$user->rights->loan->calc', '', 0, 4, __ENTITY__); insert into llx_menu (module, enabled, menu_handler, type, rowid, mainmenu, leftmenu, fk_menu, url, titre, level, langs, perms, target, usertype, position, entity) values ('', '$conf->tax->enabled', __HANDLER__, 'left', 2250__+MAX_llx_menu__, 'accountancy', 'tax_social', 2200__+MAX_llx_menu__, '/compta/sociales/index.php?leftmenu=tax_social', 'SocialContributions', 1, '', '$user->rights->tax->charges->lire', '', 0, 1, __ENTITY__); insert into llx_menu (module, enabled, menu_handler, type, rowid, mainmenu, leftmenu, fk_menu, url, titre, level, langs, perms, target, usertype, position, entity) values ('', '$conf->tax->enabled && $leftmenu=="tax_social"', __HANDLER__, 'left', 2251__+MAX_llx_menu__, 'accountancy', '', 2250__+MAX_llx_menu__, '/compta/sociales/charges.php?leftmenu=tax_social&action=create', 'MenuNewSocialContribution', 2, '', '$user->rights->tax->charges->creer', '', 0, 2, __ENTITY__); insert into llx_menu (module, enabled, menu_handler, type, rowid, mainmenu, leftmenu, fk_menu, url, titre, level, langs, perms, target, usertype, position, entity) values ('', '$conf->tax->enabled && $leftmenu=="tax_social"', __HANDLER__, 'left', 2252__+MAX_llx_menu__, 'accountancy', '', 2250__+MAX_llx_menu__, '/compta/charges/index.php?leftmenu=tax_social&mainmenu=accountancy&mode=sconly', 'Payments', 2, '', '$user->rights->tax->charges->lire', '', 0, 3, __ENTITY__); diff --git a/htdocs/core/menus/standard/eldy.lib.php b/htdocs/core/menus/standard/eldy.lib.php index 87c88ac028c..61ad1a4163f 100644 --- a/htdocs/core/menus/standard/eldy.lib.php +++ b/htdocs/core/menus/standard/eldy.lib.php @@ -817,6 +817,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/calc.php?leftmenu=tax_loan",$langs->trans("Calculator"),2,$user->rights->loan->calc); } // Social contributions diff --git a/htdocs/core/modules/modLoan.class.php b/htdocs/core/modules/modLoan.class.php index fb2de1e3a02..21ae96eba26 100644 --- a/htdocs/core/modules/modLoan.class.php +++ b/htdocs/core/modules/modLoan.class.php @@ -118,6 +118,14 @@ class modLoan extends DolibarrModules $this->rights[$r][3] = 0; $this->rights[$r][4] = 'delete'; $this->rights[$r][5] = ''; + + $r++; + $this->rights[$r][0] = 525; + $this->rights[$r][1] = 'Access loan calculator'; + $this->rights[$r][2] = 'r'; + $this->rights[$r][3] = 0; + $this->rights[$r][4] = 'calc'; + $this->rights[$r][5] = ''; $r++; $this->rights[$r][0] = 527; diff --git a/htdocs/langs/en_US/admin.lang b/htdocs/langs/en_US/admin.lang index a56a68f3b66..b1d69ae015d 100644 --- a/htdocs/langs/en_US/admin.lang +++ b/htdocs/langs/en_US/admin.lang @@ -698,6 +698,7 @@ Permission517=Export salaries Permission520=Read Loans Permission522=Create/modify loans Permission524=Delete loans +Permission525=Access loan calculator Permission527=Export loans Permission531=Read services Permission532=Create/modify services diff --git a/htdocs/langs/en_US/loan.lang b/htdocs/langs/en_US/loan.lang index 9ccff4eecf4..e59fc7898b8 100644 --- a/htdocs/langs/en_US/loan.lang +++ b/htdocs/langs/en_US/loan.lang @@ -17,6 +17,8 @@ ConfirmPayLoan=Confirm classify paid this loan 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. +# Calc +Totalsforyear=Totals for year # Admin ConfigLoan=Configuration of the module loan LOAN_ACCOUNTING_ACCOUNT_CAPITAL=Accountancy code capital by default