153 lines
5.5 KiB
PHP
153 lines
5.5 KiB
PHP
<?php
|
|
|
|
namespace App\Services\RegisterCreator\DocumentProcessors;
|
|
|
|
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Carbon\Carbon;
|
|
|
|
class TemplateProcessor extends AbstractDocumentProcessor
|
|
{
|
|
/**
|
|
* Process template type documents
|
|
*/
|
|
public function process(
|
|
array $weldLogData,
|
|
array $document,
|
|
Worksheet $sheet,
|
|
int &$currentRow,
|
|
array $settings
|
|
): int {
|
|
$this->initialize($weldLogData, $document, $sheet, $currentRow, $settings);
|
|
|
|
$this->log("Processing Template document: {$document['path']}");
|
|
|
|
$lineIdentifier = $weldLogData[$this->registerColumnBased];
|
|
|
|
// Search by line number
|
|
$normalized1 = $this->normalizeSearchTerm($lineIdentifier);
|
|
$search1 = $this->searchFiles("storage/documents/{$document['path']}/*{$normalized1}*.pdf");
|
|
|
|
// Search by test package numbers
|
|
$testPackageNumbers = db("weld_logs")
|
|
->where($this->registerColumnBased, $lineIdentifier)
|
|
->whereNotNull("test_package_no")
|
|
->where("test_package_no", "!=", "")
|
|
->distinct()
|
|
->pluck("test_package_no")
|
|
->toArray();
|
|
|
|
$this->log("Found " . count($testPackageNumbers) . " test package numbers for search");
|
|
|
|
$search2 = [];
|
|
foreach ($testPackageNumbers as $testPackageNo) {
|
|
$normalized2 = $this->normalizeSearchTerm($testPackageNo);
|
|
$result = $this->searchFiles("storage/documents/{$document['path']}/*{$normalized2}*.pdf");
|
|
if (!empty($result)) {
|
|
$search2 = array_merge($search2, $result);
|
|
}
|
|
}
|
|
|
|
// Merge and remove duplicates
|
|
$allFiles = array_unique(array_merge($search1, $search2));
|
|
$this->log("Total files found: " . count($allFiles));
|
|
|
|
if (empty($allFiles)) {
|
|
$this->log("No template files found", 'warning');
|
|
return $currentRow;
|
|
}
|
|
|
|
// Sort files by date extracted from filename (oldest first)
|
|
// Note: Excel rows are inserted in reverse (insertNewRowBefore),
|
|
// so oldest first becomes newest last in Excel (correct order)
|
|
$self = $this;
|
|
usort($allFiles, function($a, $b) use ($self) {
|
|
$dateA = strtotime($self->extractDateFromFilename(basename($a))) ?: 0;
|
|
$dateB = strtotime($self->extractDateFromFilename(basename($b))) ?: 0;
|
|
|
|
// Sort ascending (oldest first)
|
|
return $dateA <=> $dateB;
|
|
});
|
|
|
|
$this->log("Files sorted by date (oldest first for reverse insertion)");
|
|
|
|
foreach ($allFiles as $filePath) {
|
|
try {
|
|
$fileName = basename($filePath);
|
|
$documentDate = $this->extractDateFromFilename($fileName);
|
|
|
|
// Process single file
|
|
$templateFileName = str_replace(".pdf", "", $fileName);
|
|
$lineNumber = $templateFileName;
|
|
$document['title2'] = $templateFileName;
|
|
|
|
// Remove date from filename
|
|
$templateFileName = preg_replace('/_\d{2}\.\d{2}\.\d{4}/', '', $templateFileName);
|
|
$templateFileName = preg_replace('/_\d{4}-\d{2}-\d{2}/', '', $templateFileName);
|
|
$document['templateFileName'] = $templateFileName;
|
|
|
|
// Special handling for Incoming_Control
|
|
if ($document['id'] == "Incoming_Control") {
|
|
$incomingControl = db("incoming_controls")
|
|
->where("certificate_no", $lineNumber)
|
|
->first();
|
|
|
|
if ($incomingControl) {
|
|
$document['title2'] = $incomingControl->description_ru;
|
|
$this->log("Incoming Control found: {$document['title2']}");
|
|
}
|
|
}
|
|
|
|
// Update document reference for current iteration
|
|
$this->document = $document;
|
|
|
|
$newRow = $this->addRowToExcel(
|
|
[$filePath],
|
|
$lineNumber,
|
|
$documentDate
|
|
);
|
|
|
|
if ($newRow > $currentRow) {
|
|
$currentRow = $newRow;
|
|
}
|
|
} catch (\Throwable $th) {
|
|
$this->log("Error processing template file: " . basename($filePath) . " - {$th->getMessage()}", 'error');
|
|
continue;
|
|
}
|
|
}
|
|
|
|
return $currentRow;
|
|
}
|
|
|
|
/**
|
|
* Extract date from filename
|
|
*/
|
|
private function extractDateFromFilename(string $fileName): string
|
|
{
|
|
// Try YYYY-MM-DD format
|
|
if (preg_match('/(\d{4}-\d{2}-\d{2})/', $fileName, $matches)) {
|
|
return $matches[1];
|
|
}
|
|
|
|
// Try DD.MM.YYYY format
|
|
if (preg_match('/(\d{2}\.\d{2}\.\d{4})/', $fileName, $matches)) {
|
|
try {
|
|
return Carbon::createFromFormat('d.m.Y', $matches[1])->format('Y-m-d');
|
|
} catch (\Throwable $th) {
|
|
Log::debug("Could not parse date: {$matches[1]}");
|
|
}
|
|
}
|
|
|
|
// Default to latest date
|
|
$placeholderReplacer = new \App\Services\RegisterCreator\PlaceholderReplacer();
|
|
return $placeholderReplacer->getLatestDate($this->weldLogData, $this->registerColumnBased);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|