dolibarr 21.0.0-alpha
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 * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
6 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
28// Put here all includes required by your class file
29require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
30
31
35class Salary extends CommonObject
36{
40 public $element = 'salary';
41
45 public $table_element = 'salary';
46
50 public $picto = 'salary';
51
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;
75
79 public $fk_project;
80
81 public $type_payment;
82
86 public $label;
87
88 public $datesp;
89 public $dateep;
90
94 public $fk_bank;
95
100 public $fk_account;
101
105 public $accountid;
106
110 public $fk_user_author;
111
115 public $fk_user_modif;
116
120 public $user;
121
126 public $paye;
127
128 const STATUS_UNPAID = 0;
129 const STATUS_PAID = 1;
130
131 public $resteapayer;
132
138 public function __construct($db)
139 {
140 $this->db = $db;
141 $this->element = 'salary';
142 $this->table_element = 'salary';
143 }
144
152 public function update($user = null, $notrigger = 0)
153 {
154 $error = 0;
155
156 // Clean parameters
157 $this->amount = trim($this->amount);
158 $this->label = trim($this->label);
159 $this->note = trim($this->note);
160
161 // Check parameters
162 if (empty($this->fk_user) || $this->fk_user < 0) {
163 $this->error = 'ErrorBadParameter';
164 return -1;
165 }
166
167 $this->db->begin();
168
169 // Update request
170 $sql = "UPDATE ".MAIN_DB_PREFIX."salary SET";
171 $sql .= " tms='".$this->db->idate(dol_now())."',";
172 $sql .= " fk_user=".((int) $this->fk_user).",";
173 /*$sql .= " datep='".$this->db->idate($this->datep)."',";
174 $sql .= " datev='".$this->db->idate($this->datev)."',";*/
175 $sql .= " amount=".price2num($this->amount).",";
176 $sql .= " fk_projet=".((int) $this->fk_project).",";
177 $sql .= " fk_typepayment=".((int) $this->type_payment).",";
178 $sql .= " label='".$this->db->escape($this->label)."',";
179 $sql .= " datesp='".$this->db->idate($this->datesp)."',";
180 $sql .= " dateep='".$this->db->idate($this->dateep)."',";
181 $sql .= " note='".$this->db->escape($this->note)."',";
182 $sql .= " fk_bank=".($this->fk_bank > 0 ? (int) $this->fk_bank : "null").",";
183 $sql .= " fk_user_author=".((int) $this->fk_user_author).",";
184 $sql .= " fk_user_modif=".($this->fk_user_modif > 0 ? (int) $this->fk_user_modif : (int) $user->id);
185 $sql .= " WHERE rowid=".((int) $this->id);
186
187 dol_syslog(get_class($this)."::update", LOG_DEBUG);
188 $resql = $this->db->query($sql);
189 if (!$resql) {
190 $this->error = "Error ".$this->db->lasterror();
191 return -1;
192 }
193
194 // Update extrafield
195 if (!$error) {
196 $result = $this->insertExtraFields();
197 if ($result < 0) {
198 $error++;
199 }
200 }
201
202 if (!$notrigger) {
203 // Call trigger
204 $result = $this->call_trigger('SALARY_MODIFY', $user);
205 if ($result < 0) {
206 $error++;
207 }
208 // End call triggers
209 }
210
211 if (!$error) {
212 $this->db->commit();
213 return 1;
214 } else {
215 $this->db->rollback();
216 return -1;
217 }
218 }
219
220
228 public function fetch($id, $user = null)
229 {
230 $sql = "SELECT";
231 $sql .= " s.rowid,";
232 $sql .= " s.tms,";
233 $sql .= " s.fk_user,";
234 $sql .= " s.datep,";
235 $sql .= " s.datev,";
236 $sql .= " s.amount,";
237 $sql .= " s.fk_projet as fk_project,";
238 $sql .= " s.fk_typepayment,";
239 $sql .= " s.label,";
240 $sql .= " s.datesp,";
241 $sql .= " s.dateep,";
242 $sql .= " s.note,";
243 $sql .= " s.paye,";
244 $sql .= " s.fk_bank,";
245 $sql .= " s.fk_user_author,";
246 $sql .= " s.fk_user_modif,";
247 $sql .= " s.fk_account";
248 $sql .= " FROM ".MAIN_DB_PREFIX."salary as s";
249 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."bank as b ON s.fk_bank = b.rowid";
250 $sql .= " WHERE s.rowid = ".((int) $id);
251
252 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
253 $resql = $this->db->query($sql);
254 if ($resql) {
255 if ($this->db->num_rows($resql)) {
256 $obj = $this->db->fetch_object($resql);
257
258 $this->id = $obj->rowid;
259 $this->ref = $obj->rowid;
260 $this->tms = $this->db->jdate($obj->tms);
261 $this->fk_user = $obj->fk_user;
262 $this->datep = $this->db->jdate($obj->datep);
263 $this->datev = $this->db->jdate($obj->datev);
264 $this->amount = $obj->amount;
265 $this->fk_project = $obj->fk_project;
266 $this->type_payment = $obj->fk_typepayment;
267 $this->label = $obj->label;
268 $this->datesp = $this->db->jdate($obj->datesp);
269 $this->dateep = $this->db->jdate($obj->dateep);
270 $this->note = $obj->note;
271 $this->paye = $obj->paye;
272 $this->status = $obj->paye;
273 $this->fk_bank = $obj->fk_bank;
274 $this->fk_user_author = $obj->fk_user_author;
275 $this->fk_user_modif = $obj->fk_user_modif;
276 $this->fk_account = $obj->fk_account;
277 $this->accountid = $obj->fk_account;
278
279 // Retrieve all extrafield
280 // fetch optionals attributes and labels
281 $this->fetch_optionals();
282 }
283 $this->db->free($resql);
284
285 return 1;
286 } else {
287 $this->error = "Error ".$this->db->lasterror();
288 return -1;
289 }
290 }
291
292
300 public function delete($user, $notrigger = 0)
301 {
302 return $this->deleteCommon($user, $notrigger);
303 }
304
305
313 public function initAsSpecimen()
314 {
315 $this->id = 0;
316
317 $this->tms = dol_now();
318 $this->fk_user = 0;
319 $this->datep = '';
320 $this->datev = '';
321 $this->amount = '';
322 $this->label = '';
323 $this->datesp = '';
324 $this->dateep = '';
325 $this->note = '';
326 $this->fk_bank = 0;
327 $this->fk_user_author = 0;
328 $this->fk_user_modif = 0;
329
330 return 1;
331 }
332
339 public function create($user)
340 {
341 global $conf, $langs;
342
343 $error = 0;
344 $now = dol_now();
345
346 // Clean parameters
347 $this->amount = price2num(trim($this->amount));
348 $this->label = trim($this->label);
349 $this->note = trim($this->note);
350 $this->fk_bank = (int) $this->fk_bank;
351 $this->fk_user_author = (int) $this->fk_user_author;
352 $this->fk_user_modif = (int) $this->fk_user_modif;
353 $this->accountid = (int) $this->accountid;
354 $this->paye = (int) $this->paye;
355
356 // Check parameters
357 if (!$this->label) {
358 $this->error = $langs->trans("ErrorFieldRequired", $langs->transnoentities("Label"));
359 return -3;
360 }
361 if ($this->fk_user < 0 || $this->fk_user == '') {
362 $this->error = $langs->trans("ErrorFieldRequired", $langs->transnoentities("Employee"));
363 return -4;
364 }
365 if ($this->amount == '') {
366 $this->error = $langs->trans("ErrorFieldRequired", $langs->transnoentities("Amount"));
367 return -5;
368 }
369
370 $this->db->begin();
371
372 // Insert into llx_salary
373 $sql = "INSERT INTO ".MAIN_DB_PREFIX."salary (fk_user";
374 //$sql .= ", datep";
375 //$sql .= ", datev";
376 $sql .= ", amount";
377 $sql .= ", fk_projet";
378 $sql .= ", salary";
379 $sql .= ", fk_typepayment";
380 $sql .= ", fk_account";
381 if ($this->note) {
382 $sql .= ", note";
383 }
384 $sql .= ", label";
385 $sql .= ", datesp";
386 $sql .= ", dateep";
387 $sql .= ", fk_user_author";
388 $sql .= ", datec";
389 $sql .= ", fk_bank";
390 $sql .= ", entity";
391 $sql .= ") ";
392 $sql .= " VALUES (";
393 $sql .= "'".$this->db->escape($this->fk_user)."'";
394 //$sql .= ", '".$this->db->idate($this->datep)."'";
395 //$sql .= ", '".$this->db->idate($this->datev)."'";
396 $sql .= ", ".((float) $this->amount);
397 $sql .= ", ".($this->fk_project > 0 ? ((int) $this->fk_project) : 0);
398 $sql .= ", ".($this->salary > 0 ? ((float) $this->salary) : "null");
399 $sql .= ", ".($this->type_payment > 0 ? ((int) $this->type_payment) : 0);
400 $sql .= ", ".($this->accountid > 0 ? ((int) $this->accountid) : "null");
401 if ($this->note) {
402 $sql .= ", '".$this->db->escape($this->note)."'";
403 }
404 $sql .= ", '".$this->db->escape($this->label)."'";
405 $sql .= ", '".$this->db->idate($this->datesp)."'";
406 $sql .= ", '".$this->db->idate($this->dateep)."'";
407 $sql .= ", '".$this->db->escape($user->id)."'";
408 $sql .= ", '".$this->db->idate($now)."'";
409 $sql .= ", NULL";
410 $sql .= ", ".((int) $conf->entity);
411 $sql .= ")";
412
413 dol_syslog(get_class($this)."::create", LOG_DEBUG);
414 $result = $this->db->query($sql);
415 if ($result) {
416 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."salary");
417
418 if ($this->id > 0) {
419 // Update extrafield
420 if (!$error) {
421 $result = $this->insertExtraFields();
422 if ($result < 0) {
423 $error++;
424 }
425 }
426
427 // Call trigger
428 $result = $this->call_trigger('SALARY_CREATE', $user);
429 if ($result < 0) {
430 $error++;
431 }
432 // End call triggers
433 } else {
434 $error++;
435 }
436
437 if (!$error) {
438 $this->db->commit();
439 return $this->id;
440 } else {
441 $this->db->rollback();
442 return -2;
443 }
444 } else {
445 $this->error = $this->db->error();
446 $this->db->rollback();
447 return -1;
448 }
449 }
450
451 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
458 public function update_fk_bank($id_bank)
459 {
460 // phpcs:enable
461 $sql = 'UPDATE '.MAIN_DB_PREFIX.'salary SET fk_bank = '.((int) $id_bank);
462 $sql .= " WHERE rowid = ".((int) $this->id);
463 $result = $this->db->query($sql);
464 if ($result) {
465 return 1;
466 } else {
467 dol_print_error($this->db);
468 return -1;
469 }
470 }
471
479 public function getTooltipContentArray($params)
480 {
481 global $langs;
482
483 $langs->loadLangs(['salaries']);
484
485 // Complete datas
486 if (!empty($params['fromajaxtooltip']) && !isset($this->alreadypaid)) {
487 // Load the alreadypaid field
488 $this->alreadypaid = $this->getSommePaiement(0);
489 }
490
491 $datas = [];
492
493 $datas['picto'] = '<u>'.$langs->trans("Salary").'</u>';
494 if (isset($this->status) && isset($this->alreadypaid)) {
495 $datas['picto'] .= ' '.$this->getLibStatut(5, $this->alreadypaid);
496 }
497 $datas['ref'] = '<br><b>'.$langs->trans('Ref').':</b> '.$this->ref;
498
499 return $datas;
500 }
501
512 public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $morecss = '', $save_lastsearch_value = -1)
513 {
514 global $conf, $langs, $hookmanager;
515
516 if (!empty($conf->dol_no_mouse_hover)) {
517 $notooltip = 1;
518 } // Force disable tooltips
519
520 $result = '';
521 $params = [
522 'id' => $this->id,
523 'objecttype' => $this->element,
524 'option' => $option,
525 ];
526 $classfortooltip = 'classfortooltip';
527 $dataparams = '';
528 if (getDolGlobalInt('MAIN_ENABLE_AJAX_TOOLTIP')) {
529 $classfortooltip = 'classforajaxtooltip';
530 $dataparams = ' data-params="'.dol_escape_htmltag(json_encode($params)).'"';
531 $label = '';
532 } else {
533 $label = implode($this->getTooltipContentArray($params));
534 }
535
536 $url = DOL_URL_ROOT.'/salaries/card.php?id='.$this->id;
537
538 if ($option != 'nolink') {
539 // Add param to save lastsearch_values or not
540 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
541 if ($save_lastsearch_value == -1 && isset($_SERVER["PHP_SELF"]) && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
542 $add_save_lastsearch_values = 1;
543 }
544 if ($add_save_lastsearch_values) {
545 $url .= '&save_lastsearch_values=1';
546 }
547 }
548
549 $linkclose = '';
550 if (empty($notooltip)) {
551 if (getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER')) {
552 $label = $langs->trans("ShowMyObject");
553 $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
554 }
555 $linkclose .= ($label ? ' title="'.dol_escape_htmltag($label, 1).'"' : ' title="tocomplete"');
556 $linkclose .= $dataparams.' class="'.$classfortooltip.($morecss ? ' '.$morecss : '').'"';
557 } else {
558 $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
559 }
560
561 $linkstart = '<a href="'.$url.'"';
562 $linkstart .= $linkclose.'>';
563 $linkend = '</a>';
564
565 $result .= $linkstart;
566 if ($withpicto) {
567 $result .= img_object(($notooltip ? '' : $label), ($this->picto ? $this->picto : 'generic'), ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : 'class="'.(($withpicto != 2) ? 'paddingright ' : '').'"'), 0, 0, $notooltip ? 0 : 1);
568 }
569 if ($withpicto != 2) {
570 $result .= $this->ref;
571 }
572 $result .= $linkend;
573 //if ($withpicto != 2) $result.=(($addlabel && $this->label) ? $sep . dol_trunc($this->label, ($addlabel > 1 ? $addlabel : 0)) : '');
574
575 global $action, $hookmanager;
576 $hookmanager->initHooks(array('salarypayment'));
577 $parameters = array('id' => $this->id, 'getnomurl' => &$result);
578 $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
579 if ($reshook > 0) {
580 $result = $hookmanager->resPrint;
581 } else {
582 $result .= $hookmanager->resPrint;
583 }
584
585 return $result;
586 }
587
594 public function getSommePaiement($multicurrency = 0)
595 {
596 $table = 'payment_salary';
597 $field = 'fk_salary';
598
599 $sql = "SELECT sum(amount) as amount";
600 //sum(multicurrency_amount) as multicurrency_amount // Not yet supported
601 $sql .= " FROM ".MAIN_DB_PREFIX.$table;
602 $sql .= " WHERE ".$field." = ".((int) $this->id);
603
604 dol_syslog(get_class($this)."::getSommePaiement", LOG_DEBUG);
605
606 $resql = $this->db->query($sql);
607
608 if ($resql) {
609 $obj = $this->db->fetch_object($resql);
610
611 $this->db->free($resql);
612
613 if ($obj) {
614 if ($multicurrency < 0) {
615 //$this->sumpayed = $obj->amount;
616 //$this->sumpayed_multicurrency = $obj->multicurrency_amount;
617 //return array('alreadypaid'=>(float) $obj->amount, 'alreadypaid_multicurrency'=>(float) $obj->multicurrency_amount);
618 return array(); // Not yet supported
619 } elseif ($multicurrency) {
620 //$this->sumpayed_multicurrency = $obj->multicurrency_amount;
621 //return (float) $obj->multicurrency_amount;
622 return -1; // Not yet supported
623 } else {
624 //$this->sumpayed = $obj->amount;
625 return (float) $obj->amount;
626 }
627 } else {
628 return 0;
629 }
630 } else {
631 $this->error = $this->db->lasterror();
632 return -1;
633 }
634 }
635
642 public function info($id)
643 {
644 $sql = 'SELECT ps.rowid, ps.datec, ps.tms as datem, ps.fk_user_author, ps.fk_user_modif';
645 $sql .= ' FROM '.MAIN_DB_PREFIX.'salary as ps';
646 $sql .= ' WHERE ps.rowid = '.((int) $id);
647
648 dol_syslog(get_class($this).'::info', LOG_DEBUG);
649 $result = $this->db->query($sql);
650
651 if ($result) {
652 if ($this->db->num_rows($result)) {
653 $obj = $this->db->fetch_object($result);
654
655 $this->id = $obj->rowid;
656
657 $this->user_creation_id = $obj->fk_user_author;
658 $this->user_modification_id = $obj->fk_user_modif;
659 $this->date_creation = $this->db->jdate($obj->datec);
660 $this->date_modification = empty($obj->datem) ? '' : $this->db->jdate($obj->datem);
661 }
662 $this->db->free($result);
663 } else {
664 dol_print_error($this->db);
665 }
666 }
667
668 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
677 public function set_paid($user)
678 {
679 // phpcs:enable
680 dol_syslog(get_class($this)."::set_paid is deprecated, use setPaid instead", LOG_NOTICE);
681 return $this->setPaid($user);
682 }
683
690 public function setPaid($user)
691 {
692 $sql = "UPDATE ".MAIN_DB_PREFIX."salary SET";
693 $sql .= " paye = 1";
694 $sql .= " WHERE rowid = ".((int) $this->id);
695
696 $return = $this->db->query($sql);
697
698 if ($return) {
699 $this->paye = 1;
700 return 1;
701 } else {
702 return -1;
703 }
704 }
705
706 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
713 public function set_unpaid($user)
714 {
715 // phpcs:enable
716 $sql = "UPDATE ".MAIN_DB_PREFIX."salary SET";
717 $sql .= " paye = 0";
718 $sql .= " WHERE rowid = ".((int) $this->id);
719
720 $return = $this->db->query($sql);
721
722 if ($return) {
723 $this->paye = 0;
724 return 1;
725 } else {
726 return -1;
727 }
728 }
729
730
738 public function getLibStatut($mode = 0, $alreadypaid = -1)
739 {
740 return $this->LibStatut($this->paye, $mode, $alreadypaid);
741 }
742
743 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
752 public function LibStatut($status, $mode = 0, $alreadypaid = -1)
753 {
754 // phpcs:enable
755 global $langs;
756
757 // Load translation files required by the page
758 $langs->loadLangs(array("customers", "bills"));
759
760 // We reinit status array to force to redefine them because label may change according to properties values.
761 $this->labelStatus = array();
762 $this->labelStatusShort = array();
763
764 if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
765 global $langs;
766 //$langs->load("mymodule");
767 $this->labelStatus[self::STATUS_UNPAID] = $langs->transnoentitiesnoconv('BillStatusNotPaid');
768 $this->labelStatus[self::STATUS_PAID] = $langs->transnoentitiesnoconv('BillStatusPaid');
769 if ($status == self::STATUS_UNPAID && $alreadypaid != 0) {
770 $this->labelStatus[self::STATUS_UNPAID] = $langs->transnoentitiesnoconv("BillStatusStarted");
771 }
772 $this->labelStatusShort[self::STATUS_UNPAID] = $langs->transnoentitiesnoconv('BillStatusNotPaid');
773 $this->labelStatusShort[self::STATUS_PAID] = $langs->transnoentitiesnoconv('BillStatusPaid');
774 if ($status == self::STATUS_UNPAID && $alreadypaid != 0) {
775 $this->labelStatusShort[self::STATUS_UNPAID] = $langs->transnoentitiesnoconv("BillStatusStarted");
776 }
777 }
778
779 $statusType = 'status1';
780 if ($status == 0 && $alreadypaid != 0) {
781 $statusType = 'status3';
782 }
783 if ($status == 1) {
784 $statusType = 'status6';
785 }
786
787 return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusType, $mode);
788 }
789
797 public function getKanbanView($option = '', $arraydata = null)
798 {
799 global $langs;
800
801 $selected = (empty($arraydata['selected']) ? 0 : $arraydata['selected']);
802
803 $return = '<div class="box-flex-item box-flex-grow-zero">';
804 $return .= '<div class="info-box info-box-sm">';
805 $return .= '<span class="info-box-icon bg-infobox-action">';
806 $return .= img_picto('', $this->picto);
807 $return .= '</span>';
808 $return .= '<div class="info-box-content">';
809 $return .= '<span class="info-box-ref inline-block tdoverflowmax150 valignmiddle">'.(method_exists($this, 'getNomUrl') ? $this->getNomUrl(1) : $this->ref).'</span>';
810 if ($selected >= 0) {
811 $return .= '<input id="cb'.$this->id.'" class="flat checkforselect fright" type="checkbox" name="toselect[]" value="'.$this->id.'"'.($selected ? ' checked="checked"' : '').'>';
812 }
813 if (!empty($arraydata['user']) && is_object($arraydata['user'])) {
814 $return .= '<br><span class="info-box-label">'.$arraydata['user']->getNomUrl(1, '', 0, 0, 16, 0, '', 'maxwidth100').'</span>';
815 }
816 if (property_exists($this, 'amount')) {
817 $return .= '<br><span class="info-box-label amount">'.price($this->amount).'</span>';
818 if (property_exists($this, 'type_payment') && !empty($this->type_payment)) {
819 $return .= ' <span class="info-box-label opacitymedium small">';
820 if ($langs->trans("PaymentTypeShort".$this->type_payment) != "PaymentTypeShort".$this->type_payment) {
821 $return .= $langs->trans("PaymentTypeShort".$this->type_payment);
822 } elseif ($langs->trans("PaymentType".$this->type_payment) != "PaymentType".$this->type_payment) {
823 $return .= $langs->trans("PaymentType".$this->type_payment);
824 }
825 $return .= '</span>';
826 }
827 }
828 if (method_exists($this, 'LibStatut')) {
829 $return .= '<br><div class="info-box-status">'.$this->getLibStatut(3, isset($this->alreadypaid) ? $this->alreadypaid : $this->totalpaid).'</div>';
830 }
831 $return .= '</div>';
832 $return .= '</div>';
833 $return .= '</div>';
834 return $return;
835 }
836
837 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
849 public function demande_prelevement($fuser, $amount = 0, $type = 'direct-debit', $sourcetype = 'salaire', $checkduplicateamongall = 0)
850 {
851 // phpcs:enable
852 global $conf, $mysoc;
853
854 $error = 0;
855
856 dol_syslog(get_class($this)."::demande_prelevement", LOG_DEBUG);
857 if ($this->paye == 0) {
858 require_once DOL_DOCUMENT_ROOT.'/societe/class/companybankaccount.class.php';
859 $bac = new CompanyBankAccount($this->db);
860 // @phan-suppress-next-line PhanPluginSuspiciousParamPosition
861 $bac->fetch(0, '', $mysoc->id);
862
863 $sql = "SELECT count(rowid) as nb";
864 $sql .= " FROM ".$this->db->prefix()."prelevement_demande";
865 if ($type == 'salaire') {
866 $sql .= " WHERE fk_salary = ".((int) $this->id);
867 } else {
868 $sql .= " WHERE fk_facture = ".((int) $this->id);
869 }
870 $sql .= " AND type = 'ban'"; // To exclude record done for some online payments
871 if (empty($checkduplicateamongall)) {
872 $sql .= " AND traite = 0";
873 }
874
875 dol_syslog(get_class($this)."::demande_prelevement", LOG_DEBUG);
876
877 $resql = $this->db->query($sql);
878 if ($resql) {
879 $obj = $this->db->fetch_object($resql);
880 if ($obj && $obj->nb == 0) { // If no request found yet
881 $now = dol_now();
882
883 $totalpaid = $this->getSommePaiement();
884 // $totalcreditnotes = $this->getSumCreditNotesUsed();
885 // $totaldeposits = $this->getSumDepositsUsed();
886 //print "totalpaid=".$totalpaid." totalcreditnotes=".$totalcreditnotes." totaldeposts=".$totaldeposits;
887
888 // We can also use bcadd to avoid pb with floating points
889 // For example print 239.2 - 229.3 - 9.9; does not return 0.
890 //$resteapayer=bcadd($this->total_ttc,$totalpaid,$conf->global->MAIN_MAX_DECIMALS_TOT);
891 //$resteapayer=bcadd($resteapayer,$totalavoir,$conf->global->MAIN_MAX_DECIMALS_TOT);
892 // if (empty($amount)) {
893 // $amount = price2num($this->total_ttc - $totalpaid - $totalcreditnotes - $totaldeposits, 'MT');
894 // }
895
896 if (is_numeric($amount) && $amount != 0) {
897 $sql = 'INSERT INTO '.$this->db->prefix().'prelevement_demande(';
898 if ($type == 'salaire') {
899 $sql .= 'fk_salary, ';
900 } else {
901 $sql .= 'fk_facture, ';
902 }
903 $sql .= ' amount, date_demande, fk_user_demande, code_banque, code_guichet, number, cle_rib, sourcetype, type, entity)';
904 $sql .= " VALUES (".((int) $this->id);
905 $sql .= ", ".((float) price2num($amount));
906 $sql .= ", '".$this->db->idate($now)."'";
907 $sql .= ", ".((int) $fuser->id);
908 $sql .= ", '".$this->db->escape($bac->code_banque)."'";
909 $sql .= ", '".$this->db->escape($bac->code_guichet)."'";
910 $sql .= ", '".$this->db->escape($bac->number)."'";
911 $sql .= ", '".$this->db->escape($bac->cle_rib)."'";
912 $sql .= ", '".$this->db->escape($sourcetype)."'";
913 $sql .= ", 'ban'";
914 $sql .= ", ".((int) $conf->entity);
915 $sql .= ")";
916
917 dol_syslog(get_class($this)."::demande_prelevement", LOG_DEBUG);
918 $resql = $this->db->query($sql);
919 if (!$resql) {
920 $this->error = $this->db->lasterror();
921 dol_syslog(get_class($this).'::demandeprelevement Erreur');
922 $error++;
923 }
924 } else {
925 $this->error = 'WithdrawRequestErrorNilAmount';
926 dol_syslog(get_class($this).'::demandeprelevement WithdrawRequestErrorNilAmount');
927 $error++;
928 }
929
930 if (!$error) {
931 // Force payment mode of invoice to withdraw
932 $payment_mode_id = dol_getIdFromCode($this->db, ($type == 'bank-transfer' ? 'VIR' : 'PRE'), 'c_paiement', 'code', 'id', 1);
933 if ($payment_mode_id > 0) {
934 $result = $this->setPaymentMethods($payment_mode_id);
935 }
936 }
937
938 if ($error) {
939 return -1;
940 }
941 return 1;
942 } else {
943 $this->error = "A request already exists";
944 dol_syslog(get_class($this).'::demandeprelevement Can t create a request to generate a direct debit, a request already exists.');
945 return 0;
946 }
947 } else {
948 $this->error = $this->db->error();
949 dol_syslog(get_class($this).'::demandeprelevement Error -2');
950 return -2;
951 }
952 } else {
953 $this->error = "Status of invoice does not allow this";
954 dol_syslog(get_class($this)."::demandeprelevement ".$this->error." $this->status, $this->paye, $this->mode_reglement_id");
955 return -3;
956 }
957 }
958
959 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
967 public function demande_prelevement_delete($fuser, $did)
968 {
969 // phpcs:enable
970 $sql = 'DELETE FROM '.$this->db->prefix().'prelevement_demande';
971 $sql .= ' WHERE rowid = '.((int) $did);
972 $sql .= ' AND traite = 0';
973 if ($this->db->query($sql)) {
974 return 0;
975 } else {
976 $this->error = $this->db->lasterror();
977 dol_syslog(get_class($this).'::demande_prelevement_delete Error '.$this->error);
978 return -1;
979 }
980 }
981}
print $langs trans("AuditedSecurityEvents").'</strong >< span class="opacitymedium"></span >< br > status
Or an array listing all the potential status of the object: array: int of the status => translated la...
Definition security.php:626
$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...
insertExtraFields($trigger='', $userused=null)
Add/Update all extra fields values for the current object.
setPaymentMethods($id)
Change the payments methods.
deleteCommon(User $user, $notrigger=0, $forcechilddeletion=0)
Delete object in database.
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 paid 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 paid 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.
info($id)
Information on record.
__construct($db)
Constructor.
setPaid($user)
Tag social contribution as paid 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.
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
dol_now($mode='auto')
Return date for now.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
dol_getIdFromCode($db, $key, $tablename, $fieldkey='code', $fieldid='id', $entityfilter=0, $filters='')
Return an id or code from a code or id.
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
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.