Feat: Adds multi currency support (#88)
This commit is contained in:
@@ -0,0 +1,100 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Number;
|
||||
|
||||
class Currency extends Model
|
||||
{
|
||||
protected $hidden = [];
|
||||
|
||||
protected $primaryKey = 'currency';
|
||||
|
||||
protected $keyType = 'string';
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $fillable = [
|
||||
'currency',
|
||||
'label',
|
||||
];
|
||||
|
||||
/**
|
||||
* Get the attributes that should be cast.
|
||||
*
|
||||
* @return array<string, string>
|
||||
*/
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'created_at' => 'datetime',
|
||||
'updated_at' => 'datetime',
|
||||
];
|
||||
}
|
||||
|
||||
public static function forHumans(int|float $number, ?string $currency = null, ?string $locale = null): string
|
||||
{
|
||||
$symbol = Number::currencySymbol($currency, $locale);
|
||||
|
||||
return $symbol.Number::forHumans($number);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list of supported currencies
|
||||
*
|
||||
* @param bool|null $withAliases Whether to include aliases in list of currencies
|
||||
*/
|
||||
public static function list(?bool $withAliases = true): Collection
|
||||
{
|
||||
$aliases = $withAliases ? collect(config('investbrain.currency_aliases'))->map(function ($value, $currency) {
|
||||
return [
|
||||
'currency' => $currency,
|
||||
'label' => $value['label'],
|
||||
];
|
||||
})->values() : collect();
|
||||
|
||||
return $aliases->merge(self::get()->map->only(['currency', 'label']));
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts between supported currencies
|
||||
*
|
||||
* @param string|null $to (defaults to base currency)
|
||||
*/
|
||||
public static function convert(?float $value, string $from, ?string $to = null, mixed $date = null): float
|
||||
{
|
||||
if (empty($value)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Assume converting to base
|
||||
if (empty($to)) {
|
||||
$to = config('investbrain.base_currency');
|
||||
}
|
||||
|
||||
// Get rate
|
||||
[$from, $to] = [
|
||||
cache()->remember($from.'_rate_'.$date, 10, function () use ($from, $date) {
|
||||
return CurrencyRate::historic($from, $date);
|
||||
}),
|
||||
cache()->remember($to.'_rate_'.$date, 10, function () use ($to, $date) {
|
||||
return CurrencyRate::historic($to, $date);
|
||||
}),
|
||||
];
|
||||
|
||||
// get from rate
|
||||
$rate_to_base = 1 / $from;
|
||||
|
||||
// get value in base currency
|
||||
$base_currency_value = $value * $rate_to_base;
|
||||
|
||||
return (float) $base_currency_value * $to;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,251 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Jobs\BatchInsertNewCurrencyRatesJob;
|
||||
use Carbon\CarbonPeriod;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Carbon;
|
||||
use Investbrain\Frankfurter\Frankfurter;
|
||||
|
||||
class CurrencyRate extends Model
|
||||
{
|
||||
protected $hidden = [];
|
||||
|
||||
protected $primaryKey = 'currency';
|
||||
|
||||
protected $keyType = 'string';
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $fillable = [
|
||||
'date',
|
||||
'currency',
|
||||
'rate',
|
||||
];
|
||||
|
||||
/**
|
||||
* Get the attributes that should be cast.
|
||||
*
|
||||
* @return array<string, string>
|
||||
*/
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'rate' => 'float',
|
||||
'date' => 'date',
|
||||
'created_at' => 'datetime',
|
||||
'updated_at' => 'datetime',
|
||||
];
|
||||
}
|
||||
|
||||
public static function current(string $currency): float
|
||||
{
|
||||
return (float) self::historic($currency);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get historic rate for symbol
|
||||
*/
|
||||
public static function historic(string $currency, mixed $date = null): float
|
||||
{
|
||||
// No need to convert
|
||||
if ($currency === config('investbrain.base_currency')) {
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
// If we don't need historic, let's use current rate
|
||||
if (empty($date)) {
|
||||
|
||||
$date = now();
|
||||
}
|
||||
|
||||
// Make sure we have a Carbon date
|
||||
$date = Carbon::parse($date);
|
||||
|
||||
// Handle aliases
|
||||
[$currency, $adjustment] = self::getCurrencyAliasAdjustments($currency);
|
||||
|
||||
// Get or create historic rate
|
||||
$rate = self::select('rate')
|
||||
->whereDate('date', $date->toDateString())
|
||||
->where(['currency' => $currency])
|
||||
->firstOr(function () use ($date, $currency) {
|
||||
|
||||
$currencies = Currency::all()->pluck('currency')->toArray();
|
||||
|
||||
$rates = Frankfurter::setSymbols($currencies)->historical($date);
|
||||
|
||||
$date = Arr::get($rates, 'date');
|
||||
|
||||
$updates = Arr::map(Arr::get($rates, 'rates', []), function ($rate, $curr) use ($date) {
|
||||
|
||||
return [
|
||||
'currency' => $curr,
|
||||
'date' => $date,
|
||||
'rate' => $rate,
|
||||
'updated_at' => now()->toDateTimeString(),
|
||||
'created_at' => now()->toDateTimeString(),
|
||||
];
|
||||
});
|
||||
|
||||
// persist
|
||||
BatchInsertNewCurrencyRatesJob::dispatch($updates);
|
||||
|
||||
return new CurrencyRate(Arr::first($updates, fn ($update) => $update['currency'] == $currency) ?? ['rate' => 1]);
|
||||
});
|
||||
|
||||
return (float) $rate->rate * $adjustment;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get rates for range of dates
|
||||
*
|
||||
* @return array<string, float>
|
||||
*/
|
||||
public static function timeSeriesRates(string $currency, mixed $start = null, mixed $end = null): array
|
||||
{
|
||||
if (empty($start)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$end = $end ?? now();
|
||||
|
||||
$period = CarbonPeriod::create($start, $end);
|
||||
|
||||
// No need to send network request - just generate 1s
|
||||
if ($currency === config('investbrain.base_currency')) {
|
||||
|
||||
$dateRange = [];
|
||||
foreach ($period as $date) {
|
||||
$dateRange[$date->toDateString()] = 1;
|
||||
}
|
||||
|
||||
return $dateRange;
|
||||
}
|
||||
|
||||
[$currency, $adjustment] = self::getCurrencyAliasAdjustments($currency);
|
||||
|
||||
$currencies = Currency::all()->pluck('currency')->toArray();
|
||||
|
||||
// call api in chunks
|
||||
$rates = [];
|
||||
foreach (collect($period)->chunk(500) as $chunk) {
|
||||
|
||||
$chunkRates = Frankfurter::setSymbols($currencies)->timeSeries($chunk->min(), $chunk->max());
|
||||
|
||||
$rates = array_merge($rates, Arr::get($chunkRates, 'rates', []));
|
||||
}
|
||||
|
||||
// loop through each date
|
||||
$updates = [];
|
||||
foreach ($period as $date) {
|
||||
|
||||
$skip = false;
|
||||
|
||||
$lookupDate = $date->toDateString();
|
||||
|
||||
// get rates or find closest valid rate (handles missing weekend rates)
|
||||
while (! isset($rates[$lookupDate])) {
|
||||
$lookupDate = Carbon::parse($lookupDate)->subDay();
|
||||
|
||||
// prevent runaway infinite loops
|
||||
if ($lookupDate->lessThan($date->copy()->subWeek())) {
|
||||
|
||||
$skip = true;
|
||||
break;
|
||||
}
|
||||
|
||||
$lookupDate = $lookupDate->toDateString();
|
||||
}
|
||||
|
||||
if ($skip) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// make date a string
|
||||
$date = $date->toDateString();
|
||||
|
||||
// loop through each rate
|
||||
foreach ($rates[$lookupDate] as $curr => $rate) {
|
||||
|
||||
// add to updates
|
||||
$updates[] = [
|
||||
'currency' => $curr,
|
||||
'date' => $date,
|
||||
'rate' => $rate,
|
||||
'updated_at' => now()->toDateTimeString(),
|
||||
'created_at' => now()->toDateTimeString(),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
// persist
|
||||
BatchInsertNewCurrencyRatesJob::dispatch($updates);
|
||||
|
||||
return collect($updates)
|
||||
->whereBetween('date', [$start, $end ?? now()])
|
||||
->where('currency', $currency)
|
||||
->mapWithKeys(fn ($rate) => [
|
||||
$rate['date'] => $rate['rate'] * $adjustment,
|
||||
])
|
||||
->toArray();
|
||||
}
|
||||
|
||||
public static function refreshCurrencyData($force = false): void
|
||||
{
|
||||
$currencies = Currency::all()->pluck('currency')->toArray();
|
||||
|
||||
$rates = Frankfurter::setBaseCurrency(config('investbrain.base_currency'))
|
||||
->setSymbols($currencies)
|
||||
->latest();
|
||||
|
||||
$updates = [];
|
||||
foreach (Arr::get($rates, 'rates', []) as $currency => $rate) {
|
||||
|
||||
// update currency
|
||||
$updates[] = [
|
||||
'date' => now()->toDateString(),
|
||||
'currency' => $currency,
|
||||
'rate' => $rate,
|
||||
];
|
||||
}
|
||||
|
||||
// nothing to update
|
||||
if (empty($updates)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($force) {
|
||||
|
||||
// force overwrite existing rates
|
||||
CurrencyRate::upsert($updates, ['currency', 'date'], ['rate']);
|
||||
} else {
|
||||
|
||||
// only insert new rates
|
||||
CurrencyRate::insertOrIgnore($updates);
|
||||
}
|
||||
}
|
||||
|
||||
protected static function getCurrencyAliasAdjustments($currency)
|
||||
{
|
||||
$adjustment = 1;
|
||||
|
||||
if (array_key_exists($currency, config('investbrain.currency_aliases', []))) {
|
||||
|
||||
$config = config('investbrain.currency_aliases.'.$currency);
|
||||
|
||||
$adjustment = $config['adjustment'];
|
||||
$currency = $config['alias_of'];
|
||||
}
|
||||
|
||||
return [$currency, $adjustment];
|
||||
}
|
||||
}
|
||||
+142
-5
@@ -7,6 +7,7 @@ namespace App\Models;
|
||||
use App\Traits\HasCompositePrimaryKey;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class DailyChange extends Model
|
||||
{
|
||||
@@ -22,10 +23,6 @@ class DailyChange extends Model
|
||||
'portfolio_id',
|
||||
'date',
|
||||
'total_market_value',
|
||||
'total_cost_basis',
|
||||
'total_gain',
|
||||
'total_dividends_earned',
|
||||
'realized_gains',
|
||||
'notes',
|
||||
];
|
||||
|
||||
@@ -33,11 +30,16 @@ class DailyChange extends Model
|
||||
|
||||
protected $casts = [
|
||||
'date' => 'datetime',
|
||||
'total_market_value' => 'float',
|
||||
'total_cost_basis' => 'float',
|
||||
'total_gain' => 'float',
|
||||
'realized_gain_dollars' => 'float',
|
||||
'total_dividends_earned' => 'float',
|
||||
];
|
||||
|
||||
public function scopePortfolio($query, $portfolio)
|
||||
{
|
||||
return $query->where('portfolio_id', $portfolio);
|
||||
return $query->where('daily_change.portfolio_id', $portfolio);
|
||||
}
|
||||
|
||||
public function scopeMyDailyChanges()
|
||||
@@ -56,6 +58,141 @@ class DailyChange extends Model
|
||||
});
|
||||
}
|
||||
|
||||
public function scopeWithDailyPerformance($query)
|
||||
{
|
||||
$currency = auth()->user()?->getCurrency() ?? config('investbrain.base_currency');
|
||||
|
||||
$dividendSub = DB::table('holdings')
|
||||
->join('dividends', 'dividends.symbol', '=', 'holdings.symbol')
|
||||
->leftJoin('currency_rates as cr', function ($join) use ($currency) {
|
||||
$join->on('cr.date', '=', 'dividends.date')
|
||||
->where('cr.currency', '=', $currency);
|
||||
})
|
||||
->join('transactions as tx', function ($join) {
|
||||
$join->on('tx.symbol', '=', 'holdings.symbol')
|
||||
->on('tx.portfolio_id', '=', 'holdings.portfolio_id')
|
||||
->whereColumn('tx.date', '<=', 'dividends.date');
|
||||
})
|
||||
->select(['holdings.portfolio_id', 'dividends.date'])
|
||||
->selectRaw("
|
||||
((CASE WHEN tx.transaction_type = 'BUY'
|
||||
THEN tx.quantity ELSE 0 END)
|
||||
- (CASE WHEN tx.transaction_type = 'SELL'
|
||||
THEN tx.quantity ELSE 0 END))
|
||||
* SUM(
|
||||
dividends.dividend_amount_base
|
||||
* COALESCE(cr.rate, 1)
|
||||
)
|
||||
AS total_dividends_earned")
|
||||
->groupBy(['holdings.portfolio_id', 'dividends.date', 'tx.transaction_type', 'tx.quantity']);
|
||||
|
||||
$totalCostBasisSub = DB::table('transactions as tx1')
|
||||
->leftJoin('currency_rates as cr', function ($join) use ($currency) {
|
||||
$join->on('cr.date', '=', 'tx1.date')
|
||||
->where('cr.currency', '=', $currency);
|
||||
})
|
||||
->select([
|
||||
'tx1.portfolio_id',
|
||||
'tx1.date',
|
||||
'tx1.symbol',
|
||||
'tx1.transaction_type',
|
||||
'tx1.quantity',
|
||||
])
|
||||
->selectRaw("(CASE
|
||||
WHEN tx1.transaction_type = 'BUY'
|
||||
THEN COALESCE(cr.rate, 1)
|
||||
ELSE (
|
||||
SELECT
|
||||
SUM(COALESCE(cr2.rate, 1) * buy.cost_basis_base)
|
||||
/ SUM(buy.cost_basis_base)
|
||||
FROM transactions as buy
|
||||
LEFT JOIN currency_rates as cr2
|
||||
ON cr2.date = buy.date
|
||||
AND cr2.currency = '{$currency}'
|
||||
WHERE buy.symbol = tx1.symbol
|
||||
AND buy.portfolio_id = tx1.portfolio_id
|
||||
AND buy.transaction_type = 'BUY'
|
||||
AND buy.date <= tx1.date
|
||||
) END)
|
||||
AS rate")
|
||||
->selectRaw(
|
||||
"(CASE
|
||||
WHEN tx1.transaction_type = 'BUY'
|
||||
THEN AVG(tx1.cost_basis_base)
|
||||
ELSE (
|
||||
SELECT
|
||||
AVG(-buy.cost_basis_base)
|
||||
FROM transactions as buy
|
||||
WHERE buy.symbol = tx1.symbol
|
||||
AND buy.portfolio_id = tx1.portfolio_id
|
||||
AND buy.transaction_type = 'BUY'
|
||||
AND buy.date <= tx1.date
|
||||
) END)
|
||||
AS cost_basis_base")
|
||||
->selectRaw(
|
||||
"(CASE
|
||||
WHEN tx1.transaction_type = 'SELL'
|
||||
THEN tx1.sale_price_base - tx1.cost_basis_base
|
||||
ELSE 0 END)
|
||||
* tx1.quantity
|
||||
* COALESCE(cr.rate, 1)
|
||||
AS realized_gain_dollars")
|
||||
->groupBy([
|
||||
'tx1.portfolio_id',
|
||||
'tx1.date',
|
||||
'tx1.symbol',
|
||||
'tx1.transaction_type',
|
||||
'tx1.cost_basis_base',
|
||||
'tx1.quantity',
|
||||
'cr.rate',
|
||||
'tx1.sale_price_base',
|
||||
]);
|
||||
|
||||
return $query
|
||||
->select(['daily_change.portfolio_id', 'daily_change.date'])
|
||||
->leftJoinSub($totalCostBasisSub, 'cost_basis_display', function ($join) {
|
||||
$join->on('daily_change.date', '>=', 'cost_basis_display.date')
|
||||
->whereColumn('daily_change.portfolio_id', '=', 'cost_basis_display.portfolio_id');
|
||||
})
|
||||
->leftJoin('currency_rates as cr', function ($join) use ($currency) {
|
||||
$join->on('cr.date', '=', 'daily_change.date')
|
||||
->where('cr.currency', '=', $currency);
|
||||
})
|
||||
->selectRaw('
|
||||
SUM(
|
||||
cost_basis_display.cost_basis_base
|
||||
* cost_basis_display.quantity
|
||||
* cost_basis_display.rate
|
||||
) as total_cost_basis')
|
||||
->selectRaw('(
|
||||
daily_change.total_market_value * COALESCE(cr.rate, 1)
|
||||
) - SUM(
|
||||
cost_basis_display.cost_basis_base
|
||||
* cost_basis_display.quantity
|
||||
* cost_basis_display.rate
|
||||
) as total_gain')
|
||||
->selectRaw('(
|
||||
daily_change.total_market_value * COALESCE(cr.rate, 1)
|
||||
) as total_market_value')
|
||||
->selectRaw('
|
||||
SUM(
|
||||
cost_basis_display.realized_gain_dollars
|
||||
) as realized_gain_dollars')
|
||||
->selectSub(function ($query) use ($dividendSub) {
|
||||
$query->fromSub($dividendSub, 'd')
|
||||
->selectRaw('SUM(d.total_dividends_earned)')
|
||||
->whereColumn('d.date', '<=', 'daily_change.date')
|
||||
->whereColumn('d.portfolio_id', '=', 'daily_change.portfolio_id');
|
||||
}, 'total_dividends_earned')
|
||||
->groupBy([
|
||||
'daily_change.date',
|
||||
'cr.rate',
|
||||
'daily_change.total_market_value',
|
||||
'daily_change.portfolio_id',
|
||||
])
|
||||
->orderBy('daily_change.date');
|
||||
}
|
||||
|
||||
public function portfolio()
|
||||
{
|
||||
return $this->belongsTo(Portfolio::class);
|
||||
|
||||
+35
-10
@@ -4,17 +4,24 @@ declare(strict_types=1);
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Actions\CopyToBaseCurrency;
|
||||
use App\Casts\BaseCurrency;
|
||||
use App\Interfaces\MarketData\MarketDataInterface;
|
||||
use App\Traits\HasMarketData;
|
||||
use Illuminate\Database\Eloquent\Concerns\HasUuids;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Carbon;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Pipeline;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class Dividend extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
use HasMarketData;
|
||||
use HasUuids;
|
||||
|
||||
protected $fillable = [
|
||||
@@ -26,21 +33,32 @@ class Dividend extends Model
|
||||
protected $hidden = [];
|
||||
|
||||
protected $casts = [
|
||||
'date' => 'datetime',
|
||||
'last_dividend_update' => 'datetime',
|
||||
'date' => 'date',
|
||||
'last_dividend_update' => 'date',
|
||||
'dividend_amount' => 'float',
|
||||
'dividend_amount_base' => BaseCurrency::class,
|
||||
];
|
||||
|
||||
public function marketData()
|
||||
protected static function boot()
|
||||
{
|
||||
return $this->belongsTo(MarketData::class, 'symbol', 'symbol');
|
||||
parent::boot();
|
||||
|
||||
static::saving(function ($dividend) {
|
||||
|
||||
$dividend = Pipeline::send($dividend)
|
||||
->through([
|
||||
CopyToBaseCurrency::class,
|
||||
])
|
||||
->then(fn (Dividend $dividend) => $dividend);
|
||||
});
|
||||
}
|
||||
|
||||
public function holdings()
|
||||
public function holdings(): HasMany
|
||||
{
|
||||
return $this->hasMany(Holding::class, 'symbol', 'symbol');
|
||||
}
|
||||
|
||||
public function transactions()
|
||||
public function transactions(): HasMany
|
||||
{
|
||||
return $this->hasMany(Transaction::class, 'symbol', 'symbol');
|
||||
}
|
||||
@@ -84,8 +102,18 @@ class Dividend extends Model
|
||||
|
||||
// ah, we found some dividends...
|
||||
if ($dividend_data->isNotEmpty()) {
|
||||
|
||||
$market_data = MarketData::getMarketData($symbol);
|
||||
|
||||
// get historic conversion rates
|
||||
$rate_to_base = CurrencyRate::timeSeriesRates($market_data->currency, $start_date, $end_date);
|
||||
|
||||
// create mass insert
|
||||
foreach ($dividend_data as $index => $dividend) {
|
||||
$rate_to_base_date = 1 / Arr::get($rate_to_base, Carbon::parse(Arr::get($dividend, 'date'))->toDateString(), 1);
|
||||
|
||||
$dividend['dividend_amount_base'] = $dividend['dividend_amount'] * $rate_to_base_date;
|
||||
|
||||
$dividend_data[$index] = [...$dividend, ...['id' => Str::uuid()->toString(), 'updated_at' => now(), 'created_at' => now()]];
|
||||
}
|
||||
|
||||
@@ -95,9 +123,6 @@ class Dividend extends Model
|
||||
// sync to holdings
|
||||
self::syncHoldings($symbol);
|
||||
|
||||
// get market data
|
||||
$market_data = MarketData::firstOrNew(['symbol' => $symbol]);
|
||||
|
||||
// re-invest dividends
|
||||
self::reinvestDividends($dividend_data, $market_data);
|
||||
|
||||
@@ -127,7 +152,7 @@ class Dividend extends Model
|
||||
")->join('transactions', 'transactions.symbol', '=', 'dividends.symbol')
|
||||
->join('holdings', 'transactions.portfolio_id', '=', 'holdings.portfolio_id')
|
||||
->where('dividends.symbol', $symbol)
|
||||
->groupBy('holdings.portfolio_id', 'dividends.date', 'dividends.symbol', 'dividends.dividend_amount');
|
||||
->groupBy('holdings.portfolio_id', 'dividends.date', 'dividends.symbol', 'dividends.dividend_amount', 'dividends.dividend_amount_base');
|
||||
|
||||
$dividends = DB::table(DB::raw("({$subQuery->toSql()}) as sub"))
|
||||
->mergeBindings($subQuery->getQuery())
|
||||
|
||||
+215
-35
@@ -4,15 +4,18 @@ declare(strict_types=1);
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Traits\HasMarketData;
|
||||
use Illuminate\Database\Eloquent\Concerns\HasUuids;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class Holding extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
use HasMarketData;
|
||||
use HasUuids;
|
||||
|
||||
protected $fillable = [
|
||||
@@ -28,21 +31,24 @@ class Holding extends Model
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'reinvest_dividends' => 'boolean',
|
||||
'splits_synced_at' => 'datetime',
|
||||
'first_transaction_date' => 'datetime',
|
||||
'reinvest_dividends' => 'boolean',
|
||||
'quantity' => 'float',
|
||||
'average_cost_basis' => 'float',
|
||||
'total_cost_basis' => 'float',
|
||||
'realized_gain_dollars' => 'float',
|
||||
'dividends_earned' => 'float',
|
||||
'total_gain_dollars' => 'float',
|
||||
'market_gain_dollars' => 'float',
|
||||
'total_market_value' => 'float',
|
||||
'total_dividends_earned' => 'float',
|
||||
'market_data_market_value' => 'float',
|
||||
'market_data_fifty_two_week_low' => 'float',
|
||||
'market_data_fifty_two_week_high' => 'float',
|
||||
'market_gain_percent' => 'float',
|
||||
];
|
||||
|
||||
/**
|
||||
* Market data for holding
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function market_data()
|
||||
{
|
||||
return $this->hasOne(MarketData::class, 'symbol', 'symbol');
|
||||
}
|
||||
|
||||
/**
|
||||
* Related transactions for holding
|
||||
*
|
||||
@@ -61,7 +67,7 @@ class Holding extends Model
|
||||
public function dividends()
|
||||
{
|
||||
return $this->hasMany(Dividend::class, 'symbol', 'symbol')
|
||||
->select(['dividends.symbol', 'dividends.date', 'dividends.dividend_amount'])
|
||||
->select(['dividends.symbol', 'dividends.date', 'dividends.dividend_amount', 'dividends.dividend_amount_base'])
|
||||
->selectRaw("SUM(
|
||||
CASE WHEN transaction_type = 'BUY'
|
||||
AND transactions.symbol = dividends.symbol
|
||||
@@ -91,8 +97,21 @@ class Holding extends Model
|
||||
THEN transactions.quantity ELSE 0 END)
|
||||
* dividends.dividend_amount
|
||||
) AS total_received")
|
||||
->selectRaw("SUM(
|
||||
(CASE WHEN transaction_type = 'BUY'
|
||||
AND transactions.symbol = dividends.symbol
|
||||
AND transactions.portfolio_id = '$this->portfolio_id'
|
||||
AND date(transactions.date) <= date(dividends.date)
|
||||
THEN transactions.quantity ELSE 0 END
|
||||
- CASE WHEN transaction_type = 'SELL'
|
||||
AND transactions.symbol = dividends.symbol
|
||||
AND transactions.portfolio_id = '$this->portfolio_id'
|
||||
AND date(transactions.date) <= date(dividends.date)
|
||||
THEN transactions.quantity ELSE 0 END)
|
||||
* dividends.dividend_amount_base
|
||||
) AS total_received_base")
|
||||
->join('transactions', 'transactions.symbol', 'dividends.symbol')
|
||||
->groupBy(['dividends.symbol', 'dividends.date', 'dividends.dividend_amount'])
|
||||
->groupBy(['dividends.symbol', 'dividends.date', 'dividends.dividend_amount', 'dividends.dividend_amount_base'])
|
||||
->orderBy('dividends.date', 'DESC')
|
||||
->where('dividends.date', '>=', function ($query) {
|
||||
$query->selectRaw('min(transactions.date)')
|
||||
@@ -118,7 +137,7 @@ class Holding extends Model
|
||||
THEN transactions.quantity
|
||||
ELSE 0
|
||||
END)
|
||||
) * dividends.dividend_amount > 0");
|
||||
) * dividends.dividend_amount_base > 0");
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -156,12 +175,16 @@ class Holding extends Model
|
||||
{
|
||||
return $query->withAggregate('market_data', 'name')
|
||||
->withAggregate('market_data', 'market_value')
|
||||
->withAggregate('market_data', 'market_value_base')
|
||||
->withAggregate('market_data', 'fifty_two_week_low')
|
||||
->withAggregate('market_data', 'fifty_two_week_high')
|
||||
->withAggregate('market_data', 'updated_at')
|
||||
->join('market_data', 'holdings.symbol', 'market_data.symbol');
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate performance for holding in its local currency
|
||||
*/
|
||||
public function scopeWithPerformance($query)
|
||||
{
|
||||
return $query->selectRaw('COALESCE(market_data.market_value * holdings.quantity, 0) AS total_market_value')
|
||||
@@ -193,15 +216,169 @@ class Holding extends Model
|
||||
});
|
||||
}
|
||||
|
||||
public function scopeWithPortfolioMetrics($query)
|
||||
/**
|
||||
* Scope which returns collection of performance metrics for holdings
|
||||
*
|
||||
* @param string $currency Allows casting to specified currency
|
||||
*/
|
||||
public function scopeGetPortfolioMetrics($query, $currency = null): Collection
|
||||
{
|
||||
return $query->selectRaw('COALESCE(SUM(holdings.dividends_earned), 0) AS total_dividends_earned')
|
||||
->selectRaw('COALESCE(SUM(holdings.realized_gain_dollars), 0) AS realized_gain_dollars')
|
||||
->selectRaw('COALESCE(SUM(holdings.quantity * market_data.market_value), 0) AS total_market_value')
|
||||
->selectRaw('COALESCE(SUM(holdings.total_cost_basis), 0) AS total_cost_basis')
|
||||
->selectRaw('COALESCE(SUM(holdings.quantity * market_data.market_value), 0) - COALESCE(SUM(holdings.total_cost_basis), 0) AS total_gain_dollars')
|
||||
// ->selectRaw('COALESCE((@total_gain_dollars / @sum_total_cost_basis) * 100,0) AS total_gain_percent')
|
||||
->join('market_data', 'market_data.symbol', '=', 'holdings.symbol');
|
||||
$result = $query->withPortfolioMetrics($currency)->get();
|
||||
|
||||
return collect([
|
||||
'total_cost_basis' => $result->sum('total_cost_basis'),
|
||||
'total_market_value' => $result->sum('total_market_value'),
|
||||
'total_gain_dollars' => $result->sum('total_gain_dollars'),
|
||||
'realized_gain_dollars' => $result->sum('realized_gain_dollars'),
|
||||
'total_dividends_earned' => $result->sum('total_dividends_earned'),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Scope to collect performance metrics for holdings
|
||||
*
|
||||
* @param string $currency Allows casting to specified currency
|
||||
*/
|
||||
public function scopeWithPortfolioMetrics($query, $currency = null): mixed
|
||||
{
|
||||
$currency = $currency ?? auth()->user()->getCurrency();
|
||||
|
||||
return $query->select([
|
||||
'holdings.symbol',
|
||||
'holdings.portfolio_id',
|
||||
'transactions_display.total_cost_basis',
|
||||
'transactions_display.realized_gain_dollars',
|
||||
'dividends_display.total_dividends_earned',
|
||||
])
|
||||
->groupBy([
|
||||
'holdings.symbol',
|
||||
'holdings.quantity',
|
||||
'holdings.portfolio_id',
|
||||
'cr.rate',
|
||||
'transactions_display.total_cost_basis',
|
||||
'transactions_display.realized_gain_dollars',
|
||||
'dividends_display.total_dividends_earned',
|
||||
'market_data.market_value_base',
|
||||
])
|
||||
->leftJoin('currency_rates as cr', function ($join) use ($currency) {
|
||||
$join->where('cr.currency', '=', $currency);
|
||||
|
||||
if (config('database.default') === 'sqlite') {
|
||||
|
||||
$join->whereRaw("strftime('%Y-%m-%d', cr.date) = ?", [now()->toDateString()]);
|
||||
} else {
|
||||
|
||||
$join->on('cr.date', '=', DB::raw("'".now()->toDateString()."'"));
|
||||
}
|
||||
})
|
||||
->leftJoin('market_data', function ($join) {
|
||||
$join->on('market_data.symbol', '=', 'holdings.symbol');
|
||||
})
|
||||
->selectRaw(
|
||||
'holdings.quantity * market_data.market_value_base * COALESCE(cr.rate, 1) AS total_market_value'
|
||||
)
|
||||
->selectRaw('(
|
||||
holdings.quantity * market_data.market_value_base * COALESCE(cr.rate, 1)
|
||||
) - transactions_display.total_cost_basis as total_gain_dollars')
|
||||
->leftJoinSub(
|
||||
DB::table('transactions')
|
||||
->leftJoin('currency_rates as cr', function ($join) use ($currency) {
|
||||
$join->on('cr.date', '=', 'transactions.date')
|
||||
->where('cr.currency', '=', $currency);
|
||||
})
|
||||
->select(['transactions.symbol', 'transactions.portfolio_id'])
|
||||
->leftJoinSub(
|
||||
DB::table('transactions')
|
||||
->leftJoin('currency_rates as cr', function ($join) use ($currency) {
|
||||
$join
|
||||
->on('cr.date', '=', 'transactions.date')
|
||||
->where('cr.currency', '=', $currency);
|
||||
})
|
||||
->select([
|
||||
'transactions.symbol',
|
||||
'transactions.portfolio_id',
|
||||
'transactions.quantity',
|
||||
'transactions.date',
|
||||
])
|
||||
->selectRaw(
|
||||
"(CASE
|
||||
WHEN transactions.transaction_type = 'BUY'
|
||||
THEN COALESCE(cr.rate, 1)
|
||||
ELSE (
|
||||
SELECT
|
||||
SUM(COALESCE(cr2.rate, 1) * buy.cost_basis_base)
|
||||
/ SUM(buy.cost_basis_base)
|
||||
FROM transactions as buy
|
||||
LEFT JOIN currency_rates as cr2
|
||||
ON cr2.date = buy.date
|
||||
AND cr2.currency = '{$currency}'
|
||||
WHERE buy.symbol = transactions.symbol
|
||||
AND buy.portfolio_id = transactions.portfolio_id
|
||||
AND buy.transaction_type = 'BUY'
|
||||
AND buy.date <= transactions.date
|
||||
) END)
|
||||
AS rate"
|
||||
)
|
||||
->selectRaw(
|
||||
"(CASE
|
||||
WHEN transactions.transaction_type = 'BUY'
|
||||
THEN AVG(transactions.cost_basis_base)
|
||||
ELSE (
|
||||
SELECT
|
||||
AVG(-buy.cost_basis_base)
|
||||
FROM transactions as buy
|
||||
WHERE buy.symbol = transactions.symbol
|
||||
AND buy.portfolio_id = transactions.portfolio_id
|
||||
AND buy.transaction_type = 'BUY'
|
||||
AND buy.date <= transactions.date
|
||||
) END)
|
||||
AS cost_basis_base"
|
||||
)
|
||||
->groupBy([
|
||||
'transactions.symbol',
|
||||
'transactions.date',
|
||||
'transactions.portfolio_id',
|
||||
'transactions.transaction_type',
|
||||
'transactions.quantity',
|
||||
'cr.rate',
|
||||
]), 'cost_basis_display', function ($join) {
|
||||
$join->on('transactions.symbol', '=', 'cost_basis_display.symbol')
|
||||
->on('transactions.portfolio_id', '=', 'cost_basis_display.portfolio_id')
|
||||
->on('transactions.date', '=', 'cost_basis_display.date');
|
||||
})
|
||||
->selectRaw(
|
||||
"SUM(CASE WHEN transactions.transaction_type = 'SELL' THEN (transactions.sale_price_base - transactions.cost_basis_base) * transactions.quantity * COALESCE(cr.rate, 1) ELSE 0 END) AS realized_gain_dollars"
|
||||
)
|
||||
->selectRaw(
|
||||
'SUM(cost_basis_display.cost_basis_base * cost_basis_display.quantity * cost_basis_display.rate) AS total_cost_basis'
|
||||
)
|
||||
->groupBy(['transactions.symbol', 'transactions.portfolio_id']),
|
||||
'transactions_display',
|
||||
function ($join) {
|
||||
$join->on('holdings.symbol', '=', 'transactions_display.symbol')
|
||||
->on('holdings.portfolio_id', '=', 'transactions_display.portfolio_id');
|
||||
}
|
||||
)
|
||||
->leftJoinSub(
|
||||
DB::table('dividends')
|
||||
->join('transactions as tx', function ($join) {
|
||||
$join->on('tx.symbol', '=', 'dividends.symbol')
|
||||
->on('tx.date', '<=', 'dividends.date');
|
||||
})
|
||||
->leftJoin('currency_rates as cr', function ($join) use ($currency) {
|
||||
$join->on('cr.date', '=', 'dividends.date')
|
||||
->where('cr.currency', '=', $currency);
|
||||
})
|
||||
->select(['dividends.symbol'])
|
||||
->selectRaw(
|
||||
"SUM(((CASE WHEN transaction_type = 'BUY' THEN tx.quantity ELSE 0 END) - (CASE WHEN transaction_type = 'SELL' THEN tx.quantity ELSE 0 END)) * dividends.dividend_amount_base * COALESCE(cr.rate, 1)) AS total_dividends_earned"
|
||||
)
|
||||
->groupBy(['dividends.symbol']),
|
||||
'dividends_display',
|
||||
function ($join) {
|
||||
$join->on('holdings.symbol', '=', 'dividends_display.symbol');
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
public function syncTransactionsAndDividends()
|
||||
@@ -209,14 +386,14 @@ class Holding extends Model
|
||||
// pull existing transaction data
|
||||
$query = Transaction::where([
|
||||
'portfolio_id' => $this->portfolio_id,
|
||||
'symbol' => $this->symbol,
|
||||
'transactions.symbol' => $this->symbol,
|
||||
])->selectRaw("SUM(CASE WHEN transaction_type = 'BUY' THEN quantity ELSE 0 END) AS qty_purchases")
|
||||
->selectRaw("SUM(CASE WHEN transaction_type = 'SELL' THEN quantity ELSE 0 END) AS qty_sales")
|
||||
->selectRaw("SUM(CASE WHEN transaction_type = 'SELL' THEN (sale_price - cost_basis) * quantity ELSE 0 END) AS realized_gain_dollars")
|
||||
->selectRaw("SUM(CASE WHEN transaction_type = 'BUY' THEN (quantity * cost_basis) ELSE 0 END) AS total_cost_basis")
|
||||
->selectRaw("SUM(CASE WHEN transaction_type = 'SELL' THEN (quantity * sale_price) ELSE 0 END) AS total_sale_price")
|
||||
->first();
|
||||
|
||||
$total_quantity = round($query->qty_purchases - $query->qty_sales, 3);
|
||||
$total_quantity = round($query->qty_purchases - $query->qty_sales, 4);
|
||||
|
||||
$average_cost_basis = (
|
||||
$query->qty_purchases > 0
|
||||
@@ -229,9 +406,7 @@ class Holding extends Model
|
||||
'quantity' => $total_quantity,
|
||||
'average_cost_basis' => $average_cost_basis,
|
||||
'total_cost_basis' => $total_quantity * $average_cost_basis,
|
||||
'realized_gain_dollars' => $query->qty_purchases > 0 && $query->total_sale_price > 0
|
||||
? $query->total_sale_price - ($query->qty_sales * ($query->total_cost_basis / $query->qty_purchases))
|
||||
: 0,
|
||||
'realized_gain_dollars' => $query->realized_gain_dollars ?? 0,
|
||||
'dividends_earned' => $this->dividends->sum('total_received'),
|
||||
]);
|
||||
|
||||
@@ -253,6 +428,11 @@ class Holding extends Model
|
||||
return $purchases - $sales;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method that enables calculating daily performance for a given holding
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function dailyPerformance(
|
||||
?\Illuminate\Support\Carbon $start_date = null,
|
||||
?\Illuminate\Support\Carbon $end_date = null,
|
||||
@@ -277,11 +457,11 @@ class Holding extends Model
|
||||
// Default CTE time series query (for MySQL and SQLite)
|
||||
$timeSeriesQuery = DB::table(DB::raw("(
|
||||
WITH RECURSIVE date_series AS (
|
||||
SELECT '{$start_date->format('Y-m-d')}' AS date
|
||||
SELECT '{$start_date->toDateString()}' AS date
|
||||
UNION ALL
|
||||
SELECT $date_interval
|
||||
FROM date_series
|
||||
WHERE date < '{$end_date->format('Y-m-d')}'
|
||||
WHERE date < '{$end_date->toDateString()}'
|
||||
)
|
||||
SELECT date_series.date
|
||||
FROM date_series
|
||||
@@ -292,8 +472,8 @@ class Holding extends Model
|
||||
|
||||
$timeSeriesQuery = DB::table(DB::raw("
|
||||
generate_series(
|
||||
date '{$start_date->format('Y-m-d')}',
|
||||
date '{$end_date->format('Y-m-d')}',
|
||||
date '{$start_date->toDateString()}',
|
||||
date '{$end_date->toDateString()}',
|
||||
interval '1 day'
|
||||
) as date_series"));
|
||||
|
||||
@@ -335,12 +515,12 @@ class Holding extends Model
|
||||
CASE
|
||||
WHEN ({$quantityQuery}) = 0 THEN 0
|
||||
ELSE SUM(CASE
|
||||
WHEN transactions.transaction_type = 'BUY' THEN transactions.quantity * transactions.cost_basis
|
||||
WHEN transactions.transaction_type = 'BUY' THEN transactions.quantity * transactions.cost_basis_base
|
||||
ELSE 0
|
||||
END)
|
||||
END AS cost_basis
|
||||
"),
|
||||
DB::raw("COALESCE(SUM(CASE WHEN transaction_type = 'SELL' THEN ((sale_price - cost_basis) * quantity) ELSE 0 END), 0) AS realized_gains"),
|
||||
DB::raw("COALESCE(SUM(CASE WHEN transaction_type = 'SELL' THEN ((sale_price_base - cost_basis_base) * quantity) ELSE 0 END), 0) AS realized_gains"),
|
||||
])
|
||||
->leftJoin('transactions', function ($join) {
|
||||
$join->on(DB::raw('DATE(transactions.date)'), '<=', 'date_series.date')
|
||||
@@ -357,7 +537,7 @@ class Holding extends Model
|
||||
{
|
||||
$formattedTransactions = '';
|
||||
foreach ($this->transactions->sortByDesc('date') as $transaction) {
|
||||
$formattedTransactions .= ' * '.$transaction->date->format('Y-m-d')
|
||||
$formattedTransactions .= ' * '.$transaction->date->toDateString()
|
||||
.' '.$transaction->transaction_type
|
||||
.' '.$transaction->quantity
|
||||
.' @ '.$transaction->cost_basis
|
||||
|
||||
@@ -4,9 +4,12 @@ declare(strict_types=1);
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Actions\CopyToBaseCurrency;
|
||||
use App\Casts\BaseCurrency;
|
||||
use App\Interfaces\MarketData\MarketDataInterface;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Facades\Pipeline;
|
||||
|
||||
class MarketData extends Model
|
||||
{
|
||||
@@ -21,7 +24,9 @@ class MarketData extends Model
|
||||
protected $fillable = [
|
||||
'symbol',
|
||||
'name',
|
||||
'currency',
|
||||
'market_value',
|
||||
'market_value_base',
|
||||
'fifty_two_week_high',
|
||||
'fifty_two_week_low',
|
||||
'forward_pe',
|
||||
@@ -29,21 +34,40 @@ class MarketData extends Model
|
||||
'market_cap',
|
||||
'book_value',
|
||||
'last_dividend_date',
|
||||
'last_dividend_amount',
|
||||
'dividend_yield',
|
||||
'meta_data',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'last_dividend_date' => 'datetime',
|
||||
'market_value' => 'float',
|
||||
'market_value_base' => BaseCurrency::class,
|
||||
'fifty_two_week_high' => 'float',
|
||||
'fifty_two_week_low' => 'float',
|
||||
'forward_pe' => 'float',
|
||||
'trailing_pe' => 'float',
|
||||
'market_cap' => 'float',
|
||||
'market_cap' => 'integer',
|
||||
'book_value' => 'float',
|
||||
'last_dividend_date' => 'datetime',
|
||||
'last_dividend_amount' => 'float',
|
||||
'dividend_yield' => 'float',
|
||||
'meta_data' => 'json',
|
||||
];
|
||||
|
||||
protected static function boot()
|
||||
{
|
||||
parent::boot();
|
||||
|
||||
static::saving(function ($market_data) {
|
||||
|
||||
$market_data = Pipeline::send($market_data)
|
||||
->through([
|
||||
CopyToBaseCurrency::class,
|
||||
])
|
||||
->then(fn (MarketData $market_data) => $market_data);
|
||||
});
|
||||
}
|
||||
|
||||
public function holdings()
|
||||
{
|
||||
return $this->hasMany(Holding::class, 'symbol', 'symbol');
|
||||
@@ -54,7 +78,7 @@ class MarketData extends Model
|
||||
return $query->where('symbol', $symbol);
|
||||
}
|
||||
|
||||
public static function getMarketData($symbol, $force = false)
|
||||
public static function getMarketData($symbol, $force = false): self
|
||||
{
|
||||
$market_data = self::firstOrNew([
|
||||
'symbol' => $symbol,
|
||||
|
||||
@@ -136,6 +136,9 @@ class Portfolio extends Model
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes daily change history for a portfolio to the database
|
||||
*/
|
||||
public function syncDailyChanges(): void
|
||||
{
|
||||
$holdings = $this->holdings()
|
||||
@@ -147,11 +150,9 @@ class Portfolio extends Model
|
||||
->groupBy(['holdings.symbol', 'holdings.portfolio_id'])
|
||||
->get();
|
||||
|
||||
$dividends = Dividend::whereIn('symbol', $holdings->pluck('symbol'))->get();
|
||||
|
||||
$total_performance = [];
|
||||
|
||||
$holdings->each(function ($holding) use (&$total_performance, $dividends) {
|
||||
$holdings->each(function ($holding) use (&$total_performance) {
|
||||
|
||||
$period = CarbonPeriod::create(
|
||||
$holding->first_transaction_date,
|
||||
@@ -160,34 +161,25 @@ class Portfolio extends Model
|
||||
: now()
|
||||
);
|
||||
|
||||
$holding->setRelation('dividends', $dividends->where('symbol', $holding->symbol));
|
||||
|
||||
$daily_performance = $holding->dailyPerformance($holding->first_transaction_date, now());
|
||||
$dividends = $holding->dividends->keyBy(function ($dividend, $key) {
|
||||
return $dividend['date']->format('Y-m-d');
|
||||
});
|
||||
$all_history = app(MarketDataInterface::class)->history($holding->symbol, $holding->first_transaction_date, now());
|
||||
$currency_rates = CurrencyRate::timeSeriesRates($holding->market_data->currency, $holding->first_transaction_date, now());
|
||||
|
||||
$dividends_earned = 0;
|
||||
$holding_performance = [];
|
||||
|
||||
foreach ($period as $date) {
|
||||
$date = $date->format('Y-m-d');
|
||||
$date = $date->toDateString();
|
||||
|
||||
$close = $this->getMostRecentCloseData($all_history, $date);
|
||||
|
||||
$total_market_value = $daily_performance->get($date)->owned * $close;
|
||||
$dividends_earned += $daily_performance->get($date)->owned * ($dividends->get($date)?->dividend_amount ?? 0);
|
||||
|
||||
if (Carbon::parse($date)->isWeekday()) {
|
||||
|
||||
$holding_performance[$date] = [
|
||||
'date' => $date,
|
||||
'portfolio_id' => $this->id,
|
||||
'total_market_value' => $total_market_value,
|
||||
'total_cost_basis' => $daily_performance->get($date)->cost_basis,
|
||||
'total_gain' => $total_market_value - $daily_performance->get($date)->cost_basis,
|
||||
'realized_gains' => $daily_performance->get($date)->realized_gains,
|
||||
'total_dividends_earned' => $dividends_earned,
|
||||
'total_market_value' => $total_market_value * (1 / Arr::get($currency_rates, $date, 1)),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -200,10 +192,6 @@ class Portfolio extends Model
|
||||
} else {
|
||||
|
||||
$total_performance[$date]['total_market_value'] += $performance['total_market_value'];
|
||||
$total_performance[$date]['total_cost_basis'] += $performance['total_cost_basis'];
|
||||
$total_performance[$date]['total_gain'] += $performance['total_gain'];
|
||||
$total_performance[$date]['realized_gains'] += $performance['realized_gains'];
|
||||
$total_performance[$date]['total_dividends_earned'] += $performance['total_dividends_earned'];
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -221,10 +209,6 @@ class Portfolio extends Model
|
||||
['date', 'portfolio_id'],
|
||||
[
|
||||
'total_market_value',
|
||||
'total_cost_basis',
|
||||
'total_gain',
|
||||
'realized_gains',
|
||||
'total_dividends_earned',
|
||||
]
|
||||
);
|
||||
});
|
||||
@@ -239,7 +223,7 @@ class Portfolio extends Model
|
||||
|
||||
$i++;
|
||||
|
||||
$date = Carbon::parse($date)->subDay()->format('Y-m-d');
|
||||
$date = Carbon::parse($date)->subDay()->toDateString();
|
||||
|
||||
return $this->getMostRecentCloseData($history, $date, $i);
|
||||
}
|
||||
|
||||
@@ -5,15 +5,18 @@ declare(strict_types=1);
|
||||
namespace App\Models;
|
||||
|
||||
use App\Interfaces\MarketData\MarketDataInterface;
|
||||
use App\Traits\HasMarketData;
|
||||
use Illuminate\Database\Eloquent\Concerns\HasUuids;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class Split extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
use HasMarketData;
|
||||
use HasUuids;
|
||||
|
||||
protected $fillable = [
|
||||
@@ -29,12 +32,12 @@ class Split extends Model
|
||||
'last_date' => 'datetime',
|
||||
];
|
||||
|
||||
public function holdings()
|
||||
public function holdings(): HasMany
|
||||
{
|
||||
return $this->hasMany(Holding::class, 'symbol', 'symbol');
|
||||
}
|
||||
|
||||
public function transactions()
|
||||
public function transactions(): HasMany
|
||||
{
|
||||
return $this->hasMany(Transaction::class, 'symbol', 'symbol');
|
||||
}
|
||||
@@ -114,7 +117,7 @@ class Split extends Model
|
||||
'symbol' => $split->symbol,
|
||||
'portfolio_id' => $split->portfolio_id,
|
||||
])
|
||||
->whereDate('transactions.date', '<', $split->date->format('Y-m-d'))
|
||||
->whereDate('transactions.date', '<', $split->date->toDateString())
|
||||
->selectRaw("SUM(CASE WHEN transaction_type = 'BUY' THEN quantity ELSE 0 END) -
|
||||
SUM(CASE WHEN transaction_type = 'SELL' THEN quantity ELSE 0 END) AS qty_owned")
|
||||
->value('qty_owned');
|
||||
|
||||
+22
-41
@@ -4,18 +4,24 @@ declare(strict_types=1);
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Actions\ConvertToMarketDataCurrency;
|
||||
use App\Actions\CopyToBaseCurrency;
|
||||
use App\Actions\EnsureCostBasisAddedToSale;
|
||||
use App\Casts\BaseCurrency;
|
||||
use App\Traits\HasMarketData;
|
||||
use Illuminate\Contracts\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Casts\Attribute;
|
||||
use Illuminate\Database\Eloquent\Concerns\HasUuids;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\HasOne;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Facades\Pipeline;
|
||||
|
||||
class Transaction extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
use HasMarketData;
|
||||
use HasUuids;
|
||||
|
||||
protected $fillable = [
|
||||
@@ -23,6 +29,7 @@ class Transaction extends Model
|
||||
'date',
|
||||
'portfolio_id',
|
||||
'transaction_type',
|
||||
'currency',
|
||||
'quantity',
|
||||
'cost_basis',
|
||||
'sale_price',
|
||||
@@ -36,6 +43,11 @@ class Transaction extends Model
|
||||
'date' => 'datetime',
|
||||
'split' => 'boolean',
|
||||
'reinvested_dividend' => 'boolean',
|
||||
'quantity' => 'float',
|
||||
'cost_basis' => 'float',
|
||||
'sale_price' => 'float',
|
||||
'cost_basis_base' => BaseCurrency::class,
|
||||
'sale_price_base' => BaseCurrency::class,
|
||||
];
|
||||
|
||||
protected static function boot()
|
||||
@@ -44,18 +56,19 @@ class Transaction extends Model
|
||||
|
||||
static::saving(function ($transaction) {
|
||||
|
||||
if ($transaction->transaction_type == 'SELL') {
|
||||
|
||||
$transaction->ensureCostBasisIsAddedToSale();
|
||||
}
|
||||
$transaction = Pipeline::send($transaction)
|
||||
->through([
|
||||
ConvertToMarketDataCurrency::class,
|
||||
EnsureCostBasisAddedToSale::class,
|
||||
CopyToBaseCurrency::class,
|
||||
])
|
||||
->then(fn (Transaction $transaction) => $transaction);
|
||||
});
|
||||
|
||||
static::saved(function ($transaction) {
|
||||
|
||||
$transaction->syncToHolding();
|
||||
|
||||
$transaction->refreshMarketData();
|
||||
|
||||
cache()->forget('portfolio-metrics-'.$transaction->portfolio_id);
|
||||
});
|
||||
|
||||
@@ -77,16 +90,6 @@ class Transaction extends Model
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Related market data for transaction
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function market_data(): HasOne
|
||||
{
|
||||
return $this->hasOne(MarketData::class, 'symbol', 'symbol');
|
||||
}
|
||||
|
||||
/**
|
||||
* Related portfolio
|
||||
*
|
||||
@@ -141,28 +144,6 @@ class Transaction extends Model
|
||||
});
|
||||
}
|
||||
|
||||
public function refreshMarketData(): void
|
||||
{
|
||||
MarketData::getMarketData($this->attributes['symbol']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes average cost basis to a sale transaction
|
||||
*/
|
||||
public function ensureCostBasisIsAddedToSale(): Transaction
|
||||
{
|
||||
$average_cost_basis = Transaction::where([
|
||||
'portfolio_id' => $this->portfolio_id,
|
||||
'symbol' => $this->symbol,
|
||||
'transaction_type' => 'BUY',
|
||||
])->whereDate('date', '<=', $this->date)
|
||||
->average('cost_basis');
|
||||
|
||||
$this->cost_basis = $average_cost_basis ?? 0;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Syncs the holding related to this transaction
|
||||
*/
|
||||
@@ -187,8 +168,8 @@ class Transaction extends Model
|
||||
'portfolio_id' => $this->portfolio_id,
|
||||
'symbol' => $this->symbol,
|
||||
'quantity' => $this->quantity,
|
||||
'average_cost_basis' => $this->cost_basis,
|
||||
'total_cost_basis' => $this->quantity * $this->cost_basis,
|
||||
'average_cost_basis' => $this->cost_basis_base,
|
||||
'total_cost_basis' => $this->quantity * $this->cost_basis_base,
|
||||
'splits_synced_at' => now(),
|
||||
])->syncTransactionsAndDividends();
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@ use Illuminate\Database\Eloquent\Concerns\HasUuids;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||
use Illuminate\Notifications\Notifiable;
|
||||
use Illuminate\Support\Arr;
|
||||
use Laravel\Fortify\TwoFactorAuthenticatable;
|
||||
use Laravel\Jetstream\HasProfilePhoto;
|
||||
use Laravel\Sanctum\HasApiTokens;
|
||||
@@ -31,6 +32,7 @@ class User extends Authenticatable implements MustVerifyEmail
|
||||
'name',
|
||||
'email',
|
||||
'password',
|
||||
'options',
|
||||
];
|
||||
|
||||
protected $hidden = [
|
||||
@@ -50,6 +52,8 @@ class User extends Authenticatable implements MustVerifyEmail
|
||||
return [
|
||||
'email_verified_at' => 'datetime',
|
||||
'password' => 'hashed',
|
||||
'admin' => 'boolean',
|
||||
'options' => 'json',
|
||||
];
|
||||
}
|
||||
|
||||
@@ -82,4 +86,16 @@ class User extends Authenticatable implements MustVerifyEmail
|
||||
ELSE COALESCE(market_data.market_value - transactions.cost_basis, 0)
|
||||
END AS gain_dollars');
|
||||
}
|
||||
|
||||
public function getCurrency(): string
|
||||
{
|
||||
return Arr::get($this->options, 'display_currency') ?? config('investbrain.base_currency');
|
||||
}
|
||||
|
||||
public function getLocale(): string
|
||||
{
|
||||
$available_locales = Arr::pluck(config('app.available_locales'), 'locale');
|
||||
|
||||
return Arr::get($this->options, 'locale') ?? request()->getPreferredLanguage($available_locales) ?? config('app.locale');
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user