dolibarr  9.0.0
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 {
35  public $element='localtax';
36 
40  public $table_element='localtax';
41 
45  public $picto='payment';
46 
47  public $ltt;
48  public $tms;
49  public $datep;
50  public $datev;
51  public $amount;
52 
56  public $label;
57 
61  public $fk_bank;
62 
66  public $fk_user_creat;
67 
71  public $fk_user_modif;
72 
78  function __construct($db)
79  {
80  $this->db = $db;
81  }
82 
83 
90  function create($user)
91  {
92  global $conf, $langs;
93 
94  $error=0;
95 
96  // Clean parameters
97  $this->amount=trim($this->amount);
98  $this->label=trim($this->label);
99  $this->note=trim($this->note);
100  $this->fk_bank=trim($this->fk_bank);
101  $this->fk_user_creat=trim($this->fk_user_creat);
102  $this->fk_user_modif=trim($this->fk_user_modif);
103 
104  // Insert request
105  $sql = "INSERT INTO ".MAIN_DB_PREFIX."localtax(";
106  $sql.= "localtaxtype,";
107  $sql.= "tms,";
108  $sql.= "datep,";
109  $sql.= "datev,";
110  $sql.= "amount,";
111  $sql.= "label,";
112  $sql.= "note,";
113  $sql.= "fk_bank,";
114  $sql.= "fk_user_creat,";
115  $sql.= "fk_user_modif";
116  $sql.= ") VALUES (";
117  $sql.= " ".$this->ltt.",";
118  $sql.= " '".$this->db->idate($this->tms)."',";
119  $sql.= " '".$this->db->idate($this->datep)."',";
120  $sql.= " '".$this->db->idate($this->datev)."',";
121  $sql.= " '".$this->db->escape($this->amount)."',";
122  $sql.= " '".$this->db->escape($this->label)."',";
123  $sql.= " '".$this->db->escape($this->note)."',";
124  $sql.= " ".($this->fk_bank <= 0 ? "NULL" : "'".$this->db->escape($this->fk_bank)."'").",";
125  $sql.= " '".$this->db->escape($this->fk_user_creat)."',";
126  $sql.= " '".$this->db->escape($this->fk_user_modif)."'";
127  $sql.= ")";
128 
129  dol_syslog(get_class($this)."::create", LOG_DEBUG);
130  $resql=$this->db->query($sql);
131  if ($resql)
132  {
133  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."localtax");
134 
135  // Call trigger
136  $result=$this->call_trigger('LOCALTAX_CREATE',$user);
137  if ($result < 0) $error++;
138  // End call triggers
139 
140  if (! $error)
141  {
142  $this->db->commit();
143  return $this->id;
144  }
145  else
146  {
147  $this->db->rollback();
148  return -1;
149  }
150  }
151  else
152  {
153  $this->error="Error ".$this->db->lasterror();
154  $this->db->rollback();
155  return -1;
156  }
157  }
158 
166  function update(User $user, $notrigger=0)
167  {
168  global $conf, $langs;
169 
170  $error=0;
171 
172  // Clean parameters
173  $this->amount=trim($this->amount);
174  $this->label=trim($this->label);
175  $this->note=trim($this->note);
176  $this->fk_bank=trim($this->fk_bank);
177  $this->fk_user_creat=trim($this->fk_user_creat);
178  $this->fk_user_modif=trim($this->fk_user_modif);
179 
180  $this->db->begin();
181 
182  // Update request
183  $sql = "UPDATE ".MAIN_DB_PREFIX."localtax SET";
184  $sql.= " localtaxtype=".$this->ltt.",";
185  $sql.= " tms='".$this->db->idate($this->tms)."',";
186  $sql.= " datep='".$this->db->idate($this->datep)."',";
187  $sql.= " datev='".$this->db->idate($this->datev)."',";
188  $sql.= " amount=".price2num($this->amount).",";
189  $sql.= " label='".$this->db->escape($this->label)."',";
190  $sql.= " note='".$this->db->escape($this->note)."',";
191  $sql.= " fk_bank=".$this->fk_bank.",";
192  $sql.= " fk_user_creat=".$this->fk_user_creat.",";
193  $sql.= " fk_user_modif=".$this->fk_user_modif;
194  $sql.= " WHERE rowid=".$this->id;
195 
196  dol_syslog(get_class($this)."::update", LOG_DEBUG);
197  $resql = $this->db->query($sql);
198  if (! $resql)
199  {
200  $this->error="Error ".$this->db->lasterror();
201  $error++;
202  }
203 
204  if (! $error && ! $notrigger)
205  {
206  // Call trigger
207  $result=$this->call_trigger('LOCALTAX_MODIFY',$user);
208  if ($result < 0) $error++;
209  // End call triggers
210  }
211 
212  if (! $error)
213  {
214  $this->db->commit();
215  return 1;
216  }
217  else
218  {
219  $this->db->rollback();
220  return -1;
221  }
222  }
223 
224 
231  function fetch($id)
232  {
233  global $langs;
234  $sql = "SELECT";
235  $sql.= " t.rowid,";
236  $sql.= " t.localtaxtype,";
237  $sql.= " t.tms,";
238  $sql.= " t.datep,";
239  $sql.= " t.datev,";
240  $sql.= " t.amount,";
241  $sql.= " t.label,";
242  $sql.= " t.note,";
243  $sql.= " t.fk_bank,";
244  $sql.= " t.fk_user_creat,";
245  $sql.= " t.fk_user_modif,";
246  $sql.= " b.fk_account,";
247  $sql.= " b.fk_type,";
248  $sql.= " b.rappro";
249  $sql.= " FROM ".MAIN_DB_PREFIX."localtax as t";
250  $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."bank as b ON t.fk_bank = b.rowid";
251  $sql.= " WHERE t.rowid = ".$id;
252 
253  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
254  $resql=$this->db->query($sql);
255  if ($resql)
256  {
257  if ($this->db->num_rows($resql))
258  {
259  $obj = $this->db->fetch_object($resql);
260 
261  $this->id = $obj->rowid;
262  $this->ref = $obj->rowid;
263  $this->ltt = $obj->localtaxtype;
264  $this->tms = $this->db->jdate($obj->tms);
265  $this->datep = $this->db->jdate($obj->datep);
266  $this->datev = $this->db->jdate($obj->datev);
267  $this->amount = $obj->amount;
268  $this->label = $obj->label;
269  $this->note = $obj->note;
270  $this->fk_bank = $obj->fk_bank;
271  $this->fk_user_creat = $obj->fk_user_creat;
272  $this->fk_user_modif = $obj->fk_user_modif;
273  $this->fk_account = $obj->fk_account;
274  $this->fk_type = $obj->fk_type;
275  $this->rappro = $obj->rappro;
276  }
277  $this->db->free($resql);
278 
279  return 1;
280  }
281  else
282  {
283  $this->error="Error ".$this->db->lasterror();
284  return -1;
285  }
286  }
287 
288 
295  function delete($user)
296  {
297  // Call trigger
298  $result=$this->call_trigger('LOCALTAX_DELETE',$user);
299  if ($result < 0) return -1;
300  // End call triggers
301 
302 
303  $sql = "DELETE FROM ".MAIN_DB_PREFIX."localtax";
304  $sql.= " WHERE rowid=".$this->id;
305 
306  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
307  $resql = $this->db->query($sql);
308  if (! $resql)
309  {
310  $this->error="Error ".$this->db->lasterror();
311  return -1;
312  }
313 
314  return 1;
315  }
316 
317 
325  function initAsSpecimen()
326  {
327  $this->id=0;
328 
329  $this->tms='';
330  $this->ltt=0;
331  $this->datep='';
332  $this->datev='';
333  $this->amount='';
334  $this->label='';
335  $this->note='';
336  $this->fk_bank='';
337  $this->fk_user_creat='';
338  $this->fk_user_modif='';
339  }
340 
341 
348  function solde($year = 0)
349  {
350 
351  $reglee = $this->localtax_sum_reglee($year);
352 
353  $payee = $this->localtax_sum_payee($year);
354  $collectee = $this->localtax_sum_collectee($year);
355 
356  $solde = $reglee - ($collectee - $payee);
357 
358  return $solde;
359  }
360 
361  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
368  function localtax_sum_collectee($year = 0)
369  {
370  // phpcs:enable
371  $sql = "SELECT sum(f.localtax) as amount";
372  $sql .= " FROM ".MAIN_DB_PREFIX."facture as f WHERE f.paye = 1";
373  if ($year)
374  {
375  $sql .= " AND f.datef >= '$year-01-01' AND f.datef <= '$year-12-31' ";
376  }
377 
378  $result = $this->db->query($sql);
379  if ($result)
380  {
381  if ($this->db->num_rows($result))
382  {
383  $obj = $this->db->fetch_object($result);
384  $ret = $obj->amount;
385  $this->db->free($result);
386  return $ret;
387  }
388  else
389  {
390  $this->db->free($result);
391  return 0;
392  }
393  }
394  else
395  {
396  print $this->db->lasterror();
397  return -1;
398  }
399  }
400 
401  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
408  function localtax_sum_payee($year = 0)
409  {
410  // phpcs:enable
411 
412  $sql = "SELECT sum(f.total_localtax) as total_localtax";
413  $sql .= " FROM ".MAIN_DB_PREFIX."facture_fourn as f";
414  if ($year)
415  {
416  $sql .= " WHERE f.datef >= '$year-01-01' AND f.datef <= '$year-12-31' ";
417  }
418 
419  $result = $this->db->query($sql);
420  if ($result)
421  {
422  if ($this->db->num_rows($result))
423  {
424  $obj = $this->db->fetch_object($result);
425  $ret = $obj->total_localtax;
426  $this->db->free($result);
427  return $ret;
428  }
429  else
430  {
431  $this->db->free($result);
432  return 0;
433  }
434  }
435  else
436  {
437  print $this->db->lasterror();
438  return -1;
439  }
440  }
441 
442 
443  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
451  function localtax_sum_reglee($year = 0)
452  {
453  // phpcs:enable
454 
455  $sql = "SELECT sum(f.amount) as amount";
456  $sql .= " FROM ".MAIN_DB_PREFIX."localtax as f";
457  if ($year)
458  {
459  $sql .= " WHERE f.datev >= '$year-01-01' AND f.datev <= '$year-12-31' ";
460  }
461 
462  $result = $this->db->query($sql);
463  if ($result)
464  {
465  if ($this->db->num_rows($result))
466  {
467  $obj = $this->db->fetch_object($result);
468  $ret = $obj->amount;
469  $this->db->free($result);
470  return $ret;
471  }
472  else
473  {
474  $this->db->free($result);
475  return 0;
476  }
477  }
478  else
479  {
480  print $this->db->lasterror();
481  return -1;
482  }
483  }
484 
485 
492  function addPayment($user)
493  {
494  global $conf,$langs;
495 
496  $this->db->begin();
497 
498  // Check parameters
499  $this->amount=price2num($this->amount);
500  if (! $this->label)
501  {
502  $this->error=$langs->trans("ErrorFieldRequired",$langs->transnoentities("Label"));
503  return -3;
504  }
505  if ($this->amount <= 0)
506  {
507  $this->error=$langs->trans("ErrorFieldRequired",$langs->transnoentities("Amount"));
508  return -4;
509  }
510  if (! empty($conf->banque->enabled) && (empty($this->accountid) || $this->accountid <= 0))
511  {
512  $this->error=$langs->trans("ErrorFieldRequired",$langs->transnoentities("Account"));
513  return -5;
514  }
515  if (! empty($conf->banque->enabled) && (empty($this->paymenttype) || $this->paymenttype <= 0))
516  {
517  $this->error=$langs->trans("ErrorFieldRequired",$langs->transnoentities("PaymentMode"));
518  return -5;
519  }
520 
521  // Insertion dans table des paiement localtax
522  $sql = "INSERT INTO ".MAIN_DB_PREFIX."localtax (localtaxtype, datep, datev, amount";
523  if ($this->note) $sql.=", note";
524  if ($this->label) $sql.=", label";
525  $sql.= ", fk_user_creat, fk_bank";
526  $sql.= ") ";
527  $sql.= " VALUES (".$this->ltt.", '".$this->db->idate($this->datep)."',";
528  $sql.= "'".$this->db->idate($this->datev)."'," . $this->amount;
529  if ($this->note) $sql.=", '".$this->db->escape($this->note)."'";
530  if ($this->label) $sql.=", '".$this->db->escape($this->label)."'";
531  $sql.=", '".$user->id."', NULL";
532  $sql.= ")";
533 
534  dol_syslog(get_class($this)."::addPayment", LOG_DEBUG);
535  $result = $this->db->query($sql);
536  if ($result)
537  {
538  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."localtax"); // TODO devrait s'appeler paiementlocaltax
539  if ($this->id > 0)
540  {
541  $ok=1;
542  if (! empty($conf->banque->enabled))
543  {
544  // Insertion dans llx_bank
545  require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
546 
547  $acc = new Account($this->db);
548  $result=$acc->fetch($this->accountid);
549  if ($result <= 0) dol_print_error($this->db);
550 
551  $bank_line_id = $acc->addline($this->datep, $this->paymenttype, $this->label, -abs($this->amount), '', '', $user);
552 
553  // Mise a jour fk_bank dans llx_localtax. On connait ainsi la ligne de localtax qui a g�n�r� l'�criture bancaire
554  if ($bank_line_id > 0)
555  {
556  $this->update_fk_bank($bank_line_id);
557  }
558  else
559  {
560  $this->error=$acc->error;
561  $ok=0;
562  }
563 
564  // Mise a jour liens
565  $result=$acc->add_url_line($bank_line_id, $this->id, DOL_URL_ROOT.'/compta/localtax/card.php?id=', "(VATPayment)", "payment_vat");
566  if ($result < 0)
567  {
568  $this->error=$acc->error;
569  $ok=0;
570  }
571  }
572 
573  if ($ok)
574  {
575  $this->db->commit();
576  return $this->id;
577  }
578  else
579  {
580  $this->db->rollback();
581  return -3;
582  }
583  }
584  else
585  {
586  $this->error=$this->db->lasterror();
587  $this->db->rollback();
588  return -2;
589  }
590  }
591  else
592  {
593  $this->error=$this->db->lasterror();
594  $this->db->rollback();
595  return -1;
596  }
597  }
598 
599  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
606  function update_fk_bank($id)
607  {
608  // phpcs:enable
609  $sql = 'UPDATE '.MAIN_DB_PREFIX.'localtax SET fk_bank = '.$id;
610  $sql.= ' WHERE rowid = '.$this->id;
611  $result = $this->db->query($sql);
612  if ($result)
613  {
614  return 1;
615  }
616  else
617  {
618  dol_print_error($this->db);
619  return -1;
620  }
621  }
622 
623 
631  function getNomUrl($withpicto=0, $option='')
632  {
633  global $langs;
634 
635  $result='';
636  $label=$langs->trans("ShowVatPayment").': '.$this->ref;
637 
638  $link = '<a href="'.DOL_URL_ROOT.'/compta/localtax/card.php?id='.$this->id.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
639  $linkend='</a>';
640 
641  $picto='payment';
642 
643  if ($withpicto) $result.=($link.img_object($label, $picto, 'class="classfortooltip"').$linkend);
644  if ($withpicto && $withpicto != 2) $result.=' ';
645  if ($withpicto != 2) $result.=$link.$this->ref.$linkend;
646  return $result;
647  }
648 
655  function getLibStatut($mode=0)
656  {
657  return $this->LibStatut($this->statut,$mode);
658  }
659 
660  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
668  function LibStatut($status, $mode=0)
669  {
670  // phpcs:enable
671  global $langs; // TODO Renvoyer le libelle anglais et faire traduction a affichage
672 
673  return '';
674  }
675 }
print $object label
hash of file content (md5_file(dol_osencode($destfull))
Definition: edit.php:153
__construct($db)
Constructor.
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:1053
update(User $user, $notrigger=0)
Update database.
if(! empty($search_group)) natural_search(array("g.nom" g note
Definition: list.php:123
update_fk_bank($id)
Update the link betwen localtax payment and the line into llx_bank.
Class to manage Dolibarr users.
Definition: user.class.php:41
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.
LibStatut($status, $mode=0)
Renvoi le libelle d&#39;un statut donne.
solde($year=0)
Hum la fonction s&#39;appelle &#39;Solde&#39; elle doit a mon avis calcluer le solde de localtax, non ?
Class to manage local tax.
getLibStatut($mode=0)
Retourne le libelle du statut d&#39;une facture (brouillon, validee, abandonnee, payee) ...
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 &#39;...
Parent class of all other business classes (invoices, contracts, proposals, orders, ...)