예제 #1
0
        public Order(Customer customer, string orderNumber)
        {
            ValidateCustomer(customer);
            this.UserName = customer.UserName;
            this.OrderNumber = orderNumber;
            this.ID = Guid.NewGuid();
            this.DiscountAmount = 0;
            this.DiscountReason = "--";
            this.DateCreated = DateTime.Now;
            this.CurrentState = new NewOrder(this);

            //new up the lists
            Items = new LazyList<OrderLine>();
            Transactions = new LazyList<Transaction>();

            //add the items
            AddCartItemsToOrder(customer.Cart);
            this.ShippingAmount = customer.Cart.ShippingAmount;
            this.ShippingAddress = customer.Cart.ShippingAddress;
            this.ShippingService = customer.Cart.ShippingService;
        }
예제 #2
0
        void ValidateCustomer(Customer customer)
        {
            //the customer cannot be null
            Validator.CheckNull(customer, Messages.NullCustomer);

            //neither can the their cart
            Validator.CheckNull(customer.Cart, Messages.NullCustomerCart);

            //cart must have 1 or more items
            Validator.CheckGreaterThan(x => x.Cart.Items.Count, customer, 1);
        }
예제 #3
0
        public Customer GetCustomer(string userName)
        {
            var productRepo = new SimpleProductRepository();
            Customer result = null;
            var batch = new BatchSql();

            //see if there's a customer - if not create one
            object user = CustomersTable.Select(CustomersTable.Columns.UserName)
                .Where(CustomersTable.Columns.UserName, userName)
                .BuildCommand()
                .ExecuteScalar();

            if (user == null) {
                CustomersTable.Insert(
                    new Dictionary<string, object>() {
                    {CustomersTable.Columns.UserName,userName},
                    {CustomersTable.Columns.LanguageCode,System.Globalization.CultureInfo.CurrentCulture.TwoLetterISOLanguageName}
                })
                .BuildCommand()
                .ExecuteNonQuery();
            }

            //customer
            batch.Append(CustomersTable.Select()
                .Where(CustomersTable.Columns.UserName, userName));

            //addresses
            batch.Append(AddressesTable.Select()
                .Where(AddressesTable.Columns.UserName, userName));

            //shopping cart
            Guid orderID = GetCartID(userName);
            batch.Append(OrdersTable.Select()
                .Where(OrdersTable.Columns.OrderID, orderID));

            //items
            //avert your eyes if this bothers you
            var itemSql = new SqlStatement(connectionStringName);
            itemSql.Add(string.Format("SELECT {0}, {1} ", OrderItemsTable.COLUMN_LIST, ProductsTable.COLUMN_LIST));
            itemSql.InnerJoin(
                OrderItemsTable.TABLE_NAME,
                OrderItemsTable.ColumnsQualified.SKU,
                ProductsTable.TABLE_NAME,
                ProductsTable.ColumnsQualified.SKU)
                .Where(OrderItemsTable.Columns.OrderID, orderID);

            batch.Append(itemSql);

            var cmd = batch.BuildCommand(connectionStringName);

            using (var rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection)) {
                //customer
                if (rdr.Read()) {
                    result = new Customer(userName,
                        CustomersTable.ReadEmail(rdr),
                        CustomersTable.ReadFirst(rdr),
                        CustomersTable.ReadLast(rdr));
                } else {
                    result = new Customer();
                }
                //address
                result.AddressBook = new List<Address>();
                if (rdr.NextResult()) {
                    while (rdr.Read()) {
                        result.AddressBook.Add(LoadAddress(rdr));

                    }
                }
                //cart
                result.Cart = new ShoppingCart(userName);
                if (rdr.NextResult()) {
                    if (rdr.Read()) {
                        result.Cart.ShippingAddress = result.AddressBook.SingleOrDefault(x => x.AddressID == OrdersTable.ReadShippingAddressID(rdr));
                        result.Cart.BillingAddress = result.AddressBook.SingleOrDefault(x => x.AddressID == OrdersTable.ReadBillingAddressID(rdr));
                        result.Cart.ShippingService = OrdersTable.ReadShippingService(rdr) ?? "";
                        result.Cart.ShippingAmount = OrdersTable.ReadShippingAmount(rdr);
                        result.Cart.TaxAmount = OrdersTable.ReadTaxAmount(rdr);
                    }
                }

                //items
                if (rdr.NextResult()) {
                    while (rdr.Read()) {
                        var product = productRepo.LoadProduct(rdr);
                        result.Cart.Items.Add(new ShoppingCartItem(product, CartItemsTable.ReadQuantity(rdr), CartItemsTable.ReadDateAdded(rdr)));
                    }
                }
            }

            return result;
        }
예제 #4
0
        public void SetCustomer(bool reset)
        {
            _thisUserName = this.GetCommerceUserName(reset);

            var customer = _customerRepository.GetCustomer(_thisUserName);
            if (customer == null) {
                customer = new Customer();
                customer.UserName = _thisUserName;

                customer.LastName = "";
                customer.Email = "";
                customer.LanguageCode = System.Globalization.CultureInfo.CurrentUICulture.TwoLetterISOLanguageName;
                customer.FirstName = "Guest";
                //save em
                //_customerRepository.AddCustomer(customer);
                this.RememberCommerceUser(_thisUserName);
            }
            this.CurrentCustomer = customer;
            this.CurrentCart = customer.Cart;
        }
예제 #5
0
        public void Save(Customer c)
        {
            //save the customer
            List<DbCommand> commands = new List<DbCommand>();

            SqlStatement sql = null;
            var settings=new Dictionary<string,object>(){
                    {CustomersTable.Columns.Email, c.Email},
                    {CustomersTable.Columns.First, c.Email},
                    {CustomersTable.Columns.Last, c.Email},
                    {CustomersTable.Columns.LanguageCode, c.Email}
               };

            if (SqlHelper.RecordExists(connectionStringName,CustomersTable.TABLE_NAME,CustomersTable.Columns.UserName,c.UserName)) {
                //update
                sql=CustomersTable.Update(settings)
                    .Where(CustomersTable.Columns.UserName,c.UserName);

            } else {

                //insert
                settings.Add(CustomersTable.Columns.UserName, c.UserName);
                sql = CustomersTable.Insert(settings);
            }

            sql.BuildCommand().ExecuteNonQuery();
        }