noalyss Version-10
NOALYSS : serveur de comptabilité et ERP (2002)
Loading...
Searching...
No Matches
currency_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 * PhpCompta 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 Currency thanks the view v_currency_last_value
25 */
26require_once NOALYSS_INCLUDE.'/database/currency_sql.class.php';
27require_once NOALYSS_INCLUDE.'/database/currency_history_sql.class.php';
28
29/**
30 * Manage the configuration of currency , add currency, rate, remove and update
31 * Concerned tables are v_currency_last_value_SQL , Currency_SQL , Currency_History_SQL
32 * currency_id = 0 for the default currency , -1 for a new one
33 */
35{
36
37 /**
38 * constructor
39 * @param Data $p_table
40 * @example test_currency_mtable.php
41 */
43 {
44 parent::__construct($p_table);
45
46 // If currency , cannot be deleted
47 if ($this->is_currency_used($p_table->currency_id)==TRUE)
48 {
49 $this->set_delete_row(FALSE);
50 }
51 else
52 {
53 $this->set_delete_row(TRUE);
54 }
55 $this->set_property_visible("currency_id", FALSE);
56 $this->set_property_visible("currency_history_id", FALSE);
57 $this->set_col_label("cr_code_iso", "ISO");
58 $this->set_col_label("cr_name", _("Nom"));
59 $this->set_col_label('ch_value', _("Valeur"));
60 $this->set_col_label('str_from', _("Date"));
61 $this->set_col_type("str_from", "date");
62 $this->a_order=array("cr_code_iso", "currency_id", "currency_history_id", "cr_name", "ch_value", "str_from");
63 $this->set_icon_mod("first");
64 $this->set_sort_column("cr_code_iso");
65 $this->setCssClass("inner_box2");
66 }
67
68 /**
69 * returns TRUE the currency is used otherwise FALSE. We cannot delete a currency which is used in a
70 * operation
71 * @param integer $p_id currency_id
72 * @returns boolean true if currency is used
73 */
75 {
76 global $cn;
77 $cnt_used=$cn->get_value(" select count(*) from jrn where currency_id=$1",[$p_id]);
78 if ($cnt_used > 0)
79 {
80 return TRUE;
81 }
82 return FALSE;
83 }
84 /**
85 * Box to enter either a new currency or update a existing one
86 */
87 function input()
88 {
89 $record=$this->get_table();
90 $cr_code_iso=new IText("cr_code_iso", $record->cr_code_iso);
91 $cr_code_iso->size=10;
92 $cr_name=new IText("cr_name", $record->cr_name);
93 $cr_name->size=50;
94 $a_histo=$record->cn->get_array("select id,to_char(ch_from,'DD.MM.YYYY') as str_from,ch_value
95 from
96 currency_history
97 where
98 currency_id=$1", array($record->currency_id));
99 $new_rate_date=new IDate("new_rate_date");
100 $new_rate_value=new INum("new_rate_value");
101 $new_rate_value->prec=6;
102 if ($record->currency_id!=-1)
103 {
104 require NOALYSS_TEMPLATE."/currency_mtable_input.php";
105 }
106 else
107 {
108 require NOALYSS_TEMPLATE."/currency_mtable_input_new.php";
109 }
110 }
111
112 /**
113 * Check that the value are correct :
114 * - Code iso must be unique
115 * - Name cannot be empty
116 * - At least one rate
117 * - Date of the rate
118 * - code iso is max 10 char
119 * - name is max 80
120 * Default currency (id=0) cannot be changed
121 */
122 function check()
123 {
124 global $cn;
125 $table=$this->get_table();
126 $is_error=0;
127 if ( $table->currency_id == 0) {
128 $is_error++;
129 $this->set_error("cr_code_iso", _("Devise par défaut ne peut être changée"));
130 }
131 // ------ cr_code_iso can not be empty
132 if (trim($table->cr_code_iso)=="")
133 {
134 $is_error++;
135 $this->set_error("cr_code_iso", _("Code iso ne peut pas être vide"));
136 }
137 // ------ cr_name can not be empty
138 if (trim($table->cr_name)=="")
139 {
140 $is_error++;
141 $this->set_error("cr_name", _("Nom ne peut pas être vide"));
142 }
143 // ----- for new record than we need at leat a date + value
144 if ($table->currency_id==-1)
145 {
146 // -- for new record , we need a date + value
147 if (isDate($table->str_from)==0||trim($table->str_from)=="")
148 {
149 $is_error++;
150 $this->set_error("str_from", _("Date incorrecte, il faut au moins une valeur"));
151 }
152 if (isNumber($table->ch_value)==0||trim($table->ch_value)=="")
153 {
154 $is_error++;
155 $this->set_error("ch_value", _("Valeur incorrecte, il faut au moins une valeur"));
156 }
157 }
158 else
159 {
160 if (trim($table->str_from) =="" && trim($table->ch_value)=="")
161 {
162 // we don't add any new date
163
164 }
165 // -- for update, the date and value must be valid
166 elseif (trim($table->str_from)!=""&&trim($table->ch_value)!="")
167 {
168 if (isDate($table->str_from)==0)
169 {
170 $is_error++;
171 $this->set_error("str_from", _("Date incorrecte"));
172 }
173 if (isNumber($table->ch_value)==0)
174 {
175 $is_error++;
176 $this->set_error("ch_value", _("Valeur incorrecte"));
177 }
178 // Date must be the greatest
179 $count=$cn->get_value("select count(*)
180 from
181 currency_history
182 where
183 currency_id =$1
184 and ch_from >= to_date($2,'DD.MM.YYYY') ", array($table->currency_id, $table->str_from));
185 if ($count>0)
186 {
187 $is_error++;
188 $this->set_error("str_from", _("Date doit être après la dernière valeur"));
189 }
190 }
191 elseif (trim($table->str_from)!=""||trim($table->ch_value)!="")
192 {
193 $is_error++;
194 $this->set_error("str_from", _("Valeur manquante"));
195 }
196 }
197 //-- duplicate iso code
198 $cnt=$cn->get_value("select count(*) from currency where id != $1 and cr_code_iso=$2",
199 array($table->currency_id, $table->cr_code_iso));
200 if ($cnt>0)
201 {
202 $is_error++;
203 $this->set_error("cr_code_iso", _("Code ISO existe déjà"));
204 }
205 // - check size
206 if ( trim(mb_strlen($table->cr_code_iso))>10)
207 {
208 $is_error++;
209 $this->set_error("cr_code_iso", _("Code ISO trop long max = 10"));
210 }
211 // - check size
212 if ( trim(mb_strlen($table->cr_name))>80)
213 {
214 $is_error++;
215 $this->set_error("cr_name", _("Nom trop long max=80"));
216 }
217 if ( $table->ch_value != "" && ($table->ch_value < 0 || $table->ch_value == 0)) {
218 $is_error++;
219 $this->set_error("ch_value", _("Valeur incorrecte"));
220 }
221 if ($is_error==0)
222 {
223 return TRUE;
224 }
225
226 return FALSE;
227 }
228
229 /**
230 * Either insert a new currency + one rate or add a rate to an existing currency
231 */
232 function save()
233 {
234 $cn=Dossier::connect();
235 try
236 {
237 $cn->start();
238 $record=$this->get_table();
239
240 if ($record->currency_id==-1)
241 {
242 // Append a new currency and a default value
244 $currency->setp("cr_code_iso", mb_strtoupper($record->cr_code_iso));
245 $currency->setp("cr_name", $record->cr_name);
246 $currency->insert();
247
248 $currency_history=new Currency_history_SQL($cn);
249 $currency_history->setp("ch_value", $record->ch_value);
250 $currency_history->setp("ch_from", $record->str_from);
251 $currency_history->setp("currency_id", $currency->id);
252 $currency_history->insert();
253
254 $this->table->currency_id=$currency->id;
255 $this->table->load();
256 }
257 else
258 {
259 // append a value in currency_historique and update currency
260 $currency=new Currency_SQL($cn, $record->currency_id);
261 $currency->setp("cr_code_iso", mb_strtoupper($record->cr_code_iso));
262 $currency->setp("cr_name", $record->cr_name);
263 $currency->update();
264
265 if ($record->str_from!="")
266 {
267 $currency_history=new Currency_history_SQL($cn);
268 $currency_history->setp("ch_value", $record->ch_value);
269 $currency_history->setp("ch_from", $record->str_from);
270 $currency_history->setp("currency_id", $currency->id);
271 $currency_history->insert();
272 }
273 }
274 $cn->commit();
275 }
276 catch (Exception $ex)
277 {
278 $cn->rollback();
279 throw ($ex);
280 }
281 }
282 /**
283 * Fill the object from request
284 * parameters :
285 * - cr_code_iso
286 * - cr_name
287 * - p_id
288 * - new_rate_date
289 * - new_rate_value
290 *
291 */
292 function from_request()
293 {
294 $http=new HttpInput();
295 $this->table->cr_code_iso=mb_strtoupper($http->request("cr_code_iso"));
296 $this->table->cr_name=$http->request("cr_name");
297 $this->table->currency_id=$http->request("p_id", "number");
298 $this->table->ch_value=$http->request("new_rate_value");
299 $this->table->str_from=$http->request("new_rate_date");
300 }
301 /**
302 *
303 * We cannot modify the default currency (id := 0)
304 */
305 function display_row($p_row)
306 {
307 if ($p_row['currency_id']==0)
308 {
309 $this->set_update_row(FALSE);
310 $this->set_delete_row(FALSE);
311 parent::display_row($p_row);
312 return;
313 }
314 if ($this->is_currency_used($p_row['currency_id'])==TRUE)
315 {
316 $this->set_delete_row(FALSE);
317 } else {
318 $this->set_delete_row(TRUE);
319
320 }
321
322 $this->set_update_row(TRUE);
323 parent::display_row($p_row);
324 }
325 /**
326 * Delete after checking the currency is not used
327 */
328 function delete()
329 {
330 $id=$this->get_table()->currency_id;
331 if ( $this->is_currency_used($id) == FALSE) $this->table->delete();
332 }
333}
isNumber($p_int)
isDate($p_date)
Verifie qu'une date est bien formaté en d.m.y et est valable.
for($i=0; $i< $nb_vatex_code; $i++)($i % 2==0) ? " odd " $cnt
_("actif, passif,charge,...")
$ex
Manage the configuration of currency , add currency, rate, remove and update Concerned tables are v_c...
display_row($p_row)
We cannot modify the default currency (id := 0)
save()
Either insert a new currency + one rate or add a rate to an existing currency.
__construct(V_Currency_Last_Value_SQL $p_table)
from_request()
Fill the object from request parameters :
input()
Box to enter either a new currency or update a existing one.
check()
Check that the value are correct :
is_currency_used($p_id)
returns TRUE the currency is used otherwise FALSE.
abstract of the table public.currency
abstract of the table public.currency_history
manage the http input (get , post, request) and extract from an array
Html Input : Input a date format dd.mm.yyyy The property title should be set to indicate what it is e...
This class handles only the numeric input, the input will call a javascript to change comma to period...
Html Input Text member :
Purpose is to propose a librairy to display a table content and allow to update and delete row ,...
set_icon_mod($pString)
Set the icon to modify at the right ,the first col or left of the row, if the mod if custom ,...
set_delete_row($p_value)
Enable or disable the deletion of rows.
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...
set_update_row($p_value)
Enable or disable the updating of rows.
set_sort_column($p_col)
set the column to sort by default
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
$all table
$count
if( $delta< 0) elseif( $delta==0)