Пример #1
0
        public async Task <IActionResult> ExportOrder(string orderId, ExportOrderDto orderDto)
        {
            var accountId = User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier).Value;
            var result    = await _stockService.ExportOrderAsync(accountId, orderId, orderDto);

            return(StatusCode((int)result.Code, result));
        }
Пример #2
0
        public async Task <ApiResult <string> > ExportOrderAsync(string accountId, string orderId, ExportOrderDto orderDto)
        {
            try
            {
                var checkOrder = await _context.Orders.FindAsync(orderId);

                if (checkOrder == null)
                {
                    return(new ApiResult <string>(HttpStatusCode.NotFound, ($"Không tìm thấy đơn hàng có mã: {orderId}")));
                }
                var checkEmployee = await _context.Employees.Where(x => x.AppuserId.ToString() == accountId)
                                    .SingleOrDefaultAsync();

                if (checkEmployee == null)
                {
                    return(new ApiResult <string>(HttpStatusCode.NotFound, $"Lỗi tài khoản đăng nhập"));
                }
                if (checkOrder.BranchId == checkEmployee.BranchId)
                {
                    var goodsDeliveryNote = ObjectMapper.Mapper.Map <GoodsDeliveryNote>(orderDto);
                    var sequenceNumber    = await _context.GoodsDeliveryNotes.CountAsync();

                    var goodsDeliveryNoteId = IdentifyGenerator.GenerateGoodsDeliveryNoteId(sequenceNumber + 1);
                    goodsDeliveryNote.Id         = goodsDeliveryNoteId;
                    goodsDeliveryNote.OrderId    = orderId;
                    goodsDeliveryNote.EmployeeId = checkEmployee.Id;
                    var goodsDeliveryNoteDetails = await(from od in _context.OrderDetails
                                                         where od.OrderId == checkOrder.Id && od.ProductId != null
                                                         select new GoodsDeliveryNoteDetail()
                    {
                        Id = Guid.NewGuid().ToString("D"),
                        GoodsDeliveryNoteId = goodsDeliveryNoteId,
                        ProductId           = od.ProductId,
                        Quantity            = od.Quantity,
                        UnitPrice           = od.UnitPrice
                    }).ToListAsync();
                    foreach (var g in goodsDeliveryNoteDetails)
                    {
                        var prod = await _context.Stocks.Where(x => x.ProductId == g.ProductId && x.WarehouseId == orderDto.WarehouseId).
                                   SingleOrDefaultAsync();

                        if (prod == null)
                        {
                            return(new ApiResult <string>(HttpStatusCode.NotFound, $"Sản phẩm không có trong kho"));
                        }
                        prod.RealQuantity -= g.Quantity;
                        prod.AbleToSale   -= g.Quantity;
                        if (checkOrder.PaymentStatusId == GlobalProperties.PaidPaymentId)
                        {
                            checkOrder.TransactionStatusId = GlobalProperties.FinishedTransactionId;
                        }
                        else
                        {
                            checkOrder.TransactionStatusId = GlobalProperties.TradingTransactionId;
                        }
                    }
                    goodsDeliveryNote.GoodsDeliveryNoteDetails = goodsDeliveryNoteDetails;
                    await _context.GoodsDeliveryNotes.AddAsync(goodsDeliveryNote);

                    await _context.SaveChangesAsync();

                    return(new ApiResult <string>(HttpStatusCode.OK)
                    {
                        ResultObj = goodsDeliveryNoteId, Message = "Xuất kho cho đơn hàng thành công"
                    });
                }
                return(new ApiResult <string>(HttpStatusCode.Forbidden, ($"Tài khoản hiện tại không được phép tạo phiếu xuất kho cho chi nhánh này")));
            }
            catch
            {
                return(new ApiResult <string>(HttpStatusCode.InternalServerError, "Có lỗi khi tạo phiếu xuất kho"));
            }
        }