merge settings fetch, add helpers
This commit is contained in:
parent
60093d98b0
commit
e494144c96
@ -18,47 +18,31 @@ def get_context(context):
|
|||||||
else:
|
else:
|
||||||
raise frappe.DoesNotExistError
|
raise frappe.DoesNotExistError
|
||||||
|
|
||||||
|
|
||||||
@frappe.whitelist(allow_guest=True)
|
@frappe.whitelist(allow_guest=True)
|
||||||
def get_appointment_settings():
|
def get_appointment_settings():
|
||||||
settings = frappe.get_doc('Appointment Booking Settings')
|
settings = frappe.get_doc('Appointment Booking Settings')
|
||||||
|
settings.holiday_list = frappe.get_doc('Holiday List', settings.holiday_list)
|
||||||
return settings
|
return settings
|
||||||
|
|
||||||
|
|
||||||
@frappe.whitelist(allow_guest=True)
|
|
||||||
def is_enabled():
|
|
||||||
enable_scheduling = frappe.db.get_single_value(
|
|
||||||
'Appointment Booking Settings', 'enable_scheduling')
|
|
||||||
return enable_scheduling
|
|
||||||
|
|
||||||
|
|
||||||
@frappe.whitelist(allow_guest=True)
|
|
||||||
def get_holiday_list(holiday_list_name):
|
|
||||||
holiday_list = frappe.get_doc('Holiday List', holiday_list_name)
|
|
||||||
return holiday_list
|
|
||||||
|
|
||||||
|
|
||||||
@frappe.whitelist(allow_guest=True)
|
@frappe.whitelist(allow_guest=True)
|
||||||
def get_timezones():
|
def get_timezones():
|
||||||
return pytz.all_timezones
|
return pytz.all_timezones
|
||||||
|
|
||||||
|
|
||||||
@frappe.whitelist(allow_guest=True)
|
@frappe.whitelist(allow_guest=True)
|
||||||
def get_appointment_slots(date, timezone):
|
def get_appointment_slots(date, timezone):
|
||||||
import pytz
|
import pytz
|
||||||
guest_timezone = pytz.timezone(timezone)
|
guest_timezone = pytz.timezone(timezone)
|
||||||
|
local_timezone = pytz.timezone(frappe.utils.get_time_zone())
|
||||||
format_string = '%Y-%m-%d %H:%M:%S'
|
format_string = '%Y-%m-%d %H:%M:%S'
|
||||||
query_start_time = datetime.datetime.strptime(
|
query_start_time = datetime.datetime.strptime(
|
||||||
date + ' 00:00:00', format_string)
|
date + ' 00:00:00', format_string)
|
||||||
query_end_time = datetime.datetime.strptime(
|
query_end_time = datetime.datetime.strptime(
|
||||||
date + ' 23:59:59', format_string)
|
date + ' 23:59:59', format_string)
|
||||||
local_timezone = frappe.utils.get_time_zone()
|
|
||||||
local_timezone = pytz.timezone(local_timezone)
|
query_start_time = convert_to_system_timzone(timezone,query_start_time)
|
||||||
query_start_time = guest_timezone.localize(query_start_time)
|
query_end_time = convert_to_system_timzone(timezone,query_end_time)
|
||||||
query_end_time = guest_timezone.localize(query_end_time)
|
now = convert_to_guest_timezone(timezone,datetime.datetime.now())
|
||||||
query_start_time = query_start_time.astimezone(local_timezone)
|
|
||||||
query_end_time = query_end_time.astimezone(local_timezone)
|
|
||||||
now = datetime.datetime.now()
|
|
||||||
# Database queries
|
# Database queries
|
||||||
settings = frappe.get_doc('Appointment Booking Settings')
|
settings = frappe.get_doc('Appointment Booking Settings')
|
||||||
holiday_list = frappe.get_doc('Holiday List', settings.holiday_list)
|
holiday_list = frappe.get_doc('Holiday List', settings.holiday_list)
|
||||||
@ -68,9 +52,9 @@ def get_appointment_slots(date, timezone):
|
|||||||
# Filter timeslots based on date
|
# Filter timeslots based on date
|
||||||
converted_timeslots = []
|
converted_timeslots = []
|
||||||
for timeslot in timeslots:
|
for timeslot in timeslots:
|
||||||
timeslot = local_timezone.localize(timeslot)
|
print("Unconverted Timeslot:{0}".format(timeslot))
|
||||||
timeslot = timeslot.astimezone(guest_timezone)
|
timeslot = convert_to_guest_timezone(timezone,timeslot)
|
||||||
timeslot = timeslot.replace(tzinfo=None)
|
print("Converted Timeslot:{0}".format(timeslot))
|
||||||
# Check if holiday
|
# Check if holiday
|
||||||
if _is_holiday(timeslot.date(), holiday_list):
|
if _is_holiday(timeslot.date(), holiday_list):
|
||||||
converted_timeslots.append(
|
converted_timeslots.append(
|
||||||
@ -112,11 +96,16 @@ def get_available_slots_between(query_start_time, query_end_time, settings):
|
|||||||
|
|
||||||
|
|
||||||
@frappe.whitelist(allow_guest=True)
|
@frappe.whitelist(allow_guest=True)
|
||||||
def create_appointment(date, time, contact):
|
def create_appointment(date, time, tz, contact):
|
||||||
|
import pytz
|
||||||
appointment = frappe.new_doc('Appointment')
|
appointment = frappe.new_doc('Appointment')
|
||||||
format_string = '%Y-%m-%d %H:%M:%S'
|
format_string = '%Y-%m-%d %H:%M:%S%z'
|
||||||
appointment.scheduled_time = datetime.datetime.strptime(
|
scheduled_time = datetime.datetime.strptime(
|
||||||
date+" "+time, format_string)
|
date+" "+time, format_string)
|
||||||
|
scheduled_time = scheduled_time.replace(tzinfo=None)
|
||||||
|
scheduled_time = convert_to_system_timzone(tz,scheduled_time)
|
||||||
|
scheduled_time= scheduled_time.replace(tzinfo=None)
|
||||||
|
appointment.scheduled_time = scheduled_time
|
||||||
contact = json.loads(contact)
|
contact = json.loads(contact)
|
||||||
appointment.customer_name = contact['name']
|
appointment.customer_name = contact['name']
|
||||||
appointment.customer_phone_number = contact['number']
|
appointment.customer_phone_number = contact['number']
|
||||||
@ -126,7 +115,6 @@ def create_appointment(date, time, contact):
|
|||||||
appointment.status = 'Open'
|
appointment.status = 'Open'
|
||||||
appointment.insert()
|
appointment.insert()
|
||||||
|
|
||||||
|
|
||||||
# Helper Functions
|
# Helper Functions
|
||||||
def filter_timeslots(date, timeslots):
|
def filter_timeslots(date, timeslots):
|
||||||
filtered_timeslots = []
|
filtered_timeslots = []
|
||||||
@ -135,11 +123,25 @@ def filter_timeslots(date, timeslots):
|
|||||||
filtered_timeslots.append(timeslot)
|
filtered_timeslots.append(timeslot)
|
||||||
return filtered_timeslots
|
return filtered_timeslots
|
||||||
|
|
||||||
|
def convert_to_guest_timezone(guest_tz,datetimeobject):
|
||||||
|
import pytz
|
||||||
|
guest_tz = pytz.timezone(guest_tz)
|
||||||
|
local_timezone = pytz.timezone(frappe.utils.get_time_zone())
|
||||||
|
datetimeobject = local_timezone.localize(datetimeobject)
|
||||||
|
datetimeobject = datetimeobject.astimezone(guest_tz)
|
||||||
|
return datetimeobject
|
||||||
|
|
||||||
|
def convert_to_system_timzone(guest_tz,datetimeobject):
|
||||||
|
import pytz
|
||||||
|
guest_tz = pytz.timezone(guest_tz)
|
||||||
|
datetimeobject = guest_tz.localize(datetimeobject)
|
||||||
|
system_tz = pytz.timezone(frappe.utils.get_time_zone())
|
||||||
|
datetimeobject = datetimeobject.astimezone(system_tz)
|
||||||
|
return datetimeobject
|
||||||
|
|
||||||
def check_availabilty(timeslot, settings):
|
def check_availabilty(timeslot, settings):
|
||||||
return frappe.db.count('Appointment', {'scheduled_time': timeslot}) < settings.number_of_agents
|
return frappe.db.count('Appointment', {'scheduled_time': timeslot}) < settings.number_of_agents
|
||||||
|
|
||||||
|
|
||||||
def _is_holiday(date, holiday_list):
|
def _is_holiday(date, holiday_list):
|
||||||
for holiday in holiday_list.holidays:
|
for holiday in holiday_list.holidays:
|
||||||
if holiday.holiday_date == date:
|
if holiday.holiday_date == date:
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user