ifnex/04_Laravel/app/Services/WalletService.php
Kazem Alghasi f5917c34c2 feat(logic): integrate discount code application and wallet services
Implement the core logic for applying discount codes during price
calculation and introduce the WalletService. This includes updating
the PriceCalculatorService to validate and apply discounts,
incrementing usage counts, and adding a Filament resource for
managing discount codes via the admin panel.

Additionally, refactor TrackingDataImport to use OnEachRow for better
memory management during large Excel imports.
2026-08-05 19:16:09 +03:30

54 lines
1.9 KiB
PHP

<?php
namespace App\Services;
use App\Models\Wallet;
use App\Models\WalletTransaction;
use Illuminate\Support\Facades\DB;
class WalletService
{
/**
* واریز وجه به کیف پول
*/
public function deposit(Wallet $wallet, float $amount, string $description = null, $transactionable = null): WalletTransaction
{
return DB::transaction(function () use ($wallet, $amount, $description, $transactionable) {
$wallet->increment('balance', $amount);
return $this->createTransaction($wallet, $amount, 'deposit', $description, $transactionable);
});
}
/**
* برداشت وجه از کیف پول
*/
public function withdraw(Wallet $wallet, float $amount, string $description = null, $transactionable = null): WalletTransaction
{
if ($wallet->balance < $amount) {
throw new \Exception('موجودی کیف پول کافی نیست.');
}
return DB::transaction(function () use ($wallet, $amount, $description, $transactionable) {
$wallet->decrement('balance', $amount);
// مبلغ برداشت به صورت منفی ثبت می‌شود
return $this->createTransaction($wallet, -$amount, 'withdraw', $description, $transactionable);
});
}
/**
* ثبت تراکنش در دیتابیس
*/
private function createTransaction(Wallet $wallet, float $amount, string $type, ?string $description, $transactionable): WalletTransaction
{
return WalletTransaction::create([
'wallet_id' => $wallet->id,
'amount' => $amount,
'type' => $type,
'description' => $description,
'transactionable_type' => $transactionable ? get_class($transactionable) : null,
'transactionable_id' => $transactionable?->id,
]);
}
}