Skip to content

Commit fb432a0

Browse files
author
Babichev Maxim
committed
add paid
1 parent d38f7ef commit fb432a0

File tree

6 files changed

+54
-20
lines changed

6 files changed

+54
-20
lines changed

database/migrations/2018_11_06_222923_create_transactions_table.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ public function up(): void
2424
Schema::create($this->table(), function(Blueprint $table) {
2525
$table->increments('id');
2626
$table->morphs('payable');
27-
$table->enum('type', ['deposit', 'withdraw']);
27+
$table->enum('type', ['deposit', 'withdraw'])->index();
2828
$table->bigInteger('amount');
2929
$table->boolean('confirmed');
3030
$table->json('meta')->nullable();
31-
$table->uuid('uuid')->index();
31+
$table->uuid('uuid')->unique();
3232
$table->timestamps();
3333

3434
$table->index(['payable_type', 'payable_id', 'type']);

database/migrations/2018_11_07_192923_create_transfers_table.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function up(): void
3636
$table->morphs('to');
3737
$table->unsignedInteger('deposit_id');
3838
$table->unsignedInteger('withdraw_id');
39-
$table->uuid('uuid')->index();
39+
$table->uuid('uuid')->unique();
4040
$table->timestamps();
4141

4242
$table->foreign('deposit_id')

database/migrations/2018_11_07_202152_update_transfers_table.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ public function up(): void
2525
$table->boolean('refund')
2626
->after('withdraw_id')
2727
->default(0);
28+
29+
$table->index(['from_type', 'from_id', 'refund']);
30+
$table->index(['to_type', 'to_id', 'refund']);
2831
});
2932
}
3033

src/Interfaces/Customer.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ public function pay(Product $product): Transfer;
2121
*/
2222
public function safePay(Product $product): ?Transfer;
2323

24+
/**
25+
* @param Product $product
26+
* @return null|Transfer
27+
*/
28+
public function paid(Product $product): ?Transfer;
29+
2430
/**
2531
* @param Product $product
2632
* @return bool

src/Traits/CanBePaid.php

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
namespace Bavix\Wallet\Traits;
44

55
use Bavix\Wallet\Interfaces\Product;
6-
use Bavix\Wallet\Interfaces\Wallet;
76
use Bavix\Wallet\Models\Transfer;
87
use Illuminate\Database\Eloquent\Model;
8+
use Illuminate\Database\Eloquent\ModelNotFoundException;
9+
use Illuminate\Database\Eloquent\Relations\MorphMany;
910
use Illuminate\Support\Facades\DB;
1011

1112
trait CanBePaid
@@ -38,20 +39,34 @@ public function safePay(Product $product): ?Transfer
3839

3940
/**
4041
* @param Product $product
41-
* @return bool
42-
* @throws
42+
* @return null|Transfer
4343
*/
44-
public function refund(Product $product): bool
44+
public function paid(Product $product): ?Transfer
4545
{
4646
/**
4747
* @var Model $product
4848
*/
49-
$transfer = $this->transfers()
49+
return $this->transfers()
5050
->where('to_type', $product->getMorphClass())
5151
->where('to_id', $product->getKey())
5252
->where('refund', 0)
5353
->orderBy('id', 'desc')
5454
->firstOrFail();
55+
}
56+
57+
/**
58+
* @param Product $product
59+
* @return bool
60+
* @throws
61+
*/
62+
public function refund(Product $product): bool
63+
{
64+
$transfer = $this->paid($product);
65+
66+
if (!$transfer) {
67+
throw (new ModelNotFoundException())
68+
->setModel($this->transfers()->getMorphClass());
69+
}
5570

5671
return DB::transaction(function () use ($product, $transfer) {
5772
$product->transfer($this, $product->getAmountProduct(), $product->getMetaProduct());

src/Traits/HasWallet.php

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Bavix\Wallet\Models\Transfer;
1010
use Illuminate\Database\Eloquent\Model;
1111
use Illuminate\Database\Eloquent\Relations\MorphMany;
12+
use Illuminate\Support\Collection;
1213
use Illuminate\Support\Facades\DB;
1314
use Illuminate\Support\Str;
1415

@@ -195,25 +196,34 @@ public function transfers(): MorphMany
195196
}
196197

197198
/**
198-
* @return int
199+
* @return MorphMany
199200
*/
200-
public function getBalanceAttribute(): int
201+
public function balance(): MorphMany
201202
{
202-
if (!$this->cachedBalance) {
203-
$this->cachedBalance = $this->transactions()
204-
->where('confirmed', true)
205-
->sum('amount');
206-
}
207-
208-
return $this->cachedBalance;
203+
return $this->transactions()
204+
->selectRaw('payable_id, sum(amount) as total')
205+
->groupBy('payable_id');
209206
}
210207

211208
/**
212-
* @return void
209+
* @return int
213210
*/
214-
public function resetBalance(): void
211+
public function getBalanceAttribute(): int
215212
{
216-
$this->cachedBalance = null;
213+
if (null === $this->cachedBalance) {
214+
if (!\array_key_exists('balance', $this->relations)) {
215+
$this->load('balance');
216+
}
217+
218+
/**
219+
* @var Collection $collection
220+
*/
221+
$collection = $this->getRelation('balance');
222+
$relation = $collection->first();
223+
$this->cachedBalance = (int)($relation->total ?? 0);
224+
}
225+
226+
return $this->cachedBalance;
217227
}
218228

219229
}

0 commit comments

Comments
 (0)