dolibarr 20.0.0
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; // TODO Rename into type_payment_id
82 public $type_payment_code;
83
87 public $label;
88
89 public $datesp;
90 public $dateep;
91
95 public $fk_bank;
96
101 public $fk_account;
102
106 public $accountid;
107
111 public $fk_user_author;
112
116 public $fk_user_modif;
117
121 public $user;
122
127 public $paye;
128
129 const STATUS_UNPAID = 0;
130 const STATUS_PAID = 1;
131
132 public $resteapayer;
133
134
140 public function __construct($db)
141 {
142 $this->db = $db;
143 $this->element = 'salary';
144 $this->table_element = 'salary';
145 }
146
154 public function update($user = null, $notrigger = 0)
155 {
156 $error = 0;
157
158 // Clean parameters
159 $this->amount = trim($this->amount);
160 $this->label = trim($this->label);
161 $this->note = trim($this->note);
162
163 // Check parameters
164 if (empty($this->fk_user) || $this->fk_user < 0) {
165 $this->error = 'ErrorBadParameter';
166 return -1;
167 }
168
169 $this->db->begin();
170
171 // Update request
172 $sql = "UPDATE ".MAIN_DB_PREFIX."salary SET";
173 $sql .= " tms='".$this->db->idate(dol_now())."',";
174 $sql .= " fk_user=".((int) $this->fk_user).",";
175 /*$sql .= " datep='".$this->db->idate($this->datep)."',";
176 $sql .= " datev='".$this->db->idate($this->datev)."',";*/
177 $sql .= " amount=".price2num($this->amount).",";
178 $sql .= " fk_projet=".((int) $this->fk_project).",";
179 $sql .= " fk_typepayment=".((int) $this->type_payment).",";
180 $sql .= " label='".$this->db->escape($this->label)."',";
181 $sql .= " datesp='".$this->db->idate($this->datesp)."',";
182 $sql .= " dateep='".$this->db->idate($this->dateep)."',";
183 $sql .= " note='".$this->db->escape($this->note)."',";
184 $sql .= " fk_bank=".($this->fk_bank > 0 ? (int) $this->fk_bank : "null").",";
185 $sql .= " fk_user_author=".((int) $this->fk_user_author).",";
186 $sql .= " fk_user_modif=".($this->fk_user_modif > 0 ? (int) $this->fk_user_modif : (int) $user->id);
187 $sql .= " WHERE rowid=".((int) $this->id);
188
189 dol_syslog(get_class($this)."::update", LOG_DEBUG);
190 $resql = $this->db->query($sql);
191 if (!$resql) {
192 $this->error = "Error ".$this->db->lasterror();
193 return -1;
194 }
195
196 // Update extrafield
197 if (!$error) {
198 $result = $this->insertExtraFields();
199 if ($result < 0) {
200 $error++;
201 }
202 }
203
204 if (!$notrigger) {
205 // Call trigger
206 $result = $this->call_trigger('SALARY_MODIFY', $user);
207 if ($result < 0) {
208 $error++;
209 }
210 // End call triggers
211 }
212
213 if (!$error) {
214 $this->db->commit();
215 return 1;
216 } else {
217 $this->db->rollback();
218 return -1;
219 }
220 }
221
222
230 public function fetch($id, $user = null)
231 {
232 $sql = "SELECT";
233 $sql .= " s.rowid,";
234 $sql .= " s.tms,";
235 $sql .= " s.fk_user,";
236 $sql .= " s.datep,";
237 $sql .= " s.datev,";
238 $sql .= " s.amount,";
239 $sql .= " s.fk_projet as fk_project,";
240 $sql .= " s.fk_typepayment,";
241 $sql .= " s.label,";
242 $sql .= " s.datesp,";
243 $sql .= " s.dateep,";
244 $sql .= " s.note as note_private,";
245 $sql .= " s.note_public,";
246 $sql .= " s.paye,";
247 $sql .= " s.fk_bank,";
248 $sql .= " s.fk_user_author,";
249 $sql .= " s.fk_user_modif,";
250 $sql .= " s.fk_account,";
251 $sql .= " cp.code as type_payment_code";
252 $sql .= " FROM ".MAIN_DB_PREFIX."salary as s";
253 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."bank as b ON s.fk_bank = b.rowid";
254 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_paiement as cp ON s.fk_typepayment = cp.id";
255 $sql .= " WHERE s.rowid = ".((int) $id);
256
257 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
258
259 $resql = $this->db->query($sql);
260 if ($resql) {
261 if ($this->db->num_rows($resql)) {
262 $obj = $this->db->fetch_object($resql);
263
264 $this->id = $obj->rowid;
265 $this->ref = $obj->rowid;
266 $this->tms = $this->db->jdate($obj->tms);
267 $this->fk_user = $obj->fk_user;
268 $this->datep = $this->db->jdate($obj->datep);
269 $this->datev = $this->db->jdate($obj->datev);
270 $this->amount = $obj->amount;
271 $this->fk_project = $obj->fk_project;
272 $this->type_payment = $obj->fk_typepayment;
273 $this->type_payment_code = $obj->type_payment_code;
274 $this->label = $obj->label;
275 $this->datesp = $this->db->jdate($obj->datesp);
276 $this->dateep = $this->db->jdate($obj->dateep);
277 $this->note = $obj->note_private;
278 $this->note_private = $obj->note_private;
279 $this->note_public = $obj->note_public;
280 $this->paye = $obj->paye;
281 $this->status = $obj->paye;
282 $this->fk_bank = $obj->fk_bank;
283 $this->fk_user_author = $obj->fk_user_author;
284 $this->fk_user_modif = $obj->fk_user_modif;
285 $this->fk_account = $obj->fk_account;
286 $this->accountid = $obj->fk_account;
287
288 // Retrieve all extrafield
289 // fetch optionals attributes and labels
290 $this->fetch_optionals();
291 }
292 $this->db->free($resql);
293
294 return 1;
295 } else {
296 $this->error = "Error ".$this->db->lasterror();
297 return -1;
298 }
299 }
300
301
309 public function delete($user, $notrigger = 0)
310 {
311 return $this->deleteCommon($user, $notrigger);
312 }
313
314
322 public function initAsSpecimen()
323 {
324 $this->id = 0;
325
326 $this->tms = dol_now();
327 $this->fk_user = 0;
328 $this->datep = '';
329 $this->datev = '';
330 $this->amount = '';
331 $this->label = '';
332 $this->datesp = '';
333 $this->dateep = '';
334 $this->note = '';
335 $this->fk_bank = 0;
336 $this->fk_user_author = 0;
337 $this->fk_user_modif = 0;
338
339 return 1;
340 }
341
348 public function create($user)
349 {
350 global $conf, $langs;
351
352 $error = 0;
353 $now = dol_now();
354
355 // Clean parameters
356 $this->amount = price2num(trim($this->amount));
357 $this->label = trim($this->label);
358 $this->note = trim($this->note);
359 $this->fk_bank = (int) $this->fk_bank;
360 $this->fk_user_author = (int) $this->fk_user_author;
361 $this->fk_user_modif = (int) $this->fk_user_modif;
362 $this->accountid = (int) $this->accountid;
363 $this->paye = (int) $this->paye;
364
365 // Check parameters
366 if (!$this->label) {
367 $this->error = $langs->trans("ErrorFieldRequired", $langs->transnoentities("Label"));
368 return -3;
369 }
370 if ($this->fk_user < 0 || $this->fk_user == '') {
371 $this->error = $langs->trans("ErrorFieldRequired", $langs->transnoentities("Employee"));
372 return -4;
373 }
374 if ($this->amount == '') {
375 $this->error = $langs->trans("ErrorFieldRequired", $langs->transnoentities("Amount"));
376 return -5;
377 }
378
379 $this->db->begin();
380
381 // Insert into llx_salary
382 $sql = "INSERT INTO ".MAIN_DB_PREFIX."salary (fk_user";
383 //$sql .= ", datep";
384 //$sql .= ", datev";
385 $sql .= ", amount";
386 $sql .= ", fk_projet";
387 $sql .= ", salary";
388 $sql .= ", fk_typepayment";
389 $sql .= ", fk_account";
390 if ($this->note) {
391 $sql .= ", note";
392 }
393 $sql .= ", label";
394 $sql .= ", datesp";
395 $sql .= ", dateep";
396 $sql .= ", fk_user_author";
397 $sql .= ", datec";
398 $sql .= ", fk_bank";
399 $sql .= ", entity";
400 $sql .= ") ";
401 $sql .= " VALUES (";
402 $sql .= "'".$this->db->escape($this->fk_user)."'";
403 //$sql .= ", '".$this->db->idate($this->datep)."'";
404 //$sql .= ", '".$this->db->idate($this->datev)."'";
405 $sql .= ", ".((float) $this->amount);
406 $sql .= ", ".($this->fk_project > 0 ? ((int) $this->fk_project) : 0);
407 $sql .= ", ".($this->salary > 0 ? ((float) $this->salary) : "null");
408 $sql .= ", ".($this->type_payment > 0 ? ((int) $this->type_payment) : 0);
409 $sql .= ", ".($this->accountid > 0 ? ((int) $this->accountid) : "null");
410 if ($this->note) {
411 $sql .= ", '".$this->db->escape($this->note)."'";
412 }
413 $sql .= ", '".$this->db->escape($this->label)."'";
414 $sql .= ", '".$this->db->idate($this->datesp)."'";
415 $sql .= ", '".$this->db->idate($this->dateep)."'";
416 $sql .= ", '".$this->db->escape($user->id)."'";
417 $sql .= ", '".$this->db->idate($now)."'";
418 $sql .= ", NULL";
419 $sql .= ", ".((int) $conf->entity);
420 $sql .= ")";
421
422 dol_syslog(get_class($this)."::create", LOG_DEBUG);
423 $result = $this->db->query($sql);
424 if ($result) {
425 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."salary");
426
427 if ($this->id > 0) {
428 // Update extrafield
429 if (!$error) {
430 $result = $this->insertExtraFields();
431 if ($result < 0) {
432 $error++;
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 for salary id=".((int) $this->id), 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">'.$this->getLibStatut(3, isset($this->alreadypaid) ? $this->alreadypaid : $this->totalpaid).'</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 // @phan-suppress-next-line PhanPluginSuspiciousParamPosition
870 $bac->fetch(0, '', $mysoc->id);
871
872 $sql = "SELECT count(rowid) as nb";
873 $sql .= " FROM ".$this->db->prefix()."prelevement_demande";
874 if ($type == 'salaire') {
875 $sql .= " WHERE fk_salary = ".((int) $this->id);
876 } else {
877 $sql .= " WHERE fk_facture = ".((int) $this->id);
878 }
879 $sql .= " AND type = 'ban'"; // To exclude record done for some online payments
880 if (empty($checkduplicateamongall)) {
881 $sql .= " AND traite = 0";
882 }
883
884 dol_syslog(get_class($this)."::demande_prelevement", LOG_DEBUG);
885
886 $resql = $this->db->query($sql);
887 if ($resql) {
888 $obj = $this->db->fetch_object($resql);
889 if ($obj && $obj->nb == 0) { // If no request found yet
890 $now = dol_now();
891
892 $totalpaid = $this->getSommePaiement();
893 // $totalcreditnotes = $this->getSumCreditNotesUsed();
894 // $totaldeposits = $this->getSumDepositsUsed();
895 //print "totalpaid=".$totalpaid." totalcreditnotes=".$totalcreditnotes." totaldeposts=".$totaldeposits;
896
897 // We can also use bcadd to avoid pb with floating points
898 // For example print 239.2 - 229.3 - 9.9; does not return 0.
899 //$resteapayer=bcadd($this->total_ttc,$totalpaid,$conf->global->MAIN_MAX_DECIMALS_TOT);
900 //$resteapayer=bcadd($resteapayer,$totalavoir,$conf->global->MAIN_MAX_DECIMALS_TOT);
901 // if (empty($amount)) {
902 // $amount = price2num($this->total_ttc - $totalpaid - $totalcreditnotes - $totaldeposits, 'MT');
903 // }
904
905 if (is_numeric($amount) && $amount != 0) {
906 $sql = 'INSERT INTO '.$this->db->prefix().'prelevement_demande(';
907 if ($type == 'salaire') {
908 $sql .= 'fk_salary, ';
909 } else {
910 $sql .= 'fk_facture, ';
911 }
912 $sql .= ' amount, date_demande, fk_user_demande, code_banque, code_guichet, number, cle_rib, sourcetype, type, entity)';
913 $sql .= " VALUES (".((int) $this->id);
914 $sql .= ", ".((float) price2num($amount));
915 $sql .= ", '".$this->db->idate($now)."'";
916 $sql .= ", ".((int) $fuser->id);
917 $sql .= ", '".$this->db->escape($bac->code_banque)."'";
918 $sql .= ", '".$this->db->escape($bac->code_guichet)."'";
919 $sql .= ", '".$this->db->escape($bac->number)."'";
920 $sql .= ", '".$this->db->escape($bac->cle_rib)."'";
921 $sql .= ", '".$this->db->escape($sourcetype)."'";
922 $sql .= ", 'ban'";
923 $sql .= ", ".((int) $conf->entity);
924 $sql .= ")";
925
926 dol_syslog(get_class($this)."::demande_prelevement", LOG_DEBUG);
927 $resql = $this->db->query($sql);
928 if (!$resql) {
929 $this->error = $this->db->lasterror();
930 dol_syslog(get_class($this).'::demandeprelevement Erreur');
931 $error++;
932 }
933 } else {
934 $this->error = 'WithdrawRequestErrorNilAmount';
935 dol_syslog(get_class($this).'::demandeprelevement WithdrawRequestErrorNilAmount');
936 $error++;
937 }
938
939 if (!$error) {
940 // Force payment mode of invoice to withdraw
941 $payment_mode_id = dol_getIdFromCode($this->db, ($type == 'bank-transfer' ? 'VIR' : 'PRE'), 'c_paiement', 'code', 'id', 1);
942 if ($payment_mode_id > 0) {
943 $result = $this->setPaymentMethods($payment_mode_id);
944 }
945 }
946
947 if ($error) {
948 return -1;
949 }
950 return 1;
951 } else {
952 $this->error = "A request already exists";
953 dol_syslog(get_class($this).'::demandeprelevement Can t create a request to generate a direct debit, a request already exists.');
954 return 0;
955 }
956 } else {
957 $this->error = $this->db->error();
958 dol_syslog(get_class($this).'::demandeprelevement Error -2');
959 return -2;
960 }
961 } else {
962 $this->error = "Status of invoice does not allow this";
963 dol_syslog(get_class($this)."::demandeprelevement ".$this->error." $this->status, $this->paye, $this->mode_reglement_id");
964 return -3;
965 }
966 }
967
968 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
976 public function demande_prelevement_delete($fuser, $did)
977 {
978 // phpcs:enable
979 $sql = 'DELETE FROM '.$this->db->prefix().'prelevement_demande';
980 $sql .= ' WHERE rowid = '.((int) $did);
981 $sql .= ' AND traite = 0';
982 if ($this->db->query($sql)) {
983 return 0;
984 } else {
985 $this->error = $this->db->lasterror();
986 dol_syslog(get_class($this).'::demande_prelevement_delete Error '.$this->error);
987 return -1;
988 }
989 }
990}
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:637
$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.