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
11
12 class Command(BaseCommand):
13     def add_arguments(self, parser):
14         parser.add_argument(
15             'year', type=int, metavar='YEAR',
16             help='Send receipts for the year.')
17         parser.add_argument(
18             '--emails',
19             help='Send only to these emails.')
20
21     def handle(self, *args, **options):
22         year = options['year']
23         emails = set(
24             PayUOrder.objects.filter(
25                 completed_at__year=year
26             ).order_by('schedule__email').values_list(
27                 'schedule__email', flat=True
28             ).distinct()
29         )
30         emails.update(
31             Funding.objects.exclude(email='').filter(
32                 payed_at__year=year
33             ).order_by('email').values_list(
34                 'email', flat=True
35             ).distinct()
36         )
37
38         if options['emails']:
39             emails = options['emails'].split(',')
40
41         for email in emails:
42             print(email)
43             PayUOrder.send_receipt(email, year)