private void CreateReport(string reportName, IEnumerable <Doc> docs, BizTableReport reportData) { // adding page header var pageHeaderString = reportData.PageHeaders.Aggregate("", (current, pageHeader) => current + pageHeader + "\n"); _report.AddPageHeader(pageHeaderString, Position.Centre); // adding page footer var pageFooterString = reportData.PageFooters.Aggregate("", (current, pageFooter) => current + pageFooter + "\n"); _report.AddPageFooter(pageFooterString, Position.Centre); // adding empty rows for logo _report.AddEmptyRows(6); // report header foreach (string reportHeader in reportData.ReportHeaders) { var headerRow = _report.GetNextRow(); _report.AddCell(reportHeader, 0, headerRow, TextStyle.Header2); } // Report table Row row = _report.GetNextRow(); _report.AddCell(reportName, 0, row, TextStyle.Header1); _report.SetRowHeight(row, 22); if (reportData.ReportDetails != null) { foreach (BizReportDetail repDetail in reportData.ReportDetails) { int rowShift = _report.CurrentRowIndex + 2; for (int rowIndex = 0; rowIndex < repDetail.ChildrenCountLevels; rowIndex++) { for (int columnIndex = 0; columnIndex < repDetail.LeafCount; columnIndex++) { _report.AddCell("", columnIndex, rowShift + rowIndex, TextStyle.TableHeaderGreyCenterdBorder); } } WriteReportDetail(repDetail, rowShift - 1, repDetail.ChildrenCountLevels, 0); foreach (Doc doc in docs) { Row tableRow = _report.GetNextRow(); int colIndex = 0; foreach (ReportColumn col in GetColumns(repDetail)) { Guid attrId = col.AttributeId; IEnumerable <AttributeBase> findAttrQuery = from a in doc.Attributes where a.AttrDef.Id == attrId select a; if (findAttrQuery.Any()) { AttributeBase findedAttr = findAttrQuery.First(); if (findedAttr is EnumAttribute) { var enumAttribute = findedAttr as EnumAttribute; if (enumAttribute.Value.HasValue) { string enumValue = _enumRepository.GetEnumValue(enumAttribute.AttrDef.EnumDefType.Id, enumAttribute.Value.Value); _report.AddCell(enumValue, colIndex, tableRow, TextStyle.TableRowNormal); } } else { _report.AddCell(findedAttr.ObjectValue, colIndex, tableRow, TextStyle.TableRowNormal); } } else { _report.AddCell("", colIndex, tableRow, TextStyle.TableRowNormal); } colIndex++; } } Row aggregatesRow = _report.GetNextRow(); int aggrColIndex = 0; foreach (ReportColumn col in GetColumns(repDetail)) { /* atr.AttrDef.Type.Id * 1 Int * 2 Currency * 3 Text * 4 Float * 5 Enum * 6 Doc * 7 DocList * 8 Bool * 9 DateTime */ Guid attrId = col.AttributeId; IEnumerable <AttributeBase> attrQuery = from atr in docs.SelectMany(d => d.Attributes) where atr.AttrDef.Id == attrId select atr; object aggregateValue; switch (col.AggregateOperation) { case AggregateOperation.Count: aggregateValue = attrQuery.Count(); break; case AggregateOperation.Avg: var q1 = attrQuery.Where(atr => (new[] { 1, 2, 4 }).Contains(atr.AttrDef.Type.Id)); aggregateValue = q1.Any() ? q1.Average(a => double.Parse((a.ObjectValue ?? 0).ToString())) : 0; break; case AggregateOperation.Sum: var q2 = attrQuery.Where(atr => (new[] { 1, 2, 4 }).Contains(atr.AttrDef.Type.Id)); aggregateValue = q2.Any() ? q2.Sum(a => double.Parse((a.ObjectValue ?? 0).ToString())) : 0; break; case AggregateOperation.Max: aggregateValue = attrQuery.Where(atr => (new[] { 1, 2, 4, 9 }).Contains(atr.AttrDef.Type.Id)).Max( a => a.ObjectValue); break; case AggregateOperation.Min: aggregateValue = attrQuery.Where(atr => (new[] { 1, 2, 4, 9 }).Contains(atr.AttrDef.Type.Id)).Min( a => a.ObjectValue); break; default: aggregateValue = ""; break; } _report.AddCell(aggrColIndex == 0 ? "Итог" : aggregateValue, aggrColIndex, aggregatesRow, TextStyle.TableTotal); aggrColIndex++; } } } _report.AddEmptyRow(); // report footer foreach (string reportFooter in reportData.ReportFooters) { var headerRow = _report.GetNextRow(); _report.AddCell(reportFooter, 0, headerRow, TextStyle.Header2); } // report logo // adding logo after all other operations, because it can be resized. _report.AddLogo(0, 0); }
private void CreateReport(string reportName, IEnumerable <Doc> docs, Doc docTemplate) { _report.AddEmptyRows(6); Row row = _report.GetNextRow(); _report.AddCell(reportName, 0, row, TextStyle.Header1); _report.SetRowHeight(row, 22); if (docs == null) { return; } var enumerable = docs as Doc[] ?? docs.ToArray(); if (!enumerable.Any()) { return; } Row rowHeader = _report.GetNextRow(); int columnIndex = 0; foreach (AttributeBase attribute in docTemplate.Attributes) { AttributeBase attribute1 = attribute; _report.AddCell(attribute1.AttrDef.Name, columnIndex, rowHeader, TextStyle.TableHeaderGreyCenterdBorder); int colWidth = enumerable.SelectMany(d => d.Attributes) .Where(a => a.AttrDef.Id == attribute1.AttrDef.Id && a.ObjectValue != null) .Select(aa => aa.ObjectValue.ToString().Length).Max(); _report.SetColumnWidth(columnIndex, colWidth); columnIndex++; } foreach (Doc doc in enumerable) { var tableRow = _report.GetNextRow(); var colIndex = 0; foreach (var attr in docTemplate.Attributes) { var attr1 = attr; var findAttrQuery = (from a in doc.Attributes where a.AttrDef.Id == attr1.AttrDef.Id select a).ToList(); if (findAttrQuery.Any()) { var findedAttr = findAttrQuery.First(); if (findedAttr is EnumAttribute) { var enumAttribute = findedAttr as EnumAttribute; if (enumAttribute.Value.HasValue) { string enumValue = _enumRepository.GetEnumValue(enumAttribute.AttrDef.EnumDefType.Id, enumAttribute.Value.Value); _report.AddCell(enumValue, colIndex, tableRow, TextStyle.NormalText); } } else { _report.AddCell(findedAttr.ObjectValue, colIndex, tableRow, TextStyle.NormalText); } } colIndex++; } } _report.AddLogo(0, 0); }