Unstable release 2.
[fa-stable.git] / sales / includes / cart_class.inc
index 1bb715895825ec9282f4f1e53666e91c1dc024bc..d5558d30e36da8333355b1c70c011c8c1f959806 100644 (file)
@@ -12,7 +12,7 @@ iii) a credit note
 include_once($path_to_root . "/inventory/includes/inventory_db.inc");
 include_once($path_to_root . "/taxes/tax_calc.inc");
 
-class cart 
+class cart
 {
 
        var $line_items; /*array of objects of class line_details using the product id as the pointer */
@@ -24,50 +24,54 @@ class cart
        var $default_discount; // set to the customer's discount %
        var $direct_invoice;  // direct invoicing
        var $memo_; // memo_ on direct invoicing
-       
+
        var $deliver_to;
        var $delivery_address;
        var $phone;
-       
+
        var $email;
        var $cust_ref;
        var $Comments;
        var $Location;
        var $location_name;
-       
-       var $order_no; // the order number 
+
+       var $order_no; // the order number
 
        var $customer_name;
        var $customer_id;
        var $Branch;
-       
+
        var $orig_order_date;
-       
+
        var $ship_via;
        var $freight_cost;
-       
+
        var $tax_group_id;
        var $tax_group_name;
 
+       var $lines_on_order;
+
        function Cart()
        {
                /*Constructor function initialises a new shopping cart */
                $this->line_items = array();
                $this->default_sales_type = "";
                $this->direct_invoice=false;
+               $this->lines_on_order = 0;
        }
 
-       function add_to_cart($stock_id, $qty, $price, $disc, $qty_invoiced=0, $standard_cost=0, $description=null)
+       function add_to_cart($line_no, $id, $stock_id, $qty, $price, $disc, $qty_invoiced=0, $standard_cost=0, $description=null)
        {
 
                if (isset($stock_id) && $stock_id != "" && isset($qty) && $qty > 0)
                {
 
-                       $this->line_items[$stock_id] = new line_details($stock_id, $qty, $price, $disc, 
+                       $this->line_items[$line_no] = new line_details($line_no, $id, $stock_id, $qty, $price, $disc,
                                $qty_invoiced, $standard_cost, $description);
+                       $this->lines_on_order++;
                        return 1;
-               } 
-               else 
+               }
+               else
                {
                        // shouldn't come here under normal circumstances
                        display_db_error("unexpected - adding an invalid item or null quantity", "", true);
@@ -75,98 +79,89 @@ class cart
                return 0;
        }
 
-       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];
-               return null;
-       }
-       
-       function update_cart_item($update_item, $qty, $price, $disc)
+       function update_cart_item($line_no, $qty, $price, $disc)
        {
 
                if ($qty > 0)
                {
-                       $this->line_items[$update_item]->quantity = $qty;
+                       $this->line_items[$line_no]->quantity = $qty;
                }
-               $this->line_items[$update_item]->price = $price;
-               $this->line_items[$update_item]->discount_percent = $disc;
+               $this->line_items[$line_no]->price = $price;
+               $this->line_items[$line_no]->discount_percent = $disc;
        }
-       
-       function update_add_cart_item_qty($update_item, $qty)
+
+       function update_add_cart_item_qty($line_no, $qty)
        {
-               $this->line_items[$update_item]->quantity += $qty;
-       }       
+               $this->line_items[$line_no]->quantity += $qty;
+       }
 
-       function remove_from_cart(&$stock_id)
+       function remove_from_cart($line_no)
        {
-               if (isset($stock_id))
-               {
-                       unset($this->line_items[$stock_id]);
-               }
+               $this->line_items[$line_no]->Deleted = true;
        }
-       
-       function clear_items() 
+
+       function clear_items()
        {
        unset($this->line_items);
                $this->line_items = array();
                $this->default_sales_type = "";
                $this->customer_id = $this->order_no = 0;
+               $this->lines_on_order = 0;
        }
-       
-       function count_items() 
+
+       function count_items()
        {
                return count($this->line_items);
        }
-       
+
        function get_items_total_dispatch($tax_group_id=null)
        {
                $total = 0;
-               
+
                // preload the taxgroup !
                if ($tax_group_id != null)
                        $tax_group_array = get_tax_group_items_as_array($tax_group_id);
-               else            
+               else
                        $tax_group_array = null;
-               
-               foreach ($this->line_items as $ln_itm) 
+
+               foreach ($this->line_items as $ln_itm)
                {
-                       $total += ($ln_itm->qty_dispatched * $ln_itm->taxfree_price($tax_group_id, $tax_group_array) * (1 - $ln_itm->discount_percent));                        
-               }               
+                       $total += ($ln_itm->qty_dispatched * $ln_itm->taxfree_price($tax_group_id, $tax_group_array) * (1 - $ln_itm->discount_percent));
+               }
                return $total;
        }
-       
+
        function has_items_dispatch()
        {
-               foreach ($this->line_items as $ln_itm) 
+               foreach ($this->line_items as $ln_itm)
                {
                        if ($ln_itm->qty_dispatched > 0)
-                               return true;                    
-               }               
+                               return true;
+               }
                return false;
        }
-       
+
        function get_items_total($tax_group_id=null)
        {
                $total = 0;
-               
+
                // preload the taxgroup !
                if ($tax_group_id != null)
                        $tax_group_array = get_tax_group_items_as_array($tax_group_id);
-               else            
-                       $tax_group_array = null;                
-               
+               else
+                       $tax_group_array = null;
+
                foreach ($this->line_items as $ln_itm) {
-                       $total += ($ln_itm->quantity * $ln_itm->taxfree_price($tax_group_id, $tax_group_array) * (1 - $ln_itm->discount_percent));                                              
-               }               
+                       $total += ($ln_itm->quantity * $ln_itm->taxfree_price($tax_group_id, $tax_group_array) * (1 - $ln_itm->discount_percent));
+               }
                return $total;
-       }       
+       }
 
        function any_already_delivered()
        {
                /* Checks if there have been deliveries of line items */
 
-               foreach ($this->line_items as $stock_item) 
+               foreach ($this->line_items as $stock_item)
                {
                        if ($stock_item->qty_inv !=0)
                        {
@@ -178,74 +173,83 @@ class cart
 
        }
 
-       function some_already_delivered($stock_id)
+       function some_already_delivered($line_no)
        {
                /* Checks if there have been deliveries of a specific line item */
-               if (isset($stock_id) && isset($this->line_items[$stock_id]) && 
-                       $this->line_items[$stock_id]->qty_inv != 0)
+               if (isset($this->line_items[$line_no]) &&
+                       $this->line_items[$line_no]->qty_inv != 0)
                {
                        return 1;
                }
                return 0;
        }
-       
+
     function get_taxes($tax_group_id=null, $shipping_cost=0)
     {
        $items = array();
        $prices = array();
-       
+
         if ($tax_group_id == null)
                $tax_group_id = $this->tax_group_id;
-               
+
                // preload the taxgroup !
                $tax_group_array = get_tax_group_items_as_array($tax_group_id);
-       
-        foreach ($this->line_items as $ln_itm) 
+
+        foreach ($this->line_items as $ln_itm)
         {
-               $items[] = $ln_itm->stock_id;
-               $prices[] = ($ln_itm->qty_dispatched * $ln_itm->taxfree_price($tax_group_id, $tax_group_array) * (1 - $ln_itm->discount_percent));
+               if (!$ln_itm->Deleted)
+               {
+                       $items[] = $ln_itm->stock_id;
+                       $prices[] = ($ln_itm->qty_dispatched * $ln_itm->taxfree_price($tax_group_id, $tax_group_array) * (1 - $ln_itm->discount_percent));
+               }
         }
-               
+
 
         $taxes = get_tax_for_items($items, $prices, $shipping_cost, $tax_group_id, $tax_group_array);
-        
+
         return $taxes;
-    }  
-       
+    }
+
 } /* end of class defintion */
 
-class line_details 
+class line_details
 {
+       var $line_no;
+       var $id;
        var $stock_id;
        var $item_description;
        var $units;
        var $mb_flag;
        var $tax_type;
        var $tax_type_name;
-                       
+
        var $quantity;
        var $price;
        var $discount_percent;
        var $qty_inv;
        var $qty_dispatched;
        var $standard_cost;
+       var $Deleted;
 
-       function line_details ($stock_id, $qty, $prc, $disc_percent,  
+       function line_details ($line_no, $id, $stock_id, $qty, $prc, $disc_percent,
                $qty_invoiced, $standard_cost, $description)
        {
        /* Constructor function to add a new LineDetail object with passed params */
+
+               $this->line_no = $line_no;
+               $this->id = $id;
                $item_row = get_item($stock_id);
-               
+
                if ($item_row == null)
                        display_db_error("invalid item added to order : $stock_id", "");
-               
+
                $this->mb_flag = $item_row["mb_flag"];
                $this->units = $item_row["units"];
                if ($description == null)
                        $this->item_description = $item_row["description"];
                else
                        $this->item_description = $description;
-               //$this->standard_cost = $item_row["material_cost"] + $item_row["labour_cost"] + $item_row["overhead_cost"]; 
+               //$this->standard_cost = $item_row["material_cost"] + $item_row["labour_cost"] + $item_row["overhead_cost"];
                $this->tax_type = $item_row["tax_type_id"];
                $this->tax_type_name = $item_row["tax_type_name"];
 
@@ -256,13 +260,14 @@ class line_details
                $this->qty_inv = $qty_invoiced;
                $this->qty_dispatched = $qty - $qty_invoiced;
                $this->standard_cost = $standard_cost;
+               $this->Deleted = false;
        }
-       
+
        function full_price()
        {
                return $this->price;
        }
-       
+
        function taxfree_price($tax_group_id, $tax_group_array=null)
        {
                if ($tax_group_id==null)