X-Git-Url: https://delta.frontaccounting.com/gitweb/?a=blobdiff_plain;f=purchasing%2Fincludes%2Fdb%2Fpo_db.inc;h=41ed94f09872aa42921849ad7126c6ab85f339e3;hb=40ee20a833a502a0914202b56473fb330d5774cf;hp=cb2a1e49cfeca3cd93e2a8af32575a2f5023e415;hpb=5928cc0bb4164cfd1dfc77f279f6b12d7806294f;p=fa-stable.git diff --git a/purchasing/includes/db/po_db.inc b/purchasing/includes/db/po_db.inc index cb2a1e49..41ed94f0 100644 --- a/purchasing/includes/db/po_db.inc +++ b/purchasing/includes/db/po_db.inc @@ -1,13 +1,22 @@ . +***********************************************************************/ //---------------------------------------------------------------------------------------- function delete_po($po) { - $sql = "DELETE FROM ".TB_PREF."purch_orders WHERE order_no=" . $po; + $sql = "DELETE FROM ".TB_PREF."purch_orders WHERE order_no=".db_escape($po); db_query($sql, "The order header could not be deleted"); - $sql = "DELETE FROM ".TB_PREF."purch_order_details WHERE order_no =" . $po; + $sql = "DELETE FROM ".TB_PREF."purch_order_details WHERE order_no =".db_escape($po); db_query($sql, "The order detail lines could not be deleted"); } @@ -15,17 +24,20 @@ function delete_po($po) function add_po(&$po_obj) { + global $Refs; + begin_transaction(); /*Insert to purchase order header record */ - $sql = "INSERT INTO ".TB_PREF."purch_orders (supplier_id, Comments, ord_date, reference, requisition_no, into_stock_location, delivery_address) VALUES("; + $sql = "INSERT INTO ".TB_PREF."purch_orders (supplier_id, Comments, ord_date, reference, requisition_no, into_stock_location, delivery_address, total) VALUES("; $sql .= db_escape($po_obj->supplier_id) . "," . db_escape($po_obj->Comments) . ",'" . - date2sql($po_obj->orig_order_date) . "', '" . - $po_obj->reference . "', " . - db_escape($po_obj->requisition_no) . ", " . + date2sql($po_obj->orig_order_date) . "', " . + db_escape($po_obj->reference) . ", " . + db_escape($po_obj->supp_ref) . ", " . db_escape($po_obj->Location) . ", " . - db_escape($po_obj->delivery_address) . ")"; + db_escape($po_obj->delivery_address) . ", " . + db_escape($po_obj->get_trans_total()) . ")"; db_query($sql, "The purchase order header record could not be inserted"); @@ -33,24 +45,23 @@ function add_po(&$po_obj) $po_obj->order_no = db_insert_id(); /*Insert the purchase order detail records */ - foreach ($po_obj->line_items as $po_line) + foreach ($po_obj->line_items as $line_no => $po_line) { - if ($po_line->Deleted == false) - { - $sql = "INSERT INTO ".TB_PREF."purch_order_details (order_no, item_code, description, delivery_date, unit_price, quantity_ordered) VALUES ("; - $sql .= $po_obj->order_no . ", " . db_escape($po_line->stock_id). "," . - db_escape($po_line->item_description). ",'" . - date2sql($po_line->req_del_date) . "'," . - $po_line->price . ", " . - $po_line->quantity . ")"; - db_query($sql, "One of the purchase order detail records could not be inserted"); - } + $sql = "INSERT INTO ".TB_PREF."purch_order_details (order_no, item_code, description, delivery_date, unit_price, quantity_ordered) VALUES ("; + $sql .= $po_obj->order_no . ", " . db_escape($po_line->stock_id). "," . + db_escape($po_line->item_description). ",'" . + date2sql($po_line->req_del_date) . "'," . + db_escape($po_line->price) . ", " . + db_escape($po_line->quantity). ")"; + db_query($sql, "One of the purchase order detail records could not be inserted"); + $po_obj->line_items[$line_no]->po_detail_rec = db_insert_id(); } - references::save_last($po_obj->reference, systypes::po()); + $Refs->save(ST_PURCHORDER, $po_obj->order_no, $po_obj->reference); - //add_comments(systypes::po(), $po_obj->order_no, $po_obj->orig_order_date, $po_obj->Comments); + //add_comments(ST_PURCHORDER, $po_obj->order_no, $po_obj->orig_order_date, $po_obj->Comments); + add_audit_trail(ST_PURCHORDER, $po_obj->order_no, $po_obj->orig_order_date); commit_transaction(); return $po_obj->order_no; @@ -64,50 +75,37 @@ function update_po(&$po_obj) /*Update the purchase order header with any changes */ $sql = "UPDATE ".TB_PREF."purch_orders SET Comments=" . db_escape($po_obj->Comments) . ", - requisition_no= ". db_escape( $po_obj->requisition_no). ", + requisition_no= ". db_escape( $po_obj->supp_ref). ", into_stock_location=" . db_escape($po_obj->Location). ", ord_date='" . date2sql($po_obj->orig_order_date) . "', - delivery_address=" . db_escape($po_obj->delivery_address); + delivery_address=" . db_escape($po_obj->delivery_address).", + total=". db_escape($po_obj->get_trans_total()); $sql .= " WHERE order_no = " . $po_obj->order_no; db_query($sql, "The purchase order could not be updated"); + $sql = "DELETE FROM ".TB_PREF."purch_order_details WHERE order_no=" + .db_escape($po_obj->order_no); + db_query($sql, "could not delete old purch order details"); + /*Now Update the purchase order detail records */ foreach ($po_obj->line_items as $po_line) { - - if ($po_line->Deleted==True) - { - // Sherifoz 21.06.03 Handle deleting existing lines - if ($po_line->po_detail_rec!='') - { - $sql = "DELETE FROM ".TB_PREF."purch_order_details WHERE po_detail_item='" . $po_line->po_detail_rec . "'"; - db_query($sql, "could not query purch order details"); - } - } - else if ($po_line->po_detail_rec == '') - { - // Sherifoz 21.06.03 Handle adding new lines vs. updating. if no key(po_detail_rec) then it's a new line - $sql = "INSERT INTO ".TB_PREF."purch_order_details (order_no, item_code, description, delivery_date, unit_price, quantity_ordered) VALUES ("; - $sql .= $po_obj->order_no . "," . - db_escape($po_line->stock_id). "," . - db_escape($po_line->item_description). ",'" . - date2sql($po_line->req_del_date) . "'," . - $po_line->price . ", " . $po_line->quantity . ")"; - } - else - { - $sql = "UPDATE ".TB_PREF."purch_order_details SET item_code='" . $po_line->stock_id . "', - description =" . db_escape($po_line->item_description). ", - delivery_date ='" . date2sql($po_line->req_del_date) . "', - unit_price=" . $po_line->price . ", - quantity_ordered=" . $po_line->quantity . " - WHERE po_detail_item=" . $po_line->po_detail_rec; - } + $sql = "INSERT INTO ".TB_PREF."purch_order_details (po_detail_item, order_no, item_code, + description, delivery_date, unit_price, quantity_ordered) VALUES (" + .db_escape($po_line->po_detail_rec ? $po_line->po_detail_rec : 0). "," + .$po_obj->order_no . "," + .db_escape($po_line->stock_id). "," + .db_escape($po_line->item_description). ",'" + .date2sql($po_line->req_del_date) . "'," + .db_escape($po_line->price) . ", " + .db_escape($po_line->quantity) . ")"; + db_query($sql, "One of the purchase order detail records could not be updated"); } - //add_comments(systypes::po(), $po_obj->order_no, $po_obj->orig_order_date, $po_obj->Comments); + // add_comments(ST_PURCHORDER, $po_obj->order_no, $po_obj->orig_order_date, $po_obj->Comments); + add_audit_trail($po_obj->trans_type, $po_obj->order_no, Today(), _("Updated.")); commit_transaction(); return $po_obj->order_no; @@ -117,15 +115,15 @@ function update_po(&$po_obj) function read_po_header($order_no, &$order) { - $sql = "SELECT ".TB_PREF."purch_orders.*, ".TB_PREF."suppliers.supp_name, + $sql = "SELECT ".TB_PREF."purch_orders.*, ".TB_PREF."suppliers.supp_name, ".TB_PREF."suppliers.tax_group_id, ".TB_PREF."suppliers.curr_code, ".TB_PREF."locations.location_name FROM ".TB_PREF."purch_orders, ".TB_PREF."suppliers, ".TB_PREF."locations WHERE ".TB_PREF."purch_orders.supplier_id = ".TB_PREF."suppliers.supplier_id AND ".TB_PREF."locations.loc_code = into_stock_location - AND ".TB_PREF."purch_orders.order_no = " . $order_no; + AND ".TB_PREF."purch_orders.order_no = ".db_escape($order_no); $result = db_query($sql, "The order cannot be retrieved"); - +_vd($sql); if (db_num_rows($result) == 1) { @@ -135,11 +133,13 @@ function read_po_header($order_no, &$order) $order->supplier_id = $myrow["supplier_id"]; $order->supplier_name = $myrow["supp_name"]; $order->curr_code = $myrow["curr_code"]; + $order->tax_group_id = $myrow['tax_group_id']; + $order->credit = get_current_supp_credit($order->supplier_id); $order->orig_order_date = sql2date($myrow["ord_date"]); $order->Comments = $myrow["comments"]; $order->Location = $myrow["into_stock_location"]; - $order->requisition_no = $myrow["requisition_no"]; + $order->supp_ref = $myrow["requisition_no"]; $order->reference = $myrow["reference"]; $order->delivery_address = $myrow["delivery_address"]; @@ -160,7 +160,7 @@ function read_po_items($order_no, &$order, $open_items_only=false) FROM ".TB_PREF."purch_order_details LEFT JOIN ".TB_PREF."stock_master ON ".TB_PREF."purch_order_details.item_code=".TB_PREF."stock_master.stock_id - WHERE order_no =$order_no "; + WHERE order_no =".db_escape($order_no); if ($open_items_only) $sql .= " AND (".TB_PREF."purch_order_details.quantity_ordered > ".TB_PREF."purch_order_details.quantity_received) "; @@ -171,9 +171,16 @@ function read_po_items($order_no, &$order, $open_items_only=false) if (db_num_rows($result) > 0) { - while ($myrow = db_fetch($result)) { + $data = get_purchase_data($order->supplier_id, $myrow['item_code']); + if ($data !== false) + { + if ($data['supplier_description'] != "") + $myrow['description'] = $data['supplier_description']; + //if ($data['suppliers_uom'] != "") + // $myrow['units'] = $data['suppliers_uom']; + } if (is_null($myrow["units"])) { $units = ""; @@ -183,13 +190,16 @@ function read_po_items($order_no, &$order, $open_items_only=false) $units = $myrow["units"]; } - $order->add_to_order($order->lines_on_order+1, $myrow["item_code"], + if ($order->add_to_order($order->lines_on_order, $myrow["item_code"], $myrow["quantity_ordered"],$myrow["description"], $myrow["unit_price"],$units, sql2date($myrow["delivery_date"]), - $myrow["qty_invoiced"], $myrow["quantity_received"]); - - $order->line_items[$order->lines_on_order]->po_detail_rec = $myrow["po_detail_item"]; - $order->line_items[$order->lines_on_order]->standard_cost = $myrow["std_cost_unit"]; /*Needed for receiving goods and GL interface */ + $myrow["qty_invoiced"], $myrow["quantity_received"])) { + $newline = &$order->line_items[$order->lines_on_order-1]; + $newline->po_detail_rec = $myrow["po_detail_item"]; + $newline->standard_cost = $myrow["std_cost_unit"]; /*Needed for receiving goods and GL interface */ + // set for later GRN edition +// $newline->receive_qty = $newline->quantity - $newline->qty_dispatched; + } } /* line po from purchase order details */ } //end of checks on returned data set } @@ -206,5 +216,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