period = $period; } /** * Execute the job. */ public function handle() : void { try { set_time_limit(24 * 60 * 60); $disk = Storage::disk('sftpStatement'); $processedCount = 0; $errorCount = 0; if ($this->period === '')) { Log::warning('No period provided for funds transfer data processing'); return; } // Construct the filename based on the period folder name $filename = "{$this->period}.ST.FUNDS.TRANSFER.csv"; $filePath = "{$this->period}/$filename"; Log::info("Processing funds transfer file: $filePath"); if (!$disk->exists($filePath)) { Log::warning("File not found: $filePath"); return; } // Create a temporary local copy of the file $tempFilePath = storage_path("app/temp_$filename"); file_put_contents($tempFilePath, $disk->get($filePath)); $handle = fopen($tempFilePath, "r"); if ($handle !== false) { $headers = (new TempFundsTransfer())->getFillable(); $rowCount = 0; while (($row = fgetcsv($handle, 0, "~")) !== false) { $rowCount++; // Handle case where row has more columns than headers if (count($row) > count($headers)) { $row = array_slice($row, 0, count($headers)); } if (count($headers) === count($row)) { $data = array_combine($headers, $row); try { if (isset($data['_id']) && $data['_id'] !== '_id') { TempFundsTransfer::updateOrCreate( ['_id' => $data['_id']], $data ); $processedCount++; } } catch (Exception $e) { $errorCount++; Log::error("Error processing Funds Transfer at row $rowCount in $filePath: " . $e->getMessage()); } } else { Log::warning("Row $rowCount in $filePath has incorrect column count. Expected: " . count($headers) . ", Got: " . count($row)); } } fclose($handle); Log::info("Completed processing $filePath. Processed $processedCount records with $errorCount errors."); // Clean up the temporary file unlink($tempFilePath); } else { Log::error("Unable to open file: $filePath"); } Log::info("Funds Transfer data processing completed. Total processed: $processedCount, Total errors: $errorCount"); } catch (Exception $e) { Log::error('Error in ProcessFundsTransferDataJob: ' . $e->getMessage()); throw $e; } } }