Files
admin-erahn/app/Http/Controllers/BillplzTransactionSummaryController.php
T
2024-08-12 21:44:35 +08:00

52 lines
2.4 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::get(config('api.url_arrahn_portal') . '/api/billplz-transaction-review', ['date' => $date])->effectiveUri();
$transactionData = collect( Http::get(config('api.url_arrahn_portal') . '/api/billplz-transaction-review', ['date' => $date])->json() );
$transactionData = $transactionData->filter(fn($x) => $x['arrahn_transactions'][0]['paid_at'] != null);
$billIds = $transactionData->pluck('bill_id');
$billplzBills = Http::get(config('api.url_arrahn_portal') . '/api/billplz', ['bill_ids' => json_encode($billIds)])->json();
$billplzBills = array_map(function($bill) {
$bill['paid_amount_formatted'] = floatval($bill['paid_amount']['amount']) / 100;
return $bill;
}, $billplzBills);
$sumOfArrahnTransactions = $transactionData->sum(fn($x) => $x['arrahn_transactions'][0]['total_payment']);
$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.");
}
$billPlzTransactionReview = $transactionData->zip($billplzBills)->map(function ($zippedItem) {
[$transactionData, $billplzBill] = $zippedItem;
$transactionData['billplz_bill'] = $billplzBill;
return $transactionData;
});
return view('online.billplz-transaction-summary.index', compact( 'billPlzTransactionReview', 'sumOfArrahnTransactions', 'sumOfBillplzTransactions', 'paymentGatewayCost', 'numberOfArrahnTransactions', 'date'));
}
}