Skip to content

Commit fee1578

Browse files
committed
add gift's
1 parent 831cccd commit fee1578

File tree

9 files changed

+248
-19
lines changed

9 files changed

+248
-19
lines changed

changelog.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,27 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## [Unreleased]
88
### Added
9-
- File changelog.md
9+
- File changelog.
10+
- Add `HasGift` trait.
11+
- Added status column to the `transfers` table.
12+
- Added status_last column to the `transfers` table.
13+
14+
### Fixed
15+
- Due to the addition of new functionality "gifts"
16+
there are possible problems that need to be addressed.
17+
Namely, when returning the goods,
18+
the funds would not be returned to
19+
the person who paid for it.
20+
Which would raise a lot of questions.
1021

1122
### Changed
1223
- Composer.json: add new keywords.
24+
- the $gifts argument (Boolean type) is added to
25+
the paid, refund, safeRefund, forceRefund method's.
26+
27+
### Removed
28+
- Removed column `refund` from `transfers` table.
29+
Now it has been replaced by the status column.
1330

1431
## [2.0.1] - 2018-11-21
1532
### Added
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
use Illuminate\Support\Facades\Schema;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Database\Migrations\Migration;
6+
use Illuminate\Support\Facades\DB;
7+
use Bavix\Wallet\Models\Transfer;
8+
9+
class AddStatusTransfersTable extends Migration
10+
{
11+
12+
/**
13+
* @return string
14+
*/
15+
protected function table(): string
16+
{
17+
return (new Transfer())->getTable();
18+
}
19+
20+
/**
21+
* @return void
22+
*/
23+
public function up(): void
24+
{
25+
Schema::table($this->table(), function(Blueprint $table) {
26+
$enums = [
27+
Transfer::STATUS_PAID,
28+
Transfer::STATUS_REFUND,
29+
Transfer::STATUS_GIFT,
30+
];
31+
32+
$table->enum('status', $enums)
33+
->default(Transfer::STATUS_PAID)
34+
->after('to_id');
35+
36+
$table->enum('status_last', $enums)
37+
->nullable()
38+
->after('status');
39+
});
40+
41+
DB::table($this->table())
42+
->where('refund', true)
43+
->update([
44+
'status' => Transfer::STATUS_REFUND,
45+
'status_last' => Transfer::STATUS_PAID,
46+
]);
47+
}
48+
49+
/**
50+
* @return void
51+
*/
52+
public function down(): void
53+
{
54+
DB::table($this->table())
55+
->where('status', Transfer::STATUS_REFUND)
56+
->update(['refund' => true,]);
57+
58+
Schema::table($this->table(), function (Blueprint $table) {
59+
$table->dropColumn(['status', 'status_last']);
60+
});
61+
}
62+
63+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
use Illuminate\Support\Facades\Schema;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Database\Migrations\Migration;
6+
use Illuminate\Database\SQLiteConnection;
7+
use Illuminate\Support\Facades\DB;
8+
use Bavix\Wallet\Models\Transfer;
9+
10+
class DropRefundTransfersTable extends Migration
11+
{
12+
13+
/**
14+
* @return string
15+
*/
16+
protected function table(): string
17+
{
18+
return (new Transfer())->getTable();
19+
}
20+
21+
/**
22+
* @return void
23+
*/
24+
public function up(): void
25+
{
26+
Schema::table($this->table(), function(Blueprint $table) {
27+
if (!(DB::connection() instanceof SQLiteConnection)) {
28+
$table->dropIndex('from_to_refund_ind');
29+
$table->dropIndex('from_refund_ind');
30+
$table->dropIndex('to_refund_ind');
31+
}
32+
33+
$table->dropColumn('refund');
34+
});
35+
}
36+
37+
/**
38+
* @return void
39+
*/
40+
public function down(): void
41+
{
42+
Schema::table($this->table(), function (Blueprint $table) {
43+
$table->boolean('refund')
44+
->after('withdraw_id')
45+
->default(0);
46+
47+
$table->index(['from_type', 'from_id', 'to_type', 'to_id', 'refund'], 'from_to_refund_ind');
48+
$table->index(['from_type', 'from_id', 'refund'], 'from_refund_ind');
49+
$table->index(['to_type', 'to_id', 'refund'], 'to_refund_ind');
50+
});
51+
}
52+
53+
}

src/Interfaces/Customer.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,29 +32,33 @@ public function forcePay(Product $product): Transfer;
3232

3333
/**
3434
* @param Product $product
35+
* @param bool $gifts
3536
* @return null|Transfer
3637
*/
37-
public function paid(Product $product): ?Transfer;
38+
public function paid(Product $product, bool $gifts = false): ?Transfer;
3839

3940
/**
4041
* @param Product $product
4142
* @param bool $force
43+
* @param bool $gifts
4244
* @return bool
4345
* @throws
4446
*/
45-
public function refund(Product $product, bool $force = false): bool;
47+
public function refund(Product $product, bool $force = false, bool $gifts = false): bool;
4648

4749
/**
4850
* @param Product $product
4951
* @param bool $force
52+
* @param bool $gifts
5053
* @return bool
5154
*/
52-
public function safeRefund(Product $product, bool $force = false): bool;
55+
public function safeRefund(Product $product, bool $force = false, bool $gifts = false): bool;
5356

5457
/**
5558
* @param Product $product
59+
* @param bool $gifts
5660
* @return bool
5761
*/
58-
public function forceRefund(Product $product): bool;
62+
public function forceRefund(Product $product, bool $gifts = false): bool;
5963

6064
}

src/Models/Transaction.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
* @property int $amount
1717
* @property bool $confirmed
1818
* @property array $meta
19+
* @property \Bavix\Wallet\Interfaces\Wallet $payable
1920
*/
2021
class Transaction extends Model
2122
{

src/Models/Transfer.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@
1010
* Class Transfer
1111
* @package Bavix\Wallet\Models
1212
*
13+
* @property string $status
1314
* @property int $deposit_id
1415
* @property int $withdraw_id
1516
* @property string $from_type
1617
* @property int $from_id
1718
* @property string $to_type
1819
* @property int $to_id
19-
* @property bool $refund
2020
* @property string $uuid
2121
* @property int $fee
2222
*
@@ -26,17 +26,21 @@
2626
class Transfer extends Model
2727
{
2828

29+
public const STATUS_PAID = 'paid';
30+
public const STATUS_REFUND = 'refund';
31+
public const STATUS_GIFT = 'gift';
32+
2933
/**
3034
* @var array
3135
*/
3236
protected $fillable = [
37+
'status',
3338
'deposit_id',
3439
'withdraw_id',
3540
'from_type',
3641
'from_id',
3742
'to_type',
3843
'to_id',
39-
'refund',
4044
'uuid',
4145
'fee',
4246
];

src/Models/Wallet.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Bavix\Wallet\Interfaces\Customer;
66
use Bavix\Wallet\Interfaces\WalletFloat;
77
use Bavix\Wallet\Traits\CanBePaidFloat;
8+
use Bavix\Wallet\Traits\HasGift;
89
use Bavix\Wallet\WalletProxy;
910
use Illuminate\Database\Eloquent\Model;
1011
use Illuminate\Database\Eloquent\Relations\MorphTo;
@@ -21,6 +22,7 @@ class Wallet extends Model implements Customer, WalletFloat
2122
{
2223

2324
use CanBePaidFloat;
25+
use HasGift;
2426

2527
/**
2628
* @var array

src/Traits/CanBePaid.php

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -72,69 +72,93 @@ public function forcePay(Product $product): Transfer
7272

7373
/**
7474
* @param Product $product
75+
* @param bool $gifts
7576
* @return null|Transfer
7677
*/
77-
public function paid(Product $product): ?Transfer
78+
public function paid(Product $product, bool $gifts = false): ?Transfer
7879
{
80+
$status = [Transfer::STATUS_PAID];
81+
if ($gifts) {
82+
$status[] = Transfer::STATUS_GIFT;
83+
}
84+
7985
/**
8086
* @var Model $product
87+
* @var Transfer $query
8188
*/
82-
return $this->transfers()
89+
$query = $this->transfers();
90+
return $query
8391
->where('to_type', $product->getMorphClass())
8492
->where('to_id', $product->getKey())
85-
->where('refund', 0)
93+
->whereIn('status', $status)
8694
->orderBy('id', 'desc')
8795
->first();
8896
}
8997

9098
/**
9199
* @param Product $product
92100
* @param bool $force
101+
* @param bool $gifts
93102
* @return bool
94103
* @throws
95104
*/
96-
public function refund(Product $product, bool $force = false): bool
105+
public function refund(Product $product, bool $force = false, bool $gifts = false): bool
97106
{
98-
$transfer = $this->paid($product);
107+
$transfer = $this->paid($product, $gifts);
99108

100109
if (!$transfer) {
101110
throw (new ModelNotFoundException())
102111
->setModel($this->transfers()->getMorphClass());
103112
}
104113

105114
return DB::transaction(function () use ($product, $transfer, $force) {
115+
$transfer->load('withdraw.payable');
116+
106117
if ($force) {
107-
$product->forceTransfer($this, $transfer->deposit->amount, $product->getMetaProduct());
118+
$product->forceTransfer(
119+
$transfer->withdraw->payable,
120+
$transfer->deposit->amount,
121+
$product->getMetaProduct()
122+
);
108123
} else {
109-
$product->transfer($this, $transfer->deposit->amount, $product->getMetaProduct());
124+
$product->transfer(
125+
$transfer->withdraw->payable,
126+
$transfer->deposit->amount,
127+
$product->getMetaProduct()
128+
);
110129
}
111130

112-
return $transfer->update(['refund' => 1]);
131+
return $transfer->update([
132+
'status' => Transfer::STATUS_REFUND,
133+
'status_last' => $transfer->status,
134+
]);
113135
});
114136
}
115137

116138
/**
117139
* @param Product $product
118140
* @param bool $force
141+
* @param bool $gifts
119142
* @return bool
120143
*/
121-
public function safeRefund(Product $product, bool $force = false): bool
144+
public function safeRefund(Product $product, bool $force = false, bool $gifts = false): bool
122145
{
123146
try {
124-
return $this->refund($product, $force);
147+
return $this->refund($product, $force, $gifts);
125148
} catch (\Throwable $throwable) {
126149
return false;
127150
}
128151
}
129152

130153
/**
131154
* @param Product $product
155+
* @param bool $gifts
132156
* @return bool
133157
* @throws
134158
*/
135-
public function forceRefund(Product $product): bool
159+
public function forceRefund(Product $product, bool $gifts = false): bool
136160
{
137-
return $this->refund($product, true);
161+
return $this->refund($product, true, $gifts);
138162
}
139163

140164
}

0 commit comments

Comments
 (0)