28require
'../../main.inc.php';
36require_once DOL_DOCUMENT_ROOT.
'/loan/class/loan.class.php';
37require_once DOL_DOCUMENT_ROOT.
'/loan/class/loanschedule.class.php';
38require_once DOL_DOCUMENT_ROOT.
'/loan/class/paymentloan.class.php';
39require_once DOL_DOCUMENT_ROOT.
'/compta/bank/class/account.class.php';
40require_once DOL_DOCUMENT_ROOT.
'/core/lib/loan.lib.php';
42$langs->loadLangs(array(
"bills",
"loan"));
44$action =
GETPOST(
'action',
'aZ09');
45$confirm =
GETPOST(
'confirm',
'alpha');
46$cancel =
GETPOST(
'cancel',
'alpha');
53if ($user->socid > 0) {
54 $socid = $user->socid;
58if (!$user->hasRight(
'loan',
'write')) {
73$res = $ls->fetchAll($chid);
75 foreach ($ls->lines as $l) {
78 if (empty($l->fk_bank)) {
81 } elseif ($line_id == $l->id) {
89if (!empty($line_id)) {
91 $res = $line->fetch($line_id);
93 $amount_capital =
price($line->amount_capital);
94 $amount_insurance =
price($line->amount_insurance);
95 $amount_interest =
price($line->amount_interest);
96 if (empty($datepaid)) {
97 $ts_temppaid = $line->datep;
104if (empty($line) && $loan->paid != Loan::STATUS_PAID) {
107 $amount_capital =
price($nextpayment[
'capital']);
108 $amount_insurance =
price($nextpayment[
'charge']);
109 $amount_interest =
price($nextpayment[
'interest']);
110 if (empty($datepaid)) {
111 $ts_temppaid = $nextpayment[
'date'];
116$permissiontoadd = $user->hasRight(
'loan',
'write');
123if ($action ==
'add_payment' && $permissiontoadd) {
127 $loc = DOL_URL_ROOT.
'/loan/card.php?id='.$chid;
128 header(
"Location: ".$loc);
133 setEventMessages($langs->trans(
"ErrorFieldRequired", $langs->transnoentities(
"PaymentMode")),
null,
'errors');
136 if ($datepaid ==
'') {
137 setEventMessages($langs->trans(
"ErrorFieldRequired", $langs->transnoentities(
"Date")),
null,
'errors');
141 setEventMessages($langs->trans(
"ErrorFieldRequired", $langs->transnoentities(
"AccountToCredit")),
null,
'errors');
151 if (!empty($line) && !
GETPOSTISSET(
'amount_interest')) {
152 $pay_amount_interest = $line->amount_interest;
157 $amount = (float)
price2num($pay_amount_capital + $pay_amount_insurance + $pay_amount_interest,
'MT');
160 if (!empty($line) && !empty($line->fk_bank)) {
161 setEventMessages($langs->trans(
'TermPaidAllreadyPaid'),
null,
'errors');
165 if (empty($remaindertopay)) {
166 setEventMessages(
'Empty sumpaid',
null,
'errors');
171 setEventMessages($langs->trans(
'ErrorNoPaymentDefined'),
null,
'errors');
180 $payment->chid = $chid;
181 $payment->datep = $datepaid;
182 $payment->label = $loan->label;
183 $payment->amount_capital = $pay_amount_capital;
184 $payment->amount_insurance = $pay_amount_insurance;
185 $payment->amount_interest = $pay_amount_interest;
187 $payment->paymenttype =
GETPOSTINT(
'paymenttype');
188 $payment->num_payment =
GETPOST(
'num_payment',
'alphanohtml');
189 $payment->note_private =
GETPOST(
'note_private',
'restricthtml');
190 $payment->note_public =
GETPOST(
'note_public',
'restricthtml');
193 $paymentid = $payment->create($user);
194 if ($paymentid < 0) {
195 setEventMessages($payment->error, $payment->errors,
'errors');
202 $result = $payment->addPaymentToBank($user, $chid,
'payment_loan',
'(LoanPayment)', $payment->fk_bank,
'',
'');
203 if (!($result > 0)) {
204 setEventMessages($payment->error, $payment->errors,
'errors');
210 if (!$error && !empty($line)) {
211 if (($line->amount_capital != $pay_amount_capital) || ($line->amount_insurance != $pay_amount_insurance) || ($line->amount_interest != $pay_amount_interest)) {
214 $keep = (
GETPOST(
'recalc_keep',
'aZ09') ===
'payment' ?
'payment' :
'term');
215 $nbtermold = (float) $loan->nbterm;
216 $line->fk_bank = $payment->fk_bank;
217 $line->fk_payment_loan = $payment->id;
218 $line->amount_capital = $pay_amount_capital;
219 $line->amount_insurance = $pay_amount_insurance;
220 $line->amount_interest = $pay_amount_interest;
221 $line->fk_user_modif = $user->id;
222 if ($line->update($user, 0) < 1) {
223 setEventMessages(
null, $line->errors,
'errors');
226 if (!$error && isset($ls->lines[$echance])) {
228 $lines[$echance - 1] = $line;
229 $res =
loanRecalculateSchedule($db, $user, $loan, $lines, $echance, (
float) $remaindertopay - (
float) $pay_amount_capital,
null, $keep);
231 setEventMessages($langs->trans($res[
'error']),
null,
'errors');
233 } elseif (
loanRecordChange($db, $user, $loan->id,
'payment', $datepaid, (
float) $loan->rate, (
float) $loan->rate, $keep, $res[
'payment_old'], $res[
'payment_new'], $nbtermold, $res[
'nbterm_new'], $payment->id) < 0) {
234 setEventMessages($db->lasterror(),
null,
'errors');
239 $line->fk_bank = $payment->fk_bank;
240 $line->fk_payment_loan = $payment->id;
241 $result = $line->update($user, 0);
243 setEventMessages(
null, $line->errors,
'errors');
251 $loc = DOL_URL_ROOT.
'/loan/card.php?id='.$chid;
252 header(
'Location: '.$loc);
267$form =
new Form($db);
269$title = $langs->trans(
'Loans');
270$help_url =
"EN:Module_Loan|FR:Module_Emprunt";
272llxHeader(
'', $title, $help_url,
'', 0, 0,
'',
'',
'',
'bodyforlist mod-loan page-payment-list');
276if ($action ==
'create') {
277 $total = $loan->capital;
282 $sql =
"SELECT SUM(amount_capital) as total";
283 $sql .=
" FROM ".MAIN_DB_PREFIX.
"payment_loan";
284 $sql .=
" WHERE fk_loan = ".((int) $chid);
285 $resql = $db->query($sql);
287 $obj = $db->fetch_object($resql);
288 $sumpaid = $obj->total;
292 print
'<form name="add_payment" action="'.$_SERVER[
'PHP_SELF'].
'" method="post">';
293 print
'<input type="hidden" name="token" value="'.newToken().
'">';
294 print
'<input type="hidden" name="id" value="'.$chid.
'">';
295 print
'<input type="hidden" name="chid" value="'.$chid.
'">';
296 print
'<input type="hidden" name="line_id" value="'.$line_id.
'">';
297 print
'<input type="hidden" name="remaindertopay" value="'.($total - $sumpaid).
'">';
298 print
'<input type="hidden" name="action" value="add_payment">';
321 print
'<table class="border centpercent">';
323 print
'<tr><td class="titlefield fieldrequired">'.$langs->trans(
"Date").
'</td><td colspan="2">';
324 if (empty($datepaid)) {
325 if (empty($ts_temppaid)) {
328 $datepayment = $ts_temppaid;
331 $datepayment = $datepaid;
333 print $form->selectDate($datepayment,
'', 0, 0, 0,
"add_payment", 1, 1);
337 print
'<tr><td class="fieldrequired">'.$langs->trans(
"PaymentMode").
'</td><td colspan="2">';
338 print img_picto(
'',
'money-bill-alt',
'class="pictofixedwidth"');
339 $form->select_types_paiements(
GETPOSTISSET(
"paymenttype") ?
GETPOST(
"paymenttype",
'alphanohtml') :
'',
"paymenttype");
344 print
'<td class="fieldrequired">'.$langs->trans(
'AccountToDebit').
'</td>';
345 print
'<td colspan="2">';
346 print img_picto(
'',
'bank_account',
'class="pictofixedwidth"');
347 print $form->select_comptes(
GETPOSTISSET(
"accountid") ?
GETPOSTINT(
"accountid") : $loan->fk_bank,
"accountid", 0,
'(courant:=:'.
Account::TYPE_CURRENT.
')', 1,
'', 0,
'', 1);
351 print
'<tr><td>'.$langs->trans(
'Numero');
352 print
' <em>('.$langs->trans(
"ChequeOrTransferNumber").
')</em>';
354 print
'<td colspan="2"><input name="num_payment" type="text" value="'.GETPOST(
'num_payment',
'alphanohtml').
'"></td>'.
"\n";
358 print
'<td class="tdtop">'.$langs->trans(
"NotePrivate").
'</td>';
359 print
'<td valign="top" colspan="2"><textarea name="note_private" wrap="soft" cols="60" rows="'.ROWS_3.
'"></textarea></td>';
363 print
'<td class="tdtop">'.$langs->trans(
"NotePublic").
'</td>';
364 print
'<td valign="top" colspan="2"><textarea name="note_public" wrap="soft" cols="60" rows="'.ROWS_3.
'"></textarea></td>';
372 print
'<table class="noborder centpercent">';
373 print
'<tr class="liste_titre">';
374 print
'<td class="left">'.$langs->trans(
"DateDue").
'</td>';
375 print
'<td class="right">'.$langs->trans(
"LoanCapital").
'</td>';
376 print
'<td class="right">'.$langs->trans(
"AlreadyPaid").
'</td>';
377 print
'<td class="right">'.$langs->trans(
"RemainderToPay").
'</td>';
378 print
'<td class="right">'.$langs->trans(
"Amount").
'</td>';
381 print
'<tr class="oddeven">';
383 if ($loan->datestart > 0) {
384 print
'<td class="left" valign="center">'.dol_print_date($loan->datestart,
'day').
'</td>';
386 print
'<td class="center" valign="center"><b>!!!</b></td>';
389 print
'<td class="right" valign="center">'.price($loan->capital).
"</td>";
391 print
'<td class="right" valign="center">'.price($sumpaid).
"</td>";
393 print
'<td class="right" valign="center">'.price($loan->capital - $sumpaid).
"</td>";
395 print
'<td class="right">';
396 if ($sumpaid < $loan->capital) {
397 print $langs->trans(
"LoanCapital").
': <input type="text" size="8" name="amount_capital" value="'.(
GETPOSTISSET(
'amount_capital') ?
GETPOST(
'amount_capital') : $amount_capital).
'">';
402 if ($sumpaid < $loan->capital) {
403 print
loanChargeLabel($loan->charge_type, $langs).
': <input type="text" size="8" name="amount_insurance" value="'.(
GETPOSTISSET(
'amount_insurance') ?
GETPOST(
'amount_insurance') : $amount_insurance).
'">';
408 if ($sumpaid < $loan->capital) {
409 print $langs->trans(
"Interest").
': <input type="text" size="8" name="amount_interest" value="'.(
GETPOSTISSET(
'amount_interest') ?
GETPOST(
'amount_interest') : $amount_interest).
'">';
419 if (!empty($nextpayment)) {
420 print
'<div class="opacitymedium margintoponly">'.$langs->trans(
"LoanNextPaymentPrefilled", $nextpayment[
'term'], (
int) $loan->nbterm).
'</div>';
425 print
'<div class="margintoponly">'.$langs->trans(
"LoanRecalcIfDifferent").
' ';
426 print $form->selectarray(
'recalc_keep', array(
'term' => $langs->trans(
"LoanRecalcKeepTerm"),
'payment' => $langs->trans(
"LoanRecalcKeepPayment")),
GETPOST(
'recalc_keep',
'aZ09') ?
GETPOST(
'recalc_keep',
'aZ09') :
'term');
430 print $form->buttonsSaveCancel();
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 bank accounts.
Class to manage Schedule of loans.
Class to manage payments of loans.
dol_now($mode='gmt')
Return date for now.
dol_mktime($hour, $minute, $second, $month, $day, $year, $gm='auto', $check=1)
Return a timestamp date built from detailed information (by default a local PHP server timestamp) Rep...
price2num($amount, $rounding='', $option=0, $currencycode='')
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.
GETPOSTISSET($paramname)
Return true if we are in a context of submitting the parameter $paramname from a POST of a form.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
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.
dol_get_fiche_end($notab=0)
Return tab footer of a card.
load_fiche_titre($title, $morehtmlright='', $picto='generic', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='', $morecssonpicto='widthpictotitle')
Load a title with picto.
loanNextPayment($db, $loan)
Next payment of a loan that has no schedule, worked out from the loan and the payments already made: ...
loanChargeLabel($charge_type, $outputlangs)
Label of the charge of a loan (Insurance, Account-keeping fee, Other fee).
loanRecordChange($db, $user, $loanid, $reason, $datechange, $rate_old, $rate_new, $keep, $payment_old, $payment_new, $nbterm_old, $nbterm_new, $fk_payment=0)
Record a change of a loan (rate change, or a payment that differs from the schedule) in its history.
loanRecalculateSchedule($db, $user, $loan, $lines, $from, $capital, $rate, $keep, $payment=0.0)
Recalculate the unpaid payments of a loan schedule, from one payment to the end, after a rate change ...
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.