/** * Check if booking time is still available * Return TRUE if time is available * * @return bool */ public function checkBookingTime() { /** @var WPDB $wpdb */ global $wpdb; $booked_datetime = $this->userData->get('appointment_datetime'); $endDate = new DateTime($booked_datetime); $endDate->modify("+ {$this->userData->getService()->get('duration')} sec"); $query = $wpdb->prepare("SELECT `a`.*, `ss`.`capacity`, SUM(`ca`.`number_of_persons`) AS `total_number_of_persons`\n FROM `" . AB_CustomerAppointment::getTableName() . "` `ca`\n LEFT JOIN `" . AB_Appointment::getTableName() . "` `a` ON `a`.`id` = `ca`.`appointment_id`\n LEFT JOIN `" . AB_StaffService::getTableName() . "` `ss` ON `ss`.`staff_id` = `a`.`staff_id` AND `ss`.`service_id` = `a`.`service_id`\n WHERE `a`.`staff_id` = %d\n GROUP BY `a`.`start_date` , `a`.`staff_id` , `a`.`service_id`\n HAVING\n (`a`.`start_date` = %s AND `service_id` = %d AND `total_number_of_persons` >= `capacity`) OR\n (`a`.`start_date` = %s AND `service_id` <> %d) OR\n (`a`.`start_date` > %s AND `a`.`end_date` <= %s) OR\n (`a`.`start_date` < %s AND `a`.`end_date` > %s) OR\n (`a`.`start_date` < %s AND `a`.`end_date` > %s)\n LIMIT 1", $this->userData->getStaffId(), $booked_datetime, $this->userData->get('service_id'), $booked_datetime, $this->userData->get('service_id'), $booked_datetime, $endDate->format('Y-m-d H:i:s'), $endDate->format('Y-m-d H:i:s'), $endDate->format('Y-m-d H:i:s'), $booked_datetime, $booked_datetime); return !(bool) $wpdb->get_row($query); }
/** * Apply coupon */ public function executeApplyCoupon() { $form_id = $this->getParameter('form_id'); $ab_coupon = $this->getParameter('ab_coupon'); $response = null; if (get_option('ab_settings_coupons') and $form_id) { $userData = new AB_UserBookingData($form_id); $userData->load(); if ($userData->hasData()) { $price = $this->getWpdb()->get_var($this->getWpdb()->prepare('SELECT price FROM ab_staff_service WHERE staff_id = %d AND service_id = %d', $userData->getStaffId(), $userData->getServiceId())); if ($ab_coupon === '') { $userData->setCoupon(NULL); $response = array('status' => 'reset', 'text' => $this->_prepareInfoText(4, $userData, $price)); } else { $discount = $this->getWpdb()->get_var($this->getWpdb()->prepare('SELECT `discount` FROM `ab_coupons` WHERE UPPER(`code`) = %s AND `used` = 0', strtoupper($ab_coupon))); if ($discount) { $userData->setCoupon($ab_coupon); $price -= $price * $discount / 100; $response = array('status' => 'success', 'text' => $this->_prepareInfoText(4, $userData, $price)); } else { $userData->setCoupon(NULL); $response = array('status' => 'error', 'error' => __('* This coupon code is invalid or has been used', 'ab'), 'text' => $this->_prepareInfoText(4, $userData, $price)); } } } } // Output JSON response. if ($response === null) { $response = array('status' => 'no-data'); } header('Content-Type: application/json'); echo json_encode($response); exit(0); }