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