2016-04-22 20:38:44 +00:00
|
|
|
from django.db import models
|
2016-05-14 17:39:34 +00:00
|
|
|
from django.db.models.aggregates import Sum
|
2016-05-10 15:55:54 +00:00
|
|
|
from django.contrib.postgres.fields import DateTimeRangeField, JSONField
|
2016-05-15 22:09:00 +00:00
|
|
|
from django.utils.text import slugify
|
2016-04-22 20:38:44 +00:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2016-05-06 20:33:59 +00:00
|
|
|
from django.utils import timezone
|
2016-04-22 20:38:44 +00:00
|
|
|
from bornhack.utils import CreatedUpdatedModel, UUIDModel
|
2016-05-10 20:20:01 +00:00
|
|
|
from .managers import ProductQuerySet
|
2016-05-06 20:33:59 +00:00
|
|
|
|
2016-04-22 20:38:44 +00:00
|
|
|
|
2016-05-10 20:20:01 +00:00
|
|
|
class Order(CreatedUpdatedModel):
|
|
|
|
|
2016-05-15 22:09:00 +00:00
|
|
|
class Meta:
|
|
|
|
unique_together = ('user', 'open')
|
|
|
|
|
2016-05-10 20:20:01 +00:00
|
|
|
products = models.ManyToManyField(
|
|
|
|
'shop.Product',
|
|
|
|
through='shop.OrderProductRelation'
|
|
|
|
)
|
2016-04-22 20:38:44 +00:00
|
|
|
|
|
|
|
user = models.ForeignKey(
|
|
|
|
'auth.User',
|
|
|
|
verbose_name=_('User'),
|
2016-05-10 20:20:01 +00:00
|
|
|
help_text=_('The user this order belongs to.'),
|
|
|
|
related_name='orders',
|
2016-04-22 20:38:44 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
paid = models.BooleanField(
|
|
|
|
verbose_name=_('Paid?'),
|
2016-05-10 20:20:01 +00:00
|
|
|
help_text=_('Whether this order has been paid.'),
|
2016-04-22 20:38:44 +00:00
|
|
|
default=False,
|
|
|
|
)
|
|
|
|
|
2016-05-15 22:09:00 +00:00
|
|
|
open = models.NullBooleanField(
|
|
|
|
verbose_name=_('Open?'),
|
|
|
|
help_text=_('Whether this order is open or not. "None" means closed.'),
|
|
|
|
default=True,
|
2016-05-13 06:37:47 +00:00
|
|
|
)
|
|
|
|
|
2016-05-10 20:20:01 +00:00
|
|
|
camp = models.ForeignKey(
|
|
|
|
'camps.Camp',
|
|
|
|
verbose_name=_('Camp'),
|
|
|
|
help_text=_('The camp this order is for.'),
|
2016-04-22 20:38:44 +00:00
|
|
|
)
|
|
|
|
|
2016-05-10 15:55:54 +00:00
|
|
|
CREDIT_CARD = 'credit_card'
|
2016-05-11 06:37:39 +00:00
|
|
|
BLOCKCHAIN = 'blockchain'
|
2016-05-10 15:55:54 +00:00
|
|
|
BANK_TRANSFER = 'bank_transfer'
|
|
|
|
|
|
|
|
PAYMENT_METHODS = [
|
2016-05-15 22:09:00 +00:00
|
|
|
CREDIT_CARD,
|
|
|
|
BLOCKCHAIN,
|
|
|
|
BANK_TRANSFER,
|
|
|
|
]
|
|
|
|
|
|
|
|
PAYMENT_METHOD_CHOICES = [
|
2016-05-10 15:55:54 +00:00
|
|
|
(CREDIT_CARD, 'Credit card'),
|
2016-05-11 06:37:39 +00:00
|
|
|
(BLOCKCHAIN, 'Blockchain'),
|
2016-05-10 15:55:54 +00:00
|
|
|
(BANK_TRANSFER, 'Bank transfer'),
|
|
|
|
]
|
|
|
|
|
|
|
|
payment_method = models.CharField(
|
|
|
|
max_length=50,
|
2016-05-15 22:09:00 +00:00
|
|
|
choices=PAYMENT_METHOD_CHOICES,
|
2016-05-12 17:08:54 +00:00
|
|
|
default=BLOCKCHAIN
|
2016-05-10 15:55:54 +00:00
|
|
|
)
|
|
|
|
|
2016-05-14 17:39:34 +00:00
|
|
|
def get_number_of_items(self):
|
|
|
|
return self.products.aggregate(
|
|
|
|
sum=Sum('orderproductrelation__quantity')
|
|
|
|
)['sum']
|
|
|
|
|
2016-05-16 14:14:16 +00:00
|
|
|
@property
|
|
|
|
def vat(self):
|
|
|
|
return (self.total/100)*25
|
|
|
|
|
|
|
|
@property
|
|
|
|
def total(self):
|
2016-05-16 14:09:25 +00:00
|
|
|
return self.products.aggregate(
|
|
|
|
sum=Sum(
|
|
|
|
models.F('orderproductrelation__product__price') *
|
|
|
|
models.F('orderproductrelation__quantity'),
|
|
|
|
output_field=models.IntegerField()
|
|
|
|
)
|
|
|
|
)['sum']
|
|
|
|
|
2016-05-06 20:33:59 +00:00
|
|
|
|
2016-05-10 20:20:01 +00:00
|
|
|
class ProductCategory(CreatedUpdatedModel, UUIDModel):
|
|
|
|
class Meta:
|
|
|
|
verbose_name = 'Product category'
|
|
|
|
verbose_name_plural = 'Product categories'
|
|
|
|
|
|
|
|
name = models.CharField(max_length=150)
|
2016-05-15 22:09:00 +00:00
|
|
|
slug = models.SlugField()
|
2016-05-10 20:20:01 +00:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
2016-05-10 15:55:54 +00:00
|
|
|
|
2016-05-15 22:09:00 +00:00
|
|
|
def save(self, **kwargs):
|
|
|
|
self.slug = slugify(self.name)
|
|
|
|
super(ProductCategory, self).save(**kwargs)
|
|
|
|
|
2016-04-22 20:38:44 +00:00
|
|
|
|
2016-05-10 20:20:01 +00:00
|
|
|
class Product(CreatedUpdatedModel, UUIDModel):
|
2016-04-22 20:38:44 +00:00
|
|
|
class Meta:
|
2016-05-10 20:20:01 +00:00
|
|
|
verbose_name = 'Product'
|
|
|
|
verbose_name_plural = 'Products'
|
2016-05-06 20:33:59 +00:00
|
|
|
ordering = ['available_in']
|
2016-04-22 20:38:44 +00:00
|
|
|
|
2016-05-15 22:09:00 +00:00
|
|
|
category = models.ForeignKey(
|
|
|
|
'shop.ProductCategory',
|
|
|
|
related_name='products'
|
|
|
|
)
|
2016-04-22 20:38:44 +00:00
|
|
|
|
2016-05-10 20:20:01 +00:00
|
|
|
name = models.CharField(max_length=150)
|
2016-05-15 22:09:00 +00:00
|
|
|
slug = models.SlugField()
|
2016-04-22 20:38:44 +00:00
|
|
|
|
|
|
|
price = models.IntegerField(
|
2016-05-10 20:20:01 +00:00
|
|
|
help_text=_('Price of the product (in DKK).')
|
2016-04-22 20:38:44 +00:00
|
|
|
)
|
|
|
|
|
2016-05-10 20:20:01 +00:00
|
|
|
description = models.TextField()
|
|
|
|
|
2016-04-22 20:38:44 +00:00
|
|
|
available_in = DateTimeRangeField(
|
2016-05-06 20:33:59 +00:00
|
|
|
help_text=_(
|
2016-05-10 20:20:01 +00:00
|
|
|
'Which period is this product available for purchase? | '
|
2016-05-06 20:33:59 +00:00
|
|
|
'(Format: YYYY-MM-DD HH:MM) | Only one of start/end is required'
|
|
|
|
)
|
2016-04-22 20:38:44 +00:00
|
|
|
)
|
|
|
|
|
2016-05-10 20:20:01 +00:00
|
|
|
objects = ProductQuerySet.as_manager()
|
2016-05-06 20:33:59 +00:00
|
|
|
|
2016-04-22 20:38:44 +00:00
|
|
|
def __str__(self):
|
2016-05-06 20:33:59 +00:00
|
|
|
return '{} ({} DKK)'.format(
|
|
|
|
self.name,
|
|
|
|
self.price,
|
|
|
|
)
|
|
|
|
|
2016-05-15 22:09:00 +00:00
|
|
|
def save(self, **kwargs):
|
|
|
|
self.slug = slugify(self.name)
|
|
|
|
super(Product, self).save(**kwargs)
|
|
|
|
|
2016-05-06 20:33:59 +00:00
|
|
|
def is_available(self):
|
|
|
|
now = timezone.now()
|
|
|
|
return now in self.available_in
|
2016-05-10 15:55:54 +00:00
|
|
|
|
|
|
|
|
2016-05-16 13:25:12 +00:00
|
|
|
class OrderProductRelation(CreatedUpdatedModel):
|
2016-05-10 20:20:01 +00:00
|
|
|
order = models.ForeignKey('shop.Order')
|
|
|
|
product = models.ForeignKey('shop.Product')
|
|
|
|
quantity = models.PositiveIntegerField()
|
|
|
|
handed_out = models.BooleanField(default=False)
|
|
|
|
|
2016-05-15 22:09:00 +00:00
|
|
|
@property
|
|
|
|
def total(self):
|
|
|
|
return self.product.price * self.quantity
|
|
|
|
|
2016-05-10 20:20:01 +00:00
|
|
|
|
2016-05-10 15:55:54 +00:00
|
|
|
class EpayCallback(CreatedUpdatedModel, UUIDModel):
|
|
|
|
class Meta:
|
|
|
|
verbose_name = 'Epay Callback'
|
|
|
|
verbose_name_plural = 'Epay Callbacks'
|
2016-05-11 06:37:39 +00:00
|
|
|
|
2016-05-10 15:55:54 +00:00
|
|
|
payload = JSONField()
|
|
|
|
|
|
|
|
|
|
|
|
class EpayPayment(CreatedUpdatedModel, UUIDModel):
|
|
|
|
class Meta:
|
|
|
|
verbose_name = 'Epay Payment'
|
|
|
|
verbose_name_plural = 'Epay Payments'
|
|
|
|
|
2016-05-10 20:20:01 +00:00
|
|
|
order = models.OneToOneField('shop.Order')
|
|
|
|
callback = models.ForeignKey('shop.EpayCallback')
|
2016-05-10 15:55:54 +00:00
|
|
|
txnid = models.IntegerField()
|