dolibarr 21.0.0-alpha
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-2024 Alexandre Spangaro <alexandre@inovea-conseil.com>
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';
34require_once DOL_DOCUMENT_ROOT.'/core/class/html.formprojet.class.php';
35if (isModEnabled('project')) {
36 require_once DOL_DOCUMENT_ROOT.'/projet/class/project.class.php';
37}
38
39$loanid = GETPOSTINT('loanid');
40$action = GETPOST('action', 'aZ09');
41
42// Security check
43$socid = 0;
44if (GETPOSTISSET('socid')) {
45 $socid = GETPOSTINT('socid');
46}
47if ($user->socid) {
48 $socid = $user->socid;
49}
50if (!$user->hasRight('loan', 'calc')) {
52}
53
54// Load translation files required by the page
55$langs->loadLangs(array("compta", "bills", "loan"));
56
57$object = new Loan($db);
58$object->fetch($loanid);
59
60$echeances = new LoanSchedule($db);
61$echeances->fetchAll($object->id);
62
63if ($object->paid > 0 && count($echeances->lines) == 0) {
64 $pay_without_schedule = 1;
65} else {
66 $pay_without_schedule = 0;
67}
68
69/*
70 * Actions
71 */
72
73if ($action == 'createecheancier' && empty($pay_without_schedule)) {
74 $db->begin();
75 $i = 1;
76 while ($i < $object->nbterm + 1) {
77 $date = GETPOSTINT('hi_date'.$i);
78 $mens = price2num(GETPOST('mens'.$i));
79 $int = price2num(GETPOST('hi_interets'.$i));
80 $insurance = price2num(GETPOST('hi_insurance'.$i));
81
82 $new_echeance = new LoanSchedule($db);
83
84 $new_echeance->fk_loan = $object->id;
85 $new_echeance->datec = dol_now();
86 $new_echeance->tms = dol_now();
87 $new_echeance->datep = $date;
88 $new_echeance->amount_capital = $mens - (float) $int;
89 $new_echeance->amount_insurance = $insurance;
90 $new_echeance->amount_interest = $int;
91 $new_echeance->fk_typepayment = 3;
92 $new_echeance->fk_bank = 0;
93 $new_echeance->fk_user_creat = $user->id;
94 $new_echeance->fk_user_modif = $user->id;
95 $result = $new_echeance->create($user);
96 if ($result < 0) {
97 setEventMessages($new_echeance->error, $echeance->errors, 'errors');
98 $db->rollback();
99 unset($echeances->lines);
100 break;
101 }
102 $echeances->lines[] = $new_echeance;
103 $i++;
104 }
105 if ($result > 0) {
106 $db->commit();
107 }
108}
109
110if ($action == 'updateecheancier' && empty($pay_without_schedule)) {
111 $db->begin();
112 $i = 1;
113 while ($i < $object->nbterm + 1) {
114 $mens = price2num(GETPOST('mens'.$i));
115 $int = price2num(GETPOST('hi_interets'.$i));
116 $id = GETPOST('hi_rowid'.$i);
117 $insurance = price2num(GETPOST('hi_insurance'.$i));
118
119 $new_echeance = new LoanSchedule($db);
120 $new_echeance->fetch($id);
121 $new_echeance->tms = dol_now();
122 $new_echeance->amount_capital = $mens - (float) $int;
123 $new_echeance->amount_insurance = $insurance;
124 $new_echeance->amount_interest = $int;
125 $new_echeance->fk_user_modif = $user->id;
126 $result = $new_echeance->update($user, 0);
127 if ($result < 0) {
128 setEventMessages(null, $new_echeance->errors, 'errors');
129 $db->rollback();
130 $echeances->fetchAll($object->id);
131 break;
132 }
133
134 $echeances->lines[$i - 1] = $new_echeance;
135 $i++;
136 }
137 if ($result > 0) {
138 $db->commit();
139 }
140}
141
142/*
143 * View
144 */
145$form = new Form($db);
146$formproject = new FormProjets($db);
147
148$title = $langs->trans("Loan").' - '.$langs->trans("Card");
149$help_url = 'EN:Module_Loan|FR:Module_Emprunt';
150
151llxHeader("", $title, $help_url, '', 0, 0, '', '', '', 'mod-loan page-card_schedule');
152
153$head = loan_prepare_head($object);
154print dol_get_fiche_head($head, 'FinancialCommitment', $langs->trans("Loan"), -1, 'money-bill-alt');
155
156$linkback = '<a href="'.DOL_URL_ROOT.'/loan/list.php?restore_lastsearch_values=1">'.$langs->trans("BackToList").'</a>';
157
158$morehtmlref = '<div class="refidno">';
159// Ref loan
160$morehtmlref .= $form->editfieldkey("Label", 'label', $object->label, $object, 0, 'string', '', 0, 1);
161$morehtmlref .= $form->editfieldval("Label", 'label', $object->label, $object, 0, 'string', '', null, null, '', 1);
162// Project
163if (isModEnabled('project')) {
164 $langs->loadLangs(array("projects"));
165 $morehtmlref .= '<br>'.$langs->trans('Project').' : ';
166 if ($user->hasRight('loan', 'write')) {
167 if ($action != 'classify') {
168 //$morehtmlref .= '<a class="editfielda" href="'.$_SERVER['PHP_SELF'].'?action=classify&token='.newToken().'&id='.$object->id.'">'.img_edit($langs->transnoentitiesnoconv('SetProject')).'</a> : ';
169 if ($action == 'classify') {
170 //$morehtmlref.=$form->form_project($_SERVER['PHP_SELF'] . '?id=' . $object->id, $object->socid, $object->fk_project, 'projectid', 0, 0, 1, 1);
171 $morehtmlref .= '<form method="post" action="'.$_SERVER['PHP_SELF'].'?id='.$object->id.'">';
172 $morehtmlref .= '<input type="hidden" name="action" value="classin">';
173 $morehtmlref .= '<input type="hidden" name="token" value="'.newToken().'">';
174 $morehtmlref .= $formproject->select_projects(-1, $object->fk_project, 'projectid', $maxlength, 0, 1, 0, 1, 0, 0, '', 1);
175 $morehtmlref .= '<input type="submit" class="button valignmiddle" value="'.$langs->trans("Modify").'">';
176 $morehtmlref .= '</form>';
177 } else {
178 $morehtmlref .= $form->form_project($_SERVER['PHP_SELF'].'?id='.$object->id, -1, $object->fk_project, 'none', 0, 0, 0, 1, '', 'maxwidth300');
179 }
180 }
181 } else {
182 if (!empty($object->fk_project)) {
183 $proj = new Project($db);
184 $proj->fetch($object->fk_project);
185 $morehtmlref .= ' : '.$proj->getNomUrl(1);
186 if ($proj->title) {
187 $morehtmlref .= ' - '.$proj->title;
188 }
189 } else {
190 $morehtmlref .= '';
191 }
192 }
193}
194$morehtmlref .= '</div>';
195
196$morehtmlstatus = '';
197
198dol_banner_tab($object, 'loanid', $linkback, 1, 'rowid', 'ref', $morehtmlref, '', 0, '', $morehtmlstatus);
199
200?>
201<script type="text/javascript">
202$(document).ready(function() {
203 $('[name^="mens"]').focusout(function() {
204 var echeance=$(this).attr('ech');
205 var mens=price2numjs($(this).val());
206 var idcap=echeance-1;
207 idcap = '#hi_capital'+idcap;
208 var capital=price2numjs($(idcap).val());
209 console.log("Change monthly amount echeance="+echeance+" idcap="+idcap+" capital="+capital);
210 $.ajax({
211 method: "GET",
212 dataType: 'json',
213 url: 'calcmens.php',
214 data: { echeance: echeance, mens: mens, capital:capital, rate:<?php echo $object->rate / 100; ?>, nbterm: <?php echo $object->nbterm; ?>, token: '<?php echo currentToken(); ?>' },
215 success: function(data) {
216 $.each(data, function(index, element) {
217 var idcap_res='#hi_capital'+index;
218 var idcap_res_srt='#capital'+index;
219 var interet_res='#hi_interets'+index;
220 var interet_res_str='#interets'+index;
221 var men_res='#mens'+index;
222 $(idcap_res).val(element.cap_rest);
223 $(idcap_res_srt).text(element.cap_rest_str);
224 $(interet_res).val(element.interet);
225 $(interet_res_str).text(element.interet_str);
226 $(men_res).val(element.mens);
227 });
228 }
229 });
230 });
231});
232</script>
233<?php
234
235if ($pay_without_schedule == 1) {
236 print '<div class="warning">'.$langs->trans('CantUseScheduleWithLoanStartedToPaid').'</div>'."\n";
237}
238
239print '<form name="createecheancier" action="'.$_SERVER["PHP_SELF"].'" method="POST">';
240print '<input type="hidden" name="token" value="'.newToken().'">';
241print '<input type="hidden" name="loanid" value="'.$loanid.'">';
242if (count($echeances->lines) > 0) {
243 print '<input type="hidden" name="action" value="updateecheancier">';
244} else {
245 print '<input type="hidden" name="action" value="createecheancier">';
246}
247
248//print_fiche_titre($langs->trans("FinancialCommitment"));
249print '<br>';
250
251print '<div class="div-table-responsive-no-min">';
252print '<table class="border centpercent">';
253
254$colspan = 6;
255if (count($echeances->lines) > 0) {
256 $colspan++;
257}
258
259print '<tr class="liste_titre">';
260print '<th class="center">'.$langs->trans("Term").'</th>';
261print '<th class="center">'.$langs->trans("Date").'</th>';
262print '<th class="center">'.$langs->trans("Insurance");
263print '<th class="center">'.$langs->trans("InterestAmount").'</th>';
264print '<th class="center">'.$langs->trans("Amount").'</th>';
265print '<th class="center">'.$langs->trans("CapitalRemain");
266print '<br>('.price($object->capital, 0, '', 1, -1, -1, $conf->currency).')';
267print '<input type="hidden" name="hi_capital0" id ="hi_capital0" value="'.$object->capital.'">';
268print '</th>';
269if (count($echeances->lines) > 0) {
270 print '<th class="center">'.$langs->trans('DoPayment').'</th>';
271}
272print '</tr>'."\n";
273
274if ($object->nbterm > 0 && count($echeances->lines) == 0) {
275 $i = 1;
276 $capital = $object->capital;
277 $insurance = (float) $object->insurance_amount / $object->nbterm;
278 $insurance = price2num($insurance, 'MT');
279 $regulInsurance = price2num((float) $object->insurance_amount - ((float) $insurance * $object->nbterm));
280 while ($i < $object->nbterm + 1) {
281 $mens = price2num($echeances->calcMonthlyPayments($capital, $object->rate / 100, $object->nbterm - $i + 1), 'MT');
282 $int = ($capital * ($object->rate / 12)) / 100;
283 $int = price2num($int, 'MT');
284 $insu = ((float) $insurance + (($i == 1) ? (float) $regulInsurance : 0));
285 $cap_rest = price2num((float) $capital - ((float) $mens - (float) $int), 'MT');
286 print '<tr>';
287 print '<td class="center" id="n'.$i.'">'.$i.'</td>';
288 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>';
289 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.'">';
290 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.'">';
291 print '<td class="center"><input class="width75 right" name="mens'.$i.'" id="mens'.$i.'" value="'.$mens.'" ech="'.$i.'"></td>';
292 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.'">';
293 print '</tr>'."\n";
294 $i++;
295 $capital = $cap_rest;
296 }
297} elseif (count($echeances->lines) > 0) {
298 $i = 1;
299 $capital = $object->capital;
300 $insurance = (float) $object->insurance_amount / $object->nbterm;
301 $insurance = price2num($insurance, 'MT');
302 $regulInsurance = price2num((float) $object->insurance_amount - ((float) $insurance * $object->nbterm));
303 $printed = false;
304 foreach ($echeances->lines as $line) {
305 $mens = $line->amount_capital + $line->amount_interest;
306 $int = $line->amount_interest;
307 $insu = ((float) $insurance + (($i == 1) ? (float) $regulInsurance : 0));
308 $cap_rest = price2num($capital - ($mens - $int), 'MT');
309
310 print '<tr>';
311 print '<td class="center" id="n'.$i.'"><input type="hidden" name="hi_rowid'.$i.'" id ="hi_rowid'.$i.'" value="'.$line->id.'">'.$i.'</td>';
312 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>';
313 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.'">';
314 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.'">';
315 if (empty($line->fk_bank)) {
316 print '<td class="center"><input class="right width75" name="mens'.$i.'" id="mens'.$i.'" value="'.$mens.'" ech="'.$i.'"></td>';
317 } else {
318 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.'">';
319 }
320
321 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.'">';
322 print '<td class="center">';
323 if (!empty($line->fk_bank)) {
324 print $langs->trans('Paid');
325 if (!empty($line->fk_payment_loan)) {
326 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>';
327 }
328 } elseif (!$printed) {
329 print '<a class="butAction smallpaddingimp" href="'.DOL_URL_ROOT.'/loan/payment/payment.php?id='.$object->id.'&action=create">'.$langs->trans('DoPayment').'</a>';
330 $printed = true;
331 }
332 print '</td>';
333 print '</tr>'."\n";
334 $i++;
335 $capital = $cap_rest;
336 }
337}
338
339print '</table>';
340print '</div>';
341
342print '</br>';
343
344if (count($echeances->lines) == 0) {
345 $label = $langs->trans("Create");
346} else {
347 $label = $langs->trans("Save");
348}
349print '<div class="center"><input type="submit" class="button button-add" value="'.$label.'" '.(($pay_without_schedule == 1) ? 'disabled title="'.$langs->trans('CantUseScheduleWithLoanStartedToPaid').'"' : '').'title=""></div>';
350print '</form>';
351
352// End of page
353llxFooter();
354$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($head='', $title='', $help_url='', $target='', $disablejs=0, $disablehead=0, $arrayofjs='', $arrayofcss='', $morequerystring='', $morecssonbody='', $replacemainareaby='', $disablenofollow=0, $disablenoindex=0)
Empty header.
Definition wrapper.php:70
Class to manage generation of HTML components Only common components must be here.
Class to manage building of HTML components.
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:124
llxFooter()
Footer empty.
Definition document.php:107
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:139
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.