199 lines
7.1 KiB
PHP
199 lines
7.1 KiB
PHP
<?php
|
|
namespace App\Http\Controllers;
|
|
|
|
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();
|
|
|
|
[$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;
|
|
});
|
|
|
|
$transactionDataKOOP = collect( Http::arrahn_koop()->get('/api/transaction/online/query', ['date' => $date, 'status' => 'paid'])->json() );
|
|
$transactionDataKOOP = $transactionDataKOOP->map(function($x){
|
|
$x['account_own_by'] = 'KOOP';
|
|
return $x;
|
|
});
|
|
|
|
|
|
$transactionData = $transactionDataPKB->merge($transactionDataKOOP);
|
|
|
|
$transactionDataPKB = $this->processArrahnTransactions($transactionDataPKB);
|
|
$transactionDataKOOP = $this->processArrahnTransactions($transactionDataKOOP);
|
|
$transactionData = $this->processArrahnTransactions($transactionData);
|
|
|
|
$billIds = $transactionData->map(fn($x) => ['bill_id' => $x['bill_id'], 'account_own_by' => $x['account_own_by']]);
|
|
|
|
$billIds = $billIds->chunk(50);
|
|
|
|
$queryBillIdsURL = $billIds->map(fn($x) => config('api.url_arrahn_portal') . '/api/billplz?bill_ids=' . json_encode($x));
|
|
|
|
$getBillplzPromise = function($url){
|
|
return Http::async()->get($url);
|
|
};
|
|
|
|
$resolveBillplzPromise = function($promise){
|
|
|
|
try{
|
|
$billplzBillsPromise = $promise->wait();
|
|
|
|
if($billplzBillsPromise->failed()){
|
|
throw new \Exception('Something went wrong with the API');
|
|
}
|
|
|
|
$billplzBillsPromise = $billplzBillsPromise->json();
|
|
|
|
}catch(\Exception $e){
|
|
Log::error("ArrahnPay Portal API request failed for URL: {$billplzBillsPromise->effectiveUri()->__toString()}", [
|
|
'error' => $e->getMessage(),
|
|
]);
|
|
|
|
abort( response('Portal ArrahnPay kemungkinan tidak beroperasi. Waktu Operasi Portal ArrahnPay adalah dari 7.30 AM hingga 11.30 PM', 503) );
|
|
}
|
|
|
|
return $billplzBillsPromise;
|
|
};
|
|
|
|
|
|
$mergeBillplzResponse = function($externalArray) {
|
|
$merged = [];
|
|
|
|
foreach ($externalArray as $key => $subArray) {
|
|
foreach (['KOPKB', 'PKB'] as $type) {
|
|
if (!isset($merged[$type])) {
|
|
$merged[$type] = [];
|
|
}
|
|
|
|
$merged[$type] = array_merge($merged[$type], $subArray[$type]);
|
|
}
|
|
}
|
|
|
|
return array_map('array_values', $merged);
|
|
};
|
|
|
|
$billplzBillsResponse = $queryBillIdsURL->map(fn($x) => $getBillplzPromise($x))
|
|
->map(fn($x) => $resolveBillplzPromise($x))
|
|
->all();
|
|
|
|
$billplzBills = $mergeBillplzResponse($billplzBillsResponse);
|
|
|
|
$data = array_map(fn($x, $y) => $this->processBillplzBills($x, $y), $billplzBills, [$transactionDataKOOP, $transactionDataPKB]);
|
|
|
|
|
|
|
|
[$billPlzTransactionSummaryKOOP, $billPlzTransactionSummaryPKB] = $data;
|
|
|
|
return [$billPlzTransactionSummaryPKB, $billPlzTransactionSummaryKOOP];
|
|
}
|
|
|
|
public function processArrahnTransactions($transactionData)
|
|
{
|
|
// Group transactionData by bill_id
|
|
$transactionData = $transactionData->groupBy(function($transaction) {
|
|
return $transaction['billplz_id'];
|
|
})->toArray();
|
|
|
|
$transactionData = array_map(fn($x) => ['arrahn_transactions' => $x], $transactionData);
|
|
$transactionData = array_map(function($x){
|
|
$x['bill_id'] = current($x['arrahn_transactions'])['billplz_id'];
|
|
$x['account_own_by'] = current($x['arrahn_transactions'])['account_own_by'];
|
|
return $x;
|
|
}, $transactionData);
|
|
$transactionData = collect($transactionData);
|
|
|
|
return $transactionData;
|
|
}
|
|
|
|
public function processBillplzBills($bills_group, $transactionData)
|
|
{
|
|
|
|
$bills_group = collect($bills_group)->map(function($bill) {
|
|
$bill['paid_amount_formatted'] = floatval($bill['paid_amount']['amount']) / 100;
|
|
return $bill;
|
|
});
|
|
|
|
$sumOfArrahnTransactions = $transactionData->sum(fn($x) => array_reduce($x['arrahn_transactions'], fn($carry, $item) => $carry + $item['total_payment'], 0));
|
|
$sumOfBillplzTransactions = collect($bills_group)->sum(fn($x) => $x['paid_amount_formatted']);
|
|
$numberOfArrahnTransactions = $transactionData->count();
|
|
|
|
$paymentGatewayCost = 'RM ' . number_format($sumOfBillplzTransactions - $sumOfArrahnTransactions, 2);
|
|
$sumOfArrahnTransactions = 'RM ' . number_format($sumOfArrahnTransactions, 2);
|
|
$sumOfBillplzTransactions = 'RM ' . number_format($sumOfBillplzTransactions, 2);
|
|
|
|
|
|
if ($transactionData->count() !== count($bills_group)) {
|
|
throw new \Exception("The number of items in transactionData and bills_group must be the same.");
|
|
}
|
|
|
|
$billPlzTransactionSummary = $transactionData->zip($bills_group)->map(function ($zippedItem) {
|
|
[$transactionData, $billplzBill] = $zippedItem;
|
|
|
|
$transactionData['billplz_bill'] = $billplzBill;
|
|
|
|
return $transactionData;
|
|
});
|
|
|
|
return[
|
|
'billPlzTransactionSummary' => $billPlzTransactionSummary,
|
|
'sumOfArrahnTransactions' => $sumOfArrahnTransactions,
|
|
'sumOfBillplzTransactions' => $sumOfBillplzTransactions,
|
|
'paymentGatewayCost' => $paymentGatewayCost,
|
|
'numberOfArrahnTransactions' => $numberOfArrahnTransactions,
|
|
];
|
|
}
|
|
|
|
}
|