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