Merge remote-tracking branch 'zawadzki/new-design'
[wolnelektury.git] / src / club / management / commands / send_receipts.py
1 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
3 #
4 from datetime import timedelta
5 import traceback
6 from django.core.management.base import BaseCommand, CommandError
7 from django.utils.timezone import now
8 from club.models import PayUOrder
9 from funding.models import Funding
10 from paypal.models import BillingAgreement
11
12
13 class Command(BaseCommand):
14     def add_arguments(self, parser):
15         parser.add_argument(
16             'year', type=int, metavar='YEAR',
17             help='Send receipts for the year.')
18         parser.add_argument(
19             '--emails',
20             help='Send only to these emails.')
21
22     def handle(self, *args, **options):
23         year = options['year']
24         emails = set(
25             PayUOrder.objects.filter(
26                 completed_at__year=year
27             ).order_by('schedule__email').values_list(
28                 'schedule__email', flat=True
29             ).distinct()
30         )
31         emails.update(
32             BillingAgreement.objects.all().order_by(
33                 'schedule__email').values_list(
34                 'schedule__email', flat=True
35             ).distinct()
36         )
37         emails.update(
38             Funding.objects.exclude(email='').filter(
39                 payed_at__year=year
40             ).order_by('email').values_list(
41                 'email', flat=True
42             ).distinct()
43         )
44
45         if options['emails']:
46             emails = options['emails'].split(',')
47
48         for email in emails:
49             print(email)
50             try:
51                 PayUOrder.send_receipt(email, year)
52             except:
53                 print('ERROR')