147 lines
5.3 KiB
Python
Raw Normal View History

2019-09-03 12:04:52 +05:30
import frappe
import datetime
2019-09-03 14:16:47 +05:30
import json
2019-10-03 11:58:02 +05:30
import pytz
2019-09-03 12:04:52 +05:30
2019-09-17 16:58:41 +05:30
WEEKDAYS = ["Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday", "Sunday"]
2019-09-09 15:19:57 +05:30
no_cache = 1
2019-09-09 17:04:25 +05:30
2019-09-03 12:04:52 +05:30
@frappe.whitelist(allow_guest=True)
def get_appointment_settings():
settings = frappe.get_doc('Appointment Booking Settings')
return settings
2019-09-09 17:04:25 +05:30
2019-09-03 12:04:52 +05:30
@frappe.whitelist(allow_guest=True)
def get_holiday_list(holiday_list_name):
2019-09-09 17:04:25 +05:30
holiday_list = frappe.get_doc('Holiday List', holiday_list_name)
2019-09-03 12:04:52 +05:30
return holiday_list
2019-09-09 17:04:25 +05:30
2019-09-03 12:04:52 +05:30
@frappe.whitelist(allow_guest=True)
def get_timezones():
2019-09-09 17:04:25 +05:30
timezones = frappe.get_list('Timezone', fields='*')
2019-10-03 11:58:02 +05:30
return pytz.all_timezones
2019-09-03 12:04:52 +05:30
2019-09-09 17:04:25 +05:30
2019-09-03 12:04:52 +05:30
@frappe.whitelist(allow_guest=True)
2019-09-09 17:04:25 +05:30
def get_appointment_slots(date, timezone):
2019-10-03 11:58:02 +05:30
import pytz
guest_timezone = pytz.timezone(timezone)
2019-09-03 12:04:52 +05:30
format_string = '%Y-%m-%d %H:%M:%S'
2019-09-09 17:04:25 +05:30
query_start_time = datetime.datetime.strptime(
date + ' 00:00:00', format_string)
query_end_time = datetime.datetime.strptime(
date + ' 23:59:59', format_string)
2019-10-03 11:58:02 +05:30
local_timezone = frappe.utils.get_time_zone()
local_timezone = pytz.timezone(local_timezone)
query_start_time = guest_timezone.localize(query_start_time)
query_end_time = guest_timezone.localize(query_end_time)
query_start_time = query_start_time.astimezone(local_timezone)
query_end_time = query_end_time.astimezone(local_timezone)
now = datetime.datetime.now()
2019-10-03 11:58:02 +05:30
# now = local_timezone.localize(now)
2019-09-03 12:04:52 +05:30
# Database queries
settings = frappe.get_doc('Appointment Booking Settings')
holiday_list = frappe.get_doc('Holiday List', settings.holiday_list)
2019-09-09 17:04:25 +05:30
timeslots = get_available_slots_between(
query_start_time, query_end_time, settings)
2019-09-03 12:04:52 +05:30
# Filter timeslots based on date
converted_timeslots = []
for timeslot in timeslots:
2019-10-03 11:58:02 +05:30
timeslot = local_timezone.localize(timeslot)
print(timeslot)
timeslot = timeslot.astimezone(guest_timezone)
timeslot = timeslot.replace(tzinfo=None)
2019-09-03 12:04:52 +05:30
# Check if holiday
2019-09-09 17:04:25 +05:30
if _is_holiday(timeslot.date(), holiday_list):
converted_timeslots.append(
2019-10-03 11:58:02 +05:30
dict(time=timeslot, availability=False))
2019-09-03 12:04:52 +05:30
continue
# Check availability
if check_availabilty(timeslot, settings) and timeslot >= now:
2019-09-09 17:04:25 +05:30
converted_timeslots.append(
2019-10-03 11:58:02 +05:30
dict(time=timeslot, availability=True))
2019-09-03 12:04:52 +05:30
else:
2019-09-09 17:04:25 +05:30
converted_timeslots.append(
2019-10-03 11:58:02 +05:30
dict(time=timeslot, availability=False))
2019-09-09 17:04:25 +05:30
date_required = datetime.datetime.strptime(
date + ' 00:00:00', format_string).date()
converted_timeslots = filter_timeslots(date_required, converted_timeslots)
2019-09-03 12:04:52 +05:30
return converted_timeslots
2019-09-09 17:04:25 +05:30
2019-09-03 12:04:52 +05:30
def get_available_slots_between(query_start_time, query_end_time, settings):
records = _get_records(query_start_time, query_end_time, settings)
timeslots = []
appointment_duration = datetime.timedelta(
minutes=settings.appointment_duration)
for record in records:
if record.day_of_week == WEEKDAYS[query_start_time.weekday()]:
current_time = _deltatime_to_datetime(
query_start_time, record.from_time)
end_time = _deltatime_to_datetime(
query_start_time, record.to_time)
2019-09-09 17:04:25 +05:30
else:
2019-09-03 12:04:52 +05:30
current_time = _deltatime_to_datetime(
query_end_time, record.from_time)
end_time = _deltatime_to_datetime(
query_end_time, record.to_time)
while current_time + appointment_duration <= end_time:
timeslots.append(current_time)
current_time += appointment_duration
return timeslots
2019-09-09 17:04:25 +05:30
@frappe.whitelist(allow_guest=True)
def create_appointment(date, time, contact):
2019-09-03 14:16:47 +05:30
appointment = frappe.new_doc('Appointment')
format_string = '%Y-%m-%d %H:%M:%S'
2019-09-09 17:04:25 +05:30
appointment.scheduled_time = datetime.datetime.strptime(
date+" "+time, format_string)
2019-09-03 14:16:47 +05:30
contact = json.loads(contact)
appointment.customer_name = contact['name']
2019-09-09 17:01:40 +05:30
appointment.customer_phone_number = contact['number']
2019-09-03 14:16:47 +05:30
appointment.customer_skype = contact['skype']
appointment.customer_details = contact['notes']
2019-09-23 14:16:13 +05:30
appointment.customer_email = contact['email']
2019-09-10 13:12:28 +05:30
appointment.status = 'Open'
2019-09-03 14:16:47 +05:30
appointment.insert()
2019-09-03 12:04:52 +05:30
2019-09-03 14:16:47 +05:30
# Helper Functions
2019-09-09 17:04:25 +05:30
def filter_timeslots(date, timeslots):
2019-09-03 12:04:52 +05:30
filtered_timeslots = []
for timeslot in timeslots:
if(timeslot['time'].date() == date):
filtered_timeslots.append(timeslot)
return filtered_timeslots
2019-09-09 17:04:25 +05:30
def check_availabilty(timeslot, settings):
return frappe.db.count('Appointment', {'scheduled_time': timeslot}) < settings.number_of_agents
2019-09-03 12:04:52 +05:30
def _is_holiday(date, holiday_list):
for holiday in holiday_list.holidays:
if holiday.holiday_date == date:
return True
return False
def _get_records(start_time, end_time, settings):
records = []
for record in settings.availability_of_slots:
if record.day_of_week == WEEKDAYS[start_time.weekday()] or record.day_of_week == WEEKDAYS[end_time.weekday()]:
records.append(record)
return records
def _deltatime_to_datetime(date, deltatime):
time = (datetime.datetime.min + deltatime).time()
return datetime.datetime.combine(date.date(), time)
def _datetime_to_deltatime(date_time):
2019-09-09 17:04:25 +05:30
midnight = datetime.datetime.combine(date_time.date(), datetime.time.min)
2019-10-03 11:58:02 +05:30
return (date_time-midnight)