2014-09-03 22:01:44 +00:00
|
|
|
import StringIO
|
|
|
|
import zipfile
|
|
|
|
|
2014-09-05 14:27:30 +00:00
|
|
|
from django.contrib import messages
|
2014-08-20 18:50:07 +00:00
|
|
|
from django.http import HttpResponse
|
2014-09-03 22:01:44 +00:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
|
|
|
|
|
|
|
from orchestra.utils.html import html_to_pdf
|
|
|
|
|
2014-08-20 18:50:07 +00:00
|
|
|
|
2014-09-03 22:01:44 +00:00
|
|
|
def download_bills(modeladmin, request, queryset):
|
|
|
|
if queryset.count() > 1:
|
|
|
|
stringio = StringIO.StringIO()
|
|
|
|
archive = zipfile.ZipFile(stringio, 'w')
|
|
|
|
for bill in queryset:
|
2014-09-04 15:55:43 +00:00
|
|
|
html = bill.html or bill.render()
|
|
|
|
pdf = html_to_pdf(html)
|
2014-09-03 22:01:44 +00:00
|
|
|
archive.writestr('%s.pdf' % bill.number, pdf)
|
|
|
|
archive.close()
|
|
|
|
response = HttpResponse(stringio.getvalue(), content_type='application/pdf')
|
|
|
|
response['Content-Disposition'] = 'attachment; filename="orchestra-bills.zip"'
|
|
|
|
return response
|
2014-08-20 18:50:07 +00:00
|
|
|
bill = queryset.get()
|
2014-09-03 22:01:44 +00:00
|
|
|
pdf = html_to_pdf(bill.html)
|
2014-08-29 12:45:27 +00:00
|
|
|
return HttpResponse(pdf, content_type='application/pdf')
|
2014-09-03 22:01:44 +00:00
|
|
|
download_bills.verbose_name = _("Download")
|
|
|
|
download_bills.url_name = 'download'
|
|
|
|
|
|
|
|
|
|
|
|
def view_bill(modeladmin, request, queryset):
|
|
|
|
bill = queryset.get()
|
2014-09-04 15:55:43 +00:00
|
|
|
html = bill.html or bill.render()
|
|
|
|
return HttpResponse(html)
|
2014-09-03 22:01:44 +00:00
|
|
|
view_bill.verbose_name = _("View")
|
|
|
|
view_bill.url_name = 'view'
|
|
|
|
|
|
|
|
|
2014-09-05 14:27:30 +00:00
|
|
|
from django import forms
|
|
|
|
from django.forms.models import BaseModelFormSet
|
|
|
|
from django.forms.formsets import formset_factory
|
|
|
|
from django.forms.models import modelformset_factory
|
|
|
|
from django.shortcuts import render
|
|
|
|
|
|
|
|
from .forms import SelectPaymentSourceForm
|
|
|
|
|
2014-09-03 22:01:44 +00:00
|
|
|
def close_bills(modeladmin, request, queryset):
|
2014-09-05 14:27:30 +00:00
|
|
|
queryset = queryset.filter(status=queryset.model.OPEN)
|
|
|
|
if not queryset:
|
|
|
|
messages.warning(request, _("Selected bills should be in open state"))
|
|
|
|
return
|
|
|
|
SelectPaymentSourceFormSet = modelformset_factory(queryset.model, form=SelectPaymentSourceForm, extra=0)
|
|
|
|
if request.POST.get('action') == 'close_selected_bills':
|
|
|
|
formset = SelectPaymentSourceFormSet(request.POST, queryset=queryset)
|
|
|
|
if formset.is_valid():
|
|
|
|
for form in formset.forms:
|
|
|
|
form.save()
|
|
|
|
messages.success(request, _("Selected bills have been closed"))
|
|
|
|
return
|
|
|
|
formset = SelectPaymentSourceFormSet(queryset=queryset)
|
|
|
|
return render(request, 'admin/bills/close_confirmation.html', {'formset': formset})
|
2014-09-03 22:01:44 +00:00
|
|
|
close_bills.verbose_name = _("Close")
|
|
|
|
close_bills.url_name = 'close'
|
2014-09-04 15:55:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
def send_bills(modeladmin, request, queryset):
|
|
|
|
for bill in queryset:
|
|
|
|
bill.send()
|
|
|
|
send_bills.verbose_name = _("Send")
|
|
|
|
send_bills.url_name = 'send'
|