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