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 add_forms_for_sys_type(systypes::po(), $po_obj->order_no);
52 references::save_last($po_obj->reference, systypes::po());
54 //add_comments(systypes::po(), $po_obj->order_no, $po_obj->orig_order_date, $po_obj->Comments);
58 return $po_obj->order_no;
61 //----------------------------------------------------------------------------------------
63 function update_po(&$po_obj)
67 /*Update the purchase order header with any changes */
68 $sql = "UPDATE ".TB_PREF."purch_orders SET Comments='" . db_escape($po_obj->Comments) . "',
69 requisition_no= '" . $po_obj->requisition_no . "',
70 into_stock_location='" . $po_obj->Location . "',
71 ord_date='" . date2sql($po_obj->orig_order_date) . "',
72 delivery_address='" . $po_obj->delivery_address . "'";
73 $sql .= " WHERE order_no = " . $po_obj->order_no;
74 db_query($sql, "The purchase order could not be updated");
76 /*Now Update the purchase order detail records */
77 foreach ($po_obj->line_items as $po_line)
80 if ($po_line->Deleted==True)
82 // Sherifoz 21.06.03 Handle deleting existing lines
83 if ($po_line->po_detail_rec!='')
85 $sql = "DELETE FROM ".TB_PREF."purch_order_details WHERE po_detail_item='" . $po_line->po_detail_rec . "'";
86 db_query($sql, "could not query purch order details");
89 else if ($po_line->po_detail_rec == '')
91 // Sherifoz 21.06.03 Handle adding new lines vs. updating. if no key(po_detail_rec) then it's a new line
92 $sql = "INSERT INTO ".TB_PREF."purch_order_details (order_no, item_code, description, delivery_date, unit_price, quantity_ordered) VALUES (";
93 $sql .= $po_obj->order_no . ", '" .
94 $po_line->stock_id . "','" .
95 $po_line->item_description . "','" .
96 date2sql($po_line->req_del_date) . "'," .
97 $po_line->price . ", " . $po_line->quantity . ")";
101 $sql = "UPDATE ".TB_PREF."purch_order_details SET item_code='" . $po_line->stock_id . "',
102 description ='" . $po_line->item_description . "',
103 delivery_date ='" . date2sql($po_line->req_del_date) . "',
104 unit_price=" . $po_line->price . ",
105 quantity_ordered=" . $po_line->quantity . "
106 WHERE po_detail_item=" . $po_line->po_detail_rec;
108 db_query($sql, "One of the purchase order detail records could not be updated");
111 //add_comments(systypes::po(), $po_obj->order_no, $po_obj->orig_order_date, $po_obj->Comments);
113 commit_transaction();
115 return $po_obj->order_no;
118 //----------------------------------------------------------------------------------------
120 function read_po_header($order_no, &$order)
122 $sql = "SELECT ".TB_PREF."purch_orders.*, ".TB_PREF."suppliers.supp_name,
123 ".TB_PREF."suppliers.curr_code, ".TB_PREF."locations.location_name
124 FROM ".TB_PREF."purch_orders, ".TB_PREF."suppliers, ".TB_PREF."locations
125 WHERE ".TB_PREF."purch_orders.supplier_id = ".TB_PREF."suppliers.supplier_id
126 AND ".TB_PREF."locations.loc_code = into_stock_location
127 AND ".TB_PREF."purch_orders.order_no = " . $order_no;
129 $result = db_query($sql, "The order cannot be retrieved");
131 if (db_num_rows($result) == 1)
134 $myrow = db_fetch($result);
136 $order->order_no = $order_no;
137 $order->supplier_id = $myrow["supplier_id"];
138 $order->supplier_name = $myrow["supp_name"];
139 $order->curr_code = $myrow["curr_code"];
141 $order->orig_order_date = sql2date($myrow["ord_date"]);
142 $order->Comments = $myrow["comments"];
143 $order->Location = $myrow["into_stock_location"];
144 $order->requisition_no = $myrow["requisition_no"];
145 $order->reference = $myrow["reference"];
146 $order->delivery_address = $myrow["delivery_address"];
151 display_db_error("FATAL : duplicate purchase order found", "", true);
155 //----------------------------------------------------------------------------------------
157 function read_po_items($order_no, &$order, $open_items_only=false)
159 /*now populate the line po array with the purchase order details records */
161 $sql = "SELECT ".TB_PREF."purch_order_details.*, units
162 FROM ".TB_PREF."purch_order_details
163 LEFT JOIN ".TB_PREF."stock_master
164 ON ".TB_PREF."purch_order_details.item_code=".TB_PREF."stock_master.stock_id
165 WHERE order_no =$order_no ";
167 if ($open_items_only)
168 $sql .= " AND (".TB_PREF."purch_order_details.quantity_ordered > ".TB_PREF."purch_order_details.quantity_received) ";
170 $sql .= " ORDER BY po_detail_item";
172 $result = db_query($sql, "The lines on the purchase order cannot be retrieved");
174 if (db_num_rows($result) > 0)
177 while ($myrow = db_fetch($result))
179 if (is_null($myrow["units"]))
185 $units = $myrow["units"];
188 $order->add_to_order($order->lines_on_order+1, $myrow["item_code"],
189 $myrow["quantity_ordered"],$myrow["description"],
190 $myrow["unit_price"],$units, sql2date($myrow["delivery_date"]),
191 $myrow["qty_invoiced"], $myrow["quantity_received"]);
193 $order->line_items[$order->lines_on_order]->po_detail_rec = $myrow["po_detail_item"];
194 $order->line_items[$order->lines_on_order]->standard_cost = $myrow["std_cost_unit"]; /*Needed for receiving goods and GL interface */
195 } /* line po from purchase order details */
196 } //end of checks on returned data set
199 //----------------------------------------------------------------------------------------
201 function read_po($order_no, &$order, $open_items_only=false)
203 $result = read_po_header($order_no, $order);
206 read_po_items($order_no, $order, $open_items_only);
209 //----------------------------------------------------------------------------------------