Compare commits
3 Commits
9f0ee812a9
...
3414fd9414
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3414fd9414 | ||
|
|
cb0a248ce5 | ||
|
|
57463f2429 |
@@ -55,15 +55,7 @@
|
||||
try {
|
||||
Log::info("Starting export statement job for account: {$this->account_number}, period: {$this->period}");
|
||||
|
||||
// Cek apakah data sudah diproses sebelumnya
|
||||
$existingData = ProcessedStatement::where('account_number', $this->account_number)
|
||||
->where('period', $this->period)
|
||||
->exists();
|
||||
|
||||
if (!$existingData) {
|
||||
// Jika belum ada data yang diproses, lakukan pemrosesan
|
||||
$this->processStatementData();
|
||||
}
|
||||
$this->processStatementData();
|
||||
|
||||
// Export data yang sudah diproses ke CSV
|
||||
$this->exportToCsv();
|
||||
@@ -75,79 +67,129 @@
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Process statement data and save to database
|
||||
*/
|
||||
private function processStatementData()
|
||||
: void
|
||||
{
|
||||
// Hapus data yang mungkin sudah ada untuk kombinasi account dan period yang sama
|
||||
ProcessedStatement::where('account_number', $this->account_number)
|
||||
->where('period', $this->period)
|
||||
->delete();
|
||||
$accountQuery = [
|
||||
'account_number' => $this->account_number,
|
||||
'period' => $this->period
|
||||
];
|
||||
|
||||
$totalCount = $this->getTotalEntryCount($accountQuery);
|
||||
$existingDataCount = $this->getExistingProcessedCount($accountQuery);
|
||||
|
||||
// Hanya proses jika data belum lengkap diproses
|
||||
if ($existingDataCount !== $totalCount) {
|
||||
$this->deleteExistingProcessedData($accountQuery);
|
||||
$this->processAndSaveStatementEntries($totalCount);
|
||||
}
|
||||
}
|
||||
|
||||
private function getTotalEntryCount(array $criteria)
|
||||
: int
|
||||
{
|
||||
return StmtEntry::where('account_number', $criteria['account_number'])
|
||||
->where('booking_date', $criteria['period'])
|
||||
->count();
|
||||
}
|
||||
|
||||
private function getExistingProcessedCount(array $criteria)
|
||||
: int
|
||||
{
|
||||
return ProcessedStatement::where('account_number', $criteria['account_number'])
|
||||
->where('period', $criteria['period'])
|
||||
->count();
|
||||
}
|
||||
|
||||
private function deleteExistingProcessedData(array $criteria)
|
||||
: void
|
||||
{
|
||||
ProcessedStatement::where('account_number', $criteria['account_number'])
|
||||
->where('period', $criteria['period'])
|
||||
->delete();
|
||||
}
|
||||
|
||||
private function processAndSaveStatementEntries(int $totalCount)
|
||||
: void
|
||||
{
|
||||
$runningBalance = (float) $this->saldo;
|
||||
$totalCount = StmtEntry::where('account_number', $this->account_number)
|
||||
->where('booking_date', $this->period)
|
||||
->count();
|
||||
$globalSequence = 0;
|
||||
|
||||
Log::info("Processing {$totalCount} statement entries for account: {$this->account_number}");
|
||||
|
||||
// Track the global sequence number across chunks
|
||||
$globalSequence = 0;
|
||||
|
||||
// Proses data dalam chunk untuk mengurangi penggunaan memori
|
||||
StmtEntry::with(['ft', 'transaction'])
|
||||
->where('account_number', $this->account_number)
|
||||
->where('booking_date', $this->period)
|
||||
->orderBy('date_time', 'ASC')
|
||||
->orderBy('trans_reference', 'ASC')
|
||||
->chunk($this->chunkSize, function ($entries) use (&$runningBalance, &$globalSequence) {
|
||||
$processedData = [];
|
||||
$processedData = $this->prepareProcessedData($entries, $runningBalance, $globalSequence);
|
||||
|
||||
foreach ($entries as $item) {
|
||||
$globalSequence++; // Increment the global sequence counter
|
||||
$runningBalance += (float) $item->amount_lcy;
|
||||
|
||||
try {
|
||||
$transactionDate = Carbon::createFromFormat('YmdHi', $item->booking_date . substr($item->ft?->date_time ?? '0000000000', 6, 4))
|
||||
->format('d/m/Y H:i');
|
||||
} catch (Exception $e) {
|
||||
$transactionDate = Carbon::now()->format('d/m/Y H:i');
|
||||
Log::warning("Error formatting transaction date: " . $e->getMessage());
|
||||
}
|
||||
|
||||
try {
|
||||
$actualDate = Carbon::createFromFormat('ymdHi', $item->ft?->date_time ?? '2505120000')
|
||||
->format('d/m/Y H:i');
|
||||
} catch (Exception $e) {
|
||||
$actualDate = Carbon::now()->format('d/m/Y H:i');
|
||||
Log::warning("Error formatting actual date: " . $e->getMessage());
|
||||
}
|
||||
|
||||
$processedData[] = [
|
||||
'account_number' => $this->account_number,
|
||||
'period' => $this->period,
|
||||
'sequence_no' => $globalSequence,
|
||||
'transaction_date' => $transactionDate,
|
||||
'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' => $actualDate,
|
||||
'created_at' => now(),
|
||||
'updated_at' => now(),
|
||||
];
|
||||
}
|
||||
// var_dump($processedData);
|
||||
// Simpan data dalam batch untuk kinerja yang lebih baik
|
||||
if (!empty($processedData)) {
|
||||
DB::table('processed_statements')->insert($processedData);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private function prepareProcessedData($entries, &$runningBalance, &$globalSequence)
|
||||
: array
|
||||
{
|
||||
$processedData = [];
|
||||
|
||||
foreach ($entries as $item) {
|
||||
$globalSequence++;
|
||||
$runningBalance += (float) $item->amount_lcy;
|
||||
|
||||
$transactionDate = $this->formatTransactionDate($item);
|
||||
$actualDate = $this->formatActualDate($item);
|
||||
|
||||
$processedData[] = [
|
||||
'account_number' => $this->account_number,
|
||||
'period' => $this->period,
|
||||
'sequence_no' => $globalSequence,
|
||||
'transaction_date' => $transactionDate,
|
||||
'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' => $actualDate,
|
||||
'created_at' => now(),
|
||||
'updated_at' => now(),
|
||||
];
|
||||
}
|
||||
|
||||
return $processedData;
|
||||
}
|
||||
|
||||
private function formatTransactionDate($item)
|
||||
: string
|
||||
{
|
||||
try {
|
||||
return Carbon::createFromFormat(
|
||||
'YmdHi',
|
||||
$item->booking_date . substr($item->ft?->date_time ?? '0000000000', 6, 4)
|
||||
)->format('d/m/Y H:i');
|
||||
} catch (Exception $e) {
|
||||
Log::warning("Error formatting transaction date: " . $e->getMessage());
|
||||
return Carbon::now()->format('d/m/Y H:i');
|
||||
}
|
||||
}
|
||||
|
||||
private function formatActualDate($item)
|
||||
: string
|
||||
{
|
||||
try {
|
||||
return Carbon::createFromFormat(
|
||||
'ymdHi',
|
||||
$item->ft?->date_time ?? '2505120000'
|
||||
)->format('d/m/Y H:i');
|
||||
} catch (Exception $e) {
|
||||
Log::warning("Error formatting actual date: " . $e->getMessage());
|
||||
return Carbon::now()->format('d/m/Y H:i');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate narrative for a statement entry
|
||||
*/
|
||||
@@ -247,16 +289,7 @@
|
||||
}
|
||||
}
|
||||
|
||||
return str_replace('<NL>','',$result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get transaction data by reference and field
|
||||
*/
|
||||
private function getTransaction($ref, $field)
|
||||
{
|
||||
$trans = TempFundsTransfer::where('ref_no', $ref)->first();
|
||||
return $trans->$field ?? "";
|
||||
return str_replace('<NL>', '', $result);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -265,6 +298,11 @@
|
||||
private function exportToCsv()
|
||||
: void
|
||||
{
|
||||
// Delete existing file if it exists
|
||||
if (Storage::disk($this->disk)->exists("statements/{$this->fileName}")) {
|
||||
Storage::disk($this->disk)->delete("statements/{$this->fileName}");
|
||||
}
|
||||
|
||||
$csvContent = "NO|TRANSACTION.DATE|REFERENCE.NUMBER|TRANSACTION.AMOUNT|TRANSACTION.TYPE|DESCRIPTION|END.BALANCE|ACTUAL.DATE\n";
|
||||
|
||||
// Ambil data yang sudah diproses dalam chunk untuk mengurangi penggunaan memori
|
||||
@@ -292,4 +330,13 @@
|
||||
|
||||
Log::info("Statement exported to {$this->disk} disk: statements/{$this->fileName}");
|
||||
}
|
||||
|
||||
/**
|
||||
* Get transaction data by reference and field
|
||||
*/
|
||||
private function getTransaction($ref, $field)
|
||||
{
|
||||
$trans = TempFundsTransfer::where('ref_no', $ref)->first();
|
||||
return $trans->$field ?? "";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -102,13 +102,8 @@
|
||||
]);
|
||||
|
||||
// Set the balances
|
||||
if (isset($data['open_actual_bal'])) {
|
||||
$accountBalance->actual_balance = $data['open_actual_bal'];
|
||||
}
|
||||
|
||||
if (isset($data['open_cleared_bal'])) {
|
||||
$accountBalance->cleared_balance = $data['open_cleared_bal'];
|
||||
}
|
||||
$accountBalance->actual_balance = $data['open_actual_bal'] ?? 0;
|
||||
$accountBalance->cleared_balance = $data['open_cleared_bal'] ?? 0;
|
||||
|
||||
$accountBalance->save();
|
||||
Log::info("Saved balance for account {$data['account_number']} for period $period");
|
||||
|
||||
Reference in New Issue
Block a user