コード例 #1
0
        public ActionResult DeleteConfirmed(int id)
        {
            TransProd transProd = db.TransProds.Find(id);

            db.TransProds.Remove(transProd);
            db.SaveChanges();
            return(RedirectToAction("Index"));
        }
コード例 #2
0
        // GET: TransProds/Details/5
        public ActionResult Details(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            TransProd transProd = db.TransProds.Find(id);

            if (transProd == null)
            {
                return(HttpNotFound());
            }
            return(View(transProd));
        }
コード例 #3
0
        /************************Check out *************************/

        public ActionResult CheckOut()
        {
            if (Session["useremail"] == (null))
            {
                return(RedirectToAction("Login", "Users"));
            }
            else
            {
                double total = 00;
                foreach (var prd in Cart.Products)
                {
                    //Total of all products
                    total += prd.product_price;
                    //Reduce Quantity in product table
                    var result = db.Products.SingleOrDefault(b => b.Id == prd.Id);
                    if (result != null)
                    {
                        result.stock_remaining = prd.stock_remaining - 1;
                        db.SaveChanges();
                    }
                }

                //Add transaction to database- Transaction table
                Transaction tempadd = new Transaction
                {
                    products          = temp_prod,
                    Amount_Total      = total,
                    Method_Of_Payment = 1,  //get detail
                    Date    = DateTime.Now,
                    cart_id = Cart.temp_cart_id
                };

                db.Transactions.Add(tempadd);
                db.SaveChanges();
                //Get transaction-id from database because it is set as auto increment.
                var tran_obj = db.Transactions.SingleOrDefault(t => t.cart_id == Cart.temp_cart_id);
                int tran_id  = tran_obj.Id;
                //Add each product from current transaction to tran-prod table for admin analysis.
                TransProd transProd;
                //Get Quantity of single product
                //dictionary<productId,Quantity>
                Dictionary <int, int> qnty = new Dictionary <int, int>();
                foreach (var prd in Cart.Products)
                {
                    //Increase quantity if product is already in Dictionary
                    if (qnty.ContainsKey(prd.Id))
                    {
                        int temp = qnty[prd.Id];
                        qnty.Remove(prd.Id);
                        temp++;
                        qnty.Add(prd.Id, temp);
                    }
                    else
                    {
                        qnty.Add(prd.Id, 1);
                    }
                }
                //Add data to tran-prod table for one to many relaionship
                List <int> temp1 = new List <int>(Cart.Products.Count());
                temp1.Add(-8);
                foreach (var prd in Cart.Products)
                {
                    if (temp1.Contains(prd.Id))
                    {
                    }
                    else
                    {
                        temp1.Add(prd.Id);
                        transProd                 = new TransProd();
                        transProd.ProductId       = prd.Id;
                        transProd.ProductQuantity = qnty[prd.Id];
                        transProd.TransactionId   = tran_id;
                        db.TransProds.Add(transProd);
                        db.SaveChanges();
                    }
                }

                Cart.Products.Clear();  //Clear Cart at the end of transaction
                Cart.temp_cart_id = 0;  //Set Card id =0
                Cart.UserName     = ""; //Clear Username

                return(RedirectToAction("Index", "TransProds", new { id = tran_id }));
            }
        }