dolibarr  9.0.0
paymentvarious.class.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2017 Alexandre Spangaro <aspangaro@zendsi.com>
3  * Copyright (C) 2018 Frédéric France <frederic.france@netlogic.fr>
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='variouspayment';
38 
42  public $table_element='payment_various';
43 
47  public $picto = 'bill';
48 
52  public $id;
53 
57  public $ref;
58 
59  public $tms;
60  public $datep;
61  public $datev;
62  public $sens;
63  public $amount;
64  public $type_payment;
65  public $num_payment;
66 
70  public $label;
71 
72  public $accountancy_code;
73 
77  public $fk_project;
78 
82  public $fk_bank;
83 
87  public $fk_user_author;
88 
92  public $fk_user_modif;
93 
94 
100  function __construct($db)
101  {
102  $this->db = $db;
103  $this->element = 'payment_various';
104  $this->table_element = 'payment_various';
105  }
106 
114  function update($user=null, $notrigger=0)
115  {
116  global $conf, $langs;
117 
118  $error=0;
119 
120  // Clean parameters
121  $this->amount=trim($this->amount);
122  $this->label=trim($this->label);
123  $this->note=trim($this->note);
124  $this->fk_bank = (int) $this->fk_bank;
125  $this->fk_user_author = (int) $this->fk_user_author;
126  $this->fk_user_modif = (int) $this->fk_user_modif;
127 
128  $this->db->begin();
129 
130  // Update request
131  $sql = "UPDATE ".MAIN_DB_PREFIX."payment_various SET";
132  if ($this->tms) $sql.= " tms='".$this->db->idate($this->tms)."',";
133  $sql.= " datep='".$this->db->idate($this->datep)."',";
134  $sql.= " datev='".$this->db->idate($this->datev)."',";
135  $sql.= " sens=".$this->sens.",";
136  $sql.= " amount=".price2num($this->amount).",";
137  $sql.= " fk_typepayment=".$this->fk_typepayment."',";
138  $sql.= " num_payment='".$this->db->escape($this->num_payment)."',";
139  $sql.= " label='".$this->db->escape($this->label)."',";
140  $sql.= " note='".$this->db->escape($this->note)."',";
141  $sql.= " accountancy_code='".$this->db->escape($this->accountancy_code)."',";
142  $sql.= " fk_projet='".$this->db->escape($this->fk_project)."',";
143  $sql.= " fk_bank=".($this->fk_bank > 0 ? $this->fk_bank:"null").",";
144  $sql.= " fk_user_author=".$this->fk_user_author.",";
145  $sql.= " fk_user_modif=".$this->fk_user_modif;
146  $sql.= " WHERE rowid=".$this->id;
147 
148  dol_syslog(get_class($this)."::update", LOG_DEBUG);
149  $resql = $this->db->query($sql);
150  if (! $resql)
151  {
152  $this->error="Error ".$this->db->lasterror();
153  return -1;
154  }
155 
156  if (! $notrigger)
157  {
158  // Call trigger
159  $result=$this->call_trigger('PAYMENT_VARIOUS_MODIFY',$user);
160  if ($result < 0) $error++;
161  // End call triggers
162  }
163 
164  if (! $error)
165  {
166  $this->db->commit();
167  return 1;
168  }
169  else
170  {
171  $this->db->rollback();
172  return -1;
173  }
174  }
175 
176 
184  function fetch($id, $user=null)
185  {
186  global $langs;
187  $sql = "SELECT";
188  $sql.= " v.rowid,";
189  $sql.= " v.tms,";
190  $sql.= " v.datep,";
191  $sql.= " v.datev,";
192  $sql.= " v.sens,";
193  $sql.= " v.amount,";
194  $sql.= " v.fk_typepayment,";
195  $sql.= " v.num_payment,";
196  $sql.= " v.label,";
197  $sql.= " v.note,";
198  $sql.= " v.accountancy_code,";
199  $sql.= " v.fk_projet as fk_project,";
200  $sql.= " v.fk_bank,";
201  $sql.= " v.fk_user_author,";
202  $sql.= " v.fk_user_modif,";
203  $sql.= " b.fk_account,";
204  $sql.= " b.fk_type,";
205  $sql.= " b.rappro";
206  $sql.= " FROM ".MAIN_DB_PREFIX."payment_various as v";
207  $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."bank as b ON v.fk_bank = b.rowid";
208  $sql.= " WHERE v.rowid = ".$id;
209 
210  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
211  $resql=$this->db->query($sql);
212  if ($resql)
213  {
214  if ($this->db->num_rows($resql))
215  {
216  $obj = $this->db->fetch_object($resql);
217 
218  $this->id = $obj->rowid;
219  $this->ref = $obj->rowid;
220  $this->tms = $this->db->jdate($obj->tms);
221  $this->datep = $this->db->jdate($obj->datep);
222  $this->datev = $this->db->jdate($obj->datev);
223  $this->sens = $obj->sens;
224  $this->amount = $obj->amount;
225  $this->type_payment = $obj->fk_typepayment;
226  $this->num_payment = $obj->num_payment;
227  $this->label = $obj->label;
228  $this->note = $obj->note;
229  $this->accountancy_code = $obj->accountancy_code;
230  $this->fk_project = $obj->fk_project;
231  $this->fk_bank = $obj->fk_bank;
232  $this->fk_user_author = $obj->fk_user_author;
233  $this->fk_user_modif = $obj->fk_user_modif;
234  $this->fk_account = $obj->fk_account;
235  $this->fk_type = $obj->fk_type;
236  $this->rappro = $obj->rappro;
237  }
238  $this->db->free($resql);
239 
240  return 1;
241  }
242  else
243  {
244  $this->error="Error ".$this->db->lasterror();
245  return -1;
246  }
247  }
248 
249 
256  function delete($user)
257  {
258  global $conf, $langs;
259 
260  $error=0;
261 
262  // Call trigger
263  $result=$this->call_trigger('PAYMENT_VARIOUS_DELETE',$user);
264  if ($result < 0) return -1;
265  // End call triggers
266 
267 
268  $sql = "DELETE FROM ".MAIN_DB_PREFIX."payment_various";
269  $sql.= " WHERE rowid=".$this->id;
270 
271  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
272  $resql = $this->db->query($sql);
273  if (! $resql)
274  {
275  $this->error="Error ".$this->db->lasterror();
276  return -1;
277  }
278 
279  return 1;
280  }
281 
282 
290  function initAsSpecimen()
291  {
292  $this->id=0;
293 
294  $this->tms='';
295  $this->datep='';
296  $this->datev='';
297  $this->sens='';
298  $this->amount='';
299  $this->label='';
300  $this->accountancy_code='';
301  $this->note='';
302  $this->fk_bank='';
303  $this->fk_user_author='';
304  $this->fk_user_modif='';
305  }
306 
313  function create($user)
314  {
315  global $conf,$langs;
316 
317  $error=0;
318  $now=dol_now();
319 
320  // Clean parameters
321  $this->amount=price2num(trim($this->amount));
322  $this->label=trim($this->label);
323  $this->note=trim($this->note);
324  $this->fk_bank = (int) $this->fk_bank;
325  $this->fk_user_author = (int) $this->fk_user_author;
326  $this->fk_user_modif = (int) $this->fk_user_modif;
327 
328  // Check parameters
329  if (! $this->label)
330  {
331  $this->error=$langs->trans("ErrorFieldRequired",$langs->transnoentities("Label"));
332  return -3;
333  }
334  if ($this->amount < 0 || $this->amount == '')
335  {
336  $this->error=$langs->trans("ErrorFieldRequired",$langs->transnoentities("Amount"));
337  return -5;
338  }
339  if (! empty($conf->banque->enabled) && (empty($this->accountid) || $this->accountid <= 0))
340  {
341  $this->error=$langs->trans("ErrorFieldRequired",$langs->transnoentities("Account"));
342  return -6;
343  }
344  if (! empty($conf->banque->enabled) && (empty($this->type_payment) || $this->type_payment <= 0))
345  {
346  $this->error=$langs->trans("ErrorFieldRequired",$langs->transnoentities("PaymentMode"));
347  return -7;
348  }
349 
350  $this->db->begin();
351 
352  // Insert into llx_payment_various
353  $sql = "INSERT INTO ".MAIN_DB_PREFIX."payment_various (";
354  $sql.= " datep";
355  $sql.= ", datev";
356  $sql.= ", sens";
357  $sql.= ", amount";
358  $sql.= ", fk_typepayment";
359  $sql.= ", num_payment";
360  if ($this->note) $sql.= ", note";
361  $sql.= ", label";
362  $sql.= ", accountancy_code";
363  $sql.= ", fk_projet";
364  $sql.= ", fk_user_author";
365  $sql.= ", datec";
366  $sql.= ", fk_bank";
367  $sql.= ", entity";
368  $sql.= ")";
369  $sql.= " VALUES (";
370  $sql.= "'".$this->db->idate($this->datep)."'";
371  $sql.= ", '".$this->db->idate($this->datev)."'";
372  $sql.= ", '".$this->db->escape($this->sens)."'";
373  $sql.= ", ".$this->amount;
374  $sql.= ", '".$this->db->escape($this->type_payment)."'";
375  $sql.= ", '".$this->db->escape($this->num_payment)."'";
376  if ($this->note) $sql.= ", '".$this->db->escape($this->note)."'";
377  $sql.= ", '".$this->db->escape($this->label)."'";
378  $sql.= ", '".$this->db->escape($this->accountancy_code)."'";
379  $sql.= ", ".($this->fk_project > 0? $this->fk_project : 0);
380  $sql.= ", ".$user->id;
381  $sql.= ", '".$this->db->idate($now)."'";
382  $sql.= ", NULL";
383  $sql.= ", ".$conf->entity;
384  $sql.= ")";
385 
386  dol_syslog(get_class($this)."::create", LOG_DEBUG);
387  $result = $this->db->query($sql);
388  if ($result)
389  {
390  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."payment_various");
391  $this->ref = $this->id;
392 
393  if ($this->id > 0)
394  {
395  if (! empty($conf->banque->enabled) && ! empty($this->amount))
396  {
397  // Insert into llx_bank
398  require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
399 
400  $acc = new Account($this->db);
401  $result=$acc->fetch($this->accountid);
402  if ($result <= 0) dol_print_error($this->db);
403 
404  // Insert payment into llx_bank
405  // Add link 'payment_various' in bank_url between payment and bank transaction
406  $sign=1;
407  if ($this->sens == '0') $sign=-1;
408 
409  $bank_line_id = $acc->addline(
410  $this->datep,
411  $this->type_payment,
412  $this->label,
413  $sign * abs($this->amount),
414  $this->num_payment,
415  '',
416  $user
417  );
418 
419  // Update fk_bank into llx_paiement.
420  // So we know the payment which has generate the banking ecriture
421  if ($bank_line_id > 0)
422  {
423  $this->update_fk_bank($bank_line_id);
424  }
425  else
426  {
427  $this->error=$acc->error;
428  $error++;
429  }
430 
431  if (! $error)
432  {
433  // Add link 'payment_various' in bank_url between payment and bank transaction
434  $url=DOL_URL_ROOT.'/compta/bank/various_payment/card.php?id=';
435 
436  $result=$acc->add_url_line($bank_line_id, $this->id, $url, "(VariousPayment)", "payment_various");
437  if ($result <= 0)
438  {
439  $this->error=$acc->error;
440  $error++;
441  }
442  }
443 
444  if ($result <= 0)
445  {
446  $this->error=$acc->error;
447  $error++;
448  }
449  }
450 
451  // Call trigger
452  $result=$this->call_trigger('PAYMENT_VARIOUS_CREATE',$user);
453  if ($result < 0) $error++;
454  // End call triggers
455  }
456  else $error++;
457 
458  if (! $error)
459  {
460  $this->db->commit();
461  return $this->id;
462  }
463  else
464  {
465  $this->db->rollback();
466  return -2;
467  }
468  }
469  else
470  {
471  $this->error=$this->db->error();
472  $this->db->rollback();
473  return -1;
474  }
475  }
476 
477  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
484  function update_fk_bank($id_bank)
485  {
486  // phpcs:enable
487  $sql = 'UPDATE '.MAIN_DB_PREFIX.'payment_various SET fk_bank = '.$id_bank;
488  $sql.= ' WHERE rowid = '.$this->id;
489  $result = $this->db->query($sql);
490  if ($result)
491  {
492  return 1;
493  }
494  else
495  {
496  dol_print_error($this->db);
497  return -1;
498  }
499  }
500 
501 
508  function getLibStatut($mode=0)
509  {
510  return $this->LibStatut($this->statut,$mode);
511  }
512 
513  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
521  function LibStatut($statut,$mode=0)
522  {
523  // phpcs:enable
524  global $langs;
525 
526  if ($mode == 0)
527  {
528  return $langs->trans($this->statuts[$statut]);
529  }
530  elseif ($mode == 1)
531  {
532  return $langs->trans($this->statuts_short[$statut]);
533  }
534  elseif ($mode == 2)
535  {
536  if ($statut==0) return img_picto($langs->trans($this->statuts_short[$statut]),'statut0').' '.$langs->trans($this->statuts_short[$statut]);
537  elseif ($statut==1) return img_picto($langs->trans($this->statuts_short[$statut]),'statut4').' '.$langs->trans($this->statuts_short[$statut]);
538  elseif ($statut==2) return img_picto($langs->trans($this->statuts_short[$statut]),'statut6').' '.$langs->trans($this->statuts_short[$statut]);
539  }
540  elseif ($mode == 3)
541  {
542  if ($statut==0 && ! empty($this->statuts_short[$statut])) return img_picto($langs->trans($this->statuts_short[$statut]),'statut0');
543  elseif ($statut==1 && ! empty($this->statuts_short[$statut])) return img_picto($langs->trans($this->statuts_short[$statut]),'statut4');
544  elseif ($statut==2 && ! empty($this->statuts_short[$statut])) return img_picto($langs->trans($this->statuts_short[$statut]),'statut6');
545  }
546  elseif ($mode == 4)
547  {
548  if ($statut==0 && ! empty($this->statuts_short[$statut])) return img_picto($langs->trans($this->statuts_short[$statut]),'statut0').' '.$langs->trans($this->statuts[$statut]);
549  elseif ($statut==1 && ! empty($this->statuts_short[$statut])) return img_picto($langs->trans($this->statuts_short[$statut]),'statut4').' '.$langs->trans($this->statuts[$statut]);
550  elseif ($statut==2 && ! empty($this->statuts_short[$statut])) return img_picto($langs->trans($this->statuts_short[$statut]),'statut6').' '.$langs->trans($this->statuts[$statut]);
551  }
552  elseif ($mode == 5)
553  {
554  if ($statut==0 && ! empty($this->statuts_short[$statut])) return $langs->trans($this->statuts_short[$statut]).' '.img_picto($langs->trans($this->statuts_short[$statut]),'statut0');
555  elseif ($statut==1 && ! empty($this->statuts_short[$statut])) return $langs->trans($this->statuts_short[$statut]).' '.img_picto($langs->trans($this->statuts_short[$statut]),'statut4');
556  elseif ($statut==2 && ! empty($this->statuts_short[$statut])) return $langs->trans($this->statuts_short[$statut]).' '.img_picto($langs->trans($this->statuts_short[$statut]),'statut6');
557  }
558  }
559 
560 
570  function getNomUrl($withpicto=0, $option='', $save_lastsearch_value=-1, $notooltip=0)
571  {
572  global $db, $conf, $langs, $hookmanager;
573  global $langs;
574 
575  if (! empty($conf->dol_no_mouse_hover)) $notooltip=1; // Force disable tooltips
576 
577  $result='';
578 
579  $label='<u>'.$langs->trans("ShowVariousPayment").'</u>';
580  $label.= '<br>';
581  $label.= '<b>' . $langs->trans('Ref') . ':</b> ' . $this->ref;
582 
583  $url = DOL_URL_ROOT.'/compta/bank/various_payment/card.php?id='.$this->id;
584 
585  if ($option != 'nolink')
586  {
587  // Add param to save lastsearch_values or not
588  $add_save_lastsearch_values=($save_lastsearch_value == 1 ? 1 : 0);
589  if ($save_lastsearch_value == -1 && preg_match('/list\.php/',$_SERVER["PHP_SELF"])) $add_save_lastsearch_values=1;
590  if ($add_save_lastsearch_values) $url.='&save_lastsearch_values=1';
591  }
592 
593  $linkclose='';
594  if (empty($notooltip))
595  {
596  if (! empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER))
597  {
598  $label=$langs->trans("ShowMyObject");
599  $linkclose.=' alt="'.dol_escape_htmltag($label, 1).'"';
600  }
601  $linkclose.=' title="'.dol_escape_htmltag($label, 1).'"';
602  $linkclose.=' class="classfortooltip'.($morecss?' '.$morecss:'').'"';
603 
604  /*
605  $hookmanager->initHooks(array('myobjectdao'));
606  $parameters=array('id'=>$this->id);
607  $reshook=$hookmanager->executeHooks('getnomurltooltip',$parameters,$this,$action); // Note that $action and $object may have been modified by some hooks
608  if ($reshook > 0) $linkclose = $hookmanager->resPrint;
609  */
610  }
611  else $linkclose = ($morecss?' class="'.$morecss.'"':'');
612 
613  $linkstart = '<a href="'.$url.'"';
614  $linkstart.=$linkclose.'>';
615  $linkend='</a>';
616 
617  $result .= $linkstart;
618  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);
619  if ($withpicto != 2) $result.= $this->ref;
620  $result .= $linkend;
621  //if ($withpicto != 2) $result.=(($addlabel && $this->label) ? $sep . dol_trunc($this->label, ($addlabel > 1 ? $addlabel : 0)) : '');
622 
623  global $action;
624  $hookmanager->initHooks(array('variouspayment'));
625  $parameters=array('id'=>$this->id, 'getnomurl'=>$result);
626  $reshook=$hookmanager->executeHooks('getNomUrl',$parameters,$this,$action); // Note that $action and $object may have been modified by some hooks
627  if ($reshook > 0) $result = $hookmanager->resPrint;
628  else $result .= $hookmanager->resPrint;
629 
630  return $result;
631  }
632 
639  function info($id)
640  {
641  $sql = 'SELECT v.rowid, v.datec, v.fk_user_author';
642  $sql.= ' FROM '.MAIN_DB_PREFIX.'payment_various as v';
643  $sql.= ' WHERE v.rowid = '.$id;
644 
645  dol_syslog(get_class($this).'::info', LOG_DEBUG);
646  $result = $this->db->query($sql);
647 
648  if ($result)
649  {
650  if ($this->db->num_rows($result))
651  {
652  $obj = $this->db->fetch_object($result);
653  $this->id = $obj->rowid;
654  if ($obj->fk_user_author)
655  {
656  $cuser = new User($this->db);
657  $cuser->fetch($obj->fk_user_author);
658  $this->user_creation = $cuser;
659  }
660  $this->date_creation = $this->db->jdate($obj->datec);
661  if ($obj->fk_user_modif)
662  {
663  $muser = new User($this->db);
664  $muser->fetch($obj->fk_user_modif);
665  $this->user_modif = $muser;
666  }
667  $this->date_modif = $this->db->jdate($obj->tms);
668  }
669  $this->db->free($result);
670  }
671  else
672  {
673  dol_print_error($this->db);
674  }
675  }
676 }
print $object label
hash of file content (md5_file(dol_osencode($destfull))
Definition: edit.php:153
__construct($db)
Constructor.
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 various payments.
if(! empty($search_group)) natural_search(array("g.nom" g note
Definition: list.php:123
getNomUrl($withpicto=0, $option='', $save_lastsearch_value=-1, $notooltip=0)
Send name clicable (with possibly the picto)
fetch($id, $user=null)
Load object in memory from database.
Class to manage Dolibarr users.
Definition: user.class.php:41
update_fk_bank($id_bank)
Update link between payment various and line generate into llx_bank.
LibStatut($statut, $mode=0)
Renvoi le libelle d&#39;un statut donne.
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.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='')
Write log message into outputs.
initAsSpecimen()
Initialise an instance with random values.
dol_now($mode='gmt')
Return date for now.
info($id)
Information on record.
getLibStatut($mode=0)
Retourne le libelle du statut.
call_trigger($trigger_name, $user)
Call trigger based on this instance.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='')
Show picto whatever it&#39;s its name (generic function)
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)
create($user)
Create in database.
update($user=null, $notrigger=0)
Update database.