blob: 8cda3c1b90de1296a29e0a294b6aba49fa745eea [file] [log] [blame]
pranav nachnekar27910542019-09-03 12:04:52 +05301import datetime
pranav nachnekarc5b2a582019-09-03 14:16:47 +05302import json
Chillar Anand915b3432021-09-02 16:44:59 +05303
4import frappe
Pranav Nachanekar93670fe2019-10-03 11:58:02 +05305import pytz
Mohammed Yusuf Shaikh86f418392021-07-02 12:19:24 +05306from frappe import _
Pranav Nachanekar91a56492019-09-17 16:58:41 +05307
0Pranav76b20a52019-11-07 13:24:59 +05308WEEKDAYS = ["Monday", "Tuesday", "Wednesday", "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):
0Pranav76b20a52019-11-07 13:24:59 +053014 is_enabled = frappe.db.get_single_value('Appointment Booking Settings', 'enable_scheduling')
0Pranav75db6f72019-11-07 12:47:00 +053015 if is_enabled:
16 return context
17 else:
Mohammed Yusuf Shaikh86f418392021-07-02 12:19:24 +053018 frappe.redirect_to_message(_("Appointment Scheduling Disabled"), _("Appointment Scheduling has been disabled for this site"),
19 http_status_code=302, indicator_color="red")
0Pranav0671ea82019-11-07 13:31:56 +053020 raise frappe.Redirect
Pranav Nachanekar110f4ea2019-09-09 17:04:25 +053021
pranav nachnekar27910542019-09-03 12:04:52 +053022@frappe.whitelist(allow_guest=True)
23def get_appointment_settings():
0Pranav75db6f72019-11-07 12:47:00 +053024 settings = frappe.get_doc('Appointment Booking Settings')
25 settings.holiday_list = frappe.get_doc('Holiday List', settings.holiday_list)
26 return settings
pranav nachnekar27910542019-09-03 12:04:52 +053027
pranav nachnekar27910542019-09-03 12:04:52 +053028@frappe.whitelist(allow_guest=True)
29def get_timezones():
0Pranav84ae2cc2019-12-18 15:44:04 +053030 import pytz
31 return pytz.all_timezones
pranav nachnekar27910542019-09-03 12:04:52 +053032
33@frappe.whitelist(allow_guest=True)
Pranav Nachanekar110f4ea2019-09-09 17:04:25 +053034def get_appointment_slots(date, timezone):
0Pranav75db6f72019-11-07 12:47:00 +053035 # Convert query to local timezones
36 format_string = '%Y-%m-%d %H:%M:%S'
0Pranav76b20a52019-11-07 13:24:59 +053037 query_start_time = datetime.datetime.strptime(date + ' 00:00:00', format_string)
38 query_end_time = datetime.datetime.strptime(date + ' 23:59:59', format_string)
0Pranav51208b32019-11-07 12:54:48 +053039 query_start_time = convert_to_system_timezone(timezone, query_start_time)
40 query_end_time = convert_to_system_timezone(timezone, query_end_time)
41 now = convert_to_guest_timezone(timezone, datetime.datetime.now())
0Pranave4941442019-10-31 15:38:39 +053042
0Pranav75db6f72019-11-07 12:47:00 +053043 # Database queries
44 settings = frappe.get_doc('Appointment Booking Settings')
45 holiday_list = frappe.get_doc('Holiday List', settings.holiday_list)
0Pranav76b20a52019-11-07 13:24:59 +053046 timeslots = get_available_slots_between(query_start_time, query_end_time, settings)
Pranav Nachanekar110f4ea2019-09-09 17:04:25 +053047
0Pranav75db6f72019-11-07 12:47:00 +053048 # Filter and convert timeslots
49 converted_timeslots = []
50 for timeslot in timeslots:
0Pranav51208b32019-11-07 12:54:48 +053051 converted_timeslot = convert_to_guest_timezone(timezone, timeslot)
0Pranav75db6f72019-11-07 12:47:00 +053052 # Check if holiday
53 if _is_holiday(converted_timeslot.date(), holiday_list):
0Pranav76b20a52019-11-07 13:24:59 +053054 converted_timeslots.append(dict(time=converted_timeslot, availability=False))
0Pranav75db6f72019-11-07 12:47:00 +053055 continue
56 # Check availability
57 if check_availabilty(timeslot, settings) and converted_timeslot >= now:
0Pranav76b20a52019-11-07 13:24:59 +053058 converted_timeslots.append(dict(time=converted_timeslot, availability=True))
0Pranav75db6f72019-11-07 12:47:00 +053059 else:
0Pranav76b20a52019-11-07 13:24:59 +053060 converted_timeslots.append(dict(time=converted_timeslot, availability=False))
61 date_required = datetime.datetime.strptime(date + ' 00:00:00', format_string).date()
0Pranav75db6f72019-11-07 12:47:00 +053062 converted_timeslots = filter_timeslots(date_required, converted_timeslots)
63 return converted_timeslots
pranav nachnekar27910542019-09-03 12:04:52 +053064
65def get_available_slots_between(query_start_time, query_end_time, settings):
0Pranav75db6f72019-11-07 12:47:00 +053066 records = _get_records(query_start_time, query_end_time, settings)
67 timeslots = []
68 appointment_duration = datetime.timedelta(
69 minutes=settings.appointment_duration)
70 for record in records:
71 if record.day_of_week == WEEKDAYS[query_start_time.weekday()]:
0Pranav76b20a52019-11-07 13:24:59 +053072 current_time = _deltatime_to_datetime(query_start_time, record.from_time)
73 end_time = _deltatime_to_datetime(query_start_time, record.to_time)
0Pranav75db6f72019-11-07 12:47:00 +053074 else:
0Pranav76b20a52019-11-07 13:24:59 +053075 current_time = _deltatime_to_datetime(query_end_time, record.from_time)
76 end_time = _deltatime_to_datetime(query_end_time, record.to_time)
0Pranav75db6f72019-11-07 12:47:00 +053077 while current_time + appointment_duration <= end_time:
78 timeslots.append(current_time)
79 current_time += appointment_duration
80 return timeslots
pranav nachnekar27910542019-09-03 12:04:52 +053081
Pranav Nachanekar110f4ea2019-09-09 17:04:25 +053082
83@frappe.whitelist(allow_guest=True)
0Pranave4941442019-10-31 15:38:39 +053084def create_appointment(date, time, tz, contact):
0Pranavd3605d22019-12-11 15:02:23 +053085 format_string = '%Y-%m-%d %H:%M:%S'
0Pranav83100c92019-11-07 13:37:11 +053086 scheduled_time = datetime.datetime.strptime(date + " " + time, format_string)
87 # Strip tzinfo from datetime objects since it's handled by the doctype
0Pranavf25e2a22019-11-13 12:01:36 +053088 scheduled_time = scheduled_time.replace(tzinfo = None)
0Pranav51208b32019-11-07 12:54:48 +053089 scheduled_time = convert_to_system_timezone(tz, scheduled_time)
0Pranavf25e2a22019-11-13 12:01:36 +053090 scheduled_time = scheduled_time.replace(tzinfo = None)
0Pranav83100c92019-11-07 13:37:11 +053091 # Create a appointment document from form
0Pranav83100c92019-11-07 13:37:11 +053092 appointment = frappe.new_doc('Appointment')
0Pranavdb64c692019-11-13 11:12:38 +053093 appointment.scheduled_time = scheduled_time
0Pranav75db6f72019-11-07 12:47:00 +053094 contact = json.loads(contact)
0Pranavf25e2a22019-11-13 12:01:36 +053095 appointment.customer_name = contact.get('name', None)
0Pranav51208b32019-11-07 12:54:48 +053096 appointment.customer_phone_number = contact.get('number', None)
97 appointment.customer_skype = contact.get('skype', None)
98 appointment.customer_details = contact.get('notes', None)
99 appointment.customer_email = contact.get('email', None)
0Pranav75db6f72019-11-07 12:47:00 +0530100 appointment.status = 'Open'
101 appointment.insert()
0Pranav511780a2019-11-14 12:47:08 +0530102 return appointment
pranav nachnekar27910542019-09-03 12:04:52 +0530103
pranav nachnekarc5b2a582019-09-03 14:16:47 +0530104# Helper Functions
Pranav Nachanekar110f4ea2019-09-09 17:04:25 +0530105def filter_timeslots(date, timeslots):
0Pranav75db6f72019-11-07 12:47:00 +0530106 filtered_timeslots = []
107 for timeslot in timeslots:
108 if(timeslot['time'].date() == date):
109 filtered_timeslots.append(timeslot)
110 return filtered_timeslots
pranav nachnekar27910542019-09-03 12:04:52 +0530111
0Pranavf25e2a22019-11-13 12:01:36 +0530112def convert_to_guest_timezone(guest_tz, datetimeobject):
0Pranav75db6f72019-11-07 12:47:00 +0530113 guest_tz = pytz.timezone(guest_tz)
114 local_timezone = pytz.timezone(frappe.utils.get_time_zone())
115 datetimeobject = local_timezone.localize(datetimeobject)
116 datetimeobject = datetimeobject.astimezone(guest_tz)
117 return datetimeobject
0Pranave4941442019-10-31 15:38:39 +0530118
0Pranav36098722019-11-01 12:06:42 +0530119def convert_to_system_timezone(guest_tz,datetimeobject):
0Pranav75db6f72019-11-07 12:47:00 +0530120 guest_tz = pytz.timezone(guest_tz)
121 datetimeobject = guest_tz.localize(datetimeobject)
122 system_tz = pytz.timezone(frappe.utils.get_time_zone())
123 datetimeobject = datetimeobject.astimezone(system_tz)
124 return datetimeobject
Pranav Nachanekar25148d02019-10-04 11:32:39 +0530125
Pranav Nachanekar110f4ea2019-09-09 17:04:25 +0530126def check_availabilty(timeslot, settings):
0Pranav75db6f72019-11-07 12:47:00 +0530127 return frappe.db.count('Appointment', {'scheduled_time': timeslot}) < settings.number_of_agents
Pranav Nachanekar110f4ea2019-09-09 17:04:25 +0530128
pranav nachnekar27910542019-09-03 12:04:52 +0530129def _is_holiday(date, holiday_list):
0Pranav75db6f72019-11-07 12:47:00 +0530130 for holiday in holiday_list.holidays:
131 if holiday.holiday_date == date:
132 return True
133 return False
pranav nachnekar27910542019-09-03 12:04:52 +0530134
Pranav Nachanekar25148d02019-10-04 11:32:39 +0530135
pranav nachnekar27910542019-09-03 12:04:52 +0530136def _get_records(start_time, end_time, settings):
0Pranav75db6f72019-11-07 12:47:00 +0530137 records = []
138 for record in settings.availability_of_slots:
139 if record.day_of_week == WEEKDAYS[start_time.weekday()] or record.day_of_week == WEEKDAYS[end_time.weekday()]:
140 records.append(record)
141 return records
pranav nachnekar27910542019-09-03 12:04:52 +0530142
Pranav Nachanekar25148d02019-10-04 11:32:39 +0530143
pranav nachnekar27910542019-09-03 12:04:52 +0530144def _deltatime_to_datetime(date, deltatime):
0Pranav75db6f72019-11-07 12:47:00 +0530145 time = (datetime.datetime.min + deltatime).time()
146 return datetime.datetime.combine(date.date(), time)
pranav nachnekar27910542019-09-03 12:04:52 +0530147
Pranav Nachanekar25148d02019-10-04 11:32:39 +0530148
pranav nachnekar27910542019-09-03 12:04:52 +0530149def _datetime_to_deltatime(date_time):
0Pranav75db6f72019-11-07 12:47:00 +0530150 midnight = datetime.datetime.combine(date_time.date(), datetime.time.min)
Mohammed Yusuf Shaikh86f418392021-07-02 12:19:24 +0530151 return (date_time-midnight)