Cleanup: removed all closing tags in php files.
[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 $branch_id;
30         var $reference;
31         var $original_amount;
32         
33         function items_cart($type)
34         {
35                 $this->trans_type = $type;
36                 $this->clear_items();
37         }
38
39         // --------------- line item functions
40
41         function add_to_cart($line_no, $stock_id, $qty, $standard_cost, $description=null)
42         {
43
44                 if (isset($stock_id) && $stock_id != "" && isset($qty))
45                 {
46                         $this->line_items[$line_no] = new line_item($stock_id, $qty,
47                                 $standard_cost, $description);
48                         return true;
49                 }
50                 else
51                 {
52                         // shouldn't come here under normal circumstances
53                         display_error("unexpected - adding an invalid item or null quantity", "", true);
54                 }
55
56                 return false;
57         }
58
59         function find_cart_item($stock_id)
60         {
61                 foreach($this->line_items as $line_no=>$line) {
62                         if ($line->stock_id == $stock_id)
63                                 return $this->line_items[$line_no];
64                 }
65                 return null;
66         }
67
68         function update_cart_item($line_no, $qty, $standard_cost)
69         {
70                 $this->line_items[$line_no]->quantity = $qty;
71                 $this->line_items[$line_no]->standard_cost = $standard_cost;
72         }
73
74         function remove_from_cart($line_no)
75         {
76                 array_splice($this->line_items, $line_no, 1);
77         }
78
79         function count_items()
80         {
81                 return count($this->line_items);
82         }
83
84         /*
85                 Checks cart quantities on document_date.
86                 Returns array of stock_ids which stock quantities would go negative on some day.
87         */
88         function check_qoh($location, $date_, $reverse=false)
89         {
90                 $low_stock = array();
91
92                 // collect quantities by stock_id
93                 $qtys = array();
94                 foreach ($this->line_items as $line_no => $line_item)
95                 {
96                         $qty = $reverse ? -$line_item->quantity : $line_item->quantity;
97
98                         $qtys[$line_item->stock_id]['qty'] = $qty + @$qtys[$line_item->stock_id]['qty'];
99                         $qtys[$line_item->stock_id]['line'] = $line_no;
100                 }
101
102                 foreach($qtys as $stock_id => $sum)
103                 {
104                         $fail = check_negative_stock($stock_id, $sum['qty'], $location, $date_);
105                         if ($fail)
106                                 $low_stock[] = $stock_id;
107                 }
108
109                 return $low_stock;
110         }
111
112         // ----------- GL item functions
113
114         function add_gl_item($code_id, $dimension_id, $dimension2_id, $amount, $reference, $description=null, $person_id=null)
115         {
116                 if (isset($code_id) && $code_id != "" && isset($amount) && isset($dimension_id)  &&
117                         isset($dimension2_id))
118                 {
119                         $this->gl_items[] = new gl_item($code_id, $dimension_id, $dimension2_id, $amount, $reference, $description, $person_id);
120                         return true;
121                 }
122                 else
123                 {
124                         // shouldn't come here under normal circumstances
125                         display_error("unexpected - invalid parameters in add_gl_item($code_id, $dimension_id, $dimension2_id, $amount,...)", "", true);
126                 }
127
128                 return false;
129         }
130
131         function update_gl_item($index, $code_id, $dimension_id, $dimension2_id, $amount, $reference, $description=null, $person_id=null)
132         {
133             $this->gl_items[$index]->code_id = $code_id;
134             $this->gl_items[$index]->person_id = $person_id;
135
136                 $gl_type = is_subledger_account($code_id, $person_id);
137                 if ($gl_type)
138                 {
139                         $this->gl_items[$index]->person_type_id = $gl_type > 0 ? PT_CUSTOMER : PT_SUPPLIER;
140                         $this->gl_items[$index]->person_name = get_subaccount_name($code_id, $person_id);
141                 } else
142                 {
143                         $this->gl_items[$index]->person_type_id = $this->gl_items[$index]->person_name = '';
144                 }
145                 $this->gl_items[$index]->dimension_id = $dimension_id;
146                 $this->gl_items[$index]->dimension2_id = $dimension2_id;
147                 $this->gl_items[$index]->amount = $amount;
148                 $this->gl_items[$index]->reference = $reference;
149                 if ($description == null)
150                         $this->gl_items[$index]->description = get_gl_account_name($code_id);
151                 else
152                         $this->gl_items[$index]->description = $description;
153
154         }
155
156         function remove_gl_item($index)
157         {
158                 array_splice($this->gl_items, $index, 1);
159         }
160
161         function count_gl_items()
162         {
163                 return count($this->gl_items);
164         }
165
166         function gl_items_total()
167         {
168                 $total = 0;
169                 foreach ($this->gl_items as $gl_item)
170                         $total += $gl_item->amount;
171                 return $total;
172         }
173
174         function gl_items_total_debit()
175         {
176                 $total = 0;
177                 foreach ($this->gl_items as $gl_item)
178                 {
179                         if ($gl_item->amount > 0)
180                                 $total += $gl_item->amount;
181                 }
182                 return $total;
183         }
184
185         function gl_items_total_credit()
186         {
187                 $total = 0;
188                 foreach ($this->gl_items as $gl_item)
189                 {
190                         if ($gl_item->amount < 0)
191                                 $total += $gl_item->amount;
192                 }
193                 return $total;
194         }
195
196         // ------------ common functions
197
198         function clear_items()
199         {
200         unset($this->line_items);
201                 $this->line_items = array();
202
203         unset($this->gl_items);
204                 $this->gl_items = array();
205
206         }
207         //
208         //      Check if cart contains virtual subaccount (AP/AR) postings
209         //
210         function has_sub_accounts()
211         {
212                 foreach ($this->gl_items as $gl_item)
213                 {
214                         if ($gl_item->person_id)
215                                 return true;
216                 }
217                 return false;
218         }
219 }
220
221 //--------------------------------------------------------------------------------------------
222
223 class line_item
224 {
225         var $stock_id;
226         var $item_description;
227         var $units;
228         var $mb_flag;
229
230         var $quantity;
231         var $price;
232         var $standard_cost;
233
234         function line_item ($stock_id, $qty, $standard_cost=null, $description=null)
235         {
236                 $item_row = get_item($stock_id);
237
238                 if ($item_row == null)
239                         display_error("invalid item added to order : $stock_id", "");
240
241                 $this->mb_flag = $item_row["mb_flag"];
242                 $this->units = $item_row["units"];
243
244                 if ($description == null)
245                         $this->item_description = $item_row["description"];
246                 else
247                         $this->item_description = $description;
248
249                 if ($standard_cost == null)
250                         $this->standard_cost = $item_row["actual_cost"];
251                 else
252                         $this->standard_cost = $standard_cost;
253
254                 $this->stock_id = $stock_id;
255                 $this->quantity = $qty;
256                 //$this->price = $price;
257                 $this->price = 0;
258         }
259
260         /*
261                 This method is generally obsolete and subject to removal in FA 2.4 (preserved for now to support 2.3 extensions).
262                 Use items_cart::check_qoh instead.
263         */
264         function check_qoh($location, $date_, $reverse)
265         {
266                 global $SysPrefs;
267
268         if (!$SysPrefs->allow_negative_stock())
269         {
270                         if (has_stock_holding($this->mb_flag))
271                         {
272                                 $quantity = $this->quantity;
273                                 if ($reverse)
274                                         $quantity = -$this->quantity;
275
276                                 if ($quantity >= 0)
277                                         return null;
278
279                                 $fail = check_negative_stock($this->stock_id, $quantity, $location, $date_);
280                                 if ($fail)
281                                         return $this;
282                 }
283         }
284
285         return null;
286         }
287 }
288
289 //---------------------------------------------------------------------------------------
290
291 class gl_item
292 {
293
294         var $code_id;
295         var $dimension_id;
296         var $dimension2_id;
297         var $amount;
298         var $reference;
299         var $description;
300         var $person_id;
301         var $person_type_id;
302         var $person_name;
303
304         function gl_item($code_id, $dimension_id, $dimension2_id, $amount, $reference,
305                 $description=null, $person_id=null)
306         {
307                 //echo "adding $index, $code_id, $dimension_id, $amount, $reference<br>";
308
309                 if ($description == null)
310                         $this->description = get_gl_account_name($code_id);
311                 else
312                         $this->description = $description;
313
314                 $this->code_id = $code_id;
315                 $this->person_id = $person_id;
316                 $gl_type = is_subledger_account($code_id, $person_id);
317                 if ($gl_type)
318                 {
319                         $this->person_type_id = $gl_type > 0 ? PT_CUSTOMER : PT_SUPPLIER;
320                         $this->person_name = get_subaccount_name($code_id, $person_id);
321                 }
322                 $this->dimension_id = $dimension_id;
323                 $this->dimension2_id = $dimension2_id;
324                 $this->amount = $amount;
325                 $this->reference = $reference;
326         }
327 }
328
329 //---------------------------------------------------------------------------------------
330