forked from StellarSplit/StellarSplit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplit-calculations.ts
More file actions
211 lines (187 loc) · 5.02 KB
/
Copy pathsplit-calculations.ts
File metadata and controls
211 lines (187 loc) · 5.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import type { SplitMethod, WizardState } from '../types/wizard'
export interface WizardParticipantShare {
participantId: string
subtotal: number
tax: number
tip: number
total: number
}
export interface WizardSplitCalculation {
shares: WizardParticipantShare[]
grandTotal: number
}
function round(value: number): number {
return Math.round(value * 100) / 100
}
function distributeTax(
participantSubtotal: number,
totalSubtotal: number,
totalTax: number,
): number {
if (totalTax === 0 || totalSubtotal === 0) {
return 0
}
return round(totalTax * (participantSubtotal / totalSubtotal))
}
function distributeTip(
participantSubtotal: number,
totalSubtotal: number,
totalTip: number,
splitMethod: SplitMethod,
participantCount: number,
): number {
if (totalTip === 0) {
return 0
}
if (splitMethod === 'equal') {
return round(totalTip / participantCount)
}
if (totalSubtotal === 0) {
return round(totalTip / participantCount)
}
return round(totalTip * (participantSubtotal / totalSubtotal))
}
function applyRoundingAdjustment(
shares: WizardParticipantShare[],
expectedTotal: number,
): WizardParticipantShare[] {
if (shares.length === 0) {
return shares
}
const calculatedTotal = round(
shares.reduce((sum, share) => sum + share.total, 0),
)
const adjustment = round(expectedTotal - calculatedTotal)
if (Math.abs(adjustment) < 0.01) {
return shares
}
const targetIndex = shares.reduce(
(largestIndex, currentShare, currentIndex, allShares) =>
currentShare.total > allShares[largestIndex].total ? currentIndex : largestIndex,
0,
)
const nextShares = [...shares]
nextShares[targetIndex] = {
...nextShares[targetIndex],
total: round(nextShares[targetIndex].total + adjustment),
}
return nextShares
}
function buildShare(
participantId: string,
subtotal: number,
totalSubtotal: number,
totalTax: number,
totalTip: number,
splitMethod: SplitMethod,
participantCount: number,
): WizardParticipantShare {
const roundedSubtotal = round(subtotal)
const tax = distributeTax(roundedSubtotal, totalSubtotal, totalTax)
const tip = distributeTip(
roundedSubtotal,
totalSubtotal,
totalTip,
splitMethod,
participantCount,
)
return {
participantId,
subtotal: roundedSubtotal,
tax,
tip,
total: round(roundedSubtotal + tax + tip),
}
}
export function calculateWizardSplit(
wizardState: Pick<
WizardState,
'splitMethod' | 'participants' | 'items' | 'taxAmount' | 'tipAmount' | 'totalAmount'
>,
): WizardSplitCalculation {
const participantIds = wizardState.participants.map((participant) => participant.id)
const participantCount = participantIds.length
const totalSubtotal = wizardState.totalAmount
const totalTax = wizardState.taxAmount
const totalTip = wizardState.tipAmount
const expectedTotal = round(totalSubtotal + totalTax + totalTip)
if (participantCount === 0) {
return {
shares: [],
grandTotal: expectedTotal,
}
}
let shares: WizardParticipantShare[] = []
switch (wizardState.splitMethod) {
case 'equal':
shares = participantIds.map((participantId) =>
buildShare(
participantId,
totalSubtotal / participantCount,
totalSubtotal,
totalTax,
totalTip,
wizardState.splitMethod,
participantCount,
),
)
break
case 'percentage':
shares = wizardState.participants.map((participant) =>
buildShare(
participant.id,
(totalSubtotal * (participant.percentage ?? 0)) / 100,
totalSubtotal,
totalTax,
totalTip,
wizardState.splitMethod,
participantCount,
),
)
break
case 'custom':
shares = wizardState.participants.map((participant) =>
buildShare(
participant.id,
participant.customAmount ?? 0,
totalSubtotal,
totalTax,
totalTip,
wizardState.splitMethod,
participantCount,
),
)
break
case 'itemized': {
const subtotals = new Map<string, number>()
participantIds.forEach((participantId) => subtotals.set(participantId, 0))
wizardState.items.forEach((item) => {
const assignedIds =
item.assignedTo.length > 0 ? item.assignedTo : participantIds
const sharePerPerson = item.price / Math.max(assignedIds.length, 1)
assignedIds.forEach((participantId) => {
subtotals.set(
participantId,
(subtotals.get(participantId) ?? 0) + sharePerPerson,
)
})
})
shares = participantIds.map((participantId) =>
buildShare(
participantId,
subtotals.get(participantId) ?? 0,
totalSubtotal,
totalTax,
totalTip,
wizardState.splitMethod,
participantCount,
),
)
break
}
}
return {
shares: applyRoundingAdjustment(shares, expectedTotal),
grandTotal: expectedTotal,
}
}