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