dolibarr 21.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 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
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
112 public function __construct($db)
113 {
114 $this->db = $db;
115 }
116
117
124 public function create($user)
125 {
126 global $conf, $langs;
127
128 $error = 0;
129
130 // Clean parameters
131 $this->amount = trim($this->amount);
132 $this->label = trim($this->label);
133 $this->note = trim($this->note);
134
135 // Insert request
136 $sql = "INSERT INTO ".MAIN_DB_PREFIX."localtax(";
137 $sql .= "localtaxtype,";
138 $sql .= "tms,";
139 $sql .= "datep,";
140 $sql .= "datev,";
141 $sql .= "amount,";
142 $sql .= "label,";
143 $sql .= "note,";
144 $sql .= "fk_bank,";
145 $sql .= "fk_user_creat,";
146 $sql .= "fk_user_modif";
147 $sql .= ") VALUES (";
148 $sql .= " ".((int) $this->ltt).",";
149 $sql .= " '".$this->db->idate($this->tms)."',";
150 $sql .= " '".$this->db->idate($this->datep)."',";
151 $sql .= " '".$this->db->idate($this->datev)."',";
152 $sql .= " '".$this->db->escape($this->amount)."',";
153 $sql .= " '".$this->db->escape($this->label)."',";
154 $sql .= " '".$this->db->escape($this->note)."',";
155 $sql .= " ".($this->fk_bank <= 0 ? "NULL" : (int) $this->fk_bank).",";
156 $sql .= " ".((int) $this->fk_user_creat).",";
157 $sql .= " ".((int) $this->fk_user_modif);
158 $sql .= ")";
159
160 dol_syslog(get_class($this)."::create", LOG_DEBUG);
161 $resql = $this->db->query($sql);
162 if ($resql) {
163 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."localtax");
164
165 // Call trigger
166 $result = $this->call_trigger('LOCALTAX_CREATE', $user);
167 if ($result < 0) {
168 $error++;
169 }
170 // End call triggers
171
172 if (!$error) {
173 $this->db->commit();
174 return $this->id;
175 } else {
176 $this->db->rollback();
177 return -1;
178 }
179 } else {
180 $this->error = "Error ".$this->db->lasterror();
181 $this->db->rollback();
182 return -1;
183 }
184 }
185
193 public function update(User $user, $notrigger = 0)
194 {
195 global $conf, $langs;
196
197 $error = 0;
198
199 // Clean parameters
200 $this->amount = trim($this->amount);
201 $this->label = trim($this->label);
202 $this->note = trim($this->note);
203
204 $this->db->begin();
205
206 // Update request
207 $sql = "UPDATE ".MAIN_DB_PREFIX."localtax SET";
208 $sql .= " localtaxtype=".((int) $this->ltt).",";
209 $sql .= " tms='".$this->db->idate($this->tms)."',";
210 $sql .= " datep='".$this->db->idate($this->datep)."',";
211 $sql .= " datev='".$this->db->idate($this->datev)."',";
212 $sql .= " amount=".price2num($this->amount).",";
213 $sql .= " label='".$this->db->escape($this->label)."',";
214 $sql .= " note='".$this->db->escape($this->note)."',";
215 $sql .= " fk_bank=".(int) $this->fk_bank.",";
216 $sql .= " fk_user_creat=".(int) $this->fk_user_creat.",";
217 $sql .= " fk_user_modif=".(int) $this->fk_user_modif;
218 $sql .= " WHERE rowid=".((int) $this->id);
219
220 dol_syslog(get_class($this)."::update", LOG_DEBUG);
221 $resql = $this->db->query($sql);
222 if (!$resql) {
223 $this->error = "Error ".$this->db->lasterror();
224 $error++;
225 }
226
227 if (!$error && !$notrigger) {
228 // Call trigger
229 $result = $this->call_trigger('LOCALTAX_MODIFY', $user);
230 if ($result < 0) {
231 $error++;
232 }
233 // End call triggers
234 }
235
236 if (!$error) {
237 $this->db->commit();
238 return 1;
239 } else {
240 $this->db->rollback();
241 return -1;
242 }
243 }
244
245
252 public function fetch($id)
253 {
254 $sql = "SELECT";
255 $sql .= " t.rowid,";
256 $sql .= " t.localtaxtype,";
257 $sql .= " t.tms,";
258 $sql .= " t.datep,";
259 $sql .= " t.datev,";
260 $sql .= " t.amount,";
261 $sql .= " t.label,";
262 $sql .= " t.note as note_private,";
263 $sql .= " t.fk_bank,";
264 $sql .= " t.fk_user_creat,";
265 $sql .= " t.fk_user_modif,";
266 $sql .= " b.fk_account,";
267 $sql .= " b.fk_type,";
268 $sql .= " b.rappro";
269 $sql .= " FROM ".MAIN_DB_PREFIX."localtax as t";
270 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."bank as b ON t.fk_bank = b.rowid";
271 $sql .= " WHERE t.rowid = ".((int) $id);
272
273 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
274 $resql = $this->db->query($sql);
275 if ($resql) {
276 if ($this->db->num_rows($resql)) {
277 $obj = $this->db->fetch_object($resql);
278
279 $this->id = $obj->rowid;
280 $this->ref = $obj->rowid;
281 $this->ltt = $obj->localtaxtype;
282 $this->tms = $this->db->jdate($obj->tms);
283 $this->datep = $this->db->jdate($obj->datep);
284 $this->datev = $this->db->jdate($obj->datev);
285 $this->amount = $obj->amount;
286 $this->label = $obj->label;
287 $this->note = $obj->note_private;
288 $this->note_private = $obj->note_private;
289 $this->fk_bank = $obj->fk_bank;
290 $this->fk_user_creat = $obj->fk_user_creat;
291 $this->fk_user_modif = $obj->fk_user_modif;
292 $this->fk_account = $obj->fk_account;
293 $this->fk_type = $obj->fk_type;
294 $this->rappro = $obj->rappro;
295 }
296 $this->db->free($resql);
297
298 return 1;
299 } else {
300 $this->error = "Error ".$this->db->lasterror();
301 return -1;
302 }
303 }
304
305
312 public function delete($user)
313 {
314 // Call trigger
315 $result = $this->call_trigger('LOCALTAX_DELETE', $user);
316 if ($result < 0) {
317 return -1;
318 }
319 // End call triggers
320
321 $sql = "DELETE FROM ".MAIN_DB_PREFIX."localtax";
322 $sql .= " WHERE rowid=".((int) $this->id);
323
324 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
325 $resql = $this->db->query($sql);
326 if (!$resql) {
327 $this->error = "Error ".$this->db->lasterror();
328 return -1;
329 }
330
331 return 1;
332 }
333
334
342 public function initAsSpecimen()
343 {
344 global $user;
345
346 $this->id = 0;
347
348 $this->tms = dol_now();
349 $this->ltt = 0;
350 $this->datep = '';
351 $this->datev = '';
352 $this->amount = '';
353 $this->label = '';
354 $this->note = '';
355 $this->fk_bank = 0;
356 $this->fk_user_creat = $user->id;
357 $this->fk_user_modif = $user->id;
358
359 return 1;
360 }
361
362
369 public function solde($year = 0)
370 {
371 $reglee = $this->localtax_sum_reglee($year);
372
373 $payee = $this->localtax_sum_payee($year);
374 $collectee = $this->localtax_sum_collectee($year);
375
376 $solde = $reglee - ($collectee - $payee);
377
378 return $solde;
379 }
380
381 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
388 public function localtax_sum_collectee($year = 0)
389 {
390 // phpcs:enable
391 $sql = "SELECT sum(f.localtax) as amount";
392 $sql .= " FROM ".MAIN_DB_PREFIX."facture as f";
393 $sql .= " WHERE f.paye = 1";
394 if ($year) {
395 $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'))."'";
396 }
397
398 $result = $this->db->query($sql);
399 if ($result) {
400 if ($this->db->num_rows($result)) {
401 $obj = $this->db->fetch_object($result);
402 $ret = $obj->amount;
403 $this->db->free($result);
404 return $ret;
405 } else {
406 $this->db->free($result);
407 return 0;
408 }
409 } else {
410 print $this->db->lasterror();
411 return -1;
412 }
413 }
414
415 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
422 public function localtax_sum_payee($year = 0)
423 {
424 // phpcs:enable
425
426 $sql = "SELECT sum(f.total_localtax) as total_localtax";
427 $sql .= " FROM ".MAIN_DB_PREFIX."facture_fourn as f";
428 if ($year) {
429 $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'))."'";
430 }
431
432 $result = $this->db->query($sql);
433 if ($result) {
434 if ($this->db->num_rows($result)) {
435 $obj = $this->db->fetch_object($result);
436 $ret = $obj->total_localtax;
437 $this->db->free($result);
438 return $ret;
439 } else {
440 $this->db->free($result);
441 return 0;
442 }
443 } else {
444 print $this->db->lasterror();
445 return -1;
446 }
447 }
448
449
450 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
457 public function localtax_sum_reglee($year = 0)
458 {
459 // phpcs:enable
460
461 $sql = "SELECT sum(f.amount) as amount";
462 $sql .= " FROM ".MAIN_DB_PREFIX."localtax as f";
463 if ($year) {
464 $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'))."'";
465 }
466
467 $result = $this->db->query($sql);
468 if ($result) {
469 if ($this->db->num_rows($result)) {
470 $obj = $this->db->fetch_object($result);
471 $ret = $obj->amount;
472 $this->db->free($result);
473 return $ret;
474 } else {
475 $this->db->free($result);
476 return 0;
477 }
478 } else {
479 print $this->db->lasterror();
480 return -1;
481 }
482 }
483
484
491 public function addPayment($user)
492 {
493 global $conf, $langs;
494
495 $this->db->begin();
496
497 // Check parameters
498 $this->amount = price2num($this->amount);
499 if (!$this->label) {
500 $this->error = $langs->trans("ErrorFieldRequired", $langs->transnoentities("Label"));
501 return -3;
502 }
503 if ($this->amount <= 0) {
504 $this->error = $langs->trans("ErrorFieldRequired", $langs->transnoentities("Amount"));
505 return -4;
506 }
507 if (isModEnabled("bank") && (empty($this->accountid) || $this->accountid <= 0)) {
508 $this->error = $langs->trans("ErrorFieldRequired", $langs->transnoentities("BankAccount"));
509 return -5;
510 }
511 if (isModEnabled("bank") && (empty($this->paymenttype) || $this->paymenttype <= 0)) {
512 $this->error = $langs->trans("ErrorFieldRequired", $langs->transnoentities("PaymentMode"));
513 return -5;
514 }
515
516 // Insertion dans table des paiement localtax
517 $sql = "INSERT INTO ".MAIN_DB_PREFIX."localtax (localtaxtype, datep, datev, amount";
518 if ($this->note) {
519 $sql .= ", note";
520 }
521 if ($this->label) {
522 $sql .= ", label";
523 }
524 $sql .= ", fk_user_creat, fk_bank";
525 $sql .= ") ";
526 $sql .= " VALUES (".$this->ltt.", '".$this->db->idate($this->datep)."',";
527 $sql .= "'".$this->db->idate($this->datev)."',".$this->amount;
528 if ($this->note) {
529 $sql .= ", '".$this->db->escape($this->note)."'";
530 }
531 if ($this->label) {
532 $sql .= ", '".$this->db->escape($this->label)."'";
533 }
534 $sql .= ", ".((int) $user->id).", NULL";
535 $sql .= ")";
536
537 dol_syslog(get_class($this)."::addPayment", LOG_DEBUG);
538 $result = $this->db->query($sql);
539 if ($result) {
540 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."localtax"); // TODO devrait s'appeler paiementlocaltax
541 if ($this->id > 0) {
542 $ok = 1;
543 if (isModEnabled("bank")) {
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) {
550 dol_print_error($this->db);
551 }
552
553 $bank_line_id = $acc->addline($this->datep, $this->paymenttype, $this->label, -abs((float) $this->amount), '', 0, $user);
554
555 // Update fk_bank into llx_localtax so we know the line of localtax used to generate the bank entry.
556 if ($bank_line_id > 0) {
557 $this->update_fk_bank($bank_line_id);
558 } else {
559 $this->error = $acc->error;
560 $ok = 0;
561 }
562
563 // Update the links
564 $result = $acc->add_url_line($bank_line_id, $this->id, DOL_URL_ROOT.'/compta/localtax/card.php?id=', "(VATPayment)", "payment_vat");
565 if ($result < 0) {
566 $this->error = $acc->error;
567 $ok = 0;
568 }
569 }
570
571 if ($ok) {
572 $this->db->commit();
573 return $this->id;
574 } else {
575 $this->db->rollback();
576 return -3;
577 }
578 } else {
579 $this->error = $this->db->lasterror();
580 $this->db->rollback();
581 return -2;
582 }
583 } else {
584 $this->error = $this->db->lasterror();
585 $this->db->rollback();
586 return -1;
587 }
588 }
589
590 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
597 public function update_fk_bank($id)
598 {
599 // phpcs:enable
600 $sql = 'UPDATE '.MAIN_DB_PREFIX.'localtax SET fk_bank = '.((int) $id);
601 $sql .= ' WHERE rowid = '.((int) $this->id);
602 $result = $this->db->query($sql);
603 if ($result) {
604 return 1;
605 } else {
606 dol_print_error($this->db);
607 return -1;
608 }
609 }
610
611
619 public function getNomUrl($withpicto = 0, $option = '')
620 {
621 global $langs;
622
623 $result = '';
624 $label = $langs->trans("ShowVatPayment").': '.$this->ref;
625
626 $link = '<a href="'.DOL_URL_ROOT.'/compta/localtax/card.php?id='.$this->id.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
627 $linkend = '</a>';
628
629 $picto = 'payment';
630
631 if ($withpicto) {
632 $result .= ($link.img_object($label, $picto, 'class="classfortooltip"').$linkend);
633 }
634 if ($withpicto && $withpicto != 2) {
635 $result .= ' ';
636 }
637 if ($withpicto != 2) {
638 $result .= $link.$this->ref.$linkend;
639 }
640 return $result;
641 }
642
649 public function getLibStatut($mode = 0)
650 {
651 return $this->LibStatut($this->statut, $mode);
652 }
653
654 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
662 public function LibStatut($status, $mode = 0)
663 {
664 // phpcs:enable
665 //global $langs;
666
667 return '';
668 }
669
677 public function getKanbanView($option = '', $arraydata = null)
678 {
679 global $langs;
680
681 $selected = (empty($arraydata['selected']) ? 0 : $arraydata['selected']);
682
683 $return = '<div class="box-flex-item box-flex-grow-zero">';
684 $return .= '<div class="info-box info-box-sm">';
685 $return .= '<span class="info-box-icon bg-infobox-action">';
686 $return .= img_picto('', $this->picto);
687 $return .= '</span>';
688 $return .= '<div class="info-box-content">';
689 $return .= '<span class="info-box-ref inline-block tdoverflowmax150 valignmiddle">'.(method_exists($this, 'getNomUrl') ? $this->getNomUrl() : $this->ref).'</span>';
690 if ($selected >= 0) {
691 $return .= '<input id="cb'.$this->id.'" class="flat checkforselect fright" type="checkbox" name="toselect[]" value="'.$this->id.'"'.($selected ? ' checked="checked"' : '').'>';
692 }
693 if (property_exists($this, 'label')) {
694 $return .= ' | <span class="info-box-label">'.$this->label.'</span>';
695 }
696 if (property_exists($this, 'datev')) {
697 $return .= '<br><span class="opacitymedium">'.$langs->trans("DateEnd").'</span> : <span class="info-box-label">'.dol_print_date($this->db->jdate($this->datev), 'day').'</span>';
698 }
699 if (property_exists($this, 'datep')) {
700 $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>';
701 }
702 if (property_exists($this, 'amount')) {
703 $return .= '<br><span class="opacitymedium">'.$langs->trans("Amount").'</span> : <span class="info-box-label amount">'.price($this->amount).'</span>';
704 }
705 $return .= '</div>';
706 $return .= '</div>';
707 $return .= '</div>';
708 return $return;
709 }
710}
$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 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 de la localtax des factures emises par la societe.
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:596
dol_get_last_day($year, $month=12, $gm=false)
Return GMT time for last day of a month or year.
Definition date.lib.php:615
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...
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...