2016-06-03 20:17:22 +02:00
|
|
|
from django.db import models
|
|
|
|
from django.conf import settings
|
2016-06-04 01:24:40 +02:00
|
|
|
from django.db.models import Sum
|
2016-06-03 20:17:22 +02:00
|
|
|
|
2016-06-04 14:52:08 +02:00
|
|
|
import logging
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2016-06-03 20:17:22 +02:00
|
|
|
AUTH_USER_MODEL = getattr(settings, 'AUTH_USER_MODEL', 'auth.User')
|
|
|
|
|
|
|
|
|
|
|
|
class Refund(models.Model):
|
|
|
|
title = models.CharField(max_length=100)
|
|
|
|
date = models.DateField()
|
|
|
|
created_at = models.DateTimeField(auto_now_add=True)
|
|
|
|
updated_at = models.DateTimeField(auto_now=True)
|
|
|
|
user = models.ForeignKey(
|
|
|
|
AUTH_USER_MODEL,
|
2016-06-04 03:44:41 +02:00
|
|
|
on_delete=models.PROTECT,
|
|
|
|
null=True
|
2016-06-03 20:17:22 +02:00
|
|
|
)
|
|
|
|
|
2016-06-04 01:55:27 +02:00
|
|
|
def eur_value(self) -> float:
|
2016-06-04 03:44:41 +02:00
|
|
|
value_sum = self.payment_set.all().aggregate(Sum('value')).get('value__sum')
|
|
|
|
if value_sum:
|
|
|
|
return value_sum / 100
|
|
|
|
else:
|
|
|
|
return 0
|
2016-06-04 01:24:40 +02:00
|
|
|
|
|
|
|
def __str__(self) -> str:
|
2016-06-04 14:52:08 +02:00
|
|
|
return "{0} on {1} for {2:.2f}".format(self.title, self.date, self.eur_value())
|
2016-06-04 01:24:40 +02:00
|
|
|
|
2016-06-03 20:17:22 +02:00
|
|
|
|
|
|
|
class Payment(models.Model):
|
|
|
|
title = models.CharField(max_length=100)
|
|
|
|
date = models.DateField()
|
|
|
|
value = models.IntegerField()
|
|
|
|
created_at = models.DateTimeField(auto_now_add=True)
|
|
|
|
updated_at = models.DateTimeField(auto_now=True)
|
|
|
|
user = models.ForeignKey(
|
|
|
|
AUTH_USER_MODEL,
|
|
|
|
on_delete=models.PROTECT
|
|
|
|
)
|
|
|
|
refund = models.ForeignKey(
|
|
|
|
Refund,
|
|
|
|
on_delete=models.SET_NULL,
|
|
|
|
null=True,
|
|
|
|
blank=True
|
|
|
|
)
|
2016-06-04 01:24:40 +02:00
|
|
|
|
|
|
|
def __str__(self) -> str:
|
2016-06-04 14:52:08 +02:00
|
|
|
s = '{0} on {1} for {2:.2f}'.format(self.title, self.date, self.value / 100)
|
|
|
|
logger.error(s)
|
|
|
|
return s
|
2016-06-04 01:24:40 +02:00
|
|
|
|
|
|
|
def eur_value(self) -> float:
|
|
|
|
return self.value / 100
|