public ActionResult GiftCheckout(string FriendEmail, int?SelectedCreditCardID) { AppUser userLoggedIn = db.Users.Find(User.Identity.GetUserId()); AppUser friend = db.Users.First(a => a.Email == FriendEmail); if (SelectedCreditCardID == null) { return(RedirectToAction("CheckoutPage", new { ErrorMessage = "Looks like you forgot to select a credit card. If you don't have a credit card, make sure to add one!" })); } if (DuplicatesExist()) { return(RedirectToAction("ShoppingCartIndex", new { ErrorMessage = "Looks like you have some duplicates in your shopping cart. Check back through your shopping cart!" })); } else { // throw in total price, songs, and albums into a new order Order NewOrder = new Order(); NewOrder.Customer = userLoggedIn; // add all the songs from shopping cart to this order foreach (var song in userLoggedIn.SongsInShoppingCart) { Song songToAdd = db.Songs.Find(song.Song.SongID); SongOrderBridge songBridgeToAdd = new SongOrderBridge(); decimal SongPricePostDiscounts = song.Song.SongPrice; // calculate total discounts foreach (Discount discount in songToAdd.SongDiscounts) { if (discount.IsActiveDiscount) { SongPricePostDiscounts = songToAdd.SongPrice * (1 - discount.DiscountPercentage); } } //properties of songbridgetoadd songBridgeToAdd.PriceAtPointOfPurchase = SongPricePostDiscounts * 1.0825m; songBridgeToAdd.SongInOrder = songToAdd; // add to order NewOrder.SongsInOrder.Add(songBridgeToAdd); // add all the songs to the recipient's songs list friend.Songs.Add(songToAdd); } // add all the albums from shopping cart to this order foreach (var album in userLoggedIn.AlbumsInShoppingCart) { Album albumToAdd = db.Albums.Find(album.Album.AlbumID); AlbumOrderBridge albumBridgeToAdd = new AlbumOrderBridge(); decimal AlbumPricePostDiscounts = album.Album.AlbumPrice; foreach (Discount discount in albumToAdd.AlbumDiscounts) { if (discount.IsActiveDiscount) { AlbumPricePostDiscounts = albumToAdd.AlbumPrice * (1 - discount.DiscountPercentage); } } //properties of songbridgetoadd albumBridgeToAdd.PriceAtPointOfPurchase = AlbumPricePostDiscounts * 1.0825m; albumBridgeToAdd.AlbumInOrder = albumToAdd; // add to the order NewOrder.AlbumsInOrder.Add(albumBridgeToAdd); // add all the albums to the recipient's albums list friend.Albums.Add(albumToAdd); } // add the total price to the order NewOrder.TotalPrice = (CalculateSongTotal() + CalculateAlbumTotal()) * 1.0825m; // add the credit card used NewOrder.CreditCardUsed = db.CreditCards.Find(SelectedCreditCardID); // is gift? NewOrder.IsGift = true; NewOrder.RecipientID = friend.Id; String GiftSongs = ""; foreach (var item in NewOrder.SongsInOrder) { GiftSongs += item.SongInOrder.SongName + ", "; } String GiftAlbums = " "; foreach (var item in NewOrder.AlbumsInOrder) { GiftAlbums += item.AlbumInOrder.AlbumName + ", "; } // send a new email to the recipient and the user who just placed the order EmailController.OrderGift(NewOrder.Customer, friend, GiftSongs, GiftAlbums); // clear out the shopping cart userLoggedIn.SongsInShoppingCart.Clear(); userLoggedIn.AlbumsInShoppingCart.Clear(); // add the order to the database db.Orders.Add(NewOrder); db.SaveChanges(); // take the customer to the order confirmation page so they can see the songs/albums they just purchased return(RedirectToAction("CheckoutConfirmationPage", "ShoppingCarts", new { RecipientID = friend.Id, PlacedOrderID = NewOrder.OrderID })); } }
public ActionResult Checkout(int?SelectedCreditCardID) { AppUser userLoggedIn = db.Users.Find(User.Identity.GetUserId()); // check to see if they have a credit card that they're purchasing with if (SelectedCreditCardID == null) { return(RedirectToAction("CheckoutPage", new { ErrorMessage = "Looks like you forgot to select a credit card. If you don't have a credit card, make sure to add one!" })); } // check to see that there are no duplicates first of all if (DuplicatesExist()) { return(RedirectToAction("ShoppingCartIndex", new { ErrorMessage = "Looks like you have some duplicates in your shopping cart. Check back through your shopping cart!" })); } else { // throw in total price, songs, and albums into a new order Order NewOrder = new Order(); NewOrder.Customer = userLoggedIn; // add all the songs from shopping cart to this order foreach (var song in userLoggedIn.SongsInShoppingCart) { Song songToAdd = db.Songs.Find(song.Song.SongID); SongOrderBridge songBridgeToAdd = new SongOrderBridge(); decimal SongPricePostDiscounts = song.Song.SongPrice; // calculate total discounts foreach (Discount discount in songToAdd.SongDiscounts) { if (discount.IsActiveDiscount) { SongPricePostDiscounts = songToAdd.SongPrice * (1 - discount.DiscountPercentage); } } //properties of songbridgetoadd songBridgeToAdd.PriceAtPointOfPurchase = SongPricePostDiscounts * 1.0825m; songBridgeToAdd.SongInOrder = songToAdd; // add to the order NewOrder.SongsInOrder.Add(songBridgeToAdd); // add all the songs to the customer's songs list userLoggedIn.Songs.Add(songToAdd); db.SaveChanges(); } // add all the albums from shopping cart to this order foreach (var album in userLoggedIn.AlbumsInShoppingCart) { Album albumToAdd = db.Albums.Find(album.Album.AlbumID); AlbumOrderBridge albumBridgeToAdd = new AlbumOrderBridge(); decimal AlbumPricePostDiscounts = album.Album.AlbumPrice; foreach (Discount discount in albumToAdd.AlbumDiscounts) { if (discount.IsActiveDiscount) { AlbumPricePostDiscounts = albumToAdd.AlbumPrice * (1 - discount.DiscountPercentage); } } //properties of songbridgetoadd albumBridgeToAdd.PriceAtPointOfPurchase = AlbumPricePostDiscounts * 1.0825m; albumBridgeToAdd.AlbumInOrder = albumToAdd; // add to the order NewOrder.AlbumsInOrder.Add(albumBridgeToAdd); // add all the albums to the customer's albums list userLoggedIn.Albums.Add(albumToAdd); } // add the total price to the order NewOrder.TotalPrice = (CalculateSongTotal() + CalculateAlbumTotal()) * 1.0825m; // add the credit card to the order NewOrder.CreditCardUsed = db.CreditCards.Find(SelectedCreditCardID); //isgift? NewOrder.IsGift = false; NewOrder.RecipientID = userLoggedIn.Id; //pick random song //take genre from song //query song with highest rating of that genre List <short> SongsList = new List <short>(); if (userLoggedIn.SongsInShoppingCart != null || userLoggedIn.SongsInShoppingCart.Count() > 0) { foreach (var item in userLoggedIn.SongsInShoppingCart) { foreach (var item2 in item.Song.SongGenres) { SongsList.Add(item2.GenreID); } } } if (userLoggedIn.AlbumsInShoppingCart != null || userLoggedIn.AlbumsInShoppingCart.Count() > 0) { foreach (var item in userLoggedIn.AlbumsInShoppingCart) { foreach (var item2 in item.Album.AlbumGenres) { SongsList.Add(item2.GenreID); } } } String AllSongsPurchased = ""; foreach (var item in userLoggedIn.SongsInShoppingCart) { AllSongsPurchased += item.Song.SongName + ", "; } String AllAlbumsPurchased = " "; foreach (var item in userLoggedIn.AlbumsInShoppingCart) { AllAlbumsPurchased += item.Album.AlbumName + ", "; } short RandomGenre = SongsList[0]; var query = from s in db.Songs where (s.SongGenres.Any(a => a.GenreID == RandomGenre)) select s; //query = from s in db.Songs where (s == s.SongRatings.First(x => x.RatingNumber.Max)) select s; List <Song> BestSongs = query.ToList(); String SongRecommendation = BestSongs[0].SongName; EmailController.OrderCustomer(userLoggedIn, AllSongsPurchased, AllAlbumsPurchased, SongRecommendation, NewOrder.OrderID); // clear out the shopping cart userLoggedIn.SongsInShoppingCart.Clear(); userLoggedIn.AlbumsInShoppingCart.Clear(); // add the order to the database db.Orders.Add(NewOrder); db.SaveChanges(); // take the customer to the confirmation pageso they can see the songs/albums they just purchased return(RedirectToAction("CheckoutConfirmationPage", "ShoppingCarts", new { RecipientID = userLoggedIn.Id, PlacedOrderID = NewOrder.OrderID })); } }