Smaller technical fixes.
[fa-stable.git] / purchasing / includes / db / po_db.inc
1 <?php
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 //----------------------------------------------------------------------------------------
13
14 function delete_po($po)
15 {
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");
18
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");
21 }
22
23 //----------------------------------------------------------------------------------------
24
25 function add_po(&$po_obj)
26 {
27         global $Refs;
28
29         begin_transaction();
30
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->supp_ref) . ", " .
38          db_escape($po_obj->Location) . ", " .
39          db_escape($po_obj->delivery_address) . ")";
40
41         db_query($sql, "The purchase order header record could not be inserted");
42
43      /*Get the auto increment value of the order number created from the sql above */
44      $po_obj->order_no = db_insert_id();
45
46      /*Insert the purchase order detail records */
47      foreach ($po_obj->line_items as $line_no => $po_line)
48      {
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");
56                 $po_obj->line_items[$line_no]->po_detail_rec = db_insert_id();
57      }
58
59         $Refs->save(ST_PURCHORDER, $po_obj->order_no, $po_obj->reference);
60
61         //add_comments(ST_PURCHORDER, $po_obj->order_no, $po_obj->orig_order_date, $po_obj->Comments);
62
63         add_audit_trail(ST_PURCHORDER, $po_obj->order_no, $po_obj->orig_order_date);
64         commit_transaction();
65
66         return $po_obj->order_no;
67 }
68
69 //----------------------------------------------------------------------------------------
70
71 function update_po(&$po_obj)
72 {
73         begin_transaction();
74
75     /*Update the purchase order header with any changes */
76     $sql = "UPDATE ".TB_PREF."purch_orders SET Comments=" . db_escape($po_obj->Comments) . ",
77                 requisition_no= ". db_escape( $po_obj->supp_ref). ",
78                 into_stock_location=" . db_escape($po_obj->Location). ",
79                 ord_date='" . date2sql($po_obj->orig_order_date) . "',
80                 delivery_address=" . db_escape($po_obj->delivery_address);
81     $sql .= " WHERE order_no = " . $po_obj->order_no;
82         db_query($sql, "The purchase order could not be updated");
83
84         $sql = "DELETE FROM ".TB_PREF."purch_order_details WHERE order_no="
85                 .db_escape($po_obj->order_no);
86         db_query($sql, "could not delete old purch order details");
87
88     /*Now Update the purchase order detail records */
89     foreach ($po_obj->line_items as $po_line)
90     {
91         $sql = "INSERT INTO ".TB_PREF."purch_order_details (po_detail_item, order_no, item_code, 
92                 description, delivery_date, unit_price, quantity_ordered) VALUES ("
93                         .db_escape($po_line->po_detail_rec ? $po_line->po_detail_rec : 0). ","
94                         .$po_obj->order_no . ","
95                         .db_escape($po_line->stock_id). ","
96                         .db_escape($po_line->item_description). ",'"
97                         .date2sql($po_line->req_del_date) . "',"
98                         .db_escape($po_line->price) . ", "
99                         .db_escape($po_line->quantity) . ")";
100                 
101                 db_query($sql, "One of the purchase order detail records could not be updated");
102     }
103
104         // add_comments(ST_PURCHORDER, $po_obj->order_no, $po_obj->orig_order_date, $po_obj->Comments);
105
106         add_audit_trail($po_obj->trans_type, $po_obj->order_no, Today(), _("Updated."));
107         commit_transaction();
108
109         return $po_obj->order_no;
110 }
111
112 //----------------------------------------------------------------------------------------
113
114 function read_po_header($order_no, &$order)
115 {
116         $sql = "SELECT ".TB_PREF."purch_orders.*, ".TB_PREF."suppliers.supp_name,
117                 ".TB_PREF."suppliers.curr_code, ".TB_PREF."locations.location_name
118                 FROM ".TB_PREF."purch_orders, ".TB_PREF."suppliers, ".TB_PREF."locations
119                 WHERE ".TB_PREF."purch_orders.supplier_id = ".TB_PREF."suppliers.supplier_id
120                 AND ".TB_PREF."locations.loc_code = into_stock_location
121                 AND ".TB_PREF."purch_orders.order_no = ".db_escape($order_no);
122
123         $result = db_query($sql, "The order cannot be retrieved");
124
125         if (db_num_rows($result) == 1)
126         {
127
128         $myrow = db_fetch($result);
129
130         $order->order_no = $order_no;
131         $order->supplier_id = $myrow["supplier_id"];
132         $order->supplier_name = $myrow["supp_name"];
133         $order->curr_code = $myrow["curr_code"];
134
135         $order->orig_order_date = sql2date($myrow["ord_date"]);
136         $order->Comments = $myrow["comments"];
137         $order->Location = $myrow["into_stock_location"];
138         $order->supp_ref = $myrow["requisition_no"];
139         $order->reference = $myrow["reference"];
140         $order->delivery_address = $myrow["delivery_address"];
141
142         return true;
143         }
144
145         display_db_error("FATAL : duplicate purchase order found", "", true);
146         return false;
147 }
148
149 //----------------------------------------------------------------------------------------
150
151 function read_po_items($order_no, &$order, $open_items_only=false)
152 {
153         /*now populate the line po array with the purchase order details records */
154
155         $sql = "SELECT ".TB_PREF."purch_order_details.*, units
156                 FROM ".TB_PREF."purch_order_details
157                 LEFT JOIN ".TB_PREF."stock_master
158                 ON ".TB_PREF."purch_order_details.item_code=".TB_PREF."stock_master.stock_id
159                 WHERE order_no =".db_escape($order_no);
160
161     if ($open_items_only)
162                 $sql .= " AND (".TB_PREF."purch_order_details.quantity_ordered > ".TB_PREF."purch_order_details.quantity_received) ";
163
164         $sql .= " ORDER BY po_detail_item";
165
166         $result = db_query($sql, "The lines on the purchase order cannot be retrieved");
167
168     if (db_num_rows($result) > 0)
169     {
170                 while ($myrow = db_fetch($result))
171         {
172                 $data = get_purchase_data($order->supplier_id, $myrow['item_code']);
173                 if ($data !== false)
174                 {
175                         if ($data['supplier_description'] != "")
176                                 $myrow['description'] = $data['supplier_description'];
177                         //if ($data['suppliers_uom'] != "")
178                         //      $myrow['units'] = $data['suppliers_uom'];
179                 }               
180             if (is_null($myrow["units"]))
181             {
182                         $units = "";
183             }
184             else
185             {
186                 $units = $myrow["units"];
187             }
188
189             if ($order->add_to_order($order->lines_on_order, $myrow["item_code"],
190                 $myrow["quantity_ordered"],$myrow["description"],
191                 $myrow["unit_price"],$units, sql2date($myrow["delivery_date"]),
192                 $myrow["qty_invoiced"], $myrow["quantity_received"])) {
193                         $newline = &$order->line_items[$order->lines_on_order-1];
194                                         $newline->po_detail_rec = $myrow["po_detail_item"];
195                                         $newline->standard_cost = $myrow["std_cost_unit"];  /*Needed for receiving goods and GL interface */
196                                 // set for later GRN edition
197 //                      $newline->receive_qty = $newline->quantity - $newline->qty_dispatched;
198                         }
199         } /* line po from purchase order details */
200     } //end of checks on returned data set
201 }
202
203 //----------------------------------------------------------------------------------------
204
205 function read_po($order_no, &$order, $open_items_only=false)
206 {
207         $result = read_po_header($order_no, $order);
208
209         if ($result)
210                 read_po_items($order_no, $order, $open_items_only);
211 }
212
213 //----------------------------------------------------------------------------------------
214
215 function get_po_items($order_no)
216 {
217         $sql = "SELECT item_code, quantity_ordered, quantity_received, qty_invoiced
218                 FROM ".TB_PREF."purch_order_details
219                 WHERE order_no=".db_escape($order_no)
220                 ." ORDER BY po_detail_item";
221
222         $result = db_query($sql, "could not query purch order details");
223     check_db_error("Could not check that the details of the purchase order had not been changed by another user ", $sql);
224     return $result;
225 }
226 //----------------------------------------------------------------------------------------
227
228 function get_short_info($stock_id)
229 {
230         $sql = "SELECT description, units, mb_flag
231                 FROM ".TB_PREF."stock_master WHERE stock_id = ".db_escape($stock_id);
232
233         return db_query($sql,"The stock details for " . $stock_id . " could not be retrieved");
234 }
235
236 function get_sql_for_po_search_completed()
237 {
238         global $order_number, $selected_stock_item;;
239
240         $sql = "SELECT 
241                 porder.order_no, 
242                 porder.reference, 
243                 supplier.supp_name, 
244                 location.location_name,
245                 porder.requisition_no, 
246                 porder.ord_date, 
247                 supplier.curr_code, 
248                 Sum(line.unit_price*line.quantity_ordered) AS OrderValue,
249                 porder.into_stock_location
250                 FROM ".TB_PREF."purch_orders as porder, "
251                         .TB_PREF."purch_order_details as line, "
252                         .TB_PREF."suppliers as supplier, "
253                         .TB_PREF."locations as location
254                 WHERE porder.order_no = line.order_no
255                 AND porder.supplier_id = supplier.supplier_id
256                 AND location.loc_code = porder.into_stock_location ";
257
258         if (isset($order_number) && $order_number != "")
259         {
260                 $sql .= "AND porder.reference LIKE ".db_escape('%'. $order_number . '%');
261         }
262         else
263         {
264
265                 $data_after = date2sql($_POST['OrdersAfterDate']);
266                 $date_before = date2sql($_POST['OrdersToDate']);
267
268                 $sql .= " AND porder.ord_date >= '$data_after'";
269                 $sql .= " AND porder.ord_date <= '$date_before'";
270
271                 if (isset($_POST['StockLocation']) && $_POST['StockLocation'] != ALL_TEXT)
272                 {
273                         $sql .= " AND porder.into_stock_location = ".db_escape($_POST['StockLocation']);
274                 }
275                 if (isset($selected_stock_item))
276                 {
277                         $sql .= " AND line.item_code=".db_escape($selected_stock_item);
278                 }
279
280         } //end not order number selected
281
282         $sql .= " GROUP BY porder.order_no";
283         return $sql;
284 }       
285
286 function get_sql_for_po_search()
287 {
288         global $all_items, $order_number, $selected_stock_item;;
289         
290         $sql = "SELECT 
291                 porder.order_no, 
292                 porder.reference,
293                 supplier.supp_name, 
294                 location.location_name,
295                 porder.requisition_no, 
296                 porder.ord_date,
297                 supplier.curr_code,
298                 Sum(line.unit_price*line.quantity_ordered) AS OrderValue,
299                 Sum(line.delivery_date < '". date2sql(Today()) ."'
300                 AND (line.quantity_ordered > line.quantity_received)) As OverDue
301                 FROM "
302                         .TB_PREF."purch_orders as porder, "
303                         .TB_PREF."purch_order_details as line, "
304                         .TB_PREF."suppliers as supplier, "
305                         .TB_PREF."locations as location
306                 WHERE porder.order_no = line.order_no
307                 AND porder.supplier_id = supplier.supplier_id
308                 AND location.loc_code = porder.into_stock_location
309                 AND (line.quantity_ordered > line.quantity_received) ";
310
311         if (isset($order_number) && $order_number != "")
312         {
313                 $sql .= "AND porder.reference LIKE ".db_escape('%'. $order_number . '%');
314         }
315         else
316         {
317                 $data_after = date2sql($_POST['OrdersAfterDate']);
318                 $data_before = date2sql($_POST['OrdersToDate']);
319
320                 $sql .= "  AND porder.ord_date >= '$data_after'";
321                 $sql .= "  AND porder.ord_date <= '$data_before'";
322
323                 if (isset($_POST['StockLocation']) && $_POST['StockLocation'] != $all_items)
324                 {
325                         $sql .= " AND porder.into_stock_location = ".db_escape($_POST['StockLocation']);
326                 }
327
328                 if (isset($selected_stock_item))
329                 {
330                         $sql .= " AND line.item_code=".db_escape($selected_stock_item);
331                 }
332         } //end not order number selected
333
334         $sql .= " GROUP BY porder.order_no";
335         return $sql;
336 }
337 ?>