feat(data-processing): tambahkan job untuk memproses data pelanggan dan akun

- Menambahkan kelas ProcessCopyDataJob untuk menangani pemrosesan data.
- Implementasi metode copyCustomerData untuk menyalin data pelanggan dari TempCustomer.
- Implementasi metode copyAccountData untuk menyalin data akun dari TempAccount.
- Implementasi metode copyStmtEntryData untuk memproses data entri pernyataan dari TempStmtEntry.
- Menangani kesalahan dengan logging untuk setiap proses yang gagal.
This commit is contained in:
Daeng Deni Mardaeni
2025-02-18 16:34:20 +07:00
parent 0d95bf33c3
commit 23b4725d33

View File

@@ -0,0 +1,205 @@
<?php
namespace Modules\Webstatement\Jobs;
use Exception;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;
use Modules\Webstatement\Models\Account;
use Modules\Webstatement\Models\Customer;
use Modules\Webstatement\Models\StmtEntry;
use Modules\Webstatement\Models\TempAccount;
use Modules\Webstatement\Models\TempCustomer;
use Modules\Webstatement\Models\TempStmtEntry;
class ProcessCopyDataJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* Create a new job instance.
*/
public function __construct()
{
//
}
/**
* Execute the job.
*/
public function handle()
: void
{
//$this->copyCustomerData();
//$this->copyAccountData();
$this->copyStmtEntryData();
}
public function copyCustomerData()
{
try {
$insertedCount = 0;
$batchSize = 1000;
do {
$processed = TempCustomer::select('customer_code', 'name_1', 'address', 'co_code', 'date_of_birth', 'email_1')
->take($batchSize)
->get();
foreach ($processed as $tempCustomer) {
try {
Customer::updateOrCreate(
['customer_code' => $tempCustomer->customer_code],
[
'name' => $tempCustomer->name_1,
'address' => $tempCustomer->address,
'branch_code' => $tempCustomer->co_code,
'date_of_birth' => (!empty(trim($tempCustomer->date_of_birth))) ? $tempCustomer->date_of_birth : null,
'email' => $tempCustomer->email_1,
]
);
$insertedCount++;
} catch (Exception $e) {
\Illuminate\Support\Facades\Log::error('Error updating/creating customer: ' . $e->getMessage());
// You might want to handle this error more gracefully
}
}
} while ($processed->count() == $batchSize);
return response()->json([
'message' => 'Customer data copied successfully',
'records_inserted' => $insertedCount
]);
} catch (Exception $e) {
Log::error('Error copying customer data: ' . $e->getMessage());
return response()->json(['error' => $e->getMessage()], 500);
}
}
public function copyAccountData()
{
try {
$processedCount = 0;
$batchSize = 1000;
do {
$processed = TempAccount::select('account_number', 'customer_no', 'currency', 'opening_date', 'co_code', 'open_category')
->take($batchSize)
->get();
foreach ($processed as $tempAccount) {
try {
Account::updateOrCreate(
['account_number' => $tempAccount->account_number],
[
'customer_code' => $tempAccount->customer_no,
'currency' => $tempAccount->currency,
'branch_code' => $tempAccount->co_code,
'opening_date' => (!empty(trim($tempAccount->opening_date))) ? $tempAccount->opening_date : null,
'product_category' => $tempAccount->open_category,
]
);
$processedCount++;
} catch (Exception $e) {
Log::error('Error updating/creating account: ' . $e->getMessage());
// You might want to handle this error more gracefully
}
}
} while ($processed->count() == $batchSize);
return response()->json([
'message' => 'Account data processed successfully',
'records_processed' => $processedCount
]);
} catch (Exception $e) {
Log::error('Error processing account data: ' . $e->getMessage());
return response()->json(['error' => $e->getMessage()], 500);
}
}
public function copyStmtEntryData()
{
ini_set('max_execution_time', 3600); // Set timeout to 1 hour
set_time_limit(3600); // Alternative way to set timeout
Log::info('Data StmtEntry processing job has been started');
try {
$processedCount = 0;
$batchSize = 1000;
$offset = 0;
do {
$processed = TempStmtEntry::select(
'stmt_entry_id',
'account_number',
'company_code',
'amount_lcy',
'transaction_code',
'narrative',
'product_category',
'value_date',
'amount_fcy',
'exchange_rate',
'trans_reference',
'booking_date',
'stmt_no',
'date_time',
'currency',
'crf_type',
'consol_key'
)
->limit($batchSize)
->offset($offset)
->get();
foreach ($processed as $tempStmtEntry) {
try {
StmtEntry::updateOrCreate(
['stmt_entry_id' => $tempStmtEntry->stmt_entry_id],
[
'account_number' => $tempStmtEntry->account_number,
'company_code' => $tempStmtEntry->company_code,
'amount_lcy' => $tempStmtEntry->amount_lcy,
'transaction_code' => $tempStmtEntry->transaction_code,
'narrative' => $tempStmtEntry->narrative,
'product_category' => $tempStmtEntry->product_category,
'value_date' => $tempStmtEntry->value_date,
'amount_fcy' => $tempStmtEntry->amount_fcy,
'exchange_rate' => $tempStmtEntry->exchange_rate,
'trans_reference' => $tempStmtEntry->trans_reference,
'booking_date' => $tempStmtEntry->booking_date,
'stmt_no' => $tempStmtEntry->stmt_no,
'date_time' => $tempStmtEntry->date_time,
'currency' => $tempStmtEntry->currency,
'crf_type' => $tempStmtEntry->crf_type,
'consol_key' => $tempStmtEntry->consol_key,
]
);
$processedCount++;
} catch (Exception $e) {
Log::error('Error updating/creating stmt entry: ' . $e->getMessage());
// You might want to handle this error more gracefully
}
}
$offset += $batchSize;
} while ($processed->count() == $batchSize);
Log::info('Data StmtEntry processing job has been completed');
return response()->json([
'message' => 'Statement Entry data processed successfully',
'records_processed' => $processedCount
]);
} catch (Exception $e) {
Log::error('Error processing statement entry data: ' . $e->getMessage());
return response()->json(['error' => $e->getMessage()], 500);
}
}
}