validate($request, [ 'nama' => 'required', 'mulai_proyek' => 'required', 'akhir_proyek' => 'required', 'rencana_biaya' => 'required', 'type_proyek_id' => 'required' ]); $data = $request->all(); $data['created_by'] = $this->currentName; if(empty($data['phase_id'])) $data['phase_id'] = 1; $result = Project::create($data); if(!$result) return response()->json(['status'=>'failed','message'=>'Failed to add data!','code'=> 500], 500); $this->createOfficeHours($result->id); $dataResultInsert = Project::where('nama', $data['nama'])->where('mulai_proyek', $data['mulai_proyek'])->first(); return response()->json(['status'=>'success','message'=>'Data added!', 'data_result' =>$dataResultInsert, 'code'=> 200], 200); } public function edit($id){ if(empty($id) || !is_int((int)$id)) return response()->json(['status'=>'failed','message'=>'id is required!','code'=>400], 400); $result = Project::find($id); if(!$result) return response()->json(['status'=>'failed','message'=> 'Data not found!','code'=> 404], 404); return response()->json(['status'=>'success','code'=> 200,'data'=>$result], 200); } public function update(Request $request, $id) { if(empty($id) || !is_int((int)$id)) return response()->json(['status'=>'failed','message'=>'id is required!','code'=>400], 400); $data = Project::find($id); if(!$data) return response()->json(['status'=>'failed','message'=> 'Data not found!','code'=> 404], 404); $result = $data->update($request->all()); if(!$result) return response()->json(['status'=>'failed','message'=> 'Failed to update!','code'=> 500], 500); return response()->json(['status'=>'success','message'=>'Data updated!','code' => 200], 200); } private function createOfficeHours($proyek_id) { $dataCreate = array( "proyek_id"=>$proyek_id, "monday_start"=> "08:00:00", "monday_end"=> "16:00:00", "tuesday_start"=> "08:00:00", "tuesday_end"=> "16:00:00", "wednesday_start"=> "08:00:00", "wednesday_end"=> "16:00:00", "thursday_start"=> "08:00:00", "thursday_end"=> "16:00:00", "friday_start"=> "08:00:00", "friday_end"=> "16:00:00", "saturday_start"=> "08:00:00", "saturday_end"=> "16:00:00", "sunday_start"=> "08:00:00", "sunday_end"=> "16:00:00", "created_by"=> $this->currentName ); OfficeHours::create($dataCreate); return true; } public function delete($id) { $data = Project::find($id); if(!$data) return response()->json(['status'=>'failed','message'=>'Data not found!','code'=> 404], 404); $this->deleteRelative($id); if(!$data->delete()) return response()->json(['status'=>'failed','message'=>'Delete failed!','code'=> 500], 500); return response()->json(['status'=>'success','message'=>'Data deleted!','code'=>200], 200); } private function deleteRelative($project_id) { UserToProyek::where('proyek_id', $project_id)->delete(); UserToActivity::where('proyek_id', $project_id)->delete(); AssignMaterial::where('proyek_id', $project_id)->delete(); ProjectCharter::where('proyek_id', $project_id)->delete(); ProjectApproval::where('proyek_id', $project_id)->delete(); ProjectMileStone::where('proyek_id', $project_id)->delete(); ProjectParticipants::where('proyek_id', $project_id)->delete(); $this->deleteVersionGantt($project_id); $this->deleteDokumenProject($project_id); } private function deleteVersionGantt($project_id) { $dataVg = VersionGantt::where("proyek_id", $project_id)->pluck("id"); $vhIds = $dataVg->all(); $activity = Activity::whereIn("version_gantt_id", $vhIds)->pluck('id'); $activityIds = $activity->all(); $dataRa = ReportActivity::whereIn("activity_id", $activityIds)->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(); } $dataAd = ActivityDokumen::whereIn("activity_id", $activityIds)->get(); foreach ($dataAd as $ad) { if(file_exists($this->pathActivityDocument.$ad->file)){ unlink($this->pathActivityDocument.$ad->file); } } ActivityDokumen::whereIn("activity_id", $activityIds)->delete(); CommentActivity::whereIn("activity_id", $activityIds)->delete(); Holiday::where("proyek_id", $project_id)->delete(); VersionGantt::where("proyek_id", $project_id)->delete(); Link::whereIn("version_gantt_id", $vhIds)->delete(); ShowHideColumn::whereIn("version_gantt_id", $vhIds)->delete(); Activity::whereIn("version_gantt_id", $vhIds)->delete(); ReportActivity::whereIn("activity_id", $activityIds)->delete(); } private function deleteDokumenProject($project_id) { $dataDokumen = DokumenProject::where("type_dokumen", "project-document-out-folder")->where('ref_id', $project_id)->get(); foreach ($dataDokumen as $dokumen) { if(file_exists($this->pathDocument.$dokumen->file)){ unlink($this->pathDocument.$dokumen->file); } } DokumenProject::where("type_dokumen", "project-document-out-folder")->where('ref_id', $project_id)->delete(); $dataFolder = FolderDocumentProyek::where('proyek_id', $project_id)->pluck("id"); $ref_ids = $dataFolder->all(); $dataDokumen = DokumenProject::where("type_dokumen", "project-document-in-folder")->whereIn('ref_id', $ref_ids)->get(); foreach ($dataDokumen as $dokumen) { if(file_exists($this->pathDocument.$dokumen->file)){ unlink($this->pathDocument.$dokumen->file); } } DokumenProject::where("type_dokumen", "project-document-in-folder")->whereIn('ref_id', $ref_ids)->delete(); } public function search(Request $request) { $payload = $request->all(); $dataBuilder = $this->setUpPayload($payload, 'm_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 = Project::orderBy('id', 'desc')->get(); $countData = $data->count(); if(!$data) return response()->json(['status'=>'failed','message'=>'Data not found!','code'=> 404], 404); $scheduleWarningThreshold = 10; $scheduleDangerThreshold = 5; foreach($data as $d){ $progress = $costVariance = $actualCost = 0; $lastActivity = null; $scheduleHealth = "on-track"; $rootActivity = Activity::whereNull('parent_id')->where('proyek_id', $d->id)->orderBy('version_gantt_id', 'desc')->first(); if($rootActivity){ $costVariance = $d->rencana_biaya - $rootActivity->biaya_actual; $actualCost = $rootActivity->biaya_actual ?? 0; $progress = $rootActivity->persentase_progress ?? 0; $timeleft = strtotime($d->mulai_proyek) - strtotime($rootActivity->end_date); $date1 = new \DateTime(date("Y-m-d", strtotime($d->mulai_proyek))); $date2 = new \DateTime(date("Y-m-d", strtotime($rootActivity->end_date))); $daysRemaining = $date2->diff($date1); $daysRemaining = $daysRemaining->d; if($daysRemaining <= $scheduleDangerThreshold) { $scheduleHealth = "danger"; } elseif ($daysRemaining <= $scheduleWarningThreshold) { $scheduleHealth = "warning"; } $lastActivity = date("d/m/Y", strtotime($rootActivity->end_date)); } $d->plannedInterval = date("d/m/Y", strtotime($d->mulai_proyek)) . " - " . date("d/m/Y", strtotime($d->akhir_proyek)); $d->plannedCost = $d->rencana_biaya; $d->actualCost = $actualCost; $d->lastActivity = $lastActivity ?? "-"; $d->costVariance = $costVariance; $d->costHealth = $d->budget_health; $d->scheduleHealth = $scheduleHealth; $d->progress = $progress; $d->lastGanttId = VersionGantt::where("proyek_id", $d->id)->orderBy('id', 'desc')->first()->id ?? null; $d->manpower = UserToProyek::where("proyek_id", $d->id)->count() ?? 0; $d->projectManager = DB::table('m_proyek') ->join('m_users', 'm_users.id', '=', 'm_proyek.pm_id') ->where('m_proyek.id', $d->id) ->pluck('m_users.name') ->first(); if($d->area_kerja != ''){ $d->geolocation = $this->httpReq($d->area_kerja); $d->geolocation = []; } else { $d->geolocation = []; } } $totalPlannedCost = $data->sum('plannedCost'); $totalActualCost = $data->sum('actualCost'); $manpowers = User::count(); $projectsOnDanger = Project::where('budget_health', 'danger')->count(); $projectPhases = ProjectPhase::orderBy('order', 'asc')->pluck('name'); $projectTypes = ProjectType::orderBy('id', 'asc')->pluck('name'); try { $projectsByPhase = DB::table('m_proyek') ->select('m_proyek_phase.name', 'm_proyek_phase.color', DB::raw('count(*) as total')) ->join('m_proyek_phase', 'm_proyek_phase.id', '=', 'm_proyek.phase_id') ->groupBy('m_proyek_phase.name', 'm_proyek_phase.color') ->get(); } catch (\Exception $e) { return response()->json(['message' => $e->getMessage()]); } try { $projectsByType = DB::table('m_proyek') ->select('m_type_proyek.name', DB::raw('count(*) as total')) ->join('m_type_proyek', 'm_type_proyek.id', '=', 'm_proyek.type_proyek_id') ->groupBy('m_type_proyek.name') ->get(); } catch (\Exception $e) { return response()->json(['message' => $e->getMessage()]); } return response()->json( [ 'status'=>'success', 'code'=>200, 'data'=>$data, 'totalRecord'=>$countData, 'totalPlannedCost' => $totalPlannedCost, 'totalActualCost' => $totalActualCost, 'totalRevenue' => $totalPlannedCost - $totalActualCost, 'manpowers' => $manpowers, 'projectsOnDanger'=> $projectsOnDanger, 'projectPhases' => $projectPhases, 'projectsByPhase' => $projectsByPhase, 'projectTypes' => $projectTypes, 'projectsByType' => $projectsByType, ], 200); } private function httpReq($search){ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, str_replace("ADDR", $search, API_GEOLOCATION)); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_VERBOSE, true); curl_setopt($ch, CURLOPT_STDERR, fopen('php://stderr', 'w')); $output = curl_exec($ch); curl_close($ch); return json_decode($output); } }