function index() { ///////////////// // Load Filter // ///////////////// $filters = $this->request->only('skip', 'take', 'with_count'); $filters['skip'] = max((int) $filters['skip'], 0); $filters['take'] = min((int) $filters['take'], 100); /////////// // Query // /////////// $query[] = ['$unwind' => '$schedules']; $query[] = ['$unwind' => '$schedules.vouchers']; // Sort switch ($filters['mode']) { case 'latest': $query[] = ['$sort' => ['schedules.vouchers.created_at' => -1]]; break; case 'latest-updated': $query[] = ['$sort' => ['schedules.vouchers.updated_at' => -1]]; break; case 'biggest': $query[] = ['$sort' => ['schedules.vouchers.currency' => 1, 'schedules.vouchers.amount' => -1]]; break; default: $query[] = ['$sort' => ['schedules.vouchers.price' => -1]]; break; } if ($filters['skip']) { $query[] = ['$skip' => $filters['skip']]; } if ($filters['take']) { $query[] = ['$limit' => $filters['take']]; } $data = Model::raw(function ($collection) use($query) { return $collection->aggregate($query); }); /////////////////// // Reformat data // /////////////////// foreach ($data['result'] as $k => $v) { $data['result'][$k]['_id'] = $data['result'][$k]['_id'] . ''; $data['result'][$k]['created_at'] = date('Y-m-d H:i:s', $data['result'][$k]['created_at']->sec); $data['result'][$k]['updated_at'] = date('Y-m-d H:i:s', $data['result'][$k]['updated_at']->sec); } ////////////////////// // Load Travel Agent// ////////////////////// foreach ($data['result'] as $k => $v) { $travel_agent_ids[] = $v['travel_agent_id']; } if (count($travel_agent_ids)) { $results = TravelAgent::find(array_unique($travel_agent_ids)); foreach ($results as $x) { $travel_agents[$x->_id] = $x->toArray(); } } ///////////////////////////////////// // Add travel agent to each result // ///////////////////////////////////// foreach ($data['result'] as $k => $v) { $data['result'][$k]['travel_agent'] = $travel_agents[$v['travel_agent_id']]; } ////////////// // Response // ////////////// return response()->json(JSend::success(['count' => $count, 'data' => $data['result']])->asArray())->setCallback($this->request->input('callback')); }
function tour_per_author() { /////////////////// // GET PARAMETER // /////////////////// $filters = $this->request->only('ongoing'); /////////// // Query // /////////// if ($filters['ongoing']) { $query[] = ['$match' => ['published_at' => ['$lte' => \Carbon\Carbon::now()->format('Y-m-d')], '$or' => [['schedules.departure' => ['$gte' => \Carbon\Carbon::now()->format('Y-m-d')]], ['schedules.departure_until' => ['$gte' => \Carbon\Carbon::now()->format('Y-m-d')]]]]]; } else { $query[] = ['$match' => ['published_at' => ['$lte' => \Carbon\Carbon::now()->format('Y-m-d')]]]; } $query[] = ['$group' => ['_id' => '$user_id', 'last_created_at' => ['$first' => '$created_at'], 'user_id' => ['$push' => '$user_id'], 'count' => ['$sum' => 1]]]; ////////////////////////////// // Get Result // ////////////////////////////// $data = []; $total_tour = Tour::raw(function ($collection) use($query) { return $collection->aggregate($query); }); foreach ($total_tour as $key => $value) { $new = User::find($value['_id']); if ($new) { $new['total_tour'] = $value['count']; $new['last_created_at'] = $value['last_created_at']; $data[] = $new->toArray(); } } return response()->json(JSend::success(['count' => count($data), 'data' => $data])->asArray())->setCallback($this->request->input('callback')); }
static function consolidateTotalFlight() { set_time_limit(0); $env = env('ADS_ENGINE'); if (str_is('on', $env)) { $sort = 'ads.total_balance'; } else { $sort = 'published_at'; } $tomorrow = \Carbon\Carbon::now()->addDay(1); $year = \Carbon\Carbon::now()->addMonth(12); $now = \Carbon\Carbon::now()->format('Y-m-d'); $query[] = ['$match' => ['published_at' => ['$lte' => $now], '$or' => [['schedules.departure' => ['$gte' => $now]], ['schedules.departure_until' => ['$gte' => $now]]]]]; $query[] = ['$unwind' => '$schedules']; $query[] = ['$match' => ['published_at' => ['$lte' => $now], '$or' => [['schedules.departure' => ['$gte' => $now]], ['schedules.departure_until' => ['$gte' => $now]]]]]; $data = Static::latest('created_at')->first(); if ($data) { $data_array = $data->toArray(); } ////////////////////////////////////// // Populate destination with tours // ////////////////////////////////////// foreach ($data_array['destination'] as $k => $destination) { if ($destination['label']) { //////////////////// // GET TOUR COUNT // //////////////////// $total_tour = Tour::TourScheduleBetween($tomorrow, $year)->published()->TagDestination($destination['tag'])->count(); $destination['total_tour'] = $total_tour; $query_dest = $query; unset($filter_tags); if (is_array($destination['tag'])) { foreach ($destination['tag'] as $tag) { $filter_tags[] = new Regex(preg_quote('.' . $tag), 'i'); $filter_tags[] = new Regex(preg_quote($tag . '.'), 'i'); $filter_tags[] = new Regex(preg_quote('.' . $tag . '.'), 'i'); $filter_tags[] = new Regex('^' . preg_quote($tag), '$'); } } else { $filter_tags[] = new Regex(preg_quote('.' . $destination['tag']), 'i'); $filter_tags[] = new Regex(preg_quote($destination['tag'] . '.'), 'i'); $filter_tags[] = new Regex(preg_quote('.' . $destination['tag'] . '.'), 'i'); $filter_tags[] = new Regex('^' . preg_quote($destination['tag']), '$'); } $query_dest[] = ['$match' => ['tags.type' => ['$eq' => 'destination'], 'tags.tag' => ['$in' => $filter_tags]]]; $total_flight = Tour::raw(function ($collection) use($query_dest) { return $collection->aggregate($query_dest); })->count(); $destination['total_flight'] = $total_flight; /////////////// // GET TOURS // /////////////// $tours = Tour::with('travel_agent')->TourScheduleBetween($tomorrow, $year)->published()->TagDestinationAdMode($destination['tag'])->skip(0)->take(4)->orderby($sort, 'desc')->select('name', 'slug', 'schedules', 'travel_agent_id')->get(); $total_tours = count($tours); if ($total_tours < 4) { $ids = []; foreach ($tours as $key => $value) { $ids[] = $value['_id']; } $complement = Tour::with('travel_agent')->TourScheduleBetween($tomorrow, $year)->published()->whereNotIn('_id', $ids)->TagDestination($destination['tag'])->skip(0)->take(4 - $total_tours)->orderby('published_at', 'desc')->select('name', 'slug', 'schedules', 'travel_agent_id')->get(); $destination['tours'] = array_merge($tours->toArray(), $complement->toArray()); } else { $destination['tours'] = $tours->toArray(); } $data_array['destination'][$k] = $destination; unset($destination); unset($total_tour); unset($tours); unset($complement); unset($query_dest); } } ////////////////////////////////////// // Populate tags with tours // ////////////////////////////////////// foreach ($data_array['interest'] as $k => $interest) { if ($interest['label']) { //////////////////// // GET TOUR COUNT // //////////////////// $total_tour = Tour::TourScheduleBetween($tomorrow, $year)->published()->TagInterest($interest['tag'])->count(); $interest['total_tour'] = $total_tour; $query_dest = $query; unset($filter_tags); if (is_array($interest['tag'])) { foreach ($interest['tag'] as $tag) { $filter_tags[] = new Regex(preg_quote($tag), 'i'); } } else { $filter_tags[] = new Regex(preg_quote($interest['tag']), 'i'); } $query_dest[] = ['$match' => ['tags.type' => ['$eq' => 'interest'], 'tags.tag' => ['$in' => $filter_tags]]]; $total_flight = Tour::raw(function ($collection) use($query_dest) { return $collection->aggregate($query_dest); })->count(); $interest['total_flight'] = $total_flight; /////////////// // GET TOURS // /////////////// $tours = Tour::with('travel_agent')->TourScheduleBetween($tomorrow, $year)->published()->TagInterestAdMode($interest['tag'])->skip(0)->take(4)->orderby($sort, 'desc')->select('name', 'slug', 'schedules', 'travel_agent_id')->get(); $total_tours = count($tours); if ($total_tours < 4) { $ids = []; foreach ($tours as $key => $value) { $ids[] = $value['_id']; } $complement = Tour::with('travel_agent')->TourScheduleBetween($tomorrow, $year)->published()->whereNotIn('_id', $ids)->TagInterest($interest['tag'])->skip(0)->take(4 - $total_tours)->orderby('published_at', 'desc')->select('name', 'slug', 'schedules', 'travel_agent_id')->get(); $interest['tours'] = array_merge($tours->toArray(), $complement->toArray()); } else { $interest['tours'] = $tours->toArray(); } $data_array['interest'][$k] = $interest; unset($interest); unset($total_tour); unset($tours); unset($complement); unset($query_dest); } } ////////////////////////////////////// // Populate poi tags with tours // ////////////////////////////////////// foreach ($data_array['poi'] as $k => $poi) { if ($poi['label']) { //////////////////// // GET TOUR COUNT // //////////////////// $total_tour = Tour::TourScheduleBetween($tomorrow, $year)->published()->TagPOI($poi['tag'])->count(); $poi['total_tour'] = $total_tour; $query_dest = $query; unset($filter_tags); if (is_array($poi['tag'])) { foreach ($poi['tag'] as $tag) { $filter_tags[] = new Regex(preg_quote($tag), 'i'); } } else { $filter_tags[] = new Regex(preg_quote($poi['tag']), 'i'); } $query_dest[] = ['$match' => ['tags.type' => ['$eq' => 'poi'], 'tags.tag' => ['$in' => $filter_tags]]]; $total_flight = Tour::raw(function ($collection) use($query_dest) { return $collection->aggregate($query_dest); })->count(); $poi['total_flight'] = $total_flight; /////////////// // GET TOURS // /////////////// $tours = Tour::with('travel_agent')->TourScheduleBetween($tomorrow, $year)->published()->TagPOIAdMode($poi['tag'])->skip(0)->take(4)->orderby($sort, 'desc')->select('name', 'slug', 'schedules', 'travel_agent_id')->get(); $total_tours = count($tours); if ($total_tours < 4) { $ids = []; foreach ($tours as $key => $value) { $ids[] = $value['_id']; } $complement = Tour::with('travel_agent')->TourScheduleBetween($tomorrow, $year)->published()->whereNotIn('_id', $ids)->TagPOI($poi['tag'])->skip(0)->take(4 - $total_tours)->orderby('published_at', 'desc')->select('name', 'slug', 'schedules', 'travel_agent_id')->get(); $poi['tours'] = array_merge($tours->toArray(), $complement->toArray()); } else { $poi['tours'] = $tours->toArray(); } $data_array['poi'][$k] = $poi; unset($poi); unset($total_tour); unset($tours); unset($complement); unset($query_dest); } } $data->fill($data_array); if ($data->save()) { return true; } else { return false; } }