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.

243 lines
9.4 KiB

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Project;
use App\Models\UserToProyek;
use App\Models\Activity;
use App\Models\UserToActivity;
use App\Models\AssignMaterial;
use App\Models\DokumenProject;
use App\Models\FolderDocumentProyek;
use App\Models\ProjectCharter;
use App\Models\ProjectApproval;
use App\Models\ProjectMileStone;
use App\Models\ProjectParticipants;
use App\Models\UserToVersionGantt;
use App\Models\RequestMaterial;
use App\Models\ShowHideColumn;
use App\Models\ToolsProject;
use App\Models\VersionGantt;
use App\Models\MaterialProject;
use App\Models\Image;
use App\Models\CommentActivity;
use App\Models\Link;
use App\Models\ActivityDokumen;
use App\Models\Holiday;
use App\Models\ReportActivity;
use App\Models\OfficeHours;
class ProjectController extends Controller
{
public function add(Request $request)
{
$this->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;
$result = Project::create($data);
if($result){
$this->createOfficeHours($result->id);
$dataResultInsert = Project::where('nama', $data['nama'])->where('mulai_proyek', $data['mulai_proyek'])->first();
return response()->json(['status'=>'success','message'=>'add data project successfully!', 'data_result' =>$dataResultInsert, 'code'=>200], 200);
}else{
return response()->json(['status'=>'failed','message'=>'add data project failed!','code'=>400], 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 = Project::find($id);
if($result){
return response()->json(['status'=>'success','code'=>200,'data'=>$result], 200);
}else{
return response()->json(['status'=>'failed','message'=>'failed get data project, 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 = Project::find($id);
if($data){
$result = $data->update($request->all());
}else{
return response()->json(['status'=>'failed','message'=>'data project not found!','code'=>400], 400);
die();
}
if($result){
return response()->json(['status'=>'success','message'=>'data project successfully updated!','code'=>200], 200);
}else{
return response()->json(['status'=>'failed','message'=>'data project failed updated!','code'=>400], 400);
}
}
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){
$this->deleteRelative($id);
$delete = $data->delete();
}else{
return response()->json(['status'=>'failed','message'=>'data project not found!','code'=>400], 400);
die();
}
if($delete){
return response()->json(['status'=>'success','message'=>'data project successfully deleted!','code'=>200], 200);
}else{
return response()->json(['status'=>'failed','message'=>'data project failed deleted!','code'=>400], 400);
}
}
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::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 project, please try again later!','code'=>400], 400);
}
}
public function getListProjectTask($id){
$data = UserToActivity::select("assign_hr_to_activity.user_id as user_id","m_proyek.*")->where("assign_hr_to_activity.user_id", $id)->join('m_proyek', 'assign_hr_to_activity.proyek_id', '=', 'm_proyek.id')
->groupBy("m_proyek.proyek_id")->get();
if($data){
return response()->json(['status'=>'success','code'=>200,'data'=>$data], 200);
}else{
return response()->json(['status'=>'failed','message'=>'failed get list project, please try again later!','code'=>400], 400);
}
}
}