You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

108 lines
3.4 KiB

from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.dispatch import receiver
from django_auth_ldap.backend import populate_user
class Account(models.Model):
user = models.OneToOneField(User)
card_number = models.CharField(max_length=32, unique=True, blank=True,
null=True)
pin = models.CharField(max_length=32, blank=True)
daily_digest = models.BooleanField()
credit = models.DecimalField(max_digits=5, decimal_places=2, default=0)
def __unicode__(self):
return self.user.username
@receiver(post_save, sender=User)
def user_post_save_handler(sender, instance, created, **kwargs):
if created:
# We don't have ldap_user on creation, so just add the account
account = Account(user=instance)
account.save()
else:
# When we alraedy have an account, we can add the number form LDAP (mongo shit)
if hasattr(instance, 'ldap_user') and instance.ldap_user.attrs.has_key('employeenumber'):
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
BUY_SUBJECT = 'Artikel gekauft'
if min(products.values()) <= 0:
raise ValueError('Non-positive amount in products dict.')
total_value = sum(map(lambda p: p.price * products[p], products.keys()))
if self.credit - total_value >= MAX_DEBIT:
desc = ''
for product in products.keys():
if not product.active:
raise ValueError('Trying to buy a disabled product.')
amount = products[product]
logentry = SalesLogEntry(account=self, product=product,
count=amount, unit_price=product.price)
logentry.save()
desc += '%dx %s\n' % (amount, product.name)
self.change_credit(-total_value, BUY_SUBJECT, desc)
return True
else:
return False
def buy_product(self, product, amount=1):
return self.buy_products({product: amount})
class ProductCategory(models.Model):
name = models.CharField(max_length=32, unique=True)
comment = models.CharField(max_length=128, blank=True)
def __unicode__(self):
return "%s (%s)" % (self.name, self.comment)
class Product(models.Model):
name = models.CharField(max_length=32, unique=True)
price = models.DecimalField(max_digits=5, decimal_places=2)
active = models.BooleanField(default = True)
category = models.ForeignKey(ProductCategory, blank=True, null=True)
def __unicode__(self):
return self.name
class ProductBarcode(models.Model):
barcode = models.CharField(max_length=32, unique=True)
comment = models.CharField(max_length=128, blank=True)
product = models.ForeignKey(Product)
def __unicode__(self):
return self.barcode
class Transaction(models.Model):
account = models.ForeignKey(Account)
timestamp = models.DateTimeField(auto_now_add=True)
subject = models.CharField(max_length=32)
description = models.TextField()
amount = models.DecimalField(max_digits=5, decimal_places=2)
class SalesLogEntry(models.Model):
account = models.ForeignKey(Account)
product = models.ForeignKey(Product)
count = models.IntegerField()
unit_price = models.DecimalField(max_digits=5, decimal_places=2)
timestamp = models.DateTimeField(auto_now_add=True)