Stable merged into unstable again (due to failure on binary file during previous...
[fa-stable.git] / purchasing / includes / po_class.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 /* Definition of the purch_order class to hold all the information for a purchase order and delivery
13 */
14
15 class purch_order 
16 {
17
18         var $trans_type; // order/grn/invoice (direct)
19         var $line_items;
20         var $curr_code;
21         var $supp_ref;
22         var $delivery_address;
23         var $Comments;
24         var $Location;
25         var $supplier_id;
26         var $supplier_name;
27         var $orig_order_date;
28         var $due_date;
29         var $order_no; /*Only used for modification of existing orders otherwise only established when order committed */
30         var $lines_on_order = 0;
31         var $credit;
32         var $tax_group_id;
33         var $tax_group_array = null; // saves db queries
34         var $tax_included; // type of prices
35         var $tax_algorithm;
36         var $stored_algorithm; // copy to find the change after invoice in processed
37         var $terms;
38         
39         var $reference;
40         
41         function purch_order()
42         {
43                 /*Constructor function initialises a new purchase order object */
44                 $this->line_items = array();
45                 $this->lines_on_order = $this->order_no = $this->supplier_id = 0;
46         }
47         
48         function set_supplier($supplier_id, $supplier_name, $curr_code, $tax_group_id, $tax_included, $tax_algorithm)
49         {
50                 $this->supplier_id = $supplier_id;
51                 $this->supplier_name = $supplier_name;
52                 $this->curr_code = $curr_code;
53                 $this->tax_group_id = $tax_group_id;
54                 $this->tax_included = $tax_included;
55                 $this->stored_algorithm = $this->tax_algorithm = $tax_algorithm;
56                 $this->tax_group_array = get_tax_group_items_as_array($tax_group_id);
57         }
58         
59         function add_to_order($line_no, $stock_id, $qty, $item_descr, $price, $uom, $req_del_date, $qty_inv, $qty_recd)
60         {
61                 if ($qty != 0 && isset($qty))
62                 {
63                         $this->line_items[$line_no] = new po_line_details($line_no, $stock_id, $item_descr, $qty, $price, $uom, 
64                                 $req_del_date, $qty_inv, $qty_recd);
65                         $this->lines_on_order++;
66                         return 1;
67                 }
68                 return 0;
69         }
70
71         function update_order_item($line_no, $qty, $price, $req_del_date, $description="")
72         {
73                 if ($description != "")
74                         $this->line_items[$line_no]->item_description = $description;
75                 $this->line_items[$line_no]->quantity = $qty;
76                 $this->line_items[$line_no]->price = $price;
77                 $this->line_items[$line_no]->req_del_date = $req_del_date;
78                 $this->line_items[$line_no]->item_description = $description;
79         }
80
81         function remove_from_order($line_no)
82         {
83                 array_splice($this->line_items, $line_no, 1);
84         }
85         
86         function order_has_items() 
87         {
88                 return count($this->line_items) != 0;
89         }
90         
91         function clear_items() 
92         {
93         unset($this->line_items);
94                 $this->line_items = array();
95                 
96                 $this->lines_on_order = 0;  
97                 $this->order_no = 0;
98         }
99
100         
101         function any_already_received()
102         {
103                 /* Checks if there have been deliveries or invoiced entered against any of the line items */
104                 if (count($this->line_items) > 0)
105                 {
106                         foreach ($this->line_items as $ordered_items) 
107                         {
108                                 if ($ordered_items->qty_received != 0 || $ordered_items->qty_inv != 0)
109                                 {
110                                         return 1;
111                                 }
112                         }
113                 }
114                 return 0;
115         }
116
117         function some_already_received($line_no)
118         {
119                 /* Checks if there have been deliveries or amounts invoiced against a specific line item */
120                 if (count($this->line_items) > 0)
121                 {
122                         if ($this->line_items[$line_no]->qty_received != 0 || 
123                                 $this->line_items[$line_no]->qty_inv != 0)
124                         {
125                                 return 1;
126                         }
127                 }
128                 return 0;
129         }
130         
131         //
132         //      Returns taxes for PO/GRN.
133         //      $receival=true in purchase receive context.
134         //
135         function get_taxes($shipping_cost=null, $receival=false)
136         {
137                 $items = array();
138                 $prices = array();
139                 if($shipping_cost==null)
140                         $shipping_cost = 0;//$this->freight_cost;
141
142                 foreach ($this->line_items as $ln_itm) {
143                         $items[] = $ln_itm->stock_id;
144                         $prices[] = round($ln_itm->price * ($receival ? $ln_itm->receive_qty : $ln_itm->quantity),  user_price_dec());
145                 }
146                 $taxes = get_tax_for_items($items, $prices, $shipping_cost,
147                   $this->tax_group_id, $this->tax_included,  $this->tax_group_array, $this->tax_algorithm);
148
149         // Adjustment for swiss franken, we always have 5 rappen = 1/20 franken
150             if ($this->curr_code == 'CHF') {
151                         $val = $taxes['1']['Value'];
152                         $taxes['1']['Value'] = (floatval((intval(round(($val*20),0)))/20));
153                 } 
154                 return $taxes;
155         }
156
157         /*
158                 Returns order value including all taxes
159         */
160         function get_trans_total() {
161                 
162                 $total = 0;
163                 $dec = user_price_dec();
164
165                 foreach ($this->line_items as $ln_itm) {
166                         $items[] = $ln_itm->stock_id;
167                         $value = round($ln_itm->quantity * $ln_itm->price, $dec);
168                         $prices[] =$value;
169                         $total += $value;
170                 }
171
172                 if (!$this->tax_included ) {
173                         $taxes = get_tax_for_items($items, $prices, 0, $this->tax_group_id,
174                         $this->tax_included,  $this->tax_group_array, $this->tax_algorithm);
175
176                         foreach($taxes as $tax)
177                                 $total += round($tax['Value'], $dec);
178                 }
179                 return $total;
180         }
181
182 } /* end of class defintion */
183
184 class po_line_details 
185 {
186
187         var $line_no;
188         var $po_detail_rec;
189         var $grn_item_id;
190         var $stock_id;
191         var $item_description;
192         var $price;
193         var $units;
194         var $req_del_date;
195         var $tax_type;
196         var $tax_type_name;
197
198         var $quantity;          // current/entry quantity of PO line
199         var $qty_inv;   // quantity already invoiced against this line
200         var $receive_qty;       // current/entry GRN quantity
201         var $qty_received;      // quantity already received against this line
202
203         var $standard_cost;
204         var $descr_editable;
205
206         function po_line_details($line_no, $stock_item, $item_descr, $qty, $prc, $uom, $req_del_date, 
207                 $qty_inv, $qty_recd, $grn_item_id=0)
208         {
209
210                 /* Constructor function to add a new LineDetail object with passed params */
211                 $this->line_no = $line_no;
212                 $this->stock_id = $stock_item;
213                 $item_row = get_item($stock_item);
214                 if (!$item_row) 
215                         return;
216
217                 $this->descr_editable = $item_row["editable"];
218                 if ($item_descr == null || !$this->descr_editable)
219                         $this->item_description = $item_row["description"];
220                 else
221                         $this->item_description = $item_descr;
222                 $this->quantity = $qty;
223                 $this->req_del_date = $req_del_date;
224                 $this->price = $prc;
225 //              $this->units = $uom;
226                 $this->tax_type = $item_row["tax_type_id"];
227                 $this->tax_type_name = $item_row["tax_type_name"];
228                 $this->units = $item_row["units"];
229                 $this->qty_received = $qty_recd;
230                 $this->qty_inv = $qty_inv;
231                 $this->receive_qty = 0; /*initialise these last two only */
232                 $this->standard_cost =0;
233                 $this->grn_item_id = $grn_item_id;
234         }
235         
236         function taxfree_charge_price($po)
237         {
238                 return get_tax_free_price_for_item($this->stock_id, $this->price, 
239                   $po->tax_group_id, $po->tax_included, $po->tax_group_array);
240         }
241 }
242
243 ?>