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 | 'title', |
||
28 | 'amount', |
||
29 | 'deposit_fixed', |
||
30 | 'deposit_lendable', |
||
31 | 'remit_planned', |
||
32 | 'remit_auction', |
||
33 | 'round_id', |
||
34 | 'start_sid', |
||
35 | 'end_sid', |
||
36 | ]; |
||
37 | |||
38 | /** |
||
39 | * The relationships that should always be loaded. |
||
40 | * |
||
41 | * @var array |
||
42 | */ |
||
43 | protected $with = [ |
||
44 | 'def', |
||
45 | ]; |
||
46 | |||
47 | /** |
||
48 | * The "booted" method of the model. |
||
49 | * |
||
50 | * @return void |
||
51 | */ |
||
52 | protected static function booted() |
||
53 | { |
||
54 | // Scope for pool sessions. Always applied by default. |
||
55 | static::addGlobalScope('sessions', function (Builder $query) { |
||
56 | $query->addSelect([ |
||
57 | 'pools.*', |
||
58 | 'vp.end_date', |
||
59 | 'vp.start_date', |
||
60 | 'vp.sessions_count', |
||
61 | ])->join(DB::raw('v_pools as vp'), 'vp.pool_id', '=', 'pools.id'); |
||
62 | }); |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * Get the attributes that should be cast. |
||
67 | * |
||
68 | * @return array<string, string> |
||
69 | */ |
||
70 | protected function casts(): array |
||
71 | { |
||
72 | return [ |
||
73 | 'start_date' => 'datetime:Y-m-d', |
||
74 | 'end_date' => 'datetime:Y-m-d', |
||
75 | ]; |
||
76 | } |
||
77 | |||
78 | public function notes(): Attribute |
||
79 | { |
||
80 | return Attribute::make(get: fn() => $this->def->notes); |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * @param Builder $query |
||
85 | * @param Carbon $startDate |
||
86 | * @param Carbon $endDate |
||
87 | * |
||
88 | * @return Builder |
||
89 | */ |
||
90 | private function filterOnDates(Builder $query, Carbon $startDate, Carbon $endDate): Builder |
||
91 | { |
||
92 | return $query |
||
93 | ->where('vp.end_date', '>=', $startDate) |
||
94 | ->where('vp.start_date', '<=', $endDate); |
||
95 | } |
||
96 | |||
97 | /** |
||
98 | * Scope to active pools for a given round. |
||
99 | * |
||
100 | * @param Builder $query |
||
101 | * @param Round $round |
||
102 | * |
||
103 | * @return Builder |
||
104 | */ |
||
105 | public function scopeOfRound(Builder $query, Round $round): Builder |
||
106 | { |
||
107 | $query->whereHas('def', fn($q) => |
||
108 | $q->where('guild_id', $round->guild_id)); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
109 | return $this->filterOnDates($query, $round->start_date, $round->end_date); |
||
0 ignored issues
–
show
|
|||
110 | } |
||
111 | |||
112 | /** |
||
113 | * @param Builder $query |
||
114 | * |
||
115 | * @return Builder |
||
116 | */ |
||
117 | public function scopeRemitPlanned(Builder $query): Builder |
||
118 | { |
||
119 | return $query->where('remit_planned', true); |
||
120 | } |
||
121 | |||
122 | public function subscriptions() |
||
123 | { |
||
124 | return $this->hasMany(Subscription::class); |
||
125 | } |
||
126 | |||
127 | public function receivables() |
||
128 | { |
||
129 | return $this->hasManyThrough(Receivable::class, Subscription::class); |
||
130 | } |
||
131 | |||
132 | public function payables() |
||
133 | { |
||
134 | return $this->hasManyThrough(Payable::class, Subscription::class); |
||
135 | } |
||
136 | |||
137 | public function def() |
||
138 | { |
||
139 | return $this->belongsTo(PoolDef::class, 'def_id'); |
||
140 | } |
||
141 | |||
142 | public function start() |
||
143 | { |
||
144 | return $this->belongsTo(Session::class, 'start_sid'); |
||
145 | } |
||
146 | |||
147 | public function end() |
||
148 | { |
||
149 | return $this->belongsTo(Session::class, 'end_sid'); |
||
150 | } |
||
151 | |||
152 | public function round() |
||
153 | { |
||
154 | return $this->belongsTo(Round::class); |
||
155 | } |
||
156 | |||
157 | public function sessions() |
||
158 | { |
||
159 | return $this->belongsToMany(Session::class, 'v_pool_session'); |
||
160 | } |
||
161 | |||
162 | public function disabled_sessions() |
||
163 | { |
||
164 | // Filter on the sessions in the pool timespan. |
||
165 | return $this->belongsToMany(Session::class, 'v_pool_session_disabled'); |
||
166 | } |
||
167 | |||
168 | public function fund() |
||
169 | { |
||
170 | return $this->hasOne(Fund::class); |
||
171 | } |
||
172 | } |
||
173 |