Initial commit - WASL Digital Wallet
This commit is contained in:
103
Backend/app/Models/Wallet.php
Normal file
103
Backend/app/Models/Wallet.php
Normal file
@@ -0,0 +1,103 @@
|
||||
<?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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user