! -> 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
//--------------------------------------------------------------------------------------
+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,
//--------------------------------------------------------------------------------------
+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);
//--------------------------------------------------------------------------------------
+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
+
?>
}
}
+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
"/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'),
//--------------------------------------------------------------------------------------------------
-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"),'','');
} //END WHILE LIST LOOP
end_table();
-div_end();
+ div_end();
}
//--------------------------------------------------------------------------------------------------
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';
}
{
/*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';
}
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';
{
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);
}
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;
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'),
$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');
}
//----------------------------------------------------------------------------------------
+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();
+}
?>
//----------------------------------------------------------------------------------------
+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
//----------------------------------------------------------------------------------------
+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
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
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)
{
}
//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(
//---------------------------------------------------------------------------------------------
-$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'=>''),
}
//------------------------------------------------------------------------------------------------
- $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'),
}
//------------------------------------------------------------------------------------------------
- $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'=>''),
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');
}
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."));
// 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."));
}
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."));
}
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;
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)
{
/*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))
$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."));
$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."));
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;
$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));
}