appointments.js
1 year ago
attendees.js
1 year ago
colorManipulation.js
3 years ago
customer.js
1 year ago
date.js
1 year ago
defaultCustomize.js
1 year ago
employee.js
1 year ago
events.js
1 year ago
formFieldsTemplates.js
3 years ago
formatting.js
1 year ago
helper.js
1 year ago
image.js
1 year ago
integrationApple.js
1 year ago
integrationGoogle.js
1 year ago
integrationOutlook.js
1 year ago
integrationStripe.js
1 year ago
integrationZoom.js
1 year ago
licence.js
2 years ago
objectAndArrayManipulation.js
3 years ago
pricing.js
1 year ago
recurring.js
1 year ago
responsive.js
1 year ago
scrollElements.js
4 years ago
settings.js
1 year ago
translationsElementPlus.js
1 year ago
employee.js
408 lines
| 1 | import moment from "moment"; |
| 2 | import {settings} from "../../../plugins/settings"; |
| 3 | import {useTimeInSeconds} from "./date"; |
| 4 | |
| 5 | function useParsedCustomPricing (service) { |
| 6 | if (!('customPricing' in service) || service.customPricing === null) { |
| 7 | service.customPricing = {enabled: true, durations: {}} |
| 8 | |
| 9 | service.customPricing.durations[service.duration] = {price: service.price, rules: []} |
| 10 | } else { |
| 11 | let customPricing = (typeof service.customPricing === 'object') ? service.customPricing : JSON.parse(service.customPricing) |
| 12 | |
| 13 | service.customPricing = {enabled: true, durations: {}} |
| 14 | |
| 15 | service.customPricing.durations[service.duration] = {price: service.price, rules: []} |
| 16 | |
| 17 | if (customPricing.enabled) { |
| 18 | service.customPricing.durations = Object.assign( |
| 19 | service.customPricing.durations, |
| 20 | customPricing.durations |
| 21 | ) |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | return service.customPricing |
| 26 | } |
| 27 | |
| 28 | function getEmployeeServicePrice (store, providerId, serviceId) { |
| 29 | let employeeService = store.getters['entities/getEmployeeService'](providerId, serviceId) |
| 30 | |
| 31 | let duration = store.getters['booking/getBookingDuration'] ? store.getters['booking/getBookingDuration'] : employeeService.duration |
| 32 | |
| 33 | if (employeeService.customPricing && |
| 34 | employeeService.customPricing.enabled && |
| 35 | duration && |
| 36 | duration in employeeService.customPricing.durations |
| 37 | ) { |
| 38 | return employeeService.customPricing.durations[duration].price |
| 39 | } |
| 40 | |
| 41 | return employeeService.price |
| 42 | } |
| 43 | |
| 44 | function sortForEmployeeSelection (store, employeesIds, serviceId) { |
| 45 | switch (settings.appointments.employeeSelection) { |
| 46 | case 'roundRobin': { |
| 47 | let lastBookedProviderId = store.getters['booking/getLastBookedProviderId'] |
| 48 | employeesIds = employeesIds.map(e => parseInt(e)).sort((a,b) => a-b) |
| 49 | // ! employeeId was not used in the loop |
| 50 | for (let employeeId of employeesIds) { |
| 51 | if (parseInt(employeesIds[0]) > parseInt(lastBookedProviderId)) { |
| 52 | break |
| 53 | } |
| 54 | employeesIds.push(employeesIds.shift()) |
| 55 | |
| 56 | } |
| 57 | return employeesIds |
| 58 | } |
| 59 | case 'lowestPrice': |
| 60 | return employeesIds.sort((emp1, emp2) => { |
| 61 | let price1 = getEmployeeServicePrice(store, emp1, serviceId) |
| 62 | let price2 = getEmployeeServicePrice(store, emp2, serviceId) |
| 63 | if (price1 < price2) { |
| 64 | return -1 |
| 65 | } else if (price1 === price2) { |
| 66 | return emp1 < emp2 ? -1 : 1 |
| 67 | } else { |
| 68 | return 1 |
| 69 | } |
| 70 | }) |
| 71 | case 'highestPrice': |
| 72 | return employeesIds.sort((emp1, emp2) => { |
| 73 | let price1 = getEmployeeServicePrice(store, emp1, serviceId) |
| 74 | let price2 = getEmployeeServicePrice(store, emp2, serviceId) |
| 75 | if (price1 < price2) { |
| 76 | return 1 |
| 77 | } else if (price1 === price2) { |
| 78 | return emp1 < emp2 ? -1 : 1 |
| 79 | } else { |
| 80 | return -1 |
| 81 | } |
| 82 | }) |
| 83 | case 'random': default: |
| 84 | return employeesIds |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | function checkLimitPerEmployee (employeesIds, bookingIndex, bookings, booking, appCount, chosenEmployees, serviceId) { |
| 89 | let filteredEmployeeIds = [] |
| 90 | for (let employeeId of employeesIds) { |
| 91 | let count = appCount && appCount[employeeId] && appCount[employeeId][booking.date] ? appCount[employeeId][booking.date] : 0 |
| 92 | |
| 93 | let otherServiceBookings = chosenEmployees.filter(e => e.providerId === employeeId && e.date === booking.date && e.serviceId !== serviceId && !e.existingApp) |
| 94 | let otherBookings = bookings.filter((e, index) => e.providerId === employeeId && e.date === booking.date && bookingIndex !== index && !e.existingApp) |
| 95 | |
| 96 | if (otherBookings.length + otherServiceBookings.length + count < settings.roles.limitPerEmployee.numberOfApp) { |
| 97 | filteredEmployeeIds.push(employeeId) |
| 98 | } |
| 99 | } |
| 100 | if (filteredEmployeeIds.length === 0) { |
| 101 | return {'employeeIds': filteredEmployeeIds, 'bookingFailed' : bookingIndex} |
| 102 | } |
| 103 | return {'employeeIds': filteredEmployeeIds, 'bookingFailed' : null} |
| 104 | } |
| 105 | |
| 106 | function getFrontendPeriodList (periodList) { |
| 107 | periodList.forEach((period) => { |
| 108 | period.startTime = period.startTime.substring(0, 5) |
| 109 | period.endTime = period.endTime.substring(0, 5) |
| 110 | period.locationId = period.locationId ? period.locationId : null |
| 111 | period.periodServiceList = period.periodServiceList.map(i => i.serviceId) |
| 112 | period.periodLocationList = period.periodLocationList.map(i => i.locationId) |
| 113 | }) |
| 114 | |
| 115 | return periodList.sort((a, b) => { |
| 116 | return useTimeInSeconds(a.startTime) - useTimeInSeconds(b.startTime); |
| 117 | }) |
| 118 | } |
| 119 | |
| 120 | function useFrontendEmployeeServiceList (store, employeeServiceList) { |
| 121 | let serviceList = {} |
| 122 | |
| 123 | store.getters['entities/getCategories'].forEach((category) => { |
| 124 | category.serviceList.forEach((service) => { |
| 125 | let employeeService = employeeServiceList.find(s => s.id === service.id) |
| 126 | |
| 127 | if (!(service.categoryId in serviceList)) { |
| 128 | serviceList[service.categoryId] = {} |
| 129 | } |
| 130 | |
| 131 | if (!(service.id in serviceList[service.categoryId])) { |
| 132 | serviceList[service.categoryId][service.id] = {} |
| 133 | } |
| 134 | |
| 135 | serviceList[service.categoryId][service.id] = typeof employeeService === 'undefined' ? { |
| 136 | enabled: false, |
| 137 | price: service.price.toString(), |
| 138 | minCapacity: service.minCapacity, |
| 139 | maxCapacity: service.maxCapacity, |
| 140 | customPricing: service.customPricing, |
| 141 | } : { |
| 142 | enabled: true, |
| 143 | price: employeeService.price.toString(), |
| 144 | minCapacity: employeeService.minCapacity, |
| 145 | maxCapacity: employeeService.maxCapacity, |
| 146 | customPricing: employeeService.customPricing, |
| 147 | } |
| 148 | }) |
| 149 | }) |
| 150 | |
| 151 | return serviceList |
| 152 | } |
| 153 | |
| 154 | function useFrontendEmployee (store, employee) { |
| 155 | let weekDayList = [] |
| 156 | |
| 157 | settings.weekSchedule.forEach((weekDay, index) => { |
| 158 | let employeeWeekDay = employee.weekDayList.find(i => parseInt(i.dayIndex) === index + 1) |
| 159 | |
| 160 | let timeOutList = [] |
| 161 | |
| 162 | if (typeof employeeWeekDay !== 'undefined') { |
| 163 | employeeWeekDay.timeOutList.forEach((timeOut) => { |
| 164 | timeOutList.push({ |
| 165 | startTime: timeOut.startTime.substring(0, 5), |
| 166 | endTime: timeOut.endTime.substring(0, 5), |
| 167 | }) |
| 168 | }) |
| 169 | } |
| 170 | |
| 171 | weekDayList.push( |
| 172 | typeof employeeWeekDay === 'undefined' |
| 173 | ? { |
| 174 | enabled: false, |
| 175 | id: null, |
| 176 | dayIndex: index + 1, |
| 177 | startTime: '', |
| 178 | endTime: '', |
| 179 | periodList: [], |
| 180 | timeOutList: [], |
| 181 | } |
| 182 | : Object.assign( |
| 183 | {}, |
| 184 | employeeWeekDay, |
| 185 | { |
| 186 | enabled: true, |
| 187 | startTime: employeeWeekDay.startTime.substring(0, 5), |
| 188 | endTime: employeeWeekDay.endTime.substring(0, 5), |
| 189 | periodList: getFrontendPeriodList(employeeWeekDay.periodList), |
| 190 | timeOutList: timeOutList, |
| 191 | } |
| 192 | ) |
| 193 | ) |
| 194 | }) |
| 195 | |
| 196 | let specialDayList = [] |
| 197 | |
| 198 | employee.specialDayList.forEach((specialDay) => { |
| 199 | specialDayList.push({ |
| 200 | id: specialDay.id, |
| 201 | range: [moment(specialDay.startDate).toDate(), moment(specialDay.endDate).toDate()], |
| 202 | periodList: getFrontendPeriodList(specialDay.periodList), |
| 203 | }) |
| 204 | }) |
| 205 | |
| 206 | let dayOffList = [] |
| 207 | |
| 208 | employee.dayOffList.forEach((dayOff) => { |
| 209 | dayOffList.push({ |
| 210 | id: dayOff.id, |
| 211 | name: dayOff.name, |
| 212 | repeat: dayOff.repeat, |
| 213 | range: [moment(dayOff.startDate).toDate(), moment(dayOff.endDate).toDate()], |
| 214 | }) |
| 215 | }) |
| 216 | |
| 217 | let descriptionMode = !employee.description || employee.description.startsWith('<!-- Content -->') ? 'text' : 'html' |
| 218 | |
| 219 | let description = employee.description ? employee.description.replace('<!-- Content -->', '') : '' |
| 220 | |
| 221 | return { |
| 222 | id: employee.id, |
| 223 | firstName: employee.firstName, |
| 224 | lastName: employee.lastName, |
| 225 | email: employee.email, |
| 226 | phone: employee.phone, |
| 227 | description: description, |
| 228 | descriptionMode: descriptionMode, |
| 229 | externalId: employee.externalId, |
| 230 | googleCalendar: employee.googleCalendar, |
| 231 | outlookCalendar: employee.outlookCalendar, |
| 232 | appleCalendarId: employee.appleCalendarId, |
| 233 | stripeConnect: employee.stripeConnect, |
| 234 | zoomUserId: employee.zoomUserId, |
| 235 | locationId: employee.locationId ? employee.locationId : '', |
| 236 | serviceList: employee.serviceList, |
| 237 | weekDayList: weekDayList, |
| 238 | specialDayList: specialDayList, |
| 239 | dayOffList: dayOffList, |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | function getBackendPeriodList (store, periodList) { |
| 244 | let dayPeriodList = [] |
| 245 | |
| 246 | let servicesIds = useEmployeeServices(store).map(i => i.id) |
| 247 | |
| 248 | periodList.forEach((period) => { |
| 249 | dayPeriodList.push({ |
| 250 | id: period.id, |
| 251 | locationId: period.locationId ? period.locationId : null, |
| 252 | startTime: period.startTime + ':00', |
| 253 | endTime: period.endTime + ':00', |
| 254 | periodServiceList: period.periodServiceList |
| 255 | .filter(id => servicesIds.indexOf(id) !== -1) |
| 256 | .map(id => new Object({id: null, serviceId: id})), |
| 257 | periodLocationList: period.periodLocationList.map(id => new Object({id: null, locationId: id})), |
| 258 | }) |
| 259 | }) |
| 260 | |
| 261 | return dayPeriodList |
| 262 | } |
| 263 | |
| 264 | function getBackendTimeOutList (store, timeOutList) { |
| 265 | let dayTimeOutList = [] |
| 266 | |
| 267 | timeOutList.forEach((timeOut) => { |
| 268 | dayTimeOutList.push({ |
| 269 | id: timeOut.id, |
| 270 | startTime: timeOut.startTime + ':00', |
| 271 | endTime: timeOut.endTime + ':00', |
| 272 | }) |
| 273 | }) |
| 274 | |
| 275 | return dayTimeOutList |
| 276 | } |
| 277 | |
| 278 | function useBackendEmployee (store, timeZone) { |
| 279 | let employee = store.getters['employee/getEmployee'] |
| 280 | |
| 281 | let serviceList = [] |
| 282 | |
| 283 | Object.keys(employee.serviceList).forEach((categoryId) => { |
| 284 | Object.keys(employee.serviceList[categoryId]).forEach((serviceId) => { |
| 285 | if (employee.serviceList[categoryId][serviceId].enabled) { |
| 286 | let service = store.getters['entities/getCategory'](categoryId).serviceList.find(i => i.id === parseInt(serviceId)) |
| 287 | |
| 288 | let employeeService = { |
| 289 | id: parseInt(serviceId), |
| 290 | minCapacity: parseInt(employee.serviceList[categoryId][serviceId].minCapacity), |
| 291 | maxCapacity: parseInt(employee.serviceList[categoryId][serviceId].maxCapacity), |
| 292 | price: parseFloat(employee.serviceList[categoryId][serviceId].customPricing.durations[service.duration].price), |
| 293 | customPricing: { |
| 294 | enabled: Object.keys(employee.serviceList[categoryId][serviceId].customPricing.durations).length > 1, |
| 295 | durations: {}, |
| 296 | }, |
| 297 | } |
| 298 | |
| 299 | Object.keys(employee.serviceList[categoryId][serviceId].customPricing.durations).forEach((duration) => { |
| 300 | employeeService.customPricing.durations[duration] = { |
| 301 | price: parseFloat(employee.serviceList[categoryId][serviceId].customPricing.durations[duration].price), |
| 302 | rules: [], |
| 303 | } |
| 304 | }) |
| 305 | |
| 306 | delete employeeService.customPricing.durations[service.duration] |
| 307 | |
| 308 | employeeService.customPricing = JSON.stringify(employeeService.customPricing) |
| 309 | |
| 310 | serviceList.push(employeeService) |
| 311 | } |
| 312 | }) |
| 313 | }) |
| 314 | |
| 315 | let weekDayList = [] |
| 316 | |
| 317 | employee.weekDayList.forEach((weekDay) => { |
| 318 | if (weekDay.enabled) { |
| 319 | let periodList = getBackendPeriodList(store, weekDay.periodList) |
| 320 | |
| 321 | if (periodList.length) { |
| 322 | let timeOutList = getBackendTimeOutList(store, weekDay.timeOutList) |
| 323 | |
| 324 | weekDayList.push({ |
| 325 | id: weekDay.id, |
| 326 | dayIndex: weekDay.dayIndex, |
| 327 | startTime: periodList.length ? periodList[0].startTime : weekDay.startTime, |
| 328 | endTime: periodList.length ? periodList[periodList.length - 1].endTime : weekDay.endTime, |
| 329 | periodList: periodList, |
| 330 | timeOutList: timeOutList, |
| 331 | }) |
| 332 | } |
| 333 | } |
| 334 | }) |
| 335 | |
| 336 | let specialDayList = [] |
| 337 | |
| 338 | employee.specialDayList.forEach((specialDay) => { |
| 339 | let periodList = getBackendPeriodList(store, specialDay.periodList) |
| 340 | |
| 341 | if (periodList.length) { |
| 342 | specialDayList.push({ |
| 343 | id: specialDay.id, |
| 344 | startDate: moment(specialDay.range[0]).format('YYYY-MM-DD'), |
| 345 | endDate: moment(specialDay.range[1]).format('YYYY-MM-DD'), |
| 346 | periodList: periodList, |
| 347 | }) |
| 348 | } |
| 349 | }) |
| 350 | |
| 351 | let dayOffList = [] |
| 352 | |
| 353 | employee.dayOffList.forEach((dayOff) => { |
| 354 | dayOffList.push({ |
| 355 | id: dayOff.id, |
| 356 | name: dayOff.name, |
| 357 | startDate: moment(dayOff.range[0]).format('YYYY-MM-DD'), |
| 358 | endDate: moment(dayOff.range[1]).format('YYYY-MM-DD'), |
| 359 | repeat: dayOff.repeat, |
| 360 | }) |
| 361 | }) |
| 362 | |
| 363 | let result = Object.assign( |
| 364 | {}, |
| 365 | employee, |
| 366 | { |
| 367 | description: store.getters['employee/getDescription'] && store.getters['employee/getDescriptionMode'] === 'text' |
| 368 | ? '<!-- Content -->' + store.getters['employee/getDescription'] |
| 369 | : store.getters['employee/getDescription'], |
| 370 | serviceList: serviceList, |
| 371 | weekDayList: weekDayList, |
| 372 | specialDayList: specialDayList, |
| 373 | dayOffList: dayOffList, |
| 374 | timeZone: store.getters['cabinet/getTimeZone'] === timeZone |
| 375 | ? '' |
| 376 | : store.getters['cabinet/getTimeZone'], |
| 377 | } |
| 378 | ) |
| 379 | |
| 380 | delete result.descriptionMode |
| 381 | |
| 382 | return result |
| 383 | } |
| 384 | |
| 385 | function useEmployeeServices (store) { |
| 386 | let serviceIds = [] |
| 387 | |
| 388 | Object.keys(store.getters['employee/getServiceList']).forEach(categoryId => { |
| 389 | Object.keys(store.getters['employee/getServiceList'][categoryId]).forEach((serviceId) => { |
| 390 | if (store.getters['employee/getServiceList'][categoryId][serviceId].enabled) { |
| 391 | serviceIds.push(parseInt(serviceId)) |
| 392 | } |
| 393 | }) |
| 394 | }) |
| 395 | |
| 396 | return store.getters['entities/getServices'].filter(i => serviceIds.indexOf(i.id) !== -1) |
| 397 | } |
| 398 | |
| 399 | export { |
| 400 | useEmployeeServices, |
| 401 | useParsedCustomPricing, |
| 402 | sortForEmployeeSelection, |
| 403 | checkLimitPerEmployee, |
| 404 | useFrontendEmployee, |
| 405 | useBackendEmployee, |
| 406 | useFrontendEmployeeServiceList, |
| 407 | } |
| 408 |