/// <summary> /// After all steps are down through order process we make the product and related data persistent in the store. /// </summary> /// <param name="op"></param> /// <param name="userName"></param> /// <returns></returns> public OrderStore CreateOrderStore(OrderProduct op, string userName, dynamic fullFillmentState) { var user = Ctx.Users.First(u => u.UserName == userName); // get the address base Ctx.LoadProperty(user.Profile, "Addresses"); var addresses = user.Profile.Addresses; // make permanent var billingAddress = addresses.FirstOrDefault(a => a.Invoice == true); var shippingAddress = addresses.FirstOrDefault(a => a.Invoice == false); if (shippingAddress == null && billingAddress != null) { shippingAddress = billingAddress; } else { if (billingAddress == null && shippingAddress != null) { billingAddress = shippingAddress; } } var permbillAddress = new OrderInvoiceAddress { Invoice = true, Default = true }; billingAddress.CopyProperties <AddressBook>(permbillAddress, a => a.City, a => a.Country, a => a.Region, a => a.StreetNumber, a => a.Zip, a => a.Profile, a => a.Name); Ctx.OrderInvoiceAddresses.Add(permbillAddress); var permshipAddress = new OrderShippingAddress { Invoice = false, Default = true }; shippingAddress.CopyProperties <AddressBook>(permshipAddress, a => a.City, a => a.Country, a => a.Region, a => a.StreetNumber, a => a.Zip, a => a.Profile, a => a.Name); Ctx.OrderShippingAddresses.Add(permshipAddress); op.Store.FullFillment = FullFillmentState.Created; op.Store.TransactionBag = new JavaScriptSerializer().Serialize(fullFillmentState); op.Owner = user; SaveChanges(); return(op.Store); }
public void SetShippingAddressFromPayment(int orderProductId, OrderShippingAddress shippingAddress) { var op = Ctx.OrderProducts.Find(orderProductId); if (op != null) { shippingAddress.CopyProperties <AddressBook>(op.ShippingAddress, s => s.Name, s => s.City, s => s.StreetNumber, s => s.Country ); } SaveChanges(); }
private void CreateOrderProductInternal(OrderProduct op, Product product, string userName) { // scope product and address management using (var scope = Ctx.BeginTransaction()) { var web = Ctx.OrderMedias.FirstOrDefault(m => m.Name == "Web" && m.Available); op.Media = new List <OrderMedia>(new[] { web }); var user = Ctx.Users .Include(u => u.Profile.Addresses) .Single(u => u.UserName == userName); op.Owner = user; // orders are based on specific product product.CopyProperties <Product>(op, o => o.Colored, o => o.Content, o => o.Dedication, o => o.Issue, o => o.Name, o => o.Proprietor, o => o.SubTitle, o => o.Title, o => o.Owner, o => o.Work); op.Store = new OrderStore { FullFillment = FullFillmentState.Created, Name = String.Format(ControllerResources.OrderManager_CreateOrderProductInternal_Order_for__0_, op.Name) }; var sa = new OrderShippingAddress(); var ba = new OrderInvoiceAddress(); var userAddresses = user.Profile.Addresses.OfType <AddressBook>().ToList().Where(a => a.GetType() == typeof(AddressBook)); var hasDefault = user.Profile.Addresses.Any(a => a.Default); var hasInvoice = user.Profile.Addresses.Any(a => a.Invoice); // we assume that there is only one/no Default and one/no Invoice var defaultAddress = hasDefault ? userAddresses.Single(a => a.Default) : userAddresses.FirstOrDefault(a => a.GetType() == typeof(AddressBook)); var invoiceAddress = hasInvoice ? userAddresses.Single(a => a.Invoice) : userAddresses.FirstOrDefault(a => a.GetType() == typeof(AddressBook)); if (defaultAddress != null) { defaultAddress.CopyProperties <AddressBook>(sa, d => d.City, d => d.Country, d => d.Name, d => d.Region, d => d.StreetNumber, d => d.Zip ); sa.OrderProduct = op; sa.Profile = user.Profile; sa.Default = true; Ctx.AddressBook.Add(sa); op.ShippingAddress = sa; } else { throw new NoAddressException() { UserId = user.Id, EntityId = op.Id, EntityName = "OrderProduct" }; } // prepare billing address if (invoiceAddress != null) { defaultAddress.CopyProperties <AddressBook>(ba, d => d.City, d => d.Country, d => d.Name, d => d.Region, d => d.StreetNumber, d => d.Zip ); ba.OrderProduct = op; // assignment makes the address invisible for user and prevents further changes ba.Profile = user.Profile; ba.Invoice = true; op.BillingAddress = ba; Ctx.AddressBook.Add(ba); } else { throw new NoAddressException() { UserId = user.Id, EntityId = op.Id, EntityName = "OrderProduct" }; } Ctx.OrderProducts.Add(op); SaveChanges(); scope.Commit(); } }