Moved all SQL statements from PHP files into relevant *_db.inc files.
authorJoe Hunt <joe.hunt.consulting@gmail.com>
Wed, 2 Dec 2009 23:46:11 +0000 (23:46 +0000)
committerJoe Hunt <joe.hunt.consulting@gmail.com>
Wed, 2 Dec 2009 23:46:11 +0000 (23:46 +0000)
Included inventory folder

29 files changed:
CHANGELOG.txt
admin/db/company_db.inc
admin/db/printers_db.inc
admin/db/shipping_db.inc
admin/payment_terms.php
admin/print_profiles.php
admin/printers.php
admin/shipping_companies.php
dimensions/includes/dimensions_db.inc
gl/includes/db/gl_db_account_types.inc
gl/includes/db/gl_db_accounts.inc
gl/includes/db/gl_db_bank_accounts.inc
gl/includes/db/gl_db_currencies.inc
gl/manage/bank_accounts.php
gl/manage/currencies.php
gl/manage/gl_account_classes.php
gl/manage/gl_account_types.php
gl/manage/gl_accounts.php
inventory/includes/db/items_category_db.inc
inventory/includes/db/items_db.inc
inventory/includes/db/items_locations_db.inc
inventory/includes/db/movement_types_db.inc
inventory/includes/inventory_db.inc
inventory/inquiry/stock_movements.php
inventory/manage/item_categories.php
inventory/manage/items.php
inventory/manage/locations.php
inventory/manage/movement_types.php
inventory/purchasing_data.php

index c1e27e33a8ee760364c465275f746c7c58d228b3..96990eb0436f959f93ce54d25a712c5bc2bc990b 100644 (file)
@@ -19,6 +19,40 @@ Legend:
 ! -> Note
 $ -> Affected files
 
+03-Dec-2009 Joe Hunt
+! Moved all SQL statements from PHP files into relevant *_db.inc files.
+  Included inventory folder
+$ /admin/payment_terms.php
+  /admin/printers.php
+  /admin/print_profiles.php
+  /admin/shipping_companies.php
+  /admin/db/company_db.inc
+  /admin/db/printers_db.inc
+  /admin/db/shipping_db.inc
+  /dimensions/includes/dimens_db.inc
+  /gl/gl_bank.php
+  /gl/includes/db/gl_db_accounts.inc
+  /gl/includes/db/gl_db_account_types.inc
+  /gl/includes/db/gl_db_bank_accounts.inc
+  /gl/includes/db/gl_db_curriencies.inc
+  /gl/manage/bank_accounts.php
+  /gl/manage/currencies.php
+  /gl/manage/gl_accounts.php
+  /gl/manage/gl_account_classes.php
+  /gl/manage/gl_account_types.php
+  /inventory/purchasing_data.php
+  /inventory/includes/inventory_db.inc
+  /inventory/includes/db/items_category_db.inc
+  /inventory/includes/db/items_db.inc
+  /inventory/includes/db/items_locations_db.inc
+  /inventory/includes/db/items_purchases_db.inc (New file)
+  /inventory/includes/db/movement_types_db.inc
+  /inventory/inquiry/stock_movements.php
+  /inventory/manage/items_php
+  /inventory/manage/item_categories.php
+  /inventory/manage/locations.php
+  /inventory/manage/movement_types.php
+  
 02-Dec-2009 Janusz Dobrowolski
 ! Merged changed from trunk up to 2.2.1 ---------------------------------------
 
index c95ef8de0ebfd340992c5cb3effc1552a5992110..22fc2d20d8e90a2984e9ec10548ac0d58b8bebfd 100644 (file)
@@ -193,18 +193,12 @@ function get_payment_terms_all($show_inactive)
        return db_query($sql,"could not get payment terms");
 }
 
-function customer_has_terms($selected_id)
+function key_in_foreign_table($id, $table, $key, $escaped=false)
 {
-       $sql= "SELECT COUNT(*) FROM ".TB_PREF."debtors_master WHERE payment_terms = ".db_escape($selected_id);
-       $result = db_query($sql,"check failed");
-       $myrow = db_fetch_row($result);
-       return ($myrow[0] > 0); 
-}
-
-function supplier_has_terms($selected_id)
-{
-       $sql= "SELECT COUNT(*) FROM ".TB_PREF."suppliers WHERE payment_terms = ".db_escape($selected_id);
-       $result = db_query($sql,"check failed");
+       if (!$escaped)
+               $id = db_escape($id);
+       $sql= "SELECT COUNT(*) FROM ".TB_PREF."$table WHERE $key = $id";
+       $result = db_query($sql,"check $table relations failed");
        $myrow = db_fetch_row($result);
        return ($myrow[0] > 0); 
 }
index b873ee2e3e389589c253b1f89c11e78b937a4918..43e13b4c9bbd1c016e241e35d9dc46f346538634 100644 (file)
@@ -106,12 +106,4 @@ function get_print_profile($name)
        return db_query($sql,"could not get printing profile");
 }
 
-function printer_has_profile($id)
-{
-       $sql= "SELECT COUNT(*) FROM ".TB_PREF."print_profiles WHERE printer = ".db_escape($id);
-       $result = db_query($sql,"check printers relations failed");
-       $myrow = db_fetch_row($result);
-       return ($myrow[0] > 0); 
-}
-
 ?>
\ No newline at end of file
index b447426ed63423fa7229a5935f808c39e330979b..7082b5b5482ae29f6770006733b23e76f79b3d03 100644 (file)
@@ -68,26 +68,4 @@ function get_shipper($selected_id)
        return db_fetch($result);
 }
 
-//--------------------------------------------------------------------------------------
-
-function sales_orders_has_shipper($selected_id)
-{
-       $sql= "SELECT COUNT(*) FROM ".TB_PREF."sales_orders WHERE ship_via=".db_escape($selected_id);
-       
-       $result = db_query($sql,"check failed");
-       $myrow = db_fetch_row($result);
-       return ($myrow[0] > 0); 
-}
-
-//--------------------------------------------------------------------------------------
-
-function debtor_trans_has_shipper($selected_id)
-{
-       $sql= "SELECT COUNT(*) FROM ".TB_PREF."debtor_trans WHERE ship_via=".db_escape($selected_id);
-       
-       $result = db_query($sql,"check failed");
-       $myrow = db_fetch_row($result);
-       return ($myrow[0] > 0); 
-}
-
 ?>
\ No newline at end of file
index c6a98794e792737d799370657f686972ef7cfe4d..a6aa77e77011570f350c40aa6a67594f3bb954a6 100644 (file)
@@ -74,13 +74,13 @@ if ($Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM')
 if ($Mode == 'Delete')
 {
        // PREVENT DELETES IF DEPENDENT RECORDS IN debtors_master
-       if (customer_has_terms($selected_id))
+       if (key_in_foreign_table($selected_id, 'debtors_master', 'payment_terms'))
        {
                display_error(_("Cannot delete this payment term, because customer accounts have been created referring to this term."));
        } 
        else 
        {
-               if (supplier_has_terms($selected_id))
+               if (key_in_foreign_table($selected_id, 'suppliers', 'payment_terms'))
                {
                        display_error(_("Cannot delete this payment term, because supplier accounts have been created referring to this term"));
                } 
index 5b387534800efcf551e00e0e15f6d34872f7ba5a..59f9ec8085ff1c0b10723eb8e545aac54c78bcef 100644 (file)
@@ -77,9 +77,7 @@ function check_delete($name)
 {
 // check if selected profile is used by any user
        if ($name=='') return 0; // cannot delete system default profile
-       $sql = "SELECT * FROM ".TB_PREF."users WHERE print_profile=".db_escape($name);
-       $res = db_query($sql,'cannot check printing profile usage');
-       return db_num_rows($res);
+       return key_in_foreign_table($name, 'users', 'print_profile');
 }
 //-------------------------------------------------------------------------------------------
 if ( get_post('submit'))
index cd44a8e13f7ce4d1951d5c18a1c9f7806db86484..3334d6b76c6d187c62e4e8e34d3263008e7574b7 100644 (file)
@@ -59,7 +59,7 @@ if ($Mode == 'Delete')
 {
        // PREVENT DELETES IF DEPENDENT RECORDS IN print_profiles
 
-       if (printer_has_profile($selected_id))
+       if (key_in_foreign_table($selected_id, 'print_profiles', 'printer'))
        {
                display_error(_("Cannot delete this printer definition, because print profile have been created using it."));
        } 
index 9422d8464b461c395bf1ed42cd66962ac23ce93c..fa7b264108d940c8544ea0b0991a6dfdeb1db031 100644 (file)
@@ -53,7 +53,7 @@ if ($Mode == 'Delete')
 {
 // PREVENT DELETES IF DEPENDENT RECORDS IN 'sales_orders'
 
-       if (sales_orders_has_shipper($selected_id))
+       if (key_in_foreign_table($selected_id, 'sales_orders', 'ship_via'))
        {
                $cancel_delete = 1;
                display_error(_("Cannot delete this shipping company because sales orders have been created using this shipper."));
@@ -61,8 +61,7 @@ if ($Mode == 'Delete')
        else 
        {
                // PREVENT DELETES IF DEPENDENT RECORDS IN 'debtor_trans'
-
-               if (debtor_trans_has_shipper($selected_id))
+               if (key_in_foreign_table($selected_id, 'debtor_trans', 'ship_via'))
                {
                        $cancel_delete = 1;
                        display_error(_("Cannot delete this shipping company because invoices have been created using this shipping company."));
index 3335623dce8bdb2abcaafabf1ae0ca9e2eb4a5fc..54ece7c46dd0837d9412189d3cde1423dabff111 100644 (file)
@@ -122,10 +122,11 @@ function dimension_has_deposits($id)
 
 function dimension_has_payments($id)
 {
-       $sql = "SELECT SUM(amount) FROM ".TB_PREF."gl_trans WHERE dimension_id = ".db_escape($id);
+       $sql = "SELECT COUNT(*) FROM ".TB_PREF."gl_trans WHERE dimension_id = ".db_escape($id)
+        . " OR dimension2_id = ".db_escape($id);
        $res = db_query($sql, "Transactions could not be calculated");
        $row = db_fetch_row($res);
-       return ($row[0] != 0.0);
+       return ($row[0] 0);
 }
 
 function dimension_is_closed($id)
index 4eb22d3e6b5dd36de8a13b0253effbec2ba77080..1c25db71a5a7e8fc140ee655fac052f72c84aae6 100644 (file)
@@ -113,30 +113,4 @@ function delete_account_class($id)
        db_query($sql, "could not delete account type");
 }
 
-function account_class_in_account_types($id)
-{
-       $sql= "SELECT COUNT(*) FROM ".TB_PREF."chart_types
-               WHERE class_id=".db_escape($id);
-       $result = db_query($sql, "could not query chart master");
-       $myrow = db_fetch_row($result);
-       return ($myrow[0] > 0); 
-}
-
-function account_type_in_chart_master($type)
-{
-       $sql= "SELECT COUNT(*) FROM ".TB_PREF."chart_master
-               WHERE account_type=$type";
-       $result = db_query($sql, "could not query chart master");
-       $myrow = db_fetch_row($result);
-       return ($myrow[0] > 0); 
-}
-
-function account_type_in_parent($type)
-{
-       $sql= "SELECT COUNT(*) FROM ".TB_PREF."chart_types
-               WHERE parent=$type";
-       $result = db_query($sql, "could not query chart types");
-       $myrow = db_fetch_row($result);
-       return ($myrow[0] > 0); 
-}
 ?>
\ No newline at end of file
index 3c29cd3ab6efccce205d3932231b1c6acf738052..279a55bee2fb93dd42b71a6b3c831cbd8a3900a2 100644 (file)
@@ -104,15 +104,6 @@ function get_gl_account_name($code)
        display_db_error("could not retreive the account name for $code", $sql, true);
 }
 
-function gl_account_in_transactions($acc)
-{
-       $sql= "SELECT COUNT(*) FROM ".TB_PREF."gl_trans WHERE account=$acc";
-       $result = db_query($sql,"Couldn't test for existing transactions");
-
-       $myrow = db_fetch_row($result);
-       return ($myrow[0] > 0); 
-}
-
 function gl_account_in_company_defaults($acc)
 {
        $sql= "SELECT COUNT(*) FROM ".TB_PREF."company WHERE debtors_act=$acc 
@@ -137,15 +128,6 @@ function gl_account_in_company_defaults($acc)
        return ($myrow[0] > 0); 
 }
 
-function gl_account_in_bank_accounts($acc)
-{
-       $sql= "SELECT COUNT(*) FROM ".TB_PREF."bank_accounts WHERE account_code=$acc";
-       $result = db_query($sql,"Couldn't test for bank accounts");
-
-       $myrow = db_fetch_row($result);
-       return ($myrow[0] > 0); 
-}
-
 function gl_account_in_stock_category($acc)
 {
        $sql= "SELECT COUNT(*) FROM ".TB_PREF."stock_category WHERE 
index 4377ec29774a56c61a0099e19fbd14e555d8a06b..7d79858bef83457817bd8aa0bdde9c29629128f5 100644 (file)
@@ -102,26 +102,6 @@ function get_bank_gl_account($id)
 
 //---------------------------------------------------------------------------------------------
 
-function bank_account_in_transactions($acc)
-{
-       $sql= "SELECT COUNT(*) FROM ".TB_PREF."bank_trans WHERE bank_act=$acc";
-       $result = db_query($sql,"check failed");
-       $myrow = db_fetch_row($result);
-       return ($myrow[0] > 0); 
-}
-
-//---------------------------------------------------------------------------------------------
-
-function bank_account_in_sales_pos($acc)
-{
-       $sql= "SELECT COUNT(*) FROM ".TB_PREF."sales_pos WHERE pos_account=$acc";
-       $result = db_query($sql,"check failed");
-       $myrow = db_fetch_row($result);
-       return ($myrow[0] > 0); 
-}
-
-//---------------------------------------------------------------------------------------------
-
 function add_quick_entry($description, $type, $base_amount, $base_desc)
 {
        $sql = "INSERT INTO ".TB_PREF."quick_entries (description, type, base_amount, base_desc) 
index e13a91443b5a048d1b7e848b0fa59cd028be4397..a4ddf95a4807febd1fabc9dda7f6b667194773d2 100644 (file)
@@ -68,37 +68,4 @@ function get_currencies($all=false)
        return db_query($sql, "could not get currencies");
 }
 
-//---------------------------------------------------------------------------------------------
-
-function currency_in_debtors($curr)
-{
-       $sql= "SELECT COUNT(*) FROM ".TB_PREF."debtors_master WHERE curr_code = $curr";
-       $result = db_query($sql);
-       $myrow = db_fetch_row($result);
-       return ($myrow[0] > 0); 
-}
-
-function currency_in_suppliers($curr)
-{
-       $sql= "SELECT COUNT(*) FROM ".TB_PREF."suppliers WHERE curr_code = $curr";
-       $result = db_query($sql);
-       $myrow = db_fetch_row($result);
-       return ($myrow[0] > 0); 
-}
-
-function currency_in_bank_accounts($curr)
-{
-       $sql= "SELECT COUNT(*) FROM ".TB_PREF."bank_accounts WHERE bank_curr_code = $curr";
-       $result = db_query($sql);
-       $myrow = db_fetch_row($result);
-       return ($myrow[0] > 0); 
-}
-
-function currency_in_company($curr)
-{
-       $sql= "SELECT COUNT(*) FROM ".TB_PREF."company WHERE curr_default = $curr";
-       $result = db_query($sql);
-       $myrow = db_fetch_row($result);
-       return ($myrow[0] > 0); 
-}
 ?>
\ No newline at end of file
index 64a96f8f0f5245c36a141378ec3c7f57ff3a5c81..5ec75616d7df1d181e5c751def602f260454b5af 100644 (file)
@@ -66,13 +66,13 @@ elseif( $Mode == 'Delete')
        $acc = db_escape($selected_id);
        // PREVENT DELETES IF DEPENDENT RECORDS IN 'bank_trans'
 
-       if (bank_account_in_transactions($acc))
+       if (key_in_foreign_table($acc, 'bank_trans', 'bank_act', true))
        {
                $cancel_delete = 1;
                display_error(_("Cannot delete this bank account because transactions have been created using this account."));
        }
 
-       if (bank_account_in_sales_pos($acc))
+       if (key_in_foreign_table($acc, 'sales_pos', 'pos_account', true))
        {
                $cancel_delete = 1;
                display_error(_("Cannot delete this bank account because POS definitions have been created using this account."));
index 99f80bdd2ccab620708c53d4f5f5fc0fad98e35f..856bc2b2f57b377c55ad64861506a0446db51342 100644 (file)
@@ -89,26 +89,26 @@ function check_can_delete()
        $curr = db_escape($selected_id);
 
        // PREVENT DELETES IF DEPENDENT RECORDS IN debtors_master
-       if (currency_in_debtors($curr))
+       if (key_in_foreign_table($curr, 'debtors_master', 'curr_code', true))
        {
                display_error(_("Cannot delete this currency, because customer accounts have been created referring to this currency."));
                return false;
        }
 
-       if (currency_in_suppliers($curr))
+       if (key_in_foreign_table($curr, 'suppliers', 'curr_code', true))
        {
                display_error(_("Cannot delete this currency, because supplier accounts have been created referring to this currency."));
                return false;
        }
-       
-       if (currency_in_company($curr))
+
+       if (key_in_foreign_table($curr, 'company', 'curr_default', true))               
        {
                display_error(_("Cannot delete this currency, because the company preferences uses this currency."));
                return false;
        }
        
        // see if there are any bank accounts that use this currency
-       if (currenty_in_bank_accounts($curr))
+       if (key_in_foreign_table($curr, 'bank_accounts', 'bank_curr_code', true))
        {
                display_error(_("Cannot delete this currency, because thre are bank accounts that use this currency."));
                return false;
index d90a952402b033d33a51f0d19eb68f2289a98aba..9e7e13625a58899dd96cd5f3ad5293f4cbfbcb13 100644 (file)
@@ -71,7 +71,7 @@ function can_delete($selected_id)
 {
        if ($selected_id == -1)
                return false;
-       if (account_class_in_account_types($selected_id))       
+       if (key_in_foreign_table($selected_id, 'chart_types', 'class_id'))      
        {
                display_error(_("Cannot delete this account class because GL account types have been created referring to it."));
                return false;
index cad41fbb41fba6cdcc0acf362eb551f528899a23..8efa766e7e05b2c2b1b45dc1e2f0b22d3e69b459 100644 (file)
@@ -79,13 +79,13 @@ function can_delete($selected_id)
                return false;
        $type = db_escape($selected_id);
 
-       if (account_type_in_chart_master($type))
+       if (key_in_foreign_table($type, 'chart_master', 'account_type', true))
        {
                display_error(_("Cannot delete this account group because GL accounts have been created referring to it."));
                return false;
        }
 
-       if (account_type_in_parent($type))
+       if (key_in_foreign_table($type, 'chart_types', 'parent', true))
        {
                display_error(_("Cannot delete this account group because GL account groups have been created referring to it."));
                return false;
index 5a277c6ae0640d0f020f2317159d3cbee93c2a76..7b1937fdbc1f7a79c3d62d9c98580a13e105cfdf 100644 (file)
@@ -108,7 +108,7 @@ function can_delete($selected_account)
                return false;
        $acc = db_escape($selected_account);
 
-       if (gl_account_in_transactions($acc))
+       if (key_in_foreign_table($acc, 'gl_trans', 'account', true))
        {
                display_error(_("Cannot delete this account because transactions have been created using this account."));
                return false;
@@ -119,8 +119,8 @@ function can_delete($selected_account)
                display_error(_("Cannot delete this account because it is used as one of the company default GL accounts."));
                return false;
        }
-       
-       if (gl_account_in_bank_accounts($acc))
+
+       if (key_in_foreign_table($acc, 'bank_accounts', 'account_code', true))  
        {
                display_error(_("Cannot delete this account because it is used by a bank account."));
                return false;
index 11f9486ddc3b7a2f3ef41bde988a0c1b1c500243..27a4ed6bf600da29132de2c44bf21cdf7a2dc566 100644 (file)
@@ -64,6 +64,15 @@ function delete_item_category($id)
        db_query($sql,"an item category could not be deleted");
 }
 
+function get_item_categories($show_inactive)
+{
+       $sql = "SELECT c.*, t.name as tax_name FROM ".TB_PREF."stock_category c, "
+               .TB_PREF."item_tax_types t WHERE c.dflt_tax_type=t.id";
+       if (!$show_inactive) $sql .= " AND !c.inactive";
+
+       return db_query($sql, "could not get stock categories");
+}
+
 function get_item_category($id)
 {
        $sql="SELECT * FROM ".TB_PREF."stock_category WHERE category_id=".db_escape($id);
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
index b0372971441a3ca1e49d0404fb379770d7b9103f..4a12c671cc73aa60dcf45980fb8f06cb40b68399 100644 (file)
@@ -63,6 +63,15 @@ function get_item_location($item_location)
 
 //------------------------------------------------------------------------------------
 
+function get_item_locations($show_inactive)
+{
+       $sql = "SELECT * FROM ".TB_PREF."locations";
+       if (!$show_inactive) $sql .= " WHERE !inactive";
+       return db_query($sql, "could not query locations");;
+}
+
+//------------------------------------------------------------------------------------
+
 function set_reorder_level($stock_id, $loc_code, $reorder_level)
 {
        $sql = "UPDATE ".TB_PREF."loc_stock SET reorder_level = $reorder_level
@@ -83,6 +92,4 @@ function get_loc_details($stock_id)
        return db_query($sql,"an item reorder could not be retreived");
 }
 
-//------------------------------------------------------------------------------------
-
 ?>
\ No newline at end of file
index 210cad432ea789700b29262da8b8150d7095704d..1862f7b16822139fa944016c2862ea2ec850f3fc 100644 (file)
@@ -49,4 +49,38 @@ function delete_movement_type($type_id)
        db_query($sql, "could not delete item movement type");
 }
 
+function get_stock_movements($stock_id, $StockLocation,        $BeforeDate, $AfterDate)
+{
+       $before_date = date2sql($BeforeDate);
+       $after_date = date2sql($AfterDate);
+       $sql = "SELECT type, trans_no, tran_date, person_id, qty, reference
+               FROM ".TB_PREF."stock_moves
+               WHERE loc_code=".db_escape($StockLocation)."
+               AND tran_date >= '". $after_date . "'
+               AND tran_date <= '" . $before_date . "'
+               AND stock_id = ".db_escape($stock_id) . " ORDER BY tran_date,trans_id";
+       return db_query($sql, "could not query stock moves");
+}
+
+function get_stock_movements_before($stock_id, $StockLocation, $AfterDate)
+{
+       $after_date = date2sql($AfterDate);
+       $sql = "SELECT SUM(qty) FROM ".TB_PREF."stock_moves WHERE stock_id=".db_escape($stock_id) . "
+               AND loc_code=".db_escape( $StockLocation) . "
+               AND tran_date < '" . $after_date . "'";
+       $before_qty = db_query($sql, "The starting quantity on hand could not be calculated");
+
+       $before_qty_row = db_fetch_row($before_qty);
+       return $before_qty_row[0];
+}
+
+function movement_types_in_stock_moves($selected_id)
+{
+       $sql= "SELECT COUNT(*) FROM ".TB_PREF."stock_moves 
+               WHERE type=" . ST_INVADJUST. " AND person_id=".db_escape($selected_id);
+
+       $result = db_query($sql, "could not query stock moves");
+       $myrow = db_fetch_row($result);
+       return ($myrow[0] > 0); 
+}
 ?>
\ No newline at end of file
index d7b20c9d8f72cdfee4cc32763144c272a68ce4de..34f7b3d94de0864d63602607d21ee224ba9adc96 100644 (file)
@@ -16,6 +16,7 @@ include_once($path_to_root . "/includes/manufacturing.inc");
 include_once($path_to_root . "/inventory/includes/db/items_category_db.inc");
 include_once($path_to_root . "/inventory/includes/db/items_trans_db.inc");
 include_once($path_to_root . "/inventory/includes/db/items_prices_db.inc");
+include_once($path_to_root . "/inventory/includes/db/items_purchases_db.inc");
 include_once($path_to_root . "/inventory/includes/db/items_codes_db.inc");
 include_once($path_to_root . "/inventory/includes/db/items_db.inc");
 include_once($path_to_root . "/inventory/includes/db/items_locations_db.inc");
index 61388dd19469babef0f4aa7049254124e7414d53..e8f6a25778af0558c6660fc9003374e7f62e0b0f 100644 (file)
@@ -62,15 +62,8 @@ set_global_stock_item($_POST['stock_id']);
 $before_date = date2sql($_POST['BeforeDate']);
 $after_date = date2sql($_POST['AfterDate']);
 
-$sql = "SELECT type, trans_no, tran_date, person_id, qty, reference
-       FROM ".TB_PREF."stock_moves
-       WHERE loc_code=".db_escape($_POST['StockLocation'])."
-       AND tran_date >= '". $after_date . "'
-       AND tran_date <= '" . $before_date . "'
-       AND stock_id = ".db_escape($_POST['stock_id']) . " ORDER BY tran_date,trans_id";
-$result = db_query($sql, "could not query stock moves");
-
-check_db_error("The stock movements for the selected criteria could not be retrieved",$sql);
+$result = get_stock_movements($_POST['stock_id'], $_POST['StockLocation'],
+       $_POST['BeforeDate'], $_POST['AfterDate']);
 
 div_start('doc_tbl');
 start_table($table_style);
@@ -79,19 +72,16 @@ $th = array(_("Type"), _("#"), _("Reference"), _("Date"), _("Detail"),
 
 table_header($th);
 
-$sql = "SELECT SUM(qty) FROM ".TB_PREF."stock_moves WHERE stock_id=".db_escape($_POST['stock_id']) . "
-       AND loc_code=".db_escape( $_POST['StockLocation']) . "
-       AND tran_date < '" . $after_date . "'";
-$before_qty = db_query($sql, "The starting quantity on hand could not be calculated");
-
-$before_qty_row = db_fetch_row($before_qty);
-$after_qty = $before_qty = $before_qty_row[0];
+$before_qty = get_stock_movements_before($_POST['stock_id'], $_POST['StockLocation'], $_POST['AfterDate']);
+       
+$after_qty = $before_qty;
 
+/*
 if (!isset($before_qty_row[0]))
 {
        $after_qty = $before_qty = 0;
 }
-
+*/
 start_row("class='inquirybg'");
 label_cell("<b>"._("Quantity on hand before") . " " . $_POST['AfterDate']."</b>", "align=center colspan=5");
 label_cell("&nbsp;", "colspan=2");
@@ -147,13 +137,10 @@ while ($myrow = db_fetch($result))
        elseif ($myrow["type"] == ST_SUPPRECEIVE || $myrow['type'] == ST_SUPPCREDIT)
        {
                // get the supplier name
-               $sql = "SELECT supp_name FROM ".TB_PREF."suppliers WHERE supplier_id = '" . $myrow["person_id"] . "'";
-               $supp_result = db_query($sql,"check failed");
-
-               $supp_row = db_fetch($supp_result);
+               $supp_name = get_supplier_name($myrow["person_id"]);
 
-               if (strlen($supp_row['supp_name']) > 0)
-                       $person = $supp_row['supp_name'];
+               if (strlen($supp_name) > 0)
+                       $person = $supp_name;
        }
        elseif ($myrow["type"] == ST_LOCTRANSFER || $myrow["type"] == ST_INVADJUST)
        {
index a12f56ff0da23e06975e8a06d77615c27390831e..d8dbb69ec32b9c7cc8db82f4db11216d2a1aed72 100644 (file)
@@ -67,10 +67,7 @@ if ($Mode == 'Delete')
 {
 
        // PREVENT DELETES IF DEPENDENT RECORDS IN 'stock_master'
-       $sql= "SELECT COUNT(*) FROM ".TB_PREF."stock_master WHERE category_id=".db_escape($selected_id);
-       $result = db_query($sql, "could not query stock master");
-       $myrow = db_fetch_row($result);
-       if ($myrow[0] > 0) 
+       if (key_in_foreign_table($selected_id, 'stock_master', 'category_id'))
        {
                display_error(_("Cannot delete this item category because items have been created using this item category."));
        } 
@@ -94,11 +91,7 @@ if (list_updated('mb_flag')) {
 }
 //----------------------------------------------------------------------------------
 
-$sql = "SELECT c.*, t.name as tax_name FROM ".TB_PREF."stock_category c, "
-       .TB_PREF."item_tax_types t WHERE c.dflt_tax_type=t.id";
-if (!check_value('show_inactive')) $sql .= " AND !c.inactive";
-
-$result = db_query($sql, "could not get stock categories");
+$result = get_item_categories(check_value('show_inactive'));
 
 start_form();
 start_table("$table_style width=80%");
index ac04293321b4b714c559cc374521a942a9c7ead4..5e72e40592ee777d1e5add913c9b6b5089ea3895 100644 (file)
@@ -210,50 +210,8 @@ if (get_post('clone')) {
 
 function check_usage($stock_id, $dispmsg=true)
 {
-       $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 == '') {       
+       $msg = item_in_foreign_codes($stock_id);
 
-               $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 .= ',';
-                       }
-
-               }
-       }
        if ($msg != '') {
                if($dispmsg) display_error($msg);
                return false;
index 6dae2fa31828ffe9b5307e449521cf041bd59deb..1deb1f2064df5f9db3d34fe516273bfd5fc9a8a8 100644 (file)
@@ -71,69 +71,46 @@ if ($Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM')
 
 function can_delete($selected_id)
 {
-       $sql= "SELECT COUNT(*) FROM ".TB_PREF."stock_moves WHERE loc_code=".db_escape($selected_id);
-       $result = db_query($sql, "could not query stock moves");
-       $myrow = db_fetch_row($result);
-       if ($myrow[0] > 0) 
+       if (key_in_foreign_table($selected_id, 'stock_moves', 'loc_code'))
        {
                display_error(_("Cannot delete this location because item movements have been created using this location."));
                return false;
        }
 
-       $sql= "SELECT COUNT(*) FROM ".TB_PREF."workorders WHERE loc_code=".db_escape($selected_id);
-       $result = db_query($sql, "could not query work orders");
-       $myrow = db_fetch_row($result);
-       if ($myrow[0] > 0) 
+       if (key_in_foreign_table($selected_id, 'workorders', 'loc_code'))
        {
                display_error(_("Cannot delete this location because it is used by some work orders records."));
                return false;
        }
 
-       $sql= "SELECT COUNT(*) FROM ".TB_PREF."cust_branch WHERE default_location='$selected_id'";
-       $result = db_query($sql, "could not query customer branches");
-       $myrow = db_fetch_row($result);
-       if ($myrow[0] > 0) 
+       if (key_in_foreign_table($selected_id, 'cust_branch', 'default_location'))
        {
                display_error(_("Cannot delete this location because it is used by some branch records as the default location to deliver from."));
                return false;
        }
        
-       $sql= "SELECT COUNT(*) FROM ".TB_PREF."bom WHERE loc_code=".db_escape($selected_id);
-       $result = db_query($sql, "could not query bom");
-       $myrow = db_fetch_row($result);
-       if ($myrow[0] > 0) 
+       if (key_in_foreign_table($selected_id, 'bom', 'loc_code'))
        {
                display_error(_("Cannot delete this location because it is used by some related records in other tables."));
                return false;
        }
-       $sql= "SELECT COUNT(*) FROM ".TB_PREF."grn_batch WHERE loc_code=".db_escape($selected_id);
-       $result = db_query($sql, "could not query grn batch");
-       $myrow = db_fetch_row($result);
-       if ($myrow[0] > 0) 
+       
+       if (key_in_foreign_table($selected_id, 'grn_batch', 'loc_code'))
        {
                display_error(_("Cannot delete this location because it is used by some related records in other tables."));
                return false;
        }
-       $sql= "SELECT COUNT(*) FROM ".TB_PREF."purch_orders WHERE into_stock_location=".db_escape($selected_id);
-       $result = db_query($sql, "could not query purch orders");
-       $myrow = db_fetch_row($result);
-       if ($myrow[0] > 0) 
+       if (key_in_foreign_table($selected_id, 'purch_orders', 'into_stock_location'))
        {
                display_error(_("Cannot delete this location because it is used by some related records in other tables."));
                return false;
        }
-       $sql= "SELECT COUNT(*) FROM ".TB_PREF."sales_orders WHERE from_stk_loc=".db_escape($selected_id);
-       $result = db_query($sql, "could not query sales orders");
-       $myrow = db_fetch_row($result);
-       if ($myrow[0] > 0) 
+       if (key_in_foreign_table($selected_id, 'sales_orders', 'from_stk_loc'))
        {
                display_error(_("Cannot delete this location because it is used by some related records in other tables."));
                return false;
        }
-       $sql= "SELECT COUNT(*) FROM ".TB_PREF."sales_pos WHERE pos_location=".db_escape($selected_id);
-       $result = db_query($sql, "could not query sales pos");
-       $myrow = db_fetch_row($result);
-       if ($myrow[0] > 0) 
+       if (key_in_foreign_table($selected_id, 'sales_pos', 'pos_location'))
        {
                display_error(_("Cannot delete this location because it is used by some related records in other tables."));
                return false;
@@ -162,9 +139,7 @@ if ($Mode == 'RESET')
        $_POST['show_inactive'] = $sav;
 }
 
-$sql = "SELECT * FROM ".TB_PREF."locations";
-if (!check_value('show_inactive')) $sql .= " WHERE !inactive";
-$result = db_query($sql, "could not query locations");;
+$result = get_item_locations(check_value('show_inactive'));
 
 start_form();
 start_table($table_style);
index 259f6dce5d0f6f5cc71fa9b9778e9545a3412e62..edec494bbe60c36574bf58e10fb375fef190f2c2 100644 (file)
@@ -56,12 +56,7 @@ if ($Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM')
 
 function can_delete($selected_id)
 {
-       $sql= "SELECT COUNT(*) FROM ".TB_PREF."stock_moves 
-               WHERE type=" . ST_INVADJUST. " AND person_id=".db_escape($selected_id);
-
-       $result = db_query($sql, "could not query stock moves");
-       $myrow = db_fetch_row($result);
-       if ($myrow[0] > 0) 
+       if (movement_types_in_stock_moves($selected_id))
        {
                display_error(_("Cannot delete this inventory movement type because item transactions have been created referring to it."));
                return false;
index b77a92bdac5778e94ac3dc058d942146e2c2d6aa..7bb09a54e2056e62da090ee3504bce9a3fb154e4 100644 (file)
@@ -55,26 +55,14 @@ if ($Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM')
        {
        if ($Mode == 'ADD_ITEM') 
                {
-
-               $sql = "INSERT INTO ".TB_PREF."purch_data (supplier_id, stock_id, price, suppliers_uom,
-                       conversion_factor, supplier_description) VALUES (";
-               $sql .= db_escape($_POST['supplier_id']).", ".db_escape($_POST['stock_id']). ", "
-                       .input_num('price',0) . ", ".db_escape( $_POST['suppliers_uom'] ). ", "
-                       .input_num('conversion_factor') . ", "
-                       .db_escape($_POST['supplier_description']) . ")";
-
-               db_query($sql,"The supplier purchasing details could not be added");
+                       add_item_purchasing_data($_POST['supplier_id'], $_POST['stock_id'], input_num('price',0),
+                               $_POST['suppliers_uom'], input_num('conversion_factor'), $_POST['supplier_description']);
                display_notification(_("This supplier purchasing data has been added."));
-               } else
+               } 
+               else
                {
-               $sql = "UPDATE ".TB_PREF."purch_data SET price=" . input_num('price',0) . ",
-                               suppliers_uom=".db_escape($_POST['suppliers_uom']) . ",
-                               conversion_factor=" . input_num('conversion_factor') . ",
-                               supplier_description=" . db_escape($_POST['supplier_description']) . "
-                               WHERE stock_id=".db_escape($_POST['stock_id']) . " AND
-                               supplier_id=".db_escape($selected_id);
-               db_query($sql,"The supplier purchasing details could not be updated");
-
+                       update_item_purchasing_data($selected_id, $_POST['stock_id'], input_num('price',0),
+                               $_POST['suppliers_uom'], input_num('conversion_factor'), $_POST['supplier_description']);
                display_notification(_("Supplier purchasing data has been updated."));
                }
                $Mode = 'RESET';
@@ -85,11 +73,7 @@ if ($Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM')
 
 if ($Mode == 'Delete')
 {
-
-       $sql = "DELETE FROM ".TB_PREF."purch_data WHERE supplier_id=".db_escape($selected_id)."
-               AND stock_id=".db_escape($_POST['stock_id']);
-       db_query($sql,"could not delete purchasing data");
-
+       delete_item_purchasing_data($selected_id, $_POST['stock_id']);
        display_notification(_("The purchasing data item has been sucessfully deleted."));
        $Mode = 'RESET';
 }
@@ -130,15 +114,8 @@ if ($mb_flag == -1)
 }
 else
 {
-
-    $sql = "SELECT ".TB_PREF."purch_data.*,".TB_PREF."suppliers.supp_name,"
-       .TB_PREF."suppliers.curr_code
-               FROM ".TB_PREF."purch_data INNER JOIN ".TB_PREF."suppliers
-               ON ".TB_PREF."purch_data.supplier_id=".TB_PREF."suppliers.supplier_id
-               WHERE stock_id = ".db_escape($_POST['stock_id']);
-
-    $result = db_query($sql, "The supplier purchasing details for the selected part could not be retrieved");
-  div_start('price_table');
+       $result = get_items_purchasing_data($_POST['stock_id']);
+       div_start('price_table');
     if (db_num_rows($result) == 0)
     {
        display_note(_("There is no purchasing data set up for the part selected"));
@@ -186,15 +163,7 @@ else
 $dec2 = 6;
 if ($Mode =='Edit')
 {
-
-       $sql = "SELECT ".TB_PREF."purch_data.*,".TB_PREF."suppliers.supp_name FROM ".TB_PREF."purch_data
-               INNER JOIN ".TB_PREF."suppliers ON ".TB_PREF."purch_data.supplier_id=".TB_PREF."suppliers.supplier_id
-               WHERE ".TB_PREF."purch_data.supplier_id=".db_escape($selected_id)."
-               AND ".TB_PREF."purch_data.stock_id=".db_escape($_POST['stock_id']);
-
-       $result = db_query($sql, "The supplier purchasing details for the selected supplier and item could not be retrieved");
-
-       $myrow = db_fetch($result);
+       $myrow = get_item_purchasing_data($selected_id, $_POST['stock_id']);
 
     $supp_name = $myrow["supp_name"];
     $_POST['price'] = price_decimal_format($myrow["price"], $dec2);