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
recurring.js
238 lines
| 1 | import moment from "moment"; |
| 2 | import {useCartItem} from "../public/cart"; |
| 3 | import {useSortedDateStrings} from "./helper"; |
| 4 | |
| 5 | function getFutureMonthDate (dateString, interval) { |
| 6 | let date = moment(dateString, 'YYYY-MM-DD') |
| 7 | let futureMonthDate = moment(date).add(interval, 'M') |
| 8 | let futureMonthDateEnd = moment(futureMonthDate).startOf('month') |
| 9 | |
| 10 | if (date.date() !== futureMonthDate.date() && |
| 11 | futureMonthDate.isSame(futureMonthDateEnd.format('YYYY-MM-DD')) |
| 12 | ) { |
| 13 | futureMonthDate.subtract(1, 'd') |
| 14 | } |
| 15 | |
| 16 | return futureMonthDate |
| 17 | } |
| 18 | |
| 19 | function getGivenDateOfMonth (date, monthWeek, weekDay) { |
| 20 | let firstWeek = moment(date, 'YYYY-MM-DD').startOf('month').day(weekDay) |
| 21 | |
| 22 | if (firstWeek.format('M') !== date.format('M')) { |
| 23 | firstWeek.add(7, 'days') |
| 24 | } |
| 25 | |
| 26 | let resultDate = firstWeek.add(7 * (monthWeek - 1), 'days') |
| 27 | |
| 28 | if (date.format('M') !== resultDate.format('M')) { |
| 29 | resultDate.subtract(7, 'days') |
| 30 | } |
| 31 | |
| 32 | return resultDate |
| 33 | } |
| 34 | |
| 35 | function useExpectedDates (lastAvailableDate, startDate, endDate, maxDays, type, interval, weekDays, monthRule) { |
| 36 | let lastDate = endDate ? moment(endDate) : moment().add(maxDays, 'days') |
| 37 | |
| 38 | let selectedDate = moment(startDate, 'YYYY-MM-DD') |
| 39 | |
| 40 | let expectedDates = {} |
| 41 | |
| 42 | let expectedDatesArray = [] |
| 43 | |
| 44 | switch (type) { |
| 45 | case ('daily'): |
| 46 | expectedDatesArray.push(moment(startDate, 'YYYY-MM-DD')) |
| 47 | |
| 48 | break |
| 49 | |
| 50 | case ('weekly'): |
| 51 | let selectedDateIndex = moment(startDate, 'YYYY-MM-DD').isoWeekday() |
| 52 | |
| 53 | weekDays.forEach((item, index) => { |
| 54 | if (item.selected) { |
| 55 | let dayIndex = index + 1 |
| 56 | |
| 57 | let date = moment(startDate, 'YYYY-MM-DD') |
| 58 | |
| 59 | if (selectedDateIndex < dayIndex) { |
| 60 | expectedDatesArray.push(date.add(dayIndex - selectedDateIndex, 'days')) |
| 61 | } else if (selectedDateIndex === dayIndex) { |
| 62 | expectedDatesArray.push(date) |
| 63 | } else if (selectedDateIndex > dayIndex) { |
| 64 | expectedDatesArray.push(date.subtract(selectedDateIndex - dayIndex, 'days')) |
| 65 | } |
| 66 | |
| 67 | let lDate = lastDate ? lastDate : lastAvailableDate |
| 68 | if (date.isSameOrBefore(lDate) && date.isAfter(selectedDate)) { |
| 69 | expectedDates[date.format('YYYY-MM-DD')] = date |
| 70 | } |
| 71 | } |
| 72 | }) |
| 73 | |
| 74 | break |
| 75 | |
| 76 | case ('monthly'): |
| 77 | expectedDatesArray.push(moment(startDate, 'YYYY-MM-DD')) |
| 78 | |
| 79 | break |
| 80 | } |
| 81 | |
| 82 | let isValidExpected = expectedDatesArray.length > 0 |
| 83 | |
| 84 | while (isValidExpected) { |
| 85 | for (let i = 0; i < expectedDatesArray.length; i++) { |
| 86 | switch (type) { |
| 87 | case ('daily'): |
| 88 | expectedDatesArray[i].add(interval, 'days') |
| 89 | |
| 90 | break |
| 91 | |
| 92 | case ('weekly'): |
| 93 | expectedDatesArray[i].add(interval * 7, 'days') |
| 94 | |
| 95 | break |
| 96 | |
| 97 | case ('monthly'): |
| 98 | let weekDayIndex = expectedDatesArray[i].isoWeekday() |
| 99 | expectedDatesArray[i] = monthRule === 0 ? |
| 100 | getFutureMonthDate( |
| 101 | expectedDatesArray[i].format('YYYY-MM-DD'), |
| 102 | interval |
| 103 | ) : |
| 104 | getGivenDateOfMonth( |
| 105 | getFutureMonthDate( |
| 106 | expectedDatesArray[i].startOf('month').format('YYYY-MM-DD'), |
| 107 | interval |
| 108 | ), |
| 109 | monthRule, |
| 110 | weekDayIndex |
| 111 | ) |
| 112 | |
| 113 | break |
| 114 | } |
| 115 | |
| 116 | isValidExpected = expectedDatesArray[i].isSameOrBefore(lastDate) |
| 117 | |
| 118 | if (isValidExpected) { |
| 119 | expectedDates[moment(expectedDatesArray[i]).format('YYYY-MM-DD')] = moment(expectedDatesArray[i]) |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | return expectedDates |
| 125 | } |
| 126 | |
| 127 | function useProposedDates (service, slots, expectedDates, startDate, startTime, endDate, count) { |
| 128 | let recurringStartDate = moment(startDate, 'YYYY-MM-DD') |
| 129 | let recurringEndDate = endDate ? moment(endDate, 'YYYY-MM-DD') : null |
| 130 | |
| 131 | let proposedDates = {} |
| 132 | let availableDates = {} |
| 133 | |
| 134 | useSortedDateStrings(Object.keys(slots)).forEach((dateString) => { |
| 135 | availableDates[dateString] = moment(dateString + ' 00:00:00') |
| 136 | }) |
| 137 | |
| 138 | let bookingStartString = moment(startDate, 'YYYY-MM-DD').format('YYYY-MM-DD') |
| 139 | |
| 140 | for (let expectedDateString in expectedDates) { |
| 141 | let expectedDate = moment(expectedDateString, 'YYYY-MM-DD') |
| 142 | |
| 143 | let isBefore = recurringEndDate ? expectedDate.isSameOrBefore(recurringEndDate) : true |
| 144 | |
| 145 | if (expectedDateString in slots && |
| 146 | Object.keys(slots[expectedDateString]).length && |
| 147 | !(expectedDateString in proposedDates) && |
| 148 | isBefore |
| 149 | ) { |
| 150 | let isAvailableTime = startTime in slots[expectedDateString] |
| 151 | |
| 152 | let expectedTime = isAvailableTime ? startTime : Object.keys(slots[expectedDateString])[0] |
| 153 | |
| 154 | proposedDates[expectedDateString] = { |
| 155 | isSubstituteDate: false, |
| 156 | isSubstituteTime: !isAvailableTime, |
| 157 | time: expectedTime, |
| 158 | times: Object.keys(slots[expectedDateString]), |
| 159 | slot: slots[expectedDateString][expectedTime], |
| 160 | } |
| 161 | } else if (expectedDateString !== bookingStartString && !(expectedDateString in proposedDates) && isBefore) { |
| 162 | let previousDate = null |
| 163 | let nextDate = null |
| 164 | |
| 165 | for (let availableDateString in availableDates) { |
| 166 | if ((service.recurringSub === 'past' || service.recurringSub === 'both') && |
| 167 | availableDates[availableDateString].isBefore(expectedDates[expectedDateString]) && |
| 168 | availableDates[availableDateString].isAfter(recurringStartDate) && |
| 169 | availableDateString !== expectedDateString && |
| 170 | !(availableDateString in proposedDates) |
| 171 | ) { |
| 172 | previousDate = availableDates[availableDateString] |
| 173 | } |
| 174 | |
| 175 | if ((service.recurringSub === 'future' || service.recurringSub === 'both') && |
| 176 | availableDates[availableDateString].isAfter(expectedDates[expectedDateString]) && |
| 177 | availableDateString !== expectedDateString && |
| 178 | !(availableDateString in proposedDates) |
| 179 | ) { |
| 180 | nextDate = availableDates[availableDateString] |
| 181 | |
| 182 | break |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | let previousDateTimeDiff = previousDate !== null ? |
| 187 | moment(expectedDates[expectedDateString]).diff(previousDate, 'days') : null |
| 188 | |
| 189 | let nextDateTimeDiff = nextDate !== null ? |
| 190 | moment(nextDate).diff(expectedDates[expectedDateString], 'days') : null |
| 191 | |
| 192 | let substituteDate = null |
| 193 | |
| 194 | if (previousDateTimeDiff && nextDateTimeDiff) { |
| 195 | substituteDate = previousDateTimeDiff < nextDateTimeDiff ? previousDate : nextDate |
| 196 | } else if (previousDateTimeDiff) { |
| 197 | substituteDate = previousDate |
| 198 | } else if (nextDateTimeDiff) { |
| 199 | substituteDate = nextDate |
| 200 | } |
| 201 | |
| 202 | if (substituteDate !== null && |
| 203 | (recurringEndDate ? moment(substituteDate).isSameOrBefore(recurringEndDate) : true) |
| 204 | ) { |
| 205 | let substituteDateString = moment(substituteDate).format('YYYY-MM-DD') |
| 206 | |
| 207 | if (substituteDateString in slots && |
| 208 | !(expectedDateString in proposedDates) && |
| 209 | !(substituteDateString in expectedDates) |
| 210 | ) { |
| 211 | let isAvailableTime = startTime in slots[substituteDateString] |
| 212 | |
| 213 | let substituteTime = isAvailableTime ? startTime : Object.keys(slots[substituteDateString])[0] |
| 214 | |
| 215 | proposedDates[substituteDateString] = { |
| 216 | isSubstituteDate: true, |
| 217 | isSubstituteTime: !isAvailableTime, |
| 218 | time: substituteTime, |
| 219 | times: Object.keys(slots[substituteDateString]), |
| 220 | slot: slots[substituteDateString][substituteTime], |
| 221 | } |
| 222 | } |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | if (recurringEndDate ? !isBefore : useSortedDateStrings(Object.keys(proposedDates)).length >= count) { |
| 227 | break |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | return proposedDates |
| 232 | } |
| 233 | |
| 234 | export { |
| 235 | useProposedDates, |
| 236 | useExpectedDates, |
| 237 | } |
| 238 |