2024-08-26 19:54:45 -05:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
|
2024-08-28 23:32:01 -05:00
|
|
|
use App\Models\Holding;
|
2024-08-26 21:34:13 -05:00
|
|
|
use App\Models\Portfolio;
|
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
|
2024-08-26 19:54:45 -05:00
|
|
|
class HoldingController extends Controller
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Display the specified resource.
|
|
|
|
|
*/
|
2024-08-26 21:34:13 -05:00
|
|
|
public function show(Request $request, Portfolio $portfolio, String $symbol)
|
2024-08-26 19:54:45 -05:00
|
|
|
{
|
2024-08-29 20:55:23 -05:00
|
|
|
$holding = Holding::with([
|
|
|
|
|
'market_data',
|
|
|
|
|
'transactions' => function ($query) use ($symbol) {
|
|
|
|
|
$query->where('transactions.symbol', $symbol);
|
|
|
|
|
}
|
|
|
|
|
])
|
|
|
|
|
->symbol($symbol)
|
|
|
|
|
->portfolio($portfolio->id)
|
|
|
|
|
->firstOrFail();
|
2024-08-27 21:17:54 -05:00
|
|
|
|
2024-10-31 12:09:06 -05:00
|
|
|
$formattedTransactions = $this->getFormattedTransactions($holding);
|
|
|
|
|
|
|
|
|
|
return view('holding.show', compact(['portfolio', 'holding', 'formattedTransactions']));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getFormattedTransactions($holding)
|
|
|
|
|
{
|
|
|
|
|
$formattedTransactions = '';
|
|
|
|
|
foreach($holding->transactions->where('symbol', $holding->symbol)->sortByDesc('date') as $transaction) {
|
|
|
|
|
$formattedTransactions .= " * ".$transaction->date->format('Y-m-d')
|
|
|
|
|
." ". $transaction->transaction_type
|
|
|
|
|
." ". $transaction->quantity
|
|
|
|
|
." @ ". $transaction->cost_basis
|
|
|
|
|
." each \n\n";
|
|
|
|
|
}
|
|
|
|
|
return $formattedTransactions;
|
2024-08-26 19:54:45 -05:00
|
|
|
}
|
|
|
|
|
}
|