26 require
'../main.inc.php';
27 require_once DOL_DOCUMENT_ROOT.
'/loan/class/loan.class.php';
28 require_once DOL_DOCUMENT_ROOT.
'/core/lib/loan.lib.php';
29 require_once DOL_DOCUMENT_ROOT.
'/core/lib/date.lib.php';
30 require_once DOL_DOCUMENT_ROOT.
'/loan/class/loanschedule.class.php';
31 require_once DOL_DOCUMENT_ROOT.
'/loan/class/paymentloan.class.php';
33 $loanid =
GETPOST(
'loanid',
'int');
34 $action =
GETPOST(
'action',
'aZ09');
39 $socid =
GETPOST(
'socid',
'int');
42 $socid = $user->socid;
44 if (empty($user->rights->loan->calc)) {
49 $langs->loadLangs(array(
"compta",
"bills",
"loan"));
51 $object =
new Loan($db);
52 $object->fetch($loanid);
55 $echeances->fetchAll($object->id);
57 if ($object->paid > 0 && count($echeances->lines) == 0) {
58 $pay_without_schedule = 1;
65 if ($action ==
'createecheancier' && empty($pay_without_schedule)) {
68 while ($i < $object->nbterm + 1) {
69 $date =
GETPOST(
'hi_date'.$i,
'int');
76 $new_echeance->fk_loan = $object->id;
77 $new_echeance->datec =
dol_now();
79 $new_echeance->datep = $date;
80 $new_echeance->amount_capital = $mens - $int;
81 $new_echeance->amount_insurance = $insurance;
82 $new_echeance->amount_interest = $int;
83 $new_echeance->fk_typepayment = 3;
84 $new_echeance->fk_bank = 0;
85 $new_echeance->fk_user_creat = $user->id;
86 $new_echeance->fk_user_modif = $user->id;
87 $result = $new_echeance->create($user);
91 unset($echeances->lines);
94 $echeances->lines[] = $new_echeance;
102 if ($action ==
'updateecheancier' && empty($pay_without_schedule)) {
105 while ($i < $object->nbterm + 1) {
112 $new_echeance->fetch($id);
113 $new_echeance->tms =
dol_now();
114 $new_echeance->amount_capital = $mens - $int;
115 $new_echeance->amount_insurance = $insurance;
116 $new_echeance->amount_interest = $int;
117 $new_echeance->fk_user_modif = $user->id;
118 $result = $new_echeance->update($user, 0);
122 $echeances->fetchAll($object->id);
126 $echeances->lines[$i - 1] = $new_echeance;
138 $title = $langs->trans(
"Loan").
' - '.$langs->trans(
"Card");
139 $help_url =
'EN:Module_Loan|FR:Module_Emprunt';
143 print
dol_get_fiche_head($head,
'FinancialCommitment', $langs->trans(
"Loan"), -1,
'bill');
145 $linkback =
'<a href="'.DOL_URL_ROOT.
'/loan/list.php?restore_lastsearch_values=1">'.$langs->trans(
"BackToList").
'</a>';
147 $morehtmlref =
'<div class="refidno">';
149 $morehtmlref .=
$form->editfieldkey(
"Label",
'label', $object->label, $object, 0,
'string',
'', 0, 1);
150 $morehtmlref .=
$form->editfieldval(
"Label",
'label', $object->label, $object, 0,
'string',
'',
null,
null,
'', 1);
152 if (!empty($conf->project->enabled)) {
153 $langs->loadLangs(array(
"projects"));
154 $morehtmlref .=
'<br>'.$langs->trans(
'Project').
' : ';
155 if ($user->rights->loan->write) {
156 if ($action !=
'classify') {
158 if ($action ==
'classify') {
160 $morehtmlref .=
'<form method="post" action="'.$_SERVER[
'PHP_SELF'].
'?id='.$object->id.
'">';
161 $morehtmlref .=
'<input type="hidden" name="action" value="classin">';
162 $morehtmlref .=
'<input type="hidden" name="token" value="'.newToken().
'">';
163 $morehtmlref .= $formproject->select_projects($object->socid, $object->fk_project,
'projectid', $maxlength, 0, 1, 0, 1, 0, 0,
'', 1);
164 $morehtmlref .=
'<input type="submit" class="button valignmiddle" value="'.$langs->trans(
"Modify").
'">';
165 $morehtmlref .=
'</form>';
167 $morehtmlref .=
$form->form_project($_SERVER[
'PHP_SELF'].
'?id='.$object->id, $object->socid, $object->fk_project,
'none', 0, 0, 0, 1);
171 if (!empty($object->fk_project)) {
173 $proj->fetch($object->fk_project);
174 $morehtmlref .=
' : '.$proj->getNomUrl(1);
176 $morehtmlref .=
' - '.$proj->title;
183 $morehtmlref .=
'</div>';
187 dol_banner_tab($object,
'loanid', $linkback, 1,
'rowid',
'ref', $morehtmlref,
'', 0,
'', $morehtmlright);
190 <script
type=
"text/javascript">
191 $(document).ready(
function() {
192 $(
'[name^="mens"]').focusout(
function() {
193 var echeance=$(
this).attr(
'ech');
195 var idcap=echeance-1;
196 idcap =
'#hi_capital'+idcap;
198 console.log(
"Change montly amount echeance="+echeance+
" idcap="+idcap+
" capital="+capital);
203 data: { echeance: echeance, mens: mens, capital:capital, rate:<?php echo $object->rate / 100; ?>, nbterm: <?php echo $object->nbterm; ?>, token:
'<?php echo currentToken(); ?>' },
204 success:
function(data) {
205 $.each(data,
function(index, element) {
206 var idcap_res=
'#hi_capital'+index;
207 var idcap_res_srt=
'#capital'+index;
208 var interet_res=
'#hi_interets'+index;
209 var interet_res_str=
'#interets'+index;
210 var men_res=
'#mens'+index;
211 $(idcap_res).val(element.cap_rest);
212 $(idcap_res_srt).text(element.cap_rest_str);
213 $(interet_res).val(element.interet);
214 $(interet_res_str).text(element.interet_str);
215 $(men_res).val(element.mens);
224 if ($pay_without_schedule == 1) {
225 print
'<div class="warning">'.$langs->trans(
'CantUseScheduleWithLoanStartedToPaid').
'</div>'.
"\n";
228 print
'<form name="createecheancier" action="'.$_SERVER[
"PHP_SELF"].
'" method="POST">';
229 print
'<input type="hidden" name="token" value="'.newToken().
'">';
230 print
'<input type="hidden" name="loanid" value="'.$loanid.
'">';
231 if (count($echeances->lines) > 0) {
232 print
'<input type="hidden" name="action" value="updateecheancier">';
234 print
'<input type="hidden" name="action" value="createecheancier">';
240 print
'<div class="div-table-responsive-no-min">';
241 print
'<table class="border centpercent">';
244 if (count($echeances->lines) > 0) {
248 print
'<tr class="liste_titre">';
249 print
'<th class="center">'.$langs->trans(
"Term").
'</th>';
250 print
'<th class="center">'.$langs->trans(
"Date").
'</th>';
251 print
'<th class="center">'.$langs->trans(
"Insurance");
252 print
'<th class="center">'.$langs->trans(
"InterestAmount").
'</th>';
253 print
'<th class="center">'.$langs->trans(
"Amount").
'</th>';
254 print
'<th class="center">'.$langs->trans(
"CapitalRemain");
255 print
'<br>('.price($object->capital, 0,
'', 1, -1, -1, $conf->currency).
')';
256 print
'<input type="hidden" name="hi_capital0" id ="hi_capital0" value="'.$object->capital.
'">';
258 if (count($echeances->lines) > 0) {
259 print
'<th class="center">'.$langs->trans(
'DoPayment').
'</th>';
263 if ($object->nbterm > 0 && count($echeances->lines) == 0) {
265 $capital = $object->capital;
266 $insurance = $object->insurance_amount / $object->nbterm;
267 $insurance =
price2num($insurance,
'MT');
268 $regulInsurance =
price2num($object->insurance_amount - ($insurance * $object->nbterm));
269 while ($i < $object->nbterm + 1) {
270 $mens =
price2num($echeances->calcMonthlyPayments($capital, $object->rate / 100, $object->nbterm - $i + 1),
'MT');
271 $int = ($capital * ($object->rate / 12)) / 100;
273 $insu = ($insurance + (($i == 1) ? $regulInsurance : 0));
274 $cap_rest =
price2num($capital - ($mens - $int),
'MT');
276 print
'<td class="center" id="n'.$i.
'">'.$i.
'</td>';
277 print
'<td class="center" id ="date'.$i.
'"><input type="hidden" name="hi_date'.$i.
'" id ="hi_date'.$i.
'" value="'.
dol_time_plus_duree($object->datestart, $i - 1,
'm').
'">'.
dol_print_date(
dol_time_plus_duree($object->datestart, $i - 1,
'm'),
'day').
'</td>';
278 print
'<td class="center amount" id="insurance'.$i.
'">'.
price($insurance + (($i == 1) ? $regulInsurance : 0), 0,
'', 1, -1, -1, $conf->currency).
'</td><input type="hidden" name="hi_insurance'.$i.
'" id ="hi_insurance'.$i.
'" value="'.($insurance + (($i == 1) ? $regulInsurance : 0)).
'">';
279 print
'<td class="center amount" id="interets'.$i.
'">'.
price($int, 0,
'', 1, -1, -1, $conf->currency).
'</td><input type="hidden" name="hi_interets'.$i.
'" id ="hi_interets'.$i.
'" value="'.$int.
'">';
280 print
'<td class="center"><input class="width75 right" name="mens'.$i.
'" id="mens'.$i.
'" value="'.$mens.
'" ech="'.$i.
'"></td>';
281 print
'<td class="center amount" id="capital'.$i.
'">'.
price($cap_rest).
'</td><input type="hidden" name="hi_capital'.$i.
'" id ="hi_capital'.$i.
'" value="'.$cap_rest.
'">';
284 $capital = $cap_rest;
286 } elseif (count($echeances->lines) > 0) {
288 $capital = $object->capital;
289 $insurance = $object->insurance_amount / $object->nbterm;
290 $insurance =
price2num($insurance,
'MT');
291 $regulInsurance =
price2num($object->insurance_amount - ($insurance * $object->nbterm));
293 foreach ($echeances->lines as $line) {
294 $mens = $line->amount_capital + $line->amount_interest;
295 $int = $line->amount_interest;
296 $insu = ($insurance + (($i == 1) ? $regulInsurance : 0));
297 $cap_rest =
price2num($capital - ($mens - $int),
'MT');
300 print
'<td class="center" id="n'.$i.
'"><input type="hidden" name="hi_rowid'.$i.
'" id ="hi_rowid'.$i.
'" value="'.$line->id.
'">'.$i.
'</td>';
301 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>';
302 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.
'">';
303 print
'<td class="center amount" id="interets'.$i.
'">'.
price($int, 0,
'', 1, -1, -1, $conf->currency).
'</td><input type="hidden" name="hi_interets'.$i.
'" id ="hi_interets'.$i.
'" value="'.$int.
'">';
304 if (empty($line->fk_bank)) {
305 print
'<td class="center"><input class="right width75" name="mens'.$i.
'" id="mens'.$i.
'" value="'.$mens.
'" ech="'.$i.
'"></td>';
307 print
'<td class="center amount">'.price($mens, 0,
'', 1, -1, -1, $conf->currency).
'</td><input type="hidden" name="mens'.$i.
'" id ="mens'.$i.
'" value="'.$mens.
'">';
310 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.
'">';
311 print
'<td class="center">';
312 if (!empty($line->fk_bank)) {
313 print $langs->trans(
'Paid');
314 if (!empty($line->fk_payment_loan)) {
315 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>';
317 } elseif (!$printed) {
318 print
'<a class="butAction smallpaddingimp" href="'.DOL_URL_ROOT.
'/loan/payment/payment.php?id='.$object->id.
'&action=create">'.$langs->trans(
'DoPayment').
'</a>';
324 $capital = $cap_rest;
333 if (count($echeances->lines) == 0) {
334 $label = $langs->trans(
"Create");
336 $label = $langs->trans(
"Save");
338 print
'<div class="center"><input type="submit" class="button button-add" value="'.$label.
'" '.(($pay_without_schedule == 1) ?
'disabled title="'.$langs->trans(
'CantUseScheduleWithLoanStartedToPaid').
'"' :
'').
'title=""></div>';