private void CreateCFRegisterRecord(Session ssn, ForCashFlowRecord cashFlowRecord)     //, crmCParty ourParty) {
        {
            crmCashFlowRegister cfr = new crmCashFlowRegister(ssn);

            fmCPRRepaymentTask  repaymentTask  = cashFlowRecord.repaymentTask;
            fmCDocRCB           paymentDoc     = cashFlowRecord.paymentDoc;
            fmCPRPaymentRequest paymentRequest = cashFlowRecord.paymentRequest;

            Guid token = paymentRequest.Oid;

            cfr.Token   = token;
            cfr.Section = CashFlowRegisterSection.REPAYMENT_JOURNAL;

            cfr.SourceGUID = repaymentTask.Oid;   // Будет пустой GUID
            cfr.SourceType = repaymentTask.GetType();

            cfr.PaymentDocument = paymentDoc;
            cfr.BankAccount     = repaymentTask.BankAccount;
            cfr.Bank            = repaymentTask.BankAccount.Bank;
            cfr.OperationDate   = cashFlowRecord.date;

            fmCPRPaymentRequestContract paymentRequestContract = paymentRequest as fmCPRPaymentRequestContract;

            //if (paymentRequestContract != null && paymentRequestContract.ContractDeal != null) {
            //    cfr.Contract = paymentRequestContract.ContractDeal.Contract;
            //}
            if (paymentRequestContract != null)
            {
                cfr.Contract     = paymentRequestContract.Contract;
                cfr.ContractDeal = paymentRequestContract.ContractDeal;
            }

            cfr.fmOrder  = cashFlowRecord.paymentRequestObligation.Order;
            cfr.CostItem = cashFlowRecord.paymentRequestObligation.CostItem;
            cfr.Subject  = (cashFlowRecord.paymentRequestObligation.Order != null) ? cashFlowRecord.paymentRequestObligation.Order.Subject : null;

            cfr.PrimaryParty    = paymentRequest.PartyPaySender;
            cfr.ContragentParty = paymentRequest.PartyPayReceiver;

            //cfr.ObligationUnit =
            //cfr.PaymentItem =

            cfr.ValutaPayment    = paymentDoc.GetAccountValuta();
            cfr.ValutaObligation = cashFlowRecord.paymentRequestObligation.Valuta;

            // В валюте платежа
            //cfr.SumIn = this.PaymentDocument.GetSumIn(this.BankAccount);
            //cfr.SumOut = this.PaymentDocument.GetSumOut(this.BankAccount);

            /*
             * if (paymentDoc.PaymentReceiverRequisites.INN == ourParty.INN) {   // && this.PaymentDocument.PaymentReceiverRequisites.StatementOfAccount.BankAccount == this.BankAccount) {
             *  cfr.SumIn = paymentDoc.PaymentCost;
             * }
             * if (paymentDoc.PaymentPayerRequisites.INN == ourParty.INN) {   // && this.PaymentDocument.PaymentPayerRequisites.StatementOfAccount.BankAccount == this.BankAccount) {
             *  cfr.SumOut = paymentDoc.PaymentCost;
             * }
             */
            if (paymentDoc.PaymentReceiverRequisites.BankAccount == cashFlowRecord.repaymentJournalRecord.BankAccount)     // && this.PaymentDocument.PaymentReceiverRequisites.StatementOfAccount.BankAccount == this.BankAccount) {
            //cfr.SumIn = cashFlowRecord.repaymentJournalRecord.SumIn;
            {
                cfr.SumIn = cashFlowRecord.sum;
            }
            if (paymentDoc.PaymentPayerRequisites.BankAccount == cashFlowRecord.repaymentJournalRecord.BankAccount)     // && this.PaymentDocument.PaymentPayerRequisites.StatementOfAccount.BankAccount == this.BankAccount) {
            //cfr.SumOut = cashFlowRecord.repaymentJournalRecord.SumOut;
            {
                cfr.SumOut = cashFlowRecord.sum;
            }



            // В валюте обязательств

            /*
             * if (paymentDoc.PaymentReceiverRequisites.INN == ourParty.INN) {   // && this.PaymentDocument.PaymentReceiverRequisites.StatementOfAccount.BankAccount == this.BankAccount) {
             *  cfr.SumObligationIn = cashFlowRecord.sum;
             * }
             * if (paymentDoc.PaymentPayerRequisites.INN == ourParty.INN) {   // && this.PaymentDocument.PaymentPayerRequisites.StatementOfAccount.BankAccount == this.BankAccount) {
             *  cfr.SumObligationOut = cashFlowRecord.sum;
             * }
             */
            if (paymentDoc.PaymentReceiverRequisites.BankAccount == cashFlowRecord.repaymentJournalRecord.BankAccount)     // && this.PaymentDocument.PaymentReceiverRequisites.StatementOfAccount.BankAccount == this.BankAccount) {
            //cfr.SumObligationIn = cashFlowRecord.sum;
            {
                cfr.SumObligationIn = Math.Round(cfr.SumIn * csCNMValutaCourse.GetCrossCourceOnDate(ssn, paymentDoc.GetAccountDateChange(), cfr.ValutaPayment, cfr.ValutaObligation), 4);
            }
            if (paymentDoc.PaymentPayerRequisites.BankAccount == cashFlowRecord.repaymentJournalRecord.BankAccount)     // && this.PaymentDocument.PaymentPayerRequisites.StatementOfAccount.BankAccount == this.BankAccount) {
            //cfr.SumObligationOut = cashFlowRecord.sum;
            {
                cfr.SumObligationOut = Math.Round(cfr.SumOut * csCNMValutaCourse.GetCrossCourceOnDate(ssn, paymentDoc.GetAccountDateChange(), cfr.ValutaPayment, cfr.ValutaObligation), 4);
            }

            // В рублях
            try {
                cfr.SumInAcc  = Math.Round(cfr.SumIn * csCNMValutaCourse.GetCrossCourceOnDate(ssn, paymentDoc.GetAccountDateChange(), cfr.ValutaPayment, GetValutaByCode(ssn, "RUB")), 4);
                cfr.SumOutAcc = Math.Round(cfr.SumOut * csCNMValutaCourse.GetCrossCourceOnDate(ssn, paymentDoc.GetAccountDateChange(), cfr.ValutaPayment, GetValutaByCode(ssn, "RUB")), 4);
            }
            catch {
            }

            cfr.PaymentRequestObligationGUID = cashFlowRecord.paymentRequestObligationGUID;

            //if (this.PaymentReceiverRequisites.INN == OurParty.INN && this.PaymentReceiverRequisites.StatementOfAccount.BankAccount == bankAccount) {
            //    Res = this.PaymentCost;
            //}
        }
        private void DistributeSum(Session ssn, fmCPRRepaymentJurnal repaymentJournalRecord)
        {
            // Задача привязки
            fmCPRRepaymentTask repaymentTask = new fmCPRRepaymentTask(ssn);
            fmCDocRCB          paymentDoc    = repaymentJournalRecord.PaymentDocument;

            repaymentTask.BankAccount     = repaymentJournalRecord.BankAccount;
            repaymentTask.PaymentDocument = paymentDoc;
            //repaymentTask.FillRepaymentTaskLines();
            //repaymentTask.FillRequestList();

            // Сумма, которую надо распределить между обязательствами заявки
            Decimal distributeSum = 0;                                                                  // repaymentJournalRecord.SumOut;   //GetSumRepaymentJournal(ssn, paymentDoc);

            if (paymentDoc.PaymentReceiverRequisites.BankAccount == repaymentJournalRecord.BankAccount) // && this.PaymentDocument.PaymentReceiverRequisites.StatementOfAccount.BankAccount == this.BankAccount) {
            {
                distributeSum = repaymentJournalRecord.SumIn;
            }
            if (paymentDoc.PaymentPayerRequisites.BankAccount == repaymentJournalRecord.BankAccount)     // && this.PaymentDocument.PaymentPayerRequisites.StatementOfAccount.BankAccount == this.BankAccount) {
            {
                distributeSum = repaymentJournalRecord.SumOut;
            }

            if (distributeSum == 0)
            {
                return;
            }

            List <ForCashFlowRecord> cashFlowRecordList = new List <ForCashFlowRecord>();

            // Применяем механизм пропорциональной разбивки
            Decimal intermediateDistributeSum = 0;

            if (distributeSum != 0)
            {
                Decimal totalOblSum = 0;
                foreach (fmCPRPaymentRequestObligation pro in repaymentJournalRecord.PaymentRequest.PaySettlmentOfObligations)
                {
                    totalOblSum += pro.Summ;
                }


                foreach (fmCPRPaymentRequestObligation pro in repaymentJournalRecord.PaymentRequest.PaySettlmentOfObligations)
                {
                    Decimal proSum = Math.Round((pro.Summ / totalOblSum) * distributeSum, 4);

                    // Пишем в промежуточный объект ForCashFlowRecord
                    ForCashFlowRecord cashFlowRecord = new ForCashFlowRecord();
                    cashFlowRecord.repaymentJournalRecord   = repaymentJournalRecord;
                    cashFlowRecord.paymentDoc               = paymentDoc;
                    cashFlowRecord.repaymentTask            = repaymentTask;
                    cashFlowRecord.paymentRequest           = repaymentJournalRecord.PaymentRequest;
                    cashFlowRecord.paymentRequestObligation = pro;
                    cashFlowRecord.sum  = proSum;
                    cashFlowRecord.date = paymentDoc.GetAccountDateChange();
                    cashFlowRecord.paymentRequestObligationGUID = pro.Oid;

                    cashFlowRecordList.Add(cashFlowRecord);

                    intermediateDistributeSum += proSum;
                }
            }

            // Разница между distributeSum и суммой по подуровню в результате ошибки округления
            Decimal diff = intermediateDistributeSum - distributeSum;

            if (diff != 0)
            {
                // Внесение поправки: нахождение самого большого по модулю значения и изменение этого значения
                // (без помощи linq)
                Decimal valueMax = 0;
                for (int i = 0; i < cashFlowRecordList.Count(); i++)
                {
                    if (Math.Abs(cashFlowRecordList[i].sum) > valueMax)
                    {
                        valueMax = Math.Abs(cashFlowRecordList[i].sum);
                    }
                }

                for (int i = 0; i < cashFlowRecordList.Count(); i++)
                {
                    if (Math.Abs(cashFlowRecordList[i].sum) == valueMax)
                    {
                        cashFlowRecordList[i].sum -= diff;
                        break;
                    }
                }
            }

            CreateCFRegisterRecords(ssn, cashFlowRecordList);   //, GetOurParty(ssn));
        }