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()
14 return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."debtors_master");
17 function check_db_has_customers($msg)
20 if (!db_has_customers())
22 display_error($msg, true);
28 function db_has_currencies()
30 return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."currencies");
33 function check_db_has_currencies($msg)
36 if (!db_has_currencies())
38 display_error($msg, true);
44 function db_has_currency_rates($currency, $date_, $msg=false)
46 $date = date2sql($date_);
48 if (is_company_currency($currency))
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);
57 function db_has_sales_types()
59 return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."sales_types");
62 function check_db_has_sales_types($msg)
65 if (!db_has_sales_types())
67 display_error($msg, true);
73 function db_has_item_tax_types()
75 return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."item_tax_types");
78 function check_db_has_item_tax_types($msg)
81 if (!db_has_item_tax_types())
83 display_error($msg, true);
89 function db_has_tax_types()
91 return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."tax_types");
94 function check_db_has_tax_types($msg)
97 if (!db_has_tax_types())
99 display_error($msg, true);
105 function db_has_tax_groups()
107 return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."tax_groups");
110 function check_db_has_tax_groups($msg)
112 global $path_to_root;
113 if (!db_has_tax_groups())
115 display_error($msg, true);
121 function db_customer_has_branches($customer_id)
123 return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."cust_branch "
124 ."WHERE debtor_no=".db_escape($customer_id));
127 function db_has_customer_branches()
129 return check_empty_result("SELECT COUNT(*) FROM "
130 .TB_PREF."cust_branch WHERE !inactive");
133 function check_db_has_customer_branches($msg)
135 global $path_to_root;
136 if (!db_has_customer_branches())
138 display_error($msg, true);
144 function db_has_sales_people()
146 return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."salesman");
149 function check_db_has_sales_people($msg)
151 global $path_to_root;
152 if (!db_has_sales_people())
154 display_error($msg, true);
160 function db_has_sales_areas()
162 return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."areas");
165 function check_db_has_sales_areas($msg)
167 global $path_to_root;
168 if (!db_has_sales_areas())
170 display_error($msg, true);
176 function db_has_shippers()
178 return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."shippers");
181 function check_db_has_shippers($msg)
183 global $path_to_root;
184 if (!db_has_shippers())
186 display_error($msg, true);
192 function db_has_open_workorders()
194 return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."workorders WHERE closed=0");
197 function db_has_workorders()
199 return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."workorders");
202 function check_db_has_workorders($msg)
204 global $path_to_root;
205 if (!db_has_workorders())
207 display_error($msg, true);
213 function db_has_open_dimensions()
215 return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."dimensions WHERE closed=0");
218 function db_has_dimensions()
220 return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."dimensions");
223 function check_db_has_dimensions($msg)
225 global $path_to_root;
226 if (!db_has_dimensions())
228 display_error($msg, true);
235 function db_has_suppliers()
237 return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."suppliers");
240 function check_db_has_suppliers($msg)
242 global $path_to_root;
243 if (!db_has_suppliers())
245 display_error($msg, true);
251 function db_has_stock_items()
254 return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."stock_master WHERE mb_flag!='F'");
257 function check_db_has_stock_items($msg)
259 global $path_to_root;
260 if (!db_has_stock_items())
262 display_error($msg, true);
268 function db_has_bom_stock_items()
270 return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."stock_master WHERE mb_flag='M'");
273 function check_db_has_bom_stock_items($msg)
275 global $path_to_root;
276 if (!db_has_bom_stock_items())
278 display_error($msg, true);
284 function db_has_manufacturable_items()
286 return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."stock_master WHERE (mb_flag='M')");
289 function check_db_has_manufacturable_items($msg)
291 global $path_to_root;
292 if (!db_has_manufacturable_items())
294 display_error($msg, true);
300 function db_has_purchasable_items()
302 return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."stock_master WHERE mb_flag!='M'");
305 function check_db_has_purchasable_items($msg)
307 global $path_to_root;
308 if (!db_has_purchasable_items())
310 display_error($msg, true);
316 function db_has_costable_items()
318 return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."stock_master WHERE mb_flag!='D'");
321 function check_db_has_costable_items($msg)
323 global $path_to_root;
324 if (!db_has_costable_items())
326 display_error($msg, true);
332 function check_db_has_fixed_asset_classes($msg)
334 global $path_to_root;
335 if (!db_has_fixed_asset_classes())
337 display_error($msg, true);
343 function db_has_fixed_asset_classes()
345 return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."stock_fa_class");
348 function db_has_depreciable_fixed_assets()
350 $year = get_current_fiscalyear();
351 $begin = date2sql(add_months(sql2date($year['begin']), -1));
352 $end = date2sql(add_months(sql2date($year['end']), -1));
354 return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."stock_master
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."'");
363 function check_db_has_depreciable_fixed_assets($msg)
365 global $path_to_root;
367 if (!db_has_depreciable_fixed_assets())
369 display_error($msg, true);
375 function db_has_fixed_assets()
377 return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."stock_master WHERE mb_flag='F'");
380 function check_db_has_fixed_assets($msg)
382 global $path_to_root;
384 if (!db_has_fixed_assets())
386 display_error($msg, true);
392 function db_has_purchasable_fixed_assets()
394 return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."stock_master
398 ( SELECT stock_id FROM ".TB_PREF."stock_moves WHERE type=".ST_SUPPRECEIVE." AND qty!=0 )");
401 function check_db_has_purchasable_fixed_assets($msg)
403 global $path_to_root;
405 if (!db_has_purchasable_fixed_assets())
407 display_error($msg, true);
413 function db_has_disposable_fixed_assets()
415 return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."stock_master
419 ( SELECT stock_id FROM ".TB_PREF."stock_moves WHERE type=".ST_SUPPRECEIVE." AND qty!=0 )
421 ( SELECT stock_id FROM ".TB_PREF."stock_moves WHERE (type=".ST_CUSTDELIVERY." OR type=".ST_INVADJUST.") AND qty!=0 )");
424 function check_db_has_disposable_fixed_assets($msg)
426 global $path_to_root;
428 if (!db_has_disposable_fixed_assets())
430 display_error($msg, true);
436 function db_has_stock_categories()
438 return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."stock_category WHERE dflt_mb_flag!='F'");
441 function check_db_has_fixed_asset_categories($msg)
443 global $path_to_root;
444 if (!db_has_fixed_asset_categories())
446 display_error($msg, true);
452 function db_has_fixed_asset_categories()
454 return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."stock_category WHERE dflt_mb_flag='F'");
457 function check_db_has_stock_categories($msg)
459 global $path_to_root;
460 if (!db_has_stock_categories())
462 display_error($msg, true);
468 function db_has_workcentres()
470 return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."workcentres");
473 function check_db_has_workcentres($msg)
475 global $path_to_root;
476 if (!db_has_workcentres())
478 display_error($msg, true);
484 function db_has_locations()
486 return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."locations WHERE fixed_asset=0");
489 function check_db_has_locations($msg)
491 global $path_to_root;
492 if (!db_has_locations())
494 display_error($msg, true);
500 function db_has_bank_accounts()
502 return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."bank_accounts");
505 function check_db_has_bank_accounts($msg)
507 global $path_to_root;
509 if (!db_has_bank_accounts())
511 display_error($msg, true);
517 function db_has_cash_accounts()
519 return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."bank_accounts
520 WHERE account_type=3");
523 function db_has_gl_accounts()
525 return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."chart_master");
528 function db_has_gl_account_groups()
530 return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."chart_types");
533 function check_db_has_gl_account_groups($msg)
535 global $path_to_root;
536 if (!db_has_gl_account_groups())
538 display_error($msg, true);
544 function db_has_quick_entries()
546 return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."quick_entries");
549 function db_has_tags($type)
551 return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."tags WHERE type=".db_escape($type));
554 function check_db_has_tags($type, $msg)
556 global $path_to_root;
557 if (!db_has_tags($type))
559 display_error($msg, true);
565 function check_empty_result($sql)
567 $result = db_query($sql, "could not do check empty query");
569 $myrow = db_fetch_row($result);
570 return is_array($myrow) ? $myrow[0] > 0 : false;
573 // Integer input check
574 // Return 1 if number has proper form and is within <min, max> range
576 function check_int($postname, $min=null, $max=null) {
577 if(!isset($_POST[$postname]))
579 $num = input_num($postname);
582 if (isset($min) && ($num<$min))
584 if (isset($max) && ($num>$max))
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.
593 function check_num($postname, $min=null, $max=null, $dflt=0) {
594 if(!isset($_POST[$postname]))
596 $num = input_num($postname, $dflt);
597 if ($num === false || $num === null)
599 if (isset($min) && ($num<$min))
601 if (isset($max) && ($num>$max))
606 function check_is_closed($type, $type_no, $msg=null)
608 global $systypes_array;
610 if (($type_no > 0) && is_closed_trans($type, $type_no))
613 $msg = sprintf(_("%s #%s is closed for further edition."), $systypes_array[$type], $type_no);
614 display_error($msg, true);
615 display_footer_exit();
619 function check_db_has_template_orders($msg)
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";
627 if (!check_empty_result($sql))
629 display_error($msg, true);
635 function check_deferred_income_act($msg)
637 global $path_to_root;
639 if (!get_company_pref('deferred_income_act'))
641 display_error($msg, true);
642 display_footer_exit();
646 function check_is_editable($trans_type, $trans_no, $msg=null)
648 if (!$_SESSION['wa_current_user']->can_access('SA_EDITOTHERSTRANS'))
650 $audit = get_audit_trail_last($trans_type, $trans_no);
652 if ($_SESSION['wa_current_user']->user != $audit['user'])
655 $msg = '<b>'._("You have no edit access to transactions created by other users.").'</b>';
657 display_footer_exit();
660 if (!in_array($trans_type, array(ST_SALESORDER, ST_SALESQUOTE, ST_PURCHORDER, ST_WORKORDER)))
661 check_is_closed($trans_type, $trans_no, $msg);
664 function check_reference($reference, $trans_type, $trans_no=0, $context=null, $line=null)
668 if (!$Refs->is_valid($reference, $trans_type, $context, $line))
670 display_error(_("The entered reference is invalid.")); return false;
672 elseif (!$Refs->is_new_reference($reference, $trans_type, $trans_no))
674 display_error( _("The entered reference is already in use.")); return false;
679 function check_sys_pref($name, $msg, $empty = '')
681 global $path_to_root;
683 if (get_company_pref($name) === $empty)
685 display_error(menu_link("/admin/gl_setup.php", $msg), true);
686 display_footer_exit();