Passed
Pull Request — main (#55)
by Thierry
05:38
created

Session::savings()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Siak\Tontine\Model;
4
5
use Illuminate\Support\Facades\DB;
6
use Illuminate\Database\Eloquent\Builder;
7
8
use function trans;
9
10
class Session extends Base
11
{
12
    /**
13
     * @const
14
     */
15
    const STATUS_PENDING = 0;
16
17
    /**
18
     * @const
19
     */
20
    const STATUS_OPENED = 1;
21
22
    /**
23
     * @const
24
     */
25
    const STATUS_CLOSED = 2;
26
27
    /**
28
     * Indicates if the model should be timestamped.
29
     *
30
     * @var bool
31
     */
32
    public $timestamps = false;
33
34
    /**
35
     * The attributes that are mass assignable.
36
     *
37
     * @var array
38
     */
39
    protected $fillable = [
40
        'title',
41
        'abbrev',
42
        'agenda',
43
        'report',
44
        'status',
45
        'notes',
46
        'venue',
47
        'start_at',
48
        'end_at',
49
        'host_id',
50
    ];
51
52
    /**
53
     * The attributes that should be mutated to dates.
54
     *
55
     * @var array
56
     */
57
    protected $dates = [
58
        'start_at',
59
        'end_at',
60
    ];
61
62
    /**
63
     * The relationships that should always be loaded.
64
     *
65
     * @var array
66
     */
67
    protected $with = [
68
        'host',
69
        'disabledPools',
70
    ];
71
72
    public function getNotFirstAttribute()
73
    {
74
        return $this->round->sessions()->where('start_at', '<', $this->start_at)->exists();
75
    }
76
77
    public function getNotLastAttribute()
78
    {
79
        return $this->round->sessions()->where('start_at', '>', $this->start_at)->exists();
80
    }
81
82
    public function getAbbrevAttribute($value)
83
    {
84
        return $value ?? $this->start_at->format('M y');
85
    }
86
87
    public function getDateAttribute()
88
    {
89
        return $this->start_at->translatedFormat(trans('tontine.date.format'));
90
    }
91
92
    public function getTimesAttribute()
93
    {
94
        return $this->start_at->format('H:i') . ' - ' . $this->end_at->format('H:i');
95
    }
96
97
    public function getPendingAttribute()
98
    {
99
        return $this->status === self::STATUS_PENDING;
100
    }
101
102
    public function getOpenedAttribute()
103
    {
104
        return $this->status === self::STATUS_OPENED;
105
    }
106
107
    public function getClosedAttribute()
108
    {
109
        return $this->status === self::STATUS_CLOSED;
110
    }
111
112
    public function round()
113
    {
114
        return $this->belongsTo(Round::class);
115
    }
116
117
    public function host()
118
    {
119
        return $this->belongsTo(Member::class);
120
    }
121
122
    public function payables()
123
    {
124
        return $this->hasMany(Payable::class)->orderBy('payables.id', 'asc');
125
    }
126
127
    public function payableAmounts()
128
    {
129
        return $this->hasMany(Payable::class)
130
            ->join('subscriptions', 'subscriptions.id', '=', 'payables.subscription_id')
131
            ->join('pools', 'subscriptions.pool_id', '=', 'pools.id')
132
            ->whereHas('remitment')
133
            ->groupBy('pools.id')
134
            ->select('pools.id', DB::raw('sum(pools.amount) as amount'));
135
    }
136
137
    public function receivables()
138
    {
139
        return $this->hasMany(Receivable::class);
140
    }
141
142
    public function receivableAmounts()
143
    {
144
        return $this->hasMany(Receivable::class)
145
            ->join('subscriptions', 'subscriptions.id', '=', 'receivables.subscription_id')
146
            ->join('pools', 'subscriptions.pool_id', '=', 'pools.id')
147
            ->whereHas('deposit')
148
            ->groupBy('pools.id')
149
            ->select('pools.id', DB::raw('sum(pools.amount) as amount'));
150
    }
151
152
    public function session_bills()
153
    {
154
        return $this->hasMany(SessionBill::class);
155
    }
156
157
    public function libre_bills()
158
    {
159
        return $this->hasMany(LibreBill::class);
160
    }
161
162
    public function loans()
163
    {
164
        return $this->hasMany(Loan::class);
165
    }
166
167
    public function auctions()
168
    {
169
        return $this->hasMany(Auction::class);
170
    }
171
172
    public function refunds()
173
    {
174
        return $this->hasMany(Refund::class);
175
    }
176
177
    public function partial_refunds()
178
    {
179
        return $this->hasMany(PartialRefund::class);
180
    }
181
182
    public function savings()
183
    {
184
        return $this->hasMany(Saving::class);
185
    }
186
187
    public function disbursements()
188
    {
189
        return $this->hasMany(Disbursement::class);
190
    }
191
192
    public function disabledPools()
193
    {
194
        return $this->belongsToMany(Pool::class, 'pool_session_disabled');
195
    }
196
197
    public function absents()
198
    {
199
        return $this->belongsToMany(Member::class, 'absences');
200
    }
201
202
    public function enabled(Pool $pool)
203
    {
204
        return $this->disabledPools->find($pool->id) === null;
205
    }
206
207
    public function disabled(Pool $pool)
208
    {
209
        return $this->disabledPools->find($pool->id) !== null;
210
    }
211
212
    /**
213
     * @param  Builder  $query
214
     *
215
     * @return Builder
216
     */
217
    public function scopeActive(Builder $query): Builder
218
    {
219
        return $query->where('status', '!=', self::STATUS_PENDING);
220
    }
221
}
222