from django.db import models
from django.contrib.auth.models import User

from shoppy import settings
from shoppy.util.models import moneyfmt

class Coupon(models.Model):
    creator = models.ForeignKey(User)
    valid_to = models.DateTimeField(null=True, blank=True)
    isValid = models.BooleanField()
    valueMoney = models.DecimalField(max_digits=10, decimal_places=2, null=True, blank=True)
    valuePercent = models.PositiveIntegerField(null=True, blank=True)
    minGoodsValue = models.DecimalField(max_digits=10, decimal_places=2)
    created_date = models.DateTimeField(auto_now_add=True)
    isgift = models.BooleanField()
    code = models.CharField(max_length=9)
    def __unicode__(self):
        return '%s / %s' % (self.code, self.valueMoney)
    def get_valuemoney_str(self):
        return '%s' % (moneyfmt(self.valueMoney, places=2, dp=',', sep='.'))

class CouponMail(models.Model):
    coupon = models.ForeignKey('Coupon')
    emailTo = models.EmailField()
    msgFrom = models.CharField(max_length=100,null=True, blank=True)
    msgTo = models.CharField(max_length=100,null=True, blank=True)
    message = models.CharField(max_length=255,null=True, blank=True)
    send_date = models.DateTimeField(auto_now_add=True)
    
class CouponOrder(models.Model):
    coupon = models.ForeignKey('Coupon')
    user = models.ForeignKey(User)
    #billing_addr = models.ForeignKey('contact.Contact',related_name='couponbilling', null=True, blank=True)
    paymenttype = models.ForeignKey('payment.Payment', null=True, blank=True)
    order_date = models.DateTimeField(null=True, blank=True)

class CouponAccount(models.Model):
    user = models.ForeignKey(User)
    balance = models.DecimalField(max_digits=10, decimal_places=2)

class CouponAccountTransaction(models.Model):
    cacc = models.ForeignKey('CouponAccount')
    time = models.DateTimeField(auto_now_add=True)
    amount = models.DecimalField(max_digits=10, decimal_places=2)
    message = models.CharField(max_length=255,null=True, blank=True)
    isValid = models.BooleanField()
