dolibarr 18.0.6
schedule.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2017 Franck Moreau <franck.moreau@theobald.com>
3 * Copyright (C) 2018 Alexandre Spangaro <aspangaro@open-dsi.fr>
4 * Copyright (C) 2020 Maxime DEMAREST <maxime@indelog.fr>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
26// Load Dolibarr environment
27require '../main.inc.php';
28require_once DOL_DOCUMENT_ROOT.'/loan/class/loan.class.php';
29require_once DOL_DOCUMENT_ROOT.'/core/lib/loan.lib.php';
30require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
31require_once DOL_DOCUMENT_ROOT.'/loan/class/loanschedule.class.php';
32require_once DOL_DOCUMENT_ROOT.'/loan/class/paymentloan.class.php';
33
34$loanid = GETPOST('loanid', 'int');
35$action = GETPOST('action', 'aZ09');
36
37// Security check
38$socid = 0;
39if (GETPOSTISSET('socid')) {
40 $socid = GETPOST('socid', 'int');
41}
42if ($user->socid) {
43 $socid = $user->socid;
44}
45if (empty($user->rights->loan->calc)) {
47}
48
49// Load translation files required by the page
50$langs->loadLangs(array("compta", "bills", "loan"));
51
52$object = new Loan($db);
53$object->fetch($loanid);
54
55$echeances = new LoanSchedule($db);
56$echeances->fetchAll($object->id);
57
58if ($object->paid > 0 && count($echeances->lines) == 0) {
59 $pay_without_schedule = 1;
60} else {
61 $pay_without_schedule = 0;
62}
63
64/*
65 * Actions
66 */
67
68if ($action == 'createecheancier' && empty($pay_without_schedule)) {
69 $db->begin();
70 $i = 1;
71 while ($i < $object->nbterm + 1) {
72 $date = GETPOST('hi_date'.$i, 'int');
73 $mens = price2num(GETPOST('mens'.$i));
74 $int = price2num(GETPOST('hi_interets'.$i));
75 $insurance = price2num(GETPOST('hi_insurance'.$i));
76
77 $new_echeance = new LoanSchedule($db);
78
79 $new_echeance->fk_loan = $object->id;
80 $new_echeance->datec = dol_now();
81 $new_echeance->tms = dol_now();
82 $new_echeance->datep = $date;
83 $new_echeance->amount_capital = $mens - $int;
84 $new_echeance->amount_insurance = $insurance;
85 $new_echeance->amount_interest = $int;
86 $new_echeance->fk_typepayment = 3;
87 $new_echeance->fk_bank = 0;
88 $new_echeance->fk_user_creat = $user->id;
89 $new_echeance->fk_user_modif = $user->id;
90 $result = $new_echeance->create($user);
91 if ($result < 0) {
92 setEventMessages($new_echeance->error, $echeance->errors, 'errors');
93 $db->rollback();
94 unset($echeances->lines);
95 break;
96 }
97 $echeances->lines[] = $new_echeance;
98 $i++;
99 }
100 if ($result > 0) {
101 $db->commit();
102 }
103}
104
105if ($action == 'updateecheancier' && empty($pay_without_schedule)) {
106 $db->begin();
107 $i = 1;
108 while ($i < $object->nbterm + 1) {
109 $mens = price2num(GETPOST('mens'.$i));
110 $int = price2num(GETPOST('hi_interets'.$i));
111 $id = GETPOST('hi_rowid'.$i);
112 $insurance = price2num(GETPOST('hi_insurance'.$i));
113
114 $new_echeance = new LoanSchedule($db);
115 $new_echeance->fetch($id);
116 $new_echeance->tms = dol_now();
117 $new_echeance->amount_capital = $mens - $int;
118 $new_echeance->amount_insurance = $insurance;
119 $new_echeance->amount_interest = $int;
120 $new_echeance->fk_user_modif = $user->id;
121 $result = $new_echeance->update($user, 0);
122 if ($result < 0) {
123 setEventMessages(null, $new_echeance->errors, 'errors');
124 $db->rollback();
125 $echeances->fetchAll($object->id);
126 break;
127 }
128
129 $echeances->lines[$i - 1] = $new_echeance;
130 $i++;
131 }
132 if ($result > 0) {
133 $db->commit();
134 }
135}
136
137/*
138 * View
139 */
140
141$title = $langs->trans("Loan").' - '.$langs->trans("Card");
142$help_url = 'EN:Module_Loan|FR:Module_Emprunt';
143llxHeader("", $title, $help_url);
144
145$head = loan_prepare_head($object);
146print dol_get_fiche_head($head, 'FinancialCommitment', $langs->trans("Loan"), -1, 'bill');
147
148$linkback = '<a href="'.DOL_URL_ROOT.'/loan/list.php?restore_lastsearch_values=1">'.$langs->trans("BackToList").'</a>';
149
150$morehtmlref = '<div class="refidno">';
151// Ref loan
152$morehtmlref .= $form->editfieldkey("Label", 'label', $object->label, $object, 0, 'string', '', 0, 1);
153$morehtmlref .= $form->editfieldval("Label", 'label', $object->label, $object, 0, 'string', '', null, null, '', 1);
154// Project
155if (isModEnabled('project')) {
156 $langs->loadLangs(array("projects"));
157 $morehtmlref .= '<br>'.$langs->trans('Project').' : ';
158 if ($user->rights->loan->write) {
159 if ($action != 'classify') {
160 //$morehtmlref .= '<a class="editfielda" href="'.$_SERVER['PHP_SELF'].'?action=classify&token='.newToken().'&id='.$object->id.'">'.img_edit($langs->transnoentitiesnoconv('SetProject')).'</a> : ';
161 if ($action == 'classify') {
162 //$morehtmlref.=$form->form_project($_SERVER['PHP_SELF'] . '?id=' . $object->id, $object->socid, $object->fk_project, 'projectid', 0, 0, 1, 1);
163 $morehtmlref .= '<form method="post" action="'.$_SERVER['PHP_SELF'].'?id='.$object->id.'">';
164 $morehtmlref .= '<input type="hidden" name="action" value="classin">';
165 $morehtmlref .= '<input type="hidden" name="token" value="'.newToken().'">';
166 $morehtmlref .= $formproject->select_projects(-1, $object->fk_project, 'projectid', $maxlength, 0, 1, 0, 1, 0, 0, '', 1);
167 $morehtmlref .= '<input type="submit" class="button valignmiddle" value="'.$langs->trans("Modify").'">';
168 $morehtmlref .= '</form>';
169 } else {
170 $morehtmlref .= $form->form_project($_SERVER['PHP_SELF'].'?id='.$object->id, -1, $object->fk_project, 'none', 0, 0, 0, 1, '', 'maxwidth300');
171 }
172 }
173 } else {
174 if (!empty($object->fk_project)) {
175 $proj = new Project($db);
176 $proj->fetch($object->fk_project);
177 $morehtmlref .= ' : '.$proj->getNomUrl(1);
178 if ($proj->title) {
179 $morehtmlref .= ' - '.$proj->title;
180 }
181 } else {
182 $morehtmlref .= '';
183 }
184 }
185}
186$morehtmlref .= '</div>';
187
188$morehtmlright = '';
189
190dol_banner_tab($object, 'loanid', $linkback, 1, 'rowid', 'ref', $morehtmlref, '', 0, '', $morehtmlright);
191
192?>
193<script type="text/javascript">
194$(document).ready(function() {
195 $('[name^="mens"]').focusout(function() {
196 var echeance=$(this).attr('ech');
197 var mens=price2numjs($(this).val());
198 var idcap=echeance-1;
199 idcap = '#hi_capital'+idcap;
200 var capital=price2numjs($(idcap).val());
201 console.log("Change montly amount echeance="+echeance+" idcap="+idcap+" capital="+capital);
202 $.ajax({
203 method: "GET",
204 dataType: 'json',
205 url: 'calcmens.php',
206 data: { echeance: echeance, mens: mens, capital:capital, rate:<?php echo $object->rate / 100; ?>, nbterm: <?php echo $object->nbterm; ?>, token: '<?php echo currentToken(); ?>' },
207 success: function(data) {
208 $.each(data, function(index, element) {
209 var idcap_res='#hi_capital'+index;
210 var idcap_res_srt='#capital'+index;
211 var interet_res='#hi_interets'+index;
212 var interet_res_str='#interets'+index;
213 var men_res='#mens'+index;
214 $(idcap_res).val(element.cap_rest);
215 $(idcap_res_srt).text(element.cap_rest_str);
216 $(interet_res).val(element.interet);
217 $(interet_res_str).text(element.interet_str);
218 $(men_res).val(element.mens);
219 });
220 }
221 });
222 });
223});
224</script>
225<?php
226
227if ($pay_without_schedule == 1) {
228 print '<div class="warning">'.$langs->trans('CantUseScheduleWithLoanStartedToPaid').'</div>'."\n";
229}
230
231print '<form name="createecheancier" action="'.$_SERVER["PHP_SELF"].'" method="POST">';
232print '<input type="hidden" name="token" value="'.newToken().'">';
233print '<input type="hidden" name="loanid" value="'.$loanid.'">';
234if (count($echeances->lines) > 0) {
235 print '<input type="hidden" name="action" value="updateecheancier">';
236} else {
237 print '<input type="hidden" name="action" value="createecheancier">';
238}
239
240//print_fiche_titre($langs->trans("FinancialCommitment"));
241print '<br>';
242
243print '<div class="div-table-responsive-no-min">';
244print '<table class="border centpercent">';
245
246$colspan = 6;
247if (count($echeances->lines) > 0) {
248 $colspan++;
249}
250
251print '<tr class="liste_titre">';
252print '<th class="center">'.$langs->trans("Term").'</th>';
253print '<th class="center">'.$langs->trans("Date").'</th>';
254print '<th class="center">'.$langs->trans("Insurance");
255print '<th class="center">'.$langs->trans("InterestAmount").'</th>';
256print '<th class="center">'.$langs->trans("Amount").'</th>';
257print '<th class="center">'.$langs->trans("CapitalRemain");
258print '<br>('.price($object->capital, 0, '', 1, -1, -1, $conf->currency).')';
259print '<input type="hidden" name="hi_capital0" id ="hi_capital0" value="'.$object->capital.'">';
260print '</th>';
261if (count($echeances->lines) > 0) {
262 print '<th class="center">'.$langs->trans('DoPayment').'</th>';
263}
264print '</tr>'."\n";
265
266if ($object->nbterm > 0 && count($echeances->lines) == 0) {
267 $i = 1;
268 $capital = $object->capital;
269 $insurance = $object->insurance_amount / $object->nbterm;
270 $insurance = price2num($insurance, 'MT');
271 $regulInsurance = price2num($object->insurance_amount - ($insurance * $object->nbterm));
272 while ($i < $object->nbterm + 1) {
273 $mens = price2num($echeances->calcMonthlyPayments($capital, $object->rate / 100, $object->nbterm - $i + 1), 'MT');
274 $int = ($capital * ($object->rate / 12)) / 100;
275 $int = price2num($int, 'MT');
276 $insu = ($insurance + (($i == 1) ? $regulInsurance : 0));
277 $cap_rest = price2num($capital - ($mens - $int), 'MT');
278 print '<tr>';
279 print '<td class="center" id="n'.$i.'">'.$i.'</td>';
280 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>';
281 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)).'">';
282 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.'">';
283 print '<td class="center"><input class="width75 right" name="mens'.$i.'" id="mens'.$i.'" value="'.$mens.'" ech="'.$i.'"></td>';
284 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.'">';
285 print '</tr>'."\n";
286 $i++;
287 $capital = $cap_rest;
288 }
289} elseif (count($echeances->lines) > 0) {
290 $i = 1;
291 $capital = $object->capital;
292 $insurance = $object->insurance_amount / $object->nbterm;
293 $insurance = price2num($insurance, 'MT');
294 $regulInsurance = price2num($object->insurance_amount - ($insurance * $object->nbterm));
295 $printed = false;
296 foreach ($echeances->lines as $line) {
297 $mens = $line->amount_capital + $line->amount_interest;
298 $int = $line->amount_interest;
299 $insu = ($insurance + (($i == 1) ? $regulInsurance : 0));
300 $cap_rest = price2num($capital - ($mens - $int), 'MT');
301
302 print '<tr>';
303 print '<td class="center" id="n'.$i.'"><input type="hidden" name="hi_rowid'.$i.'" id ="hi_rowid'.$i.'" value="'.$line->id.'">'.$i.'</td>';
304 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>';
305 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.'">';
306 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.'">';
307 if (empty($line->fk_bank)) {
308 print '<td class="center"><input class="right width75" name="mens'.$i.'" id="mens'.$i.'" value="'.$mens.'" ech="'.$i.'"></td>';
309 } else {
310 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.'">';
311 }
312
313 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.'">';
314 print '<td class="center">';
315 if (!empty($line->fk_bank)) {
316 print $langs->trans('Paid');
317 if (!empty($line->fk_payment_loan)) {
318 print '&nbsp;<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>';
319 }
320 } elseif (!$printed) {
321 print '<a class="butAction smallpaddingimp" href="'.DOL_URL_ROOT.'/loan/payment/payment.php?id='.$object->id.'&action=create">'.$langs->trans('DoPayment').'</a>';
322 $printed = true;
323 }
324 print '</td>';
325 print '</tr>'."\n";
326 $i++;
327 $capital = $cap_rest;
328 }
329}
330
331print '</table>';
332print '</div>';
333
334print '</br>';
335
336if (count($echeances->lines) == 0) {
337 $label = $langs->trans("Create");
338} else {
339 $label = $langs->trans("Save");
340}
341print '<div class="center"><input type="submit" class="button button-add" value="'.$label.'" '.(($pay_without_schedule == 1) ? 'disabled title="'.$langs->trans('CantUseScheduleWithLoanStartedToPaid').'"' : '').'title=""></div>';
342print '</form>';
343
344// End of page
345llxFooter();
346$db->close();
if(!defined('NOREQUIRESOC')) if(!defined( 'NOREQUIRETRAN')) if(!defined('NOTOKENRENEWAL')) if(!defined( 'NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined( 'NOREQUIREAJAX')) llxHeader()
Empty header.
Definition wrapper.php:56
llxFooter()
Empty footer.
Definition wrapper.php:70
Loan.
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.
Definition date.lib.php:122
dol_banner_tab($object, $paramid, $morehtml='', $shownav=1, $fieldid='rowid', $fieldref='ref', $morehtmlref='', $moreparam='', $nodbprefix=0, $morehtmlleft='', $morehtmlstatus='', $onlybanner=0, $morehtmlright='')
Show tab footer of a card.
dol_get_fiche_head($links=array(), $active='', $title='', $notab=0, $picto='', $pictoisfullpath=0, $morehtmlright='', $morecss='', $limittoshow=0, $moretabssuffix='', $dragdropfile=0)
Show tabs of a record.
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
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.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
dol_now($mode='auto')
Return date for now.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='', $noduplicate=0)
Set event messages in dol_events session object.
price2numjs(amount)
Function similar to PHP price2num()
loan_prepare_head($object)
Prepare array with list of tabs.
Definition loan.lib.php:33
if(preg_match('/crypted:/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
Definition repair.php:120
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.