validate($request, [ 'user_id' => 'required', 'proyek_id' => 'required' ]); $data = $request->all(); $data['created_by'] = $this->currentName; $result = UserToProyek::create($data); if($result){ return response()->json(['status'=>'success','message'=>'assign hr to proyek successfully!','code'=>200], 200); }else{ return response()->json(['status'=>'failed','message'=>'assign hr to proyek failed!','code'=>400], 400); } } public function addMultiple(Request $request){ $user_IDs = $request->user_id; $data = UserToProyek::where("proyek_id", $request->proyek_id); if($data){ $data->delete(); } if(is_array($user_IDs) && count($user_IDs) > 0){ $countRes = 0; foreach($user_IDs as $item){ $dataInsert = array( "user_id" => $item, "proyek_id" => $request->proyek_id, "created_by" => $this->currentName, 'is_customer'=>true ); $result = UserToProyek::create($dataInsert); if($result){ $countRes++; }else{ $countRes--; } } if($countRes > 0){ return response()->json(['status'=>'success' ,'message'=>'Project customer successfull created','code'=>200]); }else{ return response()->json(['status'=>'success' ,'message'=>'Project customer failed created','code'=>400]); } }else{ return response()->json(['status'=>'success' ,'message'=>'Project customer successfull created','code'=>200]); } } public function edit($id){ if(!$id || (int) $id < 0 || $id==""){ return response()->json(['status'=>'failed','message'=>'id is required!','code'=>400], 400); die(); } $result = UserToProyek::find($id); if($result){ return response()->json(['status'=>'success','code'=>200,'data'=>$result], 200); }else{ return response()->json(['status'=>'failed','message'=>'failed get data assign hr to proyek, 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); } $data = UserToProyek::find($id); if($data){ $result = $data->update($request->all()); }else{ return response()->json(['status'=>'failed','message'=>'data assign hr to proyek not found!','code'=>400], 400); die(); } if($result){ return response()->json(['status'=>'success','message'=>'data assign hr to proyek successfully updated!','code'=>200], 200); }else{ return response()->json(['status'=>'failed','message'=>'data assign hr to proyek failed updated!','code'=>400], 400); } } public function delete($id) { $data = UserToProyek::find($id); if($data){ $userId = $data->user_id; $proyekId = $data->proyek_id; $delete = $data->delete(); if($delete){ $this->deleteRelative($proyekId, $userId); } }else{ return response()->json(['status'=>'failed','message'=>'data assign hr to proyek not found!','code'=>400], 400); die(); } if($delete){ return response()->json(['status'=>'success','message'=>'data assign hr to proyek successfully deleted!','code'=>200], 200); }else{ return response()->json(['status'=>'failed','message'=>'data assign hr to proyek failed deleted!','code'=>400], 400); } } private function deleteRelative($proyekId, $userId) { $uta = UserToActivity::where("proyek_id", $proyekId)->where("user_id", $userId)->get(); foreach ($uta as $value) { $dataRa = ReportActivity::where("activity_id", $value->activity_id)->where("user_id", $value->user_id)->get(); foreach ($dataRa as $ra) { $images = Image::where("ref_id", $ra->id)->where("category", "report_activity")->get(); foreach ($images as $image) { if(file_exists($this->pathImage.$image->image)){ unlink($this->pathImage.$image->image); } } Image::where("ref_id", $ra->id)->where("category", "report_activity")->delete(); } ReportActivity::where("activity_id", $value->activity_id)->where("user_id", $value->user_id)->delete(); } UserToActivity::where("proyek_id", $proyekId)->where("user_id", $userId)->delete(); } public function search(Request $request) { $payload = $request->all(); $dataBuilder = $this->setUpPayload($payload, 'assign_hr_to_proyek'); $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 = UserToProyek::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 assign hr to proyek, please try again later!','code'=>400], 400); } } public function select(Request $request){ $search = $request->query('search'); $idProyek = (int)$request->query('idProyek'); $idActivity = (int)$request->query('idact'); $include = []; if($idActivity && $idActivity > 0){ $dataF = UserToActivity::select("user_id")->where("activity_id", $idActivity)->get(); foreach($dataF as $val){ $include[] = $val->user_id; } } if($search && !empty($search)){ $data = UserToProyek::select("m_users.id as id", "m_users.name as name")->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.'%')->whereIn("m_users.id", $include)->get(); }else{ $data = UserToProyek::select("m_users.id as id", "m_users.name as name")->where("assign_hr_to_proyek.proyek_id", $idProyek)->join('m_users', 'm_users.id', '=', 'assign_hr_to_proyek.user_id')->whereIn("m_users.id", $include)->get(); } if($data instanceof \Illuminate\Database\Eloquent\Collection){ $pm = array( "id"=>$this->currentId, "name"=> "Project Manager" ); $data->push($pm); } return response()->json($data); } private function curlReq($url, $token){ $ch = curl_init(); $headers = [ 'Authorization: '.$token ]; curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $response = curl_exec($ch); if ($response === false) $response = curl_error($ch); curl_close($ch); return json_decode($response); } public function getEmployeeIntegration(Request $request) { $search = urlencode($request->name); if(empty($search)) return response()->json(['status'=>'error', 'message'=>'Empty query string!'], 400); $url = str_replace("SEARCH", $search, config('api.adw').'/employees?emp_name=SEARCH'); $token = config('api.adw_token'); $firstResponse = $this->curlReq($url, $token); if($firstResponse->total == 0) return response()->json(['status'=>'error', 'message' => 'Data not found!', 'code'=>404], 404); $data = $firstResponse->data; return response()->json([$data]); $currentPage = 1; if($firstResponse->last_page > 0) { do { $currentPage++; $response = $this->curlReq($url.'&page='.$currentPage, $token); foreach($response->data as $d){ array_push($data, $d); } } while ($currentPage < $firstResponse->last_page); } return response()->json(['status'=>'success', 'data'=> $data, 'total' => count($data), 'code'=>200], 200); } }