Exemplo n.º 1
0
 public function postAddbatch()
 {
     $batchuniqid = Input::get('label') . date('d-m-y-h-i-s');
     $temp_array = "";
     for ($i = 0; $i < intval(Input::get('number')); $i++) {
         $temp_array[] = substr(base_convert(sha1(uniqid(mt_rand())), 16, 36), 0, 7);
     }
     $unique_array = array_unique($temp_array);
     if (count($unique_array) == Input::get('number')) {
         for ($i = 0; $i < intval(Input::get('number')); $i++) {
             $coupon = new Coupon();
             $coupon->code = strtoupper(Input::get('label') . $unique_array[$i]);
             $coupon->discount = Input::get('discount');
             $coupon->expiry = Date('Y-m-d', strtotime(Input::get('validity')));
             $coupon->type = Input::get('category_id') == 'cb' ? 'cashback' : 'discount';
             $coupon->status = 'activated';
             $coupon->min_val = Input::get('min_val');
             $coupon->parent_id = Input::get('referal');
             $coupon->batchuniqid = $batchuniqid;
             $coupon->save();
         }
         Session::flash('message', 'Coupon has been added sccessfully.');
         return View::make('coupons.addcoupons');
     } else {
         Session::flash('message', 'Some issue with code generation please try again.');
         return View::make('coupons.addcoupons');
     }
 }
Exemplo n.º 2
0
 /**
  * Generates child coupons with the $code property generated by the RandomGenerator input settings
  *
  * @param $number
  * @param int $length
  * @param string $type
  * @param string $pattern
  * @param int $separateEvery
  * @param string $separator
  * @return array
  * @throws ShopException
  */
 public function generateBulk($number, $length = 10, $type = RandomGenerator::TYPE_ALPHANUM, $pattern = 'X', $separateEvery = 0, $separator = '-')
 {
     if (!$this->isPersisted()) {
         throw new ShopException('Current coupon is not persisted, can\'t be used as parent');
     }
     $bulk = array();
     $i = 0;
     while ($i < $number) {
         $c = new Coupon();
         //$c->copy($this);
         $c->generateCode($length, $type, $pattern, $separateEvery, $separator);
         $c->module_srl = $this->module_srl;
         $c->parent_srl = $this->srl;
         $c->type = self::TYPE_CHILD;
         //save will throw ShopException if code is not unique
         try {
             $c->save();
         } catch (ShopException $e) {
             continue;
         }
         $i++;
         $bulk[] = $c;
     }
     return $bulk;
 }
Exemplo n.º 3
0
 public function actionCreate()
 {
     IsAuth::Admin();
     $model = new Coupon();
     if (isset($_POST['Coupon'])) {
         $model->date_added = new CDbExpression('NOW()');
         $model->attributes = $_POST['Coupon'];
         if ($model->save()) {
             $this->redirect(array('view', 'id' => $model->coupon_id));
         }
     }
     $this->render('create', array('model' => $model));
 }
 /**
  * Creates a new model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  */
 public function actionCreate()
 {
     $model = new Coupon();
     // Uncomment the following line if AJAX validation is needed
     // $this->performAjaxValidation($model);
     if (isset($_POST['Coupon'])) {
         $model->attributes = $_POST['Coupon'];
         if ($model->save()) {
             $this->redirect(array('view', 'id' => $model->id));
         }
     }
     $this->render('create', array('model' => $model));
 }
Exemplo n.º 5
0
 /**
  * Creates a new model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  */
 public function actionCreate()
 {
     $model = new Coupon();
     // Uncomment the following line if AJAX validation is needed
     // $this->performAjaxValidation($model);
     if (isset($_POST['Coupon'])) {
         $model->attributes = $_POST['Coupon'];
         $model->created_at = new CDbExpression('NOW()');
         $model->updated_at = new CDbExpression('NOW()');
         if ($model->save()) {
             $this->redirect(yii::app()->createUrl('/admin/coupon'));
         }
     }
     $this->render('create', array('model' => $model));
 }
Exemplo n.º 6
0
 public function actionCreate()
 {
     $model = new Coupon();
     $attributes = Yii::app()->getRequest()->getPost('Coupon');
     if ($attributes) {
         $model->attributes = $attributes;
         if ($model->save()) {
             Yii::app()->user->setFlash(yupe\widgets\YFlashMessages::SUCCESS_MESSAGE, Yii::t('CouponModule.coupon', 'Record created!'));
             $submitType = Yii::app()->getRequest()->getPost('submit-type');
             if ($submitType) {
                 $this->redirect([$submitType]);
             }
             $this->redirect(['update', 'id' => $model->id]);
         }
     }
     $this->render('create', ['model' => $model]);
 }
Exemplo n.º 7
0
 public function actionCreate()
 {
     $model = new Coupon();
     // Uncomment the following line if AJAX validation is needed
     // $this->performAjaxValidation($model);
     if (isset($_POST['Coupon'])) {
         $model->attributes = $_POST['Coupon'];
         if ($model->save()) {
             Yii::app()->user->setFlash(yupe\widgets\YFlashMessages::SUCCESS_MESSAGE, Yii::t('CouponModule.coupon', 'Record created!'));
             if (!isset($_POST['submit-type'])) {
                 $this->redirect(['update', 'id' => $model->id]);
             } else {
                 $this->redirect([$_POST['submit-type']]);
             }
         }
     }
     $this->render('create', ['model' => $model]);
 }
Exemplo n.º 8
0
 public static function gen($user, $event)
 {
     $result = false;
     $coupon = new Coupon();
     $code = Coupon::genCode(COUPON_CODE_LEN);
     if ($code == null) {
         elgg_log("ZHError ,Coupon::gen, genCode failed, zhaohu_id {$event->guid} logged_user_id " . elgg_get_logged_in_user_guid(), "ERROR");
         return $result;
     }
     $coupon->used = false;
     $coupon->code = $code;
     $coupon->user_guid = $user->guid;
     $coupon->event_guid = $event->guid;
     //$coupon->owner_guid = $user->guid;
     $coupon->count = 0;
     $coupon->save();
     return true;
     //for test
     //notifyOnCoupon($user, $event, $coupon, $group);
 }
Exemplo n.º 9
0
 public function save_coupon()
 {
     $id = Input::get('id');
     $coupon = Coupon::find($id);
     if (!$coupon) {
         $coupon = new Coupon();
     }
     $coupon->coupon_code = Input::get('coupon_code');
     $coupon->amount = Input::get('amount');
     $coupon->status = Input::get('status');
     $coupon->save();
     $message = "Successfully updated the coupon";
     $type = "success";
     return Redirect::to('/admin/coupons')->with('type', $type)->with('message', $message);
 }
Exemplo n.º 10
0
 /**
  * Inserts single codes, generates bulks
  */
 public function procShopToolInsertCoupon()
 {
     $repository = new CouponRepository();
     $args = Context::getRequestVars();
     $args->active = (int) $args->active;
     $args->module_srl = $this->module_info->module_srl;
     $logged_info = Context::get('logged_info');
     if ($logged_info->member_srl) $args->member_srl = $logged_info->member_srl;
     $args->ip = $_SERVER['REMOTE_ADDR'];
     $coupon = new Coupon($args);
     try {
         /**
          * Update [both]
          */
         if ($coupon->isPersisted() && $exists = $repository->get($coupon->srl)) { //
             /** @var $exists Coupon */
             $coupon->type = $exists->type;
             $out = $coupon->save();
             $repository->check($out);
             if ($coupon->isGroup()) {
                 $this->setMessage("Updated group <b>{$coupon->name}</b>");
             }
             elseif ($coupon->isSingle()) {
                 $this->setMessage("Updated coupon <b>{$coupon->code}</b>");
             }
         }
         /**
          * Group Insert
          */
         elseif (is_numeric($n = Context::get('codes_number')) && $n > 1) {
             $coupon->type = Coupon::TYPE_PARENT;
             $out = $coupon->save();
             $repository->check($out);
             $length = Context::get('code_length');
             $type = Context::get('code_type');
             $pattern = Context::get('code_pattern');
             if (!strstr($pattern, 'CODE')) throw new ShopException('Pattern must contain "CODE"');
             $pattern = str_replace('CODE', 'X', $pattern);
             $separateEvery = Context::get('separate_at');
             $coupons = $coupon->generateBulk($n, $length, $type, $pattern, $separateEvery);
             $this->setMessage('Generated ' . count($coupons) . ' (grouped) coupons');
         }
         /**
          * Single insert
          */
         else {
             $coupon->type = Coupon::TYPE_SINGLE;
             $out = $coupon->save();
             $repository->check($out);
             $this->setMessage("Generated single coupon with code <b>{$coupon->code}</b>");
         }
     }
     catch (Exception $e) {
         $msg = $e->getMessage();
         if (strstr($msg, 'unique_module_code')) {
             $msg = "Code '<b>{$coupon->code}</b>' is already in use";
         }
         return new Object(-1, $msg);
     }
     $this->setRedirectUrl(getNotEncodedUrl('', 'act', 'dispShopToolDiscountCodes', 'highlight', $coupon->srl));
 }
Exemplo n.º 11
0
 /**
  * 新增优惠券信息
  * POST /api/activity/{activityId}/coupon
  */
 public function actionRestcreate()
 {
     $this->checkRestAuth();
     // check activity
     $activity = Activity::model()->findByPk($_GET['activityId']);
     if ($activity == null || $activity->archived == 0) {
         return $this->sendResponse(404, 'activity is not found');
     }
     if (!isset($_POST['batch']) || !isset($_POST['code'])) {
         return $this->sendResponse(400, "missed parameters");
     }
     // 判断code是否唯一
     if (!$this->checkCodeIsUnqinue($activity->id, $_POST['code'])) {
         return $this->sendResponse(400, 'code is not unique');
     }
     $coupon = new Coupon();
     $coupon->activity_id = $activity->id;
     $coupon->batch = $_POST['batch'];
     $coupon->code = $_POST['code'];
     if (isset($_POST['createdBy'])) {
         $coupon->created_by = $_POST['createdBy'];
     }
     if ($coupon->save()) {
         $this->sendResponse(201, $coupon->id);
     } else {
         $this->sendResponse(500, 'failed to create.');
     }
 }
 /**
  * Creates a new model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  */
 public function actionCreate()
 {
     Yii::app()->clientScript->registerPackage('discount-edit');
     $id = null;
     if (isset($_GET['id'])) {
         $id = (int) $_GET['id'];
     } elseif (isset($_POST['Discount']['id'])) {
         $id = (int) $_POST['Discount']['id'];
     }
     $model = $this->loadModel($id);
     // Uncomment the following line if AJAX validation is needed
     // $this->performAjaxValidation($model);
     $coupons = $model->coupons;
     if (isset($_POST['Discount'])) {
         if (!isset($_POST['Coupon'])) {
             $model->addError('status', Yii::t('errors', 'В предложении должен быть хотябы один купон'));
         } else {
             $data = $_POST['Coupon'];
             $coupons = array();
             foreach ($data as $c) {
                 $coupon = null;
                 if (isset($c['id']) && $c['id'] > 0) {
                     $coupon = Coupon::model()->findByPk($c['id']);
                     if (isset($c['delete'])) {
                         $coupon->delete();
                     }
                 }
                 if (!isset($c['delete'])) {
                     if ($coupon === null) {
                         $coupon = new Coupon();
                     }
                     $coupon->attributes = $c;
                     $coupons[] = $coupon;
                 }
             }
         }
         $model->attributes = $_POST['Discount'];
         if (!$model->hasErrors() && $model->save()) {
             $fail = false;
             foreach ($coupons as $coupon) {
                 $coupon->discount_id = $model->id;
                 if (!$coupon->save()) {
                     $fail = true;
                 }
             }
             if (!$fail) {
                 if (isset($_POST['Cats'])) {
                     $cats = array_map('intval', explode(',', $_POST['Cats']));
                     //Normalize
                     $dicats = DiscountCategory::model()->findAllByAttributes(array('discount_id' => $model->id));
                     foreach ($dicats as $i => $d) {
                         if (FALSE !== ($k = array_search($d->category_id, $cats))) {
                             unset($cats[$k]);
                             unset($dicats[$i]);
                         } else {
                             $d->delete();
                         }
                     }
                     if (!empty($cats)) {
                         $cats = Category::model()->findAllByAttributes(array('id' => $cats));
                         foreach ($cats as $c) {
                             $DC = new DiscountCategory();
                             $DC->discount_id = $model->id;
                             $DC->category_id = $c->id;
                             if (!$DC->save()) {
                                 echo CHtml::errorSummary($DC);
                             }
                         }
                     }
                 }
                 if (isset($_POST['Region'])) {
                     $cats = array_map('intval', explode(',', $_POST['Region']));
                     //Normalize
                     $dicats = DiscountRange::model()->findAllByAttributes(array('discount_id' => $model->id));
                     foreach ($dicats as $i => $d) {
                         if (FALSE !== ($k = array_search($d->category_id, $cats))) {
                             unset($cats[$k]);
                             unset($dicats[$i]);
                         } else {
                             $d->delete();
                         }
                     }
                     if (!empty($cats)) {
                         $cats = City::model()->findAllByAttributes(array('id' => $cats));
                         foreach ($cats as $c) {
                             $DC = new DiscountRange();
                             $DC->discount_id = $model->id;
                             $DC->city_id = $c->id;
                             if (!$DC->save()) {
                                 echo CHtml::errorSummary($DC);
                             }
                         }
                     }
                 }
                 $this->redirect('/business/discount');
             }
         }
     }
     $this->render('create', array('model' => $model, 'coupons' => $coupons));
 }
Exemplo n.º 13
0
 /**
  * 生成优惠券
  */
 public function actionMakeCoupon()
 {
     if (empty($_POST)) {
         $info = CouponType::model()->findByPk($_REQUEST['id']);
         $viewData = array();
         $viewData['info'] = $info;
         $this->render('makecoupon', $viewData);
         exit;
     }
     $res = array('statusCode' => 200, 'message' => '生成成功!');
     try {
         $typeInfo = CouponType::model()->findByPk($_REQUEST['id']);
         if (empty($typeInfo)) {
             throw new exception('该优惠券类型不存在!');
         }
         $model = Coupon::model();
         $couponSns = array();
         $okNumber = 0;
         for ($i = 0; $i < $_REQUEST['send_num']; $i++) {
             $sn = $model->getCouponSn();
             if (in_array($sn, $couponSns)) {
                 continue;
             }
             $m = new Coupon();
             $m->coupon_sn = $sn;
             $m->coupon_type_id = $_REQUEST['id'];
             $m->coupon_amount = $typeInfo['coupon_amount'];
             $m->satisfied_amount = $typeInfo['satisfied_amount'];
             $m->start_time = $typeInfo['start_time'];
             $m->end_time = $typeInfo['end_time'];
             $m->add_time = time();
             $flag = $m->save();
             $okNumber += empty($flag) ? 0 : 1;
             array_push($couponSns, $sn);
         }
     } catch (Exception $e) {
         $res['statusCode'] = 300;
         $res['message'] = '失败【' . $e->getMessage() . '】';
     }
     $res['navTabId'] = 'couponTypeList';
     $res['callbackType'] = 'closeCurrent';
     $res['forwardUrl'] = '/manage/coupon/typeIndex';
     $this->ajaxDwzReturn($res);
 }