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 App\Models\UserToActivity;
use App\Models\ReportActivity;
use App\Models\Activity;
use App\Models\Image;
use App\Models\UserToProyek;
use Datatables;
class UserToActivityController extends Controller
@ -17,37 +14,20 @@ class UserToActivityController extends Controller
'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();
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]);
}
if(!UserToActivity::create($data))
return response()->json(['status'=>'failed','message'=>'Failed to add data!','code'=> 500]);
return response()->json(['status'=>'success','message'=>'Data added!', 'code'=> 200]);
}
public function addMultiple(Request $request){
$users_id = $request->user_id;
$data = UserToActivity::where("activity_id", $request->activity_id);
if($data){
$data->delete();
}
@ -55,7 +35,6 @@ class UserToActivityController extends Controller
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,
@ -63,9 +42,7 @@ class UserToActivityController extends Controller
"proyek_id" => $request->proyek_id,
"created_by" => $this->currentName
);
$result = UserToActivity::create($dataInsert);
if($result){
$countRes++;
}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);
$dataUpdateCost = array(
"rencana_biaya"=>$allCost,
@ -86,74 +65,38 @@ class UserToActivityController extends Controller
$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]);
}
return response()->json(['status'=>'success' ,'message'=>'Data added!','code'=>200]);
}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){
if(!$id || (int) $id < 0 || $id==""){
if(empty($id) || !is_int((int)$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(!$data)
return response()->json(['status'=>'failed','message'=>'Data not found!','code'=> 404], 404);
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);
}
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);
}
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{
if(!$data)
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);
}
if($data->delete()){
return response()->json(['status'=>'success','message'=>'Data deleted!','code'=>200], 200);
}
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();
return response()->json(['status'=>'failed','message'=>'Failed to delete!','code'=> 500], 500);
}
public function edit($id){
@ -164,11 +107,10 @@ class UserToActivityController extends Controller
$result = UserToActivity::find($id);
if($result){
return response()->json(['status'=>'success','code'=>200,'data'=>$result], 200);
}else{
if(!$result)
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)

Loading…
Cancel
Save