New tax system implementation.
[fa-stable.git] / purchasing / includes / supp_trans_class.inc
1 <?php
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
13 */
14
15 include_once($path_to_root . "/taxes/tax_calc.inc");
16
17 class supp_trans 
18 {
19
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 */
22         var $supplier_id;
23         var $supplier_name;
24         var $terms;
25         var $ex_rate;
26         
27         var $tax_description;
28         var $tax_group_id;
29         var $tax_included;
30         
31         var $trans_type;        // invoice or credit
32         var $trans_no;
33
34         var $Comments;
35         var $tran_date;
36         var $due_date;
37         var $src_docs = array();                // source invoice for this credit note (if any)
38
39         var $supp_reference;
40         var $reference;
41         var $ov_amount;
42         var $ov_discount;
43         var $ov_gst;
44         var $credit = 0;
45         var $currency;
46         var $tax_overrides = array();           // array of taxes manually inserted during sales invoice entry
47
48         var $dimension,
49                 $dimension2;
50
51         function __construct($trans_type, $trans_no=0)
52         {
53                 $this->trans_type = $trans_type;
54                 /*Constructor function initialises a new Supplier Transaction object */
55                 $this->read($trans_type, $trans_no);
56         }
57
58         function read($trans_type, $trans_no)
59         {
60                 $this->trans_type = $trans_type;
61                 $this->trans_no = $trans_no;
62                 $this->grn_items = array();
63                 $this->gl_codes = array();
64                 if ($trans_no) {
65                         read_supp_invoice($trans_no, $trans_type, $this);
66                         if ($trans_type == ST_SUPPCREDIT)
67                         {
68                                 $this->src_docs = find_src_invoices($trans_no);
69                         }
70                         read_supplier_details_to_trans($this, $this->supplier_id);
71                 }
72         }
73
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='')
77         {
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);
81
82                 $this->src_docs = find_src_invoices($this);
83
84                 unset($this->tax_overrides); // cancel tax overrides after cart change
85                 return 1;
86         }
87
88         function add_gl_codes_to_trans($gl_code, $gl_act_name, $gl_dim, $gl_dim2, $amount, $memo_)
89         {
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
92                 return 1;
93         }
94
95         function remove_grn_from_trans($grn_item_id)
96         {
97                 unset($this->tax_overrides); // cancel tax overrides after cart change
98             unset($this->grn_items[$grn_item_id]);
99         }
100         function remove_gl_codes_from_trans($gl_code_counter)
101         {
102                 unset($this->tax_overrides); // cancel tax overrides after cart change
103                 unset($this->gl_codes[$gl_code_counter]);
104         }
105
106         function is_valid_trans_to_post()
107         {
108                 return (count($this->grn_items) > 0 || count($this->gl_codes) > 0 || 
109                         ($this->ov_amount != 0) || ($this->ov_discount > 0));
110         }
111
112         function clear_items()
113         {
114                 unset($this->grn_items);
115                 unset($this->gl_codes);
116                 $this->ov_amount = $this->ov_discount = $this->supplier_id = 0;
117
118                 $this->grn_items = array();
119                 $this->gl_codes = array();
120         }
121
122     function get_taxes($tax_group_id=null, $shipping_cost=0, $gl_codes=true)
123     {
124         $items = array();
125         $prices = array();
126
127         if ($tax_group_id == null)
128                 $tax_group_id = $this->tax_group_id;
129
130                 // preload the taxgroup
131                 $tax_group = get_tax_group_items_as_array($tax_group_id);
132
133         foreach ($this->grn_items as $ln_itm) 
134         {
135                 $items[] = $ln_itm->item_code;
136                 $prices[] =round( ($ln_itm->this_quantity_inv * $ln_itm->chg_price),
137                                 user_price_dec());
138         }
139
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);
144
145                 if (isset($this->tax_overrides))
146                         foreach($this->tax_overrides as $id => $value) // add values entered manually
147                         {
148                                 $taxes[$id]['Override'] = $value;
149                         }
150
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)
155                 {
156                         $index = is_tax_account($gl_code->gl_code);
157                         if ($index !== false)
158                         {
159                                 if ($gl_codes)
160                                         $taxes[$index]['Value'] += $gl_code->amount;
161                                 elseif (isset($this->tax_overrides))
162                                         $taxes[$index]['Override'] -= $gl_code->amount;
163                         }
164                 }
165
166         return $taxes;
167     }
168         //
169         //      Returns total invoice amount without taxes.
170         //
171     function get_total_taxfree($tax_group_id=null)
172     {
173         $total = 0;
174
175                 // preload the taxgroup !
176                 if ($tax_group_id != null)
177                         $tax_group = get_tax_group_items_as_array($tax_group_id);
178                 else
179                         $tax_group = null;
180
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)),
183                          user_price_dec());
184
185                 foreach ($this->gl_codes as $gl_line)
186                 {
187                         if (!is_tax_account($gl_line->gl_code))
188                                 $total += $gl_line->amount;
189                 }
190                 return $total;
191     }
192         //
193         //      Returns transaction total 
194         //
195         function get_items_total()
196         {
197                 $total = 0;
198
199                 foreach ($this->grn_items as $ln_itm)
200                         $total += round($ln_itm->this_quantity_inv * $ln_itm->chg_price, user_price_dec());
201
202                 foreach ($this->gl_codes as $gl_line)
203                 {
204                         if (!is_tax_account($gl_line->gl_code) || $this->tax_included)
205                                 $total += $gl_line->amount;
206                 }
207                 return $total;
208         }
209
210         function split_line_values()
211         {
212                 // split nominal line values
213                 foreach($this->grn_items as $line)
214                         $line->split_item_value($this);
215
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)
220                 {
221                         if ($tax['Value'] != 0 && isset($tax['Override']) && ($tax['Value'] != $tax['Override']))
222                         {
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
225                                         {
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;
230                                                 else
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;
236                                                 }
237                                                 break;
238                                         }
239                         }
240                 }
241         }
242 } /* end of class defintion */
243
244 class grn_item 
245 {
246
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 */
249
250         var $id;
251         var $po_detail_item;
252         var $item_code;
253         var $item_description;
254         var $qty_recd;
255         var $prev_quantity_inv;
256         var $this_quantity_inv;
257         var $order_price;
258         var $chg_price;
259         var $std_cost_unit;
260         var $gl_code;
261         var $tax_included;
262         var $gl_amounts;        // splited line value (after call to split_line_value method
263         var $vat_category;
264
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)
268         {
269
270                 $this->id = $id;
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;
282
283                 $opts = get_item($item_code);
284                 $this->vat_category = $opts['vat_category'];
285         }
286
287         function full_charge_price($tax_group_id, $trans_type=ST_PURCHORDER)
288         {
289                 return get_full_price_for_item($trans_type, $this->item_code, 
290                   $this->chg_price, $tax_group_id, $this->tax_included);
291         }
292
293         function taxfree_charge_price($tax_group_id, $trans_type=ST_PURCHORDER)
294         {
295                 return get_tax_free_price_for_item($trans_type, $this->item_code, $this->chg_price, 
296                   $tax_group_id, $this->tax_included);
297         }
298
299         function taxfree_value($tax_group_id, $trans_type=ST_PURCHORDER)
300         {
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);
303         }
304
305         /*
306                 Splits item value to parts posted to GL.
307         */
308         function split_item_value($cart)
309         {
310
311         $vat_factor = 1;
312
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);
315         }
316 }
317
318
319 class gl_codes 
320 {
321
322         var $gl_code;
323         var $gl_act_name;
324         var $gl_dim;
325         var $gl_dim2;
326         var $amount;
327         var $memo_;
328
329         function __construct($gl_code, $gl_act_name, $gl_dim, $gl_dim2, $amount, $memo_)
330         {
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_;
337         }
338 }
339