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