69cc2f98c22a9619097b201aad5c35c715653060
[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         parser.add_argument(
22             '--resend', action='store_true',
23             help='This is a re-send.')
24
25     def handle(self, *args, **options):
26         year = options['year']
27         emails = set(
28             PayUOrder.objects.filter(
29                 completed_at__year=year
30             ).order_by('schedule__email').values_list(
31                 'schedule__email', flat=True
32             ).distinct()
33         )
34         emails.update(
35             BillingAgreement.objects.all().order_by(
36                 'schedule__email').values_list(
37                 'schedule__email', flat=True
38             ).distinct()
39         )
40         emails.update(
41             Funding.objects.exclude(email='').filter(
42                 payed_at__year=year
43             ).order_by('email').values_list(
44                 'email', flat=True
45             ).distinct()
46         )
47
48         if options['emails']:
49             emails = options['emails'].split(',')
50
51         for email in emails:
52             print(email)
53             try:
54                 PayUOrder.send_receipt(email, year, resend=options['resend'])
55             except:
56                 print('ERROR')