From 22748a5b5a46dca4a3ec762fd7c4611deb450b72 Mon Sep 17 00:00:00 2001 From: wahyu Date: Mon, 25 Sep 2023 16:32:27 +0700 Subject: [PATCH 01/14] fix search task mobile --- app/Http/Controllers/PresenceController.php | 1238 ++++++++--------- .../Controllers/UserToActivityController.php | 419 +++--- 2 files changed, 839 insertions(+), 818 deletions(-) diff --git a/app/Http/Controllers/PresenceController.php b/app/Http/Controllers/PresenceController.php index 6aef1a8..0d79917 100644 --- a/app/Http/Controllers/PresenceController.php +++ b/app/Http/Controllers/PresenceController.php @@ -1,619 +1,619 @@ -validate($request, [ - 'user_id' => 'required' - ]); - - $checkLocation = $this->checkLocation($request); - $statusBoundary = false; - $date = date_create($request->clock_time); - // assign and in boundary - if(count($checkLocation) > 0 && $checkLocation[0]['boundary']){ - $statusBoundary = true; - } - $statusRestriction = HumanResource::select('status_boundary')->where('id', $request->user_id)->first(); - if ($statusRestriction->status_boundary) { - $statusBoundary = true; - } - // not assign - if(!$checkLocation[0]['status_assign'] && $checkLocation[0]['boundary'] == false){ - $data=array( - 'id' => null, - 'boundary' => $statusBoundary - ); - return response()->json(['status'=>'failed', 'data'=>$data, 'message'=>'Tidak dapat melakukan presensi. Anda belum di assign ke area kerja.','code'=>200], 200); - } - // assign and not in boundary - if($checkLocation[0]['status_assign'] && $checkLocation[0]['boundary'] == false){ - $data=array( - 'id' => null, - 'boundary' => true - ); - return response()->json(['status'=>'failed', 'data'=>$data, 'message'=>'Tidak dapat melakukan presensi. Anda berada di luar area kerja.','code'=>200], 200); - } - - if($request->type=="out"){ - $clock_out_loc = $this->getLoc($request->clock_out_lat, $request->clock_out_lng)->display_name; - $dataUpdate = array( - "clock_out"=>$request->clock_time, - "clock_out_lat" => $request->clock_out_lat, - "clock_out_lng" => $request->clock_out_lng, - "updated_by"=>$this->currentName, - "clock_out_loc" => $clock_out_loc, - "clock_out_boundary" => $statusBoundary - ); - - $resultUpdate = $this->updateFormAdd($dataUpdate, $request->user_id); - if($resultUpdate && $resultUpdate > 0){ - if($statusBoundary){ - for ($i=0; $i < count($checkLocation); $i++) { - # code... - DB::table('clock_in_out_boundary')->insert([ - "clock_in_out_id" => $resultUpdate, - "user_id" => $request->user_id, - "activity_id" => $checkLocation[$i]['activity_id'], - "type" => $request->type, - "created_at" => $date, - "created_by" => $this->currentName - ]); - }; - }; - $data=array( - 'id' => $resultUpdate, - 'boundary' => $statusBoundary - ); - - return response()->json(['status'=>'success', 'data'=>$data,'message'=>'clock out success!','code'=>200], 200); - } - else{ - return response()->json(['status'=>'failed','message'=>'clock out failed please try again!','code'=>400], 400); - } - die(); - } - - $onlyDate = date_format($date,"Y-m-d"); - $clock_in_loc = $this->getLoc($request->clock_in_lat, $request->clock_in_lng)->display_name; - - $dataAdd = array( - 'user_id'=> $request->user_id, - 'clock_in'=> $request->clock_time, - 'date_presence'=> $onlyDate, - 'created_by' => $this->currentName, - 'clock_in_lat' => $request->clock_in_lat, - 'clock_in_lng' => $request->clock_in_lng, - 'clock_in_loc' => $clock_in_loc, - 'clock_in_boundary' => $statusBoundary - ); - - $result = Presence::create($dataAdd); - $data=array( - 'id' => $result->id, - 'boundary' => $statusBoundary - ); - if($result){ - if($statusBoundary){ - for ($i=0; $i < count($checkLocation); $i++) { - # code... - DB::table('clock_in_out_boundary')->insert([ - "clock_in_out_id" => $result->id, - "user_id" => $request->user_id, - "activity_id" => $checkLocation[$i]['activity_id'], - "type" => $request->type, - "created_at" => $date, - "created_by" => $this->currentName - ]); - }; - }; - return response()->json(['status'=>'success', 'data' => $data,'message'=>'clock in successfully!','code'=>200], 200); - }else{ - return response()->json(['status'=>'failed','message'=>'clock in failed!','code'=>400], 400); - } - } - - public function reportK3(Request $request){ - // return response()->json(['status'=>'success', 'message'=>$request->report_k3['detail'],'code'=>200], 200); - - $this->validate($request, [ - 'user_id' => 'required' - ]); - - $checkLocation = $this->checkLocation($request); - $statusBoundary = false; - $date = date_create($request->time); - // assign and in boundary - if(count($checkLocation) > 0 && $checkLocation[0]['boundary']){ - $statusBoundary = true; - } - $statusRestriction = HumanResource::select('status_boundary')->where('id', $request->user_id)->first(); - if ($statusRestriction->status_boundary) { - $statusBoundary = true; - } - // not assign - if(!$checkLocation[0]['status_assign'] && $checkLocation[0]['boundary'] == false){ - $data=array( - 'id' => null, - 'boundary' => $statusBoundary - ); - return response()->json(['status'=>'failed', 'data'=>$data, 'message'=>'Tidak dapat melakukan presensi. Anda belum di assign ke area kerja.','code'=>200], 200); - } - // assign and not in boundary - if($checkLocation[0]['status_assign'] && $checkLocation[0]['boundary'] == false){ - $data=array( - 'id' => null, - 'boundary' => true - ); - return response()->json(['status'=>'failed', 'data'=>$data, 'message'=>'Tidak dapat melakukan presensi. Anda berada di luar area kerja.','code'=>200], 200); - } - - if($request->clock_in_out['type']=="out"){ - $clock_out_loc = $this->getLoc($request->clock_in_out['clock_out_lat'], $request->clock_in_out['clock_out_lng'])->display_name; - $dataUpdate = array( - "clock_out"=>$request->time, - "clock_out_lat" => $request->clock_in_out['clock_out_lat'], - "clock_out_lng" => $request->clock_in_out['clock_out_lng'], - "updated_by"=>$this->currentName, - "clock_out_loc" => $clock_out_loc, - "clock_out_boundary" => $statusBoundary - ); - - $resultUpdate = $this->updateFormAdd($dataUpdate, $request->user_id); - if($resultUpdate && $resultUpdate > 0){ - if($statusBoundary){ - for ($i=0; $i < count($checkLocation); $i++) { - # code... - DB::table('clock_in_out_boundary')->insert([ - "clock_in_out_id" => $resultUpdate, - "user_id" => $request->user_id, - "activity_id" => $checkLocation[$i]['activity_id'], - "type" => $request->clock_in_out['type'], - "created_at" => $date, - "created_by" => $this->currentName - ]); - }; - }; - $data=array( - 'presence_id' => $resultUpdate, - 'boundary' => $statusBoundary - ); - - return response()->json(['status'=>'success', 'data'=>$data,'message'=>'clock out success!','code'=>200], 200); - } - else{ - return response()->json(['status'=>'failed','message'=>'clock out failed please try again!','code'=>400], 400); - } - die(); - } - - $onlyDate = date_format($date,"Y-m-d"); - $clock_in_loc = $this->getLoc($request->clock_in_out['clock_in_lat'], $request->clock_in_out['clock_in_lng'])->display_name; - - $dataFormK3 = array( - "user_id" => $request->user_id, - "proyek_id" => $request->report_k3['proyek_id'], - "report_date" => $request->time, - "description" => $request->report_k3['description'] - ); - - $dataFormPresence = array( - 'user_id' => $request->user_id, - 'clock_in' => $request->time, - 'date_presence' => $onlyDate, - 'created_by' => $this->currentName, - 'clock_in_lat' => $request->clock_in_out['clock_in_lat'], - 'clock_in_lng' => $request->clock_in_out['clock_in_lng'], - 'clock_in_loc' => $clock_in_loc, - 'clock_in_boundary' => $statusBoundary - ); - - $result = Presence::create($dataFormPresence); - $data=array( - 'presence_id' => $result->id, - 'boundary' => $statusBoundary - ); - if($result){ - if($statusBoundary){ - $insertk3 = $this->insertK3($dataFormK3, $request->report_k3['detail']) ; - for ($i=0; $i < count($checkLocation); $i++) { - # code... - DB::table('clock_in_out_boundary')->insert([ - "clock_in_out_id" => $result->id, - "user_id" => $request->user_id, - "activity_id" => $checkLocation[$i]['activity_id'], - "type" => $request->type, - "created_at" => $date, - "created_by" => $this->currentName - ]); - }; - $data['report_id'] = $insertk3->id; - }; - return response()->json(['status'=>'success', 'data'=> $data, 'message'=>'clock in successfully!','code'=>200], 200); - }else{ - return response()->json(['status'=>'failed','message'=>'clock in failed!','code'=>400], 400); - } - } - - private function insertK3($params, $details){ - $insert = ReportK3::create($params); - if($insert && $details){ - $this->addDetailK3($details, $insert->id); - } - return $insert; - } - - private function checkLocation($params){ - // cek user tersebut apakah punya assign task yang ada bondary nya - // geom ada di table activity - // $clock_time = - $user = HumanResource::find($params->user_id); - $geom = DB::table("assign_hr_to_activity as ahta")->select("ma.geom", "ma.id") - ->join("m_activity as ma", "ma.id", "=", "ahta.activity_id") - ->where("ahta.user_id", $params->user_id) - ->whereNotNull("ma.geom") - ->whereDate("ma.start_date", "<=", $params->time) - ->whereDate("ma.end_date", ">=", $params->time) - ->get(); - $temp = []; - if (count($geom) > 0) { - foreach($geom as $dataGeom){ - $valGeom = json_decode($dataGeom->geom); - if($params->clock_in_out['type']=="out"){ - if($valGeom->type == "FeatureCollection"){ - $multiArea = $valGeom->features; - foreach($multiArea as $area){ - if ($area->geometry->type === "Point") { - $pointCoordinates = $area->geometry->coordinates; - $pointLng = $pointCoordinates[0]; - $pointLat = $pointCoordinates[1]; - - $check = DB::select(DB::raw("SELECT ST_Distance( - ST_GeomFromGeoJSON('" . json_encode($area->geometry) . "'), - ST_GeomFromText('POINT(" . $params->clock_in_out['clock_in_lng'] . " " . $params->clock_in_out['clock_in_lat'] . ")', 4326) - ) <= " . $area->properties->radius . " as within_radius")); - - if ($check[0]->within_radius) { - break; - } - } - $check = DB::select(DB::raw("SELECT ST_Intersects(ST_GeomFromGeoJSON('".json_encode($area->geometry)."'), - ST_GeomFromText('POINT(".$params->clock_in_out['clock_out_lng']." ".$params->clock_in_out['clock_out_lat'].")', 4326)) as boundary")); - if($check[0]->boundary){ - break; - } - } - }else{ - $check = DB::select(DB::raw("SELECT ST_Intersects(ST_GeomFromGeoJSON('".json_encode($valGeom->geometry)."'), - ST_GeomFromText('POINT(".$params->clock_in_out['clock_out_lng']." ".$params->clock_in_out['clock_out_lat'].")', 4326)) as boundary")); - } - }else{ - if($valGeom->type == "FeatureCollection"){ - $multiArea = $valGeom->features; - foreach($multiArea as $area){ - if ($area->geometry->type === "Point") { - $pointCoordinates = $area->geometry->coordinates; - $pointLng = $pointCoordinates[0]; - $pointLat = $pointCoordinates[1]; - - $check = DB::select(DB::raw("SELECT ST_Distance( - ST_GeomFromGeoJSON('" . json_encode($area->geometry) . "'), - ST_GeomFromText('POINT(" . $params->clock_in_out['clock_in_lng'] . " " . $params->clock_in_out['clock_in_lat'] . ")', 4326) - ) <= " . $area->properties->radius . " as within_radius")); - - if ($check[0]->within_radius) { - break; - } - } - $check = DB::select(DB::raw("SELECT ST_Intersects(ST_GeomFromGeoJSON('".json_encode($area->geometry)."'), - ST_GeomFromText('POINT(".$params->clock_in_out['clock_in_lng']." ".$params->clock_in_out['clock_in_lat'].")', 4326)) as boundary")); - if($check[0]->boundary){ - break; - } - } - }else{ - $check = DB::select(DB::raw("SELECT ST_Intersects(ST_GeomFromGeoJSON('".json_encode($valGeom->geometry)."'), - ST_GeomFromText('POINT(".$params->clock_in_out['clock_in_lng']." ".$params->clock_in_out['clock_in_lat'].")', 4326)) as boundary")); - } - } - if(!$user->status_boundary || count($check)>0){ - if(!$user->status_boundary || (isset($check[0]->boundary) && $check[0]->boundary)){ - $temp[]=array( - "activity_id" => $dataGeom->id, - "boundary" => $check[0]->boundary, - "status_assign" => true - ); - } else if (!$user->status_boundary || (isset($check[0]->within_radius) && $check[0]->within_radius)) { - $temp[]=array( - "activity_id" => $dataGeom->id, - "boundary" => $check[0]->within_radius, - "status_assign" => true - ); - } - } - } - // assign and not in boundary - if(count($temp) < 1){ - $temp[]=array( - "activity_id" => null, - "boundary" => false, - "status_assign" => true - // "geom" => $geom, - // "cek" => $check[0]->boundary - ); - } - } - else{ - // not assign - $temp[]=array( - "activity_id" => null, - "boundary" => false, - "status_assign" => false - ); - } - return $temp; - } - - public function checkLocationTest(Request $request){ - // cek user tersebut apakah punya assign task yang ada bondary nya - // geom ada di table activity - // $clock_time = - $params = $request; - $geom = DB::table("assign_hr_to_activity as ahta")->select("ma.geom", "ma.id") - ->join("m_activity as ma", "ma.id", "=", "ahta.activity_id") - ->where("ahta.user_id", $params->user_id) - ->whereNotNull("ma.geom") - ->whereDate("ma.start_date", "<=", $params->time) - ->whereDate("ma.end_date", ">=", $params->time) - ->get(); - $temp = []; - // return json_encode($geom); - if (count($geom) > 0) { - foreach($geom as $dataGeom){ - $valGeom = json_decode($dataGeom->geom); - if($params->clock_in_out['type']=="out"){ - if($valGeom->type == "FeatureCollection"){ - // return count($valGeom->features); - $multiArea = $valGeom->features; - foreach($multiArea as $area){ - $check = DB::select(DB::raw("SELECT ST_Intersects(ST_GeomFromGeoJSON('".json_encode($area->geometry)."'), - ST_GeomFromText('POINT(".$params->clock_in_out['clock_out_lng']." ".$params->clock_in_out['clock_out_lat'].")', 4326)) as boundary")); - if($check[0]->boundary){ - break; - } - } - }else{ - $check = DB::select(DB::raw("SELECT ST_Intersects(ST_GeomFromGeoJSON('".json_encode($valGeom->geometry)."'), - ST_GeomFromText('POINT(".$params->clock_in_out['clock_out_lng']." ".$params->clock_in_out['clock_out_lat'].")', 4326)) as boundary")); - } - }else{ - if($valGeom->type == "FeatureCollection"){ - // return count($valGeom->features); - $multiArea = $valGeom->features; - foreach($multiArea as $area){ - $check = DB::select(DB::raw("SELECT ST_Intersects(ST_GeomFromGeoJSON('".json_encode($area->geometry)."'), - ST_GeomFromText('POINT(".$params->clock_in_out['clock_in_lng']." ".$params->clock_in_out['clock_in_lat'].")', 4326)) as boundary")); - if($check[0]->boundary){ - break; - } - } - }else{ - $check = DB::select(DB::raw("SELECT ST_Intersects(ST_GeomFromGeoJSON('".json_encode($valGeom->geometry)."'), - ST_GeomFromText('POINT(".$params->clock_in_out['clock_in_lng']." ".$params->clock_in_out['clock_in_lat'].")', 4326)) as boundary")); - } - } - if(count($check)>0){ - if($check[0]->boundary){ - $temp[]=array( - "activity_id" => $dataGeom->id, - "boundary" => $check[0]->boundary, - "status_assign" => true - ); - } - } - } - // assign and not in boundary - if(count($temp) < 1){ - $temp[]=array( - "activity_id" => null, - "boundary" => false, - "status_assign" => true - // "geom" => $geom, - // "cek" => $check[0]->boundary - ); - } - } - else{ - // not assign - $temp[]=array( - "activity_id" => null, - "boundary" => false, - "status_assign" => false - ); - } - return $temp; - } - - public function edit($id){ - if(!$id || (int) $id < 0 || $id==""){ - return response()->json(['status'=>'failed','message'=>'id is required!','code'=>400], 400); - die(); - } - - $result = Presence::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 clockinout($id) { - $dateTimeNow = Carbon::now()->addHour(7); - $dataPresence = Presence::where('user_id', $id)->orderBy('id', 'DESC')->first(); - - if($dataPresence){ - - $dateNow = date("Y-m-d"); - $dateA = strtotime($dataPresence->clock_in); - $dayClockin = date("Y-m-d", $dateA); - if($dayClockin == $dateNow){ - $clock_in = $dataPresence->clock_in; - $clock_out = $dataPresence->clock_out; - }else{ - $clock_in = null; - $clock_out = null; - } - $res_data = array( - "id" => $dataPresence->id, - "at" => $dateTimeNow, - "user_id"=> $id, - "clock_in"=> $clock_in, - "clock_out"=> $clock_out, - "last_clock_in"=> $dataPresence->clock_in, - "last_clock_out"=> $dataPresence->clock_out, - "in_working_time"=> true - ); - return response()->json(['status'=>'success','data'=>$res_data,'code'=>200], 200); - } - $res_data = array( - "id" => null, - "at" => $dateTimeNow, - "user_id"=> $id, - "clock_in"=> null, - "clock_out"=> null, - "last_clock_in"=> null, - "last_clock_out"=> null, - "in_working_time"=> true - ); - return response()->json(['status'=>'success','data'=>$res_data,'code'=>200], 200); - } - - private function updateFormAdd($data, $id){ - $date = date_create($data['clock_out']); - $onlyDate = date_format($date,"Y-m-d"); - - $dataPresence = Presence::where('user_id',$id) - ->where("clock_in", "<=", $data["clock_out"]) - ->orderByDesc("id") - ->first(); - - if($dataPresence){ - $queryUpdate = $dataPresence->update($data); - if($queryUpdate){ - $getDataUpdate = Presence::where('user_id', $id)->where("date_presence", $onlyDate)->first(); - return $getDataUpdate->id; - }else{ - return false; - } - }else{ - return false; - } - die(); - } - - 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 = Presence::find($id); - - if($data){ - $result = $data->update($request->all()); - }else{ - return response()->json(['status'=>'failed','message'=>'data presence not found!','code'=>400], 400); - die(); - } - - if($result){ - return response()->json(['status'=>'success','message'=>'data presence successfully updated!','code'=>200], 200); - }else{ - return response()->json(['status'=>'failed','message'=>'data presence failed updated!','code'=>400], 400); - } - } - - public function delete($id) - { - $data = Presence::find($id); - - if($data){ - $delete = $data->delete(); - }else{ - return response()->json(['status'=>'failed','message'=>'data presence not found!','code'=>400], 400); - die(); - } - - if($delete){ - return response()->json(['status'=>'success','message'=>'data presence successfully deleted!','code'=>200], 200); - }else{ - return response()->json(['status'=>'failed','message'=>'data presence failed deleted!','code'=>400], 400); - } - } - - public function search(Request $request) - { - $payload = $request->all(); - $dataBuilder = $this->setUpPayload($payload, 't_clock_in_out'); - $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 = Presence::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 presence, please try again later!','code'=>400], 400); - } - } - - public function bulkUpdateLocation() - { - $data = Presence::all(); - $finalData = []; - foreach($data as $objRow) { - $presence = Presence::find($objRow->id); - - $clock_in_lat = $objRow->clock_in_lat; - $clock_in_lng = $objRow->clock_in_lng; - $objRow->clock_in_loc = ""; - if (isset($clock_in_lat) && isset($clock_in_lng)) { - $objRow->clock_in_loc = $this->getLoc($clock_in_lat, $clock_in_lng)->display_name; - } - - $clock_out_lat = $objRow->clock_out_lat; - $clock_out_lng = $objRow->clock_out_lng; - $objRow->clock_out_loc = ""; - if (isset($clock_out_lat) && isset($clock_out_lng) && $clock_out_lng != null) { - $locAddress = $this->getLoc($clock_out_lat, $clock_out_lng); - $objRow->clock_out_loc = isset($locAddress->display_name) ? $locAddress->display_name : "-"; - } - - - $presence->clock_in_loc = $objRow->clock_in_loc; - $presence->clock_out_loc = $objRow->clock_out_loc; - $presence->save(); - } - return response()->json(['status'=>'success','message'=>'success update!','code'=>200], 200); - } -} +validate($request, [ + 'user_id' => 'required' + ]); + + $checkLocation = $this->checkLocation($request); + $statusBoundary = false; + $date = date_create($request->clock_time); + // assign and in boundary + if(count($checkLocation) > 0 && $checkLocation[0]['boundary']){ + $statusBoundary = true; + } + $statusRestriction = HumanResource::select('status_boundary')->where('id', $request->user_id)->first(); + if ($statusRestriction->status_boundary) { + $statusBoundary = true; + } + // not assign + if(!$checkLocation[0]['status_assign'] && $checkLocation[0]['boundary'] == false){ + $data=array( + 'id' => null, + 'boundary' => $statusBoundary + ); + return response()->json(['status'=>'failed', 'data'=>$data, 'message'=>'Tidak dapat melakukan presensi. Anda belum di assign ke area kerja.','code'=>200], 200); + } + // assign and not in boundary + if($checkLocation[0]['status_assign'] && $checkLocation[0]['boundary'] == false){ + $data=array( + 'id' => null, + 'boundary' => true + ); + return response()->json(['status'=>'failed', 'data'=>$data, 'message'=>'Tidak dapat melakukan presensi. Anda berada di luar area kerja.','code'=>200], 200); + } + + if($request->type=="out"){ + $clock_out_loc = $this->getLoc($request->clock_out_lat, $request->clock_out_lng)->display_name; + $dataUpdate = array( + "clock_out"=>$request->clock_time, + "clock_out_lat" => $request->clock_out_lat, + "clock_out_lng" => $request->clock_out_lng, + "updated_by"=>$this->currentName, + "clock_out_loc" => $clock_out_loc, + "clock_out_boundary" => $statusBoundary + ); + + $resultUpdate = $this->updateFormAdd($dataUpdate, $request->user_id); + if($resultUpdate && $resultUpdate > 0){ + if($statusBoundary){ + for ($i=0; $i < count($checkLocation); $i++) { + # code... + DB::table('clock_in_out_boundary')->insert([ + "clock_in_out_id" => $resultUpdate, + "user_id" => $request->user_id, + "activity_id" => $checkLocation[$i]['activity_id'], + "type" => $request->type, + "created_at" => $date, + "created_by" => $this->currentName + ]); + }; + }; + $data=array( + 'id' => $resultUpdate, + 'boundary' => $statusBoundary + ); + + return response()->json(['status'=>'success', 'data'=>$data,'message'=>'clock out success!','code'=>200], 200); + } + else{ + return response()->json(['status'=>'failed','message'=>'clock out failed please try again!','code'=>400], 400); + } + die(); + } + + $onlyDate = date_format($date,"Y-m-d"); + $clock_in_loc = $this->getLoc($request->clock_in_lat, $request->clock_in_lng)->display_name; + + $dataAdd = array( + 'user_id'=> $request->user_id, + 'clock_in'=> $request->clock_time, + 'date_presence'=> $onlyDate, + 'created_by' => $this->currentName, + 'clock_in_lat' => $request->clock_in_lat, + 'clock_in_lng' => $request->clock_in_lng, + 'clock_in_loc' => $clock_in_loc, + 'clock_in_boundary' => $statusBoundary + ); + + $result = Presence::create($dataAdd); + $data=array( + 'id' => $result->id, + 'boundary' => $statusBoundary + ); + if($result){ + if($statusBoundary){ + for ($i=0; $i < count($checkLocation); $i++) { + # code... + DB::table('clock_in_out_boundary')->insert([ + "clock_in_out_id" => $result->id, + "user_id" => $request->user_id, + "activity_id" => $checkLocation[$i]['activity_id'], + "type" => $request->type, + "created_at" => $date, + "created_by" => $this->currentName + ]); + }; + }; + return response()->json(['status'=>'success', 'data' => $data,'message'=>'clock in successfully!','code'=>200], 200); + }else{ + return response()->json(['status'=>'failed','message'=>'clock in failed!','code'=>400], 400); + } + } + + public function reportK3(Request $request){ + // return response()->json(['status'=>'success', 'message'=>$request->report_k3['detail'],'code'=>200], 200); + + $this->validate($request, [ + 'user_id' => 'required' + ]); + + $checkLocation = $this->checkLocation($request); + $statusBoundary = false; + $date = date_create($request->time); + // assign and in boundary + if(count($checkLocation) > 0 && $checkLocation[0]['boundary']){ + $statusBoundary = true; + } + $statusRestriction = HumanResource::select('status_boundary')->where('id', $request->user_id)->first(); + if ($statusRestriction->status_boundary) { + $statusBoundary = true; + } + // not assign + if(!$checkLocation[0]['status_assign'] && $checkLocation[0]['boundary'] == false){ + $data=array( + 'id' => null, + 'boundary' => $statusBoundary + ); + return response()->json(['status'=>'failed', 'data'=>$data, 'message'=>'Tidak dapat melakukan presensi. Anda belum di assign ke area kerja.','code'=>200], 200); + } + // assign and not in boundary + if($checkLocation[0]['status_assign'] && $checkLocation[0]['boundary'] == false){ + $data=array( + 'id' => null, + 'boundary' => true + ); + return response()->json(['status'=>'failed', 'data'=>$data, 'message'=>'Tidak dapat melakukan presensi. Anda berada di luar area kerja.','code'=>200], 200); + } + + if($request->clock_in_out['type']=="out"){ + $clock_out_loc = $this->getLoc($request->clock_in_out['clock_out_lat'], $request->clock_in_out['clock_out_lng'])->display_name; + $dataUpdate = array( + "clock_out"=>$request->time, + "clock_out_lat" => $request->clock_in_out['clock_out_lat'], + "clock_out_lng" => $request->clock_in_out['clock_out_lng'], + "updated_by"=>$this->currentName, + "clock_out_loc" => $clock_out_loc, + "clock_out_boundary" => $statusBoundary + ); + + $resultUpdate = $this->updateFormAdd($dataUpdate, $request->user_id); + if($resultUpdate && $resultUpdate > 0){ + if($statusBoundary){ + for ($i=0; $i < count($checkLocation); $i++) { + # code... + DB::table('clock_in_out_boundary')->insert([ + "clock_in_out_id" => $resultUpdate, + "user_id" => $request->user_id, + "activity_id" => $checkLocation[$i]['activity_id'], + "type" => $request->clock_in_out['type'], + "created_at" => $date, + "created_by" => $this->currentName + ]); + }; + }; + $data=array( + 'presence_id' => $resultUpdate, + 'boundary' => $statusBoundary + ); + + return response()->json(['status'=>'success', 'data'=>$data,'message'=>'clock out success!','code'=>200], 200); + } + else{ + return response()->json(['status'=>'failed','message'=>'clock out failed please try again!','code'=>400], 400); + } + die(); + } + + $onlyDate = date_format($date,"Y-m-d"); + $clock_in_loc = $this->getLoc($request->clock_in_out['clock_in_lat'], $request->clock_in_out['clock_in_lng'])->display_name; + + $dataFormK3 = array( + "user_id" => $request->user_id, + "proyek_id" => $request->report_k3['proyek_id'], + "report_date" => $request->time, + "description" => $request->report_k3['description'] + ); + + $dataFormPresence = array( + 'user_id' => $request->user_id, + 'clock_in' => $request->time, + 'date_presence' => $onlyDate, + 'created_by' => $this->currentName, + 'clock_in_lat' => $request->clock_in_out['clock_in_lat'], + 'clock_in_lng' => $request->clock_in_out['clock_in_lng'], + 'clock_in_loc' => $clock_in_loc, + 'clock_in_boundary' => $statusBoundary + ); + + $result = Presence::create($dataFormPresence); + $data=array( + 'presence_id' => $result->id, + 'boundary' => $statusBoundary + ); + if($result){ + if($statusBoundary){ + $insertk3 = $this->insertK3($dataFormK3, $request->report_k3['detail']) ; + for ($i=0; $i < count($checkLocation); $i++) { + # code... + DB::table('clock_in_out_boundary')->insert([ + "clock_in_out_id" => $result->id, + "user_id" => $request->user_id, + "activity_id" => $checkLocation[$i]['activity_id'], + "type" => $request->type, + "created_at" => $date, + "created_by" => $this->currentName + ]); + }; + $data['report_id'] = $insertk3->id; + }; + return response()->json(['status'=>'success', 'data'=> $data, 'message'=>'clock in successfully!','code'=>200], 200); + }else{ + return response()->json(['status'=>'failed','message'=>'clock in failed!','code'=>400], 400); + } + } + + private function insertK3($params, $details){ + $insert = ReportK3::create($params); + if($insert && $details){ + $this->addDetailK3($details, $insert->id); + } + return $insert; + } + + private function checkLocation($params){ + // cek user tersebut apakah punya assign task yang ada bondary nya + // geom ada di table activity + // $clock_time = + $user = HumanResource::find($params->user_id); + $geom = DB::table("assign_hr_to_activity as ahta")->select("ma.geom", "ma.id") + ->join("m_activity as ma", "ma.id", "=", "ahta.activity_id") + ->where("ahta.user_id", $params->user_id) + ->whereNotNull("ma.geom") + ->whereDate("ma.start_date", "<=", $params->time) + ->whereDate("ma.end_date", ">=", $params->time) + ->get(); + $temp = []; + if (count($geom) > 0) { + foreach($geom as $dataGeom){ + $valGeom = json_decode($dataGeom->geom); + if($params->clock_in_out['type']=="out"){ + if($valGeom->type == "FeatureCollection"){ + $multiArea = $valGeom->features; + foreach($multiArea as $area){ + if ($area->geometry->type === "Point") { + $pointCoordinates = $area->geometry->coordinates; + $pointLng = $pointCoordinates[0]; + $pointLat = $pointCoordinates[1]; + + $check = DB::select(DB::raw("SELECT ST_Distance( + ST_GeomFromGeoJSON('" . json_encode($area->geometry) . "'), + ST_GeomFromText('POINT(" . $params->clock_in_out['clock_in_lng'] . " " . $params->clock_in_out['clock_in_lat'] . ")', 4326) + ) <= " . $area->properties->radius . " as within_radius")); + + if ($check[0]->within_radius) { + break; + } + } + $check = DB::select(DB::raw("SELECT ST_Intersects(ST_GeomFromGeoJSON('".json_encode($area->geometry)."'), + ST_GeomFromText('POINT(".$params->clock_in_out['clock_out_lng']." ".$params->clock_in_out['clock_out_lat'].")', 4326)) as boundary")); + if($check[0]->boundary){ + break; + } + } + }else{ + $check = DB::select(DB::raw("SELECT ST_Intersects(ST_GeomFromGeoJSON('".json_encode($valGeom->geometry)."'), + ST_GeomFromText('POINT(".$params->clock_in_out['clock_out_lng']." ".$params->clock_in_out['clock_out_lat'].")', 4326)) as boundary")); + } + }else{ + if($valGeom->type == "FeatureCollection"){ + $multiArea = $valGeom->features; + foreach($multiArea as $area){ + if ($area->geometry->type === "Point") { + $pointCoordinates = $area->geometry->coordinates; + $pointLng = $pointCoordinates[0]; + $pointLat = $pointCoordinates[1]; + + $check = DB::select(DB::raw("SELECT ST_Distance( + ST_GeomFromGeoJSON('" . json_encode($area->geometry) . "'), + ST_GeomFromText('POINT(" . $params->clock_in_out['clock_in_lng'] . " " . $params->clock_in_out['clock_in_lat'] . ")', 4326) + ) <= " . $area->properties->radius . " as within_radius")); + + if ($check[0]->within_radius) { + break; + } + } + $check = DB::select(DB::raw("SELECT ST_Intersects(ST_GeomFromGeoJSON('".json_encode($area->geometry)."'), + ST_GeomFromText('POINT(".$params->clock_in_out['clock_in_lng']." ".$params->clock_in_out['clock_in_lat'].")', 4326)) as boundary")); + if($check[0]->boundary){ + break; + } + } + }else{ + $check = DB::select(DB::raw("SELECT ST_Intersects(ST_GeomFromGeoJSON('".json_encode($valGeom->geometry)."'), + ST_GeomFromText('POINT(".$params->clock_in_out['clock_in_lng']." ".$params->clock_in_out['clock_in_lat'].")', 4326)) as boundary")); + } + } + if(!$user->status_boundary || count($check)>0){ + if(!$user->status_boundary || (isset($check[0]->boundary) && $check[0]->boundary)){ + $temp[]=array( + "activity_id" => $dataGeom->id, + "boundary" => $check[0]->boundary, + "status_assign" => true + ); + } else if (!$user->status_boundary || (isset($check[0]->within_radius) && $check[0]->within_radius)) { + $temp[]=array( + "activity_id" => $dataGeom->id, + "boundary" => $check[0]->within_radius, + "status_assign" => true + ); + } + } + } + // assign and not in boundary + if(count($temp) < 1){ + $temp[]=array( + "activity_id" => null, + "boundary" => false, + "status_assign" => true + // "geom" => $geom, + // "cek" => $check[0]->boundary + ); + } + } + else{ + // not assign + $temp[]=array( + "activity_id" => null, + "boundary" => false, + "status_assign" => false + ); + } + return $temp; + } + + public function checkLocationTest(Request $request){ + // cek user tersebut apakah punya assign task yang ada bondary nya + // geom ada di table activity + // $clock_time = + $params = $request; + $geom = DB::table("assign_hr_to_activity as ahta")->select("ma.geom", "ma.id") + ->join("m_activity as ma", "ma.id", "=", "ahta.activity_id") + ->where("ahta.user_id", $params->user_id) + ->whereNotNull("ma.geom") + ->whereDate("ma.start_date", "<=", $params->time) + ->whereDate("ma.end_date", ">=", $params->time) + ->get(); + $temp = []; + // return json_encode($geom); + if (count($geom) > 0) { + foreach($geom as $dataGeom){ + $valGeom = json_decode($dataGeom->geom); + if($params->clock_in_out['type']=="out"){ + if($valGeom->type == "FeatureCollection"){ + // return count($valGeom->features); + $multiArea = $valGeom->features; + foreach($multiArea as $area){ + $check = DB::select(DB::raw("SELECT ST_Intersects(ST_GeomFromGeoJSON('".json_encode($area->geometry)."'), + ST_GeomFromText('POINT(".$params->clock_in_out['clock_out_lng']." ".$params->clock_in_out['clock_out_lat'].")', 4326)) as boundary")); + if($check[0]->boundary){ + break; + } + } + }else{ + $check = DB::select(DB::raw("SELECT ST_Intersects(ST_GeomFromGeoJSON('".json_encode($valGeom->geometry)."'), + ST_GeomFromText('POINT(".$params->clock_in_out['clock_out_lng']." ".$params->clock_in_out['clock_out_lat'].")', 4326)) as boundary")); + } + }else{ + if($valGeom->type == "FeatureCollection"){ + // return count($valGeom->features); + $multiArea = $valGeom->features; + foreach($multiArea as $area){ + $check = DB::select(DB::raw("SELECT ST_Intersects(ST_GeomFromGeoJSON('".json_encode($area->geometry)."'), + ST_GeomFromText('POINT(".$params->clock_in_out['clock_in_lng']." ".$params->clock_in_out['clock_in_lat'].")', 4326)) as boundary")); + if($check[0]->boundary){ + break; + } + } + }else{ + $check = DB::select(DB::raw("SELECT ST_Intersects(ST_GeomFromGeoJSON('".json_encode($valGeom->geometry)."'), + ST_GeomFromText('POINT(".$params->clock_in_out['clock_in_lng']." ".$params->clock_in_out['clock_in_lat'].")', 4326)) as boundary")); + } + } + if(count($check)>0){ + if($check[0]->boundary){ + $temp[]=array( + "activity_id" => $dataGeom->id, + "boundary" => $check[0]->boundary, + "status_assign" => true + ); + } + } + } + // assign and not in boundary + if(count($temp) < 1){ + $temp[]=array( + "activity_id" => null, + "boundary" => false, + "status_assign" => true + // "geom" => $geom, + // "cek" => $check[0]->boundary + ); + } + } + else{ + // not assign + $temp[]=array( + "activity_id" => null, + "boundary" => false, + "status_assign" => false + ); + } + return $temp; + } + + public function edit($id){ + if(!$id || (int) $id < 0 || $id==""){ + return response()->json(['status'=>'failed','message'=>'id is required!','code'=>400], 400); + die(); + } + + $result = Presence::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 clockinout($id) { + $dateTimeNow = Carbon::now()->addHour(7); + $dataPresence = Presence::where('user_id', $id)->orderBy('id', 'DESC')->first(); + + if($dataPresence){ + + $dateNow = date("Y-m-d"); + $dateA = strtotime($dataPresence->clock_in); + $dayClockin = date("Y-m-d", $dateA); + if($dayClockin == $dateNow){ + $clock_in = $dataPresence->clock_in; + $clock_out = $dataPresence->clock_out; + }else{ + $clock_in = null; + $clock_out = null; + } + $res_data = array( + "id" => $dataPresence->id, + "at" => $dateTimeNow, + "user_id"=> $id, + "clock_in"=> $clock_in, + "clock_out"=> $clock_out, + "last_clock_in"=> $dataPresence->clock_in, + "last_clock_out"=> $dataPresence->clock_out, + "in_working_time"=> true + ); + return response()->json(['status'=>'success','data'=>$res_data,'code'=>200], 200); + } + $res_data = array( + "id" => null, + "at" => $dateTimeNow, + "user_id"=> $id, + "clock_in"=> null, + "clock_out"=> null, + "last_clock_in"=> null, + "last_clock_out"=> null, + "in_working_time"=> true + ); + return response()->json(['status'=>'success','data'=>$res_data,'code'=>200], 200); + } + + private function updateFormAdd($data, $id){ + $date = date_create($data['clock_out']); + $onlyDate = date_format($date,"Y-m-d"); + + $dataPresence = Presence::where('user_id',$id) + ->where("clock_in", "<=", $data["clock_out"]) + ->orderByDesc("id") + ->first(); + + if($dataPresence){ + $queryUpdate = $dataPresence->update($data); + if($queryUpdate){ + $getDataUpdate = Presence::where('user_id', $id)->where("date_presence", $onlyDate)->first(); + return $getDataUpdate->id; + }else{ + return false; + } + }else{ + return false; + } + die(); + } + + 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 = Presence::find($id); + + if($data){ + $result = $data->update($request->all()); + }else{ + return response()->json(['status'=>'failed','message'=>'data presence not found!','code'=>400], 400); + die(); + } + + if($result){ + return response()->json(['status'=>'success','message'=>'data presence successfully updated!','code'=>200], 200); + }else{ + return response()->json(['status'=>'failed','message'=>'data presence failed updated!','code'=>400], 400); + } + } + + public function delete($id) + { + $data = Presence::find($id); + + if($data){ + $delete = $data->delete(); + }else{ + return response()->json(['status'=>'failed','message'=>'data presence not found!','code'=>400], 400); + die(); + } + + if($delete){ + return response()->json(['status'=>'success','message'=>'data presence successfully deleted!','code'=>200], 200); + }else{ + return response()->json(['status'=>'failed','message'=>'data presence failed deleted!','code'=>400], 400); + } + } + + public function search(Request $request) + { + $payload = $request->all(); + $dataBuilder = $this->setUpPayload($payload, 't_clock_in_out'); + $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 = Presence::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 presence, please try again later!','code'=>400], 400); + } + } + + public function bulkUpdateLocation() + { + $data = Presence::all(); + $finalData = []; + foreach($data as $objRow) { + $presence = Presence::find($objRow->id); + + $clock_in_lat = $objRow->clock_in_lat; + $clock_in_lng = $objRow->clock_in_lng; + $objRow->clock_in_loc = ""; + if (isset($clock_in_lat) && isset($clock_in_lng)) { + $objRow->clock_in_loc = $this->getLoc($clock_in_lat, $clock_in_lng)->display_name; + } + + $clock_out_lat = $objRow->clock_out_lat; + $clock_out_lng = $objRow->clock_out_lng; + $objRow->clock_out_loc = ""; + if (isset($clock_out_lat) && isset($clock_out_lng) && $clock_out_lng != null) { + $locAddress = $this->getLoc($clock_out_lat, $clock_out_lng); + $objRow->clock_out_loc = isset($locAddress->display_name) ? $locAddress->display_name : "-"; + } + + + $presence->clock_in_loc = $objRow->clock_in_loc; + $presence->clock_out_loc = $objRow->clock_out_loc; + $presence->save(); + } + return response()->json(['status'=>'success','message'=>'success update!','code'=>200], 200); + } +} diff --git a/app/Http/Controllers/UserToActivityController.php b/app/Http/Controllers/UserToActivityController.php index 14e67ed..f9b9ef3 100644 --- a/app/Http/Controllers/UserToActivityController.php +++ b/app/Http/Controllers/UserToActivityController.php @@ -1,199 +1,220 @@ -validate($request, [ - 'activity_id' => 'required', - 'user_id' => 'required' - ]); - - $data = $request->all(); - $data['created_by'] = $this->currentName; - - if(!UserToActivity::create($data)) - return response()->json(['status'=>'failed','message'=>'Failed to add data!','code'=> 500]); - - return response()->json(['status'=>'success','message'=>'Data added!', 'code'=> 200]); - } - - public function addMultiple(Request $request){ - $users_id = $request->user_id; - $users_role = $request->role_proyek_id; - - // $data = UserToActivity::where("activity_id", $request->activity_id); - // if($data){ - // $data->delete(); - // } - - if(is_array($users_id) && count($users_id) > 0){ - $countRes = 0; - foreach($users_id as $index => $item){ - $dataInsert = array( - "user_id" => $item, - "activity_id" => $request->activity_id, - "role_proyek_id" => $users_role[$index], - "proyek_id" => $request->proyek_id, - "created_by" => $this->currentName, - "version_gantt_id" => $request->version_gantt_id - ); - $result = UserToActivity::create($dataInsert); - if($result){ - $countRes++; - }else{ - $countRes--; - } - } - - if($countRes == 0) - return response()->json(['status'=>'failed' ,'message'=>'Failed to add data!','code'=> 500]); - - $allCost = $this->calculateAllCost($request->activity_id, $request->proyek_id); - $dataUpdateCost = array( - "rencana_biaya"=>$allCost, - "updated_by"=> $this->currentName - ); - $actUpdate = Activity::find($request->activity_id); - if($actUpdate){ - $actUpdate->update($dataUpdateCost); - if($actUpdate->parent_id){ - $this->updatedCostPlanning($actUpdate->parent_id); - } - } - return response()->json(['status'=>'success' ,'message'=>'Data added!','code'=>200]); - }else{ - return response()->json(['status'=>'success' ,'message'=>'Data added!','code'=>200]); - } - - } - - private function updatedCostPlanning($parent_id) { - $sumBiaya = Activity::selectRaw('sum(cast(rencana_biaya as double precision))') - ->where("parent_id", $parent_id) - ->first(); - if($parent = Activity::find($parent_id)){ - $parent->update([ - "rencana_biaya" => $sumBiaya->sum, - ]); - } - } - - 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 = UserToActivity::find($id); - if(!$data) - return response()->json(['status'=>'failed','message'=>'Data not found!','code'=> 404], 404); - - if(!$data->update($request->all())); - return response()->json(['status'=>'failed','message'=>'Failed to update!','code'=> 500], 500); - - return response()->json(['status'=>'success','message'=>'Data updated!','code'=>200], 200); - } - - public function delete($id) - { - $data = UserToActivity::find($id); - if(!$data) - return response()->json(['status'=>'failed','message'=>'data user to activity gantt Project not found!','code'=>400], 400); - - if($data->delete()){ - return response()->json(['status'=>'success','message'=>'Data deleted!','code'=>200], 200); - } - - return response()->json(['status'=>'failed','message'=>'Failed to delete!','code'=> 500], 500); - } - - public function edit($id){ - if(!$id || (int) $id < 0 || $id==""){ - return response()->json(['status'=>'failed','message'=>'id is required!','code'=>400], 400); - die(); - } - - $result = UserToActivity::find($id); - - if(!$result) - return response()->json(['status'=>'failed','message'=>'failed get data tools resource, please try again later!','code'=>400], 400); - - return response()->json(['status'=>'success','code'=>200,'data'=>$result], 200); - } - - public function search(Request $request) - { - $payload = $request->all(); - $dataBuilder = $this->setUpPayload($payload, 'assign_hr_to_activity'); - $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 = UserToActivity::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 user to activity gantt, please try again later!','code'=>400], 400); - } - } - - public function listFiltered(Request $request) - { - $startDate = $request->start_date; - $endDate = $request->end_date; - $userId = $request->user_id; - $proyekId = $request->proyek_id; - - if (isset($proyekId)) { - $data = UserToActivity::select('assign_hr_to_activity.id', 'assign_hr_to_activity.activity_id', 'm_activity.name', 'm_activity.kode_sortname', 'm_activity.start_date', 'm_activity.end_date') - ->join('m_activity', 'assign_hr_to_activity.activity_id', '=', 'm_activity.id') - ->where('m_activity.start_date', '=', $startDate) - ->where('m_activity.end_date', '=', $endDate) - ->where('assign_hr_to_activity.user_id', '=', $userId) - ->where('assign_hr_to_activity.proyek_id', '=', $proyekId) - ->get(); - }else { - $data = UserToActivity::select('assign_hr_to_activity.id', 'assign_hr_to_activity.activity_id', 'm_activity.name', 'm_activity.kode_sortname', 'm_activity.start_date', 'm_activity.end_date') - ->join('m_activity', 'assign_hr_to_activity.activity_id', '=', 'm_activity.id') - ->where('m_activity.start_date', '=', $startDate) - ->where('m_activity.end_date', '=', $endDate) - ->where('assign_hr_to_activity.user_id', '=', $userId) - ->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 user to activity gantt, please try again later!','code'=>400], 400); - } - } - - public function datatables(Request $request){ - $id_activity = $request->query('idact'); - $data = UserToActivity::select("assign_hr_to_activity.id as id", "u.name as user_name", "mrp.name as role_name") - ->join("m_role_proyek as mrp", "mrp.id", "=", "assign_hr_to_activity.role_proyek_id") - ->join("m_users as u", "u.id", "=", "assign_hr_to_activity.user_id") - ->where('assign_hr_to_activity.activity_id', $id_activity) - ->get(); - return Datatables::of($data) - ->addIndexColumn() - ->addColumn('action', function($row){ - $actionBtn = ''; - return $actionBtn; - }) - ->rawColumns(['action'])->make(true); - } -} +validate($request, [ + 'activity_id' => 'required', + 'user_id' => 'required' + ]); + + $data = $request->all(); + $data['created_by'] = $this->currentName; + + if(!UserToActivity::create($data)) + return response()->json(['status'=>'failed','message'=>'Failed to add data!','code'=> 500]); + + return response()->json(['status'=>'success','message'=>'Data added!', 'code'=> 200]); + } + + public function addMultiple(Request $request){ + $users_id = $request->user_id; + $users_role = $request->role_proyek_id; + + // $data = UserToActivity::where("activity_id", $request->activity_id); + // if($data){ + // $data->delete(); + // } + + if(is_array($users_id) && count($users_id) > 0){ + $countRes = 0; + foreach($users_id as $index => $item){ + $dataInsert = array( + "user_id" => $item, + "activity_id" => $request->activity_id, + "role_proyek_id" => $users_role[$index], + "proyek_id" => $request->proyek_id, + "created_by" => $this->currentName, + "version_gantt_id" => $request->version_gantt_id + ); + $result = UserToActivity::create($dataInsert); + if($result){ + $countRes++; + }else{ + $countRes--; + } + } + + if($countRes == 0) + return response()->json(['status'=>'failed' ,'message'=>'Failed to add data!','code'=> 500]); + + $allCost = $this->calculateAllCost($request->activity_id, $request->proyek_id); + $dataUpdateCost = array( + "rencana_biaya"=>$allCost, + "updated_by"=> $this->currentName + ); + $actUpdate = Activity::find($request->activity_id); + if($actUpdate){ + $actUpdate->update($dataUpdateCost); + if($actUpdate->parent_id){ + $this->updatedCostPlanning($actUpdate->parent_id); + } + } + return response()->json(['status'=>'success' ,'message'=>'Data added!','code'=>200]); + }else{ + return response()->json(['status'=>'success' ,'message'=>'Data added!','code'=>200]); + } + + } + + private function updatedCostPlanning($parent_id) { + $sumBiaya = Activity::selectRaw('sum(cast(rencana_biaya as double precision))') + ->where("parent_id", $parent_id) + ->first(); + if($parent = Activity::find($parent_id)){ + $parent->update([ + "rencana_biaya" => $sumBiaya->sum, + ]); + } + } + + 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 = UserToActivity::find($id); + if(!$data) + return response()->json(['status'=>'failed','message'=>'Data not found!','code'=> 404], 404); + + if(!$data->update($request->all())); + return response()->json(['status'=>'failed','message'=>'Failed to update!','code'=> 500], 500); + + return response()->json(['status'=>'success','message'=>'Data updated!','code'=>200], 200); + } + + public function delete($id) + { + $data = UserToActivity::find($id); + if(!$data) + return response()->json(['status'=>'failed','message'=>'data user to activity gantt Project not found!','code'=>400], 400); + + if($data->delete()){ + return response()->json(['status'=>'success','message'=>'Data deleted!','code'=>200], 200); + } + + return response()->json(['status'=>'failed','message'=>'Failed to delete!','code'=> 500], 500); + } + + public function edit($id){ + if(!$id || (int) $id < 0 || $id==""){ + return response()->json(['status'=>'failed','message'=>'id is required!','code'=>400], 400); + die(); + } + + $result = UserToActivity::find($id); + + if(!$result) + return response()->json(['status'=>'failed','message'=>'failed get data tools resource, please try again later!','code'=>400], 400); + + return response()->json(['status'=>'success','code'=>200,'data'=>$result], 200); + } + + public function search(Request $request) + { + $payload = $request->all(); + if (isset($payload['join_column'])) { + $join_column = $payload['join_column']; + $startDate = $join_column['start_date']; + $endDate = $join_column['end_date']; + $startDate = date("Y-m-d H:i:sO", strtotime($startDate)); + $endDate = date("Y-m-d H:i:sO", strtotime($endDate)); + unset($payload['join_column']); + } + + $dataBuilder = $this->setUpPayload($payload, 'assign_hr_to_activity'); + $builder = $dataBuilder['builder']; + $countBuilder = $dataBuilder['count']; + if (isset($join_column)) { + $builder->where(function ($query) use ($startDate, $endDate) { + $query + ->where('m_proyek.mulai_proyek', '<=', $startDate) + ->where('m_proyek.akhir_proyek', '>=', $endDate) + ->where(function ($query) use ($endDate, $startDate) { + $query + ->where('m_activity.start_date', '<=', $startDate) + ->where('m_activity.end_date', '>=', $endDate); + }); + }); + } + $dataGet = $builder->get(); + $totalRecord = $countBuilder->count(); + return response()->json(['status'=>'success','code'=>200,'data'=>$dataGet, 'totalRecord'=>$totalRecord], 200); + } + + public function list() + { + $data = UserToActivity::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 user to activity gantt, please try again later!','code'=>400], 400); + } + } + + public function listFiltered(Request $request) + { + $startDate = $request->start_date; + $endDate = $request->end_date; + $userId = $request->user_id; + $proyekId = $request->proyek_id; + + if (isset($proyekId)) { + $data = UserToActivity::select('assign_hr_to_activity.id', 'assign_hr_to_activity.activity_id', 'm_activity.name', 'm_activity.kode_sortname', 'm_activity.start_date', 'm_activity.end_date') + ->join('m_activity', 'assign_hr_to_activity.activity_id', '=', 'm_activity.id') + ->where('m_activity.start_date', '=', $startDate) + ->where('m_activity.end_date', '=', $endDate) + ->where('assign_hr_to_activity.user_id', '=', $userId) + ->where('assign_hr_to_activity.proyek_id', '=', $proyekId) + ->get(); + }else { + $data = UserToActivity::select('assign_hr_to_activity.id', 'assign_hr_to_activity.activity_id', 'm_activity.name', 'm_activity.kode_sortname', 'm_activity.start_date', 'm_activity.end_date') + ->join('m_activity', 'assign_hr_to_activity.activity_id', '=', 'm_activity.id') + ->where('m_activity.start_date', '=', $startDate) + ->where('m_activity.end_date', '=', $endDate) + ->where('assign_hr_to_activity.user_id', '=', $userId) + ->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 user to activity gantt, please try again later!','code'=>400], 400); + } + } + + public function datatables(Request $request){ + $id_activity = $request->query('idact'); + $data = UserToActivity::select("assign_hr_to_activity.id as id", "u.name as user_name", "mrp.name as role_name") + ->join("m_role_proyek as mrp", "mrp.id", "=", "assign_hr_to_activity.role_proyek_id") + ->join("m_users as u", "u.id", "=", "assign_hr_to_activity.user_id") + ->where('assign_hr_to_activity.activity_id', $id_activity) + ->get(); + return Datatables::of($data) + ->addIndexColumn() + ->addColumn('action', function($row){ + $actionBtn = ''; + return $actionBtn; + }) + ->rawColumns(['action'])->make(true); + } +} From 366ac1477762d209c6bcb70c96ef7d6dd73f3b15 Mon Sep 17 00:00:00 2001 From: wahyu Date: Tue, 26 Sep 2023 10:24:01 +0700 Subject: [PATCH 02/14] fix date filter --- .../Controllers/UserToActivityController.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/app/Http/Controllers/UserToActivityController.php b/app/Http/Controllers/UserToActivityController.php index f9b9ef3..5951aa6 100644 --- a/app/Http/Controllers/UserToActivityController.php +++ b/app/Http/Controllers/UserToActivityController.php @@ -131,10 +131,6 @@ class UserToActivityController extends Controller $payload = $request->all(); if (isset($payload['join_column'])) { $join_column = $payload['join_column']; - $startDate = $join_column['start_date']; - $endDate = $join_column['end_date']; - $startDate = date("Y-m-d H:i:sO", strtotime($startDate)); - $endDate = date("Y-m-d H:i:sO", strtotime($endDate)); unset($payload['join_column']); } @@ -142,14 +138,18 @@ class UserToActivityController extends Controller $builder = $dataBuilder['builder']; $countBuilder = $dataBuilder['count']; if (isset($join_column)) { + $startDate = $join_column['start_date']; + $endDate = $join_column['end_date']; + $startDate = date("Y-m-d H:i:sO", strtotime($startDate)); + $endDate = date("Y-m-d H:i:sO", strtotime($endDate)); $builder->where(function ($query) use ($startDate, $endDate) { $query - ->where('m_proyek.mulai_proyek', '<=', $startDate) - ->where('m_proyek.akhir_proyek', '>=', $endDate) + ->where('m_proyek.mulai_proyek', '>=', $startDate) + ->where('m_proyek.akhir_proyek', '<=', $endDate) ->where(function ($query) use ($endDate, $startDate) { $query - ->where('m_activity.start_date', '<=', $startDate) - ->where('m_activity.end_date', '>=', $endDate); + ->where('m_activity.start_date', '>=', $startDate) + ->where('m_activity.end_date', '<=', $endDate); }); }); } From 1a02dd2d03dea20092536068fc0dded0f14ab505 Mon Sep 17 00:00:00 2001 From: wahyu Date: Tue, 26 Sep 2023 13:54:21 +0700 Subject: [PATCH 03/14] add status filter --- app/Http/Controllers/UserToActivityController.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/app/Http/Controllers/UserToActivityController.php b/app/Http/Controllers/UserToActivityController.php index 5951aa6..c422cf6 100644 --- a/app/Http/Controllers/UserToActivityController.php +++ b/app/Http/Controllers/UserToActivityController.php @@ -5,6 +5,7 @@ namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\UserToActivity; use App\Models\Activity; +use App\Models\AssignMaterial; use Datatables; class UserToActivityController extends Controller @@ -140,6 +141,7 @@ class UserToActivityController extends Controller if (isset($join_column)) { $startDate = $join_column['start_date']; $endDate = $join_column['end_date']; + $status = $join_column['status']; $startDate = date("Y-m-d H:i:sO", strtotime($startDate)); $endDate = date("Y-m-d H:i:sO", strtotime($endDate)); $builder->where(function ($query) use ($startDate, $endDate) { @@ -154,6 +156,16 @@ class UserToActivityController extends Controller }); } $dataGet = $builder->get(); + if (isset($status) && $status != "") { + $filteredData = []; + foreach ($dataGet as $value) { + $assignMaterial = AssignMaterial::where('activity_id', $value->activity_id)->first(); + if ($assignMaterial->status_activity == $status) { + $filteredData[] = $value; + } + } + $dataGet = $filteredData; + } $totalRecord = $countBuilder->count(); return response()->json(['status'=>'success','code'=>200,'data'=>$dataGet, 'totalRecord'=>$totalRecord], 200); } From ab1842f5c632f2b524138ab63fa08f1f5c44b125 Mon Sep 17 00:00:00 2001 From: wahyu Date: Wed, 27 Sep 2023 09:31:18 +0700 Subject: [PATCH 04/14] add filter project id --- app/Http/Controllers/PresenceController.php | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/app/Http/Controllers/PresenceController.php b/app/Http/Controllers/PresenceController.php index 0d79917..f5309ac 100644 --- a/app/Http/Controllers/PresenceController.php +++ b/app/Http/Controllers/PresenceController.php @@ -259,13 +259,17 @@ class PresenceController extends Controller // geom ada di table activity // $clock_time = $user = HumanResource::find($params->user_id); - $geom = DB::table("assign_hr_to_activity as ahta")->select("ma.geom", "ma.id") - ->join("m_activity as ma", "ma.id", "=", "ahta.activity_id") - ->where("ahta.user_id", $params->user_id) - ->whereNotNull("ma.geom") - ->whereDate("ma.start_date", "<=", $params->time) - ->whereDate("ma.end_date", ">=", $params->time) - ->get(); + $geomQuery = DB::table("assign_hr_to_activity as ahta")->select("ma.geom", "ma.id") + ->join("m_activity as ma", "ma.id", "=", "ahta.activity_id") + ->where("ahta.user_id", $params->user_id) + ->whereNotNull("ma.geom") + ->whereDate("ma.start_date", "<=", $params->time) + ->whereDate("ma.end_date", ">=", $params->time); + if (isset($params->report_k3['proyek_id'])) { + $geom = $geomQuery->where("ma.proyek_id", $params->report_k3['proyek_id'])->get(); + } else { + $geom = $geomQuery->get(); + } $temp = []; if (count($geom) > 0) { foreach($geom as $dataGeom){ From c422bade05f132ae292668d9858a3f17407cbc76 Mon Sep 17 00:00:00 2001 From: wahyu Date: Wed, 27 Sep 2023 14:15:42 +0700 Subject: [PATCH 05/14] fix work area restriction --- app/Http/Controllers/PresenceController.php | 28 ++++++++++++++++----- 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/app/Http/Controllers/PresenceController.php b/app/Http/Controllers/PresenceController.php index f5309ac..49b9da1 100644 --- a/app/Http/Controllers/PresenceController.php +++ b/app/Http/Controllers/PresenceController.php @@ -345,6 +345,13 @@ class PresenceController extends Controller "status_assign" => true ); } + } else { + // bypass work area restriction + $temp[]=array( + "activity_id" => null, + "boundary" => true, + "status_assign" => true + ); } } // assign and not in boundary @@ -359,12 +366,21 @@ class PresenceController extends Controller } } else{ - // not assign - $temp[]=array( - "activity_id" => null, - "boundary" => false, - "status_assign" => false - ); + if ($user->status_boundary) { + // bypass work area restriction + $temp[]=array( + "activity_id" => null, + "boundary" => true, + "status_assign" => true + ); + } else { + // not assign + $temp[]=array( + "activity_id" => null, + "boundary" => false, + "status_assign" => false + ); + } } return $temp; } From b7ffe700b26afe5dd8efc14c8d176c40faac5701 Mon Sep 17 00:00:00 2001 From: ibnu Date: Wed, 27 Sep 2023 18:01:07 +0700 Subject: [PATCH 06/14] update sementara untuk clockin-out work restricted --- app/Http/Controllers/PresenceController.php | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/app/Http/Controllers/PresenceController.php b/app/Http/Controllers/PresenceController.php index 0d79917..cff437f 100644 --- a/app/Http/Controllers/PresenceController.php +++ b/app/Http/Controllers/PresenceController.php @@ -26,11 +26,11 @@ class PresenceController extends Controller $statusBoundary = true; } $statusRestriction = HumanResource::select('status_boundary')->where('id', $request->user_id)->first(); - if ($statusRestriction->status_boundary) { + if (!$statusRestriction->status_boundary) { $statusBoundary = true; } // not assign - if(!$checkLocation[0]['status_assign'] && $checkLocation[0]['boundary'] == false){ + if(!$checkLocation[0]['status_assign'] && $checkLocation[0]['boundary'] == false && $statusBoundary == false){ $data=array( 'id' => null, 'boundary' => $statusBoundary @@ -38,7 +38,7 @@ class PresenceController extends Controller return response()->json(['status'=>'failed', 'data'=>$data, 'message'=>'Tidak dapat melakukan presensi. Anda belum di assign ke area kerja.','code'=>200], 200); } // assign and not in boundary - if($checkLocation[0]['status_assign'] && $checkLocation[0]['boundary'] == false){ + if($checkLocation[0]['status_assign'] && $checkLocation[0]['boundary'] == false && $statusBoundary == false){ $data=array( 'id' => null, 'boundary' => true @@ -65,7 +65,7 @@ class PresenceController extends Controller DB::table('clock_in_out_boundary')->insert([ "clock_in_out_id" => $resultUpdate, "user_id" => $request->user_id, - "activity_id" => $checkLocation[$i]['activity_id'], + "activity_id" => $checkLocation[$i]['activity_id'] ? $checkLocation[$i]['activity_id'] : 0, "type" => $request->type, "created_at" => $date, "created_by" => $this->currentName @@ -86,7 +86,8 @@ class PresenceController extends Controller } $onlyDate = date_format($date,"Y-m-d"); - $clock_in_loc = $this->getLoc($request->clock_in_lat, $request->clock_in_lng)->display_name; + // $clock_in_loc = $this->getLoc($request->clock_in_lat, $request->clock_in_lng)->display_name; + $clock_in_loc = "test"; $dataAdd = array( 'user_id'=> $request->user_id, @@ -139,11 +140,11 @@ class PresenceController extends Controller $statusBoundary = true; } $statusRestriction = HumanResource::select('status_boundary')->where('id', $request->user_id)->first(); - if ($statusRestriction->status_boundary) { + if (!$statusRestriction->status_boundary) { $statusBoundary = true; } // not assign - if(!$checkLocation[0]['status_assign'] && $checkLocation[0]['boundary'] == false){ + if(!$checkLocation[0]['status_assign'] && $checkLocation[0]['boundary'] == false && $statusBoundary == false){ $data=array( 'id' => null, 'boundary' => $statusBoundary @@ -151,7 +152,7 @@ class PresenceController extends Controller return response()->json(['status'=>'failed', 'data'=>$data, 'message'=>'Tidak dapat melakukan presensi. Anda belum di assign ke area kerja.','code'=>200], 200); } // assign and not in boundary - if($checkLocation[0]['status_assign'] && $checkLocation[0]['boundary'] == false){ + if($checkLocation[0]['status_assign'] && $checkLocation[0]['boundary'] == false && $statusBoundary == false){ $data=array( 'id' => null, 'boundary' => true @@ -178,7 +179,7 @@ class PresenceController extends Controller DB::table('clock_in_out_boundary')->insert([ "clock_in_out_id" => $resultUpdate, "user_id" => $request->user_id, - "activity_id" => $checkLocation[$i]['activity_id'], + "activity_id" => $checkLocation[$i]['activity_id'] ? $checkLocation[$i]['activity_id'] : 0, "type" => $request->clock_in_out['type'], "created_at" => $date, "created_by" => $this->currentName @@ -232,7 +233,7 @@ class PresenceController extends Controller DB::table('clock_in_out_boundary')->insert([ "clock_in_out_id" => $result->id, "user_id" => $request->user_id, - "activity_id" => $checkLocation[$i]['activity_id'], + "activity_id" => $checkLocation[$i]['activity_id'] ? $checkLocation[$i]['activity_id'] : 0, "type" => $request->type, "created_at" => $date, "created_by" => $this->currentName From 53cb5d59f97b2e61ec64ee59c7e72ff6efcd251d Mon Sep 17 00:00:00 2001 From: wahyu Date: Mon, 2 Oct 2023 08:34:23 +0700 Subject: [PATCH 07/14] handling string --- app/Http/Controllers/ActivityController.php | 1 + 1 file changed, 1 insertion(+) diff --git a/app/Http/Controllers/ActivityController.php b/app/Http/Controllers/ActivityController.php index b8af8d4..b04972d 100644 --- a/app/Http/Controllers/ActivityController.php +++ b/app/Http/Controllers/ActivityController.php @@ -428,6 +428,7 @@ class ActivityController extends Controller $activityToUpdate = $activity->firstWhere('id', $entity['data']['id']); $entity['data']['name'] = $entity['data']['text']; $entity['data']['persentase_progress'] = $entity['data']['progress'] * 100; + $entity['data']['rencana_biaya'] = str_replace(",", ".", $entity['data']['rencana_biaya']); if (isset($entity['data']['target'])) { $this->updateOrder($entity['data']['id'], $entity['data']['target']); } From 4f3f4aa71db897e7d6018c14fa2a30a100526058 Mon Sep 17 00:00:00 2001 From: wahyu Date: Fri, 6 Oct 2023 09:33:37 +0700 Subject: [PATCH 08/14] fix sync report --- app/Http/Controllers/ProjectController.php | 44 +++++++++++++--------- 1 file changed, 27 insertions(+), 17 deletions(-) diff --git a/app/Http/Controllers/ProjectController.php b/app/Http/Controllers/ProjectController.php index aee3fe3..7f5c8f9 100644 --- a/app/Http/Controllers/ProjectController.php +++ b/app/Http/Controllers/ProjectController.php @@ -416,17 +416,21 @@ class ProjectController extends Controller $firstReport = ReportActivityMaterial::where('activity_id', $activity_id)->orderBy('report_date')->first(); $reports[] = ProjectController::setSyncDate($activity_id, $activity, $firstReport); } + $successor = Link::where('t_activity_id', $activity->id)->first(); + if ($successor) { + $predecessor = Activity::find($successor->s_activity_id); + $activity->start_date = $predecessor->end_date; + $end_date = new DateTime($activity->start_date); + $end_date->modify("+" . $activity->duration . " days"); + $activity->end_date = $end_date->format("Y-m-d H:i:sO"); + } + $activity->save(); } /* return response()->json(['status'=>'success','data'=> $reports,'code'=>200], 200); */ /* return response()->json(['status'=>'success','data'=> $activities,'code'=>200], 200); */ for ($i=0; $i < count($reports); $i++) { $activity = Activity::find($reports[$i]['activity_id']); - $successor = Link::where('t_activity_id', $activity->id)->first(); - if ($successor) { - $predecessor = Activity::find($successor->s_activity_id); - $activity->start_date = $predecessor->end_date; - } if($reports[$i]['status'] != 'done'){ $reports[$i]['max_date']->modify('-1 day'); }else if($reports[$i]['status'] == 'done'){ @@ -434,19 +438,25 @@ class ProjectController extends Controller } $activity->start_date = $reports[$i]['min_date']; //same early $activity->end_date = $reports[$i]['max_date']->setTime(23,59,59); // same early - $activity->actual_start = $reports[$i]['min_date']; - $activity->save(); - } - - foreach($activities as $activity) { - $successor = Link::where('t_activity_id', $activity->id)->first(); - if ($successor) { - $predecessor = Activity::find($successor->s_activity_id); - $activity->start_date = $predecessor->end_date; - $end_date = new DateTime($activity->start_date); - $end_date->modify("+" . $activity->duration . " days"); - $activity->end_date = $end_date->format("Y-m-d H:i:sO"); + $dayOffs = VersionGantt::where('id', $activity->version_gantt_id)->first()->config_dayoff; + $startDate = new DateTime($activity->start_date); + $endDate = new DateTime($activity->end_date); + $diff = date_diff($startDate, $endDate); + $duration = $diff->days + 1; + $daysRemaining = $duration; + // Loop until the remaining days become zero + while ($daysRemaining > 0) { + $endDate->modify('-1 day'); + // Check if the current day is a day off (Sunday or Saturday) + $currentDayOfWeek = (int) $endDate->format('w'); + if (strpos($dayOffs, (string) $currentDayOfWeek) !== false) { + // continue; // Skip the day off and continue to the next day + $duration--; + } + $daysRemaining--; // Decrease the remaining days by one } + $activity->duration = $duration; + $activity->actual_start = $reports[$i]['min_date']; $activity->save(); } From 3252a98090820c01b7b72d7a9aef669dfdb73b78 Mon Sep 17 00:00:00 2001 From: wahyu Date: Fri, 6 Oct 2023 14:06:25 +0700 Subject: [PATCH 09/14] cek rencana biaya --- app/Http/Controllers/ActivityController.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app/Http/Controllers/ActivityController.php b/app/Http/Controllers/ActivityController.php index b04972d..1b43a3e 100644 --- a/app/Http/Controllers/ActivityController.php +++ b/app/Http/Controllers/ActivityController.php @@ -428,7 +428,9 @@ class ActivityController extends Controller $activityToUpdate = $activity->firstWhere('id', $entity['data']['id']); $entity['data']['name'] = $entity['data']['text']; $entity['data']['persentase_progress'] = $entity['data']['progress'] * 100; - $entity['data']['rencana_biaya'] = str_replace(",", ".", $entity['data']['rencana_biaya']); + if (isset($entity['data']['rencana_biaya'])) { + $entity['data']['rencana_biaya'] = str_replace(",", ".", $entity['data']['rencana_biaya']); + } if (isset($entity['data']['target'])) { $this->updateOrder($entity['data']['id'], $entity['data']['target']); } From 6b74bf5852107092504dddd87b3c67ee6c2cc7fd Mon Sep 17 00:00:00 2001 From: wahyu Date: Mon, 9 Oct 2023 09:02:52 +0700 Subject: [PATCH 10/14] fix endline --- app/Models/UserToActivity.php | 122 +++++++++++++++++----------------- 1 file changed, 61 insertions(+), 61 deletions(-) diff --git a/app/Models/UserToActivity.php b/app/Models/UserToActivity.php index a33667c..76f948a 100644 --- a/app/Models/UserToActivity.php +++ b/app/Models/UserToActivity.php @@ -1,61 +1,61 @@ -activity_id); - $getWorkingHours = UserToProyek::where('user_id', $data->user_id)->where('proyek_id', $activity->proyek_id)->first(); - - $salary = ($getWorkingHours->standart_rate * $activity->duration) * ($getWorkingHours->max_used / 100); - - if($getWorkingHours->uom_standart_rate == "Hour") - $salary = ($getWorkingHours->standart_rate * 8) * $activity->duration * ($getWorkingHours->max_used / 100); - - $activity->rencana_biaya += $salary; - $activity->save(); - }); - - static::deleted(function($data) { - $activity = Activity::find($data->activity_id); - - $getWorkingHours = UserToProyek::where('user_id', $data->user_id)->where('proyek_id', $activity->proyek_id)->first(); - $salary = ($getWorkingHours->standart_rate * $activity->duration) * ($getWorkingHours->max_used / 100); - if($getWorkingHours->uom_standart_rate == "Hour"){ - $salary = ($getWorkingHours->standart_rate * 8) * $activity->duration * ($getWorkingHours->max_used / 100); - } - - $activity->rencana_biaya -= $salary; - if ($activity->rencana_biaya < 0) { - $activity->rencana_biaya = 0; - } - $activity->save(); - }); - - } - -} +activity_id); + $getWorkingHours = UserToProyek::where('user_id', $data->user_id)->where('proyek_id', $activity->proyek_id)->first(); + + $salary = ($getWorkingHours->standart_rate * $activity->duration) * ($getWorkingHours->max_used / 100); + + if($getWorkingHours->uom_standart_rate == "Hour") + $salary = ($getWorkingHours->standart_rate * 8) * $activity->duration * ($getWorkingHours->max_used / 100); + + $activity->rencana_biaya += $salary; + $activity->save(); + }); + + static::deleted(function($data) { + $activity = Activity::find($data->activity_id); + + $getWorkingHours = UserToProyek::where('user_id', $data->user_id)->where('proyek_id', $activity->proyek_id)->first(); + $salary = ($getWorkingHours->standart_rate * $activity->duration) * ($getWorkingHours->max_used / 100); + if($getWorkingHours->uom_standart_rate == "Hour"){ + $salary = ($getWorkingHours->standart_rate * 8) * $activity->duration * ($getWorkingHours->max_used / 100); + } + + $activity->rencana_biaya -= $salary; + if ($activity->rencana_biaya < 0) { + $activity->rencana_biaya = 0; + } + $activity->save(); + }); + + } + +} From 1bb91826ae8976eafb790058690ff5fa418850e3 Mon Sep 17 00:00:00 2001 From: wahyu Date: Mon, 9 Oct 2023 14:30:41 +0700 Subject: [PATCH 11/14] countDays, schedule, hr date --- app/Helpers/MasterFunctionsHelper.php | 20 ++++++++++ app/Http/Controllers/ActivityController.php | 40 +++++++++++++++++-- app/Http/Controllers/ProjectController.php | 16 +------- .../Controllers/UserToActivityController.php | 10 +++-- app/Models/UserToActivity.php | 2 + routes/web.php | 1 + 6 files changed, 67 insertions(+), 22 deletions(-) diff --git a/app/Helpers/MasterFunctionsHelper.php b/app/Helpers/MasterFunctionsHelper.php index 706d734..d681c40 100644 --- a/app/Helpers/MasterFunctionsHelper.php +++ b/app/Helpers/MasterFunctionsHelper.php @@ -36,6 +36,26 @@ class MasterFunctionsHelper return json_decode($output); } + public static function countDays($ganttId, $start, $end){ + $dayOffs = VersionGantt::where('id', $ganttId)->first()->config_dayoff; + $diff = date_diff($start, $end); + $endCount = clone $end; + $duration = $diff->days + 1; + $daysRemaining = $duration; + // Loop until the remaining days become zero + while ($daysRemaining > 0) { + $endCount->modify('-1 day'); + // Check if the current day is a day off (Sunday or Saturday) + $currentDayOfWeek = (int) $endCount->format('w'); + if (strpos($dayOffs, (string) $currentDayOfWeek) !== false) { + // continue; // Skip the day off and continue to the next day + $duration--; + } + $daysRemaining--; // Decrease the remaining days by one + } + return $duration; + } + public function getLatestGantt($id) { $maxGanttId = VersionGantt::where("proyek_id", $id)->max("id"); diff --git a/app/Http/Controllers/ActivityController.php b/app/Http/Controllers/ActivityController.php index 1b43a3e..a516e9e 100644 --- a/app/Http/Controllers/ActivityController.php +++ b/app/Http/Controllers/ActivityController.php @@ -1,6 +1,7 @@ json(['status' => 'success', 'message' => 'Activity Updated!', 'code' => 200], 200); } + public function updateSchedule($ganttId){ + if (empty($ganttId) || !is_int((int) $ganttId)) + return response()->json(['status' => 'failed', 'message' => 'id is required!', 'code' => 400], 400); + + $activities = Activity::where('version_gantt_id', $ganttId)->get(); + if (!$activities) + return response()->json(['status' => 'failed', 'message' => 'Activities not found!', 'code' => 404], 404); + $duration = 0; + foreach ($activities as $key => $activity) { + if (isset($activity->planned_start) && isset($activity->planned_end)) { + $today = new DateTime(); + $endDate = new DateTime($activity->end_date); + $startDate = new DateTime($activity->start_date); + $plannedStart = new DateTime($activity->planned_start); + $plannedEnd = new DateTime($activity->planned_end); + $duration = MasterFunctionsHelper::countDays($ganttId, $plannedStart, $plannedEnd); + if ($today >= $endDate) { + $endDate = $today; + $rerataBobot = $activity->bobot_planning / $duration; + $bobotActual = $activity->progress * $activity->bobot_planning; + $sisaBobot = $activity->bobot_planning - $bobotActual; + $sisaHari = $sisaBobot / $rerataBobot; + $endDate->modify("+".$sisaHari." Days"); + $actualDuration = MasterFunctionsHelper::countDays($ganttId, $startDate, $endDate); + $activity->duration = $actualDuration; + $activity->end_date = $endDate; + $activity->save(); + } + } + } + return response()->json(['status' => 'success', 'message' => 'Activities Updated!', 'code' => 200], 200); + } + public function batchUpdate(Request $request, $ganttId) { $entities = $request->all(); @@ -434,12 +468,12 @@ class ActivityController extends Controller if (isset($entity['data']['target'])) { $this->updateOrder($entity['data']['id'], $entity['data']['target']); } - if(!$activityToUpdate->update($entity['data'])) + if (!$activityToUpdate->update($entity['data'])) return response()->json(['status' => 'failed', 'message' => 'Failed to update activity !', 'code' => 500], 500); $updatedJobsDone = $activityToUpdate->jobs_done; - } else if ($entity['entity'] == "link"){ + } else if ($entity['entity'] == "link") { $linkToUpdate = $link->firstWhere('id', $entity['data']['id']); - if(!$linkToUpdate->update($entity['data'])) + if (!$linkToUpdate->update($entity['data'])) return response()->json(['status' => 'failed', 'message' => 'Failed to update link !', 'code' => 500], 500); } } diff --git a/app/Http/Controllers/ProjectController.php b/app/Http/Controllers/ProjectController.php index 7f5c8f9..75ecec4 100644 --- a/app/Http/Controllers/ProjectController.php +++ b/app/Http/Controllers/ProjectController.php @@ -438,23 +438,9 @@ class ProjectController extends Controller } $activity->start_date = $reports[$i]['min_date']; //same early $activity->end_date = $reports[$i]['max_date']->setTime(23,59,59); // same early - $dayOffs = VersionGantt::where('id', $activity->version_gantt_id)->first()->config_dayoff; $startDate = new DateTime($activity->start_date); $endDate = new DateTime($activity->end_date); - $diff = date_diff($startDate, $endDate); - $duration = $diff->days + 1; - $daysRemaining = $duration; - // Loop until the remaining days become zero - while ($daysRemaining > 0) { - $endDate->modify('-1 day'); - // Check if the current day is a day off (Sunday or Saturday) - $currentDayOfWeek = (int) $endDate->format('w'); - if (strpos($dayOffs, (string) $currentDayOfWeek) !== false) { - // continue; // Skip the day off and continue to the next day - $duration--; - } - $daysRemaining--; // Decrease the remaining days by one - } + $duration = MasterFunctionsHelper::countDays($activity->version_gantt_id, $startDate, $endDate); $activity->duration = $duration; $activity->actual_start = $reports[$i]['min_date']; $activity->save(); diff --git a/app/Http/Controllers/UserToActivityController.php b/app/Http/Controllers/UserToActivityController.php index c422cf6..2c3a658 100644 --- a/app/Http/Controllers/UserToActivityController.php +++ b/app/Http/Controllers/UserToActivityController.php @@ -27,7 +27,7 @@ class UserToActivityController extends Controller public function addMultiple(Request $request){ $users_id = $request->user_id; - $users_role = $request->role_proyek_id; + $users_role = $request->role_proyek_id; // $data = UserToActivity::where("activity_id", $request->activity_id); // if($data){ @@ -42,8 +42,10 @@ class UserToActivityController extends Controller "activity_id" => $request->activity_id, "role_proyek_id" => $users_role[$index], "proyek_id" => $request->proyek_id, - "created_by" => $this->currentName, - "version_gantt_id" => $request->version_gantt_id + "start_date" => $request->start_date, + "end_date" => $request->end_date, + "created_by" => $this->currentName, + "version_gantt_id" => $request->version_gantt_id ); $result = UserToActivity::create($dataInsert); if($result){ @@ -216,7 +218,7 @@ class UserToActivityController extends Controller public function datatables(Request $request){ $id_activity = $request->query('idact'); - $data = UserToActivity::select("assign_hr_to_activity.id as id", "u.name as user_name", "mrp.name as role_name") + $data = UserToActivity::select("assign_hr_to_activity.id as id", "assign_hr_to_activity.start_date", "assign_hr_to_activity.end_date", "u.name as user_name", "mrp.name as role_name") ->join("m_role_proyek as mrp", "mrp.id", "=", "assign_hr_to_activity.role_proyek_id") ->join("m_users as u", "u.id", "=", "assign_hr_to_activity.user_id") ->where('assign_hr_to_activity.activity_id', $id_activity) diff --git a/app/Models/UserToActivity.php b/app/Models/UserToActivity.php index 76f948a..1b795b1 100644 --- a/app/Models/UserToActivity.php +++ b/app/Models/UserToActivity.php @@ -18,6 +18,8 @@ class UserToActivity extends Model 'version_gantt_id', 'role_proyek_id', 'activity_id', + 'start_date', + 'end_date', 'created_at', 'created_by', 'updated_at', diff --git a/routes/web.php b/routes/web.php index bcc0e2b..7f62207 100644 --- a/routes/web.php +++ b/routes/web.php @@ -209,6 +209,7 @@ $router->group(['prefix'=>'api', 'middleware' => 'cors'], function () use ($rout $router->post('/activity/import-update', 'ActivityController@importUpdate'); $router->post('/activity/import-old', 'ActivityController@importOld'); $router->post('/activity/batch-update/{ganttId}', 'ActivityController@batchUpdate'); + $router->get('/activity/update-schedule/{ganttId}', 'ActivityController@updateSchedule'); $router->post('/task', 'ActivityController@add'); $router->get('/task/edit/{id}', 'ActivityController@edit'); $router->put('/task/{id}', 'ActivityController@update'); From d69de496bdea39a0093529fd5dc4291dce504d9c Mon Sep 17 00:00:00 2001 From: wahyu Date: Tue, 10 Oct 2023 09:25:24 +0700 Subject: [PATCH 12/14] fix endline --- .../ReportActivityMaterialController.php | 304 +++++++++--------- 1 file changed, 152 insertions(+), 152 deletions(-) diff --git a/app/Http/Controllers/ReportActivityMaterialController.php b/app/Http/Controllers/ReportActivityMaterialController.php index 86cb638..94edcb4 100644 --- a/app/Http/Controllers/ReportActivityMaterialController.php +++ b/app/Http/Controllers/ReportActivityMaterialController.php @@ -1,152 +1,152 @@ -validate($request, [ - 'activity_id' => 'required', - 'qty' => 'required' - ]); - - $activity = Activity::where('id', $request->activity_id)->first(); - - $start_date = $activity->start_date; - $start_date = substr($start_date, 0, 19); // remove the timezone offset - $startDate = new \DateTime(date("Y-m-d", strtotime($start_date))); - $reportDate = new \DateTime(date("Y-m-d", strtotime($request->report_date))); - - $data = $request->all(); - $data['created_by'] = $this->currentName; - /* $data['assign_material_id'] = AssignMaterial::where('activity_id', $request->activity_id)->pluck('id')->first(); */ - $data['assign_material_id'] = $request->assign_material_id; - $data['qty'] = $this->sanitizeDecimal($data['qty']); - if($reportDate >= $startDate){ - $created = ReportActivityMaterial::create($data); - return response()->json(['status'=>'success','message'=>'Input progress report activity created','code'=>200,'data'=>array('report_id'=>$created->id)]); - } else { - return response()->json(['status'=>'failed','message'=>'Input progress report activity failed created','code'=>400,'data'=>null], 400); - } - } - - public function updateStatusStartFinish(Request $request){ - - $payloadUpdate = array( - 'start_activity' => $request->start_activity, - 'finish_activity' => $request->finish_activity, - 'status_activity' => $request->status_activity - ); - $updateData = AssignMaterial::where('activity_id',$request->activity_id) - ->update($payloadUpdate); - if($updateData){ - if($request->status_activity == 'done'){ - $dataUpdate = array( - "persentase_progress" => 100, - "updated_by" => $this->currentName - ); - Activity::where('id', $request->activity_id)->update($dataUpdate); - } else { - $sumAssignMaterial = AssignMaterial::where('activity_id', $request->activity_id)->sum('qty_planning'); - $sumReportActivityMaterial = ReportActivityMaterial::where('activity_id', $request->activity_id)->sum('qty'); - $dataUpdate = array( - "persentase_progress" => $sumReportActivityMaterial/$sumAssignMaterial*100, - "updated_by" => $this->currentName - ); - Activity::where('id', $request->activity_id)->update($dataUpdate); - } - - return response()->json(['status'=>'success','message'=>'Update data status successfully ', 'code'=>200]); - } - else{ - return response()->json(['status'=>'failed','message'=>'Update data status failed', 'code'=>400]); - } - } - - public function delete($id) - { - if(!ReportActivityMaterial::findOrFail($id)->delete()) - return response()->json(['status'=>'failed','message'=>'Failed to deleted the data!','code'=> 500], 500); - - return response()->json(['status'=>'success','message'=> 'Data deleted!','code'=>200], 200); - } - - public function search(Request $request) - { - $payload = $request->all(); - $dataBuilder = $this->setUpPayload($payload, 'report_activity_material'); - $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 = ReportActivityMaterial::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 Repport Activity material, please try again later!','code'=>400], 400); - } - } - - public function datatables(Request $request){ - $id_activity = $request->query('idAct'); - $type = $request->query('type'); - $materialName = $request->query('materialName'); - if($type == 'plan'){ - $data = AssignMaterial::select("assign_material_to_activity.*","m.description as material_name", "m.uom as uom") - ->join("m_req_material as m", "m.id", "=", "assign_material_to_activity.material_id") - ->where('assign_material_to_activity.activity_id', $id_activity) - ->where('m.description', $materialName) - ->orderBy('assign_material_to_activity.id', 'asc') - ->get(); - return Datatables::of($data) - ->addIndexColumn() - ->addColumn('action', function($row){ - $actionBtn = ''; - if ($row->status_activity != 'done') { - $actionBtn .= ''; - } - return $actionBtn; - }) - ->rawColumns(['action'])->make(true); - }else{ - $data = ReportActivityMaterial::select("report_activity_material.*", "u.name as human_resource", "amta.status_activity") - ->join("assign_material_to_activity as amta", "amta.id", "=", "report_activity_material.assign_material_id") - ->join("m_req_material as m", "m.id", "=", "amta.material_id") - ->join("m_users as u", "u.id", "=", "report_activity_material.user_id") - ->where('report_activity_material.activity_id', $id_activity) - ->where('m.description', $materialName) - ->orderBy('report_activity_material.report_date', 'asc') - ->get(); - return Datatables::of($data) - ->addIndexColumn() - ->addColumn('action', function($row){ - $actionBtn = ''; - if ($row->status_activity != 'done') { - $actionBtn .= ''; - } - return $actionBtn; - }) - ->rawColumns(['action'])->make(true); - } - } -} +validate($request, [ + 'activity_id' => 'required', + 'qty' => 'required' + ]); + + $activity = Activity::where('id', $request->activity_id)->first(); + + $start_date = $activity->start_date; + $start_date = substr($start_date, 0, 19); // remove the timezone offset + $startDate = new \DateTime(date("Y-m-d", strtotime($start_date))); + $reportDate = new \DateTime(date("Y-m-d", strtotime($request->report_date))); + + $data = $request->all(); + $data['created_by'] = $this->currentName; + /* $data['assign_material_id'] = AssignMaterial::where('activity_id', $request->activity_id)->pluck('id')->first(); */ + $data['assign_material_id'] = $request->assign_material_id; + $data['qty'] = $this->sanitizeDecimal($data['qty']); + if($reportDate >= $startDate){ + $created = ReportActivityMaterial::create($data); + return response()->json(['status'=>'success','message'=>'Input progress report activity created','code'=>200,'data'=>array('report_id'=>$created->id)]); + } else { + return response()->json(['status'=>'failed','message'=>'Input progress report activity failed created','code'=>400,'data'=>null], 400); + } + } + + public function updateStatusStartFinish(Request $request){ + + $payloadUpdate = array( + 'start_activity' => $request->start_activity, + 'finish_activity' => $request->finish_activity, + 'status_activity' => $request->status_activity + ); + $updateData = AssignMaterial::where('activity_id',$request->activity_id) + ->update($payloadUpdate); + if($updateData){ + if($request->status_activity == 'done'){ + $dataUpdate = array( + "persentase_progress" => 100, + "updated_by" => $this->currentName + ); + Activity::where('id', $request->activity_id)->update($dataUpdate); + } else { + $sumAssignMaterial = AssignMaterial::where('activity_id', $request->activity_id)->sum('qty_planning'); + $sumReportActivityMaterial = ReportActivityMaterial::where('activity_id', $request->activity_id)->sum('qty'); + $dataUpdate = array( + "persentase_progress" => $sumReportActivityMaterial/$sumAssignMaterial*100, + "updated_by" => $this->currentName + ); + Activity::where('id', $request->activity_id)->update($dataUpdate); + } + + return response()->json(['status'=>'success','message'=>'Update data status successfully ', 'code'=>200]); + } + else{ + return response()->json(['status'=>'failed','message'=>'Update data status failed', 'code'=>400]); + } + } + + public function delete($id) + { + if(!ReportActivityMaterial::findOrFail($id)->delete()) + return response()->json(['status'=>'failed','message'=>'Failed to deleted the data!','code'=> 500], 500); + + return response()->json(['status'=>'success','message'=> 'Data deleted!','code'=>200], 200); + } + + public function search(Request $request) + { + $payload = $request->all(); + $dataBuilder = $this->setUpPayload($payload, 'report_activity_material'); + $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 = ReportActivityMaterial::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 Repport Activity material, please try again later!','code'=>400], 400); + } + } + + public function datatables(Request $request){ + $id_activity = $request->query('idAct'); + $type = $request->query('type'); + $materialName = $request->query('materialName'); + if($type == 'plan'){ + $data = AssignMaterial::select("assign_material_to_activity.*","m.description as material_name", "m.uom as uom") + ->join("m_req_material as m", "m.id", "=", "assign_material_to_activity.material_id") + ->where('assign_material_to_activity.activity_id', $id_activity) + ->where('m.description', $materialName) + ->orderBy('assign_material_to_activity.id', 'asc') + ->get(); + return Datatables::of($data) + ->addIndexColumn() + ->addColumn('action', function($row){ + $actionBtn = ''; + if ($row->status_activity != 'done') { + $actionBtn .= ''; + } + return $actionBtn; + }) + ->rawColumns(['action'])->make(true); + }else{ + $data = ReportActivityMaterial::select("report_activity_material.*", "u.name as human_resource", "amta.status_activity") + ->join("assign_material_to_activity as amta", "amta.id", "=", "report_activity_material.assign_material_id") + ->join("m_req_material as m", "m.id", "=", "amta.material_id") + ->join("m_users as u", "u.id", "=", "report_activity_material.user_id") + ->where('report_activity_material.activity_id', $id_activity) + ->where('m.description', $materialName) + ->orderBy('report_activity_material.report_date', 'asc') + ->get(); + return Datatables::of($data) + ->addIndexColumn() + ->addColumn('action', function($row){ + $actionBtn = ''; + if ($row->status_activity != 'done') { + $actionBtn .= ''; + } + return $actionBtn; + }) + ->rawColumns(['action'])->make(true); + } + } +} From c49cbe48c8a0ead73044fa811b40af5ef206684f Mon Sep 17 00:00:00 2001 From: wahyu Date: Tue, 10 Oct 2023 11:08:31 +0700 Subject: [PATCH 13/14] forcing report date --- app/Http/Controllers/ReportActivityMaterialController.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/Http/Controllers/ReportActivityMaterialController.php b/app/Http/Controllers/ReportActivityMaterialController.php index 94edcb4..eae0d86 100644 --- a/app/Http/Controllers/ReportActivityMaterialController.php +++ b/app/Http/Controllers/ReportActivityMaterialController.php @@ -35,11 +35,11 @@ class ReportActivityMaterialController extends Controller /* $data['assign_material_id'] = AssignMaterial::where('activity_id', $request->activity_id)->pluck('id')->first(); */ $data['assign_material_id'] = $request->assign_material_id; $data['qty'] = $this->sanitizeDecimal($data['qty']); - if($reportDate >= $startDate){ + if($reportDate >= $startDate || isset($data['force']) && $data['force'] == "true"){ $created = ReportActivityMaterial::create($data); return response()->json(['status'=>'success','message'=>'Input progress report activity created','code'=>200,'data'=>array('report_id'=>$created->id)]); } else { - return response()->json(['status'=>'failed','message'=>'Input progress report activity failed created','code'=>400,'data'=>null], 400); + return response()->json(['status'=>'failed','message'=>'Report date is before early start, still wanna add data ?','code'=>400,'data'=>$data], 400); } } From e4b46f6d701a6ee141c0e8c2ed401afc63fa4193 Mon Sep 17 00:00:00 2001 From: wahyu Date: Tue, 10 Oct 2023 13:13:42 +0700 Subject: [PATCH 14/14] handle zero division --- app/Http/Controllers/ActivityController.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/Http/Controllers/ActivityController.php b/app/Http/Controllers/ActivityController.php index a516e9e..50ba368 100644 --- a/app/Http/Controllers/ActivityController.php +++ b/app/Http/Controllers/ActivityController.php @@ -431,10 +431,10 @@ class ActivityController extends Controller $duration = MasterFunctionsHelper::countDays($ganttId, $plannedStart, $plannedEnd); if ($today >= $endDate) { $endDate = $today; - $rerataBobot = $activity->bobot_planning / $duration; + $rerataBobot = $duration > 0 ? $activity->bobot_planning / $duration : 0; $bobotActual = $activity->progress * $activity->bobot_planning; $sisaBobot = $activity->bobot_planning - $bobotActual; - $sisaHari = $sisaBobot / $rerataBobot; + $sisaHari = $rerataBobot > 0 ? $sisaBobot / $rerataBobot : 0; $endDate->modify("+".$sisaHari." Days"); $actualDuration = MasterFunctionsHelper::countDays($ganttId, $startDate, $endDate); $activity->duration = $actualDuration;