dolibarr  13.0.2
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 <https://www.gnu.org/licenses/>.
16  */
17 
23 require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
24 
25 
29 class Localtax extends CommonObject
30 {
34  public $element = 'localtax';
35 
39  public $table_element = 'localtax';
40 
44  public $picto = 'payment';
45 
46  public $ltt;
47  public $tms;
48  public $datep;
49  public $datev;
50  public $amount;
51 
55  public $label;
56 
60  public $fk_bank;
61 
65  public $fk_user_creat;
66 
70  public $fk_user_modif;
71 
77  public function __construct($db)
78  {
79  $this->db = $db;
80  }
81 
82 
89  public function create($user)
90  {
91  global $conf, $langs;
92 
93  $error = 0;
94 
95  // Clean parameters
96  $this->amount = trim($this->amount);
97  $this->label = trim($this->label);
98  $this->note = trim($this->note);
99 
100  // Insert request
101  $sql = "INSERT INTO ".MAIN_DB_PREFIX."localtax(";
102  $sql .= "localtaxtype,";
103  $sql .= "tms,";
104  $sql .= "datep,";
105  $sql .= "datev,";
106  $sql .= "amount,";
107  $sql .= "label,";
108  $sql .= "note,";
109  $sql .= "fk_bank,";
110  $sql .= "fk_user_creat,";
111  $sql .= "fk_user_modif";
112  $sql .= ") VALUES (";
113  $sql .= " ".$this->ltt.",";
114  $sql .= " '".$this->db->idate($this->tms)."',";
115  $sql .= " '".$this->db->idate($this->datep)."',";
116  $sql .= " '".$this->db->idate($this->datev)."',";
117  $sql .= " '".$this->db->escape($this->amount)."',";
118  $sql .= " '".$this->db->escape($this->label)."',";
119  $sql .= " '".$this->db->escape($this->note)."',";
120  $sql .= " ".($this->fk_bank <= 0 ? "NULL" : (int) $this->fk_bank).",";
121  $sql .= " ".((int) $this->fk_user_creat).",";
122  $sql .= " ".((int) $this->fk_user_modif);
123  $sql .= ")";
124 
125  dol_syslog(get_class($this)."::create", LOG_DEBUG);
126  $resql = $this->db->query($sql);
127  if ($resql)
128  {
129  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."localtax");
130 
131  // Call trigger
132  $result = $this->call_trigger('LOCALTAX_CREATE', $user);
133  if ($result < 0) $error++;
134  // End call triggers
135 
136  if (!$error)
137  {
138  $this->db->commit();
139  return $this->id;
140  } else {
141  $this->db->rollback();
142  return -1;
143  }
144  } else {
145  $this->error = "Error ".$this->db->lasterror();
146  $this->db->rollback();
147  return -1;
148  }
149  }
150 
158  public function update(User $user, $notrigger = 0)
159  {
160  global $conf, $langs;
161 
162  $error = 0;
163 
164  // Clean parameters
165  $this->amount = trim($this->amount);
166  $this->label = trim($this->label);
167  $this->note = trim($this->note);
168 
169  $this->db->begin();
170 
171  // Update request
172  $sql = "UPDATE ".MAIN_DB_PREFIX."localtax SET";
173  $sql .= " localtaxtype=".$this->ltt.",";
174  $sql .= " tms='".$this->db->idate($this->tms)."',";
175  $sql .= " datep='".$this->db->idate($this->datep)."',";
176  $sql .= " datev='".$this->db->idate($this->datev)."',";
177  $sql .= " amount=".price2num($this->amount).",";
178  $sql .= " label='".$this->db->escape($this->label)."',";
179  $sql .= " note='".$this->db->escape($this->note)."',";
180  $sql .= " fk_bank=".(int) $this->fk_bank.",";
181  $sql .= " fk_user_creat=".(int) $this->fk_user_creat.",";
182  $sql .= " fk_user_modif=".(int) $this->fk_user_modif;
183  $sql .= " WHERE rowid=".$this->id;
184 
185  dol_syslog(get_class($this)."::update", LOG_DEBUG);
186  $resql = $this->db->query($sql);
187  if (!$resql)
188  {
189  $this->error = "Error ".$this->db->lasterror();
190  $error++;
191  }
192 
193  if (!$error && !$notrigger)
194  {
195  // Call trigger
196  $result = $this->call_trigger('LOCALTAX_MODIFY', $user);
197  if ($result < 0) $error++;
198  // End call triggers
199  }
200 
201  if (!$error)
202  {
203  $this->db->commit();
204  return 1;
205  } else {
206  $this->db->rollback();
207  return -1;
208  }
209  }
210 
211 
218  public function fetch($id)
219  {
220  global $langs;
221  $sql = "SELECT";
222  $sql .= " t.rowid,";
223  $sql .= " t.localtaxtype,";
224  $sql .= " t.tms,";
225  $sql .= " t.datep,";
226  $sql .= " t.datev,";
227  $sql .= " t.amount,";
228  $sql .= " t.label,";
229  $sql .= " t.note,";
230  $sql .= " t.fk_bank,";
231  $sql .= " t.fk_user_creat,";
232  $sql .= " t.fk_user_modif,";
233  $sql .= " b.fk_account,";
234  $sql .= " b.fk_type,";
235  $sql .= " b.rappro";
236  $sql .= " FROM ".MAIN_DB_PREFIX."localtax as t";
237  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."bank as b ON t.fk_bank = b.rowid";
238  $sql .= " WHERE t.rowid = ".$id;
239 
240  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
241  $resql = $this->db->query($sql);
242  if ($resql)
243  {
244  if ($this->db->num_rows($resql))
245  {
246  $obj = $this->db->fetch_object($resql);
247 
248  $this->id = $obj->rowid;
249  $this->ref = $obj->rowid;
250  $this->ltt = $obj->localtaxtype;
251  $this->tms = $this->db->jdate($obj->tms);
252  $this->datep = $this->db->jdate($obj->datep);
253  $this->datev = $this->db->jdate($obj->datev);
254  $this->amount = $obj->amount;
255  $this->label = $obj->label;
256  $this->note = $obj->note;
257  $this->fk_bank = $obj->fk_bank;
258  $this->fk_user_creat = $obj->fk_user_creat;
259  $this->fk_user_modif = $obj->fk_user_modif;
260  $this->fk_account = $obj->fk_account;
261  $this->fk_type = $obj->fk_type;
262  $this->rappro = $obj->rappro;
263  }
264  $this->db->free($resql);
265 
266  return 1;
267  } else {
268  $this->error = "Error ".$this->db->lasterror();
269  return -1;
270  }
271  }
272 
273 
280  public function delete($user)
281  {
282  // Call trigger
283  $result = $this->call_trigger('LOCALTAX_DELETE', $user);
284  if ($result < 0) return -1;
285  // End call triggers
286 
287  $sql = "DELETE FROM ".MAIN_DB_PREFIX."localtax";
288  $sql .= " WHERE rowid=".$this->id;
289 
290  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
291  $resql = $this->db->query($sql);
292  if (!$resql)
293  {
294  $this->error = "Error ".$this->db->lasterror();
295  return -1;
296  }
297 
298  return 1;
299  }
300 
301 
309  public function initAsSpecimen()
310  {
311  global $user;
312 
313  $this->id = 0;
314 
315  $this->tms = '';
316  $this->ltt = 0;
317  $this->datep = '';
318  $this->datev = '';
319  $this->amount = '';
320  $this->label = '';
321  $this->note = '';
322  $this->fk_bank = 0;
323  $this->fk_user_creat = $user->id;
324  $this->fk_user_modif = $user->id;
325  }
326 
327 
334  public function solde($year = 0)
335  {
336  $reglee = $this->localtax_sum_reglee($year);
337 
338  $payee = $this->localtax_sum_payee($year);
339  $collectee = $this->localtax_sum_collectee($year);
340 
341  $solde = $reglee - ($collectee - $payee);
342 
343  return $solde;
344  }
345 
346  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
353  public function localtax_sum_collectee($year = 0)
354  {
355  // phpcs:enable
356  $sql = "SELECT sum(f.localtax) as amount";
357  $sql .= " FROM ".MAIN_DB_PREFIX."facture as f WHERE f.paye = 1";
358  if ($year)
359  {
360  $sql .= " AND f.datef >= '$year-01-01' AND f.datef <= '$year-12-31' ";
361  }
362 
363  $result = $this->db->query($sql);
364  if ($result)
365  {
366  if ($this->db->num_rows($result))
367  {
368  $obj = $this->db->fetch_object($result);
369  $ret = $obj->amount;
370  $this->db->free($result);
371  return $ret;
372  } else {
373  $this->db->free($result);
374  return 0;
375  }
376  } else {
377  print $this->db->lasterror();
378  return -1;
379  }
380  }
381 
382  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
389  public function localtax_sum_payee($year = 0)
390  {
391  // phpcs:enable
392 
393  $sql = "SELECT sum(f.total_localtax) as total_localtax";
394  $sql .= " FROM ".MAIN_DB_PREFIX."facture_fourn as f";
395  if ($year)
396  {
397  $sql .= " WHERE f.datef >= '$year-01-01' AND f.datef <= '$year-12-31' ";
398  }
399 
400  $result = $this->db->query($sql);
401  if ($result)
402  {
403  if ($this->db->num_rows($result))
404  {
405  $obj = $this->db->fetch_object($result);
406  $ret = $obj->total_localtax;
407  $this->db->free($result);
408  return $ret;
409  } else {
410  $this->db->free($result);
411  return 0;
412  }
413  } else {
414  print $this->db->lasterror();
415  return -1;
416  }
417  }
418 
419 
420  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
427  public function localtax_sum_reglee($year = 0)
428  {
429  // phpcs:enable
430 
431  $sql = "SELECT sum(f.amount) as amount";
432  $sql .= " FROM ".MAIN_DB_PREFIX."localtax as f";
433  if ($year)
434  {
435  $sql .= " WHERE f.datev >= '$year-01-01' AND f.datev <= '$year-12-31' ";
436  }
437 
438  $result = $this->db->query($sql);
439  if ($result)
440  {
441  if ($this->db->num_rows($result))
442  {
443  $obj = $this->db->fetch_object($result);
444  $ret = $obj->amount;
445  $this->db->free($result);
446  return $ret;
447  } else {
448  $this->db->free($result);
449  return 0;
450  }
451  } else {
452  print $this->db->lasterror();
453  return -1;
454  }
455  }
456 
457 
464  public function addPayment($user)
465  {
466  global $conf, $langs;
467 
468  $this->db->begin();
469 
470  // Check parameters
471  $this->amount = price2num($this->amount);
472  if (!$this->label)
473  {
474  $this->error = $langs->trans("ErrorFieldRequired", $langs->transnoentities("Label"));
475  return -3;
476  }
477  if ($this->amount <= 0)
478  {
479  $this->error = $langs->trans("ErrorFieldRequired", $langs->transnoentities("Amount"));
480  return -4;
481  }
482  if (!empty($conf->banque->enabled) && (empty($this->accountid) || $this->accountid <= 0))
483  {
484  $this->error = $langs->trans("ErrorFieldRequired", $langs->transnoentities("Account"));
485  return -5;
486  }
487  if (!empty($conf->banque->enabled) && (empty($this->paymenttype) || $this->paymenttype <= 0))
488  {
489  $this->error = $langs->trans("ErrorFieldRequired", $langs->transnoentities("PaymentMode"));
490  return -5;
491  }
492 
493  // Insertion dans table des paiement localtax
494  $sql = "INSERT INTO ".MAIN_DB_PREFIX."localtax (localtaxtype, datep, datev, amount";
495  if ($this->note) $sql .= ", note";
496  if ($this->label) $sql .= ", label";
497  $sql .= ", fk_user_creat, fk_bank";
498  $sql .= ") ";
499  $sql .= " VALUES (".$this->ltt.", '".$this->db->idate($this->datep)."',";
500  $sql .= "'".$this->db->idate($this->datev)."',".$this->amount;
501  if ($this->note) $sql .= ", '".$this->db->escape($this->note)."'";
502  if ($this->label) $sql .= ", '".$this->db->escape($this->label)."'";
503  $sql .= ", ".((int) $user->id).", NULL";
504  $sql .= ")";
505 
506  dol_syslog(get_class($this)."::addPayment", LOG_DEBUG);
507  $result = $this->db->query($sql);
508  if ($result)
509  {
510  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."localtax"); // TODO devrait s'appeler paiementlocaltax
511  if ($this->id > 0)
512  {
513  $ok = 1;
514  if (!empty($conf->banque->enabled))
515  {
516  // Insertion dans llx_bank
517  require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
518 
519  $acc = new Account($this->db);
520  $result = $acc->fetch($this->accountid);
521  if ($result <= 0) dol_print_error($this->db);
522 
523  $bank_line_id = $acc->addline($this->datep, $this->paymenttype, $this->label, -abs($this->amount), '', '', $user);
524 
525  // Mise a jour fk_bank dans llx_localtax. On connait ainsi la ligne de localtax qui a g�n�r� l'�criture bancaire
526  if ($bank_line_id > 0)
527  {
528  $this->update_fk_bank($bank_line_id);
529  } else {
530  $this->error = $acc->error;
531  $ok = 0;
532  }
533 
534  // Mise a jour liens
535  $result = $acc->add_url_line($bank_line_id, $this->id, DOL_URL_ROOT.'/compta/localtax/card.php?id=', "(VATPayment)", "payment_vat");
536  if ($result < 0)
537  {
538  $this->error = $acc->error;
539  $ok = 0;
540  }
541  }
542 
543  if ($ok)
544  {
545  $this->db->commit();
546  return $this->id;
547  } else {
548  $this->db->rollback();
549  return -3;
550  }
551  } else {
552  $this->error = $this->db->lasterror();
553  $this->db->rollback();
554  return -2;
555  }
556  } else {
557  $this->error = $this->db->lasterror();
558  $this->db->rollback();
559  return -1;
560  }
561  }
562 
563  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
570  public function update_fk_bank($id)
571  {
572  // phpcs:enable
573  $sql = 'UPDATE '.MAIN_DB_PREFIX.'localtax SET fk_bank = '.$id;
574  $sql .= ' WHERE rowid = '.$this->id;
575  $result = $this->db->query($sql);
576  if ($result) {
577  return 1;
578  } else {
579  dol_print_error($this->db);
580  return -1;
581  }
582  }
583 
584 
592  public 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 
616  public function getLibStatut($mode = 0)
617  {
618  return $this->LibStatut($this->statut, $mode);
619  }
620 
621  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
629  public function LibStatut($status, $mode = 0)
630  {
631  // phpcs:enable
632  global $langs; // TODO Renvoyer le libelle anglais et faire traduction a affichage
633 
634  return '';
635  }
636 }
__construct($db)
Constructor.
if(!empty($arrayfields['u.datec']['checked'])) print_liste_field_titre("DateCreationShort"u if(!empty($arrayfields['u.tms']['checked'])) print_liste_field_titre("DateModificationShort"u if(!empty($arrayfields['u.statut']['checked'])) print_liste_field_titre("Status"u statut
Definition: list.php:632
update(User $user, $notrigger=0)
Update database.
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:44
initAsSpecimen()
Initialise an instance with random values.
localtax_sum_reglee($year=0)
Total of localtax paid.
$conf db
API class for accounts.
Definition: inc.php:54
Class to manage bank accounts.
fetch($id)
Load object in memory from database.
localtax_sum_collectee($year=0)
Total de la localtax des factures emises par la societe.
price2num($amount, $rounding= '', $option=0)
Function that return a number with universal decimal format (decimal separator is &#39;...
localtax_sum_payee($year=0)
Total of localtax paid in invoice.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
create($user)
Create in database.
addPayment($user)
Add a payment of localtax.
print
Draft customers invoices.
Definition: index.php:89
call_trigger($triggerName, $user)
Call trigger based on this instance.
LibStatut($status, $mode=0)
Renvoi le libelle d&#39;un statut donne.
if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) if(!empty($conf->don->enabled)&&$user->rights->don->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)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1232
dol_print_error($db= '', $error= '', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
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) ...
getNomUrl($withpicto=0, $option= '')
Returns clickable name.
Parent class of all other business classes (invoices, contracts, proposals, orders, ...)
if(!empty($search_group)) natural_search(array("g.nom"g note
Definition: list.php:122
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $keepmoretags= '', $escapeonlyhtmltags=0)
Returns text escaped for inclusion in HTML alt or title tags, or into values of HTML input fields...