/**
  * ACTION /addtobasket
  * Add the requested item to the basket.
  */
 public function addtobasket($data)
 {
     /* Retreive the TempBasketID (Cookie) for the current users basket. If it doesn't exist, create one */
     if (Store_BasketController::get_temp_basket_id()) {
         $TempBasketID = Store_BasketController::get_temp_basket_id();
     } else {
         $TempBasketID = Store_BasketController::set_temp_basket_id();
     }
     /* Try to fetch an Order record using the TempBasketID */
     $Order = DataObject::get_one("Order", "(`TempBasketID`='" . $TempBasketID . "')");
     /**
      * If an Order record doesn't exist, create the Order record first.
      */
     if (!$Order) {
         $n = new Order();
         $n->TempBasketID = $TempBasketID;
         $n->write();
         $Order = DataObject::get_one("Order", "(`TempBasketID`='" . $TempBasketID . "')");
     }
     /**
      * Do any Order_Items exist for this Product in the current Order? If yes, increment Quantity.
      * Otherwise, add a new item.
      */
     $count = new SQLQuery("COUNT(*)");
     $count->setFrom("Order_Items")->addWhere("(`OriginalProductID`='" . $data["ProductID"] . "' AND `TempBasketID`='" . $TempBasketID . "')");
     if ($count->execute()->value() > 0) {
         DB::Query("UPDATE Order_Items SET Quantity=Quantity + " . $data["Qty"] . " WHERE (`OriginalProductID`='" . $data["ProductID"] . "' AND `TempBasketID`='" . $TempBasketID . "')");
     } else {
         /**
          * Proceed to add the selected Product to the order as an Order_Items with the same TempBasketID.
          */
         $order_item = new Order_Items();
         $order_item->OrderID = $Order->ID;
         $order_item->OriginalProductID = $data["ProductID"];
         $order_item->Quantity = $data["Qty"];
         $order_item->TempBasketID = $TempBasketID;
         /**
          * As this order is still in its 'Shopping Basket' stages we will have no customer information
          * to calculate tax with at this time. Set tax rate and class to zero for now.
          */
         $order_item->TaxClassName = "To Be Calculated";
         $order_item->TaxClassRate = "0.00";
         /* Write to the database */
         $order_item->write();
     }
     /* Take the user to their Basket */
     return $this->redirect(Store_BasketController::get_link());
 }
 /**
  * CONTROLLER ACTION /place
  * Start the order process using the current basket.
  */
 public function place(SS_HTTPRequest $request)
 {
     /** 
      * If the basket is empty there is no order to process,
      * so redirect the user to their basket.
      */
     if (!Store_BasketController::is_basket_full()) {
         return $this->redirect(Store_BasketController::get_link());
     }
     /**
      * If no-body is currently signed in, redirect them to the login/register forms. 
      */
     if (!Customer::currentUser() && $request->param("ID") !== "one") {
         return $this->redirect($this->link() . "/place/one", 403);
     }
     /**
      * If the signed in user is not part of the 'Customers' group
      * then they are not permitted to make an order so render an
      * order error page that informs them of such.
      */
     if (Customer::currentUser()) {
         if (DB::Query("\n\t\t\t\tSELECT COUNT(*) FROM group_members\n\t\t\t\tWHERE (`GroupID`='" . DataObject::get_one("Group", "(`Title`='Customers')")->ID . "' \n\t\t\t\tAND `MemberID`='" . Customer::currentUserID() . "')\n\t\t\t")->value() < 1) {
             return $this->customise(array("Title" => "An unexpected error occurred."))->renderWith(array("Store_Order_Error_MemberGroup", "Page"));
         }
     }
     /**
      * If customer is signed in, but no ID is set in the URL, redirect to /two.
      */
     if (Customer::currentUser() && !$request->param("ID")) {
         return $this->redirect($this->link() . "/place/two", 403);
     }
     /**
      * Use switch() on $request->param("ID") to determine 
      * the stage of the order process.
      */
     switch ($request->param("ID")) {
         /**
          * ORDER PROCESS STEP ONE 
          * Prompt the user to login or create an account.
          * If the user is already signed in, redirect to stage two. 
          */
         case "one":
             if (!Customer::currentUser()) {
                 Session::set('BackURL', $this->link() . "/place/two");
                 return $this->customise(array("Title" => "Login/Register"))->renderWith(array("Store_Order_Step1", "Page"));
             } else {
                 return $this->redirect($this->link() . "/place/two");
             }
             break;
             /**
              * ORDER PROCESS STEP TWO 
              * Prompt the user to select their billing address from their
              * Customer_AddressBook. Also provide forms for the customer to complete 
              * should they wish to enter new address.
              */
         /**
          * ORDER PROCESS STEP TWO 
          * Prompt the user to select their billing address from their
          * Customer_AddressBook. Also provide forms for the customer to complete 
          * should they wish to enter new address.
          */
         case "two":
             return $this->customise(array("Title" => "Select Billing Address"))->renderWith(array("Store_Order_Step2", "Page"));
             break;
             /**
              * ORDER PROCESS STEP THREE 
              * Prompt the user to select their shipping address from their
              * Customer_AddressBook. Also provide forms for the customer to complete 
              * should they wish to enter new address.
              */
         /**
          * ORDER PROCESS STEP THREE 
          * Prompt the user to select their shipping address from their
          * Customer_AddressBook. Also provide forms for the customer to complete 
          * should they wish to enter new address.
          */
         case "three":
             return $this->customise(array("Title" => "Select Delivery Address"))->renderWith(array("Store_Order_Step3", "Page"));
             break;
             /**
              * ORDER PROCESS STEP FOUR
              * Prompt the user to select their preferred courier
              * should more than one be available.
              */
         /**
          * ORDER PROCESS STEP FOUR
          * Prompt the user to select their preferred courier
          * should more than one be available.
          */
         case "four":
             return $this->customise(array("Title" => "Select Courier"))->renderWith(array("Store_Order_Step4", "Page"));
             break;
             /**
              * ORDER PROCESS STEP FIVE
              * Based on all of the information entered show the final order summary
              * including tax with a choice of payment method.
              */
         /**
          * ORDER PROCESS STEP FIVE
          * Based on all of the information entered show the final order summary
          * including tax with a choice of payment method.
          */
         case "five":
             return $this->customise(array("Title" => "Order Summary &amp; Payment"))->renderWith(array("Store_Order_Step5", "Page"));
             break;
             /**
              * ORDER PROCESS ERRORS
              * If this switch statement is used then the order process hasn't followed
              * the correct process or has encountered an error. Render an appropriate error.
              */
         /**
          * ORDER PROCESS ERRORS
          * If this switch statement is used then the order process hasn't followed
          * the correct process or has encountered an error. Render an appropriate error.
          */
         default:
             switch ($request->param("ID")) {
                 /* There doesn't appear to enough stock to satisfy your order at this time. */
                 case "order-stock":
                     return $this->customise(array("Title" => "An unexpected error occurred."))->renderWith(array("Store_Order_Error_Stock", "Page"));
                     break;
                     /* Default Error Message */
                 /* Default Error Message */
                 default:
                     return $this->customise(array("Title" => "An unexpected error occurred."))->renderWith(array("Store_Order_Error", "Page"));
                     break;
             }
             break;
     }
 }