dolibarr 18.0.6
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
89
95 public function __construct($db)
96 {
97 $this->db = $db;
98 }
99
107 public function create($user)
108 {
109 global $conf, $langs;
110
111 $error = 0;
112
113 $now = dol_now();
114 // Validate parameters
115 if (!$this->datep) {
116 $this->error = 'ErrorBadValueForParameterCreatePaymentExpenseReport';
117 return -1;
118 }
119
120 // Clean parameters
121 if (isset($this->fk_expensereport)) {
122 $this->fk_expensereport = trim($this->fk_expensereport);
123 }
124 if (isset($this->amount)) {
125 $this->amount = trim($this->amount);
126 }
127 if (isset($this->fk_typepayment)) {
128 $this->fk_typepayment = trim($this->fk_typepayment);
129 }
130 if (isset($this->num_payment)) {
131 $this->num_payment = trim($this->num_payment);
132 }
133 if (isset($this->note)) {
134 $this->note = trim($this->note);
135 }
136 if (isset($this->note_public)) {
137 $this->note_public = trim($this->note_public);
138 }
139 if (isset($this->note_private)) {
140 $this->note_private = trim($this->note_private);
141 }
142 if (isset($this->fk_bank)) {
143 $this->fk_bank = ((int) $this->fk_bank);
144 }
145 if (isset($this->fk_user_creat)) {
146 $this->fk_user_creat = ((int) $this->fk_user_creat);
147 }
148 if (isset($this->fk_user_modif)) {
149 $this->fk_user_modif = ((int) $this->fk_user_modif);
150 }
151
152 $totalamount = 0;
153 foreach ($this->amounts as $key => $value) { // How payment is dispatch
154 $newvalue = price2num($value, 'MT');
155 $this->amounts[$key] = $newvalue;
156 $totalamount += $newvalue;
157 }
158 $totalamount = price2num($totalamount);
159
160 // Check parameters
161 if ($totalamount == 0) {
162 return -1; // On accepte les montants negatifs pour les rejets de prelevement mais pas null
163 }
164
165
166 $this->db->begin();
167
168 if ($totalamount != 0) {
169 $sql = "INSERT INTO ".MAIN_DB_PREFIX."payment_expensereport (fk_expensereport, datec, datep, amount,";
170 $sql .= " fk_typepayment, num_payment, note, fk_user_creat, fk_bank)";
171 $sql .= " VALUES ($this->fk_expensereport, '".$this->db->idate($now)."',";
172 $sql .= " '".$this->db->idate($this->datep)."',";
173 $sql .= " ".price2num($totalamount).",";
174 $sql .= " ".((int) $this->fk_typepayment).", '".$this->db->escape($this->num_payment)."', '".$this->db->escape($this->note_public)."', ".((int) $user->id).",";
175 $sql .= " 0)"; // fk_bank is ID of transaction into ll_bank
176
177 dol_syslog(get_class($this)."::create", LOG_DEBUG);
178 $resql = $this->db->query($sql);
179 if ($resql) {
180 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."payment_expensereport");
181 } else {
182 $error++;
183 }
184 }
185
186 if ($totalamount != 0 && !$error) {
187 $this->amount = $totalamount;
188 $this->db->commit();
189 return $this->id;
190 } else {
191 $this->error = $this->db->error();
192 $this->db->rollback();
193 return -1;
194 }
195 }
196
203 public function fetch($id)
204 {
205 $sql = "SELECT";
206 $sql .= " t.rowid,";
207 $sql .= " t.fk_expensereport,";
208 $sql .= " t.datec,";
209 $sql .= " t.tms,";
210 $sql .= " t.datep,";
211 $sql .= " t.amount,";
212 $sql .= " t.fk_typepayment,";
213 $sql .= " t.num_payment,";
214 $sql .= " t.note as note_public,";
215 $sql .= " t.fk_bank,";
216 $sql .= " t.fk_user_creat,";
217 $sql .= " t.fk_user_modif,";
218 $sql .= " pt.code as type_code, pt.libelle as type_label,";
219 $sql .= ' b.fk_account';
220 $sql .= " FROM ".MAIN_DB_PREFIX."payment_expensereport as t";
221 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_paiement as pt ON t.fk_typepayment = pt.id";
222 $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'bank as b ON t.fk_bank = b.rowid';
223 $sql .= " WHERE t.rowid = ".((int) $id);
224
225 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
226 $resql = $this->db->query($sql);
227 if ($resql) {
228 if ($this->db->num_rows($resql)) {
229 $obj = $this->db->fetch_object($resql);
230
231 $this->id = $obj->rowid;
232 $this->ref = $obj->rowid;
233
234 $this->fk_expensereport = $obj->fk_expensereport;
235 $this->datec = $this->db->jdate($obj->datec);
236 $this->tms = $this->db->jdate($obj->tms);
237 $this->datep = $this->db->jdate($obj->datep);
238 $this->amount = $obj->amount;
239 $this->fk_typepayment = $obj->fk_typepayment;
240 $this->num_payment = $obj->num_payment;
241 $this->note_public = $obj->note_public;
242 $this->fk_bank = $obj->fk_bank;
243 $this->fk_user_creat = $obj->fk_user_creat;
244 $this->fk_user_modif = $obj->fk_user_modif;
245
246 $this->type_code = $obj->type_code;
247 $this->type_label = $obj->type_label;
248
249 $this->bank_account = $obj->fk_account;
250 $this->bank_line = $obj->fk_bank;
251 }
252 $this->db->free($resql);
253
254 return 1;
255 } else {
256 $this->error = "Error ".$this->db->lasterror();
257 return -1;
258 }
259 }
260
261 // phpcs:disable Generic.CodeAnalysis.UnusedFunctionParameter
269 public function update($user = null, $notrigger = 0)
270 {
271 // phpcs:enable
272 global $conf, $langs;
273 $error = 0;
274
275 // Clean parameters
276
277 if (isset($this->fk_expensereport)) {
278 $this->fk_expensereport = trim($this->fk_expensereport);
279 }
280 if (isset($this->amount)) {
281 $this->amount = trim($this->amount);
282 }
283 if (isset($this->fk_typepayment)) {
284 $this->fk_typepayment = trim($this->fk_typepayment);
285 }
286 if (isset($this->num_payment)) {
287 $this->num_payment = trim($this->num_payment);
288 }
289 if (isset($this->note)) {
290 $this->note = trim($this->note);
291 }
292 if (isset($this->fk_bank)) {
293 $this->fk_bank = trim($this->fk_bank);
294 }
295 if (isset($this->fk_user_creat)) {
296 $this->fk_user_creat = trim($this->fk_user_creat);
297 }
298 if (isset($this->fk_user_modif)) {
299 $this->fk_user_modif = trim($this->fk_user_modif);
300 }
301
302
303 // Check parameters
304 // Put here code to add control on parameters values
305
306 // Update request
307 $sql = "UPDATE ".MAIN_DB_PREFIX."payment_expensereport SET";
308
309 $sql .= " fk_expensereport=".(isset($this->fk_expensereport) ? $this->fk_expensereport : "null").",";
310 $sql .= " datec=".(dol_strlen($this->datec) != 0 ? "'".$this->db->idate($this->datec)."'" : 'null').",";
311 $sql .= " tms=".(dol_strlen($this->tms) != 0 ? "'".$this->db->idate($this->tms)."'" : 'null').",";
312 $sql .= " datep=".(dol_strlen($this->datep) != 0 ? "'".$this->db->idate($this->datep)."'" : 'null').",";
313 $sql .= " amount=".(isset($this->amount) ? $this->amount : "null").",";
314 $sql .= " fk_typepayment=".(isset($this->fk_typepayment) ? $this->fk_typepayment : "null").",";
315 $sql .= " num_payment=".(isset($this->num_payment) ? "'".$this->db->escape($this->num_payment)."'" : "null").",";
316 $sql .= " note=".(isset($this->note) ? "'".$this->db->escape($this->note)."'" : "null").",";
317 $sql .= " fk_bank=".(isset($this->fk_bank) ? $this->fk_bank : "null").",";
318 $sql .= " fk_user_creat=".(isset($this->fk_user_creat) ? $this->fk_user_creat : "null").",";
319 $sql .= " fk_user_modif=".(isset($this->fk_user_modif) ? $this->fk_user_modif : "null");
320
321
322 $sql .= " WHERE rowid=".((int) $this->id);
323
324 $this->db->begin();
325
326 dol_syslog(get_class($this)."::update", LOG_DEBUG);
327 $resql = $this->db->query($sql);
328 if (!$resql) {
329 $error++; $this->errors[] = "Error ".$this->db->lasterror();
330 }
331
332 // Commit or rollback
333 if ($error) {
334 foreach ($this->errors as $errmsg) {
335 dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
336 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
337 }
338 $this->db->rollback();
339 return -1 * $error;
340 } else {
341 $this->db->commit();
342 return 1;
343 }
344 }
345
346 // phpcs:disable Generic.CodeAnalysis.UnusedFunctionParameter
354 public function delete($user, $notrigger = 0)
355 {
356 // phpcs:enable
357 global $conf, $langs;
358 $error = 0;
359
360 $this->db->begin();
361
362 if (!$error) {
363 $sql = "DELETE FROM ".MAIN_DB_PREFIX."bank_url";
364 $sql .= " WHERE type='payment_expensereport' AND url_id=".((int) $this->id);
365
366 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
367 $resql = $this->db->query($sql);
368 if (!$resql) {
369 $error++; $this->errors[] = "Error ".$this->db->lasterror();
370 }
371 }
372
373 if (!$error) {
374 $sql = "DELETE FROM ".MAIN_DB_PREFIX."payment_expensereport";
375 $sql .= " WHERE rowid=".((int) $this->id);
376
377 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
378 $resql = $this->db->query($sql);
379 if (!$resql) {
380 $error++;
381 $this->errors[] = "Error ".$this->db->lasterror();
382 }
383 }
384
385 // Commit or rollback
386 if ($error) {
387 foreach ($this->errors as $errmsg) {
388 dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
389 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
390 }
391 $this->db->rollback();
392 return -1 * $error;
393 } else {
394 $this->db->commit();
395 return 1;
396 }
397 }
398
399
400
408 public function createFromClone(User $user, $fromid)
409 {
410 $error = 0;
411
412 $object = new PaymentExpenseReport($this->db);
413
414 $this->db->begin();
415
416 // Load source object
417 $object->fetch($fromid);
418 $object->id = 0;
419 $object->statut = 0;
420
421 // Clear fields
422 // ...
423
424 // Create clone
425 $object->context['createfromclone'] = 'createfromclone';
426 $result = $object->create($user);
427
428 // Other options
429 if ($result < 0) {
430 $this->error = $object->error;
431 $error++;
432 }
433
434 unset($object->context['createfromclone']);
435
436 // End
437 if (!$error) {
438 $this->db->commit();
439 return $object->id;
440 } else {
441 $this->db->rollback();
442 return -1;
443 }
444 }
445
446
453 public function getLibStatut($mode = 0)
454 {
455 return '';
456 }
457
458 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
466 public function LibStatut($status, $mode = 0)
467 {
468 // phpcs:enable
469 //global $langs;
470
471 return '';
472 }
473
474
482 public function initAsSpecimen()
483 {
484 $this->id = 0;
485
486 $this->fk_expensereport = '';
487 $this->datec = '';
488 $this->tms = '';
489 $this->datep = '';
490 $this->amount = '';
491 $this->fk_typepayment = '';
492 $this->num_payment = '';
493 $this->note = '';
494 $this->fk_bank = '';
495 $this->fk_user_creat = '';
496 $this->fk_user_modif = '';
497 }
498
499
512 public function addPaymentToBank($user, $mode, $label, $accountid, $emetteur_nom, $emetteur_banque)
513 {
514 global $langs, $conf;
515
516 $error = 0;
517
518 if (isModEnabled("banque")) {
519 include_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
520
521 $acc = new Account($this->db);
522 $acc->fetch($accountid);
523
524 //Fix me field
525 $total = $this->amount;
526
527 if ($mode == 'payment_expensereport') {
528 $amount = $total;
529 }
530
531 // Insert payment into llx_bank
532 $bank_line_id = $acc->addline(
533 $this->datep,
534 $this->fk_typepayment, // Payment mode id or code ("CHQ or VIR for example")
535 $label,
536 -$amount,
537 $this->num_payment,
538 '',
539 $user,
540 $emetteur_nom,
541 $emetteur_banque
542 );
543
544 // Update fk_bank in llx_paiement.
545 // So we wil know the payment that have generated the bank transaction
546 if ($bank_line_id > 0) {
547 $result = $this->update_fk_bank($bank_line_id);
548 if ($result <= 0) {
549 $error++;
550 dol_print_error($this->db);
551 }
552
553 // Add link 'payment', 'payment_supplier', 'payment_expensereport' in bank_url between payment and bank transaction
554 $url = '';
555 if ($mode == 'payment_expensereport') {
556 $url = DOL_URL_ROOT.'/expensereport/payment/card.php?rowid=';
557 }
558 if ($url) {
559 $result = $acc->add_url_line($bank_line_id, $this->id, $url, '(paiement)', $mode);
560 if ($result <= 0) {
561 $error++;
562 dol_print_error($this->db);
563 }
564 }
565
566 // Add link 'user' in bank_url between user and bank transaction
567 if (!$error) {
568 foreach ($this->amounts as $key => $value) { // We should have always same user but we loop in case of.
569 if ($mode == 'payment_expensereport') {
570 $fuser = new User($this->db);
571 $fuser->fetch($key);
572
573 $result = $acc->add_url_line(
574 $bank_line_id,
575 $fuser->id,
576 DOL_URL_ROOT.'/user/card.php?id=',
577 $fuser->getFullName($langs),
578 'user'
579 );
580 if ($result <= 0) {
581 $this->error = $this->db->lasterror();
582 dol_syslog(get_class($this).'::addPaymentToBank '.$this->error);
583 $error++;
584 }
585 }
586 }
587 }
588 } else {
589 $this->error = $acc->error;
590 $this->errors = $acc->errors;
591 $error++;
592 }
593 }
594
595 if (!$error) {
596 return 1;
597 } else {
598 return -1;
599 }
600 }
601
602
603 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
610 public function update_fk_bank($id_bank)
611 {
612 // phpcs:enable
613 $sql = "UPDATE ".MAIN_DB_PREFIX."payment_expensereport SET fk_bank = ".((int) $id_bank)." WHERE rowid = ".((int) $this->id);
614
615 dol_syslog(get_class($this)."::update_fk_bank", LOG_DEBUG);
616 $result = $this->db->query($sql);
617 if ($result) {
618 return 1;
619 } else {
620 $this->error = $this->db->error();
621 return 0;
622 }
623 }
624
632 public function getNomUrl($withpicto = 0, $maxlen = 0)
633 {
634 global $langs, $hookmanager;
635
636 $result = '';
637
638 if (empty($this->ref)) {
639 $this->ref = $this->label;
640 }
641 $label = img_picto('', $this->picto).' <u class="paddingrightonly">'.$langs->trans("Payment").'</u>';
642 if (isset($this->status)) {
643 $label .= ' '.$this->getLibStatut(5);
644 }
645 if (!empty($this->ref)) {
646 $label .= '<br><b>'.$langs->trans('Ref').':</b> '.$this->ref;
647 }
648 if (!empty($this->datep)) {
649 $label .= '<br><b>'.$langs->trans('Date').':</b> '.dol_print_date($this->datep, 'dayhour');
650 }
651
652 if (!empty($this->id)) {
653 $link = '<a href="'.DOL_URL_ROOT.'/expensereport/payment/card.php?id='.$this->id.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
654 $linkend = '</a>';
655
656 if ($withpicto) {
657 $result .= ($link.img_object($label, 'payment', 'class="classfortooltip"').$linkend.' ');
658 }
659 if ($withpicto && $withpicto != 2) {
660 $result .= ' ';
661 }
662 if ($withpicto != 2) {
663 $result .= $link.($maxlen ?dol_trunc($this->ref, $maxlen) : $this->ref).$linkend;
664 }
665 }
666 global $action;
667 $hookmanager->initHooks(array($this->element . 'dao'));
668 $parameters = array('id'=>$this->id, 'getnomurl' => &$result);
669 $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
670 if ($reshook > 0) {
671 $result = $hookmanager->resPrint;
672 } else {
673 $result .= $hookmanager->resPrint;
674 }
675 return $result;
676 }
677
684 public function info($id)
685 {
686 $sql = 'SELECT e.rowid, e.datec, e.fk_user_creat, e.fk_user_modif, e.tms';
687 $sql .= ' FROM '.MAIN_DB_PREFIX.'payment_expensereport as e';
688 $sql .= ' WHERE e.rowid = '.((int) $id);
689
690 dol_syslog(get_class($this).'::info', LOG_DEBUG);
691 $result = $this->db->query($sql);
692
693 if ($result) {
694 if ($this->db->num_rows($result)) {
695 $obj = $this->db->fetch_object($result);
696 $this->id = $obj->rowid;
697 if ($obj->fk_user_creat) {
698 $cuser = new User($this->db);
699 $cuser->fetch($obj->fk_user_creat);
700 $this->user_creation = $cuser;
701 }
702 if ($obj->fk_user_modif) {
703 $muser = new User($this->db);
704 $muser->fetch($obj->fk_user_modif);
705 $this->user_modification = $muser;
706 }
707 $this->date_creation = $this->db->jdate($obj->datec);
708 $this->date_modification = $this->db->jdate($obj->tms);
709 }
710 $this->db->free($result);
711 } else {
712 dol_print_error($this->db);
713 }
714 }
715
723 public function getKanbanView($option = '', $arraydata = null)
724 {
725 global $langs;
726
727 $selected = (empty($arraydata['selected']) ? 0 : $arraydata['selected']);
728
729 $return = '<div class="box-flex-item box-flex-grow-zero">';
730 $return .= '<div class="info-box info-box-sm">';
731 $return .= '<span class="info-box-icon bg-infobox-action">';
732 $return .= img_picto('', $this->picto);
733 $return .= '</span>';
734 $return .= '<div class="info-box-content">';
735 $return .= '<span class="info-box-ref inline-block tdoverflowmax150 valignmiddle">'.(method_exists($this, 'getNomUrl') ? $this->getNomUrl(1) : $this->ref).'</span>';
736 $return .= '<input id="cb'.$this->id.'" class="flat checkforselect fright" type="checkbox" name="toselect[]" value="'.$this->id.'"'.($selected ? ' checked="checked"' : '').'>';
737 if (property_exists($this, 'datep')) {
738 $return .= '<br><span class="opacitymedium">'.$langs->trans("Date").'</span> : <span class="info-box-label">'.dol_print_date($this->db->jdate($this->datep), 'dayhour').'</span>';
739 }
740 if (property_exists($this, 'fk_typepayment')) {
741 $return .= '<br><span class="opacitymedium">'.$langs->trans("Type").'</span> : <span class="info-box-label">'.$this->fk_typepayment.'</span>';
742 }
743 if (property_exists($this, 'fk_bank') && !is_null($this->fk_bank)) {
744 $return .= '<br><span class="opacitymedium">'.$langs->trans("Account").'</span> : <span class="info-box-label">'.$this->fk_bank.'</span>';
745 }
746 if (property_exists($this, 'amount') ) {
747 $return .= '<br><span class="opacitymedium">'.$langs->trans("Amount").'</span> : <span class="info-box-label amount">'.price($this->amount).'</span>';
748 }
749 if (method_exists($this, 'getLibStatut')) {
750 $return .= '<br><div class="info-box-status margintoponly">'.$this->getLibStatut(3).'</div>';
751 }
752 $return .= '</div>';
753 $return .= '</div>';
754 $return .= '</div>';
755 return $return;
756 }
757}
$object ref
Definition info.php:78
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...