public async Task <ActionResult> CreateWithdrawal(CreateWithdrawalDto dto) { var created = await _withdrawalService.CreateAsync(dto); foreach (var items in dto.WithdrawalDetails) { var check = await _vanStockService.CheckIfExist(items.ProductId, dto.VanId); var check2 = await _stockService.CheckIfExist(items.ProductId); if (check2 != null) { check2.TotalPieces -= items.TotalPieces; check2.Amount -= items.Amount; await _stockService.UpdateAsync(check2); } if (check != null) { check.TotalPieces += items.TotalPieces; check.Amount += items.Amount; await _vanStockService.UpdateAsync(check); } else { var stocks = new VanStockDto { ProductId = items.ProductId, TotalPieces = items.TotalPieces, PricePerPiece = items.PricePerPiece, Amount = items.Amount, VanId = dto.VanId }; await _vanStockService.CreateAsync(stocks); } } return(Ok(created)); }
public async Task <ActionResult> EditWithdrawal(WithdrawalHeaderDto dto) { foreach (var dtoItem in dto.WithdrawalDetails) { var check = await _vanStockService.CheckIfExist(dtoItem.ProductId, dto.VanId); var check2 = await _stockService.CheckIfExist(dtoItem.ProductId); var old = await _withdrawalDetailsService.GetDetailsAsync(dtoItem.Id); if (old != null) { if (dtoItem.TotalPieces > old.TotalPieces) { var change = dtoItem.TotalPieces - old.TotalPieces; var tAmount = dtoItem.PricePerPiece * change; check.TotalPieces += change; check.Amount += tAmount; check2.TotalPieces -= change; check2.Amount -= tAmount; await _vanStockService.UpdateAsync(check); await _stockService.UpdateAsync(check2); } else if (dtoItem.TotalPieces < old.TotalPieces) { var change = old.TotalPieces - dtoItem.TotalPieces; var tAmount = dtoItem.PricePerPiece * change; check.TotalPieces -= change; check.Amount -= tAmount; check2.TotalPieces += change; check2.Amount += tAmount; await _vanStockService.UpdateAsync(check); await _stockService.UpdateAsync(check2); } } else { if (check != null) { check.TotalPieces += dtoItem.TotalPieces; check.Amount += dtoItem.Amount; check2.TotalPieces -= dtoItem.TotalPieces; check2.Amount -= dtoItem.Amount; await _stockService.UpdateAsync(check2); await _vanStockService.UpdateAsync(check); } else { check2.TotalPieces -= dtoItem.TotalPieces; check2.Amount -= dtoItem.Amount; await _stockService.UpdateAsync(check2); var stocks = new VanStockDto { ProductId = dtoItem.ProductId, TotalPieces = dtoItem.TotalPieces, PricePerPiece = dtoItem.PricePerPiece, Amount = dtoItem.Amount, VanId = dto.VanId }; await _vanStockService.CreateAsync(stocks); } } } var updated = await _withdrawalService.UpdateAsync(dto); foreach (var items in dto.WithdrawalDetails) { var getCheck = await _withdrawalDetailsService.GetAsync(new EntityDto <int>(items.Id)); if (getCheck == null) { var toCreate = new WithdrawalDetailDto { WithdrawalHeaderId = dto.WithdrawalId, ProductId = items.ProductId, Case = items.Case, ProdCase = items.ProdCase, Box = items.Box, ProdPiece = items.ProdPiece, Piece = items.Piece, Gross = items.Gross, Discount = items.Discount, Net = items.Net, TotalProductPrice = items.TotalProductPrice, }; await _withdrawalDetailsService.CreateAsync(toCreate); } else if (getCheck != null) { var toUpdate = new WithdrawalDetailDto { Id = items.Id, CreationTime = items.CreationTime, CreatorUserId = items.CreatorUserId, TenantId = items.TenantId, WithdrawalHeaderId = items.WithdrawalHeaderId, ProductId = items.ProductId, Case = items.Case, ProdCase = items.ProdCase, Box = items.Box, ProdPiece = items.ProdPiece, Piece = items.Piece, Gross = items.Gross, Discount = items.Discount, Net = items.Net, TotalProductPrice = items.TotalProductPrice, }; await _withdrawalDetailsService.UpdateAsync(toUpdate); } } return(Ok(updated)); }