bchen-reimbursement

This commit is contained in:
Brandan Chen 2022-12-12 20:14:37 -05:00
parent 5fc8b822a1
commit 40fc02bb6b

View file

@ -109,28 +109,44 @@ class Project(db.Model):
- dict mapping each member to its balance - dict mapping each member to its balance
- dict mapping each member to how much he/she should pay others - dict mapping each member to how much he/she she should pay others
(i.e. how much he/she benefited from bills) (i.e. how much he/she benefited from bills)
- dict mapping each member to how much he/she should be paid by - dict mapping each member to how much he/she should be paid by
others (i.e. how much he/she has paid for bills) others (i.e. how much he/she has paid for bills)
balance spent paid
""" """
balances, should_pay, should_receive = (defaultdict(int) for time in (1, 2, 3)) balances, should_pay, should_receive = (defaultdict(int) for time in (1, 2, 3))
# for bill in self.get_bills_unordered().all():
# should_receive[bill.payer.id] += bill.converted_amount
# total_weight = sum(ower.weight for ower in bill.owers)
# for ower in bill.owers:
# should_pay[ower.id] += (
# ower.weight * bill.converted_amount / total_weight
# )
for bill in self.get_bills_unordered().all(): for bill in self.get_bills_unordered().all():
should_receive[bill.payer.id] += bill.converted_amount
total_weight = sum(ower.weight for ower in bill.owers) total_weight = sum(ower.weight for ower in bill.owers)
for ower in bill.owers:
should_pay[ower.id] += ( if bill.bill_type == "Expense":
ower.weight * bill.converted_amount / total_weight should_receive[bill.payer.id] += bill.converted_amount
) for ower in bill.owers:
should_pay[ower.id] += (ower.weight * bill.converted_amount / total_weight)
if bill.bill_type == "Reimbursement":
should_receive[bill.payer.id] += bill.converted_amount
for ower in bill.owers:
should_receive[ower.id] -= bill.converted_amount
for person in self.members: for person in self.members:
balance = should_receive[person.id] - should_pay[person.id] balance = should_receive[person.id] - should_pay[person.id]
balances[person.id] = balance balances[person.id] = balance
return balances, should_pay, should_receive return (
balances,
should_pay,
should_receive,
)
@property @property
def balance(self): def balance(self):
@ -163,7 +179,8 @@ class Project(db.Model):
""" """
monthly = defaultdict(lambda: defaultdict(float)) monthly = defaultdict(lambda: defaultdict(float))
for bill in self.get_bills_unordered().all(): for bill in self.get_bills_unordered().all():
monthly[bill.date.year][bill.date.month] += bill.converted_amount if bill.bill_type == "Expense":
monthly[bill.date.year][bill.date.month] += bill.converted_amount
return monthly return monthly
@property @property