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 BillView extends Base |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* The table associated with the model. |
12
|
|
|
* |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
protected $table = 'v_bills'; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Indicates if the model should be timestamped. |
19
|
|
|
* |
20
|
|
|
* @var bool |
21
|
|
|
*/ |
22
|
|
|
public $timestamps = false; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @return Attribute |
26
|
|
|
*/ |
27
|
|
|
protected function inSession(): Attribute |
28
|
|
|
{ |
29
|
|
|
return Attribute::make( |
30
|
|
|
get: fn() => $this->bill_type === Bill::TYPE_LIBRE || |
|
|
|
|
31
|
|
|
$this->bill_type === Bill::TYPE_SESSION, |
32
|
|
|
); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @return Attribute |
37
|
|
|
*/ |
38
|
|
|
protected function inRound(): Attribute |
39
|
|
|
{ |
40
|
|
|
return Attribute::make( |
41
|
|
|
get: fn() => $this->bill_type === Bill::TYPE_ROUND || |
|
|
|
|
42
|
|
|
$this->bill_type === Bill::TYPE_ONETIME, |
43
|
|
|
); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function round() |
47
|
|
|
{ |
48
|
|
|
return $this->belongsTo(Round::class); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function session() |
52
|
|
|
{ |
53
|
|
|
return $this->belongsTo(Session::class); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function charge() |
57
|
|
|
{ |
58
|
|
|
return $this->belongsTo(Charge::class); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function member() |
62
|
|
|
{ |
63
|
|
|
return $this->belongsTo(Member::class); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function bill() |
67
|
|
|
{ |
68
|
|
|
return $this->belongsTo(Bill::class); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param Builder $query |
73
|
|
|
* @param Session $session |
74
|
|
|
* |
75
|
|
|
* @return Builder |
76
|
|
|
*/ |
77
|
|
|
public function scopeOfTypeSession(Builder $query, Session $session): Builder |
78
|
|
|
{ |
79
|
|
|
return $query->where('bill_type', Bill::TYPE_SESSION) |
80
|
|
|
->where('session_id', $session->id); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param Builder $query |
85
|
|
|
* @param Session $session |
86
|
|
|
* |
87
|
|
|
* @return Builder |
88
|
|
|
*/ |
89
|
|
|
public function scopeOfTypeNotSession(Builder $query, Session $session): Builder |
90
|
|
|
{ |
91
|
|
|
return $query->where('bill_type', '!=', Bill::TYPE_SESSION) |
92
|
|
|
->where('round_id', $session->round_id) |
93
|
|
|
->whereHas('session', fn(Builder $qs) => |
94
|
|
|
$qs->where('day_date', '<=', $session->day_date)) |
95
|
|
|
->whereHas('bill', fn(Builder $qb) => |
96
|
|
|
// Take unsettled bills, or bills settled on this session.. |
97
|
|
|
$qb->where(fn(Builder $qs) => $qs |
98
|
|
|
->orWhereDoesntHave('settlement') |
99
|
|
|
->orWhereHas('settlement', fn(Builder $qt) => |
100
|
|
|
$qt->where('session_id', $session->id)))); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.