bornhack-website/src/shop/models.py

591 lines
18 KiB
Python
Raw Normal View History

import logging
from django.conf import settings
from django.db import models
from django.db.models.aggregates import Sum
2016-06-19 06:43:56 +00:00
from django.contrib import messages
from django.contrib.postgres.fields import DateTimeRangeField, JSONField
2016-05-15 22:09:00 +00:00
from django.utils.text import slugify
from django.utils.translation import ugettext_lazy as _
2016-05-06 20:33:59 +00:00
from django.utils import timezone
2018-04-03 16:44:10 +00:00
from django.urls import reverse_lazy
from django.core.exceptions import ValidationError
2016-05-31 05:43:51 +00:00
from decimal import Decimal
2016-05-31 06:19:42 +00:00
from datetime import timedelta
from unidecode import unidecode
from django.utils.dateparse import parse_datetime
from utils.models import UUIDModel, CreatedUpdatedModel
from tickets.models import ShopTicket
from .managers import ProductQuerySet, OrderQuerySet
logger = logging.getLogger("bornhack.%s" % __name__)
2016-07-12 20:33:53 +00:00
class CustomOrder(CreatedUpdatedModel):
text = models.TextField(
help_text=_('The invoice text')
)
customer = models.TextField(
2016-07-12 21:32:05 +00:00
help_text=_('The customer info for this order')
)
2016-07-12 20:33:53 +00:00
amount = models.IntegerField(
help_text=_('Amount of this custom order (in DKK, including VAT).')
)
paid = models.BooleanField(
verbose_name=_('Paid?'),
help_text=_('Check when this custom order has been paid (or if it gets cancelled out by a Credit Note)'),
2016-07-12 20:33:53 +00:00
default=False,
)
danish_vat = models.BooleanField(
help_text="Danish VAT?",
default=True
)
def __str__(self):
2016-07-12 20:33:53 +00:00
return 'custom order id #%s' % self.pk
@property
def vat(self):
if self.danish_vat:
return Decimal(round(self.amount*Decimal(0.2), 2))
else:
return 0
2016-07-12 20:33:53 +00:00
class Order(CreatedUpdatedModel):
2016-05-15 22:09:00 +00:00
class Meta:
unique_together = ('user', 'open')
2016-05-29 16:02:12 +00:00
ordering = ['-created']
2016-05-15 22:09:00 +00:00
products = models.ManyToManyField(
'shop.Product',
through='shop.OrderProductRelation'
)
2016-06-18 18:59:07 +00:00
user = models.ForeignKey(
'auth.User',
verbose_name=_('User'),
2016-07-12 20:33:53 +00:00
help_text=_('The user this shop order belongs to.'),
2016-06-18 18:59:07 +00:00
related_name='orders',
2018-03-04 15:26:35 +00:00
on_delete=models.PROTECT,
2016-06-18 18:59:07 +00:00
)
paid = models.BooleanField(
verbose_name=_('Paid?'),
2016-07-12 20:33:53 +00:00
help_text=_('Whether this shop order has been paid.'),
default=False,
)
2016-05-15 22:09:00 +00:00
open = models.NullBooleanField(
verbose_name=_('Open?'),
2016-07-12 20:33:53 +00:00
help_text=_('Whether this shop order is open or not. "None" means closed.'),
2016-05-15 22:09:00 +00:00
default=True,
2016-05-13 06:37:47 +00:00
)
CREDIT_CARD = 'credit_card'
BLOCKCHAIN = 'blockchain'
BANK_TRANSFER = 'bank_transfer'
CASH = 'cash'
PAYMENT_METHODS = [
2016-05-15 22:09:00 +00:00
CREDIT_CARD,
BLOCKCHAIN,
BANK_TRANSFER,
2016-11-09 11:27:42 +00:00
CASH,
2016-05-15 22:09:00 +00:00
]
PAYMENT_METHOD_CHOICES = [
(CREDIT_CARD, 'Credit card'),
(BLOCKCHAIN, 'Blockchain'),
(BANK_TRANSFER, 'Bank transfer'),
2016-11-09 11:27:42 +00:00
(CASH, 'Cash'),
]
payment_method = models.CharField(
max_length=50,
2016-05-15 22:09:00 +00:00
choices=PAYMENT_METHOD_CHOICES,
2016-06-04 08:01:31 +00:00
default='',
2017-03-23 19:21:19 +00:00
blank=True
)
2016-06-01 09:10:06 +00:00
cancelled = models.BooleanField(default=False)
refunded = models.BooleanField(
verbose_name=_('Refunded?'),
help_text=_('Whether this order has been refunded.'),
default=False,
)
2016-11-09 13:34:55 +00:00
customer_comment = models.TextField(
verbose_name=_('Customer comment'),
help_text=_('If you have any comments about the order please enter them here.'),
default='',
blank=True,
2016-11-09 13:34:55 +00:00
)
2016-06-01 09:10:06 +00:00
objects = OrderQuerySet.as_manager()
def __str__(self):
2016-07-12 20:33:53 +00:00
return 'shop order id #%s' % self.pk
2016-05-30 15:32:53 +00:00
def get_number_of_items(self):
return self.products.aggregate(
sum=Sum('orderproductrelation__quantity')
)['sum']
@property
def vat(self):
2016-05-31 05:47:18 +00:00
return Decimal(self.total*Decimal(0.2))
@property
def total(self):
2016-06-04 07:49:22 +00:00
if self.products.all():
return Decimal(self.products.aggregate(
sum=Sum(
models.F('orderproductrelation__product__price') *
models.F('orderproductrelation__quantity'),
output_field=models.IntegerField()
)
)['sum'])
else:
return False
2016-05-29 10:29:38 +00:00
def get_coinify_callback_url(self, request):
""" Check settings for an alternative COINIFY_CALLBACK_HOSTNAME otherwise use the one from the request """
2017-07-12 15:05:27 +00:00
if hasattr(settings, 'COINIFY_CALLBACK_HOSTNAME') and settings.COINIFY_CALLBACK_HOSTNAME:
host = settings.COINIFY_CALLBACK_HOSTNAME
else:
host = request.get_host()
return 'https://' + host + str(reverse_lazy('shop:coinify_callback', kwargs={'pk': self.pk}))
2016-05-29 10:29:38 +00:00
def get_coinify_thanks_url(self, request):
return 'https://' + request.get_host() + str(reverse_lazy('shop:coinify_thanks', kwargs={'pk': self.pk}))
2016-05-17 05:06:25 +00:00
def get_epay_accept_url(self, request):
return 'https://' + request.get_host() + str(reverse_lazy('shop:epay_thanks', kwargs={'pk': self.pk}))
def get_cancel_url(self, request):
return 'https://' + request.get_host() + str(reverse_lazy('shop:order_detail', kwargs={'pk': self.pk}))
2016-05-17 05:06:25 +00:00
2016-05-25 20:48:02 +00:00
def get_epay_callback_url(self, request):
return 'https://' + request.get_host() + str(reverse_lazy('shop:epay_callback', kwargs={'pk': self.pk}))
2016-05-17 05:06:25 +00:00
@property
def description(self):
return "Order #%s" % self.pk
2016-05-06 20:33:59 +00:00
def get_absolute_url(self):
return str(reverse_lazy('shop:order_detail', kwargs={'pk': self.pk}))
def mark_as_paid(self, request):
self.paid = True
self.open = None
for order_product in self.orderproductrelation_set.all():
# if this is a Ticket product?
if order_product.product.ticket_type:
# create the number of tickets required
for _ in range(0, order_product.quantity):
ticket = ShopTicket(
ticket_type=order_product.product.ticket_type,
order=self,
product=order_product.product,
)
ticket.save()
if request:
messages.success(request, "Created %s tickets of type: %s" % (order_product.quantity, order_product.product.ticket_type.name))
# and mark the OPR as handed_out=True
2018-03-04 14:38:40 +00:00
order_product.handed_out = True
order_product.save()
self.save()
2016-06-19 06:49:30 +00:00
def mark_as_refunded(self, request):
2016-06-19 06:38:43 +00:00
if not self.paid:
2016-06-19 06:49:30 +00:00
messages.error(request, "Order %s is not paid, so cannot mark it as refunded!" % self.pk)
2016-06-19 06:38:43 +00:00
else:
2018-03-04 14:38:40 +00:00
self.refunded = True
# delete any tickets related to this order
if self.tickets.all():
messages.success(request, "Order %s marked as refunded, deleting %s tickets..." % (self.pk, self.tickets.count()))
self.tickets.all().delete()
else:
messages.success(request, "Order %s marked as refunded, no tickets to delete" % self.pk)
2016-06-19 06:38:43 +00:00
self.save()
2016-05-29 12:28:47 +00:00
def is_not_handed_out(self):
if self.orderproductrelation_set.filter(handed_out=True).count() == 0:
return True
else:
return False
def is_partially_handed_out(self):
if self.orderproductrelation_set.filter(handed_out=True).count() != 0 and self.orderproductrelation_set.filter(handed_out=False).count() != 0:
# some products are handed out, others are not
return True
else:
return False
def is_fully_handed_out(self):
if self.orderproductrelation_set.filter(handed_out=False).count() == 0:
return True
else:
return False
@property
def handed_out_status(self):
if self.is_not_handed_out():
return "no"
elif self.is_partially_handed_out():
return "partially"
elif self.is_fully_handed_out():
return "fully"
else:
return False
2016-06-01 09:10:06 +00:00
def mark_as_cancelled(self):
self.cancelled = True
self.open = None
self.save()
@property
def coinifyapiinvoice(self):
if not self.coinify_api_invoices.exists():
return False
for tempinvoice in self.coinify_api_invoices.all():
# we already have a coinifyinvoice for this order, check if it expired
if not tempinvoice.expired:
# this invoice is not expired, we are good to go
return tempinvoice
# nope
return False
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()
public = models.BooleanField(default=True)
def __str__(self):
return self.name
2016-05-15 22:09:00 +00:00
def save(self, **kwargs):
self.slug = slugify(self.name)
super(ProductCategory, self).save(**kwargs)
class Product(CreatedUpdatedModel, UUIDModel):
class Meta:
verbose_name = 'Product'
verbose_name_plural = 'Products'
2016-08-15 16:21:12 +00:00
ordering = ['available_in', 'price', 'name']
2016-05-15 22:09:00 +00:00
category = models.ForeignKey(
'shop.ProductCategory',
2018-03-04 15:26:35 +00:00
related_name='products',
on_delete=models.PROTECT,
2016-05-15 22:09:00 +00:00
)
name = models.CharField(max_length=150)
slug = models.SlugField(unique=True, max_length=100)
price = models.IntegerField(
2016-07-12 20:33:53 +00:00
help_text=_('Price of the product (in DKK, including VAT).')
)
description = models.TextField()
available_in = DateTimeRangeField(
2016-05-06 20:33:59 +00:00
help_text=_(
'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'
)
)
ticket_type = models.ForeignKey(
'tickets.TicketType',
2018-03-04 15:26:35 +00:00
on_delete=models.PROTECT,
null=True,
blank=True
)
stock_amount = models.IntegerField(
help_text=(
'Initial amount available in stock if there is a limited '
'supply, e.g. fridge space'
),
null=True,
blank=True
)
objects = ProductQuerySet.as_manager()
2016-05-06 20:33:59 +00:00
def __str__(self):
2016-05-06 20:33:59 +00:00
return '{} ({} DKK)'.format(
self.name,
self.price,
)
def clean(self):
if self.category.name == 'Tickets' and not self.ticket_type:
raise ValidationError(
'Products with category Tickets need a ticket_type'
)
2016-05-06 20:33:59 +00:00
def is_available(self):
""" Is the product available or not?
Checks for the following:
- Whether now is in the self.available_in
- If a stock is defined, that there are items left
"""
predicates = [self.is_time_available]
if self.stock_amount:
predicates.append(self.is_stock_available)
return all(predicates)
@property
def is_time_available(self):
2016-05-06 20:33:59 +00:00
now = timezone.now()
time_available = now in self.available_in
return time_available
def is_old(self):
now = timezone.now()
if hasattr(self.available_in, 'upper') and self.available_in.upper:
return self.available_in.upper < now
return False
def is_upcoming(self):
now = timezone.now()
return self.available_in.lower > now
@property
def left_in_stock(self):
if self.stock_amount:
sold = OrderProductRelation.objects.filter(
product=self,
order__paid=True,
).aggregate(Sum('quantity'))['quantity__sum']
total_left = self.stock_amount - (sold or 0)
return total_left
return None
@property
def is_stock_available(self):
if self.stock_amount:
stock_available = self.left_in_stock > 0
return stock_available
# If there is no stock defined the product is generally available.
return True
class OrderProductRelation(CreatedUpdatedModel):
2018-03-04 15:26:35 +00:00
order = models.ForeignKey('shop.Order', on_delete=models.PROTECT)
product = models.ForeignKey('shop.Product', on_delete=models.PROTECT)
quantity = models.PositiveIntegerField()
handed_out = models.BooleanField(default=False)
2016-05-15 22:09:00 +00:00
@property
def total(self):
2016-05-31 06:01:55 +00:00
return Decimal(self.product.price * self.quantity)
2016-05-15 22:09:00 +00:00
class EpayCallback(CreatedUpdatedModel, UUIDModel):
class Meta:
verbose_name = 'Epay Callback'
verbose_name_plural = 'Epay Callbacks'
ordering = ['-created']
2017-04-08 09:04:39 +00:00
payload = JSONField()
md5valid = models.BooleanField(default=False)
def __str__(self):
2016-05-17 06:08:30 +00:00
return 'callback at %s (md5 valid: %s)' % (self.created, self.md5valid)
class EpayPayment(CreatedUpdatedModel, UUIDModel):
class Meta:
verbose_name = 'Epay Payment'
verbose_name_plural = 'Epay Payments'
2018-04-03 16:44:10 +00:00
order = models.OneToOneField('shop.Order', on_delete=models.PROTECT)
2018-03-04 15:26:35 +00:00
callback = models.ForeignKey('shop.EpayCallback', on_delete=models.PROTECT)
txnid = models.IntegerField()
2016-05-17 13:09:40 +00:00
2016-06-18 18:51:53 +00:00
class CreditNote(CreatedUpdatedModel):
2016-06-19 19:37:25 +00:00
class Meta:
ordering = ['-created']
amount = models.DecimalField(
max_digits=10,
decimal_places=2
)
text = models.TextField(
help_text="Description of what this credit note covers"
)
2016-06-18 18:51:53 +00:00
pdf = models.FileField(
null=True,
blank=True,
upload_to='creditnotes/'
)
2016-06-18 18:51:53 +00:00
user = models.ForeignKey(
'auth.User',
verbose_name=_('User'),
help_text=_('The user this credit note belongs to, if any.'),
2016-06-18 18:51:53 +00:00
related_name='creditnotes',
2018-03-04 15:26:35 +00:00
on_delete=models.PROTECT,
null=True,
blank=True
2016-06-18 18:51:53 +00:00
)
customer = models.TextField(
help_text="Customer info if no user is selected",
blank=True,
default='',
)
danish_vat = models.BooleanField(
help_text="Danish VAT?",
default=True
)
2016-06-18 18:51:53 +00:00
paid = models.BooleanField(
verbose_name=_('Paid?'),
2016-06-19 19:37:25 +00:00
help_text=_('Whether the amount in this creditnote has been paid back to the customer.'),
2016-06-18 18:51:53 +00:00
default=False,
)
2016-06-18 18:51:53 +00:00
sent_to_customer = models.BooleanField(default=False)
def clean(self):
errors = []
if self.user and self.customer:
msg = "Customer info should be blank if a user is selected."
errors.append(ValidationError({'user', msg}))
errors.append(ValidationError({'customer', msg}))
if not self.user and not self.customer:
msg = "Either pick a user or fill in Customer info"
errors.append(ValidationError({'user', msg}))
errors.append(ValidationError({'customer', msg}))
if errors:
raise ValidationError(errors)
def __str__(self):
if self.user:
return 'creditnoote#%s - %s DKK (customer: user %s)' % (
self.id,
self.amount,
self.user.email,
)
else:
2017-09-17 13:27:47 +00:00
return 'creditnoote#%s - %s DKK (customer: %s)' % (
self.id,
self.amount,
self.customer,
)
2016-06-18 18:51:53 +00:00
@property
def vat(self):
if self.danish_vat:
return Decimal(round(self.amount*Decimal(0.2), 2))
else:
return 0
2016-06-18 18:51:53 +00:00
@property
def filename(self):
return 'bornhack_creditnote_%s.pdf' % self.pk
2016-06-18 18:59:07 +00:00
2016-05-17 13:09:40 +00:00
class Invoice(CreatedUpdatedModel):
2018-04-03 16:44:10 +00:00
order = models.OneToOneField(
'shop.Order',
null=True,
blank=True,
on_delete=models.PROTECT
)
customorder = models.OneToOneField(
'shop.CustomOrder',
null=True,
blank=True,
on_delete=models.PROTECT
)
2016-05-30 18:15:35 +00:00
pdf = models.FileField(null=True, blank=True, upload_to='invoices/')
2016-05-17 13:09:40 +00:00
sent_to_customer = models.BooleanField(default=False)
def __str__(self):
2016-07-12 20:33:53 +00:00
if self.order:
return 'invoice#%s - shop order %s - %s - total %s DKK (sent to %s: %s)' % (
self.id,
self.order.id,
self.order.created,
self.order.total,
self.order.user.email,
self.sent_to_customer,
)
elif self.customorder:
return 'invoice#%s - custom order %s - %s - amount %s DKK (customer: %s)' % (
self.id,
self.customorder.id,
self.customorder.created,
self.customorder.amount,
unidecode(self.customorder.customer),
2016-07-12 20:33:53 +00:00
)
2016-05-17 13:09:40 +00:00
@property
def filename(self):
return 'bornhack_invoice_%s.pdf' % self.pk
2016-05-31 05:42:01 +00:00
def regretdate(self):
2016-05-31 06:17:14 +00:00
return self.created+timedelta(days=15)
2016-05-25 20:48:02 +00:00
2016-05-31 06:01:55 +00:00
2016-05-25 20:48:02 +00:00
class CoinifyAPIInvoice(CreatedUpdatedModel):
coinify_id = models.IntegerField(null=True)
2016-05-25 20:48:02 +00:00
invoicejson = JSONField()
order = models.ForeignKey('shop.Order', related_name="coinify_api_invoices", on_delete=models.PROTECT)
2016-05-25 20:48:02 +00:00
def __str__(self):
return "coinifyinvoice for order #%s" % self.order.id
@property
def expired(self):
2018-03-04 14:38:40 +00:00
return parse_datetime(self.invoicejson['expire_time']) < timezone.now()
class CoinifyAPICallback(CreatedUpdatedModel):
headers = JSONField()
2017-04-08 09:04:39 +00:00
payload = JSONField(blank=True)
body = models.TextField(default='')
order = models.ForeignKey('shop.Order', related_name="coinify_api_callbacks", on_delete=models.PROTECT)
authenticated = models.BooleanField(default=False)
2016-05-25 20:48:02 +00:00
def __str__(self):
return 'order #%s callback at %s' % (self.order.id, self.created)
2016-05-25 20:48:02 +00:00
class CoinifyAPIRequest(CreatedUpdatedModel):
order = models.ForeignKey('shop.Order', related_name="coinify_api_requests", on_delete=models.PROTECT)
method = models.CharField(max_length=100)
payload = JSONField()
2017-05-22 17:42:20 +00:00
response = JSONField()
def __str__(self):
return 'order %s api request %s' % (self.order.id, self.method)