private static void GenerateFinancialDocumentToPayment(Payment payment, CommercialDocument document)
        {
            DocumentMapper mapper = DependencyContainerManager.Container.Get <DocumentMapper>();

            using (DocumentCoordinator coordinator = new DocumentCoordinator(false, false))
            {
                PaymentMethod paymentMethod = DictionaryMapper.Instance.GetPaymentMethod(payment.PaymentMethodId.Value);

                Guid branchId = SessionManager.User.BranchId;

                if (DictionaryMapper.Instance.IsPaymentMethodSupportedByRegister(paymentMethod.Id.Value, branchId))
                {
                    FinancialRegister register = DictionaryMapper.Instance.GetFinancialRegisterForSpecifiedPaymentMethod(paymentMethod.Id.Value, branchId, payment.PaymentCurrencyId);

                    if (register == null)
                    {
                        throw new ClientException(ClientExceptionId.UnableToIssueFinancialDocument3);
                    }

                    FinancialDirection direction = FinancialDocumentFactory.GetFinancialDirectionForPayment(document, payment);

                    Guid?reportId = mapper.GetOpenedFinancialReportId(register.Id.Value);

                    if (reportId == null)
                    {
                        throw new ClientException(ClientExceptionId.UnableToIssueDocumentToClosedFinancialReport);
                    }

                    FinancialDocument financialDoc = new FinancialDocument();
                    financialDoc.RelatedCommercialDocument = document;
                    coordinator.TrySaveProfileIdAttribute(financialDoc);

                    if (direction == FinancialDirection.Income)
                    {
                        financialDoc.DocumentTypeId         = register.IncomeDocumentTypeId;
                        financialDoc.Number.NumberSettingId = register.IncomeNumberSettingId;
                    }
                    else
                    {
                        financialDoc.DocumentTypeId         = register.OutcomeDocumentTypeId;
                        financialDoc.Number.NumberSettingId = register.OutcomeNumberSettingId;
                    }

                    financialDoc.Contractor          = payment.Contractor;
                    financialDoc.ContractorAddressId = payment.ContractorAddressId;
                    FinancialReport report = new FinancialReport();
                    report.Id = reportId;
                    report.FinancialRegisterId   = register.Id.Value;
                    financialDoc.FinancialReport = report;
                    financialDoc.DocumentStatus  = DocumentStatus.Committed;
                    DuplicableAttributeFactory.DuplicateAttributes(document, financialDoc);

                    Payment pt = financialDoc.Payments.CreateNew();
                    pt.Amount                       = Math.Abs(payment.Amount);
                    pt.ExchangeRate                 = financialDoc.ExchangeRate = document.ExchangeRate;
                    pt.ExchangeScale                = financialDoc.ExchangeScale = document.ExchangeScale;
                    pt.ExchangeDate                 = financialDoc.ExchangeDate = document.ExchangeDate;
                    financialDoc.Amount             = pt.Amount;
                    financialDoc.DocumentCurrencyId = payment.PaymentCurrencyId;
                    PaymentSettlement settlement = pt.Settlements.CreateNew();
                    settlement.IsAutoGenerated = true;
                    settlement.RelatedPayment  = payment;
                    settlement.Amount          = pt.Amount;

                    document.AddRelatedObject(financialDoc);
                }
            }
        }