Moved all SQL statements from PHP files into relevant *_db.inc files.
[fa-stable.git] / inventory / includes / db / items_db.inc
index b0553726158e0319b1be4ba2dc1f6e1072a13ecb..534d8b7130c8de1c2c716eb393001937b94f3dda 100644 (file)
@@ -109,4 +109,51 @@ function get_items()
        return db_query($sql,"items could not be retreived");
 }
 
+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 purchase 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;
+}
 ?>
\ No newline at end of file