예제 #1
0
 public static bool AddUser(User newU)
 {
     if (GetUser(newU.UserID) == null)
     {
         newU.Skymiles = 0;
         db.User.Add(newU);
         db.SaveChanges();
         return(true);
     }
     return(false);
 }
예제 #2
0
        public static bool DeleteService(string id)
        {
            var s = GetService(id);

            if (s != null)
            {
                db.Service.Remove(s);
                db.SaveChanges();
                return(true);
            }
            return(false);
        }
예제 #3
0
        /// <summary>
        /// Method that automtically add records to cancellation table and update refunds
        /// </summary>
        /// <param name="bookingId">Takes bookigId as parameters</param>
        /// <returns>Bool value</returns>

        public bool CancellationAuto(int bookingId)
        {
            try
            {
                using (AirlineDBEntities db = new AirlineDBEntities())
                {
                    Booking book = db.Bookings.Find(bookingId);
                    if (book == null)
                    {
                        throw new CancellationException("No Bookings Available");
                    }
                    //checks whether booking is cancelled or not
                    if (book.TicketStatus.Equals("CANC"))
                    {
                        throw new CancellationException("Ticket Already Cancelled");
                    }
                    else
                    {
                        Cancellation can = new Cancellation()
                        {
                            FlightId           = book.FlightId,
                            BookingId          = bookingId,
                            DateOfCancellation = DateTime.Now,
                            //checks whether date of journey is from which date
                            RefundAmount = CalculationOfRefund(bookingId, book.DateOfJourney <= DateTime.Now ? 0 : 1)
                        };
                        db.Cancellations.Add(can);
                        db.SaveChanges();
                        //After adding the object to cancellation table, the status of booking will be updated to Cancelled in bookings table
                        book.TicketStatus = "CANC";
                        book.TicketFare   = book.TicketFare - can.RefundAmount;
                        db.SaveChanges();


                        Flight flight = db.Flights.Find(can.FlightId);
                        //To store null we are giving ?
                        int?flightAvailableSeats = flight.AvailableSeats;
                        int?noOfSeats            = book.NoOfSeats;
                        // Converting int? to int as Available Seats has integer datatype
                        flight.AvailableSeats = (int)(flightAvailableSeats + noOfSeats);
                        //updating the available seats in flight table
                        db.SaveChanges();
                        return(true);
                    }
                }
            }
            catch (CancellationException ex)
            {
                throw new CancellationException(ex.Message);
            }
        }
예제 #4
0
 public static bool AddCreditCard(CreditCard creditCard)
 {
     try
     {
         var cCardSearch = GetCreditCard(creditCard.CCNo);
         if (cCardSearch == null)
         {
             db.CreditCard.Add(creditCard);
             db.SaveChanges();
             return(true);
         }
         return(false);
     }
     catch (Exception) { return(false); }
 }
예제 #5
0
        public static bool DeleteRoute(int id)
        {
            var r = GetRoute(id);

            if (r != null)
            {
                if (db.Flight.FirstOrDefault(f => f.RNo == id) == null)
                {
                    db.Route.Remove(r);
                    db.SaveChanges();
                    return(true);
                }
            }
            return(false);
        }
예제 #6
0
        public static bool DeleteFlight(string FNo)
        {
            var f = GetFlight(FNo);

            if (f != null)
            {
                if (db.Ticket.FirstOrDefault(t => t.FNo == FNo) == null)
                {
                    db.Flight.Remove(f);
                    db.SaveChanges();
                    return(true);
                }
            }
            return(false);
        }
예제 #7
0
 public static string AddEmployee(List <Employee> newE)
 {
     try
     {
         foreach (Employee item in newE)
         {
             db.Employee.Add(item);
         }
         db.SaveChanges();
         return("");
     }
     catch (Exception e)
     {
         return(e.Message);
     }
 }
예제 #8
0
        ///// <summary>
        ///// Method updates the details of existing cancellation details
        ///// </summary>
        ///// <param name="id"></param>
        ///// <param name="customer"></param>
        ///// <returns>return bool value</returns>
        public bool UpdateCancellation(int id, Cancellation cancellation)
        {
            try
            {
                using (AirlineDBEntities db = new AirlineDBEntities())
                {
                    db.Configuration.LazyLoadingEnabled = false;
                    db.Entry(cancellation).State        = EntityState.Modified;

                    try
                    {
                        db.SaveChanges();
                        return(true);
                    }
                    catch (DbUpdateConcurrencyException)
                    {
                        if (!CancellationExists(id))
                        {
                            return(false);
                        }
                        else
                        {
                            throw;
                        }
                    }
                }
            }
            catch (CancellationException)
            {
                throw new CancellationException("Id Doesnt Exists");
            }
        }
        /// <summary>
        /// AddFlight(Flight flight) adds the flight to the Flights table
        /// </summary>
        /// <param name="flight">object of type Flight</param>
        /// <returns>integer value indicating the Id of the added flight</returns>
        public int AddFlight(Flight flight)
        {
            try
            {
                //instantiating Online_Food_Ordering_SystemEntities3 Context class
                using (AirlineDBEntities db = new AirlineDBEntities())
                {
                    //check if the foodItem already exists
                    Flight flt = db.Flights.Where(f => f.FlightName.Equals(flight.FlightName, StringComparison.OrdinalIgnoreCase)).FirstOrDefault();

                    if (flt != null)
                    {
                        //if exists then throw exception
                        throw new FlightException("Flight already there");
                    }

                    //use LINQ query to Add flight from table flights
                    db.Flights.Add(flight);

                    //save changes to the database
                    db.SaveChanges();

                    Flight flts = db.Flights.Where(f => f.FlightId == flight.FlightId).FirstOrDefault();

                    return(flts.FlightId);
                }
            }
            catch (FlightException ex)
            {
                //throw user defined FlightException
                throw new FlightException(ex.Message);
            }
        }
예제 #10
0
 /// <summary>
 /// Method to add cancelled tickets
 /// </summary>
 /// <returns></returns>
 public bool AddCancellation(Cancellation cancellation)
 {
     try
     {
         using (AirlineDBEntities db = new AirlineDBEntities())
         {
             if ((db.Cancellations.Count(c => c.CancellationId == cancellation.CancellationId)) == 0)
             {
                 //Add command that will add to database
                 db.Cancellations.Add(cancellation);
                 db.SaveChanges();
                 db.Configuration.LazyLoadingEnabled = false;
                 return(true);
             }
             else
             {
                 return(false);
             }
         }
     }
     catch (CancellationException)
     {
         throw new CancellationException("Internal Error");
     }
 }
예제 #11
0
        public static bool UpdateTicket(Ticket updateT)
        {
            var           s = GetTicket(updateT.TicketID);
            List <Ticket> d = db.Ticket.Where(item => item.OrderID == updateT.OrderID && item.Age < 14 && item.PassportNo == s.PassportNo).ToList();
            int           c = d.Count();

            if (s != null)
            {
                if (c > 0)
                {
                    s.PassportNo = updateT.PassportNo;
                    foreach (var item in d)
                    {
                        GetTicket(item.TicketID).PassportNo = updateT.PassportNo;
                    }
                }
                else
                {
                    s.PassportNo = updateT.PassportNo;
                }
                s.Firstname = updateT.Firstname;
                s.Lastname  = updateT.Lastname;
                s.Sex       = updateT.Sex;
                s.IsReturn  = updateT.IsReturn;
                s.Age       = updateT.Age;
                db.SaveChanges();
                return(true);
            }
            return(false);
        }
        ///// <summary>
        ///// Method updates the details of existing cutomer's account details
        ///// </summary>
        ///// <param name="id"></param>
        ///// <param name="booking"></param>
        ///// <returns></returns>
        public bool UpdateBooking(int id, Booking booking)
        {
            using (AirlineDBEntities db = new AirlineDBEntities())
            {
                db.Configuration.LazyLoadingEnabled = false;
                db.Entry(booking).State             = EntityState.Modified;

                try
                {
                    db.SaveChanges();
                    return(true);
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!BookingExists(id))
                    {
                        return(false);
                    }
                    else
                    {
                        throw;
                    }
                }
            }
        }
        /// <summary>
        /// Method Deletes the Flight with flightid from table flights
        /// </summary>
        /// <param name="flightid">integer indicating id of Flight</param>
        /// <returns>boolean value indicating whether flight is deleted or not</returns>
        public bool DeleteFlightById(int flightid)
        {
            try
            {
                //instantiating AirLineDBEntities Context class
                using (AirlineDBEntities db = new AirlineDBEntities())
                {
                    //use LINQ query to find the Flight with id flightid
                    Flight flt = db.Flights.Where(f => f.FlightId == flightid).FirstOrDefault();

                    if (flt != null)
                    //use LINQ query to Add Flight from table flight
                    {
                        //remove item from flight
                        db.Flights.Remove(flt);

                        //save changes to the database
                        db.SaveChanges();

                        return(true);
                    }

                    else
                    {
                        throw new FlightException("flight does not exist");
                    }
                }
            }
            catch (FlightException ex)
            {
                //throw user defined FlightException
                throw new FlightException(ex.Message);
            }
        }
예제 #14
0
 public bool Post([FromBody] Cancellation c)
 {
     try
     {
         db.Cancellations.Add(c);
         var res = db.SaveChanges();
         if (res > 0)
         {
             return(true);
         }
     }
     catch (Exception ex)
     {
         throw ex;
     }
     return(false);
 }
예제 #15
0
 public bool Post([FromBody] Flight_Schedules fs)
 {
     try
     {
         db.Flight_Schedules.Add(fs);
         var res = db.SaveChanges();
         if (res > 0)
         {
             return(true);
         }
     }
     catch (Exception ex)
     {
         throw ex;
     }
     return(false);
 }
 public bool Post([FromBody] Flight_Reservation c)
 {
     try
     {
         db.Flight_Reservation.Add(c);
         var res = db.SaveChanges();
         if (res > 0)
         {
             return(true);
         }
     }
     catch (Exception ex)
     {
         throw ex;
     }
     return(false);
 }
 public bool Post([FromBody] Passenger_Details pd)
 {
     try
     {
         db.Passenger_Details.Add(pd);
         var res = db.SaveChanges();
         if (res > 0)
         {
             return(true);
         }
     }
     catch (Exception ex)
     {
         throw ex;
     }
     return(false);
 }
 public bool Post([FromBody] User_Registration ur)
 {
     try
     {
         ur.Password = Convert.ToBase64String(System.Security.Cryptography.SHA256.Create().ComputeHash(Encoding.UTF8.GetBytes(ur.Password)));
         db.User_Registration.Add(ur);
         var res = db.SaveChanges();
         if (res > 0)
         {
             return(true);
         }
     }
     catch (Exception ex)
     {
         throw ex;
     }
     return(false);
 }
        public IHttpActionResult PutPassenger(long id, Passenger passenger)
        {
            //check for validation
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            //if the updated passenger id do not match with the given id it show BadRequest
            if (id != passenger.PassengerId)
            {
                return(BadRequest());
            }

            //show the updated/edited details of passengers
            db.Entry(passenger).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            //Exception thrown by when it was expected that SaveChanges for
            //an passenger would result in a database update but in fact no rows in
            //the database were affected
            catch (DbUpdateConcurrencyException)
            {
                //if passenger id do not match(not existed) it return NOTFound
                if (!PassengerExists(id))
                {
                    return(NotFound());
                }
                //otherwise throw the exception(DbUpdateConcurrencyException)
                else
                {
                    throw;
                }
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
        /// <summary>
        /// Method create the new booking account
        /// </summary>
        /// <returns></returns>
        public int AddBooking(Booking booking)
        {
            try {
                using (AirlineDBEntities db = new AirlineDBEntities())
                {
                    // Checks If there is destination and source is availible
                    Flight flight = db.Flights.Where <Flight>(
                        f => booking.Destination.Equals(f.Destination) &&
                        booking.Source.Equals(f.Source)
                        ).First();

                    // If data does not matches with flight data then it is null
                    if (flight == null)
                    {
                        throw new BookingsException("Invalid Details");
                    }

                    // If AvailableSeats is less then user requuired seats then it does not stores data
                    if (booking.NoOfSeats > flight.AvailableSeats)
                    {
                        throw new BookingsException("Seats unavailable");
                    }


                    //Customer customer=db.Customers.Where<Flight>(
                    //    f => booking.Destination.Equals(f.Destination)
                    //    && booking.Source.Equals(f.Source)
                    //    ).First();

                    booking.FlightId   = flight.FlightId;
                    booking.CustomerId = 100;
                    // Counts Ticket Fare
                    booking.TicketFare = booking.NoOfSeats * 2000;

                    // Sets Ticket Status
                    booking.TicketStatus = "NOTC";

                    //Adds booking data to booking table
                    db.Bookings.Add(booking);
                    //Save all cahnges made in the context in database
                    db.SaveChanges();
                    return(booking.BookingId);
                }
            }
            catch (Exception ex)
            {
                //throw user defined FlightException
                throw new BookingsException(ex.Message);
            }
        }
예제 #21
0
        ///// <summary>
        ///// Method delete the existing customer account
        ///// </summary>
        ///// <param name="id"></param>
        ///// <returns></returns>
        public bool DeleteCancellationById(int id)
        {
            using (AirlineDBEntities db = new AirlineDBEntities())
            {
                Cancellation cancellation = db.Cancellations.Find(id);
                db.Configuration.LazyLoadingEnabled = false;
                if (cancellation == null)
                {
                    return(false);;
                }

                db.usp_DeleteCancelledTicketById(id);
                db.SaveChanges();

                return(true);
            }
        }
예제 #22
0
        ///// <summary>
        ///// Method delete the existing booking account
        ///// </summary>
        ///// <param name="id"></param>
        ///// <returns></returns>
        public bool DeleteBookingBYId(int id)
        {
            using (AirlineDBEntities db = new AirlineDBEntities())
            {
                Booking booking = db.Bookings.Find(id);
                db.Configuration.LazyLoadingEnabled = false;
                if (booking == null)
                {
                    return(false);;
                }

                db.Bookings.Remove(booking);
                db.SaveChanges();

                return(true);
            }
        }
        ///// <summary>
        ///// Method delete the existing customer account
        ///// </summary>
        ///// <param name="id"></param>
        ///// <returns></returns>
        public bool DeleteCustomerById(int id)
        {
            AirlineDBEntities db = new AirlineDBEntities();
            {
                Customer customer = db.Customers.Find(id);
                db.Configuration.LazyLoadingEnabled = false;
                if (customer == null)
                {
                    return(false);;
                }

                db.Customers.Remove(customer);
                db.SaveChanges();

                return(true);
            }
        }
 /// <summary>
 /// Method create the new customer account
 /// </summary>
 /// <returns></returns>
 public bool AddCustomer(Customer customer)
 {
     AirlineDBEntities db = new AirlineDBEntities();
     {
         if ((db.Customers.Count(c => c.Email == customer.Email)) == 0)
         {
             db.Customers.Add(customer);
             db.SaveChanges();
             db.Configuration.LazyLoadingEnabled = false;
             return(true);
         }
         else
         {
             return(false);
         }
     }
 }
예제 #25
0
        public static string BlockingOrderPaid(long id, string CCNo, string CVV)
        {
            db = new AirlineDBEntities();
            var        o    = GetOrder(id);
            CreditCard card = db.CreditCard.FirstOrDefault(c => c.CCNo == CCNo);

            if (card == null || card.CVV != CVV)
            {
                return("Error: Credit Card is not valid.");
            }
            if (card.Balance < o.Total)
            {
                return("Error: Not enough money in account.");
            }
            if (o != null)
            {
                // Change status
                o.Status = 1;
                int totalDistance = 0;

                // Calculate flight distance (flightdistance x total people)
                var TicketList = TicketDAO.GetTicketList(o.OrderID);
                //int totalCount = TicketList.Count();
                foreach (var item in TicketList)
                {
                    var TInfo = TicketList.FirstOrDefault(t => t.TicketID == item.TicketID);
                    var objD  = db.FlightDistance.Where(fd => fd.AirportID1 == TInfo.Flight.Route.Departure && fd.AirportID2 == TInfo.Flight.Route.Destination).FirstOrDefault();
                    if (objD == null)
                    {
                        objD = db.FlightDistance.Where(fd => fd.AirportID1 == TInfo.Flight.Route.Destination && fd.AirportID2 == TInfo.Flight.Route.Departure).FirstOrDefault();
                    }
                    totalDistance += objD.Distance;
                }

                // Add total distance to skymiles in User
                db.User.FirstOrDefault(u => u.UserID == o.UserID).Skymiles += totalDistance;

                // Charging
                card.Balance = card.Balance - o.Total;

                db.SaveChanges();
                return("ok");
            }
            return("Error: Order ID not valid.");
        }
        ///// <summary>
        ///// Method updates the details of existing cutomer's account details
        ///// </summary>
        ///// <param name="id">id of customer</param>
        ///// <param name="customer">customer object with customer details</param>
        ///// <returns></returns>
        public bool UpdateCustomer(int id, Customer customer)
        {
            AirlineDBEntities db = new AirlineDBEntities();
            {
                db.Configuration.LazyLoadingEnabled = false;
                Customer existingCustomer = db.Customers.Where(temp => temp.CustomerId == id).FirstOrDefault();



                try
                {
                    //update customer details if customer exists
                    if (existingCustomer != null)
                    {
                        existingCustomer.DOB             = customer.DOB;
                        existingCustomer.Email           = customer.Email;
                        existingCustomer.Name            = customer.Name;
                        existingCustomer.PhoneNo         = customer.PhoneNo;
                        existingCustomer.Pwd             = customer.Pwd;
                        existingCustomer.ResidingAddress = customer.ResidingAddress;
                        existingCustomer.WalletBalance   = customer.WalletBalance;

                        db.SaveChanges();
                        return(true);
                    }
                    else
                    {
                        return(false);
                    }
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!CustomerExists(id))
                    {
                        return(false);
                    }
                    else
                    {
                        throw;
                    }
                }
            }
        }
예제 #27
0
        ///// <summary>
        ///// Method updates the details of existing booking details
        ///// </summary>
        ///// <param name="id"></param>
        ///// <param name="booking"></param>
        ///// <returns></returns>
        public bool UpdateBooking(int id, Booking booking)
        {
            using (AirlineDBEntities db = new AirlineDBEntities())
            {
                //db.Configuration.LazyLoadingEnabled = false;
                //db.Entry(booking).State = EntityState.Modified;

                try
                {
                    //use LINQ query to find the Booking with  Bookingid
                    Booking book = db.Bookings.Where(b => b.BookingId == id).FirstOrDefault();

                    if (book != null)
                    {
                        //update  Booking  details
                        book.BookingId     = booking.BookingId;
                        book.Class         = booking.Class;
                        book.DateOfBooking = booking.DateOfBooking;
                        book.DateOfJourney = booking.DateOfJourney;
                        book.Destination   = booking.Destination;
                        book.NoOfSeats     = booking.NoOfSeats;
                        book.Source        = booking.Source;
                        book.TicketFare    = booking.TicketFare;
                        book.TicketStatus  = booking.TicketStatus;
                        //save changes to the database
                        db.SaveChanges();

                        return(true);
                    }
                    else
                    {
                        throw new BookingsException("Booking does not exist");
                    }
                }
                catch (BookingsException ex)
                {
                    throw new BookingsException(ex.Message);
                }
            }
        }
        /// <summary>
        /// Method updates or edits the changes of the passed flight in the flights table
        /// </summary>
        /// <param name="flight">object of type flight </param>
        /// <returns>boolean value indicating whether flight is updated or not</returns>
        public bool UpdateFlight(Flight flight)
        {
            try
            {
                //instantiating AirLineDBEntities Context class
                using (AirlineDBEntities db = new AirlineDBEntities())
                {
                    //use LINQ query to find the Flight with  flightid
                    Flight flt = db.Flights.Where(f => f.FlightId == f.FlightId).FirstOrDefault();

                    if (flt != null)
                    {
                        //update  flight  details
                        flt.FlightName     = flight.FlightName;
                        flt.Source         = flight.Source;
                        flt.Destination    = flight.Destination;
                        flt.DepartureTime  = flight.DepartureTime;
                        flt.ArrivalTime    = flight.ArrivalTime;
                        flt.BaggageLimit   = flight.BaggageLimit;
                        flt.AvailableSeats = flight.AvailableSeats;
                        //save changes to the database
                        db.SaveChanges();

                        return(true);
                    }


                    else
                    {
                        throw new FlightException("Flight does not exist");
                    }
                }
            }
            catch (FlightException ex)
            {
                //throw user defined FlightException
                throw new FlightException(ex.Message);
            }
        }
예제 #29
0
        // Function that process Payment request
        // Will return "ok:[orderid]" if success
        // Will return any other error string when failed
        public static string ProcessPayment(Payment payment, bool isBlocked)
        {
            db = new AirlineDBEntities();
            // Checking Creditcard
            CreditCard card = db.CreditCard.FirstOrDefault(c => c.CCNo == payment.CCNo);

            if (!isBlocked)
            {
                if (card == null || card.CVV != payment.CVV)
                {
                    return("Error: Credit Card is not valid.");
                }
                if (card.Balance < payment.Total)
                {
                    return("Error: Not enough money in account.");
                }
            }

            string s = "";

            try
            {
                // Create Order
                DateTime now        = DateTime.Now;
                string   OrderIDStr = "" + now.Year + now.Month + now.Day + now.Hour + now.Minute + now.Second;
                Int64    OrderID    = Int64.Parse(OrderIDStr);
                double   Total      = 0;

                Order order = new Order();
                order.OrderID   = OrderID;
                order.OrderDate = now;
                if (isBlocked)
                {
                    order.Status = 0;
                }
                else
                {
                    order.Status = 1;
                }

                order.UserID = payment.UserID;
                order.Total  = Total;

                db.Order.Add(order);
                int ticketCount = 1;

                // Create Oneway / First Flight/ First Stop Ticket
                Flight FInfo1 = FlightDAO.GetFlight(payment.FNo1);
                payment.Passengers.ForEach(p =>
                {
                    Ticket ticket  = new Ticket();
                    Int64 TicketID = Int64.Parse(OrderID.ToString() + ticketCount);
                    double price   = 0;
                    if (p.Age >= 14)
                    {
                        price += FInfo1.BasePrice + FInfo1.Route.Aircraft.ServiceFee;
                    }
                    else
                    {
                        price += FInfo1.BasePrice * 70 / 100 + FInfo1.Route.Aircraft.ServiceFee;
                    }
                    double daysToDeparture = (FInfo1.DepartureTime - DateTime.Now).TotalDays;
                    if (daysToDeparture <= 14)
                    {
                        if (p.Age >= 14)
                        {
                            price += FInfo1.BasePrice * 0.02 * (14 - daysToDeparture);
                        }
                        else
                        {
                            price += FInfo1.BasePrice * 70 / 100 * 0.02 * (14 - daysToDeparture);
                        }
                    }

                    foreach (var item in p.Service)
                    {
                        price += ServiceDAO.GetService(item).ServiceFee;
                        db.TicketService.Add(new TicketService {
                            ServiceID = item, TicketID = TicketID
                        });
                    }
                    if (payment.Class == "F")
                    {
                        price += 20;
                    }
                    else if (payment.Class == "B")
                    {
                        price += 10;
                    }
                    ticket.TicketID   = TicketID;
                    ticket.OrderID    = OrderID;
                    ticket.FNo        = FInfo1.FNo;
                    ticket.PassportNo = p.PassportNo;
                    ticket.Class      = payment.Class;
                    ticket.Firstname  = p.Firstname;
                    ticket.Lastname   = p.Lastname;
                    ticket.Sex        = p.Sex;
                    ticket.Age        = p.Age;
                    ticket.IsReturn   = false;
                    ticket.Price      = price;
                    db.Ticket.Add(ticket);
                    Total += price;
                    ticketCount++;
                });

                // Create Second stop's ticket (if exists)
                Flight FInfo2 = FlightDAO.GetFlight(payment.FNo2);
                if (FInfo2 != null)
                {
                    payment.Passengers.ForEach(p =>
                    {
                        Ticket ticket  = new Ticket();
                        Int64 TicketID = Int64.Parse(OrderID.ToString() + ticketCount);
                        double price   = 0;
                        if (p.Age >= 14)
                        {
                            price += FInfo2.BasePrice + FInfo2.Route.Aircraft.ServiceFee;
                        }
                        else
                        {
                            price += FInfo2.BasePrice * 70 / 100 + FInfo2.Route.Aircraft.ServiceFee;
                        }
                        double daysToDeparture = (FInfo2.DepartureTime - DateTime.Now).TotalDays;
                        if (daysToDeparture <= 14)
                        {
                            if (p.Age >= 14)
                            {
                                price += FInfo2.BasePrice * 0.02 * (14 - daysToDeparture);
                            }
                            else
                            {
                                price += FInfo2.BasePrice * 70 / 100 * 0.02 * (14 - daysToDeparture);
                            }
                        }

                        foreach (var item in p.Service)
                        {
                            price += ServiceDAO.GetService(item).ServiceFee;
                            db.TicketService.Add(new TicketService {
                                ServiceID = item, TicketID = TicketID
                            });
                        }
                        if (payment.Class == "F")
                        {
                            price += 20;
                        }
                        else if (payment.Class == "B")
                        {
                            price += 10;
                        }
                        ticket.TicketID   = TicketID;
                        ticket.OrderID    = OrderID;
                        ticket.FNo        = FInfo2.FNo;
                        ticket.PassportNo = p.PassportNo;
                        ticket.Class      = payment.Class;
                        ticket.Firstname  = p.Firstname;
                        ticket.Lastname   = p.Lastname;
                        ticket.Sex        = p.Sex;
                        ticket.Age        = p.Age;
                        ticket.IsReturn   = false;
                        ticket.Price      = price;
                        db.Ticket.Add(ticket);
                        Total += price;
                        ticketCount++;
                    });
                }

                // Create Return Flight Ticket
                Flight ReFInfo = FlightDAO.GetFlight(payment.ReFNo);
                if (ReFInfo != null)
                {
                    payment.Passengers.ForEach(p =>
                    {
                        Ticket ticket  = new Ticket();
                        Int64 TicketID = Int64.Parse(OrderID.ToString() + ticketCount);
                        double price   = 0;
                        if (p.Age >= 14)
                        {
                            price += ReFInfo.BasePrice + ReFInfo.Route.Aircraft.ServiceFee;
                        }
                        else
                        {
                            price += ReFInfo.BasePrice * 70 / 100 + ReFInfo.Route.Aircraft.ServiceFee;
                        }

                        double daysToDeparture = (ReFInfo.DepartureTime - DateTime.Now).TotalDays;
                        if (daysToDeparture <= 14)
                        {
                            if (p.Age >= 14)
                            {
                                price += ReFInfo.BasePrice * 0.02 * (14 - daysToDeparture);
                            }
                            else
                            {
                                price += ReFInfo.BasePrice * 70 / 100 * 0.02 * (14 - daysToDeparture);
                            }
                        }

                        foreach (var item in p.Service)
                        {
                            price += ServiceDAO.GetService(item).ServiceFee;
                            db.TicketService.Add(new TicketService {
                                ServiceID = item, TicketID = TicketID
                            });
                        }
                        if (payment.Class == "F")
                        {
                            price += 20;
                        }
                        else if (payment.Class == "B")
                        {
                            price += 10;
                        }
                        ticket.TicketID   = TicketID;
                        ticket.OrderID    = OrderID;
                        ticket.FNo        = ReFInfo.FNo;
                        ticket.PassportNo = p.PassportNo;
                        ticket.Class      = payment.Class;
                        ticket.Firstname  = p.Firstname;
                        ticket.Lastname   = p.Lastname;
                        ticket.Sex        = p.Sex;
                        ticket.Age        = p.Age;
                        ticket.IsReturn   = true;
                        ticket.Price      = price;
                        db.Ticket.Add(ticket);
                        Total += price;
                        ticketCount++;
                    });
                }
                db.SaveChanges();

                db.Order.FirstOrDefault(o => o.OrderID == OrderID).Total = Total;

                // Seat Calculation
                var SFInfo1  = db.Flight.Where(f => f.FNo == payment.FNo1).FirstOrDefault();
                var SFInfo2  = db.Flight.Where(f => f.FNo == payment.FNo2).FirstOrDefault();
                var SReFInfo = db.Flight.Where(f => f.FNo == payment.ReFNo).FirstOrDefault();
                if (payment.Class == "F")
                {
                    SFInfo1.AvailSeatsF = SFInfo1.AvailSeatsF - payment.Passengers.Count();

                    if (SFInfo2 != null)
                    {
                        SFInfo2.AvailSeatsF = SFInfo2.AvailSeatsF - payment.Passengers.Count();
                    }
                    if (SReFInfo != null)
                    {
                        SReFInfo.AvailSeatsF = SReFInfo.AvailSeatsF - payment.Passengers.Count();
                    }
                }
                else if (payment.Class == "B")
                {
                    SFInfo1.AvailSeatsB = SFInfo1.AvailSeatsB - payment.Passengers.Count();
                    if (SFInfo2 != null)
                    {
                        SFInfo2.AvailSeatsB = SFInfo2.AvailSeatsB - payment.Passengers.Count();
                    }
                    if (SReFInfo != null)
                    {
                        SReFInfo.AvailSeatsB = SReFInfo.AvailSeatsB - payment.Passengers.Count();
                    }
                }
                else
                {
                    SFInfo1.AvailSeatsE = SFInfo1.AvailSeatsE - payment.Passengers.Count();
                    if (SFInfo2 != null)
                    {
                        SFInfo2.AvailSeatsE = SFInfo2.AvailSeatsE - payment.Passengers.Count();
                    }
                    if (SReFInfo != null)
                    {
                        SReFInfo.AvailSeatsE = SReFInfo.AvailSeatsE - payment.Passengers.Count();
                    }
                }
                db.SaveChanges();


                // Skymile
                if (order.Status == 1)
                {
                    int dis1  = 0;
                    int dis2  = 0;
                    int reDis = 0;
                    var objD1 = db.FlightDistance.Where(fd => fd.AirportID1 == FInfo1.Route.Departure && fd.AirportID2 == FInfo1.Route.Destination).FirstOrDefault();
                    if (objD1 == null)
                    {
                        objD1 = db.FlightDistance.Where(fd => fd.AirportID1 == FInfo1.Route.Destination && fd.AirportID2 == FInfo1.Route.Departure).FirstOrDefault();
                    }
                    dis1 = objD1.Distance * payment.Passengers.Count();

                    if (FInfo2 != null)
                    {
                        var objD2 = db.FlightDistance.Where(fd => fd.AirportID1 == FInfo2.Route.Departure && fd.AirportID2 == FInfo2.Route.Destination).FirstOrDefault();
                        if (objD2 == null)
                        {
                            objD2 = db.FlightDistance.Where(fd => fd.AirportID1 == FInfo2.Route.Destination && fd.AirportID2 == FInfo2.Route.Departure).FirstOrDefault();
                        }
                        dis2 = objD2.Distance * payment.Passengers.Count();
                    }

                    if (ReFInfo != null)
                    {
                        var objReD = db.FlightDistance.Where(fd => fd.AirportID1 == ReFInfo.Route.Departure && fd.AirportID2 == ReFInfo.Route.Destination).FirstOrDefault();
                        if (objReD == null)
                        {
                            objReD = db.FlightDistance.Where(fd => fd.AirportID1 == ReFInfo.Route.Destination && fd.AirportID2 == ReFInfo.Route.Departure).FirstOrDefault();
                        }
                        reDis = objReD.Distance * payment.Passengers.Count();
                    }

                    db.User.FirstOrDefault(u => u.UserID == order.UserID).Skymiles += dis1 + dis2 + reDis;
                }
                db.SaveChanges();

                // Charging creditcard
                if (!isBlocked)
                {
                    card.Balance = card.Balance - Total;
                }

                // Saving changes
                db.SaveChanges();
                return("ok:" + OrderID);
            }
            catch (Exception e)
            {
                return(e.Message);
            }
        }
예제 #30
0
        public static string CancelOrder(long id)
        {
            var o    = GetOrder(id);
            var card = bank.BankDAO.GetCreditCard(db.User.FirstOrDefault(u => u.UserID == o.UserID).CCNo);

            if (o.Status == 1)
            {
                if (card == null)
                {
                    return("Error: Credit Card is not valid.");
                }
            }
            if (o != null)
            {
                // Check if this order blocked or not
                bool IsBlocked = false;
                if (o.Status == 0)
                {
                    IsBlocked = true;
                }
                // Change status
                o.Status = 2;
                int    totalDistance = 0;
                double refund        = 0;
                var    TicketList    = TicketDAO.GetTicketList(o.OrderID);

                // If this order is not blocked one, make changes to skymiles
                if (!IsBlocked)
                {
                    // Calculate flight distance
                    foreach (var item in TicketList)
                    {
                        var TInfo = TicketList.FirstOrDefault(t => t.TicketID == item.TicketID);
                        var objD  = db.FlightDistance.Where(fd => fd.AirportID1 == TInfo.Flight.Route.Departure && fd.AirportID2 == TInfo.Flight.Route.Destination).FirstOrDefault();
                        if (objD == null)
                        {
                            objD = db.FlightDistance.Where(fd => fd.AirportID1 == TInfo.Flight.Route.Destination && fd.AirportID2 == TInfo.Flight.Route.Departure).FirstOrDefault();
                        }
                        totalDistance += objD.Distance;
                    }

                    // Subtract total distance from skymiles
                    var u = db.User.FirstOrDefault(user => user.UserID == o.UserID);
                    u.Skymiles = u.Skymiles - totalDistance;
                    db.SaveChanges();
                }


                // Add back AvailSeat to the Flights
                foreach (var item in TicketList)
                {
                    Flight FInfo = db.Flight.FirstOrDefault(f => f.FNo == item.FNo);
                    if (item.Class == "F")
                    {
                        FInfo.AvailSeatsF = FInfo.AvailSeatsF + 1;
                    }
                    else if (item.Class == "B")
                    {
                        FInfo.AvailSeatsB = FInfo.AvailSeatsB + 1;
                    }
                    else
                    {
                        FInfo.AvailSeatsE = FInfo.AvailSeatsE + 1;
                    }
                    db.SaveChanges();
                }

                // Refund to user account
                var firstFNo        = TicketList.Where(t => t.IsReturn == false).FirstOrDefault().FNo;
                var flight          = db.Flight.FirstOrDefault(f => f.FNo == firstFNo);
                var daysToDeparture = (flight.DepartureTime - DateTime.Now).TotalDays;
                if (daysToDeparture >= 14)
                {
                    refund = Convert.ToInt32(o.Total - o.Total * 0.02);
                }
                else
                {
                    refund = Convert.ToInt32(o.Total - o.Total * 0.02 * (14 - daysToDeparture));
                }
                var cardFinal = bank.BankDAO.GetCreditCard(db.User.FirstOrDefault(u => u.UserID == o.UserID).CCNo);
                cardFinal.Balance += refund;

                // Save changes
                db.SaveChanges();
                return("ok");
            }
            return("error");
        }