예제 #1
0
 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;
     }
 }