listAccount() as $clientName => $accounts) { foreach ($accounts as $accountNumber) { foreach ($this->listPeriod() as $period) { $job = new ExportStatementJob( $accountNumber, $period, $this->getAccountBalance($accountNumber, $period), $clientName // Pass the client name to the job ); $jobIds[] = app(Dispatcher::class)->dispatch($job); $data[] = [ 'client_name' => $clientName, 'account_number' => $accountNumber, 'period' => $period ]; } } } return response()->json([ 'message' => 'Statement export jobs have been queued', 'jobs' => array_map(function ($index, $jobId) use ($data) { return [ 'job_id' => $jobId, 'client_name' => $data[$index]['client_name'], 'account_number' => $data[$index]['account_number'], 'period' => $data[$index]['period'], 'file_name' => "{$data[$index]['client_name']}_{$data[$index]['account_number']}_{$data[$index]['period']}.csv" ]; }, array_keys($jobIds), $jobIds) ]); } function listAccount(){ return [ 'OY' => [ '1080426085', '1080425781', ], 'PLUANG' => [ '1081647484', '1081647485', ], 'INDORAYA' => [ '1083123710', '1083123711', '1083123712', '1083123713', '1083123714', '1083123715', '1083123716', '1083123718', '1083123719', '1083123721', '1083123722', '1083123723', '1083123724', '1083123726', '1083123727', '1083123728', '1083123730', '1083123731', '1083123732', '1083123734', '1083123735', ], 'TDC' => [ '1086677889', '1086677890', '1086677891', '1086677892', '1086677893', '1086677894', '1086677895', '1086677896', '1086677897', ], 'ASIA_PARKING' => [ '1080119298', '1080119361', '1080119425', '1080119387', '1082208069', ], 'DAU' => [ '1085151668', ], 'EGR' => [ '1085368601', ], 'SARANA_PACTINDO' => [ '1078333878', ], 'SWADAYA_PANDU' => [ '0081272689', ] ]; } function listPeriod(){ return [ date('Ymd', strtotime('-1 day')) ]; } function getAccountBalance($accountNumber, $period) { $accountBalance = AccountBalance::where('account_number', $accountNumber) ->where('period', '<', $period) ->orderBy('period', 'desc') ->first(); return $accountBalance->actual_balance ?? 0; } }