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