Пример #1
0
 public function actionCheckout()
 {
     if (Yii::app()->user->isGuest) {
         $this->redirect(array('/shop/register'));
     }
     $userId = Yii::app()->user->id;
     $User = BoxomaticUser::model()->findByPk($userId);
     if (!$User->Location) {
         Yii::app()->user->setFlash('warning', 'Please set your location');
         $this->redirect(array('/user/update', 'id' => $User->id));
     }
     $DeliveryDates = DeliveryDate::model()->with('Boxes')->findAll(array('condition' => 'DATE_SUB(date, INTERVAL -1 week) > NOW() AND date < DATE_ADD(NOW(), INTERVAL 1 MONTH)'));
     $BoxoCart = new BoxoCart();
     if (isset($_GET['set-date'])) {
         $BoxoCart->setDelivery_date_id($_GET['set-date']);
     }
     $AllDeliveryDates = DeliveryDate::model()->with('Locations')->findAll('Locations.location_id = :locationId', array(':locationId' => $User->location_id));
     if (isset($_POST['btn_recurring'])) {
         //recurring order button pressed
         $NextDD = $BoxoCart->getLastDeliveryDate();
         $DDs = $BoxoCart->Location->getFutureDeliveryDates($NextDD, (int) $_POST['months_advance'], $_POST['every']);
         $allOk = $BoxoCart->repeatCurrentOrder($DDs);
         if (!$allOk) {
             Yii::app()->user->setFlash('warning', '<strong>Warning:</strong> One or more of the products are not available on the given dates and have been removed.');
         }
     }
     $this->render('checkout', array('BoxoCart' => $BoxoCart, 'DeliveryDate' => $BoxoCart->DeliveryDate, 'Customer' => $User, 'AllDeliveryDates' => $AllDeliveryDates));
 }
Пример #2
0
 /**
  * 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));
 }