-
-
Save oluyoung/614a2484e8d60727d38f05e9b7f762f0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Frontend | |
const calculateBookingTotals = (reservations: ReservationModel[]): BookingTotals => { | |
let grossTotal = 0; | |
let taxTotal = 0; | |
let cityTaxTotal = 0; | |
let touristTaxTotal = 0; | |
let salesTaxTotal = 0; | |
reservations.forEach((reservation) => { | |
grossTotal = grossTotal + (reservation.totalGrossAmount.amount || 0); | |
if (reservation.extras) { | |
const extrasTotal = calculateExtrasTotals(reservation.extras); | |
grossTotal = grossTotal + extrasTotal.grossTotal; | |
taxTotal = taxTotal + extrasTotal.taxTotal; | |
} | |
if (reservation.taxes) { | |
reservation.taxes.forEach((tax) => { | |
if (tax.type === TaxTypeEnum.VAT) { | |
taxTotal = taxTotal + tax.amount; | |
} | |
if (tax.type === TaxTypeEnum.CityTax) { | |
cityTaxTotal = cityTaxTotal + tax.amount; | |
} | |
if (tax.type === TaxTypeEnum.TouristTax) { | |
touristTaxTotal = touristTaxTotal + tax.amount; | |
} | |
if (tax.type === TaxTypeEnum.SalesTax) { | |
salesTaxTotal = salesTaxTotal + tax.amount; | |
} | |
}); | |
} | |
}); | |
return { | |
netTotal: grossTotal - taxTotal, | |
taxTotal, | |
grossTotal: grossTotal + cityTaxTotal + touristTaxTotal + salesTaxTotal, | |
cityTaxTotal, | |
touristTaxTotal, | |
salesTaxTotal | |
} | |
} | |
// Server | |
calculateBookingTotals(reservations: ReservationModel[]) { | |
return reservations.reduce((total, reservation) => { | |
if (reservation.prePaymentAmount) { | |
total.prePaymentAmount = total.prePaymentAmount + reservation.prePaymentAmount.amount; | |
} | |
if (reservation.extras) { | |
reservation.extras.forEach((extra) => { | |
if (extra.totalGrossAmount) { | |
total.totalGrossAmount = total.totalGrossAmount + extra.totalGrossAmount.amount; | |
} | |
if (extra.prePaymentAmount) { | |
total.prePaymentAmount = total.prePaymentAmount + extra.prePaymentAmount.amount; | |
} | |
}); | |
} | |
if (reservation.taxes) { | |
const taxAmount = reservation.taxes.reduce((totalAmount, tax) => { | |
return totalAmount + tax.amount; | |
}, 0) | |
total.totalGrossAmount = total.totalGrossAmount + taxAmount; | |
} | |
if (reservation.totalGrossAmount) { | |
total.totalGrossAmount = total.totalGrossAmount + reservation.totalGrossAmount.amount; | |
} | |
return total; | |
}, { | |
prePaymentAmount: 0, | |
totalGrossAmount: 0 | |
}); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment