dolibarr  9.0.0
paymentsalary.class.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2011-2018 Alexandre Spangaro <aspangaro@zendsi.com>
3  * Copyright (C) 2014 Juanjo Menent <jmenent@2byte.es>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
25 // Put here all includes required by your class file
26 require_once DOL_DOCUMENT_ROOT .'/core/class/commonobject.class.php';
27 
28 
33 {
37  public $element='payment_salary';
38 
42  public $table_element='payment_salary';
43 
47  public $picto='payment';
48 
49  public $tms;
50 
54  public $fk_user;
55 
56  public $datep;
57  public $datev;
58  public $amount;
59 
63  public $fk_project;
64 
65  public $type_payment;
66  public $num_payment;
67 
71  public $label;
72 
73  public $datesp;
74  public $dateep;
75 
79  public $fk_bank;
80 
84  public $fk_user_author;
85 
89  public $fk_user_modif;
90 
91 
97  function __construct($db)
98  {
99  $this->db = $db;
100  $this->element = 'payment_salary';
101  $this->table_element = 'payment_salary';
102  }
103 
111  function update($user=null, $notrigger=0)
112  {
113  global $conf, $langs;
114 
115  $error=0;
116 
117  // Clean parameters
118  $this->fk_user=trim($this->fk_user);
119  $this->amount=trim($this->amount);
120  $this->label=trim($this->label);
121  $this->note=trim($this->note);
122  $this->fk_bank=trim($this->fk_bank);
123  $this->fk_user_author=trim($this->fk_user_author);
124  $this->fk_user_modif=trim($this->fk_user_modif);
125 
126  // Check parameters
127  if (empty($this->fk_user) || $this->fk_user < 0)
128  {
129  $this->error='ErrorBadParameter';
130  return -1;
131  }
132 
133  $this->db->begin();
134 
135  // Update request
136  $sql = "UPDATE ".MAIN_DB_PREFIX."payment_salary SET";
137 
138  $sql.= " tms='".$this->db->idate($this->tms)."',";
139  $sql.= " fk_user=".$this->fk_user.",";
140  $sql.= " datep='".$this->db->idate($this->datep)."',";
141  $sql.= " datev='".$this->db->idate($this->datev)."',";
142  $sql.= " amount=".price2num($this->amount).",";
143  $sql.= " fk_projet='".$this->db->escape($this->fk_project)."',";
144  $sql.= " fk_typepayment=".$this->fk_typepayment."',";
145  $sql.= " num_payment='".$this->db->escape($this->num_payment)."',";
146  $sql.= " label='".$this->db->escape($this->label)."',";
147  $sql.= " datesp='".$this->db->idate($this->datesp)."',";
148  $sql.= " dateep='".$this->db->idate($this->dateep)."',";
149  $sql.= " note='".$this->db->escape($this->note)."',";
150  $sql.= " fk_bank=".($this->fk_bank > 0 ? "'".$this->db->escape($this->fk_bank)."'":"null").",";
151  $sql.= " fk_user_author=".$this->fk_user_author.",";
152  $sql.= " fk_user_modif=".$this->fk_user_modif;
153 
154  $sql.= " WHERE rowid=".$this->id;
155 
156  dol_syslog(get_class($this)."::update", LOG_DEBUG);
157  $resql = $this->db->query($sql);
158  if (! $resql)
159  {
160  $this->error="Error ".$this->db->lasterror();
161  return -1;
162  }
163 
164  if (! $notrigger)
165  {
166  // Call trigger
167  $result=$this->call_trigger('PAYMENT_SALARY_MODIFY',$user);
168  if ($result < 0) $error++;
169  // End call triggers
170  }
171 
172  if (! $error)
173  {
174  $this->db->commit();
175  return 1;
176  }
177  else
178  {
179  $this->db->rollback();
180  return -1;
181  }
182  }
183 
184 
192  function fetch($id, $user=null)
193  {
194  global $langs;
195  $sql = "SELECT";
196  $sql.= " s.rowid,";
197 
198  $sql.= " s.tms,";
199  $sql.= " s.fk_user,";
200  $sql.= " s.datep,";
201  $sql.= " s.datev,";
202  $sql.= " s.amount,";
203  $sql.= " s.fk_projet as fk_project,";
204  $sql.= " s.fk_typepayment,";
205  $sql.= " s.num_payment,";
206  $sql.= " s.label,";
207  $sql.= " s.datesp,";
208  $sql.= " s.dateep,";
209  $sql.= " s.note,";
210  $sql.= " s.fk_bank,";
211  $sql.= " s.fk_user_author,";
212  $sql.= " s.fk_user_modif,";
213  $sql.= " b.fk_account,";
214  $sql.= " b.fk_type,";
215  $sql.= " b.rappro";
216 
217  $sql.= " FROM ".MAIN_DB_PREFIX."payment_salary as s";
218  $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."bank as b ON s.fk_bank = b.rowid";
219  $sql.= " WHERE s.rowid = ".$id;
220 
221  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
222  $resql=$this->db->query($sql);
223  if ($resql)
224  {
225  if ($this->db->num_rows($resql))
226  {
227  $obj = $this->db->fetch_object($resql);
228 
229  $this->id = $obj->rowid;
230  $this->ref = $obj->rowid;
231  $this->tms = $this->db->jdate($obj->tms);
232  $this->fk_user = $obj->fk_user;
233  $this->datep = $this->db->jdate($obj->datep);
234  $this->datev = $this->db->jdate($obj->datev);
235  $this->amount = $obj->amount;
236  $this->fk_project = $obj->fk_project;
237  $this->type_payement = $obj->fk_typepayment;
238  $this->num_payment = $obj->num_payment;
239  $this->label = $obj->label;
240  $this->datesp = $this->db->jdate($obj->datesp);
241  $this->dateep = $this->db->jdate($obj->dateep);
242  $this->note = $obj->note;
243  $this->fk_bank = $obj->fk_bank;
244  $this->fk_user_author = $obj->fk_user_author;
245  $this->fk_user_modif = $obj->fk_user_modif;
246  $this->fk_account = $obj->fk_account;
247  $this->fk_type = $obj->fk_type;
248  $this->rappro = $obj->rappro;
249  }
250  $this->db->free($resql);
251 
252  return 1;
253  }
254  else
255  {
256  $this->error="Error ".$this->db->lasterror();
257  return -1;
258  }
259  }
260 
261 
268  function delete($user)
269  {
270  global $conf, $langs;
271 
272  $error=0;
273 
274  // Call trigger
275  $result=$this->call_trigger('PAYMENT_SALARY_DELETE',$user);
276  if ($result < 0) return -1;
277  // End call triggers
278 
279 
280  $sql = "DELETE FROM ".MAIN_DB_PREFIX."payment_salary";
281  $sql.= " WHERE rowid=".$this->id;
282 
283  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
284  $resql = $this->db->query($sql);
285  if (! $resql)
286  {
287  $this->error="Error ".$this->db->lasterror();
288  return -1;
289  }
290 
291  return 1;
292  }
293 
294 
302  function initAsSpecimen()
303  {
304  $this->id=0;
305 
306  $this->tms='';
307  $this->fk_user='';
308  $this->datep='';
309  $this->datev='';
310  $this->amount='';
311  $this->label='';
312  $this->datesp='';
313  $this->dateep='';
314  $this->note='';
315  $this->fk_bank='';
316  $this->fk_user_author='';
317  $this->fk_user_modif='';
318  }
319 
326  function create($user)
327  {
328  global $conf,$langs;
329 
330  $error=0;
331  $now=dol_now();
332 
333  // Clean parameters
334  $this->amount=price2num(trim($this->amount));
335  $this->label=trim($this->label);
336  $this->note=trim($this->note);
337  $this->fk_bank=trim($this->fk_bank);
338  $this->fk_user_author=trim($this->fk_user_author);
339  $this->fk_user_modif=trim($this->fk_user_modif);
340 
341  // Check parameters
342  if (! $this->label)
343  {
344  $this->error=$langs->trans("ErrorFieldRequired",$langs->transnoentities("Label"));
345  return -3;
346  }
347  if ($this->fk_user < 0 || $this->fk_user == '')
348  {
349  $this->error=$langs->trans("ErrorFieldRequired",$langs->transnoentities("Employee"));
350  return -4;
351  }
352  if ($this->amount < 0 || $this->amount == '')
353  {
354  $this->error=$langs->trans("ErrorFieldRequired",$langs->transnoentities("Amount"));
355  return -5;
356  }
357  if (! empty($conf->banque->enabled) && (empty($this->accountid) || $this->accountid <= 0))
358  {
359  $this->error=$langs->trans("ErrorFieldRequired",$langs->transnoentities("Account"));
360  return -6;
361  }
362  if (! empty($conf->banque->enabled) && (empty($this->type_payment) || $this->type_payment <= 0))
363  {
364  $this->error=$langs->trans("ErrorFieldRequired",$langs->transnoentities("PaymentMode"));
365  return -7;
366  }
367 
368  $this->db->begin();
369 
370  // Insert into llx_payment_salary
371  $sql = "INSERT INTO ".MAIN_DB_PREFIX."payment_salary (fk_user";
372  $sql.= ", datep";
373  $sql.= ", datev";
374  $sql.= ", amount";
375  $sql.= ", fk_projet";
376  $sql.= ", salary";
377  $sql.= ", fk_typepayment";
378  $sql.= ", num_payment";
379  if ($this->note) $sql.= ", note";
380  $sql.= ", label";
381  $sql.= ", datesp";
382  $sql.= ", dateep";
383  $sql.= ", fk_user_author";
384  $sql.= ", datec";
385  $sql.= ", fk_bank";
386  $sql.= ", entity";
387  $sql.= ") ";
388  $sql.= " VALUES (";
389  $sql.= "'".$this->db->escape($this->fk_user)."'";
390  $sql.= ", '".$this->db->idate($this->datep)."'";
391  $sql.= ", '".$this->db->idate($this->datev)."'";
392  $sql.= ", ".$this->amount;
393  $sql.= ", ".($this->fk_project > 0? $this->fk_project : 0);
394  $sql.= ", ".($this->salary > 0 ? $this->salary : "null");
395  $sql.= ", ".$this->db->escape($this->type_payment);
396  $sql.= ", '".$this->db->escape($this->num_payment)."'";
397  if ($this->note) $sql.= ", '".$this->db->escape($this->note)."'";
398  $sql.= ", '".$this->db->escape($this->label)."'";
399  $sql.= ", '".$this->db->idate($this->datesp)."'";
400  $sql.= ", '".$this->db->idate($this->dateep)."'";
401  $sql.= ", '".$this->db->escape($user->id)."'";
402  $sql.= ", '".$this->db->idate($now)."'";
403  $sql.= ", NULL";
404  $sql.= ", ".$conf->entity;
405  $sql.= ")";
406 
407  dol_syslog(get_class($this)."::create", LOG_DEBUG);
408  $result = $this->db->query($sql);
409  if ($result)
410  {
411 
412  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."payment_salary");
413 
414  if ($this->id > 0)
415  {
416  if (! empty($conf->banque->enabled) && ! empty($this->amount))
417  {
418  // Insert into llx_bank
419  require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
420 
421  $acc = new Account($this->db);
422  $result=$acc->fetch($this->accountid);
423  if ($result <= 0) dol_print_error($this->db);
424 
425  // Insert payment into llx_bank
426  // Add link 'payment_salary' in bank_url between payment and bank transaction
427  $bank_line_id = $acc->addline(
428  $this->datep,
429  $this->type_payment,
430  $this->label,
431  -abs($this->amount),
432  $this->num_payment,
433  '',
434  $user,
435  '',
436  '',
437  '',
438  $this->datev
439  );
440 
441  // Update fk_bank into llx_paiement.
442  // So we know the payment which has generate the banking ecriture
443  if ($bank_line_id > 0)
444  {
445  $this->update_fk_bank($bank_line_id);
446  }
447  else
448  {
449  $this->error=$acc->error;
450  $error++;
451  }
452 
453  if (! $error)
454  {
455  // Add link 'payment_salary' in bank_url between payment and bank transaction
456  $url=DOL_URL_ROOT.'/compta/salaries/card.php?id=';
457 
458  $result=$acc->add_url_line($bank_line_id, $this->id, $url, "(SalaryPayment)", "payment_salary");
459  if ($result <= 0)
460  {
461  $this->error=$acc->error;
462  $error++;
463  }
464  }
465 
466  $fuser=new User($this->db);
467  $fuser->fetch($this->fk_user);
468 
469  // Add link 'user' in bank_url between operation and bank transaction
470  $result=$acc->add_url_line(
471  $bank_line_id,
472  $this->fk_user,
473  DOL_URL_ROOT.'/user/card.php?id=',
474  $fuser->getFullName($langs),
475  // $langs->trans("SalaryPayment").' '.$fuser->getFullName($langs).' '.dol_print_date($this->datesp,'dayrfc').' '.dol_print_date($this->dateep,'dayrfc'),
476  'user'
477  );
478 
479  if ($result <= 0)
480  {
481  $this->error=$acc->error;
482  $error++;
483  }
484  }
485 
486  // Call trigger
487  $result=$this->call_trigger('PAYMENT_SALARY_CREATE',$user);
488  if ($result < 0) $error++;
489  // End call triggers
490  }
491  else $error++;
492 
493  if (! $error)
494  {
495  $this->db->commit();
496  return $this->id;
497  }
498  else
499  {
500  $this->db->rollback();
501  return -2;
502  }
503  }
504  else
505  {
506  $this->error=$this->db->error();
507  $this->db->rollback();
508  return -1;
509  }
510  }
511 
512  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
519  function update_fk_bank($id_bank)
520  {
521  // phpcs:enable
522  $sql = 'UPDATE '.MAIN_DB_PREFIX.'payment_salary SET fk_bank = '.$id_bank;
523  $sql.= ' WHERE rowid = '.$this->id;
524  $result = $this->db->query($sql);
525  if ($result)
526  {
527  return 1;
528  }
529  else
530  {
531  dol_print_error($this->db);
532  return -1;
533  }
534  }
535 
536 
547  function getNomUrl($withpicto=0, $option='', $notooltip=0, $morecss='', $save_lastsearch_value=-1)
548  {
549  global $db, $conf, $langs, $hookmanager;
550  global $dolibarr_main_authentication, $dolibarr_main_demo;
551  global $menumanager;
552 
553  if (! empty($conf->dol_no_mouse_hover)) $notooltip=1; // Force disable tooltips
554 
555  $result = '';
556 
557  $label = '<u>' . $langs->trans("ShowSalaryPayment") . '</u>';
558  $label.= '<br>';
559  $label.= '<b>' . $langs->trans('Ref') . ':</b> ' . $this->ref;
560 
561  $url = DOL_URL_ROOT.'/compta/salaries/card.php?id='.$this->id;
562 
563  if ($option != 'nolink')
564  {
565  // Add param to save lastsearch_values or not
566  $add_save_lastsearch_values=($save_lastsearch_value == 1 ? 1 : 0);
567  if ($save_lastsearch_value == -1 && preg_match('/list\.php/',$_SERVER["PHP_SELF"])) $add_save_lastsearch_values=1;
568  if ($add_save_lastsearch_values) $url.='&save_lastsearch_values=1';
569  }
570 
571  $linkclose='';
572  if (empty($notooltip))
573  {
574  if (! empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER))
575  {
576  $label=$langs->trans("ShowMyObject");
577  $linkclose.=' alt="'.dol_escape_htmltag($label, 1).'"';
578  }
579  $linkclose.=' title="'.dol_escape_htmltag($label, 1).'"';
580  $linkclose.=' class="classfortooltip'.($morecss?' '.$morecss:'').'"';
581 
582  /*
583  $hookmanager->initHooks(array('myobjectdao'));
584  $parameters=array('id'=>$this->id);
585  $reshook=$hookmanager->executeHooks('getnomurltooltip',$parameters,$this,$action); // Note that $action and $object may have been modified by some hooks
586  if ($reshook > 0) $linkclose = $hookmanager->resPrint;
587  */
588  }
589  else $linkclose = ($morecss?' class="'.$morecss.'"':'');
590 
591  $linkstart = '<a href="'.$url.'"';
592  $linkstart.=$linkclose.'>';
593  $linkend='</a>';
594 
595  $result .= $linkstart;
596  if ($withpicto) $result.=img_object(($notooltip?'':$label), ($this->picto?$this->picto:'generic'), ($notooltip?(($withpicto != 2) ? 'class="paddingright"' : ''):'class="'.(($withpicto != 2) ? 'paddingright ' : '').'classfortooltip"'), 0, 0, $notooltip?0:1);
597  if ($withpicto != 2) $result.= $this->ref;
598  $result .= $linkend;
599  //if ($withpicto != 2) $result.=(($addlabel && $this->label) ? $sep . dol_trunc($this->label, ($addlabel > 1 ? $addlabel : 0)) : '');
600 
601  global $action,$hookmanager;
602  $hookmanager->initHooks(array('salarypayment'));
603  $parameters=array('id'=>$this->id, 'getnomurl'=>$result);
604  $reshook=$hookmanager->executeHooks('getNomUrl',$parameters,$this,$action); // Note that $action and $object may have been modified by some hooks
605  if ($reshook > 0) $result = $hookmanager->resPrint;
606  else $result .= $hookmanager->resPrint;
607 
608  return $result;
609  }
610 
617  function info($id)
618  {
619  $sql = 'SELECT ps.rowid, ps.datec, ps.fk_user_author';
620  $sql.= ' FROM '.MAIN_DB_PREFIX.'payment_salary as ps';
621  $sql.= ' WHERE ps.rowid = '.$id;
622 
623  dol_syslog(get_class($this).'::info', LOG_DEBUG);
624  $result = $this->db->query($sql);
625 
626  if ($result)
627  {
628  if ($this->db->num_rows($result))
629  {
630  $obj = $this->db->fetch_object($result);
631  $this->id = $obj->rowid;
632  if ($obj->fk_user_author)
633  {
634  $cuser = new User($this->db);
635  $cuser->fetch($obj->fk_user_author);
636  $this->user_creation = $cuser;
637  }
638  $this->date_creation = $this->db->jdate($obj->datec);
639  }
640  $this->db->free($result);
641  }
642  else
643  {
644  dol_print_error($this->db);
645  }
646  }
647 
648 
655  function getLibStatut($mode=0)
656  {
657  return $this->LibStatut($this->statut,$mode);
658  }
659 
660  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
668  function LibStatut($status,$mode=0)
669  {
670  // phpcs:enable
671  global $langs; // TODO Renvoyer le libelle anglais et faire traduction a affichage
672 
673  $langs->load('compta');
674  /*if ($mode == 0)
675  {
676  if ($status == 0) return $langs->trans('ToValidate');
677  if ($status == 1) return $langs->trans('Validated');
678  }
679  if ($mode == 1)
680  {
681  if ($status == 0) return $langs->trans('ToValidate');
682  if ($status == 1) return $langs->trans('Validated');
683  }
684  if ($mode == 2)
685  {
686  if ($status == 0) return img_picto($langs->trans('ToValidate'),'statut1').' '.$langs->trans('ToValidate');
687  if ($status == 1) return img_picto($langs->trans('Validated'),'statut4').' '.$langs->trans('Validated');
688  }
689  if ($mode == 3)
690  {
691  if ($status == 0) return img_picto($langs->trans('ToValidate'),'statut1');
692  if ($status == 1) return img_picto($langs->trans('Validated'),'statut4');
693  }
694  if ($mode == 4)
695  {
696  if ($status == 0) return img_picto($langs->trans('ToValidate'),'statut1').' '.$langs->trans('ToValidate');
697  if ($status == 1) return img_picto($langs->trans('Validated'),'statut4').' '.$langs->trans('Validated');
698  }
699  if ($mode == 5)
700  {
701  if ($status == 0) return $langs->trans('ToValidate').' '.img_picto($langs->trans('ToValidate'),'statut1');
702  if ($status == 1) return $langs->trans('Validated').' '.img_picto($langs->trans('Validated'),'statut4');
703  }
704  if ($mode == 6)
705  {
706  if ($status == 0) return $langs->trans('ToValidate').' '.img_picto($langs->trans('ToValidate'),'statut1');
707  if ($status == 1) return $langs->trans('Validated').' '.img_picto($langs->trans('Validated'),'statut4');
708  }*/
709  return '';
710  }
711 }
create($user)
Create in database.
print $object label
hash of file content (md5_file(dol_osencode($destfull))
Definition: edit.php:153
fetch($id, $user=null)
Load object in memory from database.
getLibStatut($mode=0)
Retourne le libelle du statut d&#39;une facture (brouillon, validee, abandonnee, payee) ...
if(! empty($conf->facture->enabled) && $user->rights->facture->lire) if(! empty($conf->fournisseur->enabled) && $user->rights->fournisseur->facture->lire) if(! empty($conf->don->enabled) && $user->rights->societe->lire) if(! empty($conf->tax->enabled) && $user->rights->tax->charges->lire) if(! empty($conf->facture->enabled) &&! empty($conf->commande->enabled) && $user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) if(! empty($conf->facture->enabled) && $user->rights->facture->lire) if(! empty($conf->fournisseur->enabled) && $user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1053
Class to manage salary payments.
if(! empty($search_group)) natural_search(array("g.nom" g note
Definition: list.php:123
Class to manage Dolibarr users.
Definition: user.class.php:41
update_fk_bank($id_bank)
Update link between payment salary and line generate into llx_bank.
getNomUrl($withpicto=0, $option='', $notooltip=0, $morecss='', $save_lastsearch_value=-1)
Send name clicable (with possibly the picto)
dol_print_error($db='', $error='', $errors=null)
Affiche message erreur system avec toutes les informations pour faciliter le diagnostic et la remonte...
Class to manage bank accounts.
__construct($db)
Constructor.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='')
Write log message into outputs.
dol_now($mode='gmt')
Return date for now.
call_trigger($trigger_name, $user)
Call trigger based on this instance.
LibStatut($status, $mode=0)
Renvoi le libelle d&#39;un statut donne.
info($id)
Information on record.
initAsSpecimen()
Initialise an instance with random values.
price2num($amount, $rounding='', $alreadysqlnb=0)
Function that return a number with universal decimal format (decimal separator is &#39;...
Parent class of all other business classes (invoices, contracts, proposals, orders, ...)
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
update($user=null, $notrigger=0)
Update database.