What is the connection between Transaction and client_id?
How to calculate a php variable for each individual client
Hey all,
Building a simple unit balance calculator, that takes inputs from a couple tables and calculates the current total dynamic unit balance. I have the formula set up, and is working rudimentary.
The issue i'm having is that the php variable $totalBalance is being displayed next to each client, as I've placed it as a member in the foreach loop that displays the rest of the client information.
What's the best way to associate a $totalBalance php variable to a client_id, so that each clients $totalBalance is individually calculated and displayed separately.
$totalBalance variable calculation located in my index controller:
$totalIssues = Transaction::where('transaction_type', 'Issue')->sum(\DB::raw('gross_value / unit_price - entry_fee'));
$totalRedemptions = Transaction::where('transaction_type', 'Redemption')->sum(\DB::raw('gross_value / unit_price'));
$totalBalance = $totalIssues - $totalRedemptions;
Relevant section of view that displays the $totalBalance:
<td>{{ $loop->iteration }}</td>
<td>{{ $unit_balance->entityLink->entity_name }}</td>
<td>{{ $unit_balance->priceLink->unit_price }}</td>
<td>{{ $unit_balance->unit_balance }}</td>
<td>{{ $totalBalance}}</td>
Clients can be differentiated between with their client_id, and each client_id can only have one unit_balance, which is why the above is setup the way it is, and displays names through relationship links.
@Tchopa there is a stray comma at the end of the raw SELECT statement that needs removing; forgot the END as the end of the CASE staement:
->selectRaw("
entities.*,
SUM(CASE WHEN transaction_type = 'Issue' THEN gross_value / unit_price - entry_fee END) as total_issues,
SUM(CASE WHEN transaction_type = 'Redemption' THEN gross_value / unit_price END) as total_redemptions
")
Aside, you have selected FROM transactions and JOIN transactions???
from `transactions` inner join `transactions`
Please or to participate in this conversation.