Skip to content

Commit 2d97593

Browse files
author
Babichev Maxim
committed
add $amountFloat to Transaction model
1 parent f57de91 commit 2d97593

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

src/Models/Transaction.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Bavix\Wallet\Interfaces\Wallet;
66
use Bavix\Wallet\Models\Wallet as WalletModel;
7+
use Bavix\Wallet\Services\WalletService;
78
use Illuminate\Database\Eloquent\Model;
89
use Illuminate\Database\Eloquent\Relations\BelongsTo;
910
use Illuminate\Database\Eloquent\Relations\MorphTo;
@@ -18,6 +19,7 @@
1819
* @property string $uuid
1920
* @property string $type
2021
* @property int $amount
22+
* @property float $amountFloat
2123
* @property bool $confirmed
2224
* @property array $meta
2325
* @property Wallet $payable
@@ -94,4 +96,27 @@ public function wallet(): BelongsTo
9496
return $this->belongsTo(config('wallet.wallet.model', WalletModel::class));
9597
}
9698

99+
/**
100+
* @return float
101+
*/
102+
public function getAmountFloatAttribute(): float
103+
{
104+
$decimalPlaces = app(WalletService::class)
105+
->decimalPlaces($this->wallet);
106+
107+
return $this->amount / $decimalPlaces;
108+
}
109+
110+
/**
111+
* @param float $amount
112+
* @return float
113+
*/
114+
public function setAmountFloatAttribute(float $amount): void
115+
{
116+
$decimalPlaces = app(WalletService::class)
117+
->decimalPlaces($this->wallet);
118+
119+
$this->amount = $amount * $decimalPlaces;
120+
}
121+
97122
}

tests/WalletFloatTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,11 +212,19 @@ public function testMantissa(): void
212212

213213
$transaction = $user->withdrawFloat(2556.72);
214214
$this->assertEquals($transaction->amount, -255672);
215+
$this->assertEquals($transaction->amountFloat, -2556.72);
215216
$this->assertEquals($transaction->type, Transaction::TYPE_WITHDRAW);
216217

217218
$this->assertEquals($user->balance, 1000000 - 255672);
218219
$this->assertEquals($user->balanceFloat, 10000.00 - 2556.72);
219220

221+
$transaction = $user->depositFloat(2556.72 * 2);
222+
$this->assertEquals($transaction->amount, 255672 * 2);
223+
$this->assertEquals($transaction->amountFloat, 2556.72 * 2);
224+
$this->assertEquals($transaction->type, Transaction::TYPE_DEPOSIT);
225+
226+
$this->assertEquals($user->balance, 1000000 + 255672);
227+
$this->assertEquals($user->balanceFloat, 10000.00 + 2556.72);
220228
}
221229

222230
/**

0 commit comments

Comments
 (0)