Changed license type to GPLv3 in top of files
[fa-stable.git] / includes / ui / items_cart.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 include_once($path_to_root . "/includes/prefs/sysprefs.inc");
13 include_once($path_to_root . "/inventory/includes/inventory_db.inc");
14
15 class items_cart
16 {
17         var $trans_type;
18         var $line_items;
19         var $gl_items;
20
21         var $gl_item_count;
22
23         var     $order_id;
24
25         var $editing_item, $deleting_item;
26
27         var $from_loc;
28         var $to_loc;
29         var $tran_date;
30         var $transfer_type;
31         var $increase;
32         var $memo_;
33         var $person_id;
34         var $branch_id;
35
36         function items_cart($type)
37         {
38                 $this->trans_type = $type;
39                 $this->clear_items();
40         }
41
42         // --------------- line item functions
43
44         function add_to_cart($line_no, $stock_id, $qty, $standard_cost, $description=null)
45         {
46
47                 if (isset($stock_id) && $stock_id != "" && isset($qty))
48                 {
49                         $this->line_items[$line_no] = new line_item($stock_id, $qty,
50                                 $standard_cost, $description);
51                         return true;
52                 }
53                 else
54                 {
55                         // shouldn't come here under normal circumstances
56                         display_db_error("unexpected - adding an invalid item or null quantity", "", true);
57                 }
58
59                 return false;
60         }
61
62         function find_cart_item($stock_id)
63         {
64                 foreach($this->line_items as $line_no=>$line) {
65                         if ($line->stock_id == $stock_id)
66                                 return $this->line_items[$line_no];
67                 }
68                 return null;
69         }
70
71         function update_cart_item($line_no, $qty, $standard_cost)
72         {
73                 $this->line_items[$line_no]->quantity = $qty;
74                 $this->line_items[$line_no]->standard_cost = $standard_cost;
75         }
76
77         function remove_from_cart($line_no)
78         {
79                 array_splice($this->line_items, $line_no, 1);
80         }
81
82         function count_items()
83         {
84                 return count($this->line_items);
85         }
86
87         function check_qoh($location, $date_, $reverse=false)
88         {
89                 foreach ($this->line_items as $line_no => $line_item)
90                 {
91                         $item_ret = $line_item->check_qoh($location, $date_, $reverse);
92                         if ($item_ret != null)
93                                 return $line_no;
94                 }
95                 return -1;
96         }
97
98         // ----------- GL item functions
99
100         function add_gl_item($code_id, $dimension_id, $dimension2_id, $amount, $reference, $description=null)
101         {
102                 if (isset($code_id) && $code_id != "" && isset($amount) && isset($dimension_id)  &&
103                         isset($dimension2_id))
104                 {
105                         $this->gl_items[$this->gl_item_count] = new gl_item($this->gl_item_count,
106                                 $code_id, $dimension_id, $dimension2_id, $amount, $reference, $description);
107                         $this->gl_item_count++;
108                         return true;
109                 }
110                 else
111                 {
112                         // shouldn't come here under normal circumstances
113                         display_db_error("unexpected - invalid parameters in add_gl_item($code_id, $dimension_id, $dimension2_id, $amount,...)", "", true);
114                 }
115
116                 return false;
117         }
118
119         function update_gl_item($index, $dimension_id, $dimension2_id, $amount, $reference, $description=null)
120         {
121                 $this->gl_items[$index]->index = $index;
122                 $this->gl_items[$index]->dimension_id = $dimension_id;
123                 $this->gl_items[$index]->dimension2_id = $dimension2_id;
124                 $this->gl_items[$index]->amount = $amount;
125                 $this->gl_items[$index]->reference = $reference;
126                 if ($description != null)
127                         $this->gl_items[$index]->description = $description;
128
129         }
130
131         function remove_gl_item($index)
132         {
133                 if (isset($index))
134                 {
135                         array_splice($this->gl_items, $line_no, 1);
136                 }
137         }
138
139         function count_gl_items()
140         {
141                 return count($this->gl_items);
142         }
143
144         function gl_items_total()
145         {
146                 $total = 0;
147                 foreach ($this->gl_items as $gl_item)
148                         $total += $gl_item->amount;
149                 return $total;
150         }
151
152         function gl_items_total_debit()
153         {
154                 $total = 0;
155                 foreach ($this->gl_items as $gl_item)
156                 {
157                         if ($gl_item->amount > 0)
158                                 $total += $gl_item->amount;
159                 }
160                 return $total;
161         }
162
163         function gl_items_total_credit()
164         {
165                 $total = 0;
166                 foreach ($this->gl_items as $gl_item)
167                 {
168                         if ($gl_item->amount < 0)
169                                 $total += $gl_item->amount;
170                 }
171                 return $total;
172         }
173
174         // ------------ common functions
175
176         function clear_items()
177         {
178         unset($this->line_items);
179                 $this->line_items = array();
180
181         unset($this->gl_items);
182                 $this->gl_items = array();
183                 $this->gl_item_count = 1;
184
185         }
186 }
187
188 //--------------------------------------------------------------------------------------------
189
190 class line_item
191 {
192         var $stock_id;
193         var $item_description;
194         var $units;
195         var $mb_flag;
196
197         var $quantity;
198         var $price;
199         var $standard_cost;
200
201         function line_item ($stock_id, $qty, $standard_cost=null, $description=null)
202         {
203                 $item_row = get_item($stock_id);
204
205                 if ($item_row == null)
206                         display_db_error("invalid item added to order : $stock_id", "");
207
208                 $this->mb_flag = $item_row["mb_flag"];
209                 $this->units = $item_row["units"];
210
211                 if ($description == null)
212                         $this->item_description = $item_row["description"];
213                 else
214                         $this->item_description = $description;
215
216                 if ($standard_cost == null)
217                         $this->standard_cost = $item_row["actual_cost"];
218                 else
219                         $this->standard_cost = $standard_cost;
220
221                 $this->stock_id = $stock_id;
222                 $this->quantity = $qty;
223                 //$this->price = $price;
224                 $this->price = 0;
225         }
226
227         function check_qoh($location, $date_, $reverse)
228         {
229         if (!sys_prefs::allow_negative_stock())
230         {
231                         if (has_stock_holding($this->mb_flag))
232                         {
233                                 $quantity = $this->quantity;
234                                 if ($reverse)
235                                         $quantity = -$this->quantity;
236
237                                 if ($quantity >= 0)
238                                         return null;
239
240                                 $qoh = get_qoh_on_date($this->stock_id, $location, $date_);
241                         if ($quantity + $qoh < 0)
242                         {
243                                 return $this;
244                         }
245                 }
246         }
247
248         return null;
249         }
250 }
251
252 //---------------------------------------------------------------------------------------
253
254 class gl_item
255 {
256
257         var $index;
258         var $code_id;
259         var $dimension_id;
260         var $dimension2_id;
261         var $amount;
262         var $reference;
263         var $description;
264
265         function gl_item($index, $code_id, $dimension_id, $dimension2_id, $amount, $reference,
266                 $description=null)
267         {
268                 //echo "adding $index, $code_id, $dimension_id, $amount, $reference<br>";
269
270                 if ($description == null)
271                         $this->description = get_gl_account_name($code_id);
272                 else
273                         $this->description = $description;
274
275                 $this->index = $index;
276                 $this->code_id = $code_id;
277                 $this->dimension_id = $dimension_id;
278                 $this->dimension2_id = $dimension2_id;
279                 $this->amount = $amount;
280                 $this->reference = $reference;
281         }
282 }
283
284 //---------------------------------------------------------------------------------------
285
286 ?>