3 //----------------------------------------------------------------------------------------
5 function delete_po($po)
7 $sql = "DELETE FROM ".TB_PREF."purch_orders WHERE order_no=" . $po;
8 db_query($sql, "The order header could not be deleted");
10 $sql = "DELETE FROM ".TB_PREF."purch_order_details WHERE order_no =" . $po;
11 db_query($sql, "The order detail lines could not be deleted");
14 //----------------------------------------------------------------------------------------
16 function add_po(&$po_obj)
20 /*Insert to purchase order header record */
21 $sql = "INSERT INTO ".TB_PREF."purch_orders (supplier_id, Comments, ord_date, reference, requisition_no, into_stock_location, delivery_address) VALUES(";
22 $sql .= "'" . $po_obj->supplier_id . "', '" .
23 db_escape($po_obj->Comments) . "','" .
24 date2sql($po_obj->orig_order_date) . "', '" .
25 $po_obj->reference . "', '" .
26 $po_obj->requisition_no . "', '" .
27 $po_obj->Location . "', '" .
28 $po_obj->delivery_address . "')";
30 db_query($sql, "The purchase order header record could not be inserted");
32 /*Get the auto increment value of the order number created from the sql above */
33 $po_obj->order_no = db_insert_id();
35 /*Insert the purchase order detail records */
36 foreach ($po_obj->line_items as $po_line)
38 if ($po_line->Deleted == false)
40 $sql = "INSERT INTO ".TB_PREF."purch_order_details (order_no, item_code, description, delivery_date, unit_price, quantity_ordered) VALUES (";
41 $sql .= $po_obj->order_no . ", '" . $po_line->stock_id . "','" .
42 $po_line->item_description . "','" .
43 date2sql($po_line->req_del_date) . "'," .
44 $po_line->price . ", " .
45 $po_line->quantity . ")";
46 db_query($sql, "One of the purchase order detail records could not be inserted");
50 references::save_last($po_obj->reference, systypes::po());
52 //add_comments(systypes::po(), $po_obj->order_no, $po_obj->orig_order_date, $po_obj->Comments);
56 return $po_obj->order_no;
59 //----------------------------------------------------------------------------------------
61 function update_po(&$po_obj)
65 /*Update the purchase order header with any changes */
66 $sql = "UPDATE ".TB_PREF."purch_orders SET Comments='" . db_escape($po_obj->Comments) . "',
67 requisition_no= '" . $po_obj->requisition_no . "',
68 into_stock_location='" . $po_obj->Location . "',
69 ord_date='" . date2sql($po_obj->orig_order_date) . "',
70 delivery_address='" . $po_obj->delivery_address . "'";
71 $sql .= " WHERE order_no = " . $po_obj->order_no;
72 db_query($sql, "The purchase order could not be updated");
74 /*Now Update the purchase order detail records */
75 foreach ($po_obj->line_items as $po_line)
78 if ($po_line->Deleted==True)
80 // Sherifoz 21.06.03 Handle deleting existing lines
81 if ($po_line->po_detail_rec!='')
83 $sql = "DELETE FROM ".TB_PREF."purch_order_details WHERE po_detail_item='" . $po_line->po_detail_rec . "'";
84 db_query($sql, "could not query purch order details");
87 else if ($po_line->po_detail_rec == '')
89 // Sherifoz 21.06.03 Handle adding new lines vs. updating. if no key(po_detail_rec) then it's a new line
90 $sql = "INSERT INTO ".TB_PREF."purch_order_details (order_no, item_code, description, delivery_date, unit_price, quantity_ordered) VALUES (";
91 $sql .= $po_obj->order_no . ", '" .
92 $po_line->stock_id . "','" .
93 $po_line->item_description . "','" .
94 date2sql($po_line->req_del_date) . "'," .
95 $po_line->price . ", " . $po_line->quantity . ")";
99 $sql = "UPDATE ".TB_PREF."purch_order_details SET item_code='" . $po_line->stock_id . "',
100 description ='" . $po_line->item_description . "',
101 delivery_date ='" . date2sql($po_line->req_del_date) . "',
102 unit_price=" . $po_line->price . ",
103 quantity_ordered=" . $po_line->quantity . "
104 WHERE po_detail_item=" . $po_line->po_detail_rec;
106 db_query($sql, "One of the purchase order detail records could not be updated");
109 //add_comments(systypes::po(), $po_obj->order_no, $po_obj->orig_order_date, $po_obj->Comments);
111 commit_transaction();
113 return $po_obj->order_no;
116 //----------------------------------------------------------------------------------------
118 function read_po_header($order_no, &$order)
120 $sql = "SELECT ".TB_PREF."purch_orders.*, ".TB_PREF."suppliers.supp_name,
121 ".TB_PREF."suppliers.curr_code, ".TB_PREF."locations.location_name
122 FROM ".TB_PREF."purch_orders, ".TB_PREF."suppliers, ".TB_PREF."locations
123 WHERE ".TB_PREF."purch_orders.supplier_id = ".TB_PREF."suppliers.supplier_id
124 AND ".TB_PREF."locations.loc_code = into_stock_location
125 AND ".TB_PREF."purch_orders.order_no = " . $order_no;
127 $result = db_query($sql, "The order cannot be retrieved");
129 if (db_num_rows($result) == 1)
132 $myrow = db_fetch($result);
134 $order->order_no = $order_no;
135 $order->supplier_id = $myrow["supplier_id"];
136 $order->supplier_name = $myrow["supp_name"];
137 $order->curr_code = $myrow["curr_code"];
139 $order->orig_order_date = sql2date($myrow["ord_date"]);
140 $order->Comments = $myrow["comments"];
141 $order->Location = $myrow["into_stock_location"];
142 $order->requisition_no = $myrow["requisition_no"];
143 $order->reference = $myrow["reference"];
144 $order->delivery_address = $myrow["delivery_address"];
149 display_db_error("FATAL : duplicate purchase order found", "", true);
153 //----------------------------------------------------------------------------------------
155 function read_po_items($order_no, &$order, $open_items_only=false)
157 /*now populate the line po array with the purchase order details records */
159 $sql = "SELECT ".TB_PREF."purch_order_details.*, units
160 FROM ".TB_PREF."purch_order_details
161 LEFT JOIN ".TB_PREF."stock_master
162 ON ".TB_PREF."purch_order_details.item_code=".TB_PREF."stock_master.stock_id
163 WHERE order_no =$order_no ";
165 if ($open_items_only)
166 $sql .= " AND (".TB_PREF."purch_order_details.quantity_ordered > ".TB_PREF."purch_order_details.quantity_received) ";
168 $sql .= " ORDER BY po_detail_item";
170 $result = db_query($sql, "The lines on the purchase order cannot be retrieved");
172 if (db_num_rows($result) > 0)
175 while ($myrow = db_fetch($result))
177 if (is_null($myrow["units"]))
183 $units = $myrow["units"];
186 $order->add_to_order($order->lines_on_order+1, $myrow["item_code"],
187 $myrow["quantity_ordered"],$myrow["description"],
188 $myrow["unit_price"],$units, sql2date($myrow["delivery_date"]),
189 $myrow["qty_invoiced"], $myrow["quantity_received"]);
191 $order->line_items[$order->lines_on_order]->po_detail_rec = $myrow["po_detail_item"];
192 $order->line_items[$order->lines_on_order]->standard_cost = $myrow["std_cost_unit"]; /*Needed for receiving goods and GL interface */
193 } /* line po from purchase order details */
194 } //end of checks on returned data set
197 //----------------------------------------------------------------------------------------
199 function read_po($order_no, &$order, $open_items_only=false)
201 $result = read_po_header($order_no, $order);
204 read_po_items($order_no, $order, $open_items_only);
207 //----------------------------------------------------------------------------------------