示例#1
0
 public function postStore()
 {
     $sid = \Input::get('id');
     if (isset($sid)) {
         $url = 'admin/coupons/edit/' . $sid;
     } else {
         $url = 'admin/coupons/create';
     }
     $rules = array('code' => 'required', 'amount' => 'required', 'start_date' => 'required', 'end_date' => 'required', 'amount' => 'numeric', 'min_spent' => 'numeric', 'max_spent' => 'numeric');
     $validation = \Validator::make(\Input::all(), $rules);
     if ($validation->fails()) {
         return redirect($url)->withErrors($validation)->withInput();
     }
     // If id is set, check that it exists
     if (isset($sid)) {
         $coupon = Coupon::find($sid);
         if ($coupon == null) {
             $errors = new \Illuminate\Support\MessageBag();
             $errors->add('couponError', "The coupon does not exist or may have been deleted.");
             return redirect('admin/coupons')->withErrors($errors);
         }
     }
     $code = \Input::get('code');
     $description = \Input::get('description');
     $amount = \Input::get('amount');
     $is_percent = \Input::get('is_percent') == '' ? false : true;
     $start_date = \DateTime::createFromFormat('d/m/Y h:i A', \Input::get('start_date'));
     $end_date = \DateTime::createFromFormat('d/m/Y h:i A', \Input::get('end_date'));
     $max_spent = \Input::get('max_spent');
     $min_spent = \Input::get('min_spent');
     $limit_per_coupon = \Input::get('usage_limit_per_coupon');
     $limit_per_user = \Input::get('usage_limit_per_user');
     $multiple_coupons = \Input::get('multiple_coupons') == '' ? false : true;
     $exclude_sale_item = \Input::get('exclude_sale_item') == '' ? false : true;
     $automatically_apply = \Input::get('automatically_apply') == '' ? false : true;
     // Check that end date is after start date
     if ($end_date <= $start_date) {
         $errors = new \Illuminate\Support\MessageBag();
         $errors->add('dateRangeError', "Please enter an End date later than Start date.");
         return redirect($url)->withErrors($errors)->withInput();
     }
     // Check if max spent is less than min spent, only if both not null
     if ($max_spent and $min_spent) {
         if ((double) $max_spent < (double) $min_spent) {
             $errors = new \Illuminate\Support\MessageBag();
             $errors->add('spentRangeError', "Max spent cannot be less than Min spent.");
             return redirect($url)->withErrors($errors)->withInput();
         }
     }
     $apply_to_models = array();
     $categories = \Input::get('category_id');
     if (count($categories) > 0) {
         foreach ($categories as $item) {
             $model = Category::find($item);
             if ($model != null) {
                 $apply_to_models[] = $model;
             }
         }
     }
     $products = \Input::get('product_id');
     if (count($products) > 0) {
         foreach ($products as $item) {
             $model = Product::find($item);
             if ($model != null) {
                 $apply_to_models[] = $model;
             }
         }
     }
     $pricelists = \Input::get('pricelist_id');
     if (count($pricelists) > 0) {
         foreach ($pricelists as $item) {
             $model = Pricelist::find($item);
             if ($model != null) {
                 $apply_to_models[] = $model;
             }
         }
     }
     $bundles = \Input::get('bundle_id');
     if (count($bundles) > 0) {
         foreach ($bundles as $item) {
             $model = Bundle::find($item);
             if ($model != null) {
                 $apply_to_models[] = $model;
             }
         }
     }
     // In the worst scenario, all select items have been deleted
     if (count($apply_to_models) == 0) {
         $errors = new \Illuminate\Support\MessageBag();
         $errors->add('applyToError', "You have not selected any Category, Product or Membership/Module under Restricted To.");
         return redirect($url)->withErrors($errors)->withInput();
     }
     $newCoupon = isset($sid) ? $coupon : new Coupon();
     $newCoupon->code = $code;
     $newCoupon->description = $description;
     $newCoupon->amount = $amount;
     $newCoupon->is_percent = $is_percent;
     $newCoupon->start_date = $start_date;
     $newCoupon->end_date = $end_date;
     $newCoupon->max_spent = $max_spent == 0 ? null : $max_spent;
     $newCoupon->min_spent = $min_spent == 0 ? null : $min_spent;
     $newCoupon->usage_limit_per_coupon = $limit_per_coupon == 0 ? null : $limit_per_coupon;
     $newCoupon->usage_limit_per_user = $limit_per_user == 0 ? null : $limit_per_user;
     $newCoupon->multiple_coupons = $multiple_coupons;
     $newCoupon->exclude_sale_item = $exclude_sale_item;
     $newCoupon->automatically_apply = $automatically_apply;
     $newCoupon->save();
     // Remove all existing relationships first
     if (isset($sid)) {
         $coupon->categories()->detach();
         $coupon->pricelists()->detach();
         $coupon->products()->detach();
         $coupon->bundles()->detach();
     }
     foreach ($apply_to_models as $apply_to_model) {
         $apply_to_model->coupons()->save($newCoupon);
     }
     return redirect('admin/coupons');
 }
示例#2
0
 /**
  * Add coupons to order
  *
  * @param array $coupons
  * @param object $new_order Order
  *
  * @return array
  **/
 private function addCouponToOrder($coupons, $new_order)
 {
     $errors = new \Illuminate\Support\MessageBag();
     if (count($coupons) > 0) {
         foreach ($coupons as $item) {
             $model = Coupon::find($item);
             if ($model != null) {
                 try {
                     $new_order->addCoupon($model);
                 } catch (Exception $exp) {
                     $errors->add('couponError', "Coupon " . $model->code . " cannot be added because: " . $exp->getMessage());
                 }
             }
         }
         // Set coupon discount
         $new_order->setDiscounts();
     }
     return $errors;
 }
 public function getSort($sortBy = 'create_at', $orderBy = 'desc')
 {
     $inputs = array('sortBy' => $sortBy, 'orderBy' => $orderBy);
     $rules = array('sortBy' => 'required|regex:/^[a-zA-Z0-9 _-]*$/', 'orderBy' => 'required|regex:/^[a-zA-Z0-9 _-]*$/');
     $validation = \Validator::make($inputs, $rules);
     if ($validation->fails()) {
         return redirect('admin/coupons')->withErrors($validation);
     }
     if ($orderBy != 'asc' && $orderBy != 'desc') {
         $orderBy = 'asc';
     }
     $coupons = Coupon::orderBy($sortBy, $orderBy)->paginate(20);
     return view('redminportal::coupons/view')->with('coupons', $coupons)->with('sortBy', $sortBy)->with('orderBy', $orderBy);
 }