import { CurrencyEnum } from "@scandic-hotels/common/constants/currency" import { formatPrice } from "@scandic-hotels/common/utils/numberFormatting" import type { IntlShape } from "react-intl" import type { Room } from "@/types/stores/my-stay" export function calculateTotalPrice( rooms: Pick< Room, | "cheques" | "vouchers" | "roomPoints" | "roomPointType" | "totalPoints" | "totalPrice" | "isCancelled" >[], currency: CurrencyEnum, intl: IntlShape, allRoomsAreCancelled: boolean ) { const totals = rooms.reduce( (total, room) => { if (!allRoomsAreCancelled && room.isCancelled) { return total } if (room.cheques) { total.cheques = total.cheques + room.cheques } if (room.vouchers) { total.vouchers = total.vouchers + room.vouchers } if ( room.roomPoints && room.roomPointType && room.roomPointType !== "Scandic" ) { total.partnerPoints = total.partnerPoints + room.roomPoints total.partnerPointsCurrency = room.roomPointType ?? null } if (room.totalPoints) { total.scandicFriendsPoints = total.scandicFriendsPoints + room.totalPoints } // room.totalPrice is a negative value when // its a vouchers booking (╯°□°)╯︵ ┻━┻ if (room.totalPrice > 0) { total.cash = total.cash + room.totalPrice } return total }, { cash: 0, cheques: 0, scandicFriendsPoints: 0, partnerPoints: 0, partnerPointsCurrency: null as Room["roomPointType"], vouchers: 0, } ) const priceParts: string[] = [] if (totals.vouchers) { priceParts.push( `${totals.vouchers} ${intl.formatMessage( { id: "price.numberOfVouchers", defaultMessage: "{numberOfVouchers, plural, one {Voucher} other {Vouchers}}", }, { numberOfVouchers: totals.vouchers, } )}` ) } if (totals.cheques) { priceParts.push(`${totals.cheques} ${CurrencyEnum.CC}`) } if (totals.scandicFriendsPoints) { priceParts.push(`${totals.scandicFriendsPoints} ${CurrencyEnum.POINTS}`) } if (totals.partnerPoints) { priceParts.push(`${totals.partnerPoints} ${totals.partnerPointsCurrency}`) } if (totals.cash) { const cashPrice = formatPrice(intl, totals.cash, currency) priceParts.push(cashPrice) } return priceParts.join(" + ") } export function calculateTotalPoints( rooms: Room[], allRoomsAreCancelled: boolean ) { return rooms.reduce((total, room) => { if (!allRoomsAreCancelled && room.isCancelled) { return total } return total + room.totalPoints }, 0) } export function isAllRoomsCancelled(rooms: Room[]) { return !rooms.some((room) => room.isCancelled === false) }