Merged changes from master branch up to current state.
[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_customer_has_branches($customer_id)
122 {
123         return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."cust_branch "
124                 ."WHERE debtor_no=".db_escape($customer_id));
125 }
126
127 function db_has_customer_branches()
128 {
129         return check_empty_result("SELECT COUNT(*) FROM "
130                 .TB_PREF."cust_branch WHERE !inactive");
131 }
132
133 function check_db_has_customer_branches($msg)
134 {
135         global $path_to_root;
136     if (!db_has_customer_branches()) 
137     {
138         display_error($msg, true);
139         end_page();
140         exit;   
141     }   
142 }
143
144 function db_has_sales_people()
145 {
146         return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."salesman");
147 }
148
149 function check_db_has_sales_people($msg)
150 {
151         global $path_to_root;
152     if (!db_has_sales_people()) 
153     {
154         display_error($msg, true);
155         end_page();
156         exit;   
157     }   
158 }
159
160 function db_has_sales_areas()
161 {
162         return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."areas");
163 }
164
165 function check_db_has_sales_areas($msg)
166 {
167         global $path_to_root;
168     if (!db_has_sales_areas()) 
169     {
170         display_error($msg, true);
171         end_page();
172         exit;   
173     }   
174 }
175
176 function db_has_shippers()
177 {
178         return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."shippers");
179 }
180
181 function check_db_has_shippers($msg)
182 {
183         global $path_to_root;
184     if (!db_has_shippers()) 
185     {
186         display_error($msg, true);
187         end_page();
188         exit;   
189     }   
190 }
191
192 function db_has_open_workorders()
193 {
194         return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."workorders WHERE closed=0");
195 }
196
197 function db_has_workorders()
198 {
199         return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."workorders");
200 }
201
202 function check_db_has_workorders($msg)
203 {
204         global $path_to_root;
205     if (!db_has_workorders()) 
206     {
207         display_error($msg, true);
208         end_page();
209         exit;   
210     }   
211 }
212
213 function db_has_open_dimensions()
214 {
215         return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."dimensions WHERE closed=0");
216 }
217
218 function db_has_dimensions()
219 {
220         return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."dimensions");
221 }
222
223 function check_db_has_dimensions($msg)
224 {
225         global $path_to_root;
226     if (!db_has_dimensions()) 
227     {
228         display_error($msg, true);
229         end_page();
230         exit;   
231     }   
232 }
233
234
235 function db_has_suppliers()
236 {
237         return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."suppliers");
238 }
239
240 function check_db_has_suppliers($msg)
241 {
242         global $path_to_root;
243     if (!db_has_suppliers()) 
244     {
245         display_error($msg, true);
246         end_page();
247         exit;   
248     }   
249 }
250
251 function db_has_stock_items()
252 {
253         return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."stock_master");
254 }
255
256 function check_db_has_stock_items($msg)
257 {
258         global $path_to_root;
259     if (!db_has_stock_items()) 
260     {
261         display_error($msg, true);
262         end_page();
263         exit;   
264     }   
265 }
266
267 function db_has_bom_stock_items()
268 {
269         return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."stock_master WHERE mb_flag='M'");
270 }
271
272 function check_db_has_bom_stock_items($msg)
273 {
274         global $path_to_root;
275     if (!db_has_bom_stock_items()) 
276     {
277         display_error($msg, true);
278         end_page();
279         exit;   
280     }   
281 }
282
283 function db_has_manufacturable_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_manufacturable_items($msg)
289 {
290         global $path_to_root;
291     if (!db_has_manufacturable_items()) 
292     {
293         display_error($msg, true);
294         end_page();
295         exit;   
296     }   
297 }
298
299 function db_has_purchasable_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_purchasable_items($msg)
305 {
306         global $path_to_root;
307     if (!db_has_purchasable_items()) 
308     {
309         display_error($msg, true);
310         end_page();
311         exit;   
312     }   
313 }
314
315 function db_has_costable_items()
316 {
317         return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."stock_master WHERE mb_flag!='D'");
318 }
319
320 function check_db_has_costable_items($msg)
321 {
322         global $path_to_root;
323     if (!db_has_costable_items()) 
324     {
325         display_error($msg, true);
326         end_page();
327         exit;   
328     }   
329 }
330
331 function db_has_stock_categories()
332 {
333         return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."stock_category");
334 }
335
336 function check_db_has_stock_categories($msg)
337 {
338         global $path_to_root;
339     if (!db_has_stock_categories()) 
340     {
341         display_error($msg, true);
342         end_page();
343         exit;   
344     }   
345 }
346
347 function db_has_workcentres()
348 {
349         return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."workcentres");
350 }
351
352 function check_db_has_workcentres($msg)
353 {
354         global $path_to_root;
355     if (!db_has_workcentres()) 
356     {
357         display_error($msg, true);
358         end_page();
359         exit;   
360     }   
361 }
362
363 function db_has_locations()
364 {
365         return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."locations");
366 }
367
368 function check_db_has_locations($msg)
369 {
370         global $path_to_root;
371     if (!db_has_locations()) 
372     {
373         display_error($msg, true);
374         end_page();
375         exit;   
376     }   
377 }
378
379 function db_has_bank_accounts()
380 {
381         return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."bank_accounts");
382 }
383
384 function check_db_has_bank_accounts($msg)
385 {
386         global $path_to_root;
387
388     if (!db_has_bank_accounts()) 
389     {
390         display_error($msg, true);
391         end_page();
392         exit;   
393     }   
394 }
395
396 function db_has_cash_accounts()
397 {
398         return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."bank_accounts
399                 WHERE account_type=3");
400 }
401
402 function db_has_gl_accounts()
403 {
404         return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."chart_master");
405 }
406
407 function db_has_gl_account_groups()
408 {
409         return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."chart_types");
410 }
411
412 function check_db_has_gl_account_groups($msg)
413 {
414         global $path_to_root;
415     if (!db_has_gl_account_groups()) 
416     {
417         display_error($msg, true);
418         end_page();
419         exit;   
420     }   
421 }
422
423 function db_has_quick_entries()
424 {
425         return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."quick_entries");
426 }
427
428 function db_has_tags($type)
429 {
430         return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."tags WHERE type=".db_escape($type));
431 }
432
433 function check_db_has_tags($type, $msg)
434 {
435         global $path_to_root;
436     if (!db_has_tags($type)) 
437     {
438         display_error($msg, true);
439         end_page();
440         exit;   
441     }   
442 }
443
444 function check_empty_result($sql)
445 {
446         $result = db_query($sql, "could not do check empty query");     
447         
448         $myrow = db_fetch_row($result);
449         return $myrow[0] > 0;
450 }
451 //
452 //      Integer input check 
453 //      Return 1 if number has proper form and is within <min, max> range
454 //
455 function check_int($postname, $min=null, $max=null) {
456         if(!isset($_POST[$postname]))
457           return 0;
458     $num = input_num($postname);
459     if(!is_int($num)) 
460           return 0;
461     if (isset($min) && ($num<$min)) 
462           return 0;
463     if (isset($max) && ($num>$max)) 
464           return 0;
465     return 1;
466 }
467 //
468 //      Numeric input check.
469 //      Return 1 if number has proper form and is within <min, max> range
470 //      Empty/not defined fields are defaulted to $dflt value.
471 //
472 function check_num($postname, $min=null, $max=null, $dflt=0) {
473         if(!isset($_POST[$postname]))
474           return 0;
475     $num = input_num($postname, $dflt);
476     if ($num === false || $num === null) 
477           return 0;
478     if (isset($min) && ($num<$min)) 
479           return 0;
480     if (isset($max) && ($num>$max)) 
481           return 0;
482     return 1;
483 }
484
485 function check_is_closed($type, $type_no, $msg=null)
486 {
487         global $systypes_array;
488
489     if (($type_no > 0) && is_closed_trans($type, $type_no))
490     {
491         if (!$msg)
492                 $msg = sprintf(_("%s #%s is closed for further edition."), $systypes_array[$type], $type_no);
493                 display_error($msg, true);
494                 display_footer_exit();
495         }
496 }
497
498 function check_deferred_income_act($msg)
499 {
500         global $path_to_root;
501
502     if (!get_company_pref('deferred_income_act')) 
503     {
504         display_error($msg, true);
505                 display_footer_exit();
506     }
507 }
508
509 function check_is_editable($trans_type, $trans_no, $msg=null)
510 {
511         if (!$_SESSION['wa_current_user']->can_access('SA_EDITOTHERSTRANS'))
512         {
513                 $audit = get_audit_trail_last($trans_type, $trans_no);
514
515                 if ($_SESSION['wa_current_user']->user != $audit['user'])
516                 {
517                 if (!$msg)
518                         $msg = '<b>'._("You have no edit access to transactions created by other users.").'</b>';
519                         display_note($msg);
520                         display_footer_exit();
521                 }
522         }
523         if (!in_array($trans_type, array(ST_SALESORDER, ST_SALESQUOTE, ST_PURCHORDER, ST_WORKORDER)))
524                 check_is_closed($trans_type, $trans_no, $msg);
525 }
526
527 function check_reference($reference, $trans_type, $trans_no=0, $context=null, $line=null)
528 {
529         global $Refs;
530
531         if (!$Refs->is_valid($reference, $trans_type, $context, $line))
532         {
533                 display_error(_("The entered reference is invalid.")); return false;
534         }
535         elseif (!$Refs->is_new_reference($reference, $trans_type, $trans_no))
536         {
537                 display_error( _("The entered reference is already in use.")); return false;
538         }
539         return true;
540 }