diff --git a/components/transactions/list.tsx b/components/transactions/list.tsx index 21dcea7..b5f4098 100644 --- a/components/transactions/list.tsx +++ b/components/transactions/list.tsx @@ -4,7 +4,7 @@ import { BulkActionsMenu } from "@/components/transactions/bulk-actions" import { Badge } from "@/components/ui/badge" import { Checkbox } from "@/components/ui/checkbox" import { Table, TableBody, TableCell, TableFooter, TableHead, TableHeader, TableRow } from "@/components/ui/table" -import { calcTotalPerCurrency, isTransactionIncomplete } from "@/lib/stats" +import { calcNetTotalPerCurrency, calcTotalPerCurrency, isTransactionIncomplete } from "@/lib/stats" import { cn, formatCurrency } from "@/lib/utils" import { Category, Field, Project, Transaction } from "@/prisma/client" import { formatDate } from "date-fns" @@ -112,14 +112,30 @@ export const standardFieldRenderers: Record = { ), footerValue: (transactions: Transaction[]) => { - const totalPerCurrency = calcTotalPerCurrency(transactions) + const netTotalPerCurrency = calcNetTotalPerCurrency(transactions) + const turnoverPerCurrency = calcTotalPerCurrency(transactions) + return ( -
- {Object.entries(totalPerCurrency).map(([currency, total]) => ( -
- {formatCurrency(total, currency)} -
- ))} +
+
+
Net Total
+ {Object.entries(netTotalPerCurrency).map(([currency, total]) => ( +
= 0 ? "text-green-600" : "text-red-600")} + > + {formatCurrency(total, currency)} +
+ ))} +
+
+
Turnover
+ {Object.entries(turnoverPerCurrency).map(([currency, total]) => ( +
+ {formatCurrency(total, currency)} +
+ ))} +
) }, diff --git a/lib/stats.ts b/lib/stats.ts index 74ed6d6..c43d8af 100644 --- a/lib/stats.ts +++ b/lib/stats.ts @@ -16,6 +16,32 @@ export function calcTotalPerCurrency(transactions: Transaction[]): Record { + return transactions.reduce( + (acc, transaction) => { + let amount = 0 + let currency: string | undefined + if ( + transaction.convertedTotal !== null && + transaction.convertedTotal !== undefined && + transaction.convertedCurrencyCode + ) { + amount = transaction.convertedTotal + currency = transaction.convertedCurrencyCode.toUpperCase() + } else if (transaction.total !== null && transaction.total !== undefined && transaction.currencyCode) { + amount = transaction.total + currency = transaction.currencyCode.toUpperCase() + } + if (currency && amount !== 0) { + const sign = transaction.type === "expense" ? -1 : 1 + acc[currency] = (acc[currency] || 0) + amount * sign + } + return acc + }, + {} as Record + ) +} + export const isTransactionIncomplete = (fields: Field[], transaction: Transaction): boolean => { const incompleteFields = incompleteTransactionFields(fields, transaction)