Merged changes up to version 2.3.4 into unstable.
[fa-stable.git] / includes / data_checks.inc
1 <?php
2 /**********************************************************************
3     Copyright (C) FrontAccounting, LLC.
4         Released under the terms of the GNU General Public License, GPL, 
5         as published by the Free Software Foundation, either version 3 
6         of the License, or (at your option) any later version.
7     This program is distributed in the hope that it will be useful,
8     but WITHOUT ANY WARRANTY; without even the implied warranty of
9     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
10     See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
11 ***********************************************************************/
12 function db_has_customers()
13 {
14         return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."debtors_master");
15 }
16
17 function check_db_has_customers($msg)
18 {
19         global $path_to_root;
20     if (!db_has_customers()) 
21     {
22         display_error($msg, true);
23         end_page();
24         exit;   
25     }   
26 }
27
28 function db_has_currencies()
29 {
30         return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."currencies");
31 }
32
33 function check_db_has_currencies($msg)
34 {
35         global $path_to_root;
36     if (!db_has_currencies()) 
37     {
38         display_error($msg, true);
39         end_page();
40         exit;   
41     }   
42 }
43
44 function db_has_sales_types()
45 {
46         return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."sales_types");
47 }
48
49 function check_db_has_sales_types($msg)
50 {
51         global $path_to_root;
52     if (!db_has_sales_types()) 
53     {
54         display_error($msg, true);
55         end_page();
56         exit;   
57     }   
58 }
59
60 function db_has_item_tax_types()
61 {
62         return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."item_tax_types");
63 }
64
65 function check_db_has_item_tax_types($msg)
66 {
67         global $path_to_root;
68     if (!db_has_item_tax_types()) 
69     {
70         display_error($msg, true);
71         end_page();
72         exit;   
73     }   
74 }
75
76 function db_has_tax_types()
77 {
78         return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."tax_types");
79 }
80
81 function check_db_has_tax_types($msg)
82 {
83         global $path_to_root;
84     if (!db_has_tax_types()) 
85     {
86         display_error($msg, true);
87         end_page();
88         exit;   
89     }   
90 }
91
92 function db_has_tax_groups()
93 {
94         return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."tax_groups");
95 }
96
97 function check_db_has_tax_groups($msg)
98 {
99         global $path_to_root;
100     if (!db_has_tax_groups()) 
101     {
102         display_error($msg, true);
103         end_page();
104         exit;   
105     }   
106 }
107
108 function db_has_movement_types()
109 {
110         return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."movement_types");
111 }
112
113 function check_db_has_movement_types($msg)
114 {
115         global $path_to_root;
116     if (!db_has_movement_types()) 
117     {
118         display_error($msg, true);
119         end_page();
120         exit;   
121     }   
122 }
123
124 function db_customer_has_branches($customer_id)
125 {
126         return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."cust_branch "
127                 ."WHERE debtor_no=".db_escape($customer_id));
128 }
129
130 function db_has_customer_branches()
131 {
132         return check_empty_result("SELECT COUNT(*) FROM "
133                 .TB_PREF."cust_branch WHERE !inactive");
134 }
135
136 function check_db_has_customer_branches($msg)
137 {
138         global $path_to_root;
139     if (!db_has_customer_branches()) 
140     {
141         display_error($msg, true);
142         end_page();
143         exit;   
144     }   
145 }
146
147 function db_has_sales_people()
148 {
149         return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."salesman");
150 }
151
152 function check_db_has_sales_people($msg)
153 {
154         global $path_to_root;
155     if (!db_has_sales_people()) 
156     {
157         display_error($msg, true);
158         end_page();
159         exit;   
160     }   
161 }
162
163 function db_has_sales_areas()
164 {
165         return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."areas");
166 }
167
168 function check_db_has_sales_areas($msg)
169 {
170         global $path_to_root;
171     if (!db_has_sales_areas()) 
172     {
173         display_error($msg, true);
174         end_page();
175         exit;   
176     }   
177 }
178
179 function db_has_shippers()
180 {
181         return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."shippers");
182 }
183
184 function check_db_has_shippers($msg)
185 {
186         global $path_to_root;
187     if (!db_has_shippers()) 
188     {
189         display_error($msg, true);
190         end_page();
191         exit;   
192     }   
193 }
194
195 function db_has_open_workorders()
196 {
197         return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."workorders WHERE closed=0");
198 }
199
200 function db_has_workorders()
201 {
202         return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."workorders");
203 }
204
205 function check_db_has_workorders($msg)
206 {
207         global $path_to_root;
208     if (!db_has_workorders()) 
209     {
210         display_error($msg, true);
211         end_page();
212         exit;   
213     }   
214 }
215
216 function db_has_open_dimensions()
217 {
218         return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."dimensions WHERE closed=0");
219 }
220
221 function db_has_dimensions()
222 {
223         return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."dimensions");
224 }
225
226 function check_db_has_dimensions($msg)
227 {
228         global $path_to_root;
229     if (!db_has_dimensions()) 
230     {
231         display_error($msg, true);
232         end_page();
233         exit;   
234     }   
235 }
236
237
238 function db_has_suppliers()
239 {
240         return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."suppliers");
241 }
242
243 function check_db_has_suppliers($msg)
244 {
245         global $path_to_root;
246     if (!db_has_suppliers()) 
247     {
248         display_error($msg, true);
249         end_page();
250         exit;   
251     }   
252 }
253
254 function db_has_stock_items()
255 {
256         return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."stock_master");
257 }
258
259 function check_db_has_stock_items($msg)
260 {
261         global $path_to_root;
262     if (!db_has_stock_items()) 
263     {
264         display_error($msg, true);
265         end_page();
266         exit;   
267     }   
268 }
269
270 function db_has_bom_stock_items()
271 {
272         return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."stock_master WHERE mb_flag='M'");
273 }
274
275 function check_db_has_bom_stock_items($msg)
276 {
277         global $path_to_root;
278     if (!db_has_bom_stock_items()) 
279     {
280         display_error($msg, true);
281         end_page();
282         exit;   
283     }   
284 }
285
286 function db_has_manufacturable_items()
287 {
288         return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."stock_master WHERE (mb_flag='M')");
289 }
290
291 function check_db_has_manufacturable_items($msg)
292 {
293         global $path_to_root;
294     if (!db_has_manufacturable_items()) 
295     {
296         display_error($msg, true);
297         end_page();
298         exit;   
299     }   
300 }
301
302 function db_has_purchasable_items()
303 {
304         return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."stock_master WHERE mb_flag!='M'");
305 }
306
307 function check_db_has_purchasable_items($msg)
308 {
309         global $path_to_root;
310     if (!db_has_purchasable_items()) 
311     {
312         display_error($msg, true);
313         end_page();
314         exit;   
315     }   
316 }
317
318 function db_has_costable_items()
319 {
320         return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."stock_master WHERE mb_flag!='D'");
321 }
322
323 function check_db_has_costable_items($msg)
324 {
325         global $path_to_root;
326     if (!db_has_costable_items()) 
327     {
328         display_error($msg, true);
329         end_page();
330         exit;   
331     }   
332 }
333
334 function db_has_stock_categories()
335 {
336         return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."stock_category");
337 }
338
339 function check_db_has_stock_categories($msg)
340 {
341         global $path_to_root;
342     if (!db_has_stock_categories()) 
343     {
344         display_error($msg, true);
345         end_page();
346         exit;   
347     }   
348 }
349
350 function db_has_workcentres()
351 {
352         return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."workcentres");
353 }
354
355 function check_db_has_workcentres($msg)
356 {
357         global $path_to_root;
358     if (!db_has_workcentres()) 
359     {
360         display_error($msg, true);
361         end_page();
362         exit;   
363     }   
364 }
365
366 function db_has_locations()
367 {
368         return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."locations");
369 }
370
371 function check_db_has_locations($msg)
372 {
373         global $path_to_root;
374     if (!db_has_locations()) 
375     {
376         display_error($msg, true);
377         end_page();
378         exit;   
379     }   
380 }
381
382 function db_has_bank_accounts()
383 {
384         return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."bank_accounts");
385 }
386
387 function check_db_has_bank_accounts($msg)
388 {
389         global $path_to_root;
390
391     if (!db_has_bank_accounts()) 
392     {
393         display_error($msg, true);
394         end_page();
395         exit;   
396     }   
397 }
398
399 function db_has_cash_accounts()
400 {
401         return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."bank_accounts
402                 WHERE account_type=3");
403 }
404
405 function db_has_gl_accounts()
406 {
407         return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."chart_master");
408 }
409
410 function db_has_gl_account_groups()
411 {
412         return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."chart_types");
413 }
414
415 function check_db_has_gl_account_groups($msg)
416 {
417         global $path_to_root;
418     if (!db_has_gl_account_groups()) 
419     {
420         display_error($msg, true);
421         end_page();
422         exit;   
423     }   
424 }
425
426 function db_has_quick_entries()
427 {
428         return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."quick_entries");
429 }
430
431 function db_has_tags($type)
432 {
433         return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."tags WHERE type=".db_escape($type));
434 }
435
436 function check_db_has_tags($type, $msg)
437 {
438         global $path_to_root;
439     if (!db_has_tags($type)) 
440     {
441         display_error($msg, true);
442         end_page();
443         exit;   
444     }   
445 }
446
447 function check_empty_result($sql)
448 {
449         $result = db_query($sql, "could not do check empty query");     
450         
451         $myrow = db_fetch_row($result);
452         return $myrow[0] > 0;
453 }
454 //
455 //      Integer input check 
456 //      Return 1 if number has proper form and is within <min, max> range
457 //
458 function check_int($postname, $min=null, $max=null) {
459         if(!isset($_POST[$postname]))
460           return 0;
461     $num = input_num($postname);
462     if(!is_int($num)) 
463           return 0;
464     if (isset($min) && ($num<$min)) 
465           return 0;
466     if (isset($max) && ($num>$max)) 
467           return 0;
468     return 1;
469 }
470 //
471 //      Numeric input check.
472 //      Return 1 if number has proper form and is within <min, max> range
473 //      Empty/not defined fields are defaulted to $dflt value.
474 //
475 function check_num($postname, $min=null, $max=null, $dflt=0) {
476         if(!isset($_POST[$postname]))
477           return 0;
478     $num = input_num($postname, $dflt);
479     if ($num === false || $num === null) 
480           return 0;
481     if (isset($min) && ($num<$min)) 
482           return 0;
483     if (isset($max) && ($num>$max)) 
484           return 0;
485     return 1;
486 }
487
488 function check_is_closed($type, $type_no, $msg=null)
489 {
490         global $systypes_array;
491         
492     if (($type_no > 0) && is_closed_trans($type, $type_no))
493     {
494         if (!$msg)
495                 $msg = sprintf(_("%s #%s is closed for further edition."), $systypes_array[$type], $type_no);
496         display_error($msg, true);
497         end_page();
498         exit;
499     }
500 }
501
502
503 ?>