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");
33 function items_cart($type)
35 $this->trans_type = $type;
39 // --------------- line item functions
41 function add_to_cart($line_no, $stock_id, $qty, $standard_cost, $description=null)
44 if (isset($stock_id) && $stock_id != "" && isset($qty))
46 $this->line_items[$line_no] = new line_item($stock_id, $qty,
47 $standard_cost, $description);
52 // shouldn't come here under normal circumstances
53 display_error("unexpected - adding an invalid item or null quantity", "", true);
59 function find_cart_item($stock_id)
61 foreach($this->line_items as $line_no=>$line) {
62 if ($line->stock_id == $stock_id)
63 return $this->line_items[$line_no];
68 function update_cart_item($line_no, $qty, $standard_cost)
70 $this->line_items[$line_no]->quantity = $qty;
71 $this->line_items[$line_no]->standard_cost = $standard_cost;
74 function remove_from_cart($line_no)
76 array_splice($this->line_items, $line_no, 1);
79 function count_items()
81 return count($this->line_items);
84 function check_qoh($location, $date_, $reverse=false)
86 foreach ($this->line_items as $line_no => $line_item)
88 $item_ret = $line_item->check_qoh($location, $date_, $reverse);
89 if ($item_ret != null)
95 // ----------- GL item functions
97 function add_gl_item($code_id, $dimension_id, $dimension2_id, $amount, $reference, $description=null, $person_id=null)
99 if (isset($code_id) && $code_id != "" && isset($amount) && isset($dimension_id) &&
100 isset($dimension2_id))
102 $this->gl_items[] = new gl_item($code_id, $dimension_id, $dimension2_id, $amount, $reference, $description, $person_id);
107 // shouldn't come here under normal circumstances
108 display_error("unexpected - invalid parameters in add_gl_item($code_id, $dimension_id, $dimension2_id, $amount,...)", "", true);
114 function update_gl_item($index, $code_id, $dimension_id, $dimension2_id, $amount, $reference, $description=null, $person_id=null)
116 $this->gl_items[$index]->code_id = $code_id;
117 $this->gl_items[$index]->person_id = $person_id;
119 $gl_type = is_subledger_account($code_id, $person_id);
122 $this->gl_items[$index]->person_type_id = $gl_type > 0 ? PT_CUSTOMER : PT_SUPPLIER;
123 $this->gl_items[$index]->person_name = get_subaccount_name($code_id, $person_id);
126 $this->gl_items[$index]->person_type_id = $this->gl_items[$index]->person_name = '';
128 $this->gl_items[$index]->dimension_id = $dimension_id;
129 $this->gl_items[$index]->dimension2_id = $dimension2_id;
130 $this->gl_items[$index]->amount = $amount;
131 $this->gl_items[$index]->reference = $reference;
132 if ($description == null)
133 $this->gl_items[$index]->description = get_gl_account_name($code_id);
135 $this->gl_items[$index]->description = $description;
139 function remove_gl_item($index)
141 array_splice($this->gl_items, $index, 1);
144 function count_gl_items()
146 return count($this->gl_items);
149 function gl_items_total()
152 foreach ($this->gl_items as $gl_item)
153 $total += $gl_item->amount;
157 function gl_items_total_debit()
160 foreach ($this->gl_items as $gl_item)
162 if ($gl_item->amount > 0)
163 $total += $gl_item->amount;
168 function gl_items_total_credit()
171 foreach ($this->gl_items as $gl_item)
173 if ($gl_item->amount < 0)
174 $total += $gl_item->amount;
179 // ------------ common functions
181 function clear_items()
183 unset($this->line_items);
184 $this->line_items = array();
186 unset($this->gl_items);
187 $this->gl_items = array();
191 // Check if cart contains virtual subaccount (AP/AR) postings
193 function has_sub_accounts()
195 foreach ($this->gl_items as $gl_item)
197 if ($gl_item->person_id)
204 //--------------------------------------------------------------------------------------------
209 var $item_description;
217 function line_item ($stock_id, $qty, $standard_cost=null, $description=null)
219 $item_row = get_item($stock_id);
221 if ($item_row == null)
222 display_error("invalid item added to order : $stock_id", "");
224 $this->mb_flag = $item_row["mb_flag"];
225 $this->units = $item_row["units"];
227 if ($description == null)
228 $this->item_description = $item_row["description"];
230 $this->item_description = $description;
232 if ($standard_cost == null)
233 $this->standard_cost = $item_row["actual_cost"];
235 $this->standard_cost = $standard_cost;
237 $this->stock_id = $stock_id;
238 $this->quantity = $qty;
239 //$this->price = $price;
243 function check_qoh($location, $date_, $reverse)
247 if (!$SysPrefs->allow_negative_stock())
249 if (has_stock_holding($this->mb_flag))
251 $quantity = $this->quantity;
253 $quantity = -$this->quantity;
258 $qoh = get_qoh_on_date($this->stock_id, $location, $date_);
259 if ($quantity + $qoh < 0)
270 //---------------------------------------------------------------------------------------
285 function gl_item($code_id, $dimension_id, $dimension2_id, $amount, $reference,
286 $description=null, $person_id=null)
288 //echo "adding $index, $code_id, $dimension_id, $amount, $reference<br>";
290 if ($description == null)
291 $this->description = get_gl_account_name($code_id);
293 $this->description = $description;
295 $this->code_id = $code_id;
296 $this->person_id = $person_id;
297 $gl_type = is_subledger_account($code_id, $person_id);
300 $this->person_type_id = $gl_type > 0 ? PT_CUSTOMER : PT_SUPPLIER;
301 $this->person_name = get_subaccount_name($code_id, $person_id);
303 $this->dimension_id = $dimension_id;
304 $this->dimension2_id = $dimension2_id;
305 $this->amount = $amount;
306 $this->reference = $reference;
310 //---------------------------------------------------------------------------------------