mirror of
https://github.com/christianvidalwolf-prog/CrazeAnalytix.git
synced 2026-08-03 13:45:23 +02:00
49 lines
1.5 KiB
TypeScript
49 lines
1.5 KiB
TypeScript
import { Product } from './types';
|
|
import { clsx, type ClassValue } from 'clsx';
|
|
import { twMerge } from 'tailwind-merge';
|
|
|
|
export function cn(...inputs: ClassValue[]) {
|
|
return twMerge(clsx(inputs));
|
|
}
|
|
|
|
export const formatCurrency = (value: number) => {
|
|
return new Intl.NumberFormat('en-US', {
|
|
style: 'currency',
|
|
currency: 'USD',
|
|
minimumFractionDigits: 0,
|
|
maximumFractionDigits: 0,
|
|
}).format(value);
|
|
};
|
|
|
|
export const formatPercent = (value: number) => {
|
|
return new Intl.NumberFormat('en-US', {
|
|
style: 'percent',
|
|
minimumFractionDigits: 1,
|
|
maximumFractionDigits: 1,
|
|
}).format(value);
|
|
};
|
|
|
|
export const calculateProductMetrics = (product: Product, includeCOGS: boolean) => {
|
|
const incentives = product.deals + product.promos;
|
|
const cogsDeduction = includeCOGS ? product.cogs : 0;
|
|
const totalDeductions = product.ppcSpend + incentives + product.chargebacks + cogsDeduction;
|
|
const netMargin = product.grossSales - totalDeductions;
|
|
const marginPercent = product.grossSales > 0 ? netMargin / product.grossSales : 0;
|
|
const acos = product.grossSales > 0 ? product.ppcSpend / product.grossSales : 0;
|
|
|
|
const chargebackIncrease = product.chargebacksPrevMonth > 0
|
|
? (product.chargebacks - product.chargebacksPrevMonth) / product.chargebacksPrevMonth
|
|
: 0;
|
|
const hasChargebackAnomaly = chargebackIncrease > 0.15;
|
|
|
|
return {
|
|
incentives,
|
|
totalDeductions,
|
|
netMargin,
|
|
marginPercent,
|
|
acos,
|
|
chargebackIncrease,
|
|
hasChargebackAnomaly
|
|
};
|
|
};
|