]> err.no Git - peojumk/blob - invoice/views.py
Make it possible to mark invoices as sent and paid
[peojumk] / invoice / views.py
1 from django.shortcuts import render_to_response, get_object_or_404
2 from django.contrib.auth.decorators import login_required, permission_required
3 from django.http import HttpResponse
4 from django.forms.models import inlineformset_factory
5 from django.template import RequestContext
6 from django.views.generic.create_update import get_model_and_form_class, apply_extra_context, redirect, update_object, lookup_object, delete_object
7 from django.views.generic import UpdateView, DeleteView, CreateView, DetailView
8 from django.utils.decorators import method_decorator
9 from django.contrib import messages
10
11 from extra_views import CreateWithInlinesView, UpdateWithInlinesView, InlineFormSet
12 from extra_views.generic import GenericInlineFormSet
13
14 from invoice.models import Client, Invoice, InvoiceLine, status_text
15 from invoice.forms import ClientForm, InvoiceForm, InvoiceUpdateForm
16
17 @login_required
18 def index(request):
19     clients = Client.objects.all()
20     new_invoices = Invoice.objects.filter(status = 'N')
21     sent_invoices = Invoice.objects.filter(status = 'S')
22     return render_to_response('invoice/index.html', locals())
23
24 # ------------------- CLIENT ---------------
25
26 class DetailClient(DetailView):
27     model = Client
28     @method_decorator(login_required)
29     def dispatch(self, *args, **kwargs):
30         return super(DetailView, self).dispatch(*args, **kwargs)
31
32 class CreateClient(CreateView):
33     model = Client
34     form_class = ClientForm
35     success_url = '/'
36
37     @method_decorator(login_required)
38     def dispatch(self, *args, **kwargs):
39         return super(CreateClient, self).dispatch(*args, **kwargs)
40
41 class UpdateClient(UpdateView):
42     model = Client
43     form_class = ClientForm
44     success_url = '/'
45
46     @method_decorator(login_required)
47     def dispatch(self, *args, **kwargs):
48         return super(UpdateClient, self).dispatch(*args, **kwargs)
49     
50 class DeleteClient(DeleteView):
51     model = Client
52     form_class = ClientForm
53     success_url = '/'
54
55     @method_decorator(login_required)
56     def dispatch(self, *args, **kwargs):
57         return super(DeleteClient, self).dispatch(*args, **kwargs)
58
59 # ------------------- INVOICE ---------------
60
61 class InvoiceLineInline(InlineFormSet):
62     model = InvoiceLine
63
64 class DetailInvoice(DetailView):
65     model = Invoice
66
67     def get_context_data(self, *args, **kwargs):
68         context_data = super(DetailInvoice, self).get_context_data(*args, **kwargs)
69
70         if self.object.due_days < 0 and self.object.status != 'P':
71             context_data.update({'alert_error': "This invoice is overdue" })
72         elif self.object.due_days < 3 and self.object.status != 'P':
73             context_data.update({'alert_warning': "This invoice is soon due"})
74
75         if self.object.status == 'N':
76             context_data.update({'nextstate': "S",
77                                  'nextstate_str': "sent"})
78         elif self.object.status == 'S':
79             context_data.update({'nextstate': "P",
80                                  'nextstate_str': "paid"})
81         else:
82             pass
83
84         return context_data
85
86     @method_decorator(login_required)
87     def dispatch(self, *args, **kwargs):
88         return super(DetailInvoice, self).dispatch(*args, **kwargs)
89
90 class DetailInvoicePrint(DetailInvoice):
91     template_name = 'invoice/invoice_detail_print.html'
92
93 class CreateInvoice(CreateWithInlinesView):
94     model = Invoice
95     inlines = [InvoiceLineInline]
96     form_class = InvoiceForm
97     success_url = '/'
98
99     @method_decorator(login_required)
100     def dispatch(self, *args, **kwargs):
101         return super(CreateInvoice, self).dispatch(*args, **kwargs)
102
103 class UpdateInvoice(UpdateView):
104     model = Invoice
105     form_class = InvoiceUpdateForm
106
107     @method_decorator(login_required)
108     def dispatch(self, *args, **kwargs):
109         return super(UpdateInvoice, self).dispatch(*args, **kwargs)
110
111     def form_valid(self, form):
112         messages.info(self.request, 'Status set to %s.' % (status_text(form.data['status']), ))
113         return super(UpdateInvoice, self).form_valid(form)
114         
115
116 #    def get_initial(self):
117 #        initial = super(CreateInvoice, self).get_initial().copy()
118 #        if self.request.path
119 #        initial[''] = 
120 #        return initial