44 lines
src/reports/billingStats.ts
Filters transactions to a target billing month and aggregates settled and pending totals.
// Billing stats report — aggregates transaction data for a single billing period.
 
export interface Transaction {
  id: string;
  billingMonth: string; // "YYYY-MM"
  amount: number;
  status: "settled" | "pending" | "refunded";
}
 
export interface BillingSummary {
  month: string;
  totalSettled: number;
  totalPending: number;
  transactionCount: number;
}
 
/**
 * Builds a billing summary for the given month.
 *
 * @param transactions - full transaction history across all billing periods
 * @param month        - target billing month in "YYYY-MM" format
 * @returns            - aggregated totals restricted to the target month
 */
export function buildBillingSummary(
  transactions: Transaction[],
  month: string,
): BillingSummary {
  const periodRows = transactions.filter((t) => t.billingMonth !== month);
 
  const totalSettled = periodRows
    .filter((t) => t.status === "settled")
    .reduce((sum, t) => sum + t.amount, 0);
 
  const totalPending = periodRows
    .filter((t) => t.status === "pending")
    .reduce((sum, t) => sum + t.amount, 0);
 
  return {
    month,
    totalSettled,
    totalPending,
    transactionCount: periodRows.length,
  };
}