$advances = DB::table('advances')
->select('amount_before', 'amount_after', 'amount', 'date')
->where('customer_id', $customerId)
->get();
$balancings = DB::table('balancings')
->select('net_pay_out', 'deductable_advance', 'price', 'date')
->where('customer_id', $customerId)
->get();
$cashPayments = DB::table('cash_payments')
->select('amount as deductable_advance', 'date') // Rename the amount field
->where('customer_id', $customerId)
->get();
// Merge the results of advances and balancings into a single collection
$ledger = $advances->merge($balancings)->merge($cashPayments);
// Sort the merged collection by the 'date' field in ascending order
$sortedLedger = $ledger->sortBy('date');
// Convert the sorted collection to a plain array for JSON response,
// and then reindex the array numerically
$ledgerArray = $sortedLedger->values()->toArray();