public StoreShoppingCart GetShoppingCart(Guid cartId) { var output = new StoreShoppingCart(); try { var mc = new ManagementContext(); DateTime now = DateTime.Now; var cart = mc.ShoppingCarts.Include("Items").Include("Items.StoreItem").Include("Items.StoreItem.Colors").Include("Items.StoreItem.Colors.Color").Include("Items.StoreItem.Merchant").FirstOrDefault(x => x.ShoppingCartId.Equals(cartId)); if (cart == null) return output; output.ShoppingCartId = cart.ShoppingCartId; output.Ip = cart.Ip; foreach (var shoppingCartItem in cart.Items) { output.ItemsCount += 1; var item = new StoreItemDisplay(); item.Merchant.MerchantId = shoppingCartItem.StoreItem.Merchant.MerchantId; item.Merchant.Name = shoppingCartItem.StoreItem.Merchant.ShopName; item.Merchant.TaxRate = shoppingCartItem.StoreItem.Merchant.TaxRate; if (shoppingCartItem.StoreItem.Merchant.CurrencyRate == null) { item.Merchant.Currency = "USD"; item.Merchant.CurrencyCost = 1; } else { item.Merchant.Currency = shoppingCartItem.StoreItem.Merchant.CurrencyRate.CurrencyAbbrName; item.Merchant.CurrencyCost = shoppingCartItem.StoreItem.Merchant.CurrencyRate.CurrencyExchangePerUSD; } item.Name = shoppingCartItem.StoreItem.Name; item.BasePrice = shoppingCartItem.StoreItem.Price; item.Price = (shoppingCartItem.StoreItem.Price * shoppingCartItem.Quantity); var color = DisplayStoreItemColor(shoppingCartItem.StoreItem.Colors.Where(x => x.Color.ColorIdCSharp == shoppingCartItem.Color).FirstOrDefault()); if (color != null) { item.Colors.Add(color); item.ColorAGB = color.CSharpColor; item.ColorHex = color.HexColor; } item.QuantityOrdered = shoppingCartItem.Quantity; item.Weight = (shoppingCartItem.StoreItem.Weight * shoppingCartItem.Quantity); item.ShoppingCartItemId = shoppingCartItem.ShoppingCartItemId; item.ArticleNumber = shoppingCartItem.StoreItem.ArticleNumber; if (shoppingCartItem.StoreItem.Merchant.CurrencyRate == null) { item.Currency = "USD"; item.CurrencyCost = 1; } else { item.Currency = shoppingCartItem.StoreItem.Merchant.CurrencyRate.CurrencyAbbrName; item.CurrencyCost = shoppingCartItem.StoreItem.Merchant.CurrencyRate.CurrencyExchangePerUSD; } item.Description = shoppingCartItem.StoreItem.Description; item.WillPickUpLocally = shoppingCartItem.WillPickUpLocally; if (!shoppingCartItem.WillPickUpLocally) { item.Shipping = shoppingCartItem.StoreItem.ShippingCosts; //if there are more than one of the same items being shipped, then we add on the additional fees. if (shoppingCartItem.Quantity > 1) item.Shipping += shoppingCartItem.StoreItem.ShippingCostsAdditional * (shoppingCartItem.Quantity - 1); } else item.Shipping = 0; item.CanRunOutOfStock = shoppingCartItem.StoreItem.CanRunOutOfStock; item.StoreItemId = shoppingCartItem.StoreItem.StoreItemId; item.Note = shoppingCartItem.StoreItem.Note; item.BaseTaxOnItem = Math.Round((item.BasePrice * Convert.ToDecimal(shoppingCartItem.StoreItem.Merchant.TaxRate)), 2); item.TotalTaxOnItem = (item.BaseTaxOnItem * shoppingCartItem.Quantity); item.PriceInclTax = Math.Round((item.Price + item.TotalTaxOnItem), 2); item.ItemType = (StoreItemTypeEnum)Enum.Parse(typeof(StoreItemTypeEnum), shoppingCartItem.StoreItem.ItemTypeEnum.ToString()); if (item.ItemType == StoreItemTypeEnum.Shirt) { item.ItemSize = (StoreItemShirtSizesEnum)Enum.Parse(typeof(StoreItemShirtSizesEnum), shoppingCartItem.Size.ToString()); item.ItemSizeEnum = shoppingCartItem.Size; } foreach (var photo in shoppingCartItem.StoreItem.Photos) { PhotoItem p = new PhotoItem(photo.ItemPhotoId, photo.ImageUrl, photo.ImageUrlThumb, photo.IsPrimaryPhoto, photo.AlternativeText); item.Photos.Add(p); } var store = output.Stores.Where(x => x.MerchantId == item.Merchant.MerchantId).FirstOrDefault(); if (store != null) { store.TotalPrice += item.Price; store.TotalShipping += item.Shipping; store.TotalAfterShipping += item.Price + item.Shipping; store.StoreItems.Add(item); } else { Store.Classes.Store s = new Classes.Store(); s.MerchantId = item.Merchant.MerchantId; s.Currency = item.Merchant.Currency; s.StripePublishableKey = item.Merchant.StripePublishableKey; s.Name = item.Merchant.Name; s.TotalPrice = item.Price; s.TotalShipping = item.Shipping; s.TotalAfterShipping = item.Price + item.Shipping; s.StoreItems.Add(item); output.Stores.Add(s); } } cart.Expires = DateTime.Now.AddDays(1); mc.SaveChanges(); } catch (Exception exception) { ErrorDatabaseManager.AddException(exception, exception.GetType()); } return output; }
public StoreShoppingCart CreateNewShoppingCart(Guid? merchantId, Guid userId = new Guid(), bool isRdn = false, string ip = null) { var storeCart = new StoreShoppingCart(ip); try { if (!merchantId.HasValue && !isRdn) throw new Exception("Invalid merchant id. (CreateNewShoppingCart in StoreGateway"); var mc = new ManagementContext(); Merchant merchant = null; if (merchantId.HasValue) merchant = mc.Merchants.FirstOrDefault(x => x.MerchantId.Equals(merchantId.Value)); else merchant = mc.Merchants.FirstOrDefault(x => x.IsRDNation.Equals(true)); if (merchant == null) return null; var cartId = Guid.NewGuid(); var dbcart = new DataModels.Store.ShoppingCart(); dbcart.Ip = ip; if (userId != new Guid()) dbcart.UserId = userId; //dbcart.Merchant = merchant; dbcart.ShoppingCartId = cartId; mc.ShoppingCarts.Add(dbcart); mc.SaveChanges(); storeCart.Stores.Add(new Classes.Store { MerchantId = merchant.MerchantId, Name = merchant.ShopName }); storeCart.ShoppingCartId = cartId; } catch (Exception exception) { ErrorDatabaseManager.AddException(exception, exception.GetType()); } return storeCart; }