Bug submitting forms in FA 2.3.x when checkbox field not changed. Fixed
[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_currency_rates($currency, $date_, $msg=false)
45 {
46         $date = date2sql($date_);
47         
48         if (is_company_currency($currency))
49                 return 1;
50         $ret = check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."exchange_rates WHERE curr_code = '$currency' && date_ <= '$date'");
51         if ($ret == 0 && $msg)
52                 display_error(sprintf(_("Cannot retrieve exchange rate for currency %s as of %s. Please add exchange rate manually on Exchange Rates page."),
53                                          $currency, $date_), true);
54         return $ret;                             
55 }
56
57 function db_has_sales_types()
58 {
59         return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."sales_types");
60 }
61
62 function check_db_has_sales_types($msg)
63 {
64         global $path_to_root;
65     if (!db_has_sales_types()) 
66     {
67         display_error($msg, true);
68         end_page();
69         exit;   
70     }   
71 }
72
73 function db_has_item_tax_types()
74 {
75         return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."item_tax_types");
76 }
77
78 function check_db_has_item_tax_types($msg)
79 {
80         global $path_to_root;
81     if (!db_has_item_tax_types()) 
82     {
83         display_error($msg, true);
84         end_page();
85         exit;   
86     }   
87 }
88
89 function db_has_tax_types()
90 {
91         return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."tax_types");
92 }
93
94 function check_db_has_tax_types($msg)
95 {
96         global $path_to_root;
97     if (!db_has_tax_types()) 
98     {
99         display_error($msg, true);
100         end_page();
101         exit;   
102     }   
103 }
104
105 function db_has_tax_groups()
106 {
107         return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."tax_groups");
108 }
109
110 function check_db_has_tax_groups($msg)
111 {
112         global $path_to_root;
113     if (!db_has_tax_groups()) 
114     {
115         display_error($msg, true);
116         end_page();
117         exit;   
118     }   
119 }
120
121 function db_has_movement_types()
122 {
123         return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."movement_types");
124 }
125
126 function check_db_has_movement_types($msg)
127 {
128         global $path_to_root;
129     if (!db_has_movement_types()) 
130     {
131         display_error($msg, true);
132         end_page();
133         exit;   
134     }   
135 }
136
137 function db_customer_has_branches($customer_id)
138 {
139         return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."cust_branch "
140                 ."WHERE debtor_no=".db_escape($customer_id));
141 }
142
143 function db_has_customer_branches()
144 {
145         return check_empty_result("SELECT COUNT(*) FROM "
146                 .TB_PREF."cust_branch WHERE !inactive");
147 }
148
149 function check_db_has_customer_branches($msg)
150 {
151         global $path_to_root;
152     if (!db_has_customer_branches()) 
153     {
154         display_error($msg, true);
155         end_page();
156         exit;   
157     }   
158 }
159
160 function db_has_sales_people()
161 {
162         return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."salesman");
163 }
164
165 function check_db_has_sales_people($msg)
166 {
167         global $path_to_root;
168     if (!db_has_sales_people()) 
169     {
170         display_error($msg, true);
171         end_page();
172         exit;   
173     }   
174 }
175
176 function db_has_sales_areas()
177 {
178         return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."areas");
179 }
180
181 function check_db_has_sales_areas($msg)
182 {
183         global $path_to_root;
184     if (!db_has_sales_areas()) 
185     {
186         display_error($msg, true);
187         end_page();
188         exit;   
189     }   
190 }
191
192 function db_has_shippers()
193 {
194         return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."shippers");
195 }
196
197 function check_db_has_shippers($msg)
198 {
199         global $path_to_root;
200     if (!db_has_shippers()) 
201     {
202         display_error($msg, true);
203         end_page();
204         exit;   
205     }   
206 }
207
208 function db_has_open_workorders()
209 {
210         return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."workorders WHERE closed=0");
211 }
212
213 function db_has_workorders()
214 {
215         return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."workorders");
216 }
217
218 function check_db_has_workorders($msg)
219 {
220         global $path_to_root;
221     if (!db_has_workorders()) 
222     {
223         display_error($msg, true);
224         end_page();
225         exit;   
226     }   
227 }
228
229 function db_has_open_dimensions()
230 {
231         return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."dimensions WHERE closed=0");
232 }
233
234 function db_has_dimensions()
235 {
236         return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."dimensions");
237 }
238
239 function check_db_has_dimensions($msg)
240 {
241         global $path_to_root;
242     if (!db_has_dimensions()) 
243     {
244         display_error($msg, true);
245         end_page();
246         exit;   
247     }   
248 }
249
250
251 function db_has_suppliers()
252 {
253         return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."suppliers");
254 }
255
256 function check_db_has_suppliers($msg)
257 {
258         global $path_to_root;
259     if (!db_has_suppliers()) 
260     {
261         display_error($msg, true);
262         end_page();
263         exit;   
264     }   
265 }
266
267 function db_has_stock_items()
268 {
269         return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."stock_master");
270 }
271
272 function check_db_has_stock_items($msg)
273 {
274         global $path_to_root;
275     if (!db_has_stock_items()) 
276     {
277         display_error($msg, true);
278         end_page();
279         exit;   
280     }   
281 }
282
283 function db_has_bom_stock_items()
284 {
285         return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."stock_master WHERE mb_flag='M'");
286 }
287
288 function check_db_has_bom_stock_items($msg)
289 {
290         global $path_to_root;
291     if (!db_has_bom_stock_items()) 
292     {
293         display_error($msg, true);
294         end_page();
295         exit;   
296     }   
297 }
298
299 function db_has_manufacturable_items()
300 {
301         return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."stock_master WHERE (mb_flag='M')");
302 }
303
304 function check_db_has_manufacturable_items($msg)
305 {
306         global $path_to_root;
307     if (!db_has_manufacturable_items()) 
308     {
309         display_error($msg, true);
310         end_page();
311         exit;   
312     }   
313 }
314
315 function db_has_purchasable_items()
316 {
317         return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."stock_master WHERE mb_flag!='M'");
318 }
319
320 function check_db_has_purchasable_items($msg)
321 {
322         global $path_to_root;
323     if (!db_has_purchasable_items()) 
324     {
325         display_error($msg, true);
326         end_page();
327         exit;   
328     }   
329 }
330
331 function db_has_costable_items()
332 {
333         return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."stock_master WHERE mb_flag!='D'");
334 }
335
336 function check_db_has_costable_items($msg)
337 {
338         global $path_to_root;
339     if (!db_has_costable_items()) 
340     {
341         display_error($msg, true);
342         end_page();
343         exit;   
344     }   
345 }
346
347 function db_has_stock_categories()
348 {
349         return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."stock_category");
350 }
351
352 function check_db_has_stock_categories($msg)
353 {
354         global $path_to_root;
355     if (!db_has_stock_categories()) 
356     {
357         display_error($msg, true);
358         end_page();
359         exit;   
360     }   
361 }
362
363 function db_has_workcentres()
364 {
365         return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."workcentres");
366 }
367
368 function check_db_has_workcentres($msg)
369 {
370         global $path_to_root;
371     if (!db_has_workcentres()) 
372     {
373         display_error($msg, true);
374         end_page();
375         exit;   
376     }   
377 }
378
379 function db_has_locations()
380 {
381         return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."locations");
382 }
383
384 function check_db_has_locations($msg)
385 {
386         global $path_to_root;
387     if (!db_has_locations()) 
388     {
389         display_error($msg, true);
390         end_page();
391         exit;   
392     }   
393 }
394
395 function db_has_bank_accounts()
396 {
397         return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."bank_accounts");
398 }
399
400 function check_db_has_bank_accounts($msg)
401 {
402         global $path_to_root;
403
404     if (!db_has_bank_accounts()) 
405     {
406         display_error($msg, true);
407         end_page();
408         exit;   
409     }   
410 }
411
412 function db_has_cash_accounts()
413 {
414         return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."bank_accounts
415                 WHERE account_type=3");
416 }
417
418 function db_has_gl_accounts()
419 {
420         return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."chart_master");
421 }
422
423 function db_has_gl_account_groups()
424 {
425         return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."chart_types");
426 }
427
428 function check_db_has_gl_account_groups($msg)
429 {
430         global $path_to_root;
431     if (!db_has_gl_account_groups()) 
432     {
433         display_error($msg, true);
434         end_page();
435         exit;   
436     }   
437 }
438
439 function db_has_quick_entries()
440 {
441         return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."quick_entries");
442 }
443
444 function db_has_tags($type)
445 {
446         return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."tags WHERE type=".db_escape($type));
447 }
448
449 function check_db_has_tags($type, $msg)
450 {
451         global $path_to_root;
452     if (!db_has_tags($type)) 
453     {
454         display_error($msg, true);
455         end_page();
456         exit;   
457     }   
458 }
459
460 function check_empty_result($sql)
461 {
462         $result = db_query($sql, "could not do check empty query");     
463         
464         $myrow = db_fetch_row($result);
465         return $myrow[0] > 0;
466 }
467 //
468 //      Integer input check 
469 //      Return 1 if number has proper form and is within <min, max> range
470 //
471 function check_int($postname, $min=null, $max=null) {
472         if(!isset($_POST[$postname]))
473           return 0;
474     $num = input_num($postname);
475     if(!is_int($num)) 
476           return 0;
477     if (isset($min) && ($num<$min)) 
478           return 0;
479     if (isset($max) && ($num>$max)) 
480           return 0;
481     return 1;
482 }
483 //
484 //      Numeric input check.
485 //      Return 1 if number has proper form and is within <min, max> range
486 //      Empty/not defined fields are defaulted to $dflt value.
487 //
488 function check_num($postname, $min=null, $max=null, $dflt=0) {
489         if(!isset($_POST[$postname]))
490           return 0;
491     $num = input_num($postname, $dflt);
492     if ($num === false || $num === null) 
493           return 0;
494     if (isset($min) && ($num<$min)) 
495           return 0;
496     if (isset($max) && ($num>$max)) 
497           return 0;
498     return 1;
499 }
500
501
502 ?>