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