fix bug upload file claim request

This commit is contained in:
korospace
2023-11-03 08:38:22 +07:00
parent 186312bb6a
commit 16449bd18b
3 changed files with 130 additions and 76 deletions

View File

@@ -51,86 +51,133 @@ class ClaimRequestController extends Controller
$code = $this->getNextCode();
$member = Member::find($member_id);
$newClaimRequest = ClaimRequestService::storeClaimRequest(
row: [],
code: $code,
member: $member,
paymentType: 'reimbursement',
serviceCode: $request->service_code[$key],
);
ClaimRequested::dispatch($newClaimRequest);
// Log History
$newClaimRequest->histories()->create([
'title' => 'New Claim Requested',
'description' => "Claim Requested for Member : {$member->member_id} - ({$member->full_name})",
'type' => 'info',
'system_origin' => 'client-portal'
]);
DB::beginTransaction();
// Claim Log
DB::table('claim_logs')
->insert([
'claim_request_id' => $newClaimRequest->id,
'status' => 'requested',
'date' => date('Y-m-d H:i:s'),
'description' => "Claim Requested for Member : {$member->member_id} - ({$member->full_name})",
'system_origin' => 'hospital-portal',
'created_by' => auth()->user()->id,
'created_at' => date('Y-m-d H:i:s'),
'updated_at'=> date('Y-m-d H:i:s'),
]);
try {
$newClaimRequest = ClaimRequestService::storeClaimRequest(
row: [],
code: $code,
member: $member,
paymentType: 'reimbursement',
serviceCode: $request->service_code[$key],
);
if ($request->hasFile('laboratorium')) {
foreach ($request->laboratorium[$key] as $file) {
$pathFile = File::storeFile('claim-result', $newClaimRequest->id, $file);
$newClaimRequest->files()->updateOrCreate([
'type' => 'claim-result',
'name' => File::getFileName('claim-result', $newClaimRequest->id, $file),
'original_name' => $file->getClientOriginalName(),
'extension' => $file->getClientOriginalExtension(),
'path' => $pathFile,
'created_by' => auth()->user()->id,
'updated_by' => auth()->user()->id,
]);
ClaimRequested::dispatch($newClaimRequest);
// Log History
$newClaimRequest->histories()->create([
'title' => 'New Claim Requested',
'description' => "Claim Requested for Member : {$member->member_id} - ({$member->full_name})",
'type' => 'info',
'system_origin' => 'client-portal'
]);
// Claim Log
DB::table('claim_logs')
->insert([
'claim_request_id' => $newClaimRequest->id,
'status' => 'requested',
'date' => date('Y-m-d H:i:s'),
'description' => "Claim Requested for Member : {$member->member_id} - ({$member->full_name})",
'system_origin' => 'hospital-portal',
'created_by' => auth()->user()->id,
'created_at' => date('Y-m-d H:i:s'),
'updated_at'=> date('Y-m-d H:i:s'),
]);
$storage_path = storage_path() . "/app/public";
$folder = "claim/";
if (is_dir($storage_path . "/" . $folder) == false) {
mkdir($storage_path . "/" . $folder, 0770, true);
}
if (isset($_FILES['laboratorium'])) {
foreach ($_FILES['laboratorium']['error']["member_" .$member_id] as $key => $value) {
if ($value == 0) {
$new_file_name = "claim-result-" . time() . "-" . uniqid();
$ekstension = "." . explode("/", $_FILES['laboratorium']['type']["member_" .$member_id][$key])[1];
$pathFile = $folder . $new_file_name . $ekstension;
$tmp_name = $_FILES['laboratorium']['tmp_name']["member_" .$member_id][$key];
$full_path = $_FILES['laboratorium']['full_path']["member_" .$member_id][$key];
if (move_uploaded_file($tmp_name, $storage_path . "/" . $pathFile)) {
$newClaimRequest->files()->updateOrCreate([
'type' => 'claim-result',
'name' => $new_file_name,
'original_name' => $full_path,
'extension' => $ekstension,
'path' => $pathFile,
'created_by' => auth()->user()->id,
'updated_by' => auth()->user()->id,
]);
}
}
}
}
if (isset($_FILES['prescription'])) {
foreach ($_FILES['prescription']['error']["member_" .$member_id] as $key => $value) {
if ($value == 0) {
$new_file_name = "claim-diagnosis-" . time() . "-" . uniqid();
$ekstension = "." . explode("/", $_FILES['prescription']['type']["member_" .$member_id][$key])[1];
$pathFile = $folder . $new_file_name . $ekstension;
$tmp_name = $_FILES['prescription']['tmp_name']["member_" .$member_id][$key];
$full_path = $_FILES['prescription']['full_path']["member_" .$member_id][$key];
if (move_uploaded_file($tmp_name, $storage_path . "/" . $pathFile)) {
$newClaimRequest->files()->updateOrCreate([
'type' => 'claim-diagnosis',
'name' => $new_file_name,
'original_name' => $full_path,
'extension' => $ekstension,
'path' => $pathFile,
'created_by' => auth()->user()->id,
'updated_by' => auth()->user()->id,
]);
}
}
}
}
if (isset($_FILES['invoice'])) {
foreach ($_FILES['invoice']['error']["member_" .$member_id] as $key => $value) {
if ($value == 0) {
$new_file_name = "claim-kondisi-" . time() . "-" . uniqid();
$ekstension = "." . explode("/", $_FILES['invoice']['type']["member_" .$member_id][$key])[1];
$pathFile = $folder . $new_file_name . $ekstension;
$tmp_name = $_FILES['invoice']['tmp_name']["member_" .$member_id][$key];
$full_path = $_FILES['invoice']['full_path']["member_" .$member_id][$key];
if (move_uploaded_file($tmp_name, $storage_path . "/" . $pathFile)) {
$newClaimRequest->files()->updateOrCreate([
'type' => 'claim-kondisi',
'name' => $new_file_name,
'original_name' => $full_path,
'extension' => $ekstension,
'path' => $pathFile,
'created_by' => auth()->user()->id,
'updated_by' => auth()->user()->id,
]);
}
}
}
}
DB::commit();
}
catch (\Throwable $th) {
DB::rollBack();
if ($request->hasFile('prescription')) {
foreach ($request->prescription[$key] as $file) {
$pathFile = File::storeFile('claim-diagnosis', $newClaimRequest->id, $file);
$newClaimRequest->files()->updateOrCreate([
'type' => 'claim-diagnosis',
'name' => File::getFileName('claim-diagnosis', $newClaimRequest->id, $file),
'original_name' => $file->getClientOriginalName(),
'extension' => $file->getClientOriginalExtension(),
'path' => $pathFile,
'created_by' => auth()->user()->id,
'updated_by' => auth()->user()->id,
]);
}
}
if ($request->hasFile('invoice')) {
foreach ($request->invoice[$key] as $file) {
$pathFile = File::storeFile('claim-kondisi', $newClaimRequest->id, $file);
$newClaimRequest->files()->updateOrCreate([
'type' => 'claim-kondisi',
'name' => File::getFileName('claim-kondisi', $newClaimRequest->id, $file),
'original_name' => $file->getClientOriginalName(),
'extension' => $file->getClientOriginalExtension(),
'path' => $pathFile,
'created_by' => auth()->user()->id,
'updated_by' => auth()->user()->id,
]);
}
return Helper::responseJson(status: 'failed', statusCode: 500, message: $th->getMessage());
}
}
}
return Helper::responseJson(data: $request->toArray(), message: 'Claim Request berhasil ajukan!');
}