52 lines
No EOL
1 KiB
PHP
52 lines
No EOL
1 KiB
PHP
<?php
|
|
|
|
namespace App\Models\Transactions;
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
class Purchase extends Model
|
|
{
|
|
use HasFactory;
|
|
|
|
protected $fillable = [
|
|
'date',
|
|
'shares',
|
|
'price_per_share',
|
|
'total_cost',
|
|
];
|
|
|
|
protected $casts = [
|
|
'date' => 'date',
|
|
'shares' => 'decimal:6',
|
|
'price_per_share' => 'decimal:4',
|
|
'total_cost' => 'decimal:2',
|
|
];
|
|
|
|
/**
|
|
* Calculate total shares
|
|
*/
|
|
public static function totalShares(): float
|
|
{
|
|
return static::sum('shares');
|
|
}
|
|
|
|
/**
|
|
* Calculate total investment
|
|
*/
|
|
public static function totalInvestment(): float
|
|
{
|
|
return static::sum('total_cost');
|
|
}
|
|
|
|
/**
|
|
* Get average cost per share
|
|
*/
|
|
public static function averageCostPerShare(): float
|
|
{
|
|
$totalShares = static::totalShares();
|
|
$totalCost = static::totalInvestment();
|
|
|
|
return $totalShares > 0 ? $totalCost / $totalShares : 0;
|
|
}
|
|
} |