Fixed error on new contact add.
[fa-stable.git] / includes / ui / items_cart.inc
index da1917df971d657be0caf9333bdf0e5e0ccdd19f..12e1ab244694ddbc2f9673aa43205ed4d1f3b06b 100644 (file)
@@ -1,16 +1,23 @@
 <?php
-
+/**********************************************************************
+    Copyright (C) FrontAccounting, LLC.
+       Released under the terms of the GNU General Public License, GPL, 
+       as published by the Free Software Foundation, either version 3 
+       of the License, or (at your option) any later version.
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
+    See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
+***********************************************************************/
 include_once($path_to_root . "/includes/prefs/sysprefs.inc");
 include_once($path_to_root . "/inventory/includes/inventory_db.inc");
 
 class items_cart
 {
-
+       var $trans_type;
        var $line_items;
        var $gl_items;
 
-       var $gl_item_count;
-
        var     $order_id;
 
        var $editing_item, $deleting_item;
@@ -23,22 +30,23 @@ class items_cart
        var $memo_;
        var $person_id;
        var $branch_id;
-
-       function items_cart()
+       var $reference;
+       
+       function items_cart($type)
        {
+               $this->trans_type = $type;
                $this->clear_items();
        }
 
        // --------------- line item functions
 
-       function add_to_cart($stock_id, $qty, $standard_cost, $description=null)
+       function add_to_cart($line_no, $stock_id, $qty, $standard_cost, $description=null)
        {
 
                if (isset($stock_id) && $stock_id != "" && isset($qty))
                {
-                       $this->line_items[$stock_id] = new line_item($stock_id, $qty,
+                       $this->line_items[$line_no] = new line_item($stock_id, $qty,
                                $standard_cost, $description);
-                       $this->clear_editing_flags();
                        return true;
                }
                else
@@ -52,25 +60,22 @@ class items_cart
 
        function find_cart_item($stock_id)
        {
-               if (isset($this->line_items[$stock_id]) && $this->line_items[$stock_id] != null)
-                       return $this->line_items[$stock_id];
+               foreach($this->line_items as $line_no=>$line) {
+                       if ($line->stock_id == $stock_id)
+                               return $this->line_items[$line_no];
+               }
                return null;
        }
 
-       function update_cart_item($update_item, $qty, $standard_cost)
+       function update_cart_item($line_no, $qty, $standard_cost)
        {
-               $this->line_items[$update_item]->quantity = $qty;
-               $this->line_items[$update_item]->standard_cost = $standard_cost;
-               $this->clear_editing_flags();
+               $this->line_items[$line_no]->quantity = $qty;
+               $this->line_items[$line_no]->standard_cost = $standard_cost;
        }
 
-       function remove_from_cart(&$stock_id)
+       function remove_from_cart($line_no)
        {
-               if (isset($stock_id))
-               {
-                       unset($this->line_items[$stock_id]);
-                       $this->clear_editing_flags();
-               }
+               array_splice($this->line_items, $line_no, 1);
        }
 
        function count_items()
@@ -80,12 +85,13 @@ class items_cart
 
        function check_qoh($location, $date_, $reverse=false)
        {
-               foreach ($this->line_items as $line_item)
+               foreach ($this->line_items as $line_no => $line_item)
                {
                        $item_ret = $line_item->check_qoh($location, $date_, $reverse);
                        if ($item_ret != null)
-                               return $line_item;
+                               return $line_no;
                }
+               return -1;
        }
 
        // ----------- GL item functions
@@ -95,41 +101,35 @@ class items_cart
                if (isset($code_id) && $code_id != "" && isset($amount) && isset($dimension_id)  &&
                        isset($dimension2_id))
                {
-                       $this->gl_items[$this->gl_item_count] = new gl_item($this->gl_item_count,
-                               $code_id, $dimension_id, $dimension2_id, $amount, $reference, $description);
-                       $this->gl_item_count++;
-                       $this->clear_editing_flags();
+                       $this->gl_items[] = new gl_item($code_id, $dimension_id, $dimension2_id, $amount, $reference, $description);
                        return true;
                }
                else
                {
                        // shouldn't come here under normal circumstances
-                       display_db_error("unexpected - adding an invalid item or null quantity", "", true);
+                       display_db_error("unexpected - invalid parameters in add_gl_item($code_id, $dimension_id, $dimension2_id, $amount,...)", "", true);
                }
 
                return false;
        }
 
-       function update_gl_item($index, $dimension_id, $dimension2_id, $amount, $reference, $description=null)
+       function update_gl_item($index, $code_id, $dimension_id, $dimension2_id, $amount, $reference, $description=null)
        {
-               $this->gl_items[$index]->index = $index;
+           $this->gl_items[$index]->code_id = $code_id;
                $this->gl_items[$index]->dimension_id = $dimension_id;
                $this->gl_items[$index]->dimension2_id = $dimension2_id;
                $this->gl_items[$index]->amount = $amount;
                $this->gl_items[$index]->reference = $reference;
-               if ($description != null)
+               if ($description == null)
+                       $this->gl_items[$index]->description = get_gl_account_name($code_id);
+               else
                        $this->gl_items[$index]->description = $description;
 
-               $this->clear_editing_flags();
        }
 
        function remove_gl_item($index)
        {
-               if (isset($index))
-               {
-                       unset($this->gl_items[$index]);
-                       $this->clear_editing_flags();
-               }
+               array_splice($this->gl_items, $index, 1);
        }
 
        function count_gl_items()
@@ -176,36 +176,8 @@ class items_cart
 
        unset($this->gl_items);
                $this->gl_items = array();
-               $this->gl_item_count = 1;
 
-               $this->clear_editing_flags();
        }
-
-       function clear_editing_flags()
-       {
-               $this->editing_item = $this->deleting_item = 0;
-       }
-
-       function get_editing_item()
-       {
-               return $this->editing_item;
-       }
-
-       function get_deleting_item()
-       {
-               return $this->deleting_item;
-       }
-
-       function is_editing_item($index)
-       {
-               return ($this->editing_item > 0) && ($this->editing_item == $index);
-       }
-
-       function is_deleting_item($index)
-       {
-               return ($this->deleting_item > 0) && ($this->deleting_item == $index);
-       }
-
 }
 
 //--------------------------------------------------------------------------------------------
@@ -249,7 +221,9 @@ class line_item
 
        function check_qoh($location, $date_, $reverse)
        {
-       if (!sys_prefs::allow_negative_stock())
+               global $SysPrefs;
+               
+       if (!$SysPrefs->allow_negative_stock())
        {
                        if (has_stock_holding($this->mb_flag))
                        {
@@ -277,7 +251,6 @@ class line_item
 class gl_item
 {
 
-       var $index;
        var $code_id;
        var $dimension_id;
        var $dimension2_id;
@@ -285,7 +258,7 @@ class gl_item
        var $reference;
        var $description;
 
-       function gl_item($index, $code_id, $dimension_id, $dimension2_id, $amount, $reference,
+       function gl_item($code_id, $dimension_id, $dimension2_id, $amount, $reference,
                $description=null)
        {
                //echo "adding $index, $code_id, $dimension_id, $amount, $reference<br>";
@@ -295,7 +268,6 @@ class gl_item
                else
                        $this->description = $description;
 
-               $this->index = $index;
                $this->code_id = $code_id;
                $this->dimension_id = $dimension_id;
                $this->dimension2_id = $dimension2_id;