<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class ExchangeRate extends Model { public $timestamps=false; protected $fillable=['from_currency','to_currency','rate','margin_percent','fee_flat_minor','fee_percent','effective_from','created_by','created_at']; protected function casts():array{return ['rate'=>'decimal:8','margin_percent'=>'decimal:3','fee_flat_minor'=>'integer','fee_percent'=>'decimal:3','effective_from'=>'datetime','created_at'=>'datetime'];} public function creator():BelongsTo{return $this->belongsTo(User::class,'created_by');} protected static function booted():void{static::updating(fn()=>throw new \LogicException('Exchange rates are append-only.'));static::deleting(fn()=>throw new \LogicException('Exchange rates are append-only.'));} public function scopeActiveFor($q,string $from,string $to){return $q->where('from_currency',$from)->where('to_currency',$to)->where('effective_from','<=',now())->orderByDesc('effective_from')->orderByDesc('id');} }
