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.
 
 
 
 

219 lines
9.1 KiB

<?php
namespace App\Http\Controllers\API;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use App\Models\{HumanResource, UserToActivity, UserToProyek};
use Illuminate\Support\Facades\Artisan;
class HumanResourceController extends Controller
{
public function add(Request $request)
{
$validator = Validator::make($request->all(), [
'role_id' => 'required|integer',
'divisi_id' => 'nullable|integer',
'username' => 'nullable|string|max:80',
'password' => 'nullable|string|max:100',
'session_login' => 'nullable|string',
'name' => 'required|string',
'phone_number' => 'required|string|max:15',
'email' => 'email|nullable',
'address' => 'nullable',
'fcm_token' => 'nullable',
'gender' => 'nullable|string|max:50',
'birth_place' => 'nullable|string|max:150',
'birth_date' => 'nullable|date',
'blood_type' => 'nullable|string|max:2',
'ktp_number' => 'required|max:20|string',
'employee_type' => 'required|string',
'status_resource' => 'nullable|string|max:50',
'status_boundary' => 'nullable|boolean',
'created_by' => 'nullable|string|max:80',
'updated_by' => 'nullable|string|max:80'
]);
if ($validator->fails()) {
return response()->json(['status' => 'failed', 'message' => 'Human Resource Pool failed created', 'code' => 400]);
}
$validated = $validator->validated();
if (isset($validated['password']) && $validated['password'] != "") {
$validated['password'] = bcrypt($validated['password']);
}
$result = HumanResource::create($validated);
if ($result) {
return response()->json(['status' => 'success', 'message' => 'Human Resource Pool successfull created', 'code' => 200]);
} else {
return response()->json(['status' => 'failed', 'message' => 'Human Resource Pool failed created', 'code' => 400]);
}
}
public function edit($id)
{
if (empty($id)) {
return response()->json(['status' => 'failed', 'message' => 'id is required!', 'code' => 400], 400);
}
$result = HumanResource::find($id);
if ($result) {
return response()->json(['status' => 'success', 'code' => 200, 'data' => $result], 200);
} else {
return response()->json(['status' => 'failed', 'message' => 'failed get data human resource, please try again later!', 'code' => 400], 400);
}
}
public function update(Request $request, $id)
{
if (empty($id)) {
return response()->json(['status' => 'failed', 'message' => 'id is required!', 'code' => 400], 400);
}
$humanresource = HumanResource::find($id);
$validator = Validator::make($request->all(), [
'role_id' => 'required|integer',
'divisi_id' => 'nullable|integer',
'username' => 'nullable|string|max:80',
'password' => 'nullable|string|max:100',
'session_login' => 'nullable|string',
'name' => 'required|string',
'phone_number' => 'required|string|max:15',
'email' => 'email|nullable',
'address' => 'nullable',
'fcm_token' => 'nullable',
'gender' => 'nullable|string|max:50',
'birth_place' => 'nullable|string|max:150',
'birth_date' => 'nullable|date',
'blood_type' => 'nullable|string|max:2',
'ktp_number' => 'required|max:20|string',
'employee_type' => 'required|string',
'status_resource' => 'nullable|string|max:50',
'status_boundary' => 'nullable|boolean',
'created_by' => 'nullable|string|max:80',
'updated_by' => 'nullable|string|max:80'
]);
if ($validator->fails()) {
return response()->json(['status' => 'failed', 'message' => 'Human Resource Pool failed created', 'code' => 400]);
}
$validated = $validator->validated();
if (isset($request->password) && $request->password != "") {
$validated['password'] = bcrypt($request->password);
}
// Get data in models
$getHumanResource = HumanResource::where('username', $validated['username'])->whereId($id)->exists();
if (isset($validated['username']) && $getHumanResource) {
return response()->json(['status' => 'failed', 'message' => 'Username already exists!', 'code' => 400], 400);
}
if ($humanresource) {
$result = $humanresource->update($validated);
if ($result) {
return response()->json(['status' => 'success', 'message' => 'Human Resource Pool successfully updated!', 'code' => 200], 200);
} else {
return response()->json(['status' => 'failed', 'message' => 'Human Resource Pool failed updated!', 'code' => 400], 400);
}
} else {
return response()->json(['status' => 'failed', 'message' => 'data Human Resource Pool not found!', 'code' => 400], 400);
}
}
public function delete($id)
{
$data = HumanResource::find($id);
if ($data) {
$delete = $data->delete();
if ($delete) {
return response()->json(['status' => 'success', 'message' => 'Human Resource Pool successfully deleted!', 'code' => 200], 200);
} else {
return response()->json(['status' => 'failed', 'message' => 'Human Resource Pool failed deleted!', 'code' => 400], 400);
}
} else {
return response()->json(['status' => 'failed', 'message' => 'data Human Resource Pool not found!', 'code' => 400], 400);
}
}
public function search(Request $request)
{
$payload = $request->all();
$dataBuilder = $this->setUpPayload($payload, 'm_users');
$builder = $dataBuilder['builder'];
$countBuilder = $dataBuilder['count'];
$dataGet = $builder->get();
$totalRecord = $countBuilder->count();
return response()->json(['status' => 'success', 'code' => 200, 'data' => $dataGet, 'totalRecord' => $totalRecord], 200);
}
public function list()
{
$data = HumanResource::all();
$countData = $data->count();
if ($data) {
return response()->json(['status' => 'success', 'code' => 200, 'data' => $data, 'totalRecord' => $countData], 200);
} else {
return response()->json(['status' => 'failed', 'message' => 'failed get list human resource, please try again later!', 'code' => 400], 400);
}
}
public function select(Request $request)
{
$search = $request->query('search');
$idActivity = (int)$request->query('idact');
$idProyek = (int)$request->query('idProyek');
$forbidden = [];
if ($idActivity && $idActivity > 0) {
$dataF = UserToActivity::select("user_id")->where("activity_id", $idActivity)->get();
foreach ($dataF as $val) {
$forbidden[] = $val->user_id;
}
}
if ($search && !empty($search)) {
$data = UserToProyek::select("m_users.id as id", "m_users.name as name", "assign_hr_to_proyek.project_role as proyek_role")->join('m_users', 'm_users.id', '=', 'assign_hr_to_proyek.user_id')
->where("assign_hr_to_proyek.proyek_id", $idProyek)->where("m_users.name", 'like', '%' . $search . '%')->whereNotIn("m_users.id", $forbidden)->get();
} else {
$data = UserToProyek::select("m_users.id as id", "m_users.name as name", "assign_hr_to_proyek.project_role as proyek_role")->where("assign_hr_to_proyek.proyek_id", $idProyek)->join('m_users', 'm_users.id', '=', 'assign_hr_to_proyek.user_id')->whereNotIn("m_users.id", $forbidden)->get();
}
return response()->json($data);
}
public function checkOldPassword(Request $request, $id)
{
if (empty($id)) {
return response()->json(['status' => 'failed', 'message' => 'id is required!', 'code' => 400], 400);
die();
}
$data = $request->all();
if (isset($request->old_password) && $request->old_password != "") {
$result = HumanResource::find($id);
$data['old_password'] = bcrypt($request->old_password);
if ($data['old_password'] !== $result->password) {
return response()->json(['status' => 'failed', 'message' => "old password seems doesn't match in records. please try again.", 'code' => 400], 400);
} elseif ($data['old_password'] === $result->password) {
return response()->json(['status' => 'success', 'message' => "old password match", 'data' => $result, 'code' => 200], 200);
}
} else {
return response()->json(['status' => 'failed', 'message' => 'old_password is required!', 'code' => 400], 400);
}
}
public function sync()
{
Artisan::call('sync:integration-human-resources');
}
}