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