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