public IEnumerable <GoodsInRouteListResult> AllGoodsDelivered(IUnitOfWork uow, RouteList routeList) { if (routeList == null) { throw new ArgumentNullException(nameof(routeList)); } IList <GoodsInRouteListResult> result = new List <GoodsInRouteListResult>(); if (!routeList.Addresses.Any()) { return(result); } DeliveryDocument docAlias = null; DeliveryDocumentItem docItemsAlias = null; GoodsInRouteListResult resultNodeAlias = null; result = uow.Session.QueryOver <DeliveryDocument>(() => docAlias) .Inner.JoinAlias(d => d.Items, () => docItemsAlias) .WhereRestrictionOn(d => d.RouteListItem.Id).IsIn(routeList.Addresses.Select(x => x.Id).ToArray()) .And(() => docItemsAlias.Direction == DeliveryDirection.ToClient) .SelectList(list => list .SelectGroup(() => docItemsAlias.Nomenclature.Id).WithAlias(() => resultNodeAlias.NomenclatureId) .SelectSum(() => docItemsAlias.Amount).WithAlias(() => resultNodeAlias.Amount) ).TransformUsing(Transformers.AliasToBean <GoodsInRouteListResult>()) .List <GoodsInRouteListResult>(); return(result); }
private void CreateOrUpdateDeliveryDocuments(IUnitOfWork uow, RouteList routeList, Employee employeeForCurrentUser, Nomenclature standartReturnNomenclature, ref IList <DeliveryDocument> deliveryDocuments) { foreach (var address in routeList.Addresses.Where(x => x.Status == RouteListItemStatus.Completed)) { var deliveryDocument = deliveryDocuments.FirstOrDefault(x => x.RouteListItem.Id == address.Id); if (deliveryDocument == null) { deliveryDocument = new DeliveryDocument(); deliveryDocuments.Add(deliveryDocument); } if (deliveryDocument.Author == null) { deliveryDocument.Author = employeeForCurrentUser; } if (deliveryDocument.TimeStamp == default(DateTime)) { deliveryDocument.TimeStamp = DateTime.Now; } deliveryDocument.LastEditor = employeeForCurrentUser; deliveryDocument.LastEditedTime = DateTime.Now; deliveryDocument.RouteListItem = address; foreach (DeliveryDocumentItem deliveryDocumentItem in deliveryDocument.ObservableItems) { uow.Delete(deliveryDocumentItem); } deliveryDocument.ObservableItems.Clear(); foreach (var orderItem in address.Order.OrderItems.Where(x => x.ActualCount.HasValue && x.ActualCount != 0)) { Debug.Assert(orderItem.ActualCount != null, "orderItem.ActualCount != null"); var newDeliveryDocumentItem = new DeliveryDocumentItem { Document = deliveryDocument, Amount = orderItem.ActualCount.Value, Nomenclature = orderItem.Nomenclature, Direction = DeliveryDirection.ToClient }; newDeliveryDocumentItem.CreateOrUpdateOperations(); deliveryDocument.ObservableItems.Add(newDeliveryDocumentItem); } foreach (var orderEquipment in address.Order.OrderEquipments.Where(x => x.ActualCount.HasValue && x.ActualCount != 0)) { Debug.Assert(orderEquipment.ActualCount != null, "orderEquipment.ActualCount != null"); var newDeliveryDocumentItem = new DeliveryDocumentItem { Document = deliveryDocument, Amount = (decimal)orderEquipment.ActualCount, Nomenclature = orderEquipment.Nomenclature, Direction = orderEquipment.Direction == Direction.Deliver ? DeliveryDirection.ToClient : DeliveryDirection.FromClient }; newDeliveryDocumentItem.CreateOrUpdateOperations(); deliveryDocument.ObservableItems.Add(newDeliveryDocumentItem); } if (address.BottlesReturned != 0) { var newDeliveryDocumentItem = new DeliveryDocumentItem { Document = deliveryDocument, Amount = address.BottlesReturned, Nomenclature = standartReturnNomenclature, Direction = DeliveryDirection.FromClient }; newDeliveryDocumentItem.CreateOrUpdateOperations(); deliveryDocument.ObservableItems.Add(newDeliveryDocumentItem); } uow.Save(deliveryDocument); } }