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 | * The relationships that should always be loaded. |
||||||
48 | * |
||||||
49 | * @var array |
||||||
50 | */ |
||||||
51 | protected $with = [ |
||||||
52 | 'def', |
||||||
53 | ]; |
||||||
54 | |||||||
55 | /** |
||||||
56 | * The "booted" method of the model. |
||||||
57 | * |
||||||
58 | * @return void |
||||||
59 | */ |
||||||
60 | protected static function booted() |
||||||
61 | { |
||||||
62 | // Scope for fund sessions. Always applied by default. |
||||||
63 | static::addGlobalScope('sessions', function (Builder $query) { |
||||||
64 | $query->addSelect([ |
||||||
65 | 'funds.*', |
||||||
66 | 'v.end_date', |
||||||
67 | 'v.start_date', |
||||||
68 | 'v.interest_date', |
||||||
69 | 'v.sessions_count', |
||||||
70 | ])->join(DB::raw('v_funds as v'), 'v.fund_id', '=', 'funds.id'); |
||||||
71 | }); |
||||||
72 | } |
||||||
73 | |||||||
74 | /** |
||||||
75 | * Get the attributes that should be cast. |
||||||
76 | * |
||||||
77 | * @return array<string, string> |
||||||
78 | */ |
||||||
79 | protected function casts(): array |
||||||
80 | { |
||||||
81 | return [ |
||||||
82 | 'options' => 'array', |
||||||
83 | 'start_date' => 'datetime:Y-m-d', |
||||||
84 | 'end_date' => 'datetime:Y-m-d', |
||||||
85 | 'interest_date' => 'datetime:Y-m-d', |
||||||
86 | ]; |
||||||
87 | } |
||||||
88 | |||||||
89 | public function title(): Attribute |
||||||
90 | { |
||||||
91 | return Attribute::make( |
||||||
92 | get: fn() => match(true) { |
||||||
93 | $this->def->type_user => $this->def->title, |
||||||
0 ignored issues
–
show
|
|||||||
94 | $this->pool !== null => $this->pool->title, |
||||||
0 ignored issues
–
show
|
|||||||
95 | default => trans('tontine.fund.labels.default'), |
||||||
96 | }, |
||||||
97 | ); |
||||||
98 | } |
||||||
99 | |||||||
100 | public function notes(): Attribute |
||||||
101 | { |
||||||
102 | return Attribute::make( |
||||||
103 | get: fn() => $this->def->notes, |
||||||
104 | ); |
||||||
105 | } |
||||||
106 | |||||||
107 | /** |
||||||
108 | * Get the start amount. |
||||||
109 | * |
||||||
110 | * @return Attribute |
||||||
111 | */ |
||||||
112 | public function startAmount(): Attribute |
||||||
113 | { |
||||||
114 | return Attribute::make( |
||||||
115 | get: fn() => $this->options['amount']['start'] ?? 0, |
||||||
116 | ); |
||||||
117 | } |
||||||
118 | |||||||
119 | /** |
||||||
120 | * Get the end amount. |
||||||
121 | * |
||||||
122 | * @return Attribute |
||||||
123 | */ |
||||||
124 | public function endAmount(): Attribute |
||||||
125 | { |
||||||
126 | return Attribute::make( |
||||||
127 | get: fn() => $this->options['amount']['end'] ?? 0, |
||||||
128 | ); |
||||||
129 | } |
||||||
130 | |||||||
131 | /** |
||||||
132 | * Get the profit amount. |
||||||
133 | * |
||||||
134 | * @return Attribute |
||||||
135 | */ |
||||||
136 | protected function profitAmount(): Attribute |
||||||
137 | { |
||||||
138 | return Attribute::make( |
||||||
139 | get: fn() => $this->options['amount']['profit'] ?? 0, |
||||||
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)); |
||||||
0 ignored issues
–
show
|
|||||||
167 | return $this->filterOnDates($query, $round->start_date, $round->end_date); |
||||||
0 ignored issues
–
show
|
|||||||
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)); |
||||||
0 ignored issues
–
show
|
|||||||
181 | return $this->filterOnDates($query, $session->day_date, $session->day_date); |
||||||
0 ignored issues
–
show
$session->day_date of type string is incompatible with the type Carbon\Carbon expected by parameter $endDate of Siak\Tontine\Model\Fund::filterOnDates() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() $session->day_date of type string is incompatible with the type Carbon\Carbon expected by parameter $startDate of Siak\Tontine\Model\Fund::filterOnDates() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
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'); |
||||||
0 ignored issues
–
show
|
|||||||
232 | } |
||||||
233 | } |
||||||
234 |
Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.