Beispiel #1
0
        public ActionResult PurchasedDetails()
        {
            int id = Convert.ToInt32(Session["CustomerId"]);
            List <BookingDetail> purchasedDetailsList = BookingDetailDB.getPurchasedDetails(id);

            decimal total = 0;

            foreach (BookingDetail b in purchasedDetailsList)
            {
                total += b.BasePrice + b.AgencyCommission;
            }
            ViewBag.Cost = total.ToString("c");

            return(View(purchasedDetailsList));
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            // if Session Customer variable is not null then assign it
            // to a Customer object and set up a Razor variable for customer name.
            if (Session["Customer"] != null)
            {
                cust     = (Customer)Session["Customer"];
                custName = $"{cust.CustFirstName} {cust.CustLastName}";
            }
            else
            {
                Response.Redirect("~/login.aspx");
            }

            if (!IsPostBack) // on first time page load
            {
                // populate Package, Booking and BookingDetail Lists from database
                packages       = PackageDB.GetAllPackages();
                bookings       = BookingDB.GetAllBookings();
                bookingDetails = BookingDetailDB.GetAllBookingDetails();

                // Two table LINQ join to extract relevant Fields for display in the
                // Customer Orders table. This selection is customizable depending on
                // what the clients would like to have displayed in the Customer Orders
                // table. Fields are stored in BookingSummary objects.
                List <BookingSummary> bookSummary =
                    (from b in bookings
                     join bd in bookingDetails on b.BookingId equals bd.BookingId
                     where b.CustomerId == cust.CustomerId
                     select new BookingSummary // assign the Linq results to a List of BookingSummary objects.
                {
                    BookingNo = b.BookingNo,
                    BookingDate = ((DateTime)(b.BookingDate)).ToString("d"),
                    Description = bd.Description,
                    TripStartDate = ((DateTime)bd.TripStart).ToString("d"),
                    TripEndDate = ((DateTime)bd.TripEnd).ToString("d"),
                    BBasePrice = ((decimal)(bd.BasePrice)).ToString("c"),
                    BAgencyCommission = ((decimal)(bd.AgencyCommission)).ToString("c"),
                    Total = ((decimal)(bd.BasePrice + bd.AgencyCommission)).ToString("c")
                }).ToList();

                bool isEmpty = !bookSummary.Any();
                if (!isEmpty) // if there are Travel Products, execute the following block
                {
                    bookS = true;

                    // iterate through the BookingSummary List to calculate column
                    // totals for Price, Commission and Total
                    decimal totBase1 = 0.0m;
                    decimal totComm1 = 0.0m;
                    decimal totTotl1 = 0.0m;
                    foreach (BookingSummary book in bookSummary) // iterate throught the list to calculate totals
                    {
                        string temp = "";
                        temp      = (book.BBasePrice).Remove(0, 1); // remove dollar sign prior to parsing text to decimal
                        totBase1 += decimal.Parse(temp);            // calculate running total for Base Price
                        temp      = (book.BAgencyCommission).Remove(0, 1);
                        totComm1 += decimal.Parse(temp);
                        temp      = (book.Total).Remove(0, 1);
                        totTotl1 += decimal.Parse(temp);
                    }
                    // sort the BookingSummary List by descending date
                    List <BookingSummary> newList = bookSummary.OrderByDescending(b => b.BookingDate).ToList();

                    // Build a dummy BookingSummary Record for the last Totals line in the GridView
                    BookingSummary book1 = new BookingSummary();
                    book1.BookingNo         = "";
                    book1.BookingDate       = "";
                    book1.Description       = "";
                    book1.TripStartDate     = "";
                    book1.TripEndDate       = "Totals";
                    book1.BBasePrice        = totBase1.ToString("c");
                    book1.BAgencyCommission = totComm1.ToString("c");
                    book1.Total             = totTotl1.ToString("c");

                    newList.Add(book1);                 // add the Totals line to the List

                    gvwTravelData.DataSource = newList; // assign list as data source
                    gvwTravelData.DataBind();           // bind the GridView data

                    int rowCount = gvwTravelData.Rows.Count;
                    // format the Total row
                    gvwTravelData.Rows[rowCount - 1].Cells[4].Font.Bold = true;
                    gvwTravelData.Rows[rowCount - 1].Cells[5].Font.Bold = true;
                    gvwTravelData.Rows[rowCount - 1].Cells[6].Font.Bold = true;
                    gvwTravelData.Rows[rowCount - 1].Cells[7].Font.Bold = true;

                    gvwTravelData.HeaderRow.HorizontalAlign = HorizontalAlign.Center;
                }

                // Use a LINQ query to join the Bookings and Packages tables and poulate
                // a list of BookingSummary objects for Travel Packages.
                List <BookingSummary> packSummary =
                    (from b in bookings
                     join p in packages on b.PackageId equals p.PackageId
                     where b.CustomerId == cust.CustomerId
                     select new BookingSummary
                {
                    BookingNo = b.BookingNo.ToString(),
                    BookingDate = ((DateTime)(b.BookingDate)).ToString("d"),
                    Description = p.PkgDesc,
                    TripStartDate = ((DateTime)p.PkgStartDate).ToString("d"),
                    TripEndDate = ((DateTime)p.PkgEndDate).ToString("d"),
                    BBasePrice = ((decimal)(p.PkgBasePrice)).ToString("c"),
                    BAgencyCommission = ((decimal)(p.PkgAgencyCommission)).ToString("c"),
                    Total = ((decimal)(p.PkgBasePrice + p.PkgAgencyCommission)).ToString("c")
                }).ToList();

                isEmpty = !packSummary.Any();
                if (isEmpty)
                {
                    return;
                }

                packS = true;

                // iterate through the BookingSummary List to calculate column
                // totals for Price, Commission and Total
                decimal totBase = 0.0m;
                decimal totComm = 0.0m;
                decimal totTotl = 0.0m;
                foreach (BookingSummary pack in packSummary)
                {
                    string temp = "";
                    temp     = (pack.BBasePrice).Remove(0, 1);
                    totBase += decimal.Parse(temp);
                    temp     = (pack.BAgencyCommission).Remove(0, 1);
                    totComm += decimal.Parse(temp);
                    temp     = (pack.Total).Remove(0, 1);
                    totTotl += decimal.Parse(temp);
                }
                //Build a dummy BookingSummary Record for the last Totals line in the table
                BookingSummary pack1 = new BookingSummary();
                pack1.BookingNo         = "";
                pack1.BookingDate       = "";
                pack1.Description       = "";
                pack1.TripEndDate       = "";
                pack1.TripEndDate       = "Totals";
                pack1.BBasePrice        = totBase.ToString("c");
                pack1.BAgencyCommission = totComm.ToString("c");
                pack1.Total             = totTotl.ToString("c");

                packSummary.Add(pack1);

                if (packSummary != null)
                {
                    packS = true;
                }

                gvwTravelData1.DataSource = packSummary;    // assign Package list to GridView datasource
                gvwTravelData1.DataBind();                  // bind the GridView data

                int rowCount1 = gvwTravelData1.Rows.Count;
                // format the Totals Row
                gvwTravelData1.Rows[rowCount1 - 1].Cells[4].Font.Bold = true;
                gvwTravelData1.Rows[rowCount1 - 1].Cells[5].Font.Bold = true;
                gvwTravelData1.Rows[rowCount1 - 1].Cells[6].Font.Bold = true;
                gvwTravelData1.Rows[rowCount1 - 1].Cells[7].Font.Bold = true;
            }
        }
        public ActionResult ConfirmPurchase(int id)
        {
            // If a user is logged in, confirm purchase. Otherwise, redirect to login
            if (Session["CustomerId"] != null)
            {
                // Get CustomerId
                int custID = Convert.ToInt32(Session["CustomerId"]);
                // Get PackageId
                int pkgID = id;
                // Select package with given ID to create booking
                Models.Package pkg = db.Packages.FirstOrDefault(u => u.PackageId.Equals(pkgID));
                // Insert selected package into Bookings table
                Booking book = new Booking();
                book.BookingDate   = DateTime.Now;                          // BookingDate is the time the booking is made
                book.BookingNo     = RandomStringGenerator.RandomString(6); // creates random string of 6 characters
                book.TravelerCount = 1;                                     // project document specified always 1 traveler, for now
                book.CustomerId    = custID;
                book.TripTypeId    = "B";
                book.PackageId     = pkg.PackageId;
                book.BasePrice     = pkg.PkgBasePrice;

                /* "ItineraryNo" from BookingDetails does not auto-increment, AND allows
                 * duplicate values, so this SQL query will grab the highest ItineraryNo
                 * value, which we can add 1 to to make a new ItineraryNo */
                //string query =
                //    "SELECT TOP 1 ItineraryNo " +
                //    "FROM BookingDetails " +
                //    "ORDER BY ItineraryNo DESC";
                double itinerary = BookingDetailDB.GetLastItineraryNo();
                int    bookingId = BookingDetailDB.GetLastBookingId();

                /* Switch statement that determines the destination of a package, class, and
                 * RegionId of a package. There's nothing in the Packages table showing this,
                 * so the destinations are hardcoded, as well as the potential package IDs.
                 * Ideally, a destination column would be added to the Packages table */
                string destination;
                string region;
                string classID; // first class, business class, etc...

                switch (pkgID)
                {
                case 1:                   // Caribbean
                    destination = "Santo Domingo, Dominican Republic";
                    region      = "OTHR"; // Other region
                    classID     = "ECN";  // Economy Class
                    break;

                case 2:                  // Polynesia
                    destination = "Honolulu, United States";
                    region      = "NA";  // North America
                    classID     = "ECN"; // Economy Class
                    break;

                case 3:     // Asia
                    destination = "Bangkok, Thailand";
                    region      = "ASIA";
                    classID     = "DLX"; // Deluxe Class
                    break;

                case 4:                  // Europe
                    destination = "Rome, Italy";
                    region      = "EU";  // Europe and UK
                    classID     = "FST"; // First Class
                    break;

                default:                  // New packages without any assignment here, or errors
                    destination = "Atlantis";
                    region      = "OTHR"; // Other region
                    classID     = "BSN";  // Business Class
                    break;
                }

                // Create new BookingDetail for this booking
                Models.BookingDetail detail = new Models.BookingDetail();
                detail.ItineraryNo       = itinerary + 1;
                detail.TripStart         = pkg.PkgStartDate;
                detail.TripEnd           = pkg.PkgEndDate;
                detail.Description       = pkg.PkgDesc;
                detail.Destination       = destination;
                detail.BasePrice         = pkg.PkgBasePrice;
                detail.AgencyCommission  = pkg.PkgAgencyCommission;
                detail.BookingId         = bookingId + 1;
                detail.RegionId          = region;
                detail.ClassId           = classID;
                detail.FeeId             = "BK"; // "Booking Charge", hardcoded as no column for this in Packages
                detail.ProductSupplierId = 80;   // "80" corresponds to a company that
                                                 // provides airfare; hardcoded, once again

                try
                {
                    db.Entry(book).State = EntityState.Added;

                    /* Save changes to Bookings before BookingDetails, because of
                     * foreign key depencency for BookingId */
                    db.SaveChanges();
                    db.Entry(detail).State = EntityState.Added;
                    db.SaveChanges();

                    /* Put something in TempData, which is cleared after 1 request, so
                     * success message will only display the first time each package is booked,
                     * rather than every time Dashboard/Index is loaded */
                    TempData["PkgMessage2"] = "Package successfully booked!";
                    return(RedirectToAction("Index", "Dashboard"));
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            else
            {
                return(RedirectToAction("Login", "Customer"));
            }
        }