Dashboard sipadu mbip
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

StaffController.php 9.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use Illuminate\Http\Request;
  4. use App\Http\Controllers\Api\BaseController;
  5. use Config;
  6. use File;
  7. use Carbon\Carbon;
  8. use App\Model\Staff;
  9. use App\Model\StaffDetail;
  10. use App\Model\Module\Department;
  11. use App\Model\Module\Roles;
  12. class StaffController extends BaseController
  13. {
  14. /**
  15. * Create a staff list controller.
  16. *
  17. * @return json
  18. */
  19. public function staffList($roles)
  20. {
  21. $nested_data = array();
  22. if($roles == 'sysadmin'){
  23. $staff = Staff::with(['StaffDetail' => function($q){
  24. $q->with('Department');
  25. }])->orderBy('updated_at', 'desc')->get();
  26. }else{
  27. $staff = Staff::with(['StaffDetail' => function($q){
  28. $q->with('Department');
  29. }])->orderBy('updated_at', 'desc')->where('roles_access','!=','sysadmin')->where('roles_access','!=','PPegawai')->where('roles_access','!=','PPenguatKuasa')->get();
  30. }
  31. $curr = Carbon::now()->getTimestamp();
  32. $i = 1;
  33. foreach($staff as $a)
  34. {
  35. $n1 = ''; $jabatan = '';
  36. $reg_time = $a->updated_at;
  37. $expiry_date = $reg_time->addDays(3);
  38. $expiry_date = $expiry_date->getTimestamp();
  39. if($curr < $expiry_date) {
  40. $n1 = "Baru/";
  41. }else{
  42. $n1 = "";
  43. }
  44. $temp = [];
  45. if(!empty($a->StaffDetail->department_ids)){
  46. foreach ($a->StaffDetail->Department as $key => $d) {
  47. $temp[] = $d->jbkod;
  48. $jabatan = implode( ", ", $temp );
  49. }
  50. }else{
  51. $jabatan = '-';
  52. }
  53. array_push($nested_data, array(
  54. 'index' => $n1.$i,
  55. 'nama' => $a->StaffDetail->full_name,
  56. 'ic' => $a->StaffDetail->identity,
  57. 'phone' => $a->StaffDetail->mobile,
  58. 'no_badan' => $a->StaffDetail->no_badan,
  59. 'jawatan' => $a->roles_access,
  60. 'jabatan' => $jabatan,
  61. 'tindakan' => $a->_id
  62. ));
  63. $i++;
  64. }
  65. return \DataTables::of($nested_data)->make(true);
  66. }
  67. public function officerStaffList($jbkod,$roles)
  68. {
  69. $nested_data = array();
  70. if($roles == 'sysadmin'){
  71. $staff = Staff::with(['StaffDetail' => function($q) use($jbkod){
  72. $q->where('department_ids',$jbkod);
  73. }])->orderBy('updated_at', 'desc')->where('roles_access','PenguatKuasa')->orWhere('roles_access','PPenguatKuasa')->get();
  74. }else if($jbkod == 'null'){
  75. $staff = Staff::with(['StaffDetail' => function($q){
  76. $q->with('Department');
  77. }])->orderBy('updated_at', 'desc')->where('roles_access','PenguatKuasa')->get();
  78. }else{
  79. $staff = Staff::with(['StaffDetail' => function($q) use($jbkod){
  80. $q->where('department_ids',$jbkod);
  81. }])->orderBy('updated_at', 'desc')->where('roles_access','PenguatKuasa')->get();
  82. }
  83. $curr = Carbon::now()->getTimestamp();
  84. $i = 1;
  85. foreach($staff as $a) {
  86. $n1 = ''; $jabatan = '';
  87. $reg_time = $a->updated_at;
  88. $expiry_date = $reg_time->addDays(3);
  89. $expiry_date = $expiry_date->getTimestamp();
  90. if($curr < $expiry_date) {
  91. $n1 = "Baru/";
  92. }else{
  93. $n1 = "";
  94. }
  95. if(!empty($a->StaffDetail)){
  96. if($jbkod == 'null'){
  97. $temp = [];
  98. $user = StaffDetail::with('Department')->where('_id', $roles)->first();
  99. if(!empty($a->StaffDetail->department_ids)){
  100. foreach ($a->StaffDetail->Department as $key => $d) {
  101. $temp[] = $d->jbkod;
  102. $jabatan = implode( ", ", $temp );
  103. }
  104. foreach ($a->StaffDetail->department_ids as $key => $d) {
  105. if (in_array($d, (array)$user->department_ids)) {
  106. array_push($nested_data, array(
  107. 'index' => $n1.$i,
  108. 'nama' => $a->StaffDetail->full_name,
  109. 'ic' => $a->StaffDetail->identity,
  110. 'phone' => $a->StaffDetail->mobile,
  111. 'no_badan' => $a->StaffDetail->no_badan,
  112. 'jawatan' => $a->roles_access,
  113. 'jabatan' => $jabatan,
  114. 'tindakan' => $a->_id
  115. ));
  116. $i++; break;
  117. }else{
  118. continue;
  119. }
  120. }
  121. }
  122. }else{
  123. $temp = [];
  124. if(!empty($a->StaffDetail->department_ids)){
  125. foreach ($a->StaffDetail->department_ids as $key => $d) {
  126. $department = Department::where('_id',$d)->first();
  127. if(!empty($department)){
  128. $temp[] = '['.$department->jbkod.'] '.$department->jnama;
  129. $jabatan = implode( ", ", $temp );
  130. }
  131. }
  132. }else{
  133. $jabatan = '-';
  134. }
  135. array_push($nested_data, array(
  136. 'index' => $n1.$i,
  137. 'nama' => $a->StaffDetail->full_name,
  138. 'ic' => $a->StaffDetail->identity,
  139. 'phone' => $a->StaffDetail->mobile,
  140. 'no_badan' => $a->StaffDetail->no_badan,
  141. 'jawatan' => $a->roles_access,
  142. 'jabatan' => $jabatan,
  143. 'tindakan' => $a->_id
  144. ));
  145. $i++;
  146. }
  147. }
  148. }
  149. return \DataTables::of($nested_data)->make(true);
  150. }
  151. public function staffFilterList($department,$position,$module)
  152. {
  153. $nested_data = array();
  154. if($department == "null" && $position == "null" && $module == "null"){
  155. $staff = StaffDetail::orderBy('updated_at', 'desc')->get();
  156. }else if($department != "null" && $position == "null" && $module == "null"){
  157. $staff = StaffDetail::orderBy('updated_at', 'desc')->where('department_ids',$department)->get();
  158. }else if($department != "null" && $position != "null" && $module == "null"){
  159. $staff = StaffDetail::orderBy('updated_at', 'desc')->where('department_ids',$department)->where('roles_access',$position)->get();
  160. }
  161. else if($department != "null" && $position != "null" && $module != "null"){
  162. $staff = StaffDetail::whereHas('roles', function($q) use($module) {
  163. $q->where('_id', $module)->first();
  164. })->orderBy('updated_at', 'desc')->where('department_ids',$department)->where('roles_access',$position)->get();
  165. }
  166. else if($department == "null" && $position != "null" && $module == "null"){
  167. $staff = StaffDetail::orderBy('updated_at', 'desc')->where('roles_access',$position)->get();
  168. }
  169. else if($department == "null" && $position != "null" && $module != "null"){
  170. $staff = StaffDetail::whereHas('roles', function($q) use($module) {
  171. $q->where('_id', $module)->first();
  172. })->orderBy('updated_at', 'desc')->where('roles_access',$position)->get();
  173. }
  174. else if($department == "null" && $position == "null" && $module != "null"){
  175. $staff = StaffDetail::whereHas('roles', function($q) use($module) {
  176. $q->where('_id', $module)->first();
  177. })->orderBy('updated_at', 'desc')->where('roles_access',$position)->get();
  178. }
  179. $curr = Carbon::now()->getTimestamp();
  180. $i = 1;
  181. foreach($staff as $a)
  182. {
  183. $n1 = ''; $jabatan = '';
  184. $reg_time = $a->updated_at;
  185. $expiry_date = $reg_time->addDays(3);
  186. $expiry_date = $expiry_date->getTimestamp();
  187. if($curr < $expiry_date) {
  188. $n1 = "Baru/";
  189. }else{
  190. $n1 = "";
  191. }
  192. $temp = [];
  193. if(!empty($a->department_ids)){
  194. foreach ($a->department_ids as $key => $d) {
  195. $department = Department::where('_id',$d)->first();
  196. if(!empty($department)){
  197. $temp[] = '['.$department->jbkod.'] '.$department->jnama;
  198. $jabatan = implode( ", ", $temp );
  199. }
  200. }
  201. }else{
  202. $jabatan = '-';
  203. }
  204. array_push($nested_data, array(
  205. 'index' => $n1.$i,
  206. 'nama' => $a->full_name,
  207. 'ic' => $a->identity,
  208. 'phone' => $a->mobile,
  209. 'no_badan' => $a->no_badan,
  210. 'jawatan' => $a->roles_access,
  211. 'jabatan' => $jabatan,
  212. 'tindakan' => $a->_id
  213. ));
  214. $i++;
  215. }
  216. return \DataTables::of($nested_data)->make(true);
  217. }
  218. public function staffDetail(Request $request)
  219. {
  220. $staff = StaffDetail::with('Roles')->where('_id', $request->id)->first();
  221. if(!empty($staff)){
  222. $akses = '';
  223. if($staff->roles_access == "Pegawai"){
  224. foreach($staff->roles_ids as $r){
  225. $role = Roles::where('_id',$r)->first();
  226. $akses .= $role->name . ", ";
  227. }
  228. $akses = rtrim($akses, ", ");
  229. }
  230. $department = '';$temp = [];
  231. if(!empty($staff->department_ids)){
  232. foreach ($staff->department_ids as $key => $d) {
  233. $depart = Department::where('_id',$d)->first();
  234. if(!empty($depart)){
  235. $temp[] = '['.$depart->jbkod.'] '.$depart->jnama;
  236. $department = implode( ", ", $temp );
  237. }
  238. }
  239. }else{
  240. $department = '-';
  241. }
  242. $data = [
  243. 'profile_img' => $staff->profile_img,
  244. 'full_name' => $staff->full_name,
  245. 'username' => $staff->username,
  246. 'identity' => $staff->identity,
  247. 'mobile' => $staff->mobile,
  248. 'jbkod' => $department,
  249. 'roles_access' => $staff->roles_access,
  250. 'gred' => $staff->gred,
  251. 'no_badan' => $staff->no_badan,
  252. 'email' => $staff->email,
  253. 'address' => $staff->address,
  254. 'access' => $akses
  255. ];
  256. return response()->json(['status' => 'true', 'desc' => $data]);
  257. }else{
  258. return response()->json(['status' => 'false', 'desc' => 'Rekod ini tidak ditemui']);
  259. }
  260. }
  261. public function deleteStaff(Request $request) {
  262. $staff = Staff::with('StaffDetail')->where('_id',$request->id)->first();
  263. if(!empty($staff)){
  264. $staff->StaffDetail->department()->detach();
  265. if($staff->roles_access == "Pegawai"){
  266. $staff->StaffDetail->roles()->detach();
  267. }
  268. $staff->staffdetail()->delete();
  269. $staff->delete();
  270. return response()->json(['status' => 'true', 'desc' => 'Buang rekod kakitangan ( '.$staff->email.' )']);
  271. }else{
  272. return response()->json(['status' => 'false', 'desc' => 'Buang rekod kakitangan ( '.$staff->email.' )']);
  273. }
  274. }
  275. }