Merge branch 'main' of bitbucket.org:jubilian/nhance_partner_be
This commit is contained in:
commit
ffafcd8448
1
.gitignore
vendored
1
.gitignore
vendored
@ -16,6 +16,7 @@
|
||||
/writable/uploads/enquiry/rc/*
|
||||
/writable/uploads/quotation/*
|
||||
/writable/uploads/policy/policy_pdf/*
|
||||
/writable/uploads/policy/policy_md/*
|
||||
/writable/uploads/policy/policy_payment_receipt/*
|
||||
|
||||
|
||||
|
||||
@ -150,6 +150,7 @@ $routes->group('api', ['filter' => ["jwtAuth:1,2,3,4,agent","appSignature"] ], f
|
||||
$routes->get('claim/ClaimList', 'ClaimController::ClaimList');
|
||||
$routes->get('claim/getClaimById', 'ClaimController::getClaimById');
|
||||
$routes->get('claim/downloadClaimFile', 'ClaimController::downloadClaimFile');
|
||||
$routes->get('claim/deleteClaim', 'ClaimController::deleteClaim');
|
||||
$routes->post('claim/createClaim', 'ClaimController::createClaim');
|
||||
$routes->post('claim/updateClaim', 'ClaimController::updateClaim');
|
||||
|
||||
|
||||
@ -284,6 +284,7 @@ class ClaimController extends ResourceController
|
||||
'workshop_surveyor_contact',
|
||||
'remark',
|
||||
'is_active',
|
||||
'policy_from',
|
||||
];
|
||||
|
||||
foreach ($allowedUpdateFields as $field) {
|
||||
@ -316,6 +317,12 @@ class ClaimController extends ResourceController
|
||||
$updateData['updated_by'] = $reqData['updated_by'];
|
||||
}
|
||||
|
||||
if($reqData['policy_from'] === 'External') {
|
||||
$updateData['reg_no'] = $reqData['reg_no'] ?? null;
|
||||
$updateData['claim_number'] = $reqData['claim_number'] ?? null;
|
||||
$updateData['policy_no'] = $reqData['policy_number'] ?? null;
|
||||
}
|
||||
|
||||
if (!empty($updateData) && !$this->ClaimModel->update((int) $claimId, $updateData)) {
|
||||
return $this->respond(['status' => 'failed', 'code' => 422, 'data' => $this->ClaimModel->errors()], 422);
|
||||
}
|
||||
@ -344,6 +351,63 @@ class ClaimController extends ResourceController
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Soft delete claim by id (sets is_active = 0).
|
||||
* Also soft deletes related claim files.
|
||||
*/
|
||||
public function deleteClaim()
|
||||
{
|
||||
try {
|
||||
$claimId = $this->request->getGet('id');
|
||||
|
||||
if (empty($claimId)) {
|
||||
return $this->respond(['status' => 'failed', 'code' => 400, 'data' => 'id is required'], 400);
|
||||
}
|
||||
|
||||
$claim = $this->ClaimModel
|
||||
->where('id', (int) $claimId)
|
||||
->where('is_active', 1)
|
||||
->first();
|
||||
|
||||
if (!$claim) {
|
||||
return $this->respond(['status' => 'failed', 'code' => 404, 'data' => 'Claim not found'], 404);
|
||||
}
|
||||
|
||||
$updatedBy = $this->request->getGet('updated_by');
|
||||
|
||||
$this->db->transStart();
|
||||
|
||||
$this->ClaimModel->update((int) $claimId, [
|
||||
'is_active' => 0,
|
||||
'updated_by' => $updatedBy ?? null,
|
||||
]);
|
||||
|
||||
$this->ClaimFilesModel
|
||||
->where('claim_id', (int) $claimId)
|
||||
->where('is_active', 1)
|
||||
->set([
|
||||
'is_active' => 0,
|
||||
'updated_by' => $updatedBy ?? null,
|
||||
])
|
||||
->update();
|
||||
|
||||
if ($this->db->transStatus() === false) {
|
||||
$this->db->transRollback();
|
||||
return $this->respond(['status' => 'failed', 'code' => 500, 'data' => 'Failed to delete claim'], 500);
|
||||
}
|
||||
|
||||
$this->db->transComplete();
|
||||
|
||||
return $this->respond([
|
||||
'status' => 'success',
|
||||
'code' => 200,
|
||||
'data' => 'Claim deleted successfully',
|
||||
], 200);
|
||||
} catch (\Exception $e) {
|
||||
return $this->respond(['status' => 'failed', 'code' => 500, 'data' => $e->getMessage()], 500);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch a single claim by primary id with related lookups and file preview URLs.
|
||||
* Used to populate the edit form when the user clicks the edit button on a row.
|
||||
|
||||
@ -694,6 +694,14 @@ class PolicyController extends ResourceController
|
||||
}
|
||||
}
|
||||
|
||||
public function readFile()
|
||||
{
|
||||
$readDoc = $this->checkPolicyDoc(15862);
|
||||
|
||||
dd($readDoc);
|
||||
|
||||
}
|
||||
|
||||
public function readFileAndCalculateCommission(array $data)
|
||||
{
|
||||
$policyId = $data['policy_id'];
|
||||
@ -702,6 +710,8 @@ class PolicyController extends ResourceController
|
||||
|
||||
$readDoc = $this->checkPolicyDoc($policyId);
|
||||
|
||||
|
||||
|
||||
if($readDoc['status'] == 'success')
|
||||
{
|
||||
|
||||
@ -807,7 +817,7 @@ class PolicyController extends ResourceController
|
||||
|
||||
try
|
||||
{
|
||||
|
||||
helper('policy_pdf_helper');
|
||||
|
||||
// Replace with your actual Gemini API key
|
||||
$apiKey = getenv('GEMINI_API_KEY');
|
||||
@ -819,33 +829,57 @@ class PolicyController extends ResourceController
|
||||
|
||||
$record = $this->PolicyModel->find((int)$policyId);
|
||||
$uploadedPath = WRITEPATH . 'uploads/policy/policy_pdf/';
|
||||
$filePath = $uploadedPath.$record['policy_pdf_file_name'];
|
||||
// dd($filePath);
|
||||
// Check if the file exists
|
||||
if (!file_exists($filePath)) {
|
||||
log_message('info',"Error: File not found at {$filePath}");
|
||||
$mdUploadedPath = WRITEPATH . 'uploads/policy/policy_md/';
|
||||
$pdfFilePath = $uploadedPath . $record['policy_pdf_file_name'];
|
||||
$mdFileName = preg_replace('/\.pdf$/i', '.md', $record['policy_pdf_file_name']);
|
||||
$mdFilePath = $mdUploadedPath . $mdFileName;
|
||||
|
||||
if (!file_exists($pdfFilePath)) {
|
||||
log_message('info',"Error: File not found at {$pdfFilePath}");
|
||||
if($return == true)
|
||||
{
|
||||
return $this->respond(['status'=>"failed", 'message'=> "File not found at {$filePath}"], 200);
|
||||
return $this->respond(['status'=>"failed", 'message'=> "File not found at {$pdfFilePath}"], 200);
|
||||
}else{
|
||||
return ['status'=>"failed", 'message'=> "File not found at {$filePath}"];
|
||||
return ['status'=>"failed", 'message'=> "File not found at {$pdfFilePath}"];
|
||||
}
|
||||
}
|
||||
|
||||
$markdownResult = convert_policy_pdf_to_markdown($pdfFilePath);
|
||||
if ($markdownResult['status'] !== 'success') {
|
||||
log_message('info', 'Policy PDF to markdown conversion failed: ' . $markdownResult['message']);
|
||||
if ($return == true) {
|
||||
return $this->respond(['status' => 'failed', 'message' => $markdownResult['message']], 200);
|
||||
}
|
||||
|
||||
return ['status' => 'failed', 'message' => $markdownResult['message']];
|
||||
}
|
||||
|
||||
if (!file_exists($mdFilePath)) {
|
||||
log_message('info',"Error: Markdown file not found at {$mdFilePath}");
|
||||
if($return == true)
|
||||
{
|
||||
return $this->respond(['status'=>"failed", 'message'=> "Markdown file not found at {$mdFilePath}"], 200);
|
||||
}else{
|
||||
return ['status'=>"failed", 'message'=> "Markdown file not found at {$mdFilePath}"];
|
||||
}
|
||||
}
|
||||
|
||||
$filePath = $mdFilePath;
|
||||
|
||||
// Get the file's MIME type using the finfo extension
|
||||
$finfo = finfo_open(FILEINFO_MIME_TYPE);
|
||||
$mimeType = finfo_file($finfo, $filePath);
|
||||
finfo_close($finfo);
|
||||
|
||||
// Define supported inline MIME types
|
||||
$supportedInlineMimeTypes = ['application/pdf','text/csv'];
|
||||
// Gemini inline supports text/plain reliably for markdown files
|
||||
if (!in_array($mimeType, ['text/plain', 'text/markdown', 'text/x-markdown'], true)) {
|
||||
$mimeType = 'text/plain';
|
||||
}
|
||||
|
||||
// Read the file content and encode it to Base64
|
||||
$fileContent = file_get_contents($filePath);
|
||||
$base64Content = base64_encode($fileContent);
|
||||
// The prompt you want to send to the model
|
||||
// $prompt = "give me a json with emp data like name,age,dob,mobile and email. only json not any explanations";
|
||||
$prompt = "Read the following motor policy pdf document and convert into JSON format as sample specified. Give me only JSON,not any explanations.while pick up premium break up give own damage, third party ,personal accident and taxes as mentioned in JSON format.Notes:vehicle_type should be like Two Wheeler,Four Feeler,Good Vehicle,Bus, Tractor,Commercial Vehicle.rto_state_code is first two char of reg_no and rto_city_code 3rd & 4th char of reg_no and it should be two digit numeric code. Any date should be in mysql date format";
|
||||
// Define supported inline MIME types
|
||||
$supportedInlineMimeTypes = ['application/pdf', 'text/csv', 'text/plain', 'text/markdown', 'text/x-markdown'];
|
||||
|
||||
$prompt = "Read the following motor policy document and convert into JSON format as sample specified. Give me only JSON,not any explanations.while pick up premium break up give own damage, third party ,personal accident and taxes as mentioned in JSON format.Notes:vehicle_type should be like Two Wheeler,Four Feeler,Good Vehicle,Bus, Tractor,Commercial Vehicle.rto_state_code is first two char of reg_no and rto_city_code 3rd & 4th char of reg_no and it should be two digit numeric code. Any date should be in mysql date format.(note : while fetching TP premium do not add PA AMOUNT)";
|
||||
$prompt .= '"{\"policy\":{\"intermediary_name\":\"\",\"policy_number\":\"\",\"issue_date\":\"\",\"period\":{\"start\":\"\",\"end\":\"\"},\"insurer\":\"\",\"previous_policy_number\":\"\"},\"insured\":{\"name\":\"\",\"father_name\":\"\",\"address\":[\"\"],\"mobile\":\"\",\"id_proofs\":{\"aadhaar\":\"\",\"pan\":\"\"}},\"vehicle\":{\"reg_no\":\"\",\"rto_state_code\":\"\",\"rto_city_code\":\"\",\"weight\":\"\",\"fuel_type\":\"\",\"date_of_registration\":\"\",\"year_of_manufacture\":\"\",\"engine_no\":\"\",\"chassis_no\":\"\",\"make\":\"\",\"model\":\"\",\"year\":\"\",\"cubic_capacity\":\"\",\"vehicle_type\":\"\"},\"rto\":\"\",\"premium\":{\"tp\":0,\"od\":0,\"pa\":0,\"taxes\":{\"cgst\":\"\",\"sgst\":\"\",\"igst\":\"\"},\"total\":0,\"in_words\":\"\"},\"endorsements\":[{\"code\":\"\",\"desc\":\"\"}]}"';
|
||||
|
||||
$payloadPart = null;
|
||||
@ -854,10 +888,21 @@ class PolicyController extends ResourceController
|
||||
$text_part = [
|
||||
"text" => $prompt
|
||||
];
|
||||
|
||||
// Conditionally handle the file upload based on MIME type
|
||||
if (in_array($mimeType, $supportedInlineMimeTypes)) {
|
||||
// Handle PDF as inline data
|
||||
// Read saved markdown file from policy_md directory
|
||||
$fileContent = file_get_contents($filePath);
|
||||
|
||||
if ($fileContent === false || trim($fileContent) === '') {
|
||||
log_message('info', "Markdown file is empty at {$filePath}");
|
||||
if ($return == true) {
|
||||
return $this->respond(['status' => 'failed', 'message' => "Markdown file is empty at {$filePath}"], 200);
|
||||
}
|
||||
|
||||
return ['status' => 'failed', 'message' => "Markdown file is empty at {$filePath}"];
|
||||
}
|
||||
|
||||
$base64Content = base64_encode($fileContent);
|
||||
$payloadPart = [
|
||||
"inlineData" => [
|
||||
@ -872,33 +917,15 @@ class PolicyController extends ResourceController
|
||||
$payloadPart
|
||||
]
|
||||
];
|
||||
}
|
||||
// else {
|
||||
// // Handle Excel/CSV using the Files API
|
||||
// // echo "Detected unsupported inline MIME type ({$mimeType}). Uploading via Files API...\n";
|
||||
// $fileInfo = $this->uploadFileToGemini($filePath, $apiKey); // Pass apiKey
|
||||
|
||||
// // print_r($fileInfo);
|
||||
// // The file part, using the URI from the Files API upload
|
||||
// $file_part = [
|
||||
// "fileData" => [
|
||||
// "fileUri" => $fileInfo['uri'],
|
||||
// "mimeType" => $fileInfo['mimeType']
|
||||
// ]
|
||||
// ];
|
||||
|
||||
// // The full content array, containing both parts
|
||||
// $content_parts = [
|
||||
// "parts" => [
|
||||
// $text_part,
|
||||
// $file_part
|
||||
// ]
|
||||
// ];
|
||||
// }
|
||||
|
||||
|
||||
|
||||
|
||||
} else {
|
||||
log_message('info', "Unsupported inline MIME type ({$mimeType}) for markdown file.");
|
||||
if($return == true)
|
||||
{
|
||||
return $this->respond(['status'=>"failed", 'message'=> "Unsupported inline MIME type ({$mimeType})"], 200);
|
||||
}else{
|
||||
return ['status'=>"failed", 'message'=> "Unsupported inline MIME type ({$mimeType})"];
|
||||
}
|
||||
}
|
||||
|
||||
// The final JSON payload
|
||||
$data = [
|
||||
@ -1814,7 +1841,7 @@ class PolicyController extends ResourceController
|
||||
. 'ins.short_name AS insurer_short_name, ipti.insurance_plan_type AS insurance_plan_type, '
|
||||
. 'pp.policy_number AS policy_no, pp.premium_amount AS premium, '
|
||||
. 'pp.commission_amount, pp.insured_name, pe.reg_no, pp.weight, pp.fuel_type, '
|
||||
. 'pp.vehicle_type, pp.issued_date'
|
||||
. 'pp.vehicle_type, pp.issued_date, pp.od, pp.tp, pp.pa, pp.cgst, pp.sgst, pp.igst'
|
||||
);
|
||||
$builder->select(
|
||||
'COALESCE((SELECT GROUP_CONCAT(piu.utr_no ORDER BY piu.id SEPARATOR ", ") '
|
||||
@ -1982,9 +2009,10 @@ class PolicyController extends ResourceController
|
||||
'Q3' => 'A+B (NET)',
|
||||
'R3' => 'S.TAX',
|
||||
'S3' => 'TOTAL',
|
||||
'T3' => "PAYMENT MODE",
|
||||
'U3' => 'UTR NO',
|
||||
'V3' => 'UTR DATE',
|
||||
'T3' => 'PAYOUT',
|
||||
'U3' => "PAYMENT MODE",
|
||||
'V3' => 'UTR NO',
|
||||
'W3' => 'UTR DATE',
|
||||
];
|
||||
|
||||
$headerNumberCols = ['A', 'O', 'P', 'Q', 'R', 'S', 'I'];
|
||||
@ -2008,8 +2036,17 @@ class PolicyController extends ResourceController
|
||||
$fuelType = $row['fuel_type'] ?? '';
|
||||
$vehicleType = $row['vehicle_type'] ?? '';
|
||||
$weight = $row['weight'] ?? '';
|
||||
$commissionAmt = (float) ($row['commission_amount'] ?? 0);
|
||||
$commissionAmt = (float) ($row['commission_amount'] ?? 0); // payout amount
|
||||
$premium = (float) ($row['premium'] ?? 0);
|
||||
$od = (float) ($row['od'] ?? 0);
|
||||
$tp = (float) ($row['tp'] ?? 0);
|
||||
$pa = (float) ($row['pa'] ?? 0);
|
||||
$cgst = (float) ($row['cgst'] ?? 0);
|
||||
$sgst = (float) ($row['sgst'] ?? 0);
|
||||
$igst = (float) ($row['igst'] ?? 0);
|
||||
$tpWithPa = $tp + $pa;
|
||||
$netAmount = $od + $tp + $pa;
|
||||
$serviceTax = $cgst + $sgst + $igst;
|
||||
$utrNo = $row['utr_no'] ?? '';
|
||||
$utrDate = $row['utr_date'] ?? '';
|
||||
$payoutStatus = ($row['received_or_not'] ?? '') === 'yes' ? 'LINK' : 'PENDING';
|
||||
@ -2029,14 +2066,15 @@ class PolicyController extends ResourceController
|
||||
'L' => isset($row['issued_date']) ? date('d-m-Y', strtotime($row['issued_date'])) : '',
|
||||
'M' => '', // Company / insurer – add to query if available
|
||||
'N' => '', // Make & Variant – add if available
|
||||
'O' => 0, // A (OD) – extend query if split premium available
|
||||
'P' => $premium, // B (TP) – using full premium; adjust if split
|
||||
'Q' => $premium, // A+B Net
|
||||
'R' => 0, // S.TAX – add if available
|
||||
'S' => $commissionAmt, // Commission as TOTAL
|
||||
'T' => $payoutStatus,
|
||||
'U' => $utrNo,
|
||||
'V' => $utrDate,
|
||||
'O' => $od, // A (OD) => od
|
||||
'P' => $tpWithPa, // B (TP) => tp+pa
|
||||
'Q' => $netAmount, // A+B Net => od+tp+pa
|
||||
'R' => $serviceTax, // S.TAX => cgst+igst+sgst
|
||||
'S' => $premium, // TOTAL => premium amount
|
||||
'T' => $commissionAmt, // PAYOUT => payout amount
|
||||
'U' => $payoutStatus,
|
||||
'V' => $utrNo,
|
||||
'W' => $utrDate,
|
||||
];
|
||||
|
||||
$dataNumberCols = ['A', 'I', 'O', 'P', 'Q', 'R', 'S'];
|
||||
|
||||
196
app/Helpers/policy_pdf_helper.php
Normal file
196
app/Helpers/policy_pdf_helper.php
Normal file
@ -0,0 +1,196 @@
|
||||
<?php
|
||||
|
||||
if (!function_exists('policy_md_upload_path')) {
|
||||
/**
|
||||
* Full path to the saved markdown file for a policy PDF file name.
|
||||
*/
|
||||
function policy_md_upload_path(string $pdfFileName): string
|
||||
{
|
||||
$mdFileName = preg_replace('/\.pdf$/i', '.md', $pdfFileName);
|
||||
|
||||
if ($mdFileName === $pdfFileName) {
|
||||
$mdFileName .= '.md';
|
||||
}
|
||||
|
||||
return WRITEPATH . 'uploads/policy/policy_md/' . $mdFileName;
|
||||
}
|
||||
}
|
||||
|
||||
if (!function_exists('policy_pdf_md_path')) {
|
||||
/**
|
||||
* Resolve markdown path using the same base name as the PDF file.
|
||||
* e.g. 1764999905_abc.pdf -> policy_md/1764999905_abc.md
|
||||
*/
|
||||
function policy_pdf_md_path(string $pdfPath): string
|
||||
{
|
||||
return policy_md_upload_path(basename($pdfPath));
|
||||
}
|
||||
}
|
||||
|
||||
if (!function_exists('policy_md_is_valid')) {
|
||||
/**
|
||||
* Validate that markdown content looks like extracted policy text.
|
||||
*/
|
||||
function policy_md_is_valid(string $content): bool
|
||||
{
|
||||
$content = trim($content);
|
||||
|
||||
if ($content === '' || strlen($content) < 500) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$errorMarkers = [
|
||||
'pdftotext:',
|
||||
'GLIBCXX_',
|
||||
'CXXABI_',
|
||||
'no version information available',
|
||||
];
|
||||
|
||||
foreach ($errorMarkers as $marker) {
|
||||
if (stripos($content, $marker) !== false) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
if (!function_exists('policy_pdf_extract_text')) {
|
||||
/**
|
||||
* Extract plain text from a PDF using pdftotext (poppler).
|
||||
* Uses a clean environment so LAMPP lib paths do not break pdftotext.
|
||||
*/
|
||||
function policy_pdf_extract_text(string $pdfPath): string
|
||||
{
|
||||
$pdftotext = getenv('PDFTOTEXT_BIN') ?: '/usr/bin/pdftotext';
|
||||
|
||||
if (!is_executable($pdftotext) && $pdftotext === '/usr/bin/pdftotext') {
|
||||
$which = trim((string) shell_exec('command -v pdftotext 2>/dev/null'));
|
||||
if ($which !== '') {
|
||||
$pdftotext = $which;
|
||||
}
|
||||
}
|
||||
|
||||
$command = sprintf(
|
||||
'env -i PATH=/usr/bin:/bin LD_LIBRARY_PATH= %s -layout %s - 2>&1',
|
||||
escapeshellcmd($pdftotext),
|
||||
escapeshellarg($pdfPath)
|
||||
);
|
||||
|
||||
$text = shell_exec($command);
|
||||
|
||||
if (!is_string($text)) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$text = trim($text);
|
||||
|
||||
if (!policy_md_is_valid($text)) {
|
||||
log_message('error', 'PDF text extraction failed or returned invalid content for: ' . $pdfPath);
|
||||
|
||||
return '';
|
||||
}
|
||||
|
||||
return $text;
|
||||
}
|
||||
}
|
||||
|
||||
if (!function_exists('convert_policy_pdf_to_markdown')) {
|
||||
/**
|
||||
* Convert a policy PDF to markdown, save to policy_md/, then read back from disk.
|
||||
*
|
||||
* @return array{status: string, message: string, content?: string, md_path?: string, md_file_name?: string}
|
||||
*/
|
||||
function convert_policy_pdf_to_markdown(string $pdfPath, bool $forceRefresh = false): array
|
||||
{
|
||||
if (!is_file($pdfPath)) {
|
||||
return [
|
||||
'status' => 'failed',
|
||||
'message' => "PDF file not found at {$pdfPath}",
|
||||
];
|
||||
}
|
||||
|
||||
$finfo = finfo_open(FILEINFO_MIME_TYPE);
|
||||
$mimeType = finfo_file($finfo, $pdfPath);
|
||||
finfo_close($finfo);
|
||||
|
||||
if ($mimeType !== 'application/pdf') {
|
||||
return [
|
||||
'status' => 'failed',
|
||||
'message' => "Unsupported file type ({$mimeType}). Only PDF is supported.",
|
||||
];
|
||||
}
|
||||
|
||||
$mdPath = policy_pdf_md_path($pdfPath);
|
||||
$mdFileName = basename($mdPath);
|
||||
$mdDir = dirname($mdPath);
|
||||
|
||||
if (!is_dir($mdDir) && !mkdir($mdDir, 0777, true) && !is_dir($mdDir)) {
|
||||
return [
|
||||
'status' => 'failed',
|
||||
'message' => "Unable to create markdown directory at {$mdDir}",
|
||||
];
|
||||
}
|
||||
|
||||
if (!$forceRefresh && is_file($mdPath) && filemtime($mdPath) >= filemtime($pdfPath)) {
|
||||
$cached = file_get_contents($mdPath);
|
||||
|
||||
if ($cached !== false && policy_md_is_valid($cached)) {
|
||||
log_message('info', "Using cached policy markdown: {$mdPath}");
|
||||
|
||||
return [
|
||||
'status' => 'success',
|
||||
'message' => 'Cached markdown loaded from disk',
|
||||
'content' => $cached,
|
||||
'md_path' => $mdPath,
|
||||
'md_file_name' => $mdFileName,
|
||||
];
|
||||
}
|
||||
|
||||
if ($cached !== false) {
|
||||
log_message('info', "Invalid cached markdown detected, reconverting: {$mdPath}");
|
||||
@unlink($mdPath);
|
||||
}
|
||||
}
|
||||
|
||||
$extractedText = policy_pdf_extract_text($pdfPath);
|
||||
|
||||
if ($extractedText === '') {
|
||||
return [
|
||||
'status' => 'failed',
|
||||
'message' => 'PDF text extraction failed. The file may be scanned, image-based, or pdftotext is unavailable on the server.',
|
||||
];
|
||||
}
|
||||
|
||||
$markdown = "# Motor Policy Document\n\n" . $extractedText;
|
||||
|
||||
if (file_put_contents($mdPath, $markdown) === false) {
|
||||
return [
|
||||
'status' => 'failed',
|
||||
'message' => "Failed to write markdown file at {$mdPath}",
|
||||
];
|
||||
}
|
||||
|
||||
$savedContent = file_get_contents($mdPath);
|
||||
|
||||
if ($savedContent === false || !policy_md_is_valid($savedContent)) {
|
||||
@unlink($mdPath);
|
||||
|
||||
return [
|
||||
'status' => 'failed',
|
||||
'message' => 'Saved markdown file is invalid after conversion',
|
||||
];
|
||||
}
|
||||
|
||||
log_message('info', "Policy PDF converted to markdown: {$mdPath}");
|
||||
|
||||
return [
|
||||
'status' => 'success',
|
||||
'message' => 'PDF converted to markdown and saved to disk',
|
||||
'content' => $savedContent,
|
||||
'md_path' => $mdPath,
|
||||
'md_file_name' => $mdFileName,
|
||||
];
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user