From af3eee0c0a98d23ab5821a456257a714530b71fb Mon Sep 17 00:00:00 2001 From: Babichev Maxim Date: Sat, 29 Jun 2019 19:49:48 +0300 Subject: [PATCH] remove coefficient --- config/config.php | 1 - ...29_184926_decimal_places_wallets_table.php | 41 +++++++++++++++++++ src/Models/Wallet.php | 3 ++ src/Services/WalletService.php | 10 +++++ src/Traits/HasWalletFloat.php | 33 +++++++-------- 5 files changed, 71 insertions(+), 17 deletions(-) create mode 100644 database/migrations_v2/2019_07_29_184926_decimal_places_wallets_table.php diff --git a/config/config.php b/config/config.php index a9cf21ae6..a875e498c 100644 --- a/config/config.php +++ b/config/config.php @@ -2,7 +2,6 @@ return [ 'package' => [ - 'coefficient' => 100., 'rateable' => \Bavix\Wallet\Simple\Rate::class, ], 'services' => [ diff --git a/database/migrations_v2/2019_07_29_184926_decimal_places_wallets_table.php b/database/migrations_v2/2019_07_29_184926_decimal_places_wallets_table.php new file mode 100644 index 000000000..b6ec95292 --- /dev/null +++ b/database/migrations_v2/2019_07_29_184926_decimal_places_wallets_table.php @@ -0,0 +1,41 @@ +getTable(); + } + + /** + * @return void + */ + public function up(): void + { + Schema::table($this->table(), function (Blueprint $table) { + $table->smallInteger('decimal_places') + ->default(2) + ->after('balance'); + }); + } + + /** + * @return void + */ + public function down(): void + { + Schema::table($this->table(), function (Blueprint $table) { + $table->dropColumn('decimal_places'); + }); + } + +} diff --git a/src/Models/Wallet.php b/src/Models/Wallet.php index ef184af59..26ba929b1 100644 --- a/src/Models/Wallet.php +++ b/src/Models/Wallet.php @@ -23,6 +23,7 @@ * @package Bavix\Wallet\Models * @property string $slug * @property int $balance + * @property int $decimal_places * @property \Bavix\Wallet\Interfaces\Wallet $holder */ class Wallet extends Model implements Customer, WalletFloat, Confirmable, Exchangeable @@ -43,6 +44,7 @@ class Wallet extends Model implements Customer, WalletFloat, Confirmable, Exchan 'slug', 'description', 'balance', + 'decimal_places', ]; /** @@ -50,6 +52,7 @@ class Wallet extends Model implements Customer, WalletFloat, Confirmable, Exchan */ protected $casts = [ 'balance' => 'int', + 'decimal_places' => 'int', ]; /** diff --git a/src/Services/WalletService.php b/src/Services/WalletService.php index cd71c092a..459dc3ea2 100644 --- a/src/Services/WalletService.php +++ b/src/Services/WalletService.php @@ -12,6 +12,16 @@ class WalletService { + /** + * @param Wallet $object + * @return int + */ + public function decimalPlaces(Wallet $object): int + { + $decimalPlaces = $this->getWallet($object)->decimal_places ?: 2; + return 10 ** $decimalPlaces; + } + /** * Consider the fee that the system will receive. * diff --git a/src/Traits/HasWalletFloat.php b/src/Traits/HasWalletFloat.php index 46570f4cd..85a39b824 100644 --- a/src/Traits/HasWalletFloat.php +++ b/src/Traits/HasWalletFloat.php @@ -6,6 +6,7 @@ use Bavix\Wallet\Models\Transaction; use Bavix\Wallet\Models\Transfer; use Bavix\Wallet\Services\CommonService; +use Bavix\Wallet\Services\WalletService; use function config; /** @@ -28,15 +29,8 @@ trait HasWalletFloat */ public function forceWithdrawFloat(float $amount, ?array $meta = null, bool $confirmed = true): Transaction { - return $this->forceWithdraw($amount * $this->coefficient(), $meta, $confirmed); - } - - /** - * @return float - */ - private function coefficient(): float - { - return config('wallet.package.coefficient', 100.); + $decimalPlaces = app(WalletService::class)->decimalPlaces($this); + return $this->forceWithdraw($amount * $decimalPlaces, $meta, $confirmed); } /** @@ -48,7 +42,8 @@ private function coefficient(): float */ public function depositFloat(float $amount, ?array $meta = null, bool $confirmed = true): Transaction { - return $this->deposit($amount * $this->coefficient(), $meta, $confirmed); + $decimalPlaces = app(WalletService::class)->decimalPlaces($this); + return $this->deposit($amount * $decimalPlaces, $meta, $confirmed); } /** @@ -60,7 +55,8 @@ public function depositFloat(float $amount, ?array $meta = null, bool $confirmed */ public function withdrawFloat(float $amount, ?array $meta = null, bool $confirmed = true): Transaction { - return $this->withdraw($amount * $this->coefficient(), $meta, $confirmed); + $decimalPlaces = app(WalletService::class)->decimalPlaces($this); + return $this->withdraw($amount * $decimalPlaces, $meta, $confirmed); } /** @@ -69,7 +65,8 @@ public function withdrawFloat(float $amount, ?array $meta = null, bool $confirme */ public function canWithdrawFloat(float $amount): bool { - return $this->canWithdraw($amount * $this->coefficient()); + $decimalPlaces = app(WalletService::class)->decimalPlaces($this); + return $this->canWithdraw($amount * $decimalPlaces); } /** @@ -81,7 +78,8 @@ public function canWithdrawFloat(float $amount): bool */ public function transferFloat(Wallet $wallet, float $amount, ?array $meta = null): Transfer { - return $this->transfer($wallet, $amount * $this->coefficient(), $meta); + $decimalPlaces = app(WalletService::class)->decimalPlaces($this); + return $this->transfer($wallet, $amount * $decimalPlaces, $meta); } /** @@ -92,7 +90,8 @@ public function transferFloat(Wallet $wallet, float $amount, ?array $meta = null */ public function safeTransferFloat(Wallet $wallet, float $amount, ?array $meta = null): ?Transfer { - return $this->safeTransfer($wallet, $amount * $this->coefficient(), $meta); + $decimalPlaces = app(WalletService::class)->decimalPlaces($this); + return $this->safeTransfer($wallet, $amount * $decimalPlaces, $meta); } /** @@ -103,7 +102,8 @@ public function safeTransferFloat(Wallet $wallet, float $amount, ?array $meta = */ public function forceTransferFloat(Wallet $wallet, float $amount, ?array $meta = null): Transfer { - return $this->forceTransfer($wallet, $amount * $this->coefficient(), $meta); + $decimalPlaces = app(WalletService::class)->decimalPlaces($this); + return $this->forceTransfer($wallet, $amount * $decimalPlaces, $meta); } /** @@ -111,7 +111,8 @@ public function forceTransferFloat(Wallet $wallet, float $amount, ?array $meta = */ public function getBalanceFloatAttribute(): float { - return $this->balance / $this->coefficient(); + $decimalPlaces = app(WalletService::class)->decimalPlaces($this); + return $this->balance / $decimalPlaces; } }