/**
  * lock around 20 - next 20
  *
  * @return void
  * @author 
  **/
 public function calculatebalance()
 {
     \Log::info('Re calculate ads` balance @' . date('Y-m-d H:i:s'));
     $tours = Tour::published()->upcomingtour()->adsstarted('now')->with('travel_agent')->get();
     $now = \Carbon\Carbon::now()->format('Y-m-d');
     foreach ($tours as $key => $value) {
         foreach ($value['ads'] as $key2 => $value2) {
             $query = [];
             $query[] = ['$match' => ['ads_id' => ['$eq' => $value2['ad_id']]]];
             $query[] = ['$group' => ['_id' => '$ads_id', 'count' => ['$sum' => '$credit.credit_balance'], 'total' => ['$sum' => 1]]];
             $prev_calc = TopUpLog::raw(function ($collection) use($query, $value2) {
                 return $collection->aggregate($query);
             })->toArray();
             $started = \Carbon\Carbon::parse($value2['started_at']);
             $days = $started->diffInDays(\Carbon\Carbon::parse('today'));
             $today_balance = $value2['initial_credit'] - $days * $value2['daily_rate'];
             if ($value2['started_at'] <= $now && $value2['today_balance'] > 0 && ($prev_calc[0]['count'] > 0 && $prev_calc[0]['count'] != $today_balance || is_null($prev_calc[0]))) {
                 $new_balance = $value['ads'];
                 $new_balance[$key2]['today_balance'] = $value2['initial_credit'] - $days * $value2['daily_rate'];
                 if (is_null($prev_calc[0]['count'])) {
                     $new_balance[$key2]['credit_balance'] = 0 - $new_balance[$key2]['today_balance'];
                 } else {
                     $new_balance[$key2]['credit_balance'] = 0 - ($prev_calc[0]['count'] - $new_balance[$key2]['today_balance']);
                 }
                 $value->ads = $new_balance;
                 $attributes['name'] = 'Auto debit Top Up';
                 $attributes['ads_id'] = $value2['ad_id'];
                 $attributes['transact_at'] = \Carbon\Carbon::now()->format('Y-m-d');
                 $attributes['travel_agent']['_id'] = $value->travel_agent['_id'];
                 $attributes['travel_agent']['name'] = $value->travel_agent['name'];
                 $attributes['credit']['started_at'] = $value2['started_at'];
                 $attributes['credit']['initial_credit'] = $value2['initial_credit'] * 1;
                 $attributes['credit']['daily_rate'] = $value2['daily_rate'] * 1;
                 $attributes['credit']['today_balance'] = $new_balance[$key2]['today_balance'] * 1;
                 $attributes['credit']['credit_balance'] = $new_balance[$key2]['credit_balance'];
                 $attributes['credit']['tags'] = $value2['tags'];
                 $attributes['tour']['_id'] = $value['_id'];
                 $attributes['tour']['name'] = $value['name'];
                 $attributes['tour']['tags'] = $value['tags'];
                 $top_up = new TopUpLog();
                 $top_up->fill($attributes);
                 if (!$top_up->save()) {
                     \Log::error(json_encode($top_up->getError()));
                 }
             }
         }
         if (!$value->save()) {
             \Log::error(json_encode($value->getError()));
         }
     }
     return true;
 }
Exemplo n.º 2
0
 /**
  * lock around 20 - next 20
  *
  * @return void
  * @author 
  **/
 public function checkcheapest()
 {
     \Log::info('Running Check Cheapest Tour @' . date('Y-m-d H:i:s'));
     $tours = Tour::published()->upcomingtour()->get();
     foreach ($tours as $key => $value) {
         $now = Carbon::now()->format('Y-m-d');
         $min_cost = null;
         foreach ($value['schedules'] as $key2 => $value2) {
             if (($value2['departure_until'] >= $now && $value2['departure'] >= $now || $value2['departure_until'] == '' && $value2['departure'] >= $now) && (is_null($min_cost) || $min_cost > $value2['price'])) {
                 $min_cost = $value2['price'];
             }
             // else
             // {
             // 	\Log::info($value2['departure']);
             // }
         }
         $value->cron_cheapest_price = $min_cost;
         if (!$value->save()) {
             \Log::error(json_encode($value->getError()));
         }
     }
     return true;
 }