Hello Team,
I want to implement data filtering, by using date range to filter the booking and the final column will sum up below as shown on the screenshot.

CONTROLLER METHOD.
C#
public ActionResult GetAllBookingHistoryPDF(string RoomNumber, string sDate = null, string eDate = null)
{
DateTime? startDate = null;
if (!string.IsNullOrEmpty(sDate))
startDate = Convert.ToDateTime(sDate);
DateTime? endDate = null;
if (!string.IsNullOrEmpty(eDate))
endDate = Convert.ToDateTime(eDate);
var bookingList = objHotelDbEntities.RoomBookings.Join(objHotelDbEntities.Rooms,
booking => booking.AssignRoomId,
room => room.RoomId,
(booking, room) => new { booking = booking, room = room });
var modifiedData = bookingList.GroupBy(x => new { x.booking.BookingFrom, x.booking.BookingTo, x.booking.BookingId }).Select(x => new AllBookingDetail
{
CustomerName = x.FirstOrDefault().booking.CustomerName,
Address = x.FirstOrDefault().booking.Address,
PhoneNo = x.FirstOrDefault().booking.PhoneNo,
BookingFrom = x.Key.BookingFrom,
BookingTo = x.Key.BookingTo,
BookingId = x.FirstOrDefault().booking.BookingId,
RoomNumber = x.FirstOrDefault().room.RoomNumber,
NoOfMembers = x.FirstOrDefault().booking.NoOfMembers,
numberOfDays = System.Data.Entity.DbFunctions.DiffDays(x.Key.BookingFrom, x.Key.BookingTo).Value,
RoomPrice = x.FirstOrDefault().room.RoomPrice,
TotalAmount = x.Sum(a=>a.booking.TotalAmount)
}).ToList();
if (!string.IsNullOrEmpty(sDate) && !string.IsNullOrEmpty(eDate))
{
DateTime fromDate = DateTime.Parse(sDate);
DateTime toDate = DateTime.Parse(eDate);
modifiedData = modifiedData.Where(a => (a.BookingFrom) >= fromDate && (a.BookingTo) x.BookingFrom).ToList();
}
decimal total = 0;
if (modifiedData != null && modifiedData.Count() > 0)
{
total = modifiedData.Sum(a => a.TotalAmount)
}
return Json(modifiedData, JsonRequestBehavior.AllowGet);
}
Emmmanuel FIADUFERank #947 · 2.2K reputationPosted Nov 27, 2024, 10:18 AM EST
JS Function
$scope.DateChange = function (e) { $http({ url: '/Home/GetAllBookingHistoryPDF', method: 'GET', params: { sDate: $scope.sDate, eDate: $scope.eDate } }).then(function (response) { var data = response.data; $scope.PrintBookingRecordList = data; sumOfSalesDetails($scope); }) } function sumOfSalesDetails($scope) { $scope.FinalTotal = 0; $.each($scope.PrintBookingRecordList.modifiedData, function (index, val) { $scope.FinalTotal += val.TotalAmount; }) }HTML
Sangeetha SRank #212 · 11.4K reputationPosted Nov 28, 2024, 12:27 AM EST
Hi,
public ActionResult GetAllBookingHistoryPDF(string RoomNumber, string sDate = null, string eDate = null) { // Convert the start and end dates DateTime? startDate = string.IsNullOrEmpty(sDate) ? (DateTime?)null : Convert.ToDateTime(sDate); DateTime? endDate = string.IsNullOrEmpty(eDate) ? (DateTime?)null : Convert.ToDateTime(eDate); // Join RoomBookings with Rooms var bookingList = objHotelDbEntities.RoomBookings .Join(objHotelDbEntities.Rooms, booking => booking.AssignRoomId, room => room.RoomId, (booking, room) => new { booking, room }); // Apply date range filtering before grouping if (startDate.HasValue && endDate.HasValue) { bookingList = bookingList.Where(b => b.booking.BookingFrom >= startDate.Value && b.booking.BookingTo new { x.booking.BookingFrom, x.booking.BookingTo, x.booking.BookingId }) .Select(x => new AllBookingDetail { CustomerName = x.FirstOrDefault().booking.CustomerName, Address = x.FirstOrDefault().booking.Address, PhoneNo = x.FirstOrDefault().booking.PhoneNo, BookingFrom = x.Key.BookingFrom, BookingTo = x.Key.BookingTo, BookingId = x.FirstOrDefault().booking.BookingId, RoomNumber = x.FirstOrDefault().room.RoomNumber, NoOfMembers = x.FirstOrDefault().booking.NoOfMembers, numberOfDays = System.Data.Entity.DbFunctions.DiffDays(x.Key.BookingFrom, x.Key.BookingTo).Value, RoomPrice = x.FirstOrDefault().room.RoomPrice, TotalAmount = x.Sum(a => a.booking.TotalAmount) }).ToList(); // Calculate the total amount for the filtered results decimal total = modifiedData.Sum(a => a.TotalAmount); // Return the JSON result return Json(new { bookings = modifiedData, totalAmount = total }, JsonRequestBehavior.AllowGet); }Emmmanuel FIADUFERank #947 · 2.2K reputationPosted Nov 28, 2024, 5:09 AM EST
Hello Sangeetha S
Thank you for your response, but however this code is causing the data not to even load again, as you can see in the screenshot
// Return the JSON result
return Json(new { bookings = modifiedData, totalAmount = total }, JsonRequestBehavior.AllowGet);
Emmmanuel FIADUFERank #947 · 2.2K reputationPosted Dec 3, 2024, 9:18 AM EST
Hello team ,
Please any further help will be highly appreciated