'use client'; import React, { useState, useEffect } from 'react'; import { systemConfigService } from '@/services/api/systemConfigService'; import { userService, UserResponse } from '@/services/api/userService'; import { rolePermissionService, RoleResponse } from '@/services/api/rolePermissionService'; import { inventoryPosService, PosSession } from '@/services/api/inventoryPosService'; import { Store, User, Banknote, IndianRupee, CreditCard, BarChart2 } from 'lucide-react'; interface Location { location_id: string; name: string; city?: string; } export default function UserPosAnalyticsPage() { const [locations, setLocations] = useState([]); const [selectedLocation, setSelectedLocation] = useState(''); const [allUsers, setAllUsers] = useState([]); const [posUsers, setPosUsers] = useState([]); const [selectedUser, setSelectedUser] = useState(''); const [sessions, setSessions] = useState([]); const [loading, setLoading] = useState(false); // Fetch initial data useEffect(() => { const fetchInitialData = async () => { let locs: Location[] = []; let users: UserResponse[] = []; let roles: RoleResponse[] = []; try { locs = await systemConfigService.getLocations() || []; setLocations(locs); } catch (e) { console.error('Error fetching locations:', e); } try { users = await userService.getAll() || []; setAllUsers(users); } catch (e) { console.error('Error fetching users:', e); } try { roles = await rolePermissionService.getRoles() || []; } catch (e) { console.error('Error fetching roles:', e); } try { // Find which roles have POS permissions const roleHasPos = new Map(); for (const role of roles) { try { const perms = await rolePermissionService.getRolePermissions(role.role_id) || []; const hasPos = perms.some((p: any) => p && (typeof p === 'string' ? p.startsWith('pos.') : p.permission_code?.startsWith('pos.'))); roleHasPos.set(role.role_id, hasPos); } catch (e) { console.error('Error fetching permissions for role', role.role_id, e); } } // Filter users who have a role with POS permissions const filteredUsers = users.filter(u => roleHasPos.get(u.role_id) === true); setPosUsers(filteredUsers); } catch (error) { console.error('Error processing roles/users:', error); } }; fetchInitialData(); }, []); // Fetch sessions when location and user are selected useEffect(() => { const fetchSessions = async () => { if (!selectedLocation || !selectedUser) { setSessions([]); return; } setLoading(true); try { const data = await inventoryPosService.getPosSessions(undefined, selectedLocation, selectedUser); setSessions(data); } catch (error) { console.error('Error fetching sessions:', error); } finally { setLoading(false); } }; fetchSessions(); }, [selectedLocation, selectedUser]); // Aggregate data const totalSessions = sessions.length; const totalRevenue = sessions.reduce((sum, s) => sum + (s.ecom_sales_amount || 0) + (s.service_sales_amount || 0), 0); const totalCash = sessions.reduce((sum, s) => sum + (s.cash_sales || 0), 0); const totalUPI = sessions.reduce((sum, s) => sum + (s.upi_sales || 0), 0); const totalVariance = sessions.reduce((sum, s) => sum + (s.cash_variance || 0), 0); // Filter users for the dropdown based on selected location const availableUsersForLocation = posUsers.filter(u => !selectedLocation || !u.location_ids || u.location_ids.length === 0 || u.location_ids.includes(selectedLocation) ); return (

User POS Analytics

Track point-of-sale performance and cash handling by user and location.

Filter Criteria

{selectedLocation && availableUsersForLocation.length === 0 && (

No users with POS permissions found for this location.

)}
{loading ? (
) : selectedLocation && selectedUser ? ( <>

Total Sessions

{totalSessions}

Total Revenue

₹{totalRevenue.toFixed(2)}

Cash Collected

₹{totalCash.toFixed(2)}

UPI Collected

₹{totalUPI.toFixed(2)}

Session History

Net Variance: {totalVariance >= 0 ? '+' : ''}₹{totalVariance.toFixed(2)}
{sessions.length === 0 ? ( ) : ( sessions.map((session) => ( )) )}
Session ID Status Revenue Cash Sales Variance
No sessions found for this user at this location.
{session.pos_session_id} {session.status} ₹{((session.ecom_sales_amount || 0) + (session.service_sales_amount || 0)).toFixed(2)} ₹{(session.cash_sales || 0).toFixed(2)} 0 ? 'text-emerald-500' : 'text-red-500' }`}> {(session.cash_variance || 0) > 0 ? '+' : ''}₹{(session.cash_variance || 0).toFixed(2)}
) : (

Select criteria to view analytics

Please select both a store location and a POS user from the dropdowns above to load their performance data.

)}
); }