+function item_in_foreign_codes($stock_id)
+{
+ $sqls= array(
+ "SELECT COUNT(*) FROM "
+ .TB_PREF."stock_moves WHERE stock_id=".db_escape($stock_id) =>
+ _('Cannot delete this item because there are stock movements that refer to this item.'),
+ "SELECT COUNT(*) FROM "
+ .TB_PREF."bom WHERE component=".db_escape($stock_id)=>
+ _('Cannot delete this item record because there are bills of material that require this part as a component.'),
+ "SELECT COUNT(*) FROM "
+ .TB_PREF."sales_order_details WHERE stk_code=".db_escape($stock_id) =>
+ _('Cannot delete this item because there are existing sales order items for it.'),
+ "SELECT COUNT(*) FROM "
+ .TB_PREF."purch_order_details WHERE item_code=".db_escape($stock_id)=>
+ _('Cannot delete this item because there are existing purchase order items for it.')
+ );
+
+ $msg = '';
+
+ foreach($sqls as $sql=>$err) {
+ $result = db_query($sql, "could not query stock usage");
+ $myrow = db_fetch_row($result);
+ if ($myrow[0] > 0)
+ {
+ $msg = $err; break;
+ }
+ }
+ if ($msg == '') {
+
+ $kits = get_where_used($stock_id);
+ $num_kits = db_num_rows($kits);
+ if ($num_kits) {
+ $msg = _("This item cannot be deleted because some code aliases
+ or foreign codes was entered for it, or there are kits defined
+ using this item as component")
+ .':<br>';
+
+ while($num_kits--) {
+ $kit = db_fetch($kits);
+ $msg .= "'".$kit[0]."'";
+ if ($num_kits) $msg .= ',';
+ }
+
+ }
+ }
+ return $msg;
+}
+
+function get_items_search($description, $type)
+{
+ global $SysPrefs;
+
+ $sql = "SELECT COUNT(i.item_code) AS kit, i.item_code, i.description, c.description category
+ FROM ".TB_PREF."stock_master s, ".TB_PREF."item_codes i
+ LEFT JOIN ".TB_PREF."stock_category c ON i.category_id=c.category_id
+ WHERE i.stock_id=s.stock_id
+ AND !i.inactive AND !s.inactive
+ AND ( i.item_code LIKE " . db_escape("%" . $description. "%") . " OR
+ i.description LIKE " . db_escape("%" . get_post("description"). "%") . " OR
+ c.description LIKE " . db_escape("%" . get_post("description"). "%") . ") ";
+
+ switch ($type) {
+ case "sales":
+ $sql .= " AND !s.no_sale AND mb_flag <> 'F'";
+ break;
+ case "manufactured":
+ $sql .= " AND mb_flag = 'M'";
+ break;
+ case "purchasable":
+ $sql .= " AND NOT no_purchase AND mb_flag <> 'F' AND i.item_code=i.stock_id";
+ break;
+ case "costable":
+ $sql .= " AND mb_flag <> 'D' AND mb_flag <> 'F' AND i.item_code=i.stock_id";
+ break;
+ case "component":
+ $parent = $_GET['parent'];
+ $sql .= " AND i.item_code=i.stock_id AND i.stock_id <> '$parent' AND mb_flag <> 'F' ";
+ break;
+ case "kits":
+ $sql .= " AND !i.is_foreign AND i.item_code!=i.stock_id AND mb_flag <> 'F'";
+ break;
+ case "all":
+ $sql .= " AND mb_flag <> 'F' AND i.item_code=i.stock_id";
+ break;
+ }
+
+ if (isset($SysPrefs->max_rows_in_search))
+ $limit = $SysPrefs->max_rows_in_search;
+ else
+ $limit = 10;
+
+ $sql .= " GROUP BY i.item_code ORDER BY i.description LIMIT 0,".(int)($limit);
+
+ return db_query($sql, "Failed in retreiving item list.");
+}
+