/// <summary> /// Determines if the supplied special can be applied to this transaction /// </summary> /// <param name="special"></param> /// <param name="transaction"></param> /// <returns>True if applies, false otherwise</returns> public static bool DoesSpecialApply(Special special, Transaction transaction) { return DateTimeMethods.DoDatesOverlap(special.DateStart, special.DateEnd, transaction.RentalDateStart, transaction.RentalDateEnd); }
/// <summary> /// Saves a Transaction to the data store. /// </summary> /// <param name="item">The item to save</param> public static void Save(Transaction item) { if (item.IsItemModified) { if (item.TransactionId == null) { item.TransactionId = Insert(item); } else { Update(item); } } }
/// <summary> /// Calculates the total cost of the rental transaction to the customer /// </summary> /// <param name="transaction"></param> /// <returns></returns> public static decimal CalculateTransactionPrice(Transaction transaction) { Inventory item = InventoryManager.Load(transaction.InventoryId); decimal price = item.Price; Special foundSpecial = SpecialManager.LoadSpecialForTransaction(transaction); if ( foundSpecial != null) { price = foundSpecial.Price; } int amtOfDays = (transaction.RentalDateEnd - transaction.RentalDateStart).Days; price = (price * amtOfDays); return price; }
public void CreateTestObject() { int inventoryId; Guid userId = Guid.Empty; Random rand = new Random(); string errorMessage; string userName = string.Format("NewUser_{0}", rand.Next(1, 100000)); const string passWord = "******"; string email = string.Format("emailaddress{0}@test.com", rand.Next(1, 1000000000)); MembershipCreateStatus status; MembershipUser user = Membership.CreateUser(userName, passWord, email, "quieres apple?", "no, apple es para mujeres", true, out status); if (user != null && user.ProviderUserKey is Guid) { userId = (Guid)user.ProviderUserKey; } List<Inventory> inventoryList = InventoryManager.LoadAll().ToList(); if (inventoryList.SafeAny()) { inventoryId = inventoryList.First().InventoryId.GetValueOrDefault(); } else { CarMake newMake = new CarMake { Manufacturer = "TestManufacturer", Name = string.Format("Make_{0}", rand.Next(1, 1000)) }; CarMakeManager.Save(newMake, out errorMessage); CarModel newModel = new CarModel { MakeId = newMake.MakeId.GetValueOrDefault(), Name = string.Format("Model_{0}", rand.Next(1, 1000)) }; CarModelManager.Save(newModel, out errorMessage); Location location = new Location { Name = string.Format("Location_{0}", rand.Next(1, 100000)), Address = string.Format("{0} Street St", rand.Next(10, 1000)), City = "Fake City", State = "FS", Zip = "11111", Email = "*****@*****.**", Phone = "1112223333" }; LocationManager.Save(location, out errorMessage); Inventory inventory = new Inventory { ModelId = newModel.ModelId.GetValueOrDefault(), LocationId = location.LocationId.GetValueOrDefault(), Color = "Red", Price = (decimal) ((rand.NextDouble() + 0.1)*rand.Next(10, 60)), Quantity = rand.Next(1, 10), Year = rand.Next(1990, 2015) }; inventoryId = inventory.InventoryId.GetValueOrDefault(); } TransactionTestObject = new Transaction { BillingAddress = "123 Fake Street", BillingCity = "Fake City", BillingState = "FS", BillingZip = "12345", CCV = 123, CreditCardNumber = "4444999911110000", ExpirationDate = DateTime.Today.AddYears(rand.Next(5, 10)), InventoryId = inventoryId, Price = (decimal)( ( rand.NextDouble() + 0.1 ) * rand.Next(10, 60) ), RentalDateEnd = DateTime.Today.AddDays(rand.Next(11, 50)), RentalDateStart = DateTime.Today.AddDays(rand.Next(1, 10)), TransactionDate = DateTime.Today, UserId = userId }; TransactionManager.Save(TransactionTestObject, out errorMessage); }
private Transaction GetTransactionFromForm() { Transaction tranny = new Transaction { BillingAddress = ucPaymentForm.BillingAddress, UserId = UserId, BillingCity = ucPaymentForm.BillingCity, BillingState = ucPaymentForm.BillingStateCode, BillingZip = ucPaymentForm.BillingZipCode, CCV = ucPaymentForm.BillingCCV, CreditCardNumber = ucPaymentForm.CreditCardNumber, ExpirationDate = ucPaymentForm.CreditCardExpirationDate, IsItemModified = true, InventoryId = InventoryId, IsRentalTransactionInProgress = true, Price = RentalTotal, RentalDateEnd = RentalDateEnd, RentalDateStart = RentalDateStart, TransactionDate = DateTime.Now, TransactionId = null }; return tranny; }
/// <summary> /// Save Transaction Entity /// </summary> /// <param name="item">Entity to save</param> /// <param name="errorMessage">Error Message</param> /// <returns>return true if save successfully, else return false</returns> public static bool Save(Transaction item, out string errorMessage) { bool isValid = Validate(item, out errorMessage); if (isValid) { Inventory inventoryToSave = InventoryManager.Load(item.InventoryId); if (item.IsRentalTransactionInProgress) { inventoryToSave.Quantity--; isValid = InventoryManager.Save(inventoryToSave, out errorMessage); item.Price = CalculateTransactionPrice(item); } if (isValid) { TransactionDao.Save(item); } } return isValid; }
/// <summary> /// Validate Transaction Entity /// </summary> /// <param name="item">Entity to validate</param> /// <param name="errorMessage">error message if validation failed</param> /// <returns>return true if entity passes validation logic, else return false</returns> public static bool Validate(Transaction item, out string errorMessage) { StringBuilder builder = new StringBuilder(); if (item == null) { builder.AppendHtmlLine("*An unexpected error occurred. Please try again"); } MembershipUser user = Membership.GetUser(item.UserId); if (user == null) { builder.AppendHtmlLine("*UserId must be valid"); } Inventory inventory = InventoryManager.Load(item.InventoryId); if (inventory == null) { builder.AppendHtmlLine("*InventoryId must be valid"); } else { if (item.IsRentalTransactionInProgress) { if (inventory.Quantity == 0) { builder.AppendHtmlLine("*The product you selected cannot be rented at this time. Please change your selection and try again."); } } } if (item.CreditCardNumber.IsNullOrWhiteSpace()) { builder.AppendHtmlLine("*Credit Card Number is required"); } else if (item.CreditCardNumber.Length != 16) { builder.AppendHtmlLine("*Credit Card Number is invalid"); } if (!item.ExpirationDate.IsValidWithSqlDateStandards()) { builder.AppendHtmlLine("*Expiration Date must be valid."); } if (!item.TransactionDate.IsValidWithSqlDateStandards()) { builder.AppendHtmlLine("*Transaction Date must be valid"); } if (!item.RentalDateStart.IsValidWithSqlDateStandards()) { builder.AppendHtmlLine("*Rental DateStart must be valid"); } if (!item.RentalDateEnd.IsValidWithSqlDateStandards()) { builder.AppendHtmlLine("*Rental Date End must be valid"); } if (item.RentalDateEnd.OnOrBefore(item.RentalDateStart)) { builder.AppendHtmlLine("*Rental Date End must be after Rental Date Start"); } if (!item.TransactionId.HasValue && item.ExpirationDate.Before(DateTime.Today)) { builder.AppendHtmlLine("*Credit Card must not be expired"); } if (item.BillingAddress.IsNullOrWhiteSpace()) { builder.AppendHtmlLine("*BillingAddress is required."); } if (item.BillingCity.IsNullOrWhiteSpace()) { builder.AppendHtmlLine("*BillingCity is required"); } if (item.BillingState.IsNullOrWhiteSpace()) { builder.AppendHtmlLine("*BillingState is required"); } if (item.BillingZip.IsNullOrWhiteSpace()) { builder.AppendHtmlLine("*BillingZip is required"); } errorMessage = builder.ToString(); return errorMessage.IsNullOrWhiteSpace(); }
/// <summary> /// Updates a Transaction /// </summary> /// <param name="item">The Transaction item to save</param> private static void Update(Transaction item) { List<SqlParameter> parameters = new List<SqlParameter> { new SqlParameter("@TransactionId", item.TransactionId), new SqlParameter("@UserId", item.UserId), new SqlParameter("@CreditCardNumber", item.CreditCardNumber), new SqlParameter("@ExpirationDate", item.ExpirationDate), new SqlParameter("@CCV", item.CCV), new SqlParameter("@BillingAddress", item.BillingAddress), new SqlParameter("@BillingCity", item.BillingCity), new SqlParameter("@BillingState", item.BillingState), new SqlParameter("@BillingZip", item.BillingZip), new SqlParameter("@TransactionDate", item.TransactionDate), new SqlParameter("@InventoryId", item.InventoryId), new SqlParameter("@RentalDateStart", item.RentalDateStart), new SqlParameter("@RentalDateEnd", item.RentalDateEnd), new SqlParameter("@Price", item.Price) }; DataManager.ExecuteProcedure(KarzPlusConnectionString, "PKP_UpdateTransaction", parameters); }
/// <summary> /// Loads a special for a transaction if any /// </summary> /// <param name="transaction"></param> /// <returns></returns> public static Special LoadSpecialForTransaction(Transaction transaction) { List<Special> allSpecials = LoadByInventoryId(transaction.InventoryId).ToList(); Special foundSpecial = allSpecials.FirstOrDefault( dd => DateTimeMethods.DoDatesOverlap(dd.DateStart, dd.DateEnd, transaction.RentalDateStart, transaction.RentalDateEnd)); return foundSpecial; }