2 /**********************************************************************
3 Copyright (C) FrontAccounting, LLC.
4 Released under the terms of the GNU General Public License, GPL,
5 as published by the Free Software Foundation, either version 3
6 of the License, or (at your option) any later version.
7 This program is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
11 ***********************************************************************/
12 //----------------------------------------------------------------------------------------
14 function delete_po($po)
16 $sql = "DELETE FROM ".TB_PREF."purch_orders WHERE order_no=".db_escape($po);
17 db_query($sql, "The order header could not be deleted");
19 $sql = "DELETE FROM ".TB_PREF."purch_order_details WHERE order_no =".db_escape($po);
20 db_query($sql, "The order detail lines could not be deleted");
23 //----------------------------------------------------------------------------------------
25 function add_po(&$po_obj)
31 /*Insert to purchase order header record */
32 $sql = "INSERT INTO ".TB_PREF."purch_orders (supplier_id, Comments, ord_date, reference, requisition_no, into_stock_location, delivery_address) VALUES(";
33 $sql .= db_escape($po_obj->supplier_id) . "," .
34 db_escape($po_obj->Comments) . ",'" .
35 date2sql($po_obj->orig_order_date) . "', " .
36 db_escape($po_obj->reference) . ", " .
37 db_escape($po_obj->requisition_no) . ", " .
38 db_escape($po_obj->Location) . ", " .
39 db_escape($po_obj->delivery_address) . ")";
41 db_query($sql, "The purchase order header record could not be inserted");
43 /*Get the auto increment value of the order number created from the sql above */
44 $po_obj->order_no = db_insert_id();
46 /*Insert the purchase order detail records */
47 foreach ($po_obj->line_items as $po_line)
49 $sql = "INSERT INTO ".TB_PREF."purch_order_details (order_no, item_code, description, delivery_date, unit_price, quantity_ordered) VALUES (";
50 $sql .= $po_obj->order_no . ", " . db_escape($po_line->stock_id). "," .
51 db_escape($po_line->item_description). ",'" .
52 date2sql($po_line->req_del_date) . "'," .
53 db_escape($po_line->price) . ", " .
54 db_escape($po_line->quantity). ")";
55 db_query($sql, "One of the purchase order detail records could not be inserted");
58 $Refs->save(ST_PURCHORDER, $po_obj->order_no, $po_obj->reference);
60 //add_comments(ST_PURCHORDER, $po_obj->order_no, $po_obj->orig_order_date, $po_obj->Comments);
62 add_audit_trail(ST_PURCHORDER, $po_obj->order_no, $po_obj->orig_order_date);
65 return $po_obj->order_no;
68 //----------------------------------------------------------------------------------------
70 function update_po(&$po_obj)
74 /*Update the purchase order header with any changes */
75 $sql = "UPDATE ".TB_PREF."purch_orders SET Comments=" . db_escape($po_obj->Comments) . ",
76 requisition_no= ". db_escape( $po_obj->requisition_no). ",
77 into_stock_location=" . db_escape($po_obj->Location). ",
78 ord_date='" . date2sql($po_obj->orig_order_date) . "',
79 delivery_address=" . db_escape($po_obj->delivery_address);
80 $sql .= " WHERE order_no = " . $po_obj->order_no;
81 db_query($sql, "The purchase order could not be updated");
83 $sql = "DELETE FROM ".TB_PREF."purch_order_details WHERE order_no="
84 .db_escape($po_obj->order_no);
85 db_query($sql, "could not delete old purch order details");
87 /*Now Update the purchase order detail records */
88 foreach ($po_obj->line_items as $po_line)
90 $sql = "INSERT INTO ".TB_PREF."purch_order_details (po_detail_item, order_no, item_code,
91 description, delivery_date, unit_price, quantity_ordered) VALUES ("
92 .db_escape($po_line->po_detail_rec ? $po_line->po_detail_rec : 0). ","
93 .$po_obj->order_no . ","
94 .db_escape($po_line->stock_id). ","
95 .db_escape($po_line->item_description). ",'"
96 .date2sql($po_line->req_del_date) . "',"
97 .db_escape($po_line->price) . ", "
98 .db_escape($po_line->quantity) . ")";
100 db_query($sql, "One of the purchase order detail records could not be updated");
103 // add_comments(ST_PURCHORDER, $po_obj->order_no, $po_obj->orig_order_date, $po_obj->Comments);
105 add_audit_trail($po_obj->trans_type, $po_obj->order_no, $po_obj->document_date, _("Updated."));
106 commit_transaction();
108 return $po_obj->order_no;
111 //----------------------------------------------------------------------------------------
113 function read_po_header($order_no, &$order)
115 $sql = "SELECT ".TB_PREF."purch_orders.*, ".TB_PREF."suppliers.supp_name,
116 ".TB_PREF."suppliers.curr_code, ".TB_PREF."locations.location_name
117 FROM ".TB_PREF."purch_orders, ".TB_PREF."suppliers, ".TB_PREF."locations
118 WHERE ".TB_PREF."purch_orders.supplier_id = ".TB_PREF."suppliers.supplier_id
119 AND ".TB_PREF."locations.loc_code = into_stock_location
120 AND ".TB_PREF."purch_orders.order_no = ".db_escape($order_no);
122 $result = db_query($sql, "The order cannot be retrieved");
124 if (db_num_rows($result) == 1)
127 $myrow = db_fetch($result);
129 $order->order_no = $order_no;
130 $order->supplier_id = $myrow["supplier_id"];
131 $order->supplier_name = $myrow["supp_name"];
132 $order->curr_code = $myrow["curr_code"];
134 $order->orig_order_date = sql2date($myrow["ord_date"]);
135 $order->Comments = $myrow["comments"];
136 $order->Location = $myrow["into_stock_location"];
137 $order->requisition_no = $myrow["requisition_no"];
138 $order->reference = $myrow["reference"];
139 $order->delivery_address = $myrow["delivery_address"];
144 display_db_error("FATAL : duplicate purchase order found", "", true);
148 //----------------------------------------------------------------------------------------
150 function read_po_items($order_no, &$order, $open_items_only=false)
152 /*now populate the line po array with the purchase order details records */
154 $sql = "SELECT ".TB_PREF."purch_order_details.*, units
155 FROM ".TB_PREF."purch_order_details
156 LEFT JOIN ".TB_PREF."stock_master
157 ON ".TB_PREF."purch_order_details.item_code=".TB_PREF."stock_master.stock_id
158 WHERE order_no =".db_escape($order_no);
160 if ($open_items_only)
161 $sql .= " AND (".TB_PREF."purch_order_details.quantity_ordered > ".TB_PREF."purch_order_details.quantity_received) ";
163 $sql .= " ORDER BY po_detail_item";
165 $result = db_query($sql, "The lines on the purchase order cannot be retrieved");
167 if (db_num_rows($result) > 0)
169 while ($myrow = db_fetch($result))
171 $data = get_purchase_data($order->supplier_id, $myrow['item_code']);
174 if ($data['supplier_description'] != "")
175 $myrow['description'] = $data['supplier_description'];
176 //if ($data['suppliers_uom'] != "")
177 // $myrow['units'] = $data['suppliers_uom'];
179 if (is_null($myrow["units"]))
185 $units = $myrow["units"];
188 if ($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"])) {
192 $order->line_items[$order->lines_on_order]->po_detail_rec = $myrow["po_detail_item"];
193 $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 //----------------------------------------------------------------------------------------
211 function get_po_items($order_no)
213 $sql = "SELECT item_code, quantity_ordered, quantity_received, qty_invoiced
214 FROM ".TB_PREF."purch_order_details
215 WHERE order_no=".db_escape($order_no)
216 ." ORDER BY po_detail_item";
218 $result = db_query($sql, "could not query purch order details");
219 check_db_error("Could not check that the details of the purchase order had not been changed by another user ", $sql);
222 //----------------------------------------------------------------------------------------
224 function get_short_info($stock_id)
226 $sql = "SELECT description, units, mb_flag
227 FROM ".TB_PREF."stock_master WHERE stock_id = ".db_escape($stock_id);
229 return db_query($sql,"The stock details for " . $stock_id . " could not be retrieved");
232 function get_sql_for_po_search_completed()
234 global $order_number, $selected_stock_item;;
240 location.location_name,
241 porder.requisition_no,
244 Sum(line.unit_price*line.quantity_ordered) AS OrderValue,
245 porder.into_stock_location
246 FROM ".TB_PREF."purch_orders as porder, "
247 .TB_PREF."purch_order_details as line, "
248 .TB_PREF."suppliers as supplier, "
249 .TB_PREF."locations as location
250 WHERE porder.order_no = line.order_no
251 AND porder.supplier_id = supplier.supplier_id
252 AND location.loc_code = porder.into_stock_location ";
254 if (isset($order_number) && $order_number != "")
256 $sql .= "AND porder.reference LIKE ".db_escape('%'. $order_number . '%');
261 $data_after = date2sql($_POST['OrdersAfterDate']);
262 $date_before = date2sql($_POST['OrdersToDate']);
264 $sql .= " AND porder.ord_date >= '$data_after'";
265 $sql .= " AND porder.ord_date <= '$date_before'";
267 if (isset($_POST['StockLocation']) && $_POST['StockLocation'] != ALL_TEXT)
269 $sql .= " AND porder.into_stock_location = ".db_escape($_POST['StockLocation']);
271 if (isset($selected_stock_item))
273 $sql .= " AND line.item_code=".db_escape($selected_stock_item);
276 } //end not order number selected
278 $sql .= " GROUP BY porder.order_no";
282 function get_sql_for_po_search()
284 global $all_items, $order_number, $selected_stock_item;;
290 location.location_name,
291 porder.requisition_no,
294 Sum(line.unit_price*line.quantity_ordered) AS OrderValue,
295 Sum(line.delivery_date < '". date2sql(Today()) ."'
296 AND (line.quantity_ordered > line.quantity_received)) As OverDue
298 .TB_PREF."purch_orders as porder, "
299 .TB_PREF."purch_order_details as line, "
300 .TB_PREF."suppliers as supplier, "
301 .TB_PREF."locations as location
302 WHERE porder.order_no = line.order_no
303 AND porder.supplier_id = supplier.supplier_id
304 AND location.loc_code = porder.into_stock_location
305 AND (line.quantity_ordered > line.quantity_received) ";
307 if (isset($order_number) && $order_number != "")
309 $sql .= "AND porder.reference LIKE ".db_escape('%'. $order_number . '%');
313 $data_after = date2sql($_POST['OrdersAfterDate']);
314 $data_before = date2sql($_POST['OrdersToDate']);
316 $sql .= " AND porder.ord_date >= '$data_after'";
317 $sql .= " AND porder.ord_date <= '$data_before'";
319 if (isset($_POST['StockLocation']) && $_POST['StockLocation'] != $all_items)
321 $sql .= " AND porder.into_stock_location = ".db_escape($_POST['StockLocation']);
324 if (isset($selected_stock_item))
326 $sql .= " AND line.item_code=".db_escape($selected_stock_item);
328 } //end not order number selected
330 $sql .= " GROUP BY porder.order_no";