public async Task <List <ProductStockListDto> > SaveAsync(StockBillEditDto input) { var item = input.MapTo <EntryBill>(); item.Entries.ForEach(r => { r.EntryBillId = item.Id; r.Id = Guid.NewGuid(); }); item.Code = this.GetMaxCode(); await this.entryBillRepository.InsertAsync(item); // 更新库存信息 foreach (var entry in input.Items) { var stock = new Stock() { Amount = entry.Amount , ProductId = entry.ProductId , StorageId = entry.StorageId , Price = entry.Price }; await this.stockRepository.InsertAsync(stock); } this.CurrentUnitOfWork.SaveChanges(); return(this.entryRepository.GetAllIncluding(r => r.Product, r => r.Storage) .Where(r => r.EntryBillId == item.Id).ToList().MapTo <List <ProductStockListDto> >()); }
public async Task <List <ProductStockListDto> > SaveAsync(StockBillEditDto input) { var item = input.MapTo <DeliveryBill>(); item.Deliveries = new List <Delivery>(); // 更新库存信息 foreach (var entry in input.Items) { var stocks = this._stockRepository.Get( r => r.ProductId == entry.ProductId && r.StorageId == entry.StorageId) .OrderBy(r => r.CreationTime) .ToList(); //获取所有行库存量 var totalAmount = entry.Amount; foreach (var stock in stocks) { if (stock.Amount > totalAmount) // 如果当前行库存量>出库量,则当前库存量=当前原库存量-出库量 { stock.Amount -= totalAmount; this._stockRepository.Update(stock); item.Deliveries.Add( new Delivery() { Amount = totalAmount, ProductId = entry.ProductId, DeliveryBillId = item.Id, Note = entry.Note, Price = stock.Price, StorageId = stock.StorageId }); totalAmount = 0; break; } else if (stock.Amount == totalAmount) // 如果当前行库存量==出库量,则清除当前行库存 { totalAmount = 0; this._stockRepository.Delete(stock); item.Deliveries.Add( new Delivery() { Amount = totalAmount, ProductId = entry.ProductId, DeliveryBillId = item.Id, Note = entry.Note, Price = stock.Price, StorageId = stock.StorageId }); break; } else { totalAmount -= stock.Amount; item.Deliveries.Add( new Delivery() { Amount = stock.Amount, ProductId = entry.ProductId, DeliveryBillId = item.Id, Note = entry.Note, Price = stock.Price, StorageId = stock.StorageId }); this._stockRepository.Delete(stock); } } if (totalAmount > 0) { throw new UserFriendlyException($"商品[{entry.Name}]出库数量大于库存量,出库失败"); } } item.Code = this.GetMaxCode(); await this._deliveryBillRepository.InsertAsync(item); this.CurrentUnitOfWork.SaveChanges(); return(this._deliveryRepository.GetAllIncluding(r => r.Product, r => r.Storage).Where(r => r.DeliveryBillId == item.Id).ToList().MapTo <List <ProductStockListDto> >()); }