Changed license type to GPLv3 in top of files
[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 WHERE debtor_no='$customer_id'");
127 }
128
129 function db_has_customer_branches()
130 {
131         return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."cust_branch");
132 }
133
134 function check_db_has_customer_branches($msg)
135 {
136         global $path_to_root;
137     if (!db_has_customer_branches()) 
138     {
139         display_error($msg, true);
140         end_page();
141         exit;   
142     }   
143 }
144
145 function db_has_sales_people()
146 {
147         return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."salesman");
148 }
149
150 function check_db_has_sales_people($msg)
151 {
152         global $path_to_root;
153     if (!db_has_sales_people()) 
154     {
155         display_error($msg, true);
156         end_page();
157         exit;   
158     }   
159 }
160
161 function db_has_sales_areas()
162 {
163         return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."areas");
164 }
165
166 function check_db_has_sales_areas($msg)
167 {
168         global $path_to_root;
169     if (!db_has_sales_areas()) 
170     {
171         display_error($msg, true);
172         end_page();
173         exit;   
174     }   
175 }
176
177 function db_has_shippers()
178 {
179         return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."shippers");
180 }
181
182 function check_db_has_shippers($msg)
183 {
184         global $path_to_root;
185     if (!db_has_shippers()) 
186     {
187         display_error($msg, true);
188         end_page();
189         exit;   
190     }   
191 }
192
193 function db_has_open_workorders()
194 {
195         return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."workorders WHERE closed=0");
196 }
197
198 function db_has_workorders()
199 {
200         return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."workorders");
201 }
202
203 function check_db_has_workorders($msg)
204 {
205         global $path_to_root;
206     if (!db_has_workorders()) 
207     {
208         display_error($msg, true);
209         end_page();
210         exit;   
211     }   
212 }
213
214 function db_has_open_dimensions()
215 {
216         return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."dimensions WHERE closed=0");
217 }
218
219 function db_has_dimensions()
220 {
221         return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."dimensions");
222 }
223
224 function check_db_has_dimensions($msg)
225 {
226         global $path_to_root;
227     if (!db_has_dimensions()) 
228     {
229         display_error($msg, true);
230         end_page();
231         exit;   
232     }   
233 }
234
235
236 function db_has_suppliers()
237 {
238         return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."suppliers");
239 }
240
241 function check_db_has_suppliers($msg)
242 {
243         global $path_to_root;
244     if (!db_has_suppliers()) 
245     {
246         display_error($msg, true);
247         end_page();
248         exit;   
249     }   
250 }
251
252 function db_has_stock_items()
253 {
254         return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."stock_master");
255 }
256
257 function check_db_has_stock_items($msg)
258 {
259         global $path_to_root;
260     if (!db_has_stock_items()) 
261     {
262         display_error($msg, true);
263         end_page();
264         exit;   
265     }   
266 }
267
268 function db_has_bom_stock_items()
269 {
270         return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."stock_master WHERE (mb_flag='M' OR mb_flag='K')");
271 }
272
273 function check_db_has_bom_stock_items($msg)
274 {
275         global $path_to_root;
276     if (!db_has_bom_stock_items()) 
277     {
278         display_error($msg, true);
279         end_page();
280         exit;   
281     }   
282 }
283
284 function db_has_manufacturable_items()
285 {
286         return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."stock_master WHERE (mb_flag='M')");
287 }
288
289 function check_db_has_manufacturable_items($msg)
290 {
291         global $path_to_root;
292     if (!db_has_manufacturable_items()) 
293     {
294         display_error($msg, true);
295         end_page();
296         exit;   
297     }   
298 }
299
300 function db_has_purchasable_items()
301 {
302         return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."stock_master WHERE (mb_flag!='M' AND mb_flag!='K')");
303 }
304
305 function check_db_has_purchasable_items($msg)
306 {
307         global $path_to_root;
308     if (!db_has_purchasable_items()) 
309     {
310         display_error($msg, true);
311         end_page();
312         exit;   
313     }   
314 }
315
316 function db_has_costable_items()
317 {
318         return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."stock_master WHERE (mb_flag!='D' AND mb_flag!='K')");
319 }
320
321 function check_db_has_costable_items($msg)
322 {
323         global $path_to_root;
324     if (!db_has_costable_items()) 
325     {
326         display_error($msg, true);
327         end_page();
328         exit;   
329     }   
330 }
331
332 function db_has_stock_categories()
333 {
334         return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."stock_category");
335 }
336
337 function check_db_has_stock_categories($msg)
338 {
339         global $path_to_root;
340     if (!db_has_stock_categories()) 
341     {
342         display_error($msg, true);
343         end_page();
344         exit;   
345     }   
346 }
347
348 function db_has_workcentres()
349 {
350         return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."workcentres");
351 }
352
353 function check_db_has_workcentres($msg)
354 {
355         global $path_to_root;
356     if (!db_has_workcentres()) 
357     {
358         display_error($msg, true);
359         end_page();
360         exit;   
361     }   
362 }
363
364 function db_has_locations()
365 {
366         return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."locations");
367 }
368
369 function check_db_has_locations($msg)
370 {
371         global $path_to_root;
372     if (!db_has_locations()) 
373     {
374         display_error($msg, true);
375         end_page();
376         exit;   
377     }   
378 }
379
380 function db_has_bank_accounts()
381 {
382         return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."bank_accounts");
383 }
384
385 function check_db_has_bank_accounts($msg)
386 {
387         global $path_to_root;
388     if (!db_has_bank_accounts()) 
389     {
390         display_error($msg, true);
391         end_page();
392         exit;   
393     }   
394 }
395
396 function db_has_gl_accounts()
397 {
398         return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."chart_master");
399 }
400
401 function db_has_gl_account_groups()
402 {
403         return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."chart_types");
404 }
405
406 function check_db_has_gl_account_groups($msg)
407 {
408         global $path_to_root;
409     if (!db_has_gl_account_groups()) 
410     {
411         display_error($msg, true);
412         end_page();
413         exit;   
414     }   
415 }
416
417 function db_has_quick_entries()
418 {
419         return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."quick_entries");
420 }
421
422 function check_empty_result($sql)
423 {
424         $result = db_query($sql, "could not do check empty query");     
425         
426         $myrow = db_fetch_row($result);
427         return $myrow[0] > 0;           
428 }
429 //
430 //      Integer input check 
431 //      Return 1 if number has proper form and is within <min, max> range
432 //
433 function check_int($postname, $min=null, $max=null) {
434         if(!isset($_POST[$postname]))
435           return 0;
436     $num = input_num($postname);
437     if(!is_int($num)) 
438           return 0;
439     if (isset($min) && ($num<$min)) 
440           return 0;
441     if (isset($max) && ($num>$max)) 
442           return 0;
443     return 1;
444 }
445 //
446 //      Numeric input check.
447 //      Return 1 if number has proper form and is within <min, max> range
448 //
449 function check_num($postname, $min=null, $max=null) {
450         if(!isset($_POST[$postname]))
451           return 0;
452     $num = input_num($postname);
453     if ($num === false) 
454           return 0;
455     if (isset($min) && ($num<$min)) 
456           return 0;
457     if (isset($max) && ($num>$max)) 
458           return 0;
459     return 1;
460 }
461
462
463 ?>