dolibarr  20.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  * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
4  * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.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 <https://www.gnu.org/licenses/>.
18  */
19 
25 require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
26 
27 
31 class Localtax extends CommonObject
32 {
36  public $element = 'localtax';
37 
41  public $table_element = 'localtax';
42 
46  public $picto = 'payment';
47 
48  public $ltt;
49 
50  public $datep;
51  public $datev;
52  public $amount;
53 
57  public $accountid;
58 
62  public $fk_type;
63 
64  public $paymenttype;
65 
69  public $rappro;
70 
71 
75  public $label;
76 
80  public $fk_bank;
81 
85  public $fk_user_creat;
86 
90  public $fk_user_modif;
91 
97  public function __construct($db)
98  {
99  $this->db = $db;
100  }
101 
102 
109  public function create($user)
110  {
111  global $conf, $langs;
112 
113  $error = 0;
114 
115  // Clean parameters
116  $this->amount = trim($this->amount);
117  $this->label = trim($this->label);
118  $this->note = trim($this->note);
119 
120  // Insert request
121  $sql = "INSERT INTO ".MAIN_DB_PREFIX."localtax(";
122  $sql .= "localtaxtype,";
123  $sql .= "tms,";
124  $sql .= "datep,";
125  $sql .= "datev,";
126  $sql .= "amount,";
127  $sql .= "label,";
128  $sql .= "note,";
129  $sql .= "fk_bank,";
130  $sql .= "fk_user_creat,";
131  $sql .= "fk_user_modif";
132  $sql .= ") VALUES (";
133  $sql .= " ".((int) $this->ltt).",";
134  $sql .= " '".$this->db->idate($this->tms)."',";
135  $sql .= " '".$this->db->idate($this->datep)."',";
136  $sql .= " '".$this->db->idate($this->datev)."',";
137  $sql .= " '".$this->db->escape($this->amount)."',";
138  $sql .= " '".$this->db->escape($this->label)."',";
139  $sql .= " '".$this->db->escape($this->note)."',";
140  $sql .= " ".($this->fk_bank <= 0 ? "NULL" : (int) $this->fk_bank).",";
141  $sql .= " ".((int) $this->fk_user_creat).",";
142  $sql .= " ".((int) $this->fk_user_modif);
143  $sql .= ")";
144 
145  dol_syslog(get_class($this)."::create", LOG_DEBUG);
146  $resql = $this->db->query($sql);
147  if ($resql) {
148  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."localtax");
149 
150  // Call trigger
151  $result = $this->call_trigger('LOCALTAX_CREATE', $user);
152  if ($result < 0) {
153  $error++;
154  }
155  // End call triggers
156 
157  if (!$error) {
158  $this->db->commit();
159  return $this->id;
160  } else {
161  $this->db->rollback();
162  return -1;
163  }
164  } else {
165  $this->error = "Error ".$this->db->lasterror();
166  $this->db->rollback();
167  return -1;
168  }
169  }
170 
178  public function update(User $user, $notrigger = 0)
179  {
180  global $conf, $langs;
181 
182  $error = 0;
183 
184  // Clean parameters
185  $this->amount = trim($this->amount);
186  $this->label = trim($this->label);
187  $this->note = trim($this->note);
188 
189  $this->db->begin();
190 
191  // Update request
192  $sql = "UPDATE ".MAIN_DB_PREFIX."localtax SET";
193  $sql .= " localtaxtype=".((int) $this->ltt).",";
194  $sql .= " tms='".$this->db->idate($this->tms)."',";
195  $sql .= " datep='".$this->db->idate($this->datep)."',";
196  $sql .= " datev='".$this->db->idate($this->datev)."',";
197  $sql .= " amount=".price2num($this->amount).",";
198  $sql .= " label='".$this->db->escape($this->label)."',";
199  $sql .= " note='".$this->db->escape($this->note)."',";
200  $sql .= " fk_bank=".(int) $this->fk_bank.",";
201  $sql .= " fk_user_creat=".(int) $this->fk_user_creat.",";
202  $sql .= " fk_user_modif=".(int) $this->fk_user_modif;
203  $sql .= " WHERE rowid=".((int) $this->id);
204 
205  dol_syslog(get_class($this)."::update", LOG_DEBUG);
206  $resql = $this->db->query($sql);
207  if (!$resql) {
208  $this->error = "Error ".$this->db->lasterror();
209  $error++;
210  }
211 
212  if (!$error && !$notrigger) {
213  // Call trigger
214  $result = $this->call_trigger('LOCALTAX_MODIFY', $user);
215  if ($result < 0) {
216  $error++;
217  }
218  // End call triggers
219  }
220 
221  if (!$error) {
222  $this->db->commit();
223  return 1;
224  } else {
225  $this->db->rollback();
226  return -1;
227  }
228  }
229 
230 
237  public function fetch($id)
238  {
239  $sql = "SELECT";
240  $sql .= " t.rowid,";
241  $sql .= " t.localtaxtype,";
242  $sql .= " t.tms,";
243  $sql .= " t.datep,";
244  $sql .= " t.datev,";
245  $sql .= " t.amount,";
246  $sql .= " t.label,";
247  $sql .= " t.note as note_private,";
248  $sql .= " t.fk_bank,";
249  $sql .= " t.fk_user_creat,";
250  $sql .= " t.fk_user_modif,";
251  $sql .= " b.fk_account,";
252  $sql .= " b.fk_type,";
253  $sql .= " b.rappro";
254  $sql .= " FROM ".MAIN_DB_PREFIX."localtax as t";
255  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."bank as b ON t.fk_bank = b.rowid";
256  $sql .= " WHERE t.rowid = ".((int) $id);
257 
258  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
259  $resql = $this->db->query($sql);
260  if ($resql) {
261  if ($this->db->num_rows($resql)) {
262  $obj = $this->db->fetch_object($resql);
263 
264  $this->id = $obj->rowid;
265  $this->ref = $obj->rowid;
266  $this->ltt = $obj->localtaxtype;
267  $this->tms = $this->db->jdate($obj->tms);
268  $this->datep = $this->db->jdate($obj->datep);
269  $this->datev = $this->db->jdate($obj->datev);
270  $this->amount = $obj->amount;
271  $this->label = $obj->label;
272  $this->note = $obj->note_private;
273  $this->note_private = $obj->note_private;
274  $this->fk_bank = $obj->fk_bank;
275  $this->fk_user_creat = $obj->fk_user_creat;
276  $this->fk_user_modif = $obj->fk_user_modif;
277  $this->fk_account = $obj->fk_account;
278  $this->fk_type = $obj->fk_type;
279  $this->rappro = $obj->rappro;
280  }
281  $this->db->free($resql);
282 
283  return 1;
284  } else {
285  $this->error = "Error ".$this->db->lasterror();
286  return -1;
287  }
288  }
289 
290 
297  public function delete($user)
298  {
299  // Call trigger
300  $result = $this->call_trigger('LOCALTAX_DELETE', $user);
301  if ($result < 0) {
302  return -1;
303  }
304  // End call triggers
305 
306  $sql = "DELETE FROM ".MAIN_DB_PREFIX."localtax";
307  $sql .= " WHERE rowid=".((int) $this->id);
308 
309  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
310  $resql = $this->db->query($sql);
311  if (!$resql) {
312  $this->error = "Error ".$this->db->lasterror();
313  return -1;
314  }
315 
316  return 1;
317  }
318 
319 
327  public function initAsSpecimen()
328  {
329  global $user;
330 
331  $this->id = 0;
332 
333  $this->tms = dol_now();
334  $this->ltt = 0;
335  $this->datep = '';
336  $this->datev = '';
337  $this->amount = '';
338  $this->label = '';
339  $this->note = '';
340  $this->fk_bank = 0;
341  $this->fk_user_creat = $user->id;
342  $this->fk_user_modif = $user->id;
343 
344  return 1;
345  }
346 
347 
354  public function solde($year = 0)
355  {
356  $reglee = $this->localtax_sum_reglee($year);
357 
358  $payee = $this->localtax_sum_payee($year);
359  $collectee = $this->localtax_sum_collectee($year);
360 
361  $solde = $reglee - ($collectee - $payee);
362 
363  return $solde;
364  }
365 
366  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
373  public function localtax_sum_collectee($year = 0)
374  {
375  // phpcs:enable
376  $sql = "SELECT sum(f.localtax) as amount";
377  $sql .= " FROM ".MAIN_DB_PREFIX."facture as f";
378  $sql .= " WHERE f.paye = 1";
379  if ($year) {
380  $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'))."'";
381  }
382 
383  $result = $this->db->query($sql);
384  if ($result) {
385  if ($this->db->num_rows($result)) {
386  $obj = $this->db->fetch_object($result);
387  $ret = $obj->amount;
388  $this->db->free($result);
389  return $ret;
390  } else {
391  $this->db->free($result);
392  return 0;
393  }
394  } else {
395  print $this->db->lasterror();
396  return -1;
397  }
398  }
399 
400  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
407  public function localtax_sum_payee($year = 0)
408  {
409  // phpcs:enable
410 
411  $sql = "SELECT sum(f.total_localtax) as total_localtax";
412  $sql .= " FROM ".MAIN_DB_PREFIX."facture_fourn as f";
413  if ($year) {
414  $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'))."'";
415  }
416 
417  $result = $this->db->query($sql);
418  if ($result) {
419  if ($this->db->num_rows($result)) {
420  $obj = $this->db->fetch_object($result);
421  $ret = $obj->total_localtax;
422  $this->db->free($result);
423  return $ret;
424  } else {
425  $this->db->free($result);
426  return 0;
427  }
428  } else {
429  print $this->db->lasterror();
430  return -1;
431  }
432  }
433 
434 
435  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
442  public function localtax_sum_reglee($year = 0)
443  {
444  // phpcs:enable
445 
446  $sql = "SELECT sum(f.amount) as amount";
447  $sql .= " FROM ".MAIN_DB_PREFIX."localtax as f";
448  if ($year) {
449  $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'))."'";
450  }
451 
452  $result = $this->db->query($sql);
453  if ($result) {
454  if ($this->db->num_rows($result)) {
455  $obj = $this->db->fetch_object($result);
456  $ret = $obj->amount;
457  $this->db->free($result);
458  return $ret;
459  } else {
460  $this->db->free($result);
461  return 0;
462  }
463  } else {
464  print $this->db->lasterror();
465  return -1;
466  }
467  }
468 
469 
476  public function addPayment($user)
477  {
478  global $conf, $langs;
479 
480  $this->db->begin();
481 
482  // Check parameters
483  $this->amount = price2num($this->amount);
484  if (!$this->label) {
485  $this->error = $langs->trans("ErrorFieldRequired", $langs->transnoentities("Label"));
486  return -3;
487  }
488  if ($this->amount <= 0) {
489  $this->error = $langs->trans("ErrorFieldRequired", $langs->transnoentities("Amount"));
490  return -4;
491  }
492  if (isModEnabled("bank") && (empty($this->accountid) || $this->accountid <= 0)) {
493  $this->error = $langs->trans("ErrorFieldRequired", $langs->transnoentities("BankAccount"));
494  return -5;
495  }
496  if (isModEnabled("bank") && (empty($this->paymenttype) || $this->paymenttype <= 0)) {
497  $this->error = $langs->trans("ErrorFieldRequired", $langs->transnoentities("PaymentMode"));
498  return -5;
499  }
500 
501  // Insertion dans table des paiement localtax
502  $sql = "INSERT INTO ".MAIN_DB_PREFIX."localtax (localtaxtype, datep, datev, amount";
503  if ($this->note) {
504  $sql .= ", note";
505  }
506  if ($this->label) {
507  $sql .= ", label";
508  }
509  $sql .= ", fk_user_creat, fk_bank";
510  $sql .= ") ";
511  $sql .= " VALUES (".$this->ltt.", '".$this->db->idate($this->datep)."',";
512  $sql .= "'".$this->db->idate($this->datev)."',".$this->amount;
513  if ($this->note) {
514  $sql .= ", '".$this->db->escape($this->note)."'";
515  }
516  if ($this->label) {
517  $sql .= ", '".$this->db->escape($this->label)."'";
518  }
519  $sql .= ", ".((int) $user->id).", NULL";
520  $sql .= ")";
521 
522  dol_syslog(get_class($this)."::addPayment", LOG_DEBUG);
523  $result = $this->db->query($sql);
524  if ($result) {
525  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."localtax"); // TODO devrait s'appeler paiementlocaltax
526  if ($this->id > 0) {
527  $ok = 1;
528  if (isModEnabled("bank")) {
529  // Insertion dans llx_bank
530  require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
531 
532  $acc = new Account($this->db);
533  $result = $acc->fetch($this->accountid);
534  if ($result <= 0) {
535  dol_print_error($this->db);
536  }
537 
538  $bank_line_id = $acc->addline($this->datep, $this->paymenttype, $this->label, -abs((float) $this->amount), '', '', $user);
539 
540  // Update fk_bank into llx_localtax so we know the line of localtax used to generate the bank entry.
541  if ($bank_line_id > 0) {
542  $this->update_fk_bank($bank_line_id);
543  } else {
544  $this->error = $acc->error;
545  $ok = 0;
546  }
547 
548  // Mise a jour liens
549  $result = $acc->add_url_line($bank_line_id, $this->id, DOL_URL_ROOT.'/compta/localtax/card.php?id=', "(VATPayment)", "payment_vat");
550  if ($result < 0) {
551  $this->error = $acc->error;
552  $ok = 0;
553  }
554  }
555 
556  if ($ok) {
557  $this->db->commit();
558  return $this->id;
559  } else {
560  $this->db->rollback();
561  return -3;
562  }
563  } else {
564  $this->error = $this->db->lasterror();
565  $this->db->rollback();
566  return -2;
567  }
568  } else {
569  $this->error = $this->db->lasterror();
570  $this->db->rollback();
571  return -1;
572  }
573  }
574 
575  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
582  public function update_fk_bank($id)
583  {
584  // phpcs:enable
585  $sql = 'UPDATE '.MAIN_DB_PREFIX.'localtax SET fk_bank = '.((int) $id);
586  $sql .= ' WHERE rowid = '.((int) $this->id);
587  $result = $this->db->query($sql);
588  if ($result) {
589  return 1;
590  } else {
591  dol_print_error($this->db);
592  return -1;
593  }
594  }
595 
596 
604  public function getNomUrl($withpicto = 0, $option = '')
605  {
606  global $langs;
607 
608  $result = '';
609  $label = $langs->trans("ShowVatPayment").': '.$this->ref;
610 
611  $link = '<a href="'.DOL_URL_ROOT.'/compta/localtax/card.php?id='.$this->id.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
612  $linkend = '</a>';
613 
614  $picto = 'payment';
615 
616  if ($withpicto) {
617  $result .= ($link.img_object($label, $picto, 'class="classfortooltip"').$linkend);
618  }
619  if ($withpicto && $withpicto != 2) {
620  $result .= ' ';
621  }
622  if ($withpicto != 2) {
623  $result .= $link.$this->ref.$linkend;
624  }
625  return $result;
626  }
627 
634  public function getLibStatut($mode = 0)
635  {
636  return $this->LibStatut($this->statut, $mode);
637  }
638 
639  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
647  public function LibStatut($status, $mode = 0)
648  {
649  // phpcs:enable
650  //global $langs;
651 
652  return '';
653  }
654 
662  public function getKanbanView($option = '', $arraydata = null)
663  {
664  global $langs;
665 
666  $selected = (empty($arraydata['selected']) ? 0 : $arraydata['selected']);
667 
668  $return = '<div class="box-flex-item box-flex-grow-zero">';
669  $return .= '<div class="info-box info-box-sm">';
670  $return .= '<span class="info-box-icon bg-infobox-action">';
671  $return .= img_picto('', $this->picto);
672  $return .= '</span>';
673  $return .= '<div class="info-box-content">';
674  $return .= '<span class="info-box-ref inline-block tdoverflowmax150 valignmiddle">'.(method_exists($this, 'getNomUrl') ? $this->getNomUrl() : $this->ref).'</span>';
675  if ($selected >= 0) {
676  $return .= '<input id="cb'.$this->id.'" class="flat checkforselect fright" type="checkbox" name="toselect[]" value="'.$this->id.'"'.($selected ? ' checked="checked"' : '').'>';
677  }
678  if (property_exists($this, 'label')) {
679  $return .= ' | <span class="info-box-label">'.$this->label.'</span>';
680  }
681  if (property_exists($this, 'datev')) {
682  $return .= '<br><span class="opacitymedium">'.$langs->trans("DateEnd").'</span> : <span class="info-box-label">'.dol_print_date($this->db->jdate($this->datev), 'day').'</span>';
683  }
684  if (property_exists($this, 'datep')) {
685  $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>';
686  }
687  if (property_exists($this, 'amount')) {
688  $return .= '<br><span class="opacitymedium">'.$langs->trans("Amount").'</span> : <span class="info-box-label amount">'.price($this->amount).'</span>';
689  }
690  $return .= '</div>';
691  $return .= '</div>';
692  $return .= '</div>';
693  return $return;
694  }
695 }
$object ref
Definition: info.php:79
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 function 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 between 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:50
if(isModEnabled('invoice') && $user->hasRight('facture', 'lire')) if((isModEnabled('fournisseur') &&!getDolGlobalString('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') && $user->hasRight('tax', 'charges', 'lire')) if(isModEnabled('invoice') &&isModEnabled('order') && $user->hasRight("commande", "lire") &&!getDolGlobalString('WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER')) $sql
Social contributions to pay.
Definition: index.php:745
dol_get_first_day($year, $month=1, $gm=false)
Return GMT time for first day of a month or year.
Definition: date.lib.php:595
dol_get_last_day($year, $month=12, $gm=false)
Return GMT time for last day of a month or year.
Definition: date.lib.php:614
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
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_now($mode='auto')
Return date for now.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
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...
td amount
Definition: global.inc.php:554