Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
98 / 98 |
|
100.00% |
9 / 9 |
CRAP | |
100.00% |
1 / 1 |
DayOff | |
100.00% |
98 / 98 |
|
100.00% |
9 / 9 |
34 | |
100.00% |
1 / 1 |
readByDepartmentId | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
4 | |||
readOnlyByDepartmentId | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
4 | |||
readCommon | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
5 | |||
readByScopeId | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
4 | |||
readByYear | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
4 | |||
readCommonByYear | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
4 | |||
writeCommonDayoffsByYear | |
100.00% |
18 / 18 |
|
100.00% |
1 / 1 |
5 | |||
deleteByTimeInterval | |
100.00% |
15 / 15 |
|
100.00% |
1 / 1 |
3 | |||
deleteEntity | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | namespace BO\Zmsdb; |
4 | |
5 | use BO\Zmsentities\Dayoff as Entity; |
6 | use BO\Zmsentities\Collection\DayoffList as Collection; |
7 | |
8 | class DayOff extends Base |
9 | { |
10 | /** |
11 | * common DayOff like Xmas... |
12 | * |
13 | */ |
14 | public static $commonList = null; |
15 | |
16 | public function readByDepartmentId($departmentId = 0) |
17 | { |
18 | $dayOffList = $this->readCommon(); |
19 | $departmentDayoffList = $this->readOnlyByDepartmentId($departmentId); |
20 | if (count($departmentDayoffList)) { |
21 | foreach ($departmentDayoffList as $entity) { |
22 | if ($entity instanceof Entity) { |
23 | $dayOffList->addEntity($entity); |
24 | } |
25 | } |
26 | } |
27 | return $dayOffList; |
28 | } |
29 | |
30 | public function readOnlyByDepartmentId($departmentId = 0) |
31 | { |
32 | $dayOffList = new Collection(); |
33 | $query = new Query\DayOff(Query\Base::SELECT); |
34 | $query->addEntityMapping() |
35 | ->addConditionDepartmentId($departmentId); |
36 | $result = $this->fetchList($query, new Entity()); |
37 | if (count($result)) { |
38 | foreach ($result as $entity) { |
39 | if ($entity instanceof Entity) { |
40 | $dayOffList->addEntity($entity); |
41 | } |
42 | } |
43 | } |
44 | return $dayOffList; |
45 | } |
46 | |
47 | public function readCommon() |
48 | { |
49 | if (static::$commonList === null) { |
50 | $dayOffList = new Collection(); |
51 | $query = new Query\DayOff(Query\Base::SELECT); |
52 | $query->addEntityMapping() |
53 | ->addConditionCommon(); |
54 | $result = $this->fetchList($query, new Entity()); |
55 | if (count($result)) { |
56 | foreach ($result as $entity) { |
57 | if ($entity instanceof Entity) { |
58 | $dayOffList->addEntity($entity); |
59 | } |
60 | } |
61 | } |
62 | static::$commonList = $dayOffList; |
63 | } |
64 | return clone static::$commonList; |
65 | } |
66 | |
67 | public function readByScopeId($scopeId = 0) |
68 | { |
69 | $dayOffList = $this->readCommon(); |
70 | $query = new Query\DayOff(Query\Base::SELECT); |
71 | $query->addEntityMapping() |
72 | ->addConditionScopeId($scopeId); |
73 | $result = $this->fetchList($query, new Entity()); |
74 | if (count($result)) { |
75 | foreach ($result as $entity) { |
76 | if ($entity instanceof Entity) { |
77 | $dayOffList->addEntity($entity); |
78 | } |
79 | } |
80 | } |
81 | return $dayOffList; |
82 | } |
83 | |
84 | public function readByYear($year) |
85 | { |
86 | $dayOffList = new Collection(); |
87 | $query = new Query\DayOff(Query\Base::SELECT); |
88 | $query |
89 | ->addEntityMapping() |
90 | ->addConditionYear($year); |
91 | $result = $this->fetchList($query, new Entity()); |
92 | if (count($result)) { |
93 | foreach ($result as $entity) { |
94 | if ($entity instanceof Entity) { |
95 | $dayOffList->addEntity($entity); |
96 | } |
97 | } |
98 | } |
99 | return $dayOffList; |
100 | } |
101 | |
102 | public function readCommonByYear($year) |
103 | { |
104 | $dayOffList = new Collection(); |
105 | $query = new Query\DayOff(Query\Base::SELECT); |
106 | $query |
107 | ->addEntityMapping() |
108 | ->addConditionCommon() |
109 | ->addConditionYear($year); |
110 | $result = $this->fetchList($query, new Entity()); |
111 | if (count($result)) { |
112 | foreach ($result as $entity) { |
113 | if ($entity instanceof Entity) { |
114 | $dayOffList->addEntity($entity); |
115 | } |
116 | } |
117 | } |
118 | return $dayOffList; |
119 | } |
120 | |
121 | /** |
122 | * create dayoff preferences of a department |
123 | * |
124 | * @param |
125 | * dayoffList, |
126 | * year, |
127 | * drop |
128 | * |
129 | * @return Collection dayoffList |
130 | */ |
131 | public function writeCommonDayoffsByYear($dayoffList, $year = null, $drop = true) |
132 | { |
133 | if ($drop && $year) { |
134 | static::$commonList = null; |
135 | $deleteQuery = new Query\DayOff(Query\Base::DELETE); |
136 | $deleteQuery |
137 | ->addConditionYear($year) |
138 | ->addConditionCommon(); |
139 | $this->deleteItem($deleteQuery); |
140 | } |
141 | $query = new Query\DayOff(Query\Base::INSERT); |
142 | foreach ($dayoffList as $dayoff) { |
143 | $query->addValues( |
144 | [ |
145 | 'behoerdenid' => 0, //all departments |
146 | 'Feiertag' => $dayoff['name'], |
147 | 'Datum' => (new \DateTimeImmutable())->setTimestamp($dayoff['date'])->format('Y-m-d') |
148 | ] |
149 | ); |
150 | $this->writeItem($query); |
151 | } |
152 | return ($year) ? $this->readCommonByYear($year) : $dayoffList; |
153 | } |
154 | |
155 | /** |
156 | * delete dayoff preferences by time interval |
157 | * |
158 | * @param |
159 | * deleteInSeconds |
160 | * |
161 | * @return boolean |
162 | */ |
163 | public function deleteByTimeInterval($deleteInSeconds) |
164 | { |
165 | $selectQuery = new Query\DayOff(Query\Base::SELECT); |
166 | $selectQuery |
167 | ->addEntityMapping() |
168 | ->addConditionDayoffDeleteInterval($deleteInSeconds); |
169 | $statement = $this->fetchStatement($selectQuery); |
170 | while ($dayoffData = $statement->fetch(\PDO::FETCH_ASSOC)) { |
171 | $dayoffData = (new Query\DayOff(Query\Base::SELECT))->postProcess($dayoffData); |
172 | $entity = new Entity($dayoffData); |
173 | if ($entity instanceof Entity) { |
174 | $deleteQuery = new Query\DayOff(Query\Base::DELETE); |
175 | $date = (new \DateTimeImmutable())->setTimestamp($entity->date)->format('Y-m-d'); |
176 | $deleteQuery |
177 | ->addConditionDate($date) |
178 | ->addConditionName($entity->name); |
179 | $this->deleteItem($deleteQuery); |
180 | } |
181 | } |
182 | } |
183 | |
184 | public function deleteEntity($itemId) |
185 | { |
186 | $query = new Query\DayOff(Query\Base::DELETE); |
187 | $query->addConditionDayOffId($itemId); |
188 | return ($this->deleteItem($query)); |
189 | } |
190 | } |