noalyss Version-9
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
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_label", _("label"));
51 $this->set_col_label("tva_rate", _("taux"));
52 $this->set_col_label("tva_comment", _("Description"));
53 $this->set_col_label("tva_both_side", _("Autoliquidation"));
54 $this->set_col_label("tva_sale", _("TVA Vente (C)"));
55 $this->set_col_label("tva_purchase", _("TVA Achat (D)"));
56 $this->set_col_type("tva_both_side", "select",
57 array(
58 ["value"=>0, "label"=>_("Non")],
59 ["value"=>1, "label"=>_("Oui")]
60 ));
61 $this->set_property_updatable("tva_id", true);
62 $this->set_col_label("tva_payment_purchase",_("Exigible achat"));
63 $this->set_col_type("tva_payment_purchase","select",
64 array(
65 array("value"=>'O',"label"=>"Opération"),
66 array("value"=>'P',"label"=>"Paiement")
67 )
68 );
69 $this->set_col_label("tva_payment_sale",_("Exigible vente"));
70 $this->set_col_type("tva_payment_sale","select",
71 array(
72 array("value"=>'O',"label"=>"Opération"),
73 array("value"=>'P',"label"=>"Paiement")
74 )
75 );
76 $this->a_info=["tva_purchase"=>44,"tva_both_side"=>43,"tva_sale"=>45
77 ,"tva_payment_sale"=>74,"tva_payment_purchase"=>74];
78 $this->previous_id=null;
79 }
80
81 /**
82 * @return int or null
83 */
84 public function getPreviousId()
85 {
86 return $this->previous_id;
87 }
88
89 /**
90 *
91 * @param int $previous_id
92 */
94 {
95 $this->previous_id = $previous_id;
96 return $this;
97 }
98
99 /**
100 * @brief display into a dialog box the datarow in order
101 * to be appended or modified. Can be override if you need
102 * a more complex form
103 */
104 function input()
105 {
106 $nb_order=count($this->a_order);
107 echo "<table>";
108 for ($i=0; $i<$nb_order; $i++)
109 {
110 echo "<tr>";
111 $key=$this->a_order[$i];
112 $label=$this->a_label_displaid[$key];
113 $value=$this->table->get($key);
114 $error=$this->get_error($key);
116
117 if ($this->get_property_visible($key)===TRUE)
118 {
119 $info="";
120 if ( isset($this->a_info[$key])) {
121 $info=Icon_Action::infobulle($this->a_info[$key]);
122 }
123 // Label
124 echo "<td> {$label} {$info} {$error}</td>";
125
126 if ($this->get_property_updatable($key)==TRUE)
127 {
128 echo "<td>";
129 if ($this->a_type[$key]=="select")
130 {
131 $select=new ISelect($key);
132 $select->value=$this->a_select[$key];
133 $select->selected=$value;
134 echo $select->input();
135 }
136 elseif ($key=="tva_rate")
137 {
138 $text=new INum($key);
139 $text->value=$value;
140 $text->prec=4;
141 $min_size=(strlen($value??"")<10)?10:strlen($value)+1;
142 $text->size=$min_size;
143 echo $text->input();
144 }
145 elseif ($key=='tva_purchase')
146 {
147 $text=new IPoste("tva_purchase");
148 $text->value=$value;
149 $min_size=10;
150 $text->size=$min_size;
151 $text->set_attribute('gDossier', Dossier::id());
152 $text->set_attribute('jrn', 0);
153 $text->set_attribute('account', 'tva_purchase');
154 echo $text->input();
155 $url="do.php?".http_build_query(array("gDossier"=>Dossier::id(),"ac"=>'CFGPCMN','p_start'=>4));
156 echo HtmlInput::anchor(_("Configuration poste comptable"),$url,"",'target="_blank"');
157 }
158 elseif ($key=='tva_sale')
159 {
160 $text=new IPoste("tva_sale");
161 $text->value=$value;
162 $min_size=10;
163 $text->set_attribute('gDossier', Dossier::id());
164 $text->set_attribute('jrn', 0);
165 $text->set_attribute('account', 'tva_sale');
166 $text->size=$min_size;
167 echo $text->input();
168 }
169 elseif ($this->a_type[$key]=="text")
170 {
171 $text=new IText($key);
172 $text->value=$value;
173 $min_size=(strlen($value??"")<30)?30:strlen($value)+5;
174 $text->size=$min_size;
175 echo $text->input();
176 } elseif ($key == "tva_id") {
177 $inum=new INum($key,$value);
178 echo $inum->input();
179 echo \HtmlInput::hidden("old_tva_id",$value);
180
181 }
182 echo "</td>";
183 }
184 else
185 {
186 printf('<td>%s %s</td>', h($value),
188 );
189 }
190 }
191 echo "</tr>";
192 }
193 echo "</table>";
194 }
195
196 /**
197 * @brief save the data in TVA_RATE
198 * if tva_both_side is 1 and tva_purchase or tva_sale is empty then
199 it is equal to the other value
200 *
201 */
202 function save()
203 {
204 if ( $this->previous_id == null ) {
205 throw new \Exception ("TVA184: no previous TVA id");
206 }
208 // if tva_both_side is 1 and tva_purchase or tva_sale is empty then
209 // it is equal to the other value
210 if ($this->table->tva_both_side==1)
211 {
212 if ($this->table->tva_purchase=="#"||trim($this->table->tva_purchase)
213 =="#")
214 {
215 $this->table->tva_purchase=$this->table->tva_sale;
216 }
217 if ($this->table->tva_sale=="#"||trim($this->table->tva_sale)=="#")
218 {
219 $this->table->tva_sale=$this->table->tva_purchase;
220 }
221 }
222 $new_tva_id=$this->table->tva_id;
224 $tva_rate->setp("tva_id",$new_tva_id);
225 $tva_rate->setp("tva_rate", $this->table->tva_rate);
226 $tva_rate->setp("tva_label", $this->table->tva_label);
227 $tva_rate->setp("tva_comment", $this->table->tva_comment);
228 $tva_rate->setp("tva_both_side", $this->table->tva_both_side);
229
230 // TVA accounting must be joined and separated with a comma
231 $tva_purchase=(trim($this->table->tva_purchase)=="")?"#":$this->table->tva_purchase;
232 $tva_sale=(trim($this->table->tva_sale)=="")?"#":$this->table->tva_sale;
233 $tva_rate->setp("tva_poste", $tva_purchase.",".$tva_sale);
234 $tva_rate->setp("tva_payment_sale", $this->table->tva_payment_sale);
235 $tva_rate->setp("tva_payment_purchase", $this->table->tva_payment_purchase);
236 if ( $this->previous_id == -1 ) {
237 $tva_rate->insert();
238 } else {
239 $tva_rate->update();
240 }
241 if ( $this->previous_id != - 1 && $this->previous_id != $new_tva_id) {
242 $cn->exec_sql("update tva_rate set tva_id = $1 where tva_id = $2",[$new_tva_id,$this->previous_id]);
243 $this->table->setp("tva_id",$new_tva_id);
244 }else $this->table->setp("tva_id",$tva_rate->getp("tva_id"));
245
246 }
247 /**
248 * Check data are valid
249 * 1. tva_rate between 0 & 1
250 * 2. label is uniq
251 * 3. accounting must exist
252 * @return boolean
253 */
254 function check()
255 {
257 if ( $this->previous_id == null ) {
258 throw new \Exception ("TVA184: no previous TVA id");
259 }
260 // both accounting can not be empty
261 if (trim($this->table->tva_purchase)==""&&trim($this->table->tva_sale)=="")
262 {
263 $this->set_error("tva_purchase",
264 _("Les 2 postes comptables ne peuvent être nuls"));
265 $this->set_error("tva_sale",
266 _("Les 2 postes comptables ne peuvent être nuls"));
267 }
268
269 // Check the tva rate
270 if (trim($this->table->tva_rate)==""||isNumber($this->table->tva_rate)==0||$this->table->tva_rate>1)
271 {
272 $this->set_error("tva_rate", _("Taux de TVA invalide"));
273 }
274
275 //Check the label must be unique
276 $count=$cn->get_value("select count(*) from tva_rate where tva_id<>$1 and lower(tva_label)=lower($2)",
277 [$this->getPreviousId(), $this->table->tva_label]);
278 if ($count>0)
279 {
280 $this->set_error("tva_label", _("Ce nom est déjà utilisé"));
281 }
282
283 // Check accounting exists for purchase
284 if (trim($this->table->tva_purchase)!=""&&$this->table->tva_purchase!="#")
285 {
286 $count=$cn->get_value("select count(*) from tmp_pcmn where pcm_val = $1",
287 [$this->table->tva_purchase]);
288 if ($count==0)
289 {
290 $this->set_error("tva_purchase", _("Poste comptable inexistant"));
291 }
292 }
293 // Check accounting exists for sale
294 if (trim($this->table->tva_sale)!=""&&$this->table->tva_sale!="#")
295 {
296 $count=$cn->get_value("select count(*) from tmp_pcmn where pcm_val = $1",
297 [$this->table->tva_sale]);
298 if ($count==0)
299 {
300 $this->set_error("tva_sale", _("Poste comptable inexistant"));
301 }
302 }
303
304 // check if tva_both_side is valid
305 if ($this->table->tva_both_side!=0&&$this->table->tva_both_side!=1)
306 {
307 $this->set_error("tva_both_side", _("Choix incorrect"));
308 }
309 $flag = true;
310 if ( isNumber($this->table->tva_id) == 0 || $this->table->tva_id != round($this->table->tva_id) )
311 {
312 $this->set_error("tva_id",_("Valeur invalide"));
313 $flag=false;
314 }
315 // Check if old tva_id was not overwritting something
316 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)
317 {
318 $this->set_error("tva_id",_("Code TVA déjà utilisé"));
319 }
320 // Check that tva_id is a integer not a float
321
322 if ($this->count_error()!=0)
323 return false;
324 return true;
325 }
326 /**
327 * delete if not used anywhere
328 */
329 function delete()
330 {
332 $count_purchase=$cn->get_value("select count(*) from quant_purchase where qp_vat_code = $1",[$this->table->tva_id]);
333 $count_sale=$cn->get_value("select count(*) from quant_sold where qs_vat_code = $1",[$this->table->tva_id]);
334 if ( $count_purchase > 0 || $count_sale > 0) {
335 throw new Exception(_("Effacement interdit : TVA utilisée"));
336 }
337
338 // Forbid to remove all tva
339 $count=$cn->get_value("select count(*) from tva_rate");
340 if ( $count < 2) {
341 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é"));
342 }
343 $cn->exec_sql("delete from tva_rate where tva_id=$1", [$this->table->tva_id]);
344 }
345
346}
isNumber($p_int)
Definition: ac_common.php:215
h( $row[ 'oa_description'])
$url
catch(Exception $e) $tva_rate
static id()
return the 'gDossier' value after a check
static connect()
static anchor($p_text, $p_url="", $p_js="", $p_style=' class="line" ', $p_title="click", array $p_attribute=[])
Return a simple anchor with a url or a javascript if $p_js is not null then p_url will be javascript:...
static hidden($p_name, $p_value, $p_id="")
static errorbulle($p_comment)
This class handles only the numeric input, the input will call a javascript to change comma to period...
Definition: inum.class.php:42
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.
Definition: itext.class.php:30
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_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.
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.
setPreviousId($previous_id)
abstract of the table public.tva_rate
$all table
$count
$flag
Definition: install.php:531
if( $delta< 0) elseif( $delta==0)