# -*- coding: utf-8 -*-
import datetime
import calendar
from django.db import models
from coin.offers.models import Offer


class Invoice(models.Model):

    INVOICES_STATUS_CHOICES = (
        ('open', u'A payer'),
        ('closed', u'Reglée'),
        ('trouble', u'Litige')
    )

    number = models.CharField(max_length=25)

    status = models.CharField(max_length=50, choices=INVOICES_STATUS_CHOICES,
                              default='open',
                              verbose_name='Statut')
    amount = models.DecimalField(max_digits=5, decimal_places=2,
                                 verbose_name='Montant')
    date = models.DateField(default=datetime.date.today, null=True)
    period_from = models.DateField(
        default=datetime.date(datetime.date.today().year,
                              datetime.date.today().month, 1),
        null=True,
        verbose_name=u'Début de période de facturation')
    period_to = models.DateField(
        default=(datetime.date(datetime.date.today().year,
                               datetime.date.today().month + 1, 1) -
                 datetime.timedelta(days=1)),
        null=True,
        verbose_name=u'Fin de période de facturation')
    date_due = models.DateField(
        default=(datetime.date(datetime.date.today().year,
                               datetime.date.today().month + 1, 1) -
                 datetime.timedelta(days=1)),
        null=True,
        verbose_name=u'Date d\'échéance de paiement')

    def __unicode__(self):
        return u'#%s %s€ %s' % (self.number, self.amount, self.date_due)

    class Meta:
        verbose_name = 'facture'


class InvoiceDetail(models.Model):

    label = models.CharField(max_length=100)
    amount = models.DecimalField(max_digits=5, decimal_places=2,
                                 verbose_name='Montant')
    quantity = models.IntegerField(null=True, verbose_name=u'Quantité')
    tax = models.IntegerField(null=True, default=0, verbose_name='TVA')
    invoice = models.ForeignKey(Invoice, verbose_name='Facture',
                                related_name='details')
    offer = models.ForeignKey(Offer, null=True, verbose_name='Offre')

    def __unicode__(self):
        return self.label

    class Meta:
        verbose_name = 'détail de facture'


class Payment(models.Model):
    payment_means = models.CharField(max_length=100, null=True,
                                     verbose_name='Moyen de paiement')
    amount = models.DecimalField(max_digits=7, decimal_places=2, null=True,
                                 verbose_name='Montant')
    date = models.DateField(default=datetime.date.today)
    invoce = models.ForeignKey(Invoice, verbose_name='Facture')

    class Meta:
        verbose_name = 'paiement'
