dolibarr 19.0.3
salary.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2011-2022 Alexandre Spangaro <aspangaro@open-dsi.fr>
3 * Copyright (C) 2014 Juanjo Menent <jmenent@2byte.es>
4 * Copyright (C) 2021 Gauthier VERDOL <gauthier.verdol@atm-consulting.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// Put here all includes required by your class file
27require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
28
29
33class Salary extends CommonObject
34{
38 public $element = 'salary';
39
43 public $table_element = 'salary';
44
48 public $picto = 'salary';
49
50 public $tms;
51
52 // /**
53 // * @var array List of child tables. To test if we can delete object.
54 // */
55 protected $childtables = array('payment_salary' => array('name'=>'SalaryPayment', 'fk_element'=>'fk_salary'));
56
57 // /**
58 // * @var array List of child tables. To know object to delete on cascade.
59 // * If name matches '@ClassNAme:FilePathClass;ParentFkFieldName' it will
60 // * call method deleteByParentField(parentId, ParentFkFieldName) to fetch and delete child object
61 // */
62 //protected $childtablesoncascade = array('mymodule_myobjectdet');
63
64
68 public $fk_user;
69
70 public $datep;
71 public $datev;
72
73 public $salary;
74 public $amount;
78 public $fk_project;
79
80 public $type_payment;
81
85 public $label;
86
87 public $datesp;
88 public $dateep;
89
93 public $fk_bank;
94
99 public $fk_account;
100
104 public $accountid;
105
109 public $fk_user_author;
110
114 public $fk_user_modif;
115
119 public $user;
120
124 public $paye;
125
126 const STATUS_UNPAID = 0;
127 const STATUS_PAID = 1;
128
129
135 public function __construct($db)
136 {
137 $this->db = $db;
138 $this->element = 'salary';
139 $this->table_element = 'salary';
140 }
141
149 public function update($user = null, $notrigger = 0)
150 {
151 $error = 0;
152
153 // Clean parameters
154 $this->amount = trim($this->amount);
155 $this->label = trim($this->label);
156 $this->note = trim($this->note);
157
158 // Check parameters
159 if (empty($this->fk_user) || $this->fk_user < 0) {
160 $this->error = 'ErrorBadParameter';
161 return -1;
162 }
163
164 $this->db->begin();
165
166 // Update request
167 $sql = "UPDATE ".MAIN_DB_PREFIX."salary SET";
168 $sql .= " tms='".$this->db->idate(dol_now())."',";
169 $sql .= " fk_user=".((int) $this->fk_user).",";
170 /*$sql .= " datep='".$this->db->idate($this->datep)."',";
171 $sql .= " datev='".$this->db->idate($this->datev)."',";*/
172 $sql .= " amount=".price2num($this->amount).",";
173 $sql .= " fk_projet=".((int) $this->fk_project).",";
174 $sql .= " fk_typepayment=".((int) $this->type_payment).",";
175 $sql .= " label='".$this->db->escape($this->label)."',";
176 $sql .= " datesp='".$this->db->idate($this->datesp)."',";
177 $sql .= " dateep='".$this->db->idate($this->dateep)."',";
178 $sql .= " note='".$this->db->escape($this->note)."',";
179 $sql .= " fk_bank=".($this->fk_bank > 0 ? (int) $this->fk_bank : "null").",";
180 $sql .= " fk_user_author=".((int) $this->fk_user_author).",";
181 $sql .= " fk_user_modif=".($this->fk_user_modif > 0 ? (int) $this->fk_user_modif : (int) $user->id);
182 $sql .= " WHERE rowid=".((int) $this->id);
183
184 dol_syslog(get_class($this)."::update", LOG_DEBUG);
185 $resql = $this->db->query($sql);
186 if (!$resql) {
187 $this->error = "Error ".$this->db->lasterror();
188 return -1;
189 }
190
191 // Update extrafield
192 if (!$error) {
193 if (!$error) {
194 $result = $this->insertExtraFields();
195 if ($result < 0) {
196 $error++;
197 }
198 }
199 }
200
201 if (!$notrigger) {
202 // Call trigger
203 $result = $this->call_trigger('SALARY_MODIFY', $user);
204 if ($result < 0) {
205 $error++;
206 }
207 // End call triggers
208 }
209
210 if (!$error) {
211 $this->db->commit();
212 return 1;
213 } else {
214 $this->db->rollback();
215 return -1;
216 }
217 }
218
219
227 public function fetch($id, $user = null)
228 {
229 $sql = "SELECT";
230 $sql .= " s.rowid,";
231 $sql .= " s.tms,";
232 $sql .= " s.fk_user,";
233 $sql .= " s.datep,";
234 $sql .= " s.datev,";
235 $sql .= " s.amount,";
236 $sql .= " s.fk_projet as fk_project,";
237 $sql .= " s.fk_typepayment,";
238 $sql .= " s.label,";
239 $sql .= " s.datesp,";
240 $sql .= " s.dateep,";
241 $sql .= " s.note,";
242 $sql .= " s.paye,";
243 $sql .= " s.fk_bank,";
244 $sql .= " s.fk_user_author,";
245 $sql .= " s.fk_user_modif,";
246 $sql .= " s.fk_account";
247 $sql .= " FROM ".MAIN_DB_PREFIX."salary as s";
248 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."bank as b ON s.fk_bank = b.rowid";
249 $sql .= " WHERE s.rowid = ".((int) $id);
250
251 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
252 $resql = $this->db->query($sql);
253 if ($resql) {
254 if ($this->db->num_rows($resql)) {
255 $obj = $this->db->fetch_object($resql);
256
257 $this->id = $obj->rowid;
258 $this->ref = $obj->rowid;
259 $this->tms = $this->db->jdate($obj->tms);
260 $this->fk_user = $obj->fk_user;
261 $this->datep = $this->db->jdate($obj->datep);
262 $this->datev = $this->db->jdate($obj->datev);
263 $this->amount = $obj->amount;
264 $this->fk_project = $obj->fk_project;
265 $this->type_payment = $obj->fk_typepayment;
266 $this->label = $obj->label;
267 $this->datesp = $this->db->jdate($obj->datesp);
268 $this->dateep = $this->db->jdate($obj->dateep);
269 $this->note = $obj->note;
270 $this->paye = $obj->paye;
271 $this->status = $obj->paye;
272 $this->fk_bank = $obj->fk_bank;
273 $this->fk_user_author = $obj->fk_user_author;
274 $this->fk_user_modif = $obj->fk_user_modif;
275 $this->fk_account = $obj->fk_account;
276 $this->accountid = $obj->fk_account;
277
278 // Retrieve all extrafield
279 // fetch optionals attributes and labels
280 $this->fetch_optionals();
281 }
282 $this->db->free($resql);
283
284 return 1;
285 } else {
286 $this->error = "Error ".$this->db->lasterror();
287 return -1;
288 }
289 }
290
291
299 public function delete($user, $notrigger = 0)
300 {
301 return $this->deleteCommon($user, $notrigger);
302 }
303
304
312 public function initAsSpecimen()
313 {
314 $this->id = 0;
315
316 $this->tms = '';
317 $this->fk_user = '';
318 $this->datep = '';
319 $this->datev = '';
320 $this->amount = '';
321 $this->label = '';
322 $this->datesp = '';
323 $this->dateep = '';
324 $this->note = '';
325 $this->fk_bank = '';
326 $this->fk_user_author = '';
327 $this->fk_user_modif = '';
328 }
329
336 public function create($user)
337 {
338 global $conf, $langs;
339
340 $error = 0;
341 $now = dol_now();
342
343 // Clean parameters
344 $this->amount = price2num(trim($this->amount));
345 $this->label = trim($this->label);
346 $this->note = trim($this->note);
347 $this->fk_bank = trim($this->fk_bank);
348 $this->fk_user_author = trim($this->fk_user_author);
349 $this->fk_user_modif = trim($this->fk_user_modif);
350 $this->accountid = trim($this->accountid);
351 $this->paye = trim($this->paye);
352
353 // Check parameters
354 if (!$this->label) {
355 $this->error = $langs->trans("ErrorFieldRequired", $langs->transnoentities("Label"));
356 return -3;
357 }
358 if ($this->fk_user < 0 || $this->fk_user == '') {
359 $this->error = $langs->trans("ErrorFieldRequired", $langs->transnoentities("Employee"));
360 return -4;
361 }
362 if ($this->amount == '') {
363 $this->error = $langs->trans("ErrorFieldRequired", $langs->transnoentities("Amount"));
364 return -5;
365 }
366 /* if (isModEnabled("banque") && (empty($this->accountid) || $this->accountid <= 0))
367 {
368 $this->error = $langs->trans("ErrorFieldRequired", $langs->transnoentities("Account"));
369 return -6;
370 }
371 if (isModEnabled("banque") && (empty($this->type_payment) || $this->type_payment <= 0))
372 {
373 $this->error = $langs->trans("ErrorFieldRequired", $langs->transnoentities("PaymentMode"));
374 return -7;
375 }*/
376
377 $this->db->begin();
378
379 // Insert into llx_salary
380 $sql = "INSERT INTO ".MAIN_DB_PREFIX."salary (fk_user";
381 //$sql .= ", datep";
382 //$sql .= ", datev";
383 $sql .= ", amount";
384 $sql .= ", fk_projet";
385 $sql .= ", salary";
386 $sql .= ", fk_typepayment";
387 $sql .= ", fk_account";
388 if ($this->note) {
389 $sql .= ", note";
390 }
391 $sql .= ", label";
392 $sql .= ", datesp";
393 $sql .= ", dateep";
394 $sql .= ", fk_user_author";
395 $sql .= ", datec";
396 $sql .= ", fk_bank";
397 $sql .= ", entity";
398 $sql .= ") ";
399 $sql .= " VALUES (";
400 $sql .= "'".$this->db->escape($this->fk_user)."'";
401 //$sql .= ", '".$this->db->idate($this->datep)."'";
402 //$sql .= ", '".$this->db->idate($this->datev)."'";
403 $sql .= ", ".((float) $this->amount);
404 $sql .= ", ".($this->fk_project > 0 ? ((int) $this->fk_project) : 0);
405 $sql .= ", ".($this->salary > 0 ? ((float) $this->salary) : "null");
406 $sql .= ", ".($this->type_payment > 0 ? ((int) $this->type_payment) : 0);
407 $sql .= ", ".($this->accountid > 0 ? ((int) $this->accountid) : "null");
408 if ($this->note) {
409 $sql .= ", '".$this->db->escape($this->note)."'";
410 }
411 $sql .= ", '".$this->db->escape($this->label)."'";
412 $sql .= ", '".$this->db->idate($this->datesp)."'";
413 $sql .= ", '".$this->db->idate($this->dateep)."'";
414 $sql .= ", '".$this->db->escape($user->id)."'";
415 $sql .= ", '".$this->db->idate($now)."'";
416 $sql .= ", NULL";
417 $sql .= ", ".((int) $conf->entity);
418 $sql .= ")";
419
420 dol_syslog(get_class($this)."::create", LOG_DEBUG);
421 $result = $this->db->query($sql);
422 if ($result) {
423 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."salary");
424
425 if ($this->id > 0) {
426 // Update extrafield
427 if (!$error) {
428 if (!$error) {
429 $result = $this->insertExtraFields();
430 if ($result < 0) {
431 $error++;
432 }
433 }
434 }
435
436 // Call trigger
437 $result = $this->call_trigger('SALARY_CREATE', $user);
438 if ($result < 0) {
439 $error++;
440 }
441 // End call triggers
442 } else {
443 $error++;
444 }
445
446 if (!$error) {
447 $this->db->commit();
448 return $this->id;
449 } else {
450 $this->db->rollback();
451 return -2;
452 }
453 } else {
454 $this->error = $this->db->error();
455 $this->db->rollback();
456 return -1;
457 }
458 }
459
460 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
467 public function update_fk_bank($id_bank)
468 {
469 // phpcs:enable
470 $sql = 'UPDATE '.MAIN_DB_PREFIX.'salary SET fk_bank = '.((int) $id_bank);
471 $sql .= " WHERE rowid = ".((int) $this->id);
472 $result = $this->db->query($sql);
473 if ($result) {
474 return 1;
475 } else {
476 dol_print_error($this->db);
477 return -1;
478 }
479 }
480
488 public function getTooltipContentArray($params)
489 {
490 global $langs;
491
492 $langs->loadLangs(['salaries']);
493
494 // Complete datas
495 if (!empty($params['fromajaxtooltip']) && !isset($this->alreadypaid)) {
496 // Load the alreadypaid field
497 $this->alreadypaid = $this->getSommePaiement(0);
498 }
499
500 $datas = [];
501
502 $datas['picto'] = '<u>'.$langs->trans("Salary").'</u>';
503 if (isset($this->status) && isset($this->alreadypaid)) {
504 $datas['picto'] .= ' '.$this->getLibStatut(5, $this->alreadypaid);
505 }
506 $datas['ref'] = '<br><b>'.$langs->trans('Ref').':</b> '.$this->ref;
507
508 return $datas;
509 }
510
521 public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $morecss = '', $save_lastsearch_value = -1)
522 {
523 global $conf, $langs, $hookmanager;
524
525 if (!empty($conf->dol_no_mouse_hover)) {
526 $notooltip = 1;
527 } // Force disable tooltips
528
529 $result = '';
530 $params = [
531 'id' => $this->id,
532 'objecttype' => $this->element,
533 'option' => $option,
534 ];
535 $classfortooltip = 'classfortooltip';
536 $dataparams = '';
537 if (getDolGlobalInt('MAIN_ENABLE_AJAX_TOOLTIP')) {
538 $classfortooltip = 'classforajaxtooltip';
539 $dataparams = ' data-params="'.dol_escape_htmltag(json_encode($params)).'"';
540 $label = '';
541 } else {
542 $label = implode($this->getTooltipContentArray($params));
543 }
544
545 $url = DOL_URL_ROOT.'/salaries/card.php?id='.$this->id;
546
547 if ($option != 'nolink') {
548 // Add param to save lastsearch_values or not
549 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
550 if ($save_lastsearch_value == -1 && isset($_SERVER["PHP_SELF"]) && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
551 $add_save_lastsearch_values = 1;
552 }
553 if ($add_save_lastsearch_values) {
554 $url .= '&save_lastsearch_values=1';
555 }
556 }
557
558 $linkclose = '';
559 if (empty($notooltip)) {
560 if (getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER')) {
561 $label = $langs->trans("ShowMyObject");
562 $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
563 }
564 $linkclose .= ($label ? ' title="'.dol_escape_htmltag($label, 1).'"' : ' title="tocomplete"');
565 $linkclose .= $dataparams.' class="'.$classfortooltip.($morecss ? ' '.$morecss : '').'"';
566 } else {
567 $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
568 }
569
570 $linkstart = '<a href="'.$url.'"';
571 $linkstart .= $linkclose.'>';
572 $linkend = '</a>';
573
574 $result .= $linkstart;
575 if ($withpicto) {
576 $result .= img_object(($notooltip ? '' : $label), ($this->picto ? $this->picto : 'generic'), ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : 'class="'.(($withpicto != 2) ? 'paddingright ' : '').'"'), 0, 0, $notooltip ? 0 : 1);
577 }
578 if ($withpicto != 2) {
579 $result .= $this->ref;
580 }
581 $result .= $linkend;
582 //if ($withpicto != 2) $result.=(($addlabel && $this->label) ? $sep . dol_trunc($this->label, ($addlabel > 1 ? $addlabel : 0)) : '');
583
584 global $action, $hookmanager;
585 $hookmanager->initHooks(array('salarypayment'));
586 $parameters = array('id'=>$this->id, 'getnomurl' => &$result);
587 $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
588 if ($reshook > 0) {
589 $result = $hookmanager->resPrint;
590 } else {
591 $result .= $hookmanager->resPrint;
592 }
593
594 return $result;
595 }
596
603 public function getSommePaiement($multicurrency = 0)
604 {
605 $table = 'payment_salary';
606 $field = 'fk_salary';
607
608 $sql = "SELECT sum(amount) as amount";
609 //sum(multicurrency_amount) as multicurrency_amount // Not yet supported
610 $sql .= " FROM ".MAIN_DB_PREFIX.$table;
611 $sql .= " WHERE ".$field." = ".((int) $this->id);
612
613 dol_syslog(get_class($this)."::getSommePaiement", LOG_DEBUG);
614
615 $resql = $this->db->query($sql);
616
617 if ($resql) {
618 $obj = $this->db->fetch_object($resql);
619
620 $this->db->free($resql);
621
622 if ($obj) {
623 if ($multicurrency < 0) {
624 //$this->sumpayed = $obj->amount;
625 //$this->sumpayed_multicurrency = $obj->multicurrency_amount;
626 //return array('alreadypaid'=>(float) $obj->amount, 'alreadypaid_multicurrency'=>(float) $obj->multicurrency_amount);
627 return array(); // Not yet supported
628 } elseif ($multicurrency) {
629 //$this->sumpayed_multicurrency = $obj->multicurrency_amount;
630 //return (float) $obj->multicurrency_amount;
631 return -1; // Not yet supported
632 } else {
633 //$this->sumpayed = $obj->amount;
634 return (float) $obj->amount;
635 }
636 } else {
637 return 0;
638 }
639 } else {
640 $this->error = $this->db->lasterror();
641 return -1;
642 }
643 }
644
651 public function info($id)
652 {
653 $sql = 'SELECT ps.rowid, ps.datec, ps.tms as datem, ps.fk_user_author, ps.fk_user_modif';
654 $sql .= ' FROM '.MAIN_DB_PREFIX.'salary as ps';
655 $sql .= ' WHERE ps.rowid = '.((int) $id);
656
657 dol_syslog(get_class($this).'::info', LOG_DEBUG);
658 $result = $this->db->query($sql);
659
660 if ($result) {
661 if ($this->db->num_rows($result)) {
662 $obj = $this->db->fetch_object($result);
663
664 $this->id = $obj->rowid;
665
666 $this->user_creation_id = $obj->fk_user_author;
667 $this->user_modification_id = $obj->fk_user_modif;
668 $this->date_creation = $this->db->jdate($obj->datec);
669 $this->date_modification = empty($obj->datem) ? '' : $this->db->jdate($obj->datem);
670 }
671 $this->db->free($result);
672 } else {
673 dol_print_error($this->db);
674 }
675 }
676
677 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
686 public function set_paid($user)
687 {
688 // phpcs:enable
689 dol_syslog(get_class($this)."::set_paid is deprecated, use setPaid instead", LOG_NOTICE);
690 return $this->setPaid($user);
691 }
692
699 public function setPaid($user)
700 {
701 $sql = "UPDATE ".MAIN_DB_PREFIX."salary SET";
702 $sql .= " paye = 1";
703 $sql .= " WHERE rowid = ".((int) $this->id);
704
705 $return = $this->db->query($sql);
706
707 if ($return) {
708 $this->paye = 1;
709 return 1;
710 } else {
711 return -1;
712 }
713 }
714
715 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
722 public function set_unpaid($user)
723 {
724 // phpcs:enable
725 $sql = "UPDATE ".MAIN_DB_PREFIX."salary SET";
726 $sql .= " paye = 0";
727 $sql .= " WHERE rowid = ".((int) $this->id);
728
729 $return = $this->db->query($sql);
730
731 if ($return) {
732 $this->paye = 0;
733 return 1;
734 } else {
735 return -1;
736 }
737 }
738
739
747 public function getLibStatut($mode = 0, $alreadypaid = -1)
748 {
749 return $this->LibStatut($this->paye, $mode, $alreadypaid);
750 }
751
752 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
761 public function LibStatut($status, $mode = 0, $alreadypaid = -1)
762 {
763 // phpcs:enable
764 global $langs;
765
766 // Load translation files required by the page
767 $langs->loadLangs(array("customers", "bills"));
768
769 // We reinit status array to force to redefine them because label may change according to properties values.
770 $this->labelStatus = array();
771 $this->labelStatusShort = array();
772
773 if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
774 global $langs;
775 //$langs->load("mymodule");
776 $this->labelStatus[self::STATUS_UNPAID] = $langs->transnoentitiesnoconv('BillStatusNotPaid');
777 $this->labelStatus[self::STATUS_PAID] = $langs->transnoentitiesnoconv('BillStatusPaid');
778 if ($status == self::STATUS_UNPAID && $alreadypaid != 0) {
779 $this->labelStatus[self::STATUS_UNPAID] = $langs->transnoentitiesnoconv("BillStatusStarted");
780 }
781 $this->labelStatusShort[self::STATUS_UNPAID] = $langs->transnoentitiesnoconv('BillStatusNotPaid');
782 $this->labelStatusShort[self::STATUS_PAID] = $langs->transnoentitiesnoconv('BillStatusPaid');
783 if ($status == self::STATUS_UNPAID && $alreadypaid != 0) {
784 $this->labelStatusShort[self::STATUS_UNPAID] = $langs->transnoentitiesnoconv("BillStatusStarted");
785 }
786 }
787
788 $statusType = 'status1';
789 if ($status == 0 && $alreadypaid != 0) {
790 $statusType = 'status3';
791 }
792 if ($status == 1) {
793 $statusType = 'status6';
794 }
795
796 return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusType, $mode);
797 }
798
806 public function getKanbanView($option = '', $arraydata = null)
807 {
808 global $langs;
809
810 $selected = (empty($arraydata['selected']) ? 0 : $arraydata['selected']);
811
812 $return = '<div class="box-flex-item box-flex-grow-zero">';
813 $return .= '<div class="info-box info-box-sm">';
814 $return .= '<span class="info-box-icon bg-infobox-action">';
815 $return .= img_picto('', $this->picto);
816 $return .= '</span>';
817 $return .= '<div class="info-box-content">';
818 $return .= '<span class="info-box-ref inline-block tdoverflowmax150 valignmiddle">'.(method_exists($this, 'getNomUrl') ? $this->getNomUrl(1) : $this->ref).'</span>';
819 if ($selected >= 0) {
820 $return .= '<input id="cb'.$this->id.'" class="flat checkforselect fright" type="checkbox" name="toselect[]" value="'.$this->id.'"'.($selected ? ' checked="checked"' : '').'>';
821 }
822 if (!empty($arraydata['user']) && is_object($arraydata['user'])) {
823 $return .= '<br><span class="info-box-label">'.$arraydata['user']->getNomUrl(1, '', 0, 0, 16, 0, '', 'maxwidth100').'</span>';
824 }
825 if (property_exists($this, 'amount')) {
826 $return .= '<br><span class="info-box-label amount">'.price($this->amount).'</span>';
827 if (property_exists($this, 'type_payment') && !empty($this->type_payment)) {
828 $return .= ' <span class="info-box-label opacitymedium small">';
829 if ($langs->trans("PaymentTypeShort".$this->type_payment) != "PaymentTypeShort".$this->type_payment) {
830 $return .= $langs->trans("PaymentTypeShort".$this->type_payment);
831 } elseif ($langs->trans("PaymentType".$this->type_payment) != "PaymentType".$this->type_payment) {
832 $return .= $langs->trans("PaymentType".$this->type_payment);
833 }
834 $return .= '</span>';
835 }
836 }
837 if (method_exists($this, 'LibStatut')) {
838 $return .= '<br><div class="info-box-status margintoponly">'.$this->getLibStatut(3, $this->alreadypaid).'</div>';
839 }
840 $return .= '</div>';
841 $return .= '</div>';
842 $return .= '</div>';
843 return $return;
844 }
845
846 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
858 public function demande_prelevement($fuser, $amount = 0, $type = 'direct-debit', $sourcetype = 'salaire', $checkduplicateamongall = 0)
859 {
860 // phpcs:enable
861 global $conf, $mysoc;
862
863 $error = 0;
864
865 dol_syslog(get_class($this)."::demande_prelevement", LOG_DEBUG);
866 if ($this->paye == 0) {
867 require_once DOL_DOCUMENT_ROOT.'/societe/class/companybankaccount.class.php';
868 $bac = new CompanyBankAccount($this->db);
869 $bac->fetch(0, $mysoc->id);
870
871 $sql = "SELECT count(rowid) as nb";
872 $sql .= " FROM ".$this->db->prefix()."prelevement_demande";
873 if ($type == 'salaire') {
874 $sql .= " WHERE fk_salary = ".((int) $this->id);
875 } else {
876 $sql .= " WHERE fk_facture = ".((int) $this->id);
877 }
878 $sql .= " AND type = 'ban'"; // To exclude record done for some online payments
879 if (empty($checkduplicateamongall)) {
880 $sql .= " AND traite = 0";
881 }
882
883 dol_syslog(get_class($this)."::demande_prelevement", LOG_DEBUG);
884
885 $resql = $this->db->query($sql);
886 if ($resql) {
887 $obj = $this->db->fetch_object($resql);
888 if ($obj && $obj->nb == 0) { // If no request found yet
889 $now = dol_now();
890
891 $totalpaid = $this->getSommePaiement();
892 // $totalcreditnotes = $this->getSumCreditNotesUsed();
893 // $totaldeposits = $this->getSumDepositsUsed();
894 //print "totalpaid=".$totalpaid." totalcreditnotes=".$totalcreditnotes." totaldeposts=".$totaldeposits;
895
896 // We can also use bcadd to avoid pb with floating points
897 // For example print 239.2 - 229.3 - 9.9; does not return 0.
898 //$resteapayer=bcadd($this->total_ttc,$totalpaid,$conf->global->MAIN_MAX_DECIMALS_TOT);
899 //$resteapayer=bcadd($resteapayer,$totalavoir,$conf->global->MAIN_MAX_DECIMALS_TOT);
900 // if (empty($amount)) {
901 // $amount = price2num($this->total_ttc - $totalpaid - $totalcreditnotes - $totaldeposits, 'MT');
902 // }
903
904 if (is_numeric($amount) && $amount != 0) {
905 $sql = 'INSERT INTO '.$this->db->prefix().'prelevement_demande(';
906 if ($type == 'salaire') {
907 $sql .= 'fk_salary, ';
908 } else {
909 $sql .= 'fk_facture, ';
910 }
911 $sql .= ' amount, date_demande, fk_user_demande, code_banque, code_guichet, number, cle_rib, sourcetype, type, entity)';
912 $sql .= " VALUES (".((int) $this->id);
913 $sql .= ", ".((float) price2num($amount));
914 $sql .= ", '".$this->db->idate($now)."'";
915 $sql .= ", ".((int) $fuser->id);
916 $sql .= ", '".$this->db->escape($bac->code_banque)."'";
917 $sql .= ", '".$this->db->escape($bac->code_guichet)."'";
918 $sql .= ", '".$this->db->escape($bac->number)."'";
919 $sql .= ", '".$this->db->escape($bac->cle_rib)."'";
920 $sql .= ", '".$this->db->escape($sourcetype)."'";
921 $sql .= ", 'ban'";
922 $sql .= ", ".((int) $conf->entity);
923 $sql .= ")";
924
925 dol_syslog(get_class($this)."::demande_prelevement", LOG_DEBUG);
926 $resql = $this->db->query($sql);
927 if (!$resql) {
928 $this->error = $this->db->lasterror();
929 dol_syslog(get_class($this).'::demandeprelevement Erreur');
930 $error++;
931 }
932 } else {
933 $this->error = 'WithdrawRequestErrorNilAmount';
934 dol_syslog(get_class($this).'::demandeprelevement WithdrawRequestErrorNilAmount');
935 $error++;
936 }
937
938 if (!$error) {
939 // Force payment mode of invoice to withdraw
940 $payment_mode_id = dol_getIdFromCode($this->db, ($type == 'bank-transfer' ? 'VIR' : 'PRE'), 'c_paiement', 'code', 'id', 1);
941 if ($payment_mode_id > 0) {
942 $result = $this->setPaymentMethods($payment_mode_id);
943 }
944 }
945
946 if ($error) {
947 return -1;
948 }
949 return 1;
950 } else {
951 $this->error = "A request already exists";
952 dol_syslog(get_class($this).'::demandeprelevement Can t create a request to generate a direct debit, a request already exists.');
953 return 0;
954 }
955 } else {
956 $this->error = $this->db->error();
957 dol_syslog(get_class($this).'::demandeprelevement Error -2');
958 return -2;
959 }
960 } else {
961 $this->error = "Status of invoice does not allow this";
962 dol_syslog(get_class($this)."::demandeprelevement ".$this->error." $this->status, $this->paye, $this->mode_reglement_id");
963 return -3;
964 }
965 }
966
967 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
975 public function demande_prelevement_delete($fuser, $did)
976 {
977 // phpcs:enable
978 $sql = 'DELETE FROM '.$this->db->prefix().'prelevement_demande';
979 $sql .= ' WHERE rowid = '.((int) $did);
980 $sql .= ' AND traite = 0';
981 if ($this->db->query($sql)) {
982 return 0;
983 } else {
984 $this->error = $this->db->lasterror();
985 dol_syslog(get_class($this).'::demande_prelevement_delete Error '.$this->error);
986 return -1;
987 }
988 }
989}
print $langs trans("AuditedSecurityEvents").'</strong >< span class="opacitymedium"></span >< br > status
Definition security.php:604
$object ref
Definition info.php:79
Parent class of all other business classes (invoices, contracts, proposals, orders,...
fetch_optionals($rowid=null, $optionsArray=null)
Function to get extra fields of an object into $this->array_options This method is in most cases call...
deleteCommon(User $user, $notrigger=false, $forcechilddeletion=0)
Delete object in database.
insertExtraFields($trigger='', $userused=null)
Add/Update all extra fields values for the current object.
setPaymentMethods($id)
Change the payments methods.
call_trigger($triggerName, $user)
Call trigger based on this instance.
Class to manage bank accounts description of third parties.
Class to manage salary payments.
fetch($id, $user=null)
Load object in memory from database.
initAsSpecimen()
Initialise an instance with random values.
update($user=null, $notrigger=0)
Update database.
getTooltipContentArray($params)
getTooltipContentArray
getKanbanView($option='', $arraydata=null)
Return clicable link of object (with eventually picto)
update_fk_bank($id_bank)
Update link between payment salary and line generate into llx_bank.
LibStatut($status, $mode=0, $alreadypaid=-1)
Return label of a given status.
set_paid($user)
Tag social contribution as payed completely.
demande_prelevement_delete($fuser, $did)
Remove a direct debit request or a credit transfer request.
getNomUrl($withpicto=0, $option='', $notooltip=0, $morecss='', $save_lastsearch_value=-1)
Send name clicable (with possibly the picto)
set_unpaid($user)
Remove tag payed on social contribution.
create($user)
Create in database.
getSommePaiement($multicurrency=0)
Return amount of payments already done.
getLibStatut($mode=0, $alreadypaid=-1)
Return label of current status.
$paye
1 if salary paid COMPLETELY, 0 otherwise (do not use it anymore, use statut and close_code)
info($id)
Information on record.
__construct($db)
Constructor.
setPaid($user)
Tag social contribution as payed completely.
demande_prelevement($fuser, $amount=0, $type='direct-debit', $sourcetype='salaire', $checkduplicateamongall=0)
Create a withdrawal request for a direct debit order or a credit transfer order.
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...
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
dol_now($mode='auto')
Return date for now.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
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_getIdFromCode($db, $key, $tablename, $fieldkey='code', $fieldid='id', $entityfilter=0, $filters='')
Return an id or code from a code or id.
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.