dolibarr  7.0.0-beta
tva.class.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2002-2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3  * Copyright (C) 2004-2008 Laurent Destailleur <eldy@users.sourceforge.net>
4  * Copyright (C) 2011-2017 Alexandre Spangaro <aspangaro@zendsi.com>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
26 // Put here all includes required by your class file
27 require_once DOL_DOCUMENT_ROOT .'/core/class/commonobject.class.php';
28 
29 
33 class Tva extends CommonObject
34 {
35  //public $element='tva'; //!< Id that identify managed objects
36  //public $table_element='tva'; //!< Name of table without prefix where object is stored
37  public $picto='payment';
38 
39  var $tms;
40  var $datep;
41  var $datev;
42  var $amount;
43  var $type_payment;
44  var $num_payment;
45  var $label;
46  var $fk_bank;
47  var $fk_user_creat;
48  var $fk_user_modif;
49 
50 
51 
57  function __construct($db)
58  {
59  $this->db = $db;
60  $this->element = 'tva';
61  $this->table_element = 'tva';
62  return 1;
63  }
64 
65 
72  function create($user)
73  {
74  global $conf, $langs;
75 
76  $error=0;
77  $now=dol_now();
78 
79  // Clean parameters
80  $this->amount=trim($this->amount);
81  $this->label=trim($this->label);
82  $this->note=trim($this->note);
83  $this->fk_bank=trim($this->fk_bank);
84  $this->fk_user_creat=trim($this->fk_user_creat);
85  $this->fk_user_modif=trim($this->fk_user_modif);
86 
87  // Check parameters
88  // Put here code to add control on parameters values
89 
90  $this->db->begin();
91 
92  // Insert request
93  $sql = "INSERT INTO ".MAIN_DB_PREFIX."tva(";
94  $sql.= "datec,";
95  $sql.= "datep,";
96  $sql.= "datev,";
97  $sql.= "amount,";
98  $sql.= "label,";
99  $sql.= "note,";
100  $sql.= "fk_bank,";
101  $sql.= "fk_user_creat,";
102  $sql.= "fk_user_modif";
103  $sql.= ") VALUES (";
104  $sql.= " '".$this->db->idate($now)."',";
105  $sql.= " '".$this->db->idate($this->datep)."',";
106  $sql.= " '".$this->db->idate($this->datev)."',";
107  $sql.= " '".$this->db->escape($this->amount)."',";
108  $sql.= " '".$this->db->escape($this->label)."',";
109  $sql.= " '".$this->db->escape($this->note)."',";
110  $sql.= " ".($this->fk_bank <= 0 ? "NULL" : "'".$this->db->escape($this->fk_bank)."'").",";
111  $sql.= " '".$this->db->escape($this->fk_user_creat)."',";
112  $sql.= " '".$this->db->escape($this->fk_user_modif)."'";
113  $sql.= ")";
114 
115  dol_syslog(get_class($this)."::create", LOG_DEBUG);
116  $resql=$this->db->query($sql);
117  if ($resql)
118  {
119  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."tva");
120 
121  // Call trigger
122  $result=$this->call_trigger('TVA_CREATE',$user);
123  if ($result < 0) $error++;
124  // End call triggers
125 
126  if (! $error)
127  {
128  $this->db->commit();
129  return $this->id;
130  }
131  else
132  {
133  $this->db->rollback();
134  return -1;
135  }
136  }
137  else
138  {
139  $this->error="Error ".$this->db->lasterror();
140  $this->db->rollback();
141  return -1;
142  }
143  }
144 
152  function update($user=null, $notrigger=0)
153  {
154  global $conf, $langs;
155 
156  $error=0;
157 
158  // Clean parameters
159  $this->amount=trim($this->amount);
160  $this->label=trim($this->label);
161  $this->note=trim($this->note);
162  $this->fk_bank=trim($this->fk_bank);
163  $this->fk_user_creat=trim($this->fk_user_creat);
164  $this->fk_user_modif=trim($this->fk_user_modif);
165 
166  // Check parameters
167  // Put here code to add control on parameters values
168 
169  $this->db->begin();
170 
171  // Update request
172  $sql = "UPDATE ".MAIN_DB_PREFIX."tva SET";
173  $sql.= " tms='".$this->db->idate($this->tms)."',";
174  $sql.= " datep='".$this->db->idate($this->datep)."',";
175  $sql.= " datev='".$this->db->idate($this->datev)."',";
176  $sql.= " amount=".price2num($this->amount).",";
177  $sql.= " label='".$this->db->escape($this->label)."',";
178  $sql.= " note='".$this->db->escape($this->note)."',";
179  $sql.= " fk_bank=".$this->fk_bank.",";
180  $sql.= " fk_user_creat=".$this->fk_user_creat.",";
181  $sql.= " fk_user_modif=".($this->fk_user_modif>0?$this->fk_user_modif:$user->id)."";
182  $sql.= " WHERE rowid=".$this->id;
183 
184  dol_syslog(get_class($this)."::update", LOG_DEBUG);
185  $resql = $this->db->query($sql);
186  if (! $resql)
187  {
188  $this->error="Error ".$this->db->lasterror();
189  $error++;
190  }
191 
192  if (! $error && ! $notrigger)
193  {
194  // Call trigger
195  $result=$this->call_trigger('TVA_MODIFY',$user);
196  if ($result < 0) $error++;
197  // End call triggers
198  }
199 
200  if (! $error)
201  {
202  $this->db->commit();
203  return 1;
204  }
205  else
206  {
207  $this->db->rollback();
208  return -1;
209  }
210  }
211 
212 
220  function fetch($id, $user=null)
221  {
222  global $langs;
223  $sql = "SELECT";
224  $sql.= " t.rowid,";
225 
226  $sql.= " t.tms,";
227  $sql.= " t.datep,";
228  $sql.= " t.datev,";
229  $sql.= " t.amount,";
230  $sql.= " t.fk_typepayment,";
231  $sql.= " t.num_payment,";
232  $sql.= " t.label,";
233  $sql.= " t.note,";
234  $sql.= " t.fk_bank,";
235  $sql.= " t.fk_user_creat,";
236  $sql.= " t.fk_user_modif,";
237  $sql.= " b.fk_account,";
238  $sql.= " b.fk_type,";
239  $sql.= " b.rappro";
240 
241  $sql.= " FROM ".MAIN_DB_PREFIX."tva as t";
242  $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."bank as b ON t.fk_bank = b.rowid";
243  $sql.= " WHERE t.rowid = ".$id;
244 
245  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
246  $resql=$this->db->query($sql);
247  if ($resql)
248  {
249  if ($this->db->num_rows($resql))
250  {
251  $obj = $this->db->fetch_object($resql);
252 
253  $this->id = $obj->rowid;
254  $this->ref = $obj->rowid;
255  $this->tms = $this->db->jdate($obj->tms);
256  $this->datep = $this->db->jdate($obj->datep);
257  $this->datev = $this->db->jdate($obj->datev);
258  $this->amount = $obj->amount;
259  $this->type_payment = $obj->fk_typepayment;
260  $this->num_payment = $obj->num_payment;
261  $this->label = $obj->label;
262  $this->note = $obj->note;
263  $this->fk_bank = $obj->fk_bank;
264  $this->fk_user_creat = $obj->fk_user_creat;
265  $this->fk_user_modif = $obj->fk_user_modif;
266  $this->fk_account = $obj->fk_account;
267  $this->fk_type = $obj->fk_type;
268  $this->rappro = $obj->rappro;
269  }
270  $this->db->free($resql);
271 
272  return 1;
273  }
274  else
275  {
276  $this->error="Error ".$this->db->lasterror();
277  return -1;
278  }
279  }
280 
281 
288  function delete($user)
289  {
290  global $conf, $langs;
291 
292  $error=0;
293 
294  // Call trigger
295  $result=$this->call_trigger('TVA_DELETE',$user);
296  if ($result < 0) return -1;
297  // End call triggers
298 
299  $sql = "DELETE FROM ".MAIN_DB_PREFIX."tva";
300  $sql.= " WHERE rowid=".$this->id;
301 
302  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
303  $resql = $this->db->query($sql);
304  if (! $resql)
305  {
306  $this->error="Error ".$this->db->lasterror();
307  return -1;
308  }
309 
310 
311  return 1;
312  }
313 
314 
322  function initAsSpecimen()
323  {
324  $this->id=0;
325 
326  $this->tms='';
327  $this->datep='';
328  $this->datev='';
329  $this->amount='';
330  $this->label='';
331  $this->note='';
332  $this->fk_bank='';
333  $this->fk_user_creat='';
334  $this->fk_user_modif='';
335  }
336 
337 
344  function solde($year = 0)
345  {
346 
347  $reglee = $this->tva_sum_reglee($year);
348 
349  $payee = $this->tva_sum_payee($year);
350  $collectee = $this->tva_sum_collectee($year);
351 
352  $solde = $reglee - ($collectee - $payee);
353 
354  return $solde;
355  }
356 
363  function tva_sum_collectee($year = 0)
364  {
365 
366  $sql = "SELECT sum(f.tva) as amount";
367  $sql .= " FROM ".MAIN_DB_PREFIX."facture as f WHERE f.paye = 1";
368  if ($year)
369  {
370  $sql .= " AND f.datef >= '".$year."-01-01' AND f.datef <= '".$year."-12-31' ";
371  }
372 
373  $result = $this->db->query($sql);
374  if ($result)
375  {
376  if ($this->db->num_rows($result))
377  {
378  $obj = $this->db->fetch_object($result);
379  $ret = $obj->amount;
380  $this->db->free($result);
381  return $ret;
382  }
383  else
384  {
385  $this->db->free($result);
386  return 0;
387  }
388  }
389  else
390  {
391  print $this->db->lasterror();
392  return -1;
393  }
394  }
395 
402  function tva_sum_payee($year = 0)
403  {
404 
405  $sql = "SELECT sum(f.total_tva) as total_tva";
406  $sql .= " FROM ".MAIN_DB_PREFIX."facture_fourn as f";
407  if ($year)
408  {
409  $sql .= " WHERE f.datef >= '".$year."-01-01' AND f.datef <= '".$year."-12-31' ";
410  }
411 
412  $result = $this->db->query($sql);
413  if ($result)
414  {
415  if ($this->db->num_rows($result))
416  {
417  $obj = $this->db->fetch_object($result);
418  $ret = $obj->total_tva;
419  $this->db->free($result);
420  return $ret;
421  }
422  else
423  {
424  $this->db->free($result);
425  return 0;
426  }
427  }
428  else
429  {
430  print $this->db->lasterror();
431  return -1;
432  }
433  }
434 
435 
442  function tva_sum_reglee($year = 0)
443  {
444 
445  $sql = "SELECT sum(f.amount) as amount";
446  $sql .= " FROM ".MAIN_DB_PREFIX."tva as f";
447 
448  if ($year)
449  {
450  $sql .= " WHERE f.datev >= '".$year."-01-01' AND f.datev <= '".$year."-12-31' ";
451  }
452 
453  $result = $this->db->query($sql);
454  if ($result)
455  {
456  if ($this->db->num_rows($result))
457  {
458  $obj = $this->db->fetch_object($result);
459  $ret = $obj->amount;
460  $this->db->free($result);
461  return $ret;
462  }
463  else
464  {
465  $this->db->free($result);
466  return 0;
467  }
468  }
469  else
470  {
471  print $this->db->lasterror();
472  return -1;
473  }
474  }
475 
476 
483  function addPayment($user)
484  {
485  global $conf,$langs;
486 
487  $this->db->begin();
488 
489  // Clean parameters
490  $this->amount=price2num(trim($this->amount));
491  $this->label=trim($this->label);
492  $this->note=trim($this->note);
493  $this->fk_bank=trim($this->fk_bank);
494  $this->fk_user_creat=trim($this->fk_user_creat);
495  $this->fk_user_modif=trim($this->fk_user_modif);
496  if (empty($this->datec)) $this->datec = dol_now();
497 
498  // Check parameters
499  if (! $this->label)
500  {
501  $this->error=$langs->trans("ErrorFieldRequired",$langs->transnoentities("Label"));
502  return -3;
503  }
504  if ($this->amount == '')
505  {
506  $this->error=$langs->trans("ErrorFieldRequired",$langs->transnoentities("Amount"));
507  return -4;
508  }
509  if (! empty($conf->banque->enabled) && (empty($this->accountid) || $this->accountid <= 0))
510  {
511  $this->error=$langs->trans("ErrorFieldRequired",$langs->transnoentities("Account"));
512  return -5;
513  }
514  if (! empty($conf->banque->enabled) && (empty($this->type_payment) || $this->type_payment <= 0))
515  {
516  $this->error=$langs->trans("ErrorFieldRequired",$langs->transnoentities("PaymentMode"));
517  return -5;
518  }
519 
520  // Insert into llx_tva
521  $sql = "INSERT INTO ".MAIN_DB_PREFIX."tva (";
522  $sql.= "datec";
523  $sql.= ", datep";
524  $sql.= ", datev";
525  $sql.= ", amount";
526  $sql.= ", fk_typepayment";
527  $sql.= ", num_payment";
528  if ($this->note) $sql.= ", note";
529  if ($this->label) $sql.= ", label";
530  $sql.= ", fk_user_creat";
531  $sql.= ", fk_bank";
532  $sql.= ", entity";
533  $sql.= ") ";
534  $sql.= " VALUES (";
535  $sql.= " '".$this->db->idate($this->datec)."'";
536  $sql.= ", '".$this->db->idate($this->datep)."'";
537  $sql.= ", '".$this->db->idate($this->datev)."'";
538  $sql.= ", ".$this->amount;
539  $sql.= ", '".$this->db->escape($this->type_payment)."'";
540  $sql.= ", '".$this->db->escape($this->num_payment)."'";
541  if ($this->note) $sql.=", '".$this->db->escape($this->note)."'";
542  if ($this->label) $sql.=", '".$this->db->escape($this->label)."'";
543  $sql.= ", '".$this->db->escape($user->id)."'";
544  $sql.= ", NULL";
545  $sql.= ", ".$conf->entity;
546  $sql.= ")";
547 
548  dol_syslog(get_class($this)."::addPayment", LOG_DEBUG);
549  $result = $this->db->query($sql);
550  if ($result)
551  {
552  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."tva"); // TODO should be called 'payment_vat'
553 
554  // Call trigger
555  //XXX: Should be done just befor commit no ?
556  $result=$this->call_trigger('TVA_ADDPAYMENT',$user);
557  if ($result < 0)
558  {
559  $this->id = 0;
560  $ok = 0;
561  }
562  // End call triggers
563 
564  if ($this->id > 0)
565  {
566  $ok=1;
567  if (! empty($conf->banque->enabled) && ! empty($this->amount))
568  {
569  // Insert into llx_bank
570  require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
571 
572  $acc = new Account($this->db);
573  $result=$acc->fetch($this->accountid);
574  if ($result <= 0) dol_print_error($this->db);
575 
576  if ($this->amount > 0) {
577  $bank_line_id = $acc->addline($this->datep, $this->type_payment, $this->label, -abs($this->amount), '', '', $user);
578  } else {
579  $bank_line_id = $acc->addline($this->datep, $this->type_payment, $this->label, abs($this->amount), '', '', $user);
580  }
581 
582  // Update fk_bank into llx_tva. So we know vat line used to generate bank transaction
583  if ($bank_line_id > 0)
584  {
585  $this->update_fk_bank($bank_line_id);
586  }
587  else
588  {
589  $this->error=$acc->error;
590  $ok=0;
591  }
592 
593  // Update links
594  $result=$acc->add_url_line($bank_line_id, $this->id, DOL_URL_ROOT.'/compta/tva/card.php?id=', "(VATPayment)", "payment_vat");
595  if ($result < 0)
596  {
597  $this->error=$acc->error;
598  $ok=0;
599  }
600  }
601 
602  if ($ok)
603  {
604  $this->db->commit();
605  return $this->id;
606  }
607  else
608  {
609  $this->db->rollback();
610  return -3;
611  }
612  }
613  else
614  {
615  $this->db->rollback();
616  return -2;
617  }
618  }
619  else
620  {
621  $this->error=$this->db->error();
622  $this->db->rollback();
623  return -1;
624  }
625  }
626 
633  function update_fk_bank($id_bank)
634  {
635  $sql = 'UPDATE '.MAIN_DB_PREFIX.'tva SET fk_bank = '.$id_bank;
636  $sql.= ' WHERE rowid = '.$this->id;
637  $result = $this->db->query($sql);
638  if ($result)
639  {
640  return 1;
641  }
642  else
643  {
644  dol_print_error($this->db);
645  return -1;
646  }
647  }
648 
656  function getNomUrl($withpicto=0,$option='')
657  {
658  global $langs;
659 
660  $result='';
661  $label=$langs->trans("ShowVatPayment").': '.$this->ref;
662 
663  $link = '<a href="'.DOL_URL_ROOT.'/compta/tva/card.php?id='.$this->id.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
664  $linkend='</a>';
665 
666  $picto='payment';
667 
668  if ($withpicto) $result.=($link.img_object($label, $picto, 'class="classfortooltip"').$linkend);
669  if ($withpicto && $withpicto != 2) $result.=' ';
670  if ($withpicto != 2) $result.=$link.$this->ref.$linkend;
671  return $result;
672  }
673 
680  function info($id)
681  {
682  $sql = "SELECT t.rowid, t.tms, t.fk_user_modif, t.datec, t.fk_user_creat";
683  $sql.= " FROM ".MAIN_DB_PREFIX."tva as t";
684  $sql.= " WHERE t.rowid = ".$id;
685 
686  dol_syslog(get_class($this)."::info", LOG_DEBUG);
687  $result=$this->db->query($sql);
688  if ($result)
689  {
690  if ($this->db->num_rows($result))
691  {
692  $obj = $this->db->fetch_object($result);
693 
694  $this->id = $obj->rowid;
695 
696  if ($obj->fk_user_creat) {
697  $cuser = new User($this->db);
698  $cuser->fetch($obj->fk_user_creat);
699  $this->user_creation = $cuser;
700  }
701 
702  if ($obj->fk_user_modif) {
703  $muser = new User($this->db);
704  $muser->fetch($obj->fk_user_modif);
705  $this->user_modification = $muser;
706  }
707 
708  $this->date_creation = $this->db->jdate($obj->datec);
709  $this->date_modification = $this->db->jdate($obj->tms);
710  $this->import_key = $obj->import_key;
711  }
712 
713  $this->db->free($result);
714 
715  }
716  else
717  {
718  dol_print_error($this->db);
719  }
720  }
721 
728  function getLibStatut($mode=0)
729  {
730  return $this->LibStatut($this->statut,$mode);
731  }
732 
740  function LibStatut($status,$mode=0)
741  {
742  global $langs; // TODO Renvoyer le libelle anglais et faire traduction a affichage
743 
744  return '';
745  }
746 }
addPayment($user)
Create in database.
Definition: tva.class.php:483
__construct($db)
Constructor.
Definition: tva.class.php:57
tva_sum_payee($year=0)
VAT payed.
Definition: tva.class.php:402
LibStatut($status, $mode=0)
Renvoi le libelle d'un statut donne.
Definition: tva.class.php:740
Class to manage Dolibarr users.
Definition: user.class.php:39
info($id)
Informations of vat payment object.
Definition: tva.class.php:680
initAsSpecimen()
Initialise an instance with random values.
Definition: tva.class.php:322
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...
tva_sum_collectee($year=0)
Total of the VAT from invoices emitted by the thirdparty.
Definition: tva.class.php:363
Class to manage bank accounts.
Put here description of your class.
Definition: tva.class.php:33
solde($year=0)
Balance of VAT.
Definition: tva.class.php:344
update($user=null, $notrigger=0)
Update database.
Definition: tva.class.php:152
getLibStatut($mode=0)
Retourne le libelle du statut d'une facture (brouillon, validee, abandonnee, payee) ...
Definition: tva.class.php:728
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='')
Write log message into outputs.
fetch($id, $user=null)
Load object in memory from database.
Definition: tva.class.php:220
dol_now($mode='gmt')
Return date for now.
create($user)
Create in database.
Definition: tva.class.php:72
print
Draft customers invoices.
Definition: index.php:91
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
tva_sum_reglee($year=0)
Total of the VAT payed.
Definition: tva.class.php:442
update_fk_bank($id_bank)
Update link between payment tva and line generate into llx_bank.
Definition: tva.class.php:633
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 '...
getNomUrl($withpicto=0, $option='')
Send name clicable (with possibly the picto)
Definition: tva.class.php:656
Parent class of all other business classes (invoices, contracts, proposals, orders, ...)