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