dolibarr  19.0.0-dev
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 .= " ".((int) $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  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."localtax");
129 
130  // Call trigger
131  $result = $this->call_trigger('LOCALTAX_CREATE', $user);
132  if ($result < 0) {
133  $error++;
134  }
135  // End call triggers
136 
137  if (!$error) {
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=".((int) $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=".((int) $this->id);
184 
185  dol_syslog(get_class($this)."::update", LOG_DEBUG);
186  $resql = $this->db->query($sql);
187  if (!$resql) {
188  $this->error = "Error ".$this->db->lasterror();
189  $error++;
190  }
191 
192  if (!$error && !$notrigger) {
193  // Call trigger
194  $result = $this->call_trigger('LOCALTAX_MODIFY', $user);
195  if ($result < 0) {
196  $error++;
197  }
198  // End call triggers
199  }
200 
201  if (!$error) {
202  $this->db->commit();
203  return 1;
204  } else {
205  $this->db->rollback();
206  return -1;
207  }
208  }
209 
210 
217  public function fetch($id)
218  {
219  global $langs;
220  $sql = "SELECT";
221  $sql .= " t.rowid,";
222  $sql .= " t.localtaxtype,";
223  $sql .= " t.tms,";
224  $sql .= " t.datep,";
225  $sql .= " t.datev,";
226  $sql .= " t.amount,";
227  $sql .= " t.label,";
228  $sql .= " t.note,";
229  $sql .= " t.fk_bank,";
230  $sql .= " t.fk_user_creat,";
231  $sql .= " t.fk_user_modif,";
232  $sql .= " b.fk_account,";
233  $sql .= " b.fk_type,";
234  $sql .= " b.rappro";
235  $sql .= " FROM ".MAIN_DB_PREFIX."localtax as t";
236  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."bank as b ON t.fk_bank = b.rowid";
237  $sql .= " WHERE t.rowid = ".((int) $id);
238 
239  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
240  $resql = $this->db->query($sql);
241  if ($resql) {
242  if ($this->db->num_rows($resql)) {
243  $obj = $this->db->fetch_object($resql);
244 
245  $this->id = $obj->rowid;
246  $this->ref = $obj->rowid;
247  $this->ltt = $obj->localtaxtype;
248  $this->tms = $this->db->jdate($obj->tms);
249  $this->datep = $this->db->jdate($obj->datep);
250  $this->datev = $this->db->jdate($obj->datev);
251  $this->amount = $obj->amount;
252  $this->label = $obj->label;
253  $this->note = $obj->note;
254  $this->fk_bank = $obj->fk_bank;
255  $this->fk_user_creat = $obj->fk_user_creat;
256  $this->fk_user_modif = $obj->fk_user_modif;
257  $this->fk_account = $obj->fk_account;
258  $this->fk_type = $obj->fk_type;
259  $this->rappro = $obj->rappro;
260  }
261  $this->db->free($resql);
262 
263  return 1;
264  } else {
265  $this->error = "Error ".$this->db->lasterror();
266  return -1;
267  }
268  }
269 
270 
277  public function delete($user)
278  {
279  // Call trigger
280  $result = $this->call_trigger('LOCALTAX_DELETE', $user);
281  if ($result < 0) {
282  return -1;
283  }
284  // End call triggers
285 
286  $sql = "DELETE FROM ".MAIN_DB_PREFIX."localtax";
287  $sql .= " WHERE rowid=".((int) $this->id);
288 
289  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
290  $resql = $this->db->query($sql);
291  if (!$resql) {
292  $this->error = "Error ".$this->db->lasterror();
293  return -1;
294  }
295 
296  return 1;
297  }
298 
299 
307  public function initAsSpecimen()
308  {
309  global $user;
310 
311  $this->id = 0;
312 
313  $this->tms = '';
314  $this->ltt = 0;
315  $this->datep = '';
316  $this->datev = '';
317  $this->amount = '';
318  $this->label = '';
319  $this->note = '';
320  $this->fk_bank = 0;
321  $this->fk_user_creat = $user->id;
322  $this->fk_user_modif = $user->id;
323  }
324 
325 
332  public function solde($year = 0)
333  {
334  $reglee = $this->localtax_sum_reglee($year);
335 
336  $payee = $this->localtax_sum_payee($year);
337  $collectee = $this->localtax_sum_collectee($year);
338 
339  $solde = $reglee - ($collectee - $payee);
340 
341  return $solde;
342  }
343 
344  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
351  public function localtax_sum_collectee($year = 0)
352  {
353  // phpcs:enable
354  $sql = "SELECT sum(f.localtax) as amount";
355  $sql .= " FROM ".MAIN_DB_PREFIX."facture as f";
356  $sql .= " WHERE f.paye = 1";
357  if ($year) {
358  $sql .= " AND f.datef BETWEEN '".$this->db->idate(dol_get_first_day($year, 1, 'gmt'))."' AND '".$this->db->idate(dol_get_last_day($year, 1, 'gmt'))."'";
359  }
360 
361  $result = $this->db->query($sql);
362  if ($result) {
363  if ($this->db->num_rows($result)) {
364  $obj = $this->db->fetch_object($result);
365  $ret = $obj->amount;
366  $this->db->free($result);
367  return $ret;
368  } else {
369  $this->db->free($result);
370  return 0;
371  }
372  } else {
373  print $this->db->lasterror();
374  return -1;
375  }
376  }
377 
378  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
385  public function localtax_sum_payee($year = 0)
386  {
387  // phpcs:enable
388 
389  $sql = "SELECT sum(f.total_localtax) as total_localtax";
390  $sql .= " FROM ".MAIN_DB_PREFIX."facture_fourn as f";
391  if ($year) {
392  $sql .= " WHERE f.datef BETWEEN '".$this->db->idate(dol_get_first_day($year, 1, 'gmt'))."' AND '".$this->db->idate(dol_get_last_day($year, 1, 'gmt'))."'";
393  }
394 
395  $result = $this->db->query($sql);
396  if ($result) {
397  if ($this->db->num_rows($result)) {
398  $obj = $this->db->fetch_object($result);
399  $ret = $obj->total_localtax;
400  $this->db->free($result);
401  return $ret;
402  } else {
403  $this->db->free($result);
404  return 0;
405  }
406  } else {
407  print $this->db->lasterror();
408  return -1;
409  }
410  }
411 
412 
413  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
420  public function localtax_sum_reglee($year = 0)
421  {
422  // phpcs:enable
423 
424  $sql = "SELECT sum(f.amount) as amount";
425  $sql .= " FROM ".MAIN_DB_PREFIX."localtax as f";
426  if ($year) {
427  $sql .= " WHERE f.datev BETWEEN '".$this->db->idate(dol_get_first_day($year, 1, 'gmt'))."' AND '".$this->db->idate(dol_get_last_day($year, 1, 'gmt'))."'";
428  }
429 
430  $result = $this->db->query($sql);
431  if ($result) {
432  if ($this->db->num_rows($result)) {
433  $obj = $this->db->fetch_object($result);
434  $ret = $obj->amount;
435  $this->db->free($result);
436  return $ret;
437  } else {
438  $this->db->free($result);
439  return 0;
440  }
441  } else {
442  print $this->db->lasterror();
443  return -1;
444  }
445  }
446 
447 
454  public function addPayment($user)
455  {
456  global $conf, $langs;
457 
458  $this->db->begin();
459 
460  // Check parameters
461  $this->amount = price2num($this->amount);
462  if (!$this->label) {
463  $this->error = $langs->trans("ErrorFieldRequired", $langs->transnoentities("Label"));
464  return -3;
465  }
466  if ($this->amount <= 0) {
467  $this->error = $langs->trans("ErrorFieldRequired", $langs->transnoentities("Amount"));
468  return -4;
469  }
470  if (isModEnabled("banque") && (empty($this->accountid) || $this->accountid <= 0)) {
471  $this->error = $langs->trans("ErrorFieldRequired", $langs->transnoentities("Account"));
472  return -5;
473  }
474  if (isModEnabled("banque") && (empty($this->paymenttype) || $this->paymenttype <= 0)) {
475  $this->error = $langs->trans("ErrorFieldRequired", $langs->transnoentities("PaymentMode"));
476  return -5;
477  }
478 
479  // Insertion dans table des paiement localtax
480  $sql = "INSERT INTO ".MAIN_DB_PREFIX."localtax (localtaxtype, datep, datev, amount";
481  if ($this->note) {
482  $sql .= ", note";
483  }
484  if ($this->label) {
485  $sql .= ", label";
486  }
487  $sql .= ", fk_user_creat, fk_bank";
488  $sql .= ") ";
489  $sql .= " VALUES (".$this->ltt.", '".$this->db->idate($this->datep)."',";
490  $sql .= "'".$this->db->idate($this->datev)."',".$this->amount;
491  if ($this->note) {
492  $sql .= ", '".$this->db->escape($this->note)."'";
493  }
494  if ($this->label) {
495  $sql .= ", '".$this->db->escape($this->label)."'";
496  }
497  $sql .= ", ".((int) $user->id).", NULL";
498  $sql .= ")";
499 
500  dol_syslog(get_class($this)."::addPayment", LOG_DEBUG);
501  $result = $this->db->query($sql);
502  if ($result) {
503  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."localtax"); // TODO devrait s'appeler paiementlocaltax
504  if ($this->id > 0) {
505  $ok = 1;
506  if (isModEnabled("banque")) {
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) {
513  dol_print_error($this->db);
514  }
515 
516  $bank_line_id = $acc->addline($this->datep, $this->paymenttype, $this->label, -abs($this->amount), '', '', $user);
517 
518  // Mise a jour fk_bank dans llx_localtax. On connait ainsi la ligne de localtax qui a g�n�r� l'�criture bancaire
519  if ($bank_line_id > 0) {
520  $this->update_fk_bank($bank_line_id);
521  } else {
522  $this->error = $acc->error;
523  $ok = 0;
524  }
525 
526  // Mise a jour liens
527  $result = $acc->add_url_line($bank_line_id, $this->id, DOL_URL_ROOT.'/compta/localtax/card.php?id=', "(VATPayment)", "payment_vat");
528  if ($result < 0) {
529  $this->error = $acc->error;
530  $ok = 0;
531  }
532  }
533 
534  if ($ok) {
535  $this->db->commit();
536  return $this->id;
537  } else {
538  $this->db->rollback();
539  return -3;
540  }
541  } else {
542  $this->error = $this->db->lasterror();
543  $this->db->rollback();
544  return -2;
545  }
546  } else {
547  $this->error = $this->db->lasterror();
548  $this->db->rollback();
549  return -1;
550  }
551  }
552 
553  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
560  public function update_fk_bank($id)
561  {
562  // phpcs:enable
563  $sql = 'UPDATE '.MAIN_DB_PREFIX.'localtax SET fk_bank = '.((int) $id);
564  $sql .= ' WHERE rowid = '.((int) $this->id);
565  $result = $this->db->query($sql);
566  if ($result) {
567  return 1;
568  } else {
569  dol_print_error($this->db);
570  return -1;
571  }
572  }
573 
574 
582  public function getNomUrl($withpicto = 0, $option = '')
583  {
584  global $langs;
585 
586  $result = '';
587  $label = $langs->trans("ShowVatPayment").': '.$this->ref;
588 
589  $link = '<a href="'.DOL_URL_ROOT.'/compta/localtax/card.php?id='.$this->id.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
590  $linkend = '</a>';
591 
592  $picto = 'payment';
593 
594  if ($withpicto) {
595  $result .= ($link.img_object($label, $picto, 'class="classfortooltip"').$linkend);
596  }
597  if ($withpicto && $withpicto != 2) {
598  $result .= ' ';
599  }
600  if ($withpicto != 2) {
601  $result .= $link.$this->ref.$linkend;
602  }
603  return $result;
604  }
605 
612  public function getLibStatut($mode = 0)
613  {
614  return $this->LibStatut($this->statut, $mode);
615  }
616 
617  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
625  public function LibStatut($status, $mode = 0)
626  {
627  // phpcs:enable
628  //global $langs;
629 
630  return '';
631  }
632 
640  public function getKanbanView($option = '', $arraydata = null)
641  {
642  global $langs;
643 
644  $selected = (empty($arraydata['selected']) ? 0 : $arraydata['selected']);
645 
646  $return = '<div class="box-flex-item box-flex-grow-zero">';
647  $return .= '<div class="info-box info-box-sm">';
648  $return .= '<span class="info-box-icon bg-infobox-action">';
649  $return .= img_picto('', $this->picto);
650  $return .= '</span>';
651  $return .= '<div class="info-box-content">';
652  $return .= '<span class="info-box-ref inline-block tdoverflowmax150 valignmiddle">'.(method_exists($this, 'getNomUrl') ? $this->getNomUrl() : $this->ref).'</span>';
653  $return .= '<input id="cb'.$this->id.'" class="flat checkforselect fright" type="checkbox" name="toselect[]" value="'.$this->id.'"'.($selected ? ' checked="checked"' : '').'>';
654  if (property_exists($this, 'label')) {
655  $return .= ' | <span class="info-box-label">'.$this->label.'</span>';
656  }
657  if (property_exists($this, 'datev')) {
658  $return .= '<br><span class="opacitymedium">'.$langs->trans("DateEnd").'</span> : <span class="info-box-label">'.dol_print_date($this->db->jdate($this->datev), 'day').'</span>';
659  }
660  if (property_exists($this, 'datep')) {
661  $return .= '<br><span class="opacitymedium">'.$langs->trans("DatePayment", '', '', '', '', 5).'</span> : <span class="info-box-label">'.dol_print_date($this->db->jdate($this->datep), 'day').'</span>';
662  }
663  if (property_exists($this, 'amount')) {
664  $return .= '<br><span class="opacitymedium">'.$langs->trans("Amount").'</span> : <span class="info-box-label amount">'.price($this->amount).'</span>';
665  }
666  $return .= '</div>';
667  $return .= '</div>';
668  $return .= '</div>';
669  return $return;
670  }
671 }
$object ref
Definition: info.php:78
Class to manage bank accounts.
Parent class of all other business classes (invoices, contracts, proposals, orders,...
call_trigger($triggerName, $user)
Call trigger based on this instance.
Class to manage local tax.
fetch($id)
Load object in memory from database.
LibStatut($status, $mode=0)
Return the label of a given status.
addPayment($user)
Add a payment of localtax.
solde($year=0)
Hum la fonction s'appelle 'Solde' elle doit a mon avis calcluer le solde de localtax,...
localtax_sum_payee($year=0)
Total of localtax paid in invoice.
getKanbanView($option='', $arraydata=null)
Return clicable link of object (with eventually picto)
initAsSpecimen()
Initialise an instance with random values.
getNomUrl($withpicto=0, $option='')
Returns clickable name.
update(User $user, $notrigger=0)
Update database.
__construct($db)
Constructor.
getLibStatut($mode=0)
Return the label of the status.
update_fk_bank($id)
Update the link betwen localtax payment and the line into llx_bank.
create($user)
Create in database.
localtax_sum_collectee($year=0)
Total de la localtax des factures emises par la societe.
localtax_sum_reglee($year=0)
Total of localtax paid.
Class to manage Dolibarr users.
Definition: user.class.php:48
if(isModEnabled('facture') && $user->hasRight('facture', 'lire')) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("supplier_invoice", "lire"))) if(isModEnabled('don') && $user->hasRight('don', 'lire')) if(isModEnabled('tax') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->hasRight("commande", "lire") &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $sql
Social contributions to pay.
Definition: index.php:746
dol_get_first_day($year, $month=1, $gm=false)
Return GMT time for first day of a month or year.
Definition: date.lib.php:576
dol_get_last_day($year, $month=12, $gm=false)
Return GMT time for last day of a month or year.
Definition: date.lib.php:595
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
price($amount, $form=0, $outlangs='', $trunc=1, $rounding=-1, $forcerounding=-1, $currency_code='')
Function to format a value into an amount for visual output Function used into PDF and HTML pages.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
isModEnabled($module)
Is Dolibarr module enabled.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0, $cleanalsojavascript=0)
Returns text escaped for inclusion in HTML alt or title or value tags, or into values of HTML input f...