dolibarr  7.0.0-beta
localtax.class.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2011-2014 Juanjo Menent <jmenent@2byte.es>
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 require_once DOL_DOCUMENT_ROOT .'/core/class/commonobject.class.php';
25 
26 
30 class Localtax extends CommonObject
31 {
32  var $ltt;
33  var $tms;
34  var $datep;
35  var $datev;
36  var $amount;
37  var $label;
38  var $fk_bank;
39  var $fk_user_creat;
40  var $fk_user_modif;
41 
47  function __construct($db)
48  {
49  $this->db = $db;
50  }
51 
52 
59  function create($user)
60  {
61  global $conf, $langs;
62 
63  $error=0;
64 
65  // Clean parameters
66  $this->amount=trim($this->amount);
67  $this->label=trim($this->label);
68  $this->note=trim($this->note);
69  $this->fk_bank=trim($this->fk_bank);
70  $this->fk_user_creat=trim($this->fk_user_creat);
71  $this->fk_user_modif=trim($this->fk_user_modif);
72 
73  // Insert request
74  $sql = "INSERT INTO ".MAIN_DB_PREFIX."localtax(";
75  $sql.= "localtaxtype,";
76  $sql.= "tms,";
77  $sql.= "datep,";
78  $sql.= "datev,";
79  $sql.= "amount,";
80  $sql.= "label,";
81  $sql.= "note,";
82  $sql.= "fk_bank,";
83  $sql.= "fk_user_creat,";
84  $sql.= "fk_user_modif";
85  $sql.= ") VALUES (";
86  $sql.= " ".$this->ltt.",";
87  $sql.= " '".$this->db->idate($this->tms)."',";
88  $sql.= " '".$this->db->idate($this->datep)."',";
89  $sql.= " '".$this->db->idate($this->datev)."',";
90  $sql.= " '".$this->db->escape($this->amount)."',";
91  $sql.= " '".$this->db->escape($this->label)."',";
92  $sql.= " '".$this->db->escape($this->note)."',";
93  $sql.= " ".($this->fk_bank <= 0 ? "NULL" : "'".$this->db->escape($this->fk_bank)."'").",";
94  $sql.= " '".$this->db->escape($this->fk_user_creat)."',";
95  $sql.= " '".$this->db->escape($this->fk_user_modif)."'";
96  $sql.= ")";
97 
98  dol_syslog(get_class($this)."::create", LOG_DEBUG);
99  $resql=$this->db->query($sql);
100  if ($resql)
101  {
102  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."localtax");
103 
104  // Call trigger
105  $result=$this->call_trigger('LOCALTAX_CREATE',$user);
106  if ($result < 0) $error++;
107  // End call triggers
108 
109  if (! $error)
110  {
111  $this->db->commit();
112  return $this->id;
113  }
114  else
115  {
116  $this->db->rollback();
117  return -1;
118  }
119  }
120  else
121  {
122  $this->error="Error ".$this->db->lasterror();
123  $this->db->rollback();
124  return -1;
125  }
126  }
127 
135  function update($user=null, $notrigger=0)
136  {
137  global $conf, $langs;
138 
139  $error=0;
140 
141  // Clean parameters
142  $this->amount=trim($this->amount);
143  $this->label=trim($this->label);
144  $this->note=trim($this->note);
145  $this->fk_bank=trim($this->fk_bank);
146  $this->fk_user_creat=trim($this->fk_user_creat);
147  $this->fk_user_modif=trim($this->fk_user_modif);
148 
149  $this->db->begin();
150 
151  // Update request
152  $sql = "UPDATE ".MAIN_DB_PREFIX."localtax SET";
153  $sql.= " localtaxtype=".$this->ltt.",";
154  $sql.= " tms='".$this->db->idate($this->tms)."',";
155  $sql.= " datep='".$this->db->idate($this->datep)."',";
156  $sql.= " datev='".$this->db->idate($this->datev)."',";
157  $sql.= " amount=".price2num($this->amount).",";
158  $sql.= " label='".$this->db->escape($this->label)."',";
159  $sql.= " note='".$this->db->escape($this->note)."',";
160  $sql.= " fk_bank=".$this->fk_bank.",";
161  $sql.= " fk_user_creat=".$this->fk_user_creat.",";
162  $sql.= " fk_user_modif=".$this->fk_user_modif;
163  $sql.= " WHERE rowid=".$this->id;
164 
165  dol_syslog(get_class($this)."::update", LOG_DEBUG);
166  $resql = $this->db->query($sql);
167  if (! $resql)
168  {
169  $this->error="Error ".$this->db->lasterror();
170  $error++;
171  }
172 
173  if (! $error && ! $notrigger)
174  {
175  // Call trigger
176  $result=$this->call_trigger('LOCALTAX_MODIFY',$user);
177  if ($result < 0) $error++;
178  // End call triggers
179  }
180 
181  if (! $error)
182  {
183  $this->db->commit();
184  return 1;
185  }
186  else
187  {
188  $this->db->rollback();
189  return -1;
190  }
191  }
192 
193 
200  function fetch($id)
201  {
202  global $langs;
203  $sql = "SELECT";
204  $sql.= " t.rowid,";
205  $sql.= " t.localtaxtype,";
206  $sql.= " t.tms,";
207  $sql.= " t.datep,";
208  $sql.= " t.datev,";
209  $sql.= " t.amount,";
210  $sql.= " t.label,";
211  $sql.= " t.note,";
212  $sql.= " t.fk_bank,";
213  $sql.= " t.fk_user_creat,";
214  $sql.= " t.fk_user_modif,";
215  $sql.= " b.fk_account,";
216  $sql.= " b.fk_type,";
217  $sql.= " b.rappro";
218  $sql.= " FROM ".MAIN_DB_PREFIX."localtax as t";
219  $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."bank as b ON t.fk_bank = b.rowid";
220  $sql.= " WHERE t.rowid = ".$id;
221 
222  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
223  $resql=$this->db->query($sql);
224  if ($resql)
225  {
226  if ($this->db->num_rows($resql))
227  {
228  $obj = $this->db->fetch_object($resql);
229 
230  $this->id = $obj->rowid;
231  $this->ref = $obj->rowid;
232  $this->ltt = $obj->localtaxtype;
233  $this->tms = $this->db->jdate($obj->tms);
234  $this->datep = $this->db->jdate($obj->datep);
235  $this->datev = $this->db->jdate($obj->datev);
236  $this->amount = $obj->amount;
237  $this->label = $obj->label;
238  $this->note = $obj->note;
239  $this->fk_bank = $obj->fk_bank;
240  $this->fk_user_creat = $obj->fk_user_creat;
241  $this->fk_user_modif = $obj->fk_user_modif;
242  $this->fk_account = $obj->fk_account;
243  $this->fk_type = $obj->fk_type;
244  $this->rappro = $obj->rappro;
245  }
246  $this->db->free($resql);
247 
248  return 1;
249  }
250  else
251  {
252  $this->error="Error ".$this->db->lasterror();
253  return -1;
254  }
255  }
256 
257 
264  function delete($user)
265  {
266  // Call trigger
267  $result=$this->call_trigger('LOCALTAX_DELETE',$user);
268  if ($result < 0) return -1;
269  // End call triggers
270 
271 
272  $sql = "DELETE FROM ".MAIN_DB_PREFIX."localtax";
273  $sql.= " WHERE rowid=".$this->id;
274 
275  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
276  $resql = $this->db->query($sql);
277  if (! $resql)
278  {
279  $this->error="Error ".$this->db->lasterror();
280  return -1;
281  }
282 
283  return 1;
284  }
285 
286 
294  function initAsSpecimen()
295  {
296  $this->id=0;
297 
298  $this->tms='';
299  $this->ltt=0;
300  $this->datep='';
301  $this->datev='';
302  $this->amount='';
303  $this->label='';
304  $this->note='';
305  $this->fk_bank='';
306  $this->fk_user_creat='';
307  $this->fk_user_modif='';
308  }
309 
310 
317  function solde($year = 0)
318  {
319 
320  $reglee = $this->localtax_sum_reglee($year);
321 
322  $payee = $this->localtax_sum_payee($year);
323  $collectee = $this->localtax_sum_collectee($year);
324 
325  $solde = $reglee - ($collectee - $payee);
326 
327  return $solde;
328  }
329 
336  function localtax_sum_collectee($year = 0)
337  {
338  $sql = "SELECT sum(f.localtax) as amount";
339  $sql .= " FROM ".MAIN_DB_PREFIX."facture as f WHERE f.paye = 1";
340  if ($year)
341  {
342  $sql .= " AND f.datef >= '$year-01-01' AND f.datef <= '$year-12-31' ";
343  }
344 
345  $result = $this->db->query($sql);
346  if ($result)
347  {
348  if ($this->db->num_rows($result))
349  {
350  $obj = $this->db->fetch_object($result);
351  $ret = $obj->amount;
352  $this->db->free($result);
353  return $ret;
354  }
355  else
356  {
357  $this->db->free($result);
358  return 0;
359  }
360  }
361  else
362  {
363  print $this->db->lasterror();
364  return -1;
365  }
366  }
367 
374  function localtax_sum_payee($year = 0)
375  {
376 
377  $sql = "SELECT sum(f.total_localtax) as total_localtax";
378  $sql .= " FROM ".MAIN_DB_PREFIX."facture_fourn as f";
379  if ($year)
380  {
381  $sql .= " WHERE f.datef >= '$year-01-01' AND f.datef <= '$year-12-31' ";
382  }
383 
384  $result = $this->db->query($sql);
385  if ($result)
386  {
387  if ($this->db->num_rows($result))
388  {
389  $obj = $this->db->fetch_object($result);
390  $ret = $obj->total_localtax;
391  $this->db->free($result);
392  return $ret;
393  }
394  else
395  {
396  $this->db->free($result);
397  return 0;
398  }
399  }
400  else
401  {
402  print $this->db->lasterror();
403  return -1;
404  }
405  }
406 
407 
415  function localtax_sum_reglee($year = 0)
416  {
417 
418  $sql = "SELECT sum(f.amount) as amount";
419  $sql .= " FROM ".MAIN_DB_PREFIX."localtax as f";
420  if ($year)
421  {
422  $sql .= " WHERE f.datev >= '$year-01-01' AND f.datev <= '$year-12-31' ";
423  }
424 
425  $result = $this->db->query($sql);
426  if ($result)
427  {
428  if ($this->db->num_rows($result))
429  {
430  $obj = $this->db->fetch_object($result);
431  $ret = $obj->amount;
432  $this->db->free($result);
433  return $ret;
434  }
435  else
436  {
437  $this->db->free($result);
438  return 0;
439  }
440  }
441  else
442  {
443  print $this->db->lasterror();
444  return -1;
445  }
446  }
447 
448 
455  function addPayment($user)
456  {
457  global $conf,$langs;
458 
459  $this->db->begin();
460 
461  // Check parameters
462  $this->amount=price2num($this->amount);
463  if (! $this->label)
464  {
465  $this->error=$langs->trans("ErrorFieldRequired",$langs->transnoentities("Label"));
466  return -3;
467  }
468  if ($this->amount <= 0)
469  {
470  $this->error=$langs->trans("ErrorFieldRequired",$langs->transnoentities("Amount"));
471  return -4;
472  }
473  if (! empty($conf->banque->enabled) && (empty($this->accountid) || $this->accountid <= 0))
474  {
475  $this->error=$langs->trans("ErrorFieldRequired",$langs->transnoentities("Account"));
476  return -5;
477  }
478  if (! empty($conf->banque->enabled) && (empty($this->paymenttype) || $this->paymenttype <= 0))
479  {
480  $this->error=$langs->trans("ErrorFieldRequired",$langs->transnoentities("PaymentMode"));
481  return -5;
482  }
483 
484  // Insertion dans table des paiement localtax
485  $sql = "INSERT INTO ".MAIN_DB_PREFIX."localtax (localtaxtype, datep, datev, amount";
486  if ($this->note) $sql.=", note";
487  if ($this->label) $sql.=", label";
488  $sql.= ", fk_user_creat, fk_bank";
489  $sql.= ") ";
490  $sql.= " VALUES (".$this->ltt.", '".$this->db->idate($this->datep)."',";
491  $sql.= "'".$this->db->idate($this->datev)."'," . $this->amount;
492  if ($this->note) $sql.=", '".$this->db->escape($this->note)."'";
493  if ($this->label) $sql.=", '".$this->db->escape($this->label)."'";
494  $sql.=", '".$user->id."', NULL";
495  $sql.= ")";
496 
497  dol_syslog(get_class($this)."::addPayment", LOG_DEBUG);
498  $result = $this->db->query($sql);
499  if ($result)
500  {
501  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."localtax"); // TODO devrait s'appeler paiementlocaltax
502  if ($this->id > 0)
503  {
504  $ok=1;
505  if (! empty($conf->banque->enabled))
506  {
507  // Insertion dans llx_bank
508  require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
509 
510  $acc = new Account($this->db);
511  $result=$acc->fetch($this->accountid);
512  if ($result <= 0) dol_print_error($this->db);
513 
514  $bank_line_id = $acc->addline($this->datep, $this->paymenttype, $this->label, -abs($this->amount), '', '', $user);
515 
516  // Mise a jour fk_bank dans llx_localtax. On connait ainsi la ligne de localtax qui a g�n�r� l'�criture bancaire
517  if ($bank_line_id > 0)
518  {
519  $this->update_fk_bank($bank_line_id);
520  }
521  else
522  {
523  $this->error=$acc->error;
524  $ok=0;
525  }
526 
527  // Mise a jour liens
528  $result=$acc->add_url_line($bank_line_id, $this->id, DOL_URL_ROOT.'/compta/localtax/card.php?id=', "(VATPayment)", "payment_vat");
529  if ($result < 0)
530  {
531  $this->error=$acc->error;
532  $ok=0;
533  }
534  }
535 
536  if ($ok)
537  {
538  $this->db->commit();
539  return $this->id;
540  }
541  else
542  {
543  $this->db->rollback();
544  return -3;
545  }
546  }
547  else
548  {
549  $this->error=$this->db->lasterror();
550  $this->db->rollback();
551  return -2;
552  }
553  }
554  else
555  {
556  $this->error=$this->db->lasterror();
557  $this->db->rollback();
558  return -1;
559  }
560  }
561 
568  function update_fk_bank($id)
569  {
570  $sql = 'UPDATE '.MAIN_DB_PREFIX.'localtax SET fk_bank = '.$id;
571  $sql.= ' WHERE rowid = '.$this->id;
572  $result = $this->db->query($sql);
573  if ($result)
574  {
575  return 1;
576  }
577  else
578  {
579  dol_print_error($this->db);
580  return -1;
581  }
582  }
583 
584 
592  function getNomUrl($withpicto=0, $option='')
593  {
594  global $langs;
595 
596  $result='';
597  $label=$langs->trans("ShowVatPayment").': '.$this->ref;
598 
599  $link = '<a href="'.DOL_URL_ROOT.'/compta/localtax/card.php?id='.$this->id.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
600  $linkend='</a>';
601 
602  $picto='payment';
603 
604  if ($withpicto) $result.=($link.img_object($label, $picto, 'class="classfortooltip"').$linkend);
605  if ($withpicto && $withpicto != 2) $result.=' ';
606  if ($withpicto != 2) $result.=$link.$this->ref.$linkend;
607  return $result;
608  }
609 
610 }
__construct($db)
Constructor.
update($user=null, $notrigger=0)
Update database.
update_fk_bank($id)
Update the link betwen localtax payment and the line into llx_bank.
initAsSpecimen()
Initialise an instance with random values.
localtax_sum_reglee($year=0)
localtax payed Total de la localtax payed
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.
fetch($id)
Load object in memory from database.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='')
Write log message into outputs.
localtax_sum_collectee($year=0)
Total de la localtax des factures emises par la societe.
getNomUrl($withpicto=0, $option='')
Returns clickable name.
localtax_sum_payee($year=0)
localtax payed
create($user)
Create in database.
addPayment($user)
Add a payment of localtax.
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
solde($year=0)
Hum la fonction s'appelle 'Solde' elle doit a mon avis calcluer le solde de localtax, non ?
Class to manage local tax.
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, ...)