-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.py
198 lines (151 loc) · 5.35 KB
/
util.py
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
from typing import List
from model import Ledger
from schema import CreateTransaction, UserSplit
def convert_percentage_to_amount(
transaction_split: CreateTransaction,
) -> CreateTransaction:
total_amount = transaction_split.total_amount
from_users = convert_uneven_percentage_to_amount(
transaction_split.from_users, total_amount
)
to_users = convert_uneven_percentage_to_amount(
transaction_split.to_users, total_amount
)
return CreateTransaction(
description=transaction_split.description,
total_amount=total_amount,
split_type="uneven",
computation_type="amount",
from_users=from_users,
to_users=to_users,
)
def compute_transaction_to_ledger(
transaction_split: CreateTransaction,
) -> List[Ledger]:
ledger_entries = []
from_users = list(transaction_split.from_users)
to_users = list(transaction_split.to_users)
while from_users and to_users:
from_user = from_users[0]
from_amount = from_user.value
to_user = to_users[0]
to_amount = to_user.value
if from_amount >= to_amount:
ledger_amount = to_amount
else:
ledger_amount = from_amount
from_amount -= ledger_amount
to_amount -= ledger_amount
ledger_entry = Ledger(
from_user_id=from_user.user_id,
to_user_id=to_user.user_id,
amount=ledger_amount,
)
ledger_entries.append(ledger_entry)
from_user.value = from_amount
to_user.value = to_amount
if from_amount == 0:
from_users.pop(0)
if to_amount == 0:
to_users.pop(0)
return ledger_entries
def convert_even_to_uneven_users(
users: List[UserSplit], total_amount: int
) -> List[UserSplit]:
num_users = len(users)
even_amount = total_amount // num_users
uneven_users = [
UserSplit(user_id=user.user_id, value=even_amount) for user in users
]
residual_amount = total_amount - even_amount * num_users
if residual_amount > 0:
uneven_users[-1].value += residual_amount
return uneven_users
def convert_uneven_percentage_to_amount(
users: List[UserSplit], total_amount: int
) -> List[UserSplit]:
uneven_users = []
for user in users:
proportion = user.value
amount = total_amount * proportion / 100
uneven_users.append(UserSplit(user_id=user.user_id, value=amount))
total_assigned_amount = sum(user.value for user in uneven_users)
residual_amount = total_amount - total_assigned_amount
if residual_amount > 0:
uneven_users[-1].value += residual_amount
return uneven_users
def convert_even_to_uneven(
transaction: CreateTransaction,
) -> CreateTransaction:
from_users = convert_even_to_uneven_users(
transaction.from_users, transaction.total_amount
)
to_users = convert_even_to_uneven_users(
transaction.to_users, transaction.total_amount
)
return CreateTransaction(
description=transaction.description,
total_amount=transaction.total_amount,
split_type="uneven",
computation_type=transaction.computation_type,
from_users=from_users,
to_users=to_users,
)
def convert_percentage_to_amount(
transaction_split: CreateTransaction,
) -> CreateTransaction:
total_amount = transaction_split.total_amount
from_users = convert_uneven_percentage_to_amount(
transaction_split.from_users, total_amount
)
to_users = convert_uneven_percentage_to_amount(
transaction_split.to_users, total_amount
)
return CreateTransaction(
description=transaction_split.description,
total_amount=total_amount,
split_type="uneven",
computation_type="amount",
from_users=from_users,
to_users=to_users,
)
def compute_transaction_to_ledger(
transaction_split: CreateTransaction,
) -> List[Ledger]:
ledger_entries = []
from_users = list(transaction_split.from_users)
to_users = list(transaction_split.to_users)
while from_users or to_users:
from_user = from_users[0]
from_amount = from_user.value
to_user = to_users[0]
to_amount = to_user.value
if from_amount >= to_amount:
ledger_amount = to_amount
else:
ledger_amount = from_amount
from_amount -= ledger_amount
to_amount -= ledger_amount
ledger_entry = Ledger(
from_user_id=from_user.user_id,
to_user_id=to_user.user_id,
amount=ledger_amount,
)
ledger_entries.append(ledger_entry)
from_user.value = from_amount
to_user.value = to_amount
if from_amount == 0:
from_users.pop(0)
if to_amount == 0:
to_users.pop(0)
return ledger_entries
def convert_transaction_to_ledger(transaction: CreateTransaction) -> List[Ledger]:
if transaction.split_type == "even":
return compute_transaction_to_ledger(convert_even_to_uneven(transaction))
elif transaction.split_type == "uneven":
if transaction.computation_type == "amount":
return compute_transaction_to_ledger(transaction)
elif transaction.computation_type == "percentage":
return compute_transaction_to_ledger(
convert_percentage_to_amount(transaction)
)