dolibarr  7.0.0-beta
paymentsalary.class.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2011-2015 Alexandre Spangaro <aspangaro.dolibarr@gmail.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 {
34  //public $element='payment_salary'; //!< Id that identify managed objects
35  //public $table_element='payment_salary'; //!< Name of table without prefix where object is stored
36  public $picto='payment';
37 
38  public $tms;
39  public $fk_user;
40  public $datep;
41  public $datev;
42  public $amount;
43  public $type_payment;
44  public $num_payment;
45  public $label;
46  public $datesp;
47  public $dateep;
48  public $fk_bank;
49  public $fk_user_author;
50  public $fk_user_modif;
51 
52 
58  function __construct($db)
59  {
60  $this->db = $db;
61  $this->element = 'payment_salary';
62  $this->table_element = 'payment_salary';
63  return 1;
64  }
65 
73  function update($user=null, $notrigger=0)
74  {
75  global $conf, $langs;
76 
77  $error=0;
78 
79  // Clean parameters
80  $this->fk_user=trim($this->fk_user);
81  $this->amount=trim($this->amount);
82  $this->label=trim($this->label);
83  $this->note=trim($this->note);
84  $this->fk_bank=trim($this->fk_bank);
85  $this->fk_user_author=trim($this->fk_user_author);
86  $this->fk_user_modif=trim($this->fk_user_modif);
87 
88  // Check parameters
89  if (empty($this->fk_user) || $this->fk_user < 0)
90  {
91  $this->error='ErrorBadParameter';
92  return -1;
93  }
94 
95  $this->db->begin();
96 
97  // Update request
98  $sql = "UPDATE ".MAIN_DB_PREFIX."payment_salary SET";
99 
100  $sql.= " tms='".$this->db->idate($this->tms)."',";
101  $sql.= " fk_user=".$this->fk_user.",";
102  $sql.= " datep='".$this->db->idate($this->datep)."',";
103  $sql.= " datev='".$this->db->idate($this->datev)."',";
104  $sql.= " amount=".price2num($this->amount).",";
105  $sql.= " fk_typepayment=".$this->fk_typepayment."',";
106  $sql.= " num_payment='".$this->db->escape($this->num_payment)."',";
107  $sql.= " label='".$this->db->escape($this->label)."',";
108  $sql.= " datesp='".$this->db->idate($this->datesp)."',";
109  $sql.= " dateep='".$this->db->idate($this->dateep)."',";
110  $sql.= " note='".$this->db->escape($this->note)."',";
111  $sql.= " fk_bank=".($this->fk_bank > 0 ? "'".$this->db->escape($this->fk_bank)."'":"null").",";
112  $sql.= " fk_user_author=".$this->fk_user_author.",";
113  $sql.= " fk_user_modif=".$this->fk_user_modif;
114 
115  $sql.= " WHERE rowid=".$this->id;
116 
117  dol_syslog(get_class($this)."::update", LOG_DEBUG);
118  $resql = $this->db->query($sql);
119  if (! $resql)
120  {
121  $this->error="Error ".$this->db->lasterror();
122  return -1;
123  }
124 
125  if (! $notrigger)
126  {
127  // Call trigger
128  $result=$this->call_trigger('PAYMENT_SALARY_MODIFY',$user);
129  if ($result < 0) $error++;
130  // End call triggers
131  }
132 
133  if (! $error)
134  {
135  $this->db->commit();
136  return 1;
137  }
138  else
139  {
140  $this->db->rollback();
141  return -1;
142  }
143  }
144 
145 
153  function fetch($id, $user=null)
154  {
155  global $langs;
156  $sql = "SELECT";
157  $sql.= " s.rowid,";
158 
159  $sql.= " s.tms,";
160  $sql.= " s.fk_user,";
161  $sql.= " s.datep,";
162  $sql.= " s.datev,";
163  $sql.= " s.amount,";
164  $sql.= " s.fk_typepayment,";
165  $sql.= " s.num_payment,";
166  $sql.= " s.label,";
167  $sql.= " s.datesp,";
168  $sql.= " s.dateep,";
169  $sql.= " s.note,";
170  $sql.= " s.fk_bank,";
171  $sql.= " s.fk_user_author,";
172  $sql.= " s.fk_user_modif,";
173  $sql.= " b.fk_account,";
174  $sql.= " b.fk_type,";
175  $sql.= " b.rappro";
176 
177  $sql.= " FROM ".MAIN_DB_PREFIX."payment_salary as s";
178  $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."bank as b ON s.fk_bank = b.rowid";
179  $sql.= " WHERE s.rowid = ".$id;
180 
181  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
182  $resql=$this->db->query($sql);
183  if ($resql)
184  {
185  if ($this->db->num_rows($resql))
186  {
187  $obj = $this->db->fetch_object($resql);
188 
189  $this->id = $obj->rowid;
190  $this->ref = $obj->rowid;
191  $this->tms = $this->db->jdate($obj->tms);
192  $this->fk_user = $obj->fk_user;
193  $this->datep = $this->db->jdate($obj->datep);
194  $this->datev = $this->db->jdate($obj->datev);
195  $this->amount = $obj->amount;
196  $this->type_payement = $obj->fk_typepayment;
197  $this->num_payment = $obj->num_payment;
198  $this->label = $obj->label;
199  $this->datesp = $this->db->jdate($obj->datesp);
200  $this->dateep = $this->db->jdate($obj->dateep);
201  $this->note = $obj->note;
202  $this->fk_bank = $obj->fk_bank;
203  $this->fk_user_author = $obj->fk_user_author;
204  $this->fk_user_modif = $obj->fk_user_modif;
205  $this->fk_account = $obj->fk_account;
206  $this->fk_type = $obj->fk_type;
207  $this->rappro = $obj->rappro;
208  }
209  $this->db->free($resql);
210 
211  return 1;
212  }
213  else
214  {
215  $this->error="Error ".$this->db->lasterror();
216  return -1;
217  }
218  }
219 
220 
227  function delete($user)
228  {
229  global $conf, $langs;
230 
231  $error=0;
232 
233  // Call trigger
234  $result=$this->call_trigger('PAYMENT_SALARY_DELETE',$user);
235  if ($result < 0) return -1;
236  // End call triggers
237 
238 
239  $sql = "DELETE FROM ".MAIN_DB_PREFIX."payment_salary";
240  $sql.= " WHERE rowid=".$this->id;
241 
242  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
243  $resql = $this->db->query($sql);
244  if (! $resql)
245  {
246  $this->error="Error ".$this->db->lasterror();
247  return -1;
248  }
249 
250  return 1;
251  }
252 
253 
261  function initAsSpecimen()
262  {
263  $this->id=0;
264 
265  $this->tms='';
266  $this->fk_user='';
267  $this->datep='';
268  $this->datev='';
269  $this->amount='';
270  $this->label='';
271  $this->datesp='';
272  $this->dateep='';
273  $this->note='';
274  $this->fk_bank='';
275  $this->fk_user_author='';
276  $this->fk_user_modif='';
277  }
278 
285  function create($user)
286  {
287  global $conf,$langs;
288 
289  $error=0;
290  $now=dol_now();
291 
292  // Clean parameters
293  $this->amount=price2num(trim($this->amount));
294  $this->label=trim($this->label);
295  $this->note=trim($this->note);
296  $this->fk_bank=trim($this->fk_bank);
297  $this->fk_user_author=trim($this->fk_user_author);
298  $this->fk_user_modif=trim($this->fk_user_modif);
299 
300  // Check parameters
301  if (! $this->label)
302  {
303  $this->error=$langs->trans("ErrorFieldRequired",$langs->transnoentities("Label"));
304  return -3;
305  }
306  if ($this->fk_user < 0 || $this->fk_user == '')
307  {
308  $this->error=$langs->trans("ErrorFieldRequired",$langs->transnoentities("Employee"));
309  return -4;
310  }
311  if ($this->amount < 0 || $this->amount == '')
312  {
313  $this->error=$langs->trans("ErrorFieldRequired",$langs->transnoentities("Amount"));
314  return -5;
315  }
316  if (! empty($conf->banque->enabled) && (empty($this->accountid) || $this->accountid <= 0))
317  {
318  $this->error=$langs->trans("ErrorFieldRequired",$langs->transnoentities("Account"));
319  return -6;
320  }
321  if (! empty($conf->banque->enabled) && (empty($this->type_payment) || $this->type_payment <= 0))
322  {
323  $this->error=$langs->trans("ErrorFieldRequired",$langs->transnoentities("PaymentMode"));
324  return -7;
325  }
326 
327  $this->db->begin();
328 
329  // Insert into llx_payment_salary
330  $sql = "INSERT INTO ".MAIN_DB_PREFIX."payment_salary (fk_user";
331  $sql.= ", datep";
332  $sql.= ", datev";
333  $sql.= ", amount";
334  $sql.= ", salary";
335  $sql.= ", fk_typepayment";
336  $sql.= ", num_payment";
337  if ($this->note) $sql.= ", note";
338  $sql.= ", label";
339  $sql.= ", datesp";
340  $sql.= ", dateep";
341  $sql.= ", fk_user_author";
342  $sql.= ", datec";
343  $sql.= ", fk_bank";
344  $sql.= ", entity";
345  $sql.= ") ";
346  $sql.= " VALUES (";
347  $sql.= "'".$this->db->escape($this->fk_user)."'";
348  $sql.= ", '".$this->db->idate($this->datep)."'";
349  $sql.= ", '".$this->db->idate($this->datev)."'";
350  $sql.= ", ".$this->amount;
351  $sql.= ", ".($this->salary > 0 ? $this->salary : "null");
352  $sql.= ", ".$this->db->escape($this->type_payment);
353  $sql.= ", '".$this->db->escape($this->num_payment)."'";
354  if ($this->note) $sql.= ", '".$this->db->escape($this->note)."'";
355  $sql.= ", '".$this->db->escape($this->label)."'";
356  $sql.= ", '".$this->db->idate($this->datesp)."'";
357  $sql.= ", '".$this->db->idate($this->dateep)."'";
358  $sql.= ", '".$this->db->escape($user->id)."'";
359  $sql.= ", '".$this->db->idate($now)."'";
360  $sql.= ", NULL";
361  $sql.= ", ".$conf->entity;
362  $sql.= ")";
363 
364  dol_syslog(get_class($this)."::create", LOG_DEBUG);
365  $result = $this->db->query($sql);
366  if ($result)
367  {
368 
369  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."payment_salary");
370 
371  if ($this->id > 0)
372  {
373  if (! empty($conf->banque->enabled) && ! empty($this->amount))
374  {
375  // Insert into llx_bank
376  require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
377 
378  $acc = new Account($this->db);
379  $result=$acc->fetch($this->accountid);
380  if ($result <= 0) dol_print_error($this->db);
381 
382  // Insert payment into llx_bank
383  // Add link 'payment_salary' in bank_url between payment and bank transaction
384  $bank_line_id = $acc->addline(
385  $this->datep,
386  $this->type_payment,
387  $this->label,
388  -abs($this->amount),
389  $this->num_payment,
390  '',
391  $user
392  );
393 
394  // Update fk_bank into llx_paiement.
395  // So we know the payment which has generate the banking ecriture
396  if ($bank_line_id > 0)
397  {
398  $this->update_fk_bank($bank_line_id);
399  }
400  else
401  {
402  $this->error=$acc->error;
403  $error++;
404  }
405 
406  if (! $error)
407  {
408  // Add link 'payment_salary' in bank_url between payment and bank transaction
409  $url=DOL_URL_ROOT.'/compta/salaries/card.php?id=';
410 
411  $result=$acc->add_url_line($bank_line_id, $this->id, $url, "(SalaryPayment)", "payment_salary");
412  if ($result <= 0)
413  {
414  $this->error=$acc->error;
415  $error++;
416  }
417  }
418 
419  $fuser=new User($this->db);
420  $fuser->fetch($this->fk_user);
421 
422  // Add link 'user' in bank_url between operation and bank transaction
423  $result=$acc->add_url_line(
424  $bank_line_id,
425  $this->fk_user,
426  DOL_URL_ROOT.'/user/card.php?id=',
427  $fuser->getFullName($langs),
428  // $langs->trans("SalaryPayment").' '.$fuser->getFullName($langs).' '.dol_print_date($this->datesp,'dayrfc').' '.dol_print_date($this->dateep,'dayrfc'),
429  'user'
430  );
431 
432  if ($result <= 0)
433  {
434  $this->error=$acc->error;
435  $error++;
436  }
437  }
438 
439  // Call trigger
440  $result=$this->call_trigger('PAYMENT_SALARY_CREATE',$user);
441  if ($result < 0) $error++;
442  // End call triggers
443 
444  }
445  else $error++;
446 
447  if (! $error)
448  {
449  $this->db->commit();
450  return $this->id;
451  }
452  else
453  {
454  $this->db->rollback();
455  return -2;
456  }
457  }
458  else
459  {
460  $this->error=$this->db->error();
461  $this->db->rollback();
462  return -1;
463  }
464  }
465 
472  function update_fk_bank($id_bank)
473  {
474  $sql = 'UPDATE '.MAIN_DB_PREFIX.'payment_salary SET fk_bank = '.$id_bank;
475  $sql.= ' WHERE rowid = '.$this->id;
476  $result = $this->db->query($sql);
477  if ($result)
478  {
479  return 1;
480  }
481  else
482  {
483  dol_print_error($this->db);
484  return -1;
485  }
486  }
487 
488 
496  function getNomUrl($withpicto=0,$option='')
497  {
498  global $langs;
499 
500  $result='';
501  $label=$langs->trans("ShowSalaryPayment").': '.$this->ref;
502 
503  $linkstart = '<a href="'.DOL_URL_ROOT.'/compta/salaries/card.php?id='.$this->id.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
504  $linkend='</a>';
505 
506  $picto='payment';
507 
508  $result .= $linkstart;
509  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);
510  if ($withpicto != 2) $result.= $this->ref;
511  $result .= $linkend;
512 
513  return $result;
514  }
515 
522  function info($id)
523  {
524  $sql = 'SELECT ps.rowid, ps.datec, ps.fk_user_author';
525  $sql.= ' FROM '.MAIN_DB_PREFIX.'payment_salary as ps';
526  $sql.= ' WHERE ps.rowid = '.$id;
527 
528  dol_syslog(get_class($this).'::info', LOG_DEBUG);
529  $result = $this->db->query($sql);
530 
531  if ($result)
532  {
533  if ($this->db->num_rows($result))
534  {
535  $obj = $this->db->fetch_object($result);
536  $this->id = $obj->rowid;
537  if ($obj->fk_user_author)
538  {
539  $cuser = new User($this->db);
540  $cuser->fetch($obj->fk_user_author);
541  $this->user_creation = $cuser;
542  }
543  $this->date_creation = $this->db->jdate($obj->datec);
544  }
545  $this->db->free($result);
546  }
547  else
548  {
549  dol_print_error($this->db);
550  }
551  }
552 
553 
560  function getLibStatut($mode=0)
561  {
562  return $this->LibStatut($this->statut,$mode);
563  }
564 
572  function LibStatut($status,$mode=0)
573  {
574  global $langs; // TODO Renvoyer le libelle anglais et faire traduction a affichage
575 
576  $langs->load('compta');
577  /*if ($mode == 0)
578  {
579  if ($status == 0) return $langs->trans('ToValidate');
580  if ($status == 1) return $langs->trans('Validated');
581  }
582  if ($mode == 1)
583  {
584  if ($status == 0) return $langs->trans('ToValidate');
585  if ($status == 1) return $langs->trans('Validated');
586  }
587  if ($mode == 2)
588  {
589  if ($status == 0) return img_picto($langs->trans('ToValidate'),'statut1').' '.$langs->trans('ToValidate');
590  if ($status == 1) return img_picto($langs->trans('Validated'),'statut4').' '.$langs->trans('Validated');
591  }
592  if ($mode == 3)
593  {
594  if ($status == 0) return img_picto($langs->trans('ToValidate'),'statut1');
595  if ($status == 1) return img_picto($langs->trans('Validated'),'statut4');
596  }
597  if ($mode == 4)
598  {
599  if ($status == 0) return img_picto($langs->trans('ToValidate'),'statut1').' '.$langs->trans('ToValidate');
600  if ($status == 1) return img_picto($langs->trans('Validated'),'statut4').' '.$langs->trans('Validated');
601  }
602  if ($mode == 5)
603  {
604  if ($status == 0) return $langs->trans('ToValidate').' '.img_picto($langs->trans('ToValidate'),'statut1');
605  if ($status == 1) return $langs->trans('Validated').' '.img_picto($langs->trans('Validated'),'statut4');
606  }
607  if ($mode == 6)
608  {
609  if ($status == 0) return $langs->trans('ToValidate').' '.img_picto($langs->trans('ToValidate'),'statut1');
610  if ($status == 1) return $langs->trans('Validated').' '.img_picto($langs->trans('Validated'),'statut4');
611  }*/
612  return '';
613  }
614 
615 }
create($user)
Create in database.
fetch($id, $user=null)
Load object in memory from database.
getLibStatut($mode=0)
Retourne le libelle du statut d'une facture (brouillon, validee, abandonnee, payee) ...
Class to manage salary payments.
Class to manage Dolibarr users.
Definition: user.class.php:39
update_fk_bank($id_bank)
Update link between payment salary and line generate into llx_bank.
dol_print_error($db='', $error='', $errors=null)
Affiche message erreur system avec toutes les informations pour faciliter le diagnostic et la remonte...
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0)
Returns text escaped for inclusion in HTML alt or title tags, or into values of HTML input fields...
Class to manage bank accounts.
__construct($db)
Constructor.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='')
Write log message into outputs.
getNomUrl($withpicto=0, $option='')
Send name clicable (with possibly the picto)
img_object($titlealt, $picto, $moreatt= '', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
dol_now($mode='gmt')
Return date for now.
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:1013
call_trigger($trigger_name, $user)
Call trigger based on this instance.
LibStatut($status, $mode=0)
Renvoi le libelle d'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 '...
Parent class of all other business classes (invoices, contracts, proposals, orders, ...)
update($user=null, $notrigger=0)
Update database.