Moved all SQL statements from PHP files into relevant *_db.inc files.
authorJoe Hunt <joe.hunt.consulting@gmail.com>
Fri, 4 Dec 2009 00:39:54 +0000 (00:39 +0000)
committerJoe Hunt <joe.hunt.consulting@gmail.com>
Fri, 4 Dec 2009 00:39:54 +0000 (00:39 +0000)
Included purchasing folder

22 files changed:
CHANGELOG.txt
includes/db/manufacturing_db.inc
manufacturing/includes/db/work_orders_db.inc
manufacturing/inquiry/where_used_inquiry.php
manufacturing/manage/bom_edit.php
manufacturing/manage/work_centres.php
manufacturing/search_work_orders.php
manufacturing/work_order_costs.php
purchasing/includes/db/invoice_db.inc
purchasing/includes/db/po_db.inc
purchasing/includes/db/supp_trans_db.inc
purchasing/includes/db/suppalloc_db.inc
purchasing/includes/db/suppliers_db.inc
purchasing/inquiry/po_search.php
purchasing/inquiry/po_search_completed.php
purchasing/inquiry/supplier_allocation_inquiry.php
purchasing/inquiry/supplier_inquiry.php
purchasing/manage/suppliers.php
purchasing/po_entry_items.php
purchasing/po_receive_items.php
purchasing/supplier_credit.php
purchasing/supplier_invoice.php

index 96990eb0436f959f93ce54d25a712c5bc2bc990b..1d8e60bae68f856f4415c18930afa8c68e869c8f 100644 (file)
@@ -19,6 +19,29 @@ Legend:
 ! -> Note
 $ -> Affected files
 
+04-Dec-2009 Joe Hunt
+! Moved all SQL statements from PHP files into relevant *_db.inc files.
+  Included purchasing folder
+$ /includes/db/manufacturing_db.inc
+  /manufacturing/search_work_orders.php
+  /manufacturing/work_order_costs.php
+  /manufacturing/includes/db/work_orders_db.inc
+  /manufacturing/inquiry/where_used_inquiry.php
+  /manufacturing/manage/bom_edit.php
+  /manufacturing/manage/work_centres.php
+  /purchasing/po_entry_items.php
+  /purchasing/supplier_credit.php
+  /purchasing/supplier_invoice.php
+  /purchasing/includes/db/invoice_db.inc
+  /purchasing/includes/db/po_db.inc
+  /purchasing/includes/db/suppalloc_db.inc
+  /purchasing/includes/db/supp_trans_db.inc
+  /purchasing/inquiry/po_search.php
+  /purchasing/inquiry/po_search_completed.php
+  /purchasing/inquiry/supplier_allocations_inquiry.php
+  /purchasing/inquiry/supplier_inquiry.php
+  /purchasing/manage/suppliers.php
+
 03-Dec-2009 Joe Hunt
 ! Moved all SQL statements from PHP files into relevant *_db.inc files.
   Included inventory folder
index 31fb3eceaf8d306d9243a3b1a9cc9dade80ac87e..14927475ed8ad206f406051bf239b6bde12d7320 100644 (file)
@@ -191,6 +191,35 @@ function get_mb_flag($stock_id)
 
 //--------------------------------------------------------------------------------------
 
+function add_bom($selected_parent, $component, $workcentre_added, $loc_code, $quantity)
+{
+       $sql = "INSERT INTO ".TB_PREF."bom (parent, component, workcentre_added, loc_code, quantity)
+               VALUES (".db_escape($selected_parent).", ".db_escape($component) . ","
+               .db_escape($workcentre_added) . ", ".db_escape($loc_code) . ", "
+               . $quantity . ")";
+
+       db_query($sql,"check failed");
+}
+//--------------------------------------------------------------------------------------
+
+function update_bom($selected_parent, $selected_component, $work_centre_added, $loc_code, $quantity)
+{
+       $sql = "UPDATE ".TB_PREF."bom SET workcentre_added=".db_escape($workcentre_added)
+        . ",loc_code=".db_escape($loc_code) . ",
+               quantity= " . $quantity . "
+               WHERE parent=".db_escape($selected_parent) . "
+               AND id=".db_escape($selected_component);
+       check_db_error("Could not update this bom component", $sql);
+
+       db_query($sql,"could not update bom");
+}
+       
+function delete_bom($selected_id)
+{
+       $sql = "DELETE FROM ".TB_PREF."bom WHERE id=".db_escape($selected_id);
+       db_query($sql,"Could not delete this bom components");
+}
+
 function get_bom($item)
 {
        $sql = "SELECT ".TB_PREF."bom.*, ".TB_PREF."locations.location_name, ".TB_PREF."workcentres.name AS WorkCentreDescription, 
@@ -207,6 +236,18 @@ function get_bom($item)
 
 //--------------------------------------------------------------------------------------
 
+function get_component_from_bom($selected_id)
+{
+       $sql = "SELECT ".TB_PREF."bom.*,".TB_PREF."stock_master.description FROM "
+               .TB_PREF."bom,".TB_PREF."stock_master
+               WHERE id=".db_escape($selected_id)."
+               AND ".TB_PREF."stock_master.stock_id=".TB_PREF."bom.component";
+
+       $result = db_query($sql, "could not get bom");
+       return db_fetch($result);
+}
+//--------------------------------------------------------------------------------------
+
 function has_bom($item)
 {
     $result = get_bom($item);
@@ -216,4 +257,46 @@ function has_bom($item)
 
 //--------------------------------------------------------------------------------------
 
+function is_component_already_on_bom($component, $workcentre_added, $loc_code, $selected_parent)
+{
+       $sql = "SELECT component FROM ".TB_PREF."bom
+               WHERE parent=".db_escape($selected_parent)."
+               AND component=".db_escape($component) . "
+               AND workcentre_added=".db_escape($workcentre_added) . "
+               AND loc_code=".db_escape($loc_code);
+       $result = db_query($sql,"check failed");
+
+       return (db_num_rows($result) > 0);
+}
+
+//--------------------------------------------------------------------------------------
+
+function check_for_recursive_bom($ultimate_parent, $component_to_check)
+{
+
+       /* returns true ie 1 if the bom contains the parent part as a component
+       ie the bom is recursive otherwise false ie 0 */
+
+       $sql = "SELECT component FROM ".TB_PREF."bom WHERE parent=".db_escape($component_to_check);
+       $result = db_query($sql,"could not check recursive bom");
+
+       if ($result != 0)
+       {
+               while ($myrow = db_fetch_row($result))
+               {
+                       if ($myrow[0] == $ultimate_parent)
+                       {
+                               return 1;
+                       }
+
+                       if (check_for_recursive_bom($ultimate_parent, $myrow[0]))
+                       {
+                               return 1;
+                       }
+               } //(while loop)
+       } //end if $result is true
+
+       return 0;
+} //end of function check_for_recursive_bom
+
 ?>
index f97f87a5e3813e39d97d6d014029a852850a89b4..7e8be6487754d9ff4a5f208070173236fb52bc5c 100644 (file)
@@ -309,6 +309,78 @@ function void_work_order($woid)
        }
 }
 
+function get_sql_for_work_orders($outstanding_only, $all_items)
+{
+       $sql = "SELECT
+               workorder.id,
+               workorder.wo_ref,
+               workorder.type,
+               location.location_name,
+               item.description,
+               workorder.units_reqd,
+               workorder.units_issued,
+               workorder.date_,
+               workorder.required_by,
+               workorder.released_date,
+               workorder.closed,
+               workorder.released,
+               workorder.stock_id,
+               unit.decimals
+               FROM ".TB_PREF."workorders as workorder,"
+                       .TB_PREF."stock_master as item,"
+                       .TB_PREF."item_units as unit,"
+                       .TB_PREF."locations as location
+               WHERE workorder.stock_id=item.stock_id 
+                       AND workorder.loc_code=location.loc_code
+                       AND item.units=unit.abbr";
+
+       if (check_value('OpenOnly') || $outstanding_only != 0)
+       {
+               $sql .= " AND workorder.closed=0";
+       }
+
+       if (isset($_POST['StockLocation']) && $_POST['StockLocation'] != $all_items)
+       {
+               $sql .= " AND workorder.loc_code=".db_escape($_POST['StockLocation']);
+       }
+
+       if (isset($_POST['OrderNumber']) && $_POST['OrderNumber'] != "")
+       {
+               $sql .= " AND workorder.wo_ref LIKE ".db_escape('%'.$_POST['OrderNumber'].'%');
+       }
+
+       if (isset($_POST['SelectedStockItem']) && $_POST['SelectedStockItem'] != $all_items)
+       {
+               $sql .= " AND workorder.stock_id=".db_escape($_POST['SelectedStockItem']);
+       }
+
+       if (check_value('OverdueOnly'))
+       {
+               $Today = date2sql(Today());
+
+               $sql .= " AND workorder.required_by < '$Today' ";
+       }
+       return $sql;
+}
+
+function get_sql_for_where_used()
+{
+       $sql = "SELECT 
+                       bom.parent,
+                       workcentre.name As WorkCentreName,
+                       location.location_name,
+                       bom.quantity,
+                       parent.description
+                       FROM ".TB_PREF."bom as bom, "
+                               .TB_PREF."stock_master as parent, "
+                               .TB_PREF."workcentres as workcentre, "
+                               .TB_PREF."locations as location
+                       WHERE bom.parent = parent.stock_id 
+                               AND bom.workcentre_added = workcentre.id
+                               AND bom.loc_code = location.loc_code
+                               AND bom.component=".db_escape($_POST['stock_id']);
+       return $sql;                    
+}
 //--------------------------------------------------------------------------------------
 
 ?>
\ No newline at end of file
index 14286c0acebf3f5c8f0edd24c7e0802d87f371ef..10fce898a820d658f5c2cf3649634a8ccb798f55 100644 (file)
@@ -37,20 +37,7 @@ function select_link($row)
                "/manufacturing/manage/bom_edit.php?stock_id=" . $row["parent"]);
 }
 
-$sql = "SELECT 
-               bom.parent,
-               workcentre.name As WorkCentreName,
-               location.location_name,
-               bom.quantity,
-               parent.description
-               FROM ".TB_PREF."bom as bom, "
-                       .TB_PREF."stock_master as parent, "
-                       .TB_PREF."workcentres as workcentre, "
-                       .TB_PREF."locations as location
-               WHERE bom.parent = parent.stock_id 
-                       AND bom.workcentre_added = workcentre.id
-                       AND bom.loc_code = location.loc_code
-                       AND bom.component=".db_escape($_POST['stock_id']);
+$sql = get_sql_for_where_used();
 
    $cols = array(
        _("Parent Item") => array('fun'=>'select_link'), 
index 742c2b1a8c5f72e2ac6ad028949bd5e37587f2ca..e963d5634f53192d22aaf161ad1e68b6e8f3ab88 100644 (file)
@@ -62,43 +62,12 @@ else
 
 //--------------------------------------------------------------------------------------------------
 
-function check_for_recursive_bom($ultimate_parent, $component_to_check)
-{
-
-       /* returns true ie 1 if the bom contains the parent part as a component
-       ie the bom is recursive otherwise false ie 0 */
-
-       $sql = "SELECT component FROM ".TB_PREF."bom WHERE parent=".db_escape($component_to_check);
-       $result = db_query($sql,"could not check recursive bom");
-
-       if ($result != 0)
-       {
-               while ($myrow = db_fetch_row($result))
-               {
-                       if ($myrow[0] == $ultimate_parent)
-                       {
-                               return 1;
-                       }
-
-                       if (check_for_recursive_bom($ultimate_parent, $myrow[0]))
-                       {
-                               return 1;
-                       }
-               } //(while loop)
-       } //end if $result is true
-
-       return 0;
-
-} //end of function check_for_recursive_bom
-
-//--------------------------------------------------------------------------------------------------
-
 function display_bom_items($selected_parent)
 {
        global $table_style;
 
        $result = get_bom($selected_parent);
-div_start('bom');
+       div_start('bom');
        start_table("$table_style width=60%");
        $th = array(_("Code"), _("Description"), _("Location"),
                _("Work Centre"), _("Quantity"), _("Units"),'','');
@@ -122,7 +91,7 @@ div_start('bom');
 
        } //END WHILE LIST LOOP
        end_table();
-div_end();
+       div_end();
 }
 
 //--------------------------------------------------------------------------------------------------
@@ -138,15 +107,8 @@ function on_submit($selected_parent, $selected_component=-1)
 
        if ($selected_component != -1)
        {
-
-               $sql = "UPDATE ".TB_PREF."bom SET workcentre_added=".db_escape($_POST['workcentre_added'])
-                . ",loc_code=".db_escape($_POST['loc_code']) . ",
-                       quantity= " . input_num('quantity') . "
-                       WHERE parent=".db_escape($selected_parent) . "
-                       AND id=".db_escape($selected_component);
-               check_db_error("Could not update this bom component", $sql);
-
-               db_query($sql,"could not update bom");
+               update_bom($selected_parent, $selected_component, $_POST['workcentre_added'], $_POST['loc_code'],
+                       input_num('quantity'));
                display_notification(_('Selected component has been updated'));
                $Mode = 'RESET';
        }
@@ -162,21 +124,11 @@ function on_submit($selected_parent, $selected_component=-1)
                {
 
                        /*Now check to see that the component is not already on the bom */
-                       $sql = "SELECT component FROM ".TB_PREF."bom
-                               WHERE parent=".db_escape($selected_parent)."
-                               AND component=".db_escape($_POST['component']) . "
-                               AND workcentre_added=".db_escape($_POST['workcentre_added']) . "
-                               AND loc_code=".db_escape($_POST['loc_code']);
-                       $result = db_query($sql,"check failed");
-
-                       if (db_num_rows($result) == 0)
+                       if (!is_component_already_on_bom($_POST['component'], $_POST['workcentre_added'],
+                               $_POST['loc_code'], $selected_parent))
                        {
-                               $sql = "INSERT INTO ".TB_PREF."bom (parent, component, workcentre_added, loc_code, quantity)
-                                       VALUES (".db_escape($selected_parent).", ".db_escape($_POST['component']) . ","
-                                       .db_escape($_POST['workcentre_added']) . ", ".db_escape($_POST['loc_code']) . ", "
-                                       . input_num('quantity') . ")";
-
-                               db_query($sql,"check failed");
+                               add_bom($selected_parent, $_POST['component'], $_POST['workcentre_added'],
+                                       $_POST['loc_code'], input_num('quantity'));
                                display_notification(_("A new component part has been added to the bill of material for this item."));
                                $Mode = 'RESET';
                        }
@@ -198,8 +150,7 @@ function on_submit($selected_parent, $selected_component=-1)
 
 if ($Mode == 'Delete')
 {
-       $sql = "DELETE FROM ".TB_PREF."bom WHERE id=".db_escape($selected_id);
-       db_query($sql,"Could not delete this bom components");
+       delete_bom($selected_id);
 
        display_notification(_("The component item has been deleted from this bom"));
        $Mode = 'RESET';
@@ -244,19 +195,13 @@ start_form();
        {
                if ($Mode == 'Edit') {
                        //editing a selected component from the link to the line item
-                       $sql = "SELECT ".TB_PREF."bom.*,".TB_PREF."stock_master.description FROM "
-                               .TB_PREF."bom,".TB_PREF."stock_master
-                               WHERE id=".db_escape($selected_id)."
-                               AND ".TB_PREF."stock_master.stock_id=".TB_PREF."bom.component";
-
-                       $result = db_query($sql, "could not get bom");
-                       $myrow = db_fetch($result);
+                       $myrow = get_component_from_bom($selected_id);
 
                        $_POST['loc_code'] = $myrow["loc_code"];
                        $_POST['component'] = $myrow["component"]; // by Tom Moulton
                        $_POST['workcentre_added']  = $myrow["workcentre_added"];
                        $_POST['quantity'] = number_format2($myrow["quantity"], get_qty_dec($myrow["component"]));
-               label_row(_("Component:"), $myrow["component"] . " - " . $myrow["description"]);
+                       label_row(_("Component:"), $myrow["component"] . " - " . $myrow["description"]);
                }
                hidden('selected_id', $selected_id);
        }
index 03014572f30f1e629aaf66ae5adff7068fabe124..b9199690c9b0e78f31a7a8d178781cc97555a868 100644 (file)
@@ -56,19 +56,13 @@ if ($Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM')
 
 function can_delete($selected_id)
 {
-       $sql= "SELECT COUNT(*) FROM ".TB_PREF."bom WHERE workcentre_added=".db_escape($selected_id);
-       $result = db_query($sql, "check can delete work centre");
-       $myrow = db_fetch_row($result);
-       if ($myrow[0] > 0) 
+       if (key_in_foreign_table($selected_id, 'bom', 'workcentre_added'))
        {
                display_error(_("Cannot delete this work centre because BOMs have been created referring to it."));
                return false;
        }
-       
-       $sql= "SELECT COUNT(*) FROM ".TB_PREF."wo_requirements WHERE workcentre=".db_escape($selected_id);
-       $result = db_query($sql, "check can delete work centre");
-       $myrow = db_fetch_row($result);
-       if ($myrow[0] > 0) 
+
+       if (key_in_foreign_table($selected_id, 'wo_requirements', 'workcentre'))        
        {
                display_error(_("Cannot delete this work centre because work order requirements have been created referring to it."));
                return false;
index 3fdbf1df1db44d1c2c524625868a7ba69f890d5f..f212a1a842d5cb8e04dcc8cef864d81a73f1af56 100644 (file)
@@ -153,55 +153,7 @@ function dec_amount($row, $amount)
        return number_format2($amount, $row['decimals']);
 }
 
-$sql = "SELECT
-       workorder.id,
-       workorder.wo_ref,
-       workorder.type,
-       location.location_name,
-       item.description,
-       workorder.units_reqd,
-       workorder.units_issued,
-       workorder.date_,
-       workorder.required_by,
-       workorder.released_date,
-       workorder.closed,
-       workorder.released,
-       workorder.stock_id,
-       unit.decimals
-       FROM ".TB_PREF."workorders as workorder,"
-               .TB_PREF."stock_master as item,"
-               .TB_PREF."item_units as unit,"
-               .TB_PREF."locations as location
-       WHERE workorder.stock_id=item.stock_id 
-               AND workorder.loc_code=location.loc_code
-               AND item.units=unit.abbr";
-
-if (check_value('OpenOnly') || $outstanding_only != 0)
-{
-       $sql .= " AND workorder.closed=0";
-}
-
-if (isset($_POST['StockLocation']) && $_POST['StockLocation'] != $all_items)
-{
-       $sql .= " AND workorder.loc_code=".db_escape($_POST['StockLocation']);
-}
-
-if (isset($_POST['OrderNumber']) && $_POST['OrderNumber'] != "")
-{
-       $sql .= " AND workorder.wo_ref LIKE ".db_escape('%'.$_POST['OrderNumber'].'%');
-}
-
-if (isset($_POST['SelectedStockItem']) && $_POST['SelectedStockItem'] != $all_items)
-{
-       $sql .= " AND workorder.stock_id=".db_escape($_POST['SelectedStockItem']);
-}
-
-if (check_value('OverdueOnly'))
-{
-       $Today = date2sql(Today());
-
-       $sql .= " AND workorder.required_by < '$Today' ";
-}
+$sql = get_sql_for_work_orders($outstanding_only, $all_items);
 
 $cols = array(
        _("#") => array('fun'=>'view_link'), 
index 4e7f05379b275601ee566f21911018e46e2dccc9..2e72cf10a50255d02c25b98badfa3176b82fa0e1 100644 (file)
@@ -143,9 +143,7 @@ date_row(_("Date:"), 'date_');
 
 $item_accounts = get_stock_gl_code($wo_details['stock_id']);
 $_POST['db_acc'] = $item_accounts['assembly_account'];
-$sql = "SELECT DISTINCT account_code FROM ".TB_PREF."bank_accounts";
-$rs = db_query($sql,"could not get bank accounts");
-$r = db_fetch_row($rs);
+$r = get_default_bank_account(get_company_pref('curr_default'));
 $_POST['cr_acc'] = $r[0];
 
 amount_row(_("Additional Costs:"), 'costs');
index ffdb8c17316ffebd60e78392c1eb71331be69ea0..ec3e14a88321d55b30fd60f2c5bf1ade27cb8d4c 100644 (file)
@@ -505,6 +505,47 @@ function void_supp_invoice($type, $type_no)
 }
 
 //----------------------------------------------------------------------------------------
+function get_gl_account_info($acc)
+{
+       $sql = "SELECT account_code, account_name FROM ".TB_PREF."chart_master WHERE account_code=".db_escape($acc);
+       return db_query($sql,"get account information");
+}
+
+function is_reference_already_there($supplier_id, $supp_reference)
+{
+       $sql = "SELECT Count(*) FROM ".TB_PREF."supp_trans WHERE supplier_id="
+               .db_escape($supplier_id) . " AND supp_reference=" 
+               .db_escape($supp_reference) 
+               . " AND ov_amount!=0"; // ignore voided invoice references
+
+       $result=db_query($sql,"The sql to check for the previous entry of the same invoice failed");
+
+       $myrow = db_fetch_row($result);
+       return ($myrow[0] == 1);
+}
+
+function remove_not_invoice_item($id)
+{
+       begin_transaction();
+
+       $myrow = get_grn_item_detail($id);
+
+       $grn = get_grn_batch($myrow['grn_batch_id']);
 
+       $sql = "UPDATE ".TB_PREF."purch_order_details
+               SET quantity_received = qty_invoiced, quantity_ordered = qty_invoiced WHERE po_detail_item = ".$myrow["po_detail_item"];
+       db_query($sql, "The quantity invoiced of the purchase order line could not be updated");
+
+       $sql = "UPDATE ".TB_PREF."grn_items
+               SET qty_recd = quantity_inv WHERE id = ".$myrow["id"];
+       db_query($sql, "The quantity invoiced off the items received record could not be updated");
 
+       update_average_material_cost($grn["supplier_id"], $myrow["item_code"],
+               $myrow["unit_price"], -$myrow["QtyOstdg"], Today());
+
+       add_stock_move(ST_SUPPRECEIVE, $myrow["item_code"], $myrow['grn_batch_id'], $grn['loc_code'], sql2date($grn["delivery_date"]), "",
+               -$myrow["QtyOstdg"], $myrow['std_cost_unit'], $grn["supplier_id"], 1, $myrow['unit_price']);
+
+       commit_transaction();
+}
 ?>
index ee2b7d8e38575b0a2dc92f737c88799cd6b3f51c..e89d96033a93714bdffa468d0953b8ed684c3beb 100644 (file)
@@ -227,5 +227,126 @@ function read_po($order_no, &$order, $open_items_only=false)
 
 //----------------------------------------------------------------------------------------
 
+function get_po_items($order_no)
+{
+       $sql = "SELECT item_code, quantity_ordered, quantity_received, qty_invoiced
+               FROM ".TB_PREF."purch_order_details
+               WHERE order_no=".db_escape($order_no)
+               ." ORDER BY po_detail_item";
+
+       $result = db_query($sql, "could not query purch order details");
+    check_db_error("Could not check that the details of the purchase order had not been changed by another user ", $sql);
+    return $result;
+}
+//----------------------------------------------------------------------------------------
+
+function get_short_info($stock_id)
+{
+       $sql = "SELECT description, units, mb_flag
+               FROM ".TB_PREF."stock_master WHERE stock_id = ".db_escape($stock_id);
+
+       return db_query($sql,"The stock details for " . $stock_id . " could not be retrieved");
+}
+
+function get_sql_for_po_search_completed()
+{
+       global $order_number, $selected_stock_item;;
+
+       $sql = "SELECT 
+               porder.order_no, 
+               porder.reference, 
+               supplier.supp_name, 
+               location.location_name,
+               porder.requisition_no, 
+               porder.ord_date, 
+               supplier.curr_code, 
+               Sum(line.unit_price*line.quantity_ordered) AS OrderValue,
+               porder.into_stock_location
+               FROM ".TB_PREF."purch_orders as porder, "
+                       .TB_PREF."purch_order_details as line, "
+                       .TB_PREF."suppliers as supplier, "
+                       .TB_PREF."locations as location
+               WHERE porder.order_no = line.order_no
+               AND porder.supplier_id = supplier.supplier_id
+               AND location.loc_code = porder.into_stock_location ";
+
+       if (isset($order_number) && $order_number != "")
+       {
+               $sql .= "AND porder.reference LIKE ".db_escape('%'. $order_number . '%');
+       }
+       else
+       {
+
+               $data_after = date2sql($_POST['OrdersAfterDate']);
+               $date_before = date2sql($_POST['OrdersToDate']);
+
+               $sql .= " AND porder.ord_date >= '$data_after'";
+               $sql .= " AND porder.ord_date <= '$date_before'";
+
+               if (isset($_POST['StockLocation']) && $_POST['StockLocation'] != ALL_TEXT)
+               {
+                       $sql .= " AND porder.into_stock_location = ".db_escape($_POST['StockLocation']);
+               }
+               if (isset($selected_stock_item))
+               {
+                       $sql .= " AND line.item_code=".db_escape($selected_stock_item);
+               }
+
+       } //end not order number selected
+
+       $sql .= " GROUP BY porder.order_no";
+       return $sql;
+}      
+
+function get_sql_for_po_search()
+{
+       global $all_items, $order_number, $selected_stock_item;;
+       
+       $sql = "SELECT 
+               porder.order_no, 
+               porder.reference,
+               supplier.supp_name, 
+               location.location_name,
+               porder.requisition_no, 
+               porder.ord_date,
+               supplier.curr_code,
+               Sum(line.unit_price*line.quantity_ordered) AS OrderValue,
+               Sum(line.delivery_date < '". date2sql(Today()) ."'
+               AND (line.quantity_ordered > line.quantity_received)) As OverDue
+               FROM "
+                       .TB_PREF."purch_orders as porder, "
+                       .TB_PREF."purch_order_details as line, "
+                       .TB_PREF."suppliers as supplier, "
+                       .TB_PREF."locations as location
+               WHERE porder.order_no = line.order_no
+               AND porder.supplier_id = supplier.supplier_id
+               AND location.loc_code = porder.into_stock_location
+               AND (line.quantity_ordered > line.quantity_received) ";
+
+       if (isset($order_number) && $order_number != "")
+       {
+               $sql .= "AND porder.reference LIKE ".db_escape('%'. $order_number . '%');
+       }
+       else
+       {
+               $data_after = date2sql($_POST['OrdersAfterDate']);
+               $data_before = date2sql($_POST['OrdersToDate']);
+
+               $sql .= "  AND porder.ord_date >= '$data_after'";
+               $sql .= "  AND porder.ord_date <= '$data_before'";
+
+               if (isset($_POST['StockLocation']) && $_POST['StockLocation'] != $all_items)
+               {
+                       $sql .= " AND porder.into_stock_location = ".db_escape($_POST['StockLocation']);
+               }
 
+               if (isset($selected_stock_item))
+               {
+                       $sql .= " AND line.item_code=".db_escape($selected_stock_item);
+               }
+       } //end not order number selected
+
+       $sql .= " GROUP BY porder.order_no";
+       return $sql;
+}
 ?>
\ No newline at end of file
index 2aa6d0b420dd954b756648804313ba23ca5464f8..b96248098d98fd6558e742cea33d08dc04adbd86 100644 (file)
@@ -151,4 +151,55 @@ function post_void_supp_trans($type, $type_no)
 
 //----------------------------------------------------------------------------------------
 
+function get_sql_for_supplier_inquiry()
+{
+    $date_after = date2sql($_POST['TransAfterDate']);
+    $date_to = date2sql($_POST['TransToDate']);
+
+    $sql = "SELECT trans.type, 
+               trans.trans_no,
+               trans.reference, 
+               supplier.supp_name, 
+               trans.supp_reference,
+       trans.tran_date, 
+               trans.due_date,
+               supplier.curr_code, 
+       (trans.ov_amount + trans.ov_gst  + trans.ov_discount) AS TotalAmount, 
+               trans.alloc AS Allocated,
+               ((trans.type = ".ST_SUPPINVOICE." OR trans.type = ".ST_SUPPCREDIT.") AND trans.due_date < '" . date2sql(Today()) . "') AS OverDue,
+       (ABS(trans.ov_amount + trans.ov_gst  + trans.ov_discount - trans.alloc) <= 0.005) AS Settled
+       FROM ".TB_PREF."supp_trans as trans, ".TB_PREF."suppliers as supplier
+       WHERE supplier.supplier_id = trans.supplier_id
+       AND trans.tran_date >= '$date_after'
+       AND trans.tran_date <= '$date_to'
+               AND trans.ov_amount != 0";      // exclude voided transactions
+       if ($_POST['supplier_id'] != ALL_TEXT)
+               $sql .= " AND trans.supplier_id = ".db_escape($_POST['supplier_id']);
+       if (isset($_POST['filterType']) && $_POST['filterType'] != ALL_TEXT)
+       {
+               if (($_POST['filterType'] == '1')) 
+               {
+                       $sql .= " AND (trans.type = ".ST_SUPPINVOICE." OR trans.type = ".ST_BANKDEPOSIT.")";
+               } 
+               elseif (($_POST['filterType'] == '2')) 
+               {
+                       $sql .= " AND trans.type = ".ST_SUPPINVOICE." ";
+               } 
+               elseif ($_POST['filterType'] == '3') 
+               {
+                       $sql .= " AND (trans.type = ".ST_SUPPAYMENT." OR trans.type = ".ST_BANKPAYMENT.") ";
+               } 
+               elseif (($_POST['filterType'] == '4') || ($_POST['filterType'] == '5')) 
+               {
+                       $sql .= " AND trans.type = ".ST_SUPPCREDIT."  ";
+               }
+
+               if (($_POST['filterType'] == '2') || ($_POST['filterType'] == '5')) 
+               {
+                       $today =  date2sql(Today());
+                       $sql .= " AND trans.due_date < '$today' ";
+               }
+       }
+       return $sql;
+}
 ?>
\ No newline at end of file
index 987a640db957073acbe105afc56b416e9f22740b..a68c9cfe6ee481845d96c0ca678cc61467e9524f 100644 (file)
@@ -176,5 +176,58 @@ function get_allocatable_to_supp_transactions($supplier_id, $trans_no=null, $typ
        return db_query($sql." ORDER BY trans_no", "Cannot retreive alloc to transactions");
 }
 
+function get_sql_for_supplier_allocation_inquiry()
+{
+       $date_after = date2sql($_POST['TransAfterDate']);
+       $date_to = date2sql($_POST['TransToDate']);
 
+    $sql = "SELECT 
+               trans.type, 
+               trans.trans_no,
+               trans.reference, 
+               supplier.supp_name, 
+               trans.supp_reference,
+       trans.tran_date, 
+               trans.due_date,
+               supplier.curr_code, 
+       (trans.ov_amount + trans.ov_gst  + trans.ov_discount) AS TotalAmount, 
+               trans.alloc AS Allocated,
+               ((trans.type = ".ST_SUPPINVOICE." OR trans.type = ".ST_SUPPCREDIT.") AND trans.due_date < '" . date2sql(Today()) . "') AS OverDue
+       FROM "
+                       .TB_PREF."supp_trans as trans, "
+                       .TB_PREF."suppliers as supplier
+       WHERE supplier.supplier_id = trans.supplier_id
+       AND trans.tran_date >= '$date_after'
+       AND trans.tran_date <= '$date_to'";
+
+       if ($_POST['supplier_id'] != ALL_TEXT)
+               $sql .= " AND trans.supplier_id = ".db_escape($_POST['supplier_id']);
+       if (isset($_POST['filterType']) && $_POST['filterType'] != ALL_TEXT)
+       {
+               if (($_POST['filterType'] == '1') || ($_POST['filterType'] == '2'))
+               {
+                       $sql .= " AND trans.type = ".ST_SUPPINVOICE." ";
+               }
+               elseif ($_POST['filterType'] == '3')
+               {
+                       $sql .= " AND trans.type = ".ST_SUPPAYMENT." ";
+               }
+               elseif (($_POST['filterType'] == '4') || ($_POST['filterType'] == '5'))
+               {
+                       $sql .= " AND trans.type = ".ST_SUPPCREDIT." ";
+               }
+
+               if (($_POST['filterType'] == '2') || ($_POST['filterType'] == '5'))
+               {
+                       $today =  date2sql(Today());
+                       $sql .= " AND trans.due_date < '$today' ";
+               }
+       }
+
+       if (!check_value('showSettled'))
+       {
+               $sql .= " AND (round(abs(ov_amount + ov_gst + ov_discount) - alloc,6) != 0) ";
+       }
+       return $sql;
+}
 ?>
\ No newline at end of file
index dd72a6cf94efa8628354bc9648ffe26fc31c699c..305ce5df7cfa1babf49a12e2ffbabe4a4886d952 100644 (file)
@@ -9,6 +9,80 @@
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
     See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
 ***********************************************************************/
+
+function add_supplier($supp_name, $supp_ref, $address, $supp_address, $phone, $phone2, $fax, $gst_no, $email,
+       $website, $contact, $supp_account_no, $bank_account, $credit_limit, $dimension_id, $dimension2_id, 
+       $curr_code, $payment_terms, $payable_account, $purchase_account, $payment_discount_account, 
+       $notes, $tax_group_id)
+{
+       $sql = "INSERT INTO ".TB_PREF."suppliers (supp_name, supp_ref, address, supp_address, phone, phone2, fax, gst_no, email, website,
+               contact, supp_account_no, bank_account, credit_limit, dimension_id, dimension2_id, curr_code,
+               payment_terms, payable_account, purchase_account, payment_discount_account, notes, tax_group_id)
+               VALUES (".db_escape($_POST['supp_name']). ", "
+               .db_escape($supp_ref). ", "
+               .db_escape($address) . ", "
+               .db_escape($supp_address) . ", "
+               .db_escape($phone). ", "
+               .db_escape($phone2). ", "
+               .db_escape($fax). ", "
+               .db_escape($gst_no). ", "
+               .db_escape($email). ", "
+               .db_escape($website). ", "
+               .db_escape($contact). ", "
+               .db_escape($supp_account_no). ", "
+               .db_escape($bank_account). ", "
+               .$credit_limit. ", "
+               .db_escape($dimension_id). ", "
+               .db_escape($dimension2_id). ", "
+               .db_escape($curr_code). ", "
+               .db_escape($payment_terms). ", "
+               .db_escape($payable_account). ", "
+               .db_escape($purchase_account). ", "
+               .db_escape($payment_discount_account). ", "
+               .db_escape($notes). ", "
+               .db_escape($tax_group_id). ")";
+
+       db_query($sql,"The supplier could not be added");
+}
+
+function update_supplier($supplier_id, $supp_name, $supp_ref, $address, $supp_address, $phone, $phone2, $fax, $gst_no, 
+       $email, $website, $contact, $supp_account_no, $bank_account, $credit_limit, $dimension_id, $dimension2_id, 
+       $curr_code, $payment_terms, $payable_account, $purchase_account, $payment_discount_account, 
+       $notes, $tax_group_id)
+{
+       $sql = "UPDATE ".TB_PREF."suppliers SET supp_name=".db_escape($supp_name) . ",
+               supp_ref=".db_escape($supp_ref) . ",
+               address=".db_escape($address) . ",
+               supp_address=".db_escape($supp_address) . ",
+               phone=".db_escape($phone) . ",
+               phone2=".db_escape($phone2) . ",
+               fax=".db_escape($fax) . ",
+               gst_no=".db_escape($gst_no) . ",
+               email=".db_escape($email) . ",
+               website=".db_escape($website) . ",
+               contact=".db_escape($contact) . ",
+               supp_account_no=".db_escape($supp_account_no) . ",
+               bank_account=".db_escape($bank_account) . ",
+               credit_limit=".$credit_limit . ",
+               dimension_id=".db_escape($dimension_id) . ",
+               dimension2_id=".db_escape($dimension2_id) . ",
+               curr_code=".db_escape($curr_code).",
+               payment_terms=".db_escape($payment_terms) . ",
+               payable_account=".db_escape($payable_account) . ",
+               purchase_account=".db_escape($purchase_account) . ",
+               payment_discount_account=".db_escape($payment_discount_account) . ",
+               notes=".db_escape($notes) . ",
+               tax_group_id=".db_escape($tax_group_id) . " WHERE supplier_id = ".db_escape($supplier_id);
+
+       db_query($sql,"The supplier could not be updated");
+}
+
+function delete_supplier($supplier_id)
+{
+       $sql="DELETE FROM ".TB_PREF."suppliers WHERE supplier_id=".db_escape($supplier_id);
+       db_query($sql,"check failed");
+}
+
 function get_supplier_details($supplier_id, $to=null)
 {
 
index b43a99ffcc46a94fb5b16a6e668934e328155f57..e0108a4045fddceb22f1540d57288897bcacddbb 100644 (file)
@@ -116,53 +116,9 @@ else
 }
 
 //figure out the sql required from the inputs available
-$sql = "SELECT 
-       porder.order_no, 
-       porder.reference,
-       supplier.supp_name, 
-       location.location_name,
-       porder.requisition_no, 
-       porder.ord_date,
-       supplier.curr_code,
-       Sum(line.unit_price*line.quantity_ordered) AS OrderValue,
-       Sum(line.delivery_date < '". date2sql(Today()) ."'
-       AND (line.quantity_ordered > line.quantity_received)) As OverDue
-       FROM "
-               .TB_PREF."purch_orders as porder, "
-               .TB_PREF."purch_order_details as line, "
-               .TB_PREF."suppliers as supplier, "
-               .TB_PREF."locations as location
-       WHERE porder.order_no = line.order_no
-       AND porder.supplier_id = supplier.supplier_id
-       AND location.loc_code = porder.into_stock_location
-       AND (line.quantity_ordered > line.quantity_received) ";
-
-if (isset($order_number) && $order_number != "")
-{
-       $sql .= "AND porder.reference LIKE ".db_escape('%'. $order_number . '%');
-}
-else
-{
-       $data_after = date2sql($_POST['OrdersAfterDate']);
-       $data_before = date2sql($_POST['OrdersToDate']);
-
-       $sql .= "  AND porder.ord_date >= '$data_after'";
-       $sql .= "  AND porder.ord_date <= '$data_before'";
-
-       if (isset($_POST['StockLocation']) && $_POST['StockLocation'] != $all_items)
-       {
-               $sql .= " AND porder.into_stock_location = ".db_escape($_POST['StockLocation']);
-       }
-
-       if (isset($selected_stock_item))
-       {
-               $sql .= " AND line.item_code=".db_escape($selected_stock_item);
-       }
-} //end not order number selected
-
-$sql .= " GROUP BY porder.order_no";
+$sql = get_sql_for_po_search();
 
-$result = db_query($sql,"No orders were returned");
+//$result = db_query($sql,"No orders were returned");
 
 /*show a table of the orders returned by the sql */
 $cols = array(
index 1632f5b49de15f440d70011bdffddec4ba1231a1..fad27dc53d11dbb3970a8dc211747a5ea5d23e69 100644 (file)
@@ -105,49 +105,7 @@ function prt_link($row)
 
 //---------------------------------------------------------------------------------------------
 
-$sql = "SELECT 
-       porder.order_no, 
-       porder.reference, 
-       supplier.supp_name, 
-       location.location_name,
-       porder.requisition_no, 
-       porder.ord_date, 
-       supplier.curr_code, 
-       Sum(line.unit_price*line.quantity_ordered) AS OrderValue,
-       porder.into_stock_location
-       FROM ".TB_PREF."purch_orders as porder, "
-               .TB_PREF."purch_order_details as line, "
-               .TB_PREF."suppliers as supplier, "
-               .TB_PREF."locations as location
-       WHERE porder.order_no = line.order_no
-       AND porder.supplier_id = supplier.supplier_id
-       AND location.loc_code = porder.into_stock_location ";
-
-if (isset($order_number) && $order_number != "")
-{
-       $sql .= "AND porder.reference LIKE ".db_escape('%'. $order_number . '%');
-}
-else
-{
-
-       $data_after = date2sql($_POST['OrdersAfterDate']);
-       $date_before = date2sql($_POST['OrdersToDate']);
-
-       $sql .= " AND porder.ord_date >= '$data_after'";
-       $sql .= " AND porder.ord_date <= '$date_before'";
-
-       if (isset($_POST['StockLocation']) && $_POST['StockLocation'] != ALL_TEXT)
-       {
-               $sql .= " AND porder.into_stock_location = ".db_escape($_POST['StockLocation']);
-       }
-       if (isset($selected_stock_item))
-       {
-               $sql .= " AND line.item_code=".db_escape($selected_stock_item);
-       }
-
-} //end not order number selected
-
-$sql .= " GROUP BY porder.order_no";
+$sql = get_sql_for_po_search_completed();
 
 $cols = array(
                _("#") => array('fun'=>'trans_view', 'ord'=>''), 
index 850dfc3081c344003cd61d7f7701aad1af71eaf3..14cb32900fd1a9eff3d6043087cc81e5c9f2fa1c 100644 (file)
@@ -119,57 +119,7 @@ function fmt_credit($row)
 }
 //------------------------------------------------------------------------------------------------
 
- $date_after = date2sql($_POST['TransAfterDate']);
- $date_to = date2sql($_POST['TransToDate']);
-
-    // Sherifoz 22.06.03 Also get the description
-    $sql = "SELECT 
-               trans.type, 
-               trans.trans_no,
-               trans.reference, 
-               supplier.supp_name, 
-               trans.supp_reference,
-       trans.tran_date, 
-               trans.due_date,
-               supplier.curr_code, 
-       (trans.ov_amount + trans.ov_gst  + trans.ov_discount) AS TotalAmount, 
-               trans.alloc AS Allocated,
-               ((trans.type = ".ST_SUPPINVOICE." OR trans.type = ".ST_SUPPCREDIT.") AND trans.due_date < '" . date2sql(Today()) . "') AS OverDue
-       FROM "
-                       .TB_PREF."supp_trans as trans, "
-                       .TB_PREF."suppliers as supplier
-       WHERE supplier.supplier_id = trans.supplier_id
-       AND trans.tran_date >= '$date_after'
-       AND trans.tran_date <= '$date_to'";
-
-       if ($_POST['supplier_id'] != ALL_TEXT)
-               $sql .= " AND trans.supplier_id = ".db_escape($_POST['supplier_id']);
-       if (isset($_POST['filterType']) && $_POST['filterType'] != ALL_TEXT)
-       {
-               if (($_POST['filterType'] == '1') || ($_POST['filterType'] == '2'))
-               {
-                       $sql .= " AND trans.type = ".ST_SUPPINVOICE." ";
-               }
-               elseif ($_POST['filterType'] == '3')
-               {
-                       $sql .= " AND trans.type = ".ST_SUPPAYMENT." ";
-               }
-               elseif (($_POST['filterType'] == '4') || ($_POST['filterType'] == '5'))
-               {
-                       $sql .= " AND trans.type = ".ST_SUPPCREDIT." ";
-               }
-
-               if (($_POST['filterType'] == '2') || ($_POST['filterType'] == '5'))
-               {
-                       $today =  date2sql(Today());
-                       $sql .= " AND trans.due_date < '$today' ";
-               }
-       }
-
-       if (!check_value('showSettled'))
-       {
-               $sql .= " AND (round(abs(ov_amount + ov_gst + ov_discount) - alloc,6) != 0) ";
-       }
+$sql = get_sql_for_supplier_allocation_inquiry();
 
 $cols = array(
        _("Type") => array('fun'=>'systype_name'),
index 264b551819974f904f8934ef6ffbe1ed4e41687b..7a012905589c617165d86a2bdcc657149840e8ce 100644 (file)
@@ -156,54 +156,7 @@ function check_overdue($row)
 }
 //------------------------------------------------------------------------------------------------
 
-    $date_after = date2sql($_POST['TransAfterDate']);
-    $date_to = date2sql($_POST['TransToDate']);
-
-    // Sherifoz 22.06.03 Also get the description
-    $sql = "SELECT trans.type, 
-               trans.trans_no,
-               trans.reference, 
-               supplier.supp_name, 
-               trans.supp_reference,
-       trans.tran_date, 
-               trans.due_date,
-               supplier.curr_code, 
-       (trans.ov_amount + trans.ov_gst  + trans.ov_discount) AS TotalAmount, 
-               trans.alloc AS Allocated,
-               ((trans.type = ".ST_SUPPINVOICE." OR trans.type = ".ST_SUPPCREDIT.") AND trans.due_date < '" . date2sql(Today()) . "') AS OverDue,
-       (ABS(trans.ov_amount + trans.ov_gst  + trans.ov_discount - trans.alloc) <= 0.005) AS Settled
-       FROM ".TB_PREF."supp_trans as trans, ".TB_PREF."suppliers as supplier
-       WHERE supplier.supplier_id = trans.supplier_id
-       AND trans.tran_date >= '$date_after'
-       AND trans.tran_date <= '$date_to'
-               AND trans.ov_amount != 0";      // exclude voided transactions
-       if ($_POST['supplier_id'] != ALL_TEXT)
-               $sql .= " AND trans.supplier_id = ".db_escape($_POST['supplier_id']);
-       if (isset($_POST['filterType']) && $_POST['filterType'] != ALL_TEXT)
-       {
-               if (($_POST['filterType'] == '1')) 
-               {
-                       $sql .= " AND (trans.type = ".ST_SUPPINVOICE." OR trans.type = ".ST_BANKDEPOSIT.")";
-               } 
-               elseif (($_POST['filterType'] == '2')) 
-               {
-                       $sql .= " AND trans.type = ".ST_SUPPINVOICE." ";
-               } 
-               elseif ($_POST['filterType'] == '3') 
-               {
-                       $sql .= " AND (trans.type = ".ST_SUPPAYMENT." OR trans.type = ".ST_BANKPAYMENT.") ";
-               } 
-               elseif (($_POST['filterType'] == '4') || ($_POST['filterType'] == '5')) 
-               {
-                       $sql .= " AND trans.type = ".ST_SUPPCREDIT."  ";
-               }
-
-               if (($_POST['filterType'] == '2') || ($_POST['filterType'] == '5')) 
-               {
-                       $today =  date2sql(Today());
-                       $sql .= " AND trans.due_date < '$today' ";
-               }
-       }
+$sql = get_sql_for_supplier_inquiry();
 
 $cols = array(
                        _("Type") => array('fun'=>'systype_name', 'ord'=>''), 
index 0c94f251deb9a3e4ae8c82a6e53630df2a4e4f90..f5f9445ee1c0ed433ef495547d84516b3f1f5b87 100644 (file)
@@ -57,32 +57,12 @@ if (isset($_POST['submit']))
 
                if (!$new_supplier) 
                {
-
-                       $sql = "UPDATE ".TB_PREF."suppliers SET supp_name=".db_escape($_POST['supp_name']) . ",
-                               supp_ref=".db_escape($_POST['supp_ref']) . ",
-                address=".db_escape($_POST['address']) . ",
-                supp_address=".db_escape($_POST['supp_address']) . ",
-                phone=".db_escape($_POST['phone']) . ",
-                phone2=".db_escape($_POST['phone2']) . ",
-                fax=".db_escape($_POST['fax']) . ",
-                gst_no=".db_escape($_POST['gst_no']) . ",
-                email=".db_escape($_POST['email']) . ",
-                website=".db_escape($_POST['website']) . ",
-                contact=".db_escape($_POST['contact']) . ",
-                supp_account_no=".db_escape($_POST['supp_account_no']) . ",
-                bank_account=".db_escape($_POST['bank_account']) . ",
-                credit_limit=".input_num('credit_limit', 0) . ",
-                dimension_id=".db_escape($_POST['dimension_id']) . ",
-                dimension2_id=".db_escape($_POST['dimension2_id']) . ",
-                curr_code=".db_escape($_POST['curr_code']).",
-                payment_terms=".db_escape($_POST['payment_terms']) . ",
-                               payable_account=".db_escape($_POST['payable_account']) . ",
-                               purchase_account=".db_escape($_POST['purchase_account']) . ",
-                               payment_discount_account=".db_escape($_POST['payment_discount_account']) . ",
-                notes=".db_escape($_POST['notes']) . ",
-                               tax_group_id=".db_escape($_POST['tax_group_id']) . " WHERE supplier_id = ".db_escape($_POST['supplier_id']);
-
-                       db_query($sql,"The supplier could not be updated");
+                       update_supplier($_POST['supplier_id'], $_POST['supp_name'], $_POST['supp_ref'], $_POST['address'],
+                               $_POST['supp_address'], $_POST['phone'], $_POST['phone2'], $_POST['fax'], $_POST['gst_no'],
+                               $_POST['email'], $_POST['website'], $_POST['contact'], $_POST['supp_account_no'], $_POST['bank_account'], 
+                               input_num('credit_limit', 0), $_POST['dimension_id'], $_POST['dimension2_id'], $_POST['curr_code'],
+                               $_POST['payment_terms'], $_POST['payable_account'], $_POST['purchase_account'], $_POST['payment_discount_account'],
+                               $_POST['notes'], $_POST['tax_group_id']);
                        update_record_status($_POST['supplier_id'], $_POST['inactive'],
                                'suppliers', 'supplier_id');
 
@@ -91,35 +71,13 @@ if (isset($_POST['submit']))
                } 
                else 
                {
+                       add_supplier($_POST['supp_name'], $_POST['supp_ref'], $_POST['address'], $_POST['supp_address'],
+                               $_POST['phone'], $_POST['phone2'], $_POST['fax'], $_POST['gst_no'], $_POST['email'],
+                               $_POST['website'], $_POST['contact'], $_POST['supp_account_no'], $_POST['bank_account'], 
+                               input_num('credit_limit',0), $_POST['dimension_id'], $_POST['dimension2_id'],
+                               $_POST['curr_code'], $_POST['payment_terms'], $_POST['payable_account'], $_POST['purchase_account'],
+                               $_POST['payment_discount_account'], $_POST['notes'], $_POST['tax_group_id']);
 
-                       $sql = "INSERT INTO ".TB_PREF."suppliers (supp_name, supp_ref, address, supp_address, phone, phone2, fax, gst_no, email, website,
-                               contact, supp_account_no, bank_account, credit_limit, dimension_id, dimension2_id, curr_code,
-                               payment_terms, payable_account, purchase_account, payment_discount_account, notes, tax_group_id)
-                               VALUES (".db_escape($_POST['supp_name']). ", "
-                               .db_escape($_POST['supp_ref']). ", "
-                               .db_escape($_POST['address']) . ", "
-                               .db_escape($_POST['supp_address']) . ", "
-                               .db_escape($_POST['phone']). ", "
-                               .db_escape($_POST['phone2']). ", "
-                               .db_escape($_POST['fax']). ", "
-                               .db_escape($_POST['gst_no']). ", "
-                               .db_escape($_POST['email']). ", "
-                               .db_escape($_POST['website']). ", "
-                               .db_escape($_POST['contact']). ", "
-                               .db_escape($_POST['supp_account_no']). ", "
-                               .db_escape($_POST['bank_account']). ", "
-                               .input_num('credit_limit',0). ", "
-                               .db_escape($_POST['dimension_id']). ", "
-                               .db_escape($_POST['dimension2_id']). ", "
-                               .db_escape($_POST['curr_code']). ", "
-                               .db_escape($_POST['payment_terms']). ", "
-                               .db_escape($_POST['payable_account']). ", "
-                               .db_escape($_POST['purchase_account']). ", "
-                               .db_escape($_POST['payment_discount_account']). ", "
-                               .db_escape($_POST['notes']). ", "
-                               .db_escape($_POST['tax_group_id']). ")";
-
-                       db_query($sql,"The supplier could not be added");
                        $_POST['supplier_id'] = db_insert_id();
                        $new_supplier = false;
                        display_notification(_("A new supplier has been added."));
@@ -136,10 +94,7 @@ elseif (isset($_POST['delete']) && $_POST['delete'] != "")
 
        // PREVENT DELETES IF DEPENDENT RECORDS IN 'supp_trans' , purch_orders
 
-       $sql= "SELECT COUNT(*) FROM ".TB_PREF."supp_trans WHERE supplier_id=".db_escape($_POST['supplier_id']);
-       $result = db_query($sql,"check failed");
-       $myrow = db_fetch_row($result);
-       if ($myrow[0] > 0) 
+       if (key_in_foreign_table($_POST['supplier_id'], 'supp_trans', 'supplier_id'))
        {
                $cancel_delete = 1;
                display_error(_("Cannot delete this supplier because there are transactions that refer to this supplier."));
@@ -147,10 +102,7 @@ elseif (isset($_POST['delete']) && $_POST['delete'] != "")
        } 
        else 
        {
-               $sql= "SELECT COUNT(*) FROM ".TB_PREF."purch_orders WHERE supplier_id=".db_escape($_POST['supplier_id']);
-               $result = db_query($sql,"check failed");
-               $myrow = db_fetch_row($result);
-               if ($myrow[0] > 0) 
+               if (key_in_foreign_table($_POST['supplier_id'], 'purch_orders', 'supplier_id'))
                {
                        $cancel_delete = 1;
                        display_error(_("Cannot delete the supplier record because purchase orders have been created against this supplier."));
@@ -159,8 +111,7 @@ elseif (isset($_POST['delete']) && $_POST['delete'] != "")
        }
        if ($cancel_delete == 0) 
        {
-               $sql="DELETE FROM ".TB_PREF."suppliers WHERE supplier_id=".db_escape($_POST['supplier_id']);
-               db_query($sql,"check failed");
+               delete_supplier($_POST['supplier_id']);
 
                unset($_SESSION['supplier_id']);
                $new_supplier = true;
index 988c90faf3a1b15af98e82d0c8eb5af3af9484cb..a09b2e14d74f8ff9158c4ccc912c08b944bdd7a2 100644 (file)
@@ -227,10 +227,7 @@ function handle_add_new_item()
 
                if ($allow_update == true)
                {
-                       $sql = "SELECT description, units, mb_flag
-                               FROM ".TB_PREF."stock_master WHERE stock_id = ".db_escape($_POST['stock_id']);
-
-                   $result = db_query($sql,"The stock details for " . $_POST['stock_id'] . " could not be retrieved");
+                       $result = get_short_info($_POST['stock_id']);
 
                    if (db_num_rows($result) == 0)
                    {
index bfb8371cc40a93de19563aa1d8be779002e6c0fe..9ac5becd5b14c0c0a4f99c92df5bb1e7856936a1 100644 (file)
@@ -119,13 +119,7 @@ function check_po_changed()
        /*Now need to check that the order details are the same as they were when they were read into the Items array. If they've changed then someone else must have altered them */
        // Sherifoz 22.06.03 Compare against COMPLETED items only !!
        // Otherwise if you try to fullfill item quantities separately will give error.
-       $sql = "SELECT item_code, quantity_ordered, quantity_received, qty_invoiced
-               FROM ".TB_PREF."purch_order_details
-               WHERE order_no=".db_escape($_SESSION['PO']->order_no)
-               ." ORDER BY po_detail_item";
-
-       $result = db_query($sql, "could not query purch order details");
-    check_db_error("Could not check that the details of the purchase order had not been changed by another user ", $sql);
+       $result = get_po_items($_SESSION['PO']->order_no);
 
        $line_no = 1;
        while ($myrow = db_fetch($result))
index 1a8d63e9bdca091b7b481876a72b7e6d01d80baa..69ead86d7e8ea2c6c2b6c77577a74f4d1651c66a 100644 (file)
@@ -97,8 +97,7 @@ if (isset($_POST['AddGLCodeToTrans'])){
        $Ajax->activate('gl_items');
        $input_error = false;
 
-       $sql = "SELECT account_code, account_name FROM ".TB_PREF."chart_master WHERE account_code=".db_escape($_POST['gl_code']);
-       $result = db_query($sql,"get account information");
+       $result = get_gl_account_info($_POST['gl_code']);
        if (db_num_rows($result) == 0)
        {
                display_error(_("The account code entered is not a valid code, this line cannot be added to the transaction."));
index 3440678189c5c6a8f76c1007840e1851d0f76bb6..6b24fe279700c587cfd3031cb5a1666ee0ad85d6 100644 (file)
@@ -99,8 +99,7 @@ if (isset($_POST['AddGLCodeToTrans'])){
        $Ajax->activate('gl_items');
        $input_error = false;
 
-       $sql = "SELECT account_code, account_name FROM ".TB_PREF."chart_master WHERE account_code=".db_escape($_POST['gl_code']);
-       $result = db_query($sql,"get account information");
+       $result = get_gl_account_info($_POST['gl_code']);
        if (db_num_rows($result) == 0)
        {
                display_error(_("The account code entered is not a valid code, this line cannot be added to the transaction."));
@@ -186,15 +185,7 @@ function check_data()
                return false;
        }
 
-       $sql = "SELECT Count(*) FROM ".TB_PREF."supp_trans WHERE supplier_id="
-               .db_escape($_SESSION['supp_trans']->supplier_id) . " AND supp_reference=" 
-               .db_escape( $_POST['supp_reference']) 
-               . " AND ov_amount!=0"; // ignore voided invoice references
-
-       $result=db_query($sql,"The sql to check for the previous entry of the same invoice failed");
-
-       $myrow = db_fetch_row($result);
-       if ($myrow[0] == 1)
+       if (is_reference_already_there($_SESSION['supp_trans']->supplier_id, $_POST['supp_reference']))
        {       /*Transaction reference already entered */
                display_error(_("This invoice number has already been entered. It cannot be entered again." . " (" . $_POST['supp_reference'] . ")"));
                return false;
@@ -342,27 +333,7 @@ if ($_SESSION["wa_current_user"]->can_access('SA_GRNDELETE'))
        $id2 = find_submit('void_item_id');
        if ($id2 != -1) 
        {
-               begin_transaction();
-               
-               $myrow = get_grn_item_detail($id2);
-
-               $grn = get_grn_batch($myrow['grn_batch_id']);
-
-           $sql = "UPDATE ".TB_PREF."purch_order_details
-                       SET quantity_received = qty_invoiced, quantity_ordered = qty_invoiced WHERE po_detail_item = ".$myrow["po_detail_item"];
-           db_query($sql, "The quantity invoiced of the purchase order line could not be updated");
-
-           $sql = "UPDATE ".TB_PREF."grn_items
-               SET qty_recd = quantity_inv WHERE id = ".$myrow["id"];
-               db_query($sql, "The quantity invoiced off the items received record could not be updated");
-       
-               update_average_material_cost($grn["supplier_id"], $myrow["item_code"],
-                       $myrow["unit_price"], -$myrow["QtyOstdg"], Today());
-
-               add_stock_move(ST_SUPPRECEIVE, $myrow["item_code"], $myrow['grn_batch_id'], $grn['loc_code'], sql2date($grn["delivery_date"]), "",
-                       -$myrow["QtyOstdg"], $myrow['std_cost_unit'], $grn["supplier_id"], 1, $myrow['unit_price']);
-                       
-               commit_transaction();
+               remove_not_invoice_item($id2);
                display_notification(sprintf(_('All yet non-invoiced items on delivery line # %d has been removed.'), $id2));
 
        }