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)
46 var $tax_overrides = array(); // array of taxes manually inserted during sales invoice entry
51 function __construct($trans_type, $trans_no=0)
53 $this->trans_type = $trans_type;
54 /*Constructor function initialises a new Supplier Transaction object */
55 $this->read($trans_type, $trans_no);
58 function read($trans_type, $trans_no)
60 $this->trans_type = $trans_type;
61 $this->trans_no = $trans_no;
62 $this->grn_items = array();
63 $this->gl_codes = array();
65 read_supp_invoice($trans_no, $trans_type, $this);
66 if ($trans_type == ST_SUPPCREDIT)
68 $this->src_docs = find_src_invoices($trans_no);
70 read_supplier_details_to_trans($this, $this->supplier_id);
74 function add_grn_to_trans($grn_item_id, $po_detail_item, $item_code, $item_description,
75 $qty_recd, $prev_quantity_inv, $this_quantity_inv, $order_price, $chg_price,
76 $std_cost_unit=null, $gl_code='')
78 $this->grn_items[$grn_item_id] = new grn_item($grn_item_id, $po_detail_item,
79 $item_code, $item_description, $qty_recd, $prev_quantity_inv, $this_quantity_inv,
80 $order_price, $chg_price, $std_cost_unit, $gl_code, $this->tax_included);
82 $this->src_docs = find_src_invoices($this);
84 unset($this->tax_overrides); // cancel tax overrides after cart change
88 function add_gl_codes_to_trans($gl_code, $gl_act_name, $gl_dim, $gl_dim2, $amount, $memo_)
90 $this->gl_codes[] = new gl_codes($gl_code, $gl_act_name, $gl_dim, $gl_dim2, $amount, $memo_);
91 unset($this->tax_overrides); // cancel tax overrides after cart change
95 function remove_grn_from_trans($grn_item_id)
97 unset($this->tax_overrides); // cancel tax overrides after cart change
98 unset($this->grn_items[$grn_item_id]);
100 function remove_gl_codes_from_trans($gl_code_counter)
102 unset($this->tax_overrides); // cancel tax overrides after cart change
103 unset($this->gl_codes[$gl_code_counter]);
106 function is_valid_trans_to_post()
108 return (count($this->grn_items) > 0 || count($this->gl_codes) > 0 ||
109 ($this->ov_amount != 0) || ($this->ov_discount > 0));
112 function clear_items()
114 unset($this->grn_items);
115 unset($this->gl_codes);
116 $this->ov_amount = $this->ov_discount = $this->supplier_id = 0;
118 $this->grn_items = array();
119 $this->gl_codes = array();
122 function get_taxes($tax_group_id=null, $shipping_cost=0, $gl_codes=true)
127 if ($tax_group_id == null)
128 $tax_group_id = $this->tax_group_id;
130 // preload the taxgroup
131 $tax_group = get_tax_group_items_as_array($tax_group_id);
133 foreach ($this->grn_items as $ln_itm)
135 $items[] = $ln_itm->item_code;
136 $prices[] =round( ($ln_itm->this_quantity_inv * $ln_itm->chg_price),
140 if ($tax_group_id == null)
141 $tax_group_id = $this->tax_group_id;
142 $taxes = get_tax_for_items($this->trans_type, $items, $prices, $shipping_cost, $tax_group_id,
143 $this->tax_included);
145 if (isset($this->tax_overrides))
146 foreach($this->tax_overrides as $id => $value) // add values entered manually
148 $taxes[$id]['Override'] = $value;
151 // Taxes included in gl_codes table have exact value, but count to overrides as well.
152 // Therefore when we want to now taxes only for items (gl_codes==false),
153 // we have to subtract gl_taxes from override values.
154 foreach ($this->gl_codes as $gl_code)
156 $index = is_tax_account($gl_code->gl_code);
157 if ($index !== false)
160 $taxes[$index]['Value'] += $gl_code->amount;
161 elseif (isset($this->tax_overrides))
162 $taxes[$index]['Override'] -= $gl_code->amount;
169 // Returns total invoice amount without taxes.
171 function get_total_taxfree($tax_group_id=null)
175 // preload the taxgroup !
176 if ($tax_group_id != null)
177 $tax_group = get_tax_group_items_as_array($tax_group_id);
181 foreach ($this->grn_items as $ln_itm)
182 $total += round(($ln_itm->this_quantity_inv * $ln_itm->taxfree_charge_price($tax_group_id, $tax_group)),
185 foreach ($this->gl_codes as $gl_line)
187 if (!is_tax_account($gl_line->gl_code))
188 $total += $gl_line->amount;
193 // Returns transaction total
195 function get_items_total()
199 foreach ($this->grn_items as $ln_itm)
200 $total += round($ln_itm->this_quantity_inv * $ln_itm->chg_price, user_price_dec());
202 foreach ($this->gl_codes as $gl_line)
204 if (!is_tax_account($gl_line->gl_code) || $this->tax_included)
205 $total += $gl_line->amount;
210 function split_line_values()
212 // split nominal line values
213 foreach($this->grn_items as $line)
214 $line->split_item_value($this);
216 // Exact tax values are currently entered as tax totals, so we need to move the differences back on line level.
217 // currently first item with given tax type will be fixed with the calculated difference
218 // FIXME: change UI moving tax edit to line level in line edit mode, then this workaround will be obsolete.
219 foreach($this->get_taxes() as $tax_id => $tax)
221 if ($tax['Value'] != 0 && isset($tax['Override']) && ($tax['Value'] != $tax['Override']))
223 foreach($this->grn_items as $id => $line)
224 if ($line->gl_amounts[0]['tax_type_id'] == $tax_id) // assumed single tax rate on item, so always gl_mount[0] is valid
226 $diff = $tax['Override'] - $tax['Value'];
227 $this->grn_items[$id]->gl_amounts[0]['Value'] += $diff;
228 if ($this->vat_category() != VC_NONDEDUCT)
229 $this->grn_items[$id]->gl_amounts[0]['Deductible'] += $diff;
231 $this->grn_items[$id]->gl_amounts['Cost'] += $diff;
232 // when supplier uses net prices the price is exact, so don't fix net, still record exact VAT.
233 if ($this->tax_included) {
234 $this->grn_items[$id]->gl_amounts['Net'] -= $diff;
235 $this->grn_items[$id]->gl_amounts['Cost'] -= $diff;
242 } /* end of class defintion */
247 /* Contains relavent information from the purch_order_details as well to provide in cached form,
248 all the info to do the necessary entries without looking up ie additional queries of the database again */
253 var $item_description;
255 var $prev_quantity_inv;
256 var $this_quantity_inv;
262 var $gl_amounts; // splited line value (after call to split_line_value method
265 function __construct($id, $po_detail_item, $item_code, $item_description, $qty_recd,
266 $prev_quantity_inv, $this_quantity_inv, $order_price, $chg_price,
267 $std_cost_unit, $gl_code, $tax_included)
271 $this->po_detail_item = $po_detail_item;
272 $this->item_code = $item_code;
273 $this->item_description = $item_description;
274 $this->qty_recd = $qty_recd;
275 $this->prev_quantity_inv = $prev_quantity_inv;
276 $this->this_quantity_inv = $this_quantity_inv;
277 $this->order_price =$order_price;
278 $this->chg_price = $chg_price;
279 $this->std_cost_unit = $std_cost_unit;
280 $this->gl_code = $gl_code;
281 $this->tax_included = $tax_included;
283 $opts = get_item($item_code);
284 $this->vat_category = $opts['vat_category'];
287 function full_charge_price($tax_group_id, $trans_type=ST_PURCHORDER)
289 return get_full_price_for_item($trans_type, $this->item_code,
290 $this->chg_price, $tax_group_id, $this->tax_included);
293 function taxfree_charge_price($tax_group_id, $trans_type=ST_PURCHORDER)
295 return get_tax_free_price_for_item($trans_type, $this->item_code, $this->chg_price,
296 $tax_group_id, $this->tax_included);
299 function taxfree_value($tax_group_id, $trans_type=ST_PURCHORDER)
301 return get_tax_free_price_for_item($trans_type, $this->item_code, $this->this_quantity_inv * $this->chg_price,
302 $tax_group_id, $this->tax_included, $tax_group);
306 Splits item value to parts posted to GL.
308 function split_item_value($cart)
313 return $this->gl_amounts = split_item_price($this->item_code, $this->chg_price*$this->this_quantity_inv, $cart->tax_group_id, $cart->tax_included,
314 ST_SUPPINVOICE, $vat_factor, $cart->tran_date);
329 function __construct($gl_code, $gl_act_name, $gl_dim, $gl_dim2, $amount, $memo_)
331 $this->gl_code = $gl_code;
332 $this->gl_act_name = $gl_act_name;
333 $this->gl_dim = $gl_dim;
334 $this->gl_dim2 = $gl_dim2;
335 $this->amount = $amount;
336 $this->memo_= $memo_;