1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
# |
3
|
|
|
# This file is part of SENAITE.CORE |
4
|
|
|
# |
5
|
|
|
# Copyright 2018 by it's authors. |
6
|
|
|
# Some rights reserved. See LICENSE.rst, CONTRIBUTORS.rst. |
7
|
|
|
|
8
|
|
|
from bika.lims import api |
9
|
|
|
from bika.lims.browser import BrowserView |
10
|
|
|
from bika.lims.interfaces import IInvoiceView |
11
|
|
|
from zope.interface import implements |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
class InvoiceView(BrowserView): |
15
|
|
|
"""Base view for invoices |
16
|
|
|
|
17
|
|
|
This view returns the invoice PDF |
18
|
|
|
""" |
19
|
|
|
implements(IInvoiceView) |
20
|
|
|
|
21
|
|
|
def __init__(self, context, request): |
22
|
|
|
super(InvoiceView, self).__init__(context, request) |
23
|
|
|
self.context = context |
24
|
|
|
self.request = request |
25
|
|
|
|
26
|
|
|
def __call__(self): |
27
|
|
|
filename = "invoice.pdf" |
28
|
|
|
pdf = self.context.getInvoicePDF() |
29
|
|
|
if pdf: |
30
|
|
|
data = pdf.data |
31
|
|
|
else: |
32
|
|
|
ar = self.context.getAnalysisRequest() |
33
|
|
|
so = self.context.getSupplyOrder() |
34
|
|
|
context = ar or so |
35
|
|
|
view = api.get_view("invoice_create", context=context) |
36
|
|
|
data = view.create_pdf() |
37
|
|
|
self.context.setInvoicePDF(data) |
38
|
|
|
return self.download(data, filename) |
39
|
|
|
|
40
|
|
|
def download(self, data, filename, content_type="application/pdf"): |
41
|
|
|
"""Download the PDF |
42
|
|
|
""" |
43
|
|
|
self.request.response.setHeader( |
44
|
|
|
"Content-Disposition", "inline; filename=%s" % filename) |
45
|
|
|
self.request.response.setHeader("Content-Type", content_type) |
46
|
|
|
self.request.response.setHeader("Content-Length", len(data)) |
47
|
|
|
self.request.response.setHeader("Cache-Control", "no-store") |
48
|
|
|
self.request.response.setHeader("Pragma", "no-cache") |
49
|
|
|
self.request.response.write(data) |
50
|
|
|
|