/// <summary> /// Forwards the communication package in headquarter branch. /// </summary> /// <param name="communicationPackage">The communication package.</param> /// <param name="repository">The communication package repository.</param> private void ForwardInHeadquarter(ICommunicationPackage communicationPackage, ICommunicationPackageRepository repository) { XDocument commPkg = XDocument.Parse(communicationPackage.XmlData.Content); var skipPackage = XDocument.Parse(communicationPackage.XmlData.Content).Root.Attribute("skipPackage"); if (skipPackage != null && skipPackage.Value.Equals("true", StringComparison.OrdinalIgnoreCase)) { return; } CommunicationPackageType pkgType = GetPackageType(communicationPackage); if (pkgType == CommunicationPackageType.Configuration || pkgType == CommunicationPackageType.ContractorSnapshot || pkgType == CommunicationPackageType.ContractorRelations || pkgType == CommunicationPackageType.ContractorGroupMembership || pkgType == CommunicationPackageType.ItemSnapshot || pkgType == CommunicationPackageType.ItemRelation || pkgType == CommunicationPackageType.ItemGroupMembership || pkgType == CommunicationPackageType.ItemUnitRelation || pkgType == CommunicationPackageType.WarehouseStock || pkgType == CommunicationPackageType.Payment || //pkgType == CommunicationPackageType.PaymentSettlementSnapshot || pkgType == CommunicationPackageType.FileDescriptor || pkgType == CommunicationPackageType.DictionaryPackage || pkgType == CommunicationPackageType.PriceRule || pkgType == CommunicationPackageType.PriceRuleList || pkgType == CommunicationPackageType.Custom) { IEnumerable <Guid> destinationDatabases = GetDestinations(communicationPackage, repository.Context.ConnectionManager); if (destinationDatabases != null) { repository.PutToOutgoingQueue(communicationPackage, destinationDatabases); } } else if (pkgType == CommunicationPackageType.WarehouseDocumentSnapshot) { if (commPkg.Root.Element("warehouseDocumentLine").Elements("entry").All(row => row.Element("isDistributed").Value.Equals("1"))) { ForwardShiftDocument(communicationPackage, repository, commPkg); } } else if (pkgType == CommunicationPackageType.WarehouseDocumentValuation) { // zapobiega zapetleniu paczek if (communicationPackage.DatabaseId != this.currentDatabaseId) { ForwardValuations(communicationPackage, commPkg, repository); } } }
private static void ForwardShiftDocument(ICommunicationPackage communicationPackage, ICommunicationPackageRepository repository, XDocument commPkg) { using (var wrapper = repository.Context.ConnectionManager.SynchronizeConnection()) { SqlConnectionManager.Instance.SetConnection(wrapper.Connection, (SqlTransaction)repository.Context.Transaction); } DocumentType docType = DictionaryMapper.Instance.GetDocumentType(new Guid( commPkg.Root.Element("warehouseDocumentHeader") .Element("entry").Element("documentTypeId").Value)); if (docType.WarehouseDocumentOptions.WarehouseDirection != Makolab.Fractus.Kernel.Enums.WarehouseDirection.OutcomeShift || docType.DocumentCategory == DocumentCategory.IncomeWarehouseCorrection || docType.DocumentCategory == DocumentCategory.OutcomeWarehouseCorrection) { return; } string oppositeWarehouseFieldId = DictionaryMapper.Instance.GetDocumentField(Makolab.Fractus.Kernel.Enums.DocumentFieldName.ShiftDocumentAttribute_OppositeWarehouseId).Id.Value.ToString().ToUpperInvariant(); string oppositeWarehouseId = commPkg.Root.Element("documentAttrValue").Elements() .Where(row => row.Element("documentFieldId").Value.Equals(oppositeWarehouseFieldId)) .Select(row => row.Element("textValue").Value) .SingleOrDefault(); if (oppositeWarehouseId == null) { throw new InvalidDataException("Missing opposite warehouse id in document attributes"); } Warehouse oppositeWarehouse = DictionaryMapper.Instance.GetWarehouse(new Guid(oppositeWarehouseId)); Warehouse warehouse = DictionaryMapper.Instance.GetWarehouse(new Guid(commPkg.Root.Element("warehouseDocumentHeader").Element("entry").Element("warehouseId").Value)); //skip local shift documents if (warehouse.BranchId != oppositeWarehouse.BranchId) { communicationPackage.DatabaseId = DictionaryMapper.Instance.GetBranch(oppositeWarehouse.BranchId).DatabaseId; repository.PutToOutgoingQueue(communicationPackage); } }
private static void ForwardValuations(ICommunicationPackage communicationPackage, XDocument commPkg, ICommunicationPackageRepository repository) { var distributedLines = commPkg.Root.Element("warehouseDocumentValuation").Elements("entry") .Where( row => row.Element("isDistributed") != null && row.Element("isDistributed").Value.Equals("True", StringComparison.OrdinalIgnoreCase) && row.Element("warehouseDocumentHeaderId") != null && (row.Attribute("action") == null || row.Attribute("action").Value.Equals("delete", StringComparison.OrdinalIgnoreCase) == false)) .GroupBy(row => row.Element("warehouseDocumentHeaderId").Value); //this valuation is not for shift document, so we don't forward it if (distributedLines.Count() == 0) { return; } XDocument valuationTemplate = XDocument.Parse("<root><warehouseDocumentValuation /></root>"); Guid localTransactionId = Guid.NewGuid(); using (var wrapper = repository.Context.ConnectionManager.SynchronizeConnection()) { SqlConnectionManager.Instance.SetConnection(wrapper.Connection, (SqlTransaction)repository.Context.Transaction); } using (var whDocCoord = new DocumentCoordinator(false, false)) { foreach (var warehouseDocGroup in distributedLines) { WarehouseDocument shift = null; try { shift = (WarehouseDocument)whDocCoord.LoadBusinessObject(Makolab.Fractus.Kernel.Enums.BusinessObjectType.WarehouseDocument, new Guid(warehouseDocGroup.Key)); } catch (ClientException) { } //there is a valuated document in database so we can forward valuation - no document = skip valuations for this doc if (shift != null) { string oppositeWarehouseId = (shift.Attributes.Count() > 0 && shift.Attributes.Children.Any(attr => attr.DocumentFieldName == DocumentFieldName.ShiftDocumentAttribute_OppositeWarehouseId)) ? shift.Attributes[DocumentFieldName.ShiftDocumentAttribute_OppositeWarehouseId].Value.Value : null; if (oppositeWarehouseId != null) { Warehouse oppositeWarehouse = DictionaryMapper.Instance.GetWarehouse(new Guid(oppositeWarehouseId)); Warehouse warehouse = DictionaryMapper.Instance.GetWarehouse(shift.WarehouseId); //skip local shift document valuations if (warehouse.BranchId == oppositeWarehouse.BranchId) { continue; } foreach (var valuation in warehouseDocGroup) { var whDocLine = shift.Lines.Children.Where(line => line.Id.ToString() .Equals(valuation.Element("outcomeWarehouseDocumentLineId").Value, StringComparison.OrdinalIgnoreCase)) .SingleOrDefault(); valuation.Add(new XAttribute("outcomeShiftOrdinalNumber", whDocLine.OrdinalNumber)); } var valuationPkg = new XDocument(valuationTemplate); valuationPkg.Root.Element("warehouseDocumentValuation").Add(new XAttribute("outcomeShiftId", shift.Id)); valuationPkg.Root.Element("warehouseDocumentValuation").Add(warehouseDocGroup); XmlTransferObject valuationData = new XmlTransferObject { DeferredTransactionId = communicationPackage.XmlData.DeferredTransactionId, Id = Guid.NewGuid(), LocalTransactionId = localTransactionId, XmlType = "WarehouseDocumentValuation", Content = valuationPkg.ToString(SaveOptions.DisableFormatting) }; ICommunicationPackage pkg = new CommunicationPackage(valuationData); pkg.DatabaseId = DictionaryMapper.Instance.GetBranch(oppositeWarehouse.BranchId).DatabaseId; repository.PutToOutgoingQueue(pkg); } } } } }