public function getSingleOffer($id)
 {
     $offer = Offers::with('store')->where('id', $id)->first();
     $output = ['offer' => $offer];
     return view('admin.offer', $output);
 }
 public function makeVote(request $request)
 {
     $rules = array('offer_id' => 'required');
     $Validator = $this->customValidator($request->all(), $rules, array());
     if ($Validator->fails()) {
         return response()->json(['response_code' => 'ERR_RULES', 'messages' => $Validator->errors()->all()], 400);
     }
     $input = $request->only('offer_id');
     $offer_id = $input['offer_id'];
     $user_id = Auth::user()->id;
     if ($this->ifOfferHasVote($offer_id, $user_id)) {
         $offer = Offers::find($offer_id);
         /*$offer->Votes()->updateExistingPivot($user_id,['status'=>$input['status']]);*/
         $offer->Votes()->detach([$user_id]);
         return response()->json(['response_code' => 'ERR_KDD', 'messages' => 'Kaching! Deal Downvoted']);
     }
     $offer = Offers::find($offer_id);
     $offer->Votes()->attach([$user_id]);
     return response()->json(['response_code' => 'RES_KDU', 'messages' => 'Kaching! Deal Upvoted']);
 }
 public function getStoreOffers(request $request)
 {
     $rules = array('store_id' => 'required');
     $Validator = $this->customValidator($request->all(), $rules, array());
     if ($Validator->fails()) {
         return response()->json(['response_code' => 'ERR_RULES', 'messages' => $Validator->errors()->all()], 400);
     }
     $store_id = $request->only('store_id');
     if (!$this->checkUserHasStore($store_id['store_id'], false)) {
         return response()->json(['response_code' => 'ERR_UNA', 'messages' => 'User Not Authorized'], 403);
     }
     return response()->json(['response_code' => 'RES_OFF', 'messages' => 'Offers', 'data' => Offers::with('votesCount', 'favouriteCount')->where('store_id', $store_id)->get()]);
 }
 public function getAllStoresOffers()
 {
     if (!Auth::User()->Stores->is_parent) {
         return response()->json(['response_code' => 'ERR_IR', 'messages' => 'invalid request'], 400);
     }
     $matchThese = ['status' => true, 'is_child' => true, 'parent_id' => Auth::User()->Stores->id];
     $stores = MerchantStore::where($matchThese)->get();
     $storesArr = [];
     foreach ($stores as $key => $store) {
         $storesArr[$key] = $store->id;
     }
     $offers = Offers::with('Store', 'votesCount')->whereIn('store_id', $storesArr)->orderby('created_at', 'desc')->paginate(15);
     return response()->json(['response_code' => 'RES_OFF', 'messages' => 'Offers', 'data' => $offers]);
 }
 public function editOffer(request $request)
 {
     $validator = Validator::make($request->all(), ['offer_id' => 'required', 'title' => 'required', 'startDate' => 'required', 'endDate' => 'required', 'fineprint' => 'required']);
     if ($validator->fails()) {
         return response()->json(['status' => 'fail', 'message' => $validator->errors()->all()]);
     }
     $matchThese = ['id' => $request->input('offer_id'), 'is_child' => false];
     // child offers are not editable by child merchants
     $offer = Offers::where($matchThese)->first();
     if (empty($offer)) {
         return response()->json(['status' => 'fail', 'message' => 'Not Authorized']);
     }
     if (!$this->checkUserHasStorePermission($offer->store_id)) {
         // checking if offer can be editable by logged in store
         return response()->json(['status' => 'fail', 'message' => 'Not Authorized']);
     }
     $fineprintArr = explode("\n", $request->input('fineprint'));
     $fineprint = '';
     foreach ($fineprintArr as $value) {
         if ($value != '' || !empty($value)) {
             $fineprint .= '<li>' . $value . '</li>';
         }
     }
     $updateValues = $request->only('title', 'startDate', 'endDate');
     $updateValues['fineprint'] = $fineprint;
     foreach ($updateValues as $key => $value) {
         $offer->{$key} = $value;
     }
     $offer->save();
     // if offer is parent and store editing this also parent update all child offers
     if ($offer->is_parent && $this->checkIfStoreIsParent($offer->store_id)) {
         $matchThese = ['is_child' => true, 'parent_id' => $offer->id];
         Offers::where($matchThese)->update($updateValues);
     }
     return response()->json(['status' => 'success', 'data' => $offer]);
 }