1 line
12 KiB
JavaScript
Executable File
1 line
12 KiB
JavaScript
Executable File
"use strict";App.Components.AppointmentsModal=function(){function updateTimezone(){var providerId=$selectProvider.val(),provider=vars("available_providers").find(function(availableProvider){return+availableProvider.id===+providerId});provider&&provider.timezone&&$(".provider-timezone").text(vars("timezones")[provider.timezone])}function addEventListeners(){$saveAppointment.on("click",function(){if(validateAppointmentForm()){var startDateTimeObject=App.Utils.UI.getDateTimePickerValue($startDatetime),startDatetime=moment(startDateTimeObject).format("YYYY-MM-DD HH:mm:ss"),endDateTimeObject=App.Utils.UI.getDateTimePickerValue($endDatetime),endDatetime=moment(endDateTimeObject).format("YYYY-MM-DD HH:mm:ss"),appointment={id_services:$selectService.val(),id_users_provider:$selectProvider.val(),start_datetime:startDatetime,end_datetime:endDatetime,location:$appointmentLocation.val(),color:App.Components.ColorSelection.getColor($appointmentColor),status:$appointmentStatus.val(),notes:$appointmentNotes.val(),is_unavailability:0};""!==$appointmentId.val()&&(appointment.id=$appointmentId.val());var customer={first_name:$firstName.val(),last_name:$lastName.val(),email:$email.val(),phone_number:$phoneNumber.val(),address:$address.val(),city:$city.val(),zip_code:$zipCode.val(),language:$language.val(),timezone:$timezone.val(),notes:$customerNotes.val(),custom_field_1:$customField1.val(),custom_field_2:$customField2.val(),custom_field_3:$customField3.val(),custom_field_4:$customField4.val(),custom_field_5:$customField5.val()};""!==$customerId.val()&&(customer.id=$customerId.val(),appointment.id_users_customer=customer.id);var successCallback=function(){App.Layouts.Backend.displayNotification(lang("appointment_saved")),$appointmentsModal.find(".alert").addClass("d-none"),$appointmentsModal.modal("hide"),$reloadAppointments.trigger("click")},errorCallback=function(){$appointmentsModal.find(".modal-message").text(lang("service_communication_error")),$appointmentsModal.find(".modal-message").addClass("alert-danger").removeClass("d-none"),$appointmentsModal.find(".modal-body").scrollTop(0)};App.Http.Calendar.saveAppointment(appointment,customer,successCallback,errorCallback)}}),$insertAppointment.on("click",function(){if($(".popover").remove(),resetModal(),"provider"===$selectFilterItem.find("option:selected").attr("type")){var providerId=$("#select-filter-item").val(),providers=vars("available_providers").filter(function(provider){return+provider.id===+providerId});providers.length&&($selectService.val(providers[0].services[0]).trigger("change"),$selectProvider.val(providerId))}else"service"===$selectFilterItem.find("option:selected").attr("type")?$selectService.find("option[value=\""+$selectFilterItem.val()+"\"]").prop("selected",!0):$selectService.find("option:first").prop("selected",!0).trigger("change");$selectProvider.trigger("change");var serviceId=$selectService.val(),service=vars("available_services").find(function(availableService){return+availableService.id===+serviceId}),duration=service?service.duration:60,startMoment=moment(),currentMin=parseInt(startMoment.format("mm"));0<currentMin&&15>currentMin?startMoment.set({minutes:15}):15<currentMin&&30>currentMin?startMoment.set({minutes:30}):30<currentMin&&45>currentMin?startMoment.set({minutes:45}):startMoment.add(1,"hour").set({minutes:0}),App.Utils.UI.setDateTimePickerValue($startDatetime,startMoment.toDate()),App.Utils.UI.setDateTimePickerValue($endDatetime,startMoment.add(duration,"minutes").toDate()),$appointmentsModal.find(".modal-header h3").text(lang("new_appointment_title")),$appointmentsModal.modal("show")}),$selectCustomer.on("click",function(event){$existingCustomersList.is(":visible")?($existingCustomersList.slideUp("slow"),$filterExistingCustomers.fadeOut("slow"),$(event.target).find("span").text(lang("select"))):($(event.target).find("span").text(lang("hide")),$existingCustomersList.empty(),$existingCustomersList.slideDown("slow"),$filterExistingCustomers.fadeIn("slow").val(""),vars("customers").forEach(function(customer){$("<div/>",{"data-id":customer.id,text:(customer.first_name||"[No First Name]")+" "+(customer.last_name||"[No Last Name]")}).appendTo($existingCustomersList)}))}),$appointmentsModal.on("click","#existing-customers-list div",function(event){var customerId=$(event.target).attr("data-id"),customer=vars("customers").find(function(customer){return+customer.id===+customerId});customer&&($customerId.val(customer.id),$firstName.val(customer.first_name),$lastName.val(customer.last_name),$email.val(customer.email),$phoneNumber.val(customer.phone_number),$address.val(customer.address),$city.val(customer.city),$zipCode.val(customer.zip_code),$language.val(customer.language),$timezone.val(customer.timezone),$customerNotes.val(customer.notes),$customField1.val(customer.custom_field_1),$customField2.val(customer.custom_field_2),$customField3.val(customer.custom_field_3),$customField4.val(customer.custom_field_4),$customField5.val(customer.custom_field_5)),$selectCustomer.trigger("click")});var filterExistingCustomersTimeout=null;$filterExistingCustomers.on("keyup",function(event){filterExistingCustomersTimeout&&clearTimeout(filterExistingCustomersTimeout);var keyword=$(event.target).val().toLowerCase();filterExistingCustomersTimeout=setTimeout(function(){$("#loading").css("visibility","hidden"),App.Http.Customers.search(keyword,50).done(function(response){$existingCustomersList.empty(),response.forEach(function(customer){$("<div/>",{"data-id":customer.id,text:(customer.first_name||"[No First Name]")+" "+(customer.last_name||"[No Last Name]")}).appendTo($existingCustomersList);var result=vars("customers").filter(function(existingCustomer){return+existingCustomer.id===+customer.id});result.length||vars("customers").push(customer)})}).fail(function(){$existingCustomersList.empty(),vars("customers").forEach(function(customer){(-1!==customer.first_name.toLowerCase().indexOf(keyword)||-1!==customer.last_name.toLowerCase().indexOf(keyword)||-1!==customer.email.toLowerCase().indexOf(keyword)||-1!==customer.phone_number.toLowerCase().indexOf(keyword)||-1!==customer.address.toLowerCase().indexOf(keyword)||-1!==customer.city.toLowerCase().indexOf(keyword)||-1!==customer.zip_code.toLowerCase().indexOf(keyword)||-1!==customer.notes.toLowerCase().indexOf(keyword))&&$("<div/>",{"data-id":customer.id,text:(customer.first_name||"[No First Name]")+" "+(customer.last_name||"[No Last Name]")}).appendTo($existingCustomersList)})}).always(function(){$("#loading").css("visibility","")})},1e3)}),$selectService.on("change",function(){var serviceId=$selectService.val(),providerId=$selectProvider.val();$selectProvider.empty();var service=vars("available_services").find(function(availableService){return+availableService.id===+serviceId});null!==service&&void 0!==service&&service.color&&App.Components.ColorSelection.getColor($appointmentColor,service.color);var duration=service?service.duration:60,startDateTimeObject=App.Utils.UI.getDateTimePickerValue($startDatetime),endDateTimeObject=new Date(startDateTimeObject.getTime()+6e4*duration);App.Utils.UI.setDateTimePickerValue($endDatetime,endDateTimeObject),vars("available_providers").forEach(function(provider){provider.services.forEach(function(providerServiceId){vars("role_slug")===App.Layouts.Backend.DB_SLUG_PROVIDER&&+provider.id!==vars("user_id")||vars("role_slug")===App.Layouts.Backend.DB_SLUG_SECRETARY&&-1===vars("secretary_providers").indexOf(+provider.id)||+providerServiceId===+serviceId&&$selectProvider.append(new Option(provider.first_name+" "+provider.last_name,provider.id))}),$selectProvider.find("option[value=\"".concat(providerId,"\"]")).length&&$selectProvider.val(providerId)})}),$selectProvider.on("change",function(){updateTimezone()}),$newCustomer.on("click",function(){$customerId.val(""),$firstName.val(""),$lastName.val(""),$email.val(""),$phoneNumber.val(""),$address.val(""),$city.val(""),$zipCode.val(""),$language.val(vars("default_language")),$timezone.val(vars("default_timezone")),$customerNotes.val(""),$customField1.val(""),$customField2.val(""),$customField3.val(""),$customField4.val(""),$customField5.val("")})}function resetModal(){$appointmentsModal.find("input, textarea").val(""),$appointmentsModal.find(".modal-message").addClass(".d-none");var defaultStatusValue=$appointmentStatus.find("option:first").val();$appointmentStatus.val(defaultStatusValue),$language.val(vars("default_language")),$timezone.val(vars("default_timezone")),$appointmentColor.find(".color-selection-option:first").trigger("click"),$selectService.val($selectService.eq(0).attr("value")),$selectProvider.empty(),vars("available_providers").forEach(function(provider){var serviceId=$selectService.val(),canProvideService=0<provider.services.filter(function(providerServiceId){return+providerServiceId===+serviceId}).length;canProvideService&&$selectProvider.append(new Option(provider.first_name+" "+provider.last_name,provider.id))}),$existingCustomersList.slideUp("slow"),$filterExistingCustomers.fadeOut("slow"),$selectCustomer.find("span").text(lang("select"));var serviceId=$selectService.val(),service=vars("available_services").forEach(function(service){return+service.id===+serviceId}),duration=service?service.duration:0,startDatetime=new Date,endDatetime=moment().add(duration,"minutes").toDate();App.Utils.UI.initializeDateTimePicker($startDatetime,{onClose:function onClose(){var serviceId=$selectService.val(),service=vars("available_services").find(function(availableService){return+availableService.id===+serviceId}),startDateTimeObject=App.Utils.UI.getDateTimePickerValue($startDatetime),endDateTimeObject=new Date(startDateTimeObject.getTime()+6e4*service.duration);App.Utils.UI.setDateTimePickerValue($endDatetime,endDateTimeObject)}}),App.Utils.UI.setDateTimePickerValue($startDatetime,startDatetime),App.Utils.UI.initializeDateTimePicker($endDatetime),App.Utils.UI.setDateTimePickerValue($endDatetime,endDatetime)}function validateAppointmentForm(){$appointmentsModal.find(".is-invalid").removeClass("is-invalid"),$appointmentsModal.find(".modal-message").addClass("d-none");try{var missingRequiredField=!1;if($appointmentsModal.find(".required").each(function(index,requiredField){(""===$(requiredField).val()||null===$(requiredField).val())&&($(requiredField).addClass("is-invalid"),missingRequiredField=!0)}),missingRequiredField)throw new Error(lang("fields_are_required"));if($appointmentsModal.find("#email").val()&&!App.Utils.Validation.email($appointmentsModal.find("#email").val()))throw $appointmentsModal.find("#email").addClass("is-invalid"),new Error(lang("invalid_email"));var startDateTimeObject=App.Utils.UI.getDateTimePickerValue($startDatetime),endDateTimeObject=App.Utils.UI.getDateTimePickerValue($endDatetime);if(startDateTimeObject>endDateTimeObject)throw $startDatetime.addClass("is-invalid"),$endDatetime.addClass("is-invalid"),new Error(lang("start_date_before_end_error"));return!0}catch(error){return $appointmentsModal.find(".modal-message").addClass("alert-danger").text(error.message).removeClass("d-none"),!1}}var $appointmentsModal=$("#appointments-modal"),$startDatetime=$("#start-datetime"),$endDatetime=$("#end-datetime"),$filterExistingCustomers=$("#filter-existing-customers"),$customerId=$("#customer-id"),$firstName=$("#first-name"),$lastName=$("#last-name"),$email=$("#email"),$phoneNumber=$("#phone-number"),$address=$("#address"),$city=$("#city"),$zipCode=$("#zip-code"),$language=$("#language"),$timezone=$("#timezone"),$customerNotes=$("#customer-notes"),$selectCustomer=$("#select-customer"),$saveAppointment=$("#save-appointment"),$appointmentId=$("#appointment-id"),$appointmentLocation=$("#appointment-location"),$appointmentStatus=$("#appointment-status"),$appointmentColor=$("#appointment-color"),$appointmentNotes=$("#appointment-notes"),$reloadAppointments=$("#reload-appointments"),$selectFilterItem=$("#select-filter-item"),$selectService=$("#select-service"),$selectProvider=$("#select-provider"),$insertAppointment=$("#insert-appointment"),$existingCustomersList=$("#existing-customers-list"),$newCustomer=$("#new-customer"),$customField1=$("#custom-field-1"),$customField2=$("#custom-field-2"),$customField3=$("#custom-field-3"),$customField4=$("#custom-field-4"),$customField5=$("#custom-field-5"),moment=window.moment;return document.addEventListener("DOMContentLoaded",function(){addEventListeners()}),{resetModal:resetModal}}(); |