[0000095] Bad behaviour of routine number_format and round in Windows.
[fa-stable.git] / includes / current_user.inc
1 <?php
2
3 include_once($path_to_root . "/includes/prefs/userprefs.inc");
4
5 //--------------------------------------------------------------------------
6
7 class current_user
8 {
9
10         var $loginname;
11         var $username;
12         var     $name;
13         var $company;
14         var $access;
15
16         var $logged;
17
18         var $prefs;
19
20         function current_user()
21         {
22                 $this->loginname = $username = $this->name = $this->company = "";
23                 $this->logged = false;
24
25                 $this->prefs = null;
26         }
27
28         function logged_in()
29         {
30                 return $this->logged;
31         }
32
33         function set_company($company)
34         {
35                 $this->company = $company;
36         }
37
38         function login($company, $loginname, $password)
39         {
40                 $this->set_company($company);
41
42                 $Auth_Result = get_user_for_login($loginname, $password);
43
44                 if (db_num_rows($Auth_Result) > 0)
45                 {
46             $myrow = db_fetch($Auth_Result);
47
48                     $this->access = $myrow["full_access"];
49                     $this->name = $myrow["real_name"];
50                     $this->loginname = $loginname;
51                     $this->username = $this->loginname;
52                     $this->prefs = new user_prefs($myrow);
53
54                     update_user_visitdate($loginname);
55                     $this->logged = true;
56
57                 }
58                 else
59                 {
60                         $this->logged = false;
61                 }
62
63                 return $this->logged;
64         }
65
66         function check_user_access()
67         {
68                 global $security_groups;
69                 return is_array($security_groups[$this->access]);
70         }
71
72         function can_access_page($page_level)
73         {
74                 global $security_groups;
75                 // first registered company has site admin privileges
76                 return isset($page_level) && in_array($page_level, $security_groups[$this->access])
77                         && ($this->company == 0 || $page_level != 20); 
78         }
79
80         function get_db_connection()
81         {
82         global $db_connections;
83
84         $connection = $db_connections[$this->company];
85
86         //print_r($connection);
87
88         $db = mysql_connect($connection["host"] ,
89                 $connection["dbuser"], $connection["dbpassword"]);
90         mysql_select_db($connection["dbname"],$db);
91
92                 if (!defined('TB_PREF'))
93                         define('TB_PREF', $connection["tbpref"]);
94
95         return $db;
96         }
97
98         function update_prefs($price_dec, $qty_dec, $exrate_dec, $percent_dec, $showgl, $showcodes,
99                 $date_format, $date_sep, $tho_sep, $dec_sep, $theme, $pagesize, $show_hints) {
100                 update_user_display_prefs($this->username, $price_dec, $qty_dec, $exrate_dec, $percent_dec, $showgl,
101                         $showcodes, $date_format, $date_sep, $tho_sep, $dec_sep, $theme, $pagesize, $show_hints);
102
103                 // re-read the prefs
104                 $user = get_user($this->username);
105                 $this->prefs = new user_prefs($user);
106         }
107 }
108
109 //--------------------------------------------------------------------------
110
111 function round2($number, $decimals=0)
112 {
113         $delta = ($number < 0 ? -.000001 : .000001);
114         return round($number+$delta, $decimals);
115 }
116
117 function number_format2($number, $decimals=0)
118 {
119         global $thoseps, $decseps;
120         $tsep = $thoseps[$_SESSION["wa_current_user"]->prefs->tho_sep()];
121         $dsep = $decseps[$_SESSION["wa_current_user"]->prefs->dec_sep()];
122         //return number_format($number, $decimals, $dsep,       $tsep);
123         $delta = ($number < 0 ? -.000001 : .000001);
124         return number_format($number+$delta, $decimals, $dsep,  $tsep);
125 }
126
127 function price_format($number) {
128     return number_format2($number,
129         $_SESSION["wa_current_user"]->prefs->price_dec());
130 }
131 // 2008-06-15. Added extra parameter $stock_id and reference for $dec
132 //--------------------------------------------------------------------
133 function qty_format($number, $stock_id=null, &$dec) {
134         $dec = get_qty_dec($stock_id);
135     return number_format2($number, $dec);
136 }
137 // and get_qty_dec
138 function get_qty_dec($stock_id=null)
139 {
140         global $path_to_root;
141         include_once($path_to_root."/inventory/includes/db/items_units_db.inc");
142         if ($stock_id != null)
143                 $dec = get_unit_dec($stock_id);
144         if ($stock_id == null || $dec == -1 || $dec == null)
145                 $dec = $_SESSION["wa_current_user"]->prefs->qty_dec();
146         return $dec;
147 }
148 //-------------------------------------------------------------------
149 function exrate_format($number) {
150     return number_format2($number,
151         $_SESSION["wa_current_user"]->prefs->exrate_dec());
152 }
153
154 function percent_format($number) {
155     return number_format2($number,
156         $_SESSION["wa_current_user"]->prefs->percent_dec());
157 }
158
159 function user_numeric($input) {
160     global $decseps, $thoseps;
161
162     $num = trim($input);
163     $sep = $thoseps[user_tho_sep()];
164     if($sep!='') $num = str_replace( $sep, '', $num);
165         str_replace($sep, '', $num);
166     $sep = $decseps[user_dec_sep()];
167     if($sep!='.') $num = str_replace( $sep, '.', $num);
168
169     if (!is_numeric($num))
170           return false;
171     $num = (float)$num;
172     if ($num == (int)$num)
173           return (int)$num;
174     else
175           return $num;
176 }
177
178 function user_company()
179 {
180         return $_SESSION["wa_current_user"]->company;
181 }
182
183 function user_language()
184 {
185         return $_SESSION["wa_current_user"]->prefs->language();
186 }
187
188 function user_qty_dec()
189 {
190         return $_SESSION["wa_current_user"]->prefs->qty_dec();
191 }
192
193 function user_price_dec()
194 {
195         return $_SESSION["wa_current_user"]->prefs->price_dec();
196 }
197
198 function user_exrate_dec()
199 {
200         return $_SESSION["wa_current_user"]->prefs->exrate_dec();
201 }
202
203 function user_percent_dec()
204 {
205         return $_SESSION["wa_current_user"]->prefs->percent_dec();
206 }
207
208 function user_show_gl_info()
209 {
210         return $_SESSION["wa_current_user"]->prefs->show_gl_info();
211 }
212
213 function user_show_codes()
214 {
215         return $_SESSION["wa_current_user"]->prefs->show_codes();
216 }
217
218 function user_date_format()
219 {
220         return $_SESSION["wa_current_user"]->prefs->date_format();
221 }
222
223 function user_date_display()
224 {
225         return $_SESSION["wa_current_user"]->prefs->date_display();
226 }
227
228 function user_date_sep()
229 {
230         return $_SESSION["wa_current_user"]->prefs->date_sep();
231 }
232
233 function user_tho_sep()
234 {
235         return $_SESSION["wa_current_user"]->prefs->tho_sep();
236 }
237
238 function user_dec_sep()
239 {
240         return $_SESSION["wa_current_user"]->prefs->dec_sep();
241 }
242
243 function user_theme()
244 {
245         return $_SESSION["wa_current_user"]->prefs->get_theme();
246 }
247
248 function user_pagesize()
249 {
250         return $_SESSION["wa_current_user"]->prefs->get_pagesize();
251 }
252
253 function user_hints()
254 {
255         return $_SESSION["wa_current_user"]->prefs->show_hints();
256 }
257
258 function set_user_prefs($price_dec, $qty_dec, $exrate_dec, $percent_dec, $showgl, $showcodes,
259         $date_format, $date_sep, $tho_sep, $dec_sep, $theme, $pagesize, $show_hints)
260 {
261
262         $_SESSION["wa_current_user"]->update_prefs($price_dec, $qty_dec, $exrate_dec, $percent_dec, $showgl, $showcodes,
263                 $date_format, $date_sep, $tho_sep, $dec_sep, $theme, $pagesize, $show_hints);
264 }
265
266 function add_user_js_data() {
267         global $path_to_root, $thoseps, $decseps;
268
269         $ts = $thoseps[user_tho_sep()];
270         $ds = $decseps[user_dec_sep()];
271
272     $js = "\n<script type=\"text/javascript\">\n"
273           . "<!--\n"
274           . "var user = {\n"
275           . "theme: '". $path_to_root . '/themes/'. 'default' /*user_theme()*/.'/'."',\n"
276           . "loadtxt: '"._('Requesting data...')."',\n"
277           . "ts: '$ts',\n"
278           . "ds: '$ds',\n"
279           . "pdec : " . user_price_dec() . "}\n--></script>";
280
281   add_js_source($js);
282 }
283
284 //--------------------------------------------------------------------------
285
286 ?>