cssColorManipulationMixin.js
4 years ago
currentUserMixin.js
3 years ago
customFieldMixin.js
4 years ago
dateMixin.js
3 years ago
durationMixin.js
3 years ago
employeeMixin.js
1 year ago
emptyMixin.js
2 years ago
entitiesMixin.js
1 year ago
eventMixin.js
2 years ago
formsCustomizationMixin.js
3 years ago
imageMixin.js
1 year ago
licenceMixin.js
1 year ago
phoneCountriesMixin.js
4 years ago
priceMixin.js
2 years ago
propertiesMixin.js
1 year ago
recurringMixin.js
1 year ago
responsiveMixin.js
7 years ago
serviceMixin.js
1 year ago
servicePriceMixin.js
1 year ago
settingsMixin.js
1 year ago
slotsMixin.js
3 years ago
taxesMixin.js
1 year ago
translationMixin.js
4 years ago
slotsMixin.js
248 lines
| 1 | import durationMixin from './durationMixin' |
| 2 | |
| 3 | export default { |
| 4 | |
| 5 | data: () => ({ |
| 6 | }), |
| 7 | |
| 8 | mixins: [ |
| 9 | durationMixin |
| 10 | ], |
| 11 | |
| 12 | methods: { |
| 13 | setPreferredEntitiesData (bookings) { |
| 14 | let employeesIds = this.getAllEntitiesIds(bookings, 0) |
| 15 | |
| 16 | let locationsIds = this.getAllEntitiesIds(bookings, 1) |
| 17 | |
| 18 | let isSingleEmployee = employeesIds.length === 1 |
| 19 | |
| 20 | let isSingleLocation = locationsIds.length === 1 |
| 21 | |
| 22 | bookings.forEach((booking) => { |
| 23 | if (booking.date && booking.time) { |
| 24 | if (!locationsIds.length && isSingleEmployee) { |
| 25 | booking.providerId = employeesIds[0] |
| 26 | |
| 27 | booking.locationId = null |
| 28 | } else if (!locationsIds.length && !isSingleEmployee) { |
| 29 | booking.locationId = null |
| 30 | |
| 31 | for (let i = 0; i < employeesIds.length; i++) { |
| 32 | for (let j = 0; j < booking.slots[booking.time].length; j++) { |
| 33 | if (booking.slots[booking.time][j][0] === employeesIds[i]) { |
| 34 | booking.providerId = employeesIds[i] |
| 35 | |
| 36 | break |
| 37 | } |
| 38 | } |
| 39 | } |
| 40 | } else if (isSingleLocation && isSingleEmployee) { |
| 41 | booking.providerId = employeesIds[0] |
| 42 | |
| 43 | booking.locationId = locationsIds[0] |
| 44 | } else if (!isSingleLocation && isSingleEmployee) { |
| 45 | booking.providerId = employeesIds[0] |
| 46 | |
| 47 | booking.locationId = this.getPreferredEntityId( |
| 48 | booking.slots, |
| 49 | booking.occupied, |
| 50 | booking.time, |
| 51 | booking.providerId, |
| 52 | locationsIds, |
| 53 | 1 |
| 54 | ) |
| 55 | } else if (isSingleLocation && !isSingleEmployee) { |
| 56 | booking.locationId = locationsIds[0] |
| 57 | |
| 58 | booking.providerId = this.getPreferredEntityId( |
| 59 | booking.slots, |
| 60 | booking.occupied, |
| 61 | booking.time, |
| 62 | booking.locationId, |
| 63 | employeesIds, |
| 64 | 0 |
| 65 | ) |
| 66 | } else { |
| 67 | for (let i = 0; i < locationsIds.length; i++) { |
| 68 | for (let j = 0; j < employeesIds.length; j++) { |
| 69 | let isPreferred = this.isPreferredLocationAndEmployee( |
| 70 | booking.slots, |
| 71 | booking.occupied, |
| 72 | booking.time, |
| 73 | locationsIds[i], |
| 74 | employeesIds[j] |
| 75 | ) |
| 76 | |
| 77 | if (isPreferred) { |
| 78 | booking.providerId = employeesIds[j] |
| 79 | |
| 80 | booking.locationId = locationsIds[i] |
| 81 | |
| 82 | return |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | for (let i = 0; i < locationsIds.length; i++) { |
| 88 | for (let j = 0; j < employeesIds.length; j++) { |
| 89 | for (let k = 0; k < booking.slots[booking.time].length; k++) { |
| 90 | if (booking.slots[booking.time][k][0] === employeesIds[j] && |
| 91 | booking.slots[booking.time][k][1] === locationsIds[i] |
| 92 | ) { |
| 93 | booking.providerId = employeesIds[j] |
| 94 | |
| 95 | booking.locationId = locationsIds[i] |
| 96 | |
| 97 | return |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | }) |
| 105 | }, |
| 106 | |
| 107 | getAllEntitiesIds (bookings, index) { |
| 108 | let ids = {} |
| 109 | |
| 110 | for (let i = 0; i < bookings.length; i++) { |
| 111 | if (bookings[i].date && bookings[i].time) { |
| 112 | bookings[i].slots[bookings[i].time].forEach((slotData) => { |
| 113 | if (slotData[index]) { |
| 114 | if (!(slotData[index] in ids)) { |
| 115 | ids[slotData[index]] = 0 |
| 116 | } |
| 117 | |
| 118 | ids[slotData[index]]++ |
| 119 | } |
| 120 | }) |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | let sortedEntitiesIds = [] |
| 125 | |
| 126 | Object.keys(ids).forEach((id) => { |
| 127 | sortedEntitiesIds.push({id: parseInt(id), quantity: ids[id]}) |
| 128 | }) |
| 129 | |
| 130 | sortedEntitiesIds.sort((a, b) => b.quantity - a.quantity) |
| 131 | |
| 132 | return sortedEntitiesIds.map(entity => entity.id) |
| 133 | }, |
| 134 | |
| 135 | getPreferredEntityId (availableSlots, occupiedSlots, timeString, selectedId, allIds, targetIndex) { |
| 136 | let searchIndex = targetIndex ? 0 : 1 |
| 137 | |
| 138 | let appointmentsStarts = {} |
| 139 | |
| 140 | Object.keys(occupiedSlots).forEach((time) => { |
| 141 | occupiedSlots[time].forEach((slotData) => { |
| 142 | if (slotData[searchIndex] === selectedId) { |
| 143 | appointmentsStarts[this.getStringTimeInSeconds(time)] = slotData[targetIndex] |
| 144 | } |
| 145 | }) |
| 146 | }) |
| 147 | |
| 148 | Object.keys(availableSlots).forEach((time) => { |
| 149 | availableSlots[time].forEach((slotData) => { |
| 150 | if (slotData.length >= 3 && slotData[searchIndex] === selectedId) { |
| 151 | appointmentsStarts[this.getStringTimeInSeconds(time)] = slotData[targetIndex] |
| 152 | } |
| 153 | }) |
| 154 | }) |
| 155 | |
| 156 | let availableIds = [] |
| 157 | |
| 158 | availableSlots[timeString].forEach((slotData) => { |
| 159 | if (slotData[searchIndex] === selectedId) { |
| 160 | availableIds.push(slotData[targetIndex]) |
| 161 | } |
| 162 | }) |
| 163 | |
| 164 | if (Object.keys(appointmentsStarts).length) { |
| 165 | let timeInSeconds = this.getStringTimeInSeconds(timeString) |
| 166 | |
| 167 | let closestSlot = Object.keys(appointmentsStarts).reduce((a, b) => { |
| 168 | return Math.abs(b - timeInSeconds) < Math.abs(a - timeInSeconds) ? b : a |
| 169 | }) |
| 170 | |
| 171 | if (availableIds.indexOf(appointmentsStarts[closestSlot]) !== -1) { |
| 172 | return appointmentsStarts[closestSlot] |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | for (let i = 0; i < allIds.length; i++) { |
| 177 | for (let j = 0; j < availableSlots[timeString].length; j++) { |
| 178 | if (availableSlots[timeString][j][searchIndex] === selectedId && |
| 179 | allIds[i] === availableSlots[timeString][j][targetIndex] |
| 180 | ) { |
| 181 | return availableSlots[timeString][j][targetIndex] |
| 182 | } |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | return null |
| 187 | }, |
| 188 | |
| 189 | isPreferredLocationAndEmployee (slotsData, occupiedData, timeString, locationId, employeeId) { |
| 190 | let isEmployeeLocation = false |
| 191 | |
| 192 | slotsData[timeString].forEach((slotData) => { |
| 193 | if (slotData[0] === employeeId && slotData[1] === locationId) { |
| 194 | isEmployeeLocation = true |
| 195 | } |
| 196 | }) |
| 197 | |
| 198 | // inspect if employee is available on proposed location |
| 199 | if (!isEmployeeLocation) { |
| 200 | return false |
| 201 | } |
| 202 | |
| 203 | let appointmentStarts = { |
| 204 | onLocation: {}, |
| 205 | offLocation: {} |
| 206 | } |
| 207 | |
| 208 | Object.keys(occupiedData).forEach((time) => { |
| 209 | occupiedData[time].forEach((slotData) => { |
| 210 | if (slotData[0] === employeeId && slotData[1] === locationId) { |
| 211 | appointmentStarts.onLocation[this.getStringTimeInSeconds(time)] = slotData[1] |
| 212 | } else if (slotData[0] === employeeId) { |
| 213 | appointmentStarts.offLocation[this.getStringTimeInSeconds(time)] = slotData[1] |
| 214 | } |
| 215 | }) |
| 216 | }) |
| 217 | |
| 218 | Object.keys(slotsData).forEach((time) => { |
| 219 | slotsData[time].forEach((slotData) => { |
| 220 | if (slotData.length >= 3 && slotData[0] === employeeId && slotData[1] === locationId) { |
| 221 | appointmentStarts.onLocation[this.getStringTimeInSeconds(time)] = slotData[1] |
| 222 | } else if (slotData.length >= 3 && slotData[0] === employeeId) { |
| 223 | appointmentStarts.offLocation[this.getStringTimeInSeconds(time)] = slotData[1] |
| 224 | } |
| 225 | }) |
| 226 | }) |
| 227 | |
| 228 | // inspect if employee has appointments only on proposed location, or has no appointments in that day |
| 229 | if ( |
| 230 | (!Object.keys(appointmentStarts.onLocation).length && !Object.keys(appointmentStarts.offLocation).length) || |
| 231 | (Object.keys(appointmentStarts.onLocation).length && !Object.keys(appointmentStarts.offLocation).length) |
| 232 | ) { |
| 233 | return true |
| 234 | } |
| 235 | |
| 236 | let timeInSeconds = this.getStringTimeInSeconds(timeString) |
| 237 | |
| 238 | appointmentStarts = Object.assign(appointmentStarts.onLocation, appointmentStarts.offLocation) |
| 239 | |
| 240 | let closestTime = Object.keys(appointmentStarts).reduce((a, b) => { |
| 241 | return Math.abs(b - timeInSeconds) < Math.abs(a - timeInSeconds) ? b : a |
| 242 | }) |
| 243 | |
| 244 | return locationId === appointmentStarts[closestTime] |
| 245 | } |
| 246 | } |
| 247 | } |
| 248 |