2014-07-24 15:43:23 +00:00
|
|
|
from django.contrib import admin, messages
|
|
|
|
from django.core.urlresolvers import reverse
|
2014-09-16 17:14:24 +00:00
|
|
|
from django.db import transaction
|
2014-07-24 15:43:23 +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-07-24 15:43:23 +00:00
|
|
|
from django.shortcuts import render
|
|
|
|
|
2014-10-11 16:21:51 +00:00
|
|
|
from orchestra.admin.utils import change_url
|
|
|
|
|
2014-11-21 15:39:41 +00:00
|
|
|
from .forms import BillSelectedOptionsForm, BillSelectConfirmationForm, BillSelectRelatedForm
|
2014-07-24 15:43:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
class BillSelectedOrders(object):
|
|
|
|
""" Form wizard for billing orders admin action """
|
|
|
|
short_description = _("Bill selected orders")
|
2015-04-01 15:49:21 +00:00
|
|
|
verbose_name = _("Bill")
|
2014-07-24 15:43:23 +00:00
|
|
|
template = 'admin/orders/order/bill_selected_options.html'
|
|
|
|
__name__ = 'bill_selected_orders'
|
|
|
|
|
|
|
|
def __call__(self, modeladmin, request, queryset):
|
|
|
|
""" make this monster behave like a function """
|
|
|
|
self.modeladmin = modeladmin
|
|
|
|
self.queryset = queryset
|
|
|
|
opts = modeladmin.model._meta
|
|
|
|
app_label = opts.app_label
|
|
|
|
self.context = {
|
|
|
|
'opts': opts,
|
|
|
|
'app_label': app_label,
|
|
|
|
'queryset': queryset,
|
|
|
|
'action_checkbox_name': admin.helpers.ACTION_CHECKBOX_NAME,
|
|
|
|
}
|
|
|
|
return self.set_options(request)
|
|
|
|
|
|
|
|
def set_options(self, request):
|
|
|
|
form = BillSelectedOptionsForm()
|
|
|
|
if request.POST.get('step'):
|
|
|
|
form = BillSelectedOptionsForm(request.POST)
|
|
|
|
if form.is_valid():
|
|
|
|
self.options = dict(
|
|
|
|
billing_point=form.cleaned_data['billing_point'],
|
|
|
|
fixed_point=form.cleaned_data['fixed_point'],
|
2014-10-11 16:21:51 +00:00
|
|
|
proforma=form.cleaned_data['proforma'],
|
2014-09-19 14:47:25 +00:00
|
|
|
new_open=form.cleaned_data['new_open'],
|
2014-07-24 15:43:23 +00:00
|
|
|
)
|
2015-05-14 13:28:54 +00:00
|
|
|
print(self.options)
|
2014-09-11 14:00:20 +00:00
|
|
|
if int(request.POST.get('step')) != 3:
|
|
|
|
return self.select_related(request)
|
|
|
|
else:
|
|
|
|
return self.confirmation(request)
|
2014-07-24 15:43:23 +00:00
|
|
|
self.context.update({
|
|
|
|
'title': _("Options for billing selected orders, step 1 / 3"),
|
2014-09-10 16:53:09 +00:00
|
|
|
'step': 1,
|
2014-07-24 15:43:23 +00:00
|
|
|
'form': form,
|
|
|
|
})
|
|
|
|
return render(request, self.template, self.context)
|
|
|
|
|
|
|
|
def select_related(self, request):
|
2014-09-15 12:15:32 +00:00
|
|
|
# TODO use changelist ?
|
2014-09-29 13:34:38 +00:00
|
|
|
related = self.queryset.get_related().select_related('account', 'service')
|
2014-09-14 09:52:45 +00:00
|
|
|
if not related:
|
|
|
|
return self.confirmation(request)
|
2014-09-10 16:53:09 +00:00
|
|
|
self.options['related_queryset'] = related
|
2014-07-24 15:43:23 +00:00
|
|
|
form = BillSelectRelatedForm(initial=self.options)
|
2014-09-10 16:53:09 +00:00
|
|
|
if int(request.POST.get('step')) >= 2:
|
2014-07-24 15:43:23 +00:00
|
|
|
form = BillSelectRelatedForm(request.POST, initial=self.options)
|
|
|
|
if form.is_valid():
|
|
|
|
select_related = form.cleaned_data['selected_related']
|
2014-09-11 14:00:20 +00:00
|
|
|
self.queryset = self.queryset | select_related
|
2014-07-24 15:43:23 +00:00
|
|
|
return self.confirmation(request)
|
|
|
|
self.context.update({
|
|
|
|
'title': _("Select related order for billing, step 2 / 3"),
|
2014-09-10 16:53:09 +00:00
|
|
|
'step': 2,
|
2014-07-24 15:43:23 +00:00
|
|
|
'form': form,
|
|
|
|
})
|
|
|
|
return render(request, self.template, self.context)
|
|
|
|
|
2014-09-16 17:14:24 +00:00
|
|
|
@transaction.atomic
|
2014-07-24 15:43:23 +00:00
|
|
|
def confirmation(self, request):
|
|
|
|
form = BillSelectConfirmationForm(initial=self.options)
|
2014-09-10 16:53:09 +00:00
|
|
|
if int(request.POST.get('step')) >= 3:
|
2014-09-02 15:48:07 +00:00
|
|
|
bills = self.queryset.bill(commit=True, **self.options)
|
2014-09-16 17:14:24 +00:00
|
|
|
for order in self.queryset:
|
2014-10-11 16:21:51 +00:00
|
|
|
self.modeladmin.log_change(request, order, _("Billed"))
|
2014-07-24 15:43:23 +00:00
|
|
|
if not bills:
|
|
|
|
msg = _("Selected orders do not have pending billing")
|
|
|
|
self.modeladmin.message_user(request, msg, messages.WARNING)
|
|
|
|
else:
|
2014-10-11 16:21:51 +00:00
|
|
|
num = len(bills)
|
|
|
|
if num == 1:
|
|
|
|
url = change_url(bills[0])
|
|
|
|
else:
|
|
|
|
url = reverse('admin:bills_bill_changelist')
|
2015-04-21 13:12:48 +00:00
|
|
|
ids = ','.join(map(str, bills))
|
2014-10-11 16:21:51 +00:00
|
|
|
url += '?id__in=%s' % ids
|
2014-09-10 16:53:09 +00:00
|
|
|
msg = ungettext(
|
|
|
|
'<a href="{url}">One bill</a> has been created.',
|
|
|
|
'<a href="{url}">{num} bills</a> have been created.',
|
|
|
|
num).format(url=url, num=num)
|
2014-07-24 15:43:23 +00:00
|
|
|
msg = mark_safe(msg)
|
|
|
|
self.modeladmin.message_user(request, msg, messages.INFO)
|
|
|
|
return
|
2014-09-10 16:53:09 +00:00
|
|
|
bills = self.queryset.bill(commit=False, **self.options)
|
2015-04-27 12:24:17 +00:00
|
|
|
bills_with_total = []
|
|
|
|
for account, lines in bills:
|
|
|
|
total = 0
|
|
|
|
for line in lines:
|
|
|
|
discount = sum([discount.total for discount in line.discounts])
|
|
|
|
total += line.subtotal + discount
|
|
|
|
bills_with_total.append((account, total, lines))
|
2014-07-24 15:43:23 +00:00
|
|
|
self.context.update({
|
|
|
|
'title': _("Confirmation for billing selected orders"),
|
2014-09-10 16:53:09 +00:00
|
|
|
'step': 3,
|
2014-07-24 15:43:23 +00:00
|
|
|
'form': form,
|
2015-04-27 12:24:17 +00:00
|
|
|
'bills': bills_with_total,
|
2014-07-24 15:43:23 +00:00
|
|
|
})
|
|
|
|
return render(request, self.template, self.context)
|
2014-10-16 17:14:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
@transaction.atomic
|
|
|
|
def mark_as_ignored(modeladmin, request, queryset):
|
|
|
|
""" Mark orders as ignored """
|
|
|
|
for order in queryset:
|
|
|
|
order.mark_as_ignored()
|
|
|
|
modeladmin.log_change(request, order, 'Marked as ignored')
|
|
|
|
num = len(queryset)
|
|
|
|
msg = ungettext(
|
|
|
|
_("Selected order has been marked as ignored."),
|
|
|
|
_("%i selected orders have been marked as ignored.") % num,
|
|
|
|
num)
|
|
|
|
modeladmin.message_user(request, msg)
|
|
|
|
|
|
|
|
|
|
|
|
@transaction.atomic
|
|
|
|
def mark_as_not_ignored(modeladmin, request, queryset):
|
|
|
|
""" Mark orders as ignored """
|
|
|
|
for order in queryset:
|
|
|
|
order.mark_as_not_ignored()
|
|
|
|
modeladmin.log_change(request, order, 'Marked as not ignored')
|
|
|
|
num = len(queryset)
|
|
|
|
msg = ungettext(
|
|
|
|
_("Selected order has been marked as not ignored."),
|
|
|
|
_("%i selected orders have been marked as not ignored.") % num,
|
|
|
|
num)
|
|
|
|
modeladmin.message_user(request, msg)
|
|
|
|
|