1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bavix\Wallet\Models; |
4
|
|
|
|
5
|
|
|
use Bavix\Wallet\Interfaces\Confirmable; |
6
|
|
|
use Bavix\Wallet\Interfaces\Customer; |
7
|
|
|
use Bavix\Wallet\Interfaces\Exchangeable; |
8
|
|
|
use Bavix\Wallet\Interfaces\WalletFloat; |
9
|
|
|
use Bavix\Wallet\Services\WalletService; |
10
|
|
|
use Bavix\Wallet\Traits\CanConfirm; |
11
|
|
|
use Bavix\Wallet\Traits\CanExchange; |
12
|
|
|
use Bavix\Wallet\Traits\CanPayFloat; |
13
|
|
|
use Bavix\Wallet\Traits\HasGift; |
14
|
|
|
use Illuminate\Database\Eloquent\Model; |
15
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphTo; |
16
|
|
|
use Illuminate\Support\Str; |
17
|
|
|
use function app; |
18
|
|
|
use function array_key_exists; |
19
|
|
|
use function config; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Class Wallet |
23
|
|
|
* @package Bavix\Wallet\Models |
24
|
|
|
* @property string $holder_type |
25
|
|
|
* @property int $holder_id |
26
|
|
|
* @property string $slug |
27
|
|
|
* @property int $balance |
28
|
|
|
* @property int $decimal_places |
29
|
|
|
* @property \Bavix\Wallet\Interfaces\Wallet $holder |
30
|
|
|
* @property-read string $currency |
31
|
|
|
*/ |
32
|
|
|
class Wallet extends Model implements Customer, WalletFloat, Confirmable, Exchangeable |
33
|
|
|
{ |
34
|
|
|
|
35
|
|
|
use CanConfirm; |
|
|
|
|
36
|
|
|
use CanExchange; |
37
|
|
|
use CanPayFloat; |
|
|
|
|
38
|
|
|
use HasGift; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var array |
42
|
|
|
*/ |
43
|
|
|
protected $fillable = [ |
44
|
|
|
'holder_type', |
45
|
|
|
'holder_id', |
46
|
|
|
'name', |
47
|
|
|
'slug', |
48
|
|
|
'description', |
49
|
|
|
'balance', |
50
|
|
|
'decimal_places', |
51
|
|
|
]; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var array |
55
|
|
|
*/ |
56
|
|
|
protected $casts = [ |
57
|
|
|
'balance' => 'int', |
58
|
|
|
'decimal_places' => 'int', |
59
|
|
|
]; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @inheritDoc |
63
|
|
|
*/ |
64
|
114 |
|
public function getCasts(): array |
65
|
|
|
{ |
66
|
114 |
|
$this->casts = array_merge( |
67
|
114 |
|
$this->casts, |
68
|
114 |
|
config('wallet.wallet.casts', []) |
69
|
|
|
); |
70
|
|
|
|
71
|
114 |
|
return parent::getCasts(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @return string |
76
|
|
|
*/ |
77
|
132 |
|
public function getTable(): string |
78
|
|
|
{ |
79
|
132 |
|
if (!$this->table) { |
80
|
132 |
|
$this->table = config('wallet.wallet.table', 'wallets'); |
81
|
|
|
} |
82
|
|
|
|
83
|
132 |
|
return parent::getTable(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param string $name |
88
|
|
|
* @return void |
89
|
|
|
*/ |
90
|
114 |
|
public function setNameAttribute(string $name): void |
91
|
|
|
{ |
92
|
114 |
|
$this->attributes['name'] = $name; |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Must be updated only if the model does not exist |
96
|
|
|
* or the slug is empty |
97
|
|
|
*/ |
98
|
114 |
|
if (!$this->exists && !array_key_exists('slug', $this->attributes)) { |
99
|
114 |
|
$this->attributes['slug'] = Str::slug($name); |
100
|
|
|
} |
101
|
114 |
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @return bool |
105
|
|
|
*/ |
106
|
17 |
|
public function refreshBalance(): bool |
107
|
|
|
{ |
108
|
17 |
|
return app(WalletService::class)->refresh($this); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @return int |
113
|
|
|
*/ |
114
|
17 |
|
public function getAvailableBalance(): int |
115
|
|
|
{ |
116
|
17 |
|
return $this->transactions() |
117
|
17 |
|
->where('wallet_id', $this->getKey()) |
118
|
17 |
|
->where('confirmed', true) |
119
|
17 |
|
->sum('amount'); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @return MorphTo |
124
|
|
|
*/ |
125
|
92 |
|
public function holder(): MorphTo |
126
|
|
|
{ |
127
|
92 |
|
return $this->morphTo(); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @return string |
132
|
|
|
*/ |
133
|
3 |
|
public function getCurrencyAttribute(): string |
134
|
|
|
{ |
135
|
3 |
|
$currencies = config('wallet.currencies', []); |
136
|
3 |
|
return $currencies[$this->slug] ?? Str::upper($this->slug); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
} |
140
|
|
|
|