# -*- coding: utf-8 -*-
import datetime
import random
from decimal import Decimal
from django.db import models
from django.db.models.signals import post_save
from django.dispatch import receiver
from coin.offers.models import OfferSubscription
from coin.members.models import Member


def next_invoice_number():
    "Détermine un numéro de facture aléatoire"
    return u'%s%02i-%i-%i' % (datetime.date.today().year,
                              datetime.date.today().month,
                              random.randrange(100, 999),
                              random.randrange(100, 999))


class Invoice(models.Model):

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

    number = models.CharField(max_length=25,
                              default=next_invoice_number,
                              unique=True,
                              verbose_name='Numéro')
    status = models.CharField(max_length=50, choices=INVOICES_STATUS_CHOICES,
                              default='open',
                              verbose_name='Statut')
    date = models.DateField(default=datetime.date.today, null=True)
    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')
    member = models.ForeignKey(Member, null=True, blank=True, default=None,
                               related_name='invoices',
                               verbose_name='Membre',
                               on_delete=models.SET_NULL)

    def amount(self):
        "Calcul le montant de la facture en fonction des éléments de détails"
        total = Decimal('0.0')
        for detail in self.details.all():
            total += detail.total()
        return total.quantize(Decimal('0.01'))
    amount.short_description = 'Montant'

    def amount_paid(self):
        """
        Calcul le montant payé de la facture en fonction des éléments
        de paiements
        """
        total = Decimal('0.0')
        for payment in self.payments.all():
            total += payment.amount
        return total.quantize(Decimal('0.01'))
    amount_paid.short_description = 'Montant payé'

    def amount_remaining_to_pay(self):
        """
        Calcul le montant restant à payer
        """
        return self.amount() - self.amount_paid()
    amount_remaining_to_pay.short_description = 'Reste à payer'

    def has_owner(self, uid):
        "Check if passed uid (ex gmajax) is owner of the invoice"
        return (self.member and self.member.username and
                self.member.username == uid)

    def __unicode__(self):
        return u'#%s %0.2f€ %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.DecimalField(null=True, verbose_name=u'Quantité',
                                   default=1.0, decimal_places=2, max_digits=4)
    tax = models.DecimalField(null=True, default=0.0, decimal_places=2,
                              max_digits=4, verbose_name='TVA',
                              help_text='en %')
    invoice = models.ForeignKey(Invoice, verbose_name='Facture',
                                related_name='details')
    offersubscription = models.ForeignKey(OfferSubscription, null=True,
                                          blank=True, default=None,
                                          verbose_name='Abonnement')
    period_from = models.DateField(
        default=datetime.date(datetime.date.today().year,
                              datetime.date.today().month, 1),
        null=True,
        blank=True,
        verbose_name=u'Début de période',
        help_text=u'Date de début de période sur laquelle est facturé cet item')
    period_to = models.DateField(
        default=(datetime.date(datetime.date.today().year,
                               datetime.date.today().month + 1, 1) -
                 datetime.timedelta(days=1)),
        null=True,
        blank=True,
        verbose_name=u'Fin de période',
        help_text=u'Date de fin de période sur laquelle est facturé cet item')

    def __unicode__(self):
        return self.label

    def total(self):
        "Calcul le total"
        return (self.amount * (self.tax / Decimal('100.0') +
                Decimal('1.0')) * self.quantity).quantize(Decimal('0.01'))

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


class Payment(models.Model):

    PAYMENT_MEAN_CHOICES = (
        ('cash', u'Espèces'),
        ('check', u'Chèque'),
        ('transfer', u'Virement'),
        ('other', u'Autre')
    )

    payment_mean = models.CharField(max_length=100, null=True,
                                    default='transfer',
                                    choices=PAYMENT_MEAN_CHOICES,
                                    verbose_name='Moyen de paiement')
    amount = models.DecimalField(max_digits=5, decimal_places=2, null=True,
                                 verbose_name='Montant')
    date = models.DateField(default=datetime.date.today)
    invoice = models.ForeignKey(Invoice, verbose_name='Facture',
                                related_name='payments')

    def __unicode__(self):
        return u'Paiment de %0.2f€' % self.amount

    class Meta:
        verbose_name = 'paiement'

@receiver(post_save, sender=Payment)
def set_invoice_as_paid_if_needed(sender, instance, **kwargs):
    """
    Lorsqu'un paiement est enregistré, vérifie si la facture est alors
    complétement payée. Dans ce cas elle passe en réglée
    """
    if (instance.invoice.amount_paid >= instance.invoice.amount and
       instance.invoice.status == 'open'):
       instance.invoice.status = 'closed'
       instance.invoice.save()

