111 lines
3.6 KiB
PHP
111 lines
3.6 KiB
PHP
<?php
|
|
|
|
namespace App\Controllers;
|
|
|
|
use CodeIgniter\API\ResponseTrait;
|
|
|
|
use App\Controllers\BaseController;
|
|
|
|
class ClaimsUploadController extends BaseController
|
|
{
|
|
use ResponseTrait;
|
|
protected $db;
|
|
public function __construct()
|
|
{
|
|
$this->db = \Config\Database::connect();
|
|
}
|
|
|
|
public function uploadDump()
|
|
{
|
|
$file = $this->request->getFile('file');
|
|
|
|
if (!$file || !$file->isValid()) {
|
|
return $this->respond([
|
|
'status' => 'failed',
|
|
'message' => 'Invalid file'
|
|
], 400);
|
|
}
|
|
|
|
if (!validate_upload_extension($file, UPLOAD_EXT_EXCEL)) {
|
|
return $this->respond([
|
|
'status' => 'failed',
|
|
'message' => 'Only Excel files (xls, xlsx, ods, csv) are allowed.'
|
|
], 400);
|
|
}
|
|
|
|
$uploadFile = storage_file_Upload($file, WRITEPATH . 'uploads/claims_dump/', UPLOAD_EXT_EXCEL);
|
|
if ($uploadFile === '') {
|
|
return $this->respond([
|
|
'status' => 'failed',
|
|
'message' => 'File upload failed'
|
|
], 500);
|
|
}
|
|
|
|
$data = [
|
|
'client_id' => $this->request->getPost('client_id'),
|
|
'tpa_id' => $this->request->getPost('tpa_id'),
|
|
'client_policy_id' => $this->request->getPost('client_policy_id'),
|
|
'from_date' => $this->request->getPost('from_date'),
|
|
'to_date' => $this->request->getPost('to_date'),
|
|
'upload_file' => $uploadFile,
|
|
// 'uploaded_by' => user_id()
|
|
];
|
|
|
|
$this->db->table('claims_dump_uploads')->insert($data);
|
|
$insertId = $this->db->insertID();
|
|
|
|
return $this->respond([
|
|
'status' => 'success',
|
|
'message' => 'Claims dump uploaded',
|
|
'id' => $insertId,
|
|
'upload_file' => $uploadFile,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Download a claims_dump upload by claims_dump_uploads.id (local or S3).
|
|
*/
|
|
public function downloadDump($id = null)
|
|
{
|
|
$id = (int) ($id ?? $this->request->getGet('id'));
|
|
if ($id <= 0) {
|
|
$data['message'] = 'Invalid file id';
|
|
return view('errors/404', $data);
|
|
}
|
|
|
|
$record = $this->db->table('claims_dump_uploads')->where('id', $id)->get()->getRowArray();
|
|
if (empty($record) || empty($record['upload_file'])) {
|
|
$data['message'] = 'File record not found';
|
|
return view('errors/404', $data);
|
|
}
|
|
|
|
$fileName = basename((string) $record['upload_file']);
|
|
$storage = \Config\Services::getFileStorageService();
|
|
$result = $storage->download(WRITEPATH . 'uploads/claims_dump', $fileName);
|
|
|
|
if (! ($result['success'] ?? false)) {
|
|
$data['message'] = 'The Physical File Not Found';
|
|
return view('errors/404', $data);
|
|
}
|
|
|
|
$downloadAs = storage_upload_display_name($fileName);
|
|
if (! empty($result['path']) && is_file($result['path'])) {
|
|
return $this->response->download($result['path'], null)->setFileName($downloadAs);
|
|
}
|
|
if (! empty($result['content'])) {
|
|
return $this->response->download($downloadAs, $result['content'])->setFileName($downloadAs);
|
|
}
|
|
|
|
$data['message'] = 'The Physical File Not Found';
|
|
return view('errors/404', $data);
|
|
}
|
|
|
|
/**
|
|
* Resolve claims_dump file to a local path for any processors (local first, else S3).
|
|
*/
|
|
public function resolveDumpLocalPath(string $fileName): ?string
|
|
{
|
|
return storage_ensure_local_file(WRITEPATH . 'uploads/claims_dump', $fileName);
|
|
}
|
|
}
|