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