2019-09-03 06:34:52 +00:00
|
|
|
import datetime
|
2019-09-03 08:46:47 +00:00
|
|
|
import json
|
2021-09-02 11:14:59 +00:00
|
|
|
|
2021-07-02 06:49:24 +00:00
|
|
|
import frappe
|
2019-10-03 06:28:02 +00:00
|
|
|
import pytz
|
2021-07-02 06:49:24 +00:00
|
|
|
from frappe import _
|
2023-03-04 18:31:40 +00:00
|
|
|
from frappe.utils.data import get_system_timezone
|
2019-09-17 11:28:41 +00:00
|
|
|
|
2019-11-07 07:54:59 +00:00
|
|
|
WEEKDAYS = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
|
2019-09-17 11:28:41 +00:00
|
|
|
|
2019-09-09 09:49:57 +00:00
|
|
|
no_cache = 1
|
|
|
|
|
2019-10-04 06:02:39 +00:00
|
|
|
|
2019-10-04 05:58:29 +00:00
|
|
|
def get_context(context):
|
2019-11-07 07:54:59 +00:00
|
|
|
is_enabled = frappe.db.get_single_value("Appointment Booking Settings", "enable_scheduling")
|
2019-11-07 07:17:00 +00:00
|
|
|
if is_enabled:
|
|
|
|
return context
|
|
|
|
else:
|
2021-07-02 06:49:24 +00:00
|
|
|
frappe.redirect_to_message(
|
|
|
|
_("Appointment Scheduling Disabled"),
|
|
|
|
_("Appointment Scheduling has been disabled for this site"),
|
|
|
|
http_status_code=302,
|
|
|
|
indicator_color="red",
|
|
|
|
)
|
2019-11-07 08:01:56 +00:00
|
|
|
raise frappe.Redirect
|
2019-09-09 11:34:25 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2019-09-03 06:34:52 +00:00
|
|
|
@frappe.whitelist(allow_guest=True)
|
|
|
|
def get_appointment_settings():
|
2022-12-15 11:23:51 +00:00
|
|
|
settings = frappe.get_cached_value(
|
|
|
|
"Appointment Booking Settings",
|
|
|
|
None,
|
2022-12-16 10:25:58 +00:00
|
|
|
["advance_booking_days", "appointment_duration", "success_redirect_url"],
|
2022-12-15 11:23:51 +00:00
|
|
|
as_dict=True,
|
|
|
|
)
|
2019-11-07 07:17:00 +00:00
|
|
|
return settings
|
2019-09-03 06:34:52 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2019-09-03 06:34:52 +00:00
|
|
|
@frappe.whitelist(allow_guest=True)
|
|
|
|
def get_timezones():
|
2019-12-18 10:14:04 +00:00
|
|
|
import pytz
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2019-12-18 10:14:04 +00:00
|
|
|
return pytz.all_timezones
|
2019-09-03 06:34:52 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2019-09-03 06:34:52 +00:00
|
|
|
@frappe.whitelist(allow_guest=True)
|
2019-09-09 11:34:25 +00:00
|
|
|
def get_appointment_slots(date, timezone):
|
2019-11-07 07:17:00 +00:00
|
|
|
# Convert query to local timezones
|
|
|
|
format_string = "%Y-%m-%d %H:%M:%S"
|
2019-11-07 07:54:59 +00:00
|
|
|
query_start_time = datetime.datetime.strptime(date + " 00:00:00", format_string)
|
|
|
|
query_end_time = datetime.datetime.strptime(date + " 23:59:59", format_string)
|
2019-11-07 07:24:48 +00:00
|
|
|
query_start_time = convert_to_system_timezone(timezone, query_start_time)
|
|
|
|
query_end_time = convert_to_system_timezone(timezone, query_end_time)
|
|
|
|
now = convert_to_guest_timezone(timezone, datetime.datetime.now())
|
2019-11-07 07:17:00 +00:00
|
|
|
|
|
|
|
# Database queries
|
|
|
|
settings = frappe.get_doc("Appointment Booking Settings")
|
|
|
|
holiday_list = frappe.get_doc("Holiday List", settings.holiday_list)
|
2019-11-07 07:54:59 +00:00
|
|
|
timeslots = get_available_slots_between(query_start_time, query_end_time, settings)
|
2019-11-07 07:17:00 +00:00
|
|
|
|
|
|
|
# Filter and convert timeslots
|
|
|
|
converted_timeslots = []
|
|
|
|
for timeslot in timeslots:
|
2019-11-07 07:24:48 +00:00
|
|
|
converted_timeslot = convert_to_guest_timezone(timezone, timeslot)
|
2019-11-07 07:17:00 +00:00
|
|
|
# Check if holiday
|
|
|
|
if _is_holiday(converted_timeslot.date(), holiday_list):
|
2019-11-07 07:54:59 +00:00
|
|
|
converted_timeslots.append(dict(time=converted_timeslot, availability=False))
|
2019-11-07 07:17:00 +00:00
|
|
|
continue
|
|
|
|
# Check availability
|
|
|
|
if check_availabilty(timeslot, settings) and converted_timeslot >= now:
|
2019-11-07 07:54:59 +00:00
|
|
|
converted_timeslots.append(dict(time=converted_timeslot, availability=True))
|
2019-11-07 07:17:00 +00:00
|
|
|
else:
|
2019-11-07 07:54:59 +00:00
|
|
|
converted_timeslots.append(dict(time=converted_timeslot, availability=False))
|
|
|
|
date_required = datetime.datetime.strptime(date + " 00:00:00", format_string).date()
|
2019-11-07 07:17:00 +00:00
|
|
|
converted_timeslots = filter_timeslots(date_required, converted_timeslots)
|
|
|
|
return converted_timeslots
|
2019-09-03 06:34:52 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2019-09-03 06:34:52 +00:00
|
|
|
def get_available_slots_between(query_start_time, query_end_time, settings):
|
2019-11-07 07:17:00 +00:00
|
|
|
records = _get_records(query_start_time, query_end_time, settings)
|
|
|
|
timeslots = []
|
|
|
|
appointment_duration = datetime.timedelta(minutes=settings.appointment_duration)
|
|
|
|
for record in records:
|
|
|
|
if record.day_of_week == WEEKDAYS[query_start_time.weekday()]:
|
2019-11-07 07:54:59 +00:00
|
|
|
current_time = _deltatime_to_datetime(query_start_time, record.from_time)
|
|
|
|
end_time = _deltatime_to_datetime(query_start_time, record.to_time)
|
2019-11-07 07:17:00 +00:00
|
|
|
else:
|
2019-11-07 07:54:59 +00:00
|
|
|
current_time = _deltatime_to_datetime(query_end_time, record.from_time)
|
|
|
|
end_time = _deltatime_to_datetime(query_end_time, record.to_time)
|
2019-11-07 07:17:00 +00:00
|
|
|
while current_time + appointment_duration <= end_time:
|
|
|
|
timeslots.append(current_time)
|
|
|
|
current_time += appointment_duration
|
|
|
|
return timeslots
|
2019-09-03 06:34:52 +00:00
|
|
|
|
2019-09-09 11:34:25 +00:00
|
|
|
|
|
|
|
@frappe.whitelist(allow_guest=True)
|
2019-10-31 10:08:39 +00:00
|
|
|
def create_appointment(date, time, tz, contact):
|
2022-12-16 10:25:58 +00:00
|
|
|
format_string = "%Y-%m-%d %H:%M:%S"
|
|
|
|
scheduled_time = datetime.datetime.strptime(date + " " + time, format_string)
|
2019-11-07 08:07:11 +00:00
|
|
|
# Strip tzinfo from datetime objects since it's handled by the doctype
|
2019-11-13 06:31:36 +00:00
|
|
|
scheduled_time = scheduled_time.replace(tzinfo=None)
|
2022-12-16 10:25:58 +00:00
|
|
|
scheduled_time = convert_to_system_timezone(tz, scheduled_time)
|
|
|
|
scheduled_time = scheduled_time.replace(tzinfo=None)
|
2019-11-07 08:07:11 +00:00
|
|
|
# Create a appointment document from form
|
|
|
|
appointment = frappe.new_doc("Appointment")
|
2022-12-16 10:25:58 +00:00
|
|
|
appointment.scheduled_time = scheduled_time
|
|
|
|
contact = json.loads(contact)
|
|
|
|
appointment.customer_name = contact.get("name", None)
|
|
|
|
appointment.customer_phone_number = contact.get("number", None)
|
|
|
|
appointment.customer_skype = contact.get("skype", None)
|
|
|
|
appointment.customer_details = contact.get("notes", None)
|
|
|
|
appointment.customer_email = contact.get("email", None)
|
|
|
|
appointment.status = "Open"
|
2022-12-15 11:23:51 +00:00
|
|
|
appointment.insert(ignore_permissions=True)
|
2019-11-14 07:17:08 +00:00
|
|
|
return appointment
|
2019-09-03 06:34:52 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2019-09-03 08:46:47 +00:00
|
|
|
# Helper Functions
|
2019-09-09 11:34:25 +00:00
|
|
|
def filter_timeslots(date, timeslots):
|
2019-11-07 07:17:00 +00:00
|
|
|
filtered_timeslots = []
|
|
|
|
for timeslot in timeslots:
|
|
|
|
if timeslot["time"].date() == date:
|
|
|
|
filtered_timeslots.append(timeslot)
|
|
|
|
return filtered_timeslots
|
2019-09-03 06:34:52 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2019-11-13 06:31:36 +00:00
|
|
|
def convert_to_guest_timezone(guest_tz, datetimeobject):
|
2019-11-07 07:17:00 +00:00
|
|
|
guest_tz = pytz.timezone(guest_tz)
|
2023-03-04 18:31:40 +00:00
|
|
|
local_timezone = pytz.timezone(get_system_timezone())
|
2019-11-07 07:17:00 +00:00
|
|
|
datetimeobject = local_timezone.localize(datetimeobject)
|
|
|
|
datetimeobject = datetimeobject.astimezone(guest_tz)
|
|
|
|
return datetimeobject
|
2019-10-31 10:08:39 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2019-11-01 06:36:42 +00:00
|
|
|
def convert_to_system_timezone(guest_tz, datetimeobject):
|
2019-11-07 07:17:00 +00:00
|
|
|
guest_tz = pytz.timezone(guest_tz)
|
|
|
|
datetimeobject = guest_tz.localize(datetimeobject)
|
2023-03-04 18:31:40 +00:00
|
|
|
system_tz = pytz.timezone(get_system_timezone())
|
2019-11-07 07:17:00 +00:00
|
|
|
datetimeobject = datetimeobject.astimezone(system_tz)
|
|
|
|
return datetimeobject
|
2019-10-04 06:02:39 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2019-09-09 11:34:25 +00:00
|
|
|
def check_availabilty(timeslot, settings):
|
2019-11-07 07:17:00 +00:00
|
|
|
return frappe.db.count("Appointment", {"scheduled_time": timeslot}) < settings.number_of_agents
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2019-09-09 11:34:25 +00:00
|
|
|
|
2019-09-03 06:34:52 +00:00
|
|
|
def _is_holiday(date, holiday_list):
|
2019-11-07 07:17:00 +00:00
|
|
|
for holiday in holiday_list.holidays:
|
|
|
|
if holiday.holiday_date == date:
|
|
|
|
return True
|
|
|
|
return False
|
2019-09-03 06:34:52 +00:00
|
|
|
|
2019-10-04 06:02:39 +00:00
|
|
|
|
2019-09-03 06:34:52 +00:00
|
|
|
def _get_records(start_time, end_time, settings):
|
2019-11-07 07:17:00 +00:00
|
|
|
records = []
|
|
|
|
for record in settings.availability_of_slots:
|
|
|
|
if (
|
|
|
|
record.day_of_week == WEEKDAYS[start_time.weekday()]
|
|
|
|
or record.day_of_week == WEEKDAYS[end_time.weekday()]
|
|
|
|
):
|
|
|
|
records.append(record)
|
|
|
|
return records
|
2019-09-03 06:34:52 +00:00
|
|
|
|
2019-10-04 06:02:39 +00:00
|
|
|
|
2019-09-03 06:34:52 +00:00
|
|
|
def _deltatime_to_datetime(date, deltatime):
|
2019-11-07 07:17:00 +00:00
|
|
|
time = (datetime.datetime.min + deltatime).time()
|
|
|
|
return datetime.datetime.combine(date.date(), time)
|
2019-09-03 06:34:52 +00:00
|
|
|
|
2019-10-04 06:02:39 +00:00
|
|
|
|
2019-09-03 06:34:52 +00:00
|
|
|
def _datetime_to_deltatime(date_time):
|
2019-11-07 07:17:00 +00:00
|
|
|
midnight = datetime.datetime.combine(date_time.date(), datetime.time.min)
|
2021-07-02 06:49:24 +00:00
|
|
|
return date_time - midnight
|