PluginProbe ʕ •ᴥ•ʔ
VikAppointments Services Booking Calendar / trunk
VikAppointments Services Booking Calendar vtrunk
trunk 1.2.17 1.2.18 1.2.19
vikappointments / admin / models / worktime.php
vikappointments / admin / models Last commit date
apiban.php 4 years ago apilog.php 2 years ago apiplugin.php 4 years ago apiuser.php 2 years ago apiuseroptions.php 2 years ago backup.php 4 months ago caldays.php 1 month ago calendar.php 1 month ago city.php 4 years ago closure.php 1 month ago configapp.php 4 years ago configcldays.php 4 years ago configcron.php 4 years ago configemp.php 4 years ago configsmsapi.php 4 years ago configuration.php 4 months ago conversion.php 4 years ago country.php 2 years ago coupon.php 2 years ago couponemployee.php 4 years ago coupongroup.php 2 years ago couponservice.php 4 years ago cronjob.php 2 years ago cronjoblog.php 4 years ago customer.php 4 months ago customf.php 2 years ago customfservice.php 4 years ago customizer.php 4 years ago empgroup.php 2 years ago employee.php 2 years ago empsettings.php 4 years ago file.php 4 years ago findreservation.php 1 month ago group.php 2 years ago import.php 4 years ago index.html 4 years ago invoice.php 1 month ago langcustomf.php 4 years ago langempgroup.php 4 years ago langemployee.php 4 years ago langgroup.php 4 years ago langmedia.php 4 years ago langoption.php 2 years ago langoptiongroup.php 4 years ago langoptionvar.php 4 years ago langpackage.php 4 years ago langpackgroup.php 4 years ago langpayment.php 4 years ago langservice.php 4 years ago langstatuscode.php 4 years ago langsubscr.php 4 years ago langtax.php 2 years ago langtaxrule.php 4 years ago location.php 2 years ago mailtext.php 2 years ago makerecurrence.php 1 month ago media.php 2 years ago multiorder.php 1 month ago option.php 1 year ago optiongroup.php 2 years ago optionvar.php 1 year ago orderstatus.php 2 years ago package.php 2 years ago packageservice.php 4 years ago packgroup.php 2 years ago packorder.php 2 years ago packorderitem.php 1 month ago payment.php 2 years ago rate.php 2 years ago reportsemp.php 4 months ago reportsser.php 4 months ago reservation.php 1 month ago resoptassoc.php 2 years ago restriction.php 2 years ago review.php 3 years ago serempassoc.php 1 year ago seroptassoc.php 4 years ago serrateassoc.php 4 years ago serrestrassoc.php 4 years ago service.php 2 years ago state.php 2 years ago statswidget.php 2 years ago statuscode.php 2 years ago subscription.php 1 month ago subscrorder.php 1 month ago tag.php 4 years ago tax.php 2 years ago taxrule.php 2 years ago updateprogram.php 4 years ago usernote.php 2 years ago waitinglist.php 1 month ago webhook.php 1 year ago worktime.php 1 month ago
worktime.php
329 lines
1 <?php
2 /**
3 * @package VikAppointments
4 * @subpackage core
5 * @author E4J s.r.l.
6 * @copyright Copyright (C) 2021 E4J s.r.l. All Rights Reserved.
7 * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
8 * @link https://vikwp.com
9 */
10
11 // No direct access
12 defined('ABSPATH') or die('No script kiddies please!');
13
14 VAPLoader::import('libraries.mvc.model');
15
16 /**
17 * VikAppointments employee working time model.
18 *
19 * @since 1.7
20 */
21 class VikAppointmentsModelWorktime extends JModelVAP
22 {
23 /**
24 * Lookup used to cache the services assigned to
25 * a specified employee (key).
26 *
27 * @var array
28 */
29 protected $services = array();
30
31 /**
32 * Basic item loading implementation.
33 *
34 * @param mixed $pk An optional primary key value to load the row by, or an array of fields to match.
35 * If not set the instance property value is used.
36 * @param boolean $new True to return an empty object if missing.
37 *
38 * @return mixed The record object on success, null otherwise.
39 */
40 public function getItem($pk, $new = false)
41 {
42 // load item through parent
43 $item = parent::getItem($pk, $new);
44
45 if ($item && !$item->id)
46 {
47 // use default times
48 $item->fromts = 540;
49 $item->endts = 720;
50 }
51
52 return $item;
53 }
54
55 /**
56 * Basic save implementation.
57 *
58 * @param mixed $data Either an array or an object of data to save.
59 *
60 * @return mixed The ID of the record on success, false otherwise.
61 */
62 public function save($data)
63 {
64 $data = (array) $data;
65
66 // check whether we are creating or updating
67 $is_new = empty($data['id']);
68
69 // attempt to save the working time
70 $id = parent::save($data);
71
72 if (!$id)
73 {
74 // an error occurred, do not go ahead
75 return false;
76 }
77
78 // make sure the employee was specified and we didn't update a working time for a service
79 if (!empty($data['id_employee']) && (empty($data['id_service']) || $data['id_service'] == -1))
80 {
81 $dbo = JFactory::getDbo();
82
83 $services = isset($data['services']) ? (array) $data['services'] : array();
84
85 // load services list
86 $services = $this->getServices($data['id_employee'], $services);
87
88 foreach ($services as $id_service)
89 {
90 // insert/update also the service relation
91 $data['id'] = 0;
92 $data['id_service'] = (int) $id_service;
93 $data['parent'] = $id;
94
95 if (!$is_new)
96 {
97 // lets look for a specific record to update
98 $q = $dbo->getQuery(true)
99 ->select($dbo->qn('id'))
100 ->from($dbo->qn('#__vikappointments_emp_worktime'))
101 ->where($dbo->qn('parent') . ' = ' . (int) $data['parent'])
102 ->where($dbo->qn('id_service') . ' = ' . (int) $data['id_service']);
103
104 $dbo->setQuery($q, 0, 1);
105
106 // existing record, overwrite ID to update it
107 $data['id'] = $dbo->loadResult();
108 }
109
110 // update only if we need to create a new child record
111 // or if have to update an existing one
112 if ($is_new || $data['id'])
113 {
114 // save relation
115 parent::save($data);
116 }
117 }
118 }
119
120
121 // exit;
122
123 return $id;
124 }
125
126 /**
127 * Basic delete implementation.
128 *
129 * @param mixed $ids Either the record ID or a list of records.
130 *
131 * @return boolean True on success, false otherwise.
132 */
133 public function delete($ids)
134 {
135 // only int values are accepted
136 $ids = array_map('intval', (array) $ids);
137
138 // attempt to delete the working time
139 if (!parent::delete($ids))
140 {
141 // there's nothing to delete
142 return false;
143 }
144
145 $dbo = JFactory::getDbo();
146
147 // load all working times assigned to the deleted records
148 $q = $dbo->getQuery(true)
149 ->select($dbo->qn('id'))
150 ->from($dbo->qn('#__vikappointments_emp_worktime'))
151 ->where($dbo->qn('parent') . ' IN (' . implode(',', $ids) . ')' );
152
153 $dbo->setQuery($q);
154
155 if ($columns = $dbo->loadColumn())
156 {
157 // delete records found
158 parent::delete($columns);
159 }
160
161 return true;
162 }
163
164 /**
165 * Restores the working days for the given service and
166 * employee relation.
167 *
168 * @param integer $id_service The service ID.
169 * @param integer $id_employee The employee ID.
170 *
171 * @return boolean True on success, false otherwise.
172 */
173 public function restore($id_service, $id_employee)
174 {
175 $dbo = JFactory::getDbo();
176
177 // load all employee working days
178 $q = $dbo->getQuery(true)
179 ->select('*')
180 ->from($dbo->qn('#__vikappointments_emp_worktime'))
181 ->where(array(
182 $dbo->qn('id_employee') . ' = ' . $id_employee,
183 $dbo->qn('id_service') . ' <= 0',
184 ));
185
186 $today = strtotime('00:00:00', VikAppointments::now());
187
188 // exclude special days in the past
189 $q->andWhere(array(
190 $dbo->qn('ts') . ' = -1',
191 $dbo->qn('ts') . ' >= ' . $today,
192 ), 'OR');
193
194 $dbo->setQuery($q);
195 $rows = $dbo->loadObjectList();
196
197 if (!$rows)
198 {
199 // the employee doesn't have any working days
200 return false;
201 }
202
203 $restored = false;
204
205 foreach ($rows as $row)
206 {
207 // check whether the service already have this working time
208 $q = $dbo->getQuery(true)
209 ->select(1)
210 ->from($dbo->qn('#__vikappointments_emp_worktime'))
211 ->where($dbo->qn('parent') . ' = ' . $row->id)
212 ->where($dbo->qn('id_service') . ' = ' . (int) $id_service);
213
214 $dbo->setQuery($q, 0, 1);
215 $dbo->execute();
216
217 if (!$dbo->getNumRows())
218 {
219 // make relation with parent
220 $row->parent = $row->id;
221 // make relation with service
222 $row->id_service = (int) $id_service;
223 // unset ID to create new
224 $row->id = 0;
225
226 // working time not found, restore it
227 $restored = $this->save($row) || $restored;
228 }
229 }
230
231 return $restored;
232 }
233
234 /**
235 * Retrieves a list of services assigned to the specified
236 * employee. The list of services is cached within an
237 * internal property, in order to avoid duplicate queries.
238 *
239 * @param integer $id_employee
240 *
241 * @return array
242 */
243 public function getServices($id_employee, array $services = array())
244 {
245 if (!isset($this->services[$id_employee]))
246 {
247 $this->services[$id_employee] = array();
248
249 $dbo = JFactory::getDbo();
250
251 // retrieve all services assigned to this employee
252 $q = $dbo->getQuery(true)
253 ->select($dbo->qn('id_service'))
254 ->from($dbo->qn('#__vikappointments_ser_emp_assoc'))
255 ->where($dbo->qn('id_employee') . ' = ' . (int) $id_employee);
256
257 $dbo->setQuery($q);
258 $this->services[$id_employee] = $dbo->loadColumn();
259 }
260
261 if ($services)
262 {
263 // filter the services to take only the ones that have been specified
264 return array_values(array_filter($this->services[$id_employee], function($s) use ($services)
265 {
266 return in_array($s, $services);
267 }));
268 }
269
270 return $this->services[$id_employee];
271 }
272
273 /**
274 * Returns the location related to the specified employee, service and check-in.
275 *
276 * @param string $checkin The UTC check-in date time.
277 * @param integer $id_service The service ID.
278 * @param integer $id_employee The employee ID. If not provided, it won't be used.
279 *
280 * @return mixed The location ID.
281 */
282 public function getLocation($checkin, $id_service, $id_employee = 0)
283 {
284 $dbo = JFactory::getDbo();
285
286 // get timezone
287 $tz = JModelVAP::getInstance('employee')->getTimezone($id_employee);
288
289 // create date object and adjust it to the employee timezone
290 $date = JFactory::getDate($checkin);
291 $date->setTimezone(new DateTimeZone($tz));
292
293 // convert local time into minutes int
294 $hm = JHtml::fetch('vikappointments.time2min', $date->format('H:i', $local = true));
295
296 // back at midnight for a correct comparison between the dates
297 $date->modify('00:00:00');
298
299 $q = $dbo->getQuery(true);
300
301 // search working day for a specific day of the year
302
303 $q->select($dbo->qn('id_location'));
304 $q->from($dbo->qn('#__vikappointments_emp_worktime'));
305 $q->where($dbo->qn('id_service') . ' = ' . (int) $id_service);
306
307 if ($id_employee > 0)
308 {
309 $q->where($dbo->qn('id_employee') . ' = ' . (int) $id_employee);
310 }
311
312 // filter working days by time
313 $q->where($hm . ' BETWEEN ' . $dbo->qn('fromts') . ' AND ' . $dbo->qn('endts'));
314
315 // filter by date/day
316 $q->andWhere(array(
317 $dbo->qn('day') . ' = ' . (int) $date->format('w', $local = true) . ' AND ' . $dbo->qn('ts') . ' <= 0',
318 $dbo->qn('tsdate') . ' = ' . $dbo->q($date->toSql($local = true)),
319 ));
320
321 // take working times for the days of the year first
322 $q->order($dbo->qn('ts') . ' DESC');
323
324 $dbo->setQuery($q, 0, 1);
325 // return location found
326 return max(0, (int) $dbo->loadResult());
327 }
328 }
329