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