noalyss Version-9
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->set_col_label("tva_id", _("id"));
50 $this->set_col_label("tva_code", _("Code"));
51 $this->set_col_label("tva_label", _("label"));
52 $this->set_col_label("tva_rate", _("taux"));
53 $this->set_col_label("tva_comment", _("Description"));
54 $this->set_col_label("tva_both_side", _("Autoliquidation"));
55 $this->set_col_label("tva_reverse_account", _('Poste comptable autoliquidation'));
56 $this->set_col_label("tva_sale", _("TVA Vente (C)"));
57 $this->set_col_label("tva_purchase", _("TVA Achat (D)"));
58
59 $this->set_property_visible('tva_reverse_account', false);
60
61 $this->set_col_type("tva_both_side", "select",
62 array(
63 ["value"=>0, "label"=>_("Non")],
64 ["value"=>1, "label"=>_("Oui")]
65 ));
66 $this->set_col_type("tva_reverse_account", 'custom');
67 $this->set_property_updatable("tva_id", true);
68 $this->set_col_label("tva_payment_purchase",_("Exigible achat"));
69 $this->set_col_type("tva_payment_purchase","select",
70 array(
71 array("value"=>'O',"label"=>"Opération"),
72 array("value"=>'P',"label"=>"Paiement")
73 )
74 );
75 $this->set_col_label("tva_payment_sale",_("Exigible vente"));
76 $this->set_col_type("tva_payment_sale","select",
77 array(
78 array("value"=>'O',"label"=>"Opération"),
79 array("value"=>'P',"label"=>"Paiement")
80 )
81 );
82 $this->a_info=[
83 "tva_purchase"=>44,"tva_both_side"=>43,"tva_sale"=>45
84 ,"tva_payment_sale"=>74,"tva_payment_purchase"=>74,'tva_reverse_account'=>88];
85 $this->previous_id=null;
86 $this->a_comment=array(
87 'tva_purchase'=>_("Ne donnez pas ce poste comptable si ce code n'est pas utilisé à l'achat"),
88 'tva_both_side'=>_("Autoliquidation : Utilisé en même temps au crédit et au débit"),
89 'tva_sale'=>_("Ne donnez pas ce poste comptable si ce code n'est pas utilisé à la vente"),
90 'tva_payment_purchase'=>_('TVA due ou récupérable quand l\'opération est payée ou exécutée'),
91 'tva_payment_sale'=>_('TVA due ou récupérable quand l\'opération est payée ou exécutée'),
92 '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)")
93 );
94 }
95
96 /**
97 * @return int or null
98 */
99 public function getPreviousId()
100 {
101 return $this->previous_id;
102 }
103
104 /**
105 *
106 * @param int $previous_id
107 */
109 {
110 $this->previous_id = $previous_id;
111 return $this;
112 }
113
114 /**
115 * @brief display into a dialog box the datarow in order
116 * to be appended or modified. Can be override if you need
117 * a more complex form
118 */
119 function input()
120 {
121 $nb_order=count($this->a_order);
122 $this->set_property_visible('tva_reverse_account', true);
123 echo "<table>";
124 for ($i=0; $i<$nb_order; $i++)
125 {
126 echo "<tr>";
127 $key=$this->a_order[$i];
128 $label=$this->a_label_displaid[$key];
129 $value=$this->table->get($key);
130 $error=$this->get_error($key);
131 $error=($error=="")?"":HtmlInput::errorbulle($error);
132
133 if ($this->get_property_visible($key)===TRUE)
134 {
135 $info="";
136 if ( isset($this->a_info[$key])) {
137 $info=Icon_Action::infobulle($this->a_info[$key]);
138 }
139 // Label
140 echo "<td> {$label} {$info} {$error}</td>";
141
142 if ($this->get_property_updatable($key)==TRUE)
143 {
144 echo "<td>";
145 if ($this->a_type[$key]=="select")
146 {
147 $select=new ISelect($key);
148 $select->value=$this->a_select[$key];
149 $select->selected=$value;
150 echo $select->input();
151 }
152 elseif ($key=="tva_rate")
153 {
154 $text=new INum($key);
155 $text->value=$value;
156 $text->prec=4;
157 $min_size=(strlen($value??"")<10)?10:strlen($value)+1;
158 $text->size=$min_size;
159 echo $text->input();
160 }
161 elseif ($key=='tva_purchase')
162 {
163 $text=new IPoste("tva_purchase");
164 $text->value=$value;
165 $min_size=10;
166 $text->size=$min_size;
167 $text->set_attribute('gDossier', Dossier::id());
168 $text->set_attribute('jrn', 0);
169 $text->set_attribute('account', 'tva_purchase');
170 echo $text->input();
171 $url="do.php?".http_build_query(array("gDossier"=>Dossier::id(),"ac"=>'C0PCMN','p_start'=>4));
172 echo HtmlInput::anchor(_("Configuration poste comptable"),$url,"",'target="_blank"');
173 }
174 elseif ($key=='tva_sale')
175 {
176 $text=new IPoste("tva_sale");
177 $text->value=$value;
178 $min_size=10;
179 $text->set_attribute('gDossier', Dossier::id());
180 $text->set_attribute('jrn', 0);
181 $text->set_attribute('account', 'tva_sale');
182 $text->size=$min_size;
183 echo $text->input();
184 }
185 elseif ($this->a_type[$key]=="text")
186 {
187 $text=new IText($key);
188 $text->value=$value;
189 $min_size=(strlen($value??"")<30)?30:strlen($value)+5;
190 $text->size=$min_size;
191 echo $text->input();
192 } elseif ($key == "tva_id") {
193 $inum=new INum($key,$value);
194 echo $inum->input();
195 echo \HtmlInput::hidden("old_tva_id",$value);
196
197 } elseif ($key=='tva_reverse_account')
198 {
199 $text=new IPoste("tva_reverse_account");
200 $text->value=$value;
201 $min_size=10;
202 $text->set_attribute('gDossier', Dossier::id());
203 $text->set_attribute('jrn', 0);
204 $text->set_attribute('account', 'tva_reverse_account');
205 $text->size=$min_size;
206 echo $text->input();
207 }
208 echo "</td>";
209 }
210 else
211 {
212 printf('<td>%s %s</td>', h($value),
213 HtmlInput::hidden($key, $value)
214 );
215 }
216 echo '<td class="text-muted">';
217 if (isset ($this->a_comment[$key])) {
218 echo $this->a_comment[$key];
219 }
220 echo '</td>';
221 }
222 echo "</tr>";
223 }
224 echo "</table>";
225 }
226
227 /**
228 * @brief add the TVA_REVERSE_ACCOUNT
229 * @return void
230 */
231 function from_request()
232 {
233 parent::from_request();
234 $http=new \HttpInput();
235 $this->table->tva_reverse_account=$http->request('tva_reverse_account');
236 }
237
238 /**
239 * @brief save the data in TVA_RATE
240 * if tva_both_side is 1 and tva_purchase or tva_sale is empty then
241 it is equal to the other value
242 *
243 */
244 function save()
245 {
246 if ( $this->previous_id === null ) {
247 throw new \Exception ("TVA184: no previous TVA id");
248 }
249 $cn=Dossier::connect();
250 // if tva_both_side is 1 and tva_purchase or tva_sale is empty then
251 // it is equal to the other value
252 if ($this->table->tva_both_side==1)
253 {
254 if ($this->table->tva_purchase=="#"||trim($this->table->tva_purchase)
255 =="#")
256 {
257 $this->table->tva_purchase=$this->table->tva_sale;
258 }
259 if ($this->table->tva_sale=="#"||trim($this->table->tva_sale)=="#")
260 {
261 $this->table->tva_sale=$this->table->tva_purchase;
262 }
263 }
264 $new_tva_id=$this->table->tva_id;
266 $tva_rate->setp("tva_id",$new_tva_id);
267 $tva_rate->setp("tva_rate", $this->table->tva_rate);
268 $tva_rate->setp("tva_code", $this->table->tva_code);
269 $tva_rate->setp("tva_label", $this->table->tva_label);
270 $tva_rate->setp("tva_comment", $this->table->tva_comment);
271 $tva_rate->setp("tva_both_side", $this->table->tva_both_side);
272 $tva_rate->setp("tva_reverse_account", $this->table->tva_reverse_account);
273
274 // TVA accounting must be joined and separated with a comma
275 $tva_purchase=(trim($this->table->tva_purchase)=="")?"#":$this->table->tva_purchase;
276 $tva_sale=(trim($this->table->tva_sale)=="")?"#":$this->table->tva_sale;
277 $tva_rate->setp("tva_poste", $tva_purchase.",".$tva_sale);
278 $tva_rate->setp("tva_payment_sale", $this->table->tva_payment_sale);
279 $tva_rate->setp("tva_payment_purchase", $this->table->tva_payment_purchase);
280 if ( $this->previous_id == -1 ) {
281 $tva_rate->insert();
282 } else {
283 $tva_rate->update();
284 }
285 if ( $this->previous_id != - 1 && $this->previous_id != $new_tva_id) {
286 $cn->exec_sql("update tva_rate set tva_id = $1 where tva_id = $2",[$new_tva_id,$this->previous_id]);
287 $this->table->setp("tva_id",$new_tva_id);
288 }else $this->table->setp("tva_id",$tva_rate->getp("tva_id"));
289
290 }
291 /**
292 * @brief Check data are valid
293 * 1. tva_rate between 0 & 1
294 * 2. label is uniq
295 * 3. accounting must exist
296 * @return boolean
297 */
298 function check()
299 {
300 $cn=Dossier::connect();
301 if ( $this->previous_id === null ) {
302 throw new \Exception ("TVA184: no previous TVA id");
303 }
304 // both accounting can not be empty
305 if (trim($this->table->tva_purchase??"")==""&&trim($this->table->tva_sale??"")=="")
306 {
307 $this->set_error("tva_purchase",
308 _("Les 2 postes comptables ne peuvent être nuls"));
309 $this->set_error("tva_sale",
310 _("Les 2 postes comptables ne peuvent être nuls"));
311 }
312
313 // Check the tva rate
314 if (trim($this->table->tva_rate??"")==""||isNumber($this->table->tva_rate)==0||$this->table->tva_rate>1)
315 {
316 $this->set_error("tva_rate", _("Taux de TVA invalide"));
317 }
318
319 //Check the label must be unique
320 $count=$cn->get_value("select count(*) from tva_rate where tva_id<>$1 and lower(tva_label)=lower($2)",
321 [$this->getPreviousId(), $this->table->tva_label]);
322 if ($count>0)
323 {
324 $this->set_error("tva_label", _("Ce nom est déjà utilisé"));
325 }
326
327 // Check accounting exists for purchase
328 if (trim($this->table->tva_purchase??"")!=""&&$this->table->tva_purchase!="#")
329 {
330 $count=$cn->get_value("select count(*) from tmp_pcmn where pcm_val = $1",
331 [$this->table->tva_purchase]);
332 if ($count==0)
333 {
334 $this->set_error("tva_purchase", _("Poste comptable inexistant"));
335 }
336 }
337 // Check accounting exists for sale
338 if (trim($this->table->tva_sale??"")!=""&&$this->table->tva_sale!="#")
339 {
340 $count=$cn->get_value("select count(*) from tmp_pcmn where pcm_val = $1",
341 [$this->table->tva_sale]);
342 if ($count==0)
343 {
344 $this->set_error("tva_sale", _("Poste comptable inexistant"));
345 }
346 }
347
348 // check if tva_both_side is valid
349 if ($this->table->tva_both_side!=0&&$this->table->tva_both_side!=1)
350 {
351 $this->set_error("tva_both_side", _("Choix incorrect"));
352 }
353 $flag = true;
354 // Check that tva_id is a integer not a float (TVA_ID is an integer)
355 if ( isNumber($this->table->tva_id) == 0 || $this->table->tva_id != round($this->table->tva_id) )
356 {
357 $this->set_error("tva_id",_("Valeur invalide"));
358 $flag=false;
359 }
360 // Check if old tva_id was not overwritting something
361 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)
362 {
363 $this->set_error("tva_id",_("Code TVA déjà utilisé"));
364 }
365 // Check that tva code is unique and remove not letter
366 $this->table->tva_code=strtoupper(trim( $this->table->tva_code));
367 $tva_code=$this->table->tva_code;
368
369 $tva_code=strtoupper($tva_code);
370 $tva_code=preg_replace("/[A-Z]/", "", $tva_code);
371 $tva_code=preg_replace("/[0-9]/", "", $tva_code);
372
373 if (strlen($tva_code)>0){
374 $this->set_error("tva_code", _("code tva : Uniquement des chiffres et des lettres"));
375 }
376
377 if (strlen($this->table->tva_code)>5){
378 $this->set_error("tva_code", _("code tva : Maximum 5 caractères"));
379 }
380 if (isNumber($this->table->tva_code) == 1){
381 $this->set_error("tva_code", _("code tva : doit aussi contenir des lettres"));
382 }
383 /**
384 * auto reverse VAT : check that the accounting is used
385 */
386 if ( $this->table->tva_both_side==0 && trim($this->table->tva_reverse_account??"") != "")
387 {
388 $this->set_error("tva_reverse_account", _("Pas d'autoliquidation demandé"));
389 }
390 if ( $this->table->tva_both_side==1 && trim($this->table->tva_reverse_account??"") != "")
391 {
392 $count=$cn->get_value("select count(*) from tmp_pcmn where pcm_val = $1",
393 [$this->table->tva_reverse_account]);
394 if ($count==0)
395 {
396 $this->set_error("tva_reverse_account", _("Poste comptable inexistant"));
397 }
398 }
399 // label cannot be empty
400 if ( trim($this->table->tva_label??"")=="") {
401 $this->set_error("tva_label", _('Le label ne peut être vide'));
402 }
403 if ($this->count_error()!=0)
404 return false;
405 return true;
406 }
407 /**
408 * delete if not used anywhere
409 */
410 function delete()
411 {
412 $cn=Dossier::connect();
413 $count_purchase=$cn->get_value("select count(*) from quant_purchase where qp_vat_code = $1",[$this->table->tva_id]);
414 $count_sale=$cn->get_value("select count(*) from quant_sold where qs_vat_code = $1",[$this->table->tva_id]);
415 if ( $count_purchase > 0 || $count_sale > 0) {
416 throw new Exception(_("Effacement interdit : TVA utilisée"));
417 }
418
419 // Forbid to remove all tva
420 $count=$cn->get_value("select count(*) from tva_rate");
421 if ( $count < 2) {
422 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é"));
423 }
424 $cn->exec_sql("delete from tva_rate where tva_id=$1", [$this->table->tva_id]);
425 }
426
427}
isNumber($p_int)
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.
static infobulle($p_comment)
Display a info in a bubble, text is in message_javascript.
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
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:536
if( $delta< 0) elseif( $delta==0)