private List <TrialBalanceEntry> AddSummaryAccounts(List <TrialBalanceEntry> summaryEntries,
                                                            List <TrialBalanceEntry> returnedEntries,
                                                            TrialBalanceEntry entry)
        {
            var helper = new TrialBalanceHelper(_command);

            var summaryAccounts = summaryEntries.Where(
                a => a.Account.GroupNumber == entry.Account.GroupNumber &&
                a.SubledgerAccountId == 0 &&
                a.SubledgerAccountIdParent == entry.SubledgerAccountIdParent &&
                a.Ledger.Number == entry.Ledger.Number &&
                a.Currency.Code == entry.Currency.Code &&
                a.ItemType == TrialBalanceItemType.BalanceEntry).ToList();

            foreach (var summary in summaryAccounts)
            {
                var existSummaryAccount = returnedEntries.FirstOrDefault(
                    a => a.SubledgerAccountIdParent == entry.SubledgerAccountIdParent &&
                    a.Account.Number == summary.Account.Number &&
                    a.Ledger.Number == summary.Ledger.Number &&
                    a.Currency.Code == summary.Currency.Code &&
                    a.Sector.Code == summary.Sector.Code);
                if (existSummaryAccount == null)
                {
                    returnedEntries.Add(summary);
                }
            }

            return(returnedEntries);
        }
        internal FixedList <TrialBalanceEntry> GetPostingEntries(bool isComparativeBalance = false)
        {
            var helper = new TrialBalanceHelper(_command);

            TrialBalanceCommandPeriod commandPeriod = new TrialBalanceCommandPeriod();

            if (isComparativeBalance)
            {
                commandPeriod = _command.FinalPeriod;
            }
            else
            {
                commandPeriod = _command.InitialPeriod;
            }

            FixedList <TrialBalanceEntry> postingEntries = helper.GetTrialBalanceEntries(commandPeriod);

            if (_command.ValuateBalances || _command.ValuateFinalBalances)
            {
                postingEntries = helper.ValuateToExchangeRate(postingEntries, commandPeriod);

                if (_command.ConsolidateBalancesToTargetCurrency)
                {
                    postingEntries = helper.ConsolidateToTargetCurrency(postingEntries, commandPeriod);
                }
            }
            return(postingEntries);
        }
        private List <TrialBalanceEntry> CreateOrAccumulateTotalBySubsidiaryEntry(
            List <TrialBalanceEntry> returnedEntries,
            TrialBalanceEntry entry)
        {
            var helper = new TrialBalanceHelper(_command);

            var parentEntries = new EmpiriaHashTable <TrialBalanceEntry>();

            var existTotalByAccount = returnedEntries.FirstOrDefault(
                a => a.SubledgerAccountId == entry.SubledgerAccountIdParent &&
                a.Ledger.Number == entry.Ledger.Number &&
                a.Currency.Code == entry.Currency.Code);

            if (existTotalByAccount == null)
            {
                helper.SummaryBySubsidiaryEntry(parentEntries, entry, TrialBalanceItemType.BalanceSummary);

                var parent = parentEntries.Values.FirstOrDefault();
                parent.SubledgerAccountId     = parent.SubledgerAccountIdParent;
                parent.SubledgerAccountNumber = SubsidiaryAccount.Parse(entry.SubledgerAccountIdParent).Number ?? "";
                returnedEntries.Add(parent);
            }
            else
            {
                existTotalByAccount.Sum(entry);
            }

            return(returnedEntries);
        }
        private void CreateOrAccumulateParentWithoutSector(
            List <TrialBalanceEntry> returnedEntries,
            TrialBalanceEntry entry,
            EmpiriaHashTable <TrialBalanceEntry> summaryParentEntries,
            StandardAccount currentParent)
        {
            var helper = new TrialBalanceHelper(_command);

            var entryWithoutSector = returnedEntries.FirstOrDefault(
                a => a.Ledger.Number == entry.Ledger.Number &&
                a.Currency.Code == entry.Currency.Code &&
                a.SubledgerAccountIdParent == entry.SubledgerAccountId &&
                a.Account.Number == currentParent.Number &&
                a.NotHasSector);


            if (entryWithoutSector == null)
            {
                helper.SummaryByEntry(summaryParentEntries, entry, currentParent, Sector.Empty,
                                      TrialBalanceItemType.BalanceSummary);
            }
            else
            {
                entryWithoutSector.Sum(entry);
            }
        }
        private void GenerateOrIncreaseParentEntries(List <TrialBalanceEntry> subsidiaryEntries,
                                                     List <TrialBalanceEntry> returnedEntries)
        {
            var helper = new TrialBalanceHelper(_command);

            foreach (var entry in subsidiaryEntries)
            {
                int count = 0;
                List <TrialBalanceEntry> summaryEntries = new List <TrialBalanceEntry>();
                var             summaryParentEntries    = new EmpiriaHashTable <TrialBalanceEntry>();
                StandardAccount account = entry.Account;

                while (true)
                {
                    var parent = returnedEntries.FirstOrDefault(a => a.Ledger.Number == entry.Ledger.Number &&
                                                                a.Currency.Code == entry.Currency.Code && a.SubledgerAccountId == 0 &&
                                                                a.SubledgerAccountIdParent == entry.SubledgerAccountId &&
                                                                a.Account.Number == account.Number && a.Sector.Code == entry.Sector.Code);

                    if (parent == null)
                    {
                        count++;
                        TrialBalanceItemType itemType = count == 1 ? TrialBalanceItemType.BalanceEntry :
                                                        TrialBalanceItemType.BalanceSummary;
                        helper.SummaryByEntry(summaryParentEntries, entry, account, entry.Sector, itemType);

                        if (!account.HasParent && entry.HasSector && entry.SubledgerAccountId > 0)
                        {
                            CreateOrAccumulateParentWithoutSector(returnedEntries, entry, summaryParentEntries, account);
                            break;
                        }
                        else if (!account.HasParent)
                        {
                            break;
                        }
                        else
                        {
                            account = account.GetParent();
                        }
                    }
                    else
                    {
                        parent.Sum(entry);

                        if (!account.HasParent)
                        {
                            AccumulateSubledgerAccount(returnedEntries, entry, account);
                            break;
                        }
                        else
                        {
                            account = account.GetParent();
                        }
                    }
                } // while
                summaryEntries.AddRange(summaryParentEntries.Values.ToList());
                returnedEntries.AddRange(summaryEntries);
            } // foreach
        }
        internal TrialBalance BuildForBalancesGeneration()
        {
            var helper = new TrialBalanceHelper(_command);

            FixedList <TrialBalanceEntry> trialBalance = helper.GetPostingEntries();

            var returnBalance = new FixedList <ITrialBalanceEntry>(trialBalance.Select(x => (ITrialBalanceEntry)x));

            return(new TrialBalance(_command, returnBalance));
        }
        internal TrialBalance Build()
        {
            var helper = new TrialBalanceHelper(_command);
            List <TrialBalanceEntry> trialBalanceComparative = new List <TrialBalanceEntry>();

            FixedList <TrialBalanceEntry> trialBalanceFirstPeriod = helper.GetPostingEntries();

            FixedList <TrialBalanceEntry> trialBalanceSecondPeriod = helper.GetPostingEntries(true);

            trialBalanceComparative.AddRange(trialBalanceFirstPeriod);
            trialBalanceComparative.AddRange(trialBalanceSecondPeriod);

            var returnBalance = new FixedList <ITrialBalanceEntry>(trialBalanceComparative.Select(x => (ITrialBalanceEntry)x));

            return(new TrialBalance(_command, returnBalance));
        }
        internal TrialBalance Build()
        {
            var helper = new TrialBalanceHelper(_command);

            List <TrialBalanceEntry> trialBalance = helper.GetSummaryAndPostingEntries();

            List <TrialBalanceEntry> summarySubsidiaryEntries = BalancesBySubsidiaryAccounts(trialBalance);

            trialBalance = CombineTotalSubsidiaryEntriesWithSummaryAccounts(summarySubsidiaryEntries);

            trialBalance = helper.RestrictLevels(trialBalance);

            var returnBalance = new FixedList <ITrialBalanceEntry>(trialBalance.Select(x => (ITrialBalanceEntry)x));

            return(new TrialBalance(_command, returnBalance));
        }
        internal TrialBalance Build()
        {
            var helper = new TrialBalanceHelper(_command);

            FixedList <TrialBalanceEntry> postingEntries = helper.GetPostingEntries();

            List <TrialBalanceEntry> summaryEntries = helper.GenerateSummaryEntries(postingEntries);

            List <TrialBalanceEntry> trialBalance = helper.CombineSummaryAndPostingEntries(
                summaryEntries, postingEntries);

            FixedList <TrialBalanceEntry> summaryGroupEntries = helper.GenerateTotalSummaryGroups(postingEntries);

            trialBalance = helper.CombineGroupEntriesAndPostingEntries(trialBalance, summaryGroupEntries);

            List <TrialBalanceEntry> summaryTotalDebtorCreditorEntries =
                helper.GenerateTotalSummaryDebtorCreditor(postingEntries.ToList());

            trialBalance = helper.CombineDebtorCreditorAndPostingEntries(trialBalance,
                                                                         summaryTotalDebtorCreditorEntries);

            List <TrialBalanceEntry> summaryTotalCurrencies = helper.GenerateTotalSummaryCurrency(
                summaryTotalDebtorCreditorEntries);

            trialBalance = helper.CombineCurrencyTotalsAndPostingEntries(trialBalance, summaryTotalCurrencies);

            List <TrialBalanceEntry> summaryTotalConsolidatedByLedger =
                helper.GenerateTotalSummaryConsolidatedByLedger(summaryTotalCurrencies);

            trialBalance = helper.CombineTotalConsolidatedByLedgerAndPostingEntries(
                trialBalance, summaryTotalConsolidatedByLedger);

            List <TrialBalanceEntry> summaryTrialBalanceConsolidated = helper.GenerateTotalSummaryConsolidated(
                summaryTotalCurrencies);

            trialBalance = helper.CombineTotalConsolidatedAndPostingEntries(
                trialBalance, summaryTrialBalanceConsolidated);

            trialBalance = helper.RestrictLevels(trialBalance);

            var returnBalance = new FixedList <ITrialBalanceEntry>(trialBalance.Select(x => (ITrialBalanceEntry)x));

            return(new TrialBalance(_command, returnBalance));
        }
        internal TrialBalance Build()
        {
            var helper           = new TrialBalanceHelper(_command);
            var twoColumnsHelper = new TwoCurrenciesBalanceHelper(_command);

            FixedList <TrialBalanceEntry> postingEntries = helper.GetTrialBalanceEntries(_command.InitialPeriod);

            postingEntries = helper.ValuateToExchangeRate(postingEntries, _command.InitialPeriod);

            List <TrialBalanceEntry> summaryEntries = helper.GenerateSummaryEntries(postingEntries);
            List <TrialBalanceEntry> trialBalance   = helper.CombineSummaryAndPostingEntries(summaryEntries,
                                                                                             postingEntries);

            trialBalance = helper.RestrictLevels(trialBalance);

            FixedList <TwoCurrenciesBalanceEntry> twoColumnsEntries =
                twoColumnsHelper.MergeAccountsIntoTwoColumns(trialBalance);

            FixedList <TwoCurrenciesBalanceEntry> summaryGroupEntries =
                twoColumnsHelper.GetTotalSummaryGroup(twoColumnsEntries);

            twoColumnsEntries = twoColumnsHelper.CombineGroupEntriesAndTwoColumnsEntries(
                twoColumnsEntries, summaryGroupEntries);

            List <TwoCurrenciesBalanceEntry> summaryTotalDeptorCreditorEntries =
                twoColumnsHelper.GetTotalDeptorCreditorTwoColumnsEntries(
                    twoColumnsEntries);

            twoColumnsEntries = twoColumnsHelper.CombineTotalDeptorCreditorAndTwoColumnsEntries(
                twoColumnsEntries.ToList(), summaryTotalDeptorCreditorEntries);

            List <TwoCurrenciesBalanceEntry> summaryTwoColumnsBalanceTotal =
                twoColumnsHelper.GenerateTotalSummary(summaryTotalDeptorCreditorEntries);

            twoColumnsEntries = twoColumnsHelper.CombineTotalConsolidatedAndPostingEntries(
                twoColumnsEntries, summaryTwoColumnsBalanceTotal);

            FixedList <ITrialBalanceEntry> twoColumnsBalance = twoColumnsEntries.Select(x => (ITrialBalanceEntry)x)
                                                               .ToList().ToFixedList();

            return(new TrialBalance(_command, twoColumnsBalance));
        }