*** empty log message ***
[fa-stable.git] / purchasing / includes / db / po_db.inc
1 <?php
2
3 //----------------------------------------------------------------------------------------
4
5 function delete_po($po)
6 {
7         $sql = "DELETE FROM ".TB_PREF."purch_orders WHERE order_no=" . $po;
8         db_query($sql, "The order header could not be deleted");
9
10         $sql = "DELETE FROM ".TB_PREF."purch_order_details WHERE order_no =" . $po;
11         db_query($sql, "The order detail lines could not be deleted");
12 }
13
14 //----------------------------------------------------------------------------------------
15
16 function add_po(&$po_obj)
17 {
18         begin_transaction();    
19         
20      /*Insert to purchase order header record */
21      $sql = "INSERT INTO ".TB_PREF."purch_orders (supplier_id, Comments, ord_date, reference, requisition_no, into_stock_location, delivery_address) VALUES(";
22      $sql .= "'" . $po_obj->supplier_id . "', '" . 
23          db_escape($po_obj->Comments) . "','" . 
24          date2sql($po_obj->orig_order_date) . "', '" . 
25                  $po_obj->reference . "', '" . 
26          $po_obj->requisition_no . "', '" . 
27          $po_obj->Location . "', '" . 
28          $po_obj->delivery_address . "')";
29     
30         db_query($sql, "The purchase order header record could not be inserted");
31     
32      /*Get the auto increment value of the order number created from the sql above */
33      $po_obj->order_no = db_insert_id();
34     
35      /*Insert the purchase order detail records */
36      foreach ($po_obj->line_items as $po_line) 
37      {
38         if ($po_line->Deleted == false) 
39         {
40                 $sql = "INSERT INTO ".TB_PREF."purch_order_details (order_no, item_code, description, delivery_date,    unit_price,     quantity_ordered) VALUES (";
41                 $sql .= $po_obj->order_no . ", '" . $po_line->stock_id . "','" . 
42                         $po_line->item_description . "','" . 
43                         date2sql($po_line->req_del_date) . "'," . 
44                         $po_line->price . ", " . 
45                         $po_line->quantity . ")";
46                         db_query($sql, "One of the purchase order detail records could not be inserted");
47         }
48      } 
49      
50         add_forms_for_sys_type(systypes::po(), $po_obj->order_no);     
51         
52         references::save_last($po_obj->reference, systypes::po());      
53         
54         //add_comments(systypes::po(), $po_obj->order_no, $po_obj->orig_order_date, $po_obj->Comments); 
55      
56         commit_transaction();     
57      
58         return $po_obj->order_no;
59 }
60
61 //----------------------------------------------------------------------------------------
62
63 function update_po(&$po_obj)
64 {
65         begin_transaction();    
66
67     /*Update the purchase order header with any changes */
68     $sql = "UPDATE ".TB_PREF."purch_orders SET Comments='" . db_escape($po_obj->Comments) . "', 
69                 requisition_no= '" . $po_obj->requisition_no . "', 
70                 into_stock_location='" . $po_obj->Location . "', 
71                 ord_date='" . date2sql($po_obj->orig_order_date) . "',
72                 delivery_address='" . $po_obj->delivery_address . "'";
73     $sql .= " WHERE order_no = " . $po_obj->order_no;
74         db_query($sql, "The purchase order could not be updated");
75
76     /*Now Update the purchase order detail records */
77     foreach ($po_obj->line_items as $po_line) 
78     {
79
80                 if ($po_line->Deleted==True) 
81                 {
82                         // Sherifoz 21.06.03 Handle deleting existing lines
83                         if ($po_line->po_detail_rec!='') 
84                         {
85                                 $sql = "DELETE FROM ".TB_PREF."purch_order_details WHERE po_detail_item='" . $po_line->po_detail_rec . "'";
86                                 db_query($sql, "could not query purch order details");
87                         }
88                 } 
89                 else if ($po_line->po_detail_rec == '') 
90                 {
91                         // Sherifoz 21.06.03 Handle adding new lines vs. updating. if no key(po_detail_rec) then it's a new line
92                         $sql = "INSERT INTO ".TB_PREF."purch_order_details (order_no, item_code, description, delivery_date, unit_price,        quantity_ordered) VALUES (";
93                         $sql .= $po_obj->order_no . ", '" . 
94                                 $po_line->stock_id . "','" . 
95                                 $po_line->item_description . "','" . 
96                                 date2sql($po_line->req_del_date) . "'," . 
97                                 $po_line->price . ", " . $po_line->quantity . ")";
98                 } 
99                 else 
100                 {
101                         $sql = "UPDATE ".TB_PREF."purch_order_details SET item_code='" . $po_line->stock_id . "', 
102                                 description ='" . $po_line->item_description . "',
103                                 delivery_date ='" . date2sql($po_line->req_del_date) . "', 
104                                 unit_price=" . $po_line->price . ", 
105                                 quantity_ordered=" . $po_line->quantity . " 
106                                 WHERE po_detail_item=" . $po_line->po_detail_rec;
107                 }
108                 db_query($sql, "One of the purchase order detail records could not be updated");
109     }
110      
111         //add_comments(systypes::po(), $po_obj->order_no, $po_obj->orig_order_date, $po_obj->Comments);     
112      
113         commit_transaction();     
114      
115         return $po_obj->order_no;     
116 }
117
118 //----------------------------------------------------------------------------------------
119
120 function read_po_header($order_no, &$order)
121 {
122         $sql = "SELECT ".TB_PREF."purch_orders.*, ".TB_PREF."suppliers.supp_name, 
123                 ".TB_PREF."suppliers.curr_code, ".TB_PREF."locations.location_name 
124                 FROM ".TB_PREF."purch_orders, ".TB_PREF."suppliers, ".TB_PREF."locations 
125                 WHERE ".TB_PREF."purch_orders.supplier_id = ".TB_PREF."suppliers.supplier_id
126                 AND ".TB_PREF."locations.loc_code = into_stock_location 
127                 AND ".TB_PREF."purch_orders.order_no = " . $order_no;
128
129         $result = db_query($sql, "The order cannot be retrieved");
130    
131         if (db_num_rows($result) == 1) 
132         {
133
134         $myrow = db_fetch($result);
135       
136         $order->order_no = $order_no;
137         $order->supplier_id = $myrow["supplier_id"];
138         $order->supplier_name = $myrow["supp_name"];
139         $order->curr_code = $myrow["curr_code"];
140       
141         $order->orig_order_date = sql2date($myrow["ord_date"]);
142         $order->Comments = $myrow["comments"];
143         $order->Location = $myrow["into_stock_location"];
144         $order->requisition_no = $myrow["requisition_no"];
145         $order->reference = $myrow["reference"];
146         $order->delivery_address = $myrow["delivery_address"];
147       
148         return true;
149         }       
150         
151         display_db_error("FATAL : duplicate purchase order found", "", true);
152         return false;
153 }
154
155 //----------------------------------------------------------------------------------------
156
157 function read_po_items($order_no, &$order, $open_items_only=false)
158 {
159         /*now populate the line po array with the purchase order details records */
160
161         $sql = "SELECT ".TB_PREF."purch_order_details.*, units 
162                 FROM ".TB_PREF."purch_order_details 
163                 LEFT JOIN ".TB_PREF."stock_master 
164                 ON ".TB_PREF."purch_order_details.item_code=".TB_PREF."stock_master.stock_id 
165                 WHERE order_no =$order_no ";
166       
167     if ($open_items_only)
168                 $sql .= " AND (".TB_PREF."purch_order_details.quantity_ordered > ".TB_PREF."purch_order_details.quantity_received) "; 
169           
170         $sql .= " ORDER BY po_detail_item";
171
172         $result = db_query($sql, "The lines on the purchase order cannot be retrieved");      
173
174     if (db_num_rows($result) > 0) 
175     {
176
177                 while ($myrow = db_fetch($result)) 
178         {
179             if (is_null($myrow["units"]))
180             {
181                         $units = "";
182             } 
183             else 
184             {
185                 $units = $myrow["units"];
186             }
187
188             $order->add_to_order($order->lines_on_order+1, $myrow["item_code"], 
189                 $myrow["quantity_ordered"],$myrow["description"], 
190                 $myrow["unit_price"],$units, sql2date($myrow["delivery_date"]),
191                 $myrow["qty_invoiced"], $myrow["quantity_received"]);
192             
193             $order->line_items[$order->lines_on_order]->po_detail_rec = $myrow["po_detail_item"];
194             $order->line_items[$order->lines_on_order]->standard_cost = $myrow["std_cost_unit"];  /*Needed for receiving goods and GL interface */
195         } /* line po from purchase order details */
196     } //end of checks on returned data set
197 }
198
199 //----------------------------------------------------------------------------------------
200
201 function read_po($order_no, &$order, $open_items_only=false)
202 {
203         $result = read_po_header($order_no, $order);    
204     
205         if ($result)
206                 read_po_items($order_no, $order, $open_items_only);             
207 }
208
209 //----------------------------------------------------------------------------------------
210
211
212 ?>