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