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
|
|
|
use CanConfirm; |
|
|
|
|
38
|
|
|
use CanExchange; |
39
|
|
|
use CanPayFloat; |
|
|
|
|
40
|
|
|
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
|
132 |
|
/** |
65
|
|
|
* {@inheritdoc} |
66
|
132 |
|
*/ |
67
|
132 |
|
public function getCasts(): array |
68
|
132 |
|
{ |
69
|
|
|
return array_merge( |
70
|
|
|
parent::getCasts(), |
71
|
|
|
config('wallet.wallet.casts', []) |
72
|
|
|
); |
73
|
|
|
} |
74
|
|
|
|
75
|
180 |
|
/** |
76
|
|
|
* @return string |
77
|
180 |
|
*/ |
78
|
180 |
|
public function getTable(): string |
79
|
|
|
{ |
80
|
|
|
if (! $this->table) { |
81
|
180 |
|
$this->table = config('wallet.wallet.table', 'wallets'); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return parent::getTable(); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
132 |
|
* @param string $name |
89
|
|
|
* @return void |
90
|
132 |
|
*/ |
91
|
|
|
public function setNameAttribute(string $name): void |
92
|
|
|
{ |
93
|
|
|
$this->attributes['name'] = $name; |
94
|
|
|
|
95
|
|
|
/** |
96
|
132 |
|
* Must be updated only if the model does not exist |
97
|
132 |
|
* or the slug is empty. |
98
|
|
|
*/ |
99
|
132 |
|
if (! $this->exists && ! array_key_exists('slug', $this->attributes)) { |
100
|
|
|
$this->attributes['slug'] = Str::slug($name); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
27 |
|
/** |
105
|
|
|
* Under ideal conditions, you will never need a method. |
106
|
27 |
|
* Needed to deal with out-of-sync. |
107
|
|
|
* |
108
|
|
|
* @return bool |
109
|
|
|
*/ |
110
|
|
|
public function refreshBalance(): bool |
111
|
|
|
{ |
112
|
27 |
|
return app(WalletService::class)->refresh($this); |
113
|
|
|
} |
114
|
27 |
|
|
115
|
27 |
|
/** |
116
|
27 |
|
* The method adjusts the balance by adding an additional transaction. |
117
|
27 |
|
* Used wisely, it can lead to serious problems. |
118
|
|
|
* |
119
|
|
|
* @return bool |
120
|
|
|
*/ |
121
|
|
|
public function adjustmentBalance(): bool |
122
|
|
|
{ |
123
|
110 |
|
try { |
124
|
|
|
app(WalletService::class)->adjustment($this); |
125
|
110 |
|
return true; |
126
|
|
|
} catch (\Throwable $throwable) { |
127
|
|
|
return false; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
3 |
|
/** |
132
|
|
|
* @return float|int |
133
|
3 |
|
*/ |
134
|
|
|
public function getAvailableBalance() |
135
|
3 |
|
{ |
136
|
|
|
return $this->transactions() |
137
|
|
|
->where('wallet_id', $this->getKey()) |
138
|
|
|
->where('confirmed', true) |
139
|
|
|
->sum('amount'); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @return MorphTo |
144
|
|
|
*/ |
145
|
|
|
public function holder(): MorphTo |
146
|
|
|
{ |
147
|
|
|
return $this->morphTo(); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @return string |
152
|
|
|
*/ |
153
|
|
|
public function getCurrencyAttribute(): string |
154
|
|
|
{ |
155
|
|
|
$currencies = config('wallet.currencies', []); |
156
|
|
|
|
157
|
|
|
return $currencies[$this->slug] ?? |
158
|
|
|
$this->meta['currency'] ?? |
159
|
|
|
Str::upper($this->slug); |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|