ifnex/04_Laravel/app/Models/Wallet.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

113 lines
2.8 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use App\Traits\Auditable;
class Wallet extends Model
{
use SoftDeletes, Auditable;
protected $fillable = [
'user_id',
'balance',
'total_deposited',
'total_withdrawn',
'is_frozen',
'freeze_reason',
'frozen_at',
'frozen_by',
];
protected $casts = [
'balance' => 'decimal:2',
'total_deposited' => 'decimal:2',
'total_withdrawn' => 'decimal:2',
'is_frozen' => 'boolean',
'frozen_at' => 'datetime',
];
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
public function transactions(): HasMany
{
return $this->hasMany(WalletTransaction::class)->orderByDesc('created_at');
}
public function completedTransactions(): HasMany
{
return $this->transactions()->where('status', 'completed');
}
public function pendingTransactions(): HasMany
{
return $this->transactions()->where('status', 'pending');
}
public function activityLogs(): HasMany
{
return $this->hasMany(WalletActivityLog::class)->orderByDesc('created_at');
}
public function frozenByUser(): BelongsTo
{
return $this->belongsTo(User::class, 'frozen_by');
}
// Helper methods
public function isFrozen(): bool
{
return (bool) ($this->is_frozen ?? false);
}
public function hasSufficientBalance(float $amount): bool
{
return $this->balance >= $amount && !$this->is_frozen;
}
public function freeze(string $reason, ?User $admin = null): bool
{
return $this->update([
'is_frozen' => true,
'freeze_reason' => $reason,
'frozen_at' => now(),
'frozen_by' => $admin?->id,
]);
}
public function unfreeze(?User $admin = null): bool
{
return $this->update([
'is_frozen' => false,
'freeze_reason' => null,
'frozen_at' => null,
'frozen_by' => null,
]);
}
public function refreshBalance(): self
{
$balance = $this->completedTransactions()->sum('amount');
$totalDeposited = $this->completedTransactions()
->where('type', 'deposit')
->sum('amount');
$totalWithdrawn = $this->completedTransactions()
->whereIn('type', ['withdrawal', 'order_payment'])
->sum('amount');
$this->update([
'balance' => $balance,
'total_deposited' => $totalDeposited,
'total_withdrawn' => abs($totalWithdrawn),
]);
return $this->fresh();
}
}