noalyss Version-9
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 }
66
67 /**
68 * returns TRUE the currency is used otherwise FALSE. We cannot delete a currency which is used in a
69 * operation
70 * @param integer $p_id currency_id
71 * @returns boolean true if currency is used
72 */
74 {
75 global $cn;
76 $cnt_used=$cn->get_value(" select count(*) from jrn where currency_id=$1",[$p_id]);
77 if ($cnt_used > 0)
78 {
79 return TRUE;
80 }
81 return FALSE;
82 }
83 /**
84 * Box to enter either a new currency or update a existing one
85 */
86 function input()
87 {
88 $record=$this->get_table();
89 $cr_code_iso=new IText("cr_code_iso", $record->cr_code_iso);
90 $cr_code_iso->size=10;
91 $cr_name=new IText("cr_name", $record->cr_name);
92 $cr_name->size=50;
93 $a_histo=$record->cn->get_array("select id,to_char(ch_from,'DD.MM.YYYY') as str_from,ch_value
94 from
95 currency_history
96 where
97 currency_id=$1", array($record->currency_id));
98 $new_rate_date=new IDate("new_rate_date");
99 $new_rate_value=new INum("new_rate_value");
100 $new_rate_value->prec=6;
101 if ($record->currency_id!=-1)
102 {
103 require NOALYSS_TEMPLATE."/currency_mtable_input.php";
104 }
105 else
106 {
107 require NOALYSS_TEMPLATE."/currency_mtable_input_new.php";
108 }
109 }
110
111 /**
112 * Check that the value are correct :
113 * - Code iso must be unique
114 * - Name cannot be empty
115 * - At least one rate
116 * - Date of the rate
117 * - code iso is max 10 char
118 * - name is max 80
119 * Default currency (id=0) cannot be changed
120 */
121 function check()
122 {
123 global $cn;
124 $table=$this->get_table();
125 $is_error=0;
126 if ( $table->currency_id == 0) {
127 $is_error++;
128 $this->set_error("cr_code_iso", _("Devise par défaut ne peut être changée"));
129 }
130 // ------ cr_code_iso can not be empty
131 if (trim($table->cr_code_iso)=="")
132 {
133 $is_error++;
134 $this->set_error("cr_code_iso", _("Code iso ne peut pas être vide"));
135 }
136 // ------ cr_name can not be empty
137 if (trim($table->cr_name)=="")
138 {
139 $is_error++;
140 $this->set_error("cr_name", _("Nom ne peut pas être vide"));
141 }
142 // ----- for new record than we need at leat a date + value
143 if ($table->currency_id==-1)
144 {
145 // -- for new record , we need a date + value
146 if (isDate($table->str_from)==0||trim($table->str_from)=="")
147 {
148 $is_error++;
149 $this->set_error("str_from", _("Date incorrecte, il faut au moins une valeur"));
150 }
151 if (isNumber($table->ch_value)==0||trim($table->ch_value)=="")
152 {
153 $is_error++;
154 $this->set_error("ch_value", _("Valeur incorrecte, il faut au moins une valeur"));
155 }
156 }
157 else
158 {
159 if (trim($table->str_from) =="" && trim($table->ch_value)=="")
160 {
161 // we don't add any new date
162
163 }
164 // -- for update, the date and value must be valid
165 elseif (trim($table->str_from)!=""&&trim($table->ch_value)!="")
166 {
167 if (isDate($table->str_from)==0)
168 {
169 $is_error++;
170 $this->set_error("str_from", _("Date incorrecte"));
171 }
172 if (isNumber($table->ch_value)==0)
173 {
174 $is_error++;
175 $this->set_error("ch_value", _("Valeur incorrecte"));
176 }
177 // Date must be the greatest
178 $count=$cn->get_value("select count(*)
179 from
180 currency_history
181 where
182 currency_id =$1
183 and ch_from >= to_date($2,'DD.MM.YYYY') ", array($table->currency_id, $table->str_from));
184 if ($count>0)
185 {
186 $is_error++;
187 $this->set_error("str_from", _("Date doit être après la dernière valeur"));
188 }
189 }
190 elseif (trim($table->str_from)!=""||trim($table->ch_value)!="")
191 {
192 $is_error++;
193 $this->set_error("str_from", _("Valeur manquante"));
194 }
195 }
196 //-- duplicate iso code
197 $cnt=$cn->get_value("select count(*) from currency where id != $1 and cr_code_iso=$2",
198 array($table->currency_id, $table->cr_code_iso));
199 if ($cnt>0)
200 {
201 $is_error++;
202 $this->set_error("cr_code_iso", _("Code ISO existe déjà"));
203 }
204 // - check size
205 if ( trim(mb_strlen($table->cr_code_iso))>10)
206 {
207 $is_error++;
208 $this->set_error("cr_code_iso", _("Code ISO trop long max = 10"));
209 }
210 // - check size
211 if ( trim(mb_strlen($table->cr_name))>80)
212 {
213 $is_error++;
214 $this->set_error("cr_name", _("Nom trop long max=80"));
215 }
216 if ( $table->ch_value != "" && ($table->ch_value < 0 || $table->ch_value == 0)) {
217 $is_error++;
218 $this->set_error("ch_value", _("Valeur incorrecte"));
219 }
220 if ($is_error==0)
221 {
222 return TRUE;
223 }
224
225 return FALSE;
226 }
227
228 /**
229 * Either insert a new currency + one rate or add a rate to an existing currency
230 */
231 function save()
232 {
234 try
235 {
236 $cn->start();
237 $record=$this->get_table();
238
239 if ($record->currency_id==-1)
240 {
241 // Append a new currency and a default value
243 $currency->setp("cr_code_iso", mb_strtoupper($record->cr_code_iso));
244 $currency->setp("cr_name", $record->cr_name);
245 $currency->insert();
246
247 $currency_history=new Currency_history_SQL($cn);
248 $currency_history->setp("ch_value", $record->ch_value);
249 $currency_history->setp("ch_from", $record->str_from);
250 $currency_history->setp("currency_id", $currency->id);
251 $currency_history->insert();
252
253 $this->table->currency_id=$currency->id;
254 $this->table->load();
255 }
256 else
257 {
258 // append a value in currency_historique and update currency
259 $currency=new Currency_SQL($cn, $record->currency_id);
260 $currency->setp("cr_code_iso", mb_strtoupper($record->cr_code_iso));
261 $currency->setp("cr_name", $record->cr_name);
262 $currency->update();
263
264 if ($record->str_from!="")
265 {
266 $currency_history=new Currency_history_SQL($cn);
267 $currency_history->setp("ch_value", $record->ch_value);
268 $currency_history->setp("ch_from", $record->str_from);
269 $currency_history->setp("currency_id", $currency->id);
270 $currency_history->insert();
271 }
272 }
273 $cn->commit();
274 }
275 catch (Exception $ex)
276 {
277 $cn->rollback();
278 throw ($ex);
279 }
280 }
281 /**
282 * Fill the object from request
283 * parameters :
284 * - cr_code_iso
285 * - cr_name
286 * - p_id
287 * - new_rate_date
288 * - new_rate_value
289 *
290 */
291 function from_request()
292 {
293 $http=new HttpInput();
294 $this->table->cr_code_iso=mb_strtoupper($http->request("cr_code_iso"));
295 $this->table->cr_name=$http->request("cr_name");
296 $this->table->currency_id=$http->request("p_id", "number");
297 $this->table->ch_value=$http->request("new_rate_value");
298 $this->table->str_from=$http->request("new_rate_date");
299 }
300 /**
301 *
302 * We cannot modify the default currency (id := 0)
303 */
304 function display_row($p_row)
305 {
306 if ($p_row['currency_id']==0)
307 {
308 $this->set_update_row(FALSE);
309 $this->set_delete_row(FALSE);
310 parent::display_row($p_row);
311 return;
312 }
313 if ($this->is_currency_used($p_row['currency_id'])==TRUE)
314 {
315 $this->set_delete_row(FALSE);
316 } else {
317 $this->set_delete_row(TRUE);
318
319 }
320
321 $this->set_update_row(TRUE);
322 parent::display_row($p_row);
323 }
324 /**
325 * Delete after checking the currency is not used
326 */
327 function delete()
328 {
329 $id=$this->get_table()->currency_id;
330 if ( $this->is_currency_used($id) == FALSE) $this->table->delete();
331 }
332}
isNumber($p_int)
Definition: ac_common.php:215
isDate($p_date)
Definition: ac_common.php:236
$ex
Definition: balance.inc.php:45
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
static connect()
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...
Definition: idate.class.php:34
This class handles only the numeric input, the input will call a javascript to change comma to period...
Definition: inum.class.php:42
Html Input.
Definition: itext.class.php:30
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
class_currency_history_sql.php
$all table
$count
if( $delta< 0) elseif( $delta==0)