Added audit trail.
[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=" . $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 =" . $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         begin_transaction();
28
29      /*Insert to purchase order header record */
30      $sql = "INSERT INTO ".TB_PREF."purch_orders (supplier_id, Comments, ord_date, reference, requisition_no, into_stock_location, delivery_address) VALUES(";
31      $sql .= db_escape($po_obj->supplier_id) . "," .
32          db_escape($po_obj->Comments) . ",'" .
33          date2sql($po_obj->orig_order_date) . "', '" .
34                  $po_obj->reference . "', " .
35          db_escape($po_obj->requisition_no) . ", " .
36          db_escape($po_obj->Location) . ", " .
37          db_escape($po_obj->delivery_address) . ")";
38
39         db_query($sql, "The purchase order header record could not be inserted");
40
41      /*Get the auto increment value of the order number created from the sql above */
42      $po_obj->order_no = db_insert_id();
43
44      /*Insert the purchase order detail records */
45      foreach ($po_obj->line_items as $po_line)
46      {
47         if ($po_line->Deleted == false)
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                         $po_line->price . ", " .
54                         $po_line->quantity . ")";
55                         db_query($sql, "One of the purchase order detail records could not be inserted");
56         }
57      }
58
59         references::save_last($po_obj->reference, systypes::po());
60
61         //add_comments(systypes::po(), $po_obj->order_no, $po_obj->orig_order_date, $po_obj->Comments);
62
63         add_audit_trail(systypes::po(), systypes::po(), $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->requisition_no). ",
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     /*Now Update the purchase order detail records */
85     foreach ($po_obj->line_items as $po_line)
86     {
87
88                 if ($po_line->Deleted==True)
89                 {
90                         // Sherifoz 21.06.03 Handle deleting existing lines
91                         if ($po_line->po_detail_rec!='')
92                         {
93                                 $sql = "DELETE FROM ".TB_PREF."purch_order_details WHERE po_detail_item='" . $po_line->po_detail_rec . "'";
94                                 db_query($sql, "could not query purch order details");
95                         }
96                 }
97                 else if ($po_line->po_detail_rec == '')
98                 {
99                         // Sherifoz 21.06.03 Handle adding new lines vs. updating. if no key(po_detail_rec) then it's a new line
100                         $sql = "INSERT INTO ".TB_PREF."purch_order_details (order_no, item_code, description, delivery_date, unit_price,        quantity_ordered) VALUES (";
101                         $sql .= $po_obj->order_no . "," .
102                                 db_escape($po_line->stock_id). "," .
103                                 db_escape($po_line->item_description). ",'" .
104                                 date2sql($po_line->req_del_date) . "'," .
105                                 $po_line->price . ", " . $po_line->quantity . ")";
106                 }
107                 else
108                 {
109                         $sql = "UPDATE ".TB_PREF."purch_order_details SET item_code='" . $po_line->stock_id . "',
110                                 description =" . db_escape($po_line->item_description). ",
111                                 delivery_date ='" . date2sql($po_line->req_del_date) . "',
112                                 unit_price=" . $po_line->price . ",
113                                 quantity_ordered=" . $po_line->quantity . "
114                                 WHERE po_detail_item=" . $po_line->po_detail_rec;
115                 }
116                 db_query($sql, "One of the purchase order detail records could not be updated");
117     }
118
119         //add_comments(systypes::po(), $po_obj->order_no, $po_obj->orig_order_date, $po_obj->Comments);
120
121         commit_transaction();
122
123         return $po_obj->order_no;
124 }
125
126 //----------------------------------------------------------------------------------------
127
128 function read_po_header($order_no, &$order)
129 {
130         $sql = "SELECT ".TB_PREF."purch_orders.*, ".TB_PREF."suppliers.supp_name,
131                 ".TB_PREF."suppliers.curr_code, ".TB_PREF."locations.location_name
132                 FROM ".TB_PREF."purch_orders, ".TB_PREF."suppliers, ".TB_PREF."locations
133                 WHERE ".TB_PREF."purch_orders.supplier_id = ".TB_PREF."suppliers.supplier_id
134                 AND ".TB_PREF."locations.loc_code = into_stock_location
135                 AND ".TB_PREF."purch_orders.order_no = " . $order_no;
136
137         $result = db_query($sql, "The order cannot be retrieved");
138
139         if (db_num_rows($result) == 1)
140         {
141
142         $myrow = db_fetch($result);
143
144         $order->order_no = $order_no;
145         $order->supplier_id = $myrow["supplier_id"];
146         $order->supplier_name = $myrow["supp_name"];
147         $order->curr_code = $myrow["curr_code"];
148
149         $order->orig_order_date = sql2date($myrow["ord_date"]);
150         $order->Comments = $myrow["comments"];
151         $order->Location = $myrow["into_stock_location"];
152         $order->requisition_no = $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 =$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
185                 while ($myrow = db_fetch($result))
186         {
187                 $data = get_purchase_data($order->supplier_id, $myrow['item_code']);
188                 if ($data !== false)
189                 {
190                         if ($data['supplier_description'] != "")
191                                 $myrow['description'] = $data['supplier_description'];
192                         if ($data['suppliers_uom'] != "")
193                                 $myrow['units'] = $data['suppliers_uom'];
194                 }               
195             if (is_null($myrow["units"]))
196             {
197                         $units = "";
198             }
199             else
200             {
201                 $units = $myrow["units"];
202             }
203
204             $order->add_to_order($order->lines_on_order+1, $myrow["item_code"],
205                 $myrow["quantity_ordered"],$myrow["description"],
206                 $myrow["unit_price"],$units, sql2date($myrow["delivery_date"]),
207                 $myrow["qty_invoiced"], $myrow["quantity_received"]);
208
209             $order->line_items[$order->lines_on_order]->po_detail_rec = $myrow["po_detail_item"];
210             $order->line_items[$order->lines_on_order]->standard_cost = $myrow["std_cost_unit"];  /*Needed for receiving goods and GL interface */
211         } /* line po from purchase order details */
212     } //end of checks on returned data set
213 }
214
215 //----------------------------------------------------------------------------------------
216
217 function read_po($order_no, &$order, $open_items_only=false)
218 {
219         $result = read_po_header($order_no, $order);
220
221         if ($result)
222                 read_po_items($order_no, $order, $open_items_only);
223 }
224
225 //----------------------------------------------------------------------------------------
226
227
228 ?>