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