validate($request, [ 'role_id' => 'required', 'name' => 'required', 'ktp_number' => 'required|string|unique:m_users,ktp_number', 'employee_type' => 'required', 'company_id' => 'required' ]); $data = $request->all(); if (isset($request->password) && $request->password != "") { $data['password'] = md5($request->password); } $transaction = ProductTransaction::query() ->where('company_id', $request->company_id); $cloneQueryTransaction = clone $transaction; $countCreate = false; $projectResult = HumanResource::query() ->selectRaw('count(*) as total_hr') ->where('company_id', $request->company_id) ->first(); if($transaction->where([['type_paket','Basic'],['amount','!=',null]])->exists()) { if($projectResult['total_hr'] < 10) { $countCreate = true; } } elseif ($cloneQueryTransaction->where([['type_paket','Free'],['amount',0]])->exists()) { if($projectResult['total_hr'] < 1) { $countCreate = true; } } else { $countCreate = true; } if($countCreate) { $result = HumanResource::create($data); } else { return response()->json(['status' => 'failed', 'message' => 'Limited to create human resource!', 'code' => 500], 500); } 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', 'company_id' => 'required', 'username' => 'required', 'email' => 'required' ]); $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) { if (!$id || (int) $id < 0 || $id == "") { return response()->json(['status' => 'failed', 'message' => 'id is required!', 'code' => 400], 400); } $humanresource = HumanResource::find($id); $request->ktp_number !== $humanresource['ktp_number'] ? $this->validate($request,['ktp_number'=>'unique:m_users,ktp_number|string']) : ''; $request->email !== $humanresource['email'] ? $this->validate($request,['email'=>'unique:m_users,email|email']) : ''; $request->password ? $this->validate($request,['password'=>'required|min:8']) : ''; $data = $request->all(); if (isset($request->password) && $request->password != "") { $data['password'] = md5($request->password); } if (isset($data['username']) && HumanResource::where('username', $data['username'])->where('id', '<>', $id)->exists()) { return response()->json(['status' => 'failed', 'message' => 'Username already exists!', 'code' => 400], 400); } if ($humanresource) { $result = $humanresource->update($data); } else { return response()->json(['status' => 'failed', 'message' => 'data Human Resource Pool not found!', 'code' => 400], 400); die(); } 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); } } 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); die(); } 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','company_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)); } } }