1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Siak\Tontine\Model; |
4
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
6
|
|
|
use Illuminate\Database\Eloquent\Casts\Attribute; |
7
|
|
|
use Illuminate\Database\Eloquent\Builder; |
8
|
|
|
use Illuminate\Support\Facades\DB; |
9
|
|
|
|
10
|
|
|
use function trans; |
11
|
|
|
|
12
|
|
|
class Fund extends Base |
13
|
|
|
{ |
14
|
|
|
use Traits\DateFormatter; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Indicates if the model should be timestamped. |
18
|
|
|
* |
19
|
|
|
* @var bool |
20
|
|
|
*/ |
21
|
|
|
public $timestamps = false; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* The attributes that are mass assignable. |
25
|
|
|
* |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
protected $fillable = [ |
29
|
|
|
'options', |
30
|
|
|
'def_id', |
31
|
|
|
'round_id', |
32
|
|
|
'start_sid', |
33
|
|
|
'end_sid', |
34
|
|
|
'interest_sid', |
35
|
|
|
]; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* The model's default values for attributes. |
39
|
|
|
* |
40
|
|
|
* @var array |
41
|
|
|
*/ |
42
|
|
|
protected $attributes = [ |
43
|
|
|
'options' => '{}', |
44
|
|
|
]; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Get the attributes that should be cast. |
48
|
|
|
* |
49
|
|
|
* @return array<string, string> |
50
|
|
|
*/ |
51
|
|
|
protected function casts(): array |
52
|
|
|
{ |
53
|
|
|
return [ |
54
|
|
|
'options' => 'array', |
55
|
|
|
'start_date' => 'datetime:Y-m-d', |
56
|
|
|
'end_date' => 'datetime:Y-m-d', |
57
|
|
|
'interest_date' => 'datetime:Y-m-d', |
58
|
|
|
]; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* The relationships that should always be loaded. |
63
|
|
|
* |
64
|
|
|
* @var array |
65
|
|
|
*/ |
66
|
|
|
protected $with = [ |
67
|
|
|
'def', |
68
|
|
|
]; |
69
|
|
|
|
70
|
|
|
public function title(): Attribute |
71
|
|
|
{ |
72
|
|
|
return Attribute::make( |
73
|
|
|
get: fn() => match(true) { |
74
|
|
|
$this->def->type_user => $this->def->title, |
|
|
|
|
75
|
|
|
$this->pool !== null => $this->pool->title, |
|
|
|
|
76
|
|
|
default => trans('tontine.fund.labels.default'), |
77
|
|
|
}, |
78
|
|
|
); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function notes(): Attribute |
82
|
|
|
{ |
83
|
|
|
return Attribute::make( |
84
|
|
|
get: fn() => $this->def->notes, |
85
|
|
|
); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Get the start amount. |
90
|
|
|
* |
91
|
|
|
* @return Attribute |
92
|
|
|
*/ |
93
|
|
|
public function startAmount(): Attribute |
94
|
|
|
{ |
95
|
|
|
return Attribute::make( |
96
|
|
|
get: fn() => $this->options['amount']['start'] ?? 0, |
97
|
|
|
); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Get the end amount. |
102
|
|
|
* |
103
|
|
|
* @return Attribute |
104
|
|
|
*/ |
105
|
|
|
public function endAmount(): Attribute |
106
|
|
|
{ |
107
|
|
|
return Attribute::make( |
108
|
|
|
get: fn() => $this->options['amount']['end'] ?? 0, |
109
|
|
|
); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Get the profit amount. |
114
|
|
|
* |
115
|
|
|
* @return Attribute |
116
|
|
|
*/ |
117
|
|
|
protected function profitAmount(): Attribute |
118
|
|
|
{ |
119
|
|
|
return Attribute::make( |
120
|
|
|
get: fn() => $this->options['amount']['profit'] ?? 0, |
121
|
|
|
); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* The "booted" method of the model. |
126
|
|
|
* |
127
|
|
|
* @return void |
128
|
|
|
*/ |
129
|
|
|
protected static function booted() |
130
|
|
|
{ |
131
|
|
|
// Scope for fund sessions. Always applied by default. |
132
|
|
|
static::addGlobalScope('sessions', function (Builder $query) { |
133
|
|
|
$query->addSelect([ |
134
|
|
|
'funds.*', |
135
|
|
|
'v.end_date', |
136
|
|
|
'v.start_date', |
137
|
|
|
'v.interest_date', |
138
|
|
|
'v.sessions_count', |
139
|
|
|
])->join(DB::raw('v_funds as v'), 'v.fund_id', '=', 'funds.id'); |
140
|
|
|
}); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @param Builder $query |
145
|
|
|
* @param Carbon $startDate |
146
|
|
|
* @param Carbon $endDate |
147
|
|
|
* |
148
|
|
|
* @return Builder |
149
|
|
|
*/ |
150
|
|
|
private function filterOnDates(Builder $query, Carbon $startDate, Carbon $endDate): Builder |
151
|
|
|
{ |
152
|
|
|
return $query->where('v.end_date', '>=', $startDate) |
153
|
|
|
->where('v.start_date', '<=', $endDate); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Scope to active pools for a given round. |
158
|
|
|
* |
159
|
|
|
* @param Builder $query |
160
|
|
|
* @param Round $round |
161
|
|
|
* |
162
|
|
|
* @return Builder |
163
|
|
|
*/ |
164
|
|
|
public function scopeOfRound(Builder $query, Round $round): Builder |
165
|
|
|
{ |
166
|
|
|
$query->whereHas('def', fn($q) => $q->where('guild_id', $round->guild_id)); |
|
|
|
|
167
|
|
|
return $this->filterOnDates($query, $round->start_date, $round->end_date); |
|
|
|
|
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Scope to active pools for a given session. |
172
|
|
|
* |
173
|
|
|
* @param Builder $query |
174
|
|
|
* @param Session $session |
175
|
|
|
* |
176
|
|
|
* @return Builder |
177
|
|
|
*/ |
178
|
|
|
public function scopeOfSession(Builder $query, Session $session): Builder |
179
|
|
|
{ |
180
|
|
|
$query->whereHas('def', fn($q) => $q->where('guild_id', $session->round->guild_id)); |
|
|
|
|
181
|
|
|
return $this->filterOnDates($query, $session->day_date, $session->day_date); |
|
|
|
|
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
public function def() |
185
|
|
|
{ |
186
|
|
|
return $this->belongsTo(FundDef::class, 'def_id'); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
public function start() |
190
|
|
|
{ |
191
|
|
|
return $this->belongsTo(Session::class, 'start_sid'); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
public function end() |
195
|
|
|
{ |
196
|
|
|
return $this->belongsTo(Session::class, 'end_sid'); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
public function interest() |
200
|
|
|
{ |
201
|
|
|
return $this->belongsTo(Session::class, 'interest_sid'); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
public function round() |
205
|
|
|
{ |
206
|
|
|
return $this->belongsTo(Round::class); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
public function pool() |
210
|
|
|
{ |
211
|
|
|
return $this->belongsTo(Pool::class); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
public function sessions() |
215
|
|
|
{ |
216
|
|
|
return $this->belongsToMany(Session::class, 'v_fund_session'); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
public function savings() |
220
|
|
|
{ |
221
|
|
|
return $this->hasMany(Saving::class); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* @param Builder $query |
226
|
|
|
* |
227
|
|
|
* @return Builder |
228
|
|
|
*/ |
229
|
|
|
public function scopeReal(Builder $query): Builder |
230
|
|
|
{ |
231
|
|
|
return $query->whereNull('pool_id'); |
|
|
|
|
232
|
|
|
} |
233
|
|
|
} |
234
|
|
|
|
Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.