1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bavix\Wallet\Models; |
4
|
|
|
|
5
|
|
|
use function app; |
6
|
|
|
use function array_key_exists; |
7
|
|
|
use function array_merge; |
8
|
|
|
use Bavix\Wallet\Interfaces\Confirmable; |
9
|
|
|
use Bavix\Wallet\Interfaces\Customer; |
10
|
|
|
use Bavix\Wallet\Interfaces\Exchangeable; |
11
|
|
|
use Bavix\Wallet\Interfaces\WalletFloat; |
12
|
|
|
use Bavix\Wallet\Services\WalletService; |
13
|
|
|
use Bavix\Wallet\Traits\CanConfirm; |
14
|
|
|
use Bavix\Wallet\Traits\CanExchange; |
15
|
|
|
use Bavix\Wallet\Traits\CanPayFloat; |
16
|
|
|
use Bavix\Wallet\Traits\HasGift; |
17
|
|
|
use function config; |
18
|
|
|
use Illuminate\Database\Eloquent\Model; |
19
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphTo; |
20
|
|
|
use Illuminate\Support\Str; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Class Wallet. |
24
|
|
|
* @property string $holder_type |
25
|
|
|
* @property int $holder_id |
26
|
|
|
* @property string $name |
27
|
|
|
* @property string $slug |
28
|
|
|
* @property string $description |
29
|
|
|
* @property array $meta |
30
|
|
|
* @property int $balance |
31
|
|
|
* @property int $decimal_places |
32
|
|
|
* @property \Bavix\Wallet\Interfaces\Wallet $holder |
33
|
|
|
* @property-read string $currency |
34
|
|
|
*/ |
35
|
|
|
class Wallet extends Model implements Customer, WalletFloat, Confirmable, Exchangeable |
36
|
|
|
{ |
37
|
1 |
|
use CanConfirm; |
|
|
|
|
38
|
1 |
|
use CanExchange; |
39
|
1 |
|
use CanPayFloat; |
|
|
|
|
40
|
1 |
|
use HasGift; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var array |
44
|
|
|
*/ |
45
|
|
|
protected $fillable = [ |
46
|
|
|
'holder_type', |
47
|
|
|
'holder_id', |
48
|
|
|
'name', |
49
|
|
|
'slug', |
50
|
|
|
'description', |
51
|
|
|
'meta', |
52
|
|
|
'balance', |
53
|
|
|
'decimal_places', |
54
|
|
|
]; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var array |
58
|
|
|
*/ |
59
|
|
|
protected $casts = [ |
60
|
|
|
'decimal_places' => 'int', |
61
|
|
|
'meta' => 'json', |
62
|
|
|
]; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* {@inheritdoc} |
66
|
|
|
*/ |
67
|
150 |
|
public function getCasts(): array |
68
|
|
|
{ |
69
|
150 |
|
return array_merge( |
70
|
150 |
|
parent::getCasts(), |
71
|
150 |
|
config('wallet.wallet.casts', []) |
72
|
|
|
); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @return string |
77
|
|
|
*/ |
78
|
182 |
|
public function getTable(): string |
79
|
|
|
{ |
80
|
182 |
|
if (! $this->table) { |
81
|
182 |
|
$this->table = config('wallet.wallet.table', 'wallets'); |
82
|
|
|
} |
83
|
|
|
|
84
|
182 |
|
return parent::getTable(); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param string $name |
89
|
|
|
* @return void |
90
|
|
|
*/ |
91
|
150 |
|
public function setNameAttribute(string $name): void |
92
|
|
|
{ |
93
|
150 |
|
$this->attributes['name'] = $name; |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Must be updated only if the model does not exist |
97
|
|
|
* or the slug is empty. |
98
|
|
|
*/ |
99
|
150 |
|
if (! $this->exists && ! array_key_exists('slug', $this->attributes)) { |
100
|
150 |
|
$this->attributes['slug'] = Str::slug($name); |
101
|
|
|
} |
102
|
150 |
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Under ideal conditions, you will never need a method. |
106
|
|
|
* Needed to deal with out-of-sync. |
107
|
|
|
* |
108
|
|
|
* @return bool |
109
|
|
|
*/ |
110
|
31 |
|
public function refreshBalance(): bool |
111
|
|
|
{ |
112
|
31 |
|
return app(WalletService::class)->refresh($this); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* The method adjusts the balance by adding an additional transaction. |
117
|
|
|
* Used wisely, it can lead to serious problems. |
118
|
|
|
* |
119
|
|
|
* @return bool |
120
|
|
|
*/ |
121
|
12 |
|
public function adjustmentBalance(): bool |
122
|
|
|
{ |
123
|
|
|
try { |
124
|
12 |
|
app(WalletService::class)->adjustment($this); |
125
|
|
|
|
126
|
6 |
|
return true; |
127
|
6 |
|
} catch (\Throwable $throwable) { |
128
|
6 |
|
return false; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @return float|int |
134
|
|
|
*/ |
135
|
33 |
|
public function getAvailableBalance() |
136
|
|
|
{ |
137
|
33 |
|
return $this->transactions() |
138
|
33 |
|
->where('wallet_id', $this->getKey()) |
139
|
33 |
|
->where('confirmed', true) |
140
|
33 |
|
->sum('amount'); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @return MorphTo |
145
|
|
|
*/ |
146
|
127 |
|
public function holder(): MorphTo |
147
|
|
|
{ |
148
|
127 |
|
return $this->morphTo(); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @return string |
153
|
|
|
*/ |
154
|
3 |
|
public function getCurrencyAttribute(): string |
155
|
|
|
{ |
156
|
3 |
|
$currencies = config('wallet.currencies', []); |
157
|
|
|
|
158
|
3 |
|
return $currencies[$this->slug] ?? |
159
|
3 |
|
$this->meta['currency'] ?? |
160
|
3 |
|
Str::upper($this->slug); |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|