from django.db import models

# Create your models here.
# Product Models ----
class TypeProduct(models.Model):
	m_name = models.CharField(blank=True,null=True,max_length=128)
	class Meta:
		verbose_name = "Type de Produit"
		verbose_name_plural = "Types de Produit"

class Product(models.Model):
	m_code = models.CharField(blank=True,null=True,max_length=32)
	m_name = models.CharField(blank=True,null=True,max_length=128)
	m_logo = models.FileField(blank=True,null=True)
	m_type = models.ForeignKey(TypeProduct,blank=True,null=True,on_delete=models.CASCADE)
	m_price = models.FloatField(blank=True,null=True)
	class Meta:
		verbose_name = "Produit"
		verbose_name_plural = "Produits"

class ProductActivity(models.Model):
	m_product = models.ForeignKey(Product,null=True,on_delete=models.CASCADE)
	m_qte = models.IntegerField(blank=True,null=True)
	def total(self):
		return self.m_qte * self.m_product.m_price
	class Meta:
		verbose_name = "Lot"
		verbose_name_plural = "Lots"

# Customer Models -----
class Customer(models.Model):
	m_name = models.CharField(blank=True,null=True,max_length=128)
	m_picture = models.FileField(blank=True,null=True)
	m_tel = models.CharField(blank=True,null=True,max_length=128)
	m_whats = models.CharField(blank=True,null=True,max_length=128)
	m_telgram = models.CharField(blank=True,null=True,max_length=128)
	m_facebook = models.CharField(blank=True,null=True,max_length=128)
	m_gmail = models.CharField(blank=True,null=True,max_length=128)

# Transaction Models ----
class TypeTransaction(models.Model):
	m_name = models.CharField(blank=True,null=True,max_length=128)

class Transaction(models.Model):
	m_code = models.CharField(blank=True,null=True,max_length=32)
	m_date = models.DateTimeField(auto_now=True,blank=True,null=True)
	m_sens = models.IntegerField(blank=True,null=True)
	m_products = models.ManyToManyField(ProductActivity,blank=True)
	m_description = models.TextField(blank=True,null=True)
	m_customer =  models.ForeignKey(Customer,null=True,on_delete=models.CASCADE)
	def montant(self):
		sum = 0
		for pro in self.m_products:
			sum += pro.total()
		return sum
