- rate approval: API creates pending proposals, manager approves - customer_credits + credit_settlements tables - CustomerCreditService (grant/settle via wallet) - rebuilt CustomerCreditResource - CustomerFinancialController: real foreign-currency debt - WP login redirect + custom login page wiring
114 lines
3.2 KiB
PHP
114 lines
3.2 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 credits()
|
|
{
|
|
return $this->hasMany(CustomerCredit::class)->orderBy('created_at', 'desc');
|
|
}
|
|
|
|
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']);
|
|
}
|
|
} |