wip
This commit is contained in:
+18
-10
@@ -14,11 +14,6 @@ class Holding extends Model
|
||||
|
||||
protected $with = ['market_data'];
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $fillable = [
|
||||
'portfolio_id',
|
||||
'symbol',
|
||||
@@ -31,16 +26,29 @@ class Holding extends Model
|
||||
'dividends_synced_at'
|
||||
];
|
||||
|
||||
/**
|
||||
* The attributes that should be cast to native types.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $casts = [
|
||||
'splits_synced_at' => 'datetime',
|
||||
'dividends_synced_at' => 'datetime',
|
||||
];
|
||||
|
||||
protected $appends = [
|
||||
'market_gain_percent'
|
||||
];
|
||||
|
||||
/**
|
||||
* Append the market gain / loss percent attribute
|
||||
*
|
||||
* @return int
|
||||
*
|
||||
*/
|
||||
public function getMarketGainPercentAttribute()
|
||||
{
|
||||
|
||||
return (int) !empty($this->market_data?->market_value) && !empty($this->average_cost_basis)
|
||||
? (($this->market_data->market_value - $this->average_cost_basis) / $this->average_cost_basis) * 100
|
||||
: 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Market data for holding
|
||||
*
|
||||
|
||||
@@ -25,6 +25,9 @@ class MarketData extends Model
|
||||
'market_value',
|
||||
'fifty_two_week_high',
|
||||
'fifty_two_week_low',
|
||||
'forward_pe',
|
||||
'trailing_pe',
|
||||
'market_cap'
|
||||
];
|
||||
|
||||
public static function setSplitsHoldingSynced($symbol)
|
||||
@@ -49,15 +52,17 @@ class MarketData extends Model
|
||||
]);
|
||||
|
||||
// check if new or stale
|
||||
if (!$market_data->exists || now()->diffInMinutes($market_data->updated_at) >= config('market_data.refresh')) {
|
||||
if (
|
||||
!$market_data->exists
|
||||
|| is_null($market_data->updated_at)
|
||||
|| $market_data->updated_at->diffInMinutes(now()) >= config('market_data.refresh')
|
||||
) {
|
||||
|
||||
// get quote
|
||||
// $quote = app(MarketDataInterface::class)->quote($symbol);
|
||||
$quote = app(MarketDataInterface::class)->quote($symbol);
|
||||
|
||||
// fill data
|
||||
// $market_data->fill($quote->toArray());
|
||||
|
||||
|
||||
$market_data->fill($quote->toArray());
|
||||
}
|
||||
|
||||
// save with timestamps updated
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
namespace App\Models;
|
||||
|
||||
use App\Models\MarketData;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Casts\Attribute;
|
||||
use Illuminate\Database\Eloquent\Concerns\HasUuids;
|
||||
@@ -143,6 +144,17 @@ class Transaction extends Model
|
||||
* @return void
|
||||
*/
|
||||
public function syncHolding() {
|
||||
|
||||
// sync previous symbol too
|
||||
if (Arr::has($this->changes, 'symbol')) {
|
||||
|
||||
$temp = new Transaction;
|
||||
$temp->symbol = $this->original['symbol'];
|
||||
$temp->portfolio_id = $this->portfolio_id;
|
||||
|
||||
$temp->syncHolding();
|
||||
}
|
||||
|
||||
// get the holding for a symbol and portfolio (or create one)
|
||||
$holding = Holding::firstOrNew([
|
||||
'portfolio_id' => $this->portfolio_id,
|
||||
|
||||
Reference in New Issue
Block a user