summaryrefslogtreecommitdiff
path: root/includes/model/Stats.php
blob: e355b0649ce4554006654758ef9ac6f7601fb81a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<?php

use Engelsystem\Database\Db;

/**
 * Returns the number of angels currently working.
 *
 * @return int|string
 */
function stats_currently_working()
{
    $result = Db::selectOne("
        SELECT SUM(
            (SELECT COUNT(*) FROM `ShiftEntry` WHERE `ShiftEntry`.`SID`=`Shifts`.`SID` AND `freeloaded`=0)
            ) AS `count`
        FROM `Shifts`
        WHERE (`end` >= ? AND `start` <= ?)", [
        time(),
        time()
    ]);

    if (empty($result['count'])) {
        return '-';
    }

    return $result['count'];
}

/**
 * Return the number of hours still to work.
 *
 * @return int|string
 */
function stats_hours_to_work()
{
    $result = Db::selectOne("
        SELECT ROUND(SUM(`count`)) AS `count` FROM (
            SELECT 
                (SELECT SUM(`count`) FROM `NeededAngelTypes` WHERE `NeededAngelTypes`.`shift_id`=`Shifts`.`SID`)
                * (`Shifts`.`end` - `Shifts`.`start`)/3600 AS `count`
            FROM `Shifts`
            LEFT JOIN schedule_shift AS s on Shifts.SID = s.shift_id
            WHERE `end` >= ?
            AND s.shift_id IS NULL
        
            UNION ALL
        
            SELECT
                (SELECT SUM(`count`) FROM `NeededAngelTypes` WHERE `NeededAngelTypes`.`room_id`=`Shifts`.`RID`)
                * (`Shifts`.`end` - `Shifts`.`start`)/3600 AS `count`
            FROM `Shifts`
            LEFT JOIN schedule_shift AS s on Shifts.SID = s.shift_id
            WHERE `end` >= ?
            AND NOT s.shift_id IS NULL
        ) AS `tmp`
        ", [
        time(),
        time()
    ]);
    if (empty($result['count'])) {
        return '-';
    }
    return $result['count'];
}

/**
 * Returns the number of needed angels in the next 3 hours
 *
 * @return int|string
 */
function stats_angels_needed_three_hours()
{
    $now = time();
    $in3hours = $now + 3 * 60 * 60;
    $result = Db::selectOne("
        SELECT SUM(`count`) AS `count` FROM (
            SELECT
                GREATEST(0,
                    (
                    SELECT SUM(`count`) 
                    FROM `NeededAngelTypes` 
                    JOIN `AngelTypes` ON `AngelTypes`.`id`=`NeededAngelTypes`.`angel_type_id` 
                    WHERE `AngelTypes`.`show_on_dashboard`=TRUE 
                        AND `NeededAngelTypes`.`shift_id`=`Shifts`.`SID`
                    ) - (
                    SELECT COUNT(*) FROM `ShiftEntry` 
                    JOIN `AngelTypes` ON `AngelTypes`.`id`=`ShiftEntry`.`TID` 
                    WHERE `AngelTypes`.`show_on_dashboard`=TRUE 
                        AND `ShiftEntry`.`SID`=`Shifts`.`SID` 
                        AND `freeloaded`=0
                    )
                )
                AS `count`
            FROM `Shifts`
            LEFT JOIN schedule_shift AS s on Shifts.SID = s.shift_id
            WHERE `end` > ? AND `start` < ?
            AND s.shift_id IS NULL
        
            UNION ALL
        
            SELECT
                GREATEST(0,
                    (
                    SELECT SUM(`count`) 
                    FROM `NeededAngelTypes` 
                    JOIN `AngelTypes` ON `AngelTypes`.`id`=`NeededAngelTypes`.`angel_type_id` 
                    WHERE `AngelTypes`.`show_on_dashboard`=TRUE 
                        AND `NeededAngelTypes`.`room_id`=`Shifts`.`RID`
                    ) - (
                    SELECT COUNT(*) FROM `ShiftEntry` 
                    JOIN `AngelTypes` ON `AngelTypes`.`id`=`ShiftEntry`.`TID` 
                    WHERE `AngelTypes`.`show_on_dashboard`=TRUE 
                        AND `ShiftEntry`.`SID`=`Shifts`.`SID` 
                        AND `freeloaded`=0
                    )
                )
                AS `count`
            FROM `Shifts`
            LEFT JOIN schedule_shift AS s on Shifts.SID = s.shift_id
            WHERE `end` > ? AND `start` < ?
            AND NOT s.shift_id IS NULL
        ) AS `tmp`", [
        $now,
        $in3hours,
        $now,
        $in3hours
    ]);
    if (empty($result['count'])) {
        return '-';
    }
    return $result['count'];
}

/**
 * Returns the number of needed angels for nightshifts (see config)
 *
 * @return int|string
 */
function stats_angels_needed_for_nightshifts()
{
    $nightShiftsConfig = config('night_shifts');
    $nightStartTime = $nightShiftsConfig['start'];
    $nightEndTime = $nightShiftsConfig['end'];

    $night_start = parse_date(
        'Y-m-d H:i',
        date('Y-m-d', time() + 12 * 60 * 60) . ' ' . $nightStartTime . ':00'
    );
    $night_end = $night_start + ($nightEndTime - $nightStartTime) * 60 * 60;
    $result = Db::selectOne("
        SELECT SUM(`count`) AS `count` FROM (
            SELECT
                GREATEST(0,
                    (
                    SELECT SUM(`count`) 
                    FROM `NeededAngelTypes` 
                    JOIN `AngelTypes` ON `AngelTypes`.`id`=`NeededAngelTypes`.`angel_type_id` 
                    WHERE `AngelTypes`.`show_on_dashboard`=TRUE 
                        AND `NeededAngelTypes`.`shift_id`=`Shifts`.`SID`
                    ) - (
                    SELECT COUNT(*) FROM `ShiftEntry` 
                    JOIN `AngelTypes` ON `AngelTypes`.`id`=`ShiftEntry`.`TID` 
                    WHERE `AngelTypes`.`show_on_dashboard`=TRUE 
                        AND `ShiftEntry`.`SID`=`Shifts`.`SID` 
                        AND `freeloaded`=0
                    )
                )
                AS `count`
            FROM `Shifts`
            LEFT JOIN schedule_shift AS s on Shifts.SID = s.shift_id
            WHERE `end` > ? AND `start` < ?
            AND s.shift_id IS NULL
        
            UNION ALL
        
            SELECT
                GREATEST(0,
                    (
                    SELECT SUM(`count`) 
                    FROM `NeededAngelTypes` 
                    JOIN `AngelTypes` ON `AngelTypes`.`id`=`NeededAngelTypes`.`angel_type_id` 
                    WHERE `AngelTypes`.`show_on_dashboard`=TRUE 
                        AND `NeededAngelTypes`.`room_id`=`Shifts`.`RID`
                    ) - (
                    SELECT COUNT(*) FROM `ShiftEntry` 
                    JOIN `AngelTypes` ON `AngelTypes`.`id`=`ShiftEntry`.`TID` 
                    WHERE `AngelTypes`.`show_on_dashboard`=TRUE 
                        AND `ShiftEntry`.`SID`=`Shifts`.`SID` 
                        AND `freeloaded`=0
                    )
                )
                AS `count`
            FROM `Shifts`
            LEFT JOIN schedule_shift AS s on Shifts.SID = s.shift_id
            WHERE `end` > ? AND `start` < ?
            AND NOT s.shift_id IS NULL
        ) AS `tmp`", [
        $night_start,
        $night_end,
        $night_start,
        $night_end
    ]);
    if (empty($result['count'])) {
        return '-';
    }
    return $result['count'];
}