Skip to content

Commit 678796d

Browse files
authored
Merge pull request #67 from bavix/decimal_places
Decimal places
2 parents bb9d6e3 + d7a9494 commit 678796d

File tree

5 files changed

+71
-17
lines changed

5 files changed

+71
-17
lines changed

config/config.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
return [
44
'package' => [
5-
'coefficient' => 100.,
65
'rateable' => \Bavix\Wallet\Simple\Rate::class,
76
],
87
'services' => [
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
use Bavix\Wallet\Models\Wallet;
4+
use Illuminate\Database\Migrations\Migration;
5+
use Illuminate\Database\Schema\Blueprint;
6+
use Illuminate\Support\Facades\Schema;
7+
8+
class DecimalPlacesWalletsTable extends Migration
9+
{
10+
11+
/**
12+
* @return string
13+
*/
14+
protected function table(): string
15+
{
16+
return (new Wallet())->getTable();
17+
}
18+
19+
/**
20+
* @return void
21+
*/
22+
public function up(): void
23+
{
24+
Schema::table($this->table(), function (Blueprint $table) {
25+
$table->smallInteger('decimal_places')
26+
->default(2)
27+
->after('balance');
28+
});
29+
}
30+
31+
/**
32+
* @return void
33+
*/
34+
public function down(): void
35+
{
36+
Schema::table($this->table(), function (Blueprint $table) {
37+
$table->dropColumn('decimal_places');
38+
});
39+
}
40+
41+
}

src/Models/Wallet.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
* @package Bavix\Wallet\Models
2424
* @property string $slug
2525
* @property int $balance
26+
* @property int $decimal_places
2627
* @property \Bavix\Wallet\Interfaces\Wallet $holder
2728
*/
2829
class Wallet extends Model implements Customer, WalletFloat, Confirmable, Exchangeable
@@ -43,13 +44,15 @@ class Wallet extends Model implements Customer, WalletFloat, Confirmable, Exchan
4344
'slug',
4445
'description',
4546
'balance',
47+
'decimal_places',
4648
];
4749

4850
/**
4951
* @var array
5052
*/
5153
protected $casts = [
5254
'balance' => 'int',
55+
'decimal_places' => 'int',
5356
];
5457

5558
/**

src/Services/WalletService.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,16 @@
1313
class WalletService
1414
{
1515

16+
/**
17+
* @param Wallet $object
18+
* @return int
19+
*/
20+
public function decimalPlaces(Wallet $object): int
21+
{
22+
$decimalPlaces = $this->getWallet($object)->decimal_places ?: 2;
23+
return 10 ** $decimalPlaces;
24+
}
25+
1626
/**
1727
* Consider the fee that the system will receive.
1828
*

src/Traits/HasWalletFloat.php

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Bavix\Wallet\Models\Transaction;
77
use Bavix\Wallet\Models\Transfer;
88
use Bavix\Wallet\Services\CommonService;
9+
use Bavix\Wallet\Services\WalletService;
910
use function config;
1011

1112
/**
@@ -28,15 +29,8 @@ trait HasWalletFloat
2829
*/
2930
public function forceWithdrawFloat(float $amount, ?array $meta = null, bool $confirmed = true): Transaction
3031
{
31-
return $this->forceWithdraw($amount * $this->coefficient(), $meta, $confirmed);
32-
}
33-
34-
/**
35-
* @return float
36-
*/
37-
private function coefficient(): float
38-
{
39-
return config('wallet.package.coefficient', 100.);
32+
$decimalPlaces = app(WalletService::class)->decimalPlaces($this);
33+
return $this->forceWithdraw($amount * $decimalPlaces, $meta, $confirmed);
4034
}
4135

4236
/**
@@ -48,7 +42,8 @@ private function coefficient(): float
4842
*/
4943
public function depositFloat(float $amount, ?array $meta = null, bool $confirmed = true): Transaction
5044
{
51-
return $this->deposit($amount * $this->coefficient(), $meta, $confirmed);
45+
$decimalPlaces = app(WalletService::class)->decimalPlaces($this);
46+
return $this->deposit($amount * $decimalPlaces, $meta, $confirmed);
5247
}
5348

5449
/**
@@ -60,7 +55,8 @@ public function depositFloat(float $amount, ?array $meta = null, bool $confirmed
6055
*/
6156
public function withdrawFloat(float $amount, ?array $meta = null, bool $confirmed = true): Transaction
6257
{
63-
return $this->withdraw($amount * $this->coefficient(), $meta, $confirmed);
58+
$decimalPlaces = app(WalletService::class)->decimalPlaces($this);
59+
return $this->withdraw($amount * $decimalPlaces, $meta, $confirmed);
6460
}
6561

6662
/**
@@ -69,7 +65,8 @@ public function withdrawFloat(float $amount, ?array $meta = null, bool $confirme
6965
*/
7066
public function canWithdrawFloat(float $amount): bool
7167
{
72-
return $this->canWithdraw($amount * $this->coefficient());
68+
$decimalPlaces = app(WalletService::class)->decimalPlaces($this);
69+
return $this->canWithdraw($amount * $decimalPlaces);
7370
}
7471

7572
/**
@@ -81,7 +78,8 @@ public function canWithdrawFloat(float $amount): bool
8178
*/
8279
public function transferFloat(Wallet $wallet, float $amount, ?array $meta = null): Transfer
8380
{
84-
return $this->transfer($wallet, $amount * $this->coefficient(), $meta);
81+
$decimalPlaces = app(WalletService::class)->decimalPlaces($this);
82+
return $this->transfer($wallet, $amount * $decimalPlaces, $meta);
8583
}
8684

8785
/**
@@ -92,7 +90,8 @@ public function transferFloat(Wallet $wallet, float $amount, ?array $meta = null
9290
*/
9391
public function safeTransferFloat(Wallet $wallet, float $amount, ?array $meta = null): ?Transfer
9492
{
95-
return $this->safeTransfer($wallet, $amount * $this->coefficient(), $meta);
93+
$decimalPlaces = app(WalletService::class)->decimalPlaces($this);
94+
return $this->safeTransfer($wallet, $amount * $decimalPlaces, $meta);
9695
}
9796

9897
/**
@@ -103,15 +102,17 @@ public function safeTransferFloat(Wallet $wallet, float $amount, ?array $meta =
103102
*/
104103
public function forceTransferFloat(Wallet $wallet, float $amount, ?array $meta = null): Transfer
105104
{
106-
return $this->forceTransfer($wallet, $amount * $this->coefficient(), $meta);
105+
$decimalPlaces = app(WalletService::class)->decimalPlaces($this);
106+
return $this->forceTransfer($wallet, $amount * $decimalPlaces, $meta);
107107
}
108108

109109
/**
110110
* @return float
111111
*/
112112
public function getBalanceFloatAttribute(): float
113113
{
114-
return $this->balance / $this->coefficient();
114+
$decimalPlaces = app(WalletService::class)->decimalPlaces($this);
115+
return $this->balance / $decimalPlaces;
115116
}
116117

117118
}

0 commit comments

Comments
 (0)