diff --git a/app/Exports/BillplzTransactionSummaryExport.php b/app/Exports/BillplzTransactionSummaryExport.php new file mode 100644 index 0000000..939378a --- /dev/null +++ b/app/Exports/BillplzTransactionSummaryExport.php @@ -0,0 +1,28 @@ +bill_plz_transaction_summary_pkb = $bill_plz_transaction_summary_pkb; + $this->bill_plz_transaction_summary_koop = $bill_plz_transaction_summary_koop; + $this->date = $date; + } + + + public function sheets(): array + { + return [ + new BillplzTransactionSummarySheet($this->bill_plz_transaction_summary_pkb, 'PKB (v2)', $this->date), + new BillplzTransactionSummarySheet($this->bill_plz_transaction_summary_koop, 'KOPKB (v3)', $this->date), + ]; + } +} diff --git a/app/Exports/BillplzTransactionSummarySheet.php b/app/Exports/BillplzTransactionSummarySheet.php new file mode 100644 index 0000000..67a2d71 --- /dev/null +++ b/app/Exports/BillplzTransactionSummarySheet.php @@ -0,0 +1,180 @@ +summary_data = $summary_data; + $this->sheet_title = $sheet_title; + $this->date = $date; + } + + public function title(): string + { + return $this->sheet_title; + } + + public function bindValue(Cell $cell, $value) + { + if ($cell->getColumn() === 'J' && $value !== '' && $value !== null) { + $cell->setValueExplicit((string) $value, DataType::TYPE_STRING); + return true; + } + + return parent::bindValue($cell, $value); + } + + public function columnFormats(): array + { + return [ + 'J' => NumberFormat::FORMAT_TEXT, + ]; + } + public function registerEvents(): array + { + return [ + AfterSheet::class => function (AfterSheet $event) { + $sheet = $event->sheet->getDelegate(); + $highest_column = $sheet->getHighestColumn(8); + $sheet->mergeCells('A1:' . $highest_column . '1'); + $sheet->getStyle('A1')->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER); + $sheet->getStyle('B6')->getAlignment()->setHorizontal(Alignment::HORIZONTAL_LEFT); + }, + ]; + } + + public function columnWidths(): array + { + return [ + 'A' => 25, + 'B' => 22, + 'C' => 15, + 'D' => 22, + 'E' => 15, + 'F' => 22, + 'G' => 20, + 'H' => 15, + 'I' => 30, + 'J' => 16, + 'K' => 20, + ]; + } + + public function styles(Worksheet $sheet) + { + return [ + 1 => ['alignment' => ['wrapText' => true, 'horizontal' => Alignment::HORIZONTAL_CENTER]], + 3 => ['alignment' => ['wrapText' => true]], + 4 => ['alignment' => ['wrapText' => true]], + 5 => ['alignment' => ['wrapText' => true]], + 6 => ['alignment' => ['wrapText' => true]], + 8 => ['alignment' => ['wrapText' => true]], + ]; + } + + + + public function array(): array + { + $title = 'Billplz Transaction Summary' . ($this->date ? ' (' . $this->date . ')' : ''); + $rows = []; + $rows[] = [$title]; + $rows[] = ['', '', '', '', '', '', '', '', '', '', '']; + + $rows[] = ['Sum of Billplz Transactions', $this->summary_data['sumOfBillplzTransactions'] ?? '']; + $rows[] = ['Sum of Arrahn Transactions', $this->summary_data['sumOfArrahnTransactions'] ?? '']; + $rows[] = ['Payment Gateway Cost', $this->summary_data['paymentGatewayCost'] ?? '']; + $rows[] = ['No of Arrahn Transactions', $this->summary_data['numberOfArrahnTransactions'] ?? '']; + $rows[] = ['', '', '', '', '', '', '', '', '', '', '']; + + $rows[] = [ + 'Bil', + 'Billplz ID', + 'Billplz Status', + 'Billplz Paid Amount (RM)', + 'Arrahn Status', + 'Arrahn Total Payment', + 'Arrahn Norujukan', + 'Arrahn Kodcaw', + 'Arrahn Customer Name', + 'Arrahn No. KP', + 'Arrahn Paid At', + ]; + + $bil = 1; + $details = $this->summary_data['billPlzTransactionSummary'] ?? []; + + foreach ($details as $data) { + $billplz_bill = $data['billplz_bill'] ?? null; + $arrahn_transactions = $data['arrahn_transactions'] ?? []; + + $billplz_id = $billplz_bill['id'] ?? ''; + $billplz_status = $billplz_bill['state'] ?? ''; + $billplz_paid_amount = ''; + + if (isset($billplz_bill['paid_amount']['amount'])) { + $billplz_paid_amount = floatval($billplz_bill['paid_amount']['amount']) / 100; + } + + if (empty($arrahn_transactions)) { + $rows[] = [ + $bil++, + $billplz_id, + $billplz_status, + $billplz_paid_amount, + '', + '', + '', + '', + '', + '', + '', + ]; + + continue; + } + + foreach ($arrahn_transactions as $transaction) { + $rows[] = [ + $bil++, + $billplz_id, + $billplz_status, + $billplz_paid_amount, + $transaction['status'] ?? '', + $transaction['total_payment'] ?? '', + $transaction['norujukan'] ?? '', + $transaction['kodcaw'] ?? '', + $transaction['customer_name'] ?? '', + $transaction['nokp'] ?? '', + $transaction['paid_at'] ?? '', + ]; + } + } + + return $rows; + } +} diff --git a/app/Http/Controllers/BillplzTransactionSummaryController.php b/app/Http/Controllers/BillplzTransactionSummaryController.php index 7b1c8da..128e423 100644 --- a/app/Http/Controllers/BillplzTransactionSummaryController.php +++ b/app/Http/Controllers/BillplzTransactionSummaryController.php @@ -5,14 +5,58 @@ use Illuminate\Http\Request; use Illuminate\Support\Facades\Http; use Carbon\Carbon; use Illuminate\Support\Facades\Log; +use Maatwebsite\Excel\Facades\Excel; +use App\Exports\BillplzTransactionSummaryExport; +use App\Exports\BillplzTransactionSummarySheet; + class BillplzTransactionSummaryController extends Controller { public function index(Request $request) { $date = $request->selectedDate ?? Carbon::now()->toDateString(); - - $transactionDataPKB = collect( Http::arrahn()->get('/api/transaction/online/query', ['date' => $date, 'status' => 'paid'])->json() ); + + [$billPlzTransactionSummaryPKB, $billPlzTransactionSummaryKOOP] = $this->getSummaryData($date); + + return view('online.billplz-transaction-summary.index', compact('billPlzTransactionSummaryPKB', 'billPlzTransactionSummaryKOOP', 'date')); + } + + public function export(Request $request) + { + $date = $request->selectedDate ?? Carbon::now()->toDateString(); + $tab = $request->tab ?? 'all'; + + [$billPlzTransactionSummaryPKB, $billPlzTransactionSummaryKOOP] = $this->getSummaryData($date); + + if ($tab === 'kopkb') { + $file_name = 'billplz_transaction_summary_kopkb_' . $date . '.xlsx'; + + return Excel::download( + new BillplzTransactionSummarySheet($billPlzTransactionSummaryKOOP, 'KOPKB (v3)', $date), + $file_name + ); + } + + if ($tab === 'pkb') { + $file_name = 'billplz_transaction_summary_pkb_' . $date . '.xlsx'; + + return Excel::download( + new BillplzTransactionSummarySheet($billPlzTransactionSummaryPKB, 'PKB (v2)', $date), + $file_name + ); + } + + $file_name = 'billplz_transaction_summary_' . $date . '.xlsx'; + + return Excel::download( + new BillplzTransactionSummaryExport($billPlzTransactionSummaryPKB, $billPlzTransactionSummaryKOOP, $date), + $file_name + ); + } + + private function getSummaryData($date) + { + $transactionDataPKB = collect( Http::arrahn()->get('/api/transaction/online/query', ['date' => $date, 'status' => 'paid'])->json() ); $transactionDataPKB = $transactionDataPKB->map(function($x){ $x['account_own_by'] = 'PKB'; return $x; @@ -60,14 +104,14 @@ class BillplzTransactionSummaryController extends Controller abort( response('Portal ArrahnPay kemungkinan tidak beroperasi. Waktu Operasi Portal ArrahnPay adalah dari 7.30 AM hingga 11.30 PM', 503) ); } - return $billplzBillsPromise; + return $billplzBillsPromise; }; $mergeBillplzResponse = function($externalArray) { - $merged = []; + $merged = []; - foreach ($externalArray as $key => $subArray) { + foreach ($externalArray as $key => $subArray) { foreach (['KOPKB', 'PKB'] as $type) { if (!isset($merged[$type])) { $merged[$type] = []; @@ -75,9 +119,9 @@ class BillplzTransactionSummaryController extends Controller $merged[$type] = array_merge($merged[$type], $subArray[$type]); } - } + } - return array_map('array_values', $merged); + return array_map('array_values', $merged); }; $billplzBillsResponse = $queryBillIdsURL->map(fn($x) => $getBillplzPromise($x)) @@ -88,9 +132,11 @@ class BillplzTransactionSummaryController extends Controller $data = array_map(fn($x, $y) => $this->processBillplzBills($x, $y), $billplzBills, [$transactionDataKOOP, $transactionDataPKB]); + + [$billPlzTransactionSummaryKOOP, $billPlzTransactionSummaryPKB] = $data; - return view('online.billplz-transaction-summary.index', compact('billPlzTransactionSummaryPKB' ,'billPlzTransactionSummaryKOOP', 'date')); + return [$billPlzTransactionSummaryPKB, $billPlzTransactionSummaryKOOP]; } public function processArrahnTransactions($transactionData) diff --git a/resources/views/online/billplz-transaction-summary/index.blade.php b/resources/views/online/billplz-transaction-summary/index.blade.php index 0a4276d..a73dedc 100644 --- a/resources/views/online/billplz-transaction-summary/index.blade.php +++ b/resources/views/online/billplz-transaction-summary/index.blade.php @@ -10,14 +10,23 @@ -