public function getCreate()
 {
     $products = Product::where('active', true)->orderBy('name')->lists('name', 'id');
     $bundles = Bundle::where('active', true)->orderBy('name')->lists('name', 'id');
     $coupons = Coupon::orderBy('code')->lists('code', 'id');
     $pricelists = array();
     $pricelists_get = Pricelist::join('modules', 'modules.id', '=', 'pricelists.module_id')->join('memberships', 'memberships.id', '=', 'pricelists.membership_id')->where('modules.active', true)->orderBy('modules.name')->orderBy('memberships.rank', 'desc')->select('pricelists.*')->get();
     foreach ($pricelists_get as $pricelist) {
         $pricelists[$pricelist->id] = $pricelist->name;
     }
     $payment_statuses = config('redminportal::payment_statuses');
     return view('redminportal::orders/create')->with('products', $products)->with('bundles', $bundles)->with('coupons', $coupons)->with('pricelists', $pricelists)->with('payment_statuses', $payment_statuses);
 }
 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');
 }
 public function postStore()
 {
     $sid = \Input::get('id');
     if (isset($sid)) {
         $url = 'admin/bundles/edit/' . $sid;
     } else {
         $url = 'admin/bundles/create';
     }
     $rules = array('image' => 'mimes:jpg,jpeg,png,gif|max:500', 'name' => 'required|unique:products,name' . (isset($sid) ? ',' . $sid : ''), 'short_description' => 'required', 'price' => 'numeric', 'sku' => 'required|alpha_dash|unique:bundles,sku' . (isset($sid) ? ',' . $sid : ''), 'category_id' => 'required', 'tags' => 'regex:/^[a-z,0-9 -]+$/i');
     $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)) {
         $bundle = Bundle::find($sid);
         if ($bundle == null) {
             $errors = new \Illuminate\Support\MessageBag();
             $errors->add('bundleError', "The bundle does not exist or may have been deleted.");
             return redirect('admin/bundles')->withErrors($errors);
         }
     }
     $name = Input::get('name');
     $sku = Input::get('sku');
     $price = Input::get('price');
     $short_description = Input::get('short_description');
     $long_description = Input::get('long_description');
     $image = Input::file('image');
     $featured = Input::get('featured') == '' ? false : true;
     $active = Input::get('active') == '' ? false : true;
     $category_id = Input::get('category_id');
     $tags = Input::get('tags');
     $apply_to_models = array();
     $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;
             }
         }
     }
     // 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 Product or Membership/Module under Bundled Items.");
         return redirect($url)->withErrors($errors)->withInput();
     }
     $newBundle = isset($sid) ? $bundle : new Bundle();
     $newBundle->name = $name;
     $newBundle->sku = $sku;
     $newBundle->price = isset($price) ? $price : 0;
     $newBundle->short_description = $short_description;
     $newBundle->long_description = $long_description;
     $newBundle->featured = $featured;
     $newBundle->active = $active;
     $newBundle->category_id = $category_id > 0 ? $category_id : null;
     $newBundle->save();
     // Remove all existing relationships first
     if (isset($sid)) {
         $newBundle->pricelists()->detach();
         $newBundle->products()->detach();
     }
     foreach ($apply_to_models as $apply_to_model) {
         $apply_to_model->bundles()->save($newBundle);
     }
     // Save translations
     $translations = \Config::get('redminportal::translation');
     foreach ($translations as $translation) {
         $lang = $translation['lang'];
         if ($lang == 'en') {
             continue;
         }
         $translated_content = array('name' => \Input::get($lang . '_name'), 'short_description' => \Input::get($lang . '_short_description'), 'long_description' => \Input::get($lang . '_long_description'));
         // Check if lang exist
         $translated_model = $newBundle->translations->where('lang', $lang)->first();
         if ($translated_model == null) {
             $translated_model = new Translation();
         }
         $translated_model->lang = $lang;
         $translated_model->content = json_encode($translated_content);
         $newBundle->translations()->save($translated_model);
     }
     if (!empty($tags)) {
         // Delete old tags
         $newBundle->tags()->detach();
         // Save tags
         foreach (explode(',', $tags) as $tagName) {
             Tag::addTag($newBundle, $tagName);
         }
     }
     if (Input::hasFile('image')) {
         //Upload the file
         $helper_image = new RImage();
         $filename = $helper_image->upload($image, 'bundles/' . $newBundle->id, true);
         if ($filename) {
             // create photo
             $newimage = new Image();
             $newimage->path = $filename;
             // save photo to the loaded model
             $newBundle->images()->save($newimage);
         }
     }
     return redirect('admin/bundles');
 }