from django.db import models from django.contrib.auth.models import User
class Client(models.Model): nom = models.CharField(max_length=255) email = models.EmailField(unique=True) telephone = models.CharField(max_length=20, blank=True, null=True) adresse = models.TextField(blank=True, null=True) created_at = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.nom
class Produit(models.Model): nom = models.CharField(max_length=255) description = models.TextField(blank=True, null=True) prix = models.DecimalField(max_digits=10, decimal_places=2) stock = models.IntegerField(default=0) created_at = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.nom
class Facture(models.Model): client = models.ForeignKey(Client, on_delete=models.CASCADE) date = models.DateField(auto_now_add=True) total = models.DecimalField(max_digits=10, decimal_places=2) paye = models.BooleanField(default=False) created_at = models.DateTimeField(auto_now_add=True)
def __str__(self):
return f"Facture {self.id} - {self.client.nom}"
class LigneFacture(models.Model): facture = models.ForeignKey(Facture, on_delete=models.CASCADE, related_name='lignes') produit = models.ForeignKey(Produit, on_delete=models.CASCADE) quantite = models.IntegerField() sous_total = models.DecimalField(max_digits=10, decimal_places=2)
def save(self, *args, **kwargs):
self.sous_total = self.produit.prix * self.quantite
super().save(*args, **kwargs)
def __str__(self):
return f"{self.quantite} x {self.produit.nom} ({self.sous_total}€)"