Exemplo n.º 1
0
 public function postSaveorder()
 {
     setcookie('orderdetails', json_encode($_POST), time() + 60 * 60 * 24 * 30, '/');
     $final_total = "";
     $code = $_POST['coupon'];
     $response = array('success' => 'true');
     if ($code != "") {
         $coupon_det = Coupon::where('code', '=', $code)->first()->toArray();
         if (strtotime($coupon_det['expiry']) > time() && $coupon_det['status'] == 'activated') {
             if ($coupon_det['type'] == 'discount') {
                 $final_total = intval($_POST['subtotal']) - intval($_POST['subtotal']) * intval($coupon_det['discount']) / 100;
             } else {
                 $final_total = $_POST['subtotal'] - $coupon_det['discount'];
             }
             if ($coupon_det['batchuniqid'] != "") {
                 Coupon::where('code', '=', $code)->update(array('status' => 'deactivated'));
             }
             $response = array('success' => 'true', 'message' => 'Coupon code added.', 'final_total' => $final_total);
         } else {
             $response = array('success' => 'false', 'message' => 'Coupon code expired.');
         }
     }
     return json_encode($response);
 }
Exemplo n.º 2
0
 /**
  * Get the new price after applying a group of coupon.
  * @author Anyun
  * @param  int $sale_id
  * @param  array  $promo_codes
  * @return object [newPrice,promoCode]
  */
 public static function applyCoupon($sale_id, $promo_codes = [])
 {
     $app = \Slim\Slim::getInstance();
     $sale = Price::find($sale_id);
     if (!$sale) {
         $app->halt(400, 'Invalid Sale Item');
     }
     $result = array("price" => $sale->price, "coupon" => "");
     if (!$sale) {
         // this sale not exist
         $app->halt('400', $sale_id);
     }
     //Important Rule:
     //Only ONE code will be applied to one item
     //If multiply codes effect, chose the lowest one
     $prices = Coupon::where('course_sale_id', '=', $sale_id)->whereIn('code', $promo_codes)->valid()->get();
     foreach ($prices as $key => $value) {
         if ($value->new_price < $result['price']) {
             $result['old_price'] = $sale->price;
             $result['price'] = $value->new_price;
             $result['coupon'] = $value->code;
         }
     }
     return (object) $result;
 }
Exemplo n.º 3
0
 public function delete_coupon()
 {
     $id = Request::segment(4);
     $coupon = Coupon::find($id);
     Coupon::where('id', $id)->delete();
     $message = "Successfully deleted the coupon";
     $type = "success";
     return Redirect::to('/admin/coupons')->with('type', $type)->with('message', $message);
 }
Exemplo n.º 4
0
 public function update_credit()
 {
     $user_id = $this->user_id;
     $coupon_code = Input::get('coupon_code');
     $coupon = Coupon::where('coupon_code', $coupon_code)->first();
     if ($coupon) {
         $coupon_user = UserCoupon::where('coupon_id', $coupon->id)->where('user_id', $this->user_id)->first();
         if (!$coupon_user) {
             $amount = $coupon->amount;
             $user_credit = UserCredit::where('user_id', $user_id)->first();
             $user_credit->earned += $amount;
             $user_credit->save();
             $coupon_user = new UserCoupon();
             $coupon_user->coupon_id = $coupon->id;
             $coupon_user->user_id = $this->user_id;
             $coupon_user->save();
             if (Request::format() == 'html') {
                 // Redirect to credits page
                 return Redirect::to('/user');
             } else {
                 $response_array['success'] = 'true';
                 $response_array['credits'] = $user_credit->toArray();
                 $response_code = 200;
                 $response = Response::json($response_array, $response_code);
                 return $response;
             }
         } else {
             $message = "Coupon code already used";
             $error_code = 408;
         }
     } else {
         $message = "Invalid coupon code";
         $error_code = 409;
     }
     if (Request::format() == 'html') {
         return Redirect::to('/user')->with('message', $message)->with('type', 'danger');
     } else {
         $response_array = array('success' => 'false', 'error_code' => '405', 'error' => $message);
         $response_code = 200;
         $response = Response::json($response_array, $response_code);
         return $response;
     }
 }
Exemplo n.º 5
0
 public function getDownload()
 {
     $batchid = $_GET['batchid'];
     $all_coupons = Coupon::where('batchuniqid', 'like', '%' . $batchid . '%')->get()->toArray();
     //		echo "<pre>";print_r($all_coupons);
     Excel::create('Coupons-' . date("Y-m-d", time()), function ($excel) use($all_coupons) {
         $excel->sheet('Sheetname', function ($sheet) use($all_coupons) {
             $sheet->fromArray($all_coupons);
         });
     })->export('xls')->download('xls');
     return Redirect::to('coupons.viewall')->with('coupons', Coupon::orderBy('created_at', 'DESC')->groupBy('batchuniqid')->paginate(10, $fields));
 }