blob: 7bfac89f30858bbed7e8aad3e0c6ec568cfc631a [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():
0Pranav84ae2cc2019-12-18 15:44:04 +053028 import pytz
29 return pytz.all_timezones
pranav nachnekar27910542019-09-03 12:04:52 +053030
31@frappe.whitelist(allow_guest=True)
Pranav Nachanekar110f4ea2019-09-09 17:04:25 +053032def get_appointment_slots(date, timezone):
0Pranav75db6f72019-11-07 12:47:00 +053033 # Convert query to local timezones
34 format_string = '%Y-%m-%d %H:%M:%S'
0Pranav76b20a52019-11-07 13:24:59 +053035 query_start_time = datetime.datetime.strptime(date + ' 00:00:00', format_string)
36 query_end_time = datetime.datetime.strptime(date + ' 23:59:59', format_string)
0Pranav51208b32019-11-07 12:54:48 +053037 query_start_time = convert_to_system_timezone(timezone, query_start_time)
38 query_end_time = convert_to_system_timezone(timezone, query_end_time)
39 now = convert_to_guest_timezone(timezone, datetime.datetime.now())
0Pranave4941442019-10-31 15:38:39 +053040
0Pranav75db6f72019-11-07 12:47:00 +053041 # Database queries
42 settings = frappe.get_doc('Appointment Booking Settings')
43 holiday_list = frappe.get_doc('Holiday List', settings.holiday_list)
0Pranav76b20a52019-11-07 13:24:59 +053044 timeslots = get_available_slots_between(query_start_time, query_end_time, settings)
Pranav Nachanekar110f4ea2019-09-09 17:04:25 +053045
0Pranav75db6f72019-11-07 12:47:00 +053046 # Filter and convert timeslots
47 converted_timeslots = []
48 for timeslot in timeslots:
0Pranav51208b32019-11-07 12:54:48 +053049 converted_timeslot = convert_to_guest_timezone(timezone, timeslot)
0Pranav75db6f72019-11-07 12:47:00 +053050 # Check if holiday
51 if _is_holiday(converted_timeslot.date(), holiday_list):
0Pranav76b20a52019-11-07 13:24:59 +053052 converted_timeslots.append(dict(time=converted_timeslot, availability=False))
0Pranav75db6f72019-11-07 12:47:00 +053053 continue
54 # Check availability
55 if check_availabilty(timeslot, settings) and converted_timeslot >= now:
0Pranav76b20a52019-11-07 13:24:59 +053056 converted_timeslots.append(dict(time=converted_timeslot, availability=True))
0Pranav75db6f72019-11-07 12:47:00 +053057 else:
0Pranav76b20a52019-11-07 13:24:59 +053058 converted_timeslots.append(dict(time=converted_timeslot, availability=False))
59 date_required = datetime.datetime.strptime(date + ' 00:00:00', format_string).date()
0Pranav75db6f72019-11-07 12:47:00 +053060 converted_timeslots = filter_timeslots(date_required, converted_timeslots)
61 return converted_timeslots
pranav nachnekar27910542019-09-03 12:04:52 +053062
63def get_available_slots_between(query_start_time, query_end_time, settings):
0Pranav75db6f72019-11-07 12:47:00 +053064 records = _get_records(query_start_time, query_end_time, settings)
65 timeslots = []
66 appointment_duration = datetime.timedelta(
67 minutes=settings.appointment_duration)
68 for record in records:
69 if record.day_of_week == WEEKDAYS[query_start_time.weekday()]:
0Pranav76b20a52019-11-07 13:24:59 +053070 current_time = _deltatime_to_datetime(query_start_time, record.from_time)
71 end_time = _deltatime_to_datetime(query_start_time, record.to_time)
0Pranav75db6f72019-11-07 12:47:00 +053072 else:
0Pranav76b20a52019-11-07 13:24:59 +053073 current_time = _deltatime_to_datetime(query_end_time, record.from_time)
74 end_time = _deltatime_to_datetime(query_end_time, record.to_time)
0Pranav75db6f72019-11-07 12:47:00 +053075 while current_time + appointment_duration <= end_time:
76 timeslots.append(current_time)
77 current_time += appointment_duration
78 return timeslots
pranav nachnekar27910542019-09-03 12:04:52 +053079
Pranav Nachanekar110f4ea2019-09-09 17:04:25 +053080
81@frappe.whitelist(allow_guest=True)
0Pranave4941442019-10-31 15:38:39 +053082def create_appointment(date, time, tz, contact):
0Pranavd3605d22019-12-11 15:02:23 +053083 format_string = '%Y-%m-%d %H:%M:%S'
0Pranav83100c92019-11-07 13:37:11 +053084 scheduled_time = datetime.datetime.strptime(date + " " + time, format_string)
85 # Strip tzinfo from datetime objects since it's handled by the doctype
0Pranavf25e2a22019-11-13 12:01:36 +053086 scheduled_time = scheduled_time.replace(tzinfo = None)
0Pranav51208b32019-11-07 12:54:48 +053087 scheduled_time = convert_to_system_timezone(tz, scheduled_time)
0Pranavf25e2a22019-11-13 12:01:36 +053088 scheduled_time = scheduled_time.replace(tzinfo = None)
0Pranav83100c92019-11-07 13:37:11 +053089 # Create a appointment document from form
0Pranav83100c92019-11-07 13:37:11 +053090 appointment = frappe.new_doc('Appointment')
0Pranavdb64c692019-11-13 11:12:38 +053091 appointment.scheduled_time = scheduled_time
0Pranav75db6f72019-11-07 12:47:00 +053092 contact = json.loads(contact)
0Pranavf25e2a22019-11-13 12:01:36 +053093 appointment.customer_name = contact.get('name', None)
0Pranav51208b32019-11-07 12:54:48 +053094 appointment.customer_phone_number = contact.get('number', None)
95 appointment.customer_skype = contact.get('skype', None)
96 appointment.customer_details = contact.get('notes', None)
97 appointment.customer_email = contact.get('email', None)
0Pranav75db6f72019-11-07 12:47:00 +053098 appointment.status = 'Open'
99 appointment.insert()
0Pranav511780a2019-11-14 12:47:08 +0530100 return appointment
pranav nachnekar27910542019-09-03 12:04:52 +0530101
pranav nachnekarc5b2a582019-09-03 14:16:47 +0530102# Helper Functions
Pranav Nachanekar110f4ea2019-09-09 17:04:25 +0530103def filter_timeslots(date, timeslots):
0Pranav75db6f72019-11-07 12:47:00 +0530104 filtered_timeslots = []
105 for timeslot in timeslots:
106 if(timeslot['time'].date() == date):
107 filtered_timeslots.append(timeslot)
108 return filtered_timeslots
pranav nachnekar27910542019-09-03 12:04:52 +0530109
0Pranavf25e2a22019-11-13 12:01:36 +0530110def convert_to_guest_timezone(guest_tz, datetimeobject):
0Pranav75db6f72019-11-07 12:47:00 +0530111 guest_tz = pytz.timezone(guest_tz)
112 local_timezone = pytz.timezone(frappe.utils.get_time_zone())
113 datetimeobject = local_timezone.localize(datetimeobject)
114 datetimeobject = datetimeobject.astimezone(guest_tz)
115 return datetimeobject
0Pranave4941442019-10-31 15:38:39 +0530116
0Pranav36098722019-11-01 12:06:42 +0530117def convert_to_system_timezone(guest_tz,datetimeobject):
0Pranav75db6f72019-11-07 12:47:00 +0530118 guest_tz = pytz.timezone(guest_tz)
119 datetimeobject = guest_tz.localize(datetimeobject)
120 system_tz = pytz.timezone(frappe.utils.get_time_zone())
121 datetimeobject = datetimeobject.astimezone(system_tz)
122 return datetimeobject
Pranav Nachanekar25148d02019-10-04 11:32:39 +0530123
Pranav Nachanekar110f4ea2019-09-09 17:04:25 +0530124def check_availabilty(timeslot, settings):
0Pranav75db6f72019-11-07 12:47:00 +0530125 return frappe.db.count('Appointment', {'scheduled_time': timeslot}) < settings.number_of_agents
Pranav Nachanekar110f4ea2019-09-09 17:04:25 +0530126
pranav nachnekar27910542019-09-03 12:04:52 +0530127def _is_holiday(date, holiday_list):
0Pranav75db6f72019-11-07 12:47:00 +0530128 for holiday in holiday_list.holidays:
129 if holiday.holiday_date == date:
130 return True
131 return False
pranav nachnekar27910542019-09-03 12:04:52 +0530132
Pranav Nachanekar25148d02019-10-04 11:32:39 +0530133
pranav nachnekar27910542019-09-03 12:04:52 +0530134def _get_records(start_time, end_time, settings):
0Pranav75db6f72019-11-07 12:47:00 +0530135 records = []
136 for record in settings.availability_of_slots:
137 if record.day_of_week == WEEKDAYS[start_time.weekday()] or record.day_of_week == WEEKDAYS[end_time.weekday()]:
138 records.append(record)
139 return records
pranav nachnekar27910542019-09-03 12:04:52 +0530140
Pranav Nachanekar25148d02019-10-04 11:32:39 +0530141
pranav nachnekar27910542019-09-03 12:04:52 +0530142def _deltatime_to_datetime(date, deltatime):
0Pranav75db6f72019-11-07 12:47:00 +0530143 time = (datetime.datetime.min + deltatime).time()
144 return datetime.datetime.combine(date.date(), time)
pranav nachnekar27910542019-09-03 12:04:52 +0530145
Pranav Nachanekar25148d02019-10-04 11:32:39 +0530146
pranav nachnekar27910542019-09-03 12:04:52 +0530147def _datetime_to_deltatime(date_time):
0Pranav75db6f72019-11-07 12:47:00 +0530148 midnight = datetime.datetime.combine(date_time.date(), datetime.time.min)
149 return (date_time-midnight)