fix: make sure calculations in booking flow are correct
This commit is contained in:
committed by
Michael Zetterberg
parent
3e0f503314
commit
a222ecfc5c
@@ -430,7 +430,7 @@ export function calcTotalPrice(
|
||||
? (room.breakfast.localPrice?.price ?? 0)
|
||||
: 0
|
||||
|
||||
const roomFeaturesTotal = room.roomFeatures?.reduce(
|
||||
const roomFeaturesTotal = (room.roomFeatures || []).reduce(
|
||||
(total, pkg) => {
|
||||
if (pkg.requestedPrice.totalPrice) {
|
||||
total.requestedPrice = add(
|
||||
@@ -445,45 +445,72 @@ export function calcTotalPrice(
|
||||
{ local: 0, requestedPrice: 0 }
|
||||
)
|
||||
|
||||
const result: Price = {
|
||||
requested: roomPrice.perStay.requested
|
||||
? {
|
||||
currency: roomPrice.perStay.requested.currency,
|
||||
price: add(
|
||||
acc.requested?.price ?? 0,
|
||||
roomPrice.perStay.requested.price,
|
||||
breakfastRequestedPrice * room.adults * nights
|
||||
),
|
||||
}
|
||||
: undefined,
|
||||
local: {
|
||||
currency: roomPrice.perStay.local.currency,
|
||||
price: add(
|
||||
acc.local.price,
|
||||
roomPrice.perStay.local.price,
|
||||
breakfastLocalPrice * room.adults * nights,
|
||||
roomFeaturesTotal?.local ?? 0
|
||||
),
|
||||
regularPrice: add(
|
||||
acc.local.regularPrice,
|
||||
roomPrice.perStay.local.regularPrice,
|
||||
breakfastLocalPrice * room.adults * nights,
|
||||
roomFeaturesTotal?.requestedPrice ?? 0
|
||||
),
|
||||
additionalPrice: add(
|
||||
acc.local.additionalPrice,
|
||||
roomPrice.perStay.local.additionalPrice,
|
||||
breakfastLocalPrice * room.adults * nights,
|
||||
roomFeaturesTotal?.local ?? 0
|
||||
),
|
||||
additionalPriceCurrency: roomPrice.perStay.local
|
||||
.additionalPriceCurrency
|
||||
? roomPrice.perStay.local.additionalPriceCurrency
|
||||
: undefined,
|
||||
},
|
||||
if (roomPrice.perStay.requested) {
|
||||
if (!acc.requested) {
|
||||
acc.requested = {
|
||||
currency: roomPrice.perStay.requested.currency,
|
||||
price: 0,
|
||||
}
|
||||
}
|
||||
|
||||
acc.requested.price = add(
|
||||
acc.requested.price,
|
||||
roomPrice.perStay.requested.price,
|
||||
breakfastRequestedPrice * room.adults * nights
|
||||
)
|
||||
|
||||
// TODO: Come back and verify on CC, PTS, Voucher
|
||||
if (roomPrice.perStay.requested.additionalPrice) {
|
||||
acc.requested.additionalPrice = add(
|
||||
acc.requested.additionalPrice,
|
||||
roomPrice.perStay.requested.additionalPrice
|
||||
)
|
||||
}
|
||||
|
||||
if (
|
||||
roomPrice.perStay.requested.additionalPriceCurrency &&
|
||||
!acc.requested.additionalPriceCurrency
|
||||
) {
|
||||
acc.requested.additionalPriceCurrency =
|
||||
roomPrice.perStay.requested.additionalPriceCurrency
|
||||
}
|
||||
}
|
||||
|
||||
return result
|
||||
const breakfastLocalTotalPrice =
|
||||
breakfastLocalPrice * room.adults * nights
|
||||
|
||||
acc.local.price = add(
|
||||
acc.local.price,
|
||||
roomPrice.perStay.local.price,
|
||||
breakfastLocalTotalPrice,
|
||||
roomFeaturesTotal.local
|
||||
)
|
||||
|
||||
if (roomPrice.perStay.local.regularPrice) {
|
||||
acc.local.regularPrice = add(
|
||||
acc.local.regularPrice,
|
||||
roomPrice.perStay.local.regularPrice,
|
||||
breakfastLocalTotalPrice,
|
||||
roomFeaturesTotal.local
|
||||
)
|
||||
}
|
||||
|
||||
if (roomPrice.perStay.local.additionalPrice) {
|
||||
acc.local.additionalPrice = add(
|
||||
acc.local.additionalPrice,
|
||||
roomPrice.perStay.local.additionalPrice
|
||||
)
|
||||
}
|
||||
|
||||
if (
|
||||
roomPrice.perStay.local.additionalPriceCurrency &&
|
||||
!acc.local.additionalPriceCurrency
|
||||
) {
|
||||
acc.local.additionalPriceCurrency =
|
||||
roomPrice.perStay.local.additionalPriceCurrency
|
||||
}
|
||||
|
||||
return acc
|
||||
},
|
||||
{
|
||||
requested: undefined,
|
||||
|
||||
@@ -168,100 +168,19 @@ export function createDetailsStore(
|
||||
currentRoom.steps[StepEnum.breakfast].isValid = true
|
||||
}
|
||||
|
||||
const currentTotalPriceRequested = state.totalPrice.requested
|
||||
let stateTotalRequestedPrice = 0
|
||||
if (currentTotalPriceRequested) {
|
||||
stateTotalRequestedPrice =
|
||||
currentTotalPriceRequested.price ?? 0
|
||||
}
|
||||
|
||||
const stateTotalLocalPrice = state.totalPrice.local.price
|
||||
const stateTotalLocalRegularPrice =
|
||||
state.totalPrice.local.regularPrice
|
||||
|
||||
const addToTotalPrice =
|
||||
(currentRoom.room.breakfast === undefined ||
|
||||
currentRoom.room.breakfast === false) &&
|
||||
!!breakfast
|
||||
|
||||
const subtractFromTotalPrice =
|
||||
currentRoom.room.breakfast && breakfast === false
|
||||
currentRoom.room.breakfast = breakfast
|
||||
|
||||
const nights = dt(state.booking.toDate).diff(
|
||||
state.booking.fromDate,
|
||||
"days"
|
||||
)
|
||||
|
||||
if (addToTotalPrice) {
|
||||
const breakfastTotalRequestedPrice =
|
||||
breakfast.requestedPrice.price *
|
||||
currentRoom.room.adults *
|
||||
nights
|
||||
const breakfastTotalPrice =
|
||||
breakfast.localPrice.price *
|
||||
currentRoom.room.adults *
|
||||
nights
|
||||
state.totalPrice = {
|
||||
requested: state.totalPrice.requested && {
|
||||
currency: state.totalPrice.requested.currency,
|
||||
price:
|
||||
stateTotalRequestedPrice + breakfastTotalRequestedPrice,
|
||||
},
|
||||
local: {
|
||||
currency: breakfast.localPrice.currency,
|
||||
price: stateTotalLocalPrice ?? 0 + breakfastTotalPrice,
|
||||
regularPrice: stateTotalLocalRegularPrice
|
||||
? stateTotalLocalRegularPrice + breakfastTotalPrice
|
||||
: undefined,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
if (subtractFromTotalPrice) {
|
||||
let currency = state.totalPrice.local.currency
|
||||
let currentBreakfastTotalPrice = 0
|
||||
let currentBreakfastTotalRequestedPrice = 0
|
||||
if (currentRoom.room.breakfast) {
|
||||
currentBreakfastTotalPrice =
|
||||
currentRoom.room.breakfast.localPrice.price *
|
||||
currentRoom.room.adults *
|
||||
nights
|
||||
currentBreakfastTotalRequestedPrice =
|
||||
currentRoom.room.breakfast.requestedPrice.totalPrice *
|
||||
currentRoom.room.adults *
|
||||
nights
|
||||
currency = currentRoom.room.breakfast.localPrice.currency
|
||||
}
|
||||
|
||||
let requestedPrice =
|
||||
stateTotalRequestedPrice -
|
||||
currentBreakfastTotalRequestedPrice
|
||||
if (requestedPrice < 0) {
|
||||
requestedPrice = 0
|
||||
}
|
||||
let localPrice =
|
||||
stateTotalLocalPrice - currentBreakfastTotalPrice
|
||||
if (localPrice < 0) {
|
||||
localPrice = 0
|
||||
}
|
||||
let regularPrice = stateTotalLocalRegularPrice
|
||||
? stateTotalLocalRegularPrice - currentBreakfastTotalPrice
|
||||
: undefined
|
||||
|
||||
state.totalPrice = {
|
||||
requested: state.totalPrice.requested && {
|
||||
currency: state.totalPrice.requested.currency,
|
||||
price: requestedPrice,
|
||||
},
|
||||
local: {
|
||||
currency,
|
||||
price: localPrice,
|
||||
regularPrice,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
currentRoom.room.breakfast = breakfast
|
||||
state.totalPrice = calcTotalPrice(
|
||||
state.rooms,
|
||||
currentRoom.room.roomPrice.perStay.local.currency,
|
||||
isMember,
|
||||
nights
|
||||
)
|
||||
|
||||
const isAllStepsCompleted = checkRoomProgress(
|
||||
state.rooms[idx].steps
|
||||
|
||||
Reference in New Issue
Block a user