FIX_UPDATEAPI_ENDOSEMENTPATHAPI
This commit is contained in:
parent
b1ce730687
commit
4872cd17c1
@ -268,19 +268,32 @@ class EndorsementController extends ResourceController
|
||||
if (!$endorsement) { return $this->respond([ 'status' => 'failed', 'code' => 404,'data' => 'Endorsement not found' ], 404); }
|
||||
|
||||
// FILE UPLOAD
|
||||
$uploadedCompletionFile = $endorsement['endorsement_file_name'];
|
||||
$uploadedOriginalCompletionFile = $endorsement['endorsement_file_name'];
|
||||
|
||||
$uploadFile = $this->request->getFile('endorsement_file_name');
|
||||
$uploadOriginalFile = $this->request->getFile('endorsement_file_name');
|
||||
|
||||
if ($uploadFile && $uploadFile->isValid()) {
|
||||
if ($uploadOriginalFile && $uploadOriginalFile->isValid()) {
|
||||
|
||||
$uploadPath = WRITEPATH . 'uploads/endorsement/';
|
||||
if (!is_dir($uploadPath)) {
|
||||
mkdir($uploadPath, 0777, true);
|
||||
$uploadOriginalPath = WRITEPATH . 'uploads/endorsement/';
|
||||
if (!is_dir($uploadOriginalPath)) {
|
||||
mkdir($uploadOriginalPath, 0777, true);
|
||||
}
|
||||
|
||||
$uploadedCompletionFile = time() . '_' . $uploadFile->getRandomName();
|
||||
$uploadFile->move($uploadPath, $uploadedCompletionFile);
|
||||
$uploadedOriginalCompletionFile = time() . '_' . $uploadOriginalFile->getRandomName();
|
||||
$uploadOriginalFile->move($uploadOriginalPath, $uploadedOriginalCompletionFile); // ✅ fixed variable
|
||||
}
|
||||
|
||||
// FILE REVISED UPLOAD
|
||||
$uploadedRevisedCompletionFile = $endorsement['endorsement_completion_file'];
|
||||
$uploadRevisedFile = $this->request->getFile('endorsement_completion_file');
|
||||
|
||||
if ($uploadRevisedFile && $uploadRevisedFile->isValid()) {
|
||||
$uploadRevisedPath = WRITEPATH . 'uploads/endorsement/endorsement_pdf/';
|
||||
if (!is_dir($uploadRevisedPath)) {
|
||||
mkdir($uploadRevisedPath, 0777, true);
|
||||
}
|
||||
$uploadedRevisedCompletionFile = time() . '_' . $uploadRevisedFile->getRandomName();
|
||||
$uploadRevisedFile->move($uploadRevisedPath, $uploadedRevisedCompletionFile);
|
||||
}
|
||||
|
||||
//COMMON UPDATE FIELDS
|
||||
@ -320,10 +333,10 @@ class EndorsementController extends ResourceController
|
||||
$updateData['pending_days'] = $reqData['pending_days'];
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Always update file (either old or new)
|
||||
$updateData['endorsement_file_name'] = $uploadedCompletionFile;
|
||||
|
||||
// Always update both files (new or existing)
|
||||
$updateData['endorsement_file_name'] = $uploadedOriginalCompletionFile;
|
||||
$updateData['endorsement_completion_file'] = $uploadedRevisedCompletionFile; // ✅ fixed variable
|
||||
|
||||
// External Policy Editable Fields
|
||||
if ($endorsement['policy_from'] === 'External') {
|
||||
@ -623,7 +636,7 @@ class EndorsementController extends ResourceController
|
||||
}
|
||||
}
|
||||
|
||||
public function EndorsementFilePath()
|
||||
public function EndorsementFilePathGOWTHAM()
|
||||
{
|
||||
try {
|
||||
$endorsementId = $this->request->getGet('endorsement_id');
|
||||
@ -664,6 +677,57 @@ class EndorsementController extends ResourceController
|
||||
}
|
||||
}
|
||||
|
||||
public function EndorsementFilePath()
|
||||
{
|
||||
try {
|
||||
$endorsementId = $this->request->getGet('endorsement_id');
|
||||
|
||||
if (!$endorsementId) {
|
||||
return $this->respond(['status' => 'failed', 'code' => 400, 'data' => 'endorsement_id is required'], 200);
|
||||
}
|
||||
|
||||
// Fetch record from DB
|
||||
$fileRecord = $this->EndorsementModel->where('is_active', 1)->find((int)$endorsementId);
|
||||
|
||||
if (!$fileRecord) {
|
||||
return $this->respond(['status' => 'failed', 'code' => 404, 'data' => 'File not found in database'], 200);
|
||||
}
|
||||
|
||||
// ✅ STEP 1: Check completion file first
|
||||
$completionFile = $fileRecord['endorsement_completion_file'] ?? null;
|
||||
$originalFile = $fileRecord['endorsement_file_name'] ?? null;
|
||||
|
||||
if (!empty($completionFile)) {
|
||||
// ✅ Use completion file path
|
||||
$fileName = $completionFile;
|
||||
$filePath = WRITEPATH . 'uploads/endorsement/endorsement_pdf/' . $fileName;
|
||||
} elseif (!empty($originalFile)) {
|
||||
// ✅ Fallback to original file path
|
||||
$fileName = $originalFile;
|
||||
$filePath = WRITEPATH . 'uploads/endorsement/' . $fileName;
|
||||
} else {
|
||||
// ❌ Neither file exists in DB
|
||||
return $this->respond(['status' => 'failed', 'code' => 404, 'data' => 'No file uploaded yet'], 200);
|
||||
}
|
||||
|
||||
// ✅ STEP 2: Check file exists on disk
|
||||
if (!file_exists($filePath)) {
|
||||
return $this->respond(['status' => 'failed', 'code' => 404, 'data' => 'File missing on server'], 200);
|
||||
}
|
||||
|
||||
// ✅ STEP 3: Stream file to browser / Flutter
|
||||
$mime = mime_content_type($filePath);
|
||||
|
||||
return $this->response
|
||||
->setHeader('Content-Type', $mime)
|
||||
->setHeader('Content-Disposition', 'inline; filename="' . $fileName . '"')
|
||||
->setBody(file_get_contents($filePath));
|
||||
|
||||
} catch (\Exception $e) {
|
||||
return $this->respond(['status' => 'failed', 'code' => 500, 'message' => $e->getMessage()], 500);
|
||||
}
|
||||
}
|
||||
|
||||
public function uploadEndorsementFile()
|
||||
{
|
||||
try {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user