Skip to content

Commit

Permalink
Merge pull request #6934 from atm-florian/dev_loancalc
Browse files Browse the repository at this point in the history
NEW : Add calculation function for Loan schedule
  • Loading branch information
eldy committed Jun 2, 2017
2 parents 832e9ed + b0f2db7 commit 4e39710
Show file tree
Hide file tree
Showing 7 changed files with 904 additions and 27 deletions.
21 changes: 20 additions & 1 deletion htdocs/install/mysql/migration/5.0.0-6.0.0.sql
Expand Up @@ -355,4 +355,23 @@ delete from llx_categorie_member where fk_categorie not in (select rowid from ll
delete from llx_categorie_contact where fk_categorie not in (select rowid from llx_categorie where type = 4);
delete from llx_categorie_project where fk_categorie not in (select rowid from llx_categorie where type = 5);

ALTER TABLE llx_inventory ADD COLUMN ref varchar(48);
ALTER TABLE llx_inventory ADD COLUMN ref varchar(48);

create table llx_loan_schedule
(
rowid integer AUTO_INCREMENT PRIMARY KEY,
fk_loan integer,
datec datetime,
tms timestamp,
datep datetime,
amount_capital real DEFAULT 0,
amount_insurance real DEFAULT 0,
amount_interest real DEFAULT 0,
fk_typepayment integer NOT NULL,
num_payment varchar(50),
note_private text,
note_public text,
fk_bank integer NOT NULL,
fk_user_creat integer,
fk_user_modif integer
)ENGINE=innodb;
37 changes: 37 additions & 0 deletions htdocs/install/mysql/tables/llx_loan_schedule.sql
@@ -0,0 +1,37 @@
-- ===================================================================
-- Copyright (C) 2014 Alexandre Spangaro <aspangaro.dolibarr@gmail.com>
-- Copyright (C) 2015 Frederic France <frederic.france@free.fr>
--
-- 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/>.
--
-- ===================================================================

create table llx_loan_schedule
(
rowid integer AUTO_INCREMENT PRIMARY KEY,
fk_loan integer,
datec datetime, -- creation date
tms timestamp,
datep datetime, -- payment date
amount_capital real DEFAULT 0,
amount_insurance real DEFAULT 0,
amount_interest real DEFAULT 0,
fk_typepayment integer NOT NULL,
num_payment varchar(50),
note_private text,
note_public text,
fk_bank integer NOT NULL,
fk_user_creat integer, -- creation user
fk_user_modif integer -- last modification user
)ENGINE=innodb;
1 change: 1 addition & 0 deletions htdocs/langs/en_US/loan.lang
Expand Up @@ -50,3 +50,4 @@ ConfigLoan=Configuration of the module loan
LOAN_ACCOUNTING_ACCOUNT_CAPITAL=Accounting account capital by default
LOAN_ACCOUNTING_ACCOUNT_INTEREST=Accounting account interest by default
LOAN_ACCOUNTING_ACCOUNT_INSURANCE=Accounting account insurance by default
CreateCalcSchedule=Créer / Modifier échéancier de pret
72 changes: 72 additions & 0 deletions htdocs/loan/calcmens.php
@@ -0,0 +1,72 @@
<?php
/* TVI
* Copyright (C) 2015 Florian HENRY <florian.henry@open-concept.pro>
*
* 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 2 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, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/

/**
* \file tvi/ajax/list.php
* \brief File to return datables output
*/
if (! defined('NOTOKENRENEWAL'))
define('NOTOKENRENEWAL', '1'); // Disables token renewal
if (! defined('NOREQUIREMENU'))
define('NOREQUIREMENU', '1');
// if (! defined('NOREQUIREHTML')) define('NOREQUIREHTML','1');
if (! defined('NOREQUIREAJAX'))
define('NOREQUIREAJAX', '1');
// if (! defined('NOREQUIRESOC')) define('NOREQUIRESOC','1');
// if (! defined('NOREQUIRETRAN')) define('NOREQUIRETRAN','1');


require '../main.inc.php';
require DOL_DOCUMENT_ROOT.'/loan/class/loanschedule.class.php';

$mens=GETPOST('mens');
$capital=GETPOST('capital');
$rate=GETPOST('rate');
$echance=GETPOST('echeance');
$nbterm=GETPOST('nbterm');

top_httphead();

$output=array();

$object = new LoanSchedule($db);

$int = ($capital*($rate/12));
$int = round($int ,2,PHP_ROUND_HALF_UP);
$cap_rest = round($capital - ($mens-$int),2,PHP_ROUND_HALF_UP);
$output[$echance]=array('cap_rest'=>$cap_rest,'cap_rest_str'=>price($cap_rest),'interet'=>$int,'interet_str'=>price($int,0,'',1),'mens'=>$mens);

$echance++;
$capital=$cap_rest;
while ($echance<=$nbterm) {

$mens = round($object->calc_mens($capital,$rate,$nbterm-$echance+1),2,PHP_ROUND_HALF_UP);

$int = ($capital*($rate/12));
$int = round($int ,2,PHP_ROUND_HALF_UP);
$cap_rest = round($capital - ($mens-$int),2,PHP_ROUND_HALF_UP);

$output[$echance]=array('cap_rest'=>$cap_rest,'cap_rest_str'=>price($cap_rest),'interet'=>$int,'interet_str'=>price($int,0,'',1),'mens'=>$mens);

$capital=$cap_rest;
$echance++;
}

echo json_encode($output);

66 changes: 40 additions & 26 deletions htdocs/loan/card.php
Expand Up @@ -72,7 +72,7 @@
setEventMessages($loan->error, null, 'errors');
}
}

// Delete loan
if ($action == 'confirm_delete' && $confirm == 'yes')
{
Expand All @@ -89,7 +89,7 @@
setEventMessages($loan->error, null, 'errors');
}
}

// Add loan
if ($action == 'add' && $user->rights->loan->write)
{
Expand All @@ -99,7 +99,7 @@
$dateend = dol_mktime(12, 0, 0, GETPOST('endmonth','int'), GETPOST('endday','int'), GETPOST('endyear','int'));
$capital = price2num(GETPOST('capital'));
$rate = GETPOST('rate');

if (! $capital)
{
$error++; $action = 'create';
Expand All @@ -120,7 +120,7 @@
$error++; $action = 'create';
setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("Rate")), null, 'errors');
}

if (! $error)
{
$object->label = GETPOST('label');
Expand All @@ -133,15 +133,15 @@
$object->note_private = GETPOST('note_private');
$object->note_public = GETPOST('note_public');
$object->fk_project = GETPOST('fk_project');

$accountancy_account_capital = GETPOST('accountancy_account_capital');
$accountancy_account_insurance = GETPOST('accountancy_account_insurance');
$accountancy_account_interest = GETPOST('accountancy_account_interest');

if ($accountancy_account_capital <= 0) { $object->account_capital = ''; } else { $object->account_capital = $accountancy_account_capital; }
if ($accountancy_account_insurance <= 0) { $object->account_insurance = ''; } else { $object->account_insurance = $accountancy_account_insurance; }
if ($accountancy_account_interest <= 0) { $object->account_interest = ''; } else { $object->account_interest = $accountancy_account_interest; }

$id=$object->create($user);
if ($id <= 0)
{
Expand All @@ -157,7 +157,7 @@
exit();
}
}

// Update record
else if ($action == 'update' && $user->rights->loan->write)
{
Expand All @@ -168,7 +168,7 @@
$datestart = dol_mktime(12, 0, 0, GETPOST('startmonth','int'), GETPOST('startday','int'), GETPOST('startyear','int'));
$dateend = dol_mktime(12, 0, 0, GETPOST('endmonth','int'), GETPOST('endday','int'), GETPOST('endyear','int'));
$capital = price2num(GETPOST('capital'));

if (! $capital)
{
setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("LoanCapital")), null, 'errors');
Expand All @@ -185,14 +185,14 @@
$accountancy_account_capital = GETPOST('accountancy_account_capital');
$accountancy_account_insurance = GETPOST('accountancy_account_insurance');
$accountancy_account_interest = GETPOST('accountancy_account_interest');

if ($accountancy_account_capital <= 0) { $object->account_capital = ''; } else { $object->account_capital = $accountancy_account_capital; }
if ($accountancy_account_insurance <= 0) { $object->account_insurance = ''; } else { $object->account_insurance = $accountancy_account_insurance; }
if ($accountancy_account_interest <= 0) { $object->account_interest = ''; } else { $object->account_interest = $accountancy_account_interest; }
}

$result = $object->update($user);

if ($result > 0)
{
header("Location: " . $_SERVER["PHP_SELF"] . "?id=" . $id);
Expand All @@ -209,7 +209,7 @@
exit;
}
}

// Link to a project
if ($action == 'classin' && $user->rights->loan->write)
{
Expand Down Expand Up @@ -307,12 +307,12 @@
$langs->load("projects");

print '<tr><td>'.$langs->trans("Project").'</td><td>';

$numproject=$formproject->select_projects(-1,GETPOST("fk_project"),'fk_project',16,0,1,1);

print '</td></tr>';
}

// Note Private
print '<tr>';
print '<td class="tdtop">'.$langs->trans('NotePrivate').'</td>';
Expand Down Expand Up @@ -352,7 +352,7 @@
print $formaccounting->select_account($object->accountancy_account_interest, 'accountancy_account_interest', 1, '', 0, 1);
print '</td></tr>';
}
else // For external software
else // For external software
{
// Accountancy_account_capital
print '<tr><td class="titlefieldcreate">'.$langs->trans("LoanAccountancyCapitalCode").'</td>';
Expand Down Expand Up @@ -417,10 +417,22 @@

dol_fiche_head($head, 'card', $langs->trans("Loan"), 0, 'bill');

print '<script type="text/javascript">' . "\n";
print ' function popEcheancier() {' . "\n";
print ' $div = $(\'<div id="popCalendar"><iframe width="100%" height="100%" frameborder="0" src="createschedule.php?loanid=' . $object->id . '"></iframe></div>\');' . "\n";
print ' $div.dialog({' . "\n";
print ' modal:true' . "\n";
print ' ,width:"90%"' . "\n";
print ' ,height:$(window).height() - 150' . "\n";
print ' });' . "\n";
print ' }' . "\n";
print '</script>';


// Loan card

$linkback = '<a href="' . DOL_URL_ROOT . '/loan/index.php">' . $langs->trans("BackToList") . '</a>';

$morehtmlref='<div class="refidno">';
// Ref loan
$morehtmlref.=$form->editfieldkey("Label", 'label', $object->label, $object, $user->rights->loan->write, 'string', '', 0, 1);
Expand Down Expand Up @@ -458,7 +470,7 @@
}
}
$morehtmlref.='</div>';

$object->totalpaid = $totalpaid; // To give a chance to dol_banner_tab to use already paid amount to show correct status

dol_banner_tab($object, 'id', $linkback, 1, 'rowid', 'ref', $morehtmlref, '', 0, '', $morehtmlright);
Expand Down Expand Up @@ -652,7 +664,7 @@
while ($i < $num)
{
$objp = $db->fetch_object($resql);

print '<tr class="oddeven">';
print '<td><a href="'.DOL_URL_ROOT.'/loan/payment/card.php?id='.$objp->rowid.'">'.img_object($langs->trans("Payment"),"payment").' '.$objp->rowid.'</a></td>';
print '<td>'.dol_print_date($db->jdate($objp->dp),'day')."</td>\n";
Expand Down Expand Up @@ -713,31 +725,33 @@
if (empty($reshook))
{
print '<div class="tabsAction">';

// Edit
if ($user->rights->loan->write)
{
print '<a href="javascript:popEcheancier()" class="butAction">'.$langs->trans('CreateCalcSchedule').'</a>';

print '<a class="butAction" href="'.DOL_URL_ROOT.'/loan/card.php?id='.$object->id.'&amp;action=edit">'.$langs->trans("Modify").'</a>';
}

// Emit payment
if ($object->paid == 0 && ((price2num($object->capital) > 0 && round($staytopay) < 0) || (price2num($object->capital) > 0 && round($staytopay) > 0)) && $user->rights->loan->write)
{
print '<a class="butAction" href="'.DOL_URL_ROOT.'/loan/payment/payment.php?id='.$object->id.'&amp;action=create">'.$langs->trans("DoPayment").'</a>';
}

// Classify 'paid'
if ($object->paid == 0 && round($staytopay) <=0 && $user->rights->loan->write)
{
print '<a class="butAction" href="'.DOL_URL_ROOT.'/loan/card.php?id='.$object->id.'&amp;action=paid">'.$langs->trans("ClassifyPaid").'</a>';
}

// Delete
if ($user->rights->loan->delete)
{
print '<a class="butActionDelete" href="'.DOL_URL_ROOT.'/loan/card.php?id='.$object->id.'&amp;action=delete">'.$langs->trans("Delete").'</a>';
}

print "</div>";
}
}
Expand Down

0 comments on commit 4e39710

Please sign in to comment.