60 lines
1.4 KiB
PHP
60 lines
1.4 KiB
PHP
<?php
|
|
|
|
namespace App\Models\Pricing;
|
|
|
|
use Illuminate\Database\Eloquent\Collection;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Support\Carbon;
|
|
|
|
/**
|
|
* @method static latest(string $string)
|
|
* @method static where(string $string, string $string1, string $date)
|
|
* @method static updateOrCreate(string[] $array, float[] $array1)
|
|
* @method static orderBy(string $string, string $string1)
|
|
* @property Carbon $date
|
|
* @property float $price
|
|
*/
|
|
class AssetPrice extends Model
|
|
{
|
|
use HasFactory;
|
|
|
|
protected $fillable = [
|
|
'date',
|
|
'price',
|
|
];
|
|
|
|
protected $casts = [
|
|
'date' => 'date',
|
|
'price' => 'decimal:4',
|
|
];
|
|
|
|
public static function current(): ?float
|
|
{
|
|
$latestPrice = static::latest('date')->first();
|
|
|
|
return $latestPrice ? $latestPrice->price : null;
|
|
}
|
|
|
|
public static function forDate(string $date): ?float
|
|
{
|
|
$price = static::where('date', '<=', $date)
|
|
->orderBy('date', 'desc')
|
|
->first();
|
|
|
|
return $price ? $price->price : null;
|
|
}
|
|
|
|
public static function updatePrice(string $date, float $price): self
|
|
{
|
|
return static::updateOrCreate(
|
|
['date' => $date],
|
|
['price' => $price]
|
|
);
|
|
}
|
|
|
|
public static function history(int $limit = 30): Collection
|
|
{
|
|
return static::orderBy('date', 'desc')->limit($limit)->get();
|
|
}
|
|
}
|