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