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()
253 return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."stock_master");
256 function check_db_has_stock_items($msg)
258 global $path_to_root;
259 if (!db_has_stock_items())
261 display_error($msg, true);
267 function db_has_bom_stock_items()
269 return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."stock_master WHERE mb_flag='M'");
272 function check_db_has_bom_stock_items($msg)
274 global $path_to_root;
275 if (!db_has_bom_stock_items())
277 display_error($msg, true);
283 function db_has_manufacturable_items()
285 return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."stock_master WHERE (mb_flag='M')");
288 function check_db_has_manufacturable_items($msg)
290 global $path_to_root;
291 if (!db_has_manufacturable_items())
293 display_error($msg, true);
299 function db_has_purchasable_items()
301 return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."stock_master WHERE mb_flag!='M'");
304 function check_db_has_purchasable_items($msg)
306 global $path_to_root;
307 if (!db_has_purchasable_items())
309 display_error($msg, true);
315 function db_has_costable_items()
317 return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."stock_master WHERE mb_flag!='D'");
320 function check_db_has_costable_items($msg)
322 global $path_to_root;
323 if (!db_has_costable_items())
325 display_error($msg, true);
331 function db_has_stock_categories()
333 return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."stock_category");
336 function check_db_has_stock_categories($msg)
338 global $path_to_root;
339 if (!db_has_stock_categories())
341 display_error($msg, true);
347 function db_has_workcentres()
349 return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."workcentres");
352 function check_db_has_workcentres($msg)
354 global $path_to_root;
355 if (!db_has_workcentres())
357 display_error($msg, true);
363 function db_has_locations()
365 return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."locations");
368 function check_db_has_locations($msg)
370 global $path_to_root;
371 if (!db_has_locations())
373 display_error($msg, true);
379 function db_has_bank_accounts()
381 return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."bank_accounts");
384 function check_db_has_bank_accounts($msg)
386 global $path_to_root;
388 if (!db_has_bank_accounts())
390 display_error($msg, true);
396 function db_has_cash_accounts()
398 return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."bank_accounts
399 WHERE account_type=3");
402 function db_has_gl_accounts()
404 return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."chart_master");
407 function db_has_gl_account_groups()
409 return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."chart_types");
412 function check_db_has_gl_account_groups($msg)
414 global $path_to_root;
415 if (!db_has_gl_account_groups())
417 display_error($msg, true);
423 function db_has_quick_entries()
425 return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."quick_entries");
428 function db_has_tags($type)
430 return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."tags WHERE type=".db_escape($type));
433 function check_db_has_tags($type, $msg)
435 global $path_to_root;
436 if (!db_has_tags($type))
438 display_error($msg, true);
444 function check_empty_result($sql)
446 $result = db_query($sql, "could not do check empty query");
448 $myrow = db_fetch_row($result);
449 return $myrow[0] > 0;
452 // Integer input check
453 // Return 1 if number has proper form and is within <min, max> range
455 function check_int($postname, $min=null, $max=null) {
456 if(!isset($_POST[$postname]))
458 $num = input_num($postname);
461 if (isset($min) && ($num<$min))
463 if (isset($max) && ($num>$max))
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.
472 function check_num($postname, $min=null, $max=null, $dflt=0) {
473 if(!isset($_POST[$postname]))
475 $num = input_num($postname, $dflt);
476 if ($num === false || $num === null)
478 if (isset($min) && ($num<$min))
480 if (isset($max) && ($num>$max))
485 function check_is_closed($type, $type_no, $msg=null)
487 global $systypes_array;
489 if (($type_no > 0) && is_closed_trans($type, $type_no))
492 $msg = sprintf(_("%s #%s is closed for further edition."), $systypes_array[$type], $type_no);
493 display_error($msg, true);
494 display_footer_exit();
498 function check_db_has_template_orders($msg)
500 $sql = "SELECT sorder.order_no
501 FROM ".TB_PREF."sales_orders as sorder,"
502 .TB_PREF."sales_order_details as line
503 WHERE sorder.order_no = line.order_no AND sorder.type = 1
504 GROUP BY line.order_no";
506 if (!check_empty_result($sql))
508 display_error($msg, true);
514 function check_deferred_income_act($msg)
516 global $path_to_root;
518 if (!get_company_pref('deferred_income_act'))
520 display_error($msg, true);
521 display_footer_exit();
525 function check_is_editable($trans_type, $trans_no, $msg=null)
527 if (!$_SESSION['wa_current_user']->can_access('SA_EDITOTHERSTRANS'))
529 $audit = get_audit_trail_last($trans_type, $trans_no);
531 if ($_SESSION['wa_current_user']->user != $audit['user'])
534 $msg = '<b>'._("You have no edit access to transactions created by other users.").'</b>';
536 display_footer_exit();
539 if (!in_array($trans_type, array(ST_SALESORDER, ST_SALESQUOTE, ST_PURCHORDER, ST_WORKORDER)))
540 check_is_closed($trans_type, $trans_no, $msg);
543 function check_reference($reference, $trans_type, $trans_no=0, $context=null, $line=null)
547 if (!$Refs->is_valid($reference, $trans_type, $context, $line))
549 display_error(_("The entered reference is invalid.")); return false;
551 elseif (!$Refs->is_new_reference($reference, $trans_type, $trans_no))
553 display_error( _("The entered reference is already in use.")); return false;