54 lines
2.4 KiB
PHP
54 lines
2.4 KiB
PHP
<?php
|
|
|
|
namespace App\Services\RegisterCreator\DocumentProcessors;
|
|
|
|
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
class IncomingControlProcessor extends AbstractDocumentProcessor
|
|
{
|
|
public function process(array $weldLogData, array $document, Worksheet $sheet, int &$currentRow, array $settings): int
|
|
{
|
|
$this->initialize($weldLogData, $document, $sheet, $currentRow, $settings);
|
|
|
|
$project = $weldLogData['line_number'] ?? '';
|
|
if (empty($project)) return $currentRow;
|
|
|
|
$incomingControls = db("incoming_controls")->where("project", $project)->groupBy("certificate_no", "description_ru")->get()->toArray();
|
|
$placeholderReplacer = new \App\Services\RegisterCreator\PlaceholderReplacer();
|
|
|
|
// Sort by certificate_date (oldest first) - reverse insertion order
|
|
usort($incomingControls, function($a, $b) use ($placeholderReplacer, $weldLogData) {
|
|
$a = (object) $a;
|
|
$b = (object) $b;
|
|
$dateA = $a->certificate_date ?? $a->created_at ?? $placeholderReplacer->getLatestDate($weldLogData, $this->registerColumnBased);
|
|
$dateB = $b->certificate_date ?? $b->created_at ?? $placeholderReplacer->getLatestDate($weldLogData, $this->registerColumnBased);
|
|
$timestampA = !empty($dateA) ? strtotime($dateA) : 0;
|
|
$timestampB = !empty($dateB) ? strtotime($dateB) : 0;
|
|
return $timestampA <=> $timestampB;
|
|
});
|
|
|
|
foreach ($incomingControls as $control) {
|
|
$control = (object) $control;
|
|
if (empty($control->certificate_no)) continue;
|
|
$normalized = $this->normalizeSearchTerm($control->certificate_no);
|
|
$search = $this->searchFiles("storage/documents/{$document['path']}/*{$normalized}*.pdf");
|
|
$document['title2'] = $control->description_ru;
|
|
$document['incoming_control_description'] = $control->description_ru;
|
|
$document['title3'] = $control->certificate_no;
|
|
$this->document = $document;
|
|
$documentDate = $control->certificate_date ?? $control->created_at ?? $placeholderReplacer->getLatestDate($weldLogData, $this->registerColumnBased);
|
|
$newRow = $this->addRowToExcel($search, $control->certificate_no, $documentDate);
|
|
if ($newRow > $currentRow) { $currentRow = $newRow; }
|
|
}
|
|
return $currentRow;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|