/**
  * Manages all models.
  */
 public function actionIndex($cat = null)
 {
     if (!$cat) {
         $cat = SnapUtil::config('boxomatic/supplier_product_feature_category');
     }
     $BoxoCart = new BoxoCart();
     if (isset($_GET['set-date'])) {
         $BoxoCart->setDelivery_date_id($_GET['set-date']);
     }
     if (isset($_POST['BoxoCart']['location_id'])) {
         $BoxoCart->location_id = $_POST['BoxoCart']['location_id'];
         $this->refresh();
     }
     if (isset($_POST['Order']) && isset($_POST['add_to_cart'])) {
         if ($BoxoCart->addItems($_POST['Order'])) {
             Yii::app()->user->setFlash('success', 'Item added to cart.');
             #redirect to category
             if ($cat == 'box') {
                 $this->redirect(array('shop/index', 'date' => Yii::app()->request->getParam('date'), 'cat' => SnapUtil::config('boxomatic/redirectBoxCategory')));
             }
         }
         $this->refresh();
     }
     if (isset($_POST['Order']) && (isset($_POST['update_cart']) || isset($_POST['checkout']))) {
         if ($BoxoCart->updateItems($_POST['Order'])) {
             Yii::app()->user->setFlash('success', 'Cart updated.');
         }
         if (isset($_POST['checkout'])) {
             $this->redirect(array('shop/checkout'));
         }
         $this->refresh();
     }
     $userId = Yii::app()->user->id;
     $User = BoxomaticUser::model()->findByPk($userId);
     $Category = Category::model()->findByPk($cat);
     $DeliveryDate = false;
     $dpProducts = false;
     $Location = $BoxoCart->Location;
     if ($Location) {
         if (!$BoxoCart->DeliveryDate) {
             $DeliveryDate = $BoxoCart->getNextDeliveryDate();
         } else {
             $DeliveryDate = $BoxoCart->DeliveryDate;
         }
         $BoxoCart->setDelivery_date_id($DeliveryDate->id);
         $products = SupplierProduct::getAvailableItems($DeliveryDate->id, $cat);
         $dpProducts = new CActiveDataProvider('SupplierProduct');
         $dpProducts->setData($products);
     }
     $this->render('index', array('dpProducts' => $dpProducts, 'DeliveryDate' => $DeliveryDate, 'Category' => $Category, 'Customer' => $User, 'curCat' => $cat, 'BoxoCart' => $BoxoCart, 'Location' => $Location));
 }
 /**
  * Updates a particular model.
  * If update is successful, the browser will be redirected to the 'view' page.
  * @param integer $id the ID of the model to be updated
  */
 public function actionUpdate($id)
 {
     $Customer = $this->loadModel($id);
     if ($Customer->id !== Yii::app()->user->id) {
         throw new CHttpException(403, 'You are not authorized to perform this action.');
     }
     if (isset($_POST['role']) && $_POST['role'] == 'customer') {
         $UserLoc = new UserLocation();
         $UserLoc->user_id = $Customer->user_id;
         $UserLoc->location_id = $Customer->location_id;
         $UserLoc->address = $Customer->user_address;
         $UserLoc->address2 = $Customer->user_address2;
         $UserLoc->suburb = $Customer->user_suburb;
         $UserLoc->state = $Customer->user_state;
         $UserLoc->postcode = $Customer->user_postcode;
         $UserLoc->phone = !empty($Customer->user_phone) ? $Customer->user_phone : $Customer->user_mobile;
         $UserLoc->save();
         $Customer->user_id = $Customer->user_id;
         $Customer->update(array('user_id'));
     }
     $allSaved = true;
     if (isset($_POST['Supplier'])) {
         $Supplier = $Customer->Supplier;
         $Supplier->attributes = $_POST['Supplier'];
         if (!$Supplier->update()) {
             $allSaved = false;
         }
     }
     if (isset($_POST['role'])) {
         $Customer->setRole($_POST['role']);
     }
     if (isset($_POST['BoxomaticUser'])) {
         $oldLocation = $Customer->location_id;
         $oldDeliveryDay = $Customer->delivery_day;
         $Customer->attributes = $_POST['BoxomaticUser'];
         $locationId = $_POST['BoxomaticUser']['delivery_location_key'];
         $custLocationId = new CDbExpression('NULL');
         if (strpos($locationId, '-')) {
             //has a customer location
             $parts = explode('-', $locationId);
             $locationId = $parts[1];
             $custLocationId = $parts[0];
         }
         $Customer->location_id = $locationId;
         $Customer->user_location_id = $custLocationId;
         $Customer->validate();
         if (!$Customer->update()) {
             $allSaved = false;
         }
         //Update the cart to prevent ordering on an unavailable day
         $BoxoCart = new BoxoCart();
         $BoxoCart->delivery_day = $Customer->delivery_day;
         $BoxoCart->setLocation_id($Customer->location_id);
         $BoxoCart->setDelivery_date_id($BoxoCart->getNextDeliveryDate()->id);
         //The frontend system currently doesn't handle ordering from multiple locations
         //so delete all orders if changing location
         if ($Customer->location_id != $oldLocation || $Customer->delivery_day != $oldDeliveryDay) {
             $deleted = false;
             foreach ($Customer->getFutureOrders() as $Order) {
                 $Order->delete();
                 $deleted = true;
             }
             $BoxoCart->emptyCart();
             if ($deleted) {
                 Yii::app()->user->setFlash('warning', 'All future orders removed.');
             }
         }
         if ($allSaved) {
             $this->redirect(array('user/update', 'id' => $Customer->id));
         }
     }
     $custLocDataProvider = null;
     $custLocDataProvider = new CActiveDataProvider('UserLocation', array('criteria' => array('condition' => 'user_id=' . $Customer->id)));
     $this->render('update', array('model' => $Customer, 'custLocDataProvider' => $custLocDataProvider));
 }