Cleanups.
[fa-stable.git] / includes / ui / items_cart.inc
1 <?php
2 /**********************************************************************
3     Copyright (C) FrontAccounting, LLC.
4         Released under the terms of the GNU General Public License, GPL, 
5         as published by the Free Software Foundation, either version 3 
6         of the License, or (at your option) any later version.
7     This program is distributed in the hope that it will be useful,
8     but WITHOUT ANY WARRANTY; without even the implied warranty of
9     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
10     See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
11 ***********************************************************************/
12 include_once($path_to_root . "/includes/prefs/sysprefs.inc");
13 include_once($path_to_root . "/inventory/includes/inventory_db.inc");
14
15 class items_cart
16 {
17         var $trans_type;
18         var $line_items;
19         var $gl_items;
20
21         var     $order_id;
22
23         var $from_loc;
24         var $to_loc;
25         var $tran_date;
26         var $transfer_type;
27         var $increase;
28         var $memo_;
29         var $person_id;
30         var $branch_id;
31         var $reference;
32         var $original_amount;
33         
34         function items_cart($type)
35         {
36                 $this->trans_type = $type;
37                 $this->clear_items();
38         }
39
40         // --------------- line item functions
41
42         function add_to_cart($line_no, $stock_id, $qty, $standard_cost, $description=null)
43         {
44
45                 if (isset($stock_id) && $stock_id != "" && isset($qty))
46                 {
47                         $this->line_items[$line_no] = new line_item($stock_id, $qty,
48                                 $standard_cost, $description);
49                         return true;
50                 }
51                 else
52                 {
53                         // shouldn't come here under normal circumstances
54                         display_error("unexpected - adding an invalid item or null quantity", "", true);
55                 }
56
57                 return false;
58         }
59
60         function find_cart_item($stock_id)
61         {
62                 foreach($this->line_items as $line_no=>$line) {
63                         if ($line->stock_id == $stock_id)
64                                 return $this->line_items[$line_no];
65                 }
66                 return null;
67         }
68
69         function update_cart_item($line_no, $qty, $standard_cost)
70         {
71                 $this->line_items[$line_no]->quantity = $qty;
72                 $this->line_items[$line_no]->standard_cost = $standard_cost;
73         }
74
75         function remove_from_cart($line_no)
76         {
77                 array_splice($this->line_items, $line_no, 1);
78         }
79
80         function count_items()
81         {
82                 return count($this->line_items);
83         }
84
85         function check_qoh($location, $date_, $reverse=false)
86         {
87                 foreach ($this->line_items as $line_no => $line_item)
88                 {
89                         $item_ret = $line_item->check_qoh($location, $date_, $reverse);
90                         if ($item_ret != null)
91                                 return $line_no;
92                 }
93                 return -1;
94         }
95
96         // ----------- GL item functions
97
98         function add_gl_item($code_id, $dimension_id, $dimension2_id, $amount, $reference, $description=null)
99         {
100                 if (isset($code_id) && $code_id != "" && isset($amount) && isset($dimension_id)  &&
101                         isset($dimension2_id))
102                 {
103                         $this->gl_items[] = new gl_item($code_id, $dimension_id, $dimension2_id, $amount, $reference, $description);
104                         return true;
105                 }
106                 else
107                 {
108                         // shouldn't come here under normal circumstances
109                         display_error("unexpected - invalid parameters in add_gl_item($code_id, $dimension_id, $dimension2_id, $amount,...)", "", true);
110                 }
111
112                 return false;
113         }
114
115         function update_gl_item($index, $code_id, $dimension_id, $dimension2_id, $amount, $reference, $description=null)
116         {
117             $this->gl_items[$index]->code_id = $code_id;
118                 $this->gl_items[$index]->dimension_id = $dimension_id;
119                 $this->gl_items[$index]->dimension2_id = $dimension2_id;
120                 $this->gl_items[$index]->amount = $amount;
121                 $this->gl_items[$index]->reference = $reference;
122                 if ($description == null)
123                         $this->gl_items[$index]->description = get_gl_account_name($code_id);
124                 else
125                         $this->gl_items[$index]->description = $description;
126
127         }
128
129         function remove_gl_item($index)
130         {
131                 array_splice($this->gl_items, $index, 1);
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
179         }
180 }
181
182 //--------------------------------------------------------------------------------------------
183
184 class line_item
185 {
186         var $stock_id;
187         var $item_description;
188         var $units;
189         var $mb_flag;
190
191         var $quantity;
192         var $price;
193         var $standard_cost;
194
195         function line_item ($stock_id, $qty, $standard_cost=null, $description=null)
196         {
197                 $item_row = get_item($stock_id);
198
199                 if ($item_row == null)
200                         display_error("invalid item added to order : $stock_id", "");
201
202                 $this->mb_flag = $item_row["mb_flag"];
203                 $this->units = $item_row["units"];
204
205                 if ($description == null)
206                         $this->item_description = $item_row["description"];
207                 else
208                         $this->item_description = $description;
209
210                 if ($standard_cost == null)
211                         $this->standard_cost = $item_row["actual_cost"];
212                 else
213                         $this->standard_cost = $standard_cost;
214
215                 $this->stock_id = $stock_id;
216                 $this->quantity = $qty;
217                 //$this->price = $price;
218                 $this->price = 0;
219         }
220
221         function check_qoh($location, $date_, $reverse)
222         {
223                 global $SysPrefs;
224                 
225         if (!$SysPrefs->allow_negative_stock())
226         {
227                         if (has_stock_holding($this->mb_flag))
228                         {
229                                 $quantity = $this->quantity;
230                                 if ($reverse)
231                                         $quantity = -$this->quantity;
232
233                                 if ($quantity >= 0)
234                                         return null;
235
236                                 $qoh = get_qoh_on_date($this->stock_id, $location, $date_);
237                         if ($quantity + $qoh < 0)
238                         {
239                                 return $this;
240                         }
241                 }
242         }
243
244         return null;
245         }
246 }
247
248 //---------------------------------------------------------------------------------------
249
250 class gl_item
251 {
252
253         var $code_id;
254         var $dimension_id;
255         var $dimension2_id;
256         var $amount;
257         var $reference;
258         var $description;
259
260         function gl_item($code_id, $dimension_id, $dimension2_id, $amount, $reference,
261                 $description=null)
262         {
263                 //echo "adding $index, $code_id, $dimension_id, $amount, $reference<br>";
264
265                 if ($description == null)
266                         $this->description = get_gl_account_name($code_id);
267                 else
268                         $this->description = $description;
269
270                 $this->code_id = $code_id;
271                 $this->dimension_id = $dimension_id;
272                 $this->dimension2_id = $dimension2_id;
273                 $this->amount = $amount;
274                 $this->reference = $reference;
275         }
276 }
277
278 //---------------------------------------------------------------------------------------
279
280 ?>