Browse Source

added buy_products method to Account

master
Frederic 11 years ago
parent
commit
7668291540
  1. 38
      cash/models.py

38
cash/models.py

@ -1,6 +1,7 @@ @@ -1,6 +1,7 @@
from django.db import models
from django.contrib.auth.models import User
class Account(models.Model):
user = models.OneToOneField(User)
card_number = models.CharField(max_length=32, unique=True)
@ -11,6 +12,42 @@ class Account(models.Model): @@ -11,6 +12,42 @@ class Account(models.Model):
def __unicode__(self):
return self.user.username
def buy_products(self, products):
# TODO place it somewhere else
MAX_DEBIT = -3500
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:
self.credit -= total_value
self.save()
descr = ''
for product in products.keys():
amount = products[product]
logentry = SalesLogEntry(account=self, product=product,
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()
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)
@ -50,3 +87,4 @@ class SalesLogEntry(models.Model): @@ -50,3 +87,4 @@ class SalesLogEntry(models.Model):
timestamp = models.DateTimeField(auto_now_add=True)

Loading…
Cancel
Save