BudgetAnalyser.LedgerBook.LedgerBookGridBuilderV2.AddLedgerEntryLinesVertically C# (CSharp) Method

AddLedgerEntryLinesVertically() private method

private AddLedgerEntryLinesVertically ( Grid grid, int numberOfMonthsToShow ) : void
grid System.Windows.Controls.Grid
numberOfMonthsToShow int
return void
        private void AddLedgerEntryLinesVertically(Grid grid, int numberOfMonthsToShow)
        {
            var gridColumn = 2; //because the first two columns are headings
            var monthNumber = 0;

            // Loop thru all Reconciliations from most recent to oldest adding cells to the grid vertically. 
            foreach (LedgerEntryLine line in this.ledgerBook.Reconciliations)
            {
                var gridRow = 0;
                if (++monthNumber > numberOfMonthsToShow)
                {
                    break;
                }

                // Date
                gridRow = AddDateCellToLedgerEntryLine(grid, gridRow, ref gridColumn, line);

                // Remarks
                TextBlock remarksHyperlink = AddHyperlinkToGrid(grid, "...", ref gridRow, gridColumn, NormalStyle, line.Remarks, line);
                var hyperlink = (Hyperlink)remarksHyperlink.Inlines.FirstInline;
                hyperlink.Command = this.showRemarksCommand;

                // Bank Balance
                AddBorderToGridCell(grid, BankBalanceBackground, false, gridRow, gridColumn);
                TextBlock bankBalanceText = AddHyperlinkToGrid(
                    grid,
                    line.LedgerBalance.ToString("N", CultureInfo.CurrentCulture),
                    ref gridRow,
                    gridColumn,
                    ImportantNumberStyle,
                    BuildToolTipForBankBalance(line),
                    line);
                hyperlink = (Hyperlink)bankBalanceText.Inlines.FirstInline;
                hyperlink.Command = this.showBankBalancesCommand;
                bankBalanceText.Foreground = (Brush)FindResource(BankBalanceTextBrush);

                // Balance Adjustments
                AddHyperlinkToGrid(
                    grid,
                    line.TotalBalanceAdjustments.ToString("N", CultureInfo.CurrentCulture),
                    ref gridRow,
                    gridColumn,
                    ImportantNumberStyle,
                    parameter: line);

                // Surplus
                gridRow = AddSurplusCell(grid, gridRow, gridColumn, line);

                // Ledgers
                Account currentBankAccount = null;
                foreach (LedgerBucket ledger in this.sortedLedgers)
                {
                    if (currentBankAccount != ledger.StoredInAccount)
                    {
                        gridRow++;
                        currentBankAccount = ledger.StoredInAccount;
                    }

                    LedgerEntry entry = line.Entries.FirstOrDefault(e => e.LedgerBucket.BudgetBucket == ledger.BudgetBucket);
                    decimal balance, netAmount;

                    var movedBankAccounts = false;
                    if (entry == null)
                    {
                        // New ledger added that older entries do not have.
                        balance = 0;
                        netAmount = 0;
                    }
                    else if (entry.LedgerBucket != ledger)
                    {
                        // This means the ledger has change bank accounts and should not be included in this bank account's group. Leave blank cells in this case.
                        movedBankAccounts = true;
                        balance = 0;
                        netAmount = 0;
                    }
                    else
                    {
                        balance = entry.Balance;
                        netAmount = entry.NetAmount;
                    }

                    if (ledger.BudgetBucket is SpentMonthlyExpenseBucket)
                    {
                        Border border = AddBorderToGridCell(grid, false, true, gridRow, gridColumn);
                        border.ToolTip = "This ledger has moved to another bank account.";
                        if (movedBankAccounts)
                        {
                            gridRow++;
                        }
                        else
                        {
                            AddHyperlinkToGrid(grid, balance.ToString("N", CultureInfo.CurrentCulture), ref gridRow, gridColumn, NumberStyle, parameter: entry);
                        }
                    }
                    else
                    {
                        Border border1 = AddBorderToGridCell(grid, true, false, gridRow, gridColumn);
                        Border border2 = AddBorderToGridCell(grid, false, true, gridRow + 1, gridColumn);
                        if (movedBankAccounts)
                        {
                            border1.ToolTip = "This ledger has moved to another bank account.";
                            border2.ToolTip = "This ledger has moved to another bank account.";
                            gridRow += 2;
                            continue;
                        }

                        AddHyperlinkToGrid(grid, netAmount.ToString("N", CultureInfo.CurrentCulture), ref gridRow, gridColumn, NumberStyle, parameter: entry);
                        AddHyperlinkToGrid(grid, balance.ToString("N", CultureInfo.CurrentCulture), ref gridRow, gridColumn, NumberStyle, parameter: entry);
                    }
                }

                gridColumn++;
            }
        }