mirror of
https://github.com/frappe/erpnext.git
synced 2026-06-07 07:02:54 +00:00
convert indentation to tabs
This commit is contained in:
@@ -17,203 +17,206 @@ from frappe.utils.verified_command import verify_request, get_signed_params
|
|||||||
|
|
||||||
class Appointment(Document):
|
class Appointment(Document):
|
||||||
|
|
||||||
def find_lead_by_email(self):
|
def find_lead_by_email(self):
|
||||||
lead_list = frappe.get_list(
|
lead_list = frappe.get_list(
|
||||||
'Lead', filters={'email_id': self.customer_email}, ignore_permissions=True)
|
'Lead', filters={'email_id': self.customer_email}, ignore_permissions=True)
|
||||||
if lead_list:
|
if lead_list:
|
||||||
return lead_list[0].name
|
return lead_list[0].name
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def before_insert(self):
|
def before_insert(self):
|
||||||
number_of_appointments_in_same_slot = frappe.db.count(
|
number_of_appointments_in_same_slot = frappe.db.count(
|
||||||
'Appointment', filters={'scheduled_time': self.scheduled_time})
|
'Appointment', filters={'scheduled_time': self.scheduled_time})
|
||||||
number_of_agents = frappe.db.get_single_value('Appointment Booking Settings', 'number_of_agents')
|
number_of_agents = frappe.db.get_single_value('Appointment Booking Settings', 'number_of_agents')
|
||||||
if(number_of_appointments_in_same_slot >= number_of_agents):
|
if(number_of_appointments_in_same_slot >= number_of_agents):
|
||||||
frappe.throw('Time slot is not available')
|
frappe.throw('Time slot is not available')
|
||||||
# Link lead
|
# Link lead
|
||||||
if not self.lead:
|
if not self.lead:
|
||||||
self.lead = self.find_lead_by_email()
|
self.lead = self.find_lead_by_email()
|
||||||
|
|
||||||
def after_insert(self):
|
def after_insert(self):
|
||||||
if self.lead:
|
if self.lead:
|
||||||
# Create Calendar event
|
# Create Calendar event
|
||||||
self.auto_assign()
|
self.auto_assign()
|
||||||
self.create_calendar_event()
|
self.create_calendar_event()
|
||||||
else:
|
else:
|
||||||
# Set status to unverified
|
# Set status to unverified
|
||||||
self.status = 'Unverified'
|
self.status = 'Unverified'
|
||||||
# Send email to confirm
|
# Send email to confirm
|
||||||
self.send_confirmation_email()
|
self.send_confirmation_email()
|
||||||
|
|
||||||
def send_confirmation_email(self):
|
def send_confirmation_email(self):
|
||||||
verify_url = self._get_verify_url()
|
verify_url = self._get_verify_url()
|
||||||
template = 'confirm_appointment'
|
template = 'confirm_appointment'
|
||||||
args = {
|
args = {
|
||||||
"link":verify_url,
|
"link":verify_url,
|
||||||
"site_url":frappe.utils.get_url(),
|
"site_url":frappe.utils.get_url(),
|
||||||
"full_name":self.customer_name,
|
"full_name":self.customer_name,
|
||||||
}
|
}
|
||||||
frappe.sendmail(recipients=[self.customer_email],
|
frappe.sendmail(recipients=[self.customer_email],
|
||||||
template=template,
|
template=template,
|
||||||
args=args,
|
args=args,
|
||||||
subject=_('Appointment Confirmation'))
|
subject=_('Appointment Confirmation'))
|
||||||
if frappe.session.user == "Guest":
|
if frappe.session.user == "Guest":
|
||||||
frappe.msgprint(
|
frappe.msgprint(
|
||||||
'Please check your email to confirm the appointment')
|
'Please check your email to confirm the appointment')
|
||||||
|
else :
|
||||||
|
frappe.msgprint(
|
||||||
|
'Appointment was created. But no lead was found. Please check the email to confirm')
|
||||||
|
|
||||||
def on_change(self):
|
def on_change(self):
|
||||||
# Sync Calendar
|
# Sync Calendar
|
||||||
if not self.calendar_event:
|
if not self.calendar_event:
|
||||||
return
|
return
|
||||||
cal_event = frappe.get_doc('Event', self.calendar_event)
|
cal_event = frappe.get_doc('Event', self.calendar_event)
|
||||||
cal_event.starts_on = self.scheduled_time
|
cal_event.starts_on = self.scheduled_time
|
||||||
cal_event.save(ignore_permissions=True)
|
cal_event.save(ignore_permissions=True)
|
||||||
|
|
||||||
|
|
||||||
def set_verified(self, email):
|
def set_verified(self, email):
|
||||||
if not email == self.customer_email:
|
if not email == self.customer_email:
|
||||||
frappe.throw('Email verification failed.')
|
frappe.throw('Email verification failed.')
|
||||||
# Create new lead
|
# Create new lead
|
||||||
self.create_lead_and_link()
|
self.create_lead_and_link()
|
||||||
# Remove unverified status
|
# Remove unverified status
|
||||||
self.status = 'Open'
|
self.status = 'Open'
|
||||||
# Create calender event
|
# Create calender event
|
||||||
self.auto_assign()
|
self.auto_assign()
|
||||||
self.create_calendar_event()
|
self.create_calendar_event()
|
||||||
self.save(ignore_permissions=True)
|
self.save(ignore_permissions=True)
|
||||||
frappe.db.commit()
|
frappe.db.commit()
|
||||||
|
|
||||||
def create_lead_and_link(self):
|
def create_lead_and_link(self):
|
||||||
# Return if already linked
|
# Return if already linked
|
||||||
if self.lead:
|
if self.lead:
|
||||||
return
|
return
|
||||||
lead = frappe.get_doc({
|
lead = frappe.get_doc({
|
||||||
'doctype': 'Lead',
|
'doctype': 'Lead',
|
||||||
'lead_name': self.customer_name,
|
'lead_name': self.customer_name,
|
||||||
'email_id': self.customer_email,
|
'email_id': self.customer_email,
|
||||||
'notes': self.customer_details,
|
'notes': self.customer_details,
|
||||||
'phone': self.customer_phone_number,
|
'phone': self.customer_phone_number,
|
||||||
})
|
})
|
||||||
lead.insert(ignore_permissions=True)
|
lead.insert(ignore_permissions=True)
|
||||||
# Link lead
|
# Link lead
|
||||||
self.lead = lead.name
|
self.lead = lead.name
|
||||||
|
|
||||||
def auto_assign(self):
|
def auto_assign(self):
|
||||||
from frappe.desk.form.assign_to import add as add_assignemnt
|
from frappe.desk.form.assign_to import add as add_assignemnt
|
||||||
existing_assignee = self.get_assignee_from_latest_opportunity()
|
existing_assignee = self.get_assignee_from_latest_opportunity()
|
||||||
if existing_assignee:
|
if existing_assignee:
|
||||||
# If the latest opportunity is assigned to someone
|
# If the latest opportunity is assigned to someone
|
||||||
# Assign the appointment to the same
|
# Assign the appointment to the same
|
||||||
add_assignemnt({
|
add_assignemnt({
|
||||||
'doctype': self.doctype,
|
'doctype': self.doctype,
|
||||||
'name': self.name,
|
'name': self.name,
|
||||||
'assign_to': existing_assignee
|
'assign_to': existing_assignee
|
||||||
})
|
})
|
||||||
return
|
return
|
||||||
if self._assign:
|
if self._assign:
|
||||||
return
|
return
|
||||||
available_agents = _get_agents_sorted_by_asc_workload(
|
available_agents = _get_agents_sorted_by_asc_workload(
|
||||||
self.scheduled_time.date())
|
self.scheduled_time.date())
|
||||||
for agent in available_agents:
|
for agent in available_agents:
|
||||||
if(_check_agent_availability(agent, self.scheduled_time)):
|
if(_check_agent_availability(agent, self.scheduled_time)):
|
||||||
agent = agent[0]
|
agent = agent[0]
|
||||||
add_assignemnt({
|
add_assignemnt({
|
||||||
'doctype': self.doctype,
|
'doctype': self.doctype,
|
||||||
'name': self.name,
|
'name': self.name,
|
||||||
'assign_to': agent
|
'assign_to': agent
|
||||||
})
|
})
|
||||||
break
|
break
|
||||||
|
|
||||||
def get_assignee_from_latest_opportunity(self):
|
def get_assignee_from_latest_opportunity(self):
|
||||||
if not self.lead:
|
if not self.lead:
|
||||||
return None
|
return None
|
||||||
if not frappe.db.exists('Lead', self.lead):
|
if not frappe.db.exists('Lead', self.lead):
|
||||||
return None
|
return None
|
||||||
opporutnities = frappe.get_list(
|
opporutnities = frappe.get_list(
|
||||||
'Opportunity',
|
'Opportunity',
|
||||||
filters={
|
filters={
|
||||||
'party_name': self.lead,
|
'party_name': self.lead,
|
||||||
},
|
},
|
||||||
ignore_permissions=True,
|
ignore_permissions=True,
|
||||||
order_by='creation desc')
|
order_by='creation desc')
|
||||||
if not opporutnities:
|
if not opporutnities:
|
||||||
return None
|
return None
|
||||||
latest_opportunity = frappe.get_doc('Opportunity', opporutnities[0].name )
|
latest_opportunity = frappe.get_doc('Opportunity', opporutnities[0].name )
|
||||||
assignee = latest_opportunity._assign
|
assignee = latest_opportunity._assign
|
||||||
if not assignee:
|
if not assignee:
|
||||||
return None
|
return None
|
||||||
assignee = frappe.parse_json(assignee)[0]
|
assignee = frappe.parse_json(assignee)[0]
|
||||||
return assignee
|
return assignee
|
||||||
|
|
||||||
def create_calendar_event(self):
|
def create_calendar_event(self):
|
||||||
if self.calendar_event:
|
if self.calendar_event:
|
||||||
return
|
return
|
||||||
appointment_event = frappe.get_doc({
|
appointment_event = frappe.get_doc({
|
||||||
'doctype': 'Event',
|
'doctype': 'Event',
|
||||||
'subject': ' '.join(['Appointment with', self.customer_name]),
|
'subject': ' '.join(['Appointment with', self.customer_name]),
|
||||||
'starts_on': self.scheduled_time,
|
'starts_on': self.scheduled_time,
|
||||||
'status': 'Open',
|
'status': 'Open',
|
||||||
'type': 'Public',
|
'type': 'Public',
|
||||||
'send_reminder': frappe.db.get_single_value('Appointment Booking Settings', 'email_reminders'),
|
'send_reminder': frappe.db.get_single_value('Appointment Booking Settings', 'email_reminders'),
|
||||||
'event_participants': [dict(reference_doctype='Lead', reference_docname=self.lead)]
|
'event_participants': [dict(reference_doctype='Lead', reference_docname=self.lead)]
|
||||||
})
|
})
|
||||||
employee = _get_employee_from_user(self._assign)
|
employee = _get_employee_from_user(self._assign)
|
||||||
if employee:
|
if employee:
|
||||||
appointment_event.append('event_participants', dict(
|
appointment_event.append('event_participants', dict(
|
||||||
reference_doctype='Employee',
|
reference_doctype='Employee',
|
||||||
reference_docname=employee.name))
|
reference_docname=employee.name))
|
||||||
appointment_event.insert(ignore_permissions=True)
|
appointment_event.insert(ignore_permissions=True)
|
||||||
self.calendar_event = appointment_event.name
|
self.calendar_event = appointment_event.name
|
||||||
self.save(ignore_permissions=True)
|
self.save(ignore_permissions=True)
|
||||||
|
|
||||||
def _get_verify_url(self):
|
def _get_verify_url(self):
|
||||||
verify_route = '/book-appointment/verify'
|
verify_route = '/book-appointment/verify'
|
||||||
params = {
|
params = {
|
||||||
'email': self.customer_email,
|
'email': self.customer_email,
|
||||||
'appointment': self.name
|
'appointment': self.name
|
||||||
}
|
}
|
||||||
return get_url(verify_route + '?' + get_signed_params(params))
|
return get_url(verify_route + '?' + get_signed_params(params))
|
||||||
|
|
||||||
|
|
||||||
def _get_agents_sorted_by_asc_workload(date):
|
def _get_agents_sorted_by_asc_workload(date):
|
||||||
appointments = frappe.db.get_list('Appointment', fields='*')
|
appointments = frappe.db.get_list('Appointment', fields='*')
|
||||||
agent_list = _get_agent_list_as_strings()
|
agent_list = _get_agent_list_as_strings()
|
||||||
if not appointments:
|
if not appointments:
|
||||||
return agent_list
|
return agent_list
|
||||||
appointment_counter = Counter(agent_list)
|
appointment_counter = Counter(agent_list)
|
||||||
for appointment in appointments:
|
for appointment in appointments:
|
||||||
assigned_to = frappe.parse_json(appointment._assign)
|
assigned_to = frappe.parse_json(appointment._assign)
|
||||||
if not assigned_to:
|
if not assigned_to:
|
||||||
continue
|
continue
|
||||||
if (assigned_to[0] in agent_list) and appointment.scheduled_time.date() == date:
|
if (assigned_to[0] in agent_list) and appointment.scheduled_time.date() == date:
|
||||||
appointment_counter[assigned_to[0]] += 1
|
appointment_counter[assigned_to[0]] += 1
|
||||||
sorted_agent_list = appointment_counter.most_common()
|
sorted_agent_list = appointment_counter.most_common()
|
||||||
sorted_agent_list.reverse()
|
sorted_agent_list.reverse()
|
||||||
return sorted_agent_list
|
return sorted_agent_list
|
||||||
|
|
||||||
|
|
||||||
def _get_agent_list_as_strings():
|
def _get_agent_list_as_strings():
|
||||||
agent_list_as_strings = []
|
agent_list_as_strings = []
|
||||||
agent_list = frappe.get_doc('Appointment Booking Settings').agent_list
|
agent_list = frappe.get_doc('Appointment Booking Settings').agent_list
|
||||||
for agent in agent_list:
|
for agent in agent_list:
|
||||||
agent_list_as_strings.append(agent.user)
|
agent_list_as_strings.append(agent.user)
|
||||||
return agent_list_as_strings
|
return agent_list_as_strings
|
||||||
|
|
||||||
|
|
||||||
def _check_agent_availability(agent_email, scheduled_time):
|
def _check_agent_availability(agent_email, scheduled_time):
|
||||||
appointemnts_at_scheduled_time = frappe.get_list(
|
appointemnts_at_scheduled_time = frappe.get_list(
|
||||||
'Appointment', filters={'scheduled_time': scheduled_time})
|
'Appointment', filters={'scheduled_time': scheduled_time})
|
||||||
for appointment in appointemnts_at_scheduled_time:
|
for appointment in appointemnts_at_scheduled_time:
|
||||||
if appointment._assign == agent_email:
|
if appointment._assign == agent_email:
|
||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
def _get_employee_from_user(user):
|
def _get_employee_from_user(user):
|
||||||
employee_docname = frappe.db.exists(
|
employee_docname = frappe.db.exists(
|
||||||
{'doctype': 'Employee', 'user_id': user})
|
{'doctype': 'Employee', 'user_id': user})
|
||||||
if employee_docname:
|
if employee_docname:
|
||||||
# frappe.db.exists returns a tuple of a tuple
|
# frappe.db.exists returns a tuple of a tuple
|
||||||
return frappe.get_doc('Employee', employee_docname[0][0])
|
return frappe.get_doc('Employee', employee_docname[0][0])
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|||||||
@@ -10,29 +10,29 @@ from frappe.model.document import Document
|
|||||||
|
|
||||||
|
|
||||||
class AppointmentBookingSettings(Document):
|
class AppointmentBookingSettings(Document):
|
||||||
min_date = '01/01/1970 '
|
min_date = '01/01/1970 '
|
||||||
format_string = "%d/%m/%Y %H:%M:%S"
|
format_string = "%d/%m/%Y %H:%M:%S"
|
||||||
|
|
||||||
def validate(self):
|
def validate(self):
|
||||||
self.validate_availability_of_slots()
|
self.validate_availability_of_slots()
|
||||||
|
|
||||||
def validate_availability_of_slots(self):
|
def validate_availability_of_slots(self):
|
||||||
for record in self.availability_of_slots:
|
for record in self.availability_of_slots:
|
||||||
from_time = datetime.datetime.strptime(
|
from_time = datetime.datetime.strptime(
|
||||||
self.min_date+record.from_time, self.format_string)
|
self.min_date+record.from_time, self.format_string)
|
||||||
to_time = datetime.datetime.strptime(
|
to_time = datetime.datetime.strptime(
|
||||||
self.min_date+record.to_time, self.format_string)
|
self.min_date+record.to_time, self.format_string)
|
||||||
timedelta = to_time-from_time
|
timedelta = to_time-from_time
|
||||||
self.validate_from_and_to_time(from_time, to_time)
|
self.validate_from_and_to_time(from_time, to_time)
|
||||||
self.duration_is_divisible(from_time, to_time)
|
self.duration_is_divisible(from_time, to_time)
|
||||||
|
|
||||||
def validate_from_and_to_time(self, from_time, to_time):
|
def validate_from_and_to_time(self, from_time, to_time):
|
||||||
if from_time > to_time:
|
if from_time > to_time:
|
||||||
err_msg = _('<b>From Time</b> cannot be later than <b>To Time</b> for {0}').format(record.day_of_week)
|
err_msg = _('<b>From Time</b> cannot be later than <b>To Time</b> for {0}').format(record.day_of_week)
|
||||||
frappe.throw(_(err_msg))
|
frappe.throw(_(err_msg))
|
||||||
|
|
||||||
def duration_is_divisible(self, from_time, to_time):
|
def duration_is_divisible(self, from_time, to_time):
|
||||||
timedelta = to_time - from_time
|
timedelta = to_time - from_time
|
||||||
if timedelta.total_seconds() % (self.appointment_duration * 60):
|
if timedelta.total_seconds() % (self.appointment_duration * 60):
|
||||||
frappe.throw(
|
frappe.throw(
|
||||||
_('The difference between from time and To Time must be a multiple of Appointment'))
|
_('The difference between from time and To Time must be a multiple of Appointment'))
|
||||||
|
|||||||
@@ -5,158 +5,158 @@ import pytz
|
|||||||
|
|
||||||
|
|
||||||
WEEKDAYS = ["Monday", "Tuesday", "Wednesday",
|
WEEKDAYS = ["Monday", "Tuesday", "Wednesday",
|
||||||
"Thursday", "Friday", "Saturday", "Sunday"]
|
"Thursday", "Friday", "Saturday", "Sunday"]
|
||||||
|
|
||||||
no_cache = 1
|
no_cache = 1
|
||||||
|
|
||||||
|
|
||||||
def get_context(context):
|
def get_context(context):
|
||||||
is_enabled = frappe.db.get_single_value(
|
is_enabled = frappe.db.get_single_value(
|
||||||
'Appointment Booking Settings', 'enable_scheduling')
|
'Appointment Booking Settings', 'enable_scheduling')
|
||||||
if is_enabled:
|
if is_enabled:
|
||||||
return context
|
return 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)
|
settings.holiday_list = frappe.get_doc('Holiday List', settings.holiday_list)
|
||||||
return settings
|
return settings
|
||||||
|
|
||||||
@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
|
||||||
# Convert query to local timezones
|
# Convert query to local timezones
|
||||||
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)
|
||||||
query_start_time = convert_to_system_timezone(timezone,query_start_time)
|
query_start_time = convert_to_system_timezone(timezone,query_start_time)
|
||||||
query_end_time = convert_to_system_timezone(timezone,query_end_time)
|
query_end_time = convert_to_system_timezone(timezone,query_end_time)
|
||||||
now = convert_to_guest_timezone(timezone,datetime.datetime.now())
|
now = convert_to_guest_timezone(timezone,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)
|
||||||
timeslots = get_available_slots_between(
|
timeslots = get_available_slots_between(
|
||||||
query_start_time, query_end_time, settings)
|
query_start_time, query_end_time, settings)
|
||||||
|
|
||||||
# Filter and convert timeslots
|
# Filter and convert timeslots
|
||||||
converted_timeslots = []
|
converted_timeslots = []
|
||||||
for timeslot in timeslots:
|
for timeslot in timeslots:
|
||||||
converted_timeslot = convert_to_guest_timezone(timezone,timeslot)
|
converted_timeslot = convert_to_guest_timezone(timezone,timeslot)
|
||||||
# Check if holiday
|
# Check if holiday
|
||||||
if _is_holiday(converted_timeslot.date(), holiday_list):
|
if _is_holiday(converted_timeslot.date(), holiday_list):
|
||||||
converted_timeslots.append(
|
converted_timeslots.append(
|
||||||
dict(time=converted_timeslot, availability=False))
|
dict(time=converted_timeslot, availability=False))
|
||||||
continue
|
continue
|
||||||
# Check availability
|
# Check availability
|
||||||
if check_availabilty(timeslot, settings) and converted_timeslot >= now:
|
if check_availabilty(timeslot, settings) and converted_timeslot >= now:
|
||||||
converted_timeslots.append(
|
converted_timeslots.append(
|
||||||
dict(time=converted_timeslot, availability=True))
|
dict(time=converted_timeslot, availability=True))
|
||||||
else:
|
else:
|
||||||
converted_timeslots.append(
|
converted_timeslots.append(
|
||||||
dict(time=converted_timeslot, availability=False))
|
dict(time=converted_timeslot, availability=False))
|
||||||
date_required = datetime.datetime.strptime(
|
date_required = datetime.datetime.strptime(
|
||||||
date + ' 00:00:00', format_string).date()
|
date + ' 00:00:00', format_string).date()
|
||||||
converted_timeslots = filter_timeslots(date_required, converted_timeslots)
|
converted_timeslots = filter_timeslots(date_required, converted_timeslots)
|
||||||
return converted_timeslots
|
return converted_timeslots
|
||||||
|
|
||||||
def get_available_slots_between(query_start_time, query_end_time, settings):
|
def get_available_slots_between(query_start_time, query_end_time, settings):
|
||||||
records = _get_records(query_start_time, query_end_time, settings)
|
records = _get_records(query_start_time, query_end_time, settings)
|
||||||
timeslots = []
|
timeslots = []
|
||||||
appointment_duration = datetime.timedelta(
|
appointment_duration = datetime.timedelta(
|
||||||
minutes=settings.appointment_duration)
|
minutes=settings.appointment_duration)
|
||||||
for record in records:
|
for record in records:
|
||||||
if record.day_of_week == WEEKDAYS[query_start_time.weekday()]:
|
if record.day_of_week == WEEKDAYS[query_start_time.weekday()]:
|
||||||
current_time = _deltatime_to_datetime(
|
current_time = _deltatime_to_datetime(
|
||||||
query_start_time, record.from_time)
|
query_start_time, record.from_time)
|
||||||
end_time = _deltatime_to_datetime(
|
end_time = _deltatime_to_datetime(
|
||||||
query_start_time, record.to_time)
|
query_start_time, record.to_time)
|
||||||
else:
|
else:
|
||||||
current_time = _deltatime_to_datetime(
|
current_time = _deltatime_to_datetime(
|
||||||
query_end_time, record.from_time)
|
query_end_time, record.from_time)
|
||||||
end_time = _deltatime_to_datetime(
|
end_time = _deltatime_to_datetime(
|
||||||
query_end_time, record.to_time)
|
query_end_time, record.to_time)
|
||||||
while current_time + appointment_duration <= end_time:
|
while current_time + appointment_duration <= end_time:
|
||||||
timeslots.append(current_time)
|
timeslots.append(current_time)
|
||||||
current_time += appointment_duration
|
current_time += appointment_duration
|
||||||
return timeslots
|
return timeslots
|
||||||
|
|
||||||
|
|
||||||
@frappe.whitelist(allow_guest=True)
|
@frappe.whitelist(allow_guest=True)
|
||||||
def create_appointment(date, time, tz, contact):
|
def create_appointment(date, time, tz, contact):
|
||||||
import pytz
|
import pytz
|
||||||
appointment = frappe.new_doc('Appointment')
|
appointment = frappe.new_doc('Appointment')
|
||||||
format_string = '%Y-%m-%d %H:%M:%S%z'
|
format_string = '%Y-%m-%d %H:%M:%S%z'
|
||||||
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 = scheduled_time.replace(tzinfo=None)
|
||||||
scheduled_time = convert_to_system_timezone(tz,scheduled_time)
|
scheduled_time = convert_to_system_timezone(tz,scheduled_time)
|
||||||
scheduled_time= scheduled_time.replace(tzinfo=None)
|
scheduled_time= scheduled_time.replace(tzinfo=None)
|
||||||
appointment.scheduled_time = scheduled_time
|
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']
|
||||||
appointment.customer_skype = contact['skype']
|
appointment.customer_skype = contact['skype']
|
||||||
appointment.customer_details = contact['notes']
|
appointment.customer_details = contact['notes']
|
||||||
appointment.customer_email = contact['email']
|
appointment.customer_email = contact['email']
|
||||||
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 = []
|
||||||
for timeslot in timeslots:
|
for timeslot in timeslots:
|
||||||
if(timeslot['time'].date() == date):
|
if(timeslot['time'].date() == date):
|
||||||
filtered_timeslots.append(timeslot)
|
filtered_timeslots.append(timeslot)
|
||||||
return filtered_timeslots
|
return filtered_timeslots
|
||||||
|
|
||||||
def convert_to_guest_timezone(guest_tz,datetimeobject):
|
def convert_to_guest_timezone(guest_tz,datetimeobject):
|
||||||
import pytz
|
import pytz
|
||||||
guest_tz = pytz.timezone(guest_tz)
|
guest_tz = pytz.timezone(guest_tz)
|
||||||
local_timezone = pytz.timezone(frappe.utils.get_time_zone())
|
local_timezone = pytz.timezone(frappe.utils.get_time_zone())
|
||||||
datetimeobject = local_timezone.localize(datetimeobject)
|
datetimeobject = local_timezone.localize(datetimeobject)
|
||||||
datetimeobject = datetimeobject.astimezone(guest_tz)
|
datetimeobject = datetimeobject.astimezone(guest_tz)
|
||||||
return datetimeobject
|
return datetimeobject
|
||||||
|
|
||||||
def convert_to_system_timezone(guest_tz,datetimeobject):
|
def convert_to_system_timezone(guest_tz,datetimeobject):
|
||||||
import pytz
|
import pytz
|
||||||
guest_tz = pytz.timezone(guest_tz)
|
guest_tz = pytz.timezone(guest_tz)
|
||||||
datetimeobject = guest_tz.localize(datetimeobject)
|
datetimeobject = guest_tz.localize(datetimeobject)
|
||||||
system_tz = pytz.timezone(frappe.utils.get_time_zone())
|
system_tz = pytz.timezone(frappe.utils.get_time_zone())
|
||||||
datetimeobject = datetimeobject.astimezone(system_tz)
|
datetimeobject = datetimeobject.astimezone(system_tz)
|
||||||
return datetimeobject
|
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:
|
||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
def _get_records(start_time, end_time, settings):
|
def _get_records(start_time, end_time, settings):
|
||||||
records = []
|
records = []
|
||||||
for record in settings.availability_of_slots:
|
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()]:
|
if record.day_of_week == WEEKDAYS[start_time.weekday()] or record.day_of_week == WEEKDAYS[end_time.weekday()]:
|
||||||
records.append(record)
|
records.append(record)
|
||||||
return records
|
return records
|
||||||
|
|
||||||
|
|
||||||
def _deltatime_to_datetime(date, deltatime):
|
def _deltatime_to_datetime(date, deltatime):
|
||||||
time = (datetime.datetime.min + deltatime).time()
|
time = (datetime.datetime.min + deltatime).time()
|
||||||
return datetime.datetime.combine(date.date(), time)
|
return datetime.datetime.combine(date.date(), time)
|
||||||
|
|
||||||
|
|
||||||
def _datetime_to_deltatime(date_time):
|
def _datetime_to_deltatime(date_time):
|
||||||
midnight = datetime.datetime.combine(date_time.date(), datetime.time.min)
|
midnight = datetime.datetime.combine(date_time.date(), datetime.time.min)
|
||||||
return (date_time-midnight)
|
return (date_time-midnight)
|
||||||
@@ -3,18 +3,18 @@ import frappe
|
|||||||
from frappe.utils.verified_command import verify_request
|
from frappe.utils.verified_command import verify_request
|
||||||
@frappe.whitelist(allow_guest=True)
|
@frappe.whitelist(allow_guest=True)
|
||||||
def get_context(context):
|
def get_context(context):
|
||||||
if not verify_request():
|
if not verify_request():
|
||||||
context.success = False
|
context.success = False
|
||||||
return context
|
return context
|
||||||
|
|
||||||
email = frappe.form_dict['email']
|
email = frappe.form_dict['email']
|
||||||
appointment_name = frappe.form_dict['appointment']
|
appointment_name = frappe.form_dict['appointment']
|
||||||
|
|
||||||
if email and appointment_name:
|
if email and appointment_name:
|
||||||
appointment = frappe.get_doc('Appointment',appointment_name)
|
appointment = frappe.get_doc('Appointment',appointment_name)
|
||||||
appointment.set_verified(email)
|
appointment.set_verified(email)
|
||||||
context.success = True
|
context.success = True
|
||||||
return context
|
return context
|
||||||
else:
|
else:
|
||||||
context.success = False
|
context.success = False
|
||||||
return context
|
return context
|
||||||
Reference in New Issue
Block a user