Added support for tax included prices in transactions.
[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, 
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) . ")";
43
44         db_query($sql, "The purchase order header record could not be inserted");
45
46      /*Get the auto increment value of the order number created from the sql above */
47      $po_obj->order_no = db_insert_id();
48
49      /*Insert the purchase order detail records */
50      foreach ($po_obj->line_items as $line_no => $po_line)
51      {
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();
60      }
61
62         $Refs->save(ST_PURCHORDER, $po_obj->order_no, $po_obj->reference);
63
64         //add_comments(ST_PURCHORDER, $po_obj->order_no, $po_obj->orig_order_date, $po_obj->Comments);
65
66         add_audit_trail(ST_PURCHORDER, $po_obj->order_no, $po_obj->orig_order_date);
67         commit_transaction();
68
69         return $po_obj->order_no;
70 }
71
72 //----------------------------------------------------------------------------------------
73
74 function update_po(&$po_obj)
75 {
76         begin_transaction();
77
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");
88
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");
92
93     /*Now Update the purchase order detail records */
94     foreach ($po_obj->line_items as $po_line)
95     {
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) . ")";
105                 
106                 db_query($sql, "One of the purchase order detail records could not be updated");
107     }
108
109         // add_comments(ST_PURCHORDER, $po_obj->order_no, $po_obj->orig_order_date, $po_obj->Comments);
110
111         add_audit_trail($po_obj->trans_type, $po_obj->order_no, Today(), _("Updated."));
112         commit_transaction();
113
114         return $po_obj->order_no;
115 }
116
117 //----------------------------------------------------------------------------------------
118
119 function read_po_header($order_no, &$order)
120 {
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);
127
128         $result = db_query($sql, "The order cannot be retrieved");
129
130         if (db_num_rows($result) == 1)
131         {
132
133         $myrow = db_fetch($result);
134
135         $order->order_no = $order_no;
136         
137         $order->set_supplier($myrow["supplier_id"], $myrow["supp_name"], $myrow["curr_code"],
138                 $myrow['tax_group_id'], $myrow["tax_included"]);
139
140                 $order->credit = get_current_supp_credit($order->supplier_id);
141
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"];
148
149         return true;
150         }
151
152         display_db_error("FATAL : duplicate purchase order found", "", true);
153         return false;
154 }
155
156 //----------------------------------------------------------------------------------------
157
158 function read_po_items($order_no, &$order, $open_items_only=false)
159 {
160         /*now populate the line po array with the purchase order details records */
161
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);
167
168     if ($open_items_only)
169                 $sql .= " AND (".TB_PREF."purch_order_details.quantity_ordered > ".TB_PREF."purch_order_details.quantity_received) ";
170
171         $sql .= " ORDER BY po_detail_item";
172
173         $result = db_query($sql, "The lines on the purchase order cannot be retrieved");
174
175     if (db_num_rows($result) > 0)
176     {
177                 while ($myrow = db_fetch($result))
178         {
179                 $data = get_purchase_data($order->supplier_id, $myrow['item_code']);
180                 if ($data !== false)
181                 {
182                         if ($data['supplier_description'] != "")
183                                 $myrow['description'] = $data['supplier_description'];
184                         //if ($data['suppliers_uom'] != "")
185                         //      $myrow['units'] = $data['suppliers_uom'];
186                 }               
187             if (is_null($myrow["units"]))
188             {
189                         $units = "";
190             }
191             else
192             {
193                 $units = $myrow["units"];
194             }
195
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;
205                         }
206         } /* line po from purchase order details */
207     } //end of checks on returned data set
208 }
209
210 //----------------------------------------------------------------------------------------
211
212 function read_po($order_no, &$order, $open_items_only=false)
213 {
214         $result = read_po_header($order_no, $order);
215
216         if ($result)
217                 read_po_items($order_no, $order, $open_items_only);
218 }
219
220 //----------------------------------------------------------------------------------------
221
222 function get_po_items($order_no)
223 {
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";
228
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);
231     return $result;
232 }
233 //----------------------------------------------------------------------------------------
234
235 function get_short_info($stock_id)
236 {
237         $sql = "SELECT description, units, mb_flag
238                 FROM ".TB_PREF."stock_master WHERE stock_id = ".db_escape($stock_id);
239
240         return db_query($sql,"The stock details for " . $stock_id . " could not be retrieved");
241 }
242
243 function get_sql_for_po_search_completed()
244 {
245         global $order_number, $selected_stock_item;;
246
247         $sql = "SELECT 
248                 porder.order_no, 
249                 porder.reference, 
250                 supplier.supp_name, 
251                 location.location_name,
252                 porder.requisition_no, 
253                 porder.ord_date, 
254                 supplier.curr_code, 
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 ";
264
265         if (isset($order_number) && $order_number != "")
266         {
267                 $sql .= "AND porder.reference LIKE ".db_escape('%'. $order_number . '%');
268         }
269         else
270         {
271
272                 $data_after = date2sql($_POST['OrdersAfterDate']);
273                 $date_before = date2sql($_POST['OrdersToDate']);
274
275                 $sql .= " AND porder.ord_date >= '$data_after'";
276                 $sql .= " AND porder.ord_date <= '$date_before'";
277
278                 if (isset($_POST['StockLocation']) && $_POST['StockLocation'] != ALL_TEXT)
279                 {
280                         $sql .= " AND porder.into_stock_location = ".db_escape($_POST['StockLocation']);
281                 }
282                 if (isset($selected_stock_item))
283                 {
284                         $sql .= " AND line.item_code=".db_escape($selected_stock_item);
285                 }
286
287         } //end not order number selected
288
289         $sql .= " GROUP BY porder.order_no";
290         return $sql;
291 }       
292
293 function get_sql_for_po_search()
294 {
295         global $all_items, $order_number, $selected_stock_item;;
296         
297         $sql = "SELECT 
298                 porder.order_no, 
299                 porder.reference,
300                 supplier.supp_name, 
301                 location.location_name,
302                 porder.requisition_no, 
303                 porder.ord_date,
304                 supplier.curr_code,
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
308                 FROM "
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) ";
317
318         if (isset($order_number) && $order_number != "")
319         {
320                 $sql .= "AND porder.reference LIKE ".db_escape('%'. $order_number . '%');
321         }
322         else
323         {
324                 $data_after = date2sql($_POST['OrdersAfterDate']);
325                 $data_before = date2sql($_POST['OrdersToDate']);
326
327                 $sql .= "  AND porder.ord_date >= '$data_after'";
328                 $sql .= "  AND porder.ord_date <= '$data_before'";
329
330                 if (isset($_POST['StockLocation']) && $_POST['StockLocation'] != $all_items)
331                 {
332                         $sql .= " AND porder.into_stock_location = ".db_escape($_POST['StockLocation']);
333                 }
334
335                 if (isset($selected_stock_item))
336                 {
337                         $sql .= " AND line.item_code=".db_escape($selected_stock_item);
338                 }
339         } //end not order number selected
340
341         $sql .= " GROUP BY porder.order_no";
342         return $sql;
343 }
344 ?>