views.py 11.9 KB
Newer Older
Skia's avatar
Skia committed
1
2
from django.views.generic import ListView, DetailView, RedirectView
from django.views.generic.edit import UpdateView, CreateView, DeleteView
Skia's avatar
Skia committed
3
from django.shortcuts import render
Skia's avatar
Skia committed
4
from django.core.urlresolvers import reverse_lazy
5
6
from django.forms.models import modelform_factory
from django.forms import HiddenInput
Skia's avatar
Skia committed
7
8
9
from django import forms

from ajax_select.fields import AutoCompleteSelectField, AutoCompleteSelectMultipleField
Skia's avatar
Skia committed
10

11
from core.views import CanViewMixin, CanEditMixin, CanEditPropMixin, CanCreateMixin
12
from core.views.forms import SelectFile, SelectDate
Skia's avatar
Skia committed
13
from accounting.models import BankAccount, ClubAccount, GeneralJournal, Operation, AccountingType, Company, SimplifiedAccountingType, Label
Skia's avatar
Skia committed
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49

# Main accounting view

class BankAccountListView(CanViewMixin, ListView):
    """
    A list view for the admins
    """
    model = BankAccount
    template_name = 'accounting/bank_account_list.jinja'
    ordering = ['name']

# Simplified accounting types

class SimplifiedAccountingTypeListView(CanViewMixin, ListView):
    """
    A list view for the admins
    """
    model = SimplifiedAccountingType
    template_name = 'accounting/simplifiedaccountingtype_list.jinja'

class SimplifiedAccountingTypeEditView(CanViewMixin, UpdateView):
    """
    An edit view for the admins
    """
    model = SimplifiedAccountingType
    pk_url_kwarg = "type_id"
    fields = ['label', 'accounting_type']
    template_name = 'core/edit.jinja'

class SimplifiedAccountingTypeCreateView(CanCreateMixin, CreateView):
    """
    Create an accounting type (for the admins)
    """
    model = SimplifiedAccountingType
    fields = ['label', 'accounting_type']
    template_name = 'core/create.jinja'
Skia's avatar
Skia committed
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66

# Accounting types

class AccountingTypeListView(CanViewMixin, ListView):
    """
    A list view for the admins
    """
    model = AccountingType
    template_name = 'accounting/accountingtype_list.jinja'

class AccountingTypeEditView(CanViewMixin, UpdateView):
    """
    An edit view for the admins
    """
    model = AccountingType
    pk_url_kwarg = "type_id"
    fields = ['code', 'label', 'movement_type']
Skia's avatar
Skia committed
67
    template_name = 'core/edit.jinja'
Skia's avatar
Skia committed
68

69
class AccountingTypeCreateView(CanCreateMixin, CreateView):
Skia's avatar
Skia committed
70
71
72
73
74
    """
    Create an accounting type (for the admins)
    """
    model = AccountingType
    fields = ['code', 'label', 'movement_type']
Skia's avatar
Skia committed
75
    template_name = 'core/create.jinja'
Skia's avatar
Skia committed
76
77
78
79
80
81
82
83
84

# BankAccount views

class BankAccountEditView(CanViewMixin, UpdateView):
    """
    An edit view for the admins
    """
    model = BankAccount
    pk_url_kwarg = "b_account_id"
Skia's avatar
Skia committed
85
    fields = ['name', 'iban', 'number', 'club']
Skia's avatar
Skia committed
86
    template_name = 'core/edit.jinja'
Skia's avatar
Skia committed
87
88
89
90
91
92
93
94
95

class BankAccountDetailView(CanViewMixin, DetailView):
    """
    A detail view, listing every club account
    """
    model = BankAccount
    pk_url_kwarg = "b_account_id"
    template_name = 'accounting/bank_account_details.jinja'

96
class BankAccountCreateView(CanCreateMixin, CreateView):
Skia's avatar
Skia committed
97
98
99
100
    """
    Create a bank account (for the admins)
    """
    model = BankAccount
Skia's avatar
Skia committed
101
    fields = ['name', 'club', 'iban', 'number']
Skia's avatar
Skia committed
102
    template_name = 'core/create.jinja'
Skia's avatar
Skia committed
103

Skia's avatar
Skia committed
104
class BankAccountDeleteView(CanEditPropMixin, DeleteView): # TODO change Delete to Close
Skia's avatar
Skia committed
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
    """
    Delete a bank account (for the admins)
    """
    model = BankAccount
    pk_url_kwarg = "b_account_id"
    template_name = 'core/delete_confirm.jinja'
    success_url = reverse_lazy('accounting:bank_list')

# ClubAccount views

class ClubAccountEditView(CanViewMixin, UpdateView):
    """
    An edit view for the admins
    """
    model = ClubAccount
    pk_url_kwarg = "c_account_id"
    fields = ['name', 'club', 'bank_account']
Skia's avatar
Skia committed
122
    template_name = 'core/edit.jinja'
Skia's avatar
Skia committed
123
124
125
126
127
128
129
130
131

class ClubAccountDetailView(CanViewMixin, DetailView):
    """
    A detail view, listing every journal
    """
    model = ClubAccount
    pk_url_kwarg = "c_account_id"
    template_name = 'accounting/club_account_details.jinja'

132
class ClubAccountCreateView(CanCreateMixin, CreateView):
Skia's avatar
Skia committed
133
134
135
136
137
    """
    Create a club account (for the admins)
    """
    model = ClubAccount
    fields = ['name', 'club', 'bank_account']
Skia's avatar
Skia committed
138
139
140
141
142
143
144
145
146
    template_name = 'core/create.jinja'

    def get_initial(self):
        ret = super(ClubAccountCreateView, self).get_initial()
        if 'parent' in self.request.GET.keys():
            obj = BankAccount.objects.filter(id=int(self.request.GET['parent'])).first()
            if obj is not None:
                ret['bank_account'] = obj.id
        return ret
Skia's avatar
Skia committed
147

Skia's avatar
Skia committed
148
class ClubAccountDeleteView(CanEditPropMixin, DeleteView): # TODO change Delete to Close
Skia's avatar
Skia committed
149
150
151
152
153
154
155
156
157
158
    """
    Delete a club account (for the admins)
    """
    model = ClubAccount
    pk_url_kwarg = "c_account_id"
    template_name = 'core/delete_confirm.jinja'
    success_url = reverse_lazy('accounting:bank_list')

# Journal views

Skia's avatar
Skia committed
159
class JournalCreateView(CanCreateMixin, CreateView):
Skia's avatar
Skia committed
160
161
162
163
    """
    Create a general journal
    """
    model = GeneralJournal
Skia's avatar
Skia committed
164
165
    form_class = modelform_factory(GeneralJournal, fields=['name', 'start_date', 'club_account'],
            widgets={ 'start_date': SelectDate, })
Skia's avatar
Skia committed
166
167
168
169
170
171
172
173
174
    template_name = 'core/create.jinja'

    def get_initial(self):
        ret = super(JournalCreateView, self).get_initial()
        if 'parent' in self.request.GET.keys():
            obj = ClubAccount.objects.filter(id=int(self.request.GET['parent'])).first()
            if obj is not None:
                ret['club_account'] = obj.id
        return ret
Skia's avatar
Skia committed
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189

class JournalDetailView(CanViewMixin, DetailView):
    """
    A detail view, listing every operation
    """
    model = GeneralJournal
    pk_url_kwarg = "j_id"
    template_name = 'accounting/journal_details.jinja'

class JournalEditView(CanEditMixin, UpdateView):
    """
    Update a general journal
    """
    model = GeneralJournal
    pk_url_kwarg = "j_id"
Skia's avatar
Skia committed
190
    fields = ['name', 'start_date', 'end_date', 'club_account', 'closed']
Skia's avatar
Skia committed
191
    template_name = 'core/edit.jinja'
Skia's avatar
Skia committed
192
193
194

# Operation views

Skia's avatar
Skia committed
195
196
197
198
class OperationForm(forms.ModelForm):
    class Meta:
        model = Operation
        fields = ['amount', 'remark', 'journal', 'target_type', 'target_id', 'target_label', 'date', 'mode',
Skia's avatar
Skia committed
199
            'cheque_number', 'invoice', 'simpleaccounting_type', 'accounting_type', 'label', 'done']
Skia's avatar
Skia committed
200
201
202
203
204
205
206
207
208
209
210
211
212
        widgets = {
                'journal': HiddenInput,
                'target_id': HiddenInput,
                'date': SelectDate,
                'invoice': SelectFile,
                }
    user = AutoCompleteSelectField('users', help_text=None, required=False)
    club_account = AutoCompleteSelectField('club_accounts', help_text=None, required=False)
    club = AutoCompleteSelectField('clubs', help_text=None, required=False)
    company = AutoCompleteSelectField('companies', help_text=None, required=False)

    def __init__(self, *args, **kwargs):
        super(OperationForm, self).__init__(*args, **kwargs)
Skia's avatar
Skia committed
213
        self.fields['label'].queryset = self.instance.journal.club_account.labels.order_by('name').all()
Skia's avatar
Skia committed
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
        if self.instance.target_type == "USER":
            self.fields['user'].initial = self.instance.target_id
        elif self.instance.target_type == "ACCOUNT":
            self.fields['club_account'].initial = self.instance.target_id
        elif self.instance.target_type == "CLUB":
            self.fields['club'].initial = self.instance.target_id
        elif self.instance.target_type == "COMPANY":
            self.fields['company'].initial = self.instance.target_id

    def clean(self):
        self.cleaned_data = super(OperationForm, self).clean()
        if self.cleaned_data['target_type'] == "USER":
            self.cleaned_data['target_id'] = self.cleaned_data['user'].id
        elif self.cleaned_data['target_type'] == "ACCOUNT":
            self.cleaned_data['target_id'] = self.cleaned_data['club_account'].id
        elif self.cleaned_data['target_type'] == "CLUB":
            self.cleaned_data['target_id'] = self.cleaned_data['club'].id
        elif self.cleaned_data['target_type'] == "COMPANY":
            self.cleaned_data['target_id'] = self.cleaned_data['company'].id
        return self.cleaned_data

    def save(self):
        ret = super(OperationForm, self).save()
        if self.instance.target_type == "ACCOUNT" and not self.instance.linked_operation and self.instance.target.has_open_journal():
            inst = self.instance
            club_account = inst.target
            acc_type = AccountingType.objects.exclude(movement_type="NEUTRAL").exclude(
                    movement_type=inst.accounting_type.movement_type).first() # Select a random opposite accounting type
            op = Operation(
                    journal=club_account.get_open_journal(),
                    amount=inst.amount,
                    date=inst.date,
                    remark=inst.remark,
                    mode=inst.mode,
                    cheque_number=inst.cheque_number,
                    invoice=inst.invoice,
                    done=False, # Has to be checked by hand
                    simpleaccounting_type=None,
                    accounting_type=acc_type,
                    target_type="ACCOUNT",
                    target_id=inst.journal.club_account.id,
                    target_label="",
                    linked_operation=inst,
                    )
            op.save()
            self.instance.linked_operation = op
            self.save()
        return ret

263
class OperationCreateView(CanCreateMixin, CreateView):
Skia's avatar
Skia committed
264
265
266
267
    """
    Create an operation
    """
    model = Operation
Skia's avatar
Skia committed
268
269
    form_class = OperationForm
    template_name = 'accounting/operation_edit.jinja'
Skia's avatar
Skia committed
270
271
272
273

    def get_initial(self):
        ret = super(OperationCreateView, self).get_initial()
        if 'parent' in self.request.GET.keys():
Skia's avatar
Skia committed
274
275
276
            self.journal = GeneralJournal.objects.filter(id=int(self.request.GET['parent'])).first()
            if self.journal is not None:
                ret['journal'] = self.journal.id
Skia's avatar
Skia committed
277
        return ret
Skia's avatar
Skia committed
278

Skia's avatar
Skia committed
279
280
281
282
283
284
285
    def get_context_data(self, **kwargs):
        """ Add journal to the context """
        kwargs = super(OperationCreateView, self).get_context_data(**kwargs)
        if self.journal:
            kwargs['object'] = self.journal
        return kwargs

Skia's avatar
Skia committed
286
class OperationEditView(CanEditMixin, UpdateView):
Skia's avatar
Skia committed
287
288
289
290
291
    """
    An edit view, working as detail for the moment
    """
    model = Operation
    pk_url_kwarg = "op_id"
Skia's avatar
Skia committed
292
293
    form_class = OperationForm
    template_name = 'accounting/operation_edit.jinja'
Skia's avatar
Skia committed
294

Skia's avatar
Skia committed
295
296
    def get_context_data(self, **kwargs):
        """ Add journal to the context """
Skia's avatar
Skia committed
297
        kwargs = super(OperationEditView, self).get_context_data(**kwargs)
Skia's avatar
Skia committed
298
299
300
        kwargs['object'] = self.object.journal
        return kwargs

Skia's avatar
Skia committed
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# Company views

class CompanyCreateView(CanCreateMixin, CreateView):
    """
    Create a company
    """
    model = Company
    fields = ['name']
    template_name = 'core/create.jinja'

class CompanyEditView(CanCreateMixin, UpdateView):
    """
    Edit a company
    """
    model = Company
    pk_url_kwarg = "co_id"
    fields = ['name']
Skia's avatar
Skia committed
318
    template_name = 'core/edit.jinja'
Skia's avatar
Skia committed
319

Skia's avatar
Skia committed
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
# Label views

class LabelListView(CanViewMixin, DetailView):
    model = ClubAccount
    pk_url_kwarg = "clubaccount_id"
    template_name = 'accounting/label_list.jinja'

class LabelCreateView(CanEditMixin, CreateView): # FIXME we need to check the rights before creating the object
    model = Label
    form_class = modelform_factory(Label, fields=['name', 'club_account'], widgets={
        'club_account': HiddenInput,
        })
    template_name = 'core/create.jinja'

    def get_initial(self):
        ret = super(LabelCreateView, self).get_initial()
        if 'parent' in self.request.GET.keys():
            obj = ClubAccount.objects.filter(id=int(self.request.GET['parent'])).first()
            if obj is not None:
                ret['club_account'] = obj.id
        return ret

class LabelEditView(CanEditMixin, UpdateView):
    model = Label
    pk_url_kwarg = "label_id"
    fields = ['name']
    template_name = 'core/edit.jinja'

class LabelDeleteView(CanEditMixin, DeleteView):
    model = Label
    pk_url_kwarg = "label_id"
    template_name = 'core/delete_confirm.jinja'

    def get_success_url(self):
        return self.object.get_absolute_url()