blob: 11073131b1f2c29e5f32617528dc91229c9edaf8 [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
7WEEKDAYS = ["Monday", "Tuesday", "Wednesday",
0Pranav75db6f72019-11-07 12:47:00 +05308 "Thursday", "Friday", "Saturday", "Sunday"]
Pranav Nachanekar91a56492019-09-17 16:58:41 +05309
Pranav Nachanekar63dbacd2019-09-09 15:19:57 +053010no_cache = 1
11
Pranav Nachanekar25148d02019-10-04 11:32:39 +053012
Pranav Nachanekar9e36a9e2019-10-04 11:28:29 +053013def get_context(context):
0Pranav75db6f72019-11-07 12:47:00 +053014 is_enabled = frappe.db.get_single_value(
15 'Appointment Booking Settings', 'enable_scheduling')
16 if is_enabled:
17 return context
18 else:
19 raise frappe.DoesNotExistError
Pranav Nachanekar110f4ea2019-09-09 17:04:25 +053020
pranav nachnekar27910542019-09-03 12:04:52 +053021@frappe.whitelist(allow_guest=True)
22def get_appointment_settings():
0Pranav75db6f72019-11-07 12:47:00 +053023 settings = frappe.get_doc('Appointment Booking Settings')
24 settings.holiday_list = frappe.get_doc('Holiday List', settings.holiday_list)
25 return settings
pranav nachnekar27910542019-09-03 12:04:52 +053026
pranav nachnekar27910542019-09-03 12:04:52 +053027@frappe.whitelist(allow_guest=True)
28def get_timezones():
0Pranav75db6f72019-11-07 12:47:00 +053029 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 import pytz
34 # Convert query to local timezones
35 format_string = '%Y-%m-%d %H:%M:%S'
36 query_start_time = datetime.datetime.strptime(
37 date + ' 00:00:00', format_string)
38 query_end_time = datetime.datetime.strptime(
39 date + ' 23:59:59', format_string)
40 query_start_time = convert_to_system_timezone(timezone,query_start_time)
41 query_end_time = convert_to_system_timezone(timezone,query_end_time)
42 now = convert_to_guest_timezone(timezone,datetime.datetime.now())
0Pranave4941442019-10-31 15:38:39 +053043
0Pranav75db6f72019-11-07 12:47:00 +053044 # Database queries
45 settings = frappe.get_doc('Appointment Booking Settings')
46 holiday_list = frappe.get_doc('Holiday List', settings.holiday_list)
47 timeslots = get_available_slots_between(
48 query_start_time, query_end_time, settings)
Pranav Nachanekar110f4ea2019-09-09 17:04:25 +053049
0Pranav75db6f72019-11-07 12:47:00 +053050 # Filter and convert timeslots
51 converted_timeslots = []
52 for timeslot in timeslots:
53 converted_timeslot = convert_to_guest_timezone(timezone,timeslot)
54 # Check if holiday
55 if _is_holiday(converted_timeslot.date(), holiday_list):
56 converted_timeslots.append(
57 dict(time=converted_timeslot, availability=False))
58 continue
59 # Check availability
60 if check_availabilty(timeslot, settings) and converted_timeslot >= now:
61 converted_timeslots.append(
62 dict(time=converted_timeslot, availability=True))
63 else:
64 converted_timeslots.append(
65 dict(time=converted_timeslot, availability=False))
66 date_required = datetime.datetime.strptime(
67 date + ' 00:00:00', format_string).date()
68 converted_timeslots = filter_timeslots(date_required, converted_timeslots)
69 return converted_timeslots
pranav nachnekar27910542019-09-03 12:04:52 +053070
71def get_available_slots_between(query_start_time, query_end_time, settings):
0Pranav75db6f72019-11-07 12:47:00 +053072 records = _get_records(query_start_time, query_end_time, settings)
73 timeslots = []
74 appointment_duration = datetime.timedelta(
75 minutes=settings.appointment_duration)
76 for record in records:
77 if record.day_of_week == WEEKDAYS[query_start_time.weekday()]:
78 current_time = _deltatime_to_datetime(
79 query_start_time, record.from_time)
80 end_time = _deltatime_to_datetime(
81 query_start_time, record.to_time)
82 else:
83 current_time = _deltatime_to_datetime(
84 query_end_time, record.from_time)
85 end_time = _deltatime_to_datetime(
86 query_end_time, record.to_time)
87 while current_time + appointment_duration <= end_time:
88 timeslots.append(current_time)
89 current_time += appointment_duration
90 return timeslots
pranav nachnekar27910542019-09-03 12:04:52 +053091
Pranav Nachanekar110f4ea2019-09-09 17:04:25 +053092
93@frappe.whitelist(allow_guest=True)
0Pranave4941442019-10-31 15:38:39 +053094def create_appointment(date, time, tz, contact):
0Pranav75db6f72019-11-07 12:47:00 +053095 import pytz
96 appointment = frappe.new_doc('Appointment')
97 format_string = '%Y-%m-%d %H:%M:%S%z'
98 scheduled_time = datetime.datetime.strptime(
99 date+" "+time, format_string)
100 scheduled_time = scheduled_time.replace(tzinfo=None)
101 scheduled_time = convert_to_system_timezone(tz,scheduled_time)
102 scheduled_time= scheduled_time.replace(tzinfo=None)
103 appointment.scheduled_time = scheduled_time
104 contact = json.loads(contact)
105 appointment.customer_name = contact['name']
106 appointment.customer_phone_number = contact['number']
107 appointment.customer_skype = contact['skype']
108 appointment.customer_details = contact['notes']
109 appointment.customer_email = contact['email']
110 appointment.status = 'Open'
111 appointment.insert()
pranav nachnekar27910542019-09-03 12:04:52 +0530112
pranav nachnekarc5b2a582019-09-03 14:16:47 +0530113# Helper Functions
Pranav Nachanekar110f4ea2019-09-09 17:04:25 +0530114def filter_timeslots(date, timeslots):
0Pranav75db6f72019-11-07 12:47:00 +0530115 filtered_timeslots = []
116 for timeslot in timeslots:
117 if(timeslot['time'].date() == date):
118 filtered_timeslots.append(timeslot)
119 return filtered_timeslots
pranav nachnekar27910542019-09-03 12:04:52 +0530120
0Pranave4941442019-10-31 15:38:39 +0530121def convert_to_guest_timezone(guest_tz,datetimeobject):
0Pranav75db6f72019-11-07 12:47:00 +0530122 import pytz
123 guest_tz = pytz.timezone(guest_tz)
124 local_timezone = pytz.timezone(frappe.utils.get_time_zone())
125 datetimeobject = local_timezone.localize(datetimeobject)
126 datetimeobject = datetimeobject.astimezone(guest_tz)
127 return datetimeobject
0Pranave4941442019-10-31 15:38:39 +0530128
0Pranav36098722019-11-01 12:06:42 +0530129def convert_to_system_timezone(guest_tz,datetimeobject):
0Pranav75db6f72019-11-07 12:47:00 +0530130 import pytz
131 guest_tz = pytz.timezone(guest_tz)
132 datetimeobject = guest_tz.localize(datetimeobject)
133 system_tz = pytz.timezone(frappe.utils.get_time_zone())
134 datetimeobject = datetimeobject.astimezone(system_tz)
135 return datetimeobject
Pranav Nachanekar25148d02019-10-04 11:32:39 +0530136
Pranav Nachanekar110f4ea2019-09-09 17:04:25 +0530137def check_availabilty(timeslot, settings):
0Pranav75db6f72019-11-07 12:47:00 +0530138 return frappe.db.count('Appointment', {'scheduled_time': timeslot}) < settings.number_of_agents
Pranav Nachanekar110f4ea2019-09-09 17:04:25 +0530139
pranav nachnekar27910542019-09-03 12:04:52 +0530140def _is_holiday(date, holiday_list):
0Pranav75db6f72019-11-07 12:47:00 +0530141 for holiday in holiday_list.holidays:
142 if holiday.holiday_date == date:
143 return True
144 return False
pranav nachnekar27910542019-09-03 12:04:52 +0530145
Pranav Nachanekar25148d02019-10-04 11:32:39 +0530146
pranav nachnekar27910542019-09-03 12:04:52 +0530147def _get_records(start_time, end_time, settings):
0Pranav75db6f72019-11-07 12:47:00 +0530148 records = []
149 for record in settings.availability_of_slots:
150 if record.day_of_week == WEEKDAYS[start_time.weekday()] or record.day_of_week == WEEKDAYS[end_time.weekday()]:
151 records.append(record)
152 return records
pranav nachnekar27910542019-09-03 12:04:52 +0530153
Pranav Nachanekar25148d02019-10-04 11:32:39 +0530154
pranav nachnekar27910542019-09-03 12:04:52 +0530155def _deltatime_to_datetime(date, deltatime):
0Pranav75db6f72019-11-07 12:47:00 +0530156 time = (datetime.datetime.min + deltatime).time()
157 return datetime.datetime.combine(date.date(), time)
pranav nachnekar27910542019-09-03 12:04:52 +0530158
Pranav Nachanekar25148d02019-10-04 11:32:39 +0530159
pranav nachnekar27910542019-09-03 12:04:52 +0530160def _datetime_to_deltatime(date_time):
0Pranav75db6f72019-11-07 12:47:00 +0530161 midnight = datetime.datetime.combine(date_time.date(), datetime.time.min)
162 return (date_time-midnight)