暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

TravelController.php 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. <?php
  2. namespace App\Http\Controllers\Api\V1;
  3. use App\Http\Controllers\Controller;
  4. use Illuminate\Support\Facades\DB;
  5. use App\Model\Travel;
  6. use App\Model\TravelLog;
  7. use App\Model\PointLog;
  8. use App\Model\Profile;
  9. use App\Model\Raharja\EventOrganisasiMembers;
  10. use App\Model\Raharja\EventOrganisasi;
  11. use Illuminate\Support\Facades\Auth;
  12. use Hash;
  13. use Helper;
  14. use Validator;
  15. use Request;
  16. use Carbon\Carbon;
  17. class TravelController extends Controller
  18. {
  19. public function __construct()
  20. {
  21. DB::enableQueryLog();
  22. }
  23. public function getTravel()
  24. {
  25. $query = Travel::get();
  26. $query = array_map(function ($item) {
  27. $item['log'] = TravelLog::where('travel_id', $item['id'])->get();
  28. return $item;
  29. }, $query->toArray());
  30. if($query){
  31. $result = $query;
  32. $res_status = true;
  33. $msg = 'Mendapatkan Data';
  34. $status_msg = $msg;
  35. return $this->resSuccess(null, $res_status, $msg, $status_msg, $result);
  36. }else {
  37. $res_status = false;
  38. $msg = 'Data tidak ditemukan';
  39. $status_msg = $msg;
  40. return $this->resSuccess(null, $res_status, $msg, $status_msg, null);
  41. }
  42. }
  43. public function getSingleTravel($travel_id)
  44. {
  45. $query = Travel::where('travel_id', Helper::hash($travel_id, 'decode'))->get();
  46. $query = array_map(function ($item) {
  47. $item['log'] = TravelLog::where('travel_id', $item['id'])->get();
  48. return $item;
  49. }, $query->toArray());
  50. if($query){
  51. $result = $query;
  52. $res_status = true;
  53. $msg = 'Mendapatkan Data';
  54. $status_msg = $msg;
  55. return $this->resSuccess(null, $res_status, $msg, $status_msg, $result);
  56. }else {
  57. $res_status = false;
  58. $msg = 'Data tidak ditemukan';
  59. $status_msg = $msg;
  60. return $this->resSuccess(null, $res_status, $msg, $status_msg, null);
  61. }
  62. }
  63. public function getLog($travel_id)
  64. {
  65. $query = TravelLog::where('travel_id', Helper::hash($travel_id, 'decode'))->get();
  66. if($query){
  67. $result = $query;
  68. $res_status = true;
  69. $msg = 'Mendapatkan Data';
  70. $status_msg = $msg;
  71. return $this->resSuccess(null, $res_status, $msg, $status_msg, $result);
  72. }else {
  73. $res_status = false;
  74. $msg = 'Data tidak ditemukan';
  75. $status_msg = $msg;
  76. return $this->resSuccess(null, $res_status, $msg, $status_msg, null);
  77. }
  78. }
  79. public function postRequest(Request $request)
  80. {
  81. $validator = Validator::make(Request::all(), [
  82. 'user_id' => ['required'],
  83. ]);
  84. // dd(Helper::hash(Request::all()['user_id'], 'decode'));
  85. if(!$validator->fails()){
  86. $travel = new Travel();
  87. $travel->user_id = Helper::hash(Request::all()['user_id'], 'decode');
  88. $travel->save();
  89. // dd(Helper::hash($travel->id, 'encode'));
  90. if ($travel) {
  91. $res_status = true;
  92. $msg = 'Mendapatkan Data';
  93. $status_msg = $msg;
  94. $travel->hash_id = Helper::hash($travel->id, 'encode');
  95. $travel->user_id = Helper::hash($travel->user_id, 'encode');
  96. $result = $travel;
  97. return $this->resSuccess(null, $res_status, $msg, $status_msg, $result);
  98. }else{
  99. $res_status = false;
  100. $msg = 'Data tidak ditemukan';
  101. $status_msg = $msg;
  102. return $this->resSuccess(null, $res_status, $msg, $status_msg, $result);
  103. }
  104. }else {
  105. $res_status = false;
  106. $msg = 'Error';
  107. $status_msg = $validator->errors();
  108. return $this->resSuccess(null, $res_status, $msg, $status_msg, null);
  109. }
  110. }
  111. public function postLog(Request $request)
  112. {
  113. $validator = Validator::make(Request::all(), [
  114. 'travel_id' => ['required'],
  115. 'check_vehicle' => ['required'],
  116. 'take_call' => ['required'],
  117. 'speeding' => ['required'],
  118. 'sudden_brake' => ['required'],
  119. 'bonus_poin' => ['required'],
  120. 'long_distance' => ['required'],
  121. 'time_travel' => ['required'],
  122. ]);
  123. if(!$validator->fails()){
  124. $list_type = ['check_vehicle','take_call','speeding','sudden_brake','bonus_poin'];
  125. $total = 0;
  126. foreach ($list_type as $key => $value) {
  127. $travel_log[$key] = new TravelLog;
  128. $travel_log[$key]->travel_id = Helper::hash(Request::all()['travel_id'], 'decode');
  129. $travel_log[$key]->type_point = $value;
  130. $travel_log[$key]->point = Request::all()[$value];
  131. $travel_log[$key]->save();
  132. $travel_log[$key]->travel_id = Request::all()['travel_id'];
  133. $travel_log[$key]->hash_id = Helper::hash($travel_log[$key]->id, 'encode');
  134. $total += Request::all()[$value];
  135. }
  136. $user = Travel::find(Helper::hash($travel_log[0]->travel_id, 'decode'))->user_id;
  137. $is_org = Profile::where('user_id', $user)->pluck('is_have_organization')->first();
  138. if($is_org == 'yes'){
  139. $org_id = Profile::where('user_id', $user)->pluck('organization_id')->first();
  140. $evn_id = EventOrganisasiMembers::where('kelompok_driver_id', $org_id)->pluck('event_organisasi_id')->last();
  141. $evn_check = EventOrganisasi::where('id', $evn_id)->first();
  142. if($evn_check['start_date'] <= Carbon::now()->format('Y-m-d') && $evn_check['end_date'] >= Carbon::now()->format('Y-m-d')){
  143. $event_id = 1;
  144. }else{
  145. $event_id = 0;
  146. }
  147. }else{
  148. $event_id = 0;
  149. }
  150. $point = new PointLog();
  151. $point->user_id = Travel::find(Helper::hash($travel_log[0]->travel_id, 'decode'))->user_id;
  152. $point->type_point = 'safety_riding';
  153. $point->point = $total;
  154. $point->event_id = $event_id;
  155. $point->save();
  156. $point->user_id = Helper::hash($point->user_id, 'encode');
  157. $point->hash_id = Helper::hash($point->id, 'encode');
  158. $travel = Travel::find(Helper::hash($travel_log[0]->travel_id, 'decode'));
  159. $travel->long_distance = Request::all()['long_distance'];
  160. $travel->time_travel = Request::all()['time_travel'];
  161. $travel->save();
  162. if ($travel_log && $point && $travel) {
  163. $res_status = true;
  164. $msg = 'Mendapatkan Data';
  165. $status_msg = $msg;
  166. $result = [
  167. 'travel_log' => $travel_log,
  168. 'point' => $point,
  169. 'travel' => $travel,
  170. ];
  171. return $this->resSuccess(null, $res_status, $msg, $status_msg, $result);
  172. }else{
  173. $res_status = false;
  174. $msg = 'Data tidak ditemukan';
  175. $status_msg = $msg;
  176. return $this->resSuccess(null, $res_status, $msg, $status_msg, $result);
  177. }
  178. }else {
  179. $res_status = false;
  180. $msg = 'Error';
  181. $status_msg = $validator->errors();
  182. return $this->resSuccess(null, $res_status, $msg, $status_msg, null);
  183. }
  184. }
  185. public function resSuccess($param = null, $status = null, $msg = null, $status_msg = null, $result = null)
  186. {
  187. $response['response'] = array(
  188. 'status' => $status,
  189. 'message' => $msg,
  190. 'status_msg' => $status_msg,
  191. );
  192. $response['param'] = !empty($param) ? $param : '';
  193. $response['results'] = $result;
  194. return response()->json($response, 200);
  195. }
  196. }