/// <summary>
        /// Saves the business object.
        /// </summary>
        /// <param name="document"><see cref="WarehouseDocument"/> to save.</param>
        /// <returns>Xml containing result of oper</returns>
        public XDocument SaveBusinessObject(WarehouseDocument document)
        {
            DictionaryMapper.Instance.CheckForChanges();

            //correct the document that comes from client
            this.CorrectDocumentConsistency(document);

            this.ExecuteDocumentOptions(document);

            //validate
            document.Validate();

            //Walidacja zmian w dokumencie realizującym zamówienie sprzedażowe
            document.CheckDoesRealizeClosedSalesOrder(coordinator);

            //load alternate version
            if (!document.IsNew)
            {
                IBusinessObject alternateBusinessObject = this.mapper.LoadBusinessObject(document.BOType, document.Id.Value);
                document.SetAlternateVersion(alternateBusinessObject);
            }

            //update status
            document.UpdateStatus(true);

            if (document.AlternateVersion != null)
            {
                document.AlternateVersion.UpdateStatus(false);
            }

            SqlConnectionManager.Instance.BeginTransaction();

            try
            {
                DictionaryMapper.Instance.CheckForChanges();
                this.mapper.CheckBusinessObjectVersion(document);

                //logika dla automatycznego zamykania ZS w realizacji bezzaliczkowej
                this.coordinator.TryCloseSalesOrdersWhileRealization(document);

                this.coordinator.UpdateStock(document);

                this.ValidationDuringTransaction(document);

                if (document.DocumentStatus == DocumentStatus.Canceled && ConfigurationMapper.Instance.IsWmsEnabled)
                {
                    DependencyContainerManager.Container.Get <WarehouseMapper>().DeleteShiftsForDocument(document.Id.Value);
                }

                if (!document.IsNew && document.DocumentStatus == DocumentStatus.Committed)
                {
                    this.mapper.DeleteIncomeOutcomeRelations(document);
                }

                if (document.DocumentStatus == DocumentStatus.Committed)
                {
                    this.ProcessIncomeOutcome(document);
                }

                DocumentLogicHelper.AssignNumber(document, this.mapper);

                //Make operations list
                XDocument operations = XDocument.Parse("<root/>");

                document.SaveChanges(operations);

                if (document.AlternateVersion != null)
                {
                    document.AlternateVersion.SaveChanges(operations);
                }

                if (operations.Root.HasElements)
                {
                    // this.mapper.UpdateStockForCanceledDocument(document);
                    this.mapper.ExecuteOperations(operations);
                    this.mapper.ValuateOutcomeWarehouseDocument(document);
                    this.mapper.CreateCommunicationXml(document);
                    this.mapper.CreateCommunicationXmlForDocumentRelations(operations);
                    this.mapper.UpdateDictionaryIndex(document);
                }


                Coordinator.LogSaveBusinessObjectOperation();

                document.SaveRelatedObjects();

                operations = XDocument.Parse("<root/>");

                document.SaveRelations(operations);

                if (document.AlternateVersion != null)
                {
                    ((WarehouseDocument)document.AlternateVersion).SaveRelations(operations);
                }

                if (operations.Root.HasElements)
                {
                    this.mapper.ExecuteOperations(operations);
                    this.mapper.CreateCommunicationXmlForDocumentRelations(operations); //generowanie paczek dla relacji dokumentow
                }

                this.mapper.DeleteDocumentAccountingData(document);

                this.mapper.UpdateReservationAndOrderStock(document);

                WarehouseCoordinator.ProcessWarehouseManagamentSystem(document.ShiftTransaction);

                //Custom validation
                this.mapper.ExecuteOnCommitValidationCustomProcedure(document);
                Coordinator.LogSaveBusinessObjectOperation();

                if (document.DraftId != null)
                {
                    this.mapper.DeleteDraft(document.DraftId.Value);
                }

                XDocument returnXml = XDocument.Parse(String.Format(CultureInfo.InvariantCulture, "<root><id>{0}</id></root>", document.Id.ToUpperString()));

                if (this.coordinator.CanCommitTransaction)
                {
                    if (!ConfigurationMapper.Instance.ForceRollbackTransaction)
                    {
                        SqlConnectionManager.Instance.CommitTransaction();
                    }
                    else
                    {
                        SqlConnectionManager.Instance.RollbackTransaction();
                    }
                }

                return(returnXml);
            }
            catch (SqlException sqle)
            {
                RoboFramework.Tools.RandomLogHelper.GetLog().Debug("FractusRefactorTraceCatch:80");
                Coordinator.ProcessSqlException(sqle, document.BOType, this.coordinator.CanCommitTransaction);
                throw;
            }
            catch (Exception)
            {
                RoboFramework.Tools.RandomLogHelper.GetLog().Debug("FractusRefactorTraceCatch:81");
                if (this.coordinator.CanCommitTransaction)
                {
                    SqlConnectionManager.Instance.RollbackTransaction();
                }
                throw;
            }
        }
        public void ChangeDocumentStatus(Document document, DocumentStatus requestedStatus)
        {
            WarehouseDocument  warehouseDocument  = document as WarehouseDocument;
            FinancialDocument  financialDocument  = document as FinancialDocument;
            ServiceDocument    serviceDocument    = document as ServiceDocument;
            InventoryDocument  inventoryDocument  = document as InventoryDocument;
            ComplaintDocument  complaintDocument  = document as ComplaintDocument;
            CommercialDocument commercialDocument = document as CommercialDocument;

            DocumentStatus actualStatus = document.DocumentStatus;
            Guid           documentId   = document.Id.Value;

            bool useStandardCancelProcessing = false;

            #region MM+
            if (warehouseDocument != null && warehouseDocument.WarehouseDirection == WarehouseDirection.IncomeShift)             //MM+
            {
                if (actualStatus == DocumentStatus.Saved && requestedStatus == DocumentStatus.Committed)
                {
                    warehouseDocument.DocumentStatus = DocumentStatus.Committed;
                    warehouseDocument.IssueDate      = SessionManager.VolatileElements.CurrentDateTime;

                    foreach (WarehouseDocumentLine line in warehouseDocument.Lines.Children)
                    {
                        line.Direction  = 1;
                        line.IncomeDate = SessionManager.VolatileElements.CurrentDateTime;
                    }

                    this.coordinator.SaveBusinessObject(warehouseDocument);
                }
                else if ((actualStatus == DocumentStatus.Committed || actualStatus == DocumentStatus.Saved) && requestedStatus == DocumentStatus.Canceled)
                {
                    useStandardCancelProcessing = true;
                }
            }
            #endregion
            #region MM-
            else if (warehouseDocument != null && warehouseDocument.WarehouseDirection == WarehouseDirection.OutcomeShift)             //MM-
            {
                if (actualStatus == DocumentStatus.Committed && requestedStatus == DocumentStatus.Canceled)
                {
                    useStandardCancelProcessing = true;

                    foreach (var line in warehouseDocument.Lines)
                    {
                        line.IncomeOutcomeRelations.RemoveAll();
                    }
                }
            }
            #endregion
            #region FinancialDocument
            else if (financialDocument != null)
            {
                if (actualStatus == DocumentStatus.Committed && requestedStatus == DocumentStatus.Canceled)
                {
                    DocumentStatusChangeLogic.CancelFinancialDocument(financialDocument);

                    this.coordinator.SaveBusinessObject(financialDocument);
                }
            }
            #endregion
            #region WarehouseDocumentCorrection
            else if (warehouseDocument != null && (warehouseDocument.DocumentType.DocumentCategory == DocumentCategory.IncomeWarehouseCorrection ||
                                                   warehouseDocument.DocumentType.DocumentCategory == DocumentCategory.OutcomeWarehouseCorrection))
            {
                SqlConnectionManager.Instance.BeginTransaction();

                try
                {
                    if (ConfigurationMapper.Instance.IsWmsEnabled)
                    {
                        DependencyContainerManager.Container.Get <WarehouseMapper>().DeleteShiftsForDocument(warehouseDocument.Id.Value);
                    }

                    this.mapper.CancelWarehouseDocument(warehouseDocument.Id.Value);
                    Coordinator.LogSaveBusinessObjectOperation();

                    if (!ConfigurationMapper.Instance.ForceRollbackTransaction)
                    {
                        SqlConnectionManager.Instance.CommitTransaction();
                    }
                    else
                    {
                        SqlConnectionManager.Instance.RollbackTransaction();
                    }
                }
                catch (SqlException sqle)
                {
                    RoboFramework.Tools.RandomLogHelper.GetLog().Debug("FractusRefactorTraceCatch:59");
                    Coordinator.ProcessSqlException(sqle, document.BOType, true);
                    throw;
                }
                catch (Exception)
                {
                    RoboFramework.Tools.RandomLogHelper.GetLog().Debug("FractusRefactorTraceCatch:60");
                    SqlConnectionManager.Instance.RollbackTransaction();
                    throw;
                }
            }
            #endregion
            #region ServiceDocument
            else if (serviceDocument != null)
            {
                if (actualStatus == DocumentStatus.Saved && requestedStatus == DocumentStatus.Canceled)
                {
                    using (DocumentCoordinator c = new DocumentCoordinator(false, true))
                    {
                        c.CancelServiceDocument(serviceDocument);
                    }
                }
            }
            #endregion
            #region InventoryDocument
            else if (inventoryDocument != null)
            {
                if (actualStatus == DocumentStatus.Saved && (requestedStatus == DocumentStatus.Committed || requestedStatus == DocumentStatus.Canceled))
                {
                    inventoryDocument.DocumentStatus = requestedStatus;

                    using (DocumentCoordinator c = new DocumentCoordinator(false, true))
                    {
                        c.SaveBusinessObject(inventoryDocument);
                    }
                }
            }
            #endregion
            #region ComplaintDocument
            else if (complaintDocument != null)
            {
                if (actualStatus == DocumentStatus.Saved && requestedStatus == DocumentStatus.Canceled)
                {
                    SqlConnectionManager.Instance.BeginTransaction();
                    this.coordinator.CanCommitTransaction = false;

                    foreach (DocumentRelation relation in complaintDocument.Relations)
                    {
                        if (relation.RelationType == DocumentRelationType.ComplaintToInternalIncome ||
                            relation.RelationType == DocumentRelationType.ComplaintToInternalOutcome)
                        {
                            using (DocumentCoordinator c = new DocumentCoordinator(false, false))
                            {
                                XDocument xml = new XDocument(new XElement("root"));
                                xml.Root.Add(new XElement("warehouseDocumentId", relation.RelatedDocument.Id.ToUpperString()));
                                xml.Root.Add(new XElement("status", "-20"));
                                c.ChangeDocumentStatus(xml);
                            }
                        }
                    }

                    complaintDocument.DocumentStatus = DocumentStatus.Canceled;
                    complaintDocument.Attributes[DocumentFieldName.Attribute_ProcessState].Value.Value = "closed";

                    try
                    {
                        this.coordinator.SaveBusinessObject(complaintDocument);

                        if (!ConfigurationMapper.Instance.ForceRollbackTransaction)
                        {
                            SqlConnectionManager.Instance.CommitTransaction();
                        }
                        else
                        {
                            SqlConnectionManager.Instance.RollbackTransaction();
                        }
                    }
                    catch (SqlException sqle)
                    {
                        RoboFramework.Tools.RandomLogHelper.GetLog().Debug("FractusRefactorTraceCatch:61");
                        Coordinator.ProcessSqlException(sqle, document.BOType, this.coordinator.CanCommitTransaction);
                        throw;
                    }
                    catch (Exception)
                    {
                        RoboFramework.Tools.RandomLogHelper.GetLog().Debug("FractusRefactorTraceCatch:62");
                        SqlConnectionManager.Instance.RollbackTransaction();
                        throw;
                    }
                }
                else if (actualStatus == DocumentStatus.Saved && requestedStatus == DocumentStatus.Committed)
                {
                    if (complaintDocument.DocumentOptions.Where(o => o is CloseProcessOption).FirstOrDefault() == null)
                    {
                        complaintDocument.DocumentOptions.Add(new CloseProcessOption());
                    }

                    this.coordinator.SaveBusinessObject(complaintDocument);
                }
            }
            #endregion
            #region SalesOrder
            else if (commercialDocument != null && commercialDocument.DocumentType.DocumentCategory == DocumentCategory.SalesOrder)
            {
                if (actualStatus == DocumentStatus.Committed && requestedStatus == DocumentStatus.Saved)                 //anulowanie rozliczenia
                {
                    SalesOrderFactory.OpenSalesOrder(commercialDocument);
                    commercialDocument.Attributes.Remove(commercialDocument.Attributes[DocumentFieldName.Attribute_SettlementDate]);
                    commercialDocument.DocumentStatus = DocumentStatus.Saved;
                }
                else if (actualStatus == DocumentStatus.Saved && requestedStatus == DocumentStatus.Canceled)
                {
                    commercialDocument.Attributes[DocumentFieldName.Attribute_ProcessState].Value.Value = "canceled";
                    commercialDocument.DocumentStatus = DocumentStatus.Canceled;

                    foreach (CommercialDocumentLine line in commercialDocument.Lines.Children)
                    {
                        line.CommercialDirection = 0;
                        line.OrderDirection      = 0;
                        line.CommercialWarehouseRelations.RemoveAll();
                    }
                }
                else if (actualStatus == DocumentStatus.Saved && requestedStatus == DocumentStatus.Committed)
                {
                    bool result = SalesOrderFactory.TryCloseSalesOrder(commercialDocument);

                    if (!result)
                    {
                        throw new ClientException(ClientExceptionId.ErrorClosingSalesOrder);
                    }
                }

                this.coordinator.SaveBusinessObject(document);
            }
            #endregion
            #region ProductionOrder
            else if (commercialDocument != null && commercialDocument.DocumentType.DocumentCategory == DocumentCategory.ProductionOrder)
            {
                if (actualStatus == DocumentStatus.Saved && requestedStatus == DocumentStatus.Committed)
                {
                    commercialDocument.Attributes[DocumentFieldName.Attribute_ProcessState].Value.Value = "closed";
                    commercialDocument.DocumentStatus = DocumentStatus.Committed;
                }
                else if (actualStatus == DocumentStatus.Saved && requestedStatus == DocumentStatus.Canceled)
                {
                    commercialDocument.Attributes[DocumentFieldName.Attribute_ProcessState].Value.Value = "canceled";
                    commercialDocument.DocumentStatus = DocumentStatus.Canceled;
                }

                this.coordinator.SaveBusinessObject(document);
            }
            #endregion
            else if (actualStatus == DocumentStatus.Committed && requestedStatus == DocumentStatus.Canceled)
            {
                useStandardCancelProcessing = true;
            }

            //standardowa część anulowania dokumentu
            if (useStandardCancelProcessing)
            {
                SqlConnectionManager.Instance.BeginTransaction();
                this.coordinator.CanCommitTransaction = false;

                document.DocumentStatus = DocumentStatus.Canceled;

                if (document.BOType == BusinessObjectType.CommercialDocument)
                {
                    commercialDocument.ValidateSalesOrderRealizedLines(false);                    //muszę najpierw sprawdzić są pow. z ZS bo potem usuwam atrybut
                    this.ResetCommercialDocumentLinesFlags(commercialDocument);
                }
                else
                {
                    warehouseDocument.CheckDoesRealizeClosedSalesOrder(coordinator);                    //sprawdzenie przed usunięciem relacji
                    this.ResetWarehouseDocumentLinesFlags(warehouseDocument);
                }
                document.Relations.RemoveAll();

                try
                {
                    this.coordinator.SaveBusinessObject(document);

                    if (document.BOType == BusinessObjectType.CommercialDocument)
                    {
                        this.mapper.UnrelateCommercialDocumentFromWarehouseDocuments(documentId);
                    }
                    else
                    {
                        this.mapper.DeleteWarehouseDocumentRelations((WarehouseDocument)document);
                    }

                    if (!ConfigurationMapper.Instance.ForceRollbackTransaction)
                    {
                        SqlConnectionManager.Instance.CommitTransaction();
                    }
                    else
                    {
                        SqlConnectionManager.Instance.RollbackTransaction();
                    }
                }
                catch (SqlException sqle)
                {
                    RoboFramework.Tools.RandomLogHelper.GetLog().Debug("FractusRefactorTraceCatch:63");
                    Coordinator.ProcessSqlException(sqle, document.BOType, this.coordinator.CanCommitTransaction);
                    throw;
                }
                catch (Exception)
                {
                    RoboFramework.Tools.RandomLogHelper.GetLog().Debug("FractusRefactorTraceCatch:64");
                    SqlConnectionManager.Instance.RollbackTransaction();
                    throw;
                }
            }
        }