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