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.

150 lines
5.8 KiB

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\ReportK3;
use App\Models\ReportK3Detail;
use App\Models\ProjectToChecklistK3;
use App\Models\ChecklistK3;
use App\Models\Image;
use Illuminate\Support\Facades\DB;
class ReportK3Controller extends Controller
{
public function add(Request $request)
{
$this->validate($request, [
'user_id' => 'required',
'report_date' => 'required',
'proyek_id' => 'required'
]);
$data = $request->all();
$data['created_by'] = $this->currentName;
$result = ReportK3::create($data);
if($result){
if($request->has("detail")){
$this->addDetailK3($request->detail, $result->id);
}
return response()->json(['status'=>'success', 'report_id'=>$result->id,'message'=>'add report k3 successfully!','code'=>200], 200);
}else{
return response()->json(['status'=>'failed','message'=>'add data report k3 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 = ReportK3::find($id);
if($result){
return response()->json(['status'=>'success','code'=>200,'data'=>$result], 200);
}else{
return response()->json(['status'=>'failed','message'=>'failed get data report k3, 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 = ReportK3::find($id);
if($data){
$result = $data->update($request->all());
}else{
return response()->json(['status'=>'failed','message'=>'data report k3 not found!','code'=>400], 400);
die();
}
if($result){
return response()->json(['status'=>'success','message'=>'data report k3 successfully updated!','code'=>200], 200);
}else{
return response()->json(['status'=>'failed','message'=>'data report k3 failed updated!','code'=>400], 400);
}
}
public function delete($id)
{
$data = ReportK3::find($id);
if($data){
$delete = $data->delete();
}else{
return response()->json(['status'=>'failed','message'=>'data report k3 not found!','code'=>400], 400);
die();
}
if($delete){
return response()->json(['status'=>'success','message'=>'data report k3 successfully deleted!','code'=>200], 200);
}else{
return response()->json(['status'=>'failed','message'=>'data report k3 failed deleted!','code'=>400], 400);
}
}
public function search(Request $request)
{
$payload = $request->all();
$dataBuilder = $this->setUpPayload($payload, 't_report_k3');
$builder = $dataBuilder['builder'];
$countBuilder = $dataBuilder['count'];
$dataGet = $builder->get();
if($request->has("child_data")){
$newDataGet = [];
$dataChild = $request->child_data;
if(isset($dataChild['table_name']) && isset($dataChild['column_name']) && isset($dataChild['column_table'])){
$columnForeign = $dataChild['column_name'];
$columnSelf = $dataChild['column_table'];
foreach ($dataGet as $value) {
$resultData = DB::table($dataChild['table_name'])->where($columnSelf,$value->{$columnForeign})->get();
$value->childData = $resultData;
$value->k3_checked = $resultData->pluck("name_checklist_k3")->all();
// $dataImage = Image::where("category", "report_k3")->where("ref_id", $value->id)->first();
// if($dataImage){
// $value->image = $dataImage->image;
// }else{
// $value->image = null;
// }
$dataImage = Image::where("category", "report_k3")->where("ref_id", $value->id)->get();
$images = [];
if (count($dataImage) > 0) {
foreach($dataImage as $reportImage) {
array_push($images, $reportImage);
}
}
$value->image = $images;
$idsChecklist = $resultData->pluck("checklist_k3_id")->all();
$CheklistK3Ids = ProjectToChecklistK3::whereNotIn("checklist_k3_id", $idsChecklist)->where("proyek_id", $value->proyek_id)->pluck("checklist_k3_id");
$value->k3_not_checked = ChecklistK3::whereIn("id", $CheklistK3Ids)->pluck("name")->all();
$newDataGet[] = $value;
}
$dataGet = $newDataGet;
}
}
$totalRecord = $countBuilder->count();
return response()->json(['status'=>'success','code'=>200,'data'=>$dataGet, 'totalRecord'=>$totalRecord], 200);
}
public function list()
{
$data = ReportK3::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 report k3, please try again later!','code'=>400], 400);
}
}
}