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 | class Pool extends Base |
||
11 | { |
||
12 | use Traits\DateFormatter; |
||
13 | |||
14 | /** |
||
15 | * Indicates if the model should be timestamped. |
||
16 | * |
||
17 | * @var bool |
||
18 | */ |
||
19 | public $timestamps = false; |
||
20 | |||
21 | /** |
||
22 | * The attributes that are mass assignable. |
||
23 | * |
||
24 | * @var array |
||
25 | */ |
||
26 | protected $fillable = [ |
||
27 | 'round_id', |
||
28 | 'start_sid', |
||
29 | 'end_sid', |
||
30 | ]; |
||
31 | |||
32 | /** |
||
33 | * The relationships that should always be loaded. |
||
34 | * |
||
35 | * @var array |
||
36 | */ |
||
37 | protected $with = [ |
||
38 | 'def', |
||
39 | ]; |
||
40 | |||
41 | /** |
||
42 | * The "booted" method of the model. |
||
43 | * |
||
44 | * @return void |
||
45 | */ |
||
46 | protected static function booted() |
||
47 | { |
||
48 | // Scope for pool sessions. Always applied by default. |
||
49 | static::addGlobalScope('sessions', function (Builder $query) { |
||
50 | $query->addSelect([ |
||
51 | 'pools.*', |
||
52 | 'vp.end_date', |
||
53 | 'vp.start_date', |
||
54 | 'vp.sessions_count', |
||
55 | ])->join(DB::raw('v_pools as vp'), 'vp.pool_id', '=', 'pools.id'); |
||
56 | }); |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | * Get the attributes that should be cast. |
||
61 | * |
||
62 | * @return array<string, string> |
||
63 | */ |
||
64 | protected function casts(): array |
||
65 | { |
||
66 | return [ |
||
67 | 'start_date' => 'datetime:Y-m-d', |
||
68 | 'end_date' => 'datetime:Y-m-d', |
||
69 | ]; |
||
70 | } |
||
71 | |||
72 | public function title(): Attribute |
||
73 | { |
||
74 | return Attribute::make(get: fn() => $this->def->title); |
||
75 | } |
||
76 | |||
77 | public function amount(): Attribute |
||
78 | { |
||
79 | return Attribute::make(get: fn() => $this->def->amount); |
||
80 | } |
||
81 | |||
82 | public function notes(): Attribute |
||
83 | { |
||
84 | return Attribute::make(get: fn() => $this->def->notes); |
||
85 | } |
||
86 | |||
87 | public function depositFixed(): Attribute |
||
88 | { |
||
89 | return Attribute::make( |
||
90 | get: fn() => ($this->def->properties['deposit']['fixed'] ?? true) === true, |
||
91 | ); |
||
92 | } |
||
93 | |||
94 | public function depositLendable(): Attribute |
||
95 | { |
||
96 | return Attribute::make( |
||
97 | get: fn() => ($this->def->properties['deposit']['lendable'] ?? false) === true, |
||
98 | ); |
||
99 | } |
||
100 | |||
101 | public function remitPlanned(): Attribute |
||
102 | { |
||
103 | return Attribute::make( |
||
104 | get: fn() => ($this->def->properties['remit']['planned'] ?? true) === true, |
||
105 | ); |
||
106 | } |
||
107 | |||
108 | public function remitAuction(): Attribute |
||
109 | { |
||
110 | return Attribute::make( |
||
111 | get: fn() => ($this->def->properties['remit']['auction'] ?? false) === true, |
||
112 | ); |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * @param Builder $query |
||
117 | * @param Carbon $startDate |
||
118 | * @param Carbon $endDate |
||
119 | * |
||
120 | * @return Builder |
||
121 | */ |
||
122 | private function filterOnDates(Builder $query, Carbon $startDate, Carbon $endDate): Builder |
||
123 | { |
||
124 | return $query->where('vp.end_date', '>=', $startDate) |
||
125 | ->where('vp.start_date', '<=', $endDate); |
||
126 | } |
||
127 | |||
128 | /** |
||
129 | * Scope to active pools for a given round. |
||
130 | * |
||
131 | * @param Builder $query |
||
132 | * @param Round $round |
||
133 | * |
||
134 | * @return Builder |
||
135 | */ |
||
136 | public function scopeOfRound(Builder $query, Round $round): Builder |
||
137 | { |
||
138 | $query->whereHas('def', fn($q) => $q->where('guild_id', $round->guild_id)); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
139 | return $this->filterOnDates($query, $round->start_date, $round->end_date); |
||
0 ignored issues
–
show
|
|||
140 | } |
||
141 | |||
142 | /** |
||
143 | * @param Builder $query |
||
144 | * |
||
145 | * @return Builder |
||
146 | */ |
||
147 | public function scopeRemitPlanned(Builder $query): Builder |
||
148 | { |
||
149 | return $query->whereHas('def', fn($q) => $q->where('properties->remit->planned', true)); |
||
0 ignored issues
–
show
|
|||
150 | } |
||
151 | |||
152 | public function subscriptions() |
||
153 | { |
||
154 | return $this->hasMany(Subscription::class); |
||
155 | } |
||
156 | |||
157 | public function def() |
||
158 | { |
||
159 | return $this->belongsTo(PoolDef::class, 'def_id'); |
||
160 | } |
||
161 | |||
162 | public function start() |
||
163 | { |
||
164 | return $this->belongsTo(Session::class, 'start_sid'); |
||
165 | } |
||
166 | |||
167 | public function end() |
||
168 | { |
||
169 | return $this->belongsTo(Session::class, 'end_sid'); |
||
170 | } |
||
171 | |||
172 | public function round() |
||
173 | { |
||
174 | return $this->belongsTo(Round::class); |
||
175 | } |
||
176 | |||
177 | public function sessions() |
||
178 | { |
||
179 | return $this->belongsToMany(Session::class, 'v_pool_session'); |
||
180 | } |
||
181 | |||
182 | public function disabled_sessions() |
||
183 | { |
||
184 | // Filter on the sessions in the pool timespan. |
||
185 | return $this->belongsToMany(Session::class, 'v_pool_session_disabled'); |
||
186 | } |
||
187 | } |
||
188 |