public async Task <Result <GoodsReceivingNoteResponse> > Handle( CreateGoodsReceivingNoteCommand request, CancellationToken cancellationToken) { var purchaseProposalForm = await _context.PurchaseProposalForms .Include(x => x.PurchaseProposalDetails) .SingleOrDefaultAsync(x => x.Id == request.PurchaseProposalFormId); /** Make sure purchase proposal form valid */ if (purchaseProposalForm == null) { throw new NotFoundException(); } if (purchaseProposalForm.Status != PurchaseProposalFormStatus.Processing) { throw new BadRequestException(new ApiError("Chỉ được nhập kho cho đề nghị mua hàng đang 'được xử lý'")); } var purchaseProposalDetails = purchaseProposalForm.PurchaseProposalDetails .ToList(); var productsInGoodsReceivingNote = new List <ProductInGoodsReceivingNote>(); /** Make sure all product in goods receiving note valid */ _mapper.Map <List <CreateGoodsReceivingDetailCommand>, List <ProductInGoodsReceivingNote> >( request.GoodsReceivingDetails, productsInGoodsReceivingNote); productsInGoodsReceivingNote = _goodsReceivingNoteServices.MakeSureProductsOfNewGoodsReceivingNoteSastifyProposal( productsInGoodsReceivingNote, purchaseProposalDetails); if (request.SupplierId != null) { // This mean we do import from a specific supplier var supplier = await _context.Suppliers .SingleOrDefaultAsync(x => x.Id == request.SupplierId); if (supplier == null) { throw new NotFoundException(); } await _goodsReceivingNoteServices.MakeSureNewProductMustBelongToSupplier( supplier.Id, productsInGoodsReceivingNote); request.SupplierName = supplier.Name; } /** Prepare entity to save to DB */ // Map list valid product back to command to map to entity _mapper.Map <List <ProductInGoodsReceivingNote>, List <CreateGoodsReceivingDetailCommand> >( productsInGoodsReceivingNote, request.GoodsReceivingDetails); var goodsReceivingNoteEntity = _mapper.Map <GoodsReceivingNote>( request); goodsReceivingNoteEntity.Status = GoodsReceivingNoteStatus.New; // Calculate total price of each goods receiving detail item goodsReceivingNoteEntity.GoodsReceivingDetails = _goodsReceivingNoteServices .CalculatePriceOfProducsInGoodsReceivingNote( goodsReceivingNoteEntity.GoodsReceivingDetails.ToList()); // After has total price of each goods receiving item then // calculate total price of this goods receiving note goodsReceivingNoteEntity.TotalPrice = goodsReceivingNoteEntity.GoodsReceivingDetails .Sum(x => x.TotalPrice); await _context.GoodsReceivingNotes.AddAsync(goodsReceivingNoteEntity); var created = await _context.SaveChangesAsync(); if (created > 0) { return(new Result <GoodsReceivingNoteResponse>( _mapper.Map <GoodsReceivingNoteResponse>(goodsReceivingNoteEntity) )); } return(new Result <GoodsReceivingNoteResponse>( _mapper.Map <GoodsReceivingNoteResponse>( new BadRequestException(new ApiError("Tạo phiếu nhập kho xảy ra lỗi, xin thử lại")) ) )); }
public async Task <Result <List <GoodsReceivingDetailResponse> > > Handle( BulkCreateGoodsReceivingDetailCommand request, CancellationToken cancellationToken) { var goodsReceivingNote = await _context.GoodsReceivingNotes .SingleOrDefaultAsync(x => x.Id == request.GoodsReceivingNoteId); /** Make sure add product to valid goods receiving note */ if (goodsReceivingNote == null) { return(new Result <List <GoodsReceivingDetailResponse> >( new NotFoundException() )); } if (goodsReceivingNote.Status != GoodsReceivingNoteStatus.New) { return(new Result <List <GoodsReceivingDetailResponse> >( new BadRequestException( new ApiError("Chỉ được phép thêm sản phẩm vào phiếu nhập kho khi đang ở trạng thái 'Mới'")) )); } /** Validate new product comparing to purchase proposal form */ var purchaseProposalDetails = await _context.PurchaseProposalDetails .Where(x => x.PurchaseProposalFormId == goodsReceivingNote.PurchaseProposalFormId) .ToListAsync(); var productsInGoodsReceivingNote = new List <ProductInGoodsReceivingNote>(); _mapper.Map <List <CreateGoodsReceivingDetailCommand>, List <ProductInGoodsReceivingNote> >( request.GoodsReceivingDetails, productsInGoodsReceivingNote); productsInGoodsReceivingNote = await _goodsReceivingNoteServices .ValidateWhenAddNewProductToExistedGoodsReceivingNote( goodsReceivingNote.Id, purchaseProposalDetails, productsInGoodsReceivingNote); if (goodsReceivingNote.SupplierId != null) { // if goods receiving note belong to supplier => all new products // must be belonged to that supplier too await _goodsReceivingNoteServices.MakeSureNewProductMustBelongToSupplier( goodsReceivingNote.SupplierId.GetValueOrDefault(), productsInGoodsReceivingNote); } _mapper.Map <List <ProductInGoodsReceivingNote>, List <CreateGoodsReceivingDetailCommand> >( productsInGoodsReceivingNote, request.GoodsReceivingDetails); /** Prepare info for entities to save */ List <GoodsReceivingDetail> goodsReceivingDetailEntities = _mapper.Map <List <GoodsReceivingDetail> >( request.GoodsReceivingDetails ); foreach (var grd in goodsReceivingDetailEntities) { grd.GoodsReceivingNoteId = request.GoodsReceivingNoteId; } // Calculate total price of each goods receiving detail item goodsReceivingDetailEntities = _goodsReceivingNoteServices.CalculatePriceOfProducsInGoodsReceivingNote( goodsReceivingDetailEntities); // After has total price of each goods receiving item then // calculate total price of this goods receiving note goodsReceivingNote.TotalPrice = goodsReceivingNote.TotalPrice + goodsReceivingDetailEntities .Sum(x => x.TotalPrice); await _context.GoodsReceivingDetails.AddRangeAsync(goodsReceivingDetailEntities); var created = await _context.SaveChangesAsync(); if (created > 0) { return(new Result <List <GoodsReceivingDetailResponse> >( _mapper.Map <List <GoodsReceivingDetailResponse> >(goodsReceivingDetailEntities) )); } return(new Result <List <GoodsReceivingDetailResponse> >( new BadRequestException( new ApiError("Thêm sản phẩm vào phiếu nhập kho thất bại, xin thử lại")) )); }