コード例 #1
0
ファイル: OrderManager.cs プロジェクト: rajeshwarn/texxtoor
        /// <summary>
        /// Creates a new, detached <see cref="OrderProduct"/> for further processing.
        /// </summary>
        /// <param name="product">Product this order is derived from</param>
        /// <param name="userName">Assigned user</param>
        /// <returns>An <see cref="OrderProduct"/> instance.</returns>
        public OrderProduct CreateOrderProduct(Product product, string userName)
        {
            var op = new OrderProduct();

            using (var scope = Ctx.BeginTransaction()) {
                CreateOrderProductInternal(op, product, userName);
                // associate this product with the new orderproduct
                op.SourceProduct = product;
                scope.Commit();
            }
            return(op);
        }
コード例 #2
0
ファイル: OrderManager.cs プロジェクト: rajeshwarn/texxtoor
        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();
            }
        }