31b00b8a42bb65f9b0845771f8e6297a3fc8936d
[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         //db interface
20         var $trans_no;
21         var $trans_type;        // invoice or credit
22         var $supplier_id;
23         var $reference;
24         var $supp_reference;
25         var $tran_date;
26         var $due_date;
27         var $ov_amount;
28         var $ov_discount;
29         var $ov_gst;
30         var $ex_rate;
31
32         var $tax_included;
33
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 */
36         var $Comments;
37         //---
38
39         var $supplier_name;
40         var $terms;
41         
42         var $tax_description;
43         var $tax_group_id;
44
45         var $src_docs = array();                // source invoice for this credit note (if any)
46
47         var $credit = 0;
48         var $currency;
49         var $tax_overrides = array();           // array of taxes manually inserted during sales invoice entry
50
51         var $dimension,
52                 $dimension2;
53
54         function __construct($trans_type, $trans_no=0)
55         {
56                 $this->trans_type = $trans_type;
57                 /*Constructor function initialises a new Supplier Transaction object */
58                 $this->read($trans_type, $trans_no);
59         }
60
61         function read($trans_type, $trans_no)
62         {
63                 $this->trans_type = $trans_type;
64                 $this->trans_no = $trans_no;
65                 $this->grn_items = array();
66                 $this->gl_codes = array();
67                 if ($trans_no) {
68                         read_supp_invoice($trans_no, $trans_type, $this);
69
70                         if ($trans_type == ST_SUPPCREDIT)
71                                 $this->src_docs = find_src_invoices($trans_no);
72
73                         read_supplier_details_to_trans($this, $this->supplier_id);
74                 }
75         }
76
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='')
80         {
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);
84
85                 $this->src_docs = find_src_invoices($this);
86
87                 unset($this->tax_overrides); // cancel tax overrides after cart change
88                 return 1;
89         }
90
91         function add_gl_codes_to_trans($gl_code, $gl_act_name, $gl_dim, $gl_dim2, $amount, $memo_)
92         {
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
95                 return 1;
96         }
97
98         function remove_grn_from_trans($grn_item_id)
99         {
100                 unset($this->tax_overrides); // cancel tax overrides after cart change
101             unset($this->grn_items[$grn_item_id]);
102         }
103         function remove_gl_codes_from_trans($gl_code_counter)
104         {
105                 unset($this->tax_overrides); // cancel tax overrides after cart change
106                 unset($this->gl_codes[$gl_code_counter]);
107         }
108
109         function is_valid_trans_to_post()
110         {
111                 return (count($this->grn_items) > 0 || count($this->gl_codes) > 0 || 
112                         ($this->ov_amount != 0) || ($this->ov_discount > 0));
113         }
114
115         function clear_items()
116         {
117                 unset($this->grn_items);
118                 unset($this->gl_codes);
119                 $this->ov_amount = $this->ov_discount = $this->supplier_id = 0;
120
121                 $this->grn_items = array();
122                 $this->gl_codes = array();
123         }
124
125     function get_taxes($gl_codes=true)
126     {
127         $items = array();
128         $prices = array();
129
130         foreach ($this->grn_items as $ln_itm) 
131         {
132                 $items[] = $ln_itm->item_code;
133                 $prices[] = round( $ln_itm->this_quantity_inv * $ln_itm->chg_price, user_price_dec());
134         }
135
136         $taxes = get_tax_for_items($this->trans_type, $items, $prices, 0, $this->tax_group_id, 
137                 $this->tax_included);
138
139                 if (isset($this->tax_overrides))
140                         foreach($this->tax_overrides as $id => $value) // add values entered manually
141                         {
142                                 $taxes[$id]['Override'] = $value;
143                         }
144
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)
149                 {
150                         $index = is_tax_account($gl_code->gl_code);
151                         if ($index !== false)
152                         {
153                                 if ($gl_codes)
154                                         $taxes[$index]['Value'] += $gl_code->amount;
155                                 elseif (isset($this->tax_overrides))
156                                         $taxes[$index]['Override'] -= $gl_code->amount;
157                         }
158                 }
159         return $taxes;
160     }
161         //
162         //      Returns total invoice amount without taxes.
163         //
164     function get_total_taxfree($tax_group_id=null)
165     {
166         $total = 0;
167
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)),
170                          user_price_dec());
171
172                 foreach ($this->gl_codes as $gl_line)
173                 {
174                         if (!is_tax_account($gl_line->gl_code))
175                                 $total += $gl_line->amount;
176                 }
177                 return $total;
178     }
179
180         //
181         //      Returns transaction total 
182         //
183         function get_items_total()
184         {
185                 $total = 0;
186
187                 foreach ($this->grn_items as $ln_itm)
188                         $total += round($ln_itm->this_quantity_inv * $ln_itm->chg_price, user_price_dec());
189
190                 foreach ($this->gl_codes as $gl_line)
191                 {
192                         if (!is_tax_account($gl_line->gl_code) || $this->tax_included)
193                                 $total += $gl_line->amount;
194                 }
195                 return $total;
196         }
197
198         /*
199                 Split line value to cost and taxes.
200                 Stores calculated amounts in $line->gl_amounts array.
201         */
202         function split_line_values()
203         {
204                 // split nominal line values
205                 foreach($this->grn_items as $line)
206                         $line->split_item_value($this);
207
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)
212                 {
213                         if ($tax['Value'] != 0 && isset($tax['Override']) && ($tax['Value'] != $tax['Override']))
214                         {
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
217                                         {
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;
222                                                 else
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;
228                                                 }
229                                                 break;
230                                         }
231                         }
232                 }
233         }
234 } /* end of class defintion */
235
236 class grn_item 
237 {
238
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 */
241
242         var $id;
243
244         var $gl_code;
245
246         var $po_detail_item;
247         var $item_code;
248         var $item_description;
249         var $this_quantity_inv;
250         var $chg_price;
251
252         var $qty_recd;
253         var $prev_quantity_inv;
254         var $order_price;
255         var $std_cost_unit;
256         var $tax_included;
257         var $gl_amounts;        // splited line value (after call to split_line_value method
258         var $vat_category;
259
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)
263         {
264
265                 $this->id = $id;
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;
277
278                 $opts = get_item($item_code);
279                 $this->vat_category = $opts['vat_category'];
280         }
281
282 //      function full_charge_price($tax_group_id, $trans_type=ST_PURCHORDER)
283 //      {
284 //              return get_full_price_for_item($trans_type, $this->item_code, 
285 //                $this->chg_price, $tax_group_id, $this->tax_included);
286 //      }
287
288         function taxfree_charge_price($tax_group_id, $trans_type=ST_PURCHORDER)
289         {
290                 return get_tax_free_price_for_item($trans_type, $this->item_code, $this->chg_price, 
291                   $tax_group_id, $this->tax_included);
292         }
293
294         /*
295                 Splits item value to parts posted to GL.
296         */
297         function split_item_value($cart)
298         {
299
300         $vat_factor = 1;
301
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);
304         }
305 }
306
307
308 class gl_codes 
309 {
310
311         var $gl_code;
312         var $gl_act_name;
313         var $gl_dim;
314         var $gl_dim2;
315         var $amount;
316         var $memo_;
317
318         function __construct($gl_code, $gl_act_name, $gl_dim, $gl_dim2, $amount, $memo_)
319         {
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_;
326         }
327 }
328