feat:adds LLM capabilities to chat with your portfolios and holdings
This commit is contained in:
@@ -24,6 +24,21 @@ class HoldingController extends Controller
|
||||
->portfolio($portfolio->id)
|
||||
->firstOrFail();
|
||||
|
||||
return view('holding.show', compact(['portfolio', 'holding']));
|
||||
$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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -39,7 +39,26 @@ class PortfolioController extends Controller
|
||||
->first();
|
||||
}
|
||||
);
|
||||
|
||||
$formattedHoldings = $this->getFormattedHoldings($portfolio);
|
||||
|
||||
return view('portfolio.show', compact(['portfolio', 'metrics']));
|
||||
return view('portfolio.show', compact(['portfolio', 'metrics', 'formattedHoldings']));
|
||||
}
|
||||
|
||||
public function getFormattedHoldings($portfolio)
|
||||
{
|
||||
$formattedHoldings = '';
|
||||
foreach($portfolio->holdings as $holding) {
|
||||
$formattedHoldings .= " * Holding of ".$holding->market_data->name." (".$holding->symbol.")"
|
||||
."; own ". ($holding->quantity > 0 ? $holding->quantity : 'ZERO') . " shares"
|
||||
."; avg cost basis ". $holding->average_cost_basis
|
||||
."; curr market value ". $holding->market_data->market_value
|
||||
."; unrealized gains ". $holding->market_gain_dollars
|
||||
."; realized gains ". $holding->realized_gain_dollars
|
||||
."; dividends earned ". $holding->dividends_earned
|
||||
."\n\n";
|
||||
|
||||
}
|
||||
return $formattedHoldings;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Concerns\HasUuids;
|
||||
|
||||
class AiChat extends Model
|
||||
{
|
||||
use HasUuids;
|
||||
|
||||
protected $fillable = [
|
||||
'role',
|
||||
'content'
|
||||
];
|
||||
|
||||
protected $hidden = [];
|
||||
|
||||
protected static function boot()
|
||||
{
|
||||
parent::boot();
|
||||
|
||||
static::creating(function ($chat) {
|
||||
|
||||
$chat->user_id = auth()->user()->id;
|
||||
});
|
||||
}
|
||||
|
||||
public function user() {
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
|
||||
public function chatable()
|
||||
{
|
||||
return $this->morphTo();
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@
|
||||
namespace App\Models;
|
||||
|
||||
use App\Models\Split;
|
||||
use App\Models\AiChat;
|
||||
use App\Models\Dividend;
|
||||
use App\Models\Portfolio;
|
||||
use App\Models\MarketData;
|
||||
@@ -131,6 +132,16 @@ class Holding extends Model
|
||||
->orderBy('date', 'DESC');
|
||||
}
|
||||
|
||||
/**
|
||||
* Related chats for holding
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function chats()
|
||||
{
|
||||
return $this->morphMany(AiChat::class, 'chatable');
|
||||
}
|
||||
|
||||
public function scopeWithMarketData($query)
|
||||
{
|
||||
return $query->withAggregate('market_data', 'name')
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Models\AiChat;
|
||||
use Carbon\CarbonPeriod;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Carbon;
|
||||
@@ -64,6 +65,16 @@ class Portfolio extends Model
|
||||
return $this->hasMany(DailyChange::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Related chats for portfolio
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function chats()
|
||||
{
|
||||
return $this->morphMany(AiChat::class, 'chatable');
|
||||
}
|
||||
|
||||
public function scopeMyPortfolios()
|
||||
{
|
||||
return $this->whereHas('users', function ($query) {
|
||||
|
||||
Reference in New Issue
Block a user