131 lines
4.7 KiB
PHP
131 lines
4.7 KiB
PHP
<?php
|
|
// Calibration Logs PDF to DB Sync
|
|
// Similar structure to pdf-db-naks-technology-sync.blade.php
|
|
$table = "calibration_logs";
|
|
$mainPath = "004_QA/0025_Calibration";
|
|
$nullDownload = db($table);
|
|
|
|
if(isset($fileName)) {
|
|
// Parse fileName to extract identification info
|
|
// Example file patterns:
|
|
// - CLB-001.pdf (item_no format)
|
|
// - SERIAL123-CALIBRATION.pdf (serial_no format)
|
|
// - PASSPORT-CERT-2024.pdf (passport_certificate_no format)
|
|
// - CAL-CERT-123.pdf (calibration_certificate_no format)
|
|
|
|
$cleanFileName = $fileName;
|
|
$cleanFileName = str_replace('.pdf', '', $cleanFileName);
|
|
$cleanFileName = str_replace('.PDF', '', $cleanFileName);
|
|
|
|
|
|
// Try to match by multiple identifiers
|
|
$nullDownload = $nullDownload->where(function($query) use ($cleanFileName) {
|
|
$query->where("item_no", "like", "%$cleanFileName%")
|
|
->orWhere("serial_no", "like", "%$cleanFileName%")
|
|
->orWhere("passport_certificate_no", "like", "%$cleanFileName%")
|
|
->orWhere("calibration_certificate_no", "like", "%$cleanFileName%")
|
|
->orWhere("equipment_name", "like", "%$cleanFileName%");
|
|
});
|
|
$commitSize = 1; // Single file processing
|
|
}
|
|
|
|
$nullDownload = $nullDownload->get();
|
|
|
|
DB::beginTransaction();
|
|
|
|
$say = 0;
|
|
$k = 0;
|
|
Log::debug("📄$table PDF Link to DB Start " . simdi());
|
|
|
|
try {
|
|
foreach($nullDownload AS $record) {
|
|
$updateData = [];
|
|
|
|
$calibrationSearchTerms = [];
|
|
|
|
if(isset($record->item_no) && trim($record->item_no) != "") {
|
|
$calibrationSearchTerms[] = $record->item_no;
|
|
}
|
|
if(isset($record->passport_certificate_no) && trim($record->passport_certificate_no) != "") {
|
|
$calibrationSearchTerms[] = $record->passport_certificate_no;
|
|
}
|
|
if(isset($record->calibration_certificate_no) && trim($record->calibration_certificate_no) != "") {
|
|
$calibrationSearchTerms[] = $record->calibration_certificate_no;
|
|
}
|
|
|
|
if(count($calibrationSearchTerms) > 0 || isset($record->equipment_name)) {
|
|
$updateData['calibration_certificate'] = null;
|
|
$foundFile = false;
|
|
|
|
// Try each search term until we find a match for calibration_certificate
|
|
foreach($calibrationSearchTerms as $searchTerm) {
|
|
$data = "*$searchTerm*";
|
|
$data = str_replace("/", "*", $data);
|
|
$data = str_replace(" ", "*", $data);
|
|
|
|
$path = "storage/documents/$mainPath/{$data}";
|
|
$searchPDF = glob($path);
|
|
|
|
if(isset($searchPDF[0])) {
|
|
$updateData['calibration_certificate'] = $searchPDF[0];
|
|
$foundFile = true;
|
|
break;
|
|
}
|
|
}
|
|
|
|
// If no match found with specific terms, try equipment_name as fallback for calibration_certificate
|
|
if(!$foundFile && isset($record->equipment_name) && trim($record->equipment_name) != "") {
|
|
$data = "*" . $record->equipment_name . "*";
|
|
$data = str_replace("/", "*", $data);
|
|
$data = str_replace(" ", "*", $data);
|
|
|
|
$path = "storage/documents/$mainPath/{$data}";
|
|
$searchPDF = glob($path);
|
|
|
|
if(isset($searchPDF[0])) {
|
|
$updateData['calibration_certificate'] = $searchPDF[0];
|
|
}
|
|
}
|
|
}
|
|
|
|
if(isset($record->serial_no) && trim($record->serial_no) != "") {
|
|
$serialData = "*" . $record->serial_no . "*";
|
|
$serialData = str_replace("/", "*", $serialData);
|
|
$serialData = str_replace(" ", "*", $serialData);
|
|
|
|
$serialPath = "storage/documents/$mainPath/{$serialData}";
|
|
$serialSearchPDF = glob($serialPath);
|
|
|
|
if(isset($serialSearchPDF[0])) {
|
|
$updateData['certification_document'] = $serialSearchPDF[0];
|
|
}
|
|
}
|
|
|
|
// Update database only if there are changes
|
|
if(count($updateData) > 0) {
|
|
db($table)
|
|
->where("id", $record->id)
|
|
->update($updateData);
|
|
$k++;
|
|
|
|
if($say == $commitSize) {
|
|
DB::commit();
|
|
DB::beginTransaction();
|
|
$say = 0;
|
|
}
|
|
|
|
$say++;
|
|
}
|
|
}
|
|
DB::commit();
|
|
Log::debug("$table update PDF link for $k rows");
|
|
|
|
} catch (\Throwable $th) {
|
|
DB::rollback();
|
|
Log::error("not update PDF links " . $th->getMessage());
|
|
}
|
|
|
|
Log::debug("📄$table PDF Link to DB Finish " . simdi());
|
|
|
|
?>
|