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