|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Siak\Tontine\Model; |
|
4
|
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
|
6
|
|
|
use Database\Factories\PoolFactory; |
|
7
|
|
|
use Illuminate\Database\Eloquent\Builder; |
|
8
|
|
|
use Illuminate\Database\Eloquent\Factories\Factory; |
|
9
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory; |
|
10
|
|
|
use Illuminate\Support\Facades\DB; |
|
11
|
|
|
use Siak\Tontine\Model\Traits\HasCurrency; |
|
12
|
|
|
|
|
13
|
|
|
use function trans; |
|
14
|
|
|
|
|
15
|
|
|
class Pool extends Base |
|
16
|
|
|
{ |
|
17
|
|
|
use HasFactory; |
|
18
|
|
|
use HasCurrency; |
|
|
|
|
|
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Indicates if the model should be timestamped. |
|
22
|
|
|
* |
|
23
|
|
|
* @var bool |
|
24
|
|
|
*/ |
|
25
|
|
|
public $timestamps = false; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* The attributes that are mass assignable. |
|
29
|
|
|
* |
|
30
|
|
|
* @var array |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $fillable = [ |
|
33
|
|
|
'title', |
|
34
|
|
|
'amount', |
|
35
|
|
|
'notes', |
|
36
|
|
|
'properties', |
|
37
|
|
|
]; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* The attributes that should be mutated to dates. |
|
41
|
|
|
* |
|
42
|
|
|
* @var array |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $dates = [ |
|
45
|
|
|
'start_at', |
|
46
|
|
|
'end_at', |
|
47
|
|
|
]; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* The attributes that should be cast. |
|
51
|
|
|
* |
|
52
|
|
|
* @var array |
|
53
|
|
|
*/ |
|
54
|
|
|
protected $casts = [ |
|
55
|
|
|
'properties' => 'array', |
|
56
|
|
|
]; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* The model's default values for attributes. |
|
60
|
|
|
* |
|
61
|
|
|
* @var array |
|
62
|
|
|
*/ |
|
63
|
|
|
protected $attributes = [ |
|
64
|
|
|
'properties' => '{}', |
|
65
|
|
|
]; |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* The relationships that should always be loaded. |
|
69
|
|
|
* |
|
70
|
|
|
* @var array |
|
71
|
|
|
*/ |
|
72
|
|
|
protected $with = [ |
|
73
|
|
|
'pool_round', |
|
74
|
|
|
]; |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Create a new factory instance for the model. |
|
78
|
|
|
* |
|
79
|
|
|
* @return Factory |
|
80
|
|
|
*/ |
|
81
|
|
|
protected static function newFactory() |
|
82
|
|
|
{ |
|
83
|
|
|
return PoolFactory::new(); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* The "booted" method of the model. |
|
88
|
|
|
* |
|
89
|
|
|
* @return void |
|
90
|
|
|
*/ |
|
91
|
|
|
protected static function booted() |
|
92
|
|
|
{ |
|
93
|
|
|
static::addGlobalScope('dates', function (Builder $query) { |
|
94
|
|
|
$query->addSelect(['pools.*', 'v.end_at', 'v.start_at', 'v.tontine_id']) |
|
95
|
|
|
->join(DB::raw('v_pools as v'), 'v.id', '=', 'pools.id'); |
|
96
|
|
|
}); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @param Builder $query |
|
101
|
|
|
* @param Round $round |
|
102
|
|
|
* @param Carbon $startDate |
|
103
|
|
|
* @param Carbon $endDate |
|
104
|
|
|
* |
|
105
|
|
|
* @return Builder |
|
106
|
|
|
*/ |
|
107
|
|
|
private function filterOnRoundOrDates(Builder $query, Round $round, |
|
108
|
|
|
Carbon $startDate, Carbon $endDate): Builder |
|
109
|
|
|
{ |
|
110
|
|
|
return $query->where(function(Builder $query) use($round, $startDate, $endDate) { |
|
111
|
|
|
$query->orWhere('pools.round_id', $round->id) |
|
112
|
|
|
->orWhere(function(Builder $query) use($round, $startDate, $endDate) { |
|
113
|
|
|
$query->where('v.tontine_id', $round->tontine_id) |
|
114
|
|
|
->whereDate('v.end_at', '>=', $startDate->format('Y-m-d')) |
|
115
|
|
|
->whereDate('v.start_at', '<=', $endDate->format('Y-m-d')); |
|
116
|
|
|
}); |
|
117
|
|
|
}); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Scope to active pools for a given round. |
|
122
|
|
|
* |
|
123
|
|
|
* @param Builder $query |
|
124
|
|
|
* @param Round $round |
|
125
|
|
|
* |
|
126
|
|
|
* @return Builder |
|
127
|
|
|
*/ |
|
128
|
|
|
public function scopeOfRound(Builder $query, Round $round): Builder |
|
129
|
|
|
{ |
|
130
|
|
|
$startDate = $round->start_at; |
|
131
|
|
|
$endDate = $round->end_at; |
|
132
|
|
|
return $this->filterOnRoundOrDates($query, $round, $startDate, $endDate); |
|
|
|
|
|
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Scope to active pools for a given session. |
|
137
|
|
|
* |
|
138
|
|
|
* @param Builder $query |
|
139
|
|
|
* @param Session $session |
|
140
|
|
|
* |
|
141
|
|
|
* @return Builder |
|
142
|
|
|
*/ |
|
143
|
|
|
public function scopeOfSession(Builder $query, Session $session): Builder |
|
144
|
|
|
{ |
|
145
|
|
|
$date = $session->start_at; |
|
146
|
|
|
return $this->filterOnRoundOrDates($query, $session->round, $date, $date); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
public function getStartDateAttribute() |
|
150
|
|
|
{ |
|
151
|
|
|
return $this->start_at->translatedFormat(trans('tontine.date.format')); |
|
|
|
|
|
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
public function getEndDateAttribute() |
|
155
|
|
|
{ |
|
156
|
|
|
return $this->end_at->translatedFormat(trans('tontine.date.format')); |
|
|
|
|
|
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
public function getDepositFixedAttribute() |
|
160
|
|
|
{ |
|
161
|
|
|
return ($this->properties['deposit']['fixed'] ?? true) === true; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
public function getDepositLendableAttribute() |
|
165
|
|
|
{ |
|
166
|
|
|
return ($this->properties['deposit']['lendable'] ?? false) === true; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
public function getRemitPlannedAttribute() |
|
170
|
|
|
{ |
|
171
|
|
|
return ($this->properties['remit']['planned'] ?? true) === true; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
public function getRemitAuctionAttribute() |
|
175
|
|
|
{ |
|
176
|
|
|
return ($this->properties['remit']['auction'] ?? false) === true; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
public function getRemitPayableAttribute() |
|
180
|
|
|
{ |
|
181
|
|
|
return $this->remit_planned && !$this->remit_auction; |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
public function round() |
|
185
|
|
|
{ |
|
186
|
|
|
return $this->belongsTo(Round::class); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
public function pool_round() |
|
190
|
|
|
{ |
|
191
|
|
|
return $this->hasOne(PoolRound::class); |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
public function subscriptions() |
|
195
|
|
|
{ |
|
196
|
|
|
return $this->hasMany(Subscription::class); |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
public function disabledSessions() |
|
200
|
|
|
{ |
|
201
|
|
|
return $this->belongsToMany(Session::class, 'pool_session_disabled'); |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
public function tontine() |
|
205
|
|
|
{ |
|
206
|
|
|
// The "tontine_id" field is added to the table by the join |
|
207
|
|
|
// with the v_pools view. So we can now add this relationship. |
|
208
|
|
|
return $this->belongsTo(Tontine::class); |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
public function sessions() |
|
212
|
|
|
{ |
|
213
|
|
|
if(!$this->pool_round) |
|
214
|
|
|
{ |
|
215
|
|
|
return $this->round->sessions(); |
|
216
|
|
|
} |
|
217
|
|
|
return $this->tontine->sessions() |
|
218
|
|
|
->whereDate('start_at', '>=', $this->start_at->format('Y-m-d')) |
|
|
|
|
|
|
219
|
|
|
->whereDate('start_at', '<=', $this->end_at->format('Y-m-d')); |
|
|
|
|
|
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
public function enabledSessions() |
|
223
|
|
|
{ |
|
224
|
|
|
return $this->sessions()->whereDoesntHave('disabledPools', |
|
225
|
|
|
fn(Builder $query) => $query->where('pools.id', $this->id)); |
|
226
|
|
|
} |
|
227
|
|
|
} |
|
228
|
|
|
|