Backport of fixing vertical alignment on date picker icon
[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         /*
86                 Checks cart quantities on document_date.
87                 Returns array of stock_ids which stock quantities would go negative on some day.
88         */
89         function check_qoh($location, $date_, $reverse=false)
90         {
91                 $low_stock = array();
92
93                 // collect quantities by stock_id
94                 $qtys = array();
95                 foreach ($this->line_items as $line_no => $line_item)
96                 {
97                         $qty = $reverse ? -$line_item->quantity : $line_item->quantity;
98
99                         $qtys[$line_item->stock_id]['qty'] = $qty + @$qtys[$line_item->stock_id]['qty'];
100                         $qtys[$line_item->stock_id]['line'] = $line_no;
101                 }
102
103                 foreach($qtys as $stock_id => $sum)
104                 {
105                         $fail = check_negative_stock($stock_id, $sum['qty'], $location, $date_);
106                         if ($fail)
107                                 $low_stock[] = $stock_id;
108                 }
109
110                 return $low_stock;
111         }
112
113         // ----------- GL item functions
114
115         function add_gl_item($code_id, $dimension_id, $dimension2_id, $amount, $reference, $description=null)
116         {
117                 if (isset($code_id) && $code_id != "" && isset($amount) && isset($dimension_id)  &&
118                         isset($dimension2_id))
119                 {
120                         $this->gl_items[] = new gl_item($code_id, $dimension_id, $dimension2_id, $amount, $reference, $description);
121                         return true;
122                 }
123                 else
124                 {
125                         // shouldn't come here under normal circumstances
126                         display_error("unexpected - invalid parameters in add_gl_item($code_id, $dimension_id, $dimension2_id, $amount,...)", "", true);
127                 }
128
129                 return false;
130         }
131
132         function update_gl_item($index, $code_id, $dimension_id, $dimension2_id, $amount, $reference, $description=null)
133         {
134             $this->gl_items[$index]->code_id = $code_id;
135                 $this->gl_items[$index]->dimension_id = $dimension_id;
136                 $this->gl_items[$index]->dimension2_id = $dimension2_id;
137                 $this->gl_items[$index]->amount = $amount;
138                 $this->gl_items[$index]->reference = $reference;
139                 if ($description == null)
140                         $this->gl_items[$index]->description = get_gl_account_name($code_id);
141                 else
142                         $this->gl_items[$index]->description = $description;
143
144         }
145
146         function remove_gl_item($index)
147         {
148                 array_splice($this->gl_items, $index, 1);
149         }
150
151         function count_gl_items()
152         {
153                 return count($this->gl_items);
154         }
155
156         function gl_items_total()
157         {
158                 $total = 0;
159                 foreach ($this->gl_items as $gl_item)
160                         $total += $gl_item->amount;
161                 return $total;
162         }
163
164         function gl_items_total_debit()
165         {
166                 $total = 0;
167                 foreach ($this->gl_items as $gl_item)
168                 {
169                         if ($gl_item->amount > 0)
170                                 $total += $gl_item->amount;
171                 }
172                 return $total;
173         }
174
175         function gl_items_total_credit()
176         {
177                 $total = 0;
178                 foreach ($this->gl_items as $gl_item)
179                 {
180                         if ($gl_item->amount < 0)
181                                 $total += $gl_item->amount;
182                 }
183                 return $total;
184         }
185
186         // ------------ common functions
187
188         function clear_items()
189         {
190         unset($this->line_items);
191                 $this->line_items = array();
192
193         unset($this->gl_items);
194                 $this->gl_items = array();
195
196         }
197 }
198
199 //--------------------------------------------------------------------------------------------
200
201 class line_item
202 {
203         var $stock_id;
204         var $item_description;
205         var $units;
206         var $mb_flag;
207
208         var $quantity;
209         var $price;
210         var $standard_cost;
211
212         function line_item ($stock_id, $qty, $standard_cost=null, $description=null)
213         {
214                 $item_row = get_item($stock_id);
215
216                 if ($item_row == null)
217                         display_error("invalid item added to order : $stock_id", "");
218
219                 $this->mb_flag = $item_row["mb_flag"];
220                 $this->units = $item_row["units"];
221
222                 if ($description == null)
223                         $this->item_description = $item_row["description"];
224                 else
225                         $this->item_description = $description;
226
227                 if ($standard_cost == null)
228                         $this->standard_cost = $item_row["actual_cost"];
229                 else
230                         $this->standard_cost = $standard_cost;
231
232                 $this->stock_id = $stock_id;
233                 $this->quantity = $qty;
234                 //$this->price = $price;
235                 $this->price = 0;
236         }
237
238         /*
239                 This method is generally obsolete and subject to removal in FA 2.4 (preserved for now to support 2.3 extensions).
240                 Use items_cart::check_qoh instead.
241         */
242         function check_qoh($location, $date_, $reverse)
243         {
244                 global $SysPrefs;
245
246         if (!$SysPrefs->allow_negative_stock())
247         {
248                         if (has_stock_holding($this->mb_flag))
249                         {
250                                 $quantity = $this->quantity;
251                                 if ($reverse)
252                                         $quantity = -$this->quantity;
253
254                                 if ($quantity >= 0)
255                                         return null;
256
257                                 $fail = check_negative_stock($this->stock_id, $quantity, $location, $date_);
258                                 if ($fail)
259                                         return $this;
260                 }
261         }
262
263         return null;
264         }
265 }
266
267 //---------------------------------------------------------------------------------------
268
269 class gl_item
270 {
271
272         var $code_id;
273         var $dimension_id;
274         var $dimension2_id;
275         var $amount;
276         var $reference;
277         var $description;
278
279         function gl_item($code_id, $dimension_id, $dimension2_id, $amount, $reference,
280                 $description=null)
281         {
282                 //echo "adding $index, $code_id, $dimension_id, $amount, $reference<br>";
283
284                 if ($description == null)
285                         $this->description = get_gl_account_name($code_id);
286                 else
287                         $this->description = $description;
288
289                 $this->code_id = $code_id;
290                 $this->dimension_id = $dimension_id;
291                 $this->dimension2_id = $dimension2_id;
292                 $this->amount = $amount;
293                 $this->reference = $reference;
294         }
295 }
296
297 //---------------------------------------------------------------------------------------
298
299 ?>