This commit is contained in:
MassiveAtoms 2020-05-24 15:48:06 -03:00
parent 265f7611df
commit 6755c617e8
19 changed files with 162 additions and 5 deletions

Binary file not shown.

View File

@ -6,6 +6,7 @@ class OrderAdmin(admin.ModelAdmin):
list_display = ("Order_ID", "Voorraad_ID", "Amount", "Prijs" ) list_display = ("Order_ID", "Voorraad_ID", "Amount", "Prijs" )
@admin.register(Factuur) @admin.register(Factuur)
class FactuurAdmin(admin.ModelAdmin): class FactuurAdmin(admin.ModelAdmin):
list_display = ("Factuur_ID", "Klant_ID", "total" ) list_display = ("Factuur_ID", "Klant_ID", "total" )

View File

@ -0,0 +1,23 @@
# Generated by Django 3.0.6 on 2020-05-24 18:06
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('markt', '0003_factuur_orders'),
]
operations = [
migrations.AlterField(
model_name='factuur',
name='Subtotal',
field=models.FloatField(blank=True, null=True),
),
migrations.AlterField(
model_name='factuur',
name='total',
field=models.FloatField(blank=True, null=True),
),
]

View File

@ -0,0 +1,17 @@
# Generated by Django 3.0.6 on 2020-05-24 18:10
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('markt', '0004_auto_20200524_1506'),
]
operations = [
migrations.RemoveField(
model_name='order',
name='Factuur_ID',
),
]

View File

@ -0,0 +1,20 @@
# Generated by Django 3.0.6 on 2020-05-24 18:13
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('markt', '0005_remove_order_factuur_id'),
]
operations = [
migrations.AddField(
model_name='order',
name='Factuur_ID',
field=models.ForeignKey(default=1, on_delete=django.db.models.deletion.PROTECT, to='markt.Factuur'),
preserve_default=False,
),
]

View File

@ -0,0 +1,17 @@
# Generated by Django 3.0.6 on 2020-05-24 18:14
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('markt', '0006_order_factuur_id'),
]
operations = [
migrations.RemoveField(
model_name='order',
name='Factuur_ID',
),
]

View File

@ -0,0 +1,25 @@
# Generated by Django 3.0.6 on 2020-05-24 18:22
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('markt', '0007_remove_order_factuur_id'),
]
operations = [
migrations.AddField(
model_name='order',
name='Factuur_ID',
field=models.ForeignKey(default=1, on_delete=django.db.models.deletion.PROTECT, to='markt.Factuur'),
preserve_default=False,
),
migrations.AlterField(
model_name='factuur',
name='Orders',
field=models.ManyToManyField(blank=True, null=True, to='markt.Order'),
),
]

View File

@ -0,0 +1,17 @@
# Generated by Django 3.0.6 on 2020-05-24 18:28
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('markt', '0008_auto_20200524_1522'),
]
operations = [
migrations.RemoveField(
model_name='factuur',
name='Orders',
),
]

View File

@ -0,0 +1,22 @@
# Generated by Django 3.0.6 on 2020-05-24 18:30
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('markt', '0009_remove_factuur_orders'),
]
operations = [
migrations.RemoveField(
model_name='order',
name='Factuur_ID',
),
migrations.AddField(
model_name='factuur',
name='Orders',
field=models.ManyToManyField(blank=True, null=True, to='markt.Order'),
),
]

View File

@ -16,6 +16,7 @@ class Houtsoort(models.Model):
def __str__(self): def __str__(self):
"""String for representing the Model object.""" """String for representing the Model object."""
return self.Houtsoort_naam return self.Houtsoort_naam
class Meta: class Meta:
db_table = "Houtsoort" db_table = "Houtsoort"
verbose_name_plural = "Houtsoorten" verbose_name_plural = "Houtsoorten"
@ -33,6 +34,7 @@ class Formaat(models.Model):
def __str__(self): def __str__(self):
"""String for representing the Model object.""" """String for representing the Model object."""
return self.Formaat + " " + str(self.Lengte) + "m" return self.Formaat + " " + str(self.Lengte) + "m"
class Meta: class Meta:
db_table = "Formaat" db_table = "Formaat"
verbose_name_plural = "Formaten" verbose_name_plural = "Formaten"
@ -60,6 +62,7 @@ class Houtmarkt(models.Model):
def __str__(self): def __str__(self):
"""String for representing the Model object.""" """String for representing the Model object."""
return self.Houtmarkt_naam return self.Houtmarkt_naam
class Meta: class Meta:
db_table = "Houtmarkt" db_table = "Houtmarkt"
verbose_name_plural = "Houtmarkten" verbose_name_plural = "Houtmarkten"
@ -80,6 +83,7 @@ class Voorraad(models.Model):
def __str__(self): def __str__(self):
"""String for representing the Model object.""" """String for representing the Model object."""
return str(self.Format_ID) + " " + self.Hout_ID.Houtsoort_naam return str(self.Format_ID) + " " + self.Hout_ID.Houtsoort_naam
class Meta: class Meta:
db_table = "Voorraad" db_table = "Voorraad"
verbose_name_plural = "Voorraad" verbose_name_plural = "Voorraad"
@ -100,17 +104,20 @@ class Klant(models.Model):
def __str__(self): def __str__(self):
"""String for representing the Model object.""" """String for representing the Model object."""
return self.Klant_naam return self.Klant_naam
class Meta: class Meta:
db_table = "Klant" db_table = "Klant"
verbose_name_plural = "Klanten" verbose_name_plural = "Klanten"
class Order(models.Model): class Order(models.Model):
Order_ID = models.AutoField(primary_key=True) Order_ID = models.AutoField(primary_key=True)
Voorraad_ID = models.ForeignKey("Voorraad", on_delete=models.PROTECT) Voorraad_ID = models.ForeignKey("Voorraad", on_delete=models.PROTECT)
Factuur_ID = models.ForeignKey("Factuur", on_delete=models.PROTECT) # Factuur_ID = models.ForeignKey("Factuur", on_delete=models.PROTECT)
Amount = models.IntegerField() Amount = models.IntegerField()
Prijs = models.FloatField(null=True, blank=True) Prijs = models.FloatField(null=True, blank=True)
def get_absolute_url(self): def get_absolute_url(self):
"""Returns the url to access a particular instance of the model.""" """Returns the url to access a particular instance of the model."""
return reverse('model-detail-view', args=[str(self.Order_ID)]) return reverse('model-detail-view', args=[str(self.Order_ID)])
@ -118,12 +125,17 @@ class Order(models.Model):
def __str__(self): def __str__(self):
"""String for representing the Model object.""" """String for representing the Model object."""
return "{} {}".format(self.Amount, self.Voorraad_ID) return "{} {}".format(self.Amount, self.Voorraad_ID)
class Meta: class Meta:
db_table = "Order" db_table = "Order"
verbose_name_plural = "Orders" verbose_name_plural = "Orders"
def save(self, *args, **kwargs): def save(self, *args, **kwargs):
self.Prijs = self.Voorraad_ID.Prijs * self.Amount self.Prijs = self.Voorraad_ID.Prijs * self.Amount
new_amount = self.Voorraad_ID.Voorraad_Amount - self.Amount
if new_amount < 0:
raise ValueError("Er zijn maar {} in voorraad".format(self.Voorraad_ID.Voorraad_Amount))
self.Voorraad_ID.Voorraad_Amount -= self.Amount self.Voorraad_ID.Voorraad_Amount -= self.Amount
self.Voorraad_ID.save() self.Voorraad_ID.save()
return super(Order, self).save(*args, **kwargs) return super(Order, self).save(*args, **kwargs)
@ -132,7 +144,7 @@ class Order(models.Model):
class Factuur(models.Model): class Factuur(models.Model):
Factuur_ID = models.AutoField(primary_key=True) Factuur_ID = models.AutoField(primary_key=True)
Klant_ID = models.ForeignKey("Klant", on_delete=models.PROTECT) Klant_ID = models.ForeignKey("Klant", on_delete=models.PROTECT)
Orders = models.ManyToManyField("Order") Orders = models.ManyToManyField("Order", null=True, blank=True)
Korting_percent = models.FloatField() Korting_percent = models.FloatField()
Subtotal = models.FloatField(null=True, blank=True) Subtotal = models.FloatField(null=True, blank=True)
total = models.FloatField(null=True, blank=True) total = models.FloatField(null=True, blank=True)
@ -144,11 +156,14 @@ class Factuur(models.Model):
def __str__(self): def __str__(self):
"""String for representing the Model object.""" """String for representing the Model object."""
return str(self.Factuur_ID) return str(self.Factuur_ID)
def save(self, *args, **kwargs): def save(self, *args, **kwargs):
super(Factuur, self).save(*args, **kwargs)
self.Subtotal = sum([i.Prijs for i in self.Orders.all()]) self.Subtotal = sum([i.Prijs for i in self.Orders.all()])
self.total = self.Subtotal * (1 - self.Korting_percent/100) self.total = self.Subtotal * (1 - self.Korting_percent/100)
return super(Factuur, self).save(*args, **kwargs) return super(Factuur, self).save(*args, **kwargs)
class Meta: class Meta:
db_table = "Factuur" db_table = "Factuur"
verbose_name_plural = "Facturen" verbose_name_plural = "Facturen"