No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RumahSakitController.php 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. <?php
  2. namespace App\Http\Controllers\Admin\User_management;
  3. use App\Helpers\Helper;
  4. use App\Http\Controllers\Controller;
  5. use App\Model\Master\MasterRumahSakitKepememilikans;
  6. use App\Model\Master\MasterRumahSakitType;
  7. use App\Model\ModelHasRoles;
  8. use Hashids;
  9. use Illuminate\Http\Request;
  10. use Illuminate\Support\Facades\DB;
  11. use Validator;
  12. use Yajra\Datatables\Datatables;
  13. use Auth;
  14. use Illuminate\Support\Facades\Route;
  15. //load modelmu
  16. use Illuminate\Support\Str;
  17. use App\Model\User\MasterRumahSakits as user_rs;
  18. use App\User;
  19. use Spatie\Permission\Models\Role;
  20. use Hash;
  21. class RumahSakitController extends Controller
  22. {
  23. /**
  24. * Title untuk judul di web
  25. * route digunakan untuk tempat resource (file path) + routing (route/web) diusahain sama ya biar gak ngubah"
  26. */
  27. private $title = 'User Managemen | Rumah Sakit';
  28. /**jangan lupa diganti*/
  29. private $route = 'admin.user_management.rumah_sakit.'; //path awal foldernya ajah (misal folder di admin/dashboard) => 'admin.dashboard' | jangan lupa diganti
  30. private $namespace_controller = 'Admin\User_management';
  31. private $url_prefix = 'user-management/rumah-sakit/';
  32. public function __construct()
  33. {
  34. DB::enableQueryLog();
  35. /** nyalakan jika sudah set rolenya, jika ini dinyalakan halaman ini tidak akan keluar */
  36. $this->middleware('permission:rumah_sakit-list|rumah_sakit-create|rumah_sakit-update|rumah_sakit-delete', ['only' => ['index', 'create', 'update']]);
  37. $this->middleware('permission:rumah_sakit-create', ['only' => ['create', 'create_action']]);
  38. $this->middleware('permission:rumah_sakit-update', ['only' => ['update', 'update_action']]);
  39. $this->middleware('permission:rumah_sakit-delete', ['only' => ['delete']]);
  40. }
  41. /**
  42. * Ini contoh crud yang sudah jalan
  43. * index digunakna untuk tampilan awal dari menu yang akan dibuat
  44. * Untuk CRUD Biar cepat
  45. * Silahkan ganti
  46. * user_rs:: => dengan model anda
  47. * $rumah_sakit_id => ganti dengan id di model anda
  48. * rumah_sakit_id => jangan lupa disesuaikan dengan yang dideclare di route
  49. */
  50. public function index()
  51. {
  52. if (session('success')) {
  53. alert()->html('', session('success'), 'success');
  54. }
  55. if (session('error')) {
  56. alert()->html('', session('error'), 'error');
  57. }
  58. $data = [
  59. //bawaan
  60. 'title' => $this->title,
  61. 'route' => $this->route,
  62. ];
  63. // dd($data);
  64. return view($this->route . 'index', $data);
  65. }
  66. /**
  67. *digunakan untuk datatable yajra
  68. *querynya bisa ganti" sesuai hati
  69. */
  70. public function getData()
  71. {
  72. $query = user_rs::All();
  73. foreach ($query as $key => $value) {
  74. $value->no = ($key + 1);
  75. $value->email = $value->user->email ?? null;
  76. }
  77. return Datatables::of($query)
  78. ->addColumn('aksi', function ($query) {
  79. $aksi = '';
  80. if (Auth::user()->can('rumah_sakit-update')) {
  81. $aksi .= "<a href='" . route($this->route . 'update', ['rumah_sakit_id' => Hashids::encode($query->id)]) . "' class='btn btn-sm btn-primary btn-edit'>Edit</a>";
  82. }
  83. if (Auth::user()->can('rumah_sakit-delete')) {
  84. // $aksi .= "<a href='javascript:;' data-route='" . route($this->route . 'delete_action', ['rumah_sakit_id' => Hashids::encode($query->id)]) . "' class='btn btn-danger btn-sm btn-delete'>Delete</a>";
  85. }
  86. return $aksi;
  87. })
  88. ->rawColumns(['aksi'])
  89. ->toJson();
  90. }
  91. /**
  92. * create digunakan untuk menampilkan tampilan buat
  93. */
  94. public function create()
  95. {
  96. $data = [
  97. //bawaan
  98. 'title' => $this->title,
  99. 'route' => $this->route,
  100. 'roles' => Role::where('id',5)->get(),
  101. ];
  102. if (session('success')) {
  103. alert()->html('', session('success'), 'success');
  104. }
  105. if (session('error')) {
  106. alert()->html('', session('error'), 'error');
  107. }
  108. return view($this->route . 'create', $data);
  109. }
  110. /**
  111. * create_action digunakan untuk aksi post
  112. * lengkap dengan validatornya
  113. */
  114. public function create_action(Request $request)
  115. {
  116. //declare post untuk simpan semua request yang diinputkan oleh form name=""
  117. $post = $request->all();
  118. // dd($post);
  119. /**digunakan untuk set rule validator */
  120. $rules = [
  121. 'role_id' => 'required',
  122. 'nama' => 'required',
  123. 'deskripsi' => 'required',
  124. 'email' => 'required|unique:users,email',
  125. 'password' => 'required:min:6',
  126. 'alamat' => 'required',
  127. 'nama_bank_darah' => 'required',
  128. 'kode_rumah_sakit' => 'required',
  129. 'jenis' => 'required',
  130. 'kepemilikan' => 'required',
  131. ];
  132. /**digunakan untuk set message dari validatornya yang akan keluar gimna, :attribue itu udah langsung mendeteksi inputtan dari name="attribue" */
  133. $alert = [
  134. 'required' => ':attribute',
  135. 'unique' => ':attribute telah terpakai',
  136. 'min' => ':attribute :min karakter',
  137. ];
  138. $validator = Validator::make($post, $rules, $alert);
  139. if ($validator->passes()) {
  140. /**menggunakan transaction */
  141. DB::beginTransaction();
  142. $user_rs = [
  143. 'email' => $post['email'],
  144. 'name' => $post['nama'],
  145. 'password' => Hash::make($post['password']),
  146. ];
  147. $insert_user = User::create($user_rs);
  148. $insert_user->assignRole($request['role_id']);
  149. $data_user_rs = $request->only(['nama', 'deskripsi', 'alamat', 'nama_bank_darah', 'kode_rumah_sakit']);
  150. $data_user_rs['user_login_id'] = $insert_user->id;
  151. $data_user_rs['type_id'] = $post['jenis'];
  152. $data_user_rs['kepemilikan_id'] = $post['kepemilikan'];
  153. $insert_user_rs = user_rs::create($data_user_rs);
  154. if ($insert_user_rs) {
  155. DB::commit();
  156. $message = 'Berhasil';
  157. return redirect(route($this->route . 'index'))->with('success', Helper::parsing_alert($message));
  158. } else {
  159. DB::rollback();
  160. $message = 'Gagal';
  161. return redirect()->back()->with('error', Helper::parsing_alert($message));
  162. }
  163. }
  164. /**kenapa menggunakan back ? karena baliknya pasti ke halaman sebelumnya */
  165. $message = Helper::parsing_alert($validator->errors()->all());
  166. return redirect()->back()->with('error', $message)->withInput();
  167. }
  168. /** update sama seperti create hanya saja digunakan untuk update (viewnya saja) */
  169. public function update($rumah_sakit_id)
  170. {
  171. if (session('success')) {
  172. alert()->html('', session('success'), 'success');
  173. }
  174. if (session('error')) {
  175. alert()->html('', session('error'), 'error');
  176. }
  177. $rumah_sakit_id = Hashids::decode($rumah_sakit_id);
  178. if (!empty($rumah_sakit_id)) {
  179. $cek_data = user_rs::where('id', $rumah_sakit_id[0])->first();
  180. if ($cek_data) {
  181. $data = [
  182. //bawaan
  183. 'title' => $this->title,
  184. 'route' => $this->route,
  185. 'data' => $cek_data,
  186. 'roles' => Role::where('id',5)->get()
  187. ];
  188. return view($this->route . 'update', $data);
  189. }
  190. $message = 'Id tidak ditemukan atau sudah dihapus';
  191. return redirect()->back()->with('error', $message);
  192. }
  193. $message = 'Id tdak ditemukan';
  194. return redirect()->back()->with('error', $message);
  195. }
  196. /**update_action POST
  197. * Logikanya :
  198. * 1. Cek Hashids::decode dari data_id apalah benar" di hash ?
  199. * -iya
  200. * 2. Cek ID yang didapatkan dari hash apakah benar adanya ?
  201. * -iya
  202. * 3. proses validate
  203. * -iya
  204. * 4. Proses update
  205. * - iya benar
  206. * - tidak salah
  207. * - tidak return error
  208. * - tidak return Id tdak ditemukan
  209. * -tidak skip reutrn id null
  210. */
  211. public function update_action(Request $request)
  212. {
  213. $post = $request->all();
  214. /**digunakan untuk set rule validator */
  215. // dd($post);
  216. $rules = [
  217. 'role_id' => 'required',
  218. 'nama' => 'required',
  219. 'deskripsi' => 'required',
  220. 'email' => 'required',
  221. 'alamat' => 'required',
  222. 'nama_bank_darah' => 'required',
  223. 'jenis' => 'required',
  224. 'kepemilikan' => 'required',
  225. ];
  226. /**digunakan untuk set message dari validatornya yang akan keluar gimna, :attribue itu udah langsung mendeteksi inputtan dari name="attribue" */
  227. $alert = [
  228. 'required' => ':attribute',
  229. ];
  230. $validator = Validator::make($post, $rules, $alert);
  231. $rumah_sakit_id = Hashids::decode($request['id']);
  232. if (!empty($rumah_sakit_id)) {
  233. /**cek apakah data_idnya ada ? */
  234. $cek_data = user_rs::where('id', $rumah_sakit_id[0])->first();
  235. if ($cek_data) {
  236. /**cek apakah id data benar" ada di DB ? */
  237. if ($validator->passes()) {
  238. // dd($post);
  239. /**validatornya */
  240. DB::beginTransaction();
  241. $data_user = [
  242. 'name' => $request['nama'],
  243. 'email' => $request['email'],
  244. ];
  245. if ($request['password']) {
  246. $length = Str::of($request['password'])->length();
  247. if ($length < 6) {
  248. $message = 'Password minimal 6 karakter';
  249. return redirect()->back()->withInput()->with('error', $message);
  250. }
  251. $data_user['password'] = Hash::make($request['password']);
  252. }
  253. /**delete roles */
  254. ModelHasRoles::where('model_id', $cek_data->user->id)->delete();
  255. $find_user = User::findOrFail($cek_data->user->id);
  256. $update_user = $find_user->update($data_user);
  257. $find_user->assignRole($request['role_id']);
  258. $data_rs = $request->only(['nama', 'deskripsi', 'alamat', 'nama_bank_darah','kode_rumah_sakit']);
  259. $data_rs['type_id'] = $post['jenis'];
  260. $data_rs['kepemilikan_id'] = $post['kepemilikan'];
  261. $update_rs = $cek_data->update($data_rs);
  262. if ($update_user && $update_rs) {
  263. DB::commit();
  264. $message = 'Berhasil';
  265. return redirect(route($this->route . 'index'))->with('success', Helper::parsing_alert($message));
  266. } else {
  267. DB::rollback();
  268. $message = 'Gagal';
  269. return redirect()->back()->with('error', Helper::parsing_alert($message));
  270. }
  271. }
  272. $message = Helper::parsing_alert($validator->errors()->all());
  273. return redirect()->back()->with('error', $message);
  274. } else {
  275. $message = 'Id tdak ditemukan';
  276. return redirect()->back()->with('error', $message);
  277. }
  278. } else {
  279. $message = 'Id tidak boleh kosong';
  280. return redirect()->back()->with('error', $message);
  281. }
  282. }
  283. /**digunakan untuk delete
  284. * Logikanya :
  285. * 1. Cek hash id
  286. * -ya
  287. * 2. Cek id dari hash
  288. * - ya
  289. * 3. Proses delete
  290. * -Ya
  291. * -Tidak
  292. * - tidak return Id tidak ditemukan atau sudah dihapus
  293. * -tidak reutrn Id tdak ditemukan
  294. */
  295. public function delete_action($rumah_sakit_id)
  296. {
  297. $rumah_sakit_id = Hashids::decode($rumah_sakit_id);
  298. /**cek idnya apakah ada ? */
  299. if (!empty($rumah_sakit_id)) {
  300. $cek_data = user_rs::where('id', $rumah_sakit_id[0])->first();
  301. if ($cek_data) {
  302. DB::beginTransaction();
  303. $delete = $cek_data->delete();
  304. if ($delete) {
  305. DB::commit();
  306. $message = 'Succesfully';
  307. $response = [
  308. 'message' => $message,
  309. 'status' => true,
  310. ];
  311. return response()->json($response);
  312. } else {
  313. DB::rollback();
  314. $message = 'Gagal';
  315. $response = [
  316. 'message' => $message,
  317. 'status' => false,
  318. ];
  319. return response()->json($response);
  320. }
  321. }
  322. $message = 'Id tidak ditemukan atau sudah dihapus';
  323. $response = [
  324. 'message' => $message,
  325. 'status' => false,
  326. ];
  327. return response()->json($response);
  328. }
  329. $message = 'Id tdak ditemukan';
  330. $response = [
  331. 'message' => $message,
  332. 'status' => false,
  333. ];
  334. return response()->json($response);
  335. }
  336. public function selectJenisRs(Request $request){
  337. if (session('error')) {
  338. alert()->html('', session('error'), 'error');
  339. }
  340. $term = trim($request['petugas']);
  341. $query = MasterRumahSakitType::take(5);
  342. if (!empty($request['not_in'])) {
  343. $query = $query->whereNotIn('id', [$request['not_in']]);
  344. }
  345. if (!empty($term)) {
  346. $query = $query->where('nama', 'like', '%' . $term . '%');
  347. }
  348. $query = $query->get();
  349. $response = [];
  350. foreach ($query as $key => $value) {
  351. $response[] = ['id' => $value->id, 'text' => $value->nama];
  352. }
  353. return response()->json($response);
  354. }
  355. public function selectKepemilikanRs(Request $request){
  356. if (session('error')) {
  357. alert()->html('', session('error'), 'error');
  358. }
  359. $term = trim($request['petugas']);
  360. $query = MasterRumahSakitKepememilikans::take(5);
  361. if (!empty($request['not_in'])) {
  362. $query = $query->whereNotIn('id', [$request['not_in']]);
  363. }
  364. if (!empty($term)) {
  365. $query = $query->where('nama', 'like', '%' . $term . '%');
  366. }
  367. $query = $query->get();
  368. $response = [];
  369. foreach ($query as $key => $value) {
  370. $response[] = ['id' => $value->id, 'text' => $value->nama];
  371. }
  372. return response()->json($response);
  373. }
  374. //custom route
  375. public function route()
  376. {
  377. return Route::group([
  378. 'as' => $this->route,
  379. 'prefix' => $this->url_prefix,
  380. 'namespace' => $this->namespace_controller
  381. ], function () {
  382. Route::get('/', ['as' => 'index', 'uses' => 'RumahSakitController@index']);
  383. Route::get('/create', ['as' => 'create', 'uses' => 'RumahSakitController@create']);
  384. Route::get('/update/{rumah_sakit_id}', ['as' => 'update', 'uses' => 'RumahSakitController@update']);
  385. Route::get('/delete-action/{rumah_sakit_id}', ['as' => 'delete_action', 'uses' => 'RumahSakitController@delete_action']);
  386. Route::get('/get-data', ['as' => 'get_data', 'uses' => 'RumahSakitController@getData']);
  387. Route::get('/select_jenis_rs', ['as' => 'select_jenis_rs', 'uses' => 'RumahSakitController@selectJenisRs']);
  388. Route::get('/select_jenis_kepemilikan', ['as' => 'select_jenis_kepemilikan', 'uses' => 'RumahSakitController@selectKepemilikanRs']);
  389. Route::post('/create-action', ['as' => 'create_action', 'uses' => 'RumahSakitController@create_action']);
  390. Route::post('/update-action', ['as' => 'update_action', 'uses' => 'RumahSakitController@update_action']);
  391. });
  392. }
  393. //end custom route
  394. }