@@ -18,14 +18,14 @@ function createRoomBookingRow(booking, index) {
1818 row . innerHTML = `
1919 <td>${ index + 1 } </td>
2020 <td>${ booking . bookingid } </td>
21- <td>${ booking . CardDb . issuedto } </td>
22- <td>${ booking . CardDb . mobno } </td>
23- <td>${ booking . CardDb . center } </td>
21+ <td>${ booking . CardDb ? .issuedto || "" } </td>
22+ <td>${ booking . CardDb ? .mobno || "" } </td>
23+ <td>${ booking . CardDb ? .center || "" } </td>
2424 <td>${ booking . roomno || "Not Assigned" } </td>
25- <td>${ booking . roomtype } </td>
25+ <td>${ booking . roomtype || "NA" } </td>
2626 <td>${ formatDate ( booking . checkin ) } </td>
2727 <td>${ formatDate ( booking . checkout ) } </td>
28- <td>${ booking . nights } </td>
28+ <td>${ booking . nights ?? 0 } </td>
2929 <td>${ booking . status } </td>
3030 <td>${ booking . bookedBy || "Self" } </td>
3131 ` ;
@@ -44,18 +44,14 @@ async function fetchReport() {
4444 return ;
4545 }
4646
47- // Collect checkboxes
47+ // Collect checked statuses
4848 const checkedStatuses = [
4949 ...document . querySelectorAll ( 'input[name="status"]:checked' )
5050 ] . map ( cb => cb . value ) ;
5151
5252 const searchParams = new URLSearchParams ( ) ;
5353
54- // If nothing selected → fallback to default "checkedin"
55- // If all statuses checked → send them all
56- checkedStatuses . forEach ( s => searchParams . append ( "statuses" , s ) ) ;
57-
58-
54+ checkedStatuses . forEach ( s => searchParams . append ( "statuses" , s ) ) ;
5955 searchParams . append ( "utsavid" , utsav_id ) ;
6056 searchParams . append ( "type" , reportType ) ;
6157
@@ -103,32 +99,58 @@ checkedStatuses.forEach(s => searchParams.append("statuses", s));
10399----------------------------------------------------*/
104100document . addEventListener ( "DOMContentLoaded" , async function ( ) {
105101
106- // Check ALL statuses by default
107- document . querySelectorAll ( 'input[name="status"]' ) . forEach ( cb => cb . checked = true ) ;
102+ // Check ALL statuses by default
103+ document
104+ . querySelectorAll ( 'input[name="status"]' )
105+ . forEach ( cb => cb . checked = true ) ;
108106
109- // 3. Read utsav_id
107+ // Read utsav_id
110108 const params = new URLSearchParams ( window . location . search ) ;
111- let utsav_id = params . get ( "utsav_id" ) || sessionStorage . getItem ( "current_utsav_id" ) ;
109+ let utsav_id =
110+ params . get ( "utsav_id" ) ||
111+ sessionStorage . getItem ( "current_utsav_id" ) ;
112112
113113 if ( ! utsav_id ) {
114114 showErrorMessage ( "utsav_id missing in URL or session." ) ;
115115 return ;
116116 }
117117
118118 sessionStorage . setItem ( "current_utsav_id" , utsav_id ) ;
119+
119120 const hiddenField = document . getElementById ( "utsav_id" ) ;
120121 if ( hiddenField ) hiddenField . value = utsav_id ;
121122
122- // 4. Load initial report → sends only "checkedin"
123+ // Initial fetch
123124 await fetchReport ( ) ;
124125
125- // 5. Submit button triggers fetchReport()
126- document . getElementById ( "reportForm" ) . addEventListener ( "submit" , function ( event ) {
127- event . preventDefault ( ) ;
128- fetchReport ( ) ;
129- } ) ;
126+ // Submit button
127+ document
128+ . getElementById ( "reportForm" )
129+ . addEventListener ( "submit" , function ( event ) {
130+ event . preventDefault ( ) ;
131+ fetchReport ( ) ;
132+ } ) ;
130133} ) ;
131134
135+ /* ---------------------------------------------------
136+ FLATTEN DATA FOR EXCEL EXPORT (🔥 FIX)
137+ ----------------------------------------------------*/
138+ function getRoomOccupancyExportData ( ) {
139+ return roomreports . map ( b => ( {
140+ bookingid : b . bookingid ,
141+ guest_name : b . CardDb ?. issuedto || "" ,
142+ mobile_no : b . CardDb ?. mobno || "" ,
143+ center : b . CardDb ?. center || "" ,
144+ roomno : b . roomno || "NA" ,
145+ roomtype : b . roomtype || "NA" ,
146+ checkin : formatDate ( b . checkin ) ,
147+ checkout : formatDate ( b . checkout ) ,
148+ nights : b . nights ?? 0 ,
149+ status : b . status ,
150+ booked_by : b . bookedBy || "Self"
151+ } ) ) ;
152+ }
153+
132154/* ---------------------------------------------------
133155 DOWNLOAD EXCEL BUTTON SETUP
134156----------------------------------------------------*/
@@ -137,7 +159,7 @@ const setupDownloadButton = () => {
137159
138160 renderDownloadButton ( {
139161 selector : "#downloadBtnContainer" ,
140- getData : ( ) => roomreports ,
162+ getData : ( ) => getRoomOccupancyExportData ( ) , // ✅ FIXED
141163 fileName : "room_occupancy_report.xlsx" ,
142164 sheetName : "Room Occupancy"
143165 } ) ;
0 commit comments