c85f8ae7d9fdf27fcb831dbf56ec954967a7c16b
[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
33         var $Comments;
34         var $tran_date;
35         var $due_date;
36
37         var $supp_reference;
38         var $reference;
39         var $ov_amount;
40         var $ov_discount;
41         var $ov_gst;
42         var $gl_codes_counter=0;
43         var $credit = 0;
44         var $tax_overrides = array();           // array of taxes manually inserted during sales invoice entry
45
46         function supp_trans($trans_type)
47         {
48                 $this->trans_type = $trans_type;
49                 /*Constructor function initialises a new Supplier Transaction object */
50                 $this->grn_items = array();
51                 $this->gl_codes = array();
52         }
53
54         function add_grn_to_trans($grn_item_id, $po_detail_item, $item_code, $item_description, 
55                 $qty_recd, $prev_quantity_inv, $this_quantity_inv, $order_price, $chg_price, 
56                 $std_cost_unit, $gl_code)
57         {
58                 $this->grn_items[$grn_item_id] = new grn_item($grn_item_id, $po_detail_item, 
59                         $item_code, $item_description, $qty_recd, $prev_quantity_inv, $this_quantity_inv, 
60                         $order_price, $chg_price, $std_cost_unit, $gl_code, $this->tax_included);
61                 unset($this->tax_overrides); // cancel tax overrides after cart change
62                 return 1;
63         }
64
65         function add_gl_codes_to_trans($gl_code, $gl_act_name, $gl_dim, $gl_dim2, $amount, $memo_)
66         {
67                 $this->gl_codes[$this->gl_codes_counter] = new gl_codes($this->gl_codes_counter, 
68                         $gl_code, $gl_act_name, $gl_dim, $gl_dim2, $amount, $memo_);
69                 $this->gl_codes_counter++;
70                 unset($this->tax_overrides); // cancel tax overrides after cart change
71                 return 1;
72         }
73
74         function remove_grn_from_trans($grn_item_id)
75         {
76                 unset($this->tax_overrides); // cancel tax overrides after cart change
77             unset($this->grn_items[$grn_item_id]);
78         }
79         function remove_gl_codes_from_trans($gl_code_counter)
80         {
81                 unset($this->tax_overrides); // cancel tax overrides after cart change
82                 unset($this->gl_codes[$gl_code_counter]);
83         }
84
85         function is_valid_trans_to_post()
86         {
87                 return (count($this->grn_items) > 0 || count($this->gl_codes) > 0 || 
88                         ($this->ov_amount != 0) || ($this->ov_discount > 0));
89         }
90
91         function clear_items()
92         {
93                 unset($this->grn_items);
94                 unset($this->gl_codes);
95                 $this->ov_amount = $this->ov_discount = $this->supplier_id = 0;
96
97                 $this->grn_items = array();
98                 $this->gl_codes = array();
99         }
100
101     function get_taxes($tax_group_id=null, $shipping_cost=0, $gl_codes=true)
102     {
103         $items = array();
104         $prices = array();
105
106         if ($tax_group_id == null)
107                 $tax_group_id = $this->tax_group_id;
108
109                 // preload the taxgroup
110                 $tax_group = get_tax_group_items_as_array($tax_group_id);
111
112         foreach ($this->grn_items as $ln_itm) 
113         {
114                 $items[] = $ln_itm->item_code;
115                 $prices[] =round( ($ln_itm->this_quantity_inv * $ln_itm->chg_price),
116                                 user_price_dec());
117         }
118
119         if ($tax_group_id == null)
120                 $tax_group_id = $this->tax_group_id;
121         $taxes = get_tax_for_items($items, $prices, $shipping_cost, $tax_group_id, 
122                 $this->tax_included);
123
124                 if ($gl_codes)
125                 {
126                         foreach ($this->gl_codes as $gl_code)
127                         {
128                                 $index = is_tax_account($gl_code->gl_code);
129                                 if ($index !== false)
130                                 {
131                                         $taxes[$index]['Value'] += $gl_code->amount;
132                                 }
133                         }
134                 }
135
136                 if(isset($this->tax_overrides))
137                         foreach($this->tax_overrides as $id => $value) // add values entered manually
138                         {
139                                 $taxes[$id]['Override'] = $value;
140                         }
141         return $taxes;
142     }
143         //
144         //      Returns total invoice amount without taxes.
145         //
146     function get_total_taxfree($tax_group_id=null)
147     {
148         $total = 0;
149
150                 // preload the taxgroup !
151                 if ($tax_group_id != null)
152                         $tax_group = get_tax_group_items_as_array($tax_group_id);
153                 else
154                         $tax_group = null;
155
156                 foreach ($this->grn_items as $ln_itm)
157                 $total += round(($ln_itm->this_quantity_inv * $ln_itm->taxfree_charge_price($tax_group_id, $tax_group)),
158                          user_price_dec());
159
160                 foreach ($this->gl_codes as $gl_line)
161                 {
162                         if (!is_tax_account($gl_line->gl_code))
163                                 $total += $gl_line->amount;
164                 }
165                 return $total;
166     }
167
168         function get_items_total()
169         {
170                 $total = 0;
171
172                 foreach ($this->grn_items as $ln_itm)
173                         $total += round($ln_itm->this_quantity_inv * $ln_itm->chg_price, user_price_dec());
174
175                 foreach ($this->gl_codes as $gl_line)
176                 {
177                         if (!is_tax_account($gl_line->gl_code) || $this->tax_included)
178                                 $total += $gl_line->amount;
179                 }
180                 return $total;
181         }
182 } /* end of class defintion */
183
184 class grn_item 
185 {
186
187 /* Contains relavent information from the purch_order_details as well to provide in cached form,
188 all the info to do the necessary entries without looking up ie additional queries of the database again */
189
190         var $id;
191         var $po_detail_item;
192         var $item_code;
193         var $item_description;
194         var $qty_recd;
195         var $prev_quantity_inv;
196         var $this_quantity_inv;
197         var $order_price;
198         var $chg_price;
199         var $std_cost_unit;
200         var $gl_code;
201         var $tax_included;
202
203         function grn_item ($id, $po_detail_item, $item_code, $item_description, $qty_recd, 
204                 $prev_quantity_inv, $this_quantity_inv, $order_price, $chg_price,
205                 $std_cost_unit, $gl_code, $tax_included)
206         {
207
208                 $this->id = $id;
209                 $this->po_detail_item = $po_detail_item;
210                 $this->item_code = $item_code;
211                 $this->item_description = $item_description;
212                 $this->qty_recd = $qty_recd;
213                 $this->prev_quantity_inv = $prev_quantity_inv;
214                 $this->this_quantity_inv = $this_quantity_inv;
215                 $this->order_price =$order_price;
216                 $this->chg_price = $chg_price;
217                 $this->std_cost_unit = $std_cost_unit;
218                 $this->gl_code = $gl_code;
219                 $this->tax_included = $tax_included;
220         }
221
222         function full_charge_price($tax_group_id, $tax_group=null)
223         {
224                 return get_full_price_for_item($this->item_code, 
225                   $this->chg_price, $tax_group_id, $this->tax_included, $tax_group);
226         }
227
228         function taxfree_charge_price($tax_group_id, $tax_group=null)
229         {
230                 return get_tax_free_price_for_item($this->item_code, $this->chg_price, 
231                   $tax_group_id, $this->tax_included, $tax_group);
232         }
233 }
234
235
236 class gl_codes 
237 {
238
239         var $Counter;
240         var $gl_code;
241         var $gl_act_name;
242         var $gl_dim;
243         var $gl_dim2;
244         var $amount;
245         var $memo_;
246
247         function gl_codes ($Counter, $gl_code, $gl_act_name, $gl_dim, $gl_dim2, $amount, $memo_)
248         {
249
250         /* Constructor function to add a new gl_codes object with passed params */
251                 $this->Counter = $Counter;
252                 $this->gl_code = $gl_code;
253                 $this->gl_act_name = $gl_act_name;
254                 $this->gl_dim = $gl_dim;
255                 $this->gl_dim2 = $gl_dim2;
256                 $this->amount = $amount;
257                 $this->memo_= $memo_;
258         }
259 }
260
261 ?>