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