account_number = $account_number; $this->period = $period; $this->saldo = $saldo; $this->disk = $disk; $this->fileName = "{$account_number}_{$period}.csv"; } /** * Execute the job. */ public function handle() : void { try { Log::info("Starting export statement job for account: {$this->account_number}, period: {$this->period}"); $stmt = $this->getStatementData(); $mappedData = $this->mapStatementData($stmt); $this->exportToCsv($mappedData); Log::info("Export statement job completed successfully for account: {$this->account_number}, period: {$this->period}"); } catch (Exception $e) { Log::error("Error in ExportStatementJob: " . $e->getMessage()); throw $e; } } /** * Get statement data from database */ private function getStatementData() { return StmtEntry::with(['ft', 'transaction']) ->where('account_number', $this->account_number) ->where('booking_date', $this->period) ->orderBy('date_time', 'ASC') ->orderBy('trans_reference', 'ASC') ->get(); } /** * Map statement data to the required format */ private function mapStatementData($stmt) { $runningBalance = (float) $this->saldo; // Map the data to transform or format specific fields $mappedData = $stmt->sortBy(['ACTUAL.DATE', 'REFERENCE.NUMBER']) ->map(function ($item, $index) use (&$runningBalance) { $runningBalance += (float) $item->amount_lcy; return [ 'NO' => 0, // Will be updated later 'TRANSACTION.DATE' => Carbon::createFromFormat('YmdHi', $item->booking_date . substr($item->ft?->date_time ?? '0000000000', 6, 4)) ->format('d/m/Y H:i'), 'REFERENCE.NUMBER' => $item->trans_reference, 'TRANSACTION.AMOUNT' => $item->amount_lcy, 'TRANSACTION.TYPE' => $item->amount_lcy < 0 ? 'D' : 'C', 'DESCRIPTION' => $this->generateNarrative($item), 'END.BALANCE' => $runningBalance, 'ACTUAL.DATE' => Carbon::createFromFormat('ymdHi', $item->ft?->date_time ?? '2505120000') ->format('d/m/Y H:i'), ]; }) ->values(); // Apply sequential numbers return $mappedData->map(function ($item, $index) { $item['NO'] = $index + 1; return $item; }); } /** * Generate narrative for a statement entry */ private function generateNarrative($item) { $narr = ''; if ($item->transaction->narr_type) { $narr .= $item->transaction->stmt_narr . ' '; $narr .= $this->getFormatNarrative($item->transaction->narr_type, $item); } else { $narr .= $item->transaction->stmt_narr . ' '; } if ($item->ft?->recipt_no) { $narr .= 'Receipt No: ' . $item->ft->recipt_no; } return $narr; } /** * Get formatted narrative based on narrative type */ private function getFormatNarrative($narr, $item) { $narrParam = TempStmtNarrParam::where('_id', $narr)->first(); if (!$narrParam) { return ''; } $fmt = ''; if ($narrParam->_id == 'FTIN') { $fmt = 'FT.IN'; } else if ($narrParam->_id == 'FTOUT') { $fmt = 'FT.IN'; } else { $fmt = $narrParam->_id; } $narrFormat = TempStmtNarrFormat::where('_id', $fmt)->first(); if (!$narrFormat) { return ''; } // Get the format string from the database $formatString = $narrFormat->text_data ?? ''; // Parse the format string // Split by the separator ']' $parts = explode(']', $formatString); $result = ''; foreach ($parts as $index => $part) { if (empty($part)) { continue; } if ($index === 0) { // For the first part, take only what's before the '!' $splitPart = explode('!', $part); if (count($splitPart) > 0) { // Remove quotes, backslashes, and other escape characters $cleanPart = trim($splitPart[0]); // Remove quotes at the beginning and end $cleanPart = preg_replace('/^["\'\\\\]+|["\'\\\\]+$/', '', $cleanPart); // Remove any remaining backslashes $cleanPart = str_replace('\\', '', $cleanPart); // Remove any remaining quotes $cleanPart = str_replace('"', '', $cleanPart); $result .= $cleanPart; } } else { // For other parts, these are field placeholders $fieldName = strtolower(str_replace('.', '_', $part)); // Get the corresponding parameter value from narrParam $paramValue = null; // Check if the field exists as a property in narrParam if (property_exists($narrParam, $fieldName)) { $paramValue = $narrParam->$fieldName; } else if (isset($narrParam->$fieldName)) { $paramValue = $narrParam->$fieldName; } // If we found a value, add it to the result if ($paramValue !== null) { $result .= $paramValue; } else { // If no value found, try to use the original field name as a fallback if ($fieldName != 'recipt_no') { $result .= $this->getTransaction($item->trans_reference, $fieldName) . ' '; } } } } return $result; } /** * Get transaction data by reference and field */ private function getTransaction($ref, $field) { $trans = TempFundsTransfer::where('ref_no', $ref)->first(); return $trans->$field ?? ""; } /** * Export data to CSV file */ private function exportToCsv($mappedData) { $csvContent = ''; // Add headers $csvContent .= implode('|', array_keys($mappedData[0])) . "\n"; // Add data rows foreach ($mappedData as $row) { $csvContent .= implode('|', $row) . "\n"; } // Save to storage Storage::disk($this->disk)->put("statements/{$this->fileName}", $csvContent); Log::info("Statement exported to {$this->disk} disk: statements/{$this->fileName}"); } }