Skip to content

Commit

Permalink
Merge pull request bavix#159 from bavix/develop
Browse files Browse the repository at this point in the history
Postgres 12
  • Loading branch information
rez1dent3 authored Mar 26, 2020
2 parents 40d7f46 + 7baaaa1 commit 7a5fabe
Show file tree
Hide file tree
Showing 15 changed files with 100 additions and 53 deletions.
10 changes: 9 additions & 1 deletion changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [5.1.0] - 2020-03-26
### Added
- Added support `ramsey/uuid ^4.0`

### Fixed
- pg12 support

## [5.0.2] - 2020-03-22
### Fixed
- fix `bindTo` method (v4.1)
Expand Down Expand Up @@ -493,7 +500,8 @@ The operation is now executed in the transaction and updates the new `refund` fi
- Exceptions: AmountInvalid, BalanceIsEmpty.
- Models: Transfer, Transaction.

[Unreleased]: https://github.com/bavix/laravel-wallet/compare/5.0.2...develop
[Unreleased]: https://github.com/bavix/laravel-wallet/compare/5.1.0...develop
[5.1.0]: https://github.com/bavix/laravel-wallet/compare/5.0.2...5.1.0
[5.0.2]: https://github.com/bavix/laravel-wallet/compare/5.0.1...5.0.2
[5.0.1]: https://github.com/bavix/laravel-wallet/compare/5.0.0...5.0.1
[5.0.0]: https://github.com/bavix/laravel-wallet/compare/4.2.2...5.0.0
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"ext-pdo": "*",
"illuminate/database": "^6.0|^7.0",
"doctrine/dbal": "^2.8",
"ramsey/uuid": "^3.0"
"ramsey/uuid": "^3.0|^4.0"
},
"require-dev": {
"infection/infection": "^0.15",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function up(): void
$table->bigIncrements('id');
$table->morphs('payable');
$table->enum('type', ['deposit', 'withdraw'])->index();
$table->bigInteger('amount');
$table->decimal('amount', 64, 0);
$table->boolean('confirmed');
$this->json($table, 'meta')->nullable();
$table->uuid('uuid')->unique();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function up(): void
$table->string('name');
$table->string('slug')->index();
$table->string('description')->nullable();
$table->bigInteger('balance')->default(0);
$table->decimal('balance', 64, 0)->default(0);
$table->timestamps();

$table->unique(['holder_type', 'holder_id', 'slug']);
Expand All @@ -41,14 +41,15 @@ public function up(): void
*/
$default = config('wallet.wallet.default.name', 'Default Wallet');
$slug = config('wallet.wallet.default.slug', 'default');
$now = time();
$query = Transaction::query()->distinct()
->selectRaw('payable_type as holder_type')
->selectRaw('payable_id as holder_id')
->selectRaw('? as name', [$default])
->selectRaw('? as slug', [$slug])
->selectRaw('sum(amount) as balance')
->selectRaw('? as created_at', [DB::raw('now()')])
->selectRaw('? as updated_at', [DB::raw('now()')])
->selectRaw('? as created_at', [$now])
->selectRaw('? as updated_at', [$now])
->groupBy('holder_type', 'holder_id')
->orderBy('holder_type');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ protected function table(): string
public function up(): void
{
Schema::table($this->table(), function (Blueprint $table) {
$table->bigInteger('fee')
$table->decimal('fee', 64, 0)
->default(0)
->after('withdraw_id');
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ protected function table(): string
public function up(): void
{
Schema::table($this->table(), function (Blueprint $table) {
$table->bigInteger('discount')
$table->decimal('discount', 64, 0)
->default(0)
->after('withdraw_id');
});
Expand Down
14 changes: 6 additions & 8 deletions src/Commands/RefreshBalance.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Bavix\Wallet\Models\Wallet;
use Bavix\Wallet\Services\DbService;
use Illuminate\Console\Command;
use Illuminate\Database\PostgresConnection;
use Illuminate\Database\Query\JoinClause;
use Illuminate\Database\SQLiteConnection;
use function config;
Expand Down Expand Up @@ -38,14 +39,11 @@ class RefreshBalance extends Command
public function handle(): void
{
app(DbService::class)->transaction(function () {
if (app(DbService::class)->connection() instanceof SQLiteConnection) {
$connection = app(DbService::class)->connection();
if ($connection instanceof SQLiteConnection || $connection instanceof PostgresConnection) {
$wallet = config('wallet.wallet.table', 'wallets');
app(DbService::class)
->connection()
->table($wallet)
->update(['balance' => 0]);

$this->sqliteUpdate();
$connection->table($wallet)->update(['balance' => 0]);
$this->singleUpdate();
} else {
$this->multiUpdate();
}
Expand All @@ -57,7 +55,7 @@ public function handle(): void
*
* @return void
*/
protected function sqliteUpdate(): void
protected function singleUpdate(): void
{
Wallet::query()->each(static function (Wallet $wallet) {
$wallet->refreshBalance();
Expand Down
18 changes: 10 additions & 8 deletions src/Objects/Bring.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Bavix\Wallet\Models\Transaction;
use Bavix\Wallet\Models\Transfer;
use Ramsey\Uuid\Uuid;
use function abs;

class Bring
{
Expand Down Expand Up @@ -85,7 +84,7 @@ public function setStatus(string $status): self
*/
public function setDiscount(int $discount): self
{
$this->discount = $discount;
$this->discount = app(Mathable::class)->round($discount);
return $this;
}

Expand Down Expand Up @@ -182,14 +181,17 @@ public function getDiscount(): int
*/
public function getFee(): int
{
if ($this->fee === null) {
return app(Mathable::class)->sub(
app(Mathable::class)->abs($this->getWithdraw()->amount),
app(Mathable::class)->abs($this->getDeposit()->amount)
$fee = $this->fee;
if ($fee === null) {
$fee = app(Mathable::class)->round(
app(Mathable::class)->sub(
app(Mathable::class)->abs($this->getWithdraw()->amount),
app(Mathable::class)->abs($this->getDeposit()->amount)
)
);
}

return $this->fee;
return $fee;
}

/**
Expand All @@ -198,7 +200,7 @@ public function getFee(): int
*/
public function setFee($fee): self
{
$this->fee = $fee;
$this->fee = app(Mathable::class)->round($fee);
return $this;
}

Expand Down
3 changes: 2 additions & 1 deletion src/Objects/Operation.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Bavix\Wallet\Objects;

use Bavix\Wallet\Interfaces\Mathable;
use Bavix\Wallet\Interfaces\Wallet;
use Bavix\Wallet\Models\Transaction;
use Ramsey\Uuid\Uuid;
Expand Down Expand Up @@ -104,7 +105,7 @@ public function setType(string $type): self
*/
public function setAmount($amount): self
{
$this->amount = $amount;
$this->amount = app(Mathable::class)->round($amount);
return $this;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Services/CommonService.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ public function verifyWithdraw(Wallet $wallet, $amount, bool $allowZero = null):
* Create Operation without DB::transaction
*
* @param Wallet $self
* @param array $operations
* @param Operation[] $operations
* @return array
*/
public function multiOperation(Wallet $self, array $operations): array
Expand Down
14 changes: 12 additions & 2 deletions src/Simple/Store.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function getBalance($object)
$balance = method_exists($wallet, 'getRawOriginal') ?
$wallet->getRawOriginal('balance', 0) : $wallet->getOriginal('balance', 0);

$this->balanceSheets[$wallet->getKey()] = app(Mathable::class)->round($balance);
$this->balanceSheets[$wallet->getKey()] = $this->toInt($balance);
}

return $this->balanceSheets[$wallet->getKey()];
Expand All @@ -37,6 +37,7 @@ public function incBalance($object, $amount)
{
$math = app(Mathable::class);
$balance = $math->add($this->getBalance($object), $amount);
$balance = $this->toInt($balance);
$this->setBalance($object, $balance);
return $balance;
}
Expand All @@ -47,8 +48,17 @@ public function incBalance($object, $amount)
public function setBalance($object, $amount): bool
{
$wallet = app(WalletService::class)->getWallet($object);
$this->balanceSheets[$wallet->getKey()] = app(Mathable::class)->round($amount ?: 0);
$this->balanceSheets[$wallet->getKey()] = $this->toInt($amount);
return true;
}

/**
* @param string $balance
* @return string
*/
protected function toInt($balance): string
{
return app(Mathable::class)->round($balance ?: 0);
}

}
6 changes: 4 additions & 2 deletions src/Traits/HasWallet.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,14 +145,16 @@ public function withdraw($amount, ?array $meta = null, bool $confirmed = true):
*/
public function canWithdraw($amount, bool $allowZero = null): bool
{
$math = app(Mathable::class);

/**
* Allow to buy for free with a negative balance
*/
if ($allowZero && $amount === 0) {
if ($allowZero && !$math->compare($amount, 0)) {
return true;
}

return app(Mathable::class)->compare($this->balance, $amount) >= 0;
return $math->compare($this->balance, $amount) >= 0;
}

/**
Expand Down
12 changes: 10 additions & 2 deletions tests/BalanceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Bavix\Wallet\Simple\Store;
use Bavix\Wallet\Test\Models\Buyer;
use Bavix\Wallet\Test\Models\UserMulti;
use Illuminate\Database\SQLiteConnection;
use Illuminate\Support\Facades\DB;
use PDOException;
use PHPUnit\Framework\MockObject\MockObject;
Expand Down Expand Up @@ -96,16 +97,23 @@ public function testSimple(): void

$this->assertEquals($wallet->balance, 1000);

$key = $wallet->getKey();
$this->assertTrue($wallet->delete());
$this->assertFalse($wallet->exists);
$this->assertEquals($wallet->getKey(), $key);
$result = app(CommonService::class)->addBalance($wallet, 100);
$this->assertTrue($result); // automatic create default wallet

$wallet->refreshBalance();
$this->assertEquals($wallet->balance, 1000);
$balance = 0;
if ($wallet->getConnection() instanceof SQLiteConnection) {
$balance = 1000;
}

$this->assertEquals($wallet->balance, $balance);

$wallet->deposit(1);
$this->assertEquals($wallet->balance, 1001);
$this->assertEquals($wallet->balance, $balance + 1);
}

/**
Expand Down
27 changes: 16 additions & 11 deletions tests/MultiWalletTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@
use Bavix\Wallet\Exceptions\BalanceIsEmpty;
use Bavix\Wallet\Models\Transaction;
use Bavix\Wallet\Models\Transfer;
use Bavix\Wallet\Services\DbService;
use Bavix\Wallet\Test\Models\Item;
use Bavix\Wallet\Test\Models\UserCashier;
use Bavix\Wallet\Test\Models\UserMulti;
use Doctrine\DBAL\Driver\PDOException;
use Illuminate\Database\PostgresConnection;
use Illuminate\Database\QueryException;
use function compact;
use function range;
Expand Down Expand Up @@ -336,20 +339,22 @@ public function testConfirmed(): void
*/
public function testWalletUnique(): void
{
$this->expectException(QueryException::class);
if (!(app(DbService::class)->connection() instanceof PostgresConnection)) {
$this->expectException(QueryException::class);

/**
* @var UserMulti $user
*/
$user = factory(UserMulti::class)->create();
/**
* @var UserMulti $user
*/
$user = factory(UserMulti::class)->create();

$user->createWallet([
'name' => 'deposit'
]);
$user->createWallet([
'name' => 'deposit'
]);

$user->createWallet([
'name' => 'deposit'
]);
$user->createWallet([
'name' => 'deposit'
]);
}
}

/**
Expand Down
Loading

0 comments on commit 7a5fabe

Please sign in to comment.