public XDocument SaveBusinessObject(WarehouseDocument shiftDocument)
        {
            DictionaryMapper.Instance.CheckForChanges();

            this.CorrectDocumentConsistency(shiftDocument);

            shiftDocument.Validate();

            //WarehouseDocument document = (WarehouseDocument)this.coordinator.CreateNewBusinessObject(BusinessObjectType.WarehouseDocument, "outcomeShift", null);
            //document.Deserialize(shiftDocument.Serialize());
            //zmienione bo shiftTransaction sie nie przenosilo
            WarehouseDocument document = (WarehouseDocument)mapper.ConvertToBusinessObject(shiftDocument.FullXml.Root.Element("warehouseDocument"), shiftDocument.FullXml.Root.Element("options"));

            if (document.IsNew) //jezeli to edycja to nie ruszamy linii
            {
                document.Lines.RemoveAll();
            }

            SqlConnectionManager.Instance.BeginTransaction();

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

                if (document.IsNew) //jezeli to edycja to nie ruszamy linii
                {
                    this.GenerateLines(shiftDocument, document);
                }

                this.ExecuteDocumentOptions(document);

                document.Validate();

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

                DocumentLogicHelper.AssignNumber(document, this.mapper);

                document.UpdateStatus(true);

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

                this.coordinator.UpdateStock(document);

                this.ValidateShiftsToLinesRelations(document);

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

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

                document.SaveChanges(operations);

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

                document.SaveRelations(operations);

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

                if (operations.Root.HasElements)
                {
                    //this.mapper.UpdateStockForCanceledDocument(document);
                    this.mapper.ExecuteOperations(operations);

                    if (document.IsNew)
                    {
                        this.mapper.ValuateOutcomeWarehouseDocument(document);
                    }

                    this.mapper.CreateCommunicationXml(document);
                    this.mapper.CreateCommunicationXmlForDocumentRelations(operations);
                    this.mapper.UpdateDictionaryIndex(document);
                }

                //MM- może realizować rezerwację
                this.mapper.UpdateReservationAndOrderStock(document);

                WarehouseCoordinator.ProcessWarehouseManagamentSystem(document.ShiftTransaction);

                Coordinator.LogSaveBusinessObjectOperation();

                this.ProcessLocalOutcomeShift(document);

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

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

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

                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:78");
                Coordinator.ProcessSqlException(sqle, document.BOType, this.coordinator.CanCommitTransaction);
                throw;
            }
            catch (Exception)
            {
                RoboFramework.Tools.RandomLogHelper.GetLog().Debug("FractusRefactorTraceCatch:79");
                if (this.coordinator.CanCommitTransaction)
                {
                    SqlConnectionManager.Instance.RollbackTransaction();
                }
                throw;
            }
        }
        /// <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;
            }
        }