function top_up_log() { ///////////////// // Load Filter // ///////////////// $filters = $this->request->only('id', 'title', 'slug', 'skip', 'take', 'mode'); if (!$filters['take']) { $filters['take'] = 50; } else { $filters['take'] = min($filters['take'] * 1, 50); } $filters['skip'] = $filters['skip'] * 1; /////////// // Query // /////////// $q = new Model(); if ($filters['id']) { $count = $q->count(); $data = $q->where('travel_agent._id', $filters['id'])->skip($filters['skip'])->take($filters['take'])->get(); } else { $count = $q->count(); switch ($filters['mode']) { case 'latest_created': $q = $q->latest('created_at'); break; case 'latest_published': $q = $q->latest('published_at'); break; } $data = $q->skip($filters['skip'])->take($filters['take'])->get(); } ////////////// // Response // ////////////// return response()->json(JSend::success(['count' => $count, 'data' => $data->toArray()])->asArray()); }
/** * store a resource * @param Request $request http request * @param mixed $id id of the resource for updating * @return jsend jsend with newly stored source */ function store($id = null) { //////////////// // Load Data // //////////////// if ($id) { $data = Model::find($id); if (!$data) { return app()->abort(404); } } else { $data = new Model(); } /////////////////////////////////// // Assign posted data to Data // /////////////////////////////////// $data->fill($this->request->input()); $data->slug = Model::generateSlug($data->name, $data->_id); ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // REMOVE THIS AFTERWARD // ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // $data->created_at = $this->request->input('created_at'); /////////////////////////////////////////////////////////////////// // Validate data // /////////////////////////////////////////////////////////////////// # Validate Travel Agent $travel_agent = TravelAgent::find($this->request->input('travel_agent_id')); if (!$travel_agent) { return response()->json(JSend::fail(['travel_agent_id' => ['Travel Agent is not recognized']])->asArray())->setCallback($this->request->input('callback')); } $data->travel_agent_id = $travel_agent->id; /////////////////////// // EMBED IMAGES // /////////////////////// foreach ($this->request->input('images') as $x) { $images[] = new Image($x); } if (!$data->syncImages($images)) { return response()->json(JSend::fail($data->getErrors())->asArray())->setCallback($this->request->input('callback')); } /////////////////////// // EMBED TAGS // /////////////////////// foreach ($this->request->input('tags') as $x) { if ($x['tag'] && $x['type']) { $tags[] = new Tag($x); } } if (!$data->syncTags($tags)) { return response()->json(JSend::fail($data->getErrors())->asArray())->setCallback($this->request->input('callback')); } //////////////////////// // EMBED TourSchedule // //////////////////////// foreach ($this->request->input('schedules') as $x) { if ($x['_id']) { $tour_schedule = $data->schedules->where('_id', $x['_id'])->first(); if (!$tour_schedule) { return app()->abort(404); } $tour_schedule->fill($x); } else { $tour_schedule = new TourSchedule($x); } ////////////////////// // Sync Promo // ////////////////////// $promos = []; foreach ($x['promos'] as $promo_array) { if ($promo_array['_id']) { $promo = $tour_schedule->promos->where('_id', $promo_array['_id']['$id'])->first(); if (!$promo) { return app()->abort(404); } $promo->fill($promo_array); $promos[] = $promo; } else { $promos[] = new Promo($promo_array); } } if (!$tour_schedule->syncPromo($promos)) { return response()->json(JSend::fail($tour_schedule->getErrors())->asArray())->setCallback($this->request->input('callback')); } ////////////////////// // Sync Voucher // ////////////////////// $vouchers = []; foreach ($x['vouchers'] as $voucher_array) { if ($voucher_array['_id']) { $voucher = $tour_schedule->vouchers->where('_id', $voucher_array['_id']['$id'])->first(); if (!$voucher) { return App()->abort(404); } $voucher->fill($voucher_array); $vouchers[] = $voucher; } else { $vouchers[] = new Voucher($voucher_array); } } if (!$tour_schedule->syncVoucher($vouchers)) { return response()->json(JSend::fail($tour_schedule->getErrors())->asArray())->setCallback($this->request->input('callback')); } $tour_schedules[] = $tour_schedule; } if (!$data->syncTourSchedules($tour_schedules)) { return response()->json(JSend::fail($data->getErrors())->asArray())->setCallback($this->request->input('callback')); } /////////////////////// // EMBED PACKAGE // /////////////////////// if (!$data->syncTourPackage(new TourPackage($this->request->input('package')))) { return response()->json(JSend::fail($data->getErrors())->asArray())->setCallback($this->request->input('callback')); } ////////////////// // Check Top Up // ////////////////// if ($data->ads) { $older_data = $data->getOriginal(); } /////////// // Store // /////////// if ($data->save()) { $new_ads = $data->ads; if ($data->ads) { $ids_inc = []; foreach ($data->ads as $key => $value) { //1. Check if new data if (empty($value['ad_id'])) { $new_ad = $data->ads[$key]; $new_ad['ad_id'] = uniqid(); $new_ads[$key] = $new_ad; $attributes['name'] = 'Top Up'; $attributes['ads_id'] = $new_ad['ad_id']; $attributes['transact_at'] = \Carbon\Carbon::now()->format('Y-m-d'); $attributes['travel_agent']['_id'] = $data->travel_agent['_id']; $attributes['travel_agent']['name'] = $data->travel_agent['name']; $attributes['credit']['started_at'] = $data->ads[$key]['started_at']; $attributes['credit']['initial_credit'] = $data->ads[$key]['initial_credit'] * 1; $attributes['credit']['daily_rate'] = $data->ads[$key]['daily_rate'] * 1; $attributes['credit']['today_balance'] = $data->ads[$key]['today_balance'] * 1; $attributes['credit']['credit_balance'] = $data->ads[$key]['credit_balance'] * 1; $attributes['credit']['tags'] = $data->ads[$key]['tags']; $attributes['tour']['_id'] = $data['_id']; $attributes['tour']['name'] = $data['name']; $attributes['tour']['tags'] = $data['tags']->toArray(); $top_up = new TopUpLog(); $top_up->fill($attributes); if (!$top_up->save()) { $error_responses = []; foreach ($top_up->getErrors() as $field => $errors) { $error_responses[$field] = implode(', ', $errors); } return response()->json(JSend::fail($top_up->getErrors())->asArray()); } } elseif ($older_data['ads'][$key]['started_at'] != $data->ads[$key]->started_at || $older_data['ads'][$key]['initial_credit'] != $data->ads[$key]->initial_credit || $older_data['ads'][$key]['daily_rate'] != $data->ads[$key]->daily_rate || $older_data['ads'][$key]['today_balance'] != $data->ads[$key]->today_balance || $older_data['ads'][$key]['credit_balance'] != $data->ads[$key]->credit_balance) { //emptied last balance $last_balance = TopUpLog::where('ads_id', $older_data['ads'][$key]['ad_id'])->orderBy('created_at', 'desc')->first(); if ($last_balance && $last_balance['credit']['today_balance'] > 0) { $attributes['name'] = 'Emptied Top Up'; $attributes['ads_id'] = $older_data['ads'][$key]['ad_id']; $attributes['transact_at'] = \Carbon\Carbon::now()->format('Y-m-d'); $attributes['travel_agent']['_id'] = $data->travel_agent['_id']; $attributes['travel_agent']['name'] = $data->travel_agent['name']; $attributes['credit']['started_at'] = $older_data['ads'][$key]['started_at']; $attributes['credit']['initial_credit'] = $older_data['ads'][$key]['initial_credit'] * 1; $attributes['credit']['daily_rate'] = $older_data['ads'][$key]['daily_rate'] * 1; $attributes['credit']['today_balance'] = 0 * 1; $attributes['credit']['credit_balance'] = (0 - $older_data['ads'][$key]['today_balance']) * 1; $attributes['credit']['tags'] = $older_data['ads'][$key]['tags']; $attributes['tour']['_id'] = $older_data['_id']; $attributes['tour']['name'] = $older_data['name']; $attributes['tour']['tags'] = $older_data['tags']; $top_up = new TopUpLog(); $top_up->fill($attributes); if (!$top_up->save()) { $error_responses = []; foreach ($top_up->getErrors() as $field => $errors) { $error_responses[$field] = implode(', ', $errors); } return response()->json(JSend::fail($top_up->getErrors())->asArray()); } } //create new top up $new_ad = $data->ads[$key]; $new_ads[$key] = $new_ad; $attributes['name'] = 'Edited Top Up'; $attributes['ads_id'] = $new_ad['ad_id']; $attributes['transact_at'] = \Carbon\Carbon::now()->format('Y-m-d'); $attributes['travel_agent']['_id'] = $data->travel_agent['_id']; $attributes['travel_agent']['name'] = $data->travel_agent['name']; $attributes['credit']['started_at'] = $data->ads[$key]['started_at'] * 1; $attributes['credit']['initial_credit'] = $data->ads[$key]['initial_credit'] * 1; $attributes['credit']['daily_rate'] = $data->ads[$key]['daily_rate'] * 1; $attributes['credit']['today_balance'] = $data->ads[$key]['initial_credit'] * 1; $attributes['credit']['credit_balance'] = $data->ads[$key]['initial_credit'] * 1; $attributes['credit']['tags'] = $data->ads[$key]['tags']; $attributes['tour']['_id'] = $data['_id']; $attributes['tour']['name'] = $data['name']; $attributes['tour']['tags'] = $data['tags']->toArray(); $top_up = new TopUpLog(); $top_up->fill($attributes); if (!$top_up->save()) { $error_responses = []; foreach ($top_up->getErrors() as $field => $errors) { $error_responses[$field] = implode(', ', $errors); } return response()->json(JSend::fail($top_up->getErrors())->asArray()); } } if ($new_ad['ad_id']) { $ids_inc[] = $new_ad['ad_id']; } } //3. Check if deleted data foreach ($older_data['ads'] as $key => $value) { if (!in_array($value['ad_id'], $ids_inc)) { $attributes['name'] = 'Deleted Top Up'; $attributes['ads_id'] = $older_data['ads'][$key]['ad_id']; $attributes['transact_at'] = \Carbon\Carbon::now()->format('Y-m-d'); $attributes['travel_agent']['_id'] = $older_data['travel_agent']['_id']; $attributes['travel_agent']['name'] = $older_data['travel_agent']['name']; $attributes['credit']['started_at'] = $older_data['ads'][$key]['started_at'] * 1; $attributes['credit']['initial_credit'] = $older_data['ads'][$key]['initial_credit'] * 1; $attributes['credit']['daily_rate'] = $older_data['ads'][$key]['daily_rate'] * 1; $attributes['credit']['today_balance'] = 0 * 1; $attributes['credit']['credit_balance'] = (0 - $older_data['ads'][$key]['today_balance']) * 1; $attributes['credit']['tags'] = $older_data['ads'][$key]['tags']; $attributes['tour']['_id'] = $older_data['_id']; $attributes['tour']['name'] = $older_data['name']; $attributes['tour']['tags'] = $older_data['tags']; $top_up = new TopUpLog(); $top_up->fill($attributes); if (!$top_up->save()) { $error_responses = []; foreach ($top_up->getErrors() as $field => $errors) { $error_responses[$field] = implode(', ', $errors); } return response()->json(JSend::fail($top_up->getErrors())->asArray()); } } } $data->ads = $new_ads; if (!$data->save()) { return response()->json(JSend::fail($data->getErrors())->asArray()); } } return response()->json(JSend::success(['data' => $data])->asArray()); } else { $error_responses = []; foreach ($data->getErrors() as $field => $errors) { $error_responses[$field] = implode(', ', $errors); } return response()->json(JSend::fail($data->getErrors())->asArray()); } }
/** * 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; }