'decimal:2', 'new_rate' => 'decimal:2', 'change_percent' => 'decimal:4', ]; public function changedBy(): BelongsTo { return $this->belongsTo(User::class, 'changed_by'); } // Helper برای محاسبه درصد تغییر public static function calculateChangePercent($old, $new): ?float { if (!$old || $old == 0) return null; return round((($new - $old) / $old) * 100, 4); } // Helper برای ثبت تغییر public static function recordChange( string $from, string $to, float $oldRate, float $newRate, string $source = 'manual', ?int $userId = null, ?string $apiSource = null, ?string $notes = null ): self { return self::create([ 'currency_from' => $from, 'currency_to' => $to, 'old_rate' => $oldRate, 'new_rate' => $newRate, 'change_percent' => self::calculateChangePercent($oldRate, $newRate), 'change_source' => $source, 'changed_by' => $userId, 'api_source' => $apiSource, 'notes' => $notes, ]); } // Scope برای آخرین تغییر public function scopeLatest($query, string $from = 'AED', string $to = 'IRR') { return $query->where('currency_from', $from) ->where('currency_to', $to) ->orderBy('created_at', 'desc'); } }