def calculate_total(queryset, target='EUR'): totals = queryset.values('donation__amount_currency').annotate( total=Sum('donation__amount')) amounts = [ Money(tot['total'], tot['donation__amount_currency']) for tot in totals ] amounts = [convert(amount, target) for amount in amounts] return sum(amounts) or Money(0, target)
def calculate_total_raised(self): """ Calculate the total amount raised by projects """ totals = Donation.objects.filter( order__status__in=['success', 'pending'] ).values( 'amount_currency' ).annotate(total=Sum('amount')).order_by('-amount') amounts = [Money(total['total'], total['amount_currency']) for total in totals] amounts = [convert(amount, properties.DEFAULT_CURRENCY) for amount in amounts] return sum(amounts) or Money(0, properties.DEFAULT_CURRENCY)
def amount_raised(self): """ The sum of amount donated + amount matching """ if self.target: currency = self.target.currency else: currency = 'EUR' total = self.amount_donated if self.amount_matching: total += convert(self.amount_matching, currency) return total
def amount_donated(self): donations = self.donation_set.filter( order__status__in=[StatusDefinition.SUCCESS, StatusDefinition.PENDING, StatusDefinition.PLEDGED]) totals = [ Money(data['amount__sum'], data['amount_currency']) for data in donations.values('amount_currency').annotate(Sum('amount')).order_by() ] totals = [convert(amount, self.amount.currency) for amount in totals] return sum(totals) or Money(0, self.amount.currency)
def amount_donated(self): donations = self.donation_set.filter(order__status__in=[ StatusDefinition.SUCCESS, StatusDefinition.PENDING, StatusDefinition.PLEDGED ]) totals = [ Money(data['amount__sum'], data['amount_currency']) for data in donations.values('amount_currency').annotate( Sum('amount')).order_by() ] totals = [convert(amount, self.amount.currency) for amount in totals] return sum(totals) or Money(0, self.amount.currency)
def amount_donated(self): from .states import DonationStateMachine donations = self.donations.filter(status__in=[ DonationStateMachine.succeeded.value, DonationStateMachine.activity_refunded.value, ]) totals = [ Money(data['amount__sum'], data['amount_currency']) for data in donations.values('amount_currency').annotate( Sum('amount')).order_by() ] totals = [convert(amount, self.amount.currency) for amount in totals] return sum(totals) or Money(0, self.amount.currency)
def get_results(self, *args, **kwargs): self.model_admin.change_list_template = 'utils/admin/total_amount_change_list.html' super(TotalAmountAdminChangeList, self).get_results(*args, **kwargs) total_column = self.model_admin.total_column or 'amount' currency_column = '{}_currency'.format(total_column) totals = self.queryset.values(currency_column).annotate( total=Sum(total_column)).order_by() amounts = [ Money(total['total'], total[currency_column]) for total in totals ] amounts = [ convert(amount, properties.DEFAULT_CURRENCY) for amount in amounts ] self.total = sum(amounts) or Money(0, properties.DEFAULT_CURRENCY)
def pledged_total(self): """ Total amount of pledged donations """ donations = Donation.objects.filter( self.date_filter('order__confirmed'), order__status='pledged') totals = donations.values('amount_currency').annotate( total=Sum('amount')) amounts = [ Money(total['total'], total['amount_currency']) for total in totals ] if totals: donated = sum([ convert(amount, properties.DEFAULT_CURRENCY) for amount in amounts ]) else: donated = Money(0, properties.DEFAULT_CURRENCY) return donated
def amount_matched(self): """ Total amount matched on realized (done and incomplete) projects """ totals = Project.objects.filter( self.date_filter('campaign_ended')).filter( amount_extra__gt=0).values('amount_extra_currency').annotate( total=Sum('amount_extra')) amounts = [ Money(total['total'], total['amount_extra_currency']) for total in totals ] if totals: return sum([ convert(amount, properties.DEFAULT_CURRENCY) for amount in amounts ]) else: return Money(0, properties.DEFAULT_CURRENCY)
def donated_total(self): """ Total amount donated to all activities""" donations = Donation.objects.filter( self.date_filter('contribution_date'), status='succeeded') totals = donations.order_by('amount_currency').values( 'amount_currency').annotate(total=Sum('amount')) amounts = [ Money(total['total'], total['amount_currency']) for total in totals ] if totals: donated = sum([ convert(amount, properties.DEFAULT_CURRENCY) for amount in amounts ]) else: donated = Money(0, properties.DEFAULT_CURRENCY) return donated
def stats(self): activities = self.activities.filter(status='succeeded') stats = [activity.stats for activity in activities] currency = properties.DEFAULT_CURRENCY return { 'activities': len(activities), 'contributions': sum(stat['count'] for stat in stats), 'hours': sum(stat['hours'] or 0 for stat in stats if 'hours' in stat), 'amount': sum( convert( Money(stat['amount']['amount'], stat['amount'] ['currency']), currency).amount for stat in stats if 'amount' in stat), }
def donated_total(self): """ Total amount donated to all projects""" donations = Donation.objects.filter( self.date_filter('order__created'), order__status__in=['pending', 'success', 'pledged']) totals = donations.values('amount_currency').annotate( total=Sum('amount')) amounts = [ Money(total['total'], total['amount_currency']) for total in totals ] if totals: donated = sum([ convert(amount, properties.DEFAULT_CURRENCY) for amount in amounts ]) else: donated = Money(0, properties.DEFAULT_CURRENCY) return donated
def donated_total(self): from bluebottle.donations.models import Donation """ Add all donation amounts for all donations ever """ if self._get_cached('donations-total'): return self._get_cached('donations-total') donations = Donation.objects.filter(order__status__in=( StatusDefinition.PENDING, StatusDefinition.SUCCESS)) totals = donations.values('amount_currency').annotate(total=Sum('amount')) amounts = [Money(total['total'], total['amount_currency']) for total in totals] if totals: donated = int(sum([convert(amount, properties.DEFAULT_CURRENCY) for amount in amounts]).amount) else: donated = 0 self._set_cached('donations-total', donated) return donated
def get_money_total(self, status_in=None): """ Calculate the total (realtime) amount of money for donations, optionally filtered by status. """ if not self.amount_asked: # No money asked, return 0 return Money(0, 'EUR') donations = self.donation_set if status_in: donations = donations.filter(order__status__in=status_in) totals = donations.values('amount_currency').annotate(total=Sum('amount')) amounts = [Money(total['total'], total['amount_currency']) for total in totals] amounts = [convert(amount, self.amount_asked.currency) for amount in amounts] return sum(amounts) or Money(0, self.amount_asked.currency)
def amount_matched(self): """ Total amount matched on realized (done and incomplete) activities """ totals = Funding.objects.filter( self.date_filter('transition_date'), status__in=['succeeded', 'open', 'partial']).filter(amount_matching__gt=0).values( 'amount_matching_currency').annotate( total=Sum('amount_matching')) amounts = [ Money(total['total'], total['amount_matching_currency']) for total in totals ] if totals: return sum([ convert(amount, properties.DEFAULT_CURRENCY) for amount in amounts ]) else: return Money(0, properties.DEFAULT_CURRENCY)
def pledged_total(self): """ Total amount of pledged donations """ donations = PledgePayment.objects.filter( self.date_filter('donation__contribution_date'), donation__status='succeeded') totals = donations.values('donation__amount_currency').annotate( total=Sum('donation__amount')) amounts = [ Money(total['total'], total['donation__amount_currency']) for total in totals ] if totals: donated = sum([ convert(amount, properties.DEFAULT_CURRENCY) for amount in amounts ]) else: donated = Money(0, properties.DEFAULT_CURRENCY) return donated