actions.js
5 days ago
booking.js
5 days ago
cabinet.js
5 days ago
cart.js
5 days ago
catalog.js
5 days ago
coupon.js
5 days ago
customFields.js
5 days ago
events.js
5 days ago
facebookPixel.js
5 days ago
ivy.js
5 days ago
package.js
5 days ago
panel.js
5 days ago
public.js
5 days ago
renderActions.js
5 days ago
restore.js
5 days ago
slots.js
5 days ago
translation.js
5 days ago
user.js
5 days ago
package.js
202 lines
| 1 | import { useCartItem } from './cart' |
| 2 | import { usePercentageAmount } from '../common/pricing' |
| 3 | |
| 4 | function useBuildPackage(index, entity) { |
| 5 | let items = [ |
| 6 | { |
| 7 | packageId: entity.id, |
| 8 | serviceId: null, |
| 9 | index: null, |
| 10 | services: {}, |
| 11 | }, |
| 12 | ] |
| 13 | |
| 14 | entity.bookable.forEach((item, i) => { |
| 15 | if (i === 0) { |
| 16 | items[index].serviceId = item.service.id |
| 17 | } |
| 18 | |
| 19 | items[index].index = 0 |
| 20 | |
| 21 | items[index].services[item.service.id] = { |
| 22 | fetched: false, |
| 23 | slots: [], |
| 24 | providerId: null, |
| 25 | locationId: null, |
| 26 | serviceId: item.service.id, |
| 27 | quantity: item.quantity, |
| 28 | list: [...new Array(item.minimumScheduled)].map((el, index) => ({ |
| 29 | id: index + ' ' + item.service.id, |
| 30 | providerId: null, |
| 31 | locationId: null, |
| 32 | date: null, |
| 33 | time: null, |
| 34 | persons: 1, |
| 35 | extras: [], |
| 36 | duration: null, |
| 37 | })), |
| 38 | } |
| 39 | }) |
| 40 | |
| 41 | return items |
| 42 | } |
| 43 | |
| 44 | function useFilteredProviders(store) { |
| 45 | let relations = store.getters['entities/getEntitiesRelations'] |
| 46 | |
| 47 | let cartItem = useCartItem(store) |
| 48 | |
| 49 | let serviceId = cartItem.serviceId |
| 50 | |
| 51 | let activeService = cartItem.services[serviceId] |
| 52 | |
| 53 | let packageService = store.getters['entities/getPackage']( |
| 54 | store.getters['booking/getPackageId'], |
| 55 | ).bookable.find((i) => parseInt(i.service.id) === parseInt(serviceId)) |
| 56 | |
| 57 | let preselectedEmployees = store.getters['entities/getPreselected'].employee |
| 58 | |
| 59 | let employees = preselectedEmployees.length |
| 60 | ? store.getters['entities/getUnfilteredEmployees'].filter((a) => |
| 61 | preselectedEmployees.map((id) => parseInt(id)).includes(a.id), |
| 62 | ) |
| 63 | : store.getters['entities/getUnfilteredEmployees'] |
| 64 | |
| 65 | let employeesIds = [] |
| 66 | |
| 67 | if (activeService.locationId) { |
| 68 | for (let employeeId in relations) { |
| 69 | if ( |
| 70 | serviceId in relations[employeeId] && |
| 71 | relations[employeeId][serviceId].indexOf(activeService.locationId) !== -1 |
| 72 | ) { |
| 73 | employeesIds.push(parseInt(employeeId)) |
| 74 | } |
| 75 | } |
| 76 | } else { |
| 77 | let locationsIds = [] |
| 78 | |
| 79 | if (packageService.locations.length) { |
| 80 | locationsIds = packageService.locations.map((item) => item.id) |
| 81 | } else { |
| 82 | for (let employeeId in relations) { |
| 83 | if (serviceId in relations[employeeId]) { |
| 84 | locationsIds = locationsIds.concat(relations[employeeId][serviceId]) |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | for (let employeeId in relations) { |
| 90 | if (serviceId in relations[employeeId]) { |
| 91 | locationsIds.forEach((locationId) => { |
| 92 | if (relations[employeeId][serviceId].indexOf(locationId) !== -1) { |
| 93 | employeesIds.push(parseInt(employeeId)) |
| 94 | } |
| 95 | }) |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | let availableEmployeesIds = packageService.providers.length |
| 101 | ? packageService.providers.map((item) => item.id) |
| 102 | : employees.map((item) => item.id) |
| 103 | |
| 104 | return employees.filter( |
| 105 | (item) => |
| 106 | availableEmployeesIds.indexOf(item.id) !== -1 && |
| 107 | employeesIds.indexOf(item.id) !== -1 && |
| 108 | item.status === 'visible', |
| 109 | ) |
| 110 | } |
| 111 | |
| 112 | function useFilteredLocations(store) { |
| 113 | let cartItem = useCartItem(store) |
| 114 | |
| 115 | let relations = store.getters['entities/getEntitiesRelations'] |
| 116 | |
| 117 | let serviceId = cartItem.serviceId |
| 118 | |
| 119 | let activeService = cartItem.services[serviceId] |
| 120 | |
| 121 | let packageService = store.getters['entities/getPackage']( |
| 122 | store.getters['booking/getPackageId'], |
| 123 | ).bookable.find((i) => parseInt(i.service.id) === parseInt(serviceId)) |
| 124 | |
| 125 | let preselectedLocations = store.getters['entities/getPreselected'].location |
| 126 | |
| 127 | let locations = preselectedLocations.length |
| 128 | ? store.getters['entities/getUnfilteredLocations'].filter((a) => |
| 129 | preselectedLocations.map((id) => parseInt(id)).includes(a.id), |
| 130 | ) |
| 131 | : store.getters['entities/getUnfilteredLocations'] |
| 132 | |
| 133 | let locationsIds = [] |
| 134 | |
| 135 | if (activeService.providerId) { |
| 136 | locationsIds = relations[activeService.providerId][serviceId] |
| 137 | } else { |
| 138 | let employeesIds = [] |
| 139 | |
| 140 | if (packageService.providers.length) { |
| 141 | employeesIds = packageService.providers.map((item) => item.id) |
| 142 | } else { |
| 143 | for (let employeeId in relations) { |
| 144 | if (serviceId in relations[employeeId]) { |
| 145 | employeesIds.push(parseInt(employeeId)) |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | for (let employeeId in relations) { |
| 151 | if (serviceId in relations[employeeId] && employeesIds.indexOf(parseInt(employeeId)) !== -1) { |
| 152 | locationsIds = locationsIds.concat(relations[employeeId][serviceId]) |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | let availableLocationsIds = packageService.locations.length |
| 158 | ? packageService.locations.map((item) => item.id) |
| 159 | : locations.map((item) => item.id) |
| 160 | |
| 161 | return locations.filter( |
| 162 | (item) => |
| 163 | availableLocationsIds.indexOf(item.id) !== -1 && |
| 164 | locationsIds.indexOf(item.id) !== -1 && |
| 165 | item.status === 'visible', |
| 166 | ) |
| 167 | } |
| 168 | |
| 169 | function usePackageAmount(pack) { |
| 170 | return pack.discount && !pack.calculatedPrice |
| 171 | ? pack.price - usePercentageAmount(pack.price, pack.discount) |
| 172 | : pack.price |
| 173 | } |
| 174 | |
| 175 | function usePackageDiscountAmount(pack, coupon) { |
| 176 | return usePercentageAmount(usePackageAmount(pack), coupon.discount) + coupon.deduction |
| 177 | } |
| 178 | |
| 179 | function useCalculateDiscount(pack) { |
| 180 | if (pack.discount && !pack.calculatedPrice) { |
| 181 | return pack.discount |
| 182 | } |
| 183 | if (pack.price) { |
| 184 | let originalPrice = pack.bookable.reduce( |
| 185 | (partialSum, a) => partialSum + a.service.price * a.quantity, |
| 186 | 0, |
| 187 | ) |
| 188 | let newPrice = pack.price |
| 189 | return originalPrice > newPrice ? ((originalPrice - newPrice) / originalPrice) * 100 : '' |
| 190 | } |
| 191 | return 0 |
| 192 | } |
| 193 | |
| 194 | export { |
| 195 | useBuildPackage, |
| 196 | useFilteredProviders, |
| 197 | useFilteredLocations, |
| 198 | usePackageAmount, |
| 199 | useCalculateDiscount, |
| 200 | usePackageDiscountAmount, |
| 201 | } |
| 202 |