Add pending amount to booking list
All checks were successful
build-and-deploy / build-deploy (push) Successful in 39s
All checks were successful
build-and-deploy / build-deploy (push) Successful in 39s
This commit is contained in:
@@ -157,6 +157,9 @@ class BookingFlow(
|
||||
Role.HOUSEKEEPING,
|
||||
Role.FINANCE
|
||||
)
|
||||
val property = propertyRepo.findById(propertyId).orElseThrow {
|
||||
ResponseStatusException(HttpStatus.NOT_FOUND, "Property not found")
|
||||
}
|
||||
val statuses = parseStatuses(status)
|
||||
val bookings = if (statuses.isEmpty()) {
|
||||
bookingRepo.findByPropertyIdOrderByCreatedAtDesc(propertyId)
|
||||
@@ -171,8 +174,27 @@ class BookingFlow(
|
||||
.groupBy { it.bookingId }
|
||||
.mapValues { (_, rows) -> rows.map { it.roomNumber }.distinct().sorted() }
|
||||
}
|
||||
val staysByBooking = if (bookingIds.isEmpty()) {
|
||||
emptyMap()
|
||||
} else {
|
||||
roomStayRepo.findByBookingIdIn(bookingIds).groupBy { it.booking.id!! }
|
||||
}
|
||||
val paymentsByBooking = if (bookingIds.isEmpty()) {
|
||||
emptyMap()
|
||||
} else {
|
||||
paymentRepo.sumAmountByBookingIds(bookingIds)
|
||||
.associate { it.bookingId to it.total }
|
||||
}
|
||||
return bookings.map { booking ->
|
||||
val guest = booking.primaryGuest
|
||||
val stays = staysByBooking[booking.id].orEmpty()
|
||||
val expectedPay = if (stays.isEmpty()) {
|
||||
null
|
||||
} else {
|
||||
computeExpectedPay(stays, property.timezone)
|
||||
}
|
||||
val collected = paymentsByBooking[booking.id] ?: 0L
|
||||
val pending = expectedPay?.let { it - collected }
|
||||
BookingListItem(
|
||||
id = booking.id!!,
|
||||
status = booking.status.name,
|
||||
@@ -191,7 +213,8 @@ class BookingFlow(
|
||||
femaleCount = booking.femaleCount,
|
||||
totalGuestCount = booking.totalGuestCount,
|
||||
expectedGuestCount = booking.expectedGuestCount,
|
||||
notes = booking.notes
|
||||
notes = booking.notes,
|
||||
pending = pending
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -72,7 +72,8 @@ data class BookingListItem(
|
||||
val femaleCount: Int?,
|
||||
val totalGuestCount: Int?,
|
||||
val expectedGuestCount: Int?,
|
||||
val notes: String?
|
||||
val notes: String?,
|
||||
val pending: Long? = null
|
||||
)
|
||||
|
||||
data class BookingDetailResponse(
|
||||
|
||||
@@ -17,4 +17,19 @@ interface PaymentRepo : JpaRepository<Payment, UUID> {
|
||||
"""
|
||||
)
|
||||
fun sumAmountByBookingId(@Param("bookingId") bookingId: UUID): Long
|
||||
|
||||
@Query(
|
||||
"""
|
||||
select p.booking.id as bookingId, coalesce(sum(p.amount), 0) as total
|
||||
from Payment p
|
||||
where p.booking.id in :bookingIds
|
||||
group by p.booking.id
|
||||
"""
|
||||
)
|
||||
fun sumAmountByBookingIds(@Param("bookingIds") bookingIds: List<UUID>): List<BookingPaymentSumRow>
|
||||
}
|
||||
|
||||
interface BookingPaymentSumRow {
|
||||
val bookingId: UUID
|
||||
val total: Long
|
||||
}
|
||||
|
||||
@@ -43,6 +43,13 @@ interface RoomStayRepo : JpaRepository<RoomStay, UUID> {
|
||||
""")
|
||||
fun findByBookingId(@Param("bookingId") bookingId: UUID): List<RoomStay>
|
||||
|
||||
@Query("""
|
||||
select rs
|
||||
from RoomStay rs
|
||||
where rs.booking.id in :bookingIds
|
||||
""")
|
||||
fun findByBookingIdIn(@Param("bookingIds") bookingIds: List<UUID>): List<RoomStay>
|
||||
|
||||
@Query("""
|
||||
select rs.room.id
|
||||
from RoomStay rs
|
||||
|
||||
Reference in New Issue
Block a user