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,
33 requisition_no, into_stock_location, delivery_address, total, tax_included) VALUES(";
34 $sql .= db_escape($po_obj->supplier_id) . "," .
35 db_escape($po_obj->Comments) . ",'" .
36 date2sql($po_obj->orig_order_date) . "', " .
37 db_escape($po_obj->reference) . ", " .
38 db_escape($po_obj->supp_ref) . ", " .
39 db_escape($po_obj->Location) . ", " .
40 db_escape($po_obj->delivery_address) . ", " .
41 db_escape($po_obj->get_trans_total()). ", " .
42 db_escape($po_obj->tax_included) . ")";
44 db_query($sql, "The purchase order header record could not be inserted");
46 /*Get the auto increment value of the order number created from the sql above */
47 $po_obj->order_no = db_insert_id();
49 /*Insert the purchase order detail records */
50 foreach ($po_obj->line_items as $line_no => $po_line)
52 $sql = "INSERT INTO ".TB_PREF."purch_order_details (order_no, item_code, description, delivery_date, unit_price, quantity_ordered) VALUES (";
53 $sql .= $po_obj->order_no . ", " . db_escape($po_line->stock_id). "," .
54 db_escape($po_line->item_description). ",'" .
55 date2sql($po_line->req_del_date) . "'," .
56 db_escape($po_line->price) . ", " .
57 db_escape($po_line->quantity). ")";
58 db_query($sql, "One of the purchase order detail records could not be inserted");
59 $po_obj->line_items[$line_no]->po_detail_rec = db_insert_id();
62 $Refs->save(ST_PURCHORDER, $po_obj->order_no, $po_obj->reference);
64 //add_comments(ST_PURCHORDER, $po_obj->order_no, $po_obj->orig_order_date, $po_obj->Comments);
66 add_audit_trail(ST_PURCHORDER, $po_obj->order_no, $po_obj->orig_order_date);
69 return $po_obj->order_no;
72 //----------------------------------------------------------------------------------------
74 function update_po(&$po_obj)
78 /*Update the purchase order header with any changes */
79 $sql = "UPDATE ".TB_PREF."purch_orders SET Comments=" . db_escape($po_obj->Comments) . ",
80 requisition_no= ". db_escape( $po_obj->supp_ref). ",
81 into_stock_location=" . db_escape($po_obj->Location). ",
82 ord_date='" . date2sql($po_obj->orig_order_date) . "',
83 delivery_address=" . db_escape($po_obj->delivery_address).",
84 total=". db_escape($po_obj->get_trans_total()).",
85 tax_included=". db_escape($po_obj->tax_included);
86 $sql .= " WHERE order_no = " . $po_obj->order_no;
87 db_query($sql, "The purchase order could not be updated");
89 $sql = "DELETE FROM ".TB_PREF."purch_order_details WHERE order_no="
90 .db_escape($po_obj->order_no);
91 db_query($sql, "could not delete old purch order details");
93 /*Now Update the purchase order detail records */
94 foreach ($po_obj->line_items as $po_line)
96 $sql = "INSERT INTO ".TB_PREF."purch_order_details (po_detail_item, order_no, item_code,
97 description, delivery_date, unit_price, quantity_ordered) VALUES ("
98 .db_escape($po_line->po_detail_rec ? $po_line->po_detail_rec : 0). ","
99 .$po_obj->order_no . ","
100 .db_escape($po_line->stock_id). ","
101 .db_escape($po_line->item_description). ",'"
102 .date2sql($po_line->req_del_date) . "',"
103 .db_escape($po_line->price) . ", "
104 .db_escape($po_line->quantity) . ")";
106 db_query($sql, "One of the purchase order detail records could not be updated");
109 // add_comments(ST_PURCHORDER, $po_obj->order_no, $po_obj->orig_order_date, $po_obj->Comments);
111 add_audit_trail($po_obj->trans_type, $po_obj->order_no, Today(), _("Updated."));
112 commit_transaction();
114 return $po_obj->order_no;
117 //----------------------------------------------------------------------------------------
119 function read_po_header($order_no, &$order)
121 $sql = "SELECT ".TB_PREF."purch_orders.*, ".TB_PREF."suppliers.supp_name, ".TB_PREF."suppliers.tax_group_id,
122 ".TB_PREF."suppliers.curr_code, ".TB_PREF."locations.location_name
123 FROM ".TB_PREF."purch_orders, ".TB_PREF."suppliers, ".TB_PREF."locations
124 WHERE ".TB_PREF."purch_orders.supplier_id = ".TB_PREF."suppliers.supplier_id
125 AND ".TB_PREF."locations.loc_code = into_stock_location
126 AND ".TB_PREF."purch_orders.order_no = ".db_escape($order_no);
128 $result = db_query($sql, "The order cannot be retrieved");
130 if (db_num_rows($result) == 1)
133 $myrow = db_fetch($result);
135 $order->order_no = $order_no;
137 $order->set_supplier($myrow["supplier_id"], $myrow["supp_name"], $myrow["curr_code"],
138 $myrow['tax_group_id'], $myrow["tax_included"]);
140 $order->credit = get_current_supp_credit($order->supplier_id);
142 $order->orig_order_date = sql2date($myrow["ord_date"]);
143 $order->Comments = $myrow["comments"];
144 $order->Location = $myrow["into_stock_location"];
145 $order->supp_ref = $myrow["requisition_no"];
146 $order->reference = $myrow["reference"];
147 $order->delivery_address = $myrow["delivery_address"];
152 display_db_error("FATAL : duplicate purchase order found", "", true);
156 //----------------------------------------------------------------------------------------
158 function read_po_items($order_no, &$order, $open_items_only=false)
160 /*now populate the line po array with the purchase order details records */
162 $sql = "SELECT ".TB_PREF."purch_order_details.*, units
163 FROM ".TB_PREF."purch_order_details
164 LEFT JOIN ".TB_PREF."stock_master
165 ON ".TB_PREF."purch_order_details.item_code=".TB_PREF."stock_master.stock_id
166 WHERE order_no =".db_escape($order_no);
168 if ($open_items_only)
169 $sql .= " AND (".TB_PREF."purch_order_details.quantity_ordered > ".TB_PREF."purch_order_details.quantity_received) ";
171 $sql .= " ORDER BY po_detail_item";
173 $result = db_query($sql, "The lines on the purchase order cannot be retrieved");
175 if (db_num_rows($result) > 0)
177 while ($myrow = db_fetch($result))
179 $data = get_purchase_data($order->supplier_id, $myrow['item_code']);
182 if ($data['supplier_description'] != "")
183 $myrow['description'] = $data['supplier_description'];
184 //if ($data['suppliers_uom'] != "")
185 // $myrow['units'] = $data['suppliers_uom'];
187 if (is_null($myrow["units"]))
193 $units = $myrow["units"];
196 if ($order->add_to_order($order->lines_on_order, $myrow["item_code"],
197 $myrow["quantity_ordered"],$myrow["description"],
198 $myrow["unit_price"],$units, sql2date($myrow["delivery_date"]),
199 $myrow["qty_invoiced"], $myrow["quantity_received"])) {
200 $newline = &$order->line_items[$order->lines_on_order-1];
201 $newline->po_detail_rec = $myrow["po_detail_item"];
202 $newline->standard_cost = $myrow["std_cost_unit"]; /*Needed for receiving goods and GL interface */
203 // set for later GRN edition
204 // $newline->receive_qty = $newline->quantity - $newline->qty_dispatched;
206 } /* line po from purchase order details */
207 } //end of checks on returned data set
210 //----------------------------------------------------------------------------------------
212 function read_po($order_no, &$order, $open_items_only=false)
214 $result = read_po_header($order_no, $order);
217 read_po_items($order_no, $order, $open_items_only);
220 //----------------------------------------------------------------------------------------
222 function get_po_items($order_no)
224 $sql = "SELECT item_code, quantity_ordered, quantity_received, qty_invoiced
225 FROM ".TB_PREF."purch_order_details
226 WHERE order_no=".db_escape($order_no)
227 ." ORDER BY po_detail_item";
229 $result = db_query($sql, "could not query purch order details");
230 check_db_error("Could not check that the details of the purchase order had not been changed by another user ", $sql);
233 //----------------------------------------------------------------------------------------
235 function get_short_info($stock_id)
237 $sql = "SELECT description, units, mb_flag
238 FROM ".TB_PREF."stock_master WHERE stock_id = ".db_escape($stock_id);
240 return db_query($sql,"The stock details for " . $stock_id . " could not be retrieved");
243 function get_sql_for_po_search_completed()
245 global $order_number, $selected_stock_item;;
251 location.location_name,
252 porder.requisition_no,
255 Sum(line.unit_price*line.quantity_ordered) AS OrderValue,
256 porder.into_stock_location
257 FROM ".TB_PREF."purch_orders as porder, "
258 .TB_PREF."purch_order_details as line, "
259 .TB_PREF."suppliers as supplier, "
260 .TB_PREF."locations as location
261 WHERE porder.order_no = line.order_no
262 AND porder.supplier_id = supplier.supplier_id
263 AND location.loc_code = porder.into_stock_location ";
265 if (isset($order_number) && $order_number != "")
267 $sql .= "AND porder.reference LIKE ".db_escape('%'. $order_number . '%');
272 $data_after = date2sql($_POST['OrdersAfterDate']);
273 $date_before = date2sql($_POST['OrdersToDate']);
275 $sql .= " AND porder.ord_date >= '$data_after'";
276 $sql .= " AND porder.ord_date <= '$date_before'";
278 if (isset($_POST['StockLocation']) && $_POST['StockLocation'] != ALL_TEXT)
280 $sql .= " AND porder.into_stock_location = ".db_escape($_POST['StockLocation']);
282 if (isset($selected_stock_item))
284 $sql .= " AND line.item_code=".db_escape($selected_stock_item);
287 } //end not order number selected
289 $sql .= " GROUP BY porder.order_no";
293 function get_sql_for_po_search()
295 global $all_items, $order_number, $selected_stock_item;;
301 location.location_name,
302 porder.requisition_no,
305 Sum(line.unit_price*line.quantity_ordered) AS OrderValue,
306 Sum(line.delivery_date < '". date2sql(Today()) ."'
307 AND (line.quantity_ordered > line.quantity_received)) As OverDue
309 .TB_PREF."purch_orders as porder, "
310 .TB_PREF."purch_order_details as line, "
311 .TB_PREF."suppliers as supplier, "
312 .TB_PREF."locations as location
313 WHERE porder.order_no = line.order_no
314 AND porder.supplier_id = supplier.supplier_id
315 AND location.loc_code = porder.into_stock_location
316 AND (line.quantity_ordered > line.quantity_received) ";
318 if (isset($order_number) && $order_number != "")
320 $sql .= "AND porder.reference LIKE ".db_escape('%'. $order_number . '%');
324 $data_after = date2sql($_POST['OrdersAfterDate']);
325 $data_before = date2sql($_POST['OrdersToDate']);
327 $sql .= " AND porder.ord_date >= '$data_after'";
328 $sql .= " AND porder.ord_date <= '$data_before'";
330 if (isset($_POST['StockLocation']) && $_POST['StockLocation'] != $all_items)
332 $sql .= " AND porder.into_stock_location = ".db_escape($_POST['StockLocation']);
335 if (isset($selected_stock_item))
337 $sql .= " AND line.item_code=".db_escape($selected_stock_item);
339 } //end not order number selected
341 $sql .= " GROUP BY porder.order_no";