diff --git a/ihatemoney/models.py b/ihatemoney/models.py index 4be1fae8..19d6f879 100644 --- a/ihatemoney/models.py +++ b/ihatemoney/models.py @@ -109,28 +109,44 @@ class Project(db.Model): - 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) - dict mapping each member to how much he/she should be paid by 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)) - + # 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(): - 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 - ) + + if bill.bill_type == "Expense": + 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: balance = should_receive[person.id] - should_pay[person.id] balances[person.id] = balance - return balances, should_pay, should_receive + return ( + balances, + should_pay, + should_receive, + ) @property def balance(self): @@ -163,7 +179,8 @@ class Project(db.Model): """ monthly = defaultdict(lambda: defaultdict(float)) 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 @property