RoundService::getRoundSessions()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Siak\Tontine\Service\Report;
4
5
use Illuminate\Support\Collection;
6
use Illuminate\Support\Facades\DB;
7
use Siak\Tontine\Model\Debt;
8
use Siak\Tontine\Model\Round;
9
10
class RoundService
11
{
12
    use Traits\Queries;
13
14
    /**
15
     * @param Round $round
16
     *
17
     * @return Collection
18
     */
19
    public function getRoundSessions(Round $round): Collection
20
    {
21
        return $round->sessions()->active()->orderBy('day_date', 'asc')->get();
22
    }
23
24
    /**
25
     * @param Collection $sessionIds
26
     *
27
     * @return Collection
28
     */
29
    public function getSettlementAmounts(Collection $sessionIds): Collection
30
    {
31
        return DB::table('settlements')
32
            ->join('bills', 'settlements.bill_id', '=', 'bills.id')
33
            ->select(DB::raw('sum(bills.amount) as total_amount'), 'settlements.session_id')
34
            ->whereIn('settlements.session_id', $sessionIds)
35
            ->groupBy('settlements.session_id')
36
            ->pluck('total_amount', 'session_id');
37
    }
38
39
    /**
40
     * @param Collection $sessionIds
41
     *
42
     * @return Collection
43
     */
44
    public function getLoanAmounts(Collection $sessionIds): Collection
45
    {
46
        return DB::table('debts')
47
            ->join('loans', 'loans.id', '=', 'debts.loan_id')
48
            ->select(DB::raw('sum(debts.amount) as total_amount'), 'loans.session_id')
49
            ->where('debts.type', Debt::TYPE_PRINCIPAL)
50
            ->whereIn('loans.session_id', $sessionIds)
51
            ->groupBy('loans.session_id')
52
            ->pluck('total_amount', 'session_id');
53
    }
54
55
    /**
56
     * @param Collection $sessionIds
57
     *
58
     * @return Collection
59
     */
60
    public function getRefundAmounts(Collection $sessionIds): Collection
61
    {
62
        $refunds = $this->getRefundQuery()
63
            ->addSelect('refunds.session_id')
64
            ->groupBy('refunds.session_id')
65
            ->whereIn('refunds.session_id', $sessionIds)
66
            ->pluck('total_amount', 'session_id');
67
        DB::table('partial_refunds')
68
            ->whereIn('session_id', $sessionIds)
69
            ->select('session_id', DB::raw('sum(amount) as total_amount'))
70
            ->groupBy('session_id')
71
            ->get()
72
            // Merge into refunds
73
            ->each(function($partialRefund) use($refunds) {
74
                $refunds[$partialRefund->session_id] = $partialRefund->total_amount +
75
                    ($refunds[$partialRefund->session_id] ?? 0);
76
            });
77
78
        return $refunds;
79
    }
80
81
    /**
82
     * @param Collection $sessionIds
83
     *
84
     * @return Collection
85
     */
86
    public function getAuctionAmounts(Collection $sessionIds): Collection
87
    {
88
        return DB::table('auctions')
89
            ->select(DB::raw('sum(amount) as total_amount'), 'session_id')
90
            ->where('paid', true)
91
            ->whereIn('session_id', $sessionIds)
92
            ->groupBy('session_id')
93
            ->pluck('total_amount', 'session_id');
94
    }
95
96
    /**
97
     * @param Collection $sessionIds
98
     *
99
     * @return Collection
100
     */
101
    public function getSavingAmounts(Collection $sessionIds): Collection
102
    {
103
        return DB::table('savings')
104
            ->select(DB::raw('sum(amount) as total_amount'), 'session_id')
105
            ->whereIn('session_id', $sessionIds)
106
            ->groupBy('session_id')
107
            ->pluck('total_amount', 'session_id');
108
    }
109
110
    /**
111
     * @param Collection $sessionIds
112
     *
113
     * @return Collection
114
     */
115
    public function getOutflowAmounts(Collection $sessionIds): Collection
116
    {
117
        return DB::table('outflows')
118
            ->select(DB::raw('sum(amount) as total_amount'), 'session_id')
119
            ->whereIn('session_id', $sessionIds)
120
            ->groupBy('session_id')
121
            ->pluck('total_amount', 'session_id');
122
    }
123
}
124