diff --git a/app/Http/Controllers/PresenceController.php b/app/Http/Controllers/PresenceController.php index 192abea..a2664b4 100644 --- a/app/Http/Controllers/PresenceController.php +++ b/app/Http/Controllers/PresenceController.php @@ -15,14 +15,17 @@ class PresenceController extends Controller { public function add(Request $request) { - - // - // $test = $this->checkLocation($request); - // return response()->json(['status'=>$test,'code'=>200], 200); $this->validate($request, [ 'user_id' => 'required' ]); - + + $checkLocation = $this->checkLocation($request); + $statusBoundary = false; + $date = date_create($request->clock_time); + // $ActivityId = null; + if(count($checkLocation) > 0){ + $statusBoundary = true; + } if($request->type=="out"){ @@ -32,12 +35,31 @@ class PresenceController extends Controller "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_loc" => $clock_out_loc, + "clock_out_boundary" => $statusBoundary ); $resultUpdate = $this->updateFormAdd($dataUpdate, $request->user_id); if($resultUpdate && $resultUpdate > 0){ - return response()->json(['status'=>'success', 'id'=>$resultUpdate,'message'=>'clock out success!','code'=>200], 200); + 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); @@ -45,35 +67,46 @@ class PresenceController extends Controller die(); } - $date = date_create($request->clock_time); $onlyDate = date_format($date,"Y-m-d"); - $clock_in_loc = "-"; $dataAdd = array( 'user_id'=> $request->user_id, 'clock_in'=> $request->clock_time, 'date_presence'=> $onlyDate, - 'created_by' =>$this->currentName, + '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_loc' => $clock_in_loc, + 'clock_in_boundary' => $statusBoundary ); $result = Presence::create($dataAdd); - $data=array( 'id' => $result->id, - 'boundary' => true + 'boundary' => $statusBoundary ); - - if($result){ + 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); } } + private function checkLocation($params){ // cek user tersebut apakah punya assign task yang ada bondary nya // geom ada di table activity @@ -82,12 +115,27 @@ class PresenceController extends Controller ->where("ahta.user_id", $params->user_id) ->whereDate("ma.start_date", "<=", $params->clock_time) ->whereDate("ma.end_date", ">=", $params->clock_time) - ->where(DB::raw("ST_Intersects(ST_GeomFromGeoJSON('ma.geom'), ST_GeomFromText('POINT(".$params->clock_out_lng." ".$params->clock_out_lat.")', 4326))")) ->get(); - // Activity::select('geom')->where(''); - // gejson to geom - // intersect dengan point req - return $geom; + $temp = []; + foreach($geom as $dataGeom){ + $valGeom = json_decode($dataGeom->geom); + if($params->type=="out"){ + $check = DB::select(DB::raw("SELECT ST_Intersects(ST_GeomFromGeoJSON('".json_encode($valGeom->geometry)."'), ST_GeomFromText('POINT(".$params->clock_out_lng." ".$params->clock_out_lat.")', 4326)) as boundary")); + }else{ + $check = DB::select(DB::raw("SELECT ST_Intersects(ST_GeomFromGeoJSON('".json_encode($valGeom->geometry)."'), ST_GeomFromText('POINT(".$params->clock_in_lng." ".$params->clock_in_lat.")', 4326)) as boundary")); + } + foreach ($check as $key) { + if($key->boundary){ + $temp[]=array( + "activity_id" => $dataGeom->id, + "boundary" => $key->boundary + ); + } + } + + + } + return $temp; } public function edit($id){ @@ -151,7 +199,6 @@ class PresenceController extends Controller } private function updateFormAdd($data, $id){ - $date = date_create($data['clock_out']); $onlyDate = date_format($date,"Y-m-d"); diff --git a/app/Models/Presence.php b/app/Models/Presence.php index e5276cc..620a91e 100644 --- a/app/Models/Presence.php +++ b/app/Models/Presence.php @@ -3,6 +3,7 @@ namespace App\Models; use Illuminate\Database\Eloquent\Model; +use Illuminate\Support\Facades\DB; class Presence extends Model { @@ -14,7 +15,7 @@ class Presence extends Model protected $fillable = [ 'user_id', 'clock_in', 'clock_out', 'date_presence', 'clock_in_lat', 'clock_in_lng', 'clock_out_lat', 'clock_out_lng', - 'clock_in_loc', 'clock_out_loc', + 'clock_in_loc', 'clock_out_loc','clock_in_boundary', 'clock_out_boundary', 'created_at', 'created_by', 'updated_at', 'updated_by' ]; }