# Path: konfera/models/abstract.py
# class KonferaModel(models.Model):
# 
#     class Meta:
#         abstract = True
# 
#     uuid = models.UUIDField(default=uuid.uuid4, editable=False)
#     date_created = models.DateTimeField(verbose_name=_('Created'), auto_now_add=True)
#     date_modified = models.DateTimeField(verbose_name=_('Last modified'), auto_now=True)
# 
# Path: konfera/models/receipt.py
# class Receipt(AddressModel):
#     order = models.OneToOneField('Order', on_delete=models.CASCADE, related_name='receipt_of')
#     title = models.CharField(max_length=128, verbose_name=_('Name'), blank=True)
#     companyid = models.CharField(max_length=32, blank=True, verbose_name=_('Company ID'))
#     taxid = models.CharField(max_length=32, blank=True, verbose_name=_('TAX ID'))
#     vatid = models.CharField(max_length=32, blank=True, verbose_name=_('VAT ID'))
#     amount = models.DecimalField(decimal_places=2, max_digits=12, validators=[MinValueValidator(0)])
# 
#     def __str__(self):
#         return self.title
# 
# Path: konfera/settings.py
# UNPAID_ORDER_NOTIFICATION_REPEAT = getattr(settings, 'UNPAID_ORDER_NOTIFICATION_REPEAT', 3)
# 
# UNPAID_ORDER_NOTIFICATION_REPEAT_DELAY = getattr(settings, 'UNPAID_ORDER_NOTIFICATION_REPEAT_DELAY', 5)


from datetime import timedelta
from decimal import Decimal
from django.core.validators import MinValueValidator
from django.core.exceptions import ObjectDoesNotExist
from django.db import models
from django.utils import timezone
from django.utils.translation import ugettext_lazy as _
from django.db.models import Q
from konfera.models.abstract import KonferaModel
from konfera.models.receipt import Receipt
from konfera.settings import UNPAID_ORDER_NOTIFICATION_REPEAT, UNPAID_ORDER_NOTIFICATION_REPEAT_DELAY
    amount_paid = models.DecimalField(decimal_places=2, max_digits=12, validators=[MinValueValidator(0)], default=0)
    discount = models.DecimalField(decimal_places=2, max_digits=12, validators=[MinValueValidator(0)], default=0)
    processing_fee = models.DecimalField(decimal_places=2, max_digits=12, validators=[MinValueValidator(0)], default=0)
    status = models.CharField(choices=ORDER_CHOICES, default=AWAITING, max_length=20)
    purchase_date = models.DateTimeField(auto_now_add=True)
    payment_date = models.DateTimeField(blank=True, null=True)
    unpaid_notification_sent_at = models.DateField(blank=True, null=True)
    unpaid_notification_sent_amount = models.IntegerField(default=0)

    def __str__(self):
        return str(self.price - self.discount)

    def save(self, *args, **kwargs):
        if self.status == Order.PAID:
            if self.payment_date is None:
                self.payment_date = timezone.now()
            if self.amount_paid == 0:
                self.amount_paid = self.to_pay

        if self.status == Order.CANCELLED:
            self.price = 0
            self.discount = 0

            for ticket in self.ticket_set.all():
                ticket.status = ticket.CANCELLED
                ticket.save()

        super().save(*args, **kwargs)

        try:

# Path: konfera/models/abstract.py
# class KonferaModel(models.Model):
# 
#     class Meta:
#         abstract = True
# 
#     uuid = models.UUIDField(default=uuid.uuid4, editable=False)
#     date_created = models.DateTimeField(verbose_name=_('Created'), auto_now_add=True)
#     date_modified = models.DateTimeField(verbose_name=_('Last modified'), auto_now=True)
# 
# Path: konfera/models/receipt.py
# class Receipt(AddressModel):
#     order = models.OneToOneField('Order', on_delete=models.CASCADE, related_name='receipt_of')
#     title = models.CharField(max_length=128, verbose_name=_('Name'), blank=True)
#     companyid = models.CharField(max_length=32, blank=True, verbose_name=_('Company ID'))
#     taxid = models.CharField(max_length=32, blank=True, verbose_name=_('TAX ID'))
#     vatid = models.CharField(max_length=32, blank=True, verbose_name=_('VAT ID'))
#     amount = models.DecimalField(decimal_places=2, max_digits=12, validators=[MinValueValidator(0)])
# 
#     def __str__(self):
#         return self.title
# 
# Path: konfera/settings.py
# UNPAID_ORDER_NOTIFICATION_REPEAT = getattr(settings, 'UNPAID_ORDER_NOTIFICATION_REPEAT', 3)
# 
# UNPAID_ORDER_NOTIFICATION_REPEAT_DELAY = getattr(settings, 'UNPAID_ORDER_NOTIFICATION_REPEAT_DELAY', 5)


# Path: konfera/models/order.py
from datetime import timedelta
from decimal import Decimal
from django.core.validators import MinValueValidator
from django.core.exceptions import ObjectDoesNotExist
from django.db import models
from django.utils import timezone
from django.utils.translation import ugettext_lazy as _
from django.db.models import Q
from konfera.models.abstract import KonferaModel
from konfera.models.receipt import Receipt
from konfera.settings import UNPAID_ORDER_NOTIFICATION_REPEAT, UNPAID_ORDER_NOTIFICATION_REPEAT_DELAY

    amount_paid = models.DecimalField(decimal_places=2, max_digits=12, validators=[MinValueValidator(0)], default=0)
    discount = models.DecimalField(decimal_places=2, max_digits=12, validators=[MinValueValidator(0)], default=0)
    processing_fee = models.DecimalField(decimal_places=2, max_digits=12, validators=[MinValueValidator(0)], default=0)
    status = models.CharField(choices=ORDER_CHOICES, default=AWAITING, max_length=20)
    purchase_date = models.DateTimeField(auto_now_add=True)
    payment_date = models.DateTimeField(blank=True, null=True)
    unpaid_notification_sent_at = models.DateField(blank=True, null=True)
    unpaid_notification_sent_amount = models.IntegerField(default=0)

    def __str__(self):
        return str(self.price - self.discount)

    def save(self, *args, **kwargs):
        if self.status == Order.PAID:
            if self.payment_date is None:
                self.payment_date = timezone.now()
            if self.amount_paid == 0:
                self.amount_paid = self.to_pay

        if self.status == Order.CANCELLED:
            self.price = 0
            self.discount = 0

            for ticket in self.ticket_set.all():
                ticket.status = ticket.CANCELLED
                ticket.save()

        super().save(*args, **kwargs)

        try:

            self.receipt_of