LibreFeeService::getSessionBills()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Siak\Tontine\Service\Meeting\Charge;
4
5
use Illuminate\Support\Collection;
6
use Illuminate\Support\Facades\DB;
7
use Siak\Tontine\Model\Bill;
8
use Siak\Tontine\Model\Round;
9
use Siak\Tontine\Model\Session;
10
use Siak\Tontine\Service\LocaleService;
11
use Siak\Tontine\Service\Meeting\Session\SessionService;
12
use Siak\Tontine\Service\TenantService;
13
14
class LibreFeeService
15
{
16
    /**
17
     * @param LocaleService $localeService
18
     * @param TenantService $tenantService
19
     * @param SessionService $sessionService
20
     */
21
    public function __construct(protected LocaleService $localeService,
22
        protected TenantService $tenantService, protected SessionService $sessionService)
23
    {}
24
25
    /**
26
     * Get a paginated list of fees.
27
     *
28
     * @param Round $round
29
     * @param int $page
30
     *
31
     * @return Collection
32
     */
33
    public function getFees(Round $round, int $page = 0): Collection
34
    {
35
        return $round->charges()->variable()
36
            ->orderBy('id', 'desc')
37
            ->page($page, $this->tenantService->getLimit())
38
            ->get();
39
    }
40
41
    /**
42
     * Get the number of fees.
43
     *
44
     * @param Round $round
45
     *
46
     * @return int
47
     */
48
    public function getFeeCount(Round $round): int
49
    {
50
        return $round->charges()->variable()->count();
51
    }
52
53
    /**
54
     * @param Session $session
55
     *
56
     * @return Collection
57
     */
58
    private function getSessionBills(Session $session): Collection
59
    {
60
        return Bill::ofSession($session)->libre()
61
            ->select('charge_id', DB::raw('count(*) as total'), DB::raw('sum(amount) as amount'))
62
            ->groupBy('charge_id')
63
            ->get();
64
    }
65
66
    /**
67
     * @param Session $session
68
     *
69
     * @return Collection
70
     */
71
    private function getSessionSettlements(Session $session): Collection
72
    {
73
        return Bill::ofSession($session)->libre()
74
            ->select('charge_id', DB::raw('count(*) as total'), DB::raw('sum(amount) as amount'))
75
            ->whereHas('settlement', function($query) use($session) {
76
                $query->where('session_id', $session->id);
77
            })
78
            ->groupBy('charge_id')
79
            ->get();
80
    }
81
82
    /**
83
     * @param Session $session
84
     *
85
     * @return Collection
86
     */
87
    private function getRoundBills(Session $session): Collection
88
    {
89
        return Bill::ofRound($session)->libre()
90
            ->select('charge_id', DB::raw('count(*) as total'), DB::raw('sum(amount) as amount'))
91
            ->groupBy('charge_id')
92
            ->get();
93
    }
94
95
    /**
96
     * @param Session $session
97
     *
98
     * @return Collection
99
     */
100
    private function getRoundSettlements(Session $session): Collection
101
    {
102
        return Bill::ofRound($session)->libre()
103
            ->select('charge_id', DB::raw('count(*) as total'), DB::raw('sum(amount) as amount'))
104
            ->whereHas('settlement', function($query) use($session) {
105
                $query->whereHas('session', function($query) use($session) {
106
                    $query->where('id', '!=', $session->id)
107
                        ->where('round_id', $session->round_id);
108
                });
109
            })
110
            ->groupBy('charge_id')
111
            ->get();
112
    }
113
114
    /**
115
     * Get the report of bills
116
     *
117
     * @param Session $session
118
     *
119
     * @return array
120
     */
121
    public function getBills(Session $session): array
122
    {
123
        $sessionBills = $this->getSessionBills($session);
124
        $roundBills = $this->getRoundBills($session);
125
        return [
126
            'total' => [
127
                'session' => $sessionBills->pluck('total', 'charge_id'),
128
                'round' => $roundBills->pluck('total', 'charge_id'),
129
            ],
130
        ];
131
    }
132
133
    /**
134
     * Get the report of settlements
135
     *
136
     * @param Session $session
137
     *
138
     * @return array
139
     */
140
    public function getSettlements(Session $session): array
141
    {
142
        $sessionSettlements = $this->getSessionSettlements($session);
143
        $roundSettlements = $this->getRoundSettlements($session);
144
        return [
145
            'total' => [
146
                'session' => $sessionSettlements->pluck('total', 'charge_id'),
147
                'round' => $roundSettlements->pluck('total', 'charge_id'),
148
            ],
149
            'amount' => [
150
                'session' => $sessionSettlements->pluck('amount', 'charge_id'),
151
                'round' => $roundSettlements->pluck('amount', 'charge_id'),
152
            ],
153
        ];
154
    }
155
}
156