Custom Backend OSPRO Surveyor Indonesia
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.

296 lines
11 KiB

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\{HumanResource, ProductTransaction, UserToActivity, UserToProyek, User};
use Illuminate\Support\Facades\Artisan;
const URL_EMAIL = 'https://notifapp.odm-iu.com/service-mail/notif_mail.php';
class HumanResourceController extends Controller
{
public function add(Request $request)
{
$messages = [
'ktp_number.unique' => 'Nomor KTP/ID Card tidak boleh sama',
'username.unique' => 'Username tidak boleh sama',
'required' => 'Data tidak boleh kosong'
];
$this->validate($request, [
'name' => 'required|string',
'ktp_number' => 'required|string|unique:m_users,ktp_number',
'employee_type' => 'required|in:employee,subcon,freelance',
'username' => 'string|unique:m_users,username',
'email' => 'string|unique:m_users,email'
], $messages);
$data = $request->all();
!$request->is_customer ? $this->validate($request, ['role_id' => 'required', 'divisi_id' => 'required'], $messages) : '';
if (isset($request->password) && $request->password != "") {
$data['password'] = md5($request->password);
}
$result = HumanResource::create($data);
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 add_user(Request $request)
{
$this->validate($request, [
'role_id' => 'required',
'name' => 'required',
'ktp_number' => 'required|string|unique:m_users,ktp_number',
'employee_type' => 'required',
'username' => 'required',
'email' => 'required|unique:m_users,email|email'
]);
$data = $request->all();
if (isset($request->password) && $request->password != "") {
$data['password'] = md5($request->password);
}
$result = HumanResource::create($data);
if ($result) {
$this->sendEmail($request->email, $request->password);
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 (!$id || (int) $id < 0 || $id == "") {
return response()->json(['status' => 'failed', 'message' => 'id is required!', 'code' => 400], 400);
die();
}
$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)
{
$messages = [
'ktp_number.unique' => 'Nomor KTP/ID Card tidak boleh sama',
'username.unique' => 'Username tidak boleh sama',
'required' => 'Data tidak boleh kosong'
];
$this->validate($request, [
'name' => 'required|string',
'ktp_number' => 'required|string|unique:m_users,ktp_number,'.$id . ',id',
'employee_type' => 'required|in:employee,subcon,freelance',
'username' => 'unique:m_users,username,'.$id . ',id',
'email' => 'string|unique:m_users,email,'.$id . ',id'
], $messages);
!$request->is_customer ? $this->validate($request,['role_id'=>'required', 'divisi_id'=>'required'], $messages) : '';
$request->password ? $this->validate($request,['password'=>'required|min:8']) : '';
$data = $request->all();
if (!$id || (int) $id < 0 || $id == "") {
return response()->json(['status' => 'failed', 'message' => 'Id is required!', 'code' => 400], 400);
}
$humanresource = HumanResource::find($id);
if (empty($humanresource)) {
return response()->json(['status' => 'failed', 'message' => 'Data Human Resource not found!', 'code' => 400], 400);
}
if (isset($request->password) && $request->password != "") {
$data['password'] = md5($request->password);
}
// $result = $humanresource->update($data);
$result = true;
if ($result) {
return response()->json(['status' => 'success', 'data' => $result, 'message' => 'Human Resource Pool successfully updated!', 'code' => 200], 200);
} else {
return response()->json(['status' => 'failed', 'message' => 'Human Resource Pool failed updated!', 'code' => 400], 400);
}
}
public function delete($id)
{
$data = HumanResource::find($id);
if ($data) {
$delete = $data->delete();
} else {
return response()->json(['status' => 'failed', 'message' => 'data Human Resource Pool not found!', 'code' => 400], 400);
}
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);
}
}
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::select('id', 'name', 'role_id')->get();
$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(function ($query) {
$query->where("assign_hr_to_proyek.is_customer", "!=", true)
->orWhereNull("assign_hr_to_proyek.is_customer");
})
->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)
->where(function ($query) {
$query->where("assign_hr_to_proyek.is_customer", "!=", true)
->orWhereNull("assign_hr_to_proyek.is_customer");
})
->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 (!$id || (int) $id < 0 || $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'] = md5($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');
}
public function sendEmail($email, $password)
{
$user = User::select('email', 'name', 'username')->where('email', $email)->first();
if (!$user) {
return response()->json(['status' => 'error', 'message' => 'e-mail not found '], 400);
} else {
$this->reqHttpCurl($email, $password, $user->username, $user->name);
return response()->json(['status' => 'error', 'code' => 400, 'message' => 'e-mail not found '], 400);
}
}
private function reqHttpCurl($email, $password, $username, $name)
{
$postData = [
"to" => $email,
"name" => $name,
"username" => $username,
"password" => $password,
"from" => "app.integrasia@integrasiautama.com",
"alias_from" => "OSPRO",
"subject" => "Registration OSPRO",
"body" => "registration-ospro"
];
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => URL_EMAIL,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30000,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode($postData),
CURLOPT_HTTPHEADER => array(
// Set here requred headers
"accept: */*",
"accept-language: en-US,en;q=0.8",
"content-type: application/json",
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
print_r(json_decode($response));
}
}
}