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
18 var $trans_type; // order/grn/invoice (direct)
22 var $delivery_address;
29 var $order_no; /*Only used for modification of existing orders otherwise only established when order committed */
30 var $lines_on_order = 0;
33 var $tax_group_array = null; // saves db queries
34 var $tax_included; // type of prices
42 var $tax_overrides = array(); // array of taxes manually inserted during sales invoice entry (direct invoice)
44 var $prep_amount = 0; // prepayment required
45 var $alloc; // sum of payments allocated
46 var $prepayments = array();
48 var $fixed_asset = false;
50 function __construct()
52 /*Constructor function initialises a new purchase order object */
53 $this->line_items = array();
54 $this->lines_on_order = $this->order_no = $this->supplier_id = 0;
57 function set_supplier($supplier_id, $supplier_name, $curr_code, $tax_group_id, $tax_included)
59 $this->supplier_id = $supplier_id;
60 $this->supplier_name = $supplier_name;
61 $this->curr_code = $curr_code;
62 $this->tax_group_id = $tax_group_id;
63 $this->tax_included = $tax_included;
64 $this->tax_group_array = get_tax_group_items_as_array($tax_group_id);
67 function add_to_order($line_no, $stock_id, $qty, $item_descr, $price, $uom, $req_del_date, $qty_inv, $qty_recd)
69 if (isset($qty) && $qty != 0)
71 $this->line_items[$line_no] = new po_line_details($line_no, $stock_id, $item_descr, $qty, $price, $uom,
72 $req_del_date, $qty_inv, $qty_recd);
73 $this->lines_on_order++;
79 function update_order_item($line_no, $qty, $price, $req_del_date, $description="")
81 if ($description != "")
82 $this->line_items[$line_no]->item_description = $description;
83 $this->line_items[$line_no]->quantity = $qty;
84 $this->line_items[$line_no]->price = $price;
85 $this->line_items[$line_no]->req_del_date = $req_del_date;
86 $this->line_items[$line_no]->item_description = $description;
89 function remove_from_order($line_no)
91 array_splice($this->line_items, $line_no, 1);
94 function order_has_items()
96 return count($this->line_items) != 0;
99 function clear_items()
101 unset($this->line_items);
102 $this->line_items = array();
104 $this->lines_on_order = 0;
109 function any_already_received()
111 /* Checks if there have been deliveries or invoiced entered against any of the line items */
112 if (count($this->line_items) > 0)
114 foreach ($this->line_items as $ordered_items)
116 if ($ordered_items->qty_received != 0 || $ordered_items->qty_inv != 0)
125 function some_already_received($line_no)
127 /* Checks if there have been deliveries or amounts invoiced against a specific line item */
128 if (count($this->line_items) > 0)
130 if ($this->line_items[$line_no]->qty_received != 0 ||
131 $this->line_items[$line_no]->qty_inv != 0)
140 // Returns taxes for PO/GRN.
141 // $receival=true in purchase receive context.
143 function get_taxes($shipping_cost=null, $receival=false)
147 if($shipping_cost==null)
148 $shipping_cost = 0;//$this->freight_cost;
150 foreach ($this->line_items as $ln_itm) {
151 $items[] = $ln_itm->stock_id;
152 $prices[] = round($ln_itm->price * ($receival ? $ln_itm->receive_qty : $ln_itm->quantity), user_price_dec());
154 $taxes = get_tax_for_items($items, $prices, $shipping_cost,
155 $this->tax_group_id, $this->tax_included, $this->tax_group_array);
157 // Adjustment for swiss franken, we always have 5 rappen = 1/20 franken
158 if ($this->curr_code == 'CHF') {
159 $val = $taxes['1']['Value'];
160 $val1 = (floatval((intval(round(($val*20),0)))/20));
161 $taxes['1']['Value'] = $val1;
163 foreach($this->tax_overrides as $id => $value) // add values entered manually
165 $taxes[$id]['Override'] = $value;
171 Returns order value including all taxes
173 function get_trans_total() {
176 $dec = user_price_dec();
178 foreach ($this->line_items as $ln_itm) {
179 $items[] = $ln_itm->stock_id;
180 $value = round($ln_itm->quantity * $ln_itm->price, $dec);
185 if (!$this->tax_included ) {
186 $taxes = get_tax_for_items($items, $prices, 0, $this->tax_group_id,
187 $this->tax_included, $this->tax_group_array);
189 foreach($taxes as $tax)
190 $total += round($tax['Value'], $dec);
195 } /* end of class defintion */
197 class po_line_details
204 var $item_description;
211 var $quantity; // current/entry quantity of PO line
212 var $qty_inv; // quantity already invoiced against this line
213 var $receive_qty; // current/entry GRN quantity
214 var $qty_received; // quantity already received against this line
219 function __construct($line_no, $stock_item, $item_descr, $qty, $prc, $uom, $req_del_date,
220 $qty_inv, $qty_recd, $grn_item_id=0)
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);
230 $this->descr_editable = $item_row["editable"];
231 //if ($item_descr == null || !$this->descr_editable) this was overriding the prchase description!!
232 if ($item_descr == null)
233 $this->item_description = $item_row["description"];
235 $this->item_description = $item_descr;
236 $this->quantity = $qty;
237 $this->req_del_date = $req_del_date;
239 // $this->units = $uom;
240 $this->tax_type = $item_row["tax_type_id"];
241 $this->tax_type_name = $item_row["tax_type_name"];
242 $this->units = $item_row["units"];
243 $this->qty_received = $qty_recd;
244 $this->qty_inv = $qty_inv;
245 $this->receive_qty = 0; /*initialise these last two only */
246 $this->standard_cost =0;
247 $this->grn_item_id = $grn_item_id;
250 // Deprecated, use with care. If you need line value as it is calculated in invoice, use taxfree_charge_value below.
251 function taxfree_charge_price($po)
253 return get_tax_free_price_for_item($this->stock_id, $this->price,
254 $po->tax_group_id, $po->tax_included, $po->tax_group_array);
258 // GRN line tax free value.
260 function taxfree_charge_value($po)
262 return get_tax_free_price_for_item($this->stock_id, $this->receive_qty*$this->price,
263 $po->tax_group_id, $po->tax_included, $po->tax_group_array);