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