Feature 5388: Print Invoices (documents) list gets too long. Fixed by default 180...
[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
254         return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."stock_master WHERE mb_flag!='F'");
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'");
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'");
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'");
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 check_db_has_fixed_asset_classes($msg)
333 {
334         global $path_to_root;
335     if (!db_has_fixed_asset_classes()) 
336     {
337         display_error($msg, true);
338         end_page();
339         exit;
340     }
341 }
342
343 function db_has_fixed_asset_classes()
344 {
345         return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."stock_fa_class");
346 }
347
348 function db_has_depreciable_fixed_assets()
349 {
350         $year = get_current_fiscalyear();
351         $begin = date2sql(add_months(sql2date($year['begin']), -1));
352         $end = date2sql(add_months(sql2date($year['end']), -1));
353
354         return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."stock_master 
355             WHERE mb_flag='F'
356                     AND material_cost > 0
357                 AND stock_id IN ( SELECT stock_id FROM ".TB_PREF."stock_moves WHERE type=".ST_SUPPRECEIVE." AND qty!=0 )
358                     AND stock_id NOT IN ( SELECT stock_id FROM ".TB_PREF."stock_moves WHERE (type=".ST_CUSTDELIVERY." OR type=".ST_INVADJUST.") AND qty!=0 )
359                     AND depreciation_date <= '".$end."'
360                 AND depreciation_date >='".$begin."'");
361 }
362
363 function check_db_has_depreciable_fixed_assets($msg)
364 {
365         global $path_to_root;
366
367         if (!db_has_depreciable_fixed_assets()) 
368         {
369                 display_error($msg, true);
370                 end_page();
371         exit;
372         }
373 }
374
375 function db_has_fixed_assets()
376 {
377         return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."stock_master WHERE mb_flag='F'");
378 }
379
380 function check_db_has_fixed_assets($msg)
381 {
382         global $path_to_root;
383
384         if (!db_has_fixed_assets()) 
385         {
386                 display_error($msg, true);
387                 end_page();
388                 exit;
389         }
390 }
391
392 function db_has_purchasable_fixed_assets()
393 {
394         return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."stock_master 
395         WHERE mb_flag='F'
396                 AND !inactive
397                         AND stock_id NOT IN
398                                 ( SELECT stock_id FROM ".TB_PREF."stock_moves WHERE type=".ST_SUPPRECEIVE." AND qty!=0 )");
399 }
400
401 function check_db_has_purchasable_fixed_assets($msg)
402 {
403         global $path_to_root;
404
405         if (!db_has_purchasable_fixed_assets()) 
406         {
407                 display_error($msg, true);
408                 end_page();
409                 exit;
410     }
411 }
412
413 function db_has_disposable_fixed_assets()
414 {
415         return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."stock_master 
416         WHERE mb_flag='F'
417                     AND !inactive
418                     AND stock_id IN
419                                 ( SELECT stock_id FROM ".TB_PREF."stock_moves WHERE type=".ST_SUPPRECEIVE." AND qty!=0 )
420                     AND stock_id NOT IN
421                                 ( SELECT stock_id FROM ".TB_PREF."stock_moves WHERE (type=".ST_CUSTDELIVERY." OR type=".ST_INVADJUST.") AND qty!=0 )");
422 }
423
424 function check_db_has_disposable_fixed_assets($msg)
425 {
426         global $path_to_root;
427
428         if (!db_has_disposable_fixed_assets()) 
429         {
430                 display_error($msg, true);
431                 end_page();
432                 exit;
433         }
434 }
435
436 function db_has_stock_categories()
437 {
438         return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."stock_category WHERE dflt_mb_flag!='F'");
439 }
440
441 function check_db_has_fixed_asset_categories($msg)
442 {
443         global $path_to_root;
444     if (!db_has_fixed_asset_categories()) 
445     {
446         display_error($msg, true);
447         end_page();
448         exit;
449     }
450 }
451
452 function db_has_fixed_asset_categories()
453 {
454         return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."stock_category WHERE dflt_mb_flag='F'");
455 }
456
457 function check_db_has_stock_categories($msg)
458 {
459         global $path_to_root;
460     if (!db_has_stock_categories()) 
461     {
462         display_error($msg, true);
463         end_page();
464         exit;
465     }
466 }
467
468 function db_has_workcentres()
469 {
470         return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."workcentres");
471 }
472
473 function check_db_has_workcentres($msg)
474 {
475         global $path_to_root;
476     if (!db_has_workcentres()) 
477     {
478         display_error($msg, true);
479         end_page();
480         exit;   
481     }   
482 }
483
484 function db_has_locations()
485 {
486         return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."locations WHERE fixed_asset=0");
487 }
488
489 function check_db_has_locations($msg)
490 {
491         global $path_to_root;
492     if (!db_has_locations()) 
493     {
494         display_error($msg, true);
495         end_page();
496         exit;   
497     }   
498 }
499
500 function db_has_bank_accounts()
501 {
502         return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."bank_accounts");
503 }
504
505 function check_db_has_bank_accounts($msg)
506 {
507         global $path_to_root;
508
509     if (!db_has_bank_accounts()) 
510     {
511         display_error($msg, true);
512         end_page();
513         exit;   
514     }   
515 }
516
517 function db_has_cash_accounts()
518 {
519         return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."bank_accounts
520                 WHERE account_type=3");
521 }
522
523 function db_has_gl_accounts()
524 {
525         return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."chart_master");
526 }
527
528 function db_has_gl_account_groups()
529 {
530         return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."chart_types");
531 }
532
533 function check_db_has_gl_account_groups($msg)
534 {
535         global $path_to_root;
536     if (!db_has_gl_account_groups()) 
537     {
538         display_error($msg, true);
539         end_page();
540         exit;   
541     }   
542 }
543
544 function db_has_quick_entries()
545 {
546         return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."quick_entries");
547 }
548
549 function db_has_tags($type)
550 {
551         return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."tags WHERE type=".db_escape($type));
552 }
553
554 function check_db_has_tags($type, $msg)
555 {
556         global $path_to_root;
557     if (!db_has_tags($type)) 
558     {
559         display_error($msg, true);
560         end_page();
561         exit;   
562     }   
563 }
564
565 function check_empty_result($sql)
566 {
567         $result = db_query($sql, "could not do check empty query");     
568         
569         $myrow = db_fetch_row($result);
570         return $myrow[0] > 0;
571 }
572 //
573 //      Integer input check 
574 //      Return 1 if number has proper form and is within <min, max> range
575 //
576 function check_int($postname, $min=null, $max=null) {
577         if(!isset($_POST[$postname]))
578           return 0;
579     $num = input_num($postname);
580     if(!is_int($num)) 
581           return 0;
582     if (isset($min) && ($num<$min)) 
583           return 0;
584     if (isset($max) && ($num>$max)) 
585           return 0;
586     return 1;
587 }
588 //
589 //      Numeric input check.
590 //      Return 1 if number has proper form and is within <min, max> range
591 //      Empty/not defined fields are defaulted to $dflt value.
592 //
593 function check_num($postname, $min=null, $max=null, $dflt=0) {
594         if(!isset($_POST[$postname]))
595           return 0;
596     $num = input_num($postname, $dflt);
597     if ($num === false || $num === null) 
598           return 0;
599     if (isset($min) && ($num<$min)) 
600           return 0;
601     if (isset($max) && ($num>$max)) 
602           return 0;
603     return 1;
604 }
605
606 function check_is_closed($type, $type_no, $msg=null)
607 {
608         global $systypes_array;
609
610     if (($type_no > 0) && is_closed_trans($type, $type_no))
611     {
612         if (!$msg)
613                 $msg = sprintf(_("%s #%s is closed for further edition."), $systypes_array[$type], $type_no);
614                 display_error($msg, true);
615                 display_footer_exit();
616         }
617 }
618
619 function check_db_has_template_orders($msg)
620 {
621         $sql  = "SELECT sorder.order_no 
622                 FROM ".TB_PREF."sales_orders as sorder,"
623                         .TB_PREF."sales_order_details as line
624                 WHERE sorder.order_no = line.order_no AND sorder.type = 1
625                 GROUP BY line.order_no";
626
627     if (!check_empty_result($sql))
628     {
629         display_error($msg, true);
630         end_page();
631         exit;
632     }
633 }
634
635 function check_deferred_income_act($msg)
636 {
637         global $path_to_root;
638
639     if (!get_company_pref('deferred_income_act')) 
640     {
641         display_error($msg, true);
642                 display_footer_exit();
643     }
644 }
645
646 function check_is_editable($trans_type, $trans_no, $msg=null)
647 {
648         if (!$_SESSION['wa_current_user']->can_access('SA_EDITOTHERSTRANS'))
649         {
650                 $audit = get_audit_trail_last($trans_type, $trans_no);
651
652                 if ($_SESSION['wa_current_user']->user != $audit['user'])
653                 {
654                 if (!$msg)
655                         $msg = '<b>'._("You have no edit access to transactions created by other users.").'</b>';
656                         display_note($msg);
657                         display_footer_exit();
658                 }
659         }
660         if (!in_array($trans_type, array(ST_SALESORDER, ST_SALESQUOTE, ST_PURCHORDER, ST_WORKORDER)))
661                 check_is_closed($trans_type, $trans_no, $msg);
662 }
663
664 function check_reference($reference, $trans_type, $trans_no=0, $context=null, $line=null)
665 {
666         global $Refs;
667
668         if (!$Refs->is_valid($reference, $trans_type, $context, $line))
669         {
670                 display_error(_("The entered reference is invalid.")); return false;
671         }
672         elseif (!$Refs->is_new_reference($reference, $trans_type, $trans_no))
673         {
674                 display_error( _("The entered reference is already in use.")); return false;
675         }
676         return true;
677 }
678
679 function check_sys_pref($name, $msg, $empty = '')
680 {
681         global $path_to_root;
682
683         if (get_company_pref($name) === $empty)
684         {
685                 display_error(menu_link("/admin/gl_setup.php", $msg), true);
686                 display_footer_exit();
687         }
688 }