def test_transactions_change_balance(self): p1 = Account.objects.get(user__username="******") e = Event.objects.get(name="test") ######################################### Transfer.objects.create(account=p1, debit=100) Transfer.objects.create(account=p1, debit=300) Transfer.objects.create(account=p1, debit=400) p = Participation(account=p1, event=e) p.active = True p.save() Transaction.objects.create(participation=p, credit=200) Transaction.objects.create(participation=p, debit=300) ######################################### self.assertEqual(p1.balance(), 900)
def add_participants(event, newbies): """Add participants in event. Takes dict, where keys - is account models and values is participation part(int).""" if len(newbies) == 0: return # calc party-pay, participants = Participation.objects.filter(event=event, active=True) exist_parts = participants.aggregate(s=Sum('parts'))['s'] exist_parts = 0.0 if exist_parts is None else exist_parts # fix None all_parts = exist_parts + sum(newbies.values()) party_pay = event.price / all_parts recalcers = participants.filter(~Q(account__in=newbies.keys())) parent_transactions = [] # participate incomers for (acc, parts) in newbies.items(): # if not already participated participation = Participation.objects.filter(account=acc, event=event, active=False) if len(participation) == 0: participation = Participation(account=acc, parts=parts, event=event) else: participation = participation[0] participation.active = True participation.save() tr = Transaction(participation=participation, type=Transaction.PARTICIPATE) tr.credit = party_pay * parts parent_transactions.append(tr) tr.save() # create diffs for old participants # if no recalcers(incomers if first participants) we have exist_parts = 0 for newbie_transaction in parent_transactions: for participation in recalcers: assert (exist_parts != 0),\ "On add participants when we need recalc exist participants\ exist_parts should be positive(not zero)" debit = ((newbie_transaction.credit / exist_parts) * participation.parts) return_money(participation, debit, newbie_transaction)
def add_participants(event, newbies): """Add participants in event. Takes dict, where keys - is account models and values is participation part(int).""" # calc party-pay, participants = Participation.objects.filter(event=event, active=True) exist_parts = participants.aggregate(s=Sum('parts'))['s'] exist_parts = 0.0 if exist_parts is None else exist_parts # fix None all_parts = exist_parts + sum(newbies.values()) party_pay = event.price / all_parts recalcers = participants.filter(~Q(account__in=newbies.keys())) parent_transactions = [] # participate incomers for (acc, parts) in newbies.items(): # if not already participated participation = Participation.objects.filter(account=acc, active=False) if len(participation) == 0: participation = Participation(account=acc, parts=parts, event=event) else: participation = participation[0] participation.active = True participation.save() tr = Transaction(participation=participation, type=Transaction.PARTICIPATE) tr.credit = party_pay * parts parent_transactions.append(tr) tr.save() # create diffs for old participants # if no recalcers(incomers if first participants) we have exist_parts = 0 for newbie_transaction in parent_transactions: for participation in recalcers: assert (exist_parts != 0),\ "On add participants when we need recalc exist participants\ exist_parts should be positive(not zero)" debit = ((newbie_transaction.credit / exist_parts) * participation.parts) return_money(participation, debit, newbie_transaction)