@php namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\SoftDeletes; class Transaction extends Model { use HasFactory, SoftDeletes; protected $fillable = [ 'reference_number', 'transaction_date', 'type', 'amount', 'fee_amount', 'total_amount', 'exchange_rate', 'source_currency_id', 'target_currency_id', 'source_type', 'source_id', 'destination_type', 'destination_id', 'customer_id', 'branch_id', 'user_id', 'agent_id', 'status', 'notes', 'metadata' ]; protected $casts = [ 'transaction_date' => 'datetime', 'amount' => 'decimal:4', 'fee_amount' => 'decimal:4', 'total_amount' => 'decimal:4', 'exchange_rate' => 'decimal:6', 'metadata' => 'json', ]; /** * العلاقة مع العميل */ public function customer() { return $this->belongsTo(Customer::class); } /** * العلاقة مع الفرع */ public function branch() { return $this->belongsTo(Branch::class); } /** * العلاقة مع المستخدم الذي أجرى المعاملة */ public function user() { return $this->belongsTo(User::class); } /** * العلاقة مع الوكيل */ public function agent() { return $this->belongsTo(Agent::class); } /** * العلاقة مع العملة المصدر */ public function sourceCurrency() { return $this->belongsTo(Currency::class, 'source_currency_id'); } /** * العلاقة مع العملة الهدف */ public function targetCurrency() { return $this->belongsTo(Currency::class, 'target_currency_id'); } /** * العلاقة مع المصدر (متعدد الأنواع) */ public function source() { return $this->morphTo(); } /** * العلاقة مع الوجهة (متعدد الأنواع) */ public function destination() { return $this->morphTo(); } /** * العلاقة مع تفاصيل المعاملة */ public function details() { return $this->hasMany(TransactionDetail::class); } /** * العلاقة مع عمولات الوكلاء */ public function commissions() { return $this->hasMany(AgentCommission::class); } /** * إنشاء رقم مرجعي فريد للمعاملة */ public static function generateReferenceNumber() { $prefix = 'TX'; $date = now()->format('Ymd'); $random = mt_rand(1000, 9999); return $prefix . $date . $random; } /** * الحصول على اسم نوع المعاملة بالعربية */ public function getTypeNameAttribute() { $types = [ 'exchange' => 'تحويل عملات', 'remittance_send' => 'إرسال حوالة', 'remittance_receive' => 'استلام حوالة', 'deposit' => 'إيداع', 'withdraw' => 'سحب', 'transfer' => 'تحويل', 'payment' => 'دفع', 'fee' => 'رسوم', 'other' => 'أخرى', ]; return $types[$this->type] ?? $this->type; } /** * الحصول على اسم حالة المعاملة بالعربية */ public function getStatusNameAttribute() { $statuses = [ 'pending' => 'قيد الانتظار', 'completed' => 'مكتملة', 'cancelled' => 'ملغاة', 'failed' => 'فاشلة', 'refunded' => 'مستردة', ]; return $statuses[$this->status] ?? $this->status; } /** * الحصول على اسم المصدر */ public function getSourceNameAttribute() { if ($this->source_type === 'App\\Models\\Cashbox') { return 'صندوق: ' . ($this->source->name ?? 'غير محدد'); } elseif ($this->source_type === 'App\\Models\\Account') { return 'حساب: ' . ($this->source->name ?? 'غير محدد'); } return 'غير محدد'; } /** * الحصول على اسم الوجهة */ public function getDestinationNameAttribute() { if ($this->destination_type === 'App\\Models\\Cashbox') { return 'صندوق: ' . ($this->destination->name ?? 'غير محدد'); } elseif ($this->destination_type === 'App\\Models\\Account') { return 'حساب: ' . ($this->destination->name ?? 'غير محدد'); } elseif ($this->destination_type === 'App\\Models\\Customer') { return 'عميل: ' . ($this->destination->name ?? 'غير محدد'); } elseif ($this->destination_type === 'App\\Models\\Agent') { return 'وكيل: ' . ($this->destination->name ?? 'غير محدد'); } return 'غير محدد'; } }