dolibarr 19.0.4
paymentexpensereport.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2015-2017 Alexandre Spangaro <aspangaro@open-dsi.fr>
3 * Copyright (C) 2018 Nicolas ZABOURI <info@inovea-conseil.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
25require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
26
27
32{
36 public $element = 'payment_expensereport';
37
41 public $table_element = 'payment_expensereport';
42
46 public $picto = 'payment';
47
51 public $rowid;
52
56 public $fk_expensereport;
57
58 public $datec = '';
59 public $tms = '';
60 public $datep = '';
61 public $amount; // Total amount of payment
62 public $amounts = array(); // Array of amounts
63
67 public $fk_typepayment;
68
69 public $num_payment;
70
74 public $fk_bank;
75
79 public $fk_user_creat;
80
84 public $fk_user_modif;
85
86 public $type_code;
87 public $type_label;
88
92 public $bank_account;
93
97 public $bank_line;
98
102 public $label;
103
104
110 public function __construct($db)
111 {
112 $this->db = $db;
113 }
114
122 public function create($user)
123 {
124 global $conf, $langs;
125
126 $error = 0;
127
128 $now = dol_now();
129 // Validate parameters
130 if (!$this->datep) {
131 $this->error = 'ErrorBadValueForParameterCreatePaymentExpenseReport';
132 return -1;
133 }
134
135 // Clean parameters
136 if (isset($this->fk_expensereport)) {
137 $this->fk_expensereport = trim($this->fk_expensereport);
138 }
139 if (isset($this->amount)) {
140 $this->amount = trim($this->amount);
141 }
142 if (isset($this->fk_typepayment)) {
143 $this->fk_typepayment = trim($this->fk_typepayment);
144 }
145 if (isset($this->num_payment)) {
146 $this->num_payment = trim($this->num_payment);
147 }
148 if (isset($this->note)) {
149 $this->note = trim($this->note);
150 }
151 if (isset($this->note_public)) {
152 $this->note_public = trim($this->note_public);
153 }
154 if (isset($this->note_private)) {
155 $this->note_private = trim($this->note_private);
156 }
157 if (isset($this->fk_bank)) {
158 $this->fk_bank = ((int) $this->fk_bank);
159 }
160 if (isset($this->fk_user_creat)) {
161 $this->fk_user_creat = ((int) $this->fk_user_creat);
162 }
163 if (isset($this->fk_user_modif)) {
164 $this->fk_user_modif = ((int) $this->fk_user_modif);
165 }
166
167 $totalamount = 0;
168 foreach ($this->amounts as $key => $value) { // How payment is dispatch
169 $newvalue = price2num($value, 'MT');
170 $this->amounts[$key] = $newvalue;
171 $totalamount += $newvalue;
172 }
173 $totalamount = price2num($totalamount);
174
175 // Check parameters
176 if ($totalamount == 0) {
177 return -1; // On accepte les montants negatifs pour les rejets de prelevement mais pas null
178 }
179
180
181 $this->db->begin();
182
183 if ($totalamount != 0) {
184 $sql = "INSERT INTO ".MAIN_DB_PREFIX."payment_expensereport (fk_expensereport, datec, datep, amount,";
185 $sql .= " fk_typepayment, num_payment, note, fk_user_creat, fk_bank)";
186 $sql .= " VALUES ($this->fk_expensereport, '".$this->db->idate($now)."',";
187 $sql .= " '".$this->db->idate($this->datep)."',";
188 $sql .= " ".price2num($totalamount).",";
189 $sql .= " ".((int) $this->fk_typepayment).", '".$this->db->escape($this->num_payment)."', '".$this->db->escape($this->note_public)."', ".((int) $user->id).",";
190 $sql .= " 0)"; // fk_bank is ID of transaction into ll_bank
191
192 dol_syslog(get_class($this)."::create", LOG_DEBUG);
193 $resql = $this->db->query($sql);
194 if ($resql) {
195 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."payment_expensereport");
196 } else {
197 $error++;
198 }
199 }
200
201 if ($totalamount != 0 && !$error) {
202 $this->amount = $totalamount;
203 $this->db->commit();
204 return $this->id;
205 } else {
206 $this->error = $this->db->error();
207 $this->db->rollback();
208 return -1;
209 }
210 }
211
218 public function fetch($id)
219 {
220 $sql = "SELECT";
221 $sql .= " t.rowid,";
222 $sql .= " t.fk_expensereport,";
223 $sql .= " t.datec,";
224 $sql .= " t.tms,";
225 $sql .= " t.datep,";
226 $sql .= " t.amount,";
227 $sql .= " t.fk_typepayment,";
228 $sql .= " t.num_payment,";
229 $sql .= " t.note as note_public,";
230 $sql .= " t.fk_bank,";
231 $sql .= " t.fk_user_creat,";
232 $sql .= " t.fk_user_modif,";
233 $sql .= " pt.code as type_code, pt.libelle as type_label,";
234 $sql .= ' b.fk_account';
235 $sql .= " FROM ".MAIN_DB_PREFIX."payment_expensereport as t";
236 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_paiement as pt ON t.fk_typepayment = pt.id";
237 $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'bank as b ON t.fk_bank = b.rowid';
238 $sql .= " WHERE t.rowid = ".((int) $id);
239
240 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
241 $resql = $this->db->query($sql);
242 if ($resql) {
243 if ($this->db->num_rows($resql)) {
244 $obj = $this->db->fetch_object($resql);
245
246 $this->id = $obj->rowid;
247 $this->ref = $obj->rowid;
248
249 $this->fk_expensereport = $obj->fk_expensereport;
250 $this->datec = $this->db->jdate($obj->datec);
251 $this->tms = $this->db->jdate($obj->tms);
252 $this->datep = $this->db->jdate($obj->datep);
253 $this->amount = $obj->amount;
254 $this->fk_typepayment = $obj->fk_typepayment;
255 $this->num_payment = $obj->num_payment;
256 $this->note_public = $obj->note_public;
257 $this->fk_bank = $obj->fk_bank;
258 $this->fk_user_creat = $obj->fk_user_creat;
259 $this->fk_user_modif = $obj->fk_user_modif;
260
261 $this->type_code = $obj->type_code;
262 $this->type_label = $obj->type_label;
263
264 $this->bank_account = $obj->fk_account;
265 $this->bank_line = $obj->fk_bank;
266 }
267 $this->db->free($resql);
268
269 return 1;
270 } else {
271 $this->error = "Error ".$this->db->lasterror();
272 return -1;
273 }
274 }
275
276 // phpcs:disable Generic.CodeAnalysis.UnusedFunctionParameter
284 public function update($user = null, $notrigger = 0)
285 {
286 // phpcs:enable
287 global $conf, $langs;
288 $error = 0;
289
290 // Clean parameters
291
292 if (isset($this->fk_expensereport)) {
293 $this->fk_expensereport = trim($this->fk_expensereport);
294 }
295 if (isset($this->amount)) {
296 $this->amount = trim($this->amount);
297 }
298 if (isset($this->fk_typepayment)) {
299 $this->fk_typepayment = trim($this->fk_typepayment);
300 }
301 if (isset($this->num_payment)) {
302 $this->num_payment = trim($this->num_payment);
303 }
304 if (isset($this->note)) {
305 $this->note = trim($this->note);
306 }
307 if (isset($this->fk_bank)) {
308 $this->fk_bank = trim($this->fk_bank);
309 }
310 if (isset($this->fk_user_creat)) {
311 $this->fk_user_creat = trim($this->fk_user_creat);
312 }
313 if (isset($this->fk_user_modif)) {
314 $this->fk_user_modif = trim($this->fk_user_modif);
315 }
316
317
318 // Check parameters
319 // Put here code to add control on parameters values
320
321 // Update request
322 $sql = "UPDATE ".MAIN_DB_PREFIX."payment_expensereport SET";
323
324 $sql .= " fk_expensereport=".(isset($this->fk_expensereport) ? $this->fk_expensereport : "null").",";
325 $sql .= " datec=".(dol_strlen($this->datec) != 0 ? "'".$this->db->idate($this->datec)."'" : 'null').",";
326 $sql .= " tms=".(dol_strlen($this->tms) != 0 ? "'".$this->db->idate($this->tms)."'" : 'null').",";
327 $sql .= " datep=".(dol_strlen($this->datep) != 0 ? "'".$this->db->idate($this->datep)."'" : 'null').",";
328 $sql .= " amount=".(isset($this->amount) ? $this->amount : "null").",";
329 $sql .= " fk_typepayment=".(isset($this->fk_typepayment) ? $this->fk_typepayment : "null").",";
330 $sql .= " num_payment=".(isset($this->num_payment) ? "'".$this->db->escape($this->num_payment)."'" : "null").",";
331 $sql .= " note=".(isset($this->note) ? "'".$this->db->escape($this->note)."'" : "null").",";
332 $sql .= " fk_bank=".(isset($this->fk_bank) ? $this->fk_bank : "null").",";
333 $sql .= " fk_user_creat=".(isset($this->fk_user_creat) ? $this->fk_user_creat : "null").",";
334 $sql .= " fk_user_modif=".(isset($this->fk_user_modif) ? $this->fk_user_modif : "null");
335
336
337 $sql .= " WHERE rowid=".((int) $this->id);
338
339 $this->db->begin();
340
341 dol_syslog(get_class($this)."::update", LOG_DEBUG);
342 $resql = $this->db->query($sql);
343 if (!$resql) {
344 $error++;
345 $this->errors[] = "Error ".$this->db->lasterror();
346 }
347
348 // Commit or rollback
349 if ($error) {
350 foreach ($this->errors as $errmsg) {
351 dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
352 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
353 }
354 $this->db->rollback();
355 return -1 * $error;
356 } else {
357 $this->db->commit();
358 return 1;
359 }
360 }
361
362 // phpcs:disable Generic.CodeAnalysis.UnusedFunctionParameter
370 public function delete($user, $notrigger = 0)
371 {
372 // phpcs:enable
373 global $conf, $langs;
374 $error = 0;
375
376 $this->db->begin();
377
378 if (!$error) {
379 $sql = "DELETE FROM ".MAIN_DB_PREFIX."bank_url";
380 $sql .= " WHERE type='payment_expensereport' AND url_id=".((int) $this->id);
381
382 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
383 $resql = $this->db->query($sql);
384 if (!$resql) {
385 $error++;
386 $this->errors[] = "Error ".$this->db->lasterror();
387 }
388 }
389
390 if (!$error) {
391 $sql = "DELETE FROM ".MAIN_DB_PREFIX."payment_expensereport";
392 $sql .= " WHERE rowid=".((int) $this->id);
393
394 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
395 $resql = $this->db->query($sql);
396 if (!$resql) {
397 $error++;
398 $this->errors[] = "Error ".$this->db->lasterror();
399 }
400 }
401
402 // Commit or rollback
403 if ($error) {
404 foreach ($this->errors as $errmsg) {
405 dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
406 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
407 }
408 $this->db->rollback();
409 return -1 * $error;
410 } else {
411 $this->db->commit();
412 return 1;
413 }
414 }
415
416
417
425 public function createFromClone(User $user, $fromid)
426 {
427 $error = 0;
428
429 $object = new PaymentExpenseReport($this->db);
430
431 $this->db->begin();
432
433 // Load source object
434 $object->fetch($fromid);
435 $object->id = 0;
436 $object->statut = 0;
437
438 // Clear fields
439 // ...
440
441 // Create clone
442 $object->context['createfromclone'] = 'createfromclone';
443 $result = $object->create($user);
444
445 // Other options
446 if ($result < 0) {
447 $this->error = $object->error;
448 $error++;
449 }
450
451 unset($object->context['createfromclone']);
452
453 // End
454 if (!$error) {
455 $this->db->commit();
456 return $object->id;
457 } else {
458 $this->db->rollback();
459 return -1;
460 }
461 }
462
463
470 public function getLibStatut($mode = 0)
471 {
472 return '';
473 }
474
475 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
483 public function LibStatut($status, $mode = 0)
484 {
485 // phpcs:enable
486 //global $langs;
487
488 return '';
489 }
490
491
499 public function initAsSpecimen()
500 {
501 $this->id = 0;
502
503 $this->fk_expensereport = '';
504 $this->datec = '';
505 $this->tms = '';
506 $this->datep = '';
507 $this->amount = '';
508 $this->fk_typepayment = '';
509 $this->num_payment = '';
510 $this->note = '';
511 $this->fk_bank = '';
512 $this->fk_user_creat = '';
513 $this->fk_user_modif = '';
514 }
515
516
529 public function addPaymentToBank($user, $mode, $label, $accountid, $emetteur_nom, $emetteur_banque)
530 {
531 global $langs, $conf;
532
533 $error = 0;
534
535 if (isModEnabled("banque")) {
536 include_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
537
538 $acc = new Account($this->db);
539 $acc->fetch($accountid);
540
541 //Fix me field
542 $total = $this->amount;
543
544 if ($mode == 'payment_expensereport') {
545 $amount = $total;
546 }
547
548 // Insert payment into llx_bank
549 $bank_line_id = $acc->addline(
550 $this->datep,
551 $this->fk_typepayment, // Payment mode id or code ("CHQ or VIR for example")
552 $label,
553 -$amount,
554 $this->num_payment,
555 '',
556 $user,
557 $emetteur_nom,
558 $emetteur_banque
559 );
560
561 // Update fk_bank in llx_paiement.
562 // So we wil know the payment that have generated the bank transaction
563 if ($bank_line_id > 0) {
564 $result = $this->update_fk_bank($bank_line_id);
565 if ($result <= 0) {
566 $error++;
567 dol_print_error($this->db);
568 }
569
570 // Add link 'payment', 'payment_supplier', 'payment_expensereport' in bank_url between payment and bank transaction
571 $url = '';
572 if ($mode == 'payment_expensereport') {
573 $url = DOL_URL_ROOT.'/expensereport/payment/card.php?rowid=';
574 }
575 if ($url) {
576 $result = $acc->add_url_line($bank_line_id, $this->id, $url, '(paiement)', $mode);
577 if ($result <= 0) {
578 $error++;
579 dol_print_error($this->db);
580 }
581 }
582
583 // Add link 'user' in bank_url between user and bank transaction
584 if (!$error) {
585 foreach ($this->amounts as $key => $value) { // We should have always same user but we loop in case of.
586 if ($mode == 'payment_expensereport') {
587 $fuser = new User($this->db);
588 $fuser->fetch($key);
589
590 $result = $acc->add_url_line(
591 $bank_line_id,
592 $fuser->id,
593 DOL_URL_ROOT.'/user/card.php?id=',
594 $fuser->getFullName($langs),
595 'user'
596 );
597 if ($result <= 0) {
598 $this->error = $this->db->lasterror();
599 dol_syslog(get_class($this).'::addPaymentToBank '.$this->error);
600 $error++;
601 }
602 }
603 }
604 }
605 } else {
606 $this->error = $acc->error;
607 $this->errors = $acc->errors;
608 $error++;
609 }
610 }
611
612 if (!$error) {
613 return 1;
614 } else {
615 return -1;
616 }
617 }
618
619
620 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
627 public function update_fk_bank($id_bank)
628 {
629 // phpcs:enable
630 $sql = "UPDATE ".MAIN_DB_PREFIX."payment_expensereport SET fk_bank = ".((int) $id_bank)." WHERE rowid = ".((int) $this->id);
631
632 dol_syslog(get_class($this)."::update_fk_bank", LOG_DEBUG);
633 $result = $this->db->query($sql);
634 if ($result) {
635 return 1;
636 } else {
637 $this->error = $this->db->error();
638 return 0;
639 }
640 }
641
649 public function getNomUrl($withpicto = 0, $maxlen = 0)
650 {
651 global $langs, $hookmanager;
652
653 $result = '';
654
655 if (empty($this->ref)) {
656 $this->ref = $this->label;
657 }
658 $label = img_picto('', $this->picto).' <u class="paddingrightonly">'.$langs->trans("Payment").'</u>';
659 if (isset($this->status)) {
660 $label .= ' '.$this->getLibStatut(5);
661 }
662 if (!empty($this->ref)) {
663 $label .= '<br><b>'.$langs->trans('Ref').':</b> '.$this->ref;
664 }
665 if (!empty($this->datep)) {
666 $label .= '<br><b>'.$langs->trans('Date').':</b> '.dol_print_date($this->datep, 'dayhour');
667 }
668
669 if (!empty($this->id)) {
670 $link = '<a href="'.DOL_URL_ROOT.'/expensereport/payment/card.php?id='.$this->id.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
671 $linkend = '</a>';
672
673 if ($withpicto) {
674 $result .= ($link.img_object($label, 'payment', 'class="classfortooltip"').$linkend.' ');
675 }
676 if ($withpicto && $withpicto != 2) {
677 $result .= ' ';
678 }
679 if ($withpicto != 2) {
680 $result .= $link.($maxlen ? dol_trunc($this->ref, $maxlen) : $this->ref).$linkend;
681 }
682 }
683 global $action;
684 $hookmanager->initHooks(array($this->element . 'dao'));
685 $parameters = array('id'=>$this->id, 'getnomurl' => &$result);
686 $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
687 if ($reshook > 0) {
688 $result = $hookmanager->resPrint;
689 } else {
690 $result .= $hookmanager->resPrint;
691 }
692 return $result;
693 }
694
701 public function info($id)
702 {
703 $sql = 'SELECT e.rowid, e.datec, e.fk_user_creat, e.fk_user_modif, e.tms';
704 $sql .= ' FROM '.MAIN_DB_PREFIX.'payment_expensereport as e';
705 $sql .= ' WHERE e.rowid = '.((int) $id);
706
707 dol_syslog(get_class($this).'::info', LOG_DEBUG);
708 $result = $this->db->query($sql);
709
710 if ($result) {
711 if ($this->db->num_rows($result)) {
712 $obj = $this->db->fetch_object($result);
713
714 $this->id = $obj->rowid;
715
716 $this->user_creation_id = $obj->fk_user_creat;
717 $this->user_modification_id = $obj->fk_user_modif;
718 $this->date_creation = $this->db->jdate($obj->datec);
719 $this->date_modification = $this->db->jdate($obj->tms);
720 }
721 $this->db->free($result);
722 } else {
723 dol_print_error($this->db);
724 }
725 }
726
734 public function getKanbanView($option = '', $arraydata = null)
735 {
736 global $langs;
737
738 $selected = (empty($arraydata['selected']) ? 0 : $arraydata['selected']);
739
740 $return = '<div class="box-flex-item box-flex-grow-zero">';
741 $return .= '<div class="info-box info-box-sm">';
742 $return .= '<span class="info-box-icon bg-infobox-action">';
743 $return .= img_picto('', $this->picto);
744 $return .= '</span>';
745 $return .= '<div class="info-box-content">';
746 $return .= '<span class="info-box-ref inline-block tdoverflowmax150 valignmiddle">'.(method_exists($this, 'getNomUrl') ? $this->getNomUrl(1) : $this->ref).'</span>';
747 if ($selected >= 0) {
748 $return .= '<input id="cb'.$this->id.'" class="flat checkforselect fright" type="checkbox" name="toselect[]" value="'.$this->id.'"'.($selected ? ' checked="checked"' : '').'>';
749 }
750 if (property_exists($this, 'datep')) {
751 $return .= '<br><span class="opacitymedium">'.$langs->trans("Date").'</span> : <span class="info-box-label">'.dol_print_date($this->db->jdate($this->datep), 'dayhour').'</span>';
752 }
753 if (property_exists($this, 'fk_typepayment')) {
754 $return .= '<br><span class="opacitymedium">'.$langs->trans("Type").'</span> : <span class="info-box-label">'.$this->fk_typepayment.'</span>';
755 }
756 if (property_exists($this, 'fk_bank') && !is_null($this->fk_bank)) {
757 $return .= '<br><span class="opacitymedium">'.$langs->trans("Account").'</span> : <span class="info-box-label">'.$this->fk_bank.'</span>';
758 }
759 if (property_exists($this, 'amount')) {
760 $return .= '<br><span class="opacitymedium">'.$langs->trans("Amount").'</span> : <span class="info-box-label amount">'.price($this->amount).'</span>';
761 }
762 if (method_exists($this, 'getLibStatut')) {
763 $return .= '<br><div class="info-box-status">'.$this->getLibStatut(3).'</div>';
764 }
765 $return .= '</div>';
766 $return .= '</div>';
767 $return .= '</div>';
768 return $return;
769 }
770}
print $langs trans("AuditedSecurityEvents").'</strong >< span class="opacitymedium"></span >< br > status
Definition security.php:604
$object ref
Definition info.php:79
Class to manage bank accounts.
Parent class of all other business classes (invoices, contracts, proposals, orders,...
Class to manage payments of expense report.
addPaymentToBank($user, $mode, $label, $accountid, $emetteur_nom, $emetteur_banque)
Add record into bank for payment with links between this bank record and invoices of payment.
fetch($id)
Load object in memory from database.
create($user)
Create payment of expense report into database.
info($id)
Tab information on object.
getLibStatut($mode=0)
Return the label of the status.
initAsSpecimen()
Initialise an instance with random values.
getKanbanView($option='', $arraydata=null)
Return clicable link of object (with eventually picto)
getNomUrl($withpicto=0, $maxlen=0)
Return clicable name (with picto eventually)
LibStatut($status, $mode=0)
Return the label of a given status.
createFromClone(User $user, $fromid)
Load an object from its id and create a new one in database.
update($user=null, $notrigger=0)
Update database.
update_fk_bank($id_bank)
Update link between the expense report payment and the generated line in llx_bank.
Class to manage Dolibarr users.
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
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.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
dol_trunc($string, $size=40, $trunc='right', $stringencoding='UTF-8', $nodot=0, $display=0)
Truncate a string to a particular length adding '…' if string larger than length.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0, $cleanalsojavascript=0)
Returns text escaped for inclusion in HTML alt or title or value tags, or into values of HTML input f...