blob: 707be6775c93d45d79fd43a8f12f87ded637da42 [file] [log] [blame]
pranav nachnekar27910542019-09-03 12:04:52 +05301import frappe
2import datetime
pranav nachnekarc5b2a582019-09-03 14:16:47 +05303import json
Pranav Nachanekar93670fe2019-10-03 11:58:02 +05304import pytz
pranav nachnekar27910542019-09-03 12:04:52 +05305
Pranav Nachanekar91a56492019-09-17 16:58:41 +05306
0Pranav76b20a52019-11-07 13:24:59 +05307WEEKDAYS = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
Pranav Nachanekar91a56492019-09-17 16:58:41 +05308
Pranav Nachanekar63dbacd2019-09-09 15:19:57 +05309no_cache = 1
10
Pranav Nachanekar25148d02019-10-04 11:32:39 +053011
Pranav Nachanekar9e36a9e2019-10-04 11:28:29 +053012def get_context(context):
0Pranav76b20a52019-11-07 13:24:59 +053013 is_enabled = frappe.db.get_single_value('Appointment Booking Settings', 'enable_scheduling')
0Pranav75db6f72019-11-07 12:47:00 +053014 if is_enabled:
15 return context
16 else:
0Pranav0671ea82019-11-07 13:31:56 +053017 frappe.local.flags.redirect_location = '/404'
18 raise frappe.Redirect
Pranav Nachanekar110f4ea2019-09-09 17:04:25 +053019
pranav nachnekar27910542019-09-03 12:04:52 +053020@frappe.whitelist(allow_guest=True)
21def get_appointment_settings():
0Pranav75db6f72019-11-07 12:47:00 +053022 settings = frappe.get_doc('Appointment Booking Settings')
23 settings.holiday_list = frappe.get_doc('Holiday List', settings.holiday_list)
24 return settings
pranav nachnekar27910542019-09-03 12:04:52 +053025
pranav nachnekar27910542019-09-03 12:04:52 +053026@frappe.whitelist(allow_guest=True)
27def get_timezones():
0Pranav75db6f72019-11-07 12:47:00 +053028 return pytz.all_timezones
pranav nachnekar27910542019-09-03 12:04:52 +053029
30@frappe.whitelist(allow_guest=True)
Pranav Nachanekar110f4ea2019-09-09 17:04:25 +053031def get_appointment_slots(date, timezone):
0Pranav75db6f72019-11-07 12:47:00 +053032 # Convert query to local timezones
33 format_string = '%Y-%m-%d %H:%M:%S'
0Pranav76b20a52019-11-07 13:24:59 +053034 query_start_time = datetime.datetime.strptime(date + ' 00:00:00', format_string)
35 query_end_time = datetime.datetime.strptime(date + ' 23:59:59', format_string)
0Pranav51208b32019-11-07 12:54:48 +053036 query_start_time = convert_to_system_timezone(timezone, query_start_time)
37 query_end_time = convert_to_system_timezone(timezone, query_end_time)
38 now = convert_to_guest_timezone(timezone, datetime.datetime.now())
0Pranave4941442019-10-31 15:38:39 +053039
0Pranav75db6f72019-11-07 12:47:00 +053040 # Database queries
41 settings = frappe.get_doc('Appointment Booking Settings')
42 holiday_list = frappe.get_doc('Holiday List', settings.holiday_list)
0Pranav76b20a52019-11-07 13:24:59 +053043 timeslots = get_available_slots_between(query_start_time, query_end_time, settings)
Pranav Nachanekar110f4ea2019-09-09 17:04:25 +053044
0Pranav75db6f72019-11-07 12:47:00 +053045 # Filter and convert timeslots
46 converted_timeslots = []
47 for timeslot in timeslots:
0Pranav51208b32019-11-07 12:54:48 +053048 converted_timeslot = convert_to_guest_timezone(timezone, timeslot)
0Pranav75db6f72019-11-07 12:47:00 +053049 # Check if holiday
50 if _is_holiday(converted_timeslot.date(), holiday_list):
0Pranav76b20a52019-11-07 13:24:59 +053051 converted_timeslots.append(dict(time=converted_timeslot, availability=False))
0Pranav75db6f72019-11-07 12:47:00 +053052 continue
53 # Check availability
54 if check_availabilty(timeslot, settings) and converted_timeslot >= now:
0Pranav76b20a52019-11-07 13:24:59 +053055 converted_timeslots.append(dict(time=converted_timeslot, availability=True))
0Pranav75db6f72019-11-07 12:47:00 +053056 else:
0Pranav76b20a52019-11-07 13:24:59 +053057 converted_timeslots.append(dict(time=converted_timeslot, availability=False))
58 date_required = datetime.datetime.strptime(date + ' 00:00:00', format_string).date()
0Pranav75db6f72019-11-07 12:47:00 +053059 converted_timeslots = filter_timeslots(date_required, converted_timeslots)
60 return converted_timeslots
pranav nachnekar27910542019-09-03 12:04:52 +053061
62def get_available_slots_between(query_start_time, query_end_time, settings):
0Pranav75db6f72019-11-07 12:47:00 +053063 records = _get_records(query_start_time, query_end_time, settings)
64 timeslots = []
65 appointment_duration = datetime.timedelta(
66 minutes=settings.appointment_duration)
67 for record in records:
68 if record.day_of_week == WEEKDAYS[query_start_time.weekday()]:
0Pranav76b20a52019-11-07 13:24:59 +053069 current_time = _deltatime_to_datetime(query_start_time, record.from_time)
70 end_time = _deltatime_to_datetime(query_start_time, record.to_time)
0Pranav75db6f72019-11-07 12:47:00 +053071 else:
0Pranav76b20a52019-11-07 13:24:59 +053072 current_time = _deltatime_to_datetime(query_end_time, record.from_time)
73 end_time = _deltatime_to_datetime(query_end_time, record.to_time)
0Pranav75db6f72019-11-07 12:47:00 +053074 while current_time + appointment_duration <= end_time:
75 timeslots.append(current_time)
76 current_time += appointment_duration
77 return timeslots
pranav nachnekar27910542019-09-03 12:04:52 +053078
Pranav Nachanekar110f4ea2019-09-09 17:04:25 +053079
80@frappe.whitelist(allow_guest=True)
0Pranave4941442019-10-31 15:38:39 +053081def create_appointment(date, time, tz, contact):
0Pranav75db6f72019-11-07 12:47:00 +053082 format_string = '%Y-%m-%d %H:%M:%S%z'
0Pranav83100c92019-11-07 13:37:11 +053083 scheduled_time = datetime.datetime.strptime(date + " " + time, format_string)
84 # Strip tzinfo from datetime objects since it's handled by the doctype
0Pranav75db6f72019-11-07 12:47:00 +053085 scheduled_time = scheduled_time.replace(tzinfo=None)
0Pranav51208b32019-11-07 12:54:48 +053086 scheduled_time = convert_to_system_timezone(tz, scheduled_time)
87 scheduled_time = scheduled_time.replace(tzinfo=None)
0Pranav83100c92019-11-07 13:37:11 +053088 # Create a appointment document from form
0Pranav75db6f72019-11-07 12:47:00 +053089 appointment.scheduled_time = scheduled_time
0Pranav83100c92019-11-07 13:37:11 +053090 appointment = frappe.new_doc('Appointment')
0Pranav75db6f72019-11-07 12:47:00 +053091 contact = json.loads(contact)
0Pranav51208b32019-11-07 12:54:48 +053092 appointment.customer_name = contact.get('name',None)
93 appointment.customer_phone_number = contact.get('number', None)
94 appointment.customer_skype = contact.get('skype', None)
95 appointment.customer_details = contact.get('notes', None)
96 appointment.customer_email = contact.get('email', None)
0Pranav75db6f72019-11-07 12:47:00 +053097 appointment.status = 'Open'
98 appointment.insert()
pranav nachnekar27910542019-09-03 12:04:52 +053099
pranav nachnekarc5b2a582019-09-03 14:16:47 +0530100# Helper Functions
Pranav Nachanekar110f4ea2019-09-09 17:04:25 +0530101def filter_timeslots(date, timeslots):
0Pranav75db6f72019-11-07 12:47:00 +0530102 filtered_timeslots = []
103 for timeslot in timeslots:
104 if(timeslot['time'].date() == date):
105 filtered_timeslots.append(timeslot)
106 return filtered_timeslots
pranav nachnekar27910542019-09-03 12:04:52 +0530107
0Pranave4941442019-10-31 15:38:39 +0530108def convert_to_guest_timezone(guest_tz,datetimeobject):
0Pranav75db6f72019-11-07 12:47:00 +0530109 guest_tz = pytz.timezone(guest_tz)
110 local_timezone = pytz.timezone(frappe.utils.get_time_zone())
111 datetimeobject = local_timezone.localize(datetimeobject)
112 datetimeobject = datetimeobject.astimezone(guest_tz)
113 return datetimeobject
0Pranave4941442019-10-31 15:38:39 +0530114
0Pranav36098722019-11-01 12:06:42 +0530115def convert_to_system_timezone(guest_tz,datetimeobject):
0Pranav75db6f72019-11-07 12:47:00 +0530116 guest_tz = pytz.timezone(guest_tz)
117 datetimeobject = guest_tz.localize(datetimeobject)
118 system_tz = pytz.timezone(frappe.utils.get_time_zone())
119 datetimeobject = datetimeobject.astimezone(system_tz)
120 return datetimeobject
Pranav Nachanekar25148d02019-10-04 11:32:39 +0530121
Pranav Nachanekar110f4ea2019-09-09 17:04:25 +0530122def check_availabilty(timeslot, settings):
0Pranav75db6f72019-11-07 12:47:00 +0530123 return frappe.db.count('Appointment', {'scheduled_time': timeslot}) < settings.number_of_agents
Pranav Nachanekar110f4ea2019-09-09 17:04:25 +0530124
pranav nachnekar27910542019-09-03 12:04:52 +0530125def _is_holiday(date, holiday_list):
0Pranav75db6f72019-11-07 12:47:00 +0530126 for holiday in holiday_list.holidays:
127 if holiday.holiday_date == date:
128 return True
129 return False
pranav nachnekar27910542019-09-03 12:04:52 +0530130
Pranav Nachanekar25148d02019-10-04 11:32:39 +0530131
pranav nachnekar27910542019-09-03 12:04:52 +0530132def _get_records(start_time, end_time, settings):
0Pranav75db6f72019-11-07 12:47:00 +0530133 records = []
134 for record in settings.availability_of_slots:
135 if record.day_of_week == WEEKDAYS[start_time.weekday()] or record.day_of_week == WEEKDAYS[end_time.weekday()]:
136 records.append(record)
137 return records
pranav nachnekar27910542019-09-03 12:04:52 +0530138
Pranav Nachanekar25148d02019-10-04 11:32:39 +0530139
pranav nachnekar27910542019-09-03 12:04:52 +0530140def _deltatime_to_datetime(date, deltatime):
0Pranav75db6f72019-11-07 12:47:00 +0530141 time = (datetime.datetime.min + deltatime).time()
142 return datetime.datetime.combine(date.date(), time)
pranav nachnekar27910542019-09-03 12:04:52 +0530143
Pranav Nachanekar25148d02019-10-04 11:32:39 +0530144
pranav nachnekar27910542019-09-03 12:04:52 +0530145def _datetime_to_deltatime(date_time):
0Pranav75db6f72019-11-07 12:47:00 +0530146 midnight = datetime.datetime.combine(date_time.date(), datetime.time.min)
147 return (date_time-midnight)