load->database(); $query = $this->db->query('INSERT INTO services ( main_category, main_category_description, service_category, service_name, service_description, service_price, service_time, is_enabled ) VALUES( "'.$serviceArray['main_category'].'", "'.$serviceArray['main_category_description'].'", "'.$serviceArray['service_category'].'", "'.$serviceArray['service_name'].'", "'.$serviceArray['service_description'].'", "'.$serviceArray['service_price'].'", "'.$serviceArray['service_time'].'", "'.$serviceArray['is_enabled'].'" );'); } public function updateService($service_id, $serviceArray){ $this->load->database(); foreach($serviceArray as $propertyKey => $propertyValue){ $query = $this->db->query('UPDATE services SET '.$propertyKey.' = "'.$propertyValue.'" WHERE service_id = "'.$service_id.'";'); } } public function updateBooking($booking_id, $bookingArray){ $this->load->database(); foreach($bookingArray as $propertyKey => $propertyValue){ $query = $this->db->query("UPDATE bookings SET ".$propertyKey." = '".$propertyValue."' WHERE booking_id = '".$booking_id."';"); } } public function getAllServiceByServiceType(){ $this->load->database(); $this->load->model('Group_model'); $query = $this->db->query('SELECT * FROM services WHERE is_public = "1" AND is_deleted != "1" ORDER BY list_order_nr ASC;'); $serviceArray = array(); if($query->num_rows() > 0){ foreach($query->result() as $resultItem){ $isServiceAvailable = $this->Service_model->iServiceAvailable($resultItem->service_id); $resultItemTmp = array( 'service_id' => $resultItem->service_id, 'main_category' => $resultItem->main_category, 'main_category_description' => $resultItem->main_category_description, 'service_category' => $resultItem->service_category, 'service_name' => $resultItem->service_name, 'service_description' => $resultItem->service_description, 'service_group' => $resultItem->service_group, 'service_price' => $resultItem->service_price, 'service_time' => $resultItem->service_time, 'is_enabled' => $resultItem->is_enabled, 'is_public' => $resultItem->is_public, 'is_service_available' => $isServiceAvailable, 'target_url' => $resultItem->target_url ); $serviceArray[] = (object)$resultItemTmp; } } return $serviceArray; } public function getServiceById($service_id){ $this->load->database(); $this->load->model('Group_model'); $this->load->model('Service_model'); $query = $this->db->query('SELECT * FROM services WHERE service_id = "'.$service_id.'" LIMIT 1;'); if($query->num_rows() > 0){ $resultItem = $query->result()[0]; return $resultItem; } else{ return 0; } } public function getAllServices(){ $this->load->database(); $query = $this->db->query('SELECT * FROM services WHERE is_deleted != "1" ORDER BY service_name ASC;'); if($query->num_rows() > 0){ foreach($query->result() as $resultItem){ $isServiceAvailable = $this->Service_model->iServiceAvailable($resultItem->service_id); $resultItemTmp = array( 'service_id' => $resultItem->service_id, 'main_category' => $resultItem->main_category, 'main_category_description' => $resultItem->main_category_description, 'service_category' => $resultItem->service_category, 'service_name' => $resultItem->service_name, 'service_description' => $resultItem->service_description, 'service_price' => $resultItem->service_price, 'service_time' => $resultItem->service_time, 'is_enabled' => $resultItem->is_enabled, 'is_public' => $resultItem->is_public, 'is_service_available' => $isServiceAvailable, 'target_url' => $resultItem->target_url ); $serviceArray[] = (object)$resultItemTmp; } return $serviceArray; } else{ return 0; } } public function getAvailableServices(){ $this->load->database(); $query = $this->db->query('SELECT * FROM services WHERE is_enabled = "1" AND is_deleted != "1" ORDER BY service_name ASC;'); if($query->num_rows() > 0){ $serviceArray = array(); foreach($query->result() as $resultItem){ $serviceTmp = array( 'service_name' => $resultItem->service_name, 'service_id' => $resultItem->service_id, 'service_group' => $resultItem->service_group ); $serviceArray[] = (object)$serviceTmp; } return $serviceArray; } } public function getAllServiceByGroups($serviceGroupArray){ $this->load->database(); $serviceGroupArrayString = implode(',', $serviceGroupArray); $query = $this->db->query('SELECT * FROM services WHERE is_deleted != "1" AND is_public = "1" AND service_group IN ('.$serviceGroupArrayString.') ORDER BY service_id ASC;'); $serviceArray = array(); if($query->num_rows() > 0){ foreach($query->result() as $resultItem){ $isServiceAvailable = $this->Service_model->iServiceAvailable($resultItem->service_id); $resultItemTmp = array( 'service_id' => $resultItem->service_id, 'main_category' => $resultItem->main_category, 'main_category_description' => $resultItem->main_category_description, 'service_category' => $resultItem->service_category, 'service_name' => $resultItem->service_name, 'service_description' => $resultItem->service_description, 'service_group' => $resultItem->service_group, 'is_extra' => $resultItem->is_extra, 'is_car' => $resultItem->is_car, 'service_price' => $resultItem->service_price, 'service_time' => $resultItem->service_time, 'is_enabled' => $resultItem->is_enabled, 'is_public' => $resultItem->is_public, 'is_service_available' => $isServiceAvailable, 'target_url' => $resultItem->target_url ); $serviceArray[] = (object)$resultItemTmp; } } return $serviceArray; } public function deleteBooking($booking_id){ $this->load->database(); $query = $this->db->query('DELETE FROM bookings WHERE booking_id = "'.$booking_id.'";'); } public function getAllBookings(){ $this->load->database(); $this->load->model('Service_model'); $query = $this->db->query('SELECT * FROM bookings ORDER BY booking_date DESC, booking_start_time ASC;'); if($query->num_rows() > 0){ $resultsArray = array(); foreach($query->result() as $resultItem){ $resultTmp = (array)$resultItem; $groupObjectsArray = array(); if($resultTmp['group_ids'] != ''){ $selectedGroups = explode(',', $resultTmp['group_ids']); if(is_array($selectedGroups)){ foreach($selectedGroups as $selectedGroupItem){ $selectedGroup = $this->Group_model->getGroupById($selectedGroupItem); $groupObjectsArray[] = $selectedGroup; } } } $resultTmp['workgroups'] = $groupObjectsArray; if($resultTmp['service_ids'] != ''){ $servicesArray = unserialize($resultTmp['service_ids']); if(is_array($servicesArray)){ $services = array(); foreach($servicesArray as $servicesArrayItem){ $selectedService = $this->Service_model->getServiceById($servicesArrayItem); $services[] = (object)array( 'service_name' => $selectedService->service_name, 'service_price' => $selectedService->service_price, 'service_time' => $selectedService->service_time ); } $resultTmp['services'] = $services; } else{ $resultTmp['services'] = array(); } } else{ $resultTmp['services'] = array(); } $resultsArray[] = (object)$resultTmp; } return $resultsArray; } else{ return 0; } } public function getActualBookings(){ $this->load->database(); $this->load->model('Service_model'); $query = $this->db->query('SELECT * FROM bookings JOIN workgroups ON bookings.group_id = workgroups.group_id WHERE booking_date >= CURDATE() ORDER BY booking_date DESC, booking_start_time ASC;'); if($query->num_rows() > 0){ $resultsArray = array(); foreach($query->result() as $resultItem){ $resultTmp = (array)$resultItem; if($resultTmp['service_ids'] != ''){ $servicesArray = unserialize($resultTmp['service_ids']); if(is_array($servicesArray)){ $services = array(); foreach($servicesArray as $servicesArrayItem){ $selectedService = $this->Service_model->getServiceById($servicesArrayItem['service_id']); $services[] = (object)array( 'service_name' => $selectedService->service_name, 'service_price' => $selectedService->service_price, 'service_time' => $selectedService->service_time, 'quantity' => $servicesArrayItem['quantity'] ); } $resultTmp['services'] = $services; } else{ $resultTmp['services'] = array(); } } else{ $resultTmp['services'] = array(); } $resultsArray[] = (object)$resultTmp; } return $resultsArray; } else{ return 0; } } public function getBookingById($booking_id){ $this->load->database(); $this->load->model('Service_model'); $query = $this->db->query('SELECT * FROM bookings WHERE booking_id = "'.$booking_id.'" LIMIT 1;'); if($query->num_rows() > 0){ $resultTmp = (array)$resultItem; if($resultTmp['service_ids'] != ''){ $servicesArray = unserialize($resultTmp['service_ids']); if(is_array($servicesArray)){ $services = array(); foreach($servicesArray as $servicesArrayItem){ $selectedService = $this->Service_model->getServiceById($servicesArrayItem['service_id']); $services[] = (object)array( 'service_name' => $selectedService->service_name, 'service_price' => $selectedService->service_price, 'service_time' => $selectedService->service_time, 'quantity' => $servicesArrayItem['quantity'] ); } $resultTmp['services'] = $services; } else{ $resultTmp['services'] = array(); } } else{ $resultTmp['services'] = array(); } $resultsArray = (object)$resultTmp; return $resultsArray; } else{ return 0; } } public function getWorkgroupById($group_id){ $this->load->database(); $query = $this->db->query('SELECT * FROM workgroups WHERE group_id = "'.$group_id.'" LIMIT 1;'); if($query->num_rows() > 0){ return $query->result()[0]; } else{ return 0; } } public function getAvailableTimes($group_id, $selectedDate, $servicelength){ $this->load->database(); $serviceLengthDateTime = new DateTime($selectedDate.' '.$servicelength); $service_length_time = strtotime($servicelength); $availableTimeArray = array(); $selectedDayName = date('D', strtotime($selectedDate)); $dayStartTime = new DateTime($selectedDate.' 09:00'); $finishTime = new DateTime($selectedDate.' 18:01'); if($selectedDayName == 'Sun'){ return array(); } elseif($selectedDayName == 'Sat'){ $finishTime = new DateTime($selectedDate.' 16:01'); } else{ //finish time - selected service length $finishTime = new DateTime($selectedDate.' 18:01'); } $today=date_create(date('Y-m-d')); $selectedDay=date_create($selectedDate); $dateDiff=date_diff($today,$selectedDay); $tmpDateStart = date_create(date('2024-02-29')); $tmpDateEnd = date_create(date('2024-03-07')); $datelength = new DateTime($servicelength); $diff = $datelength->diff($finishTime); //max time for start $maxTime = $diff->h.':'.$diff->i.':00'; $hours = (intval($diff->h)-9)*4+1; $mins = floor(intval($diff->i) / 15); $loopSteps = intval($hours) + intval($mins); for($periodIndex = 1; $periodIndex <= $loopSteps; $periodIndex++){ $minutes_to_add_start = $periodIndex*15; $minutes_to_add_end = ($periodIndex+1)*15; $startTime = new DateTime($selectedDate.' 08:45'); $endTime = new DateTime($selectedDate.' 08:45'); $startTime->add(new DateInterval('PT' . $minutes_to_add_start . 'M')); $endTime->add(new DateInterval('PT' . $minutes_to_add_end . 'M')); $maxTimeObject = new DateTime($maxTime); $tz = new DateTimeZone('Europe/Budapest'); $addStartTimeAndLength = new DateTime($selectedDate.' '.$startTime->format('H:i:00'), $tz); $addStartTimeAndLength->add(new DateInterval('PT' . intval($serviceLengthDateTime->format('H')) . 'H')); $addStartTimeAndLength->add(new DateInterval('PT' . intval($serviceLengthDateTime->format('i')) . 'M')); $currentStartTime = $startTime->format('H:i:00'); $currentEndTime = $endTime->format('H:i:00'); /* echo '<< '; print_r($serviceLengthDateTime->format('H:i:00')); echo ' / '; print_r($currentStartTime); echo ' / '; print_r($addStartTimeAndLength->format('H:i:00')); echo ' >>'; */ $query = $this->db->query('SELECT * FROM bookings WHERE booking_date = "'.$selectedDate.'" AND group_id = "'.$group_id.'" AND ( (TIME(booking_start_time) >= CAST("'.$currentStartTime.'" AS TIME) AND TIME(booking_finish_time) <= CAST("'.$currentEndTime.'" AS TIME)) OR (TIME(booking_start_time) <= CAST("'.$addStartTimeAndLength->format('H:i:00').'" AS TIME) AND TIME(booking_finish_time) >= CAST("'.$currentEndTime.'" AS TIME)) OR (TIME(booking_start_time) >= CAST("'.$currentStartTime.'" AS TIME) AND TIME(booking_start_time) <= CAST("'.$currentEndTime.'" AS TIME) AND TIME(booking_finish_time) >= CAST("'.$currentEndTime.'" AS TIME)) OR (TIME(booking_start_time) <= CAST("'.$currentStartTime.'" AS TIME) AND TIME(booking_finish_time) >= CAST("'.$currentEndTime.'" AS TIME) AND TIME(booking_start_time) <= CAST("'.$currentStartTime.'" AS TIME)) OR (TIME(booking_start_time) <= CAST("'.$currentStartTime.'" AS TIME) AND TIME(booking_finish_time) <= CAST("'.$currentEndTime.'" AS TIME) AND TIME(booking_finish_time) >= CAST("'.$currentStartTime.'" AS TIME)) OR (TIME(booking_start_time) <= CAST("'.$currentStartTime.'" AS TIME) AND TIME(booking_finish_time) >= CAST("'.$currentEndTime.'" AS TIME)) ); '); /* $query = $this->db->query('SELECT * FROM bookings WHERE booking_date = "'.$selectedDate.'" AND group_id = "'.$group_id.'" AND ( (TIME(booking_start_time) > CAST("'.$currentStartTime.'" AS TIME) AND TIME(booking_finish_time) < CAST("'.$currentEndTime.'" AS TIME)) OR (TIME(booking_start_time) < CAST("'.$addStartTimeAndLength->format('H:i:00').'" AS TIME) AND TIME(booking_finish_time) >= CAST("'.$currentEndTime.'" AS TIME)) OR (TIME(booking_start_time) > CAST("'.$currentStartTime.'" AS TIME) AND TIME(booking_start_time) < CAST("'.$currentEndTime.'" AS TIME) AND TIME(booking_finish_time) >= CAST("'.$currentEndTime.'" AS TIME)) OR (TIME(booking_start_time) < CAST("'.$currentStartTime.'" AS TIME) AND TIME(booking_finish_time) >= CAST("'.$currentEndTime.'" AS TIME) AND TIME(booking_start_time) < CAST("'.$currentStartTime.'" AS TIME)) OR (TIME(booking_start_time) < CAST("'.$currentStartTime.'" AS TIME) AND TIME(booking_finish_time) < CAST("'.$currentEndTime.'" AS TIME) AND TIME(booking_finish_time) > CAST("'.$currentStartTime.'" AS TIME)) OR (TIME(booking_start_time) < CAST("'.$currentStartTime.'" AS TIME) AND TIME(booking_finish_time) >= CAST("'.$currentEndTime.'" AS TIME)) ); '); */ $now = strtotime(date('H:i:s')) + 60*60; $timezone = 'Europe/Budapest'; $todayStartTime = strtotime($currentStartTime.' '.$timezone); $minDiff = ($todayStartTime - $now) / 60; if($dateDiff->format("%R%a") == 0 && $minDiff > 0){ if($query->num_rows() == 0){ $availableTimeArray[] = $currentStartTime; } } elseif($dateDiff->format("%R%a") >= 0 && ($selectedDay < $tmpDateStart || $selectedDay > $tmpDateEnd) && $dateDiff->format("%R%a") != 0){ if($query->num_rows() == 0){ $availableTimeArray[] = $currentStartTime; } } } return $availableTimeArray; } public function createBooking($bookingArray){ $this->load->database(); $query = $this->db->query("INSERT INTO bookings ( guest_name, guest_email, guest_phone, guest_addresse, guest_car_reg_number, guest_car_type, group_id, booking_date, booking_start_time, booking_finish_time, service_ids, guest_confirmed, guest_confirm_code ) VALUES( '".$bookingArray['guest_name']."', '".$bookingArray['guest_email']."', '".$bookingArray['guest_phone']."', '".$bookingArray['guest_addresse']."', '".$bookingArray['guest_car_reg_number']."', '".$bookingArray['guest_car_type']."', '".$bookingArray['group_id']."', '".$bookingArray['booking_date']."', '".$bookingArray['booking_start_time']."', '".$bookingArray['booking_finish_time']."', '".$bookingArray['service_ids']."', '".$bookingArray['guest_confirmed']."', '".$bookingArray['guest_confirm_code']."' );"); } public function iServiceAvailable($service_id){ $this->load->database(); $this->load->model('Group_model'); $isServiceActive = 0; $selectedService = $this->Service_model->getServiceById($service_id); $activeGroups = $this->Group_model->getAllActiveGroups(); if(is_array($activeGroups)){ foreach($activeGroups as $activeGroupItem){ $serviceIds = unserialize($activeGroupItem->skills); if(in_array($service_id, $serviceIds) && $selectedService->is_enabled){ $isServiceActive = 1; } } } return $isServiceActive; } public function getServiceGroups(){ $this->load->database(); $this->load->model('Group_model'); $this->load->model('Service_model'); $services = $this->Service_model->getAvailableServices(); $serviceArray = array(); if(is_array($services)){ foreach($services as $serviceItem){ if(!in_array($serviceItem->service_group, $serviceArray)){ $serviceArray[] = $serviceItem->service_group; } } } return $serviceArray; } } ?>