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

208 lines
6.6 KiB
PHP

<?php
namespace App\Services\RegisterCreator\DocumentProcessors;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;
use App\Services\RegisterCreator\ExcelRowHandler;
abstract class AbstractDocumentProcessor
{
protected array $weldLogData;
protected array $document;
protected Worksheet $sheet;
protected int $currentRow;
protected string $registerColumnBased;
protected array $settings;
protected string $logFilePath;
protected ExcelRowHandler $excelRowHandler;
protected int $documentsAdded = 0; // Counter for documents added to Excel
/**
* Process the document
*/
abstract public function process(
array $weldLogData,
array $document,
Worksheet $sheet,
int &$currentRow,
array $settings
): int;
/**
* Initialize processor
*/
protected function initialize(
array $weldLogData,
array $document,
Worksheet $sheet,
int $currentRow,
array $settings
): void {
$this->weldLogData = $weldLogData;
$this->document = $document;
$this->sheet = $sheet;
$this->currentRow = $currentRow;
$this->settings = $settings;
$this->registerColumnBased = $settings['register_column_based'] ?? 'line_number';
$this->excelRowHandler = new ExcelRowHandler();
$this->documentsAdded = 0; // Reset counter for each document type
// Set documents array for dynamic type order mapping
if (isset($settings['all_documents']) && is_array($settings['all_documents'])) {
$this->excelRowHandler->setDocuments($settings['all_documents']);
Log::debug("ExcelRowHandler initialized with documents", [
'processor' => class_basename($this),
'documents_count' => count($settings['all_documents'])
]);
}
// Setup log file path
$lineIdentifier = $weldLogData[$this->registerColumnBased] ?? 'unknown';
$basePath = $settings['path'] ?? '';
$this->logFilePath = "{$basePath}/{$lineIdentifier}/log.txt";
}
/**
* Normalize search term for file matching
*/
protected function normalizeSearchTerm(string $term): string
{
$term = str_replace("/", "*", $term);
$term = str_replace(" ", "*", $term);
$term = str_replace("\\", "*", $term);
return $term;
}
/**
* Search for files using glob pattern
*/
protected function searchFiles(string $pattern): array
{
Log::debug('Searching for files', ['pattern' => $pattern]);
$files = glob($pattern);
if ($files === false) {
$files = [];
}
Log::debug('Files found', ['count' => count($files)]);
return $files;
}
/**
* Get full folder path for current line
*/
protected function getFullFolder(): string
{
$path = $this->settings['path'] ?? '';
$lineIdentifier = $this->weldLogData[$this->registerColumnBased] ?? 'unknown';
$basePath = "storage/documents/{$path}";
return "{$basePath}/{$lineIdentifier}/";
}
/**
* Write to log file
* Note: This method is now silent to keep log files clean
* Only errors and NOT FOUND messages are logged
*/
protected function log(string $message, string $level = 'info'): void
{
// Don't write info messages to log file anymore - keep it clean
// Only errors will be logged via ExcelRowHandler
// Only log to Laravel debug for development purposes
Log::debug($message, [
'processor' => class_basename($this),
'line' => $this->weldLogData[$this->registerColumnBased] ?? 'unknown'
]);
}
/**
* Get contractor name
*/
protected function getContractor(): string
{
$subcontractors = \Cache::get("subcontractors", []);
$contractorKey = $this->weldLogData['contractor'] ?? '';
if (isset($subcontractors[$contractorKey])) {
return $subcontractors[$contractorKey]->company_name_ru ?? $contractorKey;
}
return $contractorKey;
}
/**
* Add row to Excel using ExcelRowHandler service
*
* @param array $search File paths to search for
* @param string $lineNumber Line number or identifier
* @param string $documentDate Document date
*
* @return int Next row position
*/
protected function addRowToExcel(
array $search,
string $lineNumber,
string $documentDate
): int {
$fullFolder = $this->getFullFolder();
$override = $this->settings['override'] ?? false;
// Calculate current row number based on starting row + documents added so far
$startRowNo = $this->settings['row_no'] ?? 1;
$currentRowNo = $startRowNo + $this->documentsAdded;
Log::info("🟢 AbstractDocumentProcessor->addRowToExcel() CALLED", [
'processor' => class_basename($this),
'row_no' => $currentRowNo,
'start_row_no' => $startRowNo,
'documents_added' => $this->documentsAdded,
'doc_type' => $this->document['type'] ?? 'N/A',
'doc_title2' => $this->document['title2'] ?? 'N/A',
'line' => $lineNumber,
'search_files' => count($search)
]);
$oldCurrentRow = $this->currentRow;
// Ensure the actual line identifier is available in the document array
// This allows processors (like TemplateProcessor) to override $lineNumber for Excel
// while still preserving the real line number for filename generation.
$this->document['real_line_identifier'] = $this->weldLogData[$this->registerColumnBased] ?? $lineNumber;
$newRow = $this->excelRowHandler->addRow(
$search,
$this->document,
$fullFolder,
$lineNumber,
$documentDate,
$currentRowNo,
$this->sheet,
$this->currentRow,
$override
);
// If a row was added, increment the documents counter and update current row
if ($newRow > $oldCurrentRow) {
$this->documentsAdded++;
$this->currentRow = $newRow; // ← UPDATE CURRENT ROW!
}
return $newRow;
}
/**
* Get the number of documents added during processing
*/
public function getDocumentsAdded(): int
{
return $this->documentsAdded;
}
}