$convertTerm, 'result' => $rowTitleRaw ]); } // Basit dosya kontrolü ve log hazırlığı $foundFiles = []; $notFoundFiles = []; foreach ($search as $searchPath) { if (file_exists($searchPath) || Storage::exists(str_replace("storage/documents/", "", $searchPath))) { $foundFiles[] = $searchPath; } else { $notFoundFiles[] = $searchPath; } } if(!empty($foundFiles)) { // İlk bulunan dosyayı kullan $firstFoundFile = $foundFiles[0]; // Dosya türü istatistiği $fileExtension = strtolower(pathinfo($firstFoundFile, PATHINFO_EXTENSION)); if (!isset($GLOBALS['file_type_stats'])) { $GLOBALS['file_type_stats'] = []; } if (!isset($GLOBALS['file_type_stats'][$fileExtension])) { $GLOBALS['file_type_stats'][$fileExtension] = 0; } $GLOBALS['file_type_stats'][$fileExtension]++; $order = $selectDocument['order'] + 1; $addInLineNumber = ['по сварке трубопроводов(ЖСР)']; if(in_array($selectDocument['title2'], $addInLineNumber)) { $fileName = "$order - {$selectDocument['title2']} - $lineNumber.pdf"; $fullPath = "$fullFolder"."$fileName"; } else { if(isset($selectDocument['file_name'])) { $fileName = "$order - {$selectDocument['file_name']}.pdf"; } else { if(isset($selectDocument['title2'])) { $fileName = "$order - {$selectDocument['title2']}.pdf"; } else { $fileName = "$order - {$selectDocument['title2']}.pdf"; } } $fullPath = "$fullFolder"."$fileName"; } Log::debug("File name: " . $fileName); $documentDate = df($documentDate); // Sayfa sayısını al $allPath = "{$firstFoundFile}"; $command = "pdftk '$allPath' dump_data | grep NumberOfPages"; putenv('LANG=ru_RU.UTF-8'); $output = shell_exec($command); try { $pageCount = (int) trim(explode(" ", $output)[1]); } catch (\Throwable $th) { $pageCount = 1; } if($firstPage == 0) { $firstPage = 1; } else { $firstPage = $lastPage + 1; } $lastPage = $firstPage + $pageCount - 1; Cache::put("rc_lastPage", $lastPage); Cache::put("rc_firstPage", $firstPage); // Basit log formatı oluştur $simpleLog = ""; $simpleLog .= "📋 " . ($rowNo) . ". " . $rowTitle . " - " . $lineNumber . " | " . count($foundFiles) . " file(s)\n"; foreach ($foundFiles as $index => $file) { $simpleLog .= " -- " . basename($file) . "\n"; } if (!empty($notFoundFiles)) { $simpleLog .= " ❓ Not found: " . count($notFoundFiles) . " file(s)\n"; foreach ($notFoundFiles as $index => $file) { $simpleLog .= " -- " . basename($file) . " (missing)\n"; } } $simpleLog .= "\n"; // Sayfa aralığını oluştur if ($pageCount == 1) { if($firstPage == 1) { $pageIndex = $firstPage; } else { $prevPage = $firstPage - 1; $pageIndex = "$prevPage - $firstPage"; } } else { $pageIndex = "$firstPage - $lastPage"; } // Excel'e veri ekle try { $templateRowCells = []; $templateFormulas = []; $templateRow = Cache::get("rc_template_row", $currentRow); foreach ($sheet->getRowIterator($templateRow, $templateRow)->current()->getCellIterator() as $cell) { $colIndex = $cell->getColumn(); $templateRowCells[$colIndex] = $cell->getValue(); if ($cell->isFormula()) { $templateFormulas[$colIndex] = $cell->getValue(); } } $mergedCells = []; foreach ($sheet->getMergeCells() as $mergeRange) { if (preg_match('/^\D*'.$templateRow.'$/', explode(':', $mergeRange)[0])) { $mergedCells[] = $mergeRange; } } $sheet->insertNewRowBefore($currentRow + 1, 1); $replacements = []; $replacements['{rowNo}'] = $rowNo; $replacements['{rowTitle}'] = $rowTitle; $replacements['{documentReportNumber}'] = $lineNumber; $replacements['{documentDate}'] = $documentDate; $replacements['{constructor}'] = $constructor; $replacements['{pageCount}'] = $pageCount; $replacements['{pageIndex}'] = $pageIndex; // Safe get with type checking for all title fields $replacements['{title1}'] = isset($selectDocument['title1']) ? (is_array($selectDocument['title1']) ? json_encode($selectDocument['title1']) : (string)$selectDocument['title1']) : ''; $replacements['{ndt_report_no}'] = isset($selectDocument['title2']) ? (is_array($selectDocument['title2']) ? json_encode($selectDocument['title2']) : (string)$selectDocument['title2']) : ''; $replacements['{pdf_document_title}'] = isset($selectDocument['title3']) ? (is_array($selectDocument['title3']) ? json_encode($selectDocument['title3']) : (string)$selectDocument['title3']) : ''; $replacements['{ndt_register_title}'] = isset($selectDocument['title4']) ? (is_array($selectDocument['title4']) ? json_encode($selectDocument['title4']) : (string)$selectDocument['title4']) : ''; foreach ($templateRowCells as $colIndex => $cellValue) { if (isset($templateFormulas[$colIndex])) { $formula = $templateFormulas[$colIndex]; $rowOffset = ($currentRow + 1) - $templateRow; $updatedFormula = preg_replace_callback( '/([A-Z]+)(\d+)/', function($matches) use ($rowOffset) { $col = $matches[1]; $row = intval($matches[2]); $row += $rowOffset; return $col . $row; }, $formula ); $sheet->setCellValue($colIndex . ($currentRow + 1), $updatedFormula); } else { if (is_string($cellValue)) { foreach ($replacements as $placeholder => $replacement) { if (strpos($cellValue, $placeholder) !== false) { $cellValue = str_replace($placeholder, $replacement, $cellValue); } } } $sheet->setCellValue($colIndex . ($currentRow + 1), $cellValue); } } foreach ($mergedCells as $mergeRange) { $adjustedMergeRange = preg_replace_callback('/\d+/', function($matches) use ($currentRow, $templateRow) { return $matches[0] == $templateRow ? $currentRow + 1 : $matches[0]; }, $mergeRange); try { $sheet->mergeCells($adjustedMergeRange); } catch (\Throwable $th) { // Hata önemsiz } } foreach ($sheet->getRowIterator($templateRow, $templateRow)->current()->getCellIterator() as $cell) { $colIndex = $cell->getColumn(); try { $style = $sheet->getStyle($colIndex . $templateRow); $sheet->duplicateStyle($style, $colIndex . ($currentRow + 1)); } catch (\Throwable $th) { // Hata önemsiz } } $templateRowHeight = $sheet->getRowDimension($templateRow)->getRowHeight(); $sheet->getRowDimension($currentRow + 1)->setRowHeight($templateRowHeight); } catch (\Throwable $th) { $simpleLog .= " ❌ Excel error: " . $th->getMessage() . "\n\n"; throw $th; } // Dosya kopyalama $originalSearchPath = $firstFoundFile; $search[0] = str_replace("storage/documents/", "", $firstFoundFile); $fullFolder = str_replace("storage/documents/", "", $fullFolder); $fullPath = $fullFolder . basename($fileName); try { $targetDir = dirname($fullPath); if (!Storage::exists($targetDir)) { Storage::makeDirectory($targetDir); } $copyStatus = ""; if(Storage::exists($fullPath)) { if ($override) { Storage::delete($fullPath); $copyStatus = "overwritten"; } else { $currentContent = md5(Storage::get($fullPath)); $newContent = ''; if (Storage::exists($search[0])) { $newContent = md5(Storage::get($search[0])); } else if (file_exists($originalSearchPath)) { $newContent = md5(file_get_contents($originalSearchPath)); } if ($currentContent !== $newContent) { Storage::delete($fullPath); $copyStatus = "updated"; } else { $copyStatus = "same content, skipped"; } } } else { $copyStatus = "copied"; } if ($copyStatus !== "same content, skipped") { if (Storage::exists($search[0])) { Storage::copy($search[0], $fullPath); } else if (file_exists($originalSearchPath)) { $fileContent = file_get_contents($originalSearchPath); Storage::put($fullPath, $fileContent); } } $simpleLog .= " 📁 File: " . $copyStatus . "\n"; // Template dosyası kontrolü (xlsx) if(isset($selectDocument['type']) && $selectDocument['type'] === 'template') { $xlsxSourcePath = str_replace('.pdf', '.xlsx', $search[0]); $xlsxOriginalSourcePath = str_replace('.pdf', '.xlsx', $originalSearchPath); $xlsxTargetPath = str_replace('.pdf', '.xlsx', $fullPath); if(Storage::exists($xlsxSourcePath) || file_exists($xlsxOriginalSourcePath)) { $xlsxCopyStatus = ""; if(Storage::exists($xlsxTargetPath)) { if ($override) { Storage::delete($xlsxTargetPath); $xlsxCopyStatus = "overwritten"; } else { $currentXlsxContent = md5(Storage::get($xlsxTargetPath)); $newXlsxContent = ''; if (Storage::exists($xlsxSourcePath)) { $newXlsxContent = md5(Storage::get($xlsxSourcePath)); } else if (file_exists($xlsxOriginalSourcePath)) { $newXlsxContent = md5(file_get_contents($xlsxOriginalSourcePath)); } if ($currentXlsxContent !== $newXlsxContent) { Storage::delete($xlsxTargetPath); $xlsxCopyStatus = "updated"; } else { $xlsxCopyStatus = "same content, skipped"; } } } else { $xlsxCopyStatus = "copied"; } if ($xlsxCopyStatus !== "same content, skipped") { if (Storage::exists($xlsxSourcePath)) { Storage::copy($xlsxSourcePath, $xlsxTargetPath); } else if (file_exists($xlsxOriginalSourcePath)) { $xlsxContent = file_get_contents($xlsxOriginalSourcePath); Storage::put($xlsxTargetPath, $xlsxContent); } } $simpleLog .= " 📊 XLSX: " . $xlsxCopyStatus . "\n"; } else { $simpleLog .= " ❓ XLSX: not found\n"; } } } catch (\Throwable $th) { $simpleLog .= " ❌ Copy error: " . $th->getMessage() . "\n"; } $simpleLog .= "\n"; // Basit log'u dosyaya ve ekrana yaz echo($simpleLog); Storage::append($fullFolder . 'log.txt', $simpleLog); // Başarılı işlem sayacı if (!isset($GLOBALS['successful_operations'])) { $GLOBALS['successful_operations'] = 0; } $GLOBALS['successful_operations']++; return $currentRow + 1; } else { // Dosya bulunamadı - basit log $notFoundLog = "❌ " . ($rowNo) . ". " . $rowTitle . " - " . $lineNumber . " | NO FILES FOUND\n"; $notFoundLog .= " Search paths:\n"; foreach ($search as $index => $searchPath) { $notFoundLog .= " -- " . basename($searchPath) . "\n"; } $notFoundLog .= "\n"; echo($notFoundLog); try { $fullFolder = str_replace("storage/documents/", "", $fullFolder); Storage::append($fullFolder . 'log.txt', $notFoundLog); } catch (\Throwable $logError) { echo("❌ Log write error\n"); } return $currentRow; } } catch (\Throwable $th) { Log::error("Unexpected error in addRowInTable: " . $th->getMessage()); try { $errorLog = "❌ " . ($rowNo ?? 'UNKNOWN') . ". ERROR: " . $th->getMessage() . "\n\n"; $fullFolder = str_replace("storage/documents/", "", $fullFolder ?? ''); Storage::append($fullFolder . 'log.txt', $errorLog); echo($errorLog); } catch (\Throwable $logError) { echo("❌ Fatal error occurred\n"); } throw $th; } }