Lines 100.00% 8 / 8
Functions and Methods 100.00% 1 / 1
Classes and Traits 100.00% 1 / 1
Name Lines Functions and Methods CRAP Classes and Traits
ProcessStatusFree 100.00% 8 / 8 100.00% 1 / 1 2 100.00% 1 / 1
 buildDaysCondition 100.00% 8 / 8 100.00% 1 / 1 2
1<?php
2
3namespace BO\Zmsbackend\Process\Repository;
4
5class ProcessStatusFree extends \BO\Zmsbackend\Query\Base
6{
7    /**
8     * Shared free-process query used by zmsadmin / classic free-slot APIs.
9     * Count bookings via the slot_process primary key for each required hierarchy slot,
10     * then aggregate once per starting slot. The sequence join preserves the number of
11     * simultaneous appointments offered by a free slot.
12     *
13     * see also \BO\Zmsbackend\Day\Repository\Day::QUERY_DAYLIST_JOIN
14     */
15    const string QUERY_SELECT_PROCESSLIST_DAYS = '
16        SELECT
17            "free" AS status,
18            CONCAT(year, "-", month, "-", day, " ", time) AS appointments__0__date,
19            slotsRequired AS appointments__0__slotCount,
20            scopeID AS scope__id
21        FROM (
22            SELECT s.year, s.month, s.day, s.time, s.scopeID,
23                IF(a.multiple_slots_allowed, c.slotsRequired, :forceRequiredSlots) AS slotsRequired,
24                COUNT(DISTINCT h.slotID) AS ancestorCount,
25                IFNULL(GREATEST(0, MIN(
26                    CAST(IF(:slotType = "intern", s.intern, IF(:slotType = "public", s.`public`, 0)) AS SIGNED)
27                    - (SELECT COUNT(*) FROM slot_process p WHERE p.slotID = h.slotID)
28                )), 0) AS free
29            FROM
30                calendarscope c
31                INNER JOIN slot s
32                    ON c.scopeID = s.scopeID
33                        %s
34                        AND s.status = "free"
35                LEFT JOIN oeffnungszeit a ON s.availabilityID = a.OeffnungszeitID
36                INNER JOIN slot_hiera h ON h.ancestorID = s.slotID
37                    AND h.ancestorLevel <= IF(a.multiple_slots_allowed, c.slotsRequired, :forceRequiredSlots)
38                INNER JOIN slot s2 ON h.slotID = s2.slotID AND s2.status = "free"
39            WHERE NOT EXISTS (
40                SELECT 1 FROM closures cc
41                WHERE cc.StandortID = s.scopeID AND cc.year = s.year
42                    AND cc.month = s.month AND cc.day = s.day
43            )
44            ' . \BO\Zmsbackend\Day\Repository\Day::QUERY_ON_APPOINTMENT_INTERVAL . '
45            GROUP BY s.slotID
46            HAVING ancestorCount >= slotsRequired
47        ) AS available
48        INNER JOIN slot_sequence sq ON sq.slotsequence <= available.free
49    ';
50
51    /**
52     * Citizen calendar-availability free-process query only.
53     *
54     * Occupancy is pre-aggregated from slot_process (same approach as Day::QUERY_DAYLIST_JOIN_AVAILABILITY).
55     *
56     * see also \BO\Zmsbackend\Day\Repository\Day::QUERY_DAYLIST_JOIN_AVAILABILITY
57     */
58    const string QUERY_SELECT_PROCESSLIST_DAYS_AVAILABILITY = '
59        SELECT
60            -- tmp_avail.*,
61            "free" AS status,
62            CONCAT(year, "-", month, "-", day, " ", time) AS appointments__0__date,
63            slotsRequired AS appointments__0__slotCount,
64            scopeID AS scope__id
65        FROM
66            (SELECT
67               COUNT(slotID) as ancestorCount,
68               IF(MIN(available - confirmed) > 0, MIN(available - confirmed), 0) as free,
69               tmp_ancestor.*
70            FROM (SELECT
71                IFNULL(occ.confirmed, 0) confirmed,
72                IF(:slotType = "intern", s.intern,
73                    IF(:slotType = "public", s.`public`, 0)
74                    ) available,
75                IF(a.multiple_slots_allowed, c.slotsRequired, :forceRequiredSlots) slotsRequired,
76                s.*,
77                cc.id
78            FROM
79                calendarscope c
80                INNER JOIN slot s
81                    ON c.scopeID = s.scopeID
82                        %s
83                        AND s.status = "free"
84                LEFT JOIN oeffnungszeit a ON s.availabilityID = a.OeffnungszeitID
85                LEFT JOIN slot_hiera h ON h.ancestorID = s.slotID
86                    AND h.ancestorLevel <= IF(a.multiple_slots_allowed, c.slotsRequired, :forceRequiredSlots)
87                INNER JOIN slot s2 on h.slotID = s2.slotID and s2.status = "free"
88                LEFT JOIN (
89                    SELECT p.slotID, COUNT(*) AS confirmed
90                    FROM slot_process p
91                    INNER JOIN slot s_occ
92                        ON s_occ.slotID = p.slotID
93                    INNER JOIN calendarscope c_occ
94                        ON c_occ.scopeID = s_occ.scopeID
95                        AND c_occ.year = s_occ.year
96                        AND c_occ.month = s_occ.month
97                    GROUP BY p.slotID
98                ) occ ON occ.slotID = h.slotID
99                LEFT JOIN closures cc ON (s.scopeID = cc.StandortID AND s.year = cc.year AND s.month = cc.month and s.day = cc.day)
100            WHERE cc.id IS NULL
101            ' . \BO\Zmsbackend\Day\Repository\Day::QUERY_ON_APPOINTMENT_INTERVAL . '
102            ) AS tmp_ancestor
103            GROUP BY slotID
104            HAVING ancestorCount >= slotsRequired
105            ) AS tmp_avail 
106            INNER JOIN slot_sequence sq ON sq.slotsequence <= tmp_avail.free
107    ';
108
109    const string GROUPBY_SELECT_PROCESSLIST_DAY = 'GROUP BY scope__id, appointments__0__date';
110
111    public static function buildDaysCondition(array $days): string
112    {
113        $sql = 'AND (';
114        $sqlPats = [];
115
116        foreach ($days as $day) {
117            $sqlPats[] = '(c.year = ' . $day->format('Y') . '
118                        AND c.month = ' . $day->format('m') . '
119                        AND s.day = ' . $day->format('d') . '
120                        AND c.year = s.year
121                        AND c.month = s.month)';
122        }
123
124        return $sql . implode(' OR ', $sqlPats) . ')';
125    }
126}