models.py 2.47 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
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
from django.db import models, DataError
from django.utils.translation import ugettext_lazy as _

from accounting.models import CurrencyField
from counter.models import Counter, Product
from core.models import User

class Basket(models.Model):
    """
    Basket is built when the user validate its session basket and asks for payment
    """
    user = models.ForeignKey(User, related_name='baskets', verbose_name=_('user'), blank=False)
    date = models.DateTimeField(_('date'), auto_now=True)

    def get_total(self):
        total = 0
        for i in self.items.all():
            total += i.quantity * i.product_unit_price
        return total

class Invoice(models.Model):
    """
    Invoices are generated once the payment has been validated
    """
    user = models.ForeignKey(User, related_name='invoices', verbose_name=_('user'), blank=False)
    date = models.DateTimeField(_('date'), auto_now=True)
    payment_method = models.CharField(choices=[('CREDIT_CARD', _('Credit card')), ('SITH_ACCOUNT', _('Sith account'))],
            max_length=20, verbose_name=_('payment method'))
    validated = models.BooleanField(_("validated"), default=False)

    def get_total(self):
        total = 0
        for i in self.items.all():
            total += i.quantity * i.product_unit_price
        return total

    def validate(self, *args, **kwargs):
        if self.validated:
            raise DataError(_("Invoice already validated"))
        if self.payment_method == "SITH_ACCOUNT":
            self.user.customer.amount -= self.get_total()
            self.user.customer.save()
Skia's avatar
Skia committed
43
44
45
46
47
        else:
            for i in self.items.filter(type="REFILLING").all():
                self.user.customer.amount += i.product_unit_price * i.quantity
            self.user.customer.save()

Skia's avatar
Skia committed
48
49
        self.validated = True
        self.save()
50
51
52

class AbstractBaseItem(models.Model):
    product_name = models.CharField(_('product name'), max_length=255)
Skia's avatar
Skia committed
53
    type = models.CharField(_('product type'), max_length=255)
54
55
56
57
58
59
60
61
62
63
64
65
66
67
    product_unit_price = CurrencyField(_('unit price'))
    quantity = models.IntegerField(_('quantity'))

    class Meta:
        abstract = True

    def __str__(self):
        return "Item: %s (%s) x%d" % (self.product_name, self.product_unit_price, self.quantity)

class BasketItem(AbstractBaseItem):
    basket = models.ForeignKey(Basket, related_name='items', verbose_name=_('basket'))

class InvoiceItem(AbstractBaseItem):
    invoice = models.ForeignKey(Invoice, related_name='items', verbose_name=_('invoice'))