Files
web/apps/scandic-web/stores/my-stay/helpers.ts
Anton Gunnarsson 800dc5c3c1 Merged in feat/sw-3225-move-parking-information-to-booking-flow (pull request #2614)
feat(SW-3225): Move ParkingInformation to design-system

* Inline ParkingInformation types to remove trpc dependency

* Move ParkingInformation to design-system

* Move numberFormatting to common package

* Add deps to external

* Fix imports and i18n script

* Add common as dependency

* Merge branch 'master' into feat/sw-3225-move-parking-information-to-booking-flow


Approved-by: Linus Flood
2025-08-12 12:36:31 +00:00

81 lines
2.2 KiB
TypeScript

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: Room[],
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.totalPoints) {
total.points = total.points + room.totalPoints
}
// room.totalPrice is a negative value when
// its a vouchers booking (╯°□°)╯︵ ┻━┻
if (room.totalPrice && !room.vouchers) {
total.cash = total.cash + room.totalPrice
}
return total
},
{
cash: 0,
cheques: 0,
points: 0,
vouchers: 0,
}
)
let totalPrice = ""
if (totals.cheques) {
totalPrice = `${totals.cheques} ${CurrencyEnum.CC}`
}
if (totals.points) {
const appendTotalPrice = totalPrice ? `${totalPrice} + ` : ""
totalPrice = `${appendTotalPrice}${totals.points} ${CurrencyEnum.POINTS}`
}
if (totals.vouchers) {
const appendTotalPrice = totalPrice ? `${totalPrice} + ` : ""
totalPrice = `${appendTotalPrice}${totals.vouchers} ${intl.formatMessage({ defaultMessage: "Voucher" })}`
}
if (totals.cash) {
const appendTotalPrice = totalPrice ? `${totalPrice} + ` : ""
const cashPrice = formatPrice(intl, totals.cash, currency)
totalPrice = `${appendTotalPrice}${cashPrice}`
}
return totalPrice
}
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)
}