dolibarr 22.0.5
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-2025 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
73 public $datep;
77 public $datev;
78
82 public $salary;
86 public $amount;
87
91 public $fk_project;
92
96 public $type_payment; // TODO Rename into type_payment_id
100 public $type_payment_code;
101
105 public $label;
106
110 public $datesp;
114 public $dateep;
115
119 public $fk_bank;
120
126 public $fk_account;
127
131 public $accountid;
132
136 public $fk_user_author;
137
141 public $fk_user_modif;
142
146 public $user;
147
151 public $paye;
152
156 public $resteapayer;
157
158
159 const STATUS_UNPAID = 0;
160 const STATUS_PAID = 1;
161
162
163 public $fields = array(
164 'rowid' => array('type' => 'integer', 'label' => 'TechnicalID', 'enabled' => 1, 'visible' => 0, 'notnull' => 1, 'index' => 1, 'position' => 1, 'comment' => 'Id'),
165 'ref' => array('type' => 'varchar(128)', 'label' => 'Ref', 'enabled' => 1, 'position' => 10, 'visible' => 1, 'index' => 1, 'comment' => "Reference of object"),
166 'ref_ext' => array('type' => 'varchar(128)', 'label' => 'RefExt', 'enabled' => 1, 'visible' => 0, 'position' => 11),
167 'label' => array('type' => 'varchar(255)', 'label' => 'Label', 'enabled' => 1, 'position' => 12, 'notnull' => 0, 'visible' => 1),
168 'datec' => array('type' => 'datetime', 'label' => 'DateCreation', 'enabled' => 1, 'visible' => 0, 'position' => 30),
169 'datep' => array('type' => 'date', 'label' => 'Date', 'enabled' => 1, 'visible' => 0, 'position' => 40, 'comment' => 'Date'),
170 'datev' => array('type' => 'date', 'label' => 'Date', 'enabled' => 1, 'visible' => 0, 'position' => 50, 'comment' => 'Date'),
171 'fk_user' => array('type' => 'integer:User:user/class/user.class.php', 'label' => 'Employee', 'enabled' => 1, 'position' => 15, 'notnull' => 1, 'visible' => 1, 'picto' => 'user'),
172 'tms' => array('type' => 'timestamp', 'label' => 'DateModification', 'enabled' => 1, 'visible' => 0, 'notnull' => 1, 'position' => 20),
173 'salary' => array('type' => 'double(24,8)', 'label' => 'salary', 'enabled' => 1, 'visible' => 0, 'position' => 70),
174 'amount' => array('type' => 'double(24,8)', 'label' => 'Amount', 'enabled' => 1, 'visible' => 1, 'notnull' => 1, 'position' => 80),
175 'fk_projet' => array('type' => 'integer:Project:projet/class/project.class.php:1:(fk_statut:=:1)', 'label' => 'Project', 'enabled' => "isModEnabled('project')", 'visible' => 0, 'position' => 90),
176 'fk_typepayment' => array('type' => 'typepayment', 'label' => 'DefaultPaymentMode', 'enabled' => 1, 'visible' => 1, 'position' => 100, 'comment' => 'Payment type'),
177 'num_payment' => array('type' => 'string', 'label' => 'Reference', 'enabled' => 1, 'visible' => 0, 'position' => 110, 'comment' => 'Reference'),
178 'datesp' => array('type' => 'date', 'label' => 'DateStart', 'enabled' => 1, 'visible' => 1, 'position' => 130, 'comment' => 'Date'),
179 'dateep' => array('type' => 'date', 'label' => 'DateEnd', 'enabled' => 1, 'visible' => 1, 'position' => 140, 'comment' => 'Date'),
180 'entity' => array('type' => 'integer', 'label' => 'Entity', 'default' => '1', 'enabled' => 1, 'visible' => 0, 'position' => 150, 'index' => 1),
181 'note' => array('type' => 'text', 'label' => 'Note', 'enabled' => 1, 'position' => 160, 'visible' => 0,),
182 'fk_bank' => array('type' => 'integer', 'label' => 'BankId', 'enabled' => 1, 'visible' => 0, 'position' => 170),
183 'paye' => array('type' => 'smallint(6)', 'label' => 'Status', 'enabled' => 1, 'visible' => 1, 'notnull' => 1, 'position' => 180),
184 'fk_user_author' => array('type' => 'integer:User:user/class/user.class.php', 'label' => 'UserAuthor', 'enabled' => 1, 'visible' => 0, 'position' => 190),
185 'fk_user_modif' => array('type' => 'integer:User:user/class/user.class.php', 'label' => 'UserModif', 'enabled' => 1, 'position' => 200, 'visible' => 0,),
186 'note_public' => array('type' => 'text', 'label' => 'NotePublic', 'enabled' => 1, 'visible' => 0, 'position' => 220),
187 );
188
189
195 public function __construct($db)
196 {
197 $this->db = $db;
198 $this->element = 'salary';
199 $this->table_element = 'salary';
200 $this->isextrafieldmanaged = 1;
201
202 $this->fields['ref_ext']['visible'] = getDolGlobalInt('MAIN_LIST_SHOW_REF_EXT');
203 }
204
212 public function update($user = null, $notrigger = 0)
213 {
214 $error = 0;
215
216 // Clean parameters
217 $this->amount = trim($this->amount);
218 $this->label = trim($this->label);
219 $this->note = trim($this->note);
220 $this->note_private = trim($this->note_private);
221 $this->note_public = trim($this->note_public);
222
223 // Check parameters
224 if (empty($this->fk_user) || $this->fk_user < 0) {
225 $this->error = 'ErrorBadParameter';
226 return -1;
227 }
228
229 $this->db->begin();
230
231 // Update request
232 $sql = "UPDATE ".MAIN_DB_PREFIX."salary SET";
233 $sql .= " tms='".$this->db->idate(dol_now())."',";
234 $sql .= " fk_user=".((int) $this->fk_user).",";
235 /*$sql .= " datep='".$this->db->idate($this->datep)."',";
236 $sql .= " datev='".$this->db->idate($this->datev)."',";*/
237 $sql .= " amount=".price2num($this->amount).",";
238 $sql .= " fk_projet=".((int) $this->fk_project).",";
239 $sql .= " fk_typepayment=".((int) $this->type_payment).",";
240 $sql .= " label='".$this->db->escape($this->label)."',";
241 $sql .= " datesp='".$this->db->idate($this->datesp)."',";
242 $sql .= " dateep='".$this->db->idate($this->dateep)."',";
243 $sql .= " note='".$this->db->escape($this->note)."',";
244 $sql .= " fk_bank=".($this->fk_bank > 0 ? (int) $this->fk_bank : "null").",";
245 $sql .= " fk_user_author=".((int) $this->fk_user_author).",";
246 $sql .= " fk_user_modif=".($this->fk_user_modif > 0 ? (int) $this->fk_user_modif : (int) $user->id);
247 $sql .= " WHERE rowid=".((int) $this->id);
248
249 dol_syslog(get_class($this)."::update", LOG_DEBUG);
250 $resql = $this->db->query($sql);
251 if (!$resql) {
252 $this->error = "Error ".$this->db->lasterror();
253 return -1;
254 }
255
256 // Update extrafield
257 if (!$error) {
258 $result = $this->insertExtraFields();
259 if ($result < 0) {
260 $error++;
261 }
262 }
263
264 if (!$notrigger) {
265 // Call trigger
266 $result = $this->call_trigger('SALARY_MODIFY', $user);
267 if ($result < 0) {
268 $error++;
269 }
270 // End call triggers
271 }
272
273 if (!$error) {
274 $this->db->commit();
275 return 1;
276 } else {
277 $this->db->rollback();
278 return -1;
279 }
280 }
281
282
290 public function fetch($id, $user = null)
291 {
292 $sql = "SELECT";
293 $sql .= " s.rowid,";
294 $sql .= " s.tms,";
295 $sql .= " s.fk_user,";
296 $sql .= " s.datep,";
297 $sql .= " s.datev,";
298 $sql .= " s.amount,";
299 $sql .= " s.fk_projet as fk_project,";
300 $sql .= " s.fk_typepayment,";
301 $sql .= " s.label,";
302 $sql .= " s.datesp,";
303 $sql .= " s.dateep,";
304 $sql .= " s.note as note_private,";
305 $sql .= " s.note_public,";
306 $sql .= " s.paye,";
307 $sql .= " s.fk_bank,";
308 $sql .= " s.fk_user_author,";
309 $sql .= " s.fk_user_modif,";
310 $sql .= " s.fk_account,";
311 $sql .= " cp.code as type_payment_code";
312 $sql .= " FROM ".MAIN_DB_PREFIX."salary as s";
313 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."bank as b ON s.fk_bank = b.rowid";
314 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_paiement as cp ON s.fk_typepayment = cp.id";
315 $sql .= " WHERE s.rowid = ".((int) $id);
316
317 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
318
319 $resql = $this->db->query($sql);
320 if ($resql) {
321 if ($this->db->num_rows($resql)) {
322 $obj = $this->db->fetch_object($resql);
323
324 $this->id = $obj->rowid;
325 $this->ref = $obj->rowid;
326 $this->tms = $this->db->jdate($obj->tms);
327 $this->fk_user = $obj->fk_user;
328 $this->datep = $this->db->jdate($obj->datep);
329 $this->datev = $this->db->jdate($obj->datev);
330 $this->amount = $obj->amount;
331 $this->fk_project = $obj->fk_project;
332 $this->type_payment = $obj->fk_typepayment;
333 $this->type_payment_code = $obj->type_payment_code;
334 $this->label = $obj->label;
335 $this->datesp = $this->db->jdate($obj->datesp);
336 $this->dateep = $this->db->jdate($obj->dateep);
337 $this->note = $obj->note_private;
338 $this->note_private = $obj->note_private;
339 $this->note_public = $obj->note_public;
340 $this->paye = $obj->paye;
341 $this->status = $obj->paye;
342 $this->fk_bank = $obj->fk_bank;
343 $this->fk_user_author = $obj->fk_user_author;
344 $this->fk_user_modif = $obj->fk_user_modif;
345 $this->fk_account = $obj->fk_account;
346 $this->accountid = $obj->fk_account;
347
348 // Retrieve all extrafield
349 // fetch optionals attributes and labels
350 $this->fetch_optionals();
351 }
352 $this->db->free($resql);
353
354 return 1;
355 } else {
356 $this->error = "Error ".$this->db->lasterror();
357 return -1;
358 }
359 }
360
361
369 public function delete($user, $notrigger = 0)
370 {
371 return $this->deleteCommon($user, $notrigger);
372 }
373
374
382 public function initAsSpecimen()
383 {
384 $this->id = 0;
385
386 $this->tms = dol_now();
387 $this->fk_user = 0;
388 $this->datep = '';
389 $this->datev = '';
390 $this->amount = '';
391 $this->label = '';
392 $this->datesp = '';
393 $this->dateep = '';
394 $this->note = '';
395 $this->fk_bank = 0;
396 $this->fk_user_author = 0;
397 $this->fk_user_modif = 0;
398
399 return 1;
400 }
401
408 public function create($user)
409 {
410 global $conf, $langs;
411
412 $error = 0;
413 $now = dol_now();
414
415 // Clean parameters
416 $this->amount = price2num(trim($this->amount));
417 $this->label = trim($this->label);
418 $this->note = trim($this->note);
419 $this->fk_bank = (int) $this->fk_bank;
420 $this->fk_user_author = (int) $this->fk_user_author;
421 $this->fk_user_modif = (int) $this->fk_user_modif;
422 $this->accountid = (int) $this->accountid;
423 $this->paye = (int) $this->paye;
424
425 // Check parameters
426 if (!$this->label) {
427 $this->error = $langs->trans("ErrorFieldRequired", $langs->transnoentities("Label"));
428 return -3;
429 }
430 if ($this->fk_user < 0 || $this->fk_user == '') {
431 $this->error = $langs->trans("ErrorFieldRequired", $langs->transnoentities("Employee"));
432 return -4;
433 }
434 if ($this->amount == '') {
435 $this->error = $langs->trans("ErrorFieldRequired", $langs->transnoentities("Amount"));
436 return -5;
437 }
438
439 $this->db->begin();
440
441 // Insert into llx_salary
442 $sql = "INSERT INTO ".MAIN_DB_PREFIX."salary (fk_user";
443 //$sql .= ", datep";
444 //$sql .= ", datev";
445 $sql .= ", amount";
446 $sql .= ", fk_projet";
447 $sql .= ", salary";
448 $sql .= ", fk_typepayment";
449 $sql .= ", fk_account";
450 if ($this->note) {
451 $sql .= ", note";
452 }
453 $sql .= ", label";
454 $sql .= ", datesp";
455 $sql .= ", dateep";
456 $sql .= ", fk_user_author";
457 $sql .= ", datec";
458 $sql .= ", fk_bank";
459 $sql .= ", entity";
460 $sql .= ") ";
461 $sql .= " VALUES (";
462 $sql .= "'".((int) $this->fk_user)."'";
463 //$sql .= ", '".$this->db->idate($this->datep)."'";
464 //$sql .= ", '".$this->db->idate($this->datev)."'";
465 $sql .= ", ".((float) $this->amount);
466 $sql .= ", ".($this->fk_project > 0 ? ((int) $this->fk_project) : 0);
467 $sql .= ", ".($this->salary > 0 ? ((float) $this->salary) : "null");
468 $sql .= ", ".($this->type_payment > 0 ? ((int) $this->type_payment) : 0);
469 $sql .= ", ".($this->accountid > 0 ? ((int) $this->accountid) : "null");
470 if ($this->note) {
471 $sql .= ", '".$this->db->escape($this->note)."'";
472 }
473 $sql .= ", '".$this->db->escape($this->label)."'";
474 $sql .= ", '".$this->db->idate($this->datesp)."'";
475 $sql .= ", '".$this->db->idate($this->dateep)."'";
476 $sql .= ", '".((int) $user->id)."'";
477 $sql .= ", '".$this->db->idate($now)."'";
478 $sql .= ", NULL";
479 $sql .= ", ".((int) $conf->entity);
480 $sql .= ")";
481
482 dol_syslog(get_class($this)."::create", LOG_DEBUG);
483 $result = $this->db->query($sql);
484 if ($result) {
485 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."salary");
486
487 if ($this->id > 0) {
488 // Update extrafield
489 if (!$error) {
490 $result = $this->insertExtraFields();
491 if ($result < 0) {
492 $error++;
493 }
494 }
495
496 // Call trigger
497 $result = $this->call_trigger('SALARY_CREATE', $user);
498 if ($result < 0) {
499 $error++;
500 }
501 // End call triggers
502 } else {
503 $error++;
504 }
505
506 if (!$error) {
507 $this->db->commit();
508 return $this->id;
509 } else {
510 $this->db->rollback();
511 return -2;
512 }
513 } else {
514 $this->error = $this->db->error();
515 $this->db->rollback();
516 return -1;
517 }
518 }
519
520 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
527 public function update_fk_bank($id_bank)
528 {
529 // phpcs:enable
530 $sql = 'UPDATE '.MAIN_DB_PREFIX.'salary SET fk_bank = '.((int) $id_bank);
531 $sql .= " WHERE rowid = ".((int) $this->id);
532 $result = $this->db->query($sql);
533 if ($result) {
534 return 1;
535 } else {
536 dol_print_error($this->db);
537 return -1;
538 }
539 }
540
547 public function getTooltipContentArray($params)
548 {
549 global $langs;
550
551 $langs->loadLangs(['salaries']);
552
553 // Complete datas
554 if (!empty($params['fromajaxtooltip']) && !isset($this->alreadypaid)) {
555 // Load the alreadypaid field
556 $this->alreadypaid = $this->getSommePaiement(0);
557 }
558
559 $datas = [];
560
561 $datas['picto'] = '<u>'.$langs->trans("Salary").'</u>';
562 if (isset($this->status) && isset($this->alreadypaid)) {
563 $datas['picto'] .= ' '.$this->getLibStatut(5, $this->alreadypaid);
564 }
565 $datas['ref'] = '<br><b>'.$langs->trans('Ref').':</b> '.$this->ref;
566
567 return $datas;
568 }
569
580 public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $morecss = '', $save_lastsearch_value = -1)
581 {
582 global $conf, $langs, $hookmanager;
583
584 if (!empty($conf->dol_no_mouse_hover)) {
585 $notooltip = 1;
586 } // Force disable tooltips
587
588 $result = '';
589 $params = [
590 'id' => $this->id,
591 'objecttype' => $this->element,
592 'option' => $option,
593 ];
594 $classfortooltip = 'classfortooltip';
595 $dataparams = '';
596 if (getDolGlobalInt('MAIN_ENABLE_AJAX_TOOLTIP')) {
597 $classfortooltip = 'classforajaxtooltip';
598 $dataparams = ' data-params="'.dol_escape_htmltag(json_encode($params)).'"';
599 $label = '';
600 } else {
601 $label = implode($this->getTooltipContentArray($params));
602 }
603
604 $url = DOL_URL_ROOT.'/salaries/card.php?id='.$this->id;
605
606 if ($option != 'nolink') {
607 // Add param to save lastsearch_values or not
608 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
609 if ($save_lastsearch_value == -1 && isset($_SERVER["PHP_SELF"]) && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
610 $add_save_lastsearch_values = 1;
611 }
612 if ($add_save_lastsearch_values) {
613 $url .= '&save_lastsearch_values=1';
614 }
615 }
616
617 $linkclose = '';
618 if (empty($notooltip)) {
619 if (getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER')) {
620 $label = $langs->trans("ShowMyObject");
621 $linkclose .= ' alt="'.dolPrintHTMLForAttribute($label).'"';
622 }
623 $linkclose .= ($label ? ' title="'.dolPrintHTMLForAttribute($label).'"' : ' title="tocomplete"');
624 $linkclose .= $dataparams.' class="'.$classfortooltip.($morecss ? ' '.$morecss : '').'"';
625 } else {
626 $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
627 }
628
629 $linkstart = '<a href="'.$url.'"';
630 $linkstart .= $linkclose.'>';
631 $linkend = '</a>';
632
633 $result .= $linkstart;
634 if ($withpicto) {
635 $result .= img_object(($notooltip ? '' : $label), ($this->picto ? $this->picto : 'generic'), ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : 'class="'.(($withpicto != 2) ? 'paddingright ' : '').'"'), 0, 0, $notooltip ? 0 : 1);
636 }
637 if ($withpicto != 2) {
638 $result .= $this->ref;
639 }
640 $result .= $linkend;
641 //if ($withpicto != 2) $result.=(($addlabel && $this->label) ? $sep . dol_trunc($this->label, ($addlabel > 1 ? $addlabel : 0)) : '');
642
643 global $action, $hookmanager;
644 $hookmanager->initHooks(array('salarypayment'));
645 $parameters = array('id' => $this->id, 'getnomurl' => &$result);
646 $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
647 if ($reshook > 0) {
648 $result = $hookmanager->resPrint;
649 } else {
650 $result .= $hookmanager->resPrint;
651 }
652
653 return $result;
654 }
655
662 public function getSommePaiement($multicurrency = 0)
663 {
664 $table = 'payment_salary';
665 $field = 'fk_salary';
666
667 $sql = "SELECT sum(amount) as amount";
668 //sum(multicurrency_amount) as multicurrency_amount // Not yet supported
669 $sql .= " FROM ".MAIN_DB_PREFIX.$table;
670 $sql .= " WHERE ".$field." = ".((int) $this->id);
671
672 dol_syslog(get_class($this)."::getSommePaiement for salary id=".((int) $this->id), LOG_DEBUG);
673
674 $resql = $this->db->query($sql);
675
676 if ($resql) {
677 $obj = $this->db->fetch_object($resql);
678
679 $this->db->free($resql);
680
681 if ($obj) {
682 if ($multicurrency < 0) {
683 //$this->sumpayed = $obj->amount;
684 //$this->sumpayed_multicurrency = $obj->multicurrency_amount;
685 //return array('alreadypaid'=>(float) $obj->amount, 'alreadypaid_multicurrency'=>(float) $obj->multicurrency_amount);
686 return array(); // Not yet supported
687 } elseif ($multicurrency) {
688 //$this->sumpayed_multicurrency = $obj->multicurrency_amount;
689 //return (float) $obj->multicurrency_amount;
690 return -1; // Not yet supported
691 } else {
692 //$this->sumpayed = $obj->amount;
693 return (float) $obj->amount;
694 }
695 } else {
696 return 0;
697 }
698 } else {
699 $this->error = $this->db->lasterror();
700 return -1;
701 }
702 }
703
710 public function info($id)
711 {
712 $sql = 'SELECT ps.rowid, ps.datec, ps.tms as datem, ps.fk_user_author, ps.fk_user_modif';
713 $sql .= ' FROM '.MAIN_DB_PREFIX.'salary as ps';
714 $sql .= ' WHERE ps.rowid = '.((int) $id);
715
716 dol_syslog(get_class($this).'::info', LOG_DEBUG);
717 $result = $this->db->query($sql);
718
719 if ($result) {
720 if ($this->db->num_rows($result)) {
721 $obj = $this->db->fetch_object($result);
722
723 $this->id = $obj->rowid;
724
725 $this->user_creation_id = $obj->fk_user_author;
726 $this->user_modification_id = $obj->fk_user_modif;
727 $this->date_creation = $this->db->jdate($obj->datec);
728 $this->date_modification = empty($obj->datem) ? '' : $this->db->jdate($obj->datem);
729 }
730 $this->db->free($result);
731 } else {
732 dol_print_error($this->db);
733 }
734 }
735
736 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
745 public function set_paid($user)
746 {
747 // phpcs:enable
748 dol_syslog(get_class($this)."::set_paid is deprecated, use setPaid instead", LOG_NOTICE);
749 return $this->setPaid($user);
750 }
751
758 public function setPaid($user)
759 {
760 $sql = "UPDATE ".MAIN_DB_PREFIX."salary SET";
761 $sql .= " paye = 1";
762 $sql .= " WHERE rowid = ".((int) $this->id);
763
764 $return = $this->db->query($sql);
765
766 if ($return) {
767 $this->paye = 1;
768 return 1;
769 } else {
770 return -1;
771 }
772 }
773
774 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
781 public function set_unpaid($user)
782 {
783 // phpcs:enable
784 $sql = "UPDATE ".MAIN_DB_PREFIX."salary SET";
785 $sql .= " paye = 0";
786 $sql .= " WHERE rowid = ".((int) $this->id);
787
788 $return = $this->db->query($sql);
789
790 if ($return) {
791 $this->paye = 0;
792 return 1;
793 } else {
794 return -1;
795 }
796 }
797
798
806 public function getLibStatut($mode = 0, $alreadypaid = -1)
807 {
808 return $this->LibStatut($this->paye, $mode, $alreadypaid);
809 }
810
811 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
820 public function LibStatut($status, $mode = 0, $alreadypaid = -1)
821 {
822 // phpcs:enable
823 global $langs;
824
825 // Load translation files required by the page
826 $langs->loadLangs(array("customers", "bills"));
827
828 // We reinit status array to force to redefine them because label may change according to properties values.
829 $this->labelStatus = array();
830 $this->labelStatusShort = array();
831
832 if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
833 global $langs;
834 //$langs->load("mymodule");
835 $this->labelStatus[self::STATUS_UNPAID] = $langs->transnoentitiesnoconv('BillStatusNotPaid');
836 $this->labelStatus[self::STATUS_PAID] = $langs->transnoentitiesnoconv('BillStatusPaid');
837 if ($status == self::STATUS_UNPAID && $alreadypaid != 0) {
838 $this->labelStatus[self::STATUS_UNPAID] = $langs->transnoentitiesnoconv("BillStatusStarted");
839 }
840 $this->labelStatusShort[self::STATUS_UNPAID] = $langs->transnoentitiesnoconv('BillStatusNotPaid');
841 $this->labelStatusShort[self::STATUS_PAID] = $langs->transnoentitiesnoconv('BillStatusPaid');
842 if ($status == self::STATUS_UNPAID && $alreadypaid != 0) {
843 $this->labelStatusShort[self::STATUS_UNPAID] = $langs->transnoentitiesnoconv("BillStatusStarted");
844 }
845 }
846
847 $statusType = 'status1';
848 if ($status == 0 && $alreadypaid != 0) {
849 $statusType = 'status3';
850 }
851 if ($status == 1) {
852 $statusType = 'status6';
853 }
854
855 return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusType, $mode);
856 }
857
865 public function getKanbanView($option = '', $arraydata = null)
866 {
867 global $langs;
868
869 $selected = (empty($arraydata['selected']) ? 0 : $arraydata['selected']);
870
871 $return = '<div class="box-flex-item box-flex-grow-zero">';
872 $return .= '<div class="info-box info-box-sm">';
873 $return .= '<span class="info-box-icon bg-infobox-action">';
874 $return .= img_picto('', $this->picto);
875 $return .= '</span>';
876 $return .= '<div class="info-box-content">';
877 $return .= '<span class="info-box-ref inline-block tdoverflowmax150 valignmiddle">'.(method_exists($this, 'getNomUrl') ? $this->getNomUrl(1) : $this->ref).'</span>';
878 if ($selected >= 0) {
879 $return .= '<input id="cb'.$this->id.'" class="flat checkforselect fright" type="checkbox" name="toselect[]" value="'.$this->id.'"'.($selected ? ' checked="checked"' : '').'>';
880 }
881 if (!empty($arraydata['user']) && is_object($arraydata['user'])) {
882 $user = $arraydata['user'];
883 '@phan-var-force User $user';
884 $return .= '<br><span class="info-box-label">'.$user->getNomUrl(empty($arraydata['user']->photo) ? 1 : -1, '', 0, 0, 16, 0, '', 'maxwidth100').'</span>';
885 }
886 if (property_exists($this, 'amount')) {
887 $return .= '<br><span class="info-box-label amount">'.price($this->amount).'</span>';
888 if (property_exists($this, 'type_payment') && !empty($this->type_payment)) {
889 $return .= ' <span class="info-box-label opacitymedium small">';
890 if ($langs->trans("PaymentTypeShort".$this->type_payment) != "PaymentTypeShort".$this->type_payment) {
891 $return .= $langs->trans("PaymentTypeShort".$this->type_payment);
892 } elseif ($langs->trans("PaymentType".$this->type_payment) != "PaymentType".$this->type_payment) {
893 $return .= $langs->trans("PaymentType".$this->type_payment);
894 }
895 $return .= '</span>';
896 }
897 }
898 if (method_exists($this, 'LibStatut')) {
899 $return .= '<br><div class="info-box-status">'.$this->getLibStatut(3, isset($this->alreadypaid) ? $this->alreadypaid : $this->totalpaid).'</div>';
900 }
901 $return .= '</div>';
902 $return .= '</div>';
903 $return .= '</div>';
904 return $return;
905 }
906
907 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
919 public function demande_prelevement($fuser, $amount = 0, $type = 'direct-debit', $sourcetype = 'salaire', $checkduplicateamongall = 0)
920 {
921 // phpcs:enable
922 global $conf, $mysoc;
923
924 $error = 0;
925
926 dol_syslog(get_class($this)."::demande_prelevement", LOG_DEBUG);
927 if ($this->paye == 0) {
928 require_once DOL_DOCUMENT_ROOT.'/societe/class/companybankaccount.class.php';
929 $bac = new CompanyBankAccount($this->db);
930 // @phan-suppress-next-line PhanPluginSuspiciousParamPosition
931 $bac->fetch(0, '', $mysoc->id);
932
933 $sql = "SELECT count(rowid) as nb";
934 $sql .= " FROM ".$this->db->prefix()."prelevement_demande";
935 if ($type == 'salaire') {
936 $sql .= " WHERE fk_salary = ".((int) $this->id);
937 } else {
938 $sql .= " WHERE fk_facture = ".((int) $this->id);
939 }
940 $sql .= " AND type = 'ban'"; // To exclude record done for some online payments
941 if (empty($checkduplicateamongall)) {
942 $sql .= " AND traite = 0";
943 }
944
945 dol_syslog(get_class($this)."::demande_prelevement", LOG_DEBUG);
946
947 $resql = $this->db->query($sql);
948 if ($resql) {
949 $obj = $this->db->fetch_object($resql);
950 if ($obj && $obj->nb == 0) { // If no request found yet
951 $now = dol_now();
952
953 $totalpaid = $this->getSommePaiement();
954 // $totalcreditnotes = $this->getSumCreditNotesUsed();
955 // $totaldeposits = $this->getSumDepositsUsed();
956 //print "totalpaid=".$totalpaid." totalcreditnotes=".$totalcreditnotes." totaldeposts=".$totaldeposits;
957
958 // We can also use bcadd to avoid pb with floating points
959 // For example print 239.2 - 229.3 - 9.9; does not return 0.
960 //$resteapayer=bcadd($this->total_ttc,$totalpaid,$conf->global->MAIN_MAX_DECIMALS_TOT);
961 //$resteapayer=bcadd($resteapayer,$totalavoir,$conf->global->MAIN_MAX_DECIMALS_TOT);
962 // if (empty($amount)) {
963 // $amount = price2num($this->total_ttc - $totalpaid - $totalcreditnotes - $totaldeposits, 'MT');
964 // }
965
966 if (is_numeric($amount) && $amount != 0) {
967 $sql = 'INSERT INTO '.$this->db->prefix().'prelevement_demande(';
968 if ($type == 'salaire') {
969 $sql .= 'fk_salary, ';
970 } else {
971 $sql .= 'fk_facture, ';
972 }
973 $sql .= ' amount, date_demande, fk_user_demande, code_banque, code_guichet, number, cle_rib, sourcetype, type, entity)';
974 $sql .= " VALUES (".((int) $this->id);
975 $sql .= ", ".((float) price2num($amount));
976 $sql .= ", '".$this->db->idate($now)."'";
977 $sql .= ", ".((int) $fuser->id);
978 $sql .= ", '".$this->db->escape($bac->code_banque)."'";
979 $sql .= ", '".$this->db->escape($bac->code_guichet)."'";
980 $sql .= ", '".$this->db->escape($bac->number)."'";
981 $sql .= ", '".$this->db->escape($bac->cle_rib)."'";
982 $sql .= ", '".$this->db->escape($sourcetype)."'";
983 $sql .= ", 'ban'";
984 $sql .= ", ".((int) $conf->entity);
985 $sql .= ")";
986
987 dol_syslog(get_class($this)."::demande_prelevement", LOG_DEBUG);
988 $resql = $this->db->query($sql);
989 if (!$resql) {
990 $this->error = $this->db->lasterror();
991 dol_syslog(get_class($this).'::demandeprelevement Erreur');
992 $error++;
993 }
994 } else {
995 $this->error = 'WithdrawRequestErrorNilAmount';
996 dol_syslog(get_class($this).'::demandeprelevement WithdrawRequestErrorNilAmount');
997 $error++;
998 }
999
1000 if (!$error) {
1001 // Force payment mode of invoice to withdraw
1002 $payment_mode_id = dol_getIdFromCode($this->db, (($type == 'bank-transfer' || $type == 'salaire') ? 'VIR' : 'PRE'), 'c_paiement', 'code', 'id', 1);
1003 if ($payment_mode_id > 0) {
1004 $result = $this->setPaymentMethods($payment_mode_id);
1005 }
1006 }
1007
1008 if ($error) {
1009 return -1;
1010 }
1011 return 1;
1012 } else {
1013 $this->error = "A request already exists";
1014 dol_syslog(get_class($this).'::demandeprelevement Can t create a request to generate a direct debit, a request already exists.');
1015 return 0;
1016 }
1017 } else {
1018 $this->error = $this->db->error();
1019 dol_syslog(get_class($this).'::demandeprelevement Error -2');
1020 return -2;
1021 }
1022 } else {
1023 $this->error = "Status of invoice does not allow this";
1024 dol_syslog(get_class($this)."::demandeprelevement ".$this->error." $this->status, $this->paye, $this->mode_reglement_id");
1025 return -3;
1026 }
1027 }
1028
1029 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1037 public function demande_prelevement_delete($fuser, $did)
1038 {
1039 // phpcs:enable
1040 $sql = 'DELETE FROM '.$this->db->prefix().'prelevement_demande';
1041 $sql .= ' WHERE rowid = '.((int) $did);
1042 $sql .= ' AND traite = 0';
1043 if ($this->db->query($sql)) {
1044 return 0;
1045 } else {
1046 $this->error = $this->db->lasterror();
1047 dol_syslog(get_class($this).'::demande_prelevement_delete Error '.$this->error);
1048 return -1;
1049 }
1050 }
1051}
$object ref
Definition info.php:90
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 clickable 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 clickable (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.
print $langs trans("Ref").' m titre as m m statut as status
Or an array listing all the potential status of the object: array: int of the status => translated la...
Definition index.php:171
dol_getIdFromCode($db, $key, $tablename, $fieldkey='code', $fieldid='id', $entityfilter=0, $filters='', $useCache=true)
Return an id or code from a code or id.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2, $allowothertags=array())
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 '.
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $allowothertags=array())
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.
dolGetStatus($statusLabel='', $statusLabelShort='', $html='', $statusType='status0', $displayMode=0, $url='', $params=array())
Output the badge of a status.
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 a Dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79