Browse Source

clean codes & move logics to model

pull/3/head
Muhammad Sulaiman Yusuf 3 years ago
parent
commit
49d0ee7497
  1. 344
      app/Http/Controllers/UserToActivityController.php

344
app/Http/Controllers/UserToActivityController.php

@ -4,209 +4,151 @@ namespace App\Http\Controllers;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use App\Models\UserToActivity; use App\Models\UserToActivity;
use App\Models\ReportActivity;
use App\Models\Activity; use App\Models\Activity;
use App\Models\Image;
use App\Models\UserToProyek;
use Datatables; use Datatables;
class UserToActivityController extends Controller class UserToActivityController extends Controller
{ {
public function add(Request $request){ public function add(Request $request){
$this->validate($request, [ $this->validate($request, [
'activity_id' => 'required', 'activity_id' => 'required',
'user_id' => 'required' 'user_id' => 'required'
]); ]);
$data = $request->all();
$data['created_by'] = $this->currentName; $data = $request->all();
$data['created_by'] = $this->currentName;
$result = UserToActivity::create($data);
if($result){ if(!UserToActivity::create($data))
$updateActivity = Activity::find($request->activity_id); return response()->json(['status'=>'failed','message'=>'Failed to add data!','code'=> 500]);
$getWorkingHours = UserToProyek::where('user_id', $request->user_id)->first();
if($updateActivity && $getWorkingHours){ return response()->json(['status'=>'success','message'=>'Data added!', 'code'=> 200]);
$costPlanOld = $updateActivity==null? 0 : (int)$updateActivity->rencana_biaya; }
if($getWorkingHours->uom_standart_rate == "Day"){
$costUser = $getWorkingHours->standart_rate * $updateActivity->duration; public function addMultiple(Request $request){
}else if($getWorkingHours->uom_standart_rate == "Hour"){ $users_id = $request->user_id;
$costUser = ($getWorkingHours->standart_rate * 8) * $updateActivity->duration; // assumption standart working is 8 hours
} $data = UserToActivity::where("activity_id", $request->activity_id);
$costPlanNew = $costPlanOld + $costUser; if($data){
$updateActivity->rencana_biaya = $costPlanNew; $data->delete();
$updateActivity->save(); }
return response()->json(['status'=>'success','message'=>'user to activity gantt Project successfull created, and cost plant successfull update '.$costPlanNew,'code'=>200]);
}else{ if(is_array($users_id) && count($users_id) > 0){
return response()->json(['status'=>'success','message'=>'user to activity gantt Project successfull created','code'=>200]); $countRes = 0;
} foreach($users_id as $item){
}else{ $dataInsert = array(
return response()->json(['status'=>'failed','message'=>'user to activity gantt Project failed created','code'=>400]); "user_id" => $item,
} "activity_id" => $request->activity_id,
} "role_proyek_id" => $request->role_proyek_id,
"proyek_id" => $request->proyek_id,
public function addMultiple(Request $request){ "created_by" => $this->currentName
$users_id = $request->user_id; );
$result = UserToActivity::create($dataInsert);
$data = UserToActivity::where("activity_id", $request->activity_id); if($result){
$countRes++;
if($data){ }else{
$data->delete(); $countRes--;
} }
}
if(is_array($users_id) && count($users_id) > 0){
$countRes = 0; if($countRes == 0)
foreach($users_id as $item){ return response()->json(['status'=>'failed' ,'message'=>'Failed to add data!','code'=> 500]);
$dataInsert = array( $allCost = $this->calculateAllCost($request->activity_id, $request->proyek_id);
"user_id" => $item, $dataUpdateCost = array(
"activity_id" => $request->activity_id, "rencana_biaya"=>$allCost,
"role_proyek_id" => $request->role_proyek_id, "updated_by"=> $this->currentName
"proyek_id" => $request->proyek_id, );
"created_by" => $this->currentName $actUpdate = Activity::find($request->activity_id);
); if($actUpdate){
$actUpdate->update($dataUpdateCost);
$result = UserToActivity::create($dataInsert); if($actUpdate->parent_id){
$this->updatedCostPlanning($actUpdate->parent_id);
if($result){ }
$countRes++; }
}else{ return response()->json(['status'=>'success' ,'message'=>'Data added!','code'=>200]);
$countRes--; }else{
} return response()->json(['status'=>'success' ,'message'=>'Data added!','code'=>200]);
} }
if($countRes > 0){ }
$allCost = $this->calculateAllCost($request->activity_id, $request->proyek_id);
$dataUpdateCost = array( public function update(Request $request, $id){
"rencana_biaya"=>$allCost, if(empty($id) || !is_int((int)$id))
"updated_by"=> $this->currentName return response()->json(['status'=>'failed','message'=>'id is required!','code'=>400], 400);
);
$actUpdate = Activity::find($request->activity_id); $data = UserToActivity::find($id);
if($actUpdate){ if(!$data)
$actUpdate->update($dataUpdateCost); return response()->json(['status'=>'failed','message'=>'Data not found!','code'=> 404], 404);
if($actUpdate->parent_id){
$this->updatedCostPlanning($actUpdate->parent_id); 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);
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]); public function delete($id)
} {
}else{ $data = UserToActivity::find($id);
return response()->json(['status'=>'success' ,'message'=>'user to activity gantt Project successfull created','code'=>200]); 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);
public function update(Request $request, $id){ }
if(!$id || (int) $id < 0 || $id==""){ return response()->json(['status'=>'failed','message'=>'Failed to delete!','code'=> 500], 500);
return response()->json(['status'=>'failed','message'=>'id is required!','code'=>400], 400); }
}
$data = UserToActivity::find($id); public function edit($id){
if($data){ if(!$id || (int) $id < 0 || $id==""){
$result = $data->update($request->all()); return response()->json(['status'=>'failed','message'=>'id is required!','code'=>400], 400);
}else{ die();
return response()->json(['status'=>'failed','message'=>'data user to activity gantt Project not found!','code'=>400], 400); }
die();
} $result = UserToActivity::find($id);
if($result){ if(!$result)
return response()->json(['status'=>'success','message'=>'user to activity gantt Project successfully updated!','code'=>200], 200); return response()->json(['status'=>'failed','message'=>'failed get data tools resource, please try again later!','code'=>400], 400);
}else{
return response()->json(['status'=>'failed','message'=>'user to activity gantt Project failed updated!','code'=>400], 400); return response()->json(['status'=>'success','code'=>200,'data'=>$result], 200);
} }
}
public function search(Request $request)
public function delete($id) {
{ $payload = $request->all();
$data = UserToActivity::find($id); $dataBuilder = $this->setUpPayload($payload, 'assign_hr_to_activity');
if($data){ $builder = $dataBuilder['builder'];
$userId = $data->user_id; $countBuilder = $dataBuilder['count'];
$activityId = $data->activity_id; $dataGet = $builder->get();
$delete = $data->delete(); $totalRecord = $countBuilder->count();
if($delete){ return response()->json(['status'=>'success','code'=>200,'data'=>$dataGet, 'totalRecord'=>$totalRecord], 200);
$this->deleteRelative($activityId, $userId); }
}
}else{ public function list()
return response()->json(['status'=>'failed','message'=>'data user to activity gantt Project not found!','code'=>400], 400); {
die(); $data = UserToActivity::all();
} $countData = $data->count();
if($data){
if($delete){ return response()->json(['status'=>'success','code'=>200,'data'=>$data, 'totalRecord'=>$countData], 200);
return response()->json(['status'=>'success','message'=>'user to activity gantt Project successfully deleted!','code'=>200], 200); }else{
}else{ return response()->json(['status'=>'failed','message'=>'failed get list user to activity gantt, please try again later!','code'=>400], 400);
return response()->json(['status'=>'failed','message'=>'user to activity gantt Project failed deleted!','code'=>400], 400); }
} }
}
public function datatables(Request $request){
private function deleteRelative($id, $userId) $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")
$dataRa = ReportActivity::where("activity_id", $id)->where("user_id", $userId)->get(); ->join("m_role_proyek as mrp", "mrp.id", "=", "assign_hr_to_activity.role_proyek_id")
foreach ($dataRa as $ra) { ->join("m_users as u", "u.id", "=", "assign_hr_to_activity.user_id")
$image = Image::where("ref_id", $ra->id)->where("category", "report_activity")->get(); ->where('assign_hr_to_activity.activity_id', $id_activity)
foreach ($image as $value) { ->get();
if(file_exists($this->pathImage.$value->image)){ return Datatables::of($data)
unlink($this->pathImage.$value->image); ->addIndexColumn()
} ->addColumn('action', function($row){
} $actionBtn = '<a href="javascript:void(0)" data-id="'.$row->id.'" class="delete btn btn-danger btn-sm btn-hr-delete"><i class="fa fa-trash"></i></a>';
return $actionBtn;
Image::where("ref_id", $ra->id)->where("category", "report_activity")->delete(); })
} ->rawColumns(['action'])->make(true);
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 = $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 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 = '<a href="javascript:void(0)" data-id="'.$row->id.'" class="delete btn btn-danger btn-sm btn-hr-delete"><i class="fa fa-trash"></i></a>';
return $actionBtn;
})
->rawColumns(['action'])->make(true);
}
} }

Loading…
Cancel
Save