Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nicoll/include balance transactions for reports #1761

Merged
merged 18 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 1 addition & 17 deletions apps/epic-web/src/components/calculations/calculate-splits.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,4 @@
type ProductGroup = {
productName: string
productId: string
count: number
amount: number
net: number
fee: number
refunded: number
}

type Totals = {
totalGross: number
totalRefunded: number
totalNet: number
totalFee: number
productGroups: Record<string, ProductGroup>
}
import {Totals, ProductGroup} from './calculate-totals'
joelhooks marked this conversation as resolved.
Show resolved Hide resolved

type SplitData = {
percent: number
Expand Down
99 changes: 57 additions & 42 deletions apps/epic-web/src/components/calculations/calculate-totals.ts
Original file line number Diff line number Diff line change
@@ -1,56 +1,62 @@
import {SimplifiedCharge, SimplifiedRefund} from 'lib/transactions'
import {CombinedBalanceTransaction} from 'lib/transactions'

type ProductGroup = {
export type ProductGroup = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it might make sense to move these types up, maybe there's a "lib" here so we have an "sdk" for report building logic?

productName: string
productId: string
count: number
amount: number
gross: number
net: number
fee: number
refunded: number
refundCount: number
}

type Totals = {
export type Totals = {
totalGross: number
totalRefunded: number
totalNet: number
totalFee: number
totalCount: number
productGroups: Record<string, ProductGroup>
}

type RefundTotals = {
totalRefundAmount: number
refundCount: number
refundsByProduct: Record<
string,
{
count: number
amount: number
}
>
refundsByProduct: Record<string, {count: number; amount: number}>
}

export function calculateTotals(
allCharges: SimplifiedCharge[],
allRefunds: SimplifiedRefund[],
allBalanceTransactions: CombinedBalanceTransaction[],
) {
const charges = allBalanceTransactions.filter(
(transaction) =>
transaction.type === 'charge' || transaction.type === 'payment',
)
const refunds = allBalanceTransactions.filter(
(transaction) =>
transaction.type === 'refund' || transaction.type === 'payment_refund',
)

const refundTotals: RefundTotals = {
totalRefundAmount: allRefunds.reduce(
(sum, refund) => sum + refund.amount,
totalRefundAmount: refunds.reduce(
(sum, refund) => sum + Math.abs(refund.amount),
0,
),
refundCount: allRefunds.length,
refundsByProduct: allRefunds.reduce((acc, refund) => {
if (!acc[refund.product]) {
acc[refund.product] = {count: 0, amount: 0}
refundCount: refunds.length,
refundsByProduct: refunds.reduce((acc, refund) => {
const product = refund.product || 'Unknown Product'
if (!acc[product]) {
acc[product] = {count: 0, amount: 0}
}
acc[refund.product].count++
acc[refund.product].amount += refund.amount
acc[product].count++
acc[product].amount += Math.abs(refund.amount)
return acc
}, {} as Record<string, {count: number; amount: number}>),
}

const productGroups = allCharges.reduce((groups, charge) => {
const productGroups = charges.reduce((groups, charge) => {
const product = charge.product || 'Unknown Product'
const productId = charge.productId || 'unknown-id'
if (!groups[product]) {
Expand All @@ -59,50 +65,53 @@ export function calculateTotals(
productId: productId,
count: 0,
amount: 0,
gross: 0,
net: 0,
fee: 0,
refunded: 0,
refundCount: 0,
}
}
groups[product].count++
groups[product].amount += charge.amount
groups[product].gross += charge.amount
groups[product].net += charge.net
groups[product].fee += charge.fee
return groups
}, {} as Record<string, ProductGroup>)

// Add products that only have refunds to the productGroups
// Apply refunds to product groups
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

comment like this might suggest this could be a nice named function

Object.entries(refundTotals.refundsByProduct).forEach(
([product, {amount}]) => {
if (!productGroups[product]) {
([product, {amount, count}]) => {
if (productGroups[product]) {
productGroups[product].refunded += amount
productGroups[product].gross -= amount
productGroups[product].amount -= amount
productGroups[product].net -= amount
productGroups[product].refundCount += count
} else {
// If a product only has refunds, create a new entry
productGroups[product] = {
productName: product,
productId: 'unknown-id',
count: 0,
amount: 0,
net: 0,
amount: -amount,
gross: -amount,
net: -amount,
fee: 0,
refunded: 0,
refunded: amount,
refundCount: count,
}
}
},
)

// Apply refunds to all products
Object.entries(refundTotals.refundsByProduct).forEach(
([product, {amount}]) => {
if (productGroups[product]) {
productGroups[product].refunded += amount
}
},
)

// Calculate net and adjust gross for each product
Object.values(productGroups).forEach((group) => {
group.amount -= group.refunded
group.net = group.amount - group.fee
})

// Calculate totals
const totalGross = Object.values(productGroups).reduce(
(sum, group) => sum + group.gross,
0,
)
const totalAmount = Object.values(productGroups).reduce(
(sum, group) => sum + group.amount,
0,
)
Expand All @@ -114,13 +123,19 @@ export function calculateTotals(
(sum, group) => sum + group.net,
0,
)
const totalCount = Object.values(productGroups).reduce(
(sum, group) => sum + group.count - group.refundCount,
0,
)

return {
totals: {
totalGross,
totalAmount,
totalRefunded: refundTotals.totalRefundAmount,
totalNet,
totalFee,
totalCount,
productGroups,
},
refundTotals,
Expand Down
Loading
Loading