Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
77.78% covered (warning)
77.78%
14 / 18
33.33% covered (danger)
33.33%
1 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
Slot
77.78% covered (warning)
77.78%
14 / 18
33.33% covered (danger)
33.33%
1 / 3
6.40
0.00% covered (danger)
0.00%
0 / 1
 getEntityMapping
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 reverseEntityMapping
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
1 / 1
4
 addConditionSlotId
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace BO\Zmsdb\Query;
4
5class Slot extends Base implements MappingInterface
6{
7    /**
8     *
9     * @var String TABLE mysql table reference
10     */
11    const TABLE = 'slot';
12
13    const QUERY_OPTIMIZE_SLOT = 'OPTIMIZE TABLE slot;';
14    const QUERY_OPTIMIZE_SLOT_HIERA = 'OPTIMIZE TABLE slot_hiera;';
15    const QUERY_OPTIMIZE_SLOT_PROCESS = 'OPTIMIZE TABLE slot_proces;';
16    const QUERY_OPTIMIZE_PROCESS = 'OPTIMIZE TABLE buerger;';
17
18    const QUERY_LAST_CHANGED = 'SELECT MAX(updateTimestamp) AS dateString FROM slot;';
19
20    const QUERY_LAST_CHANGED_AVAILABILITY = '
21        SELECT MAX(updateTimestamp) AS dateString FROM slot WHERE availabilityID = :availabilityID AND status="free";';
22
23    const QUERY_LAST_IN_AVAILABILITY = '
24        SELECT CONCAT(year, "-", LPAD(month, 2, "0"), "-", LPAD(day, 2, "0")) AS dateString
25        FROM slot
26        WHERE availabilityID = :availabilityID AND status="free"
27        ORDER BY year DESC, month DESC, day DESC
28        LIMIT 1;';
29
30    const QUERY_LAST_CHANGED_SCOPE = '
31        SELECT MAX(updateTimestamp) AS dateString FROM slot WHERE scopeID = :scopeID;';
32
33    const QUERY_INSERT_SLOT_PROCESS = '
34        INSERT INTO slot_process
35        VALUES(?,?,?) 
36    ';
37
38    const QUERY_SELECT_BY_SCOPE_AND_DAY = '
39        SELECT
40            s.*
41        FROM slot s
42        WHERE
43            s.scopeID = :scopeID
44            AND s.year = :year
45            AND s.month = :month
46            AND s.day = :day
47    ';
48
49    const QUERY_SELECT_MISSING_PROCESS = '
50        SELECT 
51          s.slotID,
52          b.BuergerID,
53          NOW() updateTimestamp
54        FROM slot s
55          INNER JOIN buerger b ON
56            s.year = YEAR(b.Datum)
57            AND s.month = MONTH(b.Datum)
58            AND s.day = DAY(b.Datum)
59            AND s.scopeID = b.StandortID
60            AND b.Uhrzeit BETWEEN s.time AND SEC_TO_TIME(TIME_TO_SEC(s.time) + (s.slotTimeInMinutes * 60) - 1)
61            AND s.status = "free"
62          LEFT JOIN slot_process sp ON b.BuergerID = sp.processID
63        WHERE
64          sp.processID IS NULL
65    ';
66    const QUERY_SELECT_MISSING_PROCESS_BY_SCOPE = '
67          AND s.scopeID = :scopeID
68    ';
69
70
71    const QUERY_INSERT_SLOT_PROCESS_ID = '
72        REPLACE INTO slot_process
73        SELECT 
74          s.slotID,
75          b.BuergerID,
76          NOW()
77        FROM slot s
78          INNER JOIN buerger b ON
79            s.year = YEAR(b.Datum)
80            AND s.month = MONTH(b.Datum)
81            AND s.day = DAY(b.Datum)
82            AND s.scopeID = b.StandortID
83            AND s.status = "free"
84            AND b.Uhrzeit BETWEEN s.time AND SEC_TO_TIME(TIME_TO_SEC(s.time) + (s.slotTimeInMinutes * 60) - 1)
85        WHERE
86          b.BuergerID = :processId
87    ';
88    const QUERY_DELETE_SLOT_PROCESS_CANCELLED = '
89        DELETE sp 
90            FROM slot_process sp LEFT JOIN slot s USING (slotID)
91            WHERE (s.status = "cancelled" OR s.status IS NULL)
92    ';
93    const QUERY_DELETE_SLOT_PROCESS_CANCELLED_BY_SCOPE = '
94                AND s.scopeID = :scopeID
95    ';
96
97
98    const QUERY_UPDATE_SLOT_MISSING_AVAILABILITY_BY_SCOPE = '
99    UPDATE
100         slot s
101           LEFT JOIN oeffnungszeit a ON s.availabilityID = a.OeffnungszeitID
102           SET s.status = "cancelled"
103           WHERE
104             (
105               a.OeffnungszeitID IS NULL
106               OR a.Endedatum < :dateString
107             )
108             AND s.scopeID = :scopeID
109    ';
110
111    const QUERY_UPDATE_SLOT_MISSING_AVAILABILITY = '
112    UPDATE
113         slot s
114           LEFT JOIN oeffnungszeit a ON s.availabilityID = a.OeffnungszeitID
115           SET s.status = "cancelled"
116           WHERE
117             a.OeffnungszeitID IS NULL
118               OR a.Endedatum < :dateString
119    ';
120
121    const QUERY_SELECT_DELETABLE_SLOT_PROCESS = '
122        SELECT sp.processID AS processId
123            FROM slot_process sp
124              LEFT JOIN buerger b ON sp.processID = b.BuergerID
125              LEFT JOIN slot s ON sp.slotID = s.slotID
126            WHERE (
127                b.BuergerID IS NULL
128                OR (
129                  b.updateTimestamp > sp.updateTimestamp
130                  AND (
131                    b.Uhrzeit NOT BETWEEN s.time AND SEC_TO_TIME(TIME_TO_SEC(s.time) + (s.slotTimeInMinutes * 60) - 1)
132                    OR s.month != MONTH(b.Datum)
133                    OR s.day != DAY(b.Datum)
134                    OR s.scopeID != b.StandortID
135                  )
136                )
137              ) 
138    ';
139    const QUERY_SELECT_DELETABLE_SLOT_PROCESS_BY_SCOPE = '
140              AND b.StandortID = :scopeID
141    ';
142
143    const QUERY_DELETE_SLOT_PROCESS_ID = '
144        DELETE sp 
145            FROM slot_process sp 
146            WHERE sp.processID = :processId
147    ';
148
149    const QUERY_UPDATE_SLOT_STATUS = "
150        UPDATE slot
151          LEFT JOIN (
152          SELECT s.slotID,
153          IF(s.status IN ('free', 'full'), IF(IFNULL(COUNT(p.slotID), 0) < intern, 'free', 'full'), s.status) newstatus
154          FROM slot s
155            LEFT JOIN slot_process p ON s.slotID = p.slotID
156          GROUP BY s.slotID
157          ) calc ON slot.slotID = calc.slotID
158        SET
159          slot.status = calc.newstatus
160        WHERE slot.status != calc.newstatus
161";
162
163    const QUERY_SELECT_SLOT = '
164    SELECT slotID FROM slot WHERE
165      scopeID = :scopeID
166      AND year = :year
167      AND month = :month
168      AND day = :day
169      AND time = :time
170      AND availabilityID = :availabilityID
171    LIMIT 1
172';
173
174    const QUERY_INSERT_ANCESTOR = '
175    INSERT INTO slot_hiera SET slotID = :slotID, ancestorID = :ancestorID, ancestorLevel = :ancestorLevel
176';
177
178    const QUERY_DELETE_ANCESTOR = '
179    DELETE FROM slot_hiera WHERE slotID = :slotID
180';
181
182    const QUERY_CANCEL_AVAILABILITY = '
183        UPDATE slot SET status = "cancelled" WHERE availabilityID = :availabilityID
184';
185
186    const QUERY_CANCEL_SLOT_OLD_BY_SCOPE = '
187    UPDATE slot SET status =  "cancelled" 
188        WHERE scopeID = :scopeID AND (
189            (year < :year)
190            OR (year = :year AND  month < :month) 
191            OR (year = :year AND  month = :month AND  day <= :day AND time < :time)
192        )
193';
194
195    const QUERY_CANCEL_SLOT_OLD = '
196    UPDATE slot SET status =  "cancelled" 
197        WHERE (year < :year)
198            OR (year = :year AND  month < :month) 
199            OR (year = :year AND  month = :month AND  day <= :day AND time < :time)
200';
201
202    const QUERY_DELETE_SLOT_OLD = '
203    DELETE FROM slot 
204        WHERE (year < :year) 
205            OR (year = :year AND  month < :month) 
206            OR (year = :year AND  month = :month AND  day < :day)
207';
208
209    const QUERY_DELETE_SLOT_HIERA = '
210        DELETE sh 
211            FROM slot_hiera sh LEFT JOIN slot s USING(slotID)
212            WHERE s.slotID IS NULL
213    ';
214
215
216    public function getEntityMapping()
217    {
218        return [
219        ];
220    }
221
222    public function reverseEntityMapping(
223        \BO\Zmsentities\Slot $slot,
224        \BO\Zmsentities\Availability $availability,
225        \DateTimeInterface $date
226    ) {
227        $data = array();
228        $data['scopeID'] = $availability->scope->id;
229        $data['availabilityID'] = $availability->id;
230        $data['year'] = $date->format('Y');
231        $data['month'] = $date->format('m');
232        $data['day'] = $date->format('d');
233        $data['time'] = $slot->getTimeString();
234        $data['public'] = isset($slot['public']) ? $slot['public'] : $availability->workstationCount['public'];
235        $data['callcenter'] = isset($slot['callcenter']) ?
236            $slot['callcenter'] : $availability->workstationCount['callcenter'];
237        $data['intern'] = isset($slot['intern']) ? $slot['intern'] : $availability->workstationCount['intern'];
238        $data['status'] = $slot->status;
239        $data['slotTimeInMinutes'] = $availability->slotTimeInMinutes;
240        return $data;
241    }
242
243    public function addConditionSlotId($slotID)
244    {
245        $this->query->where('slot.slotID', '=', $slotID);
246        return $this;
247    }
248}