- Backend: FastAPI app with all models, services, schemas, routes - Admin Panel: Next.js dashboard with all modules (CRM, POS, repairs, warranties, billing, notifications, etc.) - Ecommerce: storefront (submodule) + product catalog - AI Agents: .agents/ skill library for Antigravity IDE - Docs: architecture, billing, notification gateway markdown - Submodules: Backend/OpenWA, Ecommerce/File 2/storefront
315 lines
15 KiB
TypeScript
315 lines
15 KiB
TypeScript
'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<Location[]>([]);
|
|
const [selectedLocation, setSelectedLocation] = useState<string>('');
|
|
|
|
const [allUsers, setAllUsers] = useState<UserResponse[]>([]);
|
|
const [posUsers, setPosUsers] = useState<UserResponse[]>([]);
|
|
const [selectedUser, setSelectedUser] = useState<string>('');
|
|
|
|
const [sessions, setSessions] = useState<PosSession[]>([]);
|
|
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<string, boolean>();
|
|
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 (
|
|
<div className="p-6">
|
|
<div className="flex flex-col md:flex-row justify-between items-start md:items-center mb-8 gap-4">
|
|
<div>
|
|
<h1 className="text-2xl font-bold text-gray-900 dark:text-white flex items-center gap-2">
|
|
<BarChart2 className="w-6 h-6 text-primary" />
|
|
User POS Analytics
|
|
</h1>
|
|
<p className="text-gray-500 dark:text-gray-400 mt-1">
|
|
Track point-of-sale performance and cash handling by user and location.
|
|
</p>
|
|
</div>
|
|
</div>
|
|
|
|
<div className="bg-white dark:bg-[#0A0D14] border border-gray-200 dark:border-gray-800 rounded-xl p-6 mb-8 shadow-sm">
|
|
<h2 className="text-lg font-semibold text-gray-900 dark:text-white mb-4">Filter Criteria</h2>
|
|
<div className="grid grid-cols-1 md:grid-cols-2 gap-6">
|
|
<div>
|
|
<label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
|
|
Select Location
|
|
</label>
|
|
<div className="relative">
|
|
<div className="absolute inset-y-0 left-0 pl-3 flex items-center pointer-events-none">
|
|
<Store className="h-5 w-5 text-gray-400" />
|
|
</div>
|
|
<select
|
|
value={selectedLocation}
|
|
onChange={(e) => {
|
|
setSelectedLocation(e.target.value);
|
|
setSelectedUser(''); // reset user
|
|
}}
|
|
className="pl-10 w-full bg-gray-50 dark:bg-[#11141D] border border-gray-300 dark:border-gray-700 text-gray-900 dark:text-white rounded-lg focus:ring-primary focus:border-primary block p-2.5 outline-none transition-colors"
|
|
>
|
|
<option value="">-- Select Store Location --</option>
|
|
{locations.map(loc => (
|
|
<option key={loc.location_id} value={loc.location_id}>
|
|
{loc.name} - {loc.city}
|
|
</option>
|
|
))}
|
|
</select>
|
|
</div>
|
|
</div>
|
|
|
|
<div>
|
|
<label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
|
|
Select User
|
|
</label>
|
|
<div className="relative">
|
|
<div className="absolute inset-y-0 left-0 pl-3 flex items-center pointer-events-none">
|
|
<User className="h-5 w-5 text-gray-400" />
|
|
</div>
|
|
<select
|
|
value={selectedUser}
|
|
onChange={(e) => setSelectedUser(e.target.value)}
|
|
disabled={!selectedLocation}
|
|
className="pl-10 w-full bg-gray-50 dark:bg-[#11141D] border border-gray-300 dark:border-gray-700 text-gray-900 dark:text-white rounded-lg focus:ring-primary focus:border-primary block p-2.5 outline-none transition-colors disabled:opacity-50 disabled:cursor-not-allowed"
|
|
>
|
|
<option value="">-- Select POS User --</option>
|
|
{availableUsersForLocation.map(u => (
|
|
<option key={u.user_id} value={u.user_id}>
|
|
{u.first_name} {u.last_name} ({u.email})
|
|
</option>
|
|
))}
|
|
</select>
|
|
</div>
|
|
{selectedLocation && availableUsersForLocation.length === 0 && (
|
|
<p className="text-xs text-amber-500 mt-2">No users with POS permissions found for this location.</p>
|
|
)}
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
{loading ? (
|
|
<div className="flex justify-center py-12">
|
|
<div className="animate-spin rounded-full h-8 w-8 border-b-2 border-primary"></div>
|
|
</div>
|
|
) : selectedLocation && selectedUser ? (
|
|
<>
|
|
<div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4 gap-6 mb-8">
|
|
<div className="bg-white dark:bg-[#0A0D14] border border-gray-200 dark:border-gray-800 rounded-xl p-5 shadow-sm">
|
|
<div className="flex items-center gap-4">
|
|
<div className="w-12 h-12 rounded-lg bg-blue-100 dark:bg-blue-900/30 flex items-center justify-center text-blue-600 dark:text-blue-400">
|
|
<BarChart2 className="w-6 h-6" />
|
|
</div>
|
|
<div>
|
|
<p className="text-sm text-gray-500 dark:text-gray-400 font-medium">Total Sessions</p>
|
|
<p className="text-2xl font-bold text-gray-900 dark:text-white">{totalSessions}</p>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div className="bg-white dark:bg-[#0A0D14] border border-gray-200 dark:border-gray-800 rounded-xl p-5 shadow-sm">
|
|
<div className="flex items-center gap-4">
|
|
<div className="w-12 h-12 rounded-lg bg-emerald-100 dark:bg-emerald-900/30 flex items-center justify-center text-emerald-600 dark:text-emerald-400">
|
|
<IndianRupee className="w-6 h-6" />
|
|
</div>
|
|
<div>
|
|
<p className="text-sm text-gray-500 dark:text-gray-400 font-medium">Total Revenue</p>
|
|
<p className="text-2xl font-bold text-gray-900 dark:text-white">₹{totalRevenue.toFixed(2)}</p>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div className="bg-white dark:bg-[#0A0D14] border border-gray-200 dark:border-gray-800 rounded-xl p-5 shadow-sm">
|
|
<div className="flex items-center gap-4">
|
|
<div className="w-12 h-12 rounded-lg bg-indigo-100 dark:bg-indigo-900/30 flex items-center justify-center text-indigo-600 dark:text-indigo-400">
|
|
<Banknote className="w-6 h-6" />
|
|
</div>
|
|
<div>
|
|
<p className="text-sm text-gray-500 dark:text-gray-400 font-medium">Cash Collected</p>
|
|
<p className="text-2xl font-bold text-gray-900 dark:text-white">₹{totalCash.toFixed(2)}</p>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div className="bg-white dark:bg-[#0A0D14] border border-gray-200 dark:border-gray-800 rounded-xl p-5 shadow-sm">
|
|
<div className="flex items-center gap-4">
|
|
<div className="w-12 h-12 rounded-lg bg-purple-100 dark:bg-purple-900/30 flex items-center justify-center text-purple-600 dark:text-purple-400">
|
|
<CreditCard className="w-6 h-6" />
|
|
</div>
|
|
<div>
|
|
<p className="text-sm text-gray-500 dark:text-gray-400 font-medium">UPI Collected</p>
|
|
<p className="text-2xl font-bold text-gray-900 dark:text-white">₹{totalUPI.toFixed(2)}</p>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<div className="bg-white dark:bg-[#0A0D14] border border-gray-200 dark:border-gray-800 rounded-xl overflow-hidden shadow-sm">
|
|
<div className="px-6 py-4 border-b border-gray-200 dark:border-gray-800 flex justify-between items-center">
|
|
<h2 className="text-lg font-semibold text-gray-900 dark:text-white">Session History</h2>
|
|
<span className={`px-3 py-1 rounded-full text-xs font-medium ${
|
|
totalVariance === 0
|
|
? 'bg-emerald-100 text-emerald-700 dark:bg-emerald-900/30 dark:text-emerald-400'
|
|
: 'bg-red-100 text-red-700 dark:bg-red-900/30 dark:text-red-400'
|
|
}`}>
|
|
Net Variance: {totalVariance >= 0 ? '+' : ''}₹{totalVariance.toFixed(2)}
|
|
</span>
|
|
</div>
|
|
<div className="overflow-x-auto">
|
|
<table className="w-full text-sm text-left text-gray-500 dark:text-gray-400">
|
|
<thead className="text-xs text-gray-700 uppercase bg-gray-50 dark:bg-[#11141D] dark:text-gray-300">
|
|
<tr>
|
|
<th scope="col" className="px-6 py-4">Session ID</th>
|
|
<th scope="col" className="px-6 py-4">Status</th>
|
|
<th scope="col" className="px-6 py-4">Revenue</th>
|
|
<th scope="col" className="px-6 py-4">Cash Sales</th>
|
|
<th scope="col" className="px-6 py-4">Variance</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
{sessions.length === 0 ? (
|
|
<tr>
|
|
<td colSpan={5} className="px-6 py-8 text-center text-gray-500 dark:text-gray-400">
|
|
No sessions found for this user at this location.
|
|
</td>
|
|
</tr>
|
|
) : (
|
|
sessions.map((session) => (
|
|
<tr key={session.pos_session_id} className="border-b border-gray-200 dark:border-gray-800 hover:bg-gray-50 dark:hover:bg-[#11141D]/50 transition-colors">
|
|
<td className="px-6 py-4 font-medium text-gray-900 dark:text-white whitespace-nowrap">
|
|
{session.pos_session_id}
|
|
</td>
|
|
<td className="px-6 py-4">
|
|
<span className={`px-2.5 py-1 rounded-full text-xs font-medium ${
|
|
session.status === 'OPEN' ? 'bg-emerald-100 text-emerald-700 dark:bg-emerald-900/30 dark:text-emerald-400' : 'bg-gray-100 text-gray-700 dark:bg-gray-800 dark:text-gray-300'
|
|
}`}>
|
|
{session.status}
|
|
</span>
|
|
</td>
|
|
<td className="px-6 py-4 text-gray-900 dark:text-gray-300">
|
|
₹{((session.ecom_sales_amount || 0) + (session.service_sales_amount || 0)).toFixed(2)}
|
|
</td>
|
|
<td className="px-6 py-4 text-gray-900 dark:text-gray-300">
|
|
₹{(session.cash_sales || 0).toFixed(2)}
|
|
</td>
|
|
<td className="px-6 py-4">
|
|
<span className={`${
|
|
(session.cash_variance || 0) === 0 ? 'text-gray-500' : (session.cash_variance || 0) > 0 ? 'text-emerald-500' : 'text-red-500'
|
|
}`}>
|
|
{(session.cash_variance || 0) > 0 ? '+' : ''}₹{(session.cash_variance || 0).toFixed(2)}
|
|
</span>
|
|
</td>
|
|
</tr>
|
|
))
|
|
)}
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
</>
|
|
) : (
|
|
<div className="bg-gray-50 dark:bg-[#11141D] rounded-xl border border-gray-200 dark:border-gray-800 p-12 text-center">
|
|
<BarChart2 className="w-12 h-12 text-gray-400 mx-auto mb-4" />
|
|
<h3 className="text-lg font-medium text-gray-900 dark:text-white mb-2">Select criteria to view analytics</h3>
|
|
<p className="text-gray-500 dark:text-gray-400 max-w-md mx-auto">
|
|
Please select both a store location and a POS user from the dropdowns above to load their performance data.
|
|
</p>
|
|
</div>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|