示例#1
0
 function homepage_projection()
 {
     $projections = [];
     $query[] = ['$match' => ['ads.today_balance' => ['$gt' => 0]]];
     ///////////
     // Query //
     ///////////
     $data = Tour::with('travel_agent')->raw(function ($collection) use($query) {
         return $collection->aggregate($query);
     });
     //////////////////////////////
     // 			Get Result 		//
     //////////////////////////////
     foreach ($data as $key2 => $value2) {
         foreach ($value2->ads as $key3 => $value3) {
             $last_balance = $value3['today_balance'];
             $daily_rate = $value3['daily_rate'];
             $initial_credit = $value3['initial_credit'];
             while ($last_balance > 0) {
                 $started = \Carbon\Carbon::parse($value2['started_at']);
                 $days = $started->addDays(($initial_credit - $last_balance) / $daily_rate);
                 $projection['on'] = $days->format('Y-m-d');
                 $projection['balance'] = $last_balance;
                 $projection['tour'] = ['_id' => $value2['_id'], 'name' => $value2['name']];
                 $projection['travel_agent'] = ['_id' => $value2['travel_agent']['_id'], 'name' => $value2['travel_agent']['name']];
                 $last_balance = $last_balance - $daily_rate;
                 $projections[] = $projection;
             }
         }
     }
     return response()->json(JSend::success(['count' => count($data), 'data' => $projections])->asArray())->setCallback($this->request->input('callback'));
 }
示例#2
0
 function index(Request $request)
 {
     $env = env('ADS_ENGINE');
     if (str_is('on', $env)) {
         $sort = 'ads.total_balance';
     } else {
         $sort = 'published_at';
     }
     /////////////////
     // Load Filter //
     /////////////////
     $filters = $request->only('id', 'name', 'slug', 'scheduled_since', 'scheduled_until', 'price_min', 'price_max', 'travel_agent_id', 'tags_destination', 'tags_poi', 'tags_interest', 'type', 'duration_day_min', 'duration_day_max', 'tour_package', 'promo', 'upcoming', 'mode', 'skip', 'take', 'with_count', 'with_travel_agent', 'with_related_tours', 'ask_for_recommended', 'with_unpublished');
     ///////////
     // Query //
     ///////////
     if (!$filters['take']) {
         $filters['take'] = 50;
     } else {
         $filters['take'] = min($filters['take'] * 1, 50);
     }
     $filters['skip'] = $filters['skip'] * 1;
     if ($filters['id']) {
         $data = new Collection();
         $dt = Model::with('travel_agent')->find($filters['id']);
         if ($filters['with_related_tours'] && $dt) {
             $dt->related_tours = Model::getRelatedTour($dt, 0, 12);
         }
         $data->push($dt);
     } else {
         /////////////////
         // MAKE QUERY  //
         /////////////////
         $q = Model::name($filters['name'])->slug($filters['slug']);
         if ($filters['with_unpublished']) {
             $q = $q;
         } else {
             $q = $q->published();
         }
         //////////////////////////////////////////////////
         // FILTER BY SCHEDULE (INC BUDGET) 				//
         //////////////////////////////////////////////////
         if ($filters['scheduled_since']) {
             try {
                 $filters['scheduled_since'] = \Carbon\Carbon::parse($filters['scheduled_since']);
             } catch (\Exception $e) {
                 return response()->json(JSend::fail(['schedule' => ['Invalid scheduled_since parameter: must be a valid date']])->asArray())->setCallback($this->request->input('callback'));
             }
         }
         if ($filters['scheduled_until']) {
             try {
                 $filters['scheduled_until'] = \Carbon\Carbon::parse($filters['scheduled_until']);
             } catch (\Exception $e) {
                 return response()->json(JSend::fail(['scheduled_until' => ['Invalid scheduled_until parameter: must be a valid date']])->asArray())->setCallback($this->request->input('callback'));
             }
         }
         if ($filters['scheduled_since'] && $filters['scheduled_until']) {
             $q = $q->TourScheduleBetween($filters['scheduled_since'], $filters['scheduled_until'], head(explode('-', $filters['budget'])), last(explode('-', $filters['budget'])), $type);
         }
         ///////////////////
         // FILTER BUDGET //
         ///////////////////
         if ($filters['price_min'] && $filters['price_max']) {
             $q = $q->priceBetween($filters['price_min'], $filters['price_max']);
         }
         /////////////////////////////
         // FILTER BY TRAVEL AGENT  //
         /////////////////////////////
         if ($filters['travel_agent_id']) {
             $q = $q->withTravelAgentId($filters['travel_agent_id']);
         }
         ///////////////////
         // FILTER BY TAG //
         ///////////////////
         if ($filters['tags_destination'] || $filters['tags_poi'] || $filters['tags_interest']) {
             $q = $q->TagDestination($filters['tags_destination']);
             $q = $q->TagPoi($filters['tags_poi']);
             $q = $q->TagInterest($filters['tags_interest']);
         }
         ////////////////////
         // FILTER BY TYPE //
         ////////////////////
         if ($filters['type']) {
             $q = $q->type($filters['type']);
         }
         ////////////////////////
         // FILTER BY DURATION //
         ////////////////////////
         if ($filters['duration_day_min'] && $filters['duration_day_max']) {
             $q = $q->durationDayBetween($filters['duration_day_min'], $filters['duration_day_max']);
         }
         ////////////////////////////
         // FILTER BY TOUR PACKAGE //
         ////////////////////////////
         if ($filters['tour_package']) {
             $q = $q->tourPackage($filters['tour_package']);
         }
         ////////////////////////////
         // FILTER BY PROMO 		  //
         ////////////////////////////
         if ($filters['promo']) {
             $q = $q->inPromo($filters['promo']);
         }
         ////////////////////////////
         // FILTER BY UPCOMING 	 //
         ////////////////////////////
         if ($filters['upcoming']) {
             $q = $q->upcomingtour();
         }
         ////////////////////
         // DO COUNT QUERY //
         ////////////////////
         if ($filters['with_count']) {
             $count = $q->count();
         }
         //////////
         // TAKE //
         //////////
         $filters['take'] = min(max(1, $filters['take']), 50);
         //////////
         // MODE //
         //////////
         switch (strtolower($filters['mode'])) {
             case 'latest':
                 $q = $q->orderBy($sort, 'desc');
                 break;
             case 'price-asc':
                 $q = $q->orderby('cron_cheapest_price', 'asc');
                 break;
             case 'price-desc':
                 $q = $q->orderby('cron_cheapest_price', 'desc');
                 break;
             case 'duration-asc':
                 $q = $q->orderBy('duration.day', 'asc');
                 break;
             case 'duration-desc':
                 $q = $q->orderBy('duration.day', 'desc');
                 break;
             case 'latest-created':
                 $q = $q->latest('created_at');
                 break;
         }
         ///////////////////////
         // WITH TRAVEL AGENT //
         ///////////////////////
         if ($filters['with_travel_agent']) {
             $q = $q->with('travel_agent');
         }
         //////////////
         // DO QUERY //
         //////////////
         $data = $q->skip($filters['skip'])->take($filters['take'])->get();
         ////////////////////////
         // WITH RELATED TOURS //
         ////////////////////////
         if ($filters['with_related_tours']) {
             foreach ($data as $k => $v) {
                 $data[$k]->related_tours = Model::getRelatedTour($v, 0, 12);
             }
         }
     }
     /////////////////
     // Recommended //
     /////////////////
     if ($count < 1 && $filters['ask_for_recommended']) {
         $recommended = [];
         $total_recommended = 4;
         $i = 0;
         $ids = [];
         foreach ($filters as $key => $value) {
             $q = Model::whereNotIn('_id', $ids)->published();
             //////////////////////////////////////////////////
             // FILTER BY SCHEDULE (INC BUDGET) 				//
             //////////////////////////////////////////////////
             if ($key == 'scheduled_since') {
                 try {
                     $value = \Carbon\Carbon::parse($value);
                     $q = $q->TourScheduleBetween($value, \Carbon\Carbon::now());
                 } catch (\Exception $e) {
                     return response()->json(JSend::fail(['schedule' => ['Invalid scheduled_since parameter: must be a valid date']])->asArray())->setCallback($this->request->input('callback'));
                 }
             }
             if ($key == 'scheduled_until') {
                 try {
                     $value = \Carbon\Carbon::parse($value);
                     $q = $q->TourScheduleBetween(\Carbon\Carbon::now(), $value);
                 } catch (\Exception $e) {
                     return response()->json(JSend::fail(['scheduled_until' => ['Invalid scheduled_until parameter: must be a valid date']])->asArray())->setCallback($this->request->input('callback'));
                 }
             }
             ///////////////////
             // FILTER BUDGET //
             ///////////////////
             if ($key == 'price_min') {
                 $q = $q->priceBetween(0, $value);
             }
             if ($key == 'price_max') {
                 $q = $q->priceBetween(0, $value);
             }
             /////////////////////////////
             // FILTER BY TRAVEL AGENT  //
             /////////////////////////////
             if ($key == 'travel_agent_id') {
                 $q = $q->withTravelAgentId($value);
             }
             ///////////////////
             // FILTER BY TAG //
             ///////////////////
             if ($key == 'tags_destination') {
                 $q = $q->TagDestination($value);
             }
             if ($key == 'tags_poi') {
                 $q = $q->TagPoi($value);
             }
             if ($key == 'tags_interest') {
                 $q = $q->TagInterest($value);
             }
             ////////////////////
             // FILTER BY TYPE //
             ////////////////////
             if ($key == 'type') {
                 $q = $q->type($value);
             }
             ////////////////////////
             // FILTER BY DURATION //
             ////////////////////////
             if ($key == 'duration_day_min') {
                 $q = $q->durationDayBetween(0, $value);
             }
             if ($key == 'duration_day_max') {
                 $q = $q->durationDayBetween(0, $value);
             }
             ////////////////////////////
             // FILTER BY TOUR PACKAGE //
             ////////////////////////////
             if ($key == 'tour_package') {
                 $q = $q->tourPackage($value);
             }
             ////////////////////////////
             // FILTER BY PROMO 		  //
             ////////////////////////////
             if ($key == 'promo') {
                 $q = $q->inPromo($value);
             }
             $i = $i + 1;
             if ($i < count($filters) - 1 && $total_recommended > 0) {
                 $data = $q->orderby($sort)->take(1)->with('travel_agent')->get();
                 if (count($data) > 0) {
                     $total_recommended = $total_recommended - 1;
                     $recommended = array_merge($recommended, $data->toArray());
                     $ids[] = $data[0]['_id'];
                 }
             } elseif ($total_recommended > 0) {
                 $data = $q->orderby($sort)->take($total_recommended)->with('travel_agent')->get();
                 if (count($data) == $total_recommended) {
                     $total_recommended = 0;
                     $recommended = array_merge($recommended, $data->toArray());
                     foreach ($data as $key2 => $value2) {
                         $ids[] = $value2['_id'];
                     }
                 } elseif (count($data) > 0) {
                     $total_recommended = $total_recommended - count($data);
                     $recommended = array_merge($recommended, $data->toArray());
                     foreach ($data as $key2 => $value2) {
                         $ids[] = $value2['_id'];
                     }
                 }
             }
         }
         if ($total_recommended > 0) {
             $q = Model::whereNotIn('_id', $ids)->published();
             $data = $q->orderby($sort)->take($total_recommended)->with('travel_agent')->get();
             $recommended = array_merge($recommended, $data->toArray());
             foreach ($data as $key2 => $value2) {
                 $ids[] = $value2['_id'];
             }
         }
         return response()->json(JSend::success(['count' => $count, 'data' => $recommended])->asArray())->setCallback($this->request->input('callback'));
     }
     //////////////
     // Response //
     //////////////
     return response()->json(JSend::success(['count' => $count, 'data' => $data->toArray()])->asArray())->setCallback($this->request->input('callback'));
 }
示例#3
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;
     }
 }