dolibarr  7.0.0-beta
paymentvarious.class.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2017 Alexandre Spangaro <aspangaro@zendsi.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
24 // Put here all includes required by your class file
25 require_once DOL_DOCUMENT_ROOT .'/core/class/commonobject.class.php';
26 
27 
32 {
33  public $element='variouspayment';
34  public $table_element='payment_various';
35  public $picto = 'bill';
36 
37  var $tms;
38  var $datep;
39  var $datev;
40  var $sens;
41  var $amount;
42  var $type_payment;
43  var $num_payment;
44  var $label;
45  var $accountancy_code;
46  var $fk_project;
47  var $fk_bank;
48  var $fk_user_author;
49  var $fk_user_modif;
50 
51 
57  function __construct($db)
58  {
59  $this->db = $db;
60  $this->element = 'payment_various';
61  $this->table_element = 'payment_various';
62  return 1;
63  }
64 
72  function update($user=null, $notrigger=0)
73  {
74  global $conf, $langs;
75 
76  $error=0;
77 
78  // Clean parameters
79  $this->amount=trim($this->amount);
80  $this->label=trim($this->label);
81  $this->note=trim($this->note);
82  $this->fk_bank=trim($this->fk_bank);
83  $this->fk_user_author=trim($this->fk_user_author);
84  $this->fk_user_modif=trim($this->fk_user_modif);
85 
86  $this->db->begin();
87 
88  // Update request
89  $sql = "UPDATE ".MAIN_DB_PREFIX."payment_various SET";
90 
91  $sql.= " tms='".$this->db->idate($this->tms)."',";
92  $sql.= " datep='".$this->db->idate($this->datep)."',";
93  $sql.= " datev='".$this->db->idate($this->datev)."',";
94  $sql.= " sens=".$this->sens.",";
95  $sql.= " amount=".price2num($this->amount).",";
96  $sql.= " fk_typepayment=".$this->fk_typepayment."',";
97  $sql.= " num_payment='".$this->db->escape($this->num_payment)."',";
98  $sql.= " label='".$this->db->escape($this->label)."',";
99  $sql.= " note='".$this->db->escape($this->note)."',";
100  $sql.= " accountancy_code='".$this->db->escape($this->accountancy_code)."',";
101  $sql.= " fk_projet='".$this->db->escape($this->fk_project)."',";
102  $sql.= " fk_bank=".($this->fk_bank > 0 ? $this->fk_bank:"null").",";
103  $sql.= " fk_user_author=".$this->fk_user_author.",";
104  $sql.= " fk_user_modif=".$this->fk_user_modif;
105 
106  $sql.= " WHERE rowid=".$this->id;
107 
108  dol_syslog(get_class($this)."::update", LOG_DEBUG);
109  $resql = $this->db->query($sql);
110  if (! $resql)
111  {
112  $this->error="Error ".$this->db->lasterror();
113  return -1;
114  }
115 
116  if (! $notrigger)
117  {
118  // Call trigger
119  $result=$this->call_trigger('PAYMENT_VARIOUS_MODIFY',$user);
120  if ($result < 0) $error++;
121  // End call triggers
122  }
123 
124  if (! $error)
125  {
126  $this->db->commit();
127  return 1;
128  }
129  else
130  {
131  $this->db->rollback();
132  return -1;
133  }
134  }
135 
136 
144  function fetch($id, $user=null)
145  {
146  global $langs;
147  $sql = "SELECT";
148  $sql.= " v.rowid,";
149 
150  $sql.= " v.tms,";
151  $sql.= " v.datep,";
152  $sql.= " v.datev,";
153  $sql.= " v.sens,";
154  $sql.= " v.amount,";
155  $sql.= " v.fk_typepayment,";
156  $sql.= " v.num_payment,";
157  $sql.= " v.label,";
158  $sql.= " v.note,";
159  $sql.= " v.accountancy_code,";
160  $sql.= " v.fk_projet as fk_project,";
161  $sql.= " v.fk_bank,";
162  $sql.= " v.fk_user_author,";
163  $sql.= " v.fk_user_modif,";
164  $sql.= " b.fk_account,";
165  $sql.= " b.fk_type,";
166  $sql.= " b.rappro";
167 
168  $sql.= " FROM ".MAIN_DB_PREFIX."payment_various as v";
169  $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."bank as b ON v.fk_bank = b.rowid";
170  $sql.= " WHERE v.rowid = ".$id;
171 
172  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
173  $resql=$this->db->query($sql);
174  if ($resql)
175  {
176  if ($this->db->num_rows($resql))
177  {
178  $obj = $this->db->fetch_object($resql);
179 
180  $this->id = $obj->rowid;
181  $this->ref = $obj->rowid;
182  $this->tms = $this->db->jdate($obj->tms);
183  $this->datep = $this->db->jdate($obj->datep);
184  $this->datev = $this->db->jdate($obj->datev);
185  $this->sens = $obj->sens;
186  $this->amount = $obj->amount;
187  $this->type_payement = $obj->fk_typepayment;
188  $this->num_payment = $obj->num_payment;
189  $this->label = $obj->label;
190  $this->note = $obj->note;
191  $this->accountancy_code = $obj->accountancy_code;
192  $this->fk_project = $obj->fk_project;
193  $this->fk_bank = $obj->fk_bank;
194  $this->fk_user_author = $obj->fk_user_author;
195  $this->fk_user_modif = $obj->fk_user_modif;
196  $this->fk_account = $obj->fk_account;
197  $this->fk_type = $obj->fk_type;
198  $this->rappro = $obj->rappro;
199  }
200  $this->db->free($resql);
201 
202  return 1;
203  }
204  else
205  {
206  $this->error="Error ".$this->db->lasterror();
207  return -1;
208  }
209  }
210 
211 
218  function delete($user)
219  {
220  global $conf, $langs;
221 
222  $error=0;
223 
224  // Call trigger
225  $result=$this->call_trigger('PAYMENT_VARIOUS_DELETE',$user);
226  if ($result < 0) return -1;
227  // End call triggers
228 
229 
230  $sql = "DELETE FROM ".MAIN_DB_PREFIX."payment_various";
231  $sql.= " WHERE rowid=".$this->id;
232 
233  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
234  $resql = $this->db->query($sql);
235  if (! $resql)
236  {
237  $this->error="Error ".$this->db->lasterror();
238  return -1;
239  }
240 
241  return 1;
242  }
243 
244 
252  function initAsSpecimen()
253  {
254  $this->id=0;
255 
256  $this->tms='';
257  $this->datep='';
258  $this->datev='';
259  $this->sens='';
260  $this->amount='';
261  $this->label='';
262  $this->accountancy_code='';
263  $this->note='';
264  $this->fk_bank='';
265  $this->fk_user_author='';
266  $this->fk_user_modif='';
267  }
268 
275  function create($user)
276  {
277  global $conf,$langs;
278 
279  $error=0;
280  $now=dol_now();
281 
282  // Clean parameters
283  $this->amount=price2num(trim($this->amount));
284  $this->label=trim($this->label);
285  $this->note=trim($this->note);
286  $this->fk_bank=trim($this->fk_bank);
287  $this->fk_user_author=trim($this->fk_user_author);
288  $this->fk_user_modif=trim($this->fk_user_modif);
289 
290  // Check parameters
291  if (! $this->label)
292  {
293  $this->error=$langs->trans("ErrorFieldRequired",$langs->transnoentities("Label"));
294  return -3;
295  }
296  if ($this->amount < 0 || $this->amount == '')
297  {
298  $this->error=$langs->trans("ErrorFieldRequired",$langs->transnoentities("Amount"));
299  return -5;
300  }
301  if (! empty($conf->banque->enabled) && (empty($this->accountid) || $this->accountid <= 0))
302  {
303  $this->error=$langs->trans("ErrorFieldRequired",$langs->transnoentities("Account"));
304  return -6;
305  }
306  if (! empty($conf->banque->enabled) && (empty($this->type_payment) || $this->type_payment <= 0))
307  {
308  $this->error=$langs->trans("ErrorFieldRequired",$langs->transnoentities("PaymentMode"));
309  return -7;
310  }
311 
312  $this->db->begin();
313 
314  // Insert into llx_payment_various
315  $sql = "INSERT INTO ".MAIN_DB_PREFIX."payment_various (";
316  $sql.= " datep";
317  $sql.= ", datev";
318  $sql.= ", sens";
319  $sql.= ", amount";
320  $sql.= ", fk_typepayment";
321  $sql.= ", num_payment";
322  if ($this->note) $sql.= ", note";
323  $sql.= ", label";
324  $sql.= ", accountancy_code";
325  $sql.= ", fk_projet";
326  $sql.= ", fk_user_author";
327  $sql.= ", datec";
328  $sql.= ", fk_bank";
329  $sql.= ", entity";
330  $sql.= ")";
331  $sql.= " VALUES (";
332  $sql.= "'".$this->db->idate($this->datep)."'";
333  $sql.= ", '".$this->db->idate($this->datev)."'";
334  $sql.= ", '".$this->db->escape($this->sens)."'";
335  $sql.= ", ".$this->amount;
336  $sql.= ", '".$this->db->escape($this->type_payment)."'";
337  $sql.= ", '".$this->db->escape($this->num_payment)."'";
338  if ($this->note) $sql.= ", '".$this->db->escape($this->note)."'";
339  $sql.= ", '".$this->db->escape($this->label)."'";
340  $sql.= ", '".$this->db->escape($this->accountancy_code)."'";
341  $sql.= ", ".($this->fk_project > 0? $this->fk_project : 0);
342  $sql.= ", ".$user->id;
343  $sql.= ", '".$this->db->idate($now)."'";
344  $sql.= ", NULL";
345  $sql.= ", ".$conf->entity;
346  $sql.= ")";
347 
348  dol_syslog(get_class($this)."::create", LOG_DEBUG);
349  $result = $this->db->query($sql);
350  if ($result)
351  {
352  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."payment_various");
353 
354  if ($this->id > 0)
355  {
356  if (! empty($conf->banque->enabled) && ! empty($this->amount))
357  {
358  // Insert into llx_bank
359  require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
360 
361  $acc = new Account($this->db);
362  $result=$acc->fetch($this->accountid);
363  if ($result <= 0) dol_print_error($this->db);
364 
365  // Insert payment into llx_bank
366  // Add link 'payment_various' in bank_url between payment and bank transaction
367  if ($this->sens == '0') $sign='-';
368 
369  $bank_line_id = $acc->addline(
370  $this->datep,
371  $this->type_payment,
372  $this->label,
373  $sign.abs($this->amount),
374  $this->num_payment,
375  '',
376  $user
377  );
378 
379  // Update fk_bank into llx_paiement.
380  // So we know the payment which has generate the banking ecriture
381  if ($bank_line_id > 0)
382  {
383  $this->update_fk_bank($bank_line_id);
384  }
385  else
386  {
387  $this->error=$acc->error;
388  $error++;
389  }
390 
391  if (! $error)
392  {
393  // Add link 'payment_various' in bank_url between payment and bank transaction
394  $url=DOL_URL_ROOT.'/compta/bank/various_payment/card.php?id=';
395 
396  $result=$acc->add_url_line($bank_line_id, $this->id, $url, "(VariousPayment)", "payment_various");
397  if ($result <= 0)
398  {
399  $this->error=$acc->error;
400  $error++;
401  }
402  }
403 
404  if ($result <= 0)
405  {
406  $this->error=$acc->error;
407  $error++;
408  }
409  }
410 
411  // Call trigger
412  $result=$this->call_trigger('PAYMENT_VARIOUS_CREATE',$user);
413  if ($result < 0) $error++;
414  // End call triggers
415 
416  }
417  else $error++;
418 
419  if (! $error)
420  {
421  $this->db->commit();
422  return $this->id;
423  }
424  else
425  {
426  $this->db->rollback();
427  return -2;
428  }
429  }
430  else
431  {
432  $this->error=$this->db->error();
433  $this->db->rollback();
434  return -1;
435  }
436  }
437 
444  function update_fk_bank($id_bank)
445  {
446  $sql = 'UPDATE '.MAIN_DB_PREFIX.'payment_various SET fk_bank = '.$id_bank;
447  $sql.= ' WHERE rowid = '.$this->id;
448  $result = $this->db->query($sql);
449  if ($result)
450  {
451  return 1;
452  }
453  else
454  {
455  dol_print_error($this->db);
456  return -1;
457  }
458  }
459 
460 
467  function getLibStatut($mode=0)
468  {
469  return $this->LibStatut($this->statut,$mode);
470  }
471 
479  function LibStatut($statut,$mode=0)
480  {
481  global $langs;
482 
483  if ($mode == 0)
484  {
485  return $langs->trans($this->statuts[$statut]);
486  }
487  if ($mode == 1)
488  {
489  return $langs->trans($this->statuts_short[$statut]);
490  }
491  if ($mode == 2)
492  {
493  if ($statut==0) return img_picto($langs->trans($this->statuts_short[$statut]),'statut0').' '.$langs->trans($this->statuts_short[$statut]);
494  if ($statut==1) return img_picto($langs->trans($this->statuts_short[$statut]),'statut4').' '.$langs->trans($this->statuts_short[$statut]);
495  if ($statut==2) return img_picto($langs->trans($this->statuts_short[$statut]),'statut6').' '.$langs->trans($this->statuts_short[$statut]);
496  }
497  if ($mode == 3)
498  {
499  if ($statut==0 && ! empty($this->statuts_short[$statut])) return img_picto($langs->trans($this->statuts_short[$statut]),'statut0');
500  if ($statut==1 && ! empty($this->statuts_short[$statut])) return img_picto($langs->trans($this->statuts_short[$statut]),'statut4');
501  if ($statut==2 && ! empty($this->statuts_short[$statut])) return img_picto($langs->trans($this->statuts_short[$statut]),'statut6');
502  }
503  if ($mode == 4)
504  {
505  if ($statut==0 && ! empty($this->statuts_short[$statut])) return img_picto($langs->trans($this->statuts_short[$statut]),'statut0').' '.$langs->trans($this->statuts[$statut]);
506  if ($statut==1 && ! empty($this->statuts_short[$statut])) return img_picto($langs->trans($this->statuts_short[$statut]),'statut4').' '.$langs->trans($this->statuts[$statut]);
507  if ($statut==2 && ! empty($this->statuts_short[$statut])) return img_picto($langs->trans($this->statuts_short[$statut]),'statut6').' '.$langs->trans($this->statuts[$statut]);
508  }
509  if ($mode == 5)
510  {
511  if ($statut==0 && ! empty($this->statuts_short[$statut])) return $langs->trans($this->statuts_short[$statut]).' '.img_picto($langs->trans($this->statuts_short[$statut]),'statut0');
512  if ($statut==1 && ! empty($this->statuts_short[$statut])) return $langs->trans($this->statuts_short[$statut]).' '.img_picto($langs->trans($this->statuts_short[$statut]),'statut4');
513  if ($statut==2 && ! empty($this->statuts_short[$statut])) return $langs->trans($this->statuts_short[$statut]).' '.img_picto($langs->trans($this->statuts_short[$statut]),'statut6');
514  }
515  }
516 
517 
525  function getNomUrl($withpicto=0,$option='')
526  {
527  global $langs;
528 
529  $result='';
530  $label=$langs->trans("ShowVariousPayment").': '.$this->ref;
531 
532  $linkstart = '<a href="'.DOL_URL_ROOT.'/compta/bank/various_payment/card.php?id='.$this->id.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
533  $linkend='</a>';
534 
535  $result .= $linkstart;
536  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);
537  if ($withpicto != 2) $result.= ($maxlen?dol_trunc($this->ref,$maxlen):$this->ref);
538  $result .= $linkend;
539 
540  return $result;
541  }
542 
549  function info($id)
550  {
551  $sql = 'SELECT v.rowid, v.datec, v.fk_user_author';
552  $sql.= ' FROM '.MAIN_DB_PREFIX.'payment_various as v';
553  $sql.= ' WHERE v.rowid = '.$id;
554 
555  dol_syslog(get_class($this).'::info', LOG_DEBUG);
556  $result = $this->db->query($sql);
557 
558  if ($result)
559  {
560  if ($this->db->num_rows($result))
561  {
562  $obj = $this->db->fetch_object($result);
563  $this->id = $obj->rowid;
564  if ($obj->fk_user_author)
565  {
566  $cuser = new User($this->db);
567  $cuser->fetch($obj->fk_user_author);
568  $this->user_creation = $cuser;
569  }
570  $this->date_creation = $this->db->jdate($obj->datec);
571  if ($obj->fk_user_modif)
572  {
573  $muser = new User($this->db);
574  $muser->fetch($obj->fk_user_modif);
575  $this->user_modif = $muser;
576  }
577  $this->date_modif = $this->db->jdate($obj->tms);
578  }
579  $this->db->free($result);
580  }
581  else
582  {
583  dol_print_error($this->db);
584  }
585  }
586 
587 }
img_picto($titlealt, $picto, $moreatt= '', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='')
Show picto whatever it's its name (generic function)
__construct($db)
Constructor.
dol_trunc($string, $size=40, $trunc='right', $stringencoding='UTF-8', $nodot=0, $display=0)
Truncate a string to a particular length adding '...' if string larger than length.
Class to manage various payments.
fetch($id, $user=null)
Load object in memory from database.
Class to manage Dolibarr users.
Definition: user.class.php:39
update_fk_bank($id_bank)
Update link between payment various and line generate into llx_bank.
LibStatut($statut, $mode=0)
Renvoi le libelle d'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...
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.
$element
Id that identify managed objects.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='')
Write log message into outputs.
initAsSpecimen()
Initialise an instance with random values.
$table_element
Name of table without prefix where object is stored.
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.
info($id)
Information on record.
getNomUrl($withpicto=0, $option='')
Send name clicable (with possibly the picto)
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
getLibStatut($mode=0)
Retourne le libelle du statut.
call_trigger($trigger_name, $user)
Call trigger based on this instance.
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, ...)
create($user)
Create in database.
update($user=null, $notrigger=0)
Update database.