blob: 4f455614ba1a851300e8bd26292c9737b9196dc8 [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
Mohammed Yusuf Shaikh86f418392021-07-02 12:19:24 +05305from frappe import _
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:
Mohammed Yusuf Shaikh86f418392021-07-02 12:19:24 +053017 frappe.redirect_to_message(_("Appointment Scheduling Disabled"), _("Appointment Scheduling has been disabled for this site"),
18 http_status_code=302, indicator_color="red")
0Pranav0671ea82019-11-07 13:31:56 +053019 raise frappe.Redirect
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():
0Pranav84ae2cc2019-12-18 15:44:04 +053029 import pytz
30 return pytz.all_timezones
pranav nachnekar27910542019-09-03 12:04:52 +053031
32@frappe.whitelist(allow_guest=True)
Pranav Nachanekar110f4ea2019-09-09 17:04:25 +053033def get_appointment_slots(date, timezone):
0Pranav75db6f72019-11-07 12:47:00 +053034 # Convert query to local timezones
35 format_string = '%Y-%m-%d %H:%M:%S'
0Pranav76b20a52019-11-07 13:24:59 +053036 query_start_time = datetime.datetime.strptime(date + ' 00:00:00', format_string)
37 query_end_time = datetime.datetime.strptime(date + ' 23:59:59', format_string)
0Pranav51208b32019-11-07 12:54:48 +053038 query_start_time = convert_to_system_timezone(timezone, query_start_time)
39 query_end_time = convert_to_system_timezone(timezone, query_end_time)
40 now = convert_to_guest_timezone(timezone, datetime.datetime.now())
0Pranave4941442019-10-31 15:38:39 +053041
0Pranav75db6f72019-11-07 12:47:00 +053042 # Database queries
43 settings = frappe.get_doc('Appointment Booking Settings')
44 holiday_list = frappe.get_doc('Holiday List', settings.holiday_list)
0Pranav76b20a52019-11-07 13:24:59 +053045 timeslots = get_available_slots_between(query_start_time, query_end_time, settings)
Pranav Nachanekar110f4ea2019-09-09 17:04:25 +053046
0Pranav75db6f72019-11-07 12:47:00 +053047 # Filter and convert timeslots
48 converted_timeslots = []
49 for timeslot in timeslots:
0Pranav51208b32019-11-07 12:54:48 +053050 converted_timeslot = convert_to_guest_timezone(timezone, timeslot)
0Pranav75db6f72019-11-07 12:47:00 +053051 # Check if holiday
52 if _is_holiday(converted_timeslot.date(), holiday_list):
0Pranav76b20a52019-11-07 13:24:59 +053053 converted_timeslots.append(dict(time=converted_timeslot, availability=False))
0Pranav75db6f72019-11-07 12:47:00 +053054 continue
55 # Check availability
56 if check_availabilty(timeslot, settings) and converted_timeslot >= now:
0Pranav76b20a52019-11-07 13:24:59 +053057 converted_timeslots.append(dict(time=converted_timeslot, availability=True))
0Pranav75db6f72019-11-07 12:47:00 +053058 else:
0Pranav76b20a52019-11-07 13:24:59 +053059 converted_timeslots.append(dict(time=converted_timeslot, availability=False))
60 date_required = datetime.datetime.strptime(date + ' 00:00:00', format_string).date()
0Pranav75db6f72019-11-07 12:47:00 +053061 converted_timeslots = filter_timeslots(date_required, converted_timeslots)
62 return converted_timeslots
pranav nachnekar27910542019-09-03 12:04:52 +053063
64def get_available_slots_between(query_start_time, query_end_time, settings):
0Pranav75db6f72019-11-07 12:47:00 +053065 records = _get_records(query_start_time, query_end_time, settings)
66 timeslots = []
67 appointment_duration = datetime.timedelta(
68 minutes=settings.appointment_duration)
69 for record in records:
70 if record.day_of_week == WEEKDAYS[query_start_time.weekday()]:
0Pranav76b20a52019-11-07 13:24:59 +053071 current_time = _deltatime_to_datetime(query_start_time, record.from_time)
72 end_time = _deltatime_to_datetime(query_start_time, record.to_time)
0Pranav75db6f72019-11-07 12:47:00 +053073 else:
0Pranav76b20a52019-11-07 13:24:59 +053074 current_time = _deltatime_to_datetime(query_end_time, record.from_time)
75 end_time = _deltatime_to_datetime(query_end_time, record.to_time)
0Pranav75db6f72019-11-07 12:47:00 +053076 while current_time + appointment_duration <= end_time:
77 timeslots.append(current_time)
78 current_time += appointment_duration
79 return timeslots
pranav nachnekar27910542019-09-03 12:04:52 +053080
Pranav Nachanekar110f4ea2019-09-09 17:04:25 +053081
82@frappe.whitelist(allow_guest=True)
0Pranave4941442019-10-31 15:38:39 +053083def create_appointment(date, time, tz, contact):
0Pranavd3605d22019-12-11 15:02:23 +053084 format_string = '%Y-%m-%d %H:%M:%S'
0Pranav83100c92019-11-07 13:37:11 +053085 scheduled_time = datetime.datetime.strptime(date + " " + time, format_string)
86 # Strip tzinfo from datetime objects since it's handled by the doctype
0Pranavf25e2a22019-11-13 12:01:36 +053087 scheduled_time = scheduled_time.replace(tzinfo = None)
0Pranav51208b32019-11-07 12:54:48 +053088 scheduled_time = convert_to_system_timezone(tz, scheduled_time)
0Pranavf25e2a22019-11-13 12:01:36 +053089 scheduled_time = scheduled_time.replace(tzinfo = None)
0Pranav83100c92019-11-07 13:37:11 +053090 # Create a appointment document from form
0Pranav83100c92019-11-07 13:37:11 +053091 appointment = frappe.new_doc('Appointment')
0Pranavdb64c692019-11-13 11:12:38 +053092 appointment.scheduled_time = scheduled_time
0Pranav75db6f72019-11-07 12:47:00 +053093 contact = json.loads(contact)
0Pranavf25e2a22019-11-13 12:01:36 +053094 appointment.customer_name = contact.get('name', None)
0Pranav51208b32019-11-07 12:54:48 +053095 appointment.customer_phone_number = contact.get('number', None)
96 appointment.customer_skype = contact.get('skype', None)
97 appointment.customer_details = contact.get('notes', None)
98 appointment.customer_email = contact.get('email', None)
0Pranav75db6f72019-11-07 12:47:00 +053099 appointment.status = 'Open'
100 appointment.insert()
0Pranav511780a2019-11-14 12:47:08 +0530101 return appointment
pranav nachnekar27910542019-09-03 12:04:52 +0530102
pranav nachnekarc5b2a582019-09-03 14:16:47 +0530103# Helper Functions
Pranav Nachanekar110f4ea2019-09-09 17:04:25 +0530104def filter_timeslots(date, timeslots):
0Pranav75db6f72019-11-07 12:47:00 +0530105 filtered_timeslots = []
106 for timeslot in timeslots:
107 if(timeslot['time'].date() == date):
108 filtered_timeslots.append(timeslot)
109 return filtered_timeslots
pranav nachnekar27910542019-09-03 12:04:52 +0530110
0Pranavf25e2a22019-11-13 12:01:36 +0530111def convert_to_guest_timezone(guest_tz, datetimeobject):
0Pranav75db6f72019-11-07 12:47:00 +0530112 guest_tz = pytz.timezone(guest_tz)
113 local_timezone = pytz.timezone(frappe.utils.get_time_zone())
114 datetimeobject = local_timezone.localize(datetimeobject)
115 datetimeobject = datetimeobject.astimezone(guest_tz)
116 return datetimeobject
0Pranave4941442019-10-31 15:38:39 +0530117
0Pranav36098722019-11-01 12:06:42 +0530118def convert_to_system_timezone(guest_tz,datetimeobject):
0Pranav75db6f72019-11-07 12:47:00 +0530119 guest_tz = pytz.timezone(guest_tz)
120 datetimeobject = guest_tz.localize(datetimeobject)
121 system_tz = pytz.timezone(frappe.utils.get_time_zone())
122 datetimeobject = datetimeobject.astimezone(system_tz)
123 return datetimeobject
Pranav Nachanekar25148d02019-10-04 11:32:39 +0530124
Pranav Nachanekar110f4ea2019-09-09 17:04:25 +0530125def check_availabilty(timeslot, settings):
0Pranav75db6f72019-11-07 12:47:00 +0530126 return frappe.db.count('Appointment', {'scheduled_time': timeslot}) < settings.number_of_agents
Pranav Nachanekar110f4ea2019-09-09 17:04:25 +0530127
pranav nachnekar27910542019-09-03 12:04:52 +0530128def _is_holiday(date, holiday_list):
0Pranav75db6f72019-11-07 12:47:00 +0530129 for holiday in holiday_list.holidays:
130 if holiday.holiday_date == date:
131 return True
132 return False
pranav nachnekar27910542019-09-03 12:04:52 +0530133
Pranav Nachanekar25148d02019-10-04 11:32:39 +0530134
pranav nachnekar27910542019-09-03 12:04:52 +0530135def _get_records(start_time, end_time, settings):
0Pranav75db6f72019-11-07 12:47:00 +0530136 records = []
137 for record in settings.availability_of_slots:
138 if record.day_of_week == WEEKDAYS[start_time.weekday()] or record.day_of_week == WEEKDAYS[end_time.weekday()]:
139 records.append(record)
140 return records
pranav nachnekar27910542019-09-03 12:04:52 +0530141
Pranav Nachanekar25148d02019-10-04 11:32:39 +0530142
pranav nachnekar27910542019-09-03 12:04:52 +0530143def _deltatime_to_datetime(date, deltatime):
0Pranav75db6f72019-11-07 12:47:00 +0530144 time = (datetime.datetime.min + deltatime).time()
145 return datetime.datetime.combine(date.date(), time)
pranav nachnekar27910542019-09-03 12:04:52 +0530146
Pranav Nachanekar25148d02019-10-04 11:32:39 +0530147
pranav nachnekar27910542019-09-03 12:04:52 +0530148def _datetime_to_deltatime(date_time):
0Pranav75db6f72019-11-07 12:47:00 +0530149 midnight = datetime.datetime.combine(date_time.date(), datetime.time.min)
Mohammed Yusuf Shaikh86f418392021-07-02 12:19:24 +0530150 return (date_time-midnight)