@@ -34,17 +34,51 @@ export async function buildFloorState(db: DbInstance, includeInactive: boolean)
3434 const sessionIds = openSessions . map ( ( s ) => s . id ) ;
3535 const orderRows = sessionIds . length > 0
3636 ? await db
37- . select ( { tableSessionId : schema . orders . tableSessionId , status : schema . orders . status , createdAt : schema . orders . createdAt } )
37+ . select ( { id : schema . orders . id , tableSessionId : schema . orders . tableSessionId , status : schema . orders . status , createdAt : schema . orders . createdAt } )
3838 . from ( schema . orders )
3939 . where ( inArray ( schema . orders . tableSessionId , sessionIds ) )
4040 : [ ] ;
4141
42- const counts = new Map < string , { orderCount : number ; readyCount : number ; oldestSubmittedAt : number | null } > ( ) ;
42+ const orderIds = orderRows . map ( ( o ) => o . id ) ;
43+ const itemRows = includeInactive && orderIds . length > 0
44+ ? await db
45+ . select ( { orderId : schema . orderItems . orderId , price : schema . orderItems . price , quantity : schema . orderItems . quantity } )
46+ . from ( schema . orderItems )
47+ . where ( inArray ( schema . orderItems . orderId , orderIds ) )
48+ : [ ] ;
49+ const orderTotal = new Map < string , number > ( ) ;
50+ for ( const item of itemRows ) orderTotal . set ( item . orderId , ( orderTotal . get ( item . orderId ) ?? 0 ) + item . price * item . quantity ) ;
51+
52+ const checks = includeInactive && sessionIds . length > 0
53+ ? await db
54+ . select ( )
55+ . from ( schema . checks )
56+ . where ( inArray ( schema . checks . tableSessionId , sessionIds ) )
57+ : [ ] ;
58+ const checkBySession = new Map < string , typeof checks [ number ] > ( ) ;
59+ for ( const check of checks ) {
60+ const prev = checkBySession . get ( check . tableSessionId ) ;
61+ if ( ! prev || check . createdAt > prev . createdAt ) checkBySession . set ( check . tableSessionId , check ) ;
62+ }
63+
64+ const checkTotal = ( check : typeof checks [ number ] ) : number => {
65+ const lines = check . lines ?? [ ] ;
66+ const adjustments = check . adjustments ?? [ ] ;
67+ const subtotal = lines . reduce ( ( sum , line ) => sum + line . quantity * line . unitPrice , 0 ) ;
68+ const discount = check . discount
69+ ? check . discount . type === 'percent' ? Math . round ( subtotal * check . discount . value / 100 ) : check . discount . value
70+ : 0 ;
71+ const adjusted = subtotal - discount + adjustments . reduce ( ( sum , a ) => sum + a . amount , 0 ) ;
72+ return Math . max ( 0 , adjusted ) ;
73+ } ;
74+
75+ const counts = new Map < string , { orderCount : number ; readyCount : number ; oldestSubmittedAt : number | null ; provisionalTotal : number } > ( ) ;
4376 for ( const o of orderRows ) {
4477 if ( ! o . tableSessionId ) continue ;
45- const c = counts . get ( o . tableSessionId ) ?? { orderCount : 0 , readyCount : 0 , oldestSubmittedAt : null } ;
78+ const c = counts . get ( o . tableSessionId ) ?? { orderCount : 0 , readyCount : 0 , oldestSubmittedAt : null , provisionalTotal : 0 } ;
4679 // "Open orders" = anything not served/rejected; "ready" = ready to serve.
4780 if ( o . status === 'submitted' || o . status === 'ready' ) c . orderCount ++ ;
81+ if ( o . status !== 'rejected' ) c . provisionalTotal += orderTotal . get ( o . id ) ?? 0 ;
4882 if ( o . status === 'ready' ) c . readyCount ++ ;
4983 if ( o . status === 'submitted' && ( c . oldestSubmittedAt === null || o . createdAt < c . oldestSubmittedAt ) ) {
5084 c . oldestSubmittedAt = o . createdAt ;
@@ -69,6 +103,11 @@ export async function buildFloorState(db: DbInstance, includeInactive: boolean)
69103 x : t . x ,
70104 y : t . y ,
71105 shape : t . shape ,
106+ ...( includeInactive ? {
107+ provisionalTotal : count ?. provisionalTotal ?? 0 ,
108+ checkStatus : session ? checkBySession . get ( session . id ) ?. status ?? null : null ,
109+ checkTotal : session && checkBySession . has ( session . id ) ? checkTotal ( checkBySession . get ( session . id ) ! ) : null ,
110+ } : { } ) ,
72111 oldestSubmittedAt : count ?. oldestSubmittedAt ?? null ,
73112 } ;
74113 } ) ,
0 commit comments