예제 #1
0
        /// <summary>
        /// Высчитывает все недосданные и пересданные водителем номенклатуры и записывает их в <see cref="DriverDiscrepancyDocument"/>
        /// </summary>
        private void CreateOrUpdateDiscrepancyDocument(IUnitOfWork uow, RouteList routeList, Employee employeeForCurrentUser, IList <DeliveryDocument> deliveryDocuments)
        {
            var terminalNomenclatureId = terminalNomenclatureProvider.GetNomenclatureIdForTerminal;

            DriverDiscrepancyDocument discrepancyDocument = uow.Session.QueryOver <DriverDiscrepancyDocument>()
                                                            .Where(x => x.RouteList.Id == routeList.Id).Take(1).SingleOrDefault() ?? new DriverDiscrepancyDocument();

            if (discrepancyDocument.Author == null)
            {
                discrepancyDocument.Author = employeeForCurrentUser;
            }

            if (discrepancyDocument.TimeStamp == default(DateTime))
            {
                discrepancyDocument.TimeStamp = DateTime.Now;
            }

            discrepancyDocument.LastEditor     = employeeForCurrentUser;
            discrepancyDocument.LastEditedTime = DateTime.Now;
            discrepancyDocument.RouteList      = routeList;

            foreach (DriverDiscrepancyDocumentItem item in discrepancyDocument.ObservableItems)
            {
                uow.Delete(item);
            }
            discrepancyDocument.ObservableItems.Clear();

            var loaded = routeListRepository.AllGoodsLoaded(uow, routeList)
                         .ToDictionary(x => x.NomenclatureId, x => x.Amount);

            var transferedFromThisRL = routeListRepository.AllGoodsTransferredFrom(uow, routeList)
                                       .ToDictionary(x => x.NomenclatureId, x => x.Amount);

            var transferedToThisRL = routeListRepository.AllGoodsTransferredTo(uow, routeList)
                                     .ToDictionary(x => x.NomenclatureId, x => x.Amount);

            var delivered = routeListRepository.AllGoodsDelivered(deliveryDocuments)
                            .ToDictionary(x => x.NomenclatureId, x => x.Amount);

            var receivedFromClient = routeListRepository.AllGoodsReceivedFromClient(deliveryDocuments)
                                     .ToDictionary(x => x.NomenclatureId, x => x.Amount);

            var unloaded = routeListRepository.GetReturnsToWarehouse(uow, routeList.Id)
                           .ToDictionary(x => x.NomenclatureId, x => x.Amount);

            var nomenclatureIds =
                loaded.Keys
                .Union(transferedFromThisRL.Keys)
                .Union(transferedToThisRL.Keys)
                .Union(delivered.Keys)
                .Union(receivedFromClient.Keys)
                .Union(unloaded.Keys)
                .Where(x => x != terminalNomenclatureId);

            foreach (var nomId in nomenclatureIds)
            {
                loaded.TryGetValue(nomId, out decimal loadedAmount);
                transferedFromThisRL.TryGetValue(nomId, out decimal transferedFromThisRLAmount);
                transferedToThisRL.TryGetValue(nomId, out decimal transferedToThisAmount);
                delivered.TryGetValue(nomId, out decimal deliveredAmount);
                receivedFromClient.TryGetValue(nomId, out decimal receivedFromClientAmount);
                unloaded.TryGetValue(nomId, out decimal unloadedAmount);

                var discrepancyValue = loadedAmount - transferedFromThisRLAmount + transferedToThisAmount - deliveredAmount + receivedFromClientAmount - unloadedAmount;
                if (discrepancyValue == 0)
                {
                    continue;
                }

                var newDiscrepanceItem = new DriverDiscrepancyDocumentItem {
                    Document          = discrepancyDocument,
                    Nomenclature      = uow.GetById <Nomenclature>(nomId),
                    DiscrepancyReason = discrepancyValue > 0 ? DiscrepancyReason.UnloadedDeficiently : DiscrepancyReason.UnloadedExcessively,
                    Amount            = Math.Abs(discrepancyValue)
                };
                newDiscrepanceItem.CreateOrUpdateOperations();
                discrepancyDocument.ObservableItems.Add(newDiscrepanceItem);
            }

            if (discrepancyDocument.ObservableItems.Any())
            {
                uow.Save(discrepancyDocument);
            }
        }