150 lines
4.3 KiB
TypeScript
150 lines
4.3 KiB
TypeScript
"use client"
|
|
import { CurrencyEnum } from "@scandic-hotels/common/constants/currency"
|
|
import { dt } from "@scandic-hotels/common/dt"
|
|
|
|
import PriceDetailsModal from "../../../components/PriceDetailsModal"
|
|
import { useBookingConfirmationStore } from "../../../stores/booking-confirmation"
|
|
import { mapToPrice } from "./mapToPrice"
|
|
|
|
import type { Price } from "../../../types/price"
|
|
|
|
export default function PriceDetails() {
|
|
const {
|
|
bookingCode,
|
|
currency,
|
|
hotelOffersBreakfast,
|
|
fromDate,
|
|
rooms,
|
|
vat,
|
|
toDate,
|
|
} = useBookingConfirmationStore((state) => ({
|
|
bookingCode: state.bookingCode ?? undefined,
|
|
currency: state.currencyCode,
|
|
hotelOffersBreakfast: state.hotelOffersBreakfast,
|
|
fromDate: state.fromDate,
|
|
rooms: state.rooms,
|
|
toDate: state.toDate,
|
|
vat: state.vat,
|
|
}))
|
|
|
|
if (!rooms[0]) {
|
|
return null
|
|
}
|
|
|
|
const checkInDate = dt(fromDate).format("YYYY-MM-DD")
|
|
const checkOutDate = dt(toDate).format("YYYY-MM-DD")
|
|
const nights = dt(toDate)
|
|
.startOf("day")
|
|
.diff(dt(fromDate).startOf("day"), "days")
|
|
|
|
const totalPrice = rooms.reduce<Price>(
|
|
(total, room) => {
|
|
if (!room) {
|
|
return total
|
|
}
|
|
|
|
if (room.cheques) {
|
|
// CorporateCheque Booking
|
|
total.local.currency = CurrencyEnum.CC
|
|
total.local.price = total.local.price + room.cheques
|
|
} else if (room.roomPoints) {
|
|
// Redemption Booking
|
|
total.local.currency = CurrencyEnum.POINTS
|
|
total.local.price = total.local.price + room.roomPoints
|
|
} else if (room.vouchers) {
|
|
// Vouchers Booking
|
|
total.local.currency = CurrencyEnum.Voucher
|
|
total.local.price = total.local.price + room.vouchers
|
|
} else {
|
|
// Price Booking
|
|
total.local.price = total.local.price + room.totalPrice
|
|
}
|
|
|
|
// Corporate Cheque
|
|
if (room.cheques) {
|
|
if (room.roomPrice) {
|
|
total.local.additionalPrice =
|
|
(total.local.additionalPrice || 0) + room.totalPrice
|
|
total.local.additionalPriceCurrency = currency
|
|
} else {
|
|
const pkgsSum = room.packages.reduce(
|
|
(total, pkg) => total + pkg.totalPrice,
|
|
0
|
|
)
|
|
total.local.additionalPrice =
|
|
(total.local.additionalPrice || 0) + pkgsSum
|
|
const pkgsCurrency = room.packages.find(
|
|
(pkg) => pkg.currency
|
|
)?.currency
|
|
if (!total.local.additionalPriceCurrency) {
|
|
total.local.additionalPriceCurrency = pkgsCurrency ?? currency
|
|
}
|
|
}
|
|
}
|
|
|
|
// Redemption
|
|
if (room.roomPoints) {
|
|
if (room.roomPrice) {
|
|
total.local.additionalPrice =
|
|
(total.local.additionalPrice || 0) + room.totalPrice
|
|
total.local.additionalPriceCurrency = currency
|
|
} else {
|
|
const pkgsSum = room.packages.reduce(
|
|
(total, pkg) => total + pkg.totalPrice,
|
|
0
|
|
)
|
|
total.local.additionalPrice =
|
|
(total.local.additionalPrice || 0) + pkgsSum
|
|
const pkgsCurrency = room.packages.find(
|
|
(pkg) => pkg.currency
|
|
)?.currency
|
|
if (!total.local.additionalPriceCurrency) {
|
|
total.local.additionalPriceCurrency = pkgsCurrency ?? currency
|
|
}
|
|
}
|
|
}
|
|
|
|
// Voucher
|
|
if (room.vouchers && room.packages) {
|
|
const pkgsSum = room.packages.reduce(
|
|
(total, pkg) => total + pkg.totalPrice,
|
|
0
|
|
)
|
|
total.local.additionalPrice =
|
|
(total.local.additionalPrice || 0) + pkgsSum
|
|
const pkgsCurrency = room.packages.find((pkg) => pkg.currency)?.currency
|
|
if (!total.local.additionalPriceCurrency) {
|
|
total.local.additionalPriceCurrency = pkgsCurrency ?? currency
|
|
}
|
|
}
|
|
|
|
return total
|
|
},
|
|
{
|
|
local: {
|
|
currency,
|
|
price: 0,
|
|
},
|
|
requested: undefined,
|
|
}
|
|
)
|
|
const mappedRooms = mapToPrice(rooms, nights)
|
|
const isCampaignRate = rooms.every(
|
|
(room) => room?.rateDefinition.isCampaignRate
|
|
)
|
|
|
|
return (
|
|
<PriceDetailsModal
|
|
bookingCode={bookingCode}
|
|
fromDate={checkInDate}
|
|
rooms={mappedRooms}
|
|
toDate={checkOutDate}
|
|
totalPrice={totalPrice}
|
|
vat={vat}
|
|
defaultCurrency={currency}
|
|
isCampaignRate={isCampaignRate}
|
|
hotelOffersBreakfast={hotelOffersBreakfast}
|
|
/>
|
|
)
|
|
}
|