/// <summary>
        /// Creates L record elements. Exports GL transactions and account balances.
        /// </summary>
        public IList <LedgerRecord> CreateLedgerRecords(GAFPeriod gafPeriod)
        {
            var taxPeriod = _gafRepository.GetTaxPeriodByKey(gafPeriod.BranchID, gafPeriod.TaxAgencyID, gafPeriod.TaxPeriodID);

            var finPeriods = _gafRepository.GetFinPeriodsInInterval(taxPeriod.StartDate, taxPeriod.EndDate)
                             .ToList();

            var finYears = finPeriods.Select(finPeriod => finPeriod.FinYear)
                           .Distinct();

            var lastYearPeriod = finYears.Select(finYear => _gafRepository.FindLastYearNotAdjustmentPeriod(finYear))
                                 .SingleOrDefault();

            if (lastYearPeriod != null && finPeriods.Select(finPeriod => finPeriod.FinPeriodID).Contains(lastYearPeriod.FinPeriodID))
            {
                finPeriods.AddRange(_gafRepository.GetAdjustmentFinPeriods(lastYearPeriod.FinYear));
            }

            var records = new List <LedgerRecord>();

            var branch = _gafRepository.GetBranchByID(gafPeriod.BranchID);

            var minFinPeriod = finPeriods.First();

            var accountsWithGLHistoryData = _gafRepository.GetAccountsWithDataToCalcBeginBalancesExcludingYTDNetIncAcc(branch.BranchID, branch.LedgerID, minFinPeriod.FinPeriodID);

            foreach (var accountWithGLHistoryData in accountsWithGLHistoryData)
            {
                var account = (Account)accountWithGLHistoryData;
                var glHistoryForLastActivityPeriod = (AH)accountWithGLHistoryData;
                var glHistoryForSelectedPeriod     = (GLHistory)accountWithGLHistoryData;

                decimal?beginBalanceForPeriod = 0m;

                if (glHistoryForLastActivityPeriod.FinPeriodID != null)
                {
                    var selectedPeriodPtdCreditTotal = glHistoryForSelectedPeriod.FinPtdCredit ?? 0m;
                    var selectedPeriodPtdDebitTotal  = glHistoryForSelectedPeriod.FinPtdDebit ?? 0m;
                    var selectedPeriodPtdSaldo       = AccountRules.CalcSaldo(account.Type, selectedPeriodPtdDebitTotal, selectedPeriodPtdCreditTotal);

                    beginBalanceForPeriod = GetBalanceSign(account.Type) * (glHistoryForLastActivityPeriod.FinYtdBalance - selectedPeriodPtdSaldo);
                }

                var balanceRecord = BuildLedgerRecordForBalance(account, minFinPeriod.StartDate, beginBalanceForPeriod.Value);
                records.Add(balanceRecord);

                foreach (var finPeriod in finPeriods)
                {
                    var glTrans = _gafRepository.GetPostedGLTrans(branch.BranchID, branch.LedgerID, account.AccountID, finPeriod.FinPeriodID);

                    foreach (var glTran in glTrans)
                    {
                        var record = BuildLedgerRecordForTran(account, glTran, records.LastOrDefault());

                        records.Add(record);
                    }

                    _gafRepository.ClearCaches();
                }
            }

            return(records);
        }