26 require
'../../main.inc.php';
27 require_once DOL_DOCUMENT_ROOT.
'/loan/class/loan.class.php';
28 require_once DOL_DOCUMENT_ROOT.
'/loan/class/loanschedule.class.php';
29 require_once DOL_DOCUMENT_ROOT.
'/loan/class/paymentloan.class.php';
30 require_once DOL_DOCUMENT_ROOT.
'/compta/bank/class/account.class.php';
31 require_once DOL_DOCUMENT_ROOT.
'/core/lib/loan.lib.php';
33 $langs->loadLangs(array(
"bills",
"loan"));
36 $action =
GETPOST(
'action',
'aZ09');
37 $cancel =
GETPOST(
'cancel',
'alpha');
42 if ($user->socid > 0) {
43 $socid = $user->socid;
45 $socid =
GETPOST(
'socid',
'int');
47 if (empty($user->rights->loan->write)) {
51 $loan =
new Loan($db);
57 $res = $ls->fetchAll($chid);
59 foreach ($ls->lines as $l) {
62 if (empty($l->fk_bank)) {
65 } elseif ($line_id == $l->id) {
73 if (!empty($line_id)) {
75 $res = $line->fetch($line_id);
77 $amount_capital =
price($line->amount_capital);
78 $amount_insurance =
price($line->amount_insurance);
79 $amount_interest =
price($line->amount_interest);
80 if (empty($datepaid)) {
81 $ts_temppaid = $line->datep;
91 if ($action ==
'add_payment') {
95 $loc = DOL_URL_ROOT.
'/loan/card.php?id='.$chid;
96 header(
"Location: ".$loc);
100 if (!
GETPOST(
'paymenttype',
'int') > 0) {
101 setEventMessages($langs->trans(
"ErrorFieldRequired", $langs->transnoentities(
"PaymentMode")),
null,
'errors');
104 if ($datepaid ==
'') {
105 setEventMessages($langs->trans(
"ErrorFieldRequired", $langs->transnoentities(
"Date")),
null,
'errors');
108 if (!empty($conf->banque->enabled) && !
GETPOST(
'accountid',
'int') > 0) {
109 setEventMessages($langs->trans(
"ErrorFieldRequired", $langs->transnoentities(
"AccountToCredit")),
null,
'errors');
120 $pay_amount_interest = $line->amount_interest;
125 $amount = $pay_amount_capital + $pay_amount_insurance + $pay_amount_interest;
128 if (!empty($line) && !empty($line->fk_bank)) {
133 if (empty($remaindertopay)) {
148 $payment->chid = $chid;
149 $payment->datep = $datepaid;
150 $payment->label = $loan->label;
151 $payment->amount_capital = $pay_amount_capital;
152 $payment->amount_insurance = $pay_amount_insurance;
153 $payment->amount_interest = $pay_amount_interest;
154 $payment->fk_bank =
GETPOST(
'accountid',
'int');
155 $payment->paymenttype =
GETPOST(
'paymenttype',
'int');
156 $payment->num_payment =
GETPOST(
'num_payment');
157 $payment->note_private =
GETPOST(
'note_private',
'restricthtml');
158 $payment->note_public =
GETPOST(
'note_public',
'restricthtml');
161 $paymentid = $payment->create($user);
162 if ($paymentid < 0) {
169 $result = $payment->addPaymentToBank($user, $chid,
'payment_loan',
'(LoanPayment)', $payment->fk_bank,
'',
'');
170 if (!($result > 0)) {
177 if (!$error && !empty($line)) {
179 if (($line->amount_capital <> $pay_amount_capital) || ($line->amount_insurance <> $pay_amount_insurance) || ($line->amount_interest <> $pay_amount_interest)) {
180 $arr_term =
loanCalcMonthlyPayment(($pay_amount_capital + $pay_amount_interest), $remaindertopay, ($loan->rate / 100), $echance, $loan->nbterm);
181 foreach ($arr_term as $k => $v) {
183 if ($k == $echance) {
184 $ls->lines[$k - 1]->fk_bank = $payment->fk_bank;
185 $ls->lines[$k - 1]->fk_payment_loan = $payment->id;
187 $ls->lines[$k - 1]->amount_capital = $v[
'mens'] - $v[
'interet'];
188 $ls->lines[$k - 1]->amount_interest = $v[
'interet'];
189 $ls->lines[$k - 1]->tms =
dol_now();
190 $ls->lines[$k - 1]->fk_user_modif = $user->id;
191 $result = $ls->lines[$k - 1]->update($user, 0);
200 $line->fk_bank = $payment->fk_bank;
201 $line->fk_payment_loan = $payment->id;
202 $result = $line->update($user, 0);
212 $loc = DOL_URL_ROOT.
'/loan/card.php?id='.$chid;
213 header(
'Location: '.$loc);
235 if ($action ==
'create') {
236 $total = $loan->capital;
240 $sql =
"SELECT SUM(amount_capital) as total";
241 $sql .=
" FROM ".MAIN_DB_PREFIX.
"payment_loan";
242 $sql .=
" WHERE fk_loan = ".((int) $chid);
243 $resql = $db->query($sql);
245 $obj = $db->fetch_object(
$resql);
246 $sumpaid = $obj->total;
250 print
'<form name="add_payment" action="'.$_SERVER[
'PHP_SELF'].
'" method="post">';
251 print
'<input type="hidden" name="token" value="'.newToken().
'">';
252 print
'<input type="hidden" name="id" value="'.$chid.
'">';
253 print
'<input type="hidden" name="chid" value="'.$chid.
'">';
254 print
'<input type="hidden" name="line_id" value="'.$line_id.
'">';
255 print
'<input type="hidden" name="remaindertopay" value="'.($total - $sumpaid).
'">';
256 print
'<input type="hidden" name="action" value="add_payment">';
279 print
'<table class="border centpercent">';
281 print
'<tr><td class="titlefield fieldrequired">'.$langs->trans(
"Date").
'</td><td colspan="2">';
282 if (empty($datepaid)) {
283 if (empty($ts_temppaid)) {
284 $datepayment = empty($conf->global->MAIN_AUTOFILL_DATE) ?-1 :
dol_now();
286 $datepayment = $ts_temppaid;
289 $datepayment = $datepaid;
291 print
$form->selectDate($datepayment,
'',
'',
'',
'',
"add_payment", 1, 1);
295 print
'<tr><td class="fieldrequired">'.$langs->trans(
"PaymentMode").
'</td><td colspan="2">';
296 print
img_picto(
'',
'money-bill-alt',
'class="pictofixedwidth"');
297 $form->select_types_paiements(
GETPOSTISSET(
"paymenttype") ?
GETPOST(
"paymenttype",
'alphanohtml') : $loan->fk_typepayment,
"paymenttype");
302 print
'<td class="fieldrequired">'.$langs->trans(
'AccountToDebit').
'</td>';
303 print
'<td colspan="2">';
304 print
img_picto(
'',
'bank_account',
'class="pictofixedwidth"');
305 $form->select_comptes(
GETPOSTISSET(
"accountid") ?
GETPOST(
"accountid",
'int') : $loan->accountid,
"accountid", 0,
'courant = '.Account::TYPE_CURRENT, 1);
309 print
'<tr><td>'.$langs->trans(
'Numero');
310 print
' <em>('.$langs->trans(
"ChequeOrTransferNumber").
')</em>';
312 print
'<td colspan="2"><input name="num_payment" type="text" value="'.GETPOST(
'num_payment',
'alphanohtml').
'"></td>'.
"\n";
316 print
'<td class="tdtop">'.$langs->trans(
"NotePrivate").
'</td>';
317 print
'<td valign="top" colspan="2"><textarea name="note_private" wrap="soft" cols="60" rows="'.ROWS_3.
'"></textarea></td>';
321 print
'<td class="tdtop">'.$langs->trans(
"NotePublic").
'</td>';
322 print
'<td valign="top" colspan="2"><textarea name="note_public" wrap="soft" cols="60" rows="'.ROWS_3.
'"></textarea></td>';
330 print
'<table class="noborder centpercent">';
331 print
'<tr class="liste_titre">';
332 print
'<td class="left">'.$langs->trans(
"DateDue").
'</td>';
333 print
'<td class="right">'.$langs->trans(
"LoanCapital").
'</td>';
334 print
'<td class="right">'.$langs->trans(
"AlreadyPaid").
'</td>';
335 print
'<td class="right">'.$langs->trans(
"RemainderToPay").
'</td>';
336 print
'<td class="right">'.$langs->trans(
"Amount").
'</td>';
339 print
'<tr class="oddeven">';
341 if ($loan->datestart > 0) {
342 print
'<td class="left" valign="center">'.dol_print_date($loan->datestart,
'day').
'</td>';
344 print
'<td class="center" valign="center"><b>!!!</b></td>';
347 print
'<td class="right" valign="center">'.price($loan->capital).
"</td>";
349 print
'<td class="right" valign="center">'.price($sumpaid).
"</td>";
351 print
'<td class="right" valign="center">'.price($loan->capital - $sumpaid).
"</td>";
353 print
'<td class="right">';
354 if ($sumpaid < $loan->capital) {
355 print $langs->trans(
"LoanCapital").
': <input type="text" size="8" name="amount_capital" value="'.(
GETPOSTISSET(
'amount_capital') ?
GETPOST(
'amount_capital') : $amount_capital).
'">';
360 if ($sumpaid < $loan->capital) {
361 print $langs->trans(
"Insurance").
': <input type="text" size="8" name="amount_insurance" value="'.(
GETPOSTISSET(
'amount_insurance') ?
GETPOST(
'amount_insurance') : $amount_insurance).
'">';
366 if ($sumpaid < $loan->capital) {
367 print $langs->trans(
"Interest").
': <input type="text" size="8" name="amount_interest" value="'.(
GETPOSTISSET(
'amount_interest') ?
GETPOST(
'amount_interest') : $amount_interest).
'" '.(!empty($line) ?
'disabled title="'.$langs->trans(
'CantModifyInterestIfScheduleIsUsed').
'"' :
'').
'>';
377 print
$form->buttonsSaveCancel();