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