Exemplo n.º 1
0
        /// <summary>
        /// Register - add Customer Row row from serialized dictionary
        /// </summary>
        /// <param name="dictionaryCust">packaged customer info</param>
        /// <returns>int representing newly updated id for customer or -1 if error</returns>
        public int Register(byte[] bytCustomer)
        {
            int custId = -1;
            Customer cust = new Customer();
            eStoreDBEntities dbContext = new eStoreDBEntities();

            try
            {
                Dictionary<string, Object> dictionaryCustomer = (Dictionary<string, Object>)Deserializer(bytCustomer);
                dbContext = new eStoreDBEntities();
                //  Change to retrieve and update based on Username  when SimpleMembership is added (next 2 lines)
                String username = Convert.ToString(dictionaryCustomer["username"]);
                cust = dbContext.Customers.FirstOrDefault(c => c.Username == username);
                cust.Username = Convert.ToString(dictionaryCustomer["username"]);
                cust.FirstName = Convert.ToString(dictionaryCustomer["firstname"]);
                cust.LastName = Convert.ToString(dictionaryCustomer["lastname"]);
                cust.Email = Convert.ToString(dictionaryCustomer["email"]);
                cust.Age = Convert.ToInt32(dictionaryCustomer["age"]);
                cust.Address1 = Convert.ToString(dictionaryCustomer["address1"]);
                cust.City = Convert.ToString(dictionaryCustomer["city"]);
                cust.Mailcode = Convert.ToString(dictionaryCustomer["mailcode"]);
                cust.Region = Convert.ToString(dictionaryCustomer["region"]);
                cust.Country = Convert.ToString(dictionaryCustomer["country"]);
                cust.Creditcardtype = Convert.ToString(dictionaryCustomer["creditcardtype"]);
                dbContext.SaveChanges();
                custId = cust.CustomerID;
            }
            catch (Exception ex)
            {
                ErrorRoutine(ex, "CustomerModel", "Register");
            }
            return custId;
        }
Exemplo n.º 2
0
        /// <summary>
        /// Add Order Method - Adds order row, line item rows via trans
        /// </summary>
        /// <param name="HashtableOrder"></param>
        /// <returns>populated dictionary with new order #, bo flag or error</returns>
        public byte[] AddOrder(byte[] bytOrder)
        {
            Dictionary<string, Object> dictionaryOrder = (Dictionary<string, Object>)Deserializer(bytOrder);
            Dictionary<string, Object> dictionaryReturnValues = new Dictionary<string, Object>();
            // deserialize dictionary contents into local variables
            int[] qty = (int[])dictionaryOrder["qty"];
            string[] prodcd = (string[])dictionaryOrder["prodcd"];
            Decimal[] sellPrice = (Decimal[])dictionaryOrder["msrp"];
            int cid = Convert.ToInt32(dictionaryOrder["cid"]);

            bool boFlg = false;
            Decimal oTotal = Convert.ToDecimal(dictionaryOrder["amt"]);
            eStoreDBEntities dbContext;

            // Define a transaction scope for the operations.
            using (TransactionScope transaction = new TransactionScope())
            {
                try
                {
                    dbContext = new eStoreDBEntities();
                    Order myOrder = new Order();

                    // back to the db to get the right Customer based on session customer id
                    var selectedCusts = dbContext.Customers.Where(c => c.CustomerID == cid);
                    myOrder.Customer = selectedCusts.First();

                    // build the order
                    myOrder.OrderDate = DateTime.Now;
                    myOrder.OrderAmount = oTotal;
                    dbContext.Orders.Add(myOrder); // Add order and get OrderID for line Item

                    for (int idx = 0; idx < qty.Length; idx++)
                    {
                        if (qty[idx] > 0)
                        {
                            OrderLineitem item = new OrderLineitem();
                            string pcd = prodcd[idx];
                            var selectedProds = dbContext.Products.Where(p => p.ProductID == pcd);
                            item.Product = selectedProds.First(); // got product for item

                            if (item.Product.QtyOnHand > qty[idx])   // enough stock
                            {
                                item.Product.QtyOnHand = item.Product.QtyOnHand - qty[idx];
                                item.QtySold = qty[idx];
                                item.QtyOrdered = qty[idx];
                                item.QtyBackOrdered = 0;
                                item.SellingPrice = sellPrice[idx];
                            }
                            else  // not enough stock
                            {
                                item.QtyBackOrdered = qty[idx] - item.Product.QtyOnHand;
                                item.Product.QtyOnBackOrder = item.Product.QtyOnBackOrder + (qty[idx] - item.Product.QtyOnHand);
                                item.Product.QtyOnHand = 0;
                                item.QtyOrdered = qty[idx];
                                item.QtySold = item.QtyOrdered - item.QtyBackOrdered;
                                item.SellingPrice = sellPrice[idx];
                                boFlg = true; // something backordered
                            }
                            myOrder.OrderLineitems.Add(item);
                        }
                    }

                    dbContext.SaveChanges();   // made it this far, persist changes
                    // throw new Exception("Rollback");  // test trans by uncommenting out this line

                    // Mark the transaction as complete.
                    transaction.Complete();

                    dictionaryReturnValues.Add("orderid", myOrder.OrderID);
                    dictionaryReturnValues.Add("boflag", boFlg);
                    dictionaryReturnValues.Add("message", "");
                }
                catch (Exception e)  // if the catch is hit, the trans will be rolled back by the framework
                {
                    ErrorRoutine(e, "OrderData", "AddOrder");
                    dictionaryReturnValues.Add("message", "Problem with Order");
                }
                return Serializer(dictionaryReturnValues);
            }
        }