public dynamic GetList([FromQuery] VoucherCriteriaModel criteria, [FromQuery] IDataTablesRequest request)
        {
            try
            {
                //var query = _accountService.GetByCriteria(criteria);
                var query = _voucherService.GetAll();
                if (!string.IsNullOrEmpty(criteria.VoucherCode))
                {
                    query = query.Where(x => x.VoucherCode == criteria.VoucherCode);
                }

                if (criteria.FromStartDate.HasValue)
                {
                    query = query.Where(x => x.StartDate >= criteria.FromStartDate);
                }

                if (criteria.ToStartDate.HasValue)
                {
                    query = query.Where(x => x.StartDate <= criteria.ToStartDate);
                }

                if (criteria.FromEndDate.HasValue)
                {
                    query = query.Where(x => x.EndDate >= criteria.FromEndDate);
                }

                if (criteria.ToEndDate.HasValue)
                {
                    query = query.Where(x => x.EndDate <= criteria.ToEndDate);
                }

                var brands =
                    query
                    .Select(x => new VoucherRequestModel
                {
                    VoucherId    = x.VoucherId,
                    VoucherCode  = x.VoucherCode,
                    StartDate    = x.StartDate,
                    EndDate      = x.EndDate,
                    Price        = x.Price,
                    CustomerName = x.Account.FullName,
                    Email        = x.Account.Email,
                })
                    .ToList()
                    .AsQueryable();

                var filteredData = brands;

                return(ToDataTableResponse <VoucherRequestModel>(filteredData, request));
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, this.GetType().Name);
                return(ToDataTableResponse <VoucherRequestModel>());
            }
        }
        public ActionResult <IEnumerable <Voucher> > GetVouchers(
            [FromQuery(Name = "promotion-id")] int promotionId, [FromQuery(Name = "begin-date")] DateTime beginDate,
            [FromQuery(Name = "expired-date")] DateTime expiredDate, [FromQuery] string ids,
            [FromQuery(Name = "start-id")] int startId)
        {
            string query = ControllerContext.HttpContext.Request.QueryString.Value;
            IEnumerable <Voucher> list = _voucherSer.GetAll(query, promotionId, ids,
                                                            startId, beginDate, expiredDate);

            if (list == null)
            {
                return(NotFound());
            }
            return(Ok(list));
        }
        public ActionResult Index()
        {
            if (Session["UserId"] == null)
            {
                Session["UserId"] = _userSessionService.NewUser();
            }

            var response = new DealControllerIndexData
            {
                VoucherDetails = _userSessionService.GetVoucherForUser(Session["UserId"].ToString()),
                Vouchers       = VoucherDetailsMapper.Map(_voucherService.GetAll().VoucherDetails),
                Total          = _userSessionService.GetBasketTotalForUser(Session["UserId"].ToString()),
                LoggedIn       = _userSessionService.IsLoggedIn(Session["UserId"].ToString())
            };

            return(View(response));
        }
Esempio n. 4
0
        public async Task <IActionResult> GetAll()
        {
            var products = await _VoucherService.GetAll();

            return(Ok(products));
        }
 public IHttpActionResult GetAll()
 {
     return(Ok(VoucherDetailsMapper.Map(_voucherService.GetAll().VoucherDetails)));
 }
        public async Task <CreateOrderResponseModel> CreateOrder([FromBody] CreateOrderRequestModel model)
        {
            var response = new CreateOrderResponseModel
            {
                Result = false,
            };

            Voucher voucher = null;

            if (model.VoucherCode != null)
            {
                voucher =
                    _voucherService.GetAll()
                    .Where(x => x.VoucherCode == model.VoucherCode && x.IsDeleted == 0 && x.IsUsed == 0)
                    .FirstOrDefault();


                if (voucher != null)
                {
                    if (voucher.StartDate <= DateTime.Now && voucher.EndDate >= DateTime.Now)
                    {
                    }
                    else
                    {
                        voucher = null;
                    }
                }
            }

            var order = new Order
            {
                ContactName     = model.ContactName,
                Email           = model.Email,
                Phone           = model.Phone,
                Address         = model.Address,
                PaymentMethodId = PaymentMethodEnum.PAYMENT_AT_STORE,
                OrderStatusId   = OrderStatusEnum.WAIT,
                Discount        = 0,
                TotalAmount     = 0,
                TotalPrice      = 0,
                Note            = model.Note,
                OrderDetails    = new List <OrderDetail>(),
            };

            if (voucher != null)
            {
                order.VoucherId = voucher.VoucherId;
                order.Discount  = (double)voucher.Price;
            }

            //if(model.VoucherCode != null)
            //{
            //    var voucher = GetById from DB;

            //    order.Discount = voucher.Value;
            //}

            var currentUser = CurrentUser;

            if (currentUser != null)
            {
                order.CustomerId = currentUser.Id;
            }

            ApplyUserCreateEntity(order);


            double total = 0;

            foreach (var detail in model.OrderDetails)
            {
                var product = await _productService.GetByIdWithoutInclude(detail.ProductId);

                total += (double)product.Price * detail.Quantity;

                var orderDetail = new OrderDetail
                {
                    ColorId   = detail.ColorId,
                    ProductId = detail.ProductId,
                    Quantity  = detail.Quantity,
                    Price     = (double)product.Price,
                };

                ApplyUserCreateEntity(orderDetail);

                order.OrderDetails.Add(orderDetail);
            }

            order.TotalAmount = total;
            order.TotalPrice  = total - order.Discount;

            order.OrderCode = DateTime.Now.ToString("yyyyMMddHHmmss");

            response.Result = await _orderService.Insert(order);

            //response.Messages.Add(response.Result ? SuccessMessage : FailMessage);

            if (response.Result == true)
            {
                response.Messages.Add(SuccessMessage);

                // Send Email
                var subject  = "Đặt hàng thành công";
                var bodyHtml = await _viewRenderService.RenderToStringAsync <Order>("EmailTemplates/OrderEmailTemplate", order);

                var alias = "";
                await _emailService.Send(subject, bodyHtml, alias, new List <string>() { order.Email });

                if (voucher != null)
                {
                    voucher.IsUsed = 1;
                    await _voucherService.Update(voucher);
                }
            }
            else
            {
                response.Messages.Add(FailMessage);
            }

            if (response.Result)
            {
                response.OrderId   = order.OrderId;
                response.OrderCode = order.OrderCode;
            }

            return(response);
        }