コード例 #1
0
        public IActionResult ShoppingCart()
        {
            DetailsViewModel     detailsViewModel = new DetailsViewModel();
            TotalViewModel       totalViewModel   = new TotalViewModel();
            List <CartViewModel> cart             = new List <CartViewModel>();
            var Result = Db.Carts.Include(c => c.Food).Where(c => c.ApplicationUserId == HttpContext.User.Claims.FirstOrDefault().Value).ToList(); /*to filter by Id and application user id*/

            var FinalTotal = 0;

            foreach (var item in Result)
            {
                var total = item.Food.Price * item.Quantity;
                cart.Add(new CartViewModel
                {
                    Id       = item.Id,
                    Price    = item.Food.Price,
                    Quantity = item.Quantity,
                    Name     = item.Food.Name,
                    Total    = total,
                    Currency = item.Food.Currency
                });
                FinalTotal += total;
            }
            totalViewModel.FinalTotal       = FinalTotal;
            totalViewModel.cartViewModels   = cart;
            detailsViewModel.TotalViewModel = totalViewModel;

            return(View(detailsViewModel));
        }
コード例 #2
0
        public IActionResult Total()
        {
            var totals         = new List <TotalViewModel>();
            var listUserDetail = new List <UserDetails>();

            using (var db = new ApplicationDbContext())
            {
                listUserDetail = db.UserDetails.ToList();
            }

            var listMaKH = listUserDetail.GroupBy(x => x.MaKH).Select(x => x.Key);
            var index    = 1;

            foreach (var maKh in listMaKH)
            {
                var totalView = new TotalViewModel
                {
                    Id   = index,
                    MaKH = maKh,
                    Tinh = listUserDetail.FirstOrDefault(x => x.MaKH == maKh).Tinh
                };
                var listOneKH = listUserDetail.Where(x => x.MaKH == maKh).ToList();
                foreach (var one in listOneKH)
                {
                    switch (one.Thang)
                    {
                    case EnumThang.Thang01:
                        totalView.DoanhThuThang1 += one.TienThanhToan;
                        break;

                    case EnumThang.Thang02:
                        totalView.DoanhThuThang2 += one.TienThanhToan;
                        break;

                    case EnumThang.Thang03:
                        totalView.DoanhThuThang3 += one.TienThanhToan;
                        break;

                    case EnumThang.Thang04:
                        totalView.DoanhThuThang4 += one.TienThanhToan;
                        break;

                    case EnumThang.Thang05:
                        totalView.DoanhThuThang5 += one.TienThanhToan;
                        break;

                    case EnumThang.Thang06:
                        totalView.DoanhThuThang6 += one.TienThanhToan;
                        break;

                    case EnumThang.Thang07:
                        totalView.DoanhThuThang7 += one.TienThanhToan;
                        break;

                    case EnumThang.Thang08:
                        totalView.DoanhThuThang8 += one.TienThanhToan;
                        break;

                    case EnumThang.Thang09:
                        totalView.DoanhThuThang9 += one.TienThanhToan;
                        break;

                    case EnumThang.Thang10:
                        totalView.DoanhThuThang10 += one.TienThanhToan;
                        break;

                    case EnumThang.Thang11:
                        totalView.DoanhThuThang11 += one.TienThanhToan;
                        break;

                    case EnumThang.Thang12:
                        totalView.DoanhThuThang12 += one.TienThanhToan;
                        break;

                    default:
                        break;
                    }
                }

                totals.Add(totalView);
                index++;
            }

            return(View(totals));
        }