blob: e4af7e8e43681ac89865054dda3af3ccad95ded3 [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():
0Pranav793ba8f2019-11-14 11:25:49 +053028 from babel.dates import get_timezone, get_timezone_name, Locale
29 from frappe.utils.momentjs import get_all_timezones
30
31 translated_dict = {}
32 locale = Locale.parse(frappe.local.lang, sep="-")
33
34 for tz in get_all_timezones():
35 timezone_name = get_timezone_name(get_timezone(tz), locale=locale, width='short')
36 if timezone_name:
37 translated_dict[tz] = timezone_name + ' - ' + tz
38
39 return translated_dict
pranav nachnekar27910542019-09-03 12:04:52 +053040
41@frappe.whitelist(allow_guest=True)
Pranav Nachanekar110f4ea2019-09-09 17:04:25 +053042def get_appointment_slots(date, timezone):
0Pranav75db6f72019-11-07 12:47:00 +053043 # Convert query to local timezones
44 format_string = '%Y-%m-%d %H:%M:%S'
0Pranav76b20a52019-11-07 13:24:59 +053045 query_start_time = datetime.datetime.strptime(date + ' 00:00:00', format_string)
46 query_end_time = datetime.datetime.strptime(date + ' 23:59:59', format_string)
0Pranav51208b32019-11-07 12:54:48 +053047 query_start_time = convert_to_system_timezone(timezone, query_start_time)
48 query_end_time = convert_to_system_timezone(timezone, query_end_time)
49 now = convert_to_guest_timezone(timezone, datetime.datetime.now())
0Pranave4941442019-10-31 15:38:39 +053050
0Pranav75db6f72019-11-07 12:47:00 +053051 # Database queries
52 settings = frappe.get_doc('Appointment Booking Settings')
53 holiday_list = frappe.get_doc('Holiday List', settings.holiday_list)
0Pranav76b20a52019-11-07 13:24:59 +053054 timeslots = get_available_slots_between(query_start_time, query_end_time, settings)
Pranav Nachanekar110f4ea2019-09-09 17:04:25 +053055
0Pranav75db6f72019-11-07 12:47:00 +053056 # Filter and convert timeslots
57 converted_timeslots = []
58 for timeslot in timeslots:
0Pranav51208b32019-11-07 12:54:48 +053059 converted_timeslot = convert_to_guest_timezone(timezone, timeslot)
0Pranav75db6f72019-11-07 12:47:00 +053060 # Check if holiday
61 if _is_holiday(converted_timeslot.date(), holiday_list):
0Pranav76b20a52019-11-07 13:24:59 +053062 converted_timeslots.append(dict(time=converted_timeslot, availability=False))
0Pranav75db6f72019-11-07 12:47:00 +053063 continue
64 # Check availability
65 if check_availabilty(timeslot, settings) and converted_timeslot >= now:
0Pranav76b20a52019-11-07 13:24:59 +053066 converted_timeslots.append(dict(time=converted_timeslot, availability=True))
0Pranav75db6f72019-11-07 12:47:00 +053067 else:
0Pranav76b20a52019-11-07 13:24:59 +053068 converted_timeslots.append(dict(time=converted_timeslot, availability=False))
69 date_required = datetime.datetime.strptime(date + ' 00:00:00', format_string).date()
0Pranav75db6f72019-11-07 12:47:00 +053070 converted_timeslots = filter_timeslots(date_required, converted_timeslots)
71 return converted_timeslots
pranav nachnekar27910542019-09-03 12:04:52 +053072
73def get_available_slots_between(query_start_time, query_end_time, settings):
0Pranav75db6f72019-11-07 12:47:00 +053074 records = _get_records(query_start_time, query_end_time, settings)
75 timeslots = []
76 appointment_duration = datetime.timedelta(
77 minutes=settings.appointment_duration)
78 for record in records:
79 if record.day_of_week == WEEKDAYS[query_start_time.weekday()]:
0Pranav76b20a52019-11-07 13:24:59 +053080 current_time = _deltatime_to_datetime(query_start_time, record.from_time)
81 end_time = _deltatime_to_datetime(query_start_time, record.to_time)
0Pranav75db6f72019-11-07 12:47:00 +053082 else:
0Pranav76b20a52019-11-07 13:24:59 +053083 current_time = _deltatime_to_datetime(query_end_time, record.from_time)
84 end_time = _deltatime_to_datetime(query_end_time, record.to_time)
0Pranav75db6f72019-11-07 12:47:00 +053085 while current_time + appointment_duration <= end_time:
86 timeslots.append(current_time)
87 current_time += appointment_duration
88 return timeslots
pranav nachnekar27910542019-09-03 12:04:52 +053089
Pranav Nachanekar110f4ea2019-09-09 17:04:25 +053090
91@frappe.whitelist(allow_guest=True)
0Pranave4941442019-10-31 15:38:39 +053092def create_appointment(date, time, tz, contact):
0Pranavd3605d22019-12-11 15:02:23 +053093 format_string = '%Y-%m-%d %H:%M:%S'
0Pranav83100c92019-11-07 13:37:11 +053094 scheduled_time = datetime.datetime.strptime(date + " " + time, format_string)
95 # Strip tzinfo from datetime objects since it's handled by the doctype
0Pranavf25e2a22019-11-13 12:01:36 +053096 scheduled_time = scheduled_time.replace(tzinfo = None)
0Pranav51208b32019-11-07 12:54:48 +053097 scheduled_time = convert_to_system_timezone(tz, scheduled_time)
0Pranavf25e2a22019-11-13 12:01:36 +053098 scheduled_time = scheduled_time.replace(tzinfo = None)
0Pranav83100c92019-11-07 13:37:11 +053099 # Create a appointment document from form
0Pranav83100c92019-11-07 13:37:11 +0530100 appointment = frappe.new_doc('Appointment')
0Pranavdb64c692019-11-13 11:12:38 +0530101 appointment.scheduled_time = scheduled_time
0Pranav75db6f72019-11-07 12:47:00 +0530102 contact = json.loads(contact)
0Pranavf25e2a22019-11-13 12:01:36 +0530103 appointment.customer_name = contact.get('name', None)
0Pranav51208b32019-11-07 12:54:48 +0530104 appointment.customer_phone_number = contact.get('number', None)
105 appointment.customer_skype = contact.get('skype', None)
106 appointment.customer_details = contact.get('notes', None)
107 appointment.customer_email = contact.get('email', None)
0Pranav75db6f72019-11-07 12:47:00 +0530108 appointment.status = 'Open'
109 appointment.insert()
0Pranav511780a2019-11-14 12:47:08 +0530110 return appointment
pranav nachnekar27910542019-09-03 12:04:52 +0530111
pranav nachnekarc5b2a582019-09-03 14:16:47 +0530112# Helper Functions
Pranav Nachanekar110f4ea2019-09-09 17:04:25 +0530113def filter_timeslots(date, timeslots):
0Pranav75db6f72019-11-07 12:47:00 +0530114 filtered_timeslots = []
115 for timeslot in timeslots:
116 if(timeslot['time'].date() == date):
117 filtered_timeslots.append(timeslot)
118 return filtered_timeslots
pranav nachnekar27910542019-09-03 12:04:52 +0530119
0Pranavf25e2a22019-11-13 12:01:36 +0530120def convert_to_guest_timezone(guest_tz, datetimeobject):
0Pranav75db6f72019-11-07 12:47:00 +0530121 guest_tz = pytz.timezone(guest_tz)
122 local_timezone = pytz.timezone(frappe.utils.get_time_zone())
123 datetimeobject = local_timezone.localize(datetimeobject)
124 datetimeobject = datetimeobject.astimezone(guest_tz)
125 return datetimeobject
0Pranave4941442019-10-31 15:38:39 +0530126
0Pranav36098722019-11-01 12:06:42 +0530127def convert_to_system_timezone(guest_tz,datetimeobject):
0Pranav75db6f72019-11-07 12:47:00 +0530128 guest_tz = pytz.timezone(guest_tz)
129 datetimeobject = guest_tz.localize(datetimeobject)
130 system_tz = pytz.timezone(frappe.utils.get_time_zone())
131 datetimeobject = datetimeobject.astimezone(system_tz)
132 return datetimeobject
Pranav Nachanekar25148d02019-10-04 11:32:39 +0530133
Pranav Nachanekar110f4ea2019-09-09 17:04:25 +0530134def check_availabilty(timeslot, settings):
0Pranav75db6f72019-11-07 12:47:00 +0530135 return frappe.db.count('Appointment', {'scheduled_time': timeslot}) < settings.number_of_agents
Pranav Nachanekar110f4ea2019-09-09 17:04:25 +0530136
pranav nachnekar27910542019-09-03 12:04:52 +0530137def _is_holiday(date, holiday_list):
0Pranav75db6f72019-11-07 12:47:00 +0530138 for holiday in holiday_list.holidays:
139 if holiday.holiday_date == date:
140 return True
141 return False
pranav nachnekar27910542019-09-03 12:04:52 +0530142
Pranav Nachanekar25148d02019-10-04 11:32:39 +0530143
pranav nachnekar27910542019-09-03 12:04:52 +0530144def _get_records(start_time, end_time, settings):
0Pranav75db6f72019-11-07 12:47:00 +0530145 records = []
146 for record in settings.availability_of_slots:
147 if record.day_of_week == WEEKDAYS[start_time.weekday()] or record.day_of_week == WEEKDAYS[end_time.weekday()]:
148 records.append(record)
149 return records
pranav nachnekar27910542019-09-03 12:04:52 +0530150
Pranav Nachanekar25148d02019-10-04 11:32:39 +0530151
pranav nachnekar27910542019-09-03 12:04:52 +0530152def _deltatime_to_datetime(date, deltatime):
0Pranav75db6f72019-11-07 12:47:00 +0530153 time = (datetime.datetime.min + deltatime).time()
154 return datetime.datetime.combine(date.date(), time)
pranav nachnekar27910542019-09-03 12:04:52 +0530155
Pranav Nachanekar25148d02019-10-04 11:32:39 +0530156
pranav nachnekar27910542019-09-03 12:04:52 +0530157def _datetime_to_deltatime(date_time):
0Pranav75db6f72019-11-07 12:47:00 +0530158 midnight = datetime.datetime.combine(date_time.date(), datetime.time.min)
159 return (date_time-midnight)