2025-07-10 17:37:30 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Models\Pricing;
|
|
|
|
|
|
|
|
|
|
use Illuminate\Database\Eloquent\Collection;
|
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2025-08-01 00:36:05 +02:00
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
2025-07-10 17:37:30 +02:00
|
|
|
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 = [
|
2025-08-01 00:36:05 +02:00
|
|
|
'asset_id',
|
2025-07-10 17:37:30 +02:00
|
|
|
'date',
|
|
|
|
|
'price',
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
protected $casts = [
|
|
|
|
|
'date' => 'date',
|
|
|
|
|
'price' => 'decimal:4',
|
|
|
|
|
];
|
|
|
|
|
|
2025-08-01 00:36:05 +02:00
|
|
|
public function asset(): BelongsTo
|
2025-07-10 17:37:30 +02:00
|
|
|
{
|
2025-08-01 00:36:05 +02:00
|
|
|
return $this->belongsTo(\App\Models\Asset::class);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function current(int $assetId = null): ?float
|
|
|
|
|
{
|
|
|
|
|
$query = static::latest('date');
|
|
|
|
|
|
|
|
|
|
if ($assetId) {
|
|
|
|
|
$query->where('asset_id', $assetId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$latestPrice = $query->first();
|
2025-07-10 17:37:30 +02:00
|
|
|
|
|
|
|
|
return $latestPrice ? $latestPrice->price : null;
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-01 00:36:05 +02:00
|
|
|
public static function forDate(string $date, int $assetId = null): ?float
|
2025-07-10 17:37:30 +02:00
|
|
|
{
|
2025-08-01 00:36:05 +02:00
|
|
|
$query = static::where('date', '<=', $date)
|
|
|
|
|
->orderBy('date', 'desc');
|
|
|
|
|
|
|
|
|
|
if ($assetId) {
|
|
|
|
|
$query->where('asset_id', $assetId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$price = $query->first();
|
2025-07-10 17:37:30 +02:00
|
|
|
|
|
|
|
|
return $price ? $price->price : null;
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-01 00:36:05 +02:00
|
|
|
public static function updatePrice(int $assetId, string $date, float $price): self
|
2025-07-10 17:37:30 +02:00
|
|
|
{
|
|
|
|
|
return static::updateOrCreate(
|
2025-08-01 00:36:05 +02:00
|
|
|
['asset_id' => $assetId, 'date' => $date],
|
2025-07-10 17:37:30 +02:00
|
|
|
['price' => $price]
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-01 00:36:05 +02:00
|
|
|
public static function history(int $assetId = null, int $limit = 30): Collection
|
2025-07-10 17:37:30 +02:00
|
|
|
{
|
2025-08-01 00:36:05 +02:00
|
|
|
$query = static::orderBy('date', 'desc')->limit($limit);
|
|
|
|
|
|
|
|
|
|
if ($assetId) {
|
|
|
|
|
$query->where('asset_id', $assetId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $query->get();
|
2025-07-10 17:37:30 +02:00
|
|
|
}
|
|
|
|
|
}
|