public AdditionalLoadingDocument CreateAdditionLoadingDocument(IUnitOfWork uow, RouteList routeList) { if (uow == null) { throw new ArgumentNullException(nameof(uow)); } if (routeList == null) { throw new ArgumentNullException(nameof(routeList)); } if (routeList.AdditionalLoadingDocument != null) { throw new InvalidOperationException("Может быть создан только один документ запаса"); } var availableWeight = routeList.Car.CarModel.MaxWeight - routeList.GetTotalWeight(); var availableVolume = routeList.Car.CarModel.MaxVolume - routeList.GetTotalVolume(); if (availableWeight < 0 || availableVolume < 0) { return(null); } var document = new AdditionalLoadingDocument { Author = _employeeRepository.GetEmployeeForCurrentUser(uow), CreationDate = DateTime.Now, }; foreach (var item in CreateAdditionalLoadingItems(uow, availableWeight, availableVolume)) { item.AdditionalLoadingDocument = document; document.Items.Add(item); } return(document.Items.Any() ? document : null); }