You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
			
				
					230 lines
				
				9.1 KiB
			
		
		
			
		
	
	
					230 lines
				
				9.1 KiB
			| 
								 
											2 years ago
										 
									 | 
							
								<?php
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								namespace App\Http\Controllers\Api\V1;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								use App\Models\Coupon;
							 | 
						||
| 
								 | 
							
								use App\Models\Review;
							 | 
						||
| 
								 | 
							
								use App\Models\Restaurant;
							 | 
						||
| 
								 | 
							
								use Illuminate\Http\Request;
							 | 
						||
| 
								 | 
							
								use App\CentralLogics\Helpers;
							 | 
						||
| 
								 | 
							
								use Illuminate\Support\Facades\DB;
							 | 
						||
| 
								 | 
							
								use App\Http\Controllers\Controller;
							 | 
						||
| 
								 | 
							
								use Illuminate\Support\Facades\Auth;
							 | 
						||
| 
								 | 
							
								use App\CentralLogics\RestaurantLogic;
							 | 
						||
| 
								 | 
							
								use Illuminate\Support\Facades\Validator;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								class RestaurantController extends Controller
							 | 
						||
| 
								 | 
							
								{
							 | 
						||
| 
								 | 
							
								    public function get_restaurants(Request $request, $filter_data="all")
							 | 
						||
| 
								 | 
							
								    {
							 | 
						||
| 
								 | 
							
								        if (!$request->hasHeader('zoneId')) {
							 | 
						||
| 
								 | 
							
								            $errors = [];
							 | 
						||
| 
								 | 
							
								            array_push($errors, ['code' => 'zoneId', 'message' => translate('messages.zone_id_required')]);
							 | 
						||
| 
								 | 
							
								            return response()->json([
							 | 
						||
| 
								 | 
							
								                'errors' => $errors
							 | 
						||
| 
								 | 
							
								            ], 403);
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        $longitude= $request->header('longitude');
							 | 
						||
| 
								 | 
							
								        $latitude= $request->header('latitude');
							 | 
						||
| 
								 | 
							
								        $type = $request->query('type', 'all');
							 | 
						||
| 
								 | 
							
								        $cuisine= $request->query('cuisine', 'all');
							 | 
						||
| 
								 | 
							
								        $name= $request->query('name');
							 | 
						||
| 
								 | 
							
								        $zone_id= json_decode($request->header('zoneId'), true);
							 | 
						||
| 
								 | 
							
								        $restaurants = RestaurantLogic::get_restaurants($zone_id, $filter_data, $request['limit'], $request['offset'],$type, $name,$longitude,$latitude,$cuisine);
							 | 
						||
| 
								 | 
							
								        $restaurants['restaurants'] = Helpers::restaurant_data_formatting($restaurants['restaurants'], true);
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        return response()->json($restaurants, 200);
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    public function get_latest_restaurants(Request $request, $filter_data="all")
							 | 
						||
| 
								 | 
							
								    {
							 | 
						||
| 
								 | 
							
								        if (!$request->hasHeader('zoneId')) {
							 | 
						||
| 
								 | 
							
								            $errors = [];
							 | 
						||
| 
								 | 
							
								            array_push($errors, ['code' => 'zoneId', 'message' => translate('messages.zone_id_required')]);
							 | 
						||
| 
								 | 
							
								            return response()->json([
							 | 
						||
| 
								 | 
							
								                'errors' => $errors
							 | 
						||
| 
								 | 
							
								            ], 403);
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        $type = $request->query('type', 'all');
							 | 
						||
| 
								 | 
							
								        $longitude= $request->header('longitude');
							 | 
						||
| 
								 | 
							
								        $latitude= $request->header('latitude');
							 | 
						||
| 
								 | 
							
								        $zone_id= json_decode($request->header('zoneId'), true);
							 | 
						||
| 
								 | 
							
								        $restaurants = RestaurantLogic::get_latest_restaurants($zone_id, $request['limit'], $request['offset'], $type ,$longitude,$latitude);
							 | 
						||
| 
								 | 
							
								        $restaurants['restaurants'] = Helpers::restaurant_data_formatting($restaurants['restaurants'], true);
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        return response()->json($restaurants['restaurants'], 200);
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    public function get_popular_restaurants(Request $request)
							 | 
						||
| 
								 | 
							
								    {
							 | 
						||
| 
								 | 
							
								        if (!$request->hasHeader('zoneId')) {
							 | 
						||
| 
								 | 
							
								            $errors = [];
							 | 
						||
| 
								 | 
							
								            array_push($errors, ['code' => 'zoneId', 'message' => translate('messages.zone_id_required')]);
							 | 
						||
| 
								 | 
							
								            return response()->json([
							 | 
						||
| 
								 | 
							
								                'errors' => $errors
							 | 
						||
| 
								 | 
							
								            ], 403);
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								        $longitude= $request->header('longitude');
							 | 
						||
| 
								 | 
							
								        $latitude= $request->header('latitude');
							 | 
						||
| 
								 | 
							
								        $type = $request->query('type', 'all');
							 | 
						||
| 
								 | 
							
								        $zone_id= json_decode($request->header('zoneId'), true);
							 | 
						||
| 
								 | 
							
								        $restaurants = RestaurantLogic::get_popular_restaurants($zone_id, $request['limit'], $request['offset'], $type,$longitude,$latitude);
							 | 
						||
| 
								 | 
							
								        $restaurants['restaurants'] = Helpers::restaurant_data_formatting($restaurants['restaurants'], true);
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        return response()->json($restaurants['restaurants'], 200);
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								    public function recently_viewed_restaurants(Request $request)
							 | 
						||
| 
								 | 
							
								    {
							 | 
						||
| 
								 | 
							
								        if (!$request->hasHeader('zoneId')) {
							 | 
						||
| 
								 | 
							
								            $errors = [];
							 | 
						||
| 
								 | 
							
								            array_push($errors, ['code' => 'zoneId', 'message' => translate('messages.zone_id_required')]);
							 | 
						||
| 
								 | 
							
								            return response()->json([
							 | 
						||
| 
								 | 
							
								                'errors' => $errors
							 | 
						||
| 
								 | 
							
								            ], 403);
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								        $longitude= $request->header('longitude');
							 | 
						||
| 
								 | 
							
								        $latitude= $request->header('latitude');
							 | 
						||
| 
								 | 
							
								        $type = $request->query('type', 'all');
							 | 
						||
| 
								 | 
							
								        $zone_id= json_decode($request->header('zoneId'), true);
							 | 
						||
| 
								 | 
							
								        $restaurants = RestaurantLogic::recently_viewed_restaurants_data($zone_id, $request['limit'], $request['offset'], $type,$longitude,$latitude);
							 | 
						||
| 
								 | 
							
								        $restaurants['restaurants'] = Helpers::restaurant_data_formatting($restaurants['restaurants'], true);
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        return response()->json($restaurants['restaurants'], 200);
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    public function get_details($id)
							 | 
						||
| 
								 | 
							
								    {
							 | 
						||
| 
								 | 
							
								        $restaurant = RestaurantLogic::get_restaurant_details($id);
							 | 
						||
| 
								 | 
							
								        if($restaurant)
							 | 
						||
| 
								 | 
							
								        {
							 | 
						||
| 
								 | 
							
								            $category_ids = DB::table('food')
							 | 
						||
| 
								 | 
							
								            ->join('categories', 'food.category_id', '=', 'categories.id')
							 | 
						||
| 
								 | 
							
								            ->selectRaw('IF((categories.position = "0"), categories.id, categories.parent_id) as categories')
							 | 
						||
| 
								 | 
							
								            ->where('food.restaurant_id', $id)
							 | 
						||
| 
								 | 
							
								            ->where('categories.status',1)
							 | 
						||
| 
								 | 
							
								            ->groupBy('categories')
							 | 
						||
| 
								 | 
							
								            ->get();
							 | 
						||
| 
								 | 
							
								            $restaurant = Helpers::restaurant_data_formatting($restaurant);
							 | 
						||
| 
								 | 
							
								            $restaurant['category_ids'] = array_map('intval', $category_ids->pluck('categories')->toArray());
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								            if(auth('api')->user() !== null){
							 | 
						||
| 
								 | 
							
								                $customer_id =auth('api')->user()->id;
							 | 
						||
| 
								 | 
							
								                Helpers::visitor_log('restaurant',$customer_id,$restaurant->id,false);
							 | 
						||
| 
								 | 
							
								            }
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        return response()->json($restaurant, 200);
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    public function get_searched_restaurants(Request $request)
							 | 
						||
| 
								 | 
							
								    {
							 | 
						||
| 
								 | 
							
								        if (!$request->hasHeader('zoneId')) {
							 | 
						||
| 
								 | 
							
								            $errors = [];
							 | 
						||
| 
								 | 
							
								            array_push($errors, ['code' => 'zoneId', 'message' => translate('messages.zone_id_required')]);
							 | 
						||
| 
								 | 
							
								            return response()->json([
							 | 
						||
| 
								 | 
							
								                'errors' => $errors
							 | 
						||
| 
								 | 
							
								            ], 403);
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								        $validator = Validator::make($request->all(), [
							 | 
						||
| 
								 | 
							
								            'name' => 'required',
							 | 
						||
| 
								 | 
							
								        ]);
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        if ($validator->fails()) {
							 | 
						||
| 
								 | 
							
								            return response()->json(['errors' => Helpers::error_processor($validator)], 403);
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        $type = $request->query('type', 'all');
							 | 
						||
| 
								 | 
							
								        $longitude= $request->header('longitude');
							 | 
						||
| 
								 | 
							
								        $latitude= $request->header('latitude');
							 | 
						||
| 
								 | 
							
								        $zone_id= json_decode($request->header('zoneId'), true);
							 | 
						||
| 
								 | 
							
								        $restaurants = RestaurantLogic::search_restaurants($request['name'], $zone_id, $request->category_id,$request['limit'], $request['offset'], $type,$longitude,$latitude);
							 | 
						||
| 
								 | 
							
								        $restaurants['restaurants'] = Helpers::restaurant_data_formatting($restaurants['restaurants'], true);
							 | 
						||
| 
								 | 
							
								        return response()->json($restaurants, 200);
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    public function reviews(Request $request)
							 | 
						||
| 
								 | 
							
								    {
							 | 
						||
| 
								 | 
							
								        $validator = Validator::make($request->all(), [
							 | 
						||
| 
								 | 
							
								            'restaurant_id' => 'required',
							 | 
						||
| 
								 | 
							
								        ]);
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        if ($validator->fails()) {
							 | 
						||
| 
								 | 
							
								            return response()->json(['errors' => Helpers::error_processor($validator)], 403);
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								        $id = $request['restaurant_id'];
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        $reviews = Review::with(['customer', 'food'])
							 | 
						||
| 
								 | 
							
								        ->whereHas('food', function($query)use($id){
							 | 
						||
| 
								 | 
							
								            return $query->where('restaurant_id', $id);
							 | 
						||
| 
								 | 
							
								        })
							 | 
						||
| 
								 | 
							
								        ->active()->latest()->get();
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        $storage = [];
							 | 
						||
| 
								 | 
							
								        foreach ($reviews as $item) {
							 | 
						||
| 
								 | 
							
								            $item['attachment'] = json_decode($item['attachment']);
							 | 
						||
| 
								 | 
							
								            $item['food_name'] = null;
							 | 
						||
| 
								 | 
							
								            $item['food_image'] = null;
							 | 
						||
| 
								 | 
							
								            $item['customer_name'] = null;
							 | 
						||
| 
								 | 
							
								            if($item->food)
							 | 
						||
| 
								 | 
							
								            {
							 | 
						||
| 
								 | 
							
								                $item['food_name'] = $item->food->name;
							 | 
						||
| 
								 | 
							
								                $item['food_image'] = $item->food->image;
							 | 
						||
| 
								 | 
							
								                if(count($item->food->translations)>0)
							 | 
						||
| 
								 | 
							
								                {
							 | 
						||
| 
								 | 
							
								                    $translate = array_column($item->food->translations->toArray(), 'value', 'key');
							 | 
						||
| 
								 | 
							
								                    $item['food_name'] = $translate['name'];
							 | 
						||
| 
								 | 
							
								                }
							 | 
						||
| 
								 | 
							
								            }
							 | 
						||
| 
								 | 
							
								            if($item->customer)
							 | 
						||
| 
								 | 
							
								            {
							 | 
						||
| 
								 | 
							
								                $item['customer_name'] = $item->customer->f_name.' '.$item->customer->l_name;
							 | 
						||
| 
								 | 
							
								            }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								            unset($item['food']);
							 | 
						||
| 
								 | 
							
								            unset($item['customer']);
							 | 
						||
| 
								 | 
							
								            array_push($storage, $item);
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        return response()->json($storage, 200);
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    // public function get_product_rating($id)
							 | 
						||
| 
								 | 
							
								    // {
							 | 
						||
| 
								 | 
							
								    //     try {
							 | 
						||
| 
								 | 
							
								    //         $product = Food::find($id);
							 | 
						||
| 
								 | 
							
								    //         $overallRating = ProductLogic::get_overall_rating($product->reviews);
							 | 
						||
| 
								 | 
							
								    //         return response()->json(floatval($overallRating[0]), 200);
							 | 
						||
| 
								 | 
							
								    //     } catch (\Exception $e) {
							 | 
						||
| 
								 | 
							
								    //         return response()->json(['errors' => $e], 403);
							 | 
						||
| 
								 | 
							
								    //     }
							 | 
						||
| 
								 | 
							
								    // }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    public function get_coupons(Request $request){
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        $validator = Validator::make($request->all(), [
							 | 
						||
| 
								 | 
							
								            'restaurant_id' => 'required',
							 | 
						||
| 
								 | 
							
								        ]);
							 | 
						||
| 
								 | 
							
								        if ($validator->fails()) {
							 | 
						||
| 
								 | 
							
								            return response()->json(['errors' => Helpers::error_processor($validator)], 403);
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								        $restaurant_id=$request->restaurant_id;
							 | 
						||
| 
								 | 
							
								        $customer_id=$request->customer_id;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        $coupons = Coupon::Where(function ($q) use ($restaurant_id,$customer_id) {
							 | 
						||
| 
								 | 
							
								            $q->Where('coupon_type', 'restaurant_wise')->whereJsonContains('data', [$restaurant_id])
							 | 
						||
| 
								 | 
							
								                ->where(function ($q1) use ($customer_id) {
							 | 
						||
| 
								 | 
							
								                    $q1->whereJsonContains('customer_id', [$customer_id])->orWhereJsonContains('customer_id', ['all']);
							 | 
						||
| 
								 | 
							
								                });
							 | 
						||
| 
								 | 
							
								        })->orWhereHas('restaurant',function($q) use ($restaurant_id){
							 | 
						||
| 
								 | 
							
								            $q->where('id',$restaurant_id);
							 | 
						||
| 
								 | 
							
								        })
							 | 
						||
| 
								 | 
							
								        ->active()->whereDate('expire_date', '>=', date('Y-m-d'))->whereDate('start_date', '<=', date('Y-m-d'))
							 | 
						||
| 
								 | 
							
								        ->get();
							 | 
						||
| 
								 | 
							
								        return response()->json($coupons, 200);
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								}
							 |