Example #1
0
 public function actionDeleteCoupon()
 {
     $coupons = new Coupon();
     if (\Yii::$app->request->post('coupon_id') != '') {
         $coupons->deleteCoupon(\Yii::$app->request->post('coupon_id'));
         return $this->redirect(['index']);
     }
 }
 /**
  * Displays a single CouponType model.
  * @param integer $id
  * @return mixed
  */
 public function actionSend($id)
 {
     //if(!Yii::$app->user->can('viewYourAuth')) throw new ForbiddenHttpException(Yii::t('app', 'No Auth'));
     if (Yii::$app->request->post()) {
         if (isset(Yii::$app->request->post('CouponType')['users'])) {
             $strUser = Yii::$app->request->post('CouponType')['users'];
             $arrayUser = explode(',', $strUser);
             foreach ($arrayUser as $user) {
                 $user = User::find()->where(['username' => $user])->one();
                 $userId = isset($user) ? $user->id : null;
                 $couponType = CouponType::findOne($id);
                 if ($userId) {
                     $coupon = new Coupon();
                     $coupon->user_id = $userId;
                     $coupon->coupon_type_id = $id;
                     $coupon->money = $couponType->money;
                     $coupon->min_amount = $couponType->min_amount;
                     $coupon->started_at = $couponType->started_at;
                     $coupon->ended_at = $couponType->ended_at;
                     $coupon->user_id = $userId;
                     $coupon->save();
                 }
             }
         } elseif (isset(Yii::$app->request->post('CouponType')['numbers']) && Yii::$app->request->post('CouponType')['numbers'] > 0) {
             for ($i = 0; $i < Yii::$app->request->post('CouponType')['numbers']; $i++) {
                 $couponType = CouponType::findOne($id);
                 $coupon = new Coupon();
                 $coupon->coupon_type_id = $id;
                 $coupon->money = $couponType->money;
                 $coupon->min_amount = $couponType->min_amount;
                 $coupon->started_at = $couponType->started_at;
                 $coupon->ended_at = $couponType->ended_at;
                 $coupon->sn = Yii::$app->security->generateRandomString(8);
                 $coupon->save();
             }
         }
         return $this->redirect(['coupon/index']);
     } else {
         $model = $this->findModel($id);
         if ($model->type == CouponType::COUPON_TYPE_AMOUNT) {
             $arrayUserId = ArrayHelper::map(Order::find()->where('amount > :amount', [':amount' => $model->min_goods_amount])->all(), 'user_id', 'user_id');
             $arrayUserName = ArrayHelper::map(User::find()->where(['id' => $arrayUserId])->all(), 'id', 'username');
             $model->users = implode(',', $arrayUserName);
         }
         return $this->render('sendForm', ['model' => $model]);
     }
 }
Example #3
0
 /**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     $query = Coupon::find();
     $query->orderBy(['created_at' => SORT_DESC]);
     $dataProvider = new ActiveDataProvider(['query' => $query]);
     if ($this->load($params) && !$this->validate()) {
         return $dataProvider;
     }
     $query->andFilterWhere(['id' => $this->id, 'user_id' => $this->user_id, 'coupon_type_id' => $this->coupon_type_id, 'money' => $this->money, 'min_amount' => $this->min_amount, 'started_at' => $this->started_at, 'ended_at' => $this->ended_at, 'order_id' => $this->order_id, 'used_at' => $this->used_at, 'created_at' => $this->created_at, 'updated_at' => $this->updated_at, 'created_by' => $this->created_by, 'updated_by' => $this->updated_by]);
     $query->andFilterWhere(['like', 'sn', $this->sn]);
     return $dataProvider;
 }
Example #4
0
 public function actionCoupon($type = 1)
 {
     $now = time();
     if ($type == 1) {
         $query = Coupon::find()->where(['and', 'user_id = ' . Yii::$app->user->id, 'used_at = 0', 'ended_at >= ' . $now]);
     } elseif ($type == 2) {
         $query = Coupon::find()->where(['and', 'user_id = ' . Yii::$app->user->id, 'used_at > 0']);
     } elseif ($type == 3) {
         $query = Coupon::find()->where(['and', 'user_id = ' . Yii::$app->user->id, 'ended_at < ' . $now]);
     }
     $dataProvider = new ActiveDataProvider(['query' => $query, 'pagination' => ['defaultPageSize' => Yii::$app->params['defaultPageSizeOrder']], 'sort' => ['defaultOrder' => ['created_at' => SORT_DESC]]]);
     return $this->render('coupon', ['models' => $dataProvider->getModels(), 'pagination' => $dataProvider->pagination]);
 }
Example #5
0
 public function actionAjaxCouponCode($sn)
 {
     $sn = trim($sn);
     Yii::$app->response->format = Response::FORMAT_JSON;
     if (!$sn) {
         return ['status' => -1];
     }
     $coupon = Coupon::find()->where(['sn' => $sn])->one();
     if (!$coupon) {
         return ['status' => -1];
     }
     if ($coupon->used_at > 0) {
         return ['status' => -2];
     } elseif ($coupon->ended_at < time()) {
         return ['status' => -3];
     }
     return ['status' => 1, 'sn' => $coupon->sn, 'money' => $coupon->money];
 }
Example #6
0
 /**
  * Finds the Coupon model based on its primary key value.
  * If the model is not found, a 404 HTTP exception will be thrown.
  * @param integer $id
  * @return Coupon the loaded model
  * @throws NotFoundHttpException if the model cannot be found
  */
 protected function findModel($id)
 {
     if (($model = Coupon::findOne($id)) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }
Example #7
0
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getCoupons()
 {
     return $this->hasMany(Coupon::className(), ['coupon_type_id' => 'id']);
 }