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 Supplier Transactions class to hold all the information for an accounts payable invoice or credit note
15 include_once($path_to_root . "/taxes/tax_calc.inc");
20 var $grn_items; /*array of objects of class grn_item using the id as the pointer */
21 var $gl_codes; /*array of objects of class gl_codes using a counter as the pointer */
31 var $trans_type; // invoice or credit
37 var $src_docs = array(); // source invoice for this credit note (if any)
44 var $gl_codes_counter=0;
47 var $stored_algorithm;
49 var $tax_overrides = array(); // array of taxes manually inserted during sales invoice entry
54 function supp_trans($trans_type, $trans_no=0)
56 $this->trans_type = $trans_type;
57 /*Constructor function initialises a new Supplier Transaction object */
58 $this->read($trans_type, $trans_no);
61 function read($trans_type, $trans_no)
63 $this->trans_type = $trans_type;
64 $this->trans_no = $trans_no;
65 $this->grn_items = array();
66 $this->gl_codes = array();
68 read_supp_invoice($trans_no, $trans_type, $this);
69 if ($trans_type == ST_SUPPCREDIT)
71 $this->src_docs = find_src_invoices($trans_no);
73 read_supplier_details_to_trans($this, $this->supplier_id);
77 function add_grn_to_trans($grn_item_id, $po_detail_item, $item_code, $item_description,
78 $qty_recd, $prev_quantity_inv, $this_quantity_inv, $order_price, $chg_price,
79 $std_cost_unit, $gl_code)
81 $this->grn_items[$grn_item_id] = new grn_item($grn_item_id, $po_detail_item,
82 $item_code, $item_description, $qty_recd, $prev_quantity_inv, $this_quantity_inv,
83 $order_price, $chg_price, $std_cost_unit, $gl_code, $this->tax_included);
85 $this->src_docs = find_src_invoices($this);
87 unset($this->tax_overrides); // cancel tax overrides after cart change
91 function add_gl_codes_to_trans($gl_code, $gl_act_name, $gl_dim, $gl_dim2, $amount, $memo_)
93 $this->gl_codes[$this->gl_codes_counter] = new gl_codes($this->gl_codes_counter,
94 $gl_code, $gl_act_name, $gl_dim, $gl_dim2, $amount, $memo_);
95 $this->gl_codes_counter++;
96 unset($this->tax_overrides); // cancel tax overrides after cart change
100 function remove_grn_from_trans($grn_item_id)
102 unset($this->tax_overrides); // cancel tax overrides after cart change
103 unset($this->grn_items[$grn_item_id]);
105 function remove_gl_codes_from_trans($gl_code_counter)
107 unset($this->tax_overrides); // cancel tax overrides after cart change
108 unset($this->gl_codes[$gl_code_counter]);
111 function is_valid_trans_to_post()
113 return (count($this->grn_items) > 0 || count($this->gl_codes) > 0 ||
114 ($this->ov_amount != 0) || ($this->ov_discount > 0));
117 function clear_items()
119 unset($this->grn_items);
120 unset($this->gl_codes);
121 $this->ov_amount = $this->ov_discount = $this->supplier_id = 0;
123 $this->grn_items = array();
124 $this->gl_codes = array();
127 function get_taxes($tax_group_id=null, $shipping_cost=0, $gl_codes=true)
132 if ($tax_group_id == null)
133 $tax_group_id = $this->tax_group_id;
135 // preload the taxgroup
136 $tax_group = get_tax_group_items_as_array($tax_group_id);
138 foreach ($this->grn_items as $ln_itm)
140 $items[] = $ln_itm->item_code;
141 $prices[] =round( ($ln_itm->this_quantity_inv * $ln_itm->chg_price),
145 if ($tax_group_id == null)
146 $tax_group_id = $this->tax_group_id;
147 $taxes = get_tax_for_items($items, $prices, $shipping_cost, $tax_group_id,
148 $this->tax_included, null, $this->tax_algorithm);
150 if (isset($this->tax_overrides))
151 foreach($this->tax_overrides as $id => $value) // add values entered manually
153 $taxes[$id]['Override'] = $value;
156 // Taxes included in gl_codes table have exact value, but count to overrides as well.
157 // Therefore when we want to now taxes only for items (gl_codes==false),
158 // we have to subtract gl_taxes from override values.
159 foreach ($this->gl_codes as $gl_code)
161 $index = is_tax_account($gl_code->gl_code);
162 if ($index !== false)
165 $taxes[$index]['Value'] += $gl_code->amount;
166 elseif (isset($this->tax_overrides))
167 $taxes[$index]['Override'] -= $gl_code->amount;
174 // Returns total invoice amount without taxes.
176 function get_total_taxfree($tax_group_id=null)
180 // preload the taxgroup !
181 if ($tax_group_id != null)
182 $tax_group = get_tax_group_items_as_array($tax_group_id);
186 foreach ($this->grn_items as $ln_itm)
187 $total += round(($ln_itm->this_quantity_inv * $ln_itm->taxfree_charge_price($tax_group_id, $tax_group)),
190 foreach ($this->gl_codes as $gl_line)
192 if (!is_tax_account($gl_line->gl_code))
193 $total += $gl_line->amount;
198 // Returns transaction total
200 function get_items_total()
204 foreach ($this->grn_items as $ln_itm)
205 $total += round($ln_itm->this_quantity_inv * $ln_itm->chg_price, user_price_dec());
207 foreach ($this->gl_codes as $gl_line)
209 if (!is_tax_account($gl_line->gl_code) || $this->tax_included)
210 $total += $gl_line->amount;
214 } /* end of class defintion */
219 /* Contains relavent information from the purch_order_details as well to provide in cached form,
220 all the info to do the necessary entries without looking up ie additional queries of the database again */
225 var $item_description;
227 var $prev_quantity_inv;
228 var $this_quantity_inv;
235 function grn_item ($id, $po_detail_item, $item_code, $item_description, $qty_recd,
236 $prev_quantity_inv, $this_quantity_inv, $order_price, $chg_price,
237 $std_cost_unit, $gl_code, $tax_included)
241 $this->po_detail_item = $po_detail_item;
242 $this->item_code = $item_code;
243 $this->item_description = $item_description;
244 $this->qty_recd = $qty_recd;
245 $this->prev_quantity_inv = $prev_quantity_inv;
246 $this->this_quantity_inv = $this_quantity_inv;
247 $this->order_price =$order_price;
248 $this->chg_price = $chg_price;
249 $this->std_cost_unit = $std_cost_unit;
250 $this->gl_code = $gl_code;
251 $this->tax_included = $tax_included;
254 function full_charge_price($tax_group_id, $tax_group=null)
256 return get_full_price_for_item($this->item_code,
257 $this->chg_price, $tax_group_id, $this->tax_included, $tax_group);
260 function taxfree_charge_price($tax_group_id, $tax_group=null)
262 return get_tax_free_price_for_item($this->item_code, $this->chg_price,
263 $tax_group_id, $this->tax_included, $tax_group);
279 function gl_codes ($Counter, $gl_code, $gl_act_name, $gl_dim, $gl_dim2, $amount, $memo_)
282 /* Constructor function to add a new gl_codes object with passed params */
283 $this->Counter = $Counter;
284 $this->gl_code = $gl_code;
285 $this->gl_act_name = $gl_act_name;
286 $this->gl_dim = $gl_dim;
287 $this->gl_dim2 = $gl_dim2;
288 $this->amount = $amount;
289 $this->memo_= $memo_;