Exemplo n.º 1
0
        public bool AddProduct(string productName, string description, string imagePath, string unitPrice, int labelID, string status, string unitInStock, string categoryID, string userID)
        {
            var newProduct = new Product();
            newProduct.ProductName = productName;
            newProduct.Description = description;
            newProduct.ImagePath = imagePath;
            newProduct.UnitPrice = Convert.ToDouble(unitPrice);
            newProduct.LabelID = labelID;
            newProduct.Status = Convert.ToBoolean(status);
            newProduct.UnitInStock = Convert.ToInt16(unitInStock);
            newProduct.CategoryID = Convert.ToInt16(categoryID);
            newProduct.CreatedDate = DateTime.Now.Date;
            newProduct.CreatedBy = userID;
            newProduct.UnitsAvailable = Convert.ToInt16(unitInStock);
            newProduct.UnitsLock = 0;
            newProduct.Discount = 0;

            using (ProductContext _db = new ProductContext())
            {
                // Add product to DB.
                _db.Products.Add(newProduct);
                _db.SaveChanges();
            }
            return true;
        }
Exemplo n.º 2
0
 public bool DeleteProduct(int productID)
 {
     using (ProductContext _db = new ProductContext())
     {
         var product = (from c in _db.Products where c.ProductID == productID select c).FirstOrDefault();
         if (product != null)
         {
             _db.Products.Remove(product);
             _db.SaveChanges();
         }
         return true;
     }
 }
Exemplo n.º 3
0
 internal bool UpdateCategory(int ID, string Name)
 {
     using (ProductContext _db = new ProductContext())
     {
         var cat = _db.Categories.Where(c => c.CategoryID == ID).FirstOrDefault();
         if (cat != null)
         {
             cat.CategoryName = Name;
             _db.SaveChanges();
             return true;
         }
     }
     return false;
 }
Exemplo n.º 4
0
        internal bool AddCategory(string Name)
        {
            var newCategory = new Category();

            newCategory.CategoryName = Name;

            using (ProductContext _db = new ProductContext())
            {
                // Add product to DB.
                _db.Categories.Add(newCategory);
                _db.SaveChanges();
            }

            return true;
        }
Exemplo n.º 5
0
        internal bool DeleteCategory(int ID)
        {
            using (ProductContext _db = new ProductContext())
            {
                var myItem = (from c in _db.Categories where c.CategoryID == ID select c).FirstOrDefault();

                if (myItem.Products.Count() > 0)
                {
                    return false;
                }
                _db.Categories.Remove(myItem);
                _db.SaveChanges();

            }
            return true;
        }
Exemplo n.º 6
0
 private static void CheckCart()
 {
     ProductContext _db = new ProductContext();
     ShoppingCartActions cartActions = new ShoppingCartActions();
     while (true)
     {
         foreach (var item in _db.ShoppingCartItems.ToList())
         {
             if ((DateTime.Now - (DateTime)item.DateCreated  ).TotalMinutes >= 20)
             {
                 cartActions.RemoveItem(item.CartId, item.ProductId);
             }
         }
         _db.SaveChanges();
         Thread.Sleep(5000);
     }
 }
Exemplo n.º 7
0
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                NVPAPICaller payPalCaller = new NVPAPICaller();

                string retMsg = "";
                string token = "";
                string PayerID = "";
                NVPCodec decoder = new NVPCodec();
                token = Session["token"].ToString();

                bool ret = payPalCaller.GetCheckoutDetails(token, ref PayerID, ref decoder, ref retMsg);
                if (ret)
                {
                    Session["payerId"] = PayerID;

                    var myOrder = new Order();
                    myOrder.OrderDate = Convert.ToDateTime(decoder["TIMESTAMP"].ToString());
                    myOrder.Username = User.Identity.Name;
                    myOrder.FirstName = decoder["FIRSTNAME"].ToString();
                    myOrder.LastName = decoder["LASTNAME"].ToString();
                    myOrder.Address = decoder["SHIPTOSTREET"].ToString();
                    myOrder.City = decoder["SHIPTOCITY"].ToString();
                    myOrder.State = decoder["SHIPTOSTATE"].ToString();
                    myOrder.PostalCode = decoder["SHIPTOZIP"].ToString();
                    myOrder.Country = decoder["SHIPTOCOUNTRYCODE"].ToString();
                    myOrder.Email = decoder["EMAIL"].ToString();
                    myOrder.Total = Convert.ToDecimal(decoder["AMT"].ToString());

                    // Verify total payment amount as set on CheckoutStart.aspx.
                    try
                    {
                        decimal paymentAmountOnCheckout = Convert.ToDecimal(Session["payment_amt"].ToString());
                        decimal paymentAmoutFromPayPal = Convert.ToDecimal(decoder["AMT"].ToString());
                        if (paymentAmountOnCheckout != paymentAmoutFromPayPal)
                        {
                            Response.Redirect("CheckoutError.aspx?" + "Desc=Amount%20total%20mismatch.");
                        }
                    }
                    catch (Exception)
                    {
                        Response.Redirect("CheckoutError.aspx?" + "Desc=Amount%20total%20mismatch.");
                    }

                    // Get DB context.
                    ProductContext _db = new ProductContext();

                    // Add order to DB.
                    _db.Orders.Add(myOrder);
                    _db.SaveChanges();

                    // Get the shopping cart items and process them.
                    using (MenStore.Logic.ShoppingCartActions usersShoppingCart = new MenStore.Logic.ShoppingCartActions())
                    {
                        List<CartItem> myOrderList = usersShoppingCart.GetCartItems();

                        // Add OrderDetail information to the DB for each product purchased.
                        for (int i = 0; i < myOrderList.Count; i++)
                        {
                            // Create a new OrderDetail object.
                            var myOrderDetail = new OrderDetail();
                            myOrderDetail.OrderId = myOrder.OrderId;
                            myOrderDetail.Username = User.Identity.Name;
                            myOrderDetail.ProductId = myOrderList[i].ProductId;
                            myOrderDetail.Quantity = myOrderList[i].Quantity;
                            myOrderDetail.UnitPrice = Math.Round((double)(myOrderList[i].Product.UnitPrice - (myOrderList[i].Product.UnitPrice * myOrderList[i].Product.Discount)), 2);

                            // Add OrderDetail to DB.
                            //Product p = _db.Products.Find(myOrderList[i].ProductId);
                            ////set units in stock and unlock units
                            //p.UnitsLock -= myOrderList[i].Quantity;
                            //p.UnitInStock -= myOrderList[i].Quantity;
                            _db.OrderDetails.Add(myOrderDetail);
                            _db.SaveChanges();
                        }

                        // Set OrderId.
                        Session["currentOrderId"] = myOrder.OrderId;

                        // Display Order information.
                        List<Order> orderList = new List<Order>();
                        orderList.Add(myOrder);
                        ShipInfo.DataSource = orderList;
                        ShipInfo.DataBind();

                        // Display OrderDetails.
                        OrderItemList.DataSource = myOrderList;
                        OrderItemList.DataBind();
                    }
                }
                else
                {
                    Response.Redirect("CheckoutError.aspx?" + retMsg);
                }
            }
        }
Exemplo n.º 8
0
        private static void CheckLabel()
        {
            ProductContext _db = new ProductContext();
            ProductActions productAction = new ProductActions();
            while (true)
            {
                foreach (var item in _db.Products.ToList())
                {
                    DateTime date = (DateTime)item.CreatedDate;
                    DateTime newDate = DateTime.Now;
                    // Difference in days, hours, and minutes.
                    TimeSpan ts = newDate - date;
                    // Difference in days.
                    int differenceInDays = ts.Days;
                    if (differenceInDays >= 365)
                    {
                        if (item.Discount<0.50)
                        {
                            item.Discount = 0.50;
                        }
                        item.LabelID = 3;
                    }
                    else if (productAction.GetUnitSold(item.ProductID) >= 1000)
                    {
                        item.LabelID = 4;
                    }

                    else if (differenceInDays >= 7)
                    {
                        item.LabelID = 2;
                    }

                }
                _db.SaveChanges();
                Thread.Sleep(5000);
            }
        }
Exemplo n.º 9
0
 internal void UnlockUnits(int quantity, int id)
 {
     using (ProductContext _db = new ProductContext())
     {
         Product p = _db.Products.Find(id);
         p.UnitsLock -= quantity;
         _db.SaveChanges();
     }
 }
Exemplo n.º 10
0
        public void UpdateItem(string updateCartID, int updateProductID, int quantity)
        {
            using (var _db = new ProductContext())
            {
                try
                {
                    var myItem = (from c in _db.ShoppingCartItems where c.CartId == updateCartID && c.Product.ProductID == updateProductID select c).FirstOrDefault();
                    if (myItem != null)
                    {

                        int count = quantity;
                        count -= myItem.Quantity;
                        //if (count < 0)
                        //{
                        //    count *= (-1);
                        //}
                        LockUnits(count, updateProductID);
                        myItem.Quantity = quantity;
                        _db.SaveChanges();

                    }
                }
                catch (Exception exp)
                {
                    throw new Exception("ERROR: Unable to Update Cart Item - " + exp.Message.ToString(), exp);
                }
            }
        }
Exemplo n.º 11
0
 public void RemoveItem(string removeCartID, int removeProductID)
 {
     using (var _db = new ProductContext())
     {
         try
         {
             var myItem = (from c in _db.ShoppingCartItems where c.CartId == removeCartID && c.Product.ProductID == removeProductID select c).FirstOrDefault();
             if (myItem != null)
             {
                 // Remove Item.
                 _db.ShoppingCartItems.Remove(myItem);
                 UnlockUnits(myItem.Quantity, myItem.ProductId);
                 _db.SaveChanges();
             }
         }
         catch (Exception exp)
         {
             throw new Exception("ERROR: Unable to Remove Cart Item - " + exp.Message.ToString(), exp);
         }
     }
 }
Exemplo n.º 12
0
 public bool UpdateProduct(int ID, string productName, string description, string imagePath, double unitPrice, bool status, double discount, int unitInStock, int categoryID, string userId)
 {
     using (ProductContext _db = new ProductContext())
     {
         var product = _db.Products.Where(p => p.ProductID == ID).FirstOrDefault();
         if (product != null)
         {
             product.ProductName = productName;
             product.Description = description;
             product.ImagePath = imagePath;
             product.UnitPrice = unitPrice;
             product.Status = status;
             product.UnitInStock = unitInStock;
             product.CategoryID = categoryID;
             product.ModifiedDate = DateTime.Now.Date;
             product.ModifiedBy = userId;
             product.UnitsAvailable = unitInStock - product.UnitsLock;
             product.Discount = discount;
             _db.SaveChanges();
         }
     }
     return true;
 }
Exemplo n.º 13
0
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                // Verify user has completed the checkout process.
                if ((string)Session["userCheckoutCompleted"] != "true")
                {
                    Session["userCheckoutCompleted"] = string.Empty;
                    Response.Redirect("CheckoutError.aspx?" + "Desc=Unvalidated%20Checkout.");
                }

                NVPAPICaller payPalCaller = new NVPAPICaller();

                string retMsg = "";
                string token = "";
                string finalPaymentAmount = "";
                string PayerID = "";
                NVPCodec decoder = new NVPCodec();

                token = Session["token"].ToString();
                PayerID = Session["payerId"].ToString();
                finalPaymentAmount = Session["payment_amt"].ToString();

                bool ret = payPalCaller.DoCheckoutPayment(finalPaymentAmount, token, PayerID, ref decoder, ref retMsg);
                if (ret)
                {
                    // Retrieve PayPal confirmation value.
                    string PaymentConfirmation = decoder["PAYMENTINFO_0_TRANSACTIONID"].ToString();
                    TransactionId.Text = PaymentConfirmation;

                    ProductContext _db = new ProductContext();
                    // Get the current order id.
                    int currentOrderId = -1;
                    if (Session["currentOrderId"] != string.Empty)
                    {
                        currentOrderId = Convert.ToInt32(Session["currentOrderID"]);
                    }
                    Order myCurrentOrder;
                    if (currentOrderId >= 0)
                    {
                        // Get the order based on order id.
                        myCurrentOrder = _db.Orders.Single(o => o.OrderId == currentOrderId);
                        // Update the order to reflect payment has been completed.
                        myCurrentOrder.PaymentTransactionId = PaymentConfirmation;
                        List<OrderDetail> orderDetails = new List<OrderDetail>();
                        orderDetails = _db.OrderDetails.Where(o => o.OrderId == currentOrderId).ToList();
                        foreach (var item in orderDetails)
                        {
                            Product p = _db.Products.Find(item.ProductId);
                            //set units in stock and unlock units
                            p.UnitsLock -= item.Quantity;
                            p.UnitInStock -= item.Quantity;
                        }

                        // Save to DB.
                        _db.SaveChanges();
                    }

                    // Clear shopping cart.
                    using (MenStore.Logic.ShoppingCartActions usersShoppingCart =
                        new MenStore.Logic.ShoppingCartActions())
                    {
                        usersShoppingCart.EmptyCart();
                    }

                    // Clear order id.
                    Session["currentOrderId"] = string.Empty;
                }
                else
                {
                    Response.Redirect("CheckoutError.aspx?" + retMsg);
                }
            }
        }