houtmarkt-jeremy/markt/models.py

182 lines
6.1 KiB
Python
Raw Normal View History

2020-05-24 15:00:10 +00:00
from django.db import models
# Used to generate URLs by reversing the URL patterns
from django.urls import reverse
2020-05-24 19:05:50 +00:00
from django.db.models import Sum
2020-05-24 15:00:10 +00:00
# Create your models here.
class Houtsoort(models.Model):
Hout_ID = models.AutoField(primary_key=True)
Houtsoort_naam = models.CharField(max_length=255, help_text='Houtsoort')
def get_absolute_url(self):
"""Returns the url to access a particular instance of the model."""
return reverse('model-detail-view', args=[str(self.Hout_ID)])
def __str__(self):
"""String for representing the Model object."""
return self.Houtsoort_naam
2020-05-24 18:48:06 +00:00
2020-05-24 18:06:09 +00:00
class Meta:
db_table = "Houtsoort"
verbose_name_plural = "Houtsoorten"
2020-05-24 15:00:10 +00:00
class Formaat(models.Model):
Format_ID = models.AutoField(primary_key=True)
Formaat = models.CharField(max_length=255, help_text='Formaat (vb 2x4)')
Lengte = models.FloatField()
def get_absolute_url(self):
"""Returns the url to access a particular instance of the model."""
return reverse('model-detail-view', args=[str(self.Format_ID)])
def __str__(self):
"""String for representing the Model object."""
2020-05-24 18:06:09 +00:00
return self.Formaat + " " + str(self.Lengte) + "m"
2020-05-24 18:48:06 +00:00
2020-05-24 18:06:09 +00:00
class Meta:
db_table = "Formaat"
verbose_name_plural = "Formaten"
2020-05-24 15:00:10 +00:00
class Houtmarkt(models.Model):
Houtmarkt_ID = models.AutoField(primary_key=True)
Houtmarkt_naam = models.CharField(
max_length=255, help_text='Houtmarkt naam')
Houtmarkt_tel = models.IntegerField()
Houtmarkt_addr = models.CharField(max_length=255)
2020-05-24 18:06:09 +00:00
TYPES = (
("RE", "Reseller"),
("SE", "Seller")
)
Houtmarkt_type = models.CharField(
max_length=2,
choices=TYPES
)
2020-05-24 15:00:10 +00:00
def get_absolute_url(self):
"""Returns the url to access a particular instance of the model."""
return reverse('model-detail-view', args=[str(self.Houtmarkt_ID)])
def __str__(self):
"""String for representing the Model object."""
return self.Houtmarkt_naam
2020-05-24 18:48:06 +00:00
2020-05-24 18:06:09 +00:00
class Meta:
db_table = "Houtmarkt"
verbose_name_plural = "Houtmarkten"
2020-05-24 15:00:10 +00:00
class Voorraad(models.Model):
Voorraad_ID = models.AutoField(primary_key=True)
2020-05-24 18:06:09 +00:00
Houtmarkt_ID = models.ForeignKey("Houtmarkt", on_delete=models.PROTECT)
Hout_ID = models.ForeignKey("Houtsoort", on_delete=models.PROTECT)
Format_ID = models.ForeignKey("Formaat", on_delete=models.PROTECT)
Prijs = models.FloatField()
2020-05-24 15:00:10 +00:00
Voorraad_Amount = models.IntegerField()
def get_absolute_url(self):
"""Returns the url to access a particular instance of the model."""
return reverse('model-detail-view', args=[str(self.Voorraad_ID)])
def __str__(self):
"""String for representing the Model object."""
2020-05-24 18:48:06 +00:00
return str(self.Format_ID) + " " + self.Hout_ID.Houtsoort_naam
2020-05-24 18:06:09 +00:00
class Meta:
db_table = "Voorraad"
verbose_name_plural = "Voorraad"
2020-05-24 15:00:10 +00:00
class Klant(models.Model):
Klant_ID = models.AutoField(primary_key=True)
Klant_naam = models.CharField(max_length=255, help_text='Klant naam')
Klant_tel = models.CharField(
max_length=255, help_text='Klant telefoon nummer')
Klant_Addr = models.CharField(max_length=255, help_text='Klant Adres')
Is_Houtmarkt = models.BooleanField()
def get_absolute_url(self):
"""Returns the url to access a particular instance of the model."""
return reverse('model-detail-view', args=[str(self.Klant_ID)])
def __str__(self):
"""String for representing the Model object."""
return self.Klant_naam
2020-05-24 18:48:06 +00:00
2020-05-24 18:06:09 +00:00
class Meta:
db_table = "Klant"
verbose_name_plural = "Klanten"
2020-05-24 18:48:06 +00:00
2020-05-24 18:06:09 +00:00
class Order(models.Model):
2020-05-24 18:48:06 +00:00
Order_ID = models.AutoField(primary_key=True)
2020-05-24 18:06:09 +00:00
Voorraad_ID = models.ForeignKey("Voorraad", on_delete=models.PROTECT)
2020-05-24 18:48:06 +00:00
# Factuur_ID = models.ForeignKey("Factuur", on_delete=models.PROTECT)
2020-05-24 18:06:09 +00:00
Amount = models.IntegerField()
2020-05-24 18:48:06 +00:00
2020-05-24 18:06:09 +00:00
Prijs = models.FloatField(null=True, blank=True)
2020-05-24 18:48:06 +00:00
2020-05-24 18:06:09 +00:00
def get_absolute_url(self):
"""Returns the url to access a particular instance of the model."""
return reverse('model-detail-view', args=[str(self.Order_ID)])
def __str__(self):
"""String for representing the Model object."""
return "{} {}".format(self.Amount, self.Voorraad_ID)
2020-05-24 18:48:06 +00:00
2020-05-24 18:06:09 +00:00
class Meta:
db_table = "Order"
verbose_name_plural = "Orders"
def save(self, *args, **kwargs):
self.Prijs = self.Voorraad_ID.Prijs * self.Amount
2020-05-24 18:48:06 +00:00
new_amount = self.Voorraad_ID.Voorraad_Amount - self.Amount
if new_amount < 0:
2020-05-25 05:15:12 +00:00
raise ValueError("Er zijn maar {} in voorraad".format(
self.Voorraad_ID.Voorraad_Amount))
2020-05-24 18:06:09 +00:00
self.Voorraad_ID.Voorraad_Amount -= self.Amount
self.Voorraad_ID.save()
return super(Order, self).save(*args, **kwargs)
2020-05-24 15:00:10 +00:00
class Factuur(models.Model):
Factuur_ID = models.AutoField(primary_key=True)
2020-05-24 18:06:09 +00:00
Klant_ID = models.ForeignKey("Klant", on_delete=models.PROTECT)
2020-05-24 18:48:06 +00:00
Orders = models.ManyToManyField("Order", null=True, blank=True)
2020-05-24 15:00:10 +00:00
Korting_percent = models.FloatField()
2020-05-25 05:15:12 +00:00
Subtotal = models.FloatField(null=True, blank=True)
Total = models.FloatField(null=True, blank=True)
2020-05-24 15:00:10 +00:00
def get_absolute_url(self):
"""Returns the url to access a particular instance of the model."""
2020-05-25 05:15:12 +00:00
return reverse('factuur-detail', args=[str(self.Factuur_ID)])
2020-05-24 15:00:10 +00:00
def __str__(self):
"""String for representing the Model object."""
2020-05-24 18:06:09 +00:00
return str(self.Factuur_ID)
2020-05-24 18:48:06 +00:00
2020-05-24 19:05:50 +00:00
@property
2020-05-25 05:15:12 +00:00
def subtotal(self):
if not self.Subtotal:
# TODO: Korting moet mischien hier verwerkt worden
self.Subtotal = self.Orders.all().aggregate(Sum('Prijs'))['Prijs__sum']
self.save()
return self.Subtotal
2020-05-24 19:05:50 +00:00
@property
2020-05-25 05:15:12 +00:00
def total(self):
2020-05-24 19:05:50 +00:00
if self.pk:
2020-05-25 05:15:12 +00:00
if not self.Total:
# TODO: Taxes moet eigenlijk hier verwerkt worden, niet korting
self.Total = (100 - self.Korting_percent) * self.subtotal / 100
self.save()
return self.Total
2020-05-24 19:05:50 +00:00
else:
return 0
2020-05-24 18:06:09 +00:00
class Meta:
db_table = "Factuur"
verbose_name_plural = "Facturen"