56 lines
1.3 KiB
PHP
56 lines
1.3 KiB
PHP
<?php
|
|||
|
|
|
||
|
|
namespace App\Models;
|
||
|
|
|
||
|
|
use Illuminate\Database\Eloquent\Model;
|
||
|
|
|
||
|
|
class RunwareModel extends Model
|
||
|
|
{
|
||
|
|
protected $fillable = [
|
||
|
|
'modality',
|
||
|
|
'air_id',
|
||
|
|
'label',
|
||
|
|
'unit',
|
||
|
|
'price_usd',
|
||
|
|
'is_default',
|
||
|
|
'active',
|
||
|
|
];
|
||
|
|
|
||
|
|
protected function casts(): array
|
||
|
|
{
|
||
|
|
return [
|
||
|
|
'price_usd' => 'decimal:6',
|
||
|
|
'is_default' => 'boolean',
|
||
|
|
'active' => 'boolean',
|
||
|
|
];
|
||
|
|
}
|
||
|
|
|
||
|
|
public static function defaultFor(string $modality): ?self
|
||
|
|
{
|
||
|
|
return static::query()
|
||
|
|
->where('modality', $modality)
|
||
|
|
->where('is_default', true)
|
||
|
|
->where('active', true)
|
||
|
|
->first();
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* The cheapest active model always wins the default flag, since Runware's
|
||
|
|
* own model-search API doesn't expose pricing — this locally-entered
|
||
|
|
* table is the only place "cheapest" can be computed from.
|
||
|
|
*/
|
||
|
|
public static function recomputeDefault(string $modality): void
|
||
|
|
{
|
||
|
|
static::query()->where('modality', $modality)->update(['is_default' => false]);
|
||
|
|
|
||
|
|
$cheapest = static::query()
|
||
|
|
->where('modality', $modality)
|
||
|
|
->where('active', true)
|
||
|
|
->orderBy('price_usd')
|
||
|
|
->orderBy('id')
|
||
|
|
->first();
|
||
|
|
|
||
|
|
$cheapest?->update(['is_default' => true]);
|
||
|
|
}
|
||
|
|
}
|