Exemplo n.º 1
0
        public BaseViewModel <string> DeletePaymentMethod(Guid id)
        {
            //Find PaymentMethod
            var userId        = new Guid(_repository.GetCurrentUserId());
            var paymentMethod = _repository.GetMany(_ => _.IsDelete == false && _.UserId == userId && _.Id == id).FirstOrDefault();
            //result to return
            BaseViewModel <string> result = null;

            //check PaymentMethod exist
            if (paymentMethod == null)
            {
                result = new BaseViewModel <string>()
                {
                    StatusCode  = HttpStatusCode.NotFound,
                    Code        = ErrMessageConstants.NOTFOUND,
                    Description = MessageHandler.CustomErrMessage(ErrMessageConstants.NOTFOUND)
                };
            }
            else
            {
                //update column isDelete = true
                paymentMethod.IsDelete = true;
                _repository.Update(paymentMethod);
                result = new BaseViewModel <string>();
                //save change
                Save();
            }
            return(result);
        }
        public ActionResult Edit(int id)
        {
            if (id > 0)
            {
                var order = _orderRepository.GetMany(x => !x.IsDeleted && x.Id == id)
                            .Include(x => x.OrderDetails.Select(c => c.Product.Category))
                            .FirstOrDefault();
                if (order != null)
                {
                    var toReturn = Mapper.Map <Order, OrderAdminModel>(order);

                    var orderstatues   = _orderStatusRepository.GetMany(x => !x.IsDeleted).Select(x => new { Id = x.Id, Name = x.Name }).ToList();
                    var paymentMethods = _paymentMethodRepository.GetMany(x => !x.IsDeleted).Select(x => new { Id = x.Id, Name = x.Name }).ToList();

                    ViewBag.Orderstatues   = orderstatues;
                    ViewBag.PaymentMethods = paymentMethods;

                    return(View(toReturn));
                }
                else
                {
                    return(null);
                }
            }
            return(null);
        }
Exemplo n.º 3
0
        // GET: Cart
        public ActionResult Index()
        {
            var cart = (List <CartModel>)Session["MyCart"];

            if (cart == null)
            {
                cart = new List <CartModel>();
            }
            var provinces = _provinceRepository.GetMany(x => !x.IsDeleted)
                            .OrderBy(x => x.DisplayOrder)
                            .ToList();

            var paymentMethods = _paymentMethodRepository.GetMany(x => !x.IsDeleted && x.IsActive)
                                 .OrderBy(x => x.DisplayOrder)
                                 .ToList();

            ViewBag.Provinces      = provinces;
            ViewBag.PaymentMethods = paymentMethods;

            return(View(cart));
        }
Exemplo n.º 4
0
        public BaseViewModel <OrderViewModel> CreateOrder(CreateOrderRequestViewModel order)
        {
            decimal totalPrice      = 0;
            int     totalQuantity   = 0;
            var     listOrderDetail = new HashSet <OrderDetail>();
            var     username        = _orderRepository.GetUsername();
            var     orderEntity     = _mapper.Map <Order>(order);

            orderEntity.SetDefaultInsertValue(_orderRepository.GetUsername());
            if (order.PaymentId != null)
            {
                var paymentMethod = _paymentMethodRepository.GetMany(_ => _.IsDelete == false && _.Id.Equals(new Guid(order.PaymentId)));
                if (paymentMethod == null)
                {
                    return(new BaseViewModel <OrderViewModel>()
                    {
                        StatusCode = HttpStatusCode.BadRequest,
                        Code = ErrMessageConstants.PAYMENT_METHOD_NOT_FOUND,
                        Description = MessageHandler.CustomErrMessage(ErrMessageConstants.PAYMENT_METHOD_NOT_FOUND),
                    });
                }
            }
            else
            {
                orderEntity.IsCash = true;
            }
            var location = _locationRepository.GetMany(_ => _.IsDelete == false && _.Id.Equals(new Guid(order.LocationId)));

            if (location == null)
            {
                return(new BaseViewModel <OrderViewModel>()
                {
                    StatusCode = HttpStatusCode.BadRequest,
                    Code = ErrMessageConstants.LOCATION_NOT_FOUND,
                    Description = MessageHandler.CustomErrMessage(ErrMessageConstants.LOCATION_NOT_FOUND),
                });
            }

            foreach (var product in order.Products)
            {
                var productId     = new Guid(product.Id);
                var productEntity = _productRepository.GetMany(_ => _.IsDelete == false && _.Id.Equals(productId)).FirstOrDefault();

                if (productEntity == null)
                {
                    return(new BaseViewModel <OrderViewModel>()
                    {
                        StatusCode = HttpStatusCode.BadRequest,
                        Code = ErrMessageConstants.PRODUCT_NOT_FOUND,
                        Description = MessageHandler.CustomErrMessage(ErrMessageConstants.PRODUCT_NOT_FOUND),
                    });
                }
                if (productEntity?.IsSale ?? false)
                {
                    if (productEntity?.PriceSale != product?.Price)
                    {
                        return(new BaseViewModel <OrderViewModel>()
                        {
                            StatusCode = HttpStatusCode.BadRequest,
                            Code = ErrMessageConstants.PRODUCT_PRICE_NOT_FOUND,
                            Description = MessageHandler.CustomErrMessage(ErrMessageConstants.PRODUCT_PRICE_NOT_FOUND),
                        });
                    }
                }
                else
                {
                    if (productEntity?.Price != product?.Price)
                    {
                        return(new BaseViewModel <OrderViewModel>()
                        {
                            StatusCode = HttpStatusCode.BadRequest,
                            Code = ErrMessageConstants.PRODUCT_PRICE_NOT_FOUND,
                            Description = MessageHandler.CustomErrMessage(ErrMessageConstants.PRODUCT_PRICE_NOT_FOUND),
                        });
                    }
                }
                if (productEntity.Quantity < product.Quantity)
                {
                    return(new BaseViewModel <OrderViewModel>()
                    {
                        StatusCode = HttpStatusCode.BadRequest,
                        Code = ErrMessageConstants.OUT_OF_STOCK,
                        Description = MessageHandler.CustomErrMessage(ErrMessageConstants.OUT_OF_STOCK),
                    });
                }
                productEntity.Quantity = productEntity.Quantity - product.Quantity;
                _productRepository.Update(productEntity);

                var orderDetail      = _mapper.Map <OrderDetail>(product);
                var totalDetailPrice = product.Price * product.Quantity;
                orderDetail.SetDefaultInsertValue(username);
                orderDetail.ProductId  = productId;
                orderDetail.TotalPrice = totalDetailPrice;
                orderDetail.TotalPrice = totalDetailPrice;
                orderDetail.OrderId    = orderEntity.Id;

                totalPrice    += product.Price * product.Quantity;
                totalQuantity += product.Quantity;
                listOrderDetail.Add(orderDetail);
            }



            orderEntity.TotalPrice    = totalPrice;
            orderEntity.TotalQuantity = totalQuantity;
            orderEntity.CreatedUserId = new Guid(_orderRepository.GetCurrentUserId());
            // orderEntity.OrderDetail = listOrderDetail;

            _orderRepository.Add(orderEntity);
            _orderDetailRepository.Add(listOrderDetail);


            var result = new BaseViewModel <OrderViewModel>()
            {
                Data = _mapper.Map <OrderViewModel>(orderEntity),
            };

            Save();

            return(result);
        }