dolibarr 19.0.3
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
23require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
24
25
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 $accountid;
56
60 public $fk_type;
61
62 public $paymenttype;
63
67 public $rappro;
68
69
73 public $label;
74
78 public $fk_bank;
79
83 public $fk_user_creat;
84
88 public $fk_user_modif;
89
95 public function __construct($db)
96 {
97 $this->db = $db;
98 }
99
100
107 public function create($user)
108 {
109 global $conf, $langs;
110
111 $error = 0;
112
113 // Clean parameters
114 $this->amount = trim($this->amount);
115 $this->label = trim($this->label);
116 $this->note = trim($this->note);
117
118 // Insert request
119 $sql = "INSERT INTO ".MAIN_DB_PREFIX."localtax(";
120 $sql .= "localtaxtype,";
121 $sql .= "tms,";
122 $sql .= "datep,";
123 $sql .= "datev,";
124 $sql .= "amount,";
125 $sql .= "label,";
126 $sql .= "note,";
127 $sql .= "fk_bank,";
128 $sql .= "fk_user_creat,";
129 $sql .= "fk_user_modif";
130 $sql .= ") VALUES (";
131 $sql .= " ".((int) $this->ltt).",";
132 $sql .= " '".$this->db->idate($this->tms)."',";
133 $sql .= " '".$this->db->idate($this->datep)."',";
134 $sql .= " '".$this->db->idate($this->datev)."',";
135 $sql .= " '".$this->db->escape($this->amount)."',";
136 $sql .= " '".$this->db->escape($this->label)."',";
137 $sql .= " '".$this->db->escape($this->note)."',";
138 $sql .= " ".($this->fk_bank <= 0 ? "NULL" : (int) $this->fk_bank).",";
139 $sql .= " ".((int) $this->fk_user_creat).",";
140 $sql .= " ".((int) $this->fk_user_modif);
141 $sql .= ")";
142
143 dol_syslog(get_class($this)."::create", LOG_DEBUG);
144 $resql = $this->db->query($sql);
145 if ($resql) {
146 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."localtax");
147
148 // Call trigger
149 $result = $this->call_trigger('LOCALTAX_CREATE', $user);
150 if ($result < 0) {
151 $error++;
152 }
153 // End call triggers
154
155 if (!$error) {
156 $this->db->commit();
157 return $this->id;
158 } else {
159 $this->db->rollback();
160 return -1;
161 }
162 } else {
163 $this->error = "Error ".$this->db->lasterror();
164 $this->db->rollback();
165 return -1;
166 }
167 }
168
176 public function update(User $user, $notrigger = 0)
177 {
178 global $conf, $langs;
179
180 $error = 0;
181
182 // Clean parameters
183 $this->amount = trim($this->amount);
184 $this->label = trim($this->label);
185 $this->note = trim($this->note);
186
187 $this->db->begin();
188
189 // Update request
190 $sql = "UPDATE ".MAIN_DB_PREFIX."localtax SET";
191 $sql .= " localtaxtype=".((int) $this->ltt).",";
192 $sql .= " tms='".$this->db->idate($this->tms)."',";
193 $sql .= " datep='".$this->db->idate($this->datep)."',";
194 $sql .= " datev='".$this->db->idate($this->datev)."',";
195 $sql .= " amount=".price2num($this->amount).",";
196 $sql .= " label='".$this->db->escape($this->label)."',";
197 $sql .= " note='".$this->db->escape($this->note)."',";
198 $sql .= " fk_bank=".(int) $this->fk_bank.",";
199 $sql .= " fk_user_creat=".(int) $this->fk_user_creat.",";
200 $sql .= " fk_user_modif=".(int) $this->fk_user_modif;
201 $sql .= " WHERE rowid=".((int) $this->id);
202
203 dol_syslog(get_class($this)."::update", LOG_DEBUG);
204 $resql = $this->db->query($sql);
205 if (!$resql) {
206 $this->error = "Error ".$this->db->lasterror();
207 $error++;
208 }
209
210 if (!$error && !$notrigger) {
211 // Call trigger
212 $result = $this->call_trigger('LOCALTAX_MODIFY', $user);
213 if ($result < 0) {
214 $error++;
215 }
216 // End call triggers
217 }
218
219 if (!$error) {
220 $this->db->commit();
221 return 1;
222 } else {
223 $this->db->rollback();
224 return -1;
225 }
226 }
227
228
235 public function fetch($id)
236 {
237 $sql = "SELECT";
238 $sql .= " t.rowid,";
239 $sql .= " t.localtaxtype,";
240 $sql .= " t.tms,";
241 $sql .= " t.datep,";
242 $sql .= " t.datev,";
243 $sql .= " t.amount,";
244 $sql .= " t.label,";
245 $sql .= " t.note as note_private,";
246 $sql .= " t.fk_bank,";
247 $sql .= " t.fk_user_creat,";
248 $sql .= " t.fk_user_modif,";
249 $sql .= " b.fk_account,";
250 $sql .= " b.fk_type,";
251 $sql .= " b.rappro";
252 $sql .= " FROM ".MAIN_DB_PREFIX."localtax as t";
253 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."bank as b ON t.fk_bank = b.rowid";
254 $sql .= " WHERE t.rowid = ".((int) $id);
255
256 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
257 $resql = $this->db->query($sql);
258 if ($resql) {
259 if ($this->db->num_rows($resql)) {
260 $obj = $this->db->fetch_object($resql);
261
262 $this->id = $obj->rowid;
263 $this->ref = $obj->rowid;
264 $this->ltt = $obj->localtaxtype;
265 $this->tms = $this->db->jdate($obj->tms);
266 $this->datep = $this->db->jdate($obj->datep);
267 $this->datev = $this->db->jdate($obj->datev);
268 $this->amount = $obj->amount;
269 $this->label = $obj->label;
270 $this->note = $obj->note_private;
271 $this->note_private = $obj->note_private;
272 $this->fk_bank = $obj->fk_bank;
273 $this->fk_user_creat = $obj->fk_user_creat;
274 $this->fk_user_modif = $obj->fk_user_modif;
275 $this->fk_account = $obj->fk_account;
276 $this->fk_type = $obj->fk_type;
277 $this->rappro = $obj->rappro;
278 }
279 $this->db->free($resql);
280
281 return 1;
282 } else {
283 $this->error = "Error ".$this->db->lasterror();
284 return -1;
285 }
286 }
287
288
295 public function delete($user)
296 {
297 // Call trigger
298 $result = $this->call_trigger('LOCALTAX_DELETE', $user);
299 if ($result < 0) {
300 return -1;
301 }
302 // End call triggers
303
304 $sql = "DELETE FROM ".MAIN_DB_PREFIX."localtax";
305 $sql .= " WHERE rowid=".((int) $this->id);
306
307 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
308 $resql = $this->db->query($sql);
309 if (!$resql) {
310 $this->error = "Error ".$this->db->lasterror();
311 return -1;
312 }
313
314 return 1;
315 }
316
317
325 public function initAsSpecimen()
326 {
327 global $user;
328
329 $this->id = 0;
330
331 $this->tms = '';
332 $this->ltt = 0;
333 $this->datep = '';
334 $this->datev = '';
335 $this->amount = '';
336 $this->label = '';
337 $this->note = '';
338 $this->fk_bank = 0;
339 $this->fk_user_creat = $user->id;
340 $this->fk_user_modif = $user->id;
341 }
342
343
350 public function solde($year = 0)
351 {
352 $reglee = $this->localtax_sum_reglee($year);
353
354 $payee = $this->localtax_sum_payee($year);
355 $collectee = $this->localtax_sum_collectee($year);
356
357 $solde = $reglee - ($collectee - $payee);
358
359 return $solde;
360 }
361
362 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
369 public function localtax_sum_collectee($year = 0)
370 {
371 // phpcs:enable
372 $sql = "SELECT sum(f.localtax) as amount";
373 $sql .= " FROM ".MAIN_DB_PREFIX."facture as f";
374 $sql .= " WHERE f.paye = 1";
375 if ($year) {
376 $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'))."'";
377 }
378
379 $result = $this->db->query($sql);
380 if ($result) {
381 if ($this->db->num_rows($result)) {
382 $obj = $this->db->fetch_object($result);
383 $ret = $obj->amount;
384 $this->db->free($result);
385 return $ret;
386 } else {
387 $this->db->free($result);
388 return 0;
389 }
390 } else {
391 print $this->db->lasterror();
392 return -1;
393 }
394 }
395
396 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
403 public function localtax_sum_payee($year = 0)
404 {
405 // phpcs:enable
406
407 $sql = "SELECT sum(f.total_localtax) as total_localtax";
408 $sql .= " FROM ".MAIN_DB_PREFIX."facture_fourn as f";
409 if ($year) {
410 $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'))."'";
411 }
412
413 $result = $this->db->query($sql);
414 if ($result) {
415 if ($this->db->num_rows($result)) {
416 $obj = $this->db->fetch_object($result);
417 $ret = $obj->total_localtax;
418 $this->db->free($result);
419 return $ret;
420 } else {
421 $this->db->free($result);
422 return 0;
423 }
424 } else {
425 print $this->db->lasterror();
426 return -1;
427 }
428 }
429
430
431 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
438 public function localtax_sum_reglee($year = 0)
439 {
440 // phpcs:enable
441
442 $sql = "SELECT sum(f.amount) as amount";
443 $sql .= " FROM ".MAIN_DB_PREFIX."localtax as f";
444 if ($year) {
445 $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'))."'";
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->amount;
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
472 public function addPayment($user)
473 {
474 global $conf, $langs;
475
476 $this->db->begin();
477
478 // Check parameters
479 $this->amount = price2num($this->amount);
480 if (!$this->label) {
481 $this->error = $langs->trans("ErrorFieldRequired", $langs->transnoentities("Label"));
482 return -3;
483 }
484 if ($this->amount <= 0) {
485 $this->error = $langs->trans("ErrorFieldRequired", $langs->transnoentities("Amount"));
486 return -4;
487 }
488 if (isModEnabled("banque") && (empty($this->accountid) || $this->accountid <= 0)) {
489 $this->error = $langs->trans("ErrorFieldRequired", $langs->transnoentities("Account"));
490 return -5;
491 }
492 if (isModEnabled("banque") && (empty($this->paymenttype) || $this->paymenttype <= 0)) {
493 $this->error = $langs->trans("ErrorFieldRequired", $langs->transnoentities("PaymentMode"));
494 return -5;
495 }
496
497 // Insertion dans table des paiement localtax
498 $sql = "INSERT INTO ".MAIN_DB_PREFIX."localtax (localtaxtype, datep, datev, amount";
499 if ($this->note) {
500 $sql .= ", note";
501 }
502 if ($this->label) {
503 $sql .= ", label";
504 }
505 $sql .= ", fk_user_creat, fk_bank";
506 $sql .= ") ";
507 $sql .= " VALUES (".$this->ltt.", '".$this->db->idate($this->datep)."',";
508 $sql .= "'".$this->db->idate($this->datev)."',".$this->amount;
509 if ($this->note) {
510 $sql .= ", '".$this->db->escape($this->note)."'";
511 }
512 if ($this->label) {
513 $sql .= ", '".$this->db->escape($this->label)."'";
514 }
515 $sql .= ", ".((int) $user->id).", NULL";
516 $sql .= ")";
517
518 dol_syslog(get_class($this)."::addPayment", LOG_DEBUG);
519 $result = $this->db->query($sql);
520 if ($result) {
521 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."localtax"); // TODO devrait s'appeler paiementlocaltax
522 if ($this->id > 0) {
523 $ok = 1;
524 if (isModEnabled("banque")) {
525 // Insertion dans llx_bank
526 require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
527
528 $acc = new Account($this->db);
529 $result = $acc->fetch($this->accountid);
530 if ($result <= 0) {
531 dol_print_error($this->db);
532 }
533
534 $bank_line_id = $acc->addline($this->datep, $this->paymenttype, $this->label, -abs($this->amount), '', '', $user);
535
536 // Update fk_bank into llx_localtax so we know the line of localtax used to generate the bank entry.
537 if ($bank_line_id > 0) {
538 $this->update_fk_bank($bank_line_id);
539 } else {
540 $this->error = $acc->error;
541 $ok = 0;
542 }
543
544 // Mise a jour liens
545 $result = $acc->add_url_line($bank_line_id, $this->id, DOL_URL_ROOT.'/compta/localtax/card.php?id=', "(VATPayment)", "payment_vat");
546 if ($result < 0) {
547 $this->error = $acc->error;
548 $ok = 0;
549 }
550 }
551
552 if ($ok) {
553 $this->db->commit();
554 return $this->id;
555 } else {
556 $this->db->rollback();
557 return -3;
558 }
559 } else {
560 $this->error = $this->db->lasterror();
561 $this->db->rollback();
562 return -2;
563 }
564 } else {
565 $this->error = $this->db->lasterror();
566 $this->db->rollback();
567 return -1;
568 }
569 }
570
571 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
578 public function update_fk_bank($id)
579 {
580 // phpcs:enable
581 $sql = 'UPDATE '.MAIN_DB_PREFIX.'localtax SET fk_bank = '.((int) $id);
582 $sql .= ' WHERE rowid = '.((int) $this->id);
583 $result = $this->db->query($sql);
584 if ($result) {
585 return 1;
586 } else {
587 dol_print_error($this->db);
588 return -1;
589 }
590 }
591
592
600 public function getNomUrl($withpicto = 0, $option = '')
601 {
602 global $langs;
603
604 $result = '';
605 $label = $langs->trans("ShowVatPayment").': '.$this->ref;
606
607 $link = '<a href="'.DOL_URL_ROOT.'/compta/localtax/card.php?id='.$this->id.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
608 $linkend = '</a>';
609
610 $picto = 'payment';
611
612 if ($withpicto) {
613 $result .= ($link.img_object($label, $picto, 'class="classfortooltip"').$linkend);
614 }
615 if ($withpicto && $withpicto != 2) {
616 $result .= ' ';
617 }
618 if ($withpicto != 2) {
619 $result .= $link.$this->ref.$linkend;
620 }
621 return $result;
622 }
623
630 public function getLibStatut($mode = 0)
631 {
632 return $this->LibStatut($this->statut, $mode);
633 }
634
635 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
643 public function LibStatut($status, $mode = 0)
644 {
645 // phpcs:enable
646 //global $langs;
647
648 return '';
649 }
650
658 public function getKanbanView($option = '', $arraydata = null)
659 {
660 global $langs;
661
662 $selected = (empty($arraydata['selected']) ? 0 : $arraydata['selected']);
663
664 $return = '<div class="box-flex-item box-flex-grow-zero">';
665 $return .= '<div class="info-box info-box-sm">';
666 $return .= '<span class="info-box-icon bg-infobox-action">';
667 $return .= img_picto('', $this->picto);
668 $return .= '</span>';
669 $return .= '<div class="info-box-content">';
670 $return .= '<span class="info-box-ref inline-block tdoverflowmax150 valignmiddle">'.(method_exists($this, 'getNomUrl') ? $this->getNomUrl() : $this->ref).'</span>';
671 if ($selected >= 0) {
672 $return .= '<input id="cb'.$this->id.'" class="flat checkforselect fright" type="checkbox" name="toselect[]" value="'.$this->id.'"'.($selected ? ' checked="checked"' : '').'>';
673 }
674 if (property_exists($this, 'label')) {
675 $return .= ' | <span class="info-box-label">'.$this->label.'</span>';
676 }
677 if (property_exists($this, 'datev')) {
678 $return .= '<br><span class="opacitymedium">'.$langs->trans("DateEnd").'</span> : <span class="info-box-label">'.dol_print_date($this->db->jdate($this->datev), 'day').'</span>';
679 }
680 if (property_exists($this, 'datep')) {
681 $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>';
682 }
683 if (property_exists($this, 'amount')) {
684 $return .= '<br><span class="opacitymedium">'.$langs->trans("Amount").'</span> : <span class="info-box-label amount">'.price($this->amount).'</span>';
685 }
686 $return .= '</div>';
687 $return .= '</div>';
688 $return .= '</div>';
689 return $return;
690 }
691}
$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 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.
dol_get_first_day($year, $month=1, $gm=false)
Return GMT time for first day of a month or year.
Definition date.lib.php:594
dol_get_last_day($year, $month=12, $gm=false)
Return GMT time for last day of a month or year.
Definition date.lib.php:613
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)
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...
publicphonebutton2 phonegreen basiclayout basiclayout TotalHT VATCode TotalVAT TotalLT1 TotalLT2 TotalTTC TotalHT clearboth nowraponall right right takeposterminal SELECT e e e e e statut
Definition invoice.php:1926