select("line_number") ) ->groupBy("line_number") ->get() ->pluck("line_number") ->toArray(); } else { $drawings = explode(",", get("line_number")); } $count = 0; foreach($drawings AS $draw) { $documentInfo = document_template("Welding_Jurnal"); $j = j($documentInfo->json); // Load your Excel template file $spreadsheet = IOFactory::load('storage/documents/' . $documentInfo->files); // Get the active sheet or a specific sheet where you want to make replacements $sheet = $spreadsheet->getActiveSheet(); // or $spreadsheet->getSheetByName('SheetName'); $fileName = $draw; $weldLogs = apply_welded_filter( db("weld_logs")->where("line_number", $draw) ) ->whereNotNull("welding_date") ->orderBy("welding_date", "ASC") ->get(); $line_list_fluid_ru = ""; $allRowData = ""; $basinc = ""; $sicaklik = ""; if($weldLogs->count() != 0) { $drawing_no = $weldLogs[0]->line_number; $cover_zone = __($weldLogs[0]->project); $first_welding_date = df( apply_welded_filter( db("weld_logs")->where("line_number", $draw) ) ->whereNotNull("welding_date") ->orderBy("welding_date", "ASC") ->first()->welding_date ); $last_welding_date = df(apply_welded_filter( db("weld_logs")->where("line_number", $draw) ) ->selectRaw('MAX(GREATEST( COALESCE(welding_date, "1000-01-01"), COALESCE(vt_test_date, "1000-01-01"), COALESCE(pt_test_date, "1000-01-01"), COALESCE(mt_test_date, "1000-01-01"), COALESCE(ut_test_date, "1000-01-01"), COALESCE(rt_test_date, "1000-01-01"), COALESCE(pmi_test_date, "1000-01-01"), COALESCE(ht_test_date, "1000-01-01"), COALESCE(ferrite_test_date, "1000-01-01") )) AS latest_date') ->first()->latest_date); $lineList = db("line_lists") ->where("line_no", $weldLogs[0]->line_number) ->where("fluid_code", $weldLogs[0]->fluid_code) ->first(); if($lineList) { $line_list_fluid_ru = $lineList->fluid_ru; } else { Log::error("{$weldLogs[0]->line_number} {$weldLogs[0]->fluid_code} datası bulunamadı Generate ZHCP Welding Journal ERROR! "); } $rowNumber = 1; $work_permit_documents = db("work_permit_documents")->where("duty", "NDT")->where("level", 3)->first(); $work_permit_documents2 = db("work_permit_documents")->where("duty", "инженер по сварке")->where("level", 3)->first(); $templateRowNumber = 25; $startRow = $templateRowNumber; //$sheet->getRowDimension($templateRowNumber)->setRowHeight(100); $highestRow = $sheet->getHighestRow(); // En yüksek satır numarasını al $templateRowCells = []; $mergedCells = []; // Iterate through the template row and save cell values and merge information foreach ($sheet->getRowIterator($startRow)->current()->getCellIterator() as $cell) { $templateRowCells[] = $cell->getValue(); } foreach($weldLogs AS $weldLog) { if ($startRow > $highestRow) { // $startRow = $highestRow; // Veya bir önceki satır: $highestRow - 1 } $basinc = $weldLog->operating_pressure_mpa; $sicaklik = $weldLog->operating_temperature_s; $weldLog->welding_date = rejected_date($weldLog->welding_date) ? "-" : df($weldLog->welding_date); $weldLog->vt_test_date = rejected_date($weldLog->vt_test_date) ? "-" : df($weldLog->vt_test_date); $weldLog->pt_test_date = rejected_date($weldLog->pt_test_date) ? "-" : df($weldLog->pt_test_date); $weldLog->mt_test_date = rejected_date($weldLog->mt_test_date) ? "-" : df($weldLog->mt_test_date); $weldLog->ut_test_date = rejected_date($weldLog->ut_test_date) ? "-" : df($weldLog->ut_test_date); $weldLog->rt_test_date = rejected_date($weldLog->rt_test_date) ? "-" : df($weldLog->rt_test_date); $weldLog->pmi_test_date = rejected_date($weldLog->pmi_test_date) ? "-" : df($weldLog->pmi_test_date); $weldLog->ht_test_date = rejected_date($weldLog->ht_test_date) ? "-" : df($weldLog->ht_test_date); $weldLog->ferrite_test_date = rejected_date($weldLog->ferrite_test_date) ? "-" : df($weldLog->ferrite_test_date); $weldLog->pwht_date = rejected_date($weldLog->pwht_date) ? "-" : df($weldLog->pwht_date); $naksWelder = db("naks_welders")->where("welder_id", $weldLog->welder_1)->first(); if($weldLog->welder_2 != "") { $naksWelder2 = db("naks_welders")->where("welder_id", $weldLog->welder_2)->first(); } $col24 = ""; if($work_permit_documents) { $col24 = "{$work_permit_documents->name_surname}, {$work_permit_documents->certificate_number}"; } $col25 = ""; if($work_permit_documents2) { $col25 = "{$work_permit_documents2->name_surname}, {$work_permit_documents2->certificate_number}"; } $wps = db("w_p_s")->where("details", $weldLog->wps_no)->first(); $mtos = db("incoming_controls")->where("certificate_no", $weldLog->certificate_number_of_1)->first(); $mtos2 = db("incoming_controls")->where("certificate_no", $weldLog->certificate_number_of_2)->first(); $naks_consumables = db("naks_consumables")->where("inspection_test_report", $weldLog->welding_materials_2_certificate_no)->first(); $naks_consumables2 = db("naks_consumables")->where("inspection_test_report", $weldLog->welding_materials_1_certificate_no)->first(); $naks_consumables3 = db("naks_consumables")->where("inspection_test_report", $weldLog->welding_materials_3_certificate_no)->first(); $naksConsumablesData = ""; $naksConsumablesData2 = ""; $naksConsumablesData3 = ""; if($naks_consumables) { $certification_date = df($naks_consumables->certification_date); $reportNumber = $naks_consumables->inspection_test_report; $naksConsumablesData = "⌀{$naks_consumables->diameter} {$naks_consumables->inspection_certificate_standart}, серт. $reportNumber от $certification_date"; } if($naks_consumables2) { $certification_date = df($naks_consumables2->certification_date); $reportNumber = $naks_consumables2->inspection_test_report; $naksConsumablesData2 = "⌀{$naks_consumables2->diameter} {$naks_consumables2->inspection_certificate_standart}, серт. $reportNumber от $certification_date"; } if($naks_consumables3) { $certification_date = df($naks_consumables3->certification_date); $reportNumber = $naks_consumables3->inspection_test_report; $naksConsumablesData3 = " ⌀{$naks_consumables3->diameter} {$naks_consumables3->inspection_certificate_standart}, серт. $reportNumber от $certification_date"; } $welders = "{$naksWelder?->welder_name_ru}, {$weldLog->welder_1}"; if($weldLog->welder_2 != "") { $welders .= "+ {$naksWelder2?->welder_name_ru}, {$weldLog->welder_2}"; } $jointTypeRu = ""; if($wps) { $jointTypeRu = $wps->joint_type_ru; } $manufacturing_standard_1 = ""; if($mtos) { $manufacturing_standard_1 = $mtos?->manufacturing_standard; } $manufacturing_standard_2 = ""; if($mtos2) { $manufacturing_standard_2 = $mtos?->manufacturing_standard; } if(strpos($weldLog->welding_materials_3, "Aргон") !== false) { $col6 = "-"; } else { $col6 = "{$weldLog->welding_materials_3} $naksConsumablesData3"; } if(strpos($weldLog->welding_materials_3, "Aргон") !== false) { $col7 = "{$weldLog->welding_materials_3} $naksConsumablesData3"; } else { $col7 = "-"; } $tempData = $temperatures[date("z", strtotime($weldLog->welding_date))]; if($weldLog->type_of_joint == "S") { $thisTempData = $tempData['shop_ambient']; $thisTempData2 = $tempData['temp_material_shop']; } else { $thisTempData = $tempData['field_ambient']; $thisTempData2 = $tempData['temp_material_field']; } $col9 = "20 - 50 °С"; if($thisTempData>=5) { $col9 = "просушка 20 - 50 °С"; } $col10 = "Н/П"; if($weldLog->rt_report !="") { $col19 = "РК"; } elseif($weldLog->ut_report !="") { $col19 = "узк"; } else { $col19 = "-"; } $pmiData = "н/п"; if($weldLog->pmi_report !="") { $pmiData = "Заключение №{$weldLog->pmi_report} от {$weldLog->pmi_test_date} годен "; } $htData = "н/п"; if($weldLog->ht_report !="") { $htData = "Заключение №{$weldLog->ht_report} от {$weldLog->ht_test_date} годен"; } $vtData = ""; if($weldLog->vt_report !="") { $vtData = "Акт №{$weldLog->vt_report} от {$weldLog->vt_test_date} годен \n "; } $ptData = ""; if($weldLog->pt_report !="") { $ptData = "ЦД: Заключение №{$weldLog->pt_report} от {$weldLog->pt_test_date} годен \n "; } $mtData = ""; if($weldLog->mt_report !="") { $mtData = "Мк: Заключение №{$weldLog->mt_report} от {$weldLog->mt_test_date} годен \n"; } $rtData = ""; if($weldLog->rt_report !="") { $rtData = "Рк: Заключение №{$weldLog->rt_report} от {$weldLog->rt_test_date} годен \n"; } $utData = ""; if($weldLog->ut_report !="") { $utData = "узк: Заключение №{$weldLog->ut_report} от {$weldLog->ut_test_date} годен \n"; } $ferriteData = "н/п"; if($weldLog->ferrite_report !="") { $ferriteData = "Заключение №{$weldLog->ferrite_report} от {$weldLog->ferrite_test_date} годен \n"; } if($weldLog->ut_report == "" && $weldLog->rt_report == "") { $utData = "н/п"; } $pwhtData = "н/п"; if($weldLog->pwht !="NO") { $pwhtData = "-"; } $diagram_number_pwht = "н/п"; if($weldLog->diagram_number_pwht !="") { $diagram_number_pwht = $weldLog->diagram_number_pwht; } $weldingMethod = $weldLog->welding_method; $weldingMethod = str_replace("/141", "", $weldingMethod); $weldingMethod = str_replace("/111", "", $weldingMethod); $weldingMethod = str_replace(",", " + ", $weldingMethod); $naksConsumablesTitle = "Проволока"; if($weldLog->welding_materials_2 != "") { $naksConsumablesTitle = "Электроды"; } // Save merged cell ranges in the template row foreach ($sheet->getMergeCells() as $mergeRange) { // Check if the merge range starts in the template row if (preg_match('/^\D*'.$startRow.'$/', explode(':', $mergeRange)[0])) { $mergedCells[] = $mergeRange; } } // Insert a new row before the next one (clone the template row) $sheet->insertNewRowBefore($startRow + 1, 1); // Get the newly inserted row $newRow = $sheet->getRowIterator($startRow + 1)->current(); // Copy template row's values to the new row $cellIndex = 0; // To keep track of which cell we're on $incomingCertificate1 = db("incoming_controls")->where("certificate_no", $weldLog->certificate_number_of_1)->select("manufacturing_standard")->first()?->manufacturing_standard; $incomingCertificate2 = db("incoming_controls")->where("certificate_no", $weldLog->certificate_number_of_2)->select("manufacturing_standard")->first()?->manufacturing_standard; $rowReplacements = [ '{1}' => $rowNumber, '{2}' => "{$weldLog->type_of_joint}{$weldLog->no_of_the_joint_as_per_as_built_survey} - $jointTypeRu", '{3}' => $welders, '{4}' => "{$weldLog->member_no_1} $incomingCertificate1, {$weldLog->material_no_1}, $manufacturing_standard_1 серт.№ {$weldLog->certificate_number_of_1} \n + \n {$weldLog->member_no_2}, {$weldLog->material_no_2} $incomingCertificate2, $manufacturing_standard_2 серт.№ {$weldLog->certificate_number_of_2}", '{5}' => "Ø{$weldLog->outside_diameter_1} x {$weldLog->wall_thickness_1} \n Ø{$weldLog->outside_diameter_2} x {$weldLog->wall_thickness_2}", '{6}' => "{$weldLog->welding_date} \n $thisTempData °С", '{7}' => $weldingMethod, '{8}' => "$naksConsumablesTitle {$weldLog->welding_materials_1} $naksConsumablesData, \n {$weldLog->welding_materials_2} $naksConsumablesData2, \n {$weldLog->welding_materials_3} $naksConsumablesData3", '{9}' => $col9, '{10}' => $col10, '{11}' => $pmiData, '{12}' => $pwhtData, '{13}' => $diagram_number_pwht, '{14}' => $htData, '{15}' => "$vtData $ptData $mtData", '{16}' => "$rtData $utData", '{17}' => $ferriteData, // Gerekirse daha fazla hücre ekleyebilirsiniz ]; // dump($rowReplacements); $lineCount = 10; foreach ($newRow->getCellIterator() as $newCell) { // Şablon değerini al $templateValue = $templateRowCells[$cellIndex]; /* foreach ($cleanupPatterns as $pattern) { $newCellValue = str_replace($pattern, "", $newCellValue); } */ // Şablon değeri numaralandırılmış yer tutucu mu kontrol et if (isset($rowReplacements[$templateValue])) { // Yer tutucuyu gerçek değerle değiştir $newCell->setValue($rowReplacements[$templateValue]); $cellLineCount = substr_count($rowReplacements[$templateValue], "\n") + 1; // Satır sayısını hesapla (yeni satır karakterine göre) if($cellLineCount>$lineCount) { $lineCount = $cellLineCount; } } else { // Eğer yer tutucu değilse, orijinal değeri koru veya farklı bir işlem yap // $newCell->setValue($templateValue); } $cellIndex++; // Bir sonraki hücreye geç } // Apply merged cells to the new row foreach ($mergedCells as $mergeRange) { // Adjust the merge range to the new row $adjustedMergeRange = preg_replace_callback('/\d+/', function($matches) use ($startRow) { return $matches[0] + 1; // Shift row numbers by 1 for the newly inserted row }, $mergeRange); // Apply the adjusted merge range to the new row $sheet->getRowDimension($startRow)->setRowHeight($lineCount * 150); $sheet->mergeCells($adjustedMergeRange); } // Move to the next row for the next weld log entry $startRow++; $rowNumber++; // dump($rowNumber); // After inserting all new rows, remove the original template row /* $rowData = str_replace(", - +", "", $rowData); $rowData = str_replace(", +", "", $rowData); $rowData = str_replace(", -", "", $rowData); $rowData = str_replace(", \n", "", $rowData); $rowData = str_replace("Φ \n", "", $rowData); $rowData = str_replace("+ \n", "", $rowData); $allRowData .= $rowData; $k++; */ } $sheet->removeRow($templateRowNumber); $replacements = [ '{iso_number}' => $draw, '{basinc}' => $basinc, '{sicaklik}' => $sicaklik, '{line_list_fluid_ru}' => $line_list_fluid_ru, '{drawing_no}' => $drawing_no, '{first_welding_date}' => $first_welding_date, '{last_welding_date}' => $last_welding_date, '{cover_zone}' => $cover_zone, ]; // Loop through all the cells in the sheet and replace placeholders foreach ($sheet->getRowIterator() as $row) { foreach ($row->getCellIterator() as $cell) { $cellValue = $cell->getValue(); if (is_string($cellValue)) { foreach ($replacements as $placeholder => $replacement) { if (strpos($cellValue, $placeholder) !== false) { $newValue = str_replace($placeholder, $replacement, $cellValue); $cell->setValue($newValue); } } } } } $path = "005_PTO/02_ЖСР"; $path2 = "005_PTO/00_ИД/$fileName"; $path = "$path/$fileName"; $path2 = "$path2/ЖСР-$fileName"; $path = $documentInfo->kid; //"005_PTO/06_Прочность и плотность"; $fileName = str_replace("/", "-", $fileName); $excelPath = "storage/documents/$path/$fileName.xlsx"; if (!file_exists(dirname($excelPath))) { mkdir(dirname($excelPath), 0777, true); } // Save the modified spreadsheet $writer = IOFactory::createWriter($spreadsheet, 'Xlsx'); $writer->save($excelPath); $resultPDF = xlsx_to_pdf_legacy($excelPath, "storage/documents/$path"); $pdfFile = basename($resultPDF); //dd("ok"); /* html_create($path, $document, "landscape"); html_create($path2, $document, "landscape"); */ if(!getisset("all")) { if(!isset($redirect)) { yonlendir(url("storage/documents/$path/$pdfFile")); } } else { $count++; } } } if(getisset("all")) { echo $count; } ?>