summaryrefslogtreecommitdiff
path: root/app/income/views.py
blob: 1c34068ac02bcacca3f898f469bc2f9c6162afdf (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
import datetime
from django.views.generic.detail import DetailView
from django.template.loader import render_to_string
from django.http import HttpResponse
from django.conf import settings

#from weasyprint import HTML, CSS

from .models import Invoice, InvoiceItem


class MonthlyInvoiceView(DetailView):
    model = Invoice
    template_name = "admin/income/monthly.html"
    slug_field = "slug"

    def get_context_data(self, **kwargs):
        context = super(MonthlyInvoiceView, self).get_context_data(**kwargs)
        context['object_list'] = InvoiceItem.objects.filter(time_start__range=[self.object.date_start, self.object.date_end])
        total_time = []
        for item in context['object_list']:
            total_time.append(item.rounded_total)
        hours = (sum(total_time))
        context['total_hours'] = hours
        context['total_billed'] = int(hours * 100)
        context['invoice_number'] = self.object.id+21
        return context


class DownloadMonthlyInvoiceView(MonthlyInvoiceView):
    model = Invoice
    slug_field = "slug"

    def get(self, *args, **kwargs):
        import logging
        logger = logging.getLogger('weasyprint')
        logger.addHandler(logging.FileHandler('weasyprint.log'))
        self.object = self.get_object()  # assign the object to the view
        context = self.get_context_data()
        c = {
            'object': self.object,
            'object_list': context['object_list'],
            'total_hours': context['total_hours'],
            'total_billed':  context['total_billed'],
            'invoice_number': self.object.id+23
        }
        t = render_to_string('details/invoice.html', c).encode('utf-8')
        #html = HTML(string=t, base_url=self.request.build_absolute_uri())
        #pdf = html.write_pdf(stylesheets=[CSS(settings.MEDIA_ROOT + '/pdf_gen.css')], presentational_hints=True)
        #response = HttpResponse(pdf, content_type='application/pdf')
        #response['Content-Disposition'] = 'inline; filename="invoice.pdf"'
        response = HttpResponse('', content_type='application/pdf')
        return response