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