locale
[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 from pz.models import Payment
12
13
14 class Command(BaseCommand):
15     def add_arguments(self, parser):
16         parser.add_argument(
17             'year', type=int, metavar='YEAR',
18             help='Send receipts for the year.')
19         parser.add_argument(
20             '--emails',
21             help='Send only to these emails.')
22         parser.add_argument(
23             '--resend', action='store_true',
24             help='This is a re-send.')
25
26     def handle(self, *args, **options):
27         year = options['year']
28         emails = set(
29             PayUOrder.objects.filter(
30                 completed_at__year=year
31             ).order_by('schedule__email').values_list(
32                 'schedule__email', flat=True
33             ).distinct()
34         )
35         emails.update(
36             BillingAgreement.objects.all().order_by(
37                 'schedule__email').values_list(
38                 'schedule__email', flat=True
39             ).distinct()
40         )
41         emails.update(
42             Funding.objects.exclude(email='').filter(
43                 completed_at__year=year
44             ).order_by('email').values_list(
45                 'email', flat=True
46             ).distinct()
47         )
48         emails.update(
49             Payment.objects.exclude(debit__email='').filter(
50                 realised=True,
51                 is_dd=True,
52                 booking_date__year=year,
53             ).order_by('debit__email').values_list(
54                 'debit__email', flat=True
55             ).distinct()
56         )
57
58         if options['emails']:
59             emails = options['emails'].split(',')
60
61         for email in emails:
62             print(email)
63             try:
64                 PayUOrder.send_receipt(email, year, resend=options['resend'])
65             except:
66                 print('ERROR')
67                 raise