private static List <CalculatedClauseItem> GetSummaryCalcFields(LoginUser loginUser, SummaryReport summaryReport) { List <CalculatedClauseItem> result = new List <CalculatedClauseItem>(); ReportSubcategory sub = ReportSubcategories.GetReportSubcategory(loginUser, summaryReport.Subcategory); ReportTables tables = new ReportTables(loginUser); tables.LoadAll(); ReportTableFields tableFields = new ReportTableFields(loginUser); tableFields.LoadAll(false); TimeSpan offset = loginUser.Offset; foreach (ReportSummaryCalculatedField field in summaryReport.Fields.Calculated) { StringBuilder builder = new StringBuilder(); if (field.Field.IsCustom) { CustomField customField = (CustomField)CustomFields.GetCustomField(loginUser, field.Field.FieldID); if (customField == null) { continue; } string fieldName = DataUtils.GetReportPrimaryKeyFieldName(customField.RefType); if (fieldName != "") { fieldName = DataUtils.GetCustomFieldColumn(loginUser, customField, fieldName, true, false); if (customField.FieldType == CustomFieldType.DateTime) { fieldName = string.Format("CAST(SWITCHOFFSET(TODATETIMEOFFSET({0}, '+00:00'), '{1}{2:D2}:{3:D2}') AS DATETIME)", fieldName, offset < TimeSpan.Zero ? "-" : "+", Math.Abs(offset.Hours), Math.Abs(offset.Minutes)); } result.Add(GetCalcItem(fieldName, customField.Name, field)); } } else { ReportTableField tableField = tableFields.FindByReportTableFieldID(field.Field.FieldID); ReportTable table = tables.FindByReportTableID(tableField.ReportTableID); string fieldName = table.TableName + "." + tableField.FieldName; if (tableField.DataType.Trim().ToLower() == "datetime") { fieldName = string.Format("CAST(SWITCHOFFSET(TODATETIMEOFFSET({0}, '+00:00'), '{1}{2:D2}:{3:D2}') AS DATETIME)", fieldName, offset < TimeSpan.Zero ? "-" : "+", Math.Abs(offset.Hours), Math.Abs(offset.Minutes)); } result.Add(GetCalcItem(fieldName, tableField.Alias, field)); } } return(result); }
Dictionary <int, EField> MapFieldIdToEField() { // load all 39 reporting tables to find ReportTicketsView ReportTables tables = new ReportTables(_loginUser); tables.LoadAll(); int reportTableID = tables.Where(t => t.TableName == "ReportTicketsView").Select(t => t.ReportTableID).Min(); // Min is faster than First or Single // load all 964 and find the ReportTicketsView fields ReportTableFields tableFields = new ReportTableFields(_loginUser); tableFields.LoadAll(); var queryable = tableFields.Where(f => f.ReportTableID == reportTableID); Dictionary <int, EField> mapIdToField = new Dictionary <int, EField>(); foreach (ReportTableField row in queryable) { if (!Enum.TryParse(row.FieldName, out EField eField)) { if (System.Diagnostics.Debugger.IsAttached) { System.Diagnostics.Debugger.Break(); } } mapIdToField[row.ReportTableFieldID] = eField; } return(mapIdToField); }
private static List <DescriptiveClauseItem> GetSummaryDescFields(LoginUser loginUser, SummaryReport summaryReport) { List <DescriptiveClauseItem> result = new List <DescriptiveClauseItem>(); ReportSubcategory sub = ReportSubcategories.GetReportSubcategory(loginUser, summaryReport.Subcategory); ReportTables tables = new ReportTables(loginUser); tables.LoadAll(); ReportTableFields tableFields = new ReportTableFields(loginUser); tableFields.LoadAll(); TimeSpan offset = loginUser.Offset; TicketTypes ticketTypes = new TicketTypes(loginUser); ticketTypes.LoadByOrganizationID(loginUser.OrganizationID); foreach (ReportSummaryDescriptiveField field in summaryReport.Fields.Descriptive) { if (field.Field.IsCustom) { CustomField customField = (CustomField)CustomFields.GetCustomField(loginUser, field.Field.FieldID); if (customField == null) { continue; } string fieldName = DataUtils.GetReportPrimaryKeyFieldName(customField.RefType); if (fieldName != "") { fieldName = DataUtils.GetCustomFieldColumn(loginUser, customField, fieldName, true, false); if (customField.FieldType == CustomFieldType.DateTime) { fieldName = string.Format("CAST(SWITCHOFFSET(TODATETIMEOFFSET({0}, '+00:00'), '{1}{2:D2}:{3:D2}') AS DATETIME)", fieldName, offset < TimeSpan.Zero ? "-" : "+", Math.Abs(offset.Hours), Math.Abs(offset.Minutes)); fieldName = GetDateGroupField(fieldName, field.Value1); } string alias = customField.Name; if (customField.AuxID > 0 && customField.RefType == ReferenceType.Tickets) { TicketType ticketType = ticketTypes.FindByTicketTypeID(customField.AuxID); if (ticketType != null && ticketType.OrganizationID == customField.OrganizationID) { alias = string.Format("{1} ({2})", fieldName, customField.Name, ticketType.Name); } } result.Add(new DescriptiveClauseItem(fieldName, alias)); } } else { ReportTableField tableField = tableFields.FindByReportTableFieldID(field.Field.FieldID); ReportTable table = tables.FindByReportTableID(tableField.ReportTableID); string fieldName = table.TableName + "." + tableField.FieldName; if (tableField.DataType.Trim().ToLower() == "datetime") { fieldName = string.Format("CAST(SWITCHOFFSET(TODATETIMEOFFSET({0}, '+00:00'), '{1}{2:D2}:{3:D2}') AS DATETIME)", fieldName, offset < TimeSpan.Zero ? "-" : "+", Math.Abs(offset.Hours), Math.Abs(offset.Minutes)); fieldName = GetDateGroupField(fieldName, field.Value1); } result.Add(new DescriptiveClauseItem(fieldName, tableField.Alias)); } } return(result); }
private void GetTabluarSelectClause(SqlCommand command, StringBuilder builder, TabularReport tabularReport, bool includeHiddenFields, bool isSchemaOnly, string sortField = null, string sortDir = null) { LoginUser loginUser = _userRights._loginUser; ReportSubcategory sub = ReportSubcategories.GetReportSubcategory(loginUser, tabularReport.Subcategory); ReportTables tables = new ReportTables(loginUser); tables.LoadAll(); ReportTableFields tableFields = new ReportTableFields(loginUser); tableFields.LoadAll(); TimeSpan offset = loginUser.Offset; TicketTypes ticketTypes = new TicketTypes(loginUser); ticketTypes.LoadByOrganizationID(loginUser.OrganizationID); string sortClause = ""; foreach (ReportSelectedField field in tabularReport.Fields) { if (field.IsCustom) { CustomField customField = (CustomField)CustomFields.GetCustomField(loginUser, field.FieldID); if (customField == null) { continue; } string fieldName = DataUtils.GetReportPrimaryKeyFieldName(customField.RefType); if (fieldName != "") { //handle the ticket views custom fields if (tabularReport.Subcategory == 70) { fieldName = "UserTicketsView.TicketID"; } else if (tabularReport.Subcategory == 74) { fieldName = "TicketsView.TicketID"; } fieldName = DataUtils.GetCustomFieldColumn(loginUser, customField, fieldName, true, false); string colName = fieldName; if (customField.FieldType == CustomFieldType.DateTime) { fieldName = string.Format("CAST(SWITCHOFFSET(TODATETIMEOFFSET({0}, '+00:00'), '{1}{2:D2}:{3:D2}') AS DATETIME)", fieldName, offset < TimeSpan.Zero ? "-" : "+", Math.Abs(offset.Hours), Math.Abs(offset.Minutes)); } else if (customField.FieldType == CustomFieldType.Boolean) { fieldName = string.Format("(SELECT ISNULL(({0}),0))", fieldName); } if (!string.IsNullOrWhiteSpace(sortField) && colName == sortField) { sortClause = fieldName; } builder.Append(builder.Length < 1 ? "SELECT " : ", "); string displayName = customField.Name; if (customField.AuxID > 0 && customField.RefType == ReferenceType.Tickets) { TicketType ticketType = ticketTypes.FindByTicketTypeID(customField.AuxID); if (ticketType != null && ticketType.OrganizationID == customField.OrganizationID) { displayName = $"{customField.Name} ({ticketType.Name})"; } } builder.Append($"{fieldName} AS [{displayName}]"); if (!string.IsNullOrWhiteSpace(sortField) && displayName == sortField) { sortClause = fieldName; } } } else { ReportTableField tableField = tableFields.FindByReportTableFieldID(field.FieldID); ReportTable table = tables.FindByReportTableID(tableField.ReportTableID); string fieldName = table.TableName + "." + tableField.FieldName; if (tableField.DataType.Trim().ToLower() == "datetime") { fieldName = string.Format("CAST(SWITCHOFFSET(TODATETIMEOFFSET({0}, '+00:00'), '{1}{2:D2}:{3:D2}') AS DATETIME)", fieldName, offset < TimeSpan.Zero ? "-" : "+", Math.Abs(offset.Hours), Math.Abs(offset.Minutes)); } if (!string.IsNullOrWhiteSpace(sortField) && tableField.Alias == sortField) { sortClause = fieldName; } if (builder.Length < 1) { builder.Append("SELECT " + fieldName + " AS [" + tableField.Alias + "]"); } else { builder.Append(", " + fieldName + " AS [" + tableField.Alias + "]"); } } } if (!string.IsNullOrWhiteSpace(sortClause)) { builder.Append($", ROW_NUMBER() OVER (ORDER BY {sortClause} {sortDir}) AS [RowNum]"); } if (includeHiddenFields) { ReportTable hiddenTable = tables.FindByReportTableID(sub.ReportCategoryTableID); if (!string.IsNullOrWhiteSpace(hiddenTable.LookupKeyFieldName)) { builder.Append(string.Format(", {1}.{0} AS [hidden{0}]", hiddenTable.LookupKeyFieldName, hiddenTable.TableName)); } if (sub.ReportTableID != null) { hiddenTable = tables.FindByReportTableID((int)sub.ReportTableID); if (!string.IsNullOrWhiteSpace(hiddenTable.LookupKeyFieldName)) { builder.Append(string.Format(", {1}.{0} AS [hidden{0}]", hiddenTable.LookupKeyFieldName, hiddenTable.TableName)); } } if (tabularReport.Subcategory == 70) { string dueDateField = hiddenTable.TableName + ".DueDate"; dueDateField = string.Format("CAST(SWITCHOFFSET(TODATETIMEOFFSET({0}, '+00:00'), '{1}{2:D2}:{3:D2}') AS DATETIME)", dueDateField, offset < TimeSpan.Zero ? "-" : "+", Math.Abs(offset.Hours), Math.Abs(offset.Minutes)); builder.Append(string.Format(", {0} AS [hiddenDueDate]", dueDateField)); string dateModifiedField = hiddenTable.TableName + ".DateModified"; dateModifiedField = string.Format("CAST(SWITCHOFFSET(TODATETIMEOFFSET({0}, '+00:00'), '{1}{2:D2}:{3:D2}') AS DATETIME)", dateModifiedField, offset < TimeSpan.Zero ? "-" : "+", Math.Abs(offset.Hours), Math.Abs(offset.Minutes)); builder.Append(string.Format(", {0} AS [hiddenDateModified]", dateModifiedField)); builder.Append(string.Format(", {1}.{0} AS [hidden{0}]", "SlaWarningTime", hiddenTable.TableName)); builder.Append(string.Format(", {1}.{0} AS [hidden{0}]", "SlaViolationTime", hiddenTable.TableName)); builder.Append(string.Format(", {1}.{0} AS [hidden{0}]", "IsRead", hiddenTable.TableName)); builder.Append(string.Format(", {1}.{0} AS [hidden{0}]", "IsClosed", hiddenTable.TableName)); builder.Append(string.Format(", {1}.{0} AS [hidden{0}]", "TicketTypeID", hiddenTable.TableName)); builder.Append(string.Format(", {1}.{0} AS [hidden{0}]", "UserID", hiddenTable.TableName)); builder.Append(string.Format(", {1}.{0} AS [hidden{0}]", "SeverityPosition", hiddenTable.TableName)); builder.Append(string.Format(", {1}.{0} AS [hidden{0}]", "StatusPosition", hiddenTable.TableName)); } } builder.Append(" " + sub.BaseQuery); ReportTable mainTable = tables.FindByReportTableID(sub.ReportCategoryTableID); _organizationIDFieldName = mainTable.OrganizationIDFieldName; builder.Append(" WHERE (" + mainTable.TableName + "." + mainTable.OrganizationIDFieldName + " = @OrganizationID)"); if (tabularReport.Subcategory == 70) { builder.Append(" AND (" + mainTable.TableName + ".ViewerID = @UserID)"); } _userRights.UseTicketRights((int)tabularReport.Subcategory, tables, command, builder); if (isSchemaOnly) { builder.Append(" AND (0=1)"); } }