Exemple #1
0
        ///<summary>Submits the queries to the database and makes query objects for each query with the results.  Returns false if one of the queries failed.</summary>
        public bool SubmitQueries(bool isShowMessage = false)
        {
            bool     hasRows         = false;
            bool     hasReportServer = !string.IsNullOrEmpty(PrefC.ReportingServer.DisplayStr);
            Graphics grfx            = Graphics.FromImage(new Bitmap(1, 1));
            string   displayText;
            ReportObjectCollection newReportObjects = new ReportObjectCollection();

            _sections.Add(new Section(AreaSectionType.Query, 0));
            for (int i = 0; i < _reportObjects.Count; i++)
            {
                if (_reportObjects[i].ObjectType == ReportObjectType.QueryObject)
                {
                    QueryObject query = (QueryObject)_reportObjects[i];
                    if (!query.SubmitQuery())
                    {
                        _actionCloseReportProgress?.Invoke();
                        MsgBox.Show(this, "There was an error generating this report."
                                    + (hasReportServer ? "\r\nVerify or remove the report server connection settings and try again." : ""));
                        return(false);
                    }
                    if (query.ReportTable.Rows.Count == 0)
                    {
                        continue;
                    }
                    hasRows    = true;
                    TotalRows += query.ReportTable.Rows.Count;
                    //Check if the query needs to be split up into sub queries.  E.g. one payment report query split up via payment type.
                    if (!String.IsNullOrWhiteSpace(query.ColumnNameToSplitOn))
                    {
                        ReportComplexEvent.Fire(ODEventType.ReportComplex, Lan.g("ReportComplex", "Creating Splits Based On") + " " + query.ColumnNameToSplitOn + "...");
                        //The query needs to be split up into sub queries every time the ColumnNameToSplitOn cell changes.
                        //Therefore, we need to create a separate QueryObject for every time the cell value changes.
                        string lastCellValue = "";
                        query.IsLastSplit = false;
                        QueryObject newQuery = null;
                        for (int j = 0; j < query.ReportTable.Rows.Count; j++)
                        {
                            if (query.ReportTable.Rows[j][query.ColumnNameToSplitOn].ToString() == lastCellValue)
                            {
                                if (newQuery == null)
                                {
                                    newQuery = query.DeepCopyQueryObject();
                                    newQuery.AddInitialHeader(newQuery.GetGroupTitle().StaticText, newQuery.GetGroupTitle().Font);
                                }
                                newQuery.ReportTable.ImportRow(query.ReportTable.Rows[j]);
                            }
                            else
                            {
                                //Must happen the first time through
                                if (newQuery != null)
                                {
                                    switch (newQuery.SplitByKind)
                                    {
                                    case SplitByKind.None:
                                        return(false);

                                    case SplitByKind.Enum:
                                        if (newQuery.ListEnumNames == null)
                                        {
                                            return(false);
                                        }
                                        displayText = newQuery.ListEnumNames[PIn.Int(newQuery.ReportTable.Rows[0][query.ColumnNameToSplitOn].ToString())];
                                        newQuery.GetGroupTitle().Size       = new Size((int)(grfx.MeasureString(displayText, newQuery.GetGroupTitle().Font).Width / grfx.DpiX * 100 + 2), (int)(grfx.MeasureString(displayText, newQuery.GetGroupTitle().Font).Height / grfx.DpiY * 100 + 2));
                                        newQuery.GetGroupTitle().StaticText = displayText;
                                        break;

                                    case SplitByKind.Definition:
                                        if (newQuery.DictDefNames == null)
                                        {
                                            return(false);
                                        }
                                        if (newQuery.DictDefNames.ContainsKey(PIn.Long(newQuery.ReportTable.Rows[0][query.ColumnNameToSplitOn].ToString())))
                                        {
                                            displayText = newQuery.DictDefNames[PIn.Long(newQuery.ReportTable.Rows[0][query.ColumnNameToSplitOn].ToString())];
                                            newQuery.GetGroupTitle().Size       = new Size((int)(grfx.MeasureString(displayText, newQuery.GetGroupTitle().Font).Width / grfx.DpiX * 100 + 2), (int)(grfx.MeasureString(displayText, newQuery.GetGroupTitle().Font).Height / grfx.DpiY * 100 + 2));
                                            newQuery.GetGroupTitle().StaticText = displayText;
                                        }
                                        else
                                        {
                                            newQuery.GetGroupTitle().StaticText = "Undefined";
                                        }
                                        break;

                                    case SplitByKind.Date:
                                        displayText = PIn.Date(newQuery.ReportTable.Rows[0][query.ColumnNameToSplitOn].ToString()).ToShortDateString();
                                        newQuery.GetGroupTitle().StaticText = displayText;
                                        newQuery.GetGroupTitle().Size       = new Size((int)(grfx.MeasureString(displayText, newQuery.GetGroupTitle().Font).Width / grfx.DpiX * 100 + 2), (int)(grfx.MeasureString(displayText, newQuery.GetGroupTitle().Font).Height / grfx.DpiY * 100 + 2));
                                        break;

                                    case SplitByKind.Value:
                                        displayText = newQuery.ReportTable.Rows[0][query.ColumnNameToSplitOn].ToString();
                                        newQuery.GetGroupTitle().StaticText = displayText;
                                        newQuery.GetGroupTitle().Size       = new Size((int)(grfx.MeasureString(displayText, newQuery.GetGroupTitle().Font).Width / grfx.DpiX * 100 + 2), (int)(grfx.MeasureString(displayText, newQuery.GetGroupTitle().Font).Height / grfx.DpiY * 100 + 2));
                                        break;
                                    }
                                    newQuery.SubmitQuery();
                                    newReportObjects.Add(newQuery);
                                }
                                if (newQuery == null && query.GetGroupTitle().StaticText != "")
                                {
                                    newQuery = query.DeepCopyQueryObject();
                                    newQuery.ReportTable.ImportRow(query.ReportTable.Rows[j]);
                                    newQuery.AddInitialHeader(newQuery.GetGroupTitle().StaticText, newQuery.GetGroupTitle().Font);
                                }
                                else
                                {
                                    newQuery = query.DeepCopyQueryObject();
                                    newQuery.ReportTable.ImportRow(query.ReportTable.Rows[j]);
                                }
                            }
                            lastCellValue = query.ReportTable.Rows[j][query.ColumnNameToSplitOn].ToString();
                        }
                        switch (newQuery.SplitByKind)
                        {
                        case SplitByKind.None:
                            return(false);

                        case SplitByKind.Enum:
                            if (newQuery.ListEnumNames == null)
                            {
                                return(false);
                            }
                            displayText = newQuery.ListEnumNames[PIn.Int(newQuery.ReportTable.Rows[0][query.ColumnNameToSplitOn].ToString())];
                            newQuery.GetGroupTitle().Size       = new Size((int)(grfx.MeasureString(displayText, newQuery.GetGroupTitle().Font).Width / grfx.DpiX * 100 + 2), (int)(grfx.MeasureString(displayText, newQuery.GetGroupTitle().Font).Height / grfx.DpiY * 100 + 2));
                            newQuery.GetGroupTitle().StaticText = displayText;
                            break;

                        case SplitByKind.Definition:
                            if (newQuery.DictDefNames == null)
                            {
                                return(false);
                            }
                            if (newQuery.DictDefNames.ContainsKey(PIn.Long(newQuery.ReportTable.Rows[0][query.ColumnNameToSplitOn].ToString())))
                            {
                                displayText = newQuery.DictDefNames[PIn.Long(newQuery.ReportTable.Rows[0][query.ColumnNameToSplitOn].ToString())];
                                newQuery.GetGroupTitle().Size       = new Size((int)(grfx.MeasureString(displayText, newQuery.GetGroupTitle().Font).Width / grfx.DpiX * 100 + 2), (int)(grfx.MeasureString(displayText, newQuery.GetGroupTitle().Font).Height / grfx.DpiY * 100 + 2));
                                newQuery.GetGroupTitle().StaticText = displayText;
                            }
                            else
                            {
                                newQuery.GetGroupTitle().StaticText = Lans.g(this, "Undefined");
                            }
                            break;

                        case SplitByKind.Date:
                            newQuery.GetGroupTitle().StaticText = PIn.Date(newQuery.ReportTable.Rows[0][query.ColumnNameToSplitOn].ToString()).ToShortDateString();
                            break;

                        case SplitByKind.Value:
                            newQuery.GetGroupTitle().StaticText = newQuery.ReportTable.Rows[0][query.ColumnNameToSplitOn].ToString();
                            break;
                        }
                        newQuery.SubmitQuery();
                        newQuery.IsLastSplit = true;
                        newReportObjects.Add(newQuery);
                    }
                    else
                    {
                        newReportObjects.Add(_reportObjects[i]);
                    }
                }
                else
                {
                    newReportObjects.Add(_reportObjects[i]);
                }
            }
            if (!hasRows && isShowMessage)
            {
                _actionCloseReportProgress?.Invoke();
                MsgBox.Show(this, "The report has no results to show.");
                return(false);
            }
            _reportObjects = newReportObjects;
            return(true);
        }