51 lines
2.3 KiB
PHP
51 lines
2.3 KiB
PHP
<?php
|
|
namespace App\Http\Controllers;
|
|
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Http;
|
|
use Carbon\Carbon;
|
|
|
|
class BillplzTransactionSummaryController extends Controller
|
|
{
|
|
public function index(Request $request)
|
|
{
|
|
$date = $request->selectedDate ?? Carbon::now()->toDateString();
|
|
|
|
$transactionData = Http::arrahn()->get('/api/transaction/online/query', ['date' => $date, 'status' => 'paid'])->json();
|
|
$transactionData = array_map(fn($x) => ['arrahn_transactions' => $x], $transactionData);
|
|
$transactionData = collect($transactionData);
|
|
|
|
$billIds = $transactionData->keys();
|
|
$billplzBills = Http::get(config('api.url_arrahn_portal') . '/api/billplz', ['bill_ids' => json_encode($billIds)])->json();
|
|
$billplzBills = collect($billplzBills)->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($billplzBills)->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($billplzBills)) {
|
|
throw new \Exception("The number of items in transactionData and billplzBills must be the same.");
|
|
}
|
|
|
|
$billPlzTransactionSummary = $transactionData->zip($billplzBills)->map(function ($zippedItem) {
|
|
[$transactionData, $billplzBill] = $zippedItem;
|
|
|
|
$transactionData['billplz_bill'] = $billplzBill;
|
|
|
|
return $transactionData;
|
|
});
|
|
|
|
|
|
return view('online.billplz-transaction-summary.index', compact( 'billPlzTransactionSummary', 'sumOfArrahnTransactions', 'sumOfBillplzTransactions', 'paymentGatewayCost', 'numberOfArrahnTransactions', 'date'));
|
|
}
|
|
|
|
}
|