mirror of
https://github.com/spliit-app/spliit.git
synced 2026-03-06 04:26:13 +01:00
Update balances based on shares
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
import { getGroupExpenses } from '@/lib/api'
|
import { getGroupExpenses } from '@/lib/api'
|
||||||
import { Participant } from '@prisma/client'
|
import { Participant } from '@prisma/client'
|
||||||
|
import { match } from 'ts-pattern'
|
||||||
|
|
||||||
export type Balances = Record<
|
export type Balances = Record<
|
||||||
Participant['id'],
|
Participant['id'],
|
||||||
@@ -19,34 +20,42 @@ export function getBalances(
|
|||||||
|
|
||||||
for (const expense of expenses) {
|
for (const expense of expenses) {
|
||||||
const paidBy = expense.paidById
|
const paidBy = expense.paidById
|
||||||
const paidFors = expense.paidFor.map((p) => p.participantId)
|
const paidFors = expense.paidFor
|
||||||
|
|
||||||
if (!balances[paidBy]) balances[paidBy] = { paid: 0, paidFor: 0, total: 0 }
|
if (!balances[paidBy]) balances[paidBy] = { paid: 0, paidFor: 0, total: 0 }
|
||||||
balances[paidBy].paid += expense.amount
|
balances[paidBy].paid += expense.amount
|
||||||
balances[paidBy].total += expense.amount
|
balances[paidBy].total += expense.amount
|
||||||
paidFors.forEach((paidFor, index) => {
|
|
||||||
if (!balances[paidFor])
|
|
||||||
balances[paidFor] = { paid: 0, paidFor: 0, total: 0 }
|
|
||||||
|
|
||||||
const dividedAmount = divide(
|
const totalPaidForShares = paidFors.reduce(
|
||||||
expense.amount,
|
(sum, paidFor) => sum + paidFor.shares,
|
||||||
paidFors.length,
|
0,
|
||||||
index === paidFors.length - 1,
|
)
|
||||||
)
|
let remaining = expense.amount
|
||||||
balances[paidFor].paidFor += dividedAmount
|
paidFors.forEach((paidFor, index) => {
|
||||||
balances[paidFor].total -= dividedAmount
|
if (!balances[paidFor.participantId])
|
||||||
|
balances[paidFor.participantId] = { paid: 0, paidFor: 0, total: 0 }
|
||||||
|
|
||||||
|
const isLast = index === paidFors.length - 1
|
||||||
|
|
||||||
|
const [shares, totalShares] = match(expense.splitMode)
|
||||||
|
.with('EVENLY', () => [1, paidFors.length])
|
||||||
|
.with('BY_SHARES', () => [paidFor.shares, totalPaidForShares])
|
||||||
|
.with('BY_PERCENTAGE', () => [paidFor.shares, totalPaidForShares])
|
||||||
|
.with('BY_AMOUNT', () => [paidFor.shares, totalPaidForShares])
|
||||||
|
.exhaustive()
|
||||||
|
|
||||||
|
const dividedAmount = isLast
|
||||||
|
? remaining
|
||||||
|
: Math.floor((expense.amount * shares) / totalShares)
|
||||||
|
remaining -= dividedAmount
|
||||||
|
balances[paidFor.participantId].paidFor += dividedAmount
|
||||||
|
balances[paidFor.participantId].total -= dividedAmount
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
return balances
|
return balances
|
||||||
}
|
}
|
||||||
|
|
||||||
function divide(total: number, count: number, isLast: boolean): number {
|
|
||||||
if (!isLast) return Math.floor(total / count)
|
|
||||||
|
|
||||||
return total - divide(total, count, false) * (count - 1)
|
|
||||||
}
|
|
||||||
|
|
||||||
export function getSuggestedReimbursements(
|
export function getSuggestedReimbursements(
|
||||||
balances: Balances,
|
balances: Balances,
|
||||||
): Reimbursement[] {
|
): Reimbursement[] {
|
||||||
|
|||||||
Reference in New Issue
Block a user