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