From 7f8ba7b72bcab3c4c5b620a1e87109a53de81753 Mon Sep 17 00:00:00 2001 From: dhanabalan Date: Mon, 14 Sep 2026 15:43:41 +0530 Subject: [PATCH] Added process sftp file in commands --- app/Console/Commands/ProcessSftpFiles.php | 261 ++++++++++++++++++++++ 1 file changed, 261 insertions(+) create mode 100644 app/Console/Commands/ProcessSftpFiles.php diff --git a/app/Console/Commands/ProcessSftpFiles.php b/app/Console/Commands/ProcessSftpFiles.php new file mode 100644 index 0000000..3f29b93 --- /dev/null +++ b/app/Console/Commands/ProcessSftpFiles.php @@ -0,0 +1,261 @@ +sftpService = $sftpService; + } + + public function handle() + { + $files = Storage::disk('ftp')->allFiles(); + + $folders = Storage::disk('ftp')->directories(); + + $allFiles = []; + + foreach ($folders as $folder) { + + $this->info("Checking folder: $folder"); + + try { + + // Get files inside the current folder + $files = Storage::disk('ftp')->files($folder); + + foreach ($files as $file) { + $this->info("File: " . $file); + $allFiles[] = $file; + } + + } catch (\Exception $e) { + + $this->error("Cannot access folder: " . $folder); + } + } + + if (empty($allFiles)) { + $this->info('No files to process.'); + return; + } + + $logMessages = []; + + foreach ($allFiles as $file) + { + $this->info("Processing file: $file"); + + $folderName = explode('/', $file)[0]; + + $plant = Plant::where('code', $folderName)->first(); + + if (!$plant) { + $this->warn("No plant found for folder: $folderName. Skipping file."); + $logMessages[] = $folderName; + continue; + } + + $plantId = $plant->id; + + $plantCode = $plant->code ?? null; + + $fileName = basename($file); // GMGLAS03-1818199-1.txt + $workCenterCode = explode('-', $fileName)[0]; // GMGLAS03 + + $machine = Machine::where('work_center', $workCenterCode)->first(); + if (!$machine) { + $message = "Machine '$workCenterCode' not found and file name '$file'"; + $logMessages[] = $message; + continue; + } + + $machineAgaPlant = Machine::where('plant_id', $plantId)->where('work_center', $workCenterCode)->first(); + if (!$machineAgaPlant) { + $message = "Machine '$workCenterCode' not found for plant code '$plantCode' and file name '$file'"; + $logMessages[] = $message; + continue; + } + + $machineId = $machineAgaPlant->id; + + $content = $this->sftpService->getFileContent($file); + + $lines = array_filter(preg_split("/\r\n|\n|\r/", $content)); + + $headers = array_map(fn($header) => strtolower(trim($header)), str_getcsv(array_shift($lines))); + + $dbColumns = Schema::getColumnListing('production_temps'); + + $missingColumns = array_diff($headers, $dbColumns); + + if (!empty($missingColumns)) { + + $message = "Skipping file: $file. The following columns do not exist in the database. Please add them to the database: " . implode(', ', $missingColumns); + + $logMessages[] = $message; + + $this->warn($message); + + continue; + } + + $expectedColumns = count($headers); + $isValidFile = true; + $isserialNotEmptyFile = true; + + foreach ($lines as $lineNumber => $line) { + $data = str_getcsv($line); + + if (!empty($data[0])) { + if (count($data) != $expectedColumns) { + $message = "Column mismatch in file: $file at line " . ($lineNumber + 2) . + ". Expected $expectedColumns, got " . count($data); + + $logMessages[] = $message; + + $isValidFile = false; + break; + } + } + } + + if (!$isValidFile) { + $message = "Skipping entire file due to invalid column mismatch: $file"; + $logMessages[] = $message; + continue; + } + else if (!$isserialNotEmptyFile) { + $message = "serial number not found in file name $file"; + $logMessages[] = $message; + continue; + } + + $rows = []; + + // foreach ($lines as $line) { + // $data = str_getcsv($line); + + // if (!empty($data[0])) { + // $row = []; + // foreach ($headers as $index => $column) { + // $row[$column] = isset($data[$index]) ? trim($data[$index]) : null; + // } + + // $row['plant_id'] = $plantId; + // $row['machine_id'] = $machineId; + // $row['created_at'] = now(); + // $row['updated_at'] = now(); + + // // $rows[] = $row; + // if (!empty($row['gernr'])) { + // $rows[] = $row; + // } + // } + // } + + foreach ($lines as $line) { + + $data = str_getcsv($line); + + if (!empty($data[0])) { + + $row = []; + + foreach ($headers as $index => $column) { + $row[$column] = isset($data[$index]) + ? trim($data[$index]) + : null; + } + + $row['plant_id'] = $plantId; + $row['machine_id'] = $machineId; + $row['created_at'] = now(); + $row['updated_at'] = now(); + + if (empty($row['gernr'])) { + $message = "serial number not found in file name $file"; + $logMessages[] = $message; + continue; + } + + $gamng = (float) ($row['gamng'] ?? 0); + $lmnga = (float) ($row['lmnga'] ?? 0); + + $existingRecords = ProductionTemp::where('plant_id', $plantId) + ->where('aufnr', ($row['aufnr'])) + ->get(); + + + $existingRecord = $existingRecords->where('gernr', $row['gernr'])->first(); + + $lastStatus = $existingRecords->max('pending_released_status'); + + if ($gamng == $lmnga) { + + $row['pending_released_status'] = 0; + + $this->info("Production order {$row['aufnr']} completed. " ."Serial: {$row['gernr']}. " ."GAMNG: {$row['gamng']}, LMNGA: {$row['lmnga']}. " ."pending_released_status = 0" ); + + } elseif ($existingRecord) { + + $row['pending_released_status'] = (int) $existingRecord->pending_released_status; + + $this->info("Same serial already exists. " ."Production order: {$row['aufnr']}. " ."Serial: {$row['gernr']}. " ."Keeping pending_released_status = {$row['pending_released_status']}"); + + } else { + + if ($existingRecords->isEmpty()) { + + // First release + $row['pending_released_status'] = 0; + + } else { + $row['pending_released_status'] = ((int) $lastStatus) + 1; + } + $this->info("Production order {$row['aufnr']} pending. " ."New serial: {$row['gernr']}. " . "GAMNG: {$row['gamng']}, LMNGA: {$row['lmnga']}. " ."pending_released_status = {$row['pending_released_status']}"); + } + + $rows[] = $row; + } + } + + if (!empty($rows)) { + ProductionTemp::upsert($rows, ['plant_id', 'gernr'], array_keys($rows[0])); //(update + insert) + } + } + + if (!empty($logMessages)) { + + $content = implode(PHP_EOL, $logMessages); + + Mail::raw('Please find attached machine error logs.', function ($message) use ($content) { + $message->to(env('ERROR_LOG_MAIL')) + ->subject('Machine Error Logs') + ->attachData($content, 'machine_errors.txt', [ + 'mime' => 'text/plain', + ]); + }); + } + } +} -- 2.49.1