noalyss Version-10
NOALYSS : serveur de comptabilité et ERP (2002)
Loading...
Searching...
No Matches
tva_rate_mtable.class.php
Go to the documentation of this file.
1<?php
2
3/*
4 * This file is part of NOALYSS.
5 *
6 * PhpCompta 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 2 of the License, or
9 * (at your option) any later version.
10 *
11 * NOALYSS 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 PhpCompta; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20// Copyright (2018) Author Dany De Bontridder <dany@alchimerys.be>
21
22/**
23 * @file
24 * @brief Configure the tva : code , rate, label ...
25 */
26require_once NOALYSS_INCLUDE."/database/v_tva_rate_sql.class.php";
27require_once NOALYSS_INCLUDE."/database/tva_rate_sql.class.php";
28
29/**
30 * @class Tva_Rate_MTable
31 * @brief Configure the tva : code , rate, label ...
32 * When using Manage_Table_SQL
33 */
35{
36
37 //!< previous tva_id, used to know if we update or insert,
38 private $previous_id;
39 private $a_comment; //!< Supplemental explanation
40
41 /**
42 *
43 * @param V_Tva_rate_SQL $p_table
44 * @example tva_parameter.php
45 */
46 function __construct(V_Tva_rate_SQL $p_table)
47 {
48 parent::__construct($p_table);
49 $this->icon_mod='left';
50 $this->set_col_label("tva_id", _("id"));
51 $this->set_col_label("tva_code", _("Code"));
52 $this->set_col_label("tva_label", _("label"));
53 $this->set_col_label("tva_rate", _("taux"));
54 $this->set_col_label("tva_comment", _("Description"));
55 $this->set_col_label("tva_both_side", _("Autoliquidation"));
56 $this->set_col_label("tva_reverse_account", _('Poste comptable autoliquidation'));
57 $this->set_col_label("tva_sale", _("TVA Vente (C)"));
58 $this->set_col_label("tva_purchase", _("TVA Achat (D)"));
59 $this->set_col_label("tva_peppol_code", _("Code Facture électronique UBL"));
60 $this->set_col_label("vx_code", _("Code Exemption TVA (vatex)"));
61
62 $this->set_property_visible('tva_reverse_account', false);
63 $this->set_property_visible('tva_peppol_code', false);
64
65 $this->set_col_type("tva_both_side", "select",
66 array(
67 ["value"=>0, "label"=>_("Non")],
68 ["value"=>1, "label"=>_("Oui")]
69 ));
70 $this->set_col_type("tva_reverse_account", 'custom');
71 $this->set_property_updatable("tva_id", true);
72 $this->set_col_label("tva_payment_purchase",_("Exigible achat"));
73 $this->set_col_type("tva_payment_purchase","select",
74 array(
75 array("value"=>'O',"label"=>"Opération"),
76 array("value"=>'P',"label"=>"Paiement")
77 )
78 );
79 $this->set_col_label("tva_payment_sale",_("Exigible vente"));
80 $this->set_col_type("tva_payment_sale","select",
81 array(
82 array("value"=>'O',"label"=>"Opération"),
83 array("value"=>'P',"label"=>"Paiement")
84 )
85 );
86 $this->a_info=[
87 "tva_purchase"=>44,"tva_both_side"=>43,"tva_sale"=>45
88 ,"tva_payment_sale"=>74,"tva_payment_purchase"=>74,'tva_reverse_account'=>88];
89 $this->previous_id=null;
90 $this->a_comment=array(
91 'tva_purchase'=>_("Ne donnez pas ce poste comptable si ce code n'est pas utilisé à l'achat"),
92 'tva_both_side'=>_("Autoliquidation : Utilisé en même temps au crédit et au débit"),
93 'tva_sale'=>_("Ne donnez pas ce poste comptable si ce code n'est pas utilisé à la vente"),
94 'tva_payment_purchase'=>_('TVA due ou récupérable quand l\'opération est payée ou exécutée'),
95 'tva_payment_sale'=>_('TVA due ou récupérable quand l\'opération est payée ou exécutée'),
96 'tva_reverse_account'=>_("Forcer ce poste comptable pour autoliquidation : par défault, le poste d'autoliquidation est calculé : soit celui qui est en contrepartie, soit le même (voir manuel)"),
97 'tva_peppol_code'=>_("Code TVA est utilisé pour les factures électroniques, plus d'information dans le manuel"),
98 'vx_code'=>_("Code exemption pour facture PEPPOL (ubl)")
99 );
100 $this->setTitle(_("Détail TVA"));
101 }
102
103 /**
104 * @return int or null
105 */
106 public function getPreviousId()
107 {
108 return $this->previous_id;
109 }
110
111 /**
112 *
113 * @param int $previous_id
114 */
116 {
117 $this->previous_id = $previous_id;
118 return $this;
119 }
120
121 /**
122 * @brief display into a dialog box the datarow in order
123 * to be appended or modified. Can be override if you need
124 * a more complex form
125 */
126 function input()
127 {
128 $nb_order=count($this->a_order);
129 $this->set_property_visible('tva_reverse_account', true);
130 $this->set_property_visible('tva_peppol_code', true);
131 echo "<table>";
132 for ($i=0; $i<$nb_order; $i++)
133 {
134 echo "<tr>";
135 $key=$this->a_order[$i];
136 $label=$this->a_label_displaid[$key];
137 $value=$this->table->get($key);
138 $error=$this->get_error($key);
139 $error=($error=="")?"":HtmlInput::errorbulle($error);
140
141 if ($this->get_property_visible($key)===TRUE)
142 {
143 $info="";
144 if ( isset($this->a_info[$key])) {
145 $info=Icon_Action::infobulle($this->a_info[$key]);
146 }
147 // Label
148 echo "<td> {$label} {$info} {$error}</td>";
149
150 if ($this->get_property_updatable($key)==TRUE)
151 {
152 echo "<td>";
153 if ($this->a_type[$key]=="select")
154 {
155 $select=new ISelect($key);
156 $select->value=$this->a_select[$key];
157 $select->selected=$value;
158 echo $select->input();
159 }
160 elseif ($key=="tva_rate")
161 {
162 $text=new INum($key);
163 $text->value=$value;
164 $text->prec=4;
165 $min_size=(strlen($value??"")<10)?10:strlen($value)+1;
166 $text->size=$min_size;
167 echo $text->input();
168 }
169 elseif ($key=='tva_purchase')
170 {
171 $text=new IPoste("tva_purchase");
172 $text->value=$value;
173 $min_size=10;
174 $text->size=$min_size;
175 $text->set_attribute('gDossier', Dossier::id());
176 $text->set_attribute('jrn', 0);
177 $text->set_attribute('account', 'tva_purchase');
178 echo $text->input();
179 $url="do.php?".http_build_query(array("gDossier"=>Dossier::id(),"ac"=>'C0PCMN','p_start'=>4));
180 echo HtmlInput::anchor(_("Configuration poste comptable"),$url,"",'target="_blank"');
181 }
182 elseif ($key=='tva_sale')
183 {
184 $text=new IPoste("tva_sale");
185 $text->value=$value;
186 $min_size=10;
187 $text->set_attribute('gDossier', Dossier::id());
188 $text->set_attribute('jrn', 0);
189 $text->set_attribute('account', 'tva_sale');
190 $text->size=$min_size;
191 echo $text->input();
192 }elseif ($key=='tva_peppol_code') {
193 $text=new ISelect('tva_peppol_code');
194 $text->selected=$value;
195 $text->transform(array(
196 null=>_('-')
197 ,"S"=>_('S Taux standard')
198 ,'AE'=>_('AE Autoliquidate mais pas INTRACOMM.')
199 ,'Z'=>_("Z TVA à 0%")
200 ,'K'=>_('K Autoliquidation INTRACOMM.')
201 ,'G'=>_('G TVA exempt pour export hors Europe')
202 ,'O'=>_('O TVA Hors périmètre application')
203 ,'E'=>_('E Exempté de TVA')
204 ));
205 echo $text->input();
206 } elseif ($key == 'vx_code')
207 {
208 $text=\HtmlInput::hidden("vx_code",$value);
209 echo $text;
210 if ( $value !="") {
211 $value= \Icon_Action::trash(uniqid(),"vat_code.select_value('xx')").$value;
212 }
213 echo span($value,'id="vx_value"');
214 $js=sprintf("vat_code.list_vatex()");
215 echo \Icon_Action::icon_magnifier(uniqid(),$js );
216
217 }
218 elseif ($key == "tva_id") {
219 $inum=new INum($key,$value);
220 echo $inum->input();
221 echo \HtmlInput::hidden("old_tva_id",$value);
222
223 } elseif ($key=='tva_reverse_account')
224 {
225 $text=new IPoste("tva_reverse_account");
226 $text->value=$value;
227 $min_size=10;
228 $text->set_attribute('gDossier', Dossier::id());
229 $text->set_attribute('jrn', 0);
230 $text->set_attribute('account', 'tva_reverse_account');
231 $text->size=$min_size;
232 echo $text->input();
233 }elseif ($this->a_type[$key]=="text")
234 {
235 $text=new IText($key);
236 $text->value=$value;
237 $min_size=(strlen($value??"")<30)?30:strlen($value)+5;
238 $text->size=$min_size;
239 echo $text->input();
240 }
241 echo "</td>";
242 }
243 else
244 {
245 printf('<td>%s %s</td>', h($value),
246 HtmlInput::hidden($key, $value)
247 );
248 }
249 echo '<td >';
250 if (isset ($this->a_comment[$key])) {
251 print '<p class="text-muted">';
252 echo $this->a_comment[$key];
253 print '</p>';
254 }
255 if( $key == 'vx_code') {
256 echo '<span id="vx_code_description">';
257 $row=$this->table->cn->get_row("select vx_code,vx_code_name,vx_description,vx_remark from vatex_code where vx_code=$1",
258 [$value]);
259 if ( ! empty ($row)) {
260 echo $row['vx_description'].span($row['vx_remark'],' class="text-muted" ');
261
262 }
263 echo '</span>';
264 }
265 echo '</td>';
266 }
267 echo "</tr>";
268 }
269 echo "</table>";
270 }
271
272 /**
273 * @brief add the TVA_REVERSE_ACCOUNT
274 * @return void
275 */
276 function from_request()
277 {
278 parent::from_request();
279 $http=new \HttpInput();
280 $this->table->tva_reverse_account=$http->request('tva_reverse_account');
281 $this->table->tva_peppol_code=$http->request('tva_peppol_code');
282 $this->table->vx_code=$http->request('vx_code');
283 }
284
285 /**
286 * @brief save the data in TVA_RATE
287 * if tva_both_side is 1 and tva_purchase or tva_sale is empty then
288 it is equal to the other value
289 *
290 */
291 function save()
292 {
293 if ( $this->previous_id === null ) {
294 throw new \Exception ("TVA184: no previous TVA id");
295 }
296 $cn=Dossier::connect();
297 // if tva_both_side is 1 and tva_purchase or tva_sale is empty then
298 // it is equal to the other value
299 if ($this->table->tva_both_side==1)
300 {
301 if ($this->table->tva_purchase=="#"||trim($this->table->tva_purchase)
302 =="#")
303 {
304 $this->table->tva_purchase=$this->table->tva_sale;
305 }
306 if ($this->table->tva_sale=="#"||trim($this->table->tva_sale)=="#")
307 {
308 $this->table->tva_sale=$this->table->tva_purchase;
309 }
310 }
311 $new_tva_id=$this->table->tva_id;
313 $tva_rate->setp("tva_id",$new_tva_id);
314 $tva_rate->setp("tva_rate", $this->table->tva_rate);
315 $tva_rate->setp("tva_code", $this->table->tva_code);
316 $tva_rate->setp("tva_label", $this->table->tva_label);
317 $tva_rate->setp("tva_comment", $this->table->tva_comment);
318 $tva_rate->setp("tva_both_side", $this->table->tva_both_side);
319 $tva_rate->setp("tva_reverse_account", $this->table->tva_reverse_account);
320 $tva_rate->setp("tva_peppol_code", $this->table->tva_peppol_code);
321 $tva_rate->setp("vx_code", $this->table->vx_code);
322
323 // TVA accounting must be joined and separated with a comma
324 $tva_purchase=(trim($this->table->tva_purchase)=="")?"#":$this->table->tva_purchase;
325 $tva_sale=(trim($this->table->tva_sale)=="")?"#":$this->table->tva_sale;
326 $tva_rate->setp("tva_poste", $tva_purchase.",".$tva_sale);
327 $tva_rate->setp("tva_payment_sale", $this->table->tva_payment_sale);
328 $tva_rate->setp("tva_payment_purchase", $this->table->tva_payment_purchase);
329 if ( $this->previous_id == -1 ) {
330 $tva_rate->insert();
331 } else {
332 $tva_rate->update();
333 }
334 if ( $this->previous_id != - 1 && $this->previous_id != $new_tva_id) {
335 $cn->exec_sql("update tva_rate set tva_id = $1 where tva_id = $2",[$new_tva_id,$this->previous_id]);
336 $this->table->setp("tva_id",$new_tva_id);
337 }else {
338 $this->table->setp("tva_id",$tva_rate->getp("tva_id"));
339 }
340
341 }
342 /**
343 * @brief Check data are valid
344 * 1. tva_rate between 0 & 1
345 * 2. label is uniq
346 * 3. accounting must exist
347 * @return boolean
348 */
349 function check()
350 {
351 global $g_parameter;
352 $cn=Dossier::connect();
353 if ( $this->previous_id === null ) {
354 throw new \Exception ("TVA184: no previous TVA id");
355 }
356 // both accounting can not be empty
357 if (trim($this->table->tva_purchase??"")==""&&trim($this->table->tva_sale??"")=="")
358 {
359 $this->set_error("tva_purchase",
360 _("Les 2 postes comptables ne peuvent être nuls"));
361 $this->set_error("tva_sale",
362 _("Les 2 postes comptables ne peuvent être nuls"));
363 }
364
365 // Check the tva rate
366 if (trim($this->table->tva_rate??"")==""||isNumber($this->table->tva_rate)==0||$this->table->tva_rate>1)
367 {
368 $this->set_error("tva_rate", _("Taux de TVA invalide"));
369 }
370
371 //Check the label must be unique
372 $count=$cn->get_value("select count(*) from tva_rate where tva_id<>$1 and lower(tva_label)=lower($2)",
373 [$this->getPreviousId(), $this->table->tva_label]);
374 if ($count>0)
375 {
376 $this->set_error("tva_label", _("Ce nom est déjà utilisé"));
377 }
378
379 // Check accounting exists for purchase
380 if (trim($this->table->tva_purchase??"")!=""&&$this->table->tva_purchase!="#")
381 {
382 $count=$cn->get_value("select count(*) from tmp_pcmn where pcm_val = $1",
383 [$this->table->tva_purchase]);
384 if ($count==0)
385 {
386 $this->set_error("tva_purchase", _("Poste comptable inexistant"));
387 }
388 }
389 // Check accounting exists for sale
390 if (trim($this->table->tva_sale??"")!=""&&$this->table->tva_sale!="#")
391 {
392 $count=$cn->get_value("select count(*) from tmp_pcmn where pcm_val = $1",
393 [$this->table->tva_sale]);
394 if ($count==0)
395 {
396 $this->set_error("tva_sale", _("Poste comptable inexistant"));
397 }
398 }
399
400 // check if tva_both_side is valid
401 if ($this->table->tva_both_side!=0&&$this->table->tva_both_side!=1)
402 {
403 $this->set_error("tva_both_side", _("Choix incorrect"));
404 }
405 $flag = true;
406 // Check that tva_id is a integer not a float (TVA_ID is an integer)
407 if ( isNumber($this->table->tva_id) == 0 || $this->table->tva_id != round($this->table->tva_id) )
408 {
409 $this->set_error("tva_id",_("Valeur invalide"));
410 $flag=false;
411 }
412 // Check if old tva_id was not overwritting something
413 if ( $flag && $this->previous_id != $this->table->tva_id && $cn->get_value("select count(*) from tva_rate where tva_id=$1",[$this->table->tva_id]) > 0)
414 {
415 $this->set_error("tva_id",_("Code TVA déjà utilisé"));
416 }
417 // Check that tva code is unique and remove not letter
418 $this->table->tva_code=strtoupper(trim( $this->table->tva_code));
419 $tva_code=$this->table->tva_code;
420
421 $tva_code=strtoupper($tva_code);
422 $tva_code=preg_replace("/[A-Z]/", "", $tva_code);
423 $tva_code=preg_replace("/[0-9]/", "", $tva_code);
424
425 if (strlen($tva_code)>0){
426 $this->set_error("tva_code", _("code tva : Uniquement des chiffres et des lettres"));
427 }
428
429 if (strlen($this->table->tva_code)>5){
430 $this->set_error("tva_code", _("code tva : Maximum 5 caractères"));
431 }
432 if (isNumber($this->table->tva_code) == 1){
433 $this->set_error("tva_code", _("code tva : doit aussi contenir des lettres"));
434 }
435 /**
436 * auto reverse VAT : check that the accounting is used
437 */
438 if ( $this->table->tva_both_side==0 && trim($this->table->tva_reverse_account??"") != "")
439 {
440 $this->set_error("tva_reverse_account", _("Pas d'autoliquidation demandé"));
441 }
442 if ( $this->table->tva_both_side==1 && trim($this->table->tva_reverse_account??"") != "")
443 {
444 $count=$cn->get_value("select count(*) from tmp_pcmn where pcm_val = $1",
445 [$this->table->tva_reverse_account]);
446 if ($count==0)
447 {
448 $this->set_error("tva_reverse_account", _("Poste comptable inexistant"));
449 }
450 }
451 // label cannot be empty
452 if ( trim($this->table->tva_label??"")=="") {
453 $this->set_error("tva_label", _('Le label ne peut être vide'));
454 }
455
456 // if vatex is set then code invoice must be different from S and Z
457 if ( $g_parameter->MY_INVOICE_FORMAT == 'UBL21BE' && trim($this->table->vx_code??"") != "" && in_array($this->table->tva_peppol_code,['S','Z'] ))
458 {
459 $this->set_error("vx_code",_("Le code d'exemption TVA ne peut être utilisé avec ce code Facture électronique UBL "));
460 }
461 // if vatex is set then code invoice must be different from S and Z
462 if ( $g_parameter->MY_INVOICE_FORMAT == 'UBL21BE' && trim($this->table->vx_code??"") != "" && $this->table->tva_peppol_code=="")
463 {
464 $this->set_error("vx_code",_("Le code d'exemption TVA n' pas de sens sans code Facture électronique"));
465 }
466 // if tva_peppol_code is not S or Z then a VATEX code must be supplied
467 if ( $g_parameter->MY_INVOICE_FORMAT == 'UBL21BE'
468 && ! in_array($this->table->tva_peppol_code??"",["Z","S"])
469 && trim($this->table->vx_code??"" ) =="")
470 {
471 $this->set_error("vx_code",_("Un code d'exemption de TVA doit être fourni, voyez le manuel"));
472
473 }
474 if ($this->count_error()!=0)
475 return false;
476 return true;
477 }
478 /**
479 * delete if not used anywhere
480 */
481 function delete()
482 {
483 $cn=Dossier::connect();
484 $count_purchase=$cn->get_value("select count(*) from quant_purchase where qp_vat_code = $1",[$this->table->tva_id]);
485 $count_sale=$cn->get_value("select count(*) from quant_sold where qs_vat_code = $1",[$this->table->tva_id]);
486 if ( $count_purchase > 0 || $count_sale > 0) {
487 throw new Exception(_("Effacement interdit : TVA utilisée"));
488 }
489
490 // Forbid to remove all tva
491 $count=$cn->get_value("select count(*) from tva_rate");
492 if ( $count < 2) {
493 throw new Exception(_("Vous ne pouvez pas effacer tous les taux. Si votre société n'utilise pas la TVA, changer dans le menu société"));
494 }
495 $cn->exec_sql("delete from tva_rate where tva_id=$1", [$this->table->tva_id]);
496 }
497
498}
isNumber($p_int)
span($p_string, $p_extra='')
Definition ac_common.php:43
global $g_parameter
h( $row[ 'oa_description'])
catch(Exception $e) $tva_rate
_("actif, passif,charge,...")
This class handles only the numeric input, the input will call a javascript to change comma to period...
show a button, for selecting a account and a input text for manually inserting an account the differe...
Html Input , create a tag <SELECT> ... </SELECT> if readonly == true then display the label correspon...
Html Input Text member :
static infobulle($p_comment)
Display a info in a bubble, text is in message_javascript.
static trash($p_id, $p_javascript)
Display the icon of a trashbin.
Purpose is to propose a librairy to display a table content and allow to update and delete row ,...
count_error()
returns the nb of errors found
get_property_updatable($p_key)
return True if the column is updatable otherwise false
set_property_updatable($p_key, $p_value)
set a column of the data row updatable or not
set_col_type($p_key, $p_value, $p_array=NULL)
set the type of a column , it will change in the input db box , the select must supply an array of po...
get_property_visible($p_key)
return True if the column is visible otherwise false
get_error($p_col)
retrieve the error message
setTitle($p_title)
Set the title of the diabox , default is Donnée.
set_property_visible($p_key, $p_value)
set a column of the data row visible or not
set_error($p_col, $p_message)
set the error message for a wrong input
set_col_label($p_key, $p_display)
set the name to display for a column
Configure the tva : code , rate, label ... When using Manage_Table_SQL.
$previous_id
< previous tva_id, used to know if we update or insert,
__construct(V_Tva_rate_SQL $p_table)
input()
display into a dialog box the datarow in order to be appended or modified.
from_request()
add the TVA_REVERSE_ACCOUNT
save()
save the data in TVA_RATE if tva_both_side is 1 and tva_purchase or tva_sale is empty then it is equa...
check()
Check data are valid.
$a_comment
Supplemental explanation.
abstract of the table public.tva_rate
$all table
$count
$flag
Definition install.php:535
print
Type of printing.
if( $delta< 0) elseif( $delta==0)