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