Files
loyaly-merchant/src/features/commerce/services/commerceService.ts
2026-08-06 18:13:59 +05:30

190 lines
7.6 KiB
TypeScript

import type {
BusinessAlert,
MetricCardData,
PaymentBreakdownItem,
TopProductRow,
} from '../types/commerce';
const STORE_MULTIPLIERS: Record<string, number> = {
all: 1.0,
'blr-indiranagar': 0.42,
'blr-koramangala': 0.28,
'blr-whitefield': 0.18,
'che-anna-nagar': 0.12,
'hyd-jubilee': 0.1,
};
const RANGE_MULTIPLIERS: Record<string, number> = {
'7d': 1.0,
'30d': 3.8,
'90d': 11.2,
mtd: 2.1,
ytd: 24.5,
custom: 1.5,
};
function getMult(storeId: string, range: string): {storeMult: number; rangeMult: number; totalMult: number} {
const storeMult = STORE_MULTIPLIERS[storeId] ?? 1.0;
const rangeMult = RANGE_MULTIPLIERS[range] ?? 1.0;
return {storeMult, rangeMult, totalMult: storeMult * rangeMult};
}
export function getCommerceKpis(storeId = 'all', range = '7d') {
const {totalMult, storeMult} = getMult(storeId, range);
const baseRevenue = 340000 * totalMult;
const baseOrders = Math.round(1284 * totalMult);
const baseAov = Math.round(1420 * (0.9 + storeMult * 0.25));
const baseNetSales = 318000 * totalMult;
const baseRefunds = 22000 * totalMult;
const baseConversion = Number((23.8 * (0.85 + storeMult * 0.3)).toFixed(1));
return {
revenue: {
title: 'Total Revenue',
rawValue: baseRevenue,
change: '+14.2% vs prev',
trendDirection: 'up' as const,
subtitle: 'Net revenue across selected store',
},
orders: {
title: 'Total Orders',
rawValue: baseOrders,
change: '+8.5%',
trendDirection: 'up' as const,
subtitle: 'Completed order volume',
},
aov: {
title: 'Avg Order Value',
rawValue: baseAov,
change: '+5.1%',
trendDirection: 'up' as const,
subtitle: 'Average spend per cart',
},
netSales: {
title: 'Net Sales',
rawValue: baseNetSales,
change: '+13.9%',
trendDirection: 'up' as const,
subtitle: 'Revenue after discounts',
},
refunds: {
title: 'Refunds',
rawValue: baseRefunds,
change: '-2.4%',
trendDirection: 'down' as const,
subtitle: 'Total returns rate',
},
conversion: {
title: 'Conversion',
rawValue: baseConversion,
change: '+1.4%',
trendDirection: 'up' as const,
subtitle: 'Visits to purchase ratio',
},
grossSales: {
title: 'Gross Sales',
rawValue: baseRevenue * 1.065,
change: '+12.8%',
trendDirection: 'up' as const,
},
discounts: {
title: 'Discounts & Promos',
rawValue: baseRefunds,
change: '-4.1%',
trendDirection: 'down' as const,
},
};
}
export function getRevenueTrendData(storeId = 'all', range = '7d') {
const {totalMult} = getMult(storeId, range);
return [
{time: '08:00', Revenue: Math.round(18000 * totalMult), Target: Math.round(15000 * totalMult)},
{time: '10:00', Revenue: Math.round(35000 * totalMult), Target: Math.round(30000 * totalMult)},
{time: '12:00', Revenue: Math.round(72000 * totalMult), Target: Math.round(60000 * totalMult)},
{time: '14:00', Revenue: Math.round(95000 * totalMult), Target: Math.round(80000 * totalMult)},
{time: '16:00', Revenue: Math.round(82000 * totalMult), Target: Math.round(75000 * totalMult)},
{time: '18:00', Revenue: Math.round(110000 * totalMult), Target: Math.round(90000 * totalMult)},
{time: '20:00', Revenue: Math.round(68000 * totalMult), Target: Math.round(60000 * totalMult)},
];
}
export function getOrdersTrendData(storeId = 'all', range = '7d') {
const {totalMult} = getMult(storeId, range);
return [
{time: '08:00', Orders: Math.max(1, Math.round(42 * totalMult))},
{time: '10:00', Orders: Math.max(1, Math.round(88 * totalMult))},
{time: '12:00', Orders: Math.max(1, Math.round(165 * totalMult))},
{time: '14:00', Orders: Math.max(1, Math.round(210 * totalMult))},
{time: '16:00', Orders: Math.max(1, Math.round(184 * totalMult))},
{time: '18:00', Orders: Math.max(1, Math.round(245 * totalMult))},
{time: '20:00', Orders: Math.max(1, Math.round(135 * totalMult))},
];
}
export function getPaymentBreakdownData(storeId = 'all', range = '7d'): PaymentBreakdownItem[] {
const {totalMult} = getMult(storeId, range);
const totalInr = 340000 * totalMult;
return [
{method: 'UPI', amount: `₹${(totalInr * 0.58).toLocaleString('en-IN', {maximumFractionDigits: 0})}`, percentage: 58, color: '#10b981'},
{method: 'Credit & Debit Cards', amount: `₹${(totalInr * 0.28).toLocaleString('en-IN', {maximumFractionDigits: 0})}`, percentage: 28, color: '#3b82f6'},
{method: 'Store Credit / LYT', amount: `₹${(totalInr * 0.10).toLocaleString('en-IN', {maximumFractionDigits: 0})}`, percentage: 10, color: '#f59e0b'},
{method: 'Cash & POS', amount: `₹${(totalInr * 0.04).toLocaleString('en-IN', {maximumFractionDigits: 0})}`, percentage: 4, color: '#8b5cf6'},
];
}
export const STORE_PERFORMANCE_DATA = [
{store: 'Indiranagar Flagship', Revenue: 910000},
{store: 'Koramangala 80ft', Revenue: 520000},
{store: 'Jayanagar 4th Block', Revenue: 400000},
{store: 'Whitefield Main', Revenue: 330000},
];
export function getTopProductsData(storeId = 'all', range = '7d'): TopProductRow[] {
const {totalMult} = getMult(storeId, range);
return [
{id: 'tp1', name: 'Signature Espresso Blend (1kg)', category: 'Beverage & Beans', unitsSold: Math.round(482 * totalMult), revenue: `₹${(144600 * totalMult).toLocaleString('en-IN', {maximumFractionDigits: 0})}`, growth: '+18.4%', growthDirection: 'up', stockCount: 142, stockStatus: 'in_stock'},
{id: 'tp2', name: 'Artisanal Cold Brew Pack (6x)', category: 'Ready to Drink', unitsSold: Math.round(310 * totalMult), revenue: `₹${(93000 * totalMult).toLocaleString('en-IN', {maximumFractionDigits: 0})}`, growth: '+12.1%', growthDirection: 'up', stockCount: 18, stockStatus: 'low_stock'},
{id: 'tp3', name: 'Ceramic Barista Mug (Matte Black)', category: 'Merchandise', unitsSold: Math.round(215 * totalMult), revenue: `₹${(75250 * totalMult).toLocaleString('en-IN', {maximumFractionDigits: 0})}`, growth: '+8.9%', growthDirection: 'up', stockCount: 8, stockStatus: 'low_stock'},
{id: 'tp4', name: 'Vanilla Bean Syrup (750ml)', category: 'Syrups', unitsSold: Math.round(184 * totalMult), revenue: `₹${(46000 * totalMult).toLocaleString('en-IN', {maximumFractionDigits: 0})}`, growth: '+5.4%', growthDirection: 'up', stockCount: 85, stockStatus: 'in_stock'},
{id: 'tp5', name: 'French Press Brewer (8-Cup)', category: 'Equipment', unitsSold: Math.round(94 * totalMult), revenue: `₹${(37600 * totalMult).toLocaleString('en-IN', {maximumFractionDigits: 0})}`, growth: '-2.1%', growthDirection: 'down', stockCount: 0, stockStatus: 'out_of_stock'},
];
}
export const BUSINESS_ALERTS: BusinessAlert[] = [
{
id: 'ba1',
type: 'low_stock',
title: 'Low Stock Reorder Alert',
description: 'Ceramic Barista Mug has only 8 units left at Koramangala 80ft.',
actionText: 'Reorder Stock',
severity: 'critical',
},
{
id: 'ba2',
type: 'refund_spike',
title: 'Refund Spike Detected',
description: 'Whitefield store processed 4 return requests in the last 2 hours.',
actionText: 'Review Return Audit',
severity: 'warning',
},
{
id: 'ba3',
type: 'best_seller',
title: 'Best Seller Momentum',
description: 'Signature Espresso Blend sales spiked +18.4% above weekly baseline.',
actionText: 'Promote Banner',
severity: 'success',
},
{
id: 'ba4',
type: 'promotion_opportunity',
title: 'Weekend Upsell Opportunity',
description: 'Combine Cold Brew Pack with Syrups for a +₹250 AOV expansion.',
actionText: 'Launch Campaign',
severity: 'info',
},
];