Run a Report on Quickbooks Online

Run a Report on Quickbooks Online C#.NET Code

Run a Report on Quickbooks Online C#.NET Code
                    
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Intuit.Ipp.Core;
using Intuit.Ipp.Data;
using Intuit.Ipp.ReportService;

static class SampleCalls
{
    public static void GetReportAndPrintToConsole(ServiceContext qboServiceContext, String reportName, String startDate, String endDate)
    {
        //JSON required for QBO Reports API
        qboServiceContext.IppConfiguration.Message.Response.SerializationFormat = Intuit.Ipp.Core.Configuration.SerializationFormat.Json;

        //Instantiate ReportService
        ReportService reportsService = new ReportService(qboServiceContext);

        //Set properties for Report
        reportsService.start_date = startDate;
        reportsService.end_date = endDate;

        //Execute Report API call
        Report report = reportsService.ExecuteReport(reportName);

        //Format the report data and print to the console
        PrintReportToConsole(report);
    }

    private static void PrintReportToConsole(Report report)
    {

        StringBuilder reportText = new StringBuilder();
        //Append Report Header
        PrintHeader(reportText, report);

        //Determine Maxmimum Text Lengths to format Report
        int[] maximumColumnTextSize = GetMaximumColumnTextSize(report);

        //Append Column Headers
        PrintColumnData(reportText, report.Columns, maximumColumnTextSize, 0);

        //Append Rows
        PrintRows(reportText, report.Rows, maximumColumnTextSize, 1);

        //Print Formatted Report to Console
        Console.Write(reportText.ToString());
    }

    #region " Helper Methods "

    #region " Determine Maximum Column Text Length "

    private static int[] GetMaximumColumnTextSize(Report report)
    {
        if (report.Columns == null) { return null; }
        int[] maximumColumnSize = new int[report.Columns.Count()];
        for (int columnIndex = 0; columnIndex < report.Columns.Count(); columnIndex++)
        {
            maximumColumnSize[columnIndex] = Math.Max(maximumColumnSize[columnIndex], report.Columns[columnIndex].ColTitle.Length);
        }
        return GetMaximumRowColumnTextSize(report.Rows, maximumColumnSize, 1);
    }

    private static int[] GetMaximumRowColumnTextSize(Row[] rows, int[] maximumColumnSize, int level)
    {
        for (int rowIndex = 0; rowIndex < rows.Length; rowIndex++)
        {
            Row row = rows[rowIndex];
            Header rowHeader = GetRowProperty
(row, ItemsChoiceType1.Header); if (rowHeader != null) { GetMaximumColDataTextSize(rowHeader.ColData, maximumColumnSize, level); } ColData[] colData = GetRowProperty(row, ItemsChoiceType1.ColData); if (colData != null) { GetMaximumColDataTextSize(colData, maximumColumnSize, level); } Rows nestedRows = GetRowProperty(row, ItemsChoiceType1.Rows); if (nestedRows != null) { GetMaximumRowColumnTextSize(nestedRows.Row, maximumColumnSize, level + 1); } Summary rowSummary = GetRowProperty(row, ItemsChoiceType1.Summary); if (rowSummary != null) { GetMaximumColDataTextSize(rowSummary.ColData, maximumColumnSize, level); } } return maximumColumnSize; } private static int[] GetMaximumColDataTextSize(ColData[] colData, int[] maximumColumnSize, int level) { for (int colDataIndex = 0; colDataIndex < colData.Length; colDataIndex++) { maximumColumnSize[colDataIndex] = Math.Max(maximumColumnSize[colDataIndex], (new String(' ', level * 3) + colData[colDataIndex].value).Length); } return maximumColumnSize; } #endregion #region " Print Report Sections " private static void PrintHeader(StringBuilder reportText, Report report) { const string lineDelimiter = "-----------------------------------------------------"; reportText.AppendLine(report.Header.ReportName); reportText.AppendLine(lineDelimiter); reportText.AppendLine("As of " + report.Header.StartPeriod); reportText.AppendLine(lineDelimiter); reportText.AppendLine(lineDelimiter); } private static void PrintRows(StringBuilder reportText, Row[] rows, int[] maxColumnSize, int level) { for (int rowIndex = 0; rowIndex < rows.Length; rowIndex++) { Row row = rows[rowIndex]; //Get Row Header Header rowHeader = GetRowProperty
(row, ItemsChoiceType1.Header); //Append Row Header if (rowHeader != null && rowHeader.ColData != null) { PrintColData(reportText, rowHeader.ColData, maxColumnSize, level); } //Get Row ColData ColData[] colData = GetRowProperty(row, ItemsChoiceType1.ColData); //Append ColData if (colData != null) { PrintColData(reportText, colData, maxColumnSize, level); } //Get Child Rows Rows childRows = GetRowProperty(row, ItemsChoiceType1.Rows); //Append Child Rows if (childRows != null) { PrintRows(reportText, childRows.Row, maxColumnSize, level + 1); } //Get Row Summary Summary rowSummary = GetRowProperty(row, ItemsChoiceType1.Summary); //Append Row Summary if (rowSummary != null && rowSummary.ColData != null) { PrintColData(reportText, rowSummary.ColData, maxColumnSize, level); } } } private static void PrintColData(StringBuilder reportText, ColData[] colData, int[] maxColumnSize, int level) { for (int colDataIndex = 0; colDataIndex < colData.Length; colDataIndex++) { if (colDataIndex > 0) { reportText.Append(" "); } StringBuilder rowText = new StringBuilder(); if (colDataIndex == 0) { rowText.Append(new String(' ', level * 3)); }; rowText.Append(colData[colDataIndex].value); if (rowText.Length < maxColumnSize[colDataIndex]) { rowText.Append(new String(' ', maxColumnSize[colDataIndex] - rowText.Length)); } reportText.Append(rowText.ToString()); } reportText.AppendLine(); } private static void PrintColumnData(StringBuilder reportText, Column[] columns, int[] maxColumnSize, int level) { for (int colDataIndex = 0; colDataIndex < columns.Length; colDataIndex++) { if (colDataIndex > 0) { reportText.Append(" "); } StringBuilder rowText = new StringBuilder(); if (colDataIndex == 0) { rowText.Append(new String(' ', level * 3)); }; rowText.Append(columns[colDataIndex].ColTitle); if (rowText.Length < maxColumnSize[colDataIndex]) { rowText.Append(new String(' ', maxColumnSize[colDataIndex] - rowText.Length)); } reportText.Append(rowText.ToString()); } reportText.AppendLine(); } #endregion #region " Get Row Property Helper Methods - Header, ColData, Rows (children), Summary " //Returns typed object from AnyIntuitObjects array private static T GetRowProperty(Row row, ItemsChoiceType1 itemsChoiceType) { int choiceElementIndex = GetChoiceElementIndex(row, itemsChoiceType); if (choiceElementIndex == -1) { return default(T); } else { return (T)row.AnyIntuitObjects[choiceElementIndex]; } } //Finds element index in ItemsChoiceType array private static int GetChoiceElementIndex(Row row, ItemsChoiceType1 itemsChoiceType) { if (row.ItemsElementName != null) { for (int itemsChoiceTypeIndex = 0; itemsChoiceTypeIndex < row.ItemsElementName.Count(); itemsChoiceTypeIndex++) { if (row.ItemsElementName[itemsChoiceTypeIndex] == itemsChoiceType) { return itemsChoiceTypeIndex; } } } return -1; } #endregion #endregion }