30require
'../main.inc.php';
38require_once DOL_DOCUMENT_ROOT.
'/loan/class/loan.class.php';
39require_once DOL_DOCUMENT_ROOT.
'/core/lib/loan.lib.php';
40require_once DOL_DOCUMENT_ROOT.
'/core/lib/date.lib.php';
41require_once DOL_DOCUMENT_ROOT.
'/loan/class/loanschedule.class.php';
42require_once DOL_DOCUMENT_ROOT.
'/loan/class/paymentloan.class.php';
43require_once DOL_DOCUMENT_ROOT.
'/core/class/html.formprojet.class.php';
45 require_once DOL_DOCUMENT_ROOT.
'/projet/class/project.class.php';
49$action =
GETPOST(
'action',
'aZ09');
57 $socid = $user->socid;
59if (!$user->hasRight(
'loan',
'calc')) {
64$langs->loadLangs(array(
"compta",
"bills",
"loan"));
70$echeances->fetchAll(
$object->id);
72if (
$object->paid > 0 && count($echeances->lines) == 0) {
73 $pay_without_schedule = 1;
75 $pay_without_schedule = 0;
78$permissiontoadd = $user->hasRight(
'loan',
'write');
85if ($action ==
'createecheancier' && empty($pay_without_schedule) && $permissiontoadd) {
89 while ($i < $object->nbterm + 1) {
101 $amort = (float) $mens - (
float) $int;
106 $new_echeance->fk_loan =
$object->id;
107 $new_echeance->datec =
dol_now();
108 $new_echeance->tms =
dol_now();
109 $new_echeance->datep = $date;
110 $new_echeance->amount_capital = $amort;
111 $new_echeance->amount_insurance = $insurance;
112 $new_echeance->amount_interest = $int;
113 $new_echeance->fk_typepayment = 3;
114 $new_echeance->fk_bank = 0;
115 $new_echeance->fk_user_creat = $user->id;
116 $new_echeance->fk_user_modif = $user->id;
117 $result = $new_echeance->create($user);
119 setEventMessages($new_echeance->error, $new_echeance->errors,
'errors');
121 $echeances->lines = [];
124 $echeances->lines[] = $new_echeance;
132if ($action ==
'updateecheancier' && empty($pay_without_schedule) && $permissiontoadd) {
136 while ($i < $object->nbterm + 1) {
148 $amort = (float) $mens - (
float) $int;
152 $new_echeance->fetch(
$id);
153 $new_echeance->tms =
dol_now();
154 $new_echeance->amount_capital = $amort;
155 $new_echeance->amount_insurance = $insurance;
156 $new_echeance->amount_interest = $int;
157 $new_echeance->fk_user_modif = $user->id;
158 $result = $new_echeance->update($user, 0);
160 setEventMessages(
null, $new_echeance->errors,
'errors');
162 $echeances->fetchAll(
$object->id);
166 $echeances->lines[$i - 1] = $new_echeance;
179$form =
new Form($db);
182$title = $langs->trans(
"Loan").
' - '.$langs->trans(
"FinancialCommitment");
183$help_url =
'EN:Module_Loan|FR:Module_Emprunt';
185llxHeader(
"", $title, $help_url,
'', 0, 0,
'',
'',
'',
'mod-loan page-card_schedule');
188print
dol_get_fiche_head($head,
'FinancialCommitment', $langs->trans(
"Loan"), -1,
'money-bill-alt');
190$linkback =
'<a href="'.DOL_URL_ROOT.
'/loan/list.php?restore_lastsearch_values=1">'.$langs->trans(
"BackToList").
'</a>';
192$morehtmlref =
'<div class="refidno">';
194$morehtmlref .= $form->editfieldkey(
"Label",
'label',
$object->label, $object, 0,
'string',
'', 0, 1);
195$morehtmlref .= $form->editfieldval(
"Label",
'label',
$object->label, $object, 0,
'string',
'',
null,
null,
'', 1);
198 $langs->loadLangs(array(
"projects"));
199 $morehtmlref .=
'<br>'.$langs->trans(
'Project').
' : ';
200 if ($user->hasRight(
'loan',
'write')) {
201 if ($action !=
'classify') {
203 if ($action ==
'classify') {
205 $morehtmlref .=
'<form method="post" action="'.$_SERVER[
'PHP_SELF'].
'?id='.
$object->id.
'">';
206 $morehtmlref .=
'<input type="hidden" name="action" value="classin">';
207 $morehtmlref .=
'<input type="hidden" name="token" value="'.newToken().
'">';
208 $morehtmlref .= $formproject->select_projects(-1, (
string)
$object->fk_project,
'projectid', 16, 0, 1, 0, 1, 0, 0,
'', 1);
209 $morehtmlref .=
'<input type="submit" class="button valignmiddle" value="'.$langs->trans(
"Modify").
'">';
210 $morehtmlref .=
'</form>';
212 $morehtmlref .= $form->form_project($_SERVER[
'PHP_SELF'].
'?id='.
$object->id, -1, (
string)
$object->fk_project,
'none', 0, 0, 0, 1,
'',
'maxwidth300');
216 if (!empty(
$object->fk_project)) {
218 $proj->fetch(
$object->fk_project);
219 $morehtmlref .=
' : '.$proj->getNomUrl(1);
221 $morehtmlref .=
' - '.$proj->title;
228$morehtmlref .=
'</div>';
232dol_banner_tab($object,
'loanid', $linkback, 1,
'rowid',
'ref', $morehtmlref,
'', 0,
'', $morehtmlstatus);
235<script
type=
"text/javascript">
236$(document).ready(
function() {
240 function updateTotals() {
246 $(
'[name^="hi_insurance"]').each(
function() {
247 totInsu += (price2numjs($(
this).val()) || 0);
249 $(
'[name^="hi_interets"]').each(
function() {
250 totInt += (price2numjs($(
this).val()) || 0);
252 $(
'[id^="hi_amort"]').each(
function() {
253 totAmort += (price2numjs($(
this).val()) || 0);
255 $(
'[id^="hi_mens"]').each(
function() {
256 totMens += (price2numjs($(
this).val()) || 0);
259 $(
'#total_insurance').text(pricejs(totInsu,
'MT',
'<?php echo $conf->currency; ?>'));
260 $(
'#total_interest').text(pricejs(totInt,
'MT',
'<?php echo $conf->currency; ?>'));
261 $(
'#total_amort').text(pricejs(totAmort,
'MT',
'<?php echo $conf->currency; ?>'));
262 $(
'#total_mens').text(pricejs(totMens,
'MT',
'<?php echo $conf->currency; ?>'));
265 $(
'[name^="mens"]').on(
'keyup change',
function() {
266 clearTimeout(timeout);
268 timeout = setTimeout(() => {
269 var echeance = $this.attr(
'ech');
270 var mens = $this.val();
271 var amort = $(
'#amort' + echeance).val();
272 var interet = $(
'#interets' + echeance).val();
273 calculateMens(echeance, mens, amort,
'mens', 0, interet);
277 $(
'[name^="amort"]').on(
'keyup change',
function() {
278 clearTimeout(timeout);
280 timeout = setTimeout(() => {
281 var echeance = $this.attr(
'ech');
282 var amort = $this.val();
283 var mens = $(
'#mens' + echeance).val();
284 var interet = $(
'#interets' + echeance).val();
285 calculateMens(echeance, mens, amort,
'amort', 0, interet);
289 $(
'[name^="interets"]').on(
'keyup change',
function() {
290 clearTimeout(timeout);
292 timeout = setTimeout(() => {
293 var echeance = $this.attr(
'ech');
294 var interet = $this.val();
295 var amort = $(
'#amort' + echeance).val();
296 var mens = $(
'#mens' + echeance).val();
297 calculateMens(echeance, mens, amort,
'interet', 0, interet);
301 $(
'#btn_apply_grace_period').on(
'click',
function(e) {
303 var months = parseInt($(
'#grace_period_months').val(), 10);
304 if (isNaN(months) || months < 1)
return;
305 var nbterm = <?php echo (
int)
$object->nbterm; ?>;
306 if (months >= nbterm) months = nbterm - 1;
308 calculateMens(1, 0, 0,
'amort', months, 0);
311 function calculateMens(echeance, mens, amort, source, grace_period, interet) {
312 var idcap = echeance - 1;
313 idcap =
'#hi_capital' + idcap;
314 var capital = price2numjs($(idcap).val());
315 console.log(
"calculateMens echeance=" + echeance +
" idcap=" + idcap +
" capital=" + capital +
" source=" + source +
" grace=" + grace_period);
323 mens: price2numjs(mens),
324 amort: price2numjs(amort),
325 interet: price2numjs(interet),
327 grace_period: grace_period,
329 rate: <?php echo
$object->rate / 100; ?>,
330 nbterm: <?php echo
$object->nbterm; ?>,
331 token:
'<?php echo currentToken(); ?>'
333 success:
function(data) {
334 $.each(data,
function(index, element) {
335 $(
'#hi_capital' + index).val(element.cap_rest);
336 $(
'#capital' + index).text(element.cap_rest_str);
338 if ($(
'#interets' + index).is(
'input')) {
339 $(
'#interets' + index).val(element.interet);
341 $(
'#interets' + index).text(element.interet_str);
343 $(
'#hi_interets' + index).val(element.interet);
345 if ($(
'#amort' + index).is(
'input')) {
346 $(
'#amort' + index).val(element.amort);
348 $(
'#amort' + index).text(element.amort);
350 $(
'#hi_amort' + index).val(element.amort_num);
352 if ($(
'#mens' + index).is(
'input')) {
353 $(
'#mens' + index).val(element.mens);
355 $(
'#mens' + index).text(element.mens);
357 $(
'#hi_mens' + index).val(element.mens_num);
369if ($pay_without_schedule == 1) {
370 print
'<div class="warning">'.$langs->trans(
'CantUseScheduleWithLoanStartedToPaid').
'</div>'.
"\n";
373print
'<form name="createecheancier" action="'.$_SERVER[
"PHP_SELF"].
'" method="POST">';
374print
'<input type="hidden" name="token" value="'.newToken().
'">';
375print
'<input type="hidden" name="loanid" value="'.$loanid.
'">';
376if (count($echeances->lines) > 0) {
377 print
'<input type="hidden" name="action" value="updateecheancier">';
379 print
'<input type="hidden" name="action" value="createecheancier">';
382if (empty($pay_without_schedule) && $permissiontoadd) {
383 print
'<div class="marginbottomonly inline-block valignmiddle">';
384 print
'<span class="opacitymedium">'.$langs->trans(
"GracePeriodMonths").
': </span>';
385 print
'<input type="number" id="grace_period_months" min="1" max="'.max(1,
$object->nbterm - 1).
'" value="1" class="width50 right"> ';
386 print
'<input type="button" id="btn_apply_grace_period" class="button valignmiddle" value="'.$langs->trans(
"Apply").
'">';
391print
'<div class="div-table-responsive-no-min">';
392print
'<table class="border centpercent">';
395if (count($echeances->lines) > 0) {
399print
'<tr class="liste_titre">';
400print
'<th class="center">'.$langs->trans(
"Term").
'</th>';
401print
'<th class="center">'.$langs->trans(
"Date").
'</th>';
402print
'<th class="center">'.$langs->trans(
"Insurance").
'</th>';
403print
'<th class="center">'.$langs->trans(
"InterestAmount").
'</th>';
404print
'<th class="center">'.$langs->trans(
"CapitalAmortization").
'</th>';
405print
'<th class="center">'.$langs->trans(
"Amount").
'</th>';
406print
'<th class="center">'.$langs->trans(
"CapitalRemain");
407print
'<br>('.price(
$object->capital, 0,
'', 1, -1, -1,
$conf->currency).
')';
408print
'<input type="hidden" name="hi_capital0" id ="hi_capital0" value="'.$object->capital.
'">';
410if (count($echeances->lines) > 0) {
411 print
'<th class="center">'.$langs->trans(
'DoPayment').
'</th>';
416$total_insurance = 0.0;
417$total_interest = 0.0;
421if (
$object->nbterm > 0 && count($echeances->lines) == 0) {
424 $cap_rest = (float) $capital;
426 $insurance =
price2num($insurance,
'MT');
429 while ($i < $object->nbterm + 1) {
430 $mens =
price2num($echeances->calcMonthlyPayments($capital,
$object->rate / 100,
$object->nbterm - $i + 1),
'MT');
431 $int = ($capital * (
$object->rate / 12)) / 100;
433 $amort =
price2num((
float) $mens - (
float) $int,
'MT');
434 $insu = ((float) $insurance + (($i == 1) ? (float) $regulInsurance : 0));
435 $cap_rest = (float)
price2num((
float) $capital - (float) $amort,
'MT');
438 if ($i ==
$object->nbterm && abs($cap_rest) <= 0.05 && $capital > 0) {
441 $mens =
price2num((
float) $amort + (
float) $int,
'MT');
444 $total_insurance += $insu;
445 $total_interest += $int;
446 $total_amort += $amort;
447 $total_mens += $mens;
450 print
'<td class="center" id="n'.$i.
'">'.$i.
'</td>';
452 print
'<td class="center amount" id="insurance'.$i.
'">'.
price($insu, 0,
'', 1, -1, -1,
$conf->currency).
'</td><input type="hidden" name="hi_insurance'.$i.
'" id ="hi_insurance'.$i.
'" value="'.$insu.
'">';
453 print
'<td class="center"><input class="width75 right" name="interets'.$i.
'" id="interets'.$i.
'" value="'.
price($int).
'" ech="'.$i.
'"><input type="hidden" name="hi_interets'.$i.
'" id="hi_interets'.$i.
'" value="'.$int.
'"></td>';
454 print
'<td class="center"><input class="width75 right" name="amort'.$i.
'" id="amort'.$i.
'" value="'.
price($amort).
'" ech="'.$i.
'"><input type="hidden" name="hi_amort'.$i.
'" id="hi_amort'.$i.
'" value="'.$amort.
'"></td>';
455 print
'<td class="center"><input class="width75 right" name="mens'.$i.
'" id="mens'.$i.
'" value="'.
price($mens).
'" ech="'.$i.
'"><input type="hidden" name="hi_mens'.$i.
'" id="hi_mens'.$i.
'" value="'.$mens.
'"></td>';
456 print
'<td class="center amount" id="capital'.$i.
'">'.
price($cap_rest, 0,
'', 1, -1, -1,
$conf->currency).
'</td><input type="hidden" name="hi_capital'.$i.
'" id ="hi_capital'.$i.
'" value="'.$cap_rest.
'">';
459 $capital = $cap_rest;
461} elseif (count($echeances->lines) > 0) {
464 $cap_rest = (float) $capital;
466 $insurance =
price2num($insurance,
'MT');
470 foreach ($echeances->lines as $line) {
471 $mens = $line->amount_capital + $line->amount_interest;
472 $int = $line->amount_interest;
473 $amort = $line->amount_capital;
474 $insu = ((float) $insurance + (($i == 1) ? (float) $regulInsurance : 0));
475 $cap_rest =
price2num($capital - $amort,
'MT');
477 $total_insurance += $insu;
478 $total_interest += $int;
479 $total_amort += $amort;
480 $total_mens += $mens;
483 print
'<td class="center" id="n'.$i.
'"><input type="hidden" name="hi_rowid'.$i.
'" id ="hi_rowid'.$i.
'" value="'.$line->id.
'">'.$i.
'</td>';
484 print
'<td class="center" id ="date'.$i.
'"><input type="hidden" name="hi_date'.$i.
'" id ="hi_date'.$i.
'" value="'.$line->datep.
'">'.
dol_print_date($line->datep,
'day').
'</td>';
485 print
'<td class="center amount" id="insurance'.$i.
'">'.
price($insu, 0,
'', 1, -1, -1,
$conf->currency).
'</td><input type="hidden" name="hi_insurance'.$i.
'" id ="hi_insurance'.$i.
'" value="'.$insu.
'">';
487 if (empty($line->fk_bank)) {
488 print
'<td class="center"><input class="right width75" name="interets'.$i.
'" id="interets'.$i.
'" value="'.
price($int).
'" ech="'.$i.
'"><input type="hidden" name="hi_interets'.$i.
'" id="hi_interets'.$i.
'" value="'.$int.
'"></td>';
489 print
'<td class="center"><input class="right width75" name="amort'.$i.
'" id="amort'.$i.
'" value="'.
price($amort).
'" ech="'.$i.
'"><input type="hidden" name="hi_amort'.$i.
'" id="hi_amort'.$i.
'" value="'.$amort.
'"></td>';
490 print
'<td class="center"><input class="right width75" name="mens'.$i.
'" id="mens'.$i.
'" value="'.
price($mens).
'" ech="'.$i.
'"><input type="hidden" name="hi_mens'.$i.
'" id="hi_mens'.$i.
'" value="'.$mens.
'"></td>';
492 print
'<td class="center amount" id="interets'.$i.
'">'.
price($int, 0,
'', 1, -1, -1,
$conf->currency).
'</td><input type="hidden" name="interets'.$i.
'" id="interets'.$i.
'" value="'.$int.
'"><input type="hidden" name="hi_interets'.$i.
'" id="hi_interets'.$i.
'" value="'.$int.
'">';
493 print
'<td class="center amount" id="amort'.$i.
'">'.
price($amort, 0,
'', 1, -1, -1,
$conf->currency).
'</td><input type="hidden" name="amort'.$i.
'" id ="amort'.$i.
'" value="'.$amort.
'"><input type="hidden" name="hi_amort'.$i.
'" id="hi_amort'.$i.
'" value="'.$amort.
'">';
494 print
'<td class="center amount" id="mens'.$i.
'">'.
price($mens, 0,
'', 1, -1, -1,
$conf->currency).
'</td><input type="hidden" name="mens'.$i.
'" id ="mens'.$i.
'" value="'.$mens.
'"><input type="hidden" name="hi_mens'.$i.
'" id="hi_mens'.$i.
'" value="'.$mens.
'">';
497 print
'<td class="center amount" id="capital'.$i.
'">'.
price($cap_rest, 0,
'', 1, -1, -1,
$conf->currency).
'</td><input type="hidden" name="hi_capital'.$i.
'" id ="hi_capital'.$i.
'" value="'.$cap_rest.
'">';
498 print
'<td class="center">';
499 if (!empty($line->fk_bank)) {
500 print $langs->trans(
'Paid');
501 if (!empty($line->fk_payment_loan)) {
502 print
' <a href="'.DOL_URL_ROOT.
'/loan/payment/card.php?id='.$line->fk_payment_loan.
'">('.img_object($langs->trans(
"Payment"),
"payment").
' '.$line->fk_payment_loan.
')</a>';
504 } elseif (!$printed) {
505 print
'<a class="butAction smallpaddingimp" href="'.DOL_URL_ROOT.
'/loan/payment/payment.php?id='.
$object->id.
'&action=create">'.$langs->trans(
'DoPayment').
'</a>';
511 $capital = $cap_rest;
515print
'<tr class="liste_total">';
516print
'<td class="center">'.$langs->trans(
"Total").
'</td>';
518print
'<td class="center amount" id="total_insurance">'.price($total_insurance, 0,
'', 1, -1, -1,
$conf->currency).
'</td>';
519print
'<td class="center amount" id="total_interest">'.price($total_interest, 0,
'', 1, -1, -1,
$conf->currency).
'</td>';
520print
'<td class="center amount" id="total_amort">'.price($total_amort, 0,
'', 1, -1, -1,
$conf->currency).
'</td>';
521print
'<td class="center amount" id="total_mens">'.price($total_mens, 0,
'', 1, -1, -1,
$conf->currency).
'</td>';
522print
'<td class="center amount" id="total_capital">'.price(0, 0,
'', 1, -1, -1,
$conf->currency).
'</td>';
523if (count($echeances->lines) > 0) {
533if (count($echeances->lines) == 0) {
534 $label = $langs->trans(
"Create");
536 $label = $langs->trans(
"Save");
538print
'<div class="center"><input type="submit" class="button button-add" value="'.$label.
'" '.(($pay_without_schedule == 1) ?
'disabled title="'.$langs->trans(
'CantUseScheduleWithLoanStartedToPaid').
'"' :
'').
'title=""></div>';
$id
Support class for third parties, contacts, members, users or resources.
if(! $sortfield) if(! $sortorder) $object
$propal type
'integer', 'integer:ObjectClass:PathToClass[:AddCreateButtonOrNot[:Filter[:Sortfield]]]',...
llxFooter($comment='', $zone='private', $disabledoutputofmessages=0)
Empty footer.
if(!defined('NOREQUIRESOC')) if(!defined( 'NOREQUIRETRAN')) if(!defined('NOTOKENRENEWAL')) if(!defined( 'NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined( 'NOREQUIREAJAX')) llxHeader($head='', $title='', $help_url='', $target='', $disablejs=0, $disablehead=0, $arrayofjs='', $arrayofcss='', $morequerystring='', $morecssonbody='', $replacemainareaby='', $disablenofollow=0, $disablenoindex=0)
Empty header.
Class to manage Schedule of loans.
Class to manage projects.
dol_time_plus_duree($time, $duration_value, $duration_unit, $ruleforendofmonth=0)
Add a delay to a date.
dol_now($mode='gmt')
Return date for now.
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
price($amount, $form=0, $outlangs='', $trunc=1, $rounding=-1, $forcerounding=-1, $currency_code='')
Function to format a value into an amount for visual output Function used into PDF and HTML pages.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0, $nodefault=0)
Return value of a param into GET or POST supervariable.
GETPOSTINT($paramname, $method=0, $nodefault=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false, $decorate=0)
Output date in a string format according to outputlangs (or langs if not defined).
GETPOSTISSET($paramname)
Return true if we are in a context of submitting the parameter $paramname from a POST of a form.
isModEnabled($module)
Is Dolibarr module enabled.
dol_get_fiche_head($links=array(), $active='', $title='', $notab=0, $picto='', $pictoisfullpath=0, $morehtmlright='', $morecss='', $limittoshow=0, $moretabssuffix='', $dragdropfile=0, $morecssdiv='')
Show tabs of a record.
loan_prepare_head($object)
Prepare array with list of tabs.
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.