from django.contrib.auth.models import User
from django.db import models
from django.urls import reverse
from website.models import Article


""" Order Summary """


class HistoriqueCommande(models.Model):
    date = models.DateTimeField(auto_now=True)
    utilisateur = models.ForeignKey(User,
                                    related_name='user',
                                    on_delete=models.CASCADE,
                                    null=True)

    class Meta:
        verbose_name = "Commandes"
        verbose_name_plural = "Commandes"
        ordering = ('-date',)

    def __str__(self):
        return f'Commande {self.id}'

    def get_absolute_url(self):
        return reverse('order:order_detail',
                       args=[self.id])

    def get_number_products(self):
        total = 0
        for item in self.items.all():
            total += item.quantite
        return total

    def get_articles(self):
        return [item.get_article() for item in self.items.all()]

    def get_total_cost_with_taxes(self):
        return sum(item.get_cost() for item in self.items.all())

    def get_total_taxes(self):
        return self.get_total_cost_with_taxes() - self.get_total_cost_without_taxes()

    def get_total_cost_without_taxes(self):
        return sum(item.prix_HT * item.quantite for item in self.items.all())


""" Order """


class ProduitCommande(models.Model):
    commande = models.ForeignKey(HistoriqueCommande,
                                 null=True,
                                 related_name='items',
                                 on_delete=models.CASCADE)
    article = models.ForeignKey(Article,
                                null=True,
                                related_name='orders_items',
                                on_delete=models.CASCADE)
    prix = models.DecimalField(max_digits=100,
                               decimal_places=2,
                               default=0)
    prix_HT = models.DecimalField(max_digits=100,
                                  decimal_places=2,
                                  default=0)
    taux_TVA = models.FloatField(null=True)
    quantite = models.PositiveIntegerField(default=1)

    def __str__(self):
        return '{}'.format(self.id)

    def get_absolute_url(self):
        return reverse('website:product_detail',
                       args=[self.id])

    def calculate_price_with_taxes(self, arg):
        return round(float(arg) * (self.taux_TVA / 100 + 1), 2)

    def get_cost(self):
        return self.prix * self.quantite

    def get_cost_without_taxes(self):
        return self.prix_HT * self.quantite

    def get_taxes(self):
        return float(self.get_cost()) - (float(self.get_cost()) / (self.taux_TVA / 100 + 1))

    def get_article(self):
        return self

6b97f04ac4581b215f95939357403e7d81c78296
<mask token>


class HistoriqueCommande(models.Model):
    <mask token>
    <mask token>


    class Meta:
        verbose_name = 'Commandes'
        verbose_name_plural = 'Commandes'
        ordering = '-date',
    <mask token>
    <mask token>
    <mask token>

    def get_articles(self):
        return [item.get_article() for item in self.items.all()]
    <mask token>
    <mask token>

    def get_total_cost_without_taxes(self):
        return sum(item.prix_HT * item.quantite for item in self.items.all())


<mask token>


class ProduitCommande(models.Model):
    commande = models.ForeignKey(HistoriqueCommande, null=True,
        related_name='items', on_delete=models.CASCADE)
    article = models.ForeignKey(Article, null=True, related_name=
        'orders_items', on_delete=models.CASCADE)
    prix = models.DecimalField(max_digits=100, decimal_places=2, default=0)
    prix_HT = models.DecimalField(max_digits=100, decimal_places=2, default=0)
    taux_TVA = models.FloatField(null=True)
    quantite = models.PositiveIntegerField(default=1)

    def __str__(self):
        return '{}'.format(self.id)

    def get_absolute_url(self):
        return reverse('website:product_detail', args=[self.id])

    def calculate_price_with_taxes(self, arg):
        return round(float(arg) * (self.taux_TVA / 100 + 1), 2)

    def get_cost(self):
        return self.prix * self.quantite

    def get_cost_without_taxes(self):
        return self.prix_HT * self.quantite

    def get_taxes(self):
        return float(self.get_cost()) - float(self.get_cost()) / (self.
            taux_TVA / 100 + 1)

    def get_article(self):
        return self

<mask token>


class HistoriqueCommande(models.Model):
    <mask token>
    <mask token>


    class Meta:
        verbose_name = 'Commandes'
        verbose_name_plural = 'Commandes'
        ordering = '-date',

    def __str__(self):
        return f'Commande {self.id}'
    <mask token>
    <mask token>

    def get_articles(self):
        return [item.get_article() for item in self.items.all()]
    <mask token>
    <mask token>

    def get_total_cost_without_taxes(self):
        return sum(item.prix_HT * item.quantite for item in self.items.all())


<mask token>


class ProduitCommande(models.Model):
    commande = models.ForeignKey(HistoriqueCommande, null=True,
        related_name='items', on_delete=models.CASCADE)
    article = models.ForeignKey(Article, null=True, related_name=
        'orders_items', on_delete=models.CASCADE)
    prix = models.DecimalField(max_digits=100, decimal_places=2, default=0)
    prix_HT = models.DecimalField(max_digits=100, decimal_places=2, default=0)
    taux_TVA = models.FloatField(null=True)
    quantite = models.PositiveIntegerField(default=1)

    def __str__(self):
        return '{}'.format(self.id)

    def get_absolute_url(self):
        return reverse('website:product_detail', args=[self.id])

    def calculate_price_with_taxes(self, arg):
        return round(float(arg) * (self.taux_TVA / 100 + 1), 2)

    def get_cost(self):
        return self.prix * self.quantite

    def get_cost_without_taxes(self):
        return self.prix_HT * self.quantite

    def get_taxes(self):
        return float(self.get_cost()) - float(self.get_cost()) / (self.
            taux_TVA / 100 + 1)

    def get_article(self):
        return self

<mask token>


class HistoriqueCommande(models.Model):
    <mask token>
    <mask token>


    class Meta:
        verbose_name = 'Commandes'
        verbose_name_plural = 'Commandes'
        ordering = '-date',

    def __str__(self):
        return f'Commande {self.id}'

    def get_absolute_url(self):
        return reverse('order:order_detail', args=[self.id])
    <mask token>

    def get_articles(self):
        return [item.get_article() for item in self.items.all()]
    <mask token>

    def get_total_taxes(self):
        return self.get_total_cost_with_taxes(
            ) - self.get_total_cost_without_taxes()

    def get_total_cost_without_taxes(self):
        return sum(item.prix_HT * item.quantite for item in self.items.all())


<mask token>


class ProduitCommande(models.Model):
    commande = models.ForeignKey(HistoriqueCommande, null=True,
        related_name='items', on_delete=models.CASCADE)
    article = models.ForeignKey(Article, null=True, related_name=
        'orders_items', on_delete=models.CASCADE)
    prix = models.DecimalField(max_digits=100, decimal_places=2, default=0)
    prix_HT = models.DecimalField(max_digits=100, decimal_places=2, default=0)
    taux_TVA = models.FloatField(null=True)
    quantite = models.PositiveIntegerField(default=1)

    def __str__(self):
        return '{}'.format(self.id)

    def get_absolute_url(self):
        return reverse('website:product_detail', args=[self.id])

    def calculate_price_with_taxes(self, arg):
        return round(float(arg) * (self.taux_TVA / 100 + 1), 2)

    def get_cost(self):
        return self.prix * self.quantite

    def get_cost_without_taxes(self):
        return self.prix_HT * self.quantite

    def get_taxes(self):
        return float(self.get_cost()) - float(self.get_cost()) / (self.
            taux_TVA / 100 + 1)

    def get_article(self):
        return self

<mask token>


class HistoriqueCommande(models.Model):
    date = models.DateTimeField(auto_now=True)
    utilisateur = models.ForeignKey(User, related_name='user', on_delete=
        models.CASCADE, null=True)


    class Meta:
        verbose_name = 'Commandes'
        verbose_name_plural = 'Commandes'
        ordering = '-date',

    def __str__(self):
        return f'Commande {self.id}'

    def get_absolute_url(self):
        return reverse('order:order_detail', args=[self.id])

    def get_number_products(self):
        total = 0
        for item in self.items.all():
            total += item.quantite
        return total

    def get_articles(self):
        return [item.get_article() for item in self.items.all()]

    def get_total_cost_with_taxes(self):
        return sum(item.get_cost() for item in self.items.all())

    def get_total_taxes(self):
        return self.get_total_cost_with_taxes(
            ) - self.get_total_cost_without_taxes()

    def get_total_cost_without_taxes(self):
        return sum(item.prix_HT * item.quantite for item in self.items.all())


<mask token>


class ProduitCommande(models.Model):
    commande = models.ForeignKey(HistoriqueCommande, null=True,
        related_name='items', on_delete=models.CASCADE)
    article = models.ForeignKey(Article, null=True, related_name=
        'orders_items', on_delete=models.CASCADE)
    prix = models.DecimalField(max_digits=100, decimal_places=2, default=0)
    prix_HT = models.DecimalField(max_digits=100, decimal_places=2, default=0)
    taux_TVA = models.FloatField(null=True)
    quantite = models.PositiveIntegerField(default=1)

    def __str__(self):
        return '{}'.format(self.id)

    def get_absolute_url(self):
        return reverse('website:product_detail', args=[self.id])

    def calculate_price_with_taxes(self, arg):
        return round(float(arg) * (self.taux_TVA / 100 + 1), 2)

    def get_cost(self):
        return self.prix * self.quantite

    def get_cost_without_taxes(self):
        return self.prix_HT * self.quantite

    def get_taxes(self):
        return float(self.get_cost()) - float(self.get_cost()) / (self.
            taux_TVA / 100 + 1)

    def get_article(self):
        return self

from django.contrib.auth.models import User
from django.db import models
from django.urls import reverse
from website.models import Article


""" Order Summary """


class HistoriqueCommande(models.Model):
    date = models.DateTimeField(auto_now=True)
    utilisateur = models.ForeignKey(User,
                                    related_name='user',
                                    on_delete=models.CASCADE,
                                    null=True)

    class Meta:
        verbose_name = "Commandes"
        verbose_name_plural = "Commandes"
        ordering = ('-date',)

    def __str__(self):
        return f'Commande {self.id}'

    def get_absolute_url(self):
        return reverse('order:order_detail',
                       args=[self.id])

    def get_number_products(self):
        total = 0
        for item in self.items.all():
            total += item.quantite
        return total

    def get_articles(self):
        return [item.get_article() for item in self.items.all()]

    def get_total_cost_with_taxes(self):
        return sum(item.get_cost() for item in self.items.all())

    def get_total_taxes(self):
        return self.get_total_cost_with_taxes() - self.get_total_cost_without_taxes()

    def get_total_cost_without_taxes(self):
        return sum(item.prix_HT * item.quantite for item in self.items.all())


""" Order """


class ProduitCommande(models.Model):
    commande = models.ForeignKey(HistoriqueCommande,
                                 null=True,
                                 related_name='items',
                                 on_delete=models.CASCADE)
    article = models.ForeignKey(Article,
                                null=True,
                                related_name='orders_items',
                                on_delete=models.CASCADE)
    prix = models.DecimalField(max_digits=100,
                               decimal_places=2,
                               default=0)
    prix_HT = models.DecimalField(max_digits=100,
                                  decimal_places=2,
                                  default=0)
    taux_TVA = models.FloatField(null=True)
    quantite = models.PositiveIntegerField(default=1)

    def __str__(self):
        return '{}'.format(self.id)

    def get_absolute_url(self):
        return reverse('website:product_detail',
                       args=[self.id])

    def calculate_price_with_taxes(self, arg):
        return round(float(arg) * (self.taux_TVA / 100 + 1), 2)

    def get_cost(self):
        return self.prix * self.quantite

    def get_cost_without_taxes(self):
        return self.prix_HT * self.quantite

    def get_taxes(self):
        return float(self.get_cost()) - (float(self.get_cost()) / (self.taux_TVA / 100 + 1))

    def get_article(self):
        return self
