Commit 332c81f4 authored by Alia Lawraine Olay's avatar Alia Lawraine Olay

Edited models to produce value of Total Cost for booking

parent 0e371d5e
# Generated by Django 4.0.4 on 2022-12-01 05:29
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('booking', '0002_booking_additional_item'),
]
operations = [
migrations.AddField(
model_name='booking_additem',
name='Subtotal',
field=models.IntegerField(default=0, editable=False),
),
]
# Generated by Django 4.0.4 on 2022-12-01 05:51
import booking.models
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('booking', '0003_booking_additem_subtotal'),
]
operations = [
migrations.AlterField(
model_name='booking_additem',
name='Subtotal',
field=models.IntegerField(default=booking.models.Booking_AddItem.subtotal, editable=False),
),
]
# Generated by Django 4.0.4 on 2022-12-01 06:10
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('booking', '0004_alter_booking_additem_subtotal'),
]
operations = [
migrations.AlterField(
model_name='booking_additem',
name='Quantity',
field=models.IntegerField(default=1),
),
migrations.AlterField(
model_name='booking_additem',
name='Subtotal',
field=models.IntegerField(default=0, editable=False),
),
]
# Generated by Django 4.0.4 on 2022-12-01 07:20
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('booking', '0005_alter_booking_additem_quantity_and_more'),
]
operations = [
migrations.AddField(
model_name='booking',
name='Flight_Sched',
field=models.ManyToManyField(through='booking.Booking_Sched', to='booking.schedule'),
),
migrations.AlterField(
model_name='booking',
name='Total_Cost',
field=models.IntegerField(blank=True, default=0, editable=False),
),
migrations.AlterField(
model_name='schedule',
name='Flight_Cost',
field=models.IntegerField(default=0),
),
]
...@@ -32,6 +32,26 @@ class Passenger(models.Model): ...@@ -32,6 +32,26 @@ class Passenger(models.Model):
return self.First_Name + ' ' + self.Last_Name return self.First_Name + ' ' + self.Last_Name
class City(models.Model):
Airport_Code = models.AutoField(primary_key=True, editable=False, unique=True)
Airport_name = models.CharField(max_length=50)
City_Name = models.CharField(max_length=50)
Country = models.CharField(max_length=50)
def __str__(self):
return self.Airport_name + ' (' + self.Country + ')'
class Flight(models.Model):
Flight_ID = models.AutoField(primary_key=True, editable=False, unique=True)
Flight_Code = models.CharField(max_length=6)
Origin_Airport = models.ForeignKey(City, on_delete=models.CASCADE, related_name='origin')
Destination_Airport = models.ForeignKey(City, on_delete=models.CASCADE, related_name='destination')
def __str__(self):
return self.Flight_Code + ' (' + self.Origin_Airport.City_Name + ' to ' + self.Destination_Airport.City_Name + ')'
class Additional_Item(models.Model): class Additional_Item(models.Model):
Description = models.CharField(max_length=50, primary_key=True, editable=True, unique=True) Description = models.CharField(max_length=50, primary_key=True, editable=True, unique=True)
Item_Cost = models.IntegerField(default=0) Item_Cost = models.IntegerField(default=0)
...@@ -40,16 +60,48 @@ class Additional_Item(models.Model): ...@@ -40,16 +60,48 @@ class Additional_Item(models.Model):
return self.Description return self.Description
class Schedule(models.Model):
Sched_Code = models.AutoField(primary_key=True, editable=False, unique=True)
Flight_Date = models.DateField(null=True, blank=True)
Departure_Time = models.TimeField(null=True, blank=True)
Arrival_Time = models.TimeField(null=True, blank=True)
Duration = models.CharField(max_length=50)
Flight_Cost = models.IntegerField(default=0)
Flight_Code = models.ForeignKey(Flight, on_delete=models.CASCADE)
class Booking(models.Model): class Booking(models.Model):
Booking_ID = models.AutoField(primary_key=True, editable=False, unique=True) Booking_ID = models.AutoField(primary_key=True, editable=False, unique=True)
Booking_Date = models.DateField(null=True, blank=True) Booking_Date = models.DateField(null=True, blank=True)
Total_Cost = models.IntegerField(blank=True, default=0,null=False) Total_Cost = models.IntegerField(blank=True, default=0, null=False, editable=False)
Passenger = models.ForeignKey(Passenger, on_delete=models.CASCADE) Passenger = models.ForeignKey(Passenger, on_delete=models.CASCADE)
Additional_Item = models.ManyToManyField(Additional_Item, through='Booking_AddItem') Additional_Item = models.ManyToManyField(Additional_Item, through='Booking_AddItem')
Flight_Sched = models.ManyToManyField(Schedule, through='Booking_Sched')
def __str__(self): def __str__(self):
return str(self.Booking_ID) return str(self.Booking_ID)
def flightCost(self):
costs = []
for x in self.Flight_Sched.all():
costs.append(x.Flight_Cost)
total = sum(costs)
return total
def addCost(self):
costs = []
add_item = Booking_AddItem.objects.filter(Booking_ID=self.Booking_ID)
for x in add_item:
costs.append(x.Subtotal)
total = sum(costs)
return total
def save(self, *args, **kwargs):
if not self.Booking_ID:
super(Booking, self).save(*args, **kwargs)
self.Total_Cost = self.flightCost() + self.addCost()
super(Booking, self).save(*args, **kwargs)
class Booking_AddItem(models.Model): class Booking_AddItem(models.Model):
class Meta: class Meta:
...@@ -57,26 +109,28 @@ class Booking_AddItem(models.Model): ...@@ -57,26 +109,28 @@ class Booking_AddItem(models.Model):
Booking_ID = models.ForeignKey(Booking, on_delete=models.CASCADE) Booking_ID = models.ForeignKey(Booking, on_delete=models.CASCADE)
AddItem_Desc = models.ForeignKey(Additional_Item, on_delete=models.CASCADE) AddItem_Desc = models.ForeignKey(Additional_Item, on_delete=models.CASCADE)
Quantity = models.IntegerField(default=0) Quantity = models.IntegerField(default=1)
Subtotal = models.IntegerField(default=0, editable=False) Subtotal = models.IntegerField(default=0, editable=False)
@property def subtotal(self):
def save(self):
add_item = Additional_Item.objects.get(Description = self.AddItem_Desc) add_item = Additional_Item.objects.get(Description = self.AddItem_Desc)
itemcost = add_item.Item_Cost itemcost = add_item.Item_Cost
self.subtotal = itemcost*self.Quantity subtotal = itemcost*self.Quantity
return super().save() return subtotal
def save(self, *args, **kwargs):
self.Subtotal= self.subtotal()
super(Booking_AddItem, self).save(*args, **kwargs)
class City(models.Model):
Airport_Code = models.AutoField(primary_key=True, editable=False, unique=True)
Airport_name = models.CharField(max_length=50)
City_Name = models.CharField(max_length=50)
Country = models.CharField(max_length=50)
def __str__(self): class Booking_Sched(models.Model):
return self.Airport_name + ' (' + self.Country + ')' class Meta:
unique_together = ('Booking_ID', 'Sched_Code')
Booking_ID = models.ForeignKey(Booking, on_delete=models.CASCADE)
Sched_Code = models.ForeignKey(Schedule, on_delete=models.CASCADE)
class Crew_Member(models.Model): class Crew_Member(models.Model):
Employee_ID = models.AutoField(primary_key=True, editable=False, unique=True) Employee_ID = models.AutoField(primary_key=True, editable=False, unique=True)
...@@ -131,23 +185,6 @@ class Flight_Attendant(models.Model): ...@@ -131,23 +185,6 @@ class Flight_Attendant(models.Model):
def __str__(self): def __str__(self):
return self.Employee_ID.First_Name + ' ' + self.Employee_ID.Last_Name + ' (' + self.Assigned_Class + ')' return self.Employee_ID.First_Name + ' ' + self.Employee_ID.Last_Name + ' (' + self.Assigned_Class + ')'
class Flight(models.Model):
Flight_ID = models.AutoField(primary_key=True, editable=False, unique=True)
Flight_Code = models.CharField(max_length=6)
Origin_Airport = models.ForeignKey(City, on_delete=models.CASCADE, related_name='origin')
Destination_Airport = models.ForeignKey(City, on_delete=models.CASCADE, related_name='destination')
def __str__(self):
return self.Flight_Code + ' (' + self.Origin_Airport.City_Name + ' to ' + self.Destination_Airport.City_Name + ')'
class Schedule(models.Model):
Sched_Code = models.AutoField(primary_key=True, editable=False, unique=True)
Flight_Date = models.DateField(null=True, blank=True)
Departure_Time = models.TimeField(null=True, blank=True)
Arrival_Time = models.TimeField(null=True, blank=True)
Duration = models.CharField(max_length=50)
Flight_Cost = models.CharField(max_length=50)
Flight_Code = models.ForeignKey(Flight, on_delete=models.CASCADE)
class Sched_Crew(models.Model): class Sched_Crew(models.Model):
class Meta: class Meta:
...@@ -155,10 +192,3 @@ class Sched_Crew(models.Model): ...@@ -155,10 +192,3 @@ class Sched_Crew(models.Model):
Sched_Code = models.ForeignKey(Schedule, on_delete=models.CASCADE) Sched_Code = models.ForeignKey(Schedule, on_delete=models.CASCADE)
Employee_ID = models.ForeignKey(Crew_Member, on_delete=models.CASCADE) Employee_ID = models.ForeignKey(Crew_Member, on_delete=models.CASCADE)
\ No newline at end of file
class Booking_Sched(models.Model):
class Meta:
unique_together = ('Booking_ID', 'Sched_Code')
Booking_ID = models.ForeignKey(Booking, on_delete=models.CASCADE)
Sched_Code = models.ForeignKey(Schedule, on_delete=models.CASCADE)
No preview for this file type
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment