3
0
Fork 0
mirror of https://github.com/nanoy42/coope synced 2024-12-01 17:12:24 +00:00
coope/gestion/models.py

254 lines
10 KiB
Python
Raw Normal View History

2018-08-31 12:46:35 +00:00
from django.db import models
from django.contrib.auth.models import User
2018-11-27 08:07:12 +00:00
from simple_history.models import HistoricalRecords
from django.core.validators import MinValueValidator
2018-12-02 15:28:40 +00:00
from preferences.models import PaymentMethod
from django.core.exceptions import ValidationError
2018-11-27 08:07:12 +00:00
2018-10-05 22:03:02 +00:00
2018-08-31 12:46:35 +00:00
class Product(models.Model):
2018-12-02 15:28:40 +00:00
"""
Stores a product
"""
2018-08-31 12:46:35 +00:00
P_PRESSION = 'PP'
D_PRESSION = 'DP'
G_PRESSION = 'GP'
BOTTLE = 'BT'
SOFT = 'SO'
FOOD = 'FO'
2018-10-05 22:03:02 +00:00
PANINI = 'PA'
2018-08-31 12:46:35 +00:00
TYPEINPUT_CHOICES_CATEGORIE = (
(P_PRESSION, "Pinte Pression"),
(D_PRESSION, "Demi Pression"),
(G_PRESSION, "Galopin pression"),
(BOTTLE, "Bouteille"),
(SOFT, "Soft"),
2018-10-05 22:03:02 +00:00
(FOOD, "Bouffe autre que panini"),
(PANINI, "Bouffe pour panini"),
2018-08-31 12:46:35 +00:00
)
2018-12-02 15:28:40 +00:00
class Meta:
verbose_name = "Produit"
2018-10-05 22:03:02 +00:00
name = models.CharField(max_length=40, verbose_name="Nom", unique=True)
2018-11-27 08:07:12 +00:00
amount = models.DecimalField(max_digits=5, decimal_places=2, verbose_name="Prix de vente", validators=[MinValueValidator(0)])
2018-10-05 22:03:02 +00:00
stockHold = models.IntegerField(default=0, verbose_name="Stock en soute")
stockBar = models.IntegerField(default=0, verbose_name="Stock en bar")
2018-11-27 08:07:12 +00:00
barcode = models.CharField(max_length=20, unique=True, verbose_name="Code barre")
2018-10-05 22:03:02 +00:00
category = models.CharField(max_length=2, choices=TYPEINPUT_CHOICES_CATEGORIE, default=FOOD, verbose_name="Catégorie")
needQuantityButton = models.BooleanField(default=False, verbose_name="Bouton quantité")
is_active = models.BooleanField(default=True, verbose_name="Actif")
2018-11-27 08:07:12 +00:00
volume = models.PositiveIntegerField(default=0)
deg = models.DecimalField(default=0,max_digits=5, decimal_places=2, verbose_name="Degré", validators=[MinValueValidator(0)])
2018-12-05 00:43:21 +00:00
adherentRequired = models.BooleanField(default=True, verbose_name="Adhérent requis")
2019-01-06 03:39:30 +00:00
showingMultiplier = models.PositiveIntegerField(default=1)
2018-11-27 08:07:12 +00:00
history = HistoricalRecords()
2018-08-31 12:46:35 +00:00
def __str__(self):
2018-10-05 22:03:02 +00:00
return self.name
2018-08-31 12:46:35 +00:00
2019-01-18 14:38:48 +00:00
def user_ranking(self, pk):
user = User.objects.get(pk=pk)
consumptions = ConsumptionHistory.objects.filter(customer=user).filter(product=self)
# add menu
nb = 0
for consumption in consumptions:
nb += consumption.quantity
return (user, nb)
@property
def ranking(self):
users = User.objects.all()
ranking = [self.user_ranking(user.pk) for user in users]
ranking.sort(key=lambda x:x[1], reverse=True)
return ranking[0:25]
2018-08-31 12:46:35 +00:00
def isPinte(id):
product = Product.objects.get(id=id)
if product.category != Product.P_PRESSION:
raise ValidationError(
('%(product)s n\'est pas une pinte'),
params={'product': product},
)
def isDemi(id):
product = Product.objects.get(id=id)
2018-10-05 22:03:02 +00:00
if product.category != Product.D_PRESSION:
2018-08-31 12:46:35 +00:00
raise ValidationError(
('%(product)s n\'est pas un demi'),
params={'product': product},
)
def isGalopin(id):
2018-12-05 00:43:21 +00:00
product = Product.objects.get(id=id)
2018-08-31 12:46:35 +00:00
if product.category != Product.G_PRESSION:
raise ValidationError(
('%(product)s n\'est pas un galopin'),
params={'product': product},
)
2018-10-05 22:03:02 +00:00
class Keg(models.Model):
2018-12-02 15:28:40 +00:00
"""
Stores a keg
"""
2018-11-25 12:52:32 +00:00
class Meta:
2018-12-02 15:28:40 +00:00
verbose_name = "Fût"
2018-11-25 12:52:32 +00:00
permissions = (
("open_keg", "Peut percuter les fûts"),
("close_keg", "Peut fermer les fûts")
)
2018-10-05 22:03:02 +00:00
name = models.CharField(max_length=20, unique=True, verbose_name="Nom")
stockHold = models.IntegerField(default=0, verbose_name="Stock en soute")
barcode = models.CharField(max_length=20, unique=True, verbose_name="Code barre")
2018-12-05 00:43:21 +00:00
amount = models.DecimalField(max_digits=7, decimal_places=2, verbose_name="Prix du fût", validators=[MinValueValidator(0)])
2018-10-05 22:03:02 +00:00
capacity = models.IntegerField(default=30, verbose_name="Capacité (L)")
2018-08-31 12:46:35 +00:00
pinte = models.ForeignKey(Product, on_delete=models.PROTECT, related_name="futp", validators=[isPinte])
demi = models.ForeignKey(Product, on_delete=models.PROTECT, related_name="futd", validators=[isDemi])
galopin = models.ForeignKey(Product, on_delete=models.PROTECT, related_name="futg", validators=[isGalopin],null=True, blank=True)
2018-10-05 22:03:02 +00:00
is_active = models.BooleanField(default=False, verbose_name="Actif")
2018-11-27 08:07:12 +00:00
history = HistoricalRecords()
2018-08-31 12:46:35 +00:00
def __str__(self):
return self.name
2018-10-05 22:03:02 +00:00
class KegHistory(models.Model):
2018-12-02 15:28:40 +00:00
"""
Stores a keg history, related to :model:`gestion.Keg`
"""
class Meta:
verbose_name = "Historique de fût"
2018-11-25 12:52:32 +00:00
keg = models.ForeignKey(Keg, on_delete=models.PROTECT)
2018-10-05 22:03:02 +00:00
openingDate = models.DateTimeField(auto_now_add=True)
2018-11-25 12:52:32 +00:00
quantitySold = models.DecimalField(decimal_places=2, max_digits=5, default=0)
amountSold = models.DecimalField(decimal_places=2, max_digits=5, default=0)
closingDate = models.DateTimeField(null=True, blank=True)
2018-10-05 22:03:02 +00:00
isCurrentKegHistory = models.BooleanField(default=True)
2018-11-27 08:07:12 +00:00
history = HistoricalRecords()
2018-10-05 22:03:02 +00:00
2018-11-25 12:52:32 +00:00
def __str__(self):
res = "Fût de " + str(self.keg) + " (" + str(self.openingDate) + " - "
if(self.closingDate):
res += str(self.closingDate) + ")"
else:
res += "?)"
return res
2018-08-31 12:46:35 +00:00
class Reload(models.Model):
2018-12-02 15:28:40 +00:00
"""
Stores reloads
"""
class Meta:
verbose_name = "Rechargement"
2018-10-05 22:03:02 +00:00
customer = models.ForeignKey(User, on_delete=models.PROTECT, related_name="reload_taken", verbose_name="Client")
2018-12-05 00:43:21 +00:00
amount = models.DecimalField(max_digits=7, decimal_places=2, verbose_name="Montant", validators=[MinValueValidator(0)])
2018-10-05 22:03:02 +00:00
PaymentMethod = models.ForeignKey(PaymentMethod, on_delete=models.PROTECT, verbose_name="Moyen de paiement")
2018-08-31 12:46:35 +00:00
coopeman = models.ForeignKey(User, on_delete=models.PROTECT, related_name="reload_realized")
date = models.DateTimeField(auto_now_add=True)
2018-11-27 08:07:12 +00:00
history = HistoricalRecords()
2018-08-31 12:46:35 +00:00
def __str__(self):
return "Rechargement effectue par {0} le {1} ({2} euros, coopeman : {3})".format(self.customer, self.date, self.amount, self.coopeman)
class Refund(models.Model):
2018-12-02 15:28:40 +00:00
"""
Stores refunds
"""
class Meta:
verbose_name = "Remboursement"
2018-08-31 12:46:35 +00:00
date = models.DateTimeField(auto_now_add=True)
2018-10-05 22:03:02 +00:00
customer = models.ForeignKey(User, on_delete=models.PROTECT, related_name="refund_taken", verbose_name="Client")
2018-12-05 00:43:21 +00:00
amount = models.DecimalField(max_digits=7, decimal_places=2, verbose_name="Montant", validators=[MinValueValidator(0)])
2018-08-31 12:46:35 +00:00
coopeman = models.ForeignKey(User, on_delete=models.PROTECT, related_name="refund_realized")
2018-11-27 08:07:12 +00:00
history = HistoricalRecords()
2018-08-31 12:46:35 +00:00
def __str__(self):
return "{0} remboursé de {1} le {2} (effectué par {3})".format(self.customer, self.amount, self.date, self.coopeman)
class Menu(models.Model):
2018-12-02 15:28:40 +00:00
"""
Stores menus
"""
2018-10-05 22:03:02 +00:00
name = models.CharField(max_length=255, verbose_name="Nom")
2018-12-05 00:43:21 +00:00
amount = models.DecimalField(max_digits=7, decimal_places=2, verbose_name="Montant", validators=[MinValueValidator(0)])
2018-10-05 22:03:02 +00:00
barcode = models.CharField(max_length=20, unique=True, verbose_name="Code barre")
articles = models.ManyToManyField(Product, verbose_name="Produits")
is_active = models.BooleanField(default=False, verbose_name="Actif")
2018-11-27 08:07:12 +00:00
history = HistoricalRecords()
2018-08-31 12:46:35 +00:00
def __str__(self):
return self.name
2018-11-27 08:07:12 +00:00
@property
def adherent_required(self):
res = False
for article in self.articles.all():
res = res or article.adherentRequired
return res
2018-08-31 12:46:35 +00:00
class MenuHistory(models.Model):
2018-12-02 15:28:40 +00:00
"""
Stores MenuHistory related to :model:`gestion.Menu`
"""
class Meta:
verbose_name = "Historique de menu"
2018-08-31 12:46:35 +00:00
customer = models.ForeignKey(User, on_delete=models.PROTECT, related_name="menu_taken")
quantity = models.PositiveIntegerField(default=0)
2018-11-27 08:07:12 +00:00
paymentMethod = models.ForeignKey(PaymentMethod, on_delete=models.PROTECT)
2018-08-31 12:46:35 +00:00
date = models.DateTimeField(auto_now_add=True)
menu = models.ForeignKey(Menu, on_delete=models.PROTECT)
2018-12-05 00:43:21 +00:00
amount = models.DecimalField(max_digits=7, decimal_places=2, default=0)
2018-08-31 12:46:35 +00:00
coopeman = models.ForeignKey(User, on_delete=models.PROTECT, related_name="menu_selled")
2018-11-27 08:07:12 +00:00
history = HistoricalRecords()
2018-08-31 12:46:35 +00:00
def __str__(self):
2018-11-22 21:52:15 +00:00
return "{2} a consommé {0} {1}".format(self.quantity, self.menu, self.customer)
2018-08-31 12:46:35 +00:00
class ConsumptionHistory(models.Model):
2018-12-02 15:28:40 +00:00
"""
Stores consumption history related to :model:`gestion.Product`
"""
class Meta:
verbose_name = "Consommation"
2018-08-31 12:46:35 +00:00
customer = models.ForeignKey(User, on_delete=models.PROTECT, related_name="consumption_taken")
quantity = models.PositiveIntegerField(default=0)
paymentMethod = models.ForeignKey(PaymentMethod, on_delete=models.PROTECT)
date = models.DateTimeField(auto_now_add=True)
product = models.ForeignKey(Product, on_delete=models.PROTECT)
amount = models.DecimalField(max_digits=7, decimal_places=2, default=0)
coopeman = models.ForeignKey(User, on_delete=models.PROTECT, related_name="consumption_selled")
2018-11-27 08:07:12 +00:00
history = HistoricalRecords()
2018-08-31 12:46:35 +00:00
def __str__(self):
return "{0} {1} consommé par {2} le {3} (encaissé par {4})".format(self.quantity, self.product, self.customer, self.date, self.coopeman)
2018-11-25 12:52:32 +00:00
class Consumption(models.Model):
2018-12-02 15:28:40 +00:00
"""
Stores total consumptions
"""
class Meta:
verbose_name = "Consommation totale"
2018-11-25 12:52:32 +00:00
customer = models.ForeignKey(User, on_delete=models.PROTECT, related_name="consumption_global_taken")
product = models.ForeignKey(Product, on_delete=models.PROTECT)
quantity = models.PositiveIntegerField(default=0)
2018-11-27 08:07:12 +00:00
history = HistoricalRecords()
2018-11-25 12:52:32 +00:00
def __str__(self):
return "Consommation de " + str(self.customer) + " concernant le produit " + str(self.product)
2018-12-21 20:35:09 +00:00
class Pinte(models.Model):
"""
Stores a physical pinte
"""
current_owner = models.ForeignKey(User, on_delete=models.PROTECT, null=True, default=None, related_name="pintes_owned_currently")
previous_owner = models.ForeignKey(User, on_delete=models.PROTECT, null=True, default=None, related_name="pintes_owned_previously")
2018-12-21 20:35:09 +00:00
last_update_date = models.DateTimeField(auto_now=True)
history = HistoricalRecords()