pranav nachnekar | 2791054 | 2019-09-03 12:04:52 +0530 | [diff] [blame] | 1 | import frappe |
| 2 | import datetime |
pranav nachnekar | c5b2a58 | 2019-09-03 14:16:47 +0530 | [diff] [blame] | 3 | import json |
Pranav Nachanekar | 93670fe | 2019-10-03 11:58:02 +0530 | [diff] [blame] | 4 | import pytz |
pranav nachnekar | 2791054 | 2019-09-03 12:04:52 +0530 | [diff] [blame] | 5 | |
Pranav Nachanekar | 91a5649 | 2019-09-17 16:58:41 +0530 | [diff] [blame] | 6 | |
0Pranav | 76b20a5 | 2019-11-07 13:24:59 +0530 | [diff] [blame] | 7 | WEEKDAYS = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"] |
Pranav Nachanekar | 91a5649 | 2019-09-17 16:58:41 +0530 | [diff] [blame] | 8 | |
Pranav Nachanekar | 63dbacd | 2019-09-09 15:19:57 +0530 | [diff] [blame] | 9 | no_cache = 1 |
| 10 | |
Pranav Nachanekar | 25148d0 | 2019-10-04 11:32:39 +0530 | [diff] [blame] | 11 | |
Pranav Nachanekar | 9e36a9e | 2019-10-04 11:28:29 +0530 | [diff] [blame] | 12 | def get_context(context): |
0Pranav | 76b20a5 | 2019-11-07 13:24:59 +0530 | [diff] [blame] | 13 | is_enabled = frappe.db.get_single_value('Appointment Booking Settings', 'enable_scheduling') |
0Pranav | 75db6f7 | 2019-11-07 12:47:00 +0530 | [diff] [blame] | 14 | if is_enabled: |
| 15 | return context |
| 16 | else: |
0Pranav | 0671ea8 | 2019-11-07 13:31:56 +0530 | [diff] [blame] | 17 | frappe.local.flags.redirect_location = '/404' |
| 18 | raise frappe.Redirect |
Pranav Nachanekar | 110f4ea | 2019-09-09 17:04:25 +0530 | [diff] [blame] | 19 | |
pranav nachnekar | 2791054 | 2019-09-03 12:04:52 +0530 | [diff] [blame] | 20 | @frappe.whitelist(allow_guest=True) |
| 21 | def get_appointment_settings(): |
0Pranav | 75db6f7 | 2019-11-07 12:47:00 +0530 | [diff] [blame] | 22 | settings = frappe.get_doc('Appointment Booking Settings') |
| 23 | settings.holiday_list = frappe.get_doc('Holiday List', settings.holiday_list) |
| 24 | return settings |
pranav nachnekar | 2791054 | 2019-09-03 12:04:52 +0530 | [diff] [blame] | 25 | |
pranav nachnekar | 2791054 | 2019-09-03 12:04:52 +0530 | [diff] [blame] | 26 | @frappe.whitelist(allow_guest=True) |
| 27 | def get_timezones(): |
0Pranav | 75db6f7 | 2019-11-07 12:47:00 +0530 | [diff] [blame] | 28 | return pytz.all_timezones |
pranav nachnekar | 2791054 | 2019-09-03 12:04:52 +0530 | [diff] [blame] | 29 | |
| 30 | @frappe.whitelist(allow_guest=True) |
Pranav Nachanekar | 110f4ea | 2019-09-09 17:04:25 +0530 | [diff] [blame] | 31 | def get_appointment_slots(date, timezone): |
0Pranav | 75db6f7 | 2019-11-07 12:47:00 +0530 | [diff] [blame] | 32 | # Convert query to local timezones |
| 33 | format_string = '%Y-%m-%d %H:%M:%S' |
0Pranav | 76b20a5 | 2019-11-07 13:24:59 +0530 | [diff] [blame] | 34 | query_start_time = datetime.datetime.strptime(date + ' 00:00:00', format_string) |
| 35 | query_end_time = datetime.datetime.strptime(date + ' 23:59:59', format_string) |
0Pranav | 51208b3 | 2019-11-07 12:54:48 +0530 | [diff] [blame] | 36 | query_start_time = convert_to_system_timezone(timezone, query_start_time) |
| 37 | query_end_time = convert_to_system_timezone(timezone, query_end_time) |
| 38 | now = convert_to_guest_timezone(timezone, datetime.datetime.now()) |
0Pranav | e494144 | 2019-10-31 15:38:39 +0530 | [diff] [blame] | 39 | |
0Pranav | 75db6f7 | 2019-11-07 12:47:00 +0530 | [diff] [blame] | 40 | # Database queries |
| 41 | settings = frappe.get_doc('Appointment Booking Settings') |
| 42 | holiday_list = frappe.get_doc('Holiday List', settings.holiday_list) |
0Pranav | 76b20a5 | 2019-11-07 13:24:59 +0530 | [diff] [blame] | 43 | timeslots = get_available_slots_between(query_start_time, query_end_time, settings) |
Pranav Nachanekar | 110f4ea | 2019-09-09 17:04:25 +0530 | [diff] [blame] | 44 | |
0Pranav | 75db6f7 | 2019-11-07 12:47:00 +0530 | [diff] [blame] | 45 | # Filter and convert timeslots |
| 46 | converted_timeslots = [] |
| 47 | for timeslot in timeslots: |
0Pranav | 51208b3 | 2019-11-07 12:54:48 +0530 | [diff] [blame] | 48 | converted_timeslot = convert_to_guest_timezone(timezone, timeslot) |
0Pranav | 75db6f7 | 2019-11-07 12:47:00 +0530 | [diff] [blame] | 49 | # Check if holiday |
| 50 | if _is_holiday(converted_timeslot.date(), holiday_list): |
0Pranav | 76b20a5 | 2019-11-07 13:24:59 +0530 | [diff] [blame] | 51 | converted_timeslots.append(dict(time=converted_timeslot, availability=False)) |
0Pranav | 75db6f7 | 2019-11-07 12:47:00 +0530 | [diff] [blame] | 52 | continue |
| 53 | # Check availability |
| 54 | if check_availabilty(timeslot, settings) and converted_timeslot >= now: |
0Pranav | 76b20a5 | 2019-11-07 13:24:59 +0530 | [diff] [blame] | 55 | converted_timeslots.append(dict(time=converted_timeslot, availability=True)) |
0Pranav | 75db6f7 | 2019-11-07 12:47:00 +0530 | [diff] [blame] | 56 | else: |
0Pranav | 76b20a5 | 2019-11-07 13:24:59 +0530 | [diff] [blame] | 57 | converted_timeslots.append(dict(time=converted_timeslot, availability=False)) |
| 58 | date_required = datetime.datetime.strptime(date + ' 00:00:00', format_string).date() |
0Pranav | 75db6f7 | 2019-11-07 12:47:00 +0530 | [diff] [blame] | 59 | converted_timeslots = filter_timeslots(date_required, converted_timeslots) |
| 60 | return converted_timeslots |
pranav nachnekar | 2791054 | 2019-09-03 12:04:52 +0530 | [diff] [blame] | 61 | |
| 62 | def get_available_slots_between(query_start_time, query_end_time, settings): |
0Pranav | 75db6f7 | 2019-11-07 12:47:00 +0530 | [diff] [blame] | 63 | records = _get_records(query_start_time, query_end_time, settings) |
| 64 | timeslots = [] |
| 65 | appointment_duration = datetime.timedelta( |
| 66 | minutes=settings.appointment_duration) |
| 67 | for record in records: |
| 68 | if record.day_of_week == WEEKDAYS[query_start_time.weekday()]: |
0Pranav | 76b20a5 | 2019-11-07 13:24:59 +0530 | [diff] [blame] | 69 | current_time = _deltatime_to_datetime(query_start_time, record.from_time) |
| 70 | end_time = _deltatime_to_datetime(query_start_time, record.to_time) |
0Pranav | 75db6f7 | 2019-11-07 12:47:00 +0530 | [diff] [blame] | 71 | else: |
0Pranav | 76b20a5 | 2019-11-07 13:24:59 +0530 | [diff] [blame] | 72 | current_time = _deltatime_to_datetime(query_end_time, record.from_time) |
| 73 | end_time = _deltatime_to_datetime(query_end_time, record.to_time) |
0Pranav | 75db6f7 | 2019-11-07 12:47:00 +0530 | [diff] [blame] | 74 | while current_time + appointment_duration <= end_time: |
| 75 | timeslots.append(current_time) |
| 76 | current_time += appointment_duration |
| 77 | return timeslots |
pranav nachnekar | 2791054 | 2019-09-03 12:04:52 +0530 | [diff] [blame] | 78 | |
Pranav Nachanekar | 110f4ea | 2019-09-09 17:04:25 +0530 | [diff] [blame] | 79 | |
| 80 | @frappe.whitelist(allow_guest=True) |
0Pranav | e494144 | 2019-10-31 15:38:39 +0530 | [diff] [blame] | 81 | def create_appointment(date, time, tz, contact): |
0Pranav | 75db6f7 | 2019-11-07 12:47:00 +0530 | [diff] [blame] | 82 | format_string = '%Y-%m-%d %H:%M:%S%z' |
0Pranav | 83100c9 | 2019-11-07 13:37:11 +0530 | [diff] [blame^] | 83 | scheduled_time = datetime.datetime.strptime(date + " " + time, format_string) |
| 84 | # Strip tzinfo from datetime objects since it's handled by the doctype |
0Pranav | 75db6f7 | 2019-11-07 12:47:00 +0530 | [diff] [blame] | 85 | scheduled_time = scheduled_time.replace(tzinfo=None) |
0Pranav | 51208b3 | 2019-11-07 12:54:48 +0530 | [diff] [blame] | 86 | scheduled_time = convert_to_system_timezone(tz, scheduled_time) |
| 87 | scheduled_time = scheduled_time.replace(tzinfo=None) |
0Pranav | 83100c9 | 2019-11-07 13:37:11 +0530 | [diff] [blame^] | 88 | # Create a appointment document from form |
0Pranav | 75db6f7 | 2019-11-07 12:47:00 +0530 | [diff] [blame] | 89 | appointment.scheduled_time = scheduled_time |
0Pranav | 83100c9 | 2019-11-07 13:37:11 +0530 | [diff] [blame^] | 90 | appointment = frappe.new_doc('Appointment') |
0Pranav | 75db6f7 | 2019-11-07 12:47:00 +0530 | [diff] [blame] | 91 | contact = json.loads(contact) |
0Pranav | 51208b3 | 2019-11-07 12:54:48 +0530 | [diff] [blame] | 92 | appointment.customer_name = contact.get('name',None) |
| 93 | appointment.customer_phone_number = contact.get('number', None) |
| 94 | appointment.customer_skype = contact.get('skype', None) |
| 95 | appointment.customer_details = contact.get('notes', None) |
| 96 | appointment.customer_email = contact.get('email', None) |
0Pranav | 75db6f7 | 2019-11-07 12:47:00 +0530 | [diff] [blame] | 97 | appointment.status = 'Open' |
| 98 | appointment.insert() |
pranav nachnekar | 2791054 | 2019-09-03 12:04:52 +0530 | [diff] [blame] | 99 | |
pranav nachnekar | c5b2a58 | 2019-09-03 14:16:47 +0530 | [diff] [blame] | 100 | # Helper Functions |
Pranav Nachanekar | 110f4ea | 2019-09-09 17:04:25 +0530 | [diff] [blame] | 101 | def filter_timeslots(date, timeslots): |
0Pranav | 75db6f7 | 2019-11-07 12:47:00 +0530 | [diff] [blame] | 102 | filtered_timeslots = [] |
| 103 | for timeslot in timeslots: |
| 104 | if(timeslot['time'].date() == date): |
| 105 | filtered_timeslots.append(timeslot) |
| 106 | return filtered_timeslots |
pranav nachnekar | 2791054 | 2019-09-03 12:04:52 +0530 | [diff] [blame] | 107 | |
0Pranav | e494144 | 2019-10-31 15:38:39 +0530 | [diff] [blame] | 108 | def convert_to_guest_timezone(guest_tz,datetimeobject): |
0Pranav | 75db6f7 | 2019-11-07 12:47:00 +0530 | [diff] [blame] | 109 | guest_tz = pytz.timezone(guest_tz) |
| 110 | local_timezone = pytz.timezone(frappe.utils.get_time_zone()) |
| 111 | datetimeobject = local_timezone.localize(datetimeobject) |
| 112 | datetimeobject = datetimeobject.astimezone(guest_tz) |
| 113 | return datetimeobject |
0Pranav | e494144 | 2019-10-31 15:38:39 +0530 | [diff] [blame] | 114 | |
0Pranav | 3609872 | 2019-11-01 12:06:42 +0530 | [diff] [blame] | 115 | def convert_to_system_timezone(guest_tz,datetimeobject): |
0Pranav | 75db6f7 | 2019-11-07 12:47:00 +0530 | [diff] [blame] | 116 | guest_tz = pytz.timezone(guest_tz) |
| 117 | datetimeobject = guest_tz.localize(datetimeobject) |
| 118 | system_tz = pytz.timezone(frappe.utils.get_time_zone()) |
| 119 | datetimeobject = datetimeobject.astimezone(system_tz) |
| 120 | return datetimeobject |
Pranav Nachanekar | 25148d0 | 2019-10-04 11:32:39 +0530 | [diff] [blame] | 121 | |
Pranav Nachanekar | 110f4ea | 2019-09-09 17:04:25 +0530 | [diff] [blame] | 122 | def check_availabilty(timeslot, settings): |
0Pranav | 75db6f7 | 2019-11-07 12:47:00 +0530 | [diff] [blame] | 123 | return frappe.db.count('Appointment', {'scheduled_time': timeslot}) < settings.number_of_agents |
Pranav Nachanekar | 110f4ea | 2019-09-09 17:04:25 +0530 | [diff] [blame] | 124 | |
pranav nachnekar | 2791054 | 2019-09-03 12:04:52 +0530 | [diff] [blame] | 125 | def _is_holiday(date, holiday_list): |
0Pranav | 75db6f7 | 2019-11-07 12:47:00 +0530 | [diff] [blame] | 126 | for holiday in holiday_list.holidays: |
| 127 | if holiday.holiday_date == date: |
| 128 | return True |
| 129 | return False |
pranav nachnekar | 2791054 | 2019-09-03 12:04:52 +0530 | [diff] [blame] | 130 | |
Pranav Nachanekar | 25148d0 | 2019-10-04 11:32:39 +0530 | [diff] [blame] | 131 | |
pranav nachnekar | 2791054 | 2019-09-03 12:04:52 +0530 | [diff] [blame] | 132 | def _get_records(start_time, end_time, settings): |
0Pranav | 75db6f7 | 2019-11-07 12:47:00 +0530 | [diff] [blame] | 133 | records = [] |
| 134 | for record in settings.availability_of_slots: |
| 135 | if record.day_of_week == WEEKDAYS[start_time.weekday()] or record.day_of_week == WEEKDAYS[end_time.weekday()]: |
| 136 | records.append(record) |
| 137 | return records |
pranav nachnekar | 2791054 | 2019-09-03 12:04:52 +0530 | [diff] [blame] | 138 | |
Pranav Nachanekar | 25148d0 | 2019-10-04 11:32:39 +0530 | [diff] [blame] | 139 | |
pranav nachnekar | 2791054 | 2019-09-03 12:04:52 +0530 | [diff] [blame] | 140 | def _deltatime_to_datetime(date, deltatime): |
0Pranav | 75db6f7 | 2019-11-07 12:47:00 +0530 | [diff] [blame] | 141 | time = (datetime.datetime.min + deltatime).time() |
| 142 | return datetime.datetime.combine(date.date(), time) |
pranav nachnekar | 2791054 | 2019-09-03 12:04:52 +0530 | [diff] [blame] | 143 | |
Pranav Nachanekar | 25148d0 | 2019-10-04 11:32:39 +0530 | [diff] [blame] | 144 | |
pranav nachnekar | 2791054 | 2019-09-03 12:04:52 +0530 | [diff] [blame] | 145 | def _datetime_to_deltatime(date_time): |
0Pranav | 75db6f7 | 2019-11-07 12:47:00 +0530 | [diff] [blame] | 146 | midnight = datetime.datetime.combine(date_time.date(), datetime.time.min) |
| 147 | return (date_time-midnight) |