dolibarr 25.0.0-alpha
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-2025 Frédéric France <frederic.france@free.fr>
4 * Copyright (C) 2024-2026 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
25require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
26
27
32{
36 public $element = 'localtax';
37
41 public $table_element = 'localtax';
42
46 public $picto = 'payment';
47
51 public $ltt;
52
56 public $datep;
60 public $datev;
64 public $amount;
65
69 public $accountid;
70
74 public $fk_type;
75
79 public $paymenttype;
80
84 public $rappro;
85
86
90 public $label;
91
95 public $fk_bank;
96
100 public $fk_user_creat;
101
105 public $fk_user_modif;
106
110 public $ismultientitymanaged = 1;
111
117 public function __construct($db)
118 {
119 $this->db = $db;
120 }
121
122
129 public function create($user)
130 {
131 global $conf, $langs;
132
133 $error = 0;
134
135 // Clean parameters
136 $this->amount = trim($this->amount);
137 $this->label = trim($this->label);
138 $this->note = trim($this->note);
139
140 // Set entity if not already set
141 if (empty($this->entity)) {
142 $this->entity = $conf->entity;
143 }
144
145 // Insert request
146 $sql = "INSERT INTO ".MAIN_DB_PREFIX."localtax(";
147 $sql .= "localtaxtype,";
148 $sql .= "tms,";
149 $sql .= "datep,";
150 $sql .= "datev,";
151 $sql .= "amount,";
152 $sql .= "label,";
153 $sql .= "note,";
154 $sql .= "entity,";
155 $sql .= "fk_bank,";
156 $sql .= "fk_user_creat,";
157 $sql .= "fk_user_modif";
158 $sql .= ") VALUES (";
159 $sql .= " ".((int) $this->ltt).",";
160 $sql .= " '".$this->db->idate($this->tms)."',";
161 $sql .= " '".$this->db->idate($this->datep)."',";
162 $sql .= " '".$this->db->idate($this->datev)."',";
163 $sql .= " '".$this->db->escape($this->amount)."',";
164 $sql .= " '".$this->db->escape($this->label)."',";
165 $sql .= " '".$this->db->escape($this->note)."',";
166 $sql .= " ".((int) $this->entity).",";
167 $sql .= " ".($this->fk_bank <= 0 ? "NULL" : (int) $this->fk_bank).",";
168 $sql .= " ".((int) $this->fk_user_creat).",";
169 $sql .= " ".((int) $this->fk_user_modif);
170 $sql .= ")";
171
172 dol_syslog(get_class($this)."::create", LOG_DEBUG);
173 $resql = $this->db->query($sql);
174 if ($resql) {
175 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."localtax");
176
177 // Call trigger
178 $result = $this->call_trigger('LOCALTAX_CREATE', $user);
179 if ($result < 0) {
180 $error++;
181 }
182 // End call triggers
183
184 if (!$error) {
185 $this->db->commit();
186 return $this->id;
187 } else {
188 $this->db->rollback();
189 return -1;
190 }
191 } else {
192 $this->error = "Error ".$this->db->lasterror();
193 $this->db->rollback();
194 return -1;
195 }
196 }
197
205 public function update(User $user, $notrigger = 0)
206 {
207 global $conf, $langs;
208
209 $error = 0;
210
211 // Clean parameters
212 $this->amount = trim($this->amount);
213 $this->label = trim($this->label);
214 $this->note = trim($this->note);
215
216 $this->db->begin();
217
218 // Update request
219 $sql = "UPDATE ".MAIN_DB_PREFIX."localtax SET";
220 $sql .= " localtaxtype=".((int) $this->ltt).",";
221 $sql .= " tms='".$this->db->idate($this->tms)."',";
222 $sql .= " datep='".$this->db->idate($this->datep)."',";
223 $sql .= " datev='".$this->db->idate($this->datev)."',";
224 $sql .= " amount='".$this->db->escape($this->amount)."',";
225 $sql .= " label='".$this->db->escape($this->label)."',";
226 $sql .= " note='".$this->db->escape($this->note)."',";
227 $sql .= " entity=".((int) $this->entity).",";
228 $sql .= " fk_bank=".(int) $this->fk_bank.",";
229 $sql .= " fk_user_creat=".(int) $this->fk_user_creat.",";
230 $sql .= " fk_user_modif=".(int) $this->fk_user_modif;
231 $sql .= " WHERE rowid=".((int) $this->id);
232
233 dol_syslog(get_class($this)."::update", LOG_DEBUG);
234 $resql = $this->db->query($sql);
235 if (!$resql) {
236 $this->error = "Error ".$this->db->lasterror();
237 $error++;
238 }
239
240 if (!$error && !$notrigger) {
241 // Call trigger
242 $result = $this->call_trigger('LOCALTAX_MODIFY', $user);
243 if ($result < 0) {
244 $error++;
245 }
246 // End call triggers
247 }
248
249 if (!$error) {
250 $this->db->commit();
251 return 1;
252 } else {
253 $this->db->rollback();
254 return -1;
255 }
256 }
257
258
265 public function fetch($id)
266 {
267 $sql = "SELECT";
268 $sql .= " t.rowid,";
269 $sql .= " t.localtaxtype,";
270 $sql .= " t.tms,";
271 $sql .= " t.datep,";
272 $sql .= " t.datev,";
273 $sql .= " t.amount,";
274 $sql .= " t.label,";
275 $sql .= " t.note as note_private,";
276 $sql .= " t.entity,";
277 $sql .= " t.fk_bank,";
278 $sql .= " t.fk_user_creat,";
279 $sql .= " t.fk_user_modif,";
280 $sql .= " b.fk_account,";
281 $sql .= " b.fk_type,";
282 $sql .= " b.rappro";
283 $sql .= " FROM ".MAIN_DB_PREFIX."localtax as t";
284 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."bank as b ON t.fk_bank = b.rowid";
285 $sql .= " WHERE t.rowid = ".((int) $id);
286
287 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
288 $resql = $this->db->query($sql);
289 if ($resql) {
290 if ($this->db->num_rows($resql)) {
291 $obj = $this->db->fetch_object($resql);
292
293 $this->id = $obj->rowid;
294 $this->ref = $obj->rowid;
295 $this->ltt = $obj->localtaxtype;
296 $this->tms = $this->db->jdate($obj->tms);
297 $this->datep = $this->db->jdate($obj->datep);
298 $this->datev = $this->db->jdate($obj->datev);
299 $this->amount = $obj->amount;
300 $this->label = $obj->label;
301 $this->note = $obj->note_private;
302 $this->note_private = $obj->note_private;
303 $this->entity = $obj->entity;
304 $this->fk_bank = $obj->fk_bank;
305 $this->fk_user_creat = $obj->fk_user_creat;
306 $this->fk_user_modif = $obj->fk_user_modif;
307 $this->fk_account = $obj->fk_account;
308 $this->fk_type = $obj->fk_type;
309 $this->rappro = $obj->rappro;
310 }
311 $this->db->free($resql);
312
313 return 1;
314 } else {
315 $this->error = "Error ".$this->db->lasterror();
316 return -1;
317 }
318 }
319
320
327 public function delete($user)
328 {
329 // Call trigger
330 $result = $this->call_trigger('LOCALTAX_DELETE', $user);
331 if ($result < 0) {
332 return -1;
333 }
334 // End call triggers
335
336 $sql = "DELETE FROM ".MAIN_DB_PREFIX."localtax";
337 $sql .= " WHERE rowid=".((int) $this->id);
338
339 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
340 $resql = $this->db->query($sql);
341 if (!$resql) {
342 $this->error = "Error ".$this->db->lasterror();
343 return -1;
344 }
345
346 return 1;
347 }
348
349
357 public function initAsSpecimen()
358 {
359 global $user, $conf;
360
361 $this->id = 0;
362
363 $this->tms = dol_now();
364 $this->ltt = 0;
365 $this->datep = '';
366 $this->datev = '';
367 $this->amount = '';
368 $this->label = '';
369 $this->note = '';
370 $this->entity = $conf->entity;
371 $this->fk_bank = 0;
372 $this->fk_user_creat = $user->id;
373 $this->fk_user_modif = $user->id;
374
375 return 1;
376 }
377
378
385 public function solde($year = 0)
386 {
387 $reglee = $this->localtax_sum_reglee($year);
388
389 $payee = $this->localtax_sum_payee($year);
390 $collectee = $this->localtax_sum_collectee($year);
391
392 $solde = $reglee - ($collectee - $payee);
393
394 return $solde;
395 }
396
397 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
404 public function localtax_sum_collectee($year = 0)
405 {
406 // phpcs:enable
407 $sql = "SELECT sum(f.localtax) as amount";
408 $sql .= " FROM ".MAIN_DB_PREFIX."facture as f";
409 $sql .= " WHERE f.paye = 1";
410 if ($year) {
411 $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'))."'";
412 }
413
414 $result = $this->db->query($sql);
415 if ($result) {
416 if ($this->db->num_rows($result)) {
417 $obj = $this->db->fetch_object($result);
418 $ret = $obj->amount;
419 $this->db->free($result);
420 return $ret;
421 } else {
422 $this->db->free($result);
423 return 0;
424 }
425 } else {
426 print $this->db->lasterror();
427 return -1;
428 }
429 }
430
431 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
438 public function localtax_sum_payee($year = 0)
439 {
440 // phpcs:enable
441
442 $sql = "SELECT sum(f.total_localtax) as total_localtax";
443 $sql .= " FROM ".MAIN_DB_PREFIX."facture_fourn as f";
444 if ($year) {
445 $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'))."'";
446 }
447
448 $result = $this->db->query($sql);
449 if ($result) {
450 if ($this->db->num_rows($result)) {
451 $obj = $this->db->fetch_object($result);
452 $ret = $obj->total_localtax;
453 $this->db->free($result);
454 return $ret;
455 } else {
456 $this->db->free($result);
457 return 0;
458 }
459 } else {
460 print $this->db->lasterror();
461 return -1;
462 }
463 }
464
465
466 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
473 public function localtax_sum_reglee($year = 0)
474 {
475 // phpcs:enable
476
477 $sql = "SELECT sum(f.amount) as amount";
478 $sql .= " FROM ".MAIN_DB_PREFIX."localtax as f";
479 if ($year) {
480 $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'))."'";
481 }
482
483 $result = $this->db->query($sql);
484 if ($result) {
485 if ($this->db->num_rows($result)) {
486 $obj = $this->db->fetch_object($result);
487 $ret = $obj->amount;
488 $this->db->free($result);
489 return $ret;
490 } else {
491 $this->db->free($result);
492 return 0;
493 }
494 } else {
495 print $this->db->lasterror();
496 return -1;
497 }
498 }
499
500
507 public function addPayment($user)
508 {
509 global $conf, $langs;
510
511 $this->db->begin();
512
513 // Check parameters
514 $this->amount = price2num($this->amount);
515 if (!$this->label) {
516 $this->error = $langs->trans("ErrorFieldRequired", $langs->transnoentities("Label"));
517 return -3;
518 }
519 if ($this->amount <= 0) {
520 $this->error = $langs->trans("ErrorFieldRequired", $langs->transnoentities("Amount"));
521 return -4;
522 }
523 if (isModEnabled("bank") && (empty($this->accountid) || $this->accountid <= 0)) {
524 $this->error = $langs->trans("ErrorFieldRequired", $langs->transnoentities("BankAccount"));
525 return -5;
526 }
527 if (isModEnabled("bank") && (empty($this->paymenttype) || $this->paymenttype <= 0)) {
528 $this->error = $langs->trans("ErrorFieldRequired", $langs->transnoentities("PaymentMode"));
529 return -5;
530 }
531
532 // Set entity if not already set
533 if (empty($this->entity)) {
534 $this->entity = $conf->entity;
535 }
536
537 // Insert into localtax payment table
538 $sql = "INSERT INTO ".MAIN_DB_PREFIX."localtax(localtaxtype, datep, datev, amount";
539 if ($this->note) {
540 $sql .= ", note";
541 }
542 if ($this->label) {
543 $sql .= ", label";
544 }
545 $sql .= ", entity, fk_user_creat, fk_bank";
546 $sql .= ") ";
547 $sql .= " VALUES(".((int) $this->ltt).", '".$this->db->idate($this->datep)."', ";
548 $sql .= "'".$this->db->idate($this->datev)."', ".((float) $this->amount);
549 if ($this->note) {
550 $sql .= ", '".$this->db->escape($this->note)."'";
551 }
552 if ($this->label) {
553 $sql .= ", '".$this->db->escape($this->label)."'";
554 }
555 $sql .= ", ".((int) $this->entity).", ".((int) $user->id).", NULL";
556 $sql .= ")";
557
558 dol_syslog(get_class($this)."::addPayment", LOG_DEBUG);
559 $result = $this->db->query($sql);
560 if ($result) {
561 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."localtax"); // TODO devrait s'appeler paiementlocaltax
562 if ($this->id > 0) {
563 $ok = 1;
564 if (isModEnabled("bank")) {
565 // Insert into llx_bank
566 require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
567
568 $acc = new Account($this->db);
569 $result = $acc->fetch($this->accountid);
570 if ($result <= 0) {
571 dol_print_error($this->db);
572 }
573
574 $bank_line_id = $acc->addline($this->datep, $this->paymenttype, $this->label, -abs((float) $this->amount), '', 0, $user);
575
576 // Update fk_bank into llx_localtax so we know the line of localtax used to generate the bank entry.
577 if ($bank_line_id > 0) {
578 $this->update_fk_bank($bank_line_id);
579 } else {
580 $this->error = $acc->error;
581 $ok = 0;
582 }
583
584 // Update the links
585 $result = $acc->add_url_line($bank_line_id, $this->id, DOL_URL_ROOT.'/compta/localtax/card.php?id=', "(VATPayment)", "payment_vat");
586 if ($result < 0) {
587 $this->error = $acc->error;
588 $ok = 0;
589 }
590 }
591
592 if ($ok) {
593 $this->db->commit();
594 return $this->id;
595 } else {
596 $this->db->rollback();
597 return -3;
598 }
599 } else {
600 $this->error = $this->db->lasterror();
601 $this->db->rollback();
602 return -2;
603 }
604 } else {
605 $this->error = $this->db->lasterror();
606 $this->db->rollback();
607 return -1;
608 }
609 }
610
611 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
618 public function update_fk_bank($id)
619 {
620 // phpcs:enable
621 $sql = 'UPDATE '.MAIN_DB_PREFIX.'localtax SET fk_bank = '.((int) $id);
622 $sql .= ' WHERE rowid = '.((int) $this->id);
623 $result = $this->db->query($sql);
624 if ($result) {
625 return 1;
626 } else {
627 dol_print_error($this->db);
628 return -1;
629 }
630 }
631
632
640 public function getNomUrl($withpicto = 0, $option = '')
641 {
642 global $langs;
643
644 $result = '';
645 $label = $langs->trans("ShowVatPayment").': '.$this->ref;
646
647 $link = '<a href="'.DOL_URL_ROOT.'/compta/localtax/card.php?id='.$this->id.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
648 $linkend = '</a>';
649
650 $picto = 'payment';
651
652 if ($withpicto) {
653 $result .= ($link.img_object($label, $picto, 'class="classfortooltip"').$linkend);
654 }
655 if ($withpicto && $withpicto != 2) {
656 $result .= ' ';
657 }
658 if ($withpicto != 2) {
659 $result .= $link.$this->ref.$linkend;
660 }
661 return $result;
662 }
663
670 public function getLibStatut($mode = 0)
671 {
672 return $this->LibStatut($this->statut, $mode);
673 }
674
675 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
683 public function LibStatut($status, $mode = 0)
684 {
685 // phpcs:enable
686 //global $langs;
687
688 return '';
689 }
690
698 public function getKanbanView($option = '', $arraydata = null)
699 {
700 global $langs;
701
702 $selected = (empty($arraydata['selected']) ? 0 : $arraydata['selected']);
703
704 $return = '<div class="box-flex-item box-flex-grow-zero">';
705 $return .= '<div class="info-box info-box-sm">';
706 $return .= '<span class="info-box-icon bg-infobox-action">';
707 $return .= img_picto('', $this->picto);
708 $return .= '</span>';
709 $return .= '<div class="info-box-content">';
710 $return .= '<span class="info-box-ref inline-block tdoverflowmax150 valignmiddle">'.(method_exists($this, 'getNomUrl') ? $this->getNomUrl() : $this->ref).'</span>';
711 if ($selected >= 0) {
712 $return .= '<input id="cb'.$this->id.'" class="flat checkforselect fright" type="checkbox" name="toselect[]" value="'.$this->id.'"'.($selected ? ' checked="checked"' : '').'>';
713 }
714 if (property_exists($this, 'label')) {
715 $return .= ' | <span class="info-box-label">'.$this->label.'</span>';
716 }
717 if (property_exists($this, 'datev')) {
718 $return .= '<br><span class="opacitymedium">'.$langs->trans("DateEnd").'</span> : <span class="info-box-label">'.dol_print_date($this->db->jdate($this->datev), 'day').'</span>';
719 }
720 if (property_exists($this, 'datep')) {
721 $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>';
722 }
723 if (property_exists($this, 'amount')) {
724 $return .= '<br><span class="opacitymedium">'.$langs->trans("Amount").'</span> : <span class="info-box-label amount">'.price($this->amount).'</span>';
725 }
726 $return .= '</div>';
727 $return .= '</div>';
728 $return .= '</div>';
729 return $return;
730 }
731}
$object ref
Definition info.php:90
Class to manage bank accounts.
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 clickable 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 of invoices localtax emitted by the company.
localtax_sum_reglee($year=0)
Total of localtax paid.
Class to manage Dolibarr users.
dol_get_first_day($year, $month=1, $gm=false)
Return GMT time for first day of a month or year.
Definition date.lib.php:605
dol_get_last_day($year, $month=12, $gm=false)
Return GMT time for last day of a month or year.
Definition date.lib.php:624
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
dol_now($mode='gmt')
Return date for now.
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_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false, $decorate=0)
Output date in a string format according to outputlangs (or langs if not defined).
isModEnabled($module)
Is Dolibarr module enabled.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2, $allowothertags=array())
Show picto whatever it's its name (generic function)
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
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...
Definition html.lib.php:172
print $langs trans('Date')." left Ref Label right Qty right Price right TotalHT right TotalTTC right right right right right right right right right centpercent right TotalHT right n right VAT right n right TotalVAT right n No sujeto a RE IRPF right TotalLT1 right n right TotalLT2 right n right TotalTTC right n takeposcustomercurrency takeposcustomercurrency takeposcustomercurrency takeposcustomercurrency right TotalTTC takeposcustomercurrency right takeposcustomercurrency n right Paid right PaymentTypeShortLIQ right SELECT p pos_change as p datep as p p num_paiement as f pf amount as amount
Definition receipt.php:489