Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions src/Traits/BalanceOperation.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace HPWebdeveloper\LaravelPayPocket\Traits;

use HPWebdeveloper\LaravelPayPocket\Models\WalletsLog;
use Illuminate\Support\Facades\DB;
use InvalidArgumentException;

trait BalanceOperation
Expand All @@ -22,17 +23,21 @@ public function hasBalance(): bool
*/
public function decrementAndCreateLog(int|float $value, ?string $notes = null): void
{
$this->createLog('dec', $value, $notes);
$this->decrement('balance', $value);
DB::transaction(function () use ($value, $notes) {
$this->createLog('dec', $value, $notes);
$this->decrement('balance', $value);
});
}

/**
* Increment Balance and create a log entry.
*/
public function incrementAndCreateLog(int|float $value, ?string $notes = null): void
{
$this->createLog('inc', $value, $notes);
$this->increment('balance', $value);
DB::transaction(function () use ($value, $notes) {
$this->createLog('inc', $value, $notes);
$this->increment('balance', $value);
});
}

/**
Expand All @@ -44,7 +49,7 @@ protected function createLog(string $logType, int|float $value, ?string $notes =

$newBalance = $logType === 'dec' ? $currentBalance - $value : $currentBalance + $value;

$this->createdLog = $this->logs()->create([
$walletLog = (new WalletsLog())->fill([
'wallet_name' => $this->type->value,
'from' => $currentBalance,
'to' => $newBalance,
Expand All @@ -55,7 +60,9 @@ protected function createLog(string $logType, int|float $value, ?string $notes =
'reference' => $this->generateReference(),
]);

$this->createdLog->changeStatus('Done');
$walletLog->status = 'Done';

$this->createdLog = $this->logs()->save($walletLog);
}

/**
Expand Down