Compare commits
3 Commits
9f0ee812a9
...
3414fd9414
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3414fd9414 | ||
|
|
cb0a248ce5 | ||
|
|
57463f2429 |
@@ -55,15 +55,7 @@
|
|||||||
try {
|
try {
|
||||||
Log::info("Starting export statement job for account: {$this->account_number}, period: {$this->period}");
|
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
|
// Export data yang sudah diproses ke CSV
|
||||||
$this->exportToCsv();
|
$this->exportToCsv();
|
||||||
@@ -75,55 +67,81 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Process statement data and save to database
|
|
||||||
*/
|
|
||||||
private function processStatementData()
|
private function processStatementData()
|
||||||
: void
|
: void
|
||||||
{
|
{
|
||||||
// Hapus data yang mungkin sudah ada untuk kombinasi account dan period yang sama
|
$accountQuery = [
|
||||||
ProcessedStatement::where('account_number', $this->account_number)
|
'account_number' => $this->account_number,
|
||||||
->where('period', $this->period)
|
'period' => $this->period
|
||||||
->delete();
|
];
|
||||||
|
|
||||||
$runningBalance = (float) $this->saldo;
|
$totalCount = $this->getTotalEntryCount($accountQuery);
|
||||||
$totalCount = StmtEntry::where('account_number', $this->account_number)
|
$existingDataCount = $this->getExistingProcessedCount($accountQuery);
|
||||||
->where('booking_date', $this->period)
|
|
||||||
|
// 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();
|
->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;
|
||||||
|
$globalSequence = 0;
|
||||||
|
|
||||||
Log::info("Processing {$totalCount} statement entries for account: {$this->account_number}");
|
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'])
|
StmtEntry::with(['ft', 'transaction'])
|
||||||
->where('account_number', $this->account_number)
|
->where('account_number', $this->account_number)
|
||||||
->where('booking_date', $this->period)
|
->where('booking_date', $this->period)
|
||||||
->orderBy('date_time', 'ASC')
|
->orderBy('date_time', 'ASC')
|
||||||
->orderBy('trans_reference', 'ASC')
|
->orderBy('trans_reference', 'ASC')
|
||||||
->chunk($this->chunkSize, function ($entries) use (&$runningBalance, &$globalSequence) {
|
->chunk($this->chunkSize, function ($entries) use (&$runningBalance, &$globalSequence) {
|
||||||
|
$processedData = $this->prepareProcessedData($entries, $runningBalance, $globalSequence);
|
||||||
|
|
||||||
|
if (!empty($processedData)) {
|
||||||
|
DB::table('processed_statements')->insert($processedData);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private function prepareProcessedData($entries, &$runningBalance, &$globalSequence)
|
||||||
|
: array
|
||||||
|
{
|
||||||
$processedData = [];
|
$processedData = [];
|
||||||
|
|
||||||
foreach ($entries as $item) {
|
foreach ($entries as $item) {
|
||||||
$globalSequence++; // Increment the global sequence counter
|
$globalSequence++;
|
||||||
$runningBalance += (float) $item->amount_lcy;
|
$runningBalance += (float) $item->amount_lcy;
|
||||||
|
|
||||||
try {
|
$transactionDate = $this->formatTransactionDate($item);
|
||||||
$transactionDate = Carbon::createFromFormat('YmdHi', $item->booking_date . substr($item->ft?->date_time ?? '0000000000', 6, 4))
|
$actualDate = $this->formatActualDate($item);
|
||||||
->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[] = [
|
$processedData[] = [
|
||||||
'account_number' => $this->account_number,
|
'account_number' => $this->account_number,
|
||||||
@@ -140,12 +158,36 @@
|
|||||||
'updated_at' => now(),
|
'updated_at' => now(),
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
// var_dump($processedData);
|
|
||||||
// Simpan data dalam batch untuk kinerja yang lebih baik
|
return $processedData;
|
||||||
if (!empty($processedData)) {
|
}
|
||||||
DB::table('processed_statements')->insert($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');
|
||||||
}
|
}
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -250,21 +292,17 @@
|
|||||||
return str_replace('<NL>', '', $result);
|
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 ?? "";
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Export processed data to CSV file
|
* Export processed data to CSV file
|
||||||
*/
|
*/
|
||||||
private function exportToCsv()
|
private function exportToCsv()
|
||||||
: void
|
: 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";
|
$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
|
// 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}");
|
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
|
// Set the balances
|
||||||
if (isset($data['open_actual_bal'])) {
|
$accountBalance->actual_balance = $data['open_actual_bal'] ?? 0;
|
||||||
$accountBalance->actual_balance = $data['open_actual_bal'];
|
$accountBalance->cleared_balance = $data['open_cleared_bal'] ?? 0;
|
||||||
}
|
|
||||||
|
|
||||||
if (isset($data['open_cleared_bal'])) {
|
|
||||||
$accountBalance->cleared_balance = $data['open_cleared_bal'];
|
|
||||||
}
|
|
||||||
|
|
||||||
$accountBalance->save();
|
$accountBalance->save();
|
||||||
Log::info("Saved balance for account {$data['account_number']} for period $period");
|
Log::info("Saved balance for account {$data['account_number']} for period $period");
|
||||||
|
|||||||
Reference in New Issue
Block a user