diff --git a/erpnext/www/book-appointment/2.html b/erpnext/www/book-appointment/2.html index 198b12d67c..2a8c5c916c 100644 --- a/erpnext/www/book-appointment/2.html +++ b/erpnext/www/book-appointment/2.html @@ -42,34 +42,9 @@
-
12 pm to 1 am
-
1 am to 2 am
-
2 am to 3 am
-
3 am to 4 am
-
4 am to 5 am
-
5 am to 6 am
-
6 am to 7 am
-
7 am to 8 am
-
-
-
8 am to 9 am
-
9 am to 10 am
-
10 am to 11 am
-
11 am to 12 am
-
12 am to 1 pm
-
1 pm to 2 pm
-
2 pm to 3 pm
-
3 pm to 4pm
-
-
-
4pm to 5pm
-
5 pm to 6 pm
-
6 pm to 7 pm
-
7 pm to 8 pm
-
8 pm to 9 pm
-
9 pm to 10 pm
-
10 pm to 11 pm
-
11 pm to 12 pm
+ {% for timeslot in timeslots %} +
{{ timeslot.time.time().strftime('%H : %M') }}
+ {% endfor %}
diff --git a/erpnext/www/book-appointment/2.js b/erpnext/www/book-appointment/2.js index bdcabdc5ef..113564a722 100644 --- a/erpnext/www/book-appointment/2.js +++ b/erpnext/www/book-appointment/2.js @@ -9,8 +9,12 @@ function select_time() { return } console.log(this.id) - var selected = document.getElementsByClassName('selected')[0]; - selected.classList.remove('selected'); + try{ + selected_element = document.getElementsByClassName('selected')[0] + }catch(e){ + this.classList.add('selected') + } + selected_element.classList.remove('selected'); this.classList.add('selected'); } diff --git a/erpnext/www/book-appointment/2.py b/erpnext/www/book-appointment/2.py index 688545a77d..fa8aafac0b 100644 --- a/erpnext/www/book-appointment/2.py +++ b/erpnext/www/book-appointment/2.py @@ -3,26 +3,91 @@ import datetime def get_context(context): - context.date = frappe.form_dict['date'] + # Get query parameters + date = frappe.form_dict['date'] + tz = frappe.form_dict['tz'] + tz = int(tz) + # Database queries settings = frappe.get_doc('Appointment Booking Settings') holiday_list = frappe.get_doc('Holiday List', settings.holiday_list) - if(is_holiday(context.date,holiday_list)): - context.is_holiday = True - return context - get_time_slots(context.date,settings) - # time_slots = get_time_slots(date) + # Format datetimes + format_string = '%Y-%m-%d %H:%M:%S' + start_time = datetime.datetime.strptime(date+' 00:00:00', format_string) + end_time = datetime.datetime.strptime(date+' 23:59:59', format_string) + # Convert to ist + start_time = _convert_to_ist(start_time, tz) + end_time = _convert_to_ist(end_time, tz) + timeslots = get_available_slots_between(start_time, end_time, settings) + converted_timeslots = [] + print('Appointments') + print(frappe.get_list('Appointment',fields=['from_time'])) + for timeslot in timeslots: + if timeslot > end_time or timeslot < start_time: + pass + else: + if frappe.db.count('Appointment',{'from_time':start_time.time()}) < settings.number_of_agents: + converted_timeslots.append(dict(time=_convert_to_tz(timeslot, tz), unavailable=False)) + else: + converted_timeslots.append(dict(time=_convert_to_tz(timeslot, tz),unavailable=True)) + + context.timeslots = converted_timeslots + context.date = date return context -def is_holiday(date,holiday_list): +def _is_holiday(date, holiday_list): for holiday in holiday_list.holidays: if holiday.holiday_date.isoformat() == date: - print('matched') return True return False +def _convert_to_ist(datetime_object, timezone): + offset = datetime.timedelta(minutes=timezone) + datetime_object = datetime_object + offset + offset = datetime.timedelta(minutes=-330) + datetime_object = datetime_object - offset + return datetime_object + +def _convert_to_tz(datetime_object, timezone): + offset = datetime.timedelta(minutes=timezone) + datetime_object = datetime_object - offset + offset = datetime.timedelta(minutes=-330) + datetime_object = datetime_object + offset + return datetime_object + +def get_available_slots_between(start_time_parameter, end_time_parameter, settings): + records = get_records(start_time_parameter, end_time_parameter, settings) + timeslots = [] + appointment_duration = datetime.timedelta( + minutes=settings.appointment_duration) + for record in records: + if record.day_of_week == weekdays[start_time_parameter.weekday()]: + current_time = _deltatime_to_datetime( + start_time_parameter, record.from_time) + end_time = _deltatime_to_datetime( + start_time_parameter, record.to_time) + elif record.day_of_week == weekdays[end_time_parameter.weekday()]: + current_time = _deltatime_to_datetime( + end_time_parameter, record.from_time) + end_time = _deltatime_to_datetime( + end_time_parameter, record.to_time) + while current_time + appointment_duration <= end_time: + timeslots.append(current_time) + current_time += appointment_duration + return timeslots -def _deltatime_to_time(deltatime): - return (datetime.datetime.min + deltatime).time() +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 -weekdays = ["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"] \ No newline at end of file + +def _deltatime_to_datetime(date, deltatime): + time = (datetime.datetime.min + deltatime).time() + return datetime.datetime.combine(date.date(), time) + + +weekdays = ["Monday", "Tuesday", "Wednesday", + "Thursday", "Friday", "Saturday", "Sunday"]