public function testCreateSubCategory()
 {
     $this->createNew();
     $model = Category::find(1);
     $new_model = new Category();
     $new_model->name = 'This is sub category';
     $new_model->short_description = 'This is the body 1';
     $new_model->active = true;
     $model->categories()->save($new_model);
     $new_model_2 = new Category();
     $new_model_2->name = 'This is another sub category';
     $new_model_2->short_description = 'This is the body 2';
     $new_model_2->active = true;
     $model->categories()->save($new_model_2);
     $this->assertTrue($model->categories->count() == 2);
     $this->assertTrue(Category::where('category_id', $model->id)->count() == 2);
     foreach ($model->categories as $cat) {
         if ($cat->name == 'This is sub category') {
             $this->assertTrue($cat->short_description == 'This is the body 1');
         } elseif ($cat->name == 'This is another sub category') {
             $this->assertTrue($cat->short_description == 'This is the body 2');
         } else {
             $this->assertTrue(false);
         }
     }
     // Delete main category will delete all sub categories
     $model->delete();
     $this->assertTrue(Category::where('category_id', $model->id)->count() == 0);
 }
 public function getDelete($sid)
 {
     // Find the category using the user id
     $category = Category::find($sid);
     if ($category == null) {
         $errors = new \Illuminate\Support\MessageBag();
         $errors->add('deleteError', "The category cannot be found because it does not exist or may have been deleted.");
         return redirect('/admin/categories')->withErrors($errors);
     }
     // Find if there's any child
     $children = Category::where('category_id', $sid)->count();
     if ($children > 0) {
         $errors = new \Illuminate\Support\MessageBag();
         $errors->add('deleteError', "The category '" . $category->name . "' cannot be deleted because it has " . $children . " children categories.");
         return redirect('/admin/categories')->withErrors($errors);
     }
     // Check in use by media
     $medias = Media::where('category_id', $sid)->get();
     if (count($medias) > 0) {
         $errors = new \Illuminate\Support\MessageBag();
         $errors->add('deleteError', "The category '" . $category->name . "' cannot be deleted because it is in used.");
         return redirect('/admin/categories')->withErrors($errors);
     }
     // Delete the category
     $category->delete();
     return redirect('admin/categories');
 }
 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 getDelete($sid)
 {
     // Find the category using the user id
     $category = Category::find($sid);
     if ($category == null) {
         $errors = new \Illuminate\Support\MessageBag();
         $errors->add('deleteError', Lang::get('redminportal::messages.category_error_category_not_found'));
         return redirect()->back()->withErrors($errors);
     }
     // Find if there's any child
     $children = Category::where('category_id', $sid)->count();
     if ($children > 0) {
         $errors = new \Illuminate\Support\MessageBag();
         $errors->add('deleteError', Lang::get('redminportal::messages.category_error_cannot_delete_has_child', ['name' => $category->name, 'children' => $children]));
         return redirect()->back()->withErrors($errors);
     }
     // Delete the category
     $category->delete();
     return redirect()->back();
 }