Files
citrus-cms/app/Services/RegisterCreator/DocumentProcessors/WdbDocumentProcessor.php
T
2026-04-28 21:14:25 +03:00

460 lines
16 KiB
PHP

<?php
namespace App\Services\RegisterCreator\DocumentProcessors;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
use Illuminate\Support\Facades\Log;
class WdbDocumentProcessor extends AbstractDocumentProcessor
{
/**
* Process WDB (Welding Database) 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 WDB document: {$document['path']}");
$path = $document['path'];
$wpsData = $settings['wps_data'] ?? null;
$startRowNo = $settings['row_no'] ?? 1;
// Process based on sub-type
if (strpos($path, "Naks Technology") !== false) {
return $this->processNaksTechnology($currentRow, $wpsData, $startRowNo);
}
if (strpos($path, "Naks_Welder") !== false) {
return $this->processNaksWelder($currentRow, $startRowNo);
}
if (strpos($path, "Naks_Equipments") !== false) {
return $this->processNaksEquipments($currentRow, $startRowNo);
}
if (strpos($path, "Naks_Consumables") !== false) {
return $this->processNaksConsumables($currentRow, $startRowNo);
}
if (strpos($path, "Welding Experts") !== false) {
return $this->processWeldingExperts($currentRow, $startRowNo);
}
if (strpos($path, "WPQ") !== false) {
return $this->processWpq($currentRow, $startRowNo);
}
if (strpos($path, "WPS") !== false) {
return $this->processWps($currentRow, $wpsData, $startRowNo);
}
if (strpos($path, "PQR") !== false) {
return $this->processPqr($currentRow, $wpsData, $startRowNo);
}
return $currentRow;
}
/**
* Process Naks Technology certificates
*/
private function processNaksTechnology(int &$currentRow, $wpsData, int $startRowNo = 1): int
{
if (!$wpsData) {
$this->log("WPS data not found, skipping Naks Technology", 'warning');
return $currentRow;
}
$naksCertificates = explode(" + ", $wpsData->naks_certificate_no);
$this->log("Processing " . count($naksCertificates) . " Naks Technology certificates");
foreach ($naksCertificates as $naksCertificate) {
try {
$normalized = $this->normalizeSearchTerm($naksCertificate);
$search = $this->searchFiles("storage/documents/{$this->document['path']}/*{$normalized}*.pdf");
$this->document['title3'] = $naksCertificate;
$newRow = $this->addRowToExcel(
$search,
$naksCertificate,
$wpsData->date ?? ''
);
if ($newRow > $currentRow) {
$currentRow = $newRow;
}
} catch (\Throwable $th) {
$this->log("Error processing Naks certificate: {$naksCertificate} - {$th->getMessage()}", 'error');
continue;
}
}
return $currentRow;
}
/**
* Process Naks Welder certificates
*/
private function processNaksWelder(int &$currentRow, int $startRowNo = 1): int
{
$this->log("Processing Naks Welders");
$weldersData = apply_welded_filter(
db("weld_logs")
->select("certificate_no_1", "certificate_no_2")
->where($this->registerColumnBased, $this->weldLogData[$this->registerColumnBased])
)->get();
$naksWelders = [];
foreach ($weldersData as $welderData) {
if (!empty($welderData->certificate_no_1) && !in_array($welderData->certificate_no_1, $naksWelders)) {
$naksWelders[] = $welderData->certificate_no_1;
}
if (!empty($welderData->certificate_no_2) && !in_array($welderData->certificate_no_2, $naksWelders)) {
$naksWelders[] = $welderData->certificate_no_2;
}
}
$this->log("Found " . count($naksWelders) . " welder certificates");
// Prepare welders with their dates for sorting
$weldersWithDates = [];
foreach ($naksWelders as $naksWelder) {
$welderInfo = db("naks_welders")->where("naks_certificate_no", $naksWelder)->first();
$weldersWithDates[] = [
'certificate_no' => $naksWelder,
'date' => $welderInfo->period_of_validity ?? ''
];
}
// Sort by date (oldest first) - reverse insertion order
usort($weldersWithDates, function($a, $b) {
$dateA = !empty($a['date']) ? strtotime($a['date']) : 0;
$dateB = !empty($b['date']) ? strtotime($b['date']) : 0;
return $dateA <=> $dateB;
});
$this->log("Welders sorted by date (oldest first for reverse insertion)");
foreach ($weldersWithDates as $welderData) {
try {
$naksWelder = $welderData['certificate_no'];
$normalized = $this->normalizeSearchTerm($naksWelder);
$search = $this->searchFiles("storage/documents/{$this->document['path']}/*{$normalized}*.pdf");
$this->document['title2'] = $naksWelder;
$newRow = $this->addRowToExcel(
$search,
$naksWelder,
$welderData['date']
);
if ($newRow > $currentRow) {
$currentRow = $newRow;
}
} catch (\Throwable $th) {
$this->log("Error processing welder: {$naksWelder} - {$th->getMessage()}", 'error');
continue;
}
}
return $currentRow;
}
/**
* Process Naks Equipment certificates
*/
private function processNaksEquipments(int &$currentRow, int $startRowNo = 1): int
{
$this->log("Processing Naks Equipments");
$certificates = [];
if (!empty($this->document['title2'])) {
$certificates = array_map('trim', explode(",", $this->document['title2']));
}
// Prepare equipments with dates for sorting
$equipmentsWithDates = [];
foreach ($certificates as $certificate) {
$equipmentInfo = db("welding_equipment")->where("attestation", $certificate)->first();
$equipmentsWithDates[] = [
'certificate' => $certificate,
'date' => $equipmentInfo->valid_until ?? ''
];
}
// Sort by valid_until date (oldest first) - reverse insertion order
usort($equipmentsWithDates, function($a, $b) {
$timestampA = !empty($a['date']) ? strtotime($a['date']) : 0;
$timestampB = !empty($b['date']) ? strtotime($b['date']) : 0;
return $timestampA <=> $timestampB;
});
$this->log("Equipments sorted by date (oldest first for reverse insertion)");
foreach ($equipmentsWithDates as $equipData) {
try {
$certificate = $equipData['certificate'];
$normalized = $this->normalizeSearchTerm($certificate);
$search = $this->searchFiles("storage/documents/{$this->document['path']}/*{$normalized}*.pdf");
$this->document['title2'] = $certificate;
$this->document['title3'] = $certificate;
$newRow = $this->addRowToExcel(
$search,
$certificate,
$equipData['date']
);
if ($newRow > $currentRow) {
$currentRow = $newRow;
}
} catch (\Throwable $th) {
$this->log("Error processing equipment: {$certificate} - {$th->getMessage()}", 'error');
continue;
}
}
return $currentRow;
}
/**
* Process Naks Consumables
*/
private function processNaksConsumables(int &$currentRow, int $startRowNo = 1): int
{
$this->log("Processing Naks Consumables");
$consumables = [];
if (!empty($this->document['title2'])) {
$consumables = array_map('trim', explode(",", $this->document['title2']));
}
// Prepare consumables with dates for sorting
$consumablesWithDates = [];
foreach ($consumables as $consumable) {
$consumableInfo = db("naks_consumables")->where("naks_certificate_no", $consumable)->first();
$consumablesWithDates[] = [
'consumable' => $consumable,
'date' => $consumableInfo->certificate_date ?? ''
];
}
// Sort by certificate_date (oldest first) - reverse insertion order
usort($consumablesWithDates, function($a, $b) {
$timestampA = !empty($a['date']) ? strtotime($a['date']) : 0;
$timestampB = !empty($b['date']) ? strtotime($b['date']) : 0;
return $timestampA <=> $timestampB;
});
$this->log("Consumables sorted by date (oldest first for reverse insertion)");
foreach ($consumablesWithDates as $consData) {
try {
$consumable = $consData['consumable'];
$normalized = $this->normalizeSearchTerm($consumable);
$search = $this->searchFiles("storage/documents/{$this->document['path']}/*{$normalized}*.pdf");
$this->document['title2'] = $consumable;
$newRow = $this->addRowToExcel(
$search,
$consumable,
$consData['date']
);
if ($newRow > $currentRow) {
$currentRow = $newRow;
}
} catch (\Throwable $th) {
$this->log("Error processing consumable: {$consumable} - {$th->getMessage()}", 'error');
continue;
}
}
return $currentRow;
}
/**
* Process Welding Experts
*/
private function processWeldingExperts(int &$currentRow, int $startRowNo = 1): int
{
$this->log("Processing Welding Experts");
$certificates = array_map('trim', explode(',', $this->document['title2'] ?? ''));
// Prepare experts with dates for sorting
$expertsWithDates = [];
foreach ($certificates as $certificate) {
$expertInfo = db("register_of_experts")->where("certificate_no", $certificate)->first();
$expertsWithDates[] = [
'certificate' => $certificate,
'date' => $expertInfo->expration_of_the_certificate ?? ''
];
}
// Sort by expiration date (oldest first) - reverse insertion order
usort($expertsWithDates, function($a, $b) {
$timestampA = !empty($a['date']) ? strtotime($a['date']) : 0;
$timestampB = !empty($b['date']) ? strtotime($b['date']) : 0;
return $timestampA <=> $timestampB;
});
$this->log("Experts sorted by date (oldest first for reverse insertion)");
foreach ($expertsWithDates as $expertData) {
try {
$certificate = $expertData['certificate'];
$normalized = $this->normalizeSearchTerm($certificate);
$search = $this->searchFiles("storage/documents/{$this->document['path']}/*{$normalized}*.pdf");
$this->document['title2'] = $certificate;
$newRow = $this->addRowToExcel(
$search,
$certificate,
$expertData['date']
);
if ($newRow > $currentRow) {
$currentRow = $newRow;
}
} catch (\Throwable $th) {
$this->log("Error processing expert: {$certificate} - {$th->getMessage()}", 'error');
continue;
}
}
return $currentRow;
}
/**
* Process WPQ documents
*/
private function processWpq(int &$currentRow, int $startRowNo = 1): int
{
$this->log("Processing WPQ");
$wpqs = db("welder_tests")->whereIn("wpq_document_no", [
$this->weldLogData['wpq_report_1'] ?? '',
$this->weldLogData['wpq_report_2'] ?? '',
])->get()->toArray();
$this->log("Found " . count($wpqs) . " WPQ documents");
// Sort by naks_validity date (oldest first) - reverse insertion order
usort($wpqs, function($a, $b) {
$a = (object) $a;
$b = (object) $b;
$timestampA = !empty($a->naks_validity) ? strtotime($a->naks_validity) : 0;
$timestampB = !empty($b->naks_validity) ? strtotime($b->naks_validity) : 0;
return $timestampA <=> $timestampB;
});
$this->log("WPQ documents sorted by date (oldest first for reverse insertion)");
foreach ($wpqs as $wpq) {
$wpq = (object) $wpq;
try {
$normalized = $this->normalizeSearchTerm($wpq->wpq_document_no);
$search = $this->searchFiles("storage/documents/{$this->document['path']}/*{$normalized}*.pdf");
$this->document['title3'] = $wpq->wpq_document_no;
$newRow = $this->addRowToExcel(
$search,
$wpq->wpq_document_no,
$wpq->naks_validity ?? ''
);
if ($newRow > $currentRow) {
$currentRow = $newRow;
}
} catch (\Throwable $th) {
$this->log("Error processing WPQ: {$wpq->wpq_document_no} - {$th->getMessage()}", 'error');
continue;
}
}
return $currentRow;
}
/**
* Process WPS documents
*/
private function processWps(int &$currentRow, $wpsData, int $startRowNo = 1): int
{
if (!$wpsData) {
$this->log("WPS data not found", 'warning');
return $currentRow;
}
$this->log("Processing WPS: {$wpsData->details}");
$normalized = $this->normalizeSearchTerm($wpsData->details);
$search = $this->searchFiles("storage/documents/{$this->document['path']}/*{$normalized}*.pdf");
$this->document['title3'] = $wpsData->details;
$newRow = $this->addRowToExcel(
$search,
$wpsData->details,
$wpsData->date ?? ''
);
if ($newRow > $currentRow) {
$currentRow = $newRow;
}
return $currentRow;
}
/**
* Process PQR documents
*/
private function processPqr(int &$currentRow, $wpsData, int $startRowNo = 1): int
{
if (!$wpsData) {
$this->log("WPS data not found, cannot process PQR", 'warning');
return $currentRow;
}
$this->log("Processing PQR");
$pqr = db("prosedure_qualification_records")->where("pqr_no", $wpsData->pqr_no)->first();
if ($pqr) {
$this->log("PQR found: {$pqr->pqr_no}");
$normalized = $this->normalizeSearchTerm($pqr->pqr_no);
$search = $this->searchFiles("storage/documents/{$this->document['path']}/*{$normalized}*.pdf");
$this->document['title2'] = $pqr->pqr_no;
$newRow = $this->addRowToExcel(
$search,
$pqr->pqr_no,
$pqr->approved_date ?? ''
);
if ($newRow > $currentRow) {
$currentRow = $newRow;
}
}
return $currentRow;
}
}