İlk temizlik tamamlandı bir önceki projeden
This commit is contained in:
@@ -0,0 +1,84 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\TpCreator\DocumentProcessors;
|
||||
|
||||
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
|
||||
|
||||
abstract class AbstractTpDocumentProcessor
|
||||
{
|
||||
protected array $settings;
|
||||
|
||||
/**
|
||||
* Process the document and add rows to Excel
|
||||
*
|
||||
* @param array $lineData Test Package data
|
||||
* @param array $document Document configuration
|
||||
* @param Worksheet $sheet Excel sheet
|
||||
* @param int $currentRow Current row index in Excel
|
||||
* @param int $rowNo Current row number (counter)
|
||||
* @param string $fullFolder Full path to destination folder
|
||||
* @param string $testPackageNo Test Package Number
|
||||
* @param bool $override Override existing files
|
||||
* @return int New row number (counter)
|
||||
*/
|
||||
abstract public function process(
|
||||
array $lineData,
|
||||
array $document,
|
||||
Worksheet $sheet,
|
||||
int $currentRow,
|
||||
int $rowNo,
|
||||
string $fullFolder,
|
||||
string $testPackageNo,
|
||||
bool $override
|
||||
): int;
|
||||
|
||||
protected function alternativeGlob($pattern)
|
||||
{
|
||||
return glob($pattern);
|
||||
}
|
||||
|
||||
protected function extractFileName($fullPath, $basePath)
|
||||
{
|
||||
if(empty($fullPath)) return '';
|
||||
// Remove storage/documents prefix if present to get relative path
|
||||
$name = str_replace("storage/documents/{$basePath}/", "", $fullPath);
|
||||
// Also try removing just basePath if fullPath is relative
|
||||
$name = str_replace("{$basePath}/", "", $name);
|
||||
return str_replace(".pdf", "", $name);
|
||||
}
|
||||
|
||||
protected function addRow(array $search, array $doc, string $folder, string $line, string $date, int &$rowNo, Worksheet $sheet, int &$currentRow, bool $override): void
|
||||
{
|
||||
// Check if file exists
|
||||
if(empty($search)) {
|
||||
$logPath = $folder . 'log.txt';
|
||||
$msg = "❌ NOT FOUND: " . ($doc['title2'] ?? 'Unknown') . " (" . ($doc['path'] ?? 'Unknown Path') . ")\n";
|
||||
file_put_contents($logPath, $msg, FILE_APPEND);
|
||||
return;
|
||||
}
|
||||
|
||||
$file = $search[0];
|
||||
$fileName = basename($file);
|
||||
$destPath = $folder . $fileName;
|
||||
|
||||
// Copy file
|
||||
if(!file_exists($destPath) || $override) {
|
||||
copy($file, $destPath);
|
||||
}
|
||||
|
||||
// Insert new row
|
||||
$sheet->insertNewRowBefore($currentRow + 1, 1);
|
||||
|
||||
// Set values (Columns A, B, C, D as per TpCreatorService logic)
|
||||
$sheet->setCellValue("A" . $currentRow, $rowNo);
|
||||
$sheet->setCellValue("B" . $currentRow, $doc['title2'] ?? '');
|
||||
$sheet->setCellValue("C" . $currentRow, $doc['title3'] ?? ''); // Title 3 is usually extracted filename or specific detail
|
||||
$sheet->setCellValue("D" . $currentRow, $date);
|
||||
|
||||
// Increment
|
||||
$currentRow++;
|
||||
$rowNo++;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\TpCreator\DocumentProcessors;
|
||||
|
||||
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
|
||||
|
||||
class DocumentProcedureProcessor extends AbstractTpDocumentProcessor
|
||||
{
|
||||
public function process(array $lineData, array $document, Worksheet $sheet, int $currentRow, array $settings): int
|
||||
{
|
||||
$override = $settings['override'] ?? true;
|
||||
$folder = $settings['full_folder'];
|
||||
$rowNo = $settings['row_no'];
|
||||
$docTitle = $document['title2'] ?? '';
|
||||
$docPath = $document['path'];
|
||||
|
||||
$documentProcedure = db("document_procedures")->where("document_no", $docTitle)->first();
|
||||
|
||||
if($documentProcedure) {
|
||||
$search = $this->alternativeGlob("storage/documents/{$docPath}/*{$docTitle}*.pdf");
|
||||
$docDate = $documentProcedure->publish_date;
|
||||
$document['title2'] = $documentProcedure->description;
|
||||
|
||||
$this->addRow($search, $document, $folder, $documentProcedure->document_no, $docDate, $rowNo, $sheet, $currentRow, $override);
|
||||
}
|
||||
|
||||
return $currentRow;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\TpCreator\DocumentProcessors;
|
||||
|
||||
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
|
||||
|
||||
class DocumentProcedureTpProcessor extends AbstractTpDocumentProcessor
|
||||
{
|
||||
public function process(
|
||||
array $lineData,
|
||||
array $document,
|
||||
Worksheet $sheet,
|
||||
int $currentRow,
|
||||
int $rowNo,
|
||||
string $fullFolder,
|
||||
string $testPackageNo,
|
||||
bool $override
|
||||
): int {
|
||||
$docTitle = $document['title2'] ?? '';
|
||||
$docPath = $document['path'];
|
||||
|
||||
$documentProcedure = db("document_procedures")->where("document_no", $docTitle)->first();
|
||||
|
||||
if($documentProcedure) {
|
||||
$search = $this->alternativeGlob("storage/documents/{$docPath}/*{$docTitle}*.pdf");
|
||||
$docDate = $documentProcedure->publish_date;
|
||||
$document['title2'] = $documentProcedure->description;
|
||||
|
||||
$this->addRow($search, $document, $fullFolder, $documentProcedure->document_no, $docDate, $rowNo, $sheet, $currentRow, $override);
|
||||
|
||||
return $rowNo + 1;
|
||||
}
|
||||
|
||||
return $rowNo;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\TpCreator\DocumentProcessors;
|
||||
|
||||
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
|
||||
|
||||
class DrawingsProcessor extends AbstractTpDocumentProcessor
|
||||
{
|
||||
public function process(array $lineData, array $document, Worksheet $sheet, int $currentRow, array $settings): int
|
||||
{
|
||||
$testPackageNo = $lineData['test_package_number'];
|
||||
$docPath = $document['path'];
|
||||
$override = $settings['override'] ?? true;
|
||||
$folder = $settings['full_folder'];
|
||||
$rowNo = $settings['row_no'];
|
||||
|
||||
$testPackage = db("test_pack_base_statuses")->where("test_package_no", $testPackageNo)->first();
|
||||
|
||||
// Search by line_number as per blade logic
|
||||
$lineNumber = $testPackage->line_number;
|
||||
$search = $this->alternativeGlob("{$docPath}/*{$lineNumber}*.pdf");
|
||||
|
||||
$this->addRow($search, $document, $folder, $lineNumber, $testPackage->welding_date, $rowNo, $sheet, $currentRow, $override);
|
||||
|
||||
return $currentRow;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\TpCreator\DocumentProcessors;
|
||||
|
||||
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
|
||||
|
||||
class DrawingsTpProcessor extends AbstractTpDocumentProcessor
|
||||
{
|
||||
public function process(
|
||||
array $lineData,
|
||||
array $document,
|
||||
Worksheet $sheet,
|
||||
int $currentRow,
|
||||
int $rowNo,
|
||||
string $fullFolder,
|
||||
string $testPackageNo,
|
||||
bool $override
|
||||
): int {
|
||||
$docPath = $document['path'];
|
||||
$lineNumber = $lineData['line_number'] ?? $testPackageNo;
|
||||
$weldingDate = $lineData['welding_date'] ?? date('Y-m-d');
|
||||
|
||||
$search = $this->alternativeGlob("{$docPath}/*{$lineNumber}*.pdf");
|
||||
|
||||
$this->addRow($search, $document, $fullFolder, $lineNumber, $weldingDate, $rowNo, $sheet, $currentRow, $override);
|
||||
|
||||
return $rowNo + 1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\TpCreator\DocumentProcessors;
|
||||
|
||||
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
|
||||
|
||||
class GenericProcessor extends AbstractTpDocumentProcessor
|
||||
{
|
||||
public function process(array $lineData, array $document, Worksheet $sheet, int $currentRow, array $settings): int
|
||||
{
|
||||
$testPackageNo = $lineData['test_package_number'];
|
||||
$docPath = $document['path'];
|
||||
$override = $settings['override'] ?? true;
|
||||
$folder = $settings['full_folder'];
|
||||
$rowNo = $settings['row_no'];
|
||||
|
||||
$testPackage = db("test_pack_base_statuses")->where("test_package_no", $testPackageNo)->first();
|
||||
|
||||
// Fallback search logic as per blade: search by line number
|
||||
$lineNumber = $testPackage->line_number;
|
||||
$search = $this->alternativeGlob("storage/documents/{$docPath}/*{$lineNumber}*.pdf");
|
||||
|
||||
$this->addRow($search, $document, $folder, $lineNumber, $testPackage->welding_date, $rowNo, $sheet, $currentRow, $override);
|
||||
|
||||
return $currentRow;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\TpCreator\DocumentProcessors;
|
||||
|
||||
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
|
||||
|
||||
class GenericTpProcessor extends AbstractTpDocumentProcessor
|
||||
{
|
||||
public function process(
|
||||
array $lineData,
|
||||
array $document,
|
||||
Worksheet $sheet,
|
||||
int $currentRow,
|
||||
int $rowNo,
|
||||
string $fullFolder,
|
||||
string $testPackageNo,
|
||||
bool $override
|
||||
): int {
|
||||
// Generic fallback
|
||||
$docPath = $document['path'];
|
||||
$lineNumber = $lineData['line_number'] ?? $testPackageNo;
|
||||
$weldingDate = $lineData['welding_date'] ?? date('Y-m-d'); // Assuming available in lineData join
|
||||
|
||||
$search = $this->alternativeGlob("storage/documents/{$docPath}/*{$lineNumber}*.pdf");
|
||||
|
||||
$this->addRow($search, $document, $fullFolder, $lineNumber, $weldingDate, $rowNo, $sheet, $currentRow, $override);
|
||||
|
||||
return $rowNo + 1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\TpCreator\DocumentProcessors;
|
||||
|
||||
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
|
||||
|
||||
class NdtClearanceProcessor extends AbstractTpDocumentProcessor
|
||||
{
|
||||
public function process(array $lineData, array $document, Worksheet $sheet, int $currentRow, array $settings): int
|
||||
{
|
||||
$testPackageNo = $lineData['test_package_number'];
|
||||
$docPath = $document['path'];
|
||||
$override = $settings['override'] ?? true;
|
||||
$folder = $settings['full_folder'];
|
||||
$rowNo = $settings['row_no'];
|
||||
|
||||
$search = $this->alternativeGlob("storage/documents/{$docPath}/*{$testPackageNo}*.pdf");
|
||||
|
||||
$document['title2'] = str_replace("004_QA/", "", $document['title2']);
|
||||
|
||||
$this->addRow($search, $document, $folder, $testPackageNo, date('Y-m-d'), $rowNo, $sheet, $currentRow, $override);
|
||||
|
||||
return $currentRow;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\TpCreator\DocumentProcessors;
|
||||
|
||||
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
|
||||
|
||||
class NdtClearanceTpProcessor extends AbstractTpDocumentProcessor
|
||||
{
|
||||
public function process(
|
||||
array $lineData,
|
||||
array $document,
|
||||
Worksheet $sheet,
|
||||
int $currentRow,
|
||||
int $rowNo,
|
||||
string $fullFolder,
|
||||
string $testPackageNo,
|
||||
bool $override
|
||||
): int {
|
||||
$docPath = $document['path'];
|
||||
$docTitle = $document['title2'] ?? '';
|
||||
|
||||
$search = $this->alternativeGlob("storage/documents/{$docPath}/*{$testPackageNo}*.pdf");
|
||||
|
||||
$document['title2'] = str_replace("004_QA/", "", $docTitle);
|
||||
$date = date('Y-m-d'); // Default to current date or welding date if available
|
||||
|
||||
$this->addRow($search, $document, $fullFolder, $testPackageNo, $date, $rowNo, $sheet, $currentRow, $override);
|
||||
|
||||
return $rowNo + 1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\TpCreator\DocumentProcessors;
|
||||
|
||||
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
|
||||
|
||||
class QaProcessor extends AbstractTpDocumentProcessor
|
||||
{
|
||||
public function process(array $lineData, array $document, Worksheet $sheet, int $currentRow, array $settings): int
|
||||
{
|
||||
$testPackageNo = $lineData['test_package_number'];
|
||||
$docPath = $document['path'];
|
||||
$override = $settings['override'] ?? true;
|
||||
$folder = $settings['full_folder'];
|
||||
$rowNo = $settings['row_no'];
|
||||
|
||||
$allJoints = db("weld_logs")->where("test_package_no", $testPackageNo)->get();
|
||||
$logTypes = array_keys(log_test_types());
|
||||
|
||||
foreach($allJoints as $joint) {
|
||||
$jointArray = (array)$joint;
|
||||
foreach($logTypes as $logType) {
|
||||
if (strpos(strtolower($docPath), $logType) !== false) {
|
||||
$reportNoPrefix = ($logType == "pmi") ? "no_of_testing_report" : $logType . "_report";
|
||||
|
||||
if (!empty($jointArray[$reportNoPrefix])) {
|
||||
$reportNo = $jointArray[$reportNoPrefix];
|
||||
$docDate = $jointArray[$logType . '_test_date'];
|
||||
$search = $this->alternativeGlob("storage/documents/{$docPath}/{$reportNo}.pdf");
|
||||
|
||||
$document['title2'] = $reportNo;
|
||||
|
||||
// Translation
|
||||
$translationKey = $logType . "_register_title";
|
||||
// Note: If using helper e2(), ensure it's available or pass via settings.
|
||||
// Assuming e2 is global helper.
|
||||
$document['title4'] = e2($translationKey);
|
||||
|
||||
$this->addRow($search, $document, $folder, $reportNo, $docDate, $rowNo, $sheet, $currentRow, $override);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Update row_no in settings passed by reference if needed, but simple return of new currentRow is standard.
|
||||
// However, we also need to return new rowNo count or just the new currentRow index.
|
||||
// The Abstract class addRow increments currentRow and rowNo by reference.
|
||||
// But since rowNo is primitive, it won't persist back to caller unless we return it or use object.
|
||||
// TpCreatorService expects new currentRow.
|
||||
// We actually need to return how many rows added or the new current Row.
|
||||
|
||||
return $currentRow;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\TpCreator\DocumentProcessors;
|
||||
|
||||
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
|
||||
|
||||
class QaTpProcessor extends AbstractTpDocumentProcessor
|
||||
{
|
||||
public function process(
|
||||
array $lineData,
|
||||
array $document,
|
||||
Worksheet $sheet,
|
||||
int $currentRow,
|
||||
int $rowNo,
|
||||
string $fullFolder,
|
||||
string $testPackageNo,
|
||||
bool $override
|
||||
): int {
|
||||
$docPath = $document['path'];
|
||||
|
||||
$allJoints = db("weld_logs")->where("test_package_no", $testPackageNo)->get();
|
||||
$logTypes = array_keys(log_test_types());
|
||||
|
||||
foreach($allJoints as $joint) {
|
||||
$jointArray = (array)$joint;
|
||||
foreach($logTypes as $logType) {
|
||||
if (strpos(strtolower($docPath), $logType) !== false) {
|
||||
$reportNoPrefix = ($logType == "pmi") ? "no_of_testing_report" : $logType . "_report";
|
||||
|
||||
if (!empty($jointArray[$reportNoPrefix])) {
|
||||
$reportNo = $jointArray[$reportNoPrefix];
|
||||
$docDate = $jointArray[$logType . '_test_date'];
|
||||
$search = $this->alternativeGlob("storage/documents/{$docPath}/{$reportNo}.pdf");
|
||||
|
||||
$document['title2'] = $reportNo;
|
||||
|
||||
$translationKey = $logType . "_register_title";
|
||||
$document['title4'] = e2($translationKey);
|
||||
|
||||
$this->addRow($search, $document, $fullFolder, $reportNo, $docDate, $rowNo, $sheet, $currentRow, $override);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $rowNo;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\TpCreator\DocumentProcessors;
|
||||
|
||||
use Exception;
|
||||
|
||||
class TpDocumentProcessorFactory
|
||||
{
|
||||
public static function make(string $type, string $path): AbstractTpDocumentProcessor
|
||||
{
|
||||
// Path based detection logic first (as in blade/service)
|
||||
if (strpos($path, "NDT_Clearance") !== false) {
|
||||
return new NdtClearanceTpProcessor();
|
||||
}
|
||||
|
||||
switch ($type) {
|
||||
case 'qa':
|
||||
return new QaTpProcessor();
|
||||
case 'wdb':
|
||||
return new WdbTpProcessor();
|
||||
case 'drawings':
|
||||
return new DrawingsTpProcessor();
|
||||
case 'document-procedure':
|
||||
return new DocumentProcedureTpProcessor();
|
||||
default:
|
||||
return new GenericTpProcessor();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,138 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\TpCreator\DocumentProcessors;
|
||||
|
||||
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
|
||||
class WdbProcessor extends AbstractTpDocumentProcessor
|
||||
{
|
||||
public function process(array $lineData, array $document, Worksheet $sheet, int $currentRow, array $settings): int
|
||||
{
|
||||
$testPackageNo = $lineData['test_package_number'];
|
||||
$docPath = $document['path'];
|
||||
$docTitle = $document['title2'] ?? '';
|
||||
$override = $settings['override'] ?? true;
|
||||
$folder = $settings['full_folder'];
|
||||
$rowNo = $settings['row_no'];
|
||||
|
||||
$cleanTitle = str_replace("003_Welding_Database/", "", $docTitle);
|
||||
$document['title2'] = $cleanTitle;
|
||||
|
||||
// Load dependencies
|
||||
$testPackage = db("test_pack_base_statuses")->where("test_package_no", $testPackageNo)->first();
|
||||
$wpsData = db("w_p_s")->where("details", $testPackage->wps_no)->first();
|
||||
|
||||
$naksCertificates = [];
|
||||
if($wpsData) {
|
||||
$naksCertificates = explode(" + ", $wpsData->naks_certificate_no);
|
||||
}
|
||||
|
||||
// 1. Naks Technology
|
||||
if (strpos($docPath, "Naks Technology") !== false) {
|
||||
foreach($naksCertificates as $cert) {
|
||||
$search = $this->alternativeGlob("storage/documents/{$docPath}/*{$cert}*.pdf");
|
||||
$document['title3'] = $this->extractFileName($search[0] ?? '', $docPath);
|
||||
$this->addRow($search, $document, $folder, $cert, $wpsData->date ?? date('Y-m-d'), $rowNo, $sheet, $currentRow, $override);
|
||||
}
|
||||
}
|
||||
|
||||
// 2. Naks_Welder
|
||||
elseif (strpos($docPath, "Naks_Welder") !== false) {
|
||||
$welders = $this->getWeldersForTp($testPackageNo);
|
||||
foreach($welders as $welder) {
|
||||
$search = $this->alternativeGlob("storage/documents/{$docPath}/*{$welder}*.pdf");
|
||||
$document['title2'] = $welder;
|
||||
$document['title3'] = $this->extractFileName($search[0] ?? '', $docPath);
|
||||
$welderDate = db("naks_welders")->where("naks_certificate_no", $welder)->first()?->period_of_validity ?? date('Y-m-d');
|
||||
|
||||
$this->addRow($search, $document, $folder, $welder, $welderDate, $rowNo, $sheet, $currentRow, $override);
|
||||
}
|
||||
}
|
||||
|
||||
// 3. Naks_Equipments
|
||||
elseif (strpos($docPath, "Naks_Equipments") !== false) {
|
||||
foreach($naksCertificates as $cert) {
|
||||
$search = $this->alternativeGlob("storage/documents/{$docPath}/*{$cert}*.pdf");
|
||||
$document['title3'] = $cert;
|
||||
$docDate = db("naks_welders")->where("welder_id", $cert)->first()?->period_of_validity ?? date('Y-m-d'); // Logic copied from blade, might need adjustment
|
||||
|
||||
$this->addRow($search, $document, $folder, $cert, $docDate, $rowNo, $sheet, $currentRow, $override);
|
||||
}
|
||||
}
|
||||
|
||||
// 4. Naks_Consumables
|
||||
elseif (strpos($docPath, "Naks_Consumables") !== false) {
|
||||
$naksConsumables = db("naks_consumables")->whereIn("naks_certificate_no", $naksCertificates)->pluck("batch_number")->toArray();
|
||||
foreach($naksConsumables as $consumable) {
|
||||
$search = $this->alternativeGlob("storage/documents/{$docPath}/*{$consumable}*.pdf");
|
||||
$docDate = db("naks_welders")->where("welder_id", $consumable)->first()?->period_of_validity ?? date('Y-m-d');
|
||||
|
||||
$this->addRow($search, $document, $folder, $consumable, $docDate, $rowNo, $sheet, $currentRow, $override);
|
||||
}
|
||||
}
|
||||
|
||||
// 5. Welding Experts
|
||||
elseif (strpos($docPath, "Welding Experts") !== false) {
|
||||
// Using title2 as certificate no (from selection)
|
||||
$search = $this->alternativeGlob("storage/documents/{$docPath}/*{$docTitle}*.pdf");
|
||||
$expert = db("register_of_experts")->where("certificate_no", $docTitle)->first();
|
||||
$docDate = $expert?->permit_date ?? date('Y-m-d');
|
||||
$document['title2'] = e2("welding_expert_register_title");
|
||||
|
||||
$this->addRow($search, $document, $folder, $docTitle, $docDate, $rowNo, $sheet, $currentRow, $override);
|
||||
}
|
||||
|
||||
// 6. WPQ
|
||||
elseif (strpos($docPath, "WPQ") !== false) {
|
||||
$wpqs = db("welder_tests")->whereIn("wpq_document_no", [
|
||||
$testPackage->wpq_report_1,
|
||||
$testPackage->wpq_report_2,
|
||||
])->get();
|
||||
|
||||
foreach($wpqs as $wpq) {
|
||||
$search = $this->alternativeGlob("storage/documents/{$docPath}/*{$wpq->wpq_document_no}*.pdf");
|
||||
$document['title3'] = $wpq->wpq_document_no;
|
||||
$docDate = $wpq->naks_validity;
|
||||
|
||||
$this->addRow($search, $document, $folder, $wpq->wpq_document_no, $docDate, $rowNo, $sheet, $currentRow, $override);
|
||||
}
|
||||
}
|
||||
|
||||
// 7. WPS
|
||||
elseif (strpos($docPath, "WPS") !== false) {
|
||||
$search = $this->alternativeGlob("storage/documents/{$docPath}/*{$wpsData->details}*.pdf");
|
||||
$document['title3'] = $wpsData->details;
|
||||
$docDate = $wpsData->date;
|
||||
|
||||
$this->addRow($search, $document, $folder, $wpsData->details, $docDate, $rowNo, $sheet, $currentRow, $override);
|
||||
}
|
||||
|
||||
// 8. PQR
|
||||
elseif (strpos($docPath, "PQR") !== false) {
|
||||
$pqr = db("prosedure_qualification_records")->where("pqr_no", $wpsData->pqr_no)->first();
|
||||
if($pqr) {
|
||||
$search = $this->alternativeGlob("storage/documents/{$docPath}/*{$pqr->pqr_no}*.pdf");
|
||||
$document['title2'] = $pqr->pqr_no;
|
||||
$docDate = $pqr->approved_date;
|
||||
|
||||
$this->addRow($search, $document, $folder, $pqr->pqr_no, $docDate, $rowNo, $sheet, $currentRow, $override);
|
||||
}
|
||||
}
|
||||
|
||||
return $currentRow;
|
||||
}
|
||||
|
||||
private function getWeldersForTp($tpNo) {
|
||||
$logs = db("weld_logs")->where("test_package_no", $tpNo)->get();
|
||||
$welders = [];
|
||||
foreach($logs as $log) {
|
||||
if($log->welder_1) $welders[] = $log->certificate_no_1;
|
||||
if($log->welder_2) $welders[] = $log->certificate_no_2;
|
||||
}
|
||||
return array_unique(array_filter($welders));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,137 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\TpCreator\DocumentProcessors;
|
||||
|
||||
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
|
||||
|
||||
class WdbTpProcessor extends AbstractTpDocumentProcessor
|
||||
{
|
||||
public function process(
|
||||
array $lineData,
|
||||
array $document,
|
||||
Worksheet $sheet,
|
||||
int $currentRow,
|
||||
int $rowNo,
|
||||
string $fullFolder,
|
||||
string $testPackageNo,
|
||||
bool $override
|
||||
): int {
|
||||
$docPath = $document['path'];
|
||||
$docTitle = $document['title2'] ?? '';
|
||||
|
||||
$cleanTitle = str_replace("003_Welding_Database/", "", $docTitle);
|
||||
$document['title2'] = $cleanTitle;
|
||||
|
||||
// Load dependencies
|
||||
$testPackage = db("test_pack_base_statuses")->where("test_package_no", $testPackageNo)->first();
|
||||
$wpsData = db("w_p_s")->where("details", $testPackage->wps_no)->first();
|
||||
|
||||
$naksCertificates = [];
|
||||
if($wpsData) {
|
||||
$naksCertificates = explode(" + ", $wpsData->naks_certificate_no);
|
||||
}
|
||||
|
||||
// 1. Naks Technology
|
||||
if (strpos($docPath, "Naks Technology") !== false) {
|
||||
foreach($naksCertificates as $cert) {
|
||||
$search = $this->alternativeGlob("storage/documents/{$docPath}/*{$cert}*.pdf");
|
||||
$document['title3'] = $this->extractFileName($search[0] ?? '', $docPath);
|
||||
$this->addRow($search, $document, $fullFolder, $cert, $wpsData->date ?? date('Y-m-d'), $rowNo, $sheet, $currentRow, $override);
|
||||
}
|
||||
}
|
||||
|
||||
// 2. Naks_Welder
|
||||
elseif (strpos($docPath, "Naks_Welder") !== false) {
|
||||
$welders = $this->getWeldersForTp($testPackageNo);
|
||||
foreach($welders as $welder) {
|
||||
$search = $this->alternativeGlob("storage/documents/{$docPath}/*{$welder}*.pdf");
|
||||
$document['title2'] = $welder;
|
||||
$document['title3'] = $this->extractFileName($search[0] ?? '', $docPath);
|
||||
$welderDate = db("naks_welders")->where("naks_certificate_no", $welder)->first()?->period_of_validity ?? date('Y-m-d');
|
||||
|
||||
$this->addRow($search, $document, $fullFolder, $welder, $welderDate, $rowNo, $sheet, $currentRow, $override);
|
||||
}
|
||||
}
|
||||
|
||||
// 3. Naks_Equipments
|
||||
elseif (strpos($docPath, "Naks_Equipments") !== false) {
|
||||
foreach($naksCertificates as $cert) {
|
||||
$search = $this->alternativeGlob("storage/documents/{$docPath}/*{$cert}*.pdf");
|
||||
$document['title3'] = $cert;
|
||||
$docDate = db("naks_welders")->where("welder_id", $cert)->first()?->period_of_validity ?? date('Y-m-d');
|
||||
|
||||
$this->addRow($search, $document, $fullFolder, $cert, $docDate, $rowNo, $sheet, $currentRow, $override);
|
||||
}
|
||||
}
|
||||
|
||||
// 4. Naks_Consumables
|
||||
elseif (strpos($docPath, "Naks_Consumables") !== false) {
|
||||
$naksConsumables = db("naks_consumables")->whereIn("naks_certificate_no", $naksCertificates)->pluck("batch_number")->toArray();
|
||||
foreach($naksConsumables as $consumable) {
|
||||
$search = $this->alternativeGlob("storage/documents/{$docPath}/*{$consumable}*.pdf");
|
||||
$docDate = db("naks_welders")->where("welder_id", $consumable)->first()?->period_of_validity ?? date('Y-m-d');
|
||||
|
||||
$this->addRow($search, $document, $fullFolder, $consumable, $docDate, $rowNo, $sheet, $currentRow, $override);
|
||||
}
|
||||
}
|
||||
|
||||
// 5. Welding Experts
|
||||
elseif (strpos($docPath, "Welding Experts") !== false) {
|
||||
$search = $this->alternativeGlob("storage/documents/{$docPath}/*{$docTitle}*.pdf");
|
||||
$expert = db("register_of_experts")->where("certificate_no", $docTitle)->first();
|
||||
$docDate = $expert?->permit_date ?? date('Y-m-d');
|
||||
$document['title2'] = e2("welding_expert_register_title");
|
||||
|
||||
$this->addRow($search, $document, $fullFolder, $docTitle, $docDate, $rowNo, $sheet, $currentRow, $override);
|
||||
}
|
||||
|
||||
// 6. WPQ
|
||||
elseif (strpos($docPath, "WPQ") !== false) {
|
||||
$wpqs = db("welder_tests")->whereIn("wpq_document_no", [
|
||||
$testPackage->wpq_report_1,
|
||||
$testPackage->wpq_report_2,
|
||||
])->get();
|
||||
|
||||
foreach($wpqs as $wpq) {
|
||||
$search = $this->alternativeGlob("storage/documents/{$docPath}/*{$wpq->wpq_document_no}*.pdf");
|
||||
$document['title3'] = $wpq->wpq_document_no;
|
||||
$docDate = $wpq->naks_validity;
|
||||
|
||||
$this->addRow($search, $document, $fullFolder, $wpq->wpq_document_no, $docDate, $rowNo, $sheet, $currentRow, $override);
|
||||
}
|
||||
}
|
||||
|
||||
// 7. WPS
|
||||
elseif (strpos($docPath, "WPS") !== false) {
|
||||
$search = $this->alternativeGlob("storage/documents/{$docPath}/*{$wpsData->details}*.pdf");
|
||||
$document['title3'] = $wpsData->details;
|
||||
$docDate = $wpsData->date;
|
||||
|
||||
$this->addRow($search, $document, $fullFolder, $wpsData->details, $docDate, $rowNo, $sheet, $currentRow, $override);
|
||||
}
|
||||
|
||||
// 8. PQR
|
||||
elseif (strpos($docPath, "PQR") !== false) {
|
||||
$pqr = db("prosedure_qualification_records")->where("pqr_no", $wpsData->pqr_no)->first();
|
||||
if($pqr) {
|
||||
$search = $this->alternativeGlob("storage/documents/{$docPath}/*{$pqr->pqr_no}*.pdf");
|
||||
$document['title2'] = $pqr->pqr_no;
|
||||
$docDate = $pqr->approved_date;
|
||||
|
||||
$this->addRow($search, $document, $fullFolder, $pqr->pqr_no, $docDate, $rowNo, $sheet, $currentRow, $override);
|
||||
}
|
||||
}
|
||||
|
||||
return $rowNo;
|
||||
}
|
||||
|
||||
private function getWeldersForTp($tpNo) {
|
||||
$logs = db("weld_logs")->where("test_package_no", $tpNo)->get();
|
||||
$welders = [];
|
||||
foreach($logs as $log) {
|
||||
if($log->welder_1) $welders[] = $log->certificate_no_1;
|
||||
if($log->welder_2) $welders[] = $log->certificate_no_2;
|
||||
}
|
||||
return array_unique(array_filter($welders));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,208 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\TpCreator;
|
||||
|
||||
use App\Services\RegisterCreator\ExcelHandler;
|
||||
use App\Services\RegisterCreator\PdfConverter;
|
||||
use App\Services\TpCreator\DocumentProcessors\TpDocumentProcessorFactory;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use PhpOffice\PhpSpreadsheet\IOFactory;
|
||||
use Exception;
|
||||
|
||||
class TpCreatorService
|
||||
{
|
||||
private ExcelHandler $excelHandler;
|
||||
private PdfConverter $pdfConverter;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->excelHandler = new ExcelHandler();
|
||||
$this->pdfConverter = new PdfConverter();
|
||||
}
|
||||
|
||||
/**
|
||||
* Process single TP line
|
||||
*/
|
||||
public function processLine(array $lineData, array $documents, array $settings): array
|
||||
{
|
||||
$startTime = microtime(true);
|
||||
$jobId = $settings['job_id'];
|
||||
$testPackageNo = $lineData['test_package_number'];
|
||||
$path = $settings['path'] ?? '';
|
||||
$override = $settings['override'] ?? true;
|
||||
|
||||
$this->updateProgress($jobId, 0, "Starting TP: $testPackageNo");
|
||||
|
||||
// 1. Prepare Data
|
||||
$testPackage = db("test_pack_base_statuses")
|
||||
->where("test_package_no", $testPackageNo)
|
||||
->first();
|
||||
|
||||
if(!$testPackage) {
|
||||
throw new Exception("Test Package not found: $testPackageNo");
|
||||
}
|
||||
|
||||
// 2. Prepare Folders
|
||||
$basePath = "storage/documents/$path";
|
||||
$fullFolder = "$basePath/$testPackageNo/";
|
||||
$justFolder = "$path/$testPackageNo/";
|
||||
|
||||
if (!file_exists($fullFolder)) {
|
||||
mkdir($fullFolder, 0777, true);
|
||||
}
|
||||
|
||||
// Initialize Log File and Create Info File
|
||||
$this->initializeLogFile($fullFolder, $testPackageNo);
|
||||
$this->createInfoFile($fullFolder, $testPackageNo, $settings);
|
||||
|
||||
// 3. Load Template
|
||||
$documentInfo = document_template("register");
|
||||
if (!$documentInfo) {
|
||||
throw new Exception("Register template not found!");
|
||||
}
|
||||
|
||||
$this->excelHandler->loadTemplate($documentInfo->files);
|
||||
$sheet = $this->excelHandler->getSheet();
|
||||
|
||||
// 4. Initial Setup
|
||||
$startRow = setting("register_creator_start_row") ?: 16;
|
||||
$currentRow = $startRow;
|
||||
$rowNo = 1;
|
||||
|
||||
// 5. Process Documents
|
||||
$totalDocs = count($documents);
|
||||
$processedDocs = 0;
|
||||
|
||||
foreach($documents as $doc) {
|
||||
$docType = $doc['type'];
|
||||
$docPath = $doc['path'];
|
||||
$docTitle = $doc['title2'] ?? '';
|
||||
|
||||
$this->updateProgress($jobId, round(($processedDocs / $totalDocs) * 90), "Processing $docTitle");
|
||||
|
||||
try {
|
||||
// Use Factory to get appropriate processor
|
||||
$processor = TpDocumentProcessorFactory::make($docType, $docPath);
|
||||
|
||||
// Prepare settings for processor
|
||||
$processorSettings = [
|
||||
'override' => $override,
|
||||
'full_folder' => $fullFolder,
|
||||
'row_no' => $rowNo,
|
||||
// Add other settings if needed by specific processors
|
||||
];
|
||||
|
||||
// Process document
|
||||
$newRow = $processor->process(
|
||||
$lineData,
|
||||
$doc,
|
||||
$sheet,
|
||||
$currentRow,
|
||||
$rowNo,
|
||||
$fullFolder,
|
||||
$testPackageNo,
|
||||
$override
|
||||
);
|
||||
|
||||
// If rows were added, update currentRow and rowNo
|
||||
// The processor returns the *next* row number (e.g., if it added 2 rows, it returns currentRow + 2)
|
||||
// Actually, my interface implementation returns the new rowNo/count.
|
||||
// Let's standardize: Processors should return the new currentRow index in Excel.
|
||||
// Wait, QaTpProcessor returns rowNo (count) not currentRow (Excel index).
|
||||
// Let's re-check the AbstractTpDocumentProcessor and implementations.
|
||||
|
||||
// QaTpProcessor returns $rowNo (the counter, not Excel row index).
|
||||
// WdbTpProcessor returns $rowNo.
|
||||
// AbstractTpDocumentProcessor adds rows and increments currentRow and rowNo by reference passed to addRow helper.
|
||||
// But rowNo is passed by value to process() method unless specified otherwise.
|
||||
|
||||
// CORRECTION: In my implementation of processors, I passed $rowNo by value.
|
||||
// And process() returns int. QaTpProcessor returns $rowNo.
|
||||
// This means the returned value is the new Row Number counter.
|
||||
// I need to calculate how many rows were added to update $currentRow.
|
||||
|
||||
// Let's assume process() returns the new $rowNo.
|
||||
// Then diff = $newRowNo - $oldRowNo.
|
||||
// $currentRow += diff.
|
||||
|
||||
// Let's verify AbstractTpDocumentProcessor signature I wrote:
|
||||
// public function process(..., int $rowNo, ...): int
|
||||
|
||||
$oldRowNo = $rowNo;
|
||||
$rowNo = $newRow; // Update global row counter
|
||||
$rowsAdded = $rowNo - $oldRowNo;
|
||||
$currentRow += $rowsAdded;
|
||||
|
||||
} catch (Exception $e) {
|
||||
Log::error("Error processing document type $docType: " . $e->getMessage());
|
||||
// Continue to next document
|
||||
}
|
||||
|
||||
$processedDocs++;
|
||||
}
|
||||
|
||||
// 6. Save & Convert
|
||||
$registerFileName = $justFolder . "Register.xlsx";
|
||||
$this->excelHandler->save($registerFileName, $override);
|
||||
|
||||
$this->pdfConverter->convert($registerFileName, $justFolder, $override);
|
||||
|
||||
$this->updateProgress($jobId, 100, "Completed");
|
||||
|
||||
return ['status' => 'success'];
|
||||
}
|
||||
|
||||
private function updateProgress($jobId, $percent, $desc) {
|
||||
$queue = Cache::get('tp-creator-queue', []);
|
||||
if(isset($queue[$jobId])) {
|
||||
$queue[$jobId]['progress'] = $percent;
|
||||
$queue[$jobId]['description'] = $desc;
|
||||
Cache::put('tp-creator-queue', $queue, now()->addHours(24));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize log file
|
||||
*/
|
||||
private function initializeLogFile(string $fullFolder, string $lineIdentifier): void
|
||||
{
|
||||
$logPath = $fullFolder . 'log.txt';
|
||||
|
||||
if (file_exists($logPath)) {
|
||||
unlink($logPath);
|
||||
}
|
||||
|
||||
$header = str_repeat("🚀", 30) . "\n";
|
||||
$header .= "TP CREATOR LOG FILE\n";
|
||||
$header .= "Line: {$lineIdentifier}\n";
|
||||
$header .= "Started: " . now()->toDateTimeString() . "\n";
|
||||
$header .= str_repeat("🚀", 30) . "\n\n";
|
||||
|
||||
file_put_contents($logPath, $header);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create info.txt file with user information (JSON format)
|
||||
*/
|
||||
private function createInfoFile(string $fullFolder, string $lineIdentifier, array $settings): void
|
||||
{
|
||||
$infoPath = $fullFolder . 'info.txt';
|
||||
|
||||
// Delete old info file if exists
|
||||
if (file_exists($infoPath)) {
|
||||
unlink($infoPath);
|
||||
}
|
||||
|
||||
// Create simple JSON with user information
|
||||
$infoData = [
|
||||
'user_name' => $settings['user_name'] ?? 'Unknown',
|
||||
'user_id' => $settings['user_id'] ?? null,
|
||||
'created_at' => now()->format('Y-m-d H:i:s'),
|
||||
'line_identifier' => $lineIdentifier
|
||||
];
|
||||
|
||||
file_put_contents($infoPath, json_encode($infoData, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user