public static void CreateCorrectiveDocument(XElement source, CommercialDocument destination) { Guid sourceDocumentId = new Guid(source.Element("correctedDocumentId").Value); DocumentMapper mapper = DependencyContainerManager.Container.Get <DocumentMapper>(); ICollection <Guid> previousDocumentsId = mapper.GetCommercialCorrectiveDocumentsId(sourceDocumentId); CommercialDocument sourceDocument = (CommercialDocument)mapper.LoadBusinessObject(BusinessObjectType.CommercialDocument, sourceDocumentId); CommercialDocument lastDoc = sourceDocument; foreach (Guid corrId in previousDocumentsId) { CommercialDocument correctiveDoc = (CommercialDocument)mapper.LoadBusinessObject(BusinessObjectType.CommercialDocument, corrId); CommercialCorrectiveDocumentFactory.RelateTwoCorrectiveDocuments(lastDoc, correctiveDoc, true); lastDoc = correctiveDoc; } CommercialCorrectiveDocumentFactory.CalculateDocumentsAfterCorrection(lastDoc); CommercialCorrectiveDocumentFactory.CreateNextCorrectiveDocument(lastDoc, destination); DuplicableAttributeFactory.DuplicateAttributes(lastDoc, destination); var salesOrderRelation = sourceDocument.Relations.Where(r => r.RelationType == DocumentRelationType.SalesOrderToInvoice).FirstOrDefault(); if (salesOrderRelation != null) //dokument jest do ZS wiec korekte tez tam podpinamy { var relation = destination.Relations.CreateNew(); relation.RelationType = DocumentRelationType.SalesOrderToCorrectiveCommercialDocument; relation.RelatedDocument = salesOrderRelation.RelatedDocument; } }