Custom Backend OSPRO Surveyor Indonesia
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

352 lines
14 KiB

2 years ago
<?php
namespace App\Http\Controllers;
use Log;
use Illuminate\Http\Request;
use App\Models\Presence;
use GuzzleHttp\Client;
use Illuminate\Support\Facades\Http;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
class PresenceController extends Controller
{
//const NOMINATIM_ADDR = "https://nominatim.oslogdev.com"; // https://nominatim.oslogdev.com/reverse?lat=-6.010201&lon=106.0162715&format=json
public function add(Request $request)
{
$this->validate($request, [
'user_id' => 'required'
]);
//var_dump($request);
if($request->type=="out"){
$clock_out_loc = "-";
// if (isset($request->clock_out_lat) && isset($request->clock_out_lng)) {
// $response = Http::get(config('api.nominatim') . "/reverse?lat=".$request->clock_out_lat."&lon=".$request->clock_out_lng."&format=json");
// $clock_out_loc = $response->json()["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
);
$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);
}
// enable multiple clock out at same day, so I commented this
// else if($resultUpdate=="has presence"){
// return response()->json(['status'=>'failed','message'=>'clock in failed, user has made presence today!','code'=>400], 400);
// }
else{
return response()->json(['status'=>'failed','message'=>'clock out failed please try again!','code'=>400], 400);
}
die();
}
$date = date_create($request->clock_time);
$onlyDate = date_format($date,"Y-m-d");
// enable multiple clock in at same day, so I commented this
// $cek = Presence::where('user_id',$request->user_id)->where("date_presence", $onlyDate)->count();
// if($cek > 0){
// return response()->json(['status'=>'failed','message'=>'clock in failed, user has made presence today!','code'=>400], 400);
// die();
// }
$clock_in_loc = "-";
// if (isset($request->clock_in_lat) && isset($request->clock_in_lng)) {
// $response = Http::get(config('api.nominatim') . "/reverse?lat=".$request->clock_in_lat."&lon=".$request->clock_in_lng."&format=json");
// $clock_in_loc = $response->json()["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
);
$result = Presence::create($dataAdd);
if($result){
return response()->json(['status'=>'success', 'id'=>$result->id,'message'=>'clock in successfully!','code'=>200], 200);
}else{
return response()->json(['status'=>'failed','message'=>'clock in failed!','code'=>400], 400);
}
}
public function edit($id){
if(!$id || (int) $id < 0 || $id==""){
return response()->json(['status'=>'failed','message'=>'id is required!','code'=>400], 400);
die();
}
$result = 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) {
// $client = new Client();
// $res = $client->request('GET', config('api.golang') . "/clockinout/{$id}");
// if ($res->getStatusCode() != 200) {
// return response()->json(['status'=>'failed','message'=>'failed get data clockinout, please try again later!','code'=>400], 400);
// }
// $result= $res->getBody();
// return response($result, 200)
// ->header("content-type", $res->getHeaderLine('content-type'));
// }
public function clockinout($id) {
$dateTimeNow = Carbon::now()->addHour(7);
// ambil data t_clockinout where user_id
$dataPresence = Presence::where('user_id', $id)->orderBy('id', 'DESC')->first();
// ambil data working hour shift
// $dataShift = DB::table('t_users_monthly_shift')->where('user_id', $id)->orderBy('id', 'DESC')->first();
// if($dataShift){
// // cek ke jsonb
// }else{
// $dataWorkingHour = DB::table('m_shift')->where('is_non_shift', 't')->first();
// }
// $dataWorkingHour->start_time;
// $dataWorkingHour->end_time;
// $dataWorkingHour->flex_time_minute;
// if(Carbon::now()->format('H') == ){
// $in_working_time =
// }
if($dataPresence){
// "id": 131,
// "at": "2022-04-13T02:31:44.913587582+07:00",
// "user_id": 330,
// "clock_in": null,
// "clock_out": null,
// "last_clock_in": "2022-04-11T07:44:05+07:00",
// "last_clock_out": "2022-04-11T16:05:01+07:00",
// "start_at": "2022-04-12T04:59:59+07:00",
// "end_at": "2022-04-14T00:59:01+07:00",
// "in_working_time": true
$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;
}
// new \DateTime($minDate)
$res_data = array(
"id" => $dataPresence->id,
// "at"=> Carbon::now()->timezone('Asia/Jakarta'),
"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,
// "start_at"=> "2022-04-12T04=>59=>59+07=>00",
// "end_at"=> "2022-04-14T00=>59=>01+07=>00",
"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,
// "start_at"=> "2022-04-12T04=>59=>59+07=>00",
// "end_at"=> "2022-04-14T00=>59=>01+07=>00",
"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");
// enable multiple clock out at same day, so I commented this
// $cek = Presence::where('user_id', $id)->where("date_presence", $onlyDate)->whereNotNull('clock_out')->count();
// if($cek > 0){
// return "has presence";
// die();
// }
$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 same like in goland API oke
$payload = $request->all();
// second parameter is table name
$dataBuilder = $this->setUpPayload($payload, 't_clock_in_out');
// builder for get data
$builder = $dataBuilder['builder'];
// builder for count data
$countBuilder = $dataBuilder['count'];
// execute builder
$dataGet = $builder->get();
$finalData = [];
// foreach ($dataGet as $objRow) {
// // each clock_in_lat, clock_in_lng request to Address API and push clock_in_location to $dataGet
// // format clock_in_loc (full address / display_name) for now
// $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)) {
// $response = Http::get(self::NOMINATIM_ADDR . "/reverse?lat=".$clock_in_lat."&lon=".$clock_in_lng."&format=json");
// $objRow->clock_in_loc = $response->json()["display_name"];
// }
// // each clock_out_lat, clock_out_lng request to Address API and push clock_out_location to $dataGet
// // format clock_out_loc (full adrress / display_name) for now
// $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)) {
// $response = Http::get(self::NOMINATIM_ADDR . "/reverse?lat=".$clock_out_lat."&lon=".$clock_out_lng."&format=json");
// $objRow->clock_out_loc = $response->json()["display_name"];
// }
// $finalData[] = $objRow;
// }
// execute count builder
$totalRecord = $countBuilder->count();
// dd($totalRecord);
// send response about the result
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);
// each clock_in_lat, clock_in_lng request to Address API and push clock_in_location to $dataGet
// format clock_in_loc (full address / display_name) for now
$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)) {
$response = Http::get(config('api.nominatim') . "/reverse?lat=".$clock_in_lat."&lon=".$clock_in_lng."&format=json");
$objRow->clock_in_loc = $response->json()["display_name"];
}
// each clock_out_lat, clock_out_lng request to Address API and push clock_out_location to $dataGet
// format clock_out_loc (full adrress / display_name) for now
$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)) {
$response = Http::get(config('api.nominatim') . "/reverse?lat=".$clock_out_lat."&lon=".$clock_out_lng."&format=json");
$objRow->clock_out_loc = $response->json()["display_name"];
}
// $finalData[] = $objRow;
$presence->clock_in_loc = $objRow->clock_in_loc;
$presence->clock_out_loc = $objRow->clock_out_loc;
$presence->save();
}
}
}