/// <summary> /// Consider calling <see>report</see>.<see cref="InstanceReport.SortColumns"/> first. Merges 1) non-monetary units and 2) monetary units of the same currency and 3) merges them under the same calendar/segments if possible. /// </summary> /// <param name="report">The report whose columns will be merged.</param> /// <returns>True on success, false on fail.</returns> private static bool MergeColumns(InstanceReport report) { if (report == null) { return(false); } bool hasMergedColumns = false; for (int c = 0; c < report.Columns.Count - 1; c++) { InstanceReportColumn curColumn = report.Columns[c]; InstanceReportColumn nextColumn = report.Columns[c + 1]; if (!curColumn.ReportingPeriodEquals(nextColumn) || !curColumn.SegmentAndScenarioEquals(nextColumn)) { continue; } bool curHasCurrency = !string.IsNullOrEmpty(curColumn.CurrencyCode); if (curHasCurrency) { bool nextHasCurrency = !string.IsNullOrEmpty(nextColumn.CurrencyCode); if (nextHasCurrency) { if (!string.Equals(curColumn.CurrencyCode, nextColumn.CurrencyCode)) { continue; } } } bool columnValuesAreUnique = true; foreach (InstanceReportRow row in report.Rows) { if (row.Cells[c].HasData && row.Cells[c + 1].HasData) { columnValuesAreUnique = false; break; } } if (columnValuesAreUnique) { //since we have unique columns, merge them and remove the column + cells hasMergedColumns = true; foreach (InstanceReportRow row in report.Rows) { if (!row.Cells[c].HasData && row.Cells[c + 1].HasData) { row.Cells[c].AddData(row.Cells[c + 1]); } } //please don't move this up ;-) report.RemoveColumn(c + 1); c--; curColumn.Units.AddRange(nextColumn.Units); if (string.IsNullOrEmpty(curColumn.CurrencyCode) && !string.IsNullOrEmpty(nextColumn.CurrencyCode)) { curColumn.CurrencyCode = nextColumn.CurrencyCode; curColumn.CurrencySymbol = nextColumn.CurrencySymbol; curColumn.MCU.CurrencyCode = nextColumn.MCU.CurrencyCode; curColumn.MCU.CurrencySymbol = nextColumn.MCU.CurrencySymbol; //this is probably the currency label - since we didn't have a currency, we didn't have the label //copy the reference to this label over LabelLine last = nextColumn.Labels[nextColumn.Labels.Count - 1]; last.Id = curColumn.Labels.Count; curColumn.Labels.Add(last); } } } return(hasMergedColumns); }
/// <summary> /// Consider calling <see>report</see>.<see cref="InstanceReport.SortColumns"/> first. Merges 1) non-monetary units and 2) monetary units of the same currency and 3) merges them under the same calendar/segments if possible. /// </summary> /// <param name="report">The report whose columns will be merged.</param> /// <returns>True on success, false on fail.</returns> private static bool MergeColumns( InstanceReport report ) { if( report == null ) return false; bool hasMergedColumns = false; for( int c = 0; c < report.Columns.Count - 1; c++ ) { InstanceReportColumn curColumn = report.Columns[ c ]; InstanceReportColumn nextColumn = report.Columns[ c + 1 ]; if( !curColumn.ReportingPeriodEquals( nextColumn ) || !curColumn.SegmentAndScenarioEquals( nextColumn ) ) continue; bool curHasCurrency = !string.IsNullOrEmpty( curColumn.CurrencyCode ); if( curHasCurrency ) { bool nextHasCurrency = !string.IsNullOrEmpty( nextColumn.CurrencyCode ); if( nextHasCurrency ) { if( !string.Equals( curColumn.CurrencyCode, nextColumn.CurrencyCode ) ) continue; } } bool columnValuesAreUnique = true; foreach( InstanceReportRow row in report.Rows ) { if( row.Cells[ c ].HasData && row.Cells[ c + 1 ].HasData ) { columnValuesAreUnique = false; break; } } if( columnValuesAreUnique ) { //since we have unique columns, merge them and remove the column + cells hasMergedColumns = true; foreach( InstanceReportRow row in report.Rows ) { if( !row.Cells[ c ].HasData && row.Cells[ c + 1 ].HasData ) row.Cells[ c ].AddData( row.Cells[ c + 1 ] ); } //please don't move this up ;-) report.RemoveColumn( c + 1 ); c--; curColumn.Units.AddRange( nextColumn.Units ); if( string.IsNullOrEmpty( curColumn.CurrencyCode ) && !string.IsNullOrEmpty( nextColumn.CurrencyCode ) ) { curColumn.CurrencyCode = nextColumn.CurrencyCode; curColumn.CurrencySymbol = nextColumn.CurrencySymbol; curColumn.MCU.CurrencyCode = nextColumn.MCU.CurrencyCode; curColumn.MCU.CurrencySymbol = nextColumn.MCU.CurrencySymbol; //this is probably the currency label - since we didn't have a currency, we didn't have the label //copy the reference to this label over LabelLine last = nextColumn.Labels[ nextColumn.Labels.Count - 1 ]; last.Id = curColumn.Labels.Count; curColumn.Labels.Add( last ); } } } return hasMergedColumns; }