validate($request, [ 'activity_id' => 'required', 'user_id' => 'required' ]); $data = $request->all(); $data['created_by'] = $this->currentName; $result = UserToActivity::create($data); if($result){ $updateActivity = Activity::find($request->activity_id); $getWorkingHours = UserToProyek::where('user_id', $request->user_id)->first(); if($updateActivity && $getWorkingHours){ $costPlanOld = $updateActivity==null? 0 : (int)$updateActivity->rencana_biaya; if($getWorkingHours->uom_standart_rate == "Day"){ $costUser = $getWorkingHours->standart_rate * $updateActivity->duration; }else if($getWorkingHours->uom_standart_rate == "Hour"){ $costUser = ($getWorkingHours->standart_rate * 8) * $updateActivity->duration; // assumption standart working is 8 hours } $costPlanNew = $costPlanOld + $costUser; $updateActivity->rencana_biaya = $costPlanNew; $updateActivity->save(); // UserToProyek::where('user_id', $request->user_id)->update(['rencana_biaya' => $costPlanNew]); return response()->json(['status'=>'success','message'=>'user to activity gantt Project successfull created, and cost plant successfull update '.$costPlanNew,'code'=>200]); }else{ return response()->json(['status'=>'success','message'=>'user to activity gantt Project successfull created','code'=>200]); } }else{ return response()->json(['status'=>'failed','message'=>'user to activity gantt Project failed created','code'=>400]); } } public function addMultiple(Request $request){ $users_id = $request->user_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 $item){ $dataInsert = array( "user_id" => $item, "activity_id" => $request->activity_id, "role_proyek_id" => $request->role_proyek_id, "proyek_id" => $request->proyek_id, "created_by" => $this->currentName ); $result = UserToActivity::create($dataInsert); if($result){ $countRes++; }else{ $countRes--; } } if($countRes > 0){ $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'=>'user to activity gantt Project successfull created','code'=>200]); }else{ return response()->json(['status'=>'success' ,'message'=>'user to activity gantt Project failed created','code'=>400]); } }else{ return response()->json(['status'=>'success' ,'message'=>'user to activity gantt Project successfull created','code'=>200]); } } 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 = UserToActivity::find($id); if($data){ $result = $data->update($request->all()); }else{ return response()->json(['status'=>'failed','message'=>'data user to activity gantt Project not found!','code'=>400], 400); die(); } if($result){ return response()->json(['status'=>'success','message'=>'user to activity gantt Project successfully updated!','code'=>200], 200); }else{ return response()->json(['status'=>'failed','message'=>'user to activity gantt Project failed updated!','code'=>400], 400); } } public function delete($id) { $data = UserToActivity::find($id); if($data){ $userId = $data->user_id; $activityId = $data->activity_id; $delete = $data->delete(); if($delete){ $this->deleteRelative($activityId, $userId); } }else{ return response()->json(['status'=>'failed','message'=>'data user to activity gantt Project not found!','code'=>400], 400); die(); } if($delete){ return response()->json(['status'=>'success','message'=>'user to activity gantt Project successfully deleted!','code'=>200], 200); }else{ return response()->json(['status'=>'failed','message'=>'user to activity gantt Project failed deleted!','code'=>400], 400); } } private function deleteRelative($id, $userId) { $dataRa = ReportActivity::where("activity_id", $id)->where("user_id", $userId)->get(); foreach ($dataRa as $ra) { $image = Image::where("ref_id", $ra->id)->where("category", "report_activity")->get(); foreach ($image as $value) { if(file_exists($this->pathImage.$value->image)){ unlink($this->pathImage.$value->image); } } Image::where("ref_id", $ra->id)->where("category", "report_activity")->delete(); } ReportActivity::where("activity_id", $id)->where("user_id", $userId)->delete(); } 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'=>'success','code'=>200,'data'=>$result], 200); }else{ return response()->json(['status'=>'failed','message'=>'failed get data tools resource, please try again later!','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, 'assign_hr_to_activity'); // builder for get data $builder = $dataBuilder['builder']; // builder for count data $countBuilder = $dataBuilder['count']; // execute builder $dataGet = $builder->get(); // execute count builder $totalRecord = $countBuilder->count(); // send response about the result 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 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); } }