예제 #1
0
        public async Task <Result <List <OrderDetailResponse> > > Handle(
            BulkUpdateOrderDetailsCommand request,
            CancellationToken cancellationToken)
        {
            var order = await _context.Orders
                        .SingleOrDefaultAsync(x => x.Id == request.OrderId);

            if (order == null)
            {
                return(new Result <List <OrderDetailResponse> >(
                           new NotFoundException()
                           ));
            }

            /** Only can update products in order when it is New */
            if (order.Status != OrderStatus.New)
            {
                return(new Result <List <OrderDetailResponse> >(
                           new BadRequestException(
                               new ApiError("Chỉ được phép chỉnh sửa sản phẩm trong đơn hàng khi đang ở trạng thái 'Mới'"))
                           ));
            }

            /** Make the request order detail list valid */
            var productsInOrder = new List <ProductInOrder>();

            _mapper.Map <List <UpdateOrderDetailsCommand>, List <ProductInOrder> >(
                request.OrderDetails, productsInOrder);

            productsInOrder = await _orderServices.MakeSureUpdateProductsInOrderValid(order.Id, productsInOrder);

            /** Update new change to all updated purchase propposal details */
            var orderDetailIds = productsInOrder
                                 .Select(x => x.Id);
            var updatedOrderDetailEntities = await _context.OrderDetails
                                             .Where(x => orderDetailIds.Contains(x.Id))
                                             .ToListAsync();

            /** Remove the old price of all updated order detail first then update it
             * and add the new price in */
            order.TotalPrice -= updatedOrderDetailEntities.Sum(x => x.TotalPrice);

            foreach (var od in updatedOrderDetailEntities)
            {
                // Map new info
                var matchedProductInOrder = productsInOrder
                                            .SingleOrDefault(x => x.Id == od.Id);
                _mapper.Map <ProductInOrder, OrderDetail>(
                    matchedProductInOrder, od);

                od.QuantityNeed = od.Quantity;

                // Update price after update info
                od.TotalPrice = _priceCalculateHelpers.CalculateTotalPriceOfProduct(od.Quantity, od.SinglePrice);
            }

            order.TotalPrice += updatedOrderDetailEntities.Sum(x => x.TotalPrice);

            _context.OrderDetails.UpdateRange(updatedOrderDetailEntities);
            _context.Orders.Update(order);
            var created = await _context.SaveChangesAsync();

            if (created > 0)
            {
                return(new Result <List <OrderDetailResponse> >(
                           _mapper.Map <List <OrderDetailResponse> >(updatedOrderDetailEntities)
                           ));
            }

            return(new Result <List <OrderDetailResponse> >(
                       new BadRequestException(
                           new ApiError("Chỉnh sửa sản phẩm trong đơn hàng thất bại, xin thử lại"))
                       ));
        }
예제 #2
0
        public async Task <Result <List <OrderDetailResponse> > > Handle(
            BulkCreateOrderDetailsCommand request,
            CancellationToken cancellationToken)
        {
            var order = await _context.Orders
                        .SingleOrDefaultAsync(x => x.Id == request.OrderId);

            /** Make sure add product to valid order */
            if (order == null)
            {
                return(new Result <List <OrderDetailResponse> >(
                           new NotFoundException()
                           ));
            }
            if (order.Status != OrderStatus.New)
            {
                return(new Result <List <OrderDetailResponse> >(
                           new BadRequestException(
                               new ApiError("Chỉ được phép thêm sản phẩm vào đơn hàng khi đang ở trạng thái 'Mới'"))
                           ));
            }

            /** Validate valid new products of order */
            var productsInOrder = new List <ProductInOrder>();

            _mapper.Map <List <CreateOrderDetailsCommand>, List <ProductInOrder> >(
                request.OrderDetails, productsInOrder);

            productsInOrder = await _orderServices.MakeSureProductsValidWhenAddToOrder(
                productsInOrder);

            /** Prepare entities to save */
            List <OrderDetail> orderDetailEntities = _mapper.Map <List <OrderDetail> >(
                productsInOrder
                );

            foreach (var od in orderDetailEntities)
            {
                od.QuantityNeed = od.Quantity;
                od.OrderId      = order.Id;

                // Because user only give single price of each product in order then
                // we need to calculate total price of each product
                od.TotalPrice = _priceCalculateHelpers.CalculateTotalPriceOfProduct(
                    od.SinglePrice,
                    od.Quantity);
            }

            double newProductsTotalPrice = orderDetailEntities.Sum(x => x.TotalPrice);

            order.TotalPrice += newProductsTotalPrice;

            await _context.OrderDetails.AddRangeAsync(orderDetailEntities);

            _context.Orders.Update(order);
            var created = await _context.SaveChangesAsync();

            if (created > 0)
            {
                return(new Result <List <OrderDetailResponse> >(
                           _mapper.Map <List <OrderDetailResponse> >(orderDetailEntities)
                           ));
            }

            return(new Result <List <OrderDetailResponse> >(
                       new BadRequestException(
                           new ApiError("Thêm sản phẩm vào đơn hàng thất bại, xin thử lại"))
                       ));
        }
        public async Task <Result <List <GoodsDeliveryDetailResponse> > > Handle(
            BulkCreateGoodsDeliveryDetailsCommand request,
            CancellationToken cancellationToken)
        {
            var goodsDeliveryNote = await _context.GoodsDeliveryNotes
                                    .SingleOrDefaultAsync(x => x.Id == request.GoodsDeliveryNoteId);

            /** Make sure add product to valid goods delivery note */
            if (goodsDeliveryNote == null)
            {
                return(new Result <List <GoodsDeliveryDetailResponse> >(
                           new NotFoundException()
                           ));
            }
            if (goodsDeliveryNote.Status != GoodsDeliveryNoteStatus.New)
            {
                return(new Result <List <GoodsDeliveryDetailResponse> >(
                           new BadRequestException(
                               new ApiError("Chỉ được phép thêm sản phẩm vào phiếu xuất kho khi đang ở trạng thái 'Mới'"))
                           ));
            }

            /** Make sure new products valid */
            var orderDetails = await _context.OrderDetails
                               .Where(x => x.OrderId == goodsDeliveryNote.OrderId)
                               .ToListAsync();

            var productsInGoodsDeliveryNote = new List <ProductInGoodsDeliveryNote>();

            _mapper.Map <List <CreateGoodsDeliveryDetailsCommand>, List <ProductInGoodsDeliveryNote> >(
                request.GoodsDeliveryDetails, productsInGoodsDeliveryNote);

            productsInGoodsDeliveryNote = await _goodsDeliveryNoteServices
                                          .ValidateWhenAddNewProductToExistedGoodsDeliveryNote(
                goodsDeliveryNote.Id,
                orderDetails,
                productsInGoodsDeliveryNote);

            /** Prepare info for entities to save */
            List <GoodsDeliveryDetail> goodsDeliveryDetailEntties = _mapper.Map <List <GoodsDeliveryDetail> >(
                productsInGoodsDeliveryNote
                );

            foreach (var gdd in goodsDeliveryDetailEntties)
            {
                var matchedOrderDetail = orderDetails
                                         .SingleOrDefault(x => x.ProductId == gdd.ProductId);

                gdd.GoodsDeliveryNoteId = request.GoodsDeliveryNoteId;
                gdd.SinglePrice         = matchedOrderDetail.SinglePrice;
                gdd.TotalPrice          = _priceCalculateHelpers.CalculateTotalPriceOfProduct(
                    gdd.Quantity,
                    gdd.SinglePrice
                    );
            }

            goodsDeliveryNote.TotalPrice += goodsDeliveryDetailEntties
                                            .Sum(x => x.TotalPrice);

            await _context.GoodsDeliveryDetails.AddRangeAsync(goodsDeliveryDetailEntties);

            _context.GoodsDeliveryNotes.Update(goodsDeliveryNote);
            var created = await _context.SaveChangesAsync();

            if (created > 0)
            {
                return(new Result <List <GoodsDeliveryDetailResponse> >(
                           _mapper.Map <List <GoodsDeliveryDetailResponse> >(goodsDeliveryDetailEntties)
                           ));
            }

            return(new Result <List <GoodsDeliveryDetailResponse> >(
                       new BadRequestException(
                           new ApiError("Thêm sản phẩm vào phiếu xuất kho thất bại, xin thử lại"))
                       ));
        }
예제 #4
0
        public async Task <Result <List <GoodsDeliveryDetailResponse> > > Handle(
            BulkUpdateGoodsDeliveryDetailsCommand request,
            CancellationToken cancellationToken)
        {
            var goodsDeliveryNote = await _context.GoodsDeliveryNotes
                                    .SingleOrDefaultAsync(x => x.Id == request.GoodsDeliveryNoteId);

            /** Make sure update goods receiving detail in valid goods receiving note */
            if (goodsDeliveryNote == null)
            {
                return(new Result <List <GoodsDeliveryDetailResponse> >(
                           new NotFoundException()
                           ));
            }
            if (goodsDeliveryNote.Status != GoodsDeliveryNoteStatus.New)
            {
                return(new Result <List <GoodsDeliveryDetailResponse> >(
                           new BadRequestException(
                               new ApiError("Chỉ được phép chỉnh sửa sản phẩm trong phiếu xuất kho khi phiếu đang ở trạng thái 'Mới'"))
                           ));
            }


            var orderDetails = await _context.OrderDetails
                               .Where(x => x.OrderId == goodsDeliveryNote.OrderId)
                               .ToListAsync();

            var goodsDeliveryDetailIds = request.GoodsDeliveryDetails
                                         .Select(x => x.Id)
                                         .ToList();
            var existedGoodsDeliveryDetails = await _context.GoodsDeliveryDetails
                                              .Where(x => x.GoodsDeliveryNoteId == request.GoodsDeliveryNoteId &&
                                                     goodsDeliveryDetailIds.Contains(x.Id))
                                              .ToListAsync();

            // Remove the old to price to update the new price later
            goodsDeliveryNote.TotalPrice -= existedGoodsDeliveryDetails
                                            .Sum(x => x.TotalPrice);

            var productsInGoodsDeliveryNote = new List <ProductInGoodsDeliveryNote>();

            _mapper.Map <List <UpdateGoodsDeliveryDetailsCommand>, List <ProductInGoodsDeliveryNote> >(
                request.GoodsDeliveryDetails, productsInGoodsDeliveryNote);

            /** Because the request products do not contain productId so we need to attach
             * productId to each to prevent error missing productId when update to DB */
            foreach (var product in productsInGoodsDeliveryNote)
            {
                var matchedGoodsDeliveryDetail = existedGoodsDeliveryDetails.SingleOrDefault(
                    x => x.Id == product.Id);

                product.ProductId = matchedGoodsDeliveryDetail.ProductId;
            }

            /** Validate valid updated products */
            productsInGoodsDeliveryNote = _goodsDeliveryNoteServices
                                          .ValidateWhenUpdateProductsInGoodsDeliveryNote(
                existedGoodsDeliveryDetails,
                orderDetails,
                productsInGoodsDeliveryNote);

            foreach (var p in productsInGoodsDeliveryNote)
            {
                var matchedGoodsDeliveryDetail = existedGoodsDeliveryDetails
                                                 .SingleOrDefault(x => x.ProductId == p.ProductId);

                _mapper.Map <ProductInGoodsDeliveryNote, GoodsDeliveryDetail>(
                    p, matchedGoodsDeliveryDetail);
                matchedGoodsDeliveryDetail.TotalPrice = _priceCalculateHelpers
                                                        .CalculateTotalPriceOfProduct(
                    matchedGoodsDeliveryDetail.Quantity,
                    matchedGoodsDeliveryDetail.SinglePrice
                    );
            }

            goodsDeliveryNote.TotalPrice += existedGoodsDeliveryDetails
                                            .Sum(x => x.TotalPrice);

            _context.GoodsDeliveryDetails.UpdateRange(existedGoodsDeliveryDetails);
            _context.Update(goodsDeliveryNote);
            var updated = await _context.SaveChangesAsync();

            if (updated > 0)
            {
                return(new Result <List <GoodsDeliveryDetailResponse> >(
                           _mapper.Map <List <GoodsDeliveryDetailResponse> >(existedGoodsDeliveryDetails)
                           ));
            }

            return(new Result <List <GoodsDeliveryDetailResponse> >(
                       new BadRequestException(
                           new ApiError("Chỉnh sửa sản phẩm trong phiếu xuất kho thất bại, xin thử lại"))
                       ));
        }
예제 #5
0
        public async Task <Result <GoodsDeliveryNoteResponse> > Handle(
            CreateGoodsDeliveryNotesCommand request,
            CancellationToken cancellationToken)
        {
            var order = await _context.Orders
                        .Include(x => x.OrderDetails)
                        .SingleOrDefaultAsync(x => x.Id == request.OrderId);

            /** Make sure we are creating goods delivery note for a valid order */
            if (order == null)
            {
                throw new NotFoundException();
            }
            if (order.Status != OrderStatus.Processing)
            {
                throw new BadRequestException(new ApiError("Chỉ được xuất kho cho đơn hàng đang 'được xử lý'"));
            }

            var orderDetails = order.OrderDetails
                               .ToList();
            var productsInGoodsDeliveryNote = new List <ProductInGoodsDeliveryNote>();

            _mapper.Map <List <CreateGoodsDeliveryDetailsCommand>, List <ProductInGoodsDeliveryNote> >(
                request.GoodsDeliveryDetails, productsInGoodsDeliveryNote);

            /** Make sure all product in a new goods delivery note valid */
            productsInGoodsDeliveryNote = await _goodsDeliveryNoteServices.MakeSureProductsOfNewGoodsDeliveryNoteValid(
                productsInGoodsDeliveryNote, orderDetails);

            /** Prepare entity to save to DB */
            _mapper.Map <List <ProductInGoodsDeliveryNote>, List <CreateGoodsDeliveryDetailsCommand> >(
                productsInGoodsDeliveryNote, request.GoodsDeliveryDetails);

            var goodsDeliveryNoteEntity = _mapper.Map <GoodsDeliveryNote>(
                request);

            // Map price to goods delivery note and detail
            foreach (var goodsDeliveryDetail in goodsDeliveryNoteEntity.GoodsDeliveryDetails)
            {
                var matchedOrderDetail = orderDetails
                                         .SingleOrDefault(od => od.ProductId == goodsDeliveryDetail.ProductId);

                goodsDeliveryDetail.SinglePrice = matchedOrderDetail.SinglePrice;
                goodsDeliveryDetail.TotalPrice  = _priceCalculateHelpers
                                                  .CalculateTotalPriceOfProduct(
                    goodsDeliveryDetail.Quantity,
                    goodsDeliveryDetail.SinglePrice
                    );
            }

            goodsDeliveryNoteEntity.Status      = GoodsDeliveryNoteStatus.New;
            goodsDeliveryNoteEntity.TotalPrice += goodsDeliveryNoteEntity.GoodsDeliveryDetails
                                                  .Sum(x => x.TotalPrice);

            await _context.AddAsync(goodsDeliveryNoteEntity);

            var created = await _context.SaveChangesAsync();

            if (created > 0)
            {
                return(new Result <GoodsDeliveryNoteResponse>(
                           _mapper.Map <GoodsDeliveryNoteResponse>(goodsDeliveryNoteEntity)
                           ));
            }
            return(new Result <GoodsDeliveryNoteResponse>(
                       _mapper.Map <GoodsDeliveryNoteResponse>(
                           new BadRequestException(new ApiError("Tạo phiếu xuất kho xảy ra lỗi, xin thử lại"))
                           )
                       ));
        }