models.py 3.39 KB
Newer Older
1
2
3
4
from django.db import models, DataError
from django.utils.translation import ugettext_lazy as _

from accounting.models import CurrencyField
Skia's avatar
Skia committed
5
from counter.models import Counter, Product, Customer
6
7
8
9
from core.models import User

class Basket(models.Model):
    """
Skia's avatar
Skia committed
10
    Basket is built when the user connects to an eboutic page
11
12
13
14
    """
    user = models.ForeignKey(User, related_name='baskets', verbose_name=_('user'), blank=False)
    date = models.DateTimeField(_('date'), auto_now=True)

Skia's avatar
Skia committed
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
    def add_product(self, p, q = 1):
        item = self.items.filter(product_id=p.id).first()
        if item is None:
            BasketItem(basket=self, product_id=p.id, product_name=p.name, type=p.product_type.name,
                    quantity=q, product_unit_price=p.selling_price).save()
        else:
            item.quantity += q
            item.save()

    def del_product(self, p, q = 1):
        item = self.items.filter(product_id=p.id).first()
        if item is not None:
            item.quantity -= q
            item.save()
        if item.quantity <= 0:
            item.delete()

32
33
34
35
36
37
    def get_total(self):
        total = 0
        for i in self.items.all():
            total += i.quantity * i.product_unit_price
        return total

Skia's avatar
Skia committed
38
39
40
    def __str__(self):
        return "Basket (%d items)" % self.items.all().count()

41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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"))
Skia's avatar
Skia committed
60
61
62
        from counter.models import Customer
        if not Customer.objects.filter(user=self.user).exists():
            Customer(user=self.user, account_id=Customer.generate_account_id(), amount=0).save()
63
64
65
        if self.payment_method == "SITH_ACCOUNT":
            self.user.customer.amount -= self.get_total()
            self.user.customer.save()
Skia's avatar
Skia committed
66
67
68
69
70
        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
71
72
        self.validated = True
        self.save()
73
74

class AbstractBaseItem(models.Model):
Skia's avatar
Skia committed
75
    product_id = models.IntegerField(_('product id'))
76
    product_name = models.CharField(_('product name'), max_length=255)
Skia's avatar
Skia committed
77
    type = models.CharField(_('product type'), max_length=255)
78
79
80
81
82
83
84
85
86
87
88
89
90
91
    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'))