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_has_movement_types()
123 return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."movement_types");
126 function check_db_has_movement_types($msg)
128 global $path_to_root;
129 if (!db_has_movement_types())
131 display_error($msg, true);
137 function db_customer_has_branches($customer_id)
139 return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."cust_branch "
140 ."WHERE debtor_no=".db_escape($customer_id));
143 function db_has_customer_branches()
145 return check_empty_result("SELECT COUNT(*) FROM "
146 .TB_PREF."cust_branch WHERE !inactive");
149 function check_db_has_customer_branches($msg)
151 global $path_to_root;
152 if (!db_has_customer_branches())
154 display_error($msg, true);
160 function db_has_sales_people()
162 return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."salesman");
165 function check_db_has_sales_people($msg)
167 global $path_to_root;
168 if (!db_has_sales_people())
170 display_error($msg, true);
176 function db_has_sales_areas()
178 return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."areas");
181 function check_db_has_sales_areas($msg)
183 global $path_to_root;
184 if (!db_has_sales_areas())
186 display_error($msg, true);
192 function db_has_shippers()
194 return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."shippers");
197 function check_db_has_shippers($msg)
199 global $path_to_root;
200 if (!db_has_shippers())
202 display_error($msg, true);
208 function db_has_open_workorders()
210 return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."workorders WHERE closed=0");
213 function db_has_workorders()
215 return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."workorders");
218 function check_db_has_workorders($msg)
220 global $path_to_root;
221 if (!db_has_workorders())
223 display_error($msg, true);
229 function db_has_open_dimensions()
231 return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."dimensions WHERE closed=0");
234 function db_has_dimensions()
236 return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."dimensions");
239 function check_db_has_dimensions($msg)
241 global $path_to_root;
242 if (!db_has_dimensions())
244 display_error($msg, true);
251 function db_has_suppliers()
253 return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."suppliers");
256 function check_db_has_suppliers($msg)
258 global $path_to_root;
259 if (!db_has_suppliers())
261 display_error($msg, true);
267 function db_has_stock_items()
269 return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."stock_master");
272 function check_db_has_stock_items($msg)
274 global $path_to_root;
275 if (!db_has_stock_items())
277 display_error($msg, true);
283 function db_has_bom_stock_items()
285 return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."stock_master WHERE mb_flag='M'");
288 function check_db_has_bom_stock_items($msg)
290 global $path_to_root;
291 if (!db_has_bom_stock_items())
293 display_error($msg, true);
299 function db_has_manufacturable_items()
301 return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."stock_master WHERE (mb_flag='M')");
304 function check_db_has_manufacturable_items($msg)
306 global $path_to_root;
307 if (!db_has_manufacturable_items())
309 display_error($msg, true);
315 function db_has_purchasable_items()
317 return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."stock_master WHERE mb_flag!='M'");
320 function check_db_has_purchasable_items($msg)
322 global $path_to_root;
323 if (!db_has_purchasable_items())
325 display_error($msg, true);
331 function db_has_costable_items()
333 return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."stock_master WHERE mb_flag!='D'");
336 function check_db_has_costable_items($msg)
338 global $path_to_root;
339 if (!db_has_costable_items())
341 display_error($msg, true);
347 function db_has_stock_categories()
349 return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."stock_category");
352 function check_db_has_stock_categories($msg)
354 global $path_to_root;
355 if (!db_has_stock_categories())
357 display_error($msg, true);
363 function db_has_workcentres()
365 return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."workcentres");
368 function check_db_has_workcentres($msg)
370 global $path_to_root;
371 if (!db_has_workcentres())
373 display_error($msg, true);
379 function db_has_locations()
381 return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."locations");
384 function check_db_has_locations($msg)
386 global $path_to_root;
387 if (!db_has_locations())
389 display_error($msg, true);
395 function db_has_bank_accounts()
397 return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."bank_accounts");
400 function check_db_has_bank_accounts($msg)
402 global $path_to_root;
404 if (!db_has_bank_accounts())
406 display_error($msg, true);
412 function db_has_cash_accounts()
414 return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."bank_accounts
415 WHERE account_type=3");
418 function db_has_gl_accounts()
420 return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."chart_master");
423 function db_has_gl_account_groups()
425 return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."chart_types");
428 function check_db_has_gl_account_groups($msg)
430 global $path_to_root;
431 if (!db_has_gl_account_groups())
433 display_error($msg, true);
439 function db_has_quick_entries()
441 return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."quick_entries");
444 function db_has_tags($type)
446 return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."tags WHERE type=".db_escape($type));
449 function check_db_has_tags($type, $msg)
451 global $path_to_root;
452 if (!db_has_tags($type))
454 display_error($msg, true);
460 function check_empty_result($sql)
462 $result = db_query($sql, "could not do check empty query");
464 $myrow = db_fetch_row($result);
465 return $myrow[0] > 0;
468 // Integer input check
469 // Return 1 if number has proper form and is within <min, max> range
471 function check_int($postname, $min=null, $max=null) {
472 if(!isset($_POST[$postname]))
474 $num = input_num($postname);
477 if (isset($min) && ($num<$min))
479 if (isset($max) && ($num>$max))
484 // Numeric input check.
485 // Return 1 if number has proper form and is within <min, max> range
486 // Empty/not defined fields are defaulted to $dflt value.
488 function check_num($postname, $min=null, $max=null, $dflt=0) {
489 if(!isset($_POST[$postname]))
491 $num = input_num($postname, $dflt);
492 if ($num === false || $num === null)
494 if (isset($min) && ($num<$min))
496 if (isset($max) && ($num>$max))