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),
),
]
......@@ -31,6 +31,26 @@ class Passenger(models.Model):
def __str__(self):
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):
Description = models.CharField(max_length=50, primary_key=True, editable=True, unique=True)
......@@ -40,16 +60,48 @@ class Additional_Item(models.Model):
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):
Booking_ID = models.AutoField(primary_key=True, editable=False, unique=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)
Additional_Item = models.ManyToManyField(Additional_Item, through='Booking_AddItem')
Flight_Sched = models.ManyToManyField(Schedule, through='Booking_Sched')
def __str__(self):
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 Meta:
......@@ -57,26 +109,28 @@ class Booking_AddItem(models.Model):
Booking_ID = models.ForeignKey(Booking, 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)
@property
def save(self):
def subtotal(self):
add_item = Additional_Item.objects.get(Description = self.AddItem_Desc)
itemcost = add_item.Item_Cost
self.subtotal = itemcost*self.Quantity
return super().save()
subtotal = itemcost*self.Quantity
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):
return self.Airport_name + ' (' + self.Country + ')'
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)
class Crew_Member(models.Model):
Employee_ID = models.AutoField(primary_key=True, editable=False, unique=True)
......@@ -131,34 +185,10 @@ class Flight_Attendant(models.Model):
def __str__(self):
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 Meta:
unique_together = ('Sched_Code', 'Employee_ID')
Sched_Code = models.ForeignKey(Schedule, on_delete=models.CASCADE)
Employee_ID = models.ForeignKey(Crew_Member, on_delete=models.CASCADE)
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)
Employee_ID = models.ForeignKey(Crew_Member, on_delete=models.CASCADE)
\ No newline at end of file
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