Merged changes from mai trunk up to 2.3.1
[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 $editing_item, $deleting_item;
24
25         var $from_loc;
26         var $to_loc;
27         var $tran_date;
28         var $transfer_type;
29         var $increase;
30         var $memo_;
31         var $person_id;
32         var $branch_id;
33         var $reference;
34         
35         function items_cart($type)
36         {
37                 $this->trans_type = $type;
38                 $this->clear_items();
39         }
40
41         // --------------- line item functions
42
43         function add_to_cart($line_no, $stock_id, $qty, $standard_cost, $description=null)
44         {
45
46                 if (isset($stock_id) && $stock_id != "" && isset($qty))
47                 {
48                         $this->line_items[$line_no] = new line_item($stock_id, $qty,
49                                 $standard_cost, $description);
50                         return true;
51                 }
52                 else
53                 {
54                         // shouldn't come here under normal circumstances
55                         display_error("unexpected - adding an invalid item or null quantity", "", true);
56                 }
57
58                 return false;
59         }
60
61         function find_cart_item($stock_id)
62         {
63                 foreach($this->line_items as $line_no=>$line) {
64                         if ($line->stock_id == $stock_id)
65                                 return $this->line_items[$line_no];
66                 }
67                 return null;
68         }
69
70         function update_cart_item($line_no, $qty, $standard_cost)
71         {
72                 $this->line_items[$line_no]->quantity = $qty;
73                 $this->line_items[$line_no]->standard_cost = $standard_cost;
74         }
75
76         function remove_from_cart($line_no)
77         {
78                 array_splice($this->line_items, $line_no, 1);
79         }
80
81         function count_items()
82         {
83                 return count($this->line_items);
84         }
85
86         function check_qoh($location, $date_, $reverse=false)
87         {
88                 foreach ($this->line_items as $line_no => $line_item)
89                 {
90                         $item_ret = $line_item->check_qoh($location, $date_, $reverse);
91                         if ($item_ret != null)
92                                 return $line_no;
93                 }
94                 return -1;
95         }
96
97         // ----------- GL item functions
98
99         function add_gl_item($code_id, $dimension_id, $dimension2_id, $amount, $reference, $description=null)
100         {
101                 if (isset($code_id) && $code_id != "" && isset($amount) && isset($dimension_id)  &&
102                         isset($dimension2_id))
103                 {
104                         $this->gl_items[] = new gl_item($code_id, $dimension_id, $dimension2_id, $amount, $reference, $description);
105                         return true;
106                 }
107                 else
108                 {
109                         // shouldn't come here under normal circumstances
110                         display_error("unexpected - invalid parameters in add_gl_item($code_id, $dimension_id, $dimension2_id, $amount,...)", "", true);
111                 }
112
113                 return false;
114         }
115
116         function update_gl_item($index, $code_id, $dimension_id, $dimension2_id, $amount, $reference, $description=null)
117         {
118             $this->gl_items[$index]->code_id = $code_id;
119                 $this->gl_items[$index]->dimension_id = $dimension_id;
120                 $this->gl_items[$index]->dimension2_id = $dimension2_id;
121                 $this->gl_items[$index]->amount = $amount;
122                 $this->gl_items[$index]->reference = $reference;
123                 if ($description == null)
124                         $this->gl_items[$index]->description = get_gl_account_name($code_id);
125                 else
126                         $this->gl_items[$index]->description = $description;
127
128         }
129
130         function remove_gl_item($index)
131         {
132                 array_splice($this->gl_items, $index, 1);
133         }
134
135         function count_gl_items()
136         {
137                 return count($this->gl_items);
138         }
139
140         function gl_items_total()
141         {
142                 $total = 0;
143                 foreach ($this->gl_items as $gl_item)
144                         $total += $gl_item->amount;
145                 return $total;
146         }
147
148         function gl_items_total_debit()
149         {
150                 $total = 0;
151                 foreach ($this->gl_items as $gl_item)
152                 {
153                         if ($gl_item->amount > 0)
154                                 $total += $gl_item->amount;
155                 }
156                 return $total;
157         }
158
159         function gl_items_total_credit()
160         {
161                 $total = 0;
162                 foreach ($this->gl_items as $gl_item)
163                 {
164                         if ($gl_item->amount < 0)
165                                 $total += $gl_item->amount;
166                 }
167                 return $total;
168         }
169
170         // ------------ common functions
171
172         function clear_items()
173         {
174         unset($this->line_items);
175                 $this->line_items = array();
176
177         unset($this->gl_items);
178                 $this->gl_items = array();
179
180         }
181 }
182
183 //--------------------------------------------------------------------------------------------
184
185 class line_item
186 {
187         var $stock_id;
188         var $item_description;
189         var $units;
190         var $mb_flag;
191
192         var $quantity;
193         var $price;
194         var $standard_cost;
195
196         function line_item ($stock_id, $qty, $standard_cost=null, $description=null)
197         {
198                 $item_row = get_item($stock_id);
199
200                 if ($item_row == null)
201                         display_error("invalid item added to order : $stock_id", "");
202
203                 $this->mb_flag = $item_row["mb_flag"];
204                 $this->units = $item_row["units"];
205
206                 if ($description == null)
207                         $this->item_description = $item_row["description"];
208                 else
209                         $this->item_description = $description;
210
211                 if ($standard_cost == null)
212                         $this->standard_cost = $item_row["actual_cost"];
213                 else
214                         $this->standard_cost = $standard_cost;
215
216                 $this->stock_id = $stock_id;
217                 $this->quantity = $qty;
218                 //$this->price = $price;
219                 $this->price = 0;
220         }
221
222         function check_qoh($location, $date_, $reverse)
223         {
224                 global $SysPrefs;
225                 
226         if (!$SysPrefs->allow_negative_stock())
227         {
228                         if (has_stock_holding($this->mb_flag))
229                         {
230                                 $quantity = $this->quantity;
231                                 if ($reverse)
232                                         $quantity = -$this->quantity;
233
234                                 if ($quantity >= 0)
235                                         return null;
236
237                                 $qoh = get_qoh_on_date($this->stock_id, $location, $date_);
238                         if ($quantity + $qoh < 0)
239                         {
240                                 return $this;
241                         }
242                 }
243         }
244
245         return null;
246         }
247 }
248
249 //---------------------------------------------------------------------------------------
250
251 class gl_item
252 {
253
254         var $code_id;
255         var $dimension_id;
256         var $dimension2_id;
257         var $amount;
258         var $reference;
259         var $description;
260
261         function gl_item($code_id, $dimension_id, $dimension2_id, $amount, $reference,
262                 $description=null)
263         {
264                 //echo "adding $index, $code_id, $dimension_id, $amount, $reference<br>";
265
266                 if ($description == null)
267                         $this->description = get_gl_account_name($code_id);
268                 else
269                         $this->description = $description;
270
271                 $this->code_id = $code_id;
272                 $this->dimension_id = $dimension_id;
273                 $this->dimension2_id = $dimension2_id;
274                 $this->amount = $amount;
275                 $this->reference = $reference;
276         }
277 }
278
279 //---------------------------------------------------------------------------------------
280
281 ?>