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 $y = date('Y', strtotime($year['end']));
353 return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."stock_master
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'");
362 function check_db_has_depreciable_fixed_assets($msg)
364 global $path_to_root;
366 if (!db_has_depreciable_fixed_assets())
368 display_error($msg, true);
374 function db_has_fixed_assets()
376 return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."stock_master WHERE mb_flag='F'");
379 function check_db_has_fixed_assets($msg)
381 global $path_to_root;
383 if (!db_has_fixed_assets())
385 display_error($msg, true);
391 function db_has_purchasable_fixed_assets()
393 return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."stock_master
397 ( SELECT stock_id FROM ".TB_PREF."stock_moves WHERE type=".ST_SUPPRECEIVE." AND qty!=0 )");
400 function check_db_has_purchasable_fixed_assets($msg)
402 global $path_to_root;
404 if (!db_has_purchasable_fixed_assets())
406 display_error($msg, true);
412 function db_has_disposable_fixed_assets()
414 return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."stock_master
418 ( SELECT stock_id FROM ".TB_PREF."stock_moves WHERE type=".ST_SUPPRECEIVE." AND qty!=0 )
420 ( SELECT stock_id FROM ".TB_PREF."stock_moves WHERE (type=".ST_CUSTDELIVERY." OR type=".ST_INVADJUST.") AND qty!=0 )");
423 function check_db_has_disposable_fixed_assets($msg)
425 global $path_to_root;
427 if (!db_has_disposable_fixed_assets())
429 display_error($msg, true);
435 function db_has_stock_categories()
437 return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."stock_category WHERE dflt_mb_flag!='F'");
440 function check_db_has_fixed_asset_categories($msg)
442 global $path_to_root;
443 if (!db_has_fixed_asset_categories())
445 display_error($msg, true);
451 function db_has_fixed_asset_categories()
453 return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."stock_category WHERE dflt_mb_flag='F'");
456 function check_db_has_stock_categories($msg)
458 global $path_to_root;
459 if (!db_has_stock_categories())
461 display_error($msg, true);
467 function db_has_workcentres()
469 return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."workcentres");
472 function check_db_has_workcentres($msg)
474 global $path_to_root;
475 if (!db_has_workcentres())
477 display_error($msg, true);
483 function db_has_locations()
485 return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."locations WHERE fixed_asset=0");
488 function check_db_has_locations($msg)
490 global $path_to_root;
491 if (!db_has_locations())
493 display_error($msg, true);
499 function db_has_bank_accounts()
501 return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."bank_accounts");
504 function check_db_has_bank_accounts($msg)
506 global $path_to_root;
508 if (!db_has_bank_accounts())
510 display_error($msg, true);
516 function db_has_cash_accounts()
518 return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."bank_accounts
519 WHERE account_type=3");
522 function db_has_gl_accounts()
524 return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."chart_master");
527 function db_has_gl_account_groups()
529 return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."chart_types");
532 function check_db_has_gl_account_groups($msg)
534 global $path_to_root;
535 if (!db_has_gl_account_groups())
537 display_error($msg, true);
543 function db_has_quick_entries()
545 return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."quick_entries");
548 function db_has_tags($type)
550 return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."tags WHERE type=".db_escape($type));
553 function check_db_has_tags($type, $msg)
555 global $path_to_root;
556 if (!db_has_tags($type))
558 display_error($msg, true);
564 function check_empty_result($sql)
566 $result = db_query($sql, "could not do check empty query");
568 $myrow = db_fetch_row($result);
569 return $myrow[0] > 0;
572 // Integer input check
573 // Return 1 if number has proper form and is within <min, max> range
575 function check_int($postname, $min=null, $max=null) {
576 if(!isset($_POST[$postname]))
578 $num = input_num($postname);
581 if (isset($min) && ($num<$min))
583 if (isset($max) && ($num>$max))
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.
592 function check_num($postname, $min=null, $max=null, $dflt=0) {
593 if(!isset($_POST[$postname]))
595 $num = input_num($postname, $dflt);
596 if ($num === false || $num === null)
598 if (isset($min) && ($num<$min))
600 if (isset($max) && ($num>$max))
605 function check_is_closed($type, $type_no, $msg=null)
607 global $systypes_array;
609 if (($type_no > 0) && is_closed_trans($type, $type_no))
612 $msg = sprintf(_("%s #%s is closed for further edition."), $systypes_array[$type], $type_no);
613 display_error($msg, true);
614 display_footer_exit();
618 function check_db_has_template_orders($msg)
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";
626 if (!check_empty_result($sql))
628 display_error($msg, true);
634 function check_deferred_income_act($msg)
636 global $path_to_root;
638 if (!get_company_pref('deferred_income_act'))
640 display_error($msg, true);
641 display_footer_exit();
645 function check_is_editable($trans_type, $trans_no, $msg=null)
647 if (!$_SESSION['wa_current_user']->can_access('SA_EDITOTHERSTRANS'))
649 $audit = get_audit_trail_last($trans_type, $trans_no);
651 if ($_SESSION['wa_current_user']->user != $audit['user'])
654 $msg = '<b>'._("You have no edit access to transactions created by other users.").'</b>';
656 display_footer_exit();
659 if (!in_array($trans_type, array(ST_SALESORDER, ST_SALESQUOTE, ST_PURCHORDER, ST_WORKORDER)))
660 check_is_closed($trans_type, $trans_no, $msg);
663 function check_reference($reference, $trans_type, $trans_no=0, $context=null, $line=null)
667 if (!$Refs->is_valid($reference, $trans_type, $context, $line))
669 display_error(_("The entered reference is invalid.")); return false;
671 elseif (!$Refs->is_new_reference($reference, $trans_type, $trans_no))
673 display_error( _("The entered reference is already in use.")); return false;
678 function check_sys_pref($name, $msg, $empty = '')
680 global $path_to_root;
682 if (get_company_pref($name) === $empty)
684 display_error(menu_link("/admin/gl_setup.php", $msg), true);
685 display_footer_exit();