summaryrefslogtreecommitdiff
path: root/app/income/admin.py
blob: c084eb0aa6d996bb0d70107f46536c86c7df7b32 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import datetime
from django.contrib import admin
from django.db.models import Sum
from django.contrib.gis.admin import OSMGeoAdmin
from django.conf.urls import url
from django.shortcuts import render


from .models import Gig


class GigAdmin(OSMGeoAdmin):
    list_display = ('title', 'status', 'due_date', 'payment_status', 'payment', 'publisher', 'word_count')
    list_filter = ('publisher', 'status', 'payment_status')
    fieldsets = (
        ('Gig', {
            'fields': (
                'title',
                'pitch',
                ('created', 'due_date'),
                ('payment', 'pay_type', 'payment_status', 'invoice_date'),
                ('status', 'pub_date', 'word_count'),
                'publisher',
                'pub_item'
            ),
            'classes': (
                'show',
                'extrapretty',
                'wide'
            )
        }
        ),
    )

    def get_urls(self):
        urls = super(GigAdmin, self).get_urls()
        custom_urls = [
            url(
                r'^monthly/$',
                self.admin_site.admin_view(self.get_monthly),
                name='monthly_admin'
            )
        ]
        return custom_urls + urls

    def get_monthly(self, request):
        context = {
            'title': ("This month's income"),
            'app_label': self.model._meta.app_label,
            'opts': self.model._meta,
            'has_change_permission': self.has_change_permission(request)
        }
        try:
            year = request.GET["m"].split("-")[0]
            month = request.GET["m"].split("-")[1]
        except:
            year = datetime.datetime.now().strftime('%Y')
            month = datetime.datetime.now().strftime('%m')
        qs = self.model.objects.filter(
            created__year=year,
            created__month=month,
            status__in=[1, 2, 3]
        )
        context['pitched'] = self.model.objects.filter(
            created__year=year,
            created__month=month,
            status=0
        )
        context['date'] = datetime.datetime.now()
        context['billed'] = qs.filter(payment_status=1)
        context['billed_total'] = qs.filter(payment_status=1).aggregate(total_payment=Sum('payment'))
        context['unbilled'] = qs.filter(payment_status=0)
        context['unbilled_total'] = qs.filter(payment_status=0).aggregate(total_payment=Sum('payment'))
        context['total_outstanding'] = qs.aggregate(total_payment=Sum('payment'))
        context['months'] = self.model.objects.dates('created', 'month')
        return render(request, 'admin/income_month.html', context)


admin.site.register(Gig, GigAdmin)