Styling and PR review changes

This commit is contained in:
Pranav Nachanekar 2019-09-17 16:58:41 +05:30
parent 018f0d3bbd
commit 91a564989f
6 changed files with 79 additions and 92 deletions

View File

@ -20,7 +20,6 @@ class Appointment(Document):
frappe.throw('Time slot is not available') frappe.throw('Time slot is not available')
def before_insert(self): def before_insert(self):
appointment_event = frappe.new_doc('Event')
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]),
@ -37,7 +36,6 @@ class Appointment(Document):
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]
agent = frappe.json.loads(agent)[0]
add_assignemnt({ add_assignemnt({
'doctype':self.doctype, 'doctype':self.doctype,
'name':self.name, 'name':self.name,
@ -45,33 +43,25 @@ class Appointment(Document):
}) })
employee = _get_employee_from_user(agent) employee = _get_employee_from_user(agent)
if employee: if employee:
print(employee)
calendar_event = frappe.get_doc('Event', self.calendar_event) calendar_event = frappe.get_doc('Event', self.calendar_event)
calendar_event.append('event_participants', dict( calendar_event.append('event_participants', dict(
reference_doctype= 'Employee', reference_doctype= 'Employee',
reference_docname=employee[0].name)) reference_docname= employee.name))
print(calendar_event)
calendar_event.save() calendar_event.save()
break break
def _get_agents_sorted_by_asc_workload(): def _get_agents_sorted_by_asc_workload():
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)
print(assigned_to) if not assigned_to:
if appointment._assign == '[]' or not appointment._assign:
continue continue
if assigned_to[0] in agent_list: if assigned_to[0] in agent_list:
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()
@ -81,10 +71,8 @@ def _get_agents_sorted_by_asc_workload():
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
@ -97,4 +85,7 @@ def _check_agent_availability(agent_email,scheduled_time):
def _get_employee_from_user(user): def _get_employee_from_user(user):
return frappe.get_list('Employee', fields='*',filters={'user_id':user}) employee_docname = frappe.db.exists({'doctype':'Employee','user_id':user})
if employee_docname:
return frappe.get_doc('Employee',employee_docname[0][0]) # frappe.db.exists returns a tuple of a tuple
return None

View File

@ -8,20 +8,8 @@ import unittest
import datetime import datetime
def create_appointments(number):
for i in range(1, number):
frappe.get_doc({
'doctype': 'Appointment',
'scheduled_time': datetime.datetime.min,
'customer_name': 'Test Customer'+str(i),
'customer_phone_number': '8088',
'customer_skype': 'test'+str(i),
})
def delete_appointments(): def delete_appointments():
doc_list = frappe.get_list('Appointment',filters={'scheduled_time':datetime.datetime.min,'customer_phone_number':'8088'}) pass
for doc in doc_list:
doc.delete()
class TestAppointment(unittest.TestCase): class TestAppointment(unittest.TestCase):

View File

@ -14,17 +14,22 @@ class AppointmentBookingSettings(Document):
list_of_days = [] list_of_days = []
date = '01/01/1970 ' date = '01/01/1970 '
format_string = "%d/%m/%Y %H:%M:%S" format_string = "%d/%m/%Y %H:%M:%S"
for record in self.availability_of_slots: for record in self.availability_of_slots:
list_of_days.append(record.day_of_week) list_of_days.append(record.day_of_week)
# Difference between from_time and to_time is multiple of appointment_duration # Difference between from_time and to_time is multiple of appointment_duration
from_time = datetime.datetime.strptime(date+record.from_time, format_string) from_time = datetime.datetime.strptime(date+record.from_time, format_string)
to_time = datetime.datetime.strptime(date+record.to_time, format_string) to_time = datetime.datetime.strptime(date+record.to_time, format_string)
timedelta = to_time-from_time timedelta = to_time-from_time
if(from_time > to_time): if(from_time > to_time):
frappe.throw('From Time cannot be later than To Time for '+record.day_of_week) frappe.throw('From Time cannot be later than To Time for '+record.day_of_week)
if timedelta.total_seconds() % (self.appointment_duration * 60): if timedelta.total_seconds() % (self.appointment_duration * 60):
frappe.throw('The difference between from time and To Time must be a multiple of Appointment ') frappe.throw('The difference between from time and To Time must be a multiple of Appointment ')
set_of_days = set(list_of_days) set_of_days = set(list_of_days)
if len(list_of_days) > len(set_of_days): if len(list_of_days) > len(set_of_days):
frappe.throw(_('Days of week must be unique')) frappe.throw(_('Days of week must be unique'))

View File

@ -10,8 +10,6 @@ from frappe.model.document import Document
class Timezone(Document): class Timezone(Document):
def validate(self): def validate(self):
if self.offset > 720 or self.offset < -720: if self.offset > 720 or self.offset < -720:
frappe.throw( frappe.throw('Timezone offsets must be between -720 and +720 minutes')
'Timezone offsets must be between -720 and +720 minutes')
if frappe.db.exists({'doctype':'Timezone','offset':self.offset}): if frappe.db.exists({'doctype':'Timezone','offset':self.offset}):
frappe.throw( frappe.throw('Timezone offsets need to be unique')
'Timezone offsets need to be unique')

View File

@ -13,6 +13,7 @@ async function initialise_select_date() {
} }
async function get_global_variables() { async function get_global_variables() {
// Using await
window.appointment_settings = (await frappe.call({ window.appointment_settings = (await frappe.call({
method: 'erpnext.www.book-appointment.index.get_appointment_settings' method: 'erpnext.www.book-appointment.index.get_appointment_settings'
})).message })).message
@ -29,9 +30,9 @@ async function get_global_variables() {
function setup_timezone_selector() { function setup_timezone_selector() {
let timezones_element = document.getElementById('appointment-timezone'); let timezones_element = document.getElementById('appointment-timezone');
var offset = new Date().getTimezoneOffset(); let offset = new Date().getTimezoneOffset();
window.timezones.forEach(timezone => { window.timezones.forEach(timezone => {
var opt = document.createElement('option'); let opt = document.createElement('option');
opt.value = timezone.offset; opt.value = timezone.offset;
opt.innerHTML = timezone.timezone_name; opt.innerHTML = timezone.timezone_name;
opt.defaultSelected = (offset == timezone.offset) opt.defaultSelected = (offset == timezone.offset)
@ -50,7 +51,7 @@ function setup_date_picker() {
function hide_next_button() { function hide_next_button() {
let next_button = document.getElementById('next-button'); let next_button = document.getElementById('next-button');
next_button.disabled = true; next_button.disabled = true;
next_button.onclick = ()=>{frappe.msgprint("Please select a date and time")}; next_button.onclick = () => frappe.msgprint("Please select a date and time");
} }
function show_next_button() { function show_next_button() {
@ -96,27 +97,35 @@ async function update_time_slots(selected_date, selected_timezone) {
return return
} }
window.slots.forEach((slot, index) => { window.slots.forEach((slot, index) => {
// Add a break after each 8 elements
if (index % 8 == 0) { if (index % 8 == 0) {
let break_element = document.createElement('div'); let break_element = document.createElement('div');
break_element.classList.add('w-100'); break_element.classList.add('w-100');
timeslot_container.appendChild(break_element); timeslot_container.appendChild(break_element);
} }
let start_time = new Date(slot.time) // Get and append timeslot div
var timeslot_div = document.createElement('div'); let timeslot_div = get_timeslot_div_layout(slot)
timeslot_div.classList.add('time-slot');
timeslot_div.classList.add('col-md');
if (!slot.availability) {
timeslot_div.classList.add('unavailable')
}
timeslot_div.innerHTML = get_slot_layout(start_time);
timeslot_div.id = slot.time.substr(11, 20);
timeslot_div.addEventListener('click', select_time);
timeslot_container.appendChild(timeslot_div); timeslot_container.appendChild(timeslot_div);
}); });
set_default_timeslot(); set_default_timeslot();
} }
function get_timeslot_div_layout(timeslot) {
let start_time = new Date(timeslot.time)
let timeslot_div = document.createElement('div');
timeslot_div.classList.add('time-slot');
timeslot_div.classList.add('col-md');
if (!timeslot.availability) {
timeslot_div.classList.add('unavailable')
}
timeslot_div.innerHTML = get_slot_layout(start_time);
timeslot_div.id = timeslot.time.substr(11, 20);
timeslot_div.addEventListener('click', select_time);
return timeslot_div
}
function clear_time_slots() { function clear_time_slots() {
// Clear any existing divs in timeslot container
let timeslot_container = document.getElementById('timeslot-container'); let timeslot_container = document.getElementById('timeslot-container');
while (timeslot_container.firstChild) { while (timeslot_container.firstChild) {
timeslot_container.removeChild(timeslot_container.firstChild) timeslot_container.removeChild(timeslot_container.firstChild)
@ -132,17 +141,18 @@ function get_slot_layout(time) {
} }
function select_time() { function select_time() {
if (this.classList.contains("unavailable")) { if (this.classList.contains('unavailable')) {
return return
} }
try { let selected_element = document.getElementsByClassName('selected');
selected_element = document.getElementsByClassName('selected')[0] if (!(selected_element.length > 0)){
} catch (e) { this.classList.add('selected')
this.classList.add("selected") return
} }
selected_element = selected_element[0]
window.selected_time = this.id window.selected_time = this.id
selected_element.classList.remove("selected"); selected_element.classList.remove('selected');
this.classList.add("selected"); this.classList.add('selected');
show_next_button(); show_next_button();
} }
@ -151,7 +161,7 @@ function set_default_timeslot() {
for (let i = 0; i < timeslots.length; i++) { for (let i = 0; i < timeslots.length; i++) {
const timeslot = timeslots[i]; const timeslot = timeslots[i];
if (!timeslot.classList.contains('unavailable')) { if (!timeslot.classList.contains('unavailable')) {
timeslot.classList.add("selected"); timeslot.classList.add('selected');
break; break;
} }
} }
@ -170,7 +180,7 @@ function navigate_to_page(page_number){
page2.style.display = 'block'; page2.style.display = 'block';
break; break;
default: default:
console.log("That's not a valid page") break;
} }
} }
@ -184,7 +194,7 @@ function setup_details_page(){
async function submit() { async function submit() {
// form validation here // form validation here
form_validation(); get_form_data();
let appointment = (await frappe.call({ let appointment = (await frappe.call({
method: 'erpnext.www.book-appointment.index.create_appointment', method: 'erpnext.www.book-appointment.index.create_appointment',
args: { args: {
@ -196,12 +206,10 @@ async function submit() {
frappe.msgprint(__('Appointment Created Successfully')); frappe.msgprint(__('Appointment Created Successfully'));
let button = document.getElementById('submit-button'); let button = document.getElementById('submit-button');
button.disabled = true; button.disabled = true;
button.onclick = () => { console.log('This should never have happened') } button.onclick = null
} }
function form_validation(){ function get_form_data() {
var date = window.selected_date;
var time = window.selected_time;
contact = {}; contact = {};
contact.name = document.getElementById('customer_name').value; contact.name = document.getElementById('customer_name').value;
contact.number = document.getElementById('customer_number').value; contact.number = document.getElementById('customer_number').value;
@ -209,5 +217,4 @@ function form_validation(){
contact.notes = document.getElementById('customer_notes').value; contact.notes = document.getElementById('customer_notes').value;
contact.email = document.getElementById('customer_email').value; contact.email = document.getElementById('customer_email').value;
window.contact = contact window.contact = contact
console.log({ date, time, contact });
} }

View File

@ -2,6 +2,10 @@ import frappe
import datetime import datetime
import json import json
WEEKDAYS = ["Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday", "Sunday"]
no_cache = 1 no_cache = 1
@ -98,11 +102,9 @@ def create_appointment(date, time, contact):
appointment.insert() appointment.insert()
def find_lead_by_email(email): def find_lead_by_email(email):
if frappe.db.exists({ lead_list = frappe.get_list('Lead',filters={'email_id':email})[0]
'doctype':'Lead', if lead_list:
'email_id':email return lead_list
}):
return frappe.get_list('Lead',filters={'email_id':email})[0]
frappe.throw('Email ID not associated with any Lead. Please make sure to use the email address you got this mail on') frappe.throw('Email ID not associated with any Lead. Please make sure to use the email address you got this mail on')
# Helper Functions # Helper Functions
@ -157,7 +159,3 @@ def _convert_to_tz(datetime_object, timezone):
offset = datetime.timedelta(minutes=-330) offset = datetime.timedelta(minutes=-330)
datetime_object = datetime_object + offset datetime_object = datetime_object + offset
return datetime_object return datetime_object
WEEKDAYS = ["Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday", "Sunday"]