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