123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311 |
- <?php
-
- namespace App\Http\Controllers\Api;
-
- use Illuminate\Http\Request;
- use App\Http\Controllers\Api\BaseController;
-
- use Config;
- use File;
- use Carbon\Carbon;
-
- use App\Model\Staff;
- use App\Model\StaffDetail;
- use App\Model\Module\Department;
- use App\Model\Module\Roles;
-
- class StaffController extends BaseController
- {
-
- /**
- * Create a staff list controller.
- *
- * @return json
- */
- public function staffList($roles)
- {
- $nested_data = array();
- if($roles == 'sysadmin'){
- $staff = Staff::with(['StaffDetail' => function($q){
- $q->with('Department');
- }])->orderBy('updated_at', 'desc')->get();
- }else{
- $staff = Staff::with(['StaffDetail' => function($q){
- $q->with('Department');
- }])->orderBy('updated_at', 'desc')->where('roles_access','!=','sysadmin')->where('roles_access','!=','PPegawai')->where('roles_access','!=','PPenguatKuasa')->get();
- }
-
- $curr = Carbon::now()->getTimestamp();
- $i = 1;
- foreach($staff as $a)
- {
- $n1 = ''; $jabatan = '';
- $reg_time = $a->updated_at;
- $expiry_date = $reg_time->addDays(3);
- $expiry_date = $expiry_date->getTimestamp();
-
- if($curr < $expiry_date) {
- $n1 = "Baru/";
- }else{
- $n1 = "";
- }
-
- $temp = [];
- if(!empty($a->StaffDetail->department_ids)){
- foreach ($a->StaffDetail->Department as $key => $d) {
- $temp[] = $d->jbkod;
- $jabatan = implode( ", ", $temp );
- }
- }else{
- $jabatan = '-';
- }
-
- array_push($nested_data, array(
- 'index' => $n1.$i,
- 'nama' => $a->StaffDetail->full_name,
- 'ic' => $a->StaffDetail->identity,
- 'phone' => $a->StaffDetail->mobile,
- 'no_badan' => $a->StaffDetail->no_badan,
- 'jawatan' => $a->roles_access,
- 'jabatan' => $jabatan,
- 'tindakan' => $a->_id
- ));
-
- $i++;
- }
- return \DataTables::of($nested_data)->make(true);
- }
-
- public function officerStaffList($jbkod,$roles)
- {
- $nested_data = array();
- if($roles == 'sysadmin'){
- $staff = Staff::with(['StaffDetail' => function($q) use($jbkod){
- $q->where('department_ids',$jbkod);
- }])->orderBy('updated_at', 'desc')->where('roles_access','PenguatKuasa')->orWhere('roles_access','PPenguatKuasa')->get();
- }else if($jbkod == 'null'){
- $staff = Staff::with(['StaffDetail' => function($q){
- $q->with('Department');
- }])->orderBy('updated_at', 'desc')->where('roles_access','PenguatKuasa')->get();
- }else{
- $staff = Staff::with(['StaffDetail' => function($q) use($jbkod){
- $q->where('department_ids',$jbkod);
- }])->orderBy('updated_at', 'desc')->where('roles_access','PenguatKuasa')->get();
- }
-
- $curr = Carbon::now()->getTimestamp();
- $i = 1;
- foreach($staff as $a) {
- $n1 = ''; $jabatan = '';
- $reg_time = $a->updated_at;
- $expiry_date = $reg_time->addDays(3);
- $expiry_date = $expiry_date->getTimestamp();
-
- if($curr < $expiry_date) {
- $n1 = "Baru/";
- }else{
- $n1 = "";
- }
-
- if(!empty($a->StaffDetail)){
-
- if($jbkod == 'null'){
- $temp = [];
- $user = StaffDetail::with('Department')->where('_id', $roles)->first();
-
- if(!empty($a->StaffDetail->department_ids)){
-
- foreach ($a->StaffDetail->Department as $key => $d) {
- $temp[] = $d->jbkod;
- $jabatan = implode( ", ", $temp );
- }
-
- foreach ($a->StaffDetail->department_ids as $key => $d) {
- if (in_array($d, (array)$user->department_ids)) {
- array_push($nested_data, array(
- 'index' => $n1.$i,
- 'nama' => $a->StaffDetail->full_name,
- 'ic' => $a->StaffDetail->identity,
- 'phone' => $a->StaffDetail->mobile,
- 'no_badan' => $a->StaffDetail->no_badan,
- 'jawatan' => $a->roles_access,
- 'jabatan' => $jabatan,
- 'tindakan' => $a->_id
- ));
-
- $i++; break;
- }else{
- continue;
- }
- }
- }
- }else{
- $temp = [];
- if(!empty($a->StaffDetail->department_ids)){
- foreach ($a->StaffDetail->department_ids as $key => $d) {
- $department = Department::where('_id',$d)->first();
- if(!empty($department)){
- $temp[] = '['.$department->jbkod.'] '.$department->jnama;
- $jabatan = implode( ", ", $temp );
- }
- }
- }else{
- $jabatan = '-';
- }
-
- array_push($nested_data, array(
- 'index' => $n1.$i,
- 'nama' => $a->StaffDetail->full_name,
- 'ic' => $a->StaffDetail->identity,
- 'phone' => $a->StaffDetail->mobile,
- 'no_badan' => $a->StaffDetail->no_badan,
- 'jawatan' => $a->roles_access,
- 'jabatan' => $jabatan,
- 'tindakan' => $a->_id
- ));
-
- $i++;
- }
-
- }
- }
- return \DataTables::of($nested_data)->make(true);
- }
-
- public function staffFilterList($department,$position,$module)
- {
- $nested_data = array();
-
- if($department == "null" && $position == "null" && $module == "null"){
- $staff = StaffDetail::orderBy('updated_at', 'desc')->get();
- }else if($department != "null" && $position == "null" && $module == "null"){
- $staff = StaffDetail::orderBy('updated_at', 'desc')->where('department_ids',$department)->get();
- }else if($department != "null" && $position != "null" && $module == "null"){
- $staff = StaffDetail::orderBy('updated_at', 'desc')->where('department_ids',$department)->where('roles_access',$position)->get();
- }
- else if($department != "null" && $position != "null" && $module != "null"){
- $staff = StaffDetail::whereHas('roles', function($q) use($module) {
- $q->where('_id', $module)->first();
- })->orderBy('updated_at', 'desc')->where('department_ids',$department)->where('roles_access',$position)->get();
- }
- else if($department == "null" && $position != "null" && $module == "null"){
- $staff = StaffDetail::orderBy('updated_at', 'desc')->where('roles_access',$position)->get();
- }
- else if($department == "null" && $position != "null" && $module != "null"){
- $staff = StaffDetail::whereHas('roles', function($q) use($module) {
- $q->where('_id', $module)->first();
- })->orderBy('updated_at', 'desc')->where('roles_access',$position)->get();
- }
- else if($department == "null" && $position == "null" && $module != "null"){
- $staff = StaffDetail::whereHas('roles', function($q) use($module) {
- $q->where('_id', $module)->first();
- })->orderBy('updated_at', 'desc')->where('roles_access',$position)->get();
- }
-
-
- $curr = Carbon::now()->getTimestamp();
- $i = 1;
- foreach($staff as $a)
- {
- $n1 = ''; $jabatan = '';
- $reg_time = $a->updated_at;
- $expiry_date = $reg_time->addDays(3);
- $expiry_date = $expiry_date->getTimestamp();
-
- if($curr < $expiry_date) {
- $n1 = "Baru/";
- }else{
- $n1 = "";
- }
-
- $temp = [];
- if(!empty($a->department_ids)){
- foreach ($a->department_ids as $key => $d) {
- $department = Department::where('_id',$d)->first();
- if(!empty($department)){
- $temp[] = '['.$department->jbkod.'] '.$department->jnama;
- $jabatan = implode( ", ", $temp );
- }
- }
- }else{
- $jabatan = '-';
- }
-
- array_push($nested_data, array(
- 'index' => $n1.$i,
- 'nama' => $a->full_name,
- 'ic' => $a->identity,
- 'phone' => $a->mobile,
- 'no_badan' => $a->no_badan,
- 'jawatan' => $a->roles_access,
- 'jabatan' => $jabatan,
- 'tindakan' => $a->_id
- ));
-
- $i++;
- }
- return \DataTables::of($nested_data)->make(true);
- }
-
- public function staffDetail(Request $request)
- {
- $staff = StaffDetail::with('Roles')->where('_id', $request->id)->first();
- if(!empty($staff)){
- $akses = '';
- if($staff->roles_access == "Pegawai"){
- foreach($staff->roles_ids as $r){
- $role = Roles::where('_id',$r)->first();
- $akses .= $role->name . ", ";
- }
-
- $akses = rtrim($akses, ", ");
- }
-
- $department = '';$temp = [];
- if(!empty($staff->department_ids)){
- foreach ($staff->department_ids as $key => $d) {
- $depart = Department::where('_id',$d)->first();
- if(!empty($depart)){
- $temp[] = '['.$depart->jbkod.'] '.$depart->jnama;
- $department = implode( ", ", $temp );
- }
- }
- }else{
- $department = '-';
- }
-
- $data = [
- 'profile_img' => $staff->profile_img,
- 'full_name' => $staff->full_name,
- 'username' => $staff->username,
- 'identity' => $staff->identity,
- 'mobile' => $staff->mobile,
- 'jbkod' => $department,
- 'roles_access' => $staff->roles_access,
- 'gred' => $staff->gred,
- 'no_badan' => $staff->no_badan,
- 'email' => $staff->email,
- 'address' => $staff->address,
- 'access' => $akses
- ];
- return response()->json(['status' => 'true', 'desc' => $data]);
- }else{
- return response()->json(['status' => 'false', 'desc' => 'Rekod ini tidak ditemui']);
- }
- }
-
- public function deleteStaff(Request $request) {
- $staff = Staff::with('StaffDetail')->where('_id',$request->id)->first();
- if(!empty($staff)){
- $staff->StaffDetail->department()->detach();
- if($staff->roles_access == "Pegawai"){
- $staff->StaffDetail->roles()->detach();
- }
- $staff->staffdetail()->delete();
- $staff->delete();
- return response()->json(['status' => 'true', 'desc' => 'Buang rekod kakitangan ( '.$staff->email.' )']);
- }else{
- return response()->json(['status' => 'false', 'desc' => 'Buang rekod kakitangan ( '.$staff->email.' )']);
- }
- }
- }
|