Esempio n. 1
0
        public XDocument GetReport(long masterreportId, List <ReportXMLPrintParameterModel> filters)
        {
            this.masterReport_Id = masterreportId;

            this.repo = new BuildReportRepository();

            XDocument result = this.repo.GetReportXml(this.masterReport_Id);

            ReportMaster reportMaster = this.repo.GetReportMaster(this.masterReport_Id);

            return(this.GetReport(result, null, reportMaster, filters));
        }
Esempio n. 2
0
        public XDocument GetReport(
            XDocument xmlReport,
            DatabaseModel connection,
            ReportMaster reportMaster,
            List <ReportXMLPrintParameterModel> filters)
        {
            if (filters.Count > 0 && filters.Any(p => p.IsRequired.IsTrue() && p.FilterValue.IsNullEmptyOrWhiteSpace()))
            {
                throw new ApplicationException("One or more required filters was not passed with a value. Report Print canceled.");
            }

            if (this.repo == null)
            {
                this.repo = new BuildReportRepository();
            }

            if (filters.Count == 0)
            {
                this.tableFilters = new Dictionary <string, List <ReportXMLPrintParameterModel> >();
            }
            else
            {
                this.tableFilters = filters
                                    .GroupBy(g => g.TableName)
                                    .ToDictionary(t => t.Key, c => c.ToList());
            }

            this.dataAccessConnection = connection;

            Dictionary <string, XElement[]> sqlOrderBy = xmlReport
                                                         .Root
                                                         .Descendants("ReportObject")
                                                         .Where(ro => ro.IsDataObject() && ro.Attribute("UseInOrderBy").Value == "true")
                                                         .GroupBy(tn => tn.Attribute("ObjectTable").Value)
                                                         .ToDictionary(td => td.Key, td => td.ToArray());

            Dictionary <string, string> orderByDictionary = new Dictionary <string, string>();

            foreach (KeyValuePair <string, XElement[]> orderKey in sqlOrderBy)
            {
                string[] orderString = orderKey.Value
                                       .Select(x => x.Attribute("ObjectColumn").Value)
                                       .ToArray();

                this.orderByDictionary.Add(orderKey.Key, orderString.Concatenate(","));
            }

            string reportTypeValue = xmlReport.Root.Element("ReportSettings").Attribute("ReportTypeEnum").Value;

            #region BUILD REPORT DATA

            ReportTypeEnum reportType = (ReportTypeEnum)reportTypeValue.ToInt32();

            if (reportType == ReportTypeEnum.ReportContent)
            {
                this.CreateConnectionObject();

                XElement dataNode = new XElement("ReportData");

                this.dataSections = xmlReport.Root
                                    .Descendants("ReportSection")
                                    .Where(st => st.Attribute("SectionType").Value == "5") // SectionTypeEnum.TableData
                                    .ToDictionary(d => d.Attribute("SectionIndex").Value.ToInt32());

                this.sectionIndex = 1;

                this.DoSectionSetup();

                this.BuildReportData(dataNode);

                xmlReport.Root.Add(dataNode);
            }

            #endregion

            foreach (XElement reportSum in xmlReport.Root.Descendants().Where(a => a.Attribute("ObjectType") != null &&
                                                                              a.Attribute("ObjectType").Value == "ReportSum"))
            {
                int parentSetionGroupIndex = reportSum.Attribute("ParentSectionGroupIndex").Value.ToInt32();

                int sectionGroupIndex = reportSum.Attribute("SectionGroupIndex").Value.ToInt32();

                string sumColumn = reportSum.Attribute("SumColumn").Value;

                if (sumColumn.IsNullEmptyOrWhiteSpace())
                {                   // The user did not perfom a full setup
                    continue;
                }

                foreach (XElement tableRow in xmlReport.Root
                         .Element("ReportData")
                         .Descendants()
                         .Where(pg => pg.Attribute("SectionGroupIndex") != null &&
                                pg.Attribute("SectionGroupIndex").Value.ToInt32() == parentSetionGroupIndex))
                {
                    IEnumerable <XElement> sumItems = parentSetionGroupIndex == sectionGroupIndex?
                                                      tableRow
                                                      .Elements()
                                                      .Elements()
                                                      .Where(ln => ln.Name.LocalName == sumColumn)
                                                          :
                                                          tableRow
                                                          .Descendants()
                                                          .Where(ln => ln.Name.LocalName == sumColumn &&
                                                                 ln.Parent.Parent.Attribute("SectionGroupIndex") != null &&
                                                                 ln.Parent.Parent.Attribute("SectionGroupIndex").Value.ToInt32() == sectionGroupIndex);

                    decimal result = 0;

                    if (sumItems.Count() != 0)
                    {
                        result = sumItems.Sum(si =>
                        {
                            decimal?innerResult = si.Value.TryToDecimal().Value;

                            return(innerResult.HasValue ? innerResult.Value : 0);
                        });
                    }

                    XElement sumElemet = new XElement(sumColumn);

                    sumElemet.Add(new XAttribute("Value", result));

                    sumElemet.Add(new XAttribute("TableRowIndex", tableRow.Attribute("TableRowIndex").Value));

                    reportSum.Add(sumElemet);
                }
            }

            #region PAGE OPTIONS

            if (reportMaster.FinalPage_Id.HasValue && reportMaster.FinalPage_Id.Value > 0)
            {
                XDocument finalPage = this.repo.GetReportXml(reportMaster.FinalPage_Id.Value);

                if (finalPage != null)
                {
                    xmlReport.Root.AddFirst(finalPage.Root.Elements());
                }
            }

            if (reportMaster.HeaderAndFooterPage_Id.HasValue && reportMaster.HeaderAndFooterPage_Id.Value > 0)
            {
                XDocument headerAndFooterPage = this.repo.GetReportXml(reportMaster.HeaderAndFooterPage_Id.Value);

                if (headerAndFooterPage != null)
                {
                    xmlReport.Root.AddFirst(headerAndFooterPage.Root.Elements());
                }
            }

            if (reportMaster.CoverPage_Id.HasValue && reportMaster.CoverPage_Id.Value > 0)
            {
                XDocument coverPage = this.repo.GetReportXml(reportMaster.CoverPage_Id.Value);

                if (coverPage != null)
                {
                    xmlReport.Root.AddFirst(coverPage.Root.Elements());
                }
            }

            #endregion

            #region PAGE SETUP

            XElement pageSetup = new XElement("PageSetup");

            pageSetup.Add(new XAttribute("PaperKindEnum", reportMaster.PaperKindEnum));
            pageSetup.Add(new XAttribute("PageOrientationEnum", reportMaster.PageOrientationEnum));
            pageSetup.Add(new XAttribute("PageMarginBottom", reportMaster.PageMarginBottom));
            pageSetup.Add(new XAttribute("PageMarginLeft", reportMaster.PageMarginLeft));
            pageSetup.Add(new XAttribute("PageMarginRight", reportMaster.PageMarginRight));
            pageSetup.Add(new XAttribute("PageMarginTop", reportMaster.PageMarginTop));

            xmlReport.Root.AddFirst(pageSetup);

            #endregion

            this.repo.UpdateXmlPrinteCount(reportMaster.MasterReport_Id);

            this.RunUpdateStements(xmlReport);

            return(xmlReport);
        }