114 lines
4.2 KiB
PHP
114 lines
4.2 KiB
PHP
<?php
|
|
|
|
namespace App\Services\RegisterCreator\DocumentProcessors;
|
|
|
|
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
|
|
|
|
class MaterialsProcessor extends AbstractDocumentProcessor
|
|
{
|
|
/**
|
|
* Process materials 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 Materials");
|
|
|
|
// Get all certificates from same line
|
|
$allRecords = db("weld_logs")
|
|
->where($this->registerColumnBased, $weldLogData['line_number'])
|
|
->get();
|
|
|
|
$uniqueCertificates = [];
|
|
|
|
foreach ($allRecords as $record) {
|
|
if (!empty($record->certificate_number_of_1) && !in_array($record->certificate_number_of_1, $uniqueCertificates)) {
|
|
$uniqueCertificates[] = $record->certificate_number_of_1;
|
|
}
|
|
if (!empty($record->certificate_number_of_2) && !in_array($record->certificate_number_of_2, $uniqueCertificates)) {
|
|
$uniqueCertificates[] = $record->certificate_number_of_2;
|
|
}
|
|
}
|
|
|
|
$this->log("Found " . count($uniqueCertificates) . " unique certificates");
|
|
|
|
$placeholderReplacer = new \App\Services\RegisterCreator\PlaceholderReplacer();
|
|
|
|
// Prepare certificates with dates for sorting
|
|
$certificatesWithDates = [];
|
|
foreach ($uniqueCertificates as $certificateNumber) {
|
|
$incomingControl = db("incoming_controls")
|
|
->where("certificate_no", $certificateNumber)
|
|
->first();
|
|
|
|
$documentDate = $incomingControl->certificate_date ?? $incomingControl->created_at ?? $placeholderReplacer->getLatestDate($weldLogData, $this->registerColumnBased);
|
|
|
|
$certificatesWithDates[] = [
|
|
'certificate_no' => $certificateNumber,
|
|
'incoming_control' => $incomingControl,
|
|
'date' => $documentDate
|
|
];
|
|
}
|
|
|
|
// Sort by date (oldest first) - reverse insertion order
|
|
usort($certificatesWithDates, function($a, $b) {
|
|
$timestampA = !empty($a['date']) ? strtotime($a['date']) : 0;
|
|
$timestampB = !empty($b['date']) ? strtotime($b['date']) : 0;
|
|
return $timestampA <=> $timestampB;
|
|
});
|
|
|
|
$this->log("Certificates sorted by date (oldest first for reverse insertion)");
|
|
|
|
foreach ($certificatesWithDates as $certData) {
|
|
$certificateNumber = $certData['certificate_no'];
|
|
$incomingControl = $certData['incoming_control'];
|
|
$documentDate = $certData['date'];
|
|
try {
|
|
if ($incomingControl) {
|
|
$document['title2'] = $incomingControl->description_ru;
|
|
$document['incoming_control_description'] = $incomingControl->description_ru;
|
|
$document['file_name'] = $certificateNumber;
|
|
} else {
|
|
$document['title2'] = "-";
|
|
$document['incoming_control_description'] = "-";
|
|
$document['file_name'] = $certificateNumber;
|
|
}
|
|
|
|
$normalized = $this->normalizeSearchTerm($certificateNumber);
|
|
$search = $this->searchFiles("{$document['path']}/*{$normalized}*.pdf");
|
|
|
|
// Update document reference for current iteration
|
|
$this->document = $document;
|
|
|
|
$newRow = $this->addRowToExcel(
|
|
$search,
|
|
$certificateNumber,
|
|
$documentDate
|
|
);
|
|
|
|
if ($newRow > $currentRow) {
|
|
$currentRow = $newRow;
|
|
}
|
|
} catch (\Throwable $th) {
|
|
$this->log("Error processing certificate: {$certificateNumber} - {$th->getMessage()}", 'error');
|
|
continue;
|
|
}
|
|
}
|
|
|
|
return $currentRow;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|