Skip to content

Commit

Permalink
Correct calculate_transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
nagua committed Sep 14, 2019
1 parent c5cbf37 commit b716256
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
5 changes: 3 additions & 2 deletions abrechnung/group.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# vim: tabstop=2 expandtab shiftwidth=2 softtabstop=2
import random, copy, time
import transaction as trans
import utils as u
import operator

class Group:
Expand Down Expand Up @@ -141,12 +142,12 @@ def calculate_balancing(self):
if pos.balance != 0:
if pos.balance >= -neg.balance:
#There is enough credit on the pos so neg has to transfer all to him
transaction_list.append(trans.Transaction(str(-neg.balance), neg.name, pos.name))
transaction_list.append(trans.Transaction(u.amount_to_string(-neg.balance), neg.name, pos.name))
pos.balance += neg.balance
neg.balance = 0
else:
#There is not enough credit on pos so neg has to transfer a part to him
transaction_list.append(trans.Transaction(str(pos.balance), neg.name, pos.name))
transaction_list.append(trans.Transaction(u.amount_to_string(pos.balance), neg.name, pos.name))
neg.balance += pos.balance
pos.balance = 0

Expand Down
16 changes: 16 additions & 0 deletions abrechnung/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,22 @@ def levenshtein(s1, s2):

return previous_row[-1]


def amount_to_string(amount):
negative = amount < 0
if negative:
amount *= -1

fractional_part = amount % 100
integer_part = (amount - fractional_part) // 100

ret = "{}.{:02d}".format(integer_part, fractional_part)
if negative:
ret = "-" + ret

return ret


def parse_amount(s):
try:
return try_parse_with_delim(s, '.')
Expand Down

0 comments on commit b716256

Please sign in to comment.