Browse Source

clean codes & move logics to model

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

108
app/Http/Controllers/UserToActivityController.php

@ -4,10 +4,7 @@ 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
@ -17,37 +14,20 @@ class UserToActivityController extends Controller
'activity_id' => 'required', 'activity_id' => 'required',
'user_id' => 'required' 'user_id' => 'required'
]); ]);
$data = $request->all(); $data = $request->all();
$data['created_by'] = $this->currentName; $data['created_by'] = $this->currentName;
$result = UserToActivity::create($data); if(!UserToActivity::create($data))
if($result){ return response()->json(['status'=>'failed','message'=>'Failed to add data!','code'=> 500]);
$updateActivity = Activity::find($request->activity_id);
$getWorkingHours = UserToProyek::where('user_id', $request->user_id)->first(); return response()->json(['status'=>'success','message'=>'Data added!', 'code'=> 200]);
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();
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){ public function addMultiple(Request $request){
$users_id = $request->user_id; $users_id = $request->user_id;
$data = UserToActivity::where("activity_id", $request->activity_id); $data = UserToActivity::where("activity_id", $request->activity_id);
if($data){ if($data){
$data->delete(); $data->delete();
} }
@ -55,7 +35,6 @@ class UserToActivityController extends Controller
if(is_array($users_id) && count($users_id) > 0){ if(is_array($users_id) && count($users_id) > 0){
$countRes = 0; $countRes = 0;
foreach($users_id as $item){ foreach($users_id as $item){
$dataInsert = array( $dataInsert = array(
"user_id" => $item, "user_id" => $item,
"activity_id" => $request->activity_id, "activity_id" => $request->activity_id,
@ -63,9 +42,7 @@ class UserToActivityController extends Controller
"proyek_id" => $request->proyek_id, "proyek_id" => $request->proyek_id,
"created_by" => $this->currentName "created_by" => $this->currentName
); );
$result = UserToActivity::create($dataInsert); $result = UserToActivity::create($dataInsert);
if($result){ if($result){
$countRes++; $countRes++;
}else{ }else{
@ -73,7 +50,9 @@ class UserToActivityController extends Controller
} }
} }
if($countRes > 0){ if($countRes == 0)
return response()->json(['status'=>'failed' ,'message'=>'Failed to add data!','code'=> 500]);
$allCost = $this->calculateAllCost($request->activity_id, $request->proyek_id); $allCost = $this->calculateAllCost($request->activity_id, $request->proyek_id);
$dataUpdateCost = array( $dataUpdateCost = array(
"rencana_biaya"=>$allCost, "rencana_biaya"=>$allCost,
@ -86,74 +65,38 @@ class UserToActivityController extends Controller
$this->updatedCostPlanning($actUpdate->parent_id); $this->updatedCostPlanning($actUpdate->parent_id);
} }
} }
return response()->json(['status'=>'success' ,'message'=>'Data added!','code'=>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]);
}
}else{ }else{
return response()->json(['status'=>'success' ,'message'=>'user to activity gantt Project successfull created','code'=>200]); return response()->json(['status'=>'success' ,'message'=>'Data added!','code'=>200]);
} }
} }
public function update(Request $request, $id){ public function update(Request $request, $id){
if(empty($id) || !is_int((int)$id))
if(!$id || (int) $id < 0 || $id==""){
return response()->json(['status'=>'failed','message'=>'id is required!','code'=>400], 400); return response()->json(['status'=>'failed','message'=>'id is required!','code'=>400], 400);
}
$data = UserToActivity::find($id); $data = UserToActivity::find($id);
if($data){ if(!$data)
$result = $data->update($request->all()); return response()->json(['status'=>'failed','message'=>'Data not found!','code'=> 404], 404);
}else{
return response()->json(['status'=>'failed','message'=>'data user to activity gantt Project not found!','code'=>400], 400);
die();
}
if($result){ if(!$data->update($request->all()));
return response()->json(['status'=>'success','message'=>'user to activity gantt Project successfully updated!','code'=>200], 200); return response()->json(['status'=>'failed','message'=>'Failed to update!','code'=> 500], 500);
}else{
return response()->json(['status'=>'failed','message'=>'user to activity gantt Project failed updated!','code'=>400], 400); return response()->json(['status'=>'success','message'=>'Data updated!','code'=>200], 200);
}
} }
public function delete($id) public function delete($id)
{ {
$data = UserToActivity::find($id); $data = UserToActivity::find($id);
if($data){ 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); return response()->json(['status'=>'failed','message'=>'data user to activity gantt Project not found!','code'=>400], 400);
die();
}
if($data->delete()){
if($delete){ return response()->json(['status'=>'success','message'=>'Data deleted!','code'=>200], 200);
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) return response()->json(['status'=>'failed','message'=>'Failed to delete!','code'=> 500], 500);
{
$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){ public function edit($id){
@ -164,11 +107,10 @@ class UserToActivityController extends Controller
$result = UserToActivity::find($id); $result = UserToActivity::find($id);
if($result){ 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); 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) public function search(Request $request)

Loading…
Cancel
Save