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 $line = 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);
86 $this->grn_items[$grn_item_id] = $line;
87 $this->src_docs = find_src_invoices($this);
89 unset($this->tax_overrides); // cancel tax overrides after cart change
93 function add_gl_codes_to_trans($gl_code, $gl_act_name, $gl_dim, $gl_dim2, $amount, $memo_)
95 $this->gl_codes[] = new gl_codes($gl_code, $gl_act_name, $gl_dim, $gl_dim2, $amount, $memo_);
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($gl_codes=true)
132 foreach ($this->grn_items as $ln_itm)
134 $items[] = $ln_itm->item_code;
135 $prices[] = round( $ln_itm->this_quantity_inv * $ln_itm->chg_price, user_price_dec());
138 $taxes = get_tax_for_items($this->trans_type, $items, $prices, $this->tax_group_id,
139 $this->tax_included);
141 if (isset($this->tax_overrides))
142 foreach($this->tax_overrides as $id => $value) // add values entered manually
144 $taxes[$id]['Override'] = $value;
147 // Taxes included in gl_codes table have exact value, but count to overrides as well.
148 // Therefore when we want to know taxes only for items (gl_codes==false),
149 // we have to subtract gl_taxes from override values.
150 foreach ($this->gl_codes as $gl_code)
152 $index = is_tax_account($gl_code->gl_code);
153 if ($index !== false)
156 $taxes[$index]['Value'] += $gl_code->amount;
157 elseif (isset($this->tax_overrides))
158 $taxes[$index]['Override'] -= $gl_code->amount;
164 // Returns total invoice amount without taxes.
166 function get_total_taxfree()
170 foreach ($this->grn_items as $ln_itm)
171 $total += round(($ln_itm->this_quantity_inv * $ln_itm->taxfree_charge_price()),
174 foreach ($this->gl_codes as $gl_line)
176 if (!is_tax_account($gl_line->gl_code))
177 $total += $gl_line->amount;
183 // Returns transaction total
185 function get_items_total()
189 foreach ($this->grn_items as $ln_itm)
190 $total += round($ln_itm->this_quantity_inv * $ln_itm->chg_price, user_price_dec());
192 foreach ($this->gl_codes as $gl_line)
194 if (!is_tax_account($gl_line->gl_code) || $this->tax_included)
195 $total += $gl_line->amount;
201 Split line value to cost and taxes.
202 Stores calculated amounts in $line->gl_amounts array.
204 function split_line_values()
206 // split nominal line values
207 foreach($this->grn_items as $line)
208 $line->split_item_value();
210 // Exact tax values are currently entered as tax totals, so we need to move the differences back on line level.
211 // currently first item with given tax type will be fixed with the calculated difference
212 // FIXME: change UI moving tax edit to line level in line edit mode, then this workaround will be obsolete.
213 foreach($this->get_taxes() as $tax_id => $tax)
215 if ($tax['Value'] != 0 && isset($tax['Override']) && ($tax['Value'] != $tax['Override']))
217 foreach($this->grn_items as $id => $line)
218 if ($line->gl_amounts[0]['tax_type_id'] == $tax_id) // assumed single tax rate on item, so always gl_amounts[0] is valid
220 $diff = $tax['Override'] - $tax['Value'];
221 $this->grn_items[$id]->gl_amounts[0]['Value'] += $diff;
222 if ($line->vat_category != VC_NONDEDUCT)
223 $this->grn_items[$id]->gl_amounts[0]['Deductible'] += $diff;
225 $this->grn_items[$id]->gl_amounts['Cost'] += $diff;
226 // when supplier uses net prices the price is exact, so don't fix net, still record exact VAT.
227 if ($this->tax_included) {
228 $this->grn_items[$id]->gl_amounts['Net'] -= $diff;
229 $this->grn_items[$id]->gl_amounts['Cost'] -= $diff;
236 } /* end of class defintion */
241 /* Contains relavent information from the purch_order_details as well to provide in cached form,
242 all the info to do the necessary entries without looking up ie additional queries of the database again */
250 var $item_description;
251 var $this_quantity_inv;
255 var $prev_quantity_inv;
259 var $gl_amounts; // splited line value (after call to split_line_value method
262 var $cart; // line context
264 function __construct($id, $po_detail_item, $item_code, $item_description, $qty_recd,
265 $prev_quantity_inv, $this_quantity_inv, $order_price, $chg_price,
266 $std_cost_unit, $gl_code, $tax_included)
270 $this->po_detail_item = $po_detail_item;
271 $this->item_code = $item_code;
272 $this->item_description = $item_description;
273 $this->qty_recd = $qty_recd;
274 $this->prev_quantity_inv = $prev_quantity_inv;
275 $this->this_quantity_inv = $this_quantity_inv;
276 $this->order_price = $order_price; // price on order
277 $this->chg_price = $chg_price;
278 $this->std_cost_unit = $std_cost_unit; // provisional cost for clearing
279 $this->gl_code = $gl_code;
280 $this->tax_included = $tax_included;
282 $opts = get_item($item_code);
283 $this->vat_category = $opts['vat_category'];
286 // function full_charge_price($tax_group_id, $trans_type=ST_PURCHORDER)
288 // return get_full_price_for_item($trans_type, $this->item_code,
289 // $this->chg_price, $tax_group_id, $this->tax_included);
292 function taxfree_charge_price()
294 $this->split_item_value();
295 return $this->gl_amounts['Net'];
299 Splits item value to parts posted to GL.
301 function split_item_value()
306 return $this->gl_amounts = split_item_price($this->item_code, $this->chg_price*$this->this_quantity_inv, $this->cart->tax_group_id, $this->cart->tax_included,
307 $this->cart->trans_type, $vat_factor);
322 function __construct($gl_code, $gl_act_name, $gl_dim, $gl_dim2, $amount, $memo_)
324 $this->gl_code = $gl_code;
325 $this->gl_act_name = $gl_act_name;
326 $this->gl_dim = $gl_dim;
327 $this->gl_dim2 = $gl_dim2;
328 $this->amount = $amount;
329 $this->memo_= $memo_;