private void CheckVoucherUsed(string VoucherCode)
        {
            IVoucherService voucherService = DependencyUtils.Resolve <IVoucherService>();

            if (!string.IsNullOrEmpty(VoucherCode))
            {
                var voucher = voucherService.GetVoucherIsNotUsedAndCode(VoucherCode);
                if (voucher.MembershipCardId == null)
                {
                    voucher.UsedQuantity++;
                    if (voucher.UsedQuantity >= voucher.Quantity)
                    {
                        voucher.isUsed = true;
                    }
                    var voucherVM = new VoucherAPIViewModel(voucher);
                    voucher = voucherVM.ToEntity();
                    voucherService.Update(voucher);
                }
                else
                {
                    voucher.isUsed = true;
                    var voucherVM = new VoucherAPIViewModel(voucher);
                    voucher = voucherVM.ToEntity();
                    voucherService.Update(voucher);
                }
            }
        }
        public ActionResult PutVoucher(int id, VoucherUpdateRequest entity)
        {
            if (id != entity.Id)
            {
                return(BadRequest());
            }
            bool success = _voucherSer.Update(entity);

            if (success)
            {
                var updated = _voucherSer.GetById(entity.Id);
                return(Ok(updated));
            }
            return(Problem("Update failed!"));
        }
        private async Task <ResponseViewModel> CreateVoucher(Voucher voucher)
        {
            var response = new ResponseViewModel
            {
                Result = true,
            };

            response.Result = voucher.VoucherId <= 0 ? await _voucherService.Insert(voucher) : await _voucherService.Update(voucher);

            if (response.Result == false)
            {
                response.Messages.Add($"Thao tác không thành công");
            }
            else
            {
                response.RefObjectId = voucher.VoucherId;

                // send email

                var account = await _accountService.GetById(voucher.AccountId);

                voucher.Account = account;
                var subject  = "Bạn có 1 Voucher";
                var bodyHtml = await _viewRenderService.RenderToStringAsync <Voucher>("EmailTemplates/VoucherEmailTemplate", voucher);

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

            return(response);
        }
        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);
        }