104 lines
2.4 KiB
PHP
104 lines
2.4 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Enums\WalletStatus;
|
|
use App\Models\Traits\HasMinorUnits;
|
|
use App\Models\Traits\HasUuid;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
|
|
/**
|
|
* @property string $uuid
|
|
* @property int $user_id
|
|
* @property string $currency_code
|
|
* @property int $balance_minor BIGINT — never float/decimal
|
|
* @property int $balance_pending_minor BIGINT — holds in-flight amounts
|
|
* @property string $status WalletStatus enum value
|
|
* @property int|null $daily_limit_minor
|
|
* @property int|null $monthly_limit_minor
|
|
*/
|
|
class Wallet extends BaseModel
|
|
{
|
|
use HasFactory;
|
|
use HasUuid;
|
|
use HasMinorUnits;
|
|
|
|
protected $fillable = [
|
|
'user_id',
|
|
'currency_code',
|
|
'balance_minor',
|
|
'balance_pending_minor',
|
|
'status',
|
|
'daily_limit_minor',
|
|
'monthly_limit_minor',
|
|
];
|
|
|
|
protected $casts = [
|
|
'balance_minor' => 'integer',
|
|
'balance_pending_minor' => 'integer',
|
|
'status' => WalletStatus::class,
|
|
'daily_limit_minor' => 'integer',
|
|
'monthly_limit_minor' => 'integer',
|
|
];
|
|
|
|
protected $hidden = [
|
|
'id',
|
|
'user_id',
|
|
'created_at',
|
|
'updated_at',
|
|
'deleted_at',
|
|
];
|
|
|
|
// ── Relationships ──
|
|
|
|
public function user()
|
|
{
|
|
return $this->belongsTo(User::class);
|
|
}
|
|
|
|
public function debitEntries()
|
|
{
|
|
return $this->hasMany(TransactionEntry::class, 'wallet_id')
|
|
->where('entry_type', 'debit');
|
|
}
|
|
|
|
public function creditEntries()
|
|
{
|
|
return $this->hasMany(TransactionEntry::class, 'wallet_id')
|
|
->where('entry_type', 'credit');
|
|
}
|
|
|
|
// ── Helpers ──
|
|
|
|
public function isActive(): bool
|
|
{
|
|
return $this->status === WalletStatus::ACTIVE;
|
|
}
|
|
|
|
public function canReceive(): bool
|
|
{
|
|
return $this->status->canReceive();
|
|
}
|
|
|
|
public function canSend(): bool
|
|
{
|
|
return $this->status->canSend();
|
|
}
|
|
|
|
/**
|
|
* Format balance for API response.
|
|
* e.g., "150,000.00 SYP"
|
|
*/
|
|
public function formattedBalance(): string
|
|
{
|
|
return self::formatMoney($this->balance_minor, $this->currency_code);
|
|
}
|
|
|
|
// ── Scopes ──
|
|
|
|
public function scopeActive($query)
|
|
{
|
|
return $query->where('status', WalletStatus::ACTIVE);
|
|
}
|
|
}
|