django-orchestra/orchestra/apps/bills/actions.py

179 lines
6.8 KiB
Python
Raw Normal View History

import zipfile
2015-04-02 16:14:55 +00:00
from io import StringIO
from django.contrib import messages
2014-09-06 10:56:30 +00:00
from django.contrib.admin import helpers
2014-09-30 14:46:29 +00:00
from django.core.urlresolvers import reverse
2014-10-20 19:22:18 +00:00
from django.db import transaction
2014-10-11 16:21:51 +00:00
from django.http import HttpResponse
2014-09-06 10:56:30 +00:00
from django.shortcuts import render
2014-09-30 14:46:29 +00:00
from django.utils.safestring import mark_safe
2014-10-11 16:21:51 +00:00
from django.utils.translation import ungettext, ugettext_lazy as _
2014-09-06 10:56:30 +00:00
from orchestra.admin.forms import adminmodelformset_factory
2014-10-11 16:21:51 +00:00
from orchestra.admin.utils import get_object_from_url, change_url
from orchestra.utils.html import html_to_pdf
2014-09-06 10:56:30 +00:00
from .forms import SelectSourceForm
2014-10-11 16:21:51 +00:00
from .helpers import validate_contact
2014-09-30 14:46:29 +00:00
2014-08-20 18:50:07 +00:00
def download_bills(modeladmin, request, queryset):
if queryset.count() > 1:
2015-04-02 16:14:55 +00:00
stringio = StringIO()
archive = zipfile.ZipFile(stringio, 'w')
for bill in queryset:
2014-10-11 16:21:51 +00:00
pdf = html_to_pdf(bill.html or bill.render())
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-10-11 16:21:51 +00:00
pdf = html_to_pdf(bill.html or bill.render())
2014-08-29 12:45:27 +00:00
return HttpResponse(pdf, content_type='application/pdf')
download_bills.verbose_name = _("Download")
download_bills.url_name = 'download'
def view_bill(modeladmin, request, queryset):
bill = queryset.get()
2014-10-11 16:21:51 +00:00
if not validate_contact(request, bill):
return
2014-09-04 15:55:43 +00:00
html = bill.html or bill.render()
return HttpResponse(html)
view_bill.verbose_name = _("View")
view_bill.url_name = 'view'
2014-10-20 19:22:18 +00:00
@transaction.atomic
def close_bills(modeladmin, request, queryset):
2014-09-18 15:07:39 +00:00
queryset = queryset.filter(is_open=True)
if not queryset:
messages.warning(request, _("Selected bills should be in open state"))
return
2014-09-30 14:46:29 +00:00
for bill in queryset:
2014-10-11 16:21:51 +00:00
if not validate_contact(request, bill):
return
2014-09-16 17:14:24 +00:00
SelectSourceFormSet = adminmodelformset_factory(modeladmin, SelectSourceForm, extra=0)
2014-09-06 10:56:30 +00:00
formset = SelectSourceFormSet(queryset=queryset)
2014-09-08 15:10:16 +00:00
if request.POST.get('post') == 'generic_confirmation':
2014-09-06 10:56:30 +00:00
formset = SelectSourceFormSet(request.POST, request.FILES, queryset=queryset)
if formset.is_valid():
2014-10-11 16:21:51 +00:00
transactions = []
for form in formset.forms:
2014-09-06 10:56:30 +00:00
source = form.cleaned_data['source']
2014-10-11 16:21:51 +00:00
transaction = form.instance.close(payment=source)
if transaction:
transactions.append(transaction)
2014-09-16 17:14:24 +00:00
for bill in queryset:
modeladmin.log_change(request, bill, 'Closed')
messages.success(request, _("Selected bills have been closed"))
2014-10-11 16:21:51 +00:00
if transactions:
num = len(transactions)
if num == 1:
url = change_url(transactions[0])
else:
url = reverse('admin:transactions_transaction_changelist')
url += 'id__in=%s' % ','.join([str(t.id) for t in transactions])
message = ungettext(
_('<a href="%s">One related transaction</a> has been created') % url,
_('<a href="%s">%i related transactions</a> have been created') % (url, num),
num)
messages.success(request, mark_safe(message))
return
2014-09-06 10:56:30 +00:00
opts = modeladmin.model._meta
context = {
2014-09-08 15:10:16 +00:00
'title': _("Are you sure about closing the following bills?"),
'content_message': _("Once a bill is closed it can not be further modified.</p>"
"<p>Please select a payment source for the selected bills"),
'action_name': 'Close bills',
2014-09-06 10:56:30 +00:00
'action_value': 'close_bills',
2014-09-08 15:10:16 +00:00
'display_objects': [],
2014-09-06 10:56:30 +00:00
'queryset': queryset,
'opts': opts,
'app_label': opts.app_label,
'action_checkbox_name': helpers.ACTION_CHECKBOX_NAME,
'formset': formset,
2014-09-10 16:53:09 +00:00
'obj': get_object_from_url(modeladmin, request),
2014-09-06 10:56:30 +00:00
}
2014-09-08 15:10:16 +00:00
return render(request, 'admin/orchestra/generic_confirmation.html', context)
close_bills.verbose_name = _("Close")
close_bills.url_name = 'close'
2014-09-04 15:55:43 +00:00
def send_bills(modeladmin, request, queryset):
2014-09-30 14:46:29 +00:00
for bill in queryset:
2014-10-11 16:21:51 +00:00
if not validate_contact(request, bill):
return
2014-09-04 15:55:43 +00:00
for bill in queryset:
bill.send()
2014-09-16 17:14:24 +00:00
modeladmin.log_change(request, bill, 'Sent')
2014-10-11 16:21:51 +00:00
send_bills.verbose_name = lambda bill: _("Resend" if getattr(bill, 'is_sent', False) else "Send")
2014-09-04 15:55:43 +00:00
send_bills.url_name = 'send'
2015-03-29 16:10:07 +00:00
def undo_billing(modeladmin, request, queryset):
group = {}
for line in queryset.select_related('order'):
if line.order_id:
try:
group[line.order].append(line)
except KeyError:
group[line.order] = [line]
# TODO force incomplete info
2015-04-02 16:14:55 +00:00
for order, lines in group.items():
2015-03-29 16:10:07 +00:00
# Find path from ini to end
for attr in ['order_id', 'order_billed_on', 'order_billed_until']:
if not getattr(self, attr):
raise ValidationError(_("Not enough information stored for undoing"))
sorted(lines, key=lambda l: l.created_on)
if 'a' != order.billed_on:
raise ValidationError(_("Dates don't match"))
prev = order.billed_on
2015-04-02 16:14:55 +00:00
for ix in range(0, len(lines)):
2015-03-29 16:10:07 +00:00
if lines[ix].order_b: # TODO we need to look at the periods here
pass
order.billed_until = self.order_billed_until
order.billed_on = self.order_billed_on
# TODO son't check for account equality
def move_lines(modeladmin, request, queryset):
# Validate
account = None
for line in queryset.select_related('bill'):
bill = line.bill
if bill.state != bill.OPEN:
messages.error(request, _("Can not move lines which are not in open state."))
return
elif not account:
account = bill.account
elif bill.account != account:
messages.error(request, _("Can not move lines from different accounts"))
return
target = request.GET.get('target')
if not target:
# select target
return render(request, 'admin/orchestra/generic_confirmation.html', context)
target = Bill.objects.get(pk=int(pk))
if target.account != account:
messages.error(request, _("Target account different than lines account."))
return
if request.POST.get('post') == 'generic_confirmation':
for line in queryset:
line.bill = target
line.save(update_fields=['bill'])
# TODO bill history update
messages.success(request, _("Lines moved"))
# Final confirmation
return render(request, 'admin/orchestra/generic_confirmation.html', context)
def copy_lines(modeladmin, request, queryset):
# same as move, but changing action behaviour
pass
def delete_lines(modeladmin, request, queryset):
pass