diff --git a/cash/admin.py b/cash/admin.py index a2aeda5..13ea26d 100644 --- a/cash/admin.py +++ b/cash/admin.py @@ -1,35 +1,86 @@ from django.contrib import admin from cash.models import * +from django import forms +from django.template.defaultfilters import escape +from django.core.urlresolvers import reverse +class AccountForm(forms.ModelForm): + credit_change = forms.DecimalField(max_digits = 5, decimal_places = 2, required = False) + + class Meta: + model = Account class AccountAdmin(admin.ModelAdmin): - list_display = ('user', 'card_number', 'credit') + list_display = ('user', 'card_number', 'credit', 'transaction_link') + form = AccountForm + readonly_fields = ('credit',) + + def transaction_link(self, account): + return 'Transaktionen' % (reverse("admin:cash_transaction_changelist"), account.id) + + transaction_link.allow_tags = True + + def save_model(self, request, obj, form, change): + # TODO: Auslagern + PAYOUT_SUBJECT = "Auszahlung" + DEPOSIT_SUBJECT = "Einzahlung" + DESC = "Autorisiert von %s %s" + + amount = form.cleaned_data['credit_change'] + + print amount + + if amount is not None and amount != 0: + if amount > 0: + subject = DEPOSIT_SUBJECT + else: + subject = PAYOUT_SUBJECT + + obj.change_credit(amount, subject, DESC % (request.user.first_name, request.user.last_name)) class ProductBarcodeInline(admin.TabularInline): model = ProductBarcode extra = 1 - class ProductAdmin(admin.ModelAdmin): list_display = ('name', 'category', 'price') list_filter = ['category'] inlines= [ProductBarcodeInline] - class ProductCategoryAdmin(admin.ModelAdmin): list_display = ('name', 'comment') - class SalesLogEntryAdmin(admin.ModelAdmin): list_display = ('account', 'timestamp', 'product', 'count', 'unit_price') list_filter = ['account', 'timestamp', 'product'] +class TransactionAdmin(admin.ModelAdmin): + list_display = ('account', 'timestamp', 'subject', 'description', 'amount') + list_filter = ['account', 'timestamp', 'subject'] + actions = None + date_hierarchy = 'timestamp' + + # Disable tampering with the transactions completely + def has_add_permission(self, request): + return False + def has_change_permission(self, request, obj=None): + if obj is None: + return True + return False + def has_delete_permission(self, request, obj=None): + return False + + # Needed to not trigger an ImproperlyConfigured exception + # FIXME: a bit too hacky + def changelist_view(self, request, extra_context=None): + self.list_display_links = (None, ) + return super(TransactionAdmin, self).changelist_view(request, extra_context=None) admin.site.register(Account, AccountAdmin) admin.site.register(Product, ProductAdmin) admin.site.register(ProductBarcode) admin.site.register(ProductCategory, ProductCategoryAdmin) -admin.site.register(Transaction) +admin.site.register(Transaction, TransactionAdmin) admin.site.register(SalesLogEntry, SalesLogEntryAdmin) diff --git a/cash/models.py b/cash/models.py index 3ff835a..437bd53 100644 --- a/cash/models.py +++ b/cash/models.py @@ -27,6 +27,16 @@ class Account(models.Model): instance.account.card_number = instance.ldap_user.attrs['employeenumber'][0] instance.account.save() + def change_credit(self, amount, subject, desc): + self.credit += amount + self.save() + + transaction = Transaction(account=self, subject=subject, + amount=amount, description=desc) + transaction.save() + + + def buy_products(self, products): # TODO place it somewhere else MAX_DEBIT = -35 @@ -37,10 +47,7 @@ class Account(models.Model): total_value = sum(map(lambda p: p.price * products[p], products.keys())) if self.credit - total_value >= MAX_DEBIT: - self.credit -= total_value - self.save() - - descr = '' + desc = '' for product in products.keys(): amount = products[product] @@ -48,12 +55,9 @@ class Account(models.Model): count=amount, unit_price=product.price) logentry.save() - descr += '%dx %s\n' % (amount, product.name) - - transaction = Transaction(account=self, subject=BUY_SUBJECT, - amount=(-total_value), description=descr) - transaction.save() + desc += '%dx %s\n' % (amount, product.name) + self.change_credit(-total_value, BUY_SUBJECT, desc) return True else: return False