123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241 |
- <?php
-
- namespace App\Http\Controllers\Api\V1;
-
- use App\Http\Controllers\Controller;
- use Illuminate\Support\Facades\DB;
- use App\Model\Travel;
- use App\Model\TravelLog;
- use App\Model\PointLog;
- use App\Model\Profile;
- use App\Model\Raharja\EventOrganisasiMembers;
- use App\Model\Raharja\EventOrganisasi;
- use Illuminate\Support\Facades\Auth;
- use Hash;
- use Helper;
- use Validator;
- use Request;
- use Carbon\Carbon;
-
- class TravelController extends Controller
- {
- public function __construct()
- {
- DB::enableQueryLog();
- }
-
- public function getTravel()
- {
- $query = Travel::get();
- $query = array_map(function ($item) {
- $item['log'] = TravelLog::where('travel_id', $item['id'])->get();
- return $item;
- }, $query->toArray());
-
- if($query){
- $result = $query;
-
- $res_status = true;
- $msg = 'Mendapatkan Data';
- $status_msg = $msg;
-
- return $this->resSuccess(null, $res_status, $msg, $status_msg, $result);
-
- }else {
-
- $res_status = false;
- $msg = 'Data tidak ditemukan';
- $status_msg = $msg;
-
- return $this->resSuccess(null, $res_status, $msg, $status_msg, null);
- }
- }
-
- public function getSingleTravel($travel_id)
- {
- $query = Travel::where('travel_id', Helper::hash($travel_id, 'decode'))->get();
- $query = array_map(function ($item) {
- $item['log'] = TravelLog::where('travel_id', $item['id'])->get();
- return $item;
- }, $query->toArray());
-
- if($query){
- $result = $query;
-
- $res_status = true;
- $msg = 'Mendapatkan Data';
- $status_msg = $msg;
-
- return $this->resSuccess(null, $res_status, $msg, $status_msg, $result);
-
- }else {
-
- $res_status = false;
- $msg = 'Data tidak ditemukan';
- $status_msg = $msg;
-
- return $this->resSuccess(null, $res_status, $msg, $status_msg, null);
- }
- }
-
- public function getLog($travel_id)
- {
- $query = TravelLog::where('travel_id', Helper::hash($travel_id, 'decode'))->get();
-
- if($query){
- $result = $query;
-
- $res_status = true;
- $msg = 'Mendapatkan Data';
- $status_msg = $msg;
-
- return $this->resSuccess(null, $res_status, $msg, $status_msg, $result);
-
- }else {
-
- $res_status = false;
- $msg = 'Data tidak ditemukan';
- $status_msg = $msg;
-
- return $this->resSuccess(null, $res_status, $msg, $status_msg, null);
- }
- }
-
- public function postRequest(Request $request)
- {
- $validator = Validator::make(Request::all(), [
- 'user_id' => ['required'],
- ]);
- // dd(Helper::hash(Request::all()['user_id'], 'decode'));
-
- if(!$validator->fails()){
- $travel = new Travel();
- $travel->user_id = Helper::hash(Request::all()['user_id'], 'decode');
- $travel->save();
- // dd(Helper::hash($travel->id, 'encode'));
-
- if ($travel) {
- $res_status = true;
- $msg = 'Mendapatkan Data';
- $status_msg = $msg;
- $travel->hash_id = Helper::hash($travel->id, 'encode');
- $travel->user_id = Helper::hash($travel->user_id, 'encode');
- $result = $travel;
-
- return $this->resSuccess(null, $res_status, $msg, $status_msg, $result);
- }else{
- $res_status = false;
- $msg = 'Data tidak ditemukan';
- $status_msg = $msg;
-
- return $this->resSuccess(null, $res_status, $msg, $status_msg, $result);
- }
- }else {
-
- $res_status = false;
- $msg = 'Error';
- $status_msg = $validator->errors();
-
- return $this->resSuccess(null, $res_status, $msg, $status_msg, null);
- }
- }
-
- public function postLog(Request $request)
- {
- $validator = Validator::make(Request::all(), [
- 'travel_id' => ['required'],
- 'check_vehicle' => ['required'],
- 'take_call' => ['required'],
- 'speeding' => ['required'],
- 'sudden_brake' => ['required'],
- 'bonus_poin' => ['required'],
- 'long_distance' => ['required'],
- 'time_travel' => ['required'],
- ]);
-
- if(!$validator->fails()){
- $list_type = ['check_vehicle','take_call','speeding','sudden_brake','bonus_poin'];
- $total = 0;
- foreach ($list_type as $key => $value) {
- $travel_log[$key] = new TravelLog;
- $travel_log[$key]->travel_id = Helper::hash(Request::all()['travel_id'], 'decode');
- $travel_log[$key]->type_point = $value;
- $travel_log[$key]->point = Request::all()[$value];
- $travel_log[$key]->save();
- $travel_log[$key]->travel_id = Request::all()['travel_id'];
- $travel_log[$key]->hash_id = Helper::hash($travel_log[$key]->id, 'encode');
-
- $total += Request::all()[$value];
- }
- $user = Travel::find(Helper::hash($travel_log[0]->travel_id, 'decode'))->user_id;
- $is_org = Profile::where('user_id', $user)->pluck('is_have_organization')->first();
- if($is_org == 'yes'){
- $org_id = Profile::where('user_id', $user)->pluck('organization_id')->first();
- $evn_id = EventOrganisasiMembers::where('kelompok_driver_id', $org_id)->pluck('event_organisasi_id')->last();
- $evn_check = EventOrganisasi::where('id', $evn_id)->first();
-
- if($evn_check['start_date'] <= Carbon::now()->format('Y-m-d') && $evn_check['end_date'] >= Carbon::now()->format('Y-m-d')){
- $event_id = 1;
- }else{
- $event_id = 0;
- }
- }else{
- $event_id = 0;
- }
- $point = new PointLog();
- $point->user_id = Travel::find(Helper::hash($travel_log[0]->travel_id, 'decode'))->user_id;
- $point->type_point = 'safety_riding';
- $point->point = $total;
- $point->event_id = $event_id;
- $point->save();
- $point->user_id = Helper::hash($point->user_id, 'encode');
- $point->hash_id = Helper::hash($point->id, 'encode');
-
- $travel = Travel::find(Helper::hash($travel_log[0]->travel_id, 'decode'));
- $travel->long_distance = Request::all()['long_distance'];
- $travel->time_travel = Request::all()['time_travel'];
- $travel->save();
-
- if ($travel_log && $point && $travel) {
- $res_status = true;
- $msg = 'Mendapatkan Data';
- $status_msg = $msg;
- $result = [
- 'travel_log' => $travel_log,
- 'point' => $point,
- 'travel' => $travel,
- ];
-
- return $this->resSuccess(null, $res_status, $msg, $status_msg, $result);
- }else{
- $res_status = false;
- $msg = 'Data tidak ditemukan';
- $status_msg = $msg;
-
- return $this->resSuccess(null, $res_status, $msg, $status_msg, $result);
- }
- }else {
-
- $res_status = false;
- $msg = 'Error';
- $status_msg = $validator->errors();
-
- return $this->resSuccess(null, $res_status, $msg, $status_msg, null);
- }
- }
-
- public function resSuccess($param = null, $status = null, $msg = null, $status_msg = null, $result = null)
- {
- $response['response'] = array(
- 'status' => $status,
- 'message' => $msg,
- 'status_msg' => $status_msg,
- );
- $response['param'] = !empty($param) ? $param : '';
-
- $response['results'] = $result;
-
- return response()->json($response, 200);
- }
-
- }
|