Skip to content
Merged
Show file tree
Hide file tree
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
1 change: 0 additions & 1 deletion config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

return [
'package' => [
'coefficient' => 100.,
'rateable' => \Bavix\Wallet\Simple\Rate::class,
],
'services' => [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

use Bavix\Wallet\Models\Wallet;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class DecimalPlacesWalletsTable extends Migration
{

/**
* @return string
*/
protected function table(): string
{
return (new Wallet())->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');
});
}

}
3 changes: 3 additions & 0 deletions src/Models/Wallet.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -43,13 +44,15 @@ class Wallet extends Model implements Customer, WalletFloat, Confirmable, Exchan
'slug',
'description',
'balance',
'decimal_places',
];

/**
* @var array
*/
protected $casts = [
'balance' => 'int',
'decimal_places' => 'int',
];

/**
Expand Down
10 changes: 10 additions & 0 deletions src/Services/WalletService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
33 changes: 17 additions & 16 deletions src/Traits/HasWalletFloat.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -103,15 +102,17 @@ 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);
}

/**
* @return float
*/
public function getBalanceFloatAttribute(): float
{
return $this->balance / $this->coefficient();
$decimalPlaces = app(WalletService::class)->decimalPlaces($this);
return $this->balance / $decimalPlaces;
}

}