ifnex/04_Laravel/app/Models/User.php
Kazem Alghasi d6e04d53fa feat(core): integrate Kavenegar SMS notifications and audit logging
Implement a comprehensive notification system using Kavenegar SMS
gateway and enhance system traceability through audit logging and
detailed shipment status history.

- SMS Integration:
  - Add Kavenegar SMS service with configurable API keys and sender
    numbers via system settings.
  - Implement automated SMS notifications for shipment approval,
    rejection, successful payments, and tracking updates.
  - Add administrative UI in Filament to manage SMS gateway settings
    and toggle specific notification types.
- Audit & Tracking:
  - Apply `Auditable` trait to core models (User, Shipment, Wallet,
    etc.) to track changes.
  - Refactor `ShipmentStatusHistory` to include status transitions
    (`from_status` to `to_status`) and specific reasons for changes.
  - Implement `ShipmentObserver` to automate notification triggers
    on status changes.
- Database & Config:
  - Add migrations for enhanced shipment status history tracking.
  - Update `.env.example` and `config/ifnex.php` with Kavenegar
    configuration parameters.
2026-09-10 00:52:18 +03:30

106 lines
3.0 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\DatabaseNotification;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
use Spatie\Permission\Traits\HasRoles;
use App\Traits\Auditable;
class User extends Authenticatable
{
use HasApiTokens, HasFactory, Notifiable, HasRoles, Auditable;
protected $fillable = [
'name',
'email',
'password',
'phone',
'role', // فیلد قدیمی - بعداً حذف می‌کنیم
'credit_limit', // سقف اعتبار
'credit_used', // مبلغ استفاده شده از اعتبار
];
protected $hidden = [
'password',
'remember_token',
];
protected function casts(): array
{
return [
'email_verified_at' => 'datetime',
'password' => 'hashed',
'credit_limit' => 'decimal:2',
'credit_used' => 'decimal:2',
];
}
// ─── Relationships ───────────────────────────────
public function wallet()
{
return $this->hasOne(Wallet::class);
}
public function notifications()
{
return $this->morphMany(DatabaseNotification::class, 'notifiable')->orderByDesc('created_at');
}
// ─── Helper Methods (Role Checks) ────────────────
public function isSuperAdmin(): bool
{
return $this->hasRole('super_admin');
}
public function isAdmin(): bool
{
return $this->hasAnyRole(['super_admin', 'admin']);
}
public function isStaff(): bool
{
return $this->hasRole('staff');
}
public function isCustomer(): bool
{
return $this->hasRole('customer');
}
public function markNotificationRead(string $notificationId): bool
{
$notification = $this->notifications()->where('id', $notificationId)->first();
if ($notification) {
$notification->markAsRead();
return true;
}
return false;
}
// ─── Credit Methods ──────────────────────────────
public function getAvailableCreditAttribute(): float
{
return $this->credit_limit - $this->credit_used;
}
public function hasAvailableCredit(float $amount): bool
{
return $this->available_credit >= $amount;
}
// ─── Filament Panel Access ───────────────────────
public function canAccessPanel(\Filament\Panel $panel): bool
{
// فقط super_admin, admin, staff می‌توانند به پنل ادمین دسترسی داشته باشند
// customer فقط از API استفاده می‌کند
return $this->hasAnyRole(['super_admin', 'admin', 'staff']);
}
}