Receivable::scopePaidEarlier()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 5
rs 10
1
<?php
2
3
namespace Siak\Tontine\Model;
4
5
use Illuminate\Database\Eloquent\Builder;
6
use Illuminate\Database\Eloquent\Casts\Attribute;
7
8
class Receivable extends Base
9
{
10
    /**
11
     * Indicates if the model should be timestamped.
12
     *
13
     * @var bool
14
     */
15
    public $timestamps = false;
16
17
    /**
18
     * The attributes that are mass assignable.
19
     *
20
     * @var array
21
     */
22
    protected $fillable = [
23
        'notes',
24
        'session_id',
25
    ];
26
27
    public function paidLate(): Attribute
28
    {
29
        return Attribute::make(
30
            get: fn() => $this->deposit !== null &&
31
                $this->deposit->session_id !== $this->session_id,
0 ignored issues
show
Bug introduced by
The property session_id does not exist on Siak\Tontine\Model\Deposit. Did you mean session?
Loading history...
32
        );
33
    }
34
35
    public function session()
36
    {
37
        return $this->belongsTo(Session::class);
38
    }
39
40
    public function subscription()
41
    {
42
        return $this->belongsTo(Subscription::class);
43
    }
44
45
    public function deposit()
46
    {
47
        return $this->hasOne(Deposit::class);
48
    }
49
50
    public function deposit_real()
51
    {
52
        return $this->hasOne(DepositReal::class);
53
    }
54
55
    /**
56
     * @param  Builder  $query
57
     * @param  Session $session
58
     *
59
     * @return Builder
60
     */
61
    public function scopeWhereSession(Builder $query, Session $session): Builder
62
    {
63
        return $query->where('session_id', $session->id);
64
    }
65
66
    /**
67
     * @param  Builder  $query
68
     * @param  string $search
69
     *
70
     * @return Builder
71
     */
72
    public function scopeSearch(Builder $query, string $search): Builder
73
    {
74
        return $query->whereHas('subscription', fn(Builder $qs) => $qs
0 ignored issues
show
Bug Best Practice introduced by
The expression return $query->whereHas(...ion(...) { /* ... */ }) could return the type Illuminate\Database\Query\Builder which is incompatible with the type-hinted return Illuminate\Database\Eloquent\Builder. Consider adding an additional type-check to rule them out.
Loading history...
75
            ->whereHas('member', fn(Builder $qm) => $qm->search($search)));
76
    }
77
78
    /**
79
     * @param  Builder  $query
80
     *
81
     * @return Builder
82
     */
83
    public function scopePaid(Builder $query): Builder
84
    {
85
        return $query->whereHas('deposit');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $query->whereHas('deposit') could return the type Illuminate\Database\Query\Builder which is incompatible with the type-hinted return Illuminate\Database\Eloquent\Builder. Consider adding an additional type-check to rule them out.
Loading history...
86
    }
87
88
    /**
89
     * @param  Builder  $query
90
     * @param  Session $session
91
     *
92
     * @return Builder
93
     */
94
    public function scopePaidHere(Builder $query, Session $session): Builder
95
    {
96
        return $query->whereHas('deposit', fn(Builder $qd) =>
0 ignored issues
show
Bug Best Practice introduced by
The expression return $query->whereHas(...ion(...) { /* ... */ }) could return the type Illuminate\Database\Query\Builder which is incompatible with the type-hinted return Illuminate\Database\Eloquent\Builder. Consider adding an additional type-check to rule them out.
Loading history...
97
            $qd->whereSession($session));
98
    }
99
100
    /**
101
     * @param  Builder  $query
102
     * @param  Session  $session
103
     *
104
     * @return Builder
105
     */
106
    public function scopePaidEarlier(Builder $query, Session $session): Builder
107
    {
108
        return $query->whereHas('deposit', fn(Builder $dq) =>
0 ignored issues
show
Bug Best Practice introduced by
The expression return $query->whereHas(...ion(...) { /* ... */ }) could return the type Illuminate\Database\Query\Builder which is incompatible with the type-hinted return Illuminate\Database\Eloquent\Builder. Consider adding an additional type-check to rule them out.
Loading history...
109
            $dq->whereHas('session', fn(Builder $sq) =>
110
                $sq->precedes($session, true)));
111
    }
112
113
    /**
114
     * @param  Builder  $query
115
     * @param  Session  $session
116
     *
117
     * @return Builder
118
     */
119
    public function scopePaidLater(Builder $query, Session $session): Builder
120
    {
121
        return $query->whereHas('deposit', fn(Builder $dq) =>
0 ignored issues
show
Bug Best Practice introduced by
The expression return $query->whereHas(...ion(...) { /* ... */ }) could return the type Illuminate\Database\Query\Builder which is incompatible with the type-hinted return Illuminate\Database\Eloquent\Builder. Consider adding an additional type-check to rule them out.
Loading history...
122
            $dq->whereHas('session', fn(Builder $sq) =>
123
                $sq->succeedes($session, true)));
124
    }
125
126
    /**
127
     * @param  Builder  $query
128
     *
129
     * @return Builder
130
     */
131
    public function scopeUnpaid(Builder $query): Builder
132
    {
133
        return $query->whereDoesntHave('deposit');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $query->whereDoesntHave('deposit') could return the type Illuminate\Database\Query\Builder which is incompatible with the type-hinted return Illuminate\Database\Eloquent\Builder. Consider adding an additional type-check to rule them out.
Loading history...
134
    }
135
136
    /**
137
     * @param  Builder  $query
138
     * @param  Session $session
139
     *
140
     * @return Builder
141
     */
142
    public function scopePrecedes(Builder $query, Session $session): Builder
143
    {
144
        return $query
0 ignored issues
show
Bug Best Practice introduced by
The expression return $query->whereHas(...ion(...) { /* ... */ }) could return the type Illuminate\Database\Query\Builder which is incompatible with the type-hinted return Illuminate\Database\Eloquent\Builder. Consider adding an additional type-check to rule them out.
Loading history...
145
            ->whereHas('session', fn(Builder $sq) =>
146
                $sq->precedes($session, true));
147
    }
148
149
    /**
150
     * @param  Builder  $query
151
     * @param  Session $session
152
     *
153
     * @return Builder
154
     */
155
    public function scopeSucceedes(Builder $query, Session $session): Builder
156
    {
157
        return $query
0 ignored issues
show
Bug Best Practice introduced by
The expression return $query->whereHas(...ion(...) { /* ... */ }) could return the type Illuminate\Database\Query\Builder which is incompatible with the type-hinted return Illuminate\Database\Eloquent\Builder. Consider adding an additional type-check to rule them out.
Loading history...
158
            ->whereHas('session', fn(Builder $sq) =>
159
                $sq->succeedes($session, true));
160
    }
161
162
    /**
163
     * @param  Builder  $query
164
     * @param  Session $session
165
     *
166
     * @return Builder
167
     */
168
    public function scopeLate(Builder $query, Session $session): Builder
169
    {
170
        return $query->precedes($session)
171
            ->where(fn(Builder $oq) => $oq
172
                ->orWhere(fn(Builder $uq) => $uq->unpaid())
173
                ->orWhere(fn(Builder $pq) => $pq->paidHere($session)));
174
    }
175
176
    /**
177
     * @param  Builder  $query
178
     * @param  Session $session
179
     *
180
     * @return Builder
181
     */
182
    public function scopeEarly(Builder $query, Session $session): Builder
183
    {
184
        return $query->succeedes($session)
185
            ->where(fn(Builder $oq) => $oq
186
                ->orWhere(fn(Builder $uq) => $uq->unpaid())
187
                ->orWhere(fn(Builder $pq) => $pq->paidHere($session)));
188
    }
189
}
190