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.
219 lines
8.0 KiB
219 lines
8.0 KiB
<?php |
|
|
|
namespace App\Http\Controllers; |
|
|
|
use App\Models\Image; |
|
use App\Models\Company; |
|
use App\Models\UserToProyek; |
|
use Illuminate\Http\Request; |
|
use App\Models\ReportActivity; |
|
use App\Models\UserToActivity; |
|
|
|
class UserToProyekController extends Controller |
|
{ |
|
public function add(Request $request) |
|
{ |
|
$this->validate($request, [ |
|
'user_id' => 'required', |
|
'proyek_id' => 'required' |
|
]); |
|
|
|
$data = $request->all(); |
|
|
|
$data['created_by'] = $this->currentName; |
|
|
|
$result = UserToProyek::create($data); |
|
|
|
if ($result) { |
|
return response()->json(['status' => 'success', 'message' => 'assign hr to proyek successfully!', 'code' => 200], 200); |
|
} else { |
|
return response()->json(['status' => 'failed', 'message' => 'assign hr to proyek failed!', 'code' => 400], 400); |
|
} |
|
} |
|
|
|
public function addMultiple(Request $request) |
|
{ |
|
$user_IDs = $request->user_id; |
|
|
|
$data = UserToProyek::where([ |
|
["proyek_id", $request->proyek_id], |
|
['is_customer', true] |
|
]); |
|
|
|
if ($data) { |
|
$data->delete(); |
|
} |
|
|
|
if (is_array($user_IDs) && count($user_IDs) > 0) { |
|
$countRes = 0; |
|
foreach ($user_IDs as $item) { |
|
|
|
$dataInsert = array( |
|
"user_id" => $item, |
|
"proyek_id" => $request->proyek_id, |
|
"created_by" => $this->currentName, |
|
'is_customer' => true, |
|
'project_role' => $request->project_role |
|
); |
|
|
|
$result = UserToProyek::create($dataInsert); |
|
|
|
if ($result) { |
|
$countRes++; |
|
} else { |
|
$countRes--; |
|
} |
|
} |
|
if ($countRes > 0) { |
|
return response()->json(['status' => 'success', 'message' => 'Project customer successfull created', 'code' => 200]); |
|
} else { |
|
return response()->json(['status' => 'failed', 'message' => 'Project customer failed created', 'code' => 400]); |
|
} |
|
} else { |
|
return response()->json(['status' => 'failed', 'message' => 'Project customer failed created', 'code' => 200]); |
|
} |
|
} |
|
|
|
public function edit($id) |
|
{ |
|
if (!$id || (int) $id < 0 || $id == "") { |
|
return response()->json(['status' => 'failed', 'message' => 'id is required!', 'code' => 400], 400); |
|
die(); |
|
} |
|
|
|
$result = UserToProyek::find($id); |
|
|
|
if ($result) { |
|
return response()->json(['status' => 'success', 'code' => 200, 'data' => $result], 200); |
|
} else { |
|
return response()->json(['status' => 'failed', 'message' => 'failed get data assign hr to proyek, please try again later!', 'code' => 400], 400); |
|
} |
|
} |
|
|
|
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 = UserToProyek::find($id); |
|
|
|
if ($data) { |
|
$result = $data->update($request->all()); |
|
} else { |
|
return response()->json(['status' => 'failed', 'message' => 'data assign hr to proyek not found!', 'code' => 400], 400); |
|
die(); |
|
} |
|
|
|
|
|
if ($result) { |
|
return response()->json(['status' => 'success', 'message' => 'data assign hr to proyek successfully updated!', 'code' => 200], 200); |
|
} else { |
|
return response()->json(['status' => 'failed', 'message' => 'data assign hr to proyek failed updated!', 'code' => 400], 400); |
|
} |
|
} |
|
|
|
public function delete($id, $company_id) |
|
{ |
|
$data = UserToProyek::find($id); |
|
if ($data) { |
|
$userId = $data->user_id; |
|
$proyekId = $data->proyek_id; |
|
$delete = $data->delete(); |
|
if ($delete) { |
|
$this->deleteRelative($proyekId, $userId, $company_id); |
|
} |
|
} else { |
|
return response()->json(['status' => 'failed', 'message' => 'data assign hr to proyek not found!', 'code' => 400], 400); |
|
die(); |
|
} |
|
|
|
|
|
if ($delete) { |
|
return response()->json(['status' => 'success', 'message' => 'data assign hr to proyek successfully deleted!', 'code' => 200], 200); |
|
} else { |
|
return response()->json(['status' => 'failed', 'message' => 'data assign hr to proyek failed deleted!', 'code' => 400], 400); |
|
} |
|
} |
|
|
|
private function deleteRelative($proyekId, $userId, $company_id) |
|
{ |
|
$uta = UserToActivity::where("proyek_id", $proyekId)->where("user_id", $userId)->get(); |
|
foreach ($uta as $value) { |
|
$dataRa = ReportActivity::where("activity_id", $value->activity_id)->where("user_id", $value->user_id)->get(); |
|
$company = Company::find($company_id); |
|
if ($company) { |
|
$destinationPath = $this->setCustomeDirectoryUpload($company['company_name']); |
|
foreach ($dataRa as $ra) { |
|
$images = Image::where("ref_id", $ra->id)->where("category", "report_activity")->get(); |
|
foreach ($images as $image) { |
|
if (file_exists($destinationPath['pathImage'] . $image->image)) { |
|
unlink($destinationPath['pathImage'] . $image->image); |
|
} |
|
} |
|
Image::where("ref_id", $ra->id)->where("category", "report_activity")->delete(); |
|
} |
|
ReportActivity::where("activity_id", $value->activity_id)->where("user_id", $value->user_id)->delete(); |
|
} |
|
} |
|
UserToActivity::where("proyek_id", $proyekId)->where("user_id", $userId)->delete(); |
|
} |
|
|
|
public function search(Request $request) |
|
{ |
|
$payload = $request->all(); |
|
$dataBuilder = $this->setUpPayload($payload, 'assign_hr_to_proyek'); |
|
$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 = UserToProyek::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 assign hr to proyek, please try again later!', 'code' => 400], 400); |
|
} |
|
} |
|
|
|
public function select(Request $request) |
|
{ |
|
|
|
$search = $request->query('search'); |
|
|
|
$idProyek = (int)$request->query('idProyek'); |
|
$idActivity = (int)$request->query('idact'); |
|
|
|
$include = []; |
|
|
|
if ($idActivity && $idActivity > 0) { |
|
$dataF = UserToActivity::select("user_id")->where("activity_id", $idActivity)->get(); |
|
foreach ($dataF as $val) { |
|
$include[] = $val->user_id; |
|
} |
|
} |
|
|
|
if ($search && !empty($search)) { |
|
$data = UserToProyek::select("m_users.id as id", "m_users.name as name")->join('m_users', 'm_users.id', '=', 'assign_hr_to_proyek.user_id') |
|
->where("assign_hr_to_proyek.proyek_id", $idProyek)->where("m_users.name", 'like', '%' . $search . '%')->whereIn("m_users.id", $include)->get(); |
|
} else { |
|
$data = UserToProyek::select("m_users.id as id", "m_users.name as name")->where("assign_hr_to_proyek.proyek_id", $idProyek)->join('m_users', 'm_users.id', '=', 'assign_hr_to_proyek.user_id')->whereIn("m_users.id", $include)->get(); |
|
} |
|
|
|
if ($data instanceof \Illuminate\Database\Eloquent\Collection) { |
|
$pm = array( |
|
"id" => $this->currentId, |
|
"name" => "Project Manager" |
|
); |
|
$data->push($pm); |
|
} |
|
|
|
return response()->json($data); |
|
} |
|
}
|
|
|