public async Task <IActionResult> ImportPurchaseOrder(string purchaseOrderId, ImportPurchaseOrderDto orderDto) { var accountId = User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier).Value; var result = await _stockService.ImportPurchaseOrderAsync(accountId, purchaseOrderId, orderDto); return(StatusCode((int)result.Code, result)); }
public async Task <ApiResult <string> > ImportPurchaseOrderAsync(string accountId, string purchaseOrderId, ImportPurchaseOrderDto orderDto) { var checkPurchaseOrder = await _context.PurchaseOrders.FindAsync(purchaseOrderId); if (checkPurchaseOrder == null) { return(new ApiResult <string>(HttpStatusCode.NotFound)); } 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 (checkEmployee.BranchId == checkPurchaseOrder.BrandId) { var sequencyNumber = await _context.GoodReceivedNotes.CountAsync(); var goodsReceivedId = IdentifyGenerator.GenerateGoodsReceivedNoteId(sequencyNumber + 1); var goodsReceivedNote = ObjectMapper.Mapper.Map <GoodsReceivedNote>(orderDto); var purchaseOrderDetails = await(from pod in _context.PurchaseOrderDetails where pod.PurchaseOrderId == purchaseOrderId select new GoodsReceivedNoteDetail() { Id = Guid.NewGuid().ToString("D"), GoodsReceivedNoteId = goodsReceivedId, ProductId = pod.ProductId, Quantity = pod.Quantity, UnitPrice = pod.UnitPrice }).ToListAsync(); goodsReceivedNote.Id = goodsReceivedId; goodsReceivedNote.EmployeeId = checkEmployee.Id; goodsReceivedNote.GoodsReceivedNoteDetails = purchaseOrderDetails; goodsReceivedNote.PurchaseOrderId = purchaseOrderId; foreach (var p in goodsReceivedNote.GoodsReceivedNoteDetails) { p.Id = Guid.NewGuid().ToString("D"); p.GoodsReceivedNoteId = goodsReceivedId; var productInStock = await _context.Stocks.Where(x => x.ProductId == p.ProductId && x.WarehouseId == orderDto.WarehouseId).SingleOrDefaultAsync(); if (productInStock == null) { await _context.Stocks.AddAsync(new Data.Entities.Stock() { AbleToSale = p.Quantity, ProductId = p.ProductId, RealQuantity = p.Quantity, WarehouseId = orderDto.WarehouseId }); } else { productInStock.RealQuantity += p.Quantity; productInStock.AbleToSale += p.Quantity; } } await _context.GoodReceivedNotes.AddAsync(goodsReceivedNote); checkPurchaseOrder.TransactionStatusId = GlobalProperties.TradingTransactionId; await _context.SaveChangesAsync(); return(new ApiResult <string>(HttpStatusCode.OK) { ResultObj = goodsReceivedId, Message = "Nhập kho cho phiếu nhập hàng thành công" }); } return(new ApiResult <string>(HttpStatusCode.BadRequest, $"Tài khoản hiện tại không được phép tạo phiếu nhập cho chi nhánh này")); }