def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) group_account_id = kwargs['groupaccount_id'] group = GroupAccount.objects.get(id=group_account_id) group_users = UserProfile.objects.filter(group_accounts=group.id) if UserProfile.objects.get(user=self.request.user) not in group_users: return context for user in group_users: user.balance = UserProfile.get_balance(group.id, user.id) user.n_trans_buyer = Transaction.objects.filter( buyer=user, group_account=group).count() user.n_trans_consumer = Transaction.objects.filter( consumers=user, group_account=group).count() amount__sum = Transaction.get_buyer_transactions(user.id).filter( group_account=group).aggregate(Sum('amount'))['amount__sum'] if amount__sum: user.total_bought = float(amount__sum) else: user.total_bought = 0.0 consumer_transactions = Transaction.get_consumer_transactions( user.id).filter(group_account=group) total_consumed = 0.0 for transaction in consumer_transactions: total_consumed += transaction.amount_per_person user.total_consumed = total_consumed total_consumed = 0.0 total_bought = 0.0 total_balance = 0.0 total_shares = 0 total_shared_with = 0 for user in group_users: total_consumed += user.total_consumed total_bought += user.total_bought total_balance += user.balance total_shares += user.n_trans_buyer total_shared_with += user.n_trans_consumer context['users'] = group_users context['group_name'] = group.name context['total_consumed'] = total_consumed context['total_bought'] = total_bought context['total_balance'] = total_balance context['total_shares'] = total_shares context['total_shared_with'] = total_shared_with return context
def get_balance(group_account_id, user_profile_id): from care.transaction.models import Transaction from care.transaction.models import TransactionReal buyer_transactions = Transaction.objects.filter( group_account__id=group_account_id, buyer__id=user_profile_id ) consumer_transactions = Transaction.get_consumer_transactions( user_profile_id, filters={'group_account_id': group_account_id} ) sender_real_transactions = TransactionReal.get_transactions_real_sent( user_profile_id, {'group_account__id': group_account_id}) receiver_real_transactions = TransactionReal.get_transactions_real_received( user_profile_id, {'group_account_id': group_account_id}) total_bought = 0.0 total_consumed = 0.0 total_sent = 0.0 total_received = 0.0 amount__sum = buyer_transactions.aggregate(Sum('amount'))['amount__sum'] if amount__sum: total_bought = float(amount__sum) amount__sum = sender_real_transactions.aggregate(Sum('amount'))['amount__sum'] if amount__sum: total_sent = float(amount__sum) amount__sum = receiver_real_transactions.aggregate(Sum('amount'))['amount__sum'] if amount__sum: total_received = float(amount__sum) amount_per_person__sum = ( consumer_transactions.aggregate(Sum('amount_per_person_float')) ['amount_per_person_float__sum'] ) # This is a negative number if amount_per_person__sum: total_consumed = - float(amount_per_person__sum) balance = (total_bought + total_sent - total_consumed - total_received) return balance
def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) group_account_id = kwargs['groupaccount_id'] group = GroupAccount.objects.get(id=group_account_id) group_users = UserProfile.objects.filter(group_accounts=group.id) if UserProfile.objects.get(user=self.request.user) not in group_users: return context for user in group_users: user.balance = UserProfile.get_balance(group.id, user.id) user.n_trans_buyer = Transaction.objects.filter(buyer=user, group_account=group).count() user.n_trans_consumer = Transaction.objects.filter(consumers=user, group_account=group).count() amount__sum = Transaction.get_buyer_transactions(user.id).filter(group_account=group).aggregate(Sum('amount'))['amount__sum'] if amount__sum: user.total_bought = float(amount__sum) else: user.total_bought = 0.0 consumer_transactions = Transaction.get_consumer_transactions(user.id).filter(group_account=group) total_consumed = 0.0 for transaction in consumer_transactions: total_consumed += transaction.amount_per_person user.total_consumed = total_consumed total_consumed = 0.0 total_bought = 0.0 total_balance = 0.0 total_shares = 0 total_shared_with = 0 for user in group_users: total_consumed += user.total_consumed total_bought += user.total_bought total_balance += user.balance total_shares += user.n_trans_buyer total_shared_with += user.n_trans_consumer context['users'] = group_users context['group_name'] = group.name context['total_consumed'] = total_consumed context['total_bought'] = total_bought context['total_balance'] = total_balance context['total_shares'] = total_shares context['total_shared_with'] = total_shared_with return context