import { sumPackages } from "@scandic-hotels/booking-flow/utils/SelectRate" import { CurrencyEnum } from "@scandic-hotels/common/constants/currency" import { dt } from "@scandic-hotels/common/dt" import { PriceTypeEnum } from "@/types/components/hotelReservation/myStay/myStay" import type { Price } from "@/types/components/hotelReservation/price" import type { Room } from "@/types/stores/my-stay" export function mapToPrice(room: Room) { switch (room.priceType) { case PriceTypeEnum.cheque: return { corporateCheque: { additionalPricePerStay: room.roomPrice.perStay.local.price, currency: room.roomPrice.perStay.local.currency, numberOfCheques: room.cheques, }, } case PriceTypeEnum.money: return { regular: { currency: room.currencyCode, pricePerNight: room.roomPrice.perNight.local.price, pricePerStay: room.roomPrice.perStay.local.price, regularPricePerStay: room.roomPrice.perStay.local.regularPrice || room.roomPrice.perStay.local.price, }, } case PriceTypeEnum.points: const nights = dt(room.checkOutDate) .startOf("day") .diff(dt(room.checkInDate).startOf("day"), "days") return { redemption: { additionalPricePerStay: room.roomPrice.perStay.local.price, currency: CurrencyEnum.POINTS, pointsPerNight: room.roomPoints / nights, pointsPerStay: room.roomPoints, pointsType: room.roomPointType, }, } case PriceTypeEnum.voucher: return { voucher: { numberOfVouchers: room.vouchers, }, } default: throw new Error(`Unknown payment method!`) } } export function calculateTotalPrice(rooms: Room[], currency: CurrencyEnum) { return rooms.reduce( (total, room) => { switch (room.priceType) { case PriceTypeEnum.cheque: { total.local.currency = CurrencyEnum.CC total.local.price = total.local.price + room.cheques } break case PriceTypeEnum.money: { total.local.price = total.local.price + room.totalPrice if (!total.local.currency) { total.local.currency = room.currencyCode } } break case PriceTypeEnum.points: { total.local.currency = CurrencyEnum.POINTS total.local.price = total.local.price + room.totalPoints } break case PriceTypeEnum.voucher: total.local.currency = CurrencyEnum.Voucher total.local.price = total.local.price + room.vouchers break } switch (room.priceType) { case PriceTypeEnum.cheque: case PriceTypeEnum.points: { if ( room.roomPoints && room.roomPointType && room.roomPointType !== "Scandic" ) { total.local.currency = CurrencyEnum.POINTS total.local.price = total.local.price + room.roomPoints total.local.pointsType = room.roomPointType break } if (room.totalPrice) { total.local.additionalPrice = (total.local.additionalPrice || 0) + room.totalPrice } if (!total.local.additionalPriceCurrency) { total.local.additionalPriceCurrency = currency } } break case PriceTypeEnum.voucher: { if (room.packages) { const pkgsSum = sumPackages(room.packages) total.local.additionalPrice = (total.local.additionalPrice || 0) + pkgsSum.price if (pkgsSum.currency) { total.local.additionalPriceCurrency = pkgsSum.currency } else { total.local.additionalPriceCurrency = currency } } } break case PriceTypeEnum.money: { if (room.totalPoints) { total.local.additionalPrice = (total.local.additionalPrice || 0) + room.totalPoints } if (!total.local.additionalPriceCurrency) { total.local.additionalPriceCurrency = CurrencyEnum.POINTS } } break } return total }, { local: { currency, price: 0, pointsType: undefined, }, requested: undefined, } ) }