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");
21 var $trans_type; // invoice or credit
34 var $grn_items; /*array of objects of class grn_item using the id as the pointer */
35 var $gl_codes; /*array of objects of class gl_codes using a counter as the pointer */
45 var $src_docs = array(); // source invoice for this credit note (if any)
49 var $tax_overrides = array(); // array of taxes manually inserted during sales invoice entry
54 function __construct($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);
70 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=null, $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[] = new gl_codes($gl_code, $gl_act_name, $gl_dim, $gl_dim2, $amount, $memo_);
94 unset($this->tax_overrides); // cancel tax overrides after cart change
98 function remove_grn_from_trans($grn_item_id)
100 unset($this->tax_overrides); // cancel tax overrides after cart change
101 unset($this->grn_items[$grn_item_id]);
103 function remove_gl_codes_from_trans($gl_code_counter)
105 unset($this->tax_overrides); // cancel tax overrides after cart change
106 unset($this->gl_codes[$gl_code_counter]);
109 function is_valid_trans_to_post()
111 return (count($this->grn_items) > 0 || count($this->gl_codes) > 0 ||
112 ($this->ov_amount != 0) || ($this->ov_discount > 0));
115 function clear_items()
117 unset($this->grn_items);
118 unset($this->gl_codes);
119 $this->ov_amount = $this->ov_discount = $this->supplier_id = 0;
121 $this->grn_items = array();
122 $this->gl_codes = array();
125 function get_taxes($gl_codes=true)
130 foreach ($this->grn_items as $ln_itm)
132 $items[] = $ln_itm->item_code;
133 $prices[] = round( $ln_itm->this_quantity_inv * $ln_itm->chg_price, user_price_dec());
136 $taxes = get_tax_for_items($this->trans_type, $items, $prices, 0, $this->tax_group_id,
137 $this->tax_included);
139 if (isset($this->tax_overrides))
140 foreach($this->tax_overrides as $id => $value) // add values entered manually
142 $taxes[$id]['Override'] = $value;
145 // Taxes included in gl_codes table have exact value, but count to overrides as well.
146 // Therefore when we want to know taxes only for items (gl_codes==false),
147 // we have to subtract gl_taxes from override values.
148 foreach ($this->gl_codes as $gl_code)
150 $index = is_tax_account($gl_code->gl_code);
151 if ($index !== false)
154 $taxes[$index]['Value'] += $gl_code->amount;
155 elseif (isset($this->tax_overrides))
156 $taxes[$index]['Override'] -= $gl_code->amount;
162 // Returns total invoice amount without taxes.
164 function get_total_taxfree($tax_group_id=null)
168 foreach ($this->grn_items as $ln_itm)
169 $total += round(($ln_itm->this_quantity_inv * $ln_itm->taxfree_charge_price($tax_group_id, $this->trans_type)),
172 foreach ($this->gl_codes as $gl_line)
174 if (!is_tax_account($gl_line->gl_code))
175 $total += $gl_line->amount;
181 // Returns transaction total
183 function get_items_total()
187 foreach ($this->grn_items as $ln_itm)
188 $total += round($ln_itm->this_quantity_inv * $ln_itm->chg_price, user_price_dec());
190 foreach ($this->gl_codes as $gl_line)
192 if (!is_tax_account($gl_line->gl_code) || $this->tax_included)
193 $total += $gl_line->amount;
199 Split line value to cost and taxes.
200 Stores calculated amounts in $line->gl_amounts array.
202 function split_line_values()
204 // split nominal line values
205 foreach($this->grn_items as $line)
206 $line->split_item_value($this);
208 // Exact tax values are currently entered as tax totals, so we need to move the differences back on line level.
209 // currently first item with given tax type will be fixed with the calculated difference
210 // FIXME: change UI moving tax edit to line level in line edit mode, then this workaround will be obsolete.
211 foreach($this->get_taxes() as $tax_id => $tax)
213 if ($tax['Value'] != 0 && isset($tax['Override']) && ($tax['Value'] != $tax['Override']))
215 foreach($this->grn_items as $id => $line)
216 if ($line->gl_amounts[0]['tax_type_id'] == $tax_id) // assumed single tax rate on item, so always gl_mount[0] is valid
218 $diff = $tax['Override'] - $tax['Value'];
219 $this->grn_items[$id]->gl_amounts[0]['Value'] += $diff;
220 if ($this->vat_category() != VC_NONDEDUCT)
221 $this->grn_items[$id]->gl_amounts[0]['Deductible'] += $diff;
223 $this->grn_items[$id]->gl_amounts['Cost'] += $diff;
224 // when supplier uses net prices the price is exact, so don't fix net, still record exact VAT.
225 if ($this->tax_included) {
226 $this->grn_items[$id]->gl_amounts['Net'] -= $diff;
227 $this->grn_items[$id]->gl_amounts['Cost'] -= $diff;
234 } /* end of class defintion */
239 /* Contains relavent information from the purch_order_details as well to provide in cached form,
240 all the info to do the necessary entries without looking up ie additional queries of the database again */
248 var $item_description;
249 var $this_quantity_inv;
253 var $prev_quantity_inv;
257 var $gl_amounts; // splited line value (after call to split_line_value method
260 function __construct($id, $po_detail_item, $item_code, $item_description, $qty_recd,
261 $prev_quantity_inv, $this_quantity_inv, $order_price, $chg_price,
262 $std_cost_unit, $gl_code, $tax_included)
266 $this->po_detail_item = $po_detail_item;
267 $this->item_code = $item_code;
268 $this->item_description = $item_description;
269 $this->qty_recd = $qty_recd;
270 $this->prev_quantity_inv = $prev_quantity_inv;
271 $this->this_quantity_inv = $this_quantity_inv;
272 $this->order_price = $order_price; // price on order
273 $this->chg_price = $chg_price;
274 $this->std_cost_unit = $std_cost_unit; // provisional cost for clearing
275 $this->gl_code = $gl_code;
276 $this->tax_included = $tax_included;
278 $opts = get_item($item_code);
279 $this->vat_category = $opts['vat_category'];
282 // function full_charge_price($tax_group_id, $trans_type=ST_PURCHORDER)
284 // return get_full_price_for_item($trans_type, $this->item_code,
285 // $this->chg_price, $tax_group_id, $this->tax_included);
288 function taxfree_charge_price($tax_group_id, $trans_type=ST_PURCHORDER)
290 return get_tax_free_price_for_item($trans_type, $this->item_code, $this->chg_price,
291 $tax_group_id, $this->tax_included);
295 Splits item value to parts posted to GL.
297 function split_item_value($cart)
302 return $this->gl_amounts = split_item_price($this->item_code, $this->chg_price*$this->this_quantity_inv, $cart->tax_group_id, $cart->tax_included,
303 ST_SUPPINVOICE, $vat_factor, $cart->tran_date);
318 function __construct($gl_code, $gl_act_name, $gl_dim, $gl_dim2, $amount, $memo_)
320 $this->gl_code = $gl_code;
321 $this->gl_act_name = $gl_act_name;
322 $this->gl_dim = $gl_dim;
323 $this->gl_dim2 = $gl_dim2;
324 $this->amount = $amount;
325 $this->memo_= $memo_;