184 lines
5.4 KiB
C#
184 lines
5.4 KiB
C#
<Query Kind="Program">
|
|
<Connection>
|
|
<ID>eae63890-d1e5-4142-8086-9d954a38c63c</ID>
|
|
<Persist>true</Persist>
|
|
<Server>D0011-SQL-01.sdt.local</Server>
|
|
<Database>jazz tst Edward Elmhurst 20210127.1</Database>
|
|
<ShowServer>true</ShowServer>
|
|
</Connection>
|
|
<Namespace>System.Collections.ObjectModel</Namespace>
|
|
</Query>
|
|
|
|
void Main()
|
|
{
|
|
var fmr = new FiscalMonthResolver(from fm in DimFiscalMonths
|
|
select new FiscalMonth
|
|
{
|
|
MemberGUID = fm.MemberGUID,
|
|
FiscalMonthID = fm.FiscalMonthID,
|
|
SortOrder = fm.SortOrder,
|
|
FiscalMonthCode = int.Parse(fm.FiscalMonthCode),
|
|
Name = fm.Name,
|
|
Abbreviation = fm.Abbreviation,
|
|
DaysInMonth = fm.DaysInMonth,
|
|
YTDDaysInMonth = fm.YTDDaysInMonth,
|
|
QuarterID = fm.QuarterID,
|
|
MonthColumnName = fm.MonthColumnName,
|
|
TransactionID = fm.TransactionID,
|
|
DaysInLeapYearMonth = fm.DaysInLeapYearMonth,
|
|
YTDDaysInLeapYearMonth = fm.YTDDaysInLeapYearMonth,
|
|
IsReal = fm.IsReal
|
|
});
|
|
|
|
var config = new Configuration(fmr);
|
|
var fiscalMonths = fmr.AllFiscalMonths;
|
|
var startDate = new DateTime(2020, 1, 1);
|
|
var endDate = startDate.AddYears(1).AddDays(-1);
|
|
config.GetYearMonths(startDate, endDate).Dump();
|
|
|
|
startDate = config.GetFiscalYearStartDate();
|
|
endDate = config.GetFiscalYearEndDate();
|
|
config.GetYearMonths(startDate, endDate).Dump();
|
|
fiscalMonths.Where(fm => fm.IsReal).Dump();
|
|
}
|
|
|
|
// Define other methods and classes here
|
|
|
|
public class Configuration
|
|
{
|
|
public int FiscalYearID = 2020;
|
|
public FiscalMonthResolver FiscalMonthResolver;
|
|
|
|
public Configuration(FiscalMonthResolver fmr)
|
|
{
|
|
FiscalMonthResolver = fmr;
|
|
}
|
|
|
|
public DateTime GetFiscalYearStartDate()
|
|
{
|
|
return GetFiscalYearEndDate().AddYears(-1).AddDays(1);
|
|
}
|
|
|
|
public DateTime GetFiscalYearEndDate()
|
|
{
|
|
var lastFiscalMonth = FiscalMonthResolver.LastFiscalMonth.FiscalMonthID;
|
|
return new DateTime(FiscalYearID, lastFiscalMonth, DateTime.DaysInMonth(FiscalYearID, lastFiscalMonth));
|
|
}
|
|
|
|
public IList<YearMonth> GetYearMonths(DateTime startDate, DateTime endDate)
|
|
{
|
|
var ym = new List<YearMonth>();
|
|
var currentMonth = startDate;
|
|
$"\r\nCurrent: {currentMonth:d} - End: {endDate:d}".Dump();
|
|
while (currentMonth <= endDate)
|
|
{
|
|
var fiscalMonth = FiscalMonthResolver.ConvertToFiscalYearMonth(currentMonth);
|
|
fiscalMonth.Dump();
|
|
ym.Add(new YearMonth(fiscalMonth.Year, currentMonth.Year,
|
|
FiscalMonthResolver.GetByFiscalMonthId(currentMonth.Month)));
|
|
currentMonth = currentMonth.AddMonths(1);
|
|
}
|
|
return ym;
|
|
}
|
|
}
|
|
|
|
public interface IFiscalMonthResolver
|
|
|
|
{
|
|
FiscalMonth FirstFiscalMonth { get; }
|
|
|
|
FiscalMonth LastFiscalMonth { get; }
|
|
|
|
FiscalMonth GetByFiscalMonthId(int fiscalMonthId);
|
|
|
|
FiscalMonth GetByFiscalMonthSortOrder(int sortOrder);
|
|
|
|
List<FiscalMonth> AllFiscalMonths { get; }
|
|
}
|
|
|
|
public class FiscalMonthResolver : IFiscalMonthResolver
|
|
{
|
|
public FiscalMonthResolver(IEnumerable<FiscalMonth> fiscalMonths)
|
|
{
|
|
AllFiscalMonths = fiscalMonths.ToList();
|
|
}
|
|
|
|
public FiscalMonth FirstFiscalMonth => GetByFiscalMonthSortOrder(1);
|
|
|
|
public FiscalMonth LastFiscalMonth => GetByFiscalMonthSortOrder(12);
|
|
|
|
public List<FiscalMonth> AllFiscalMonths { get; set; }
|
|
|
|
public FiscalMonth GetByFiscalMonthId(int fiscalMonthId)
|
|
{
|
|
return AllFiscalMonths.FirstOrDefault(afm => afm.FiscalMonthID == fiscalMonthId);
|
|
}
|
|
|
|
public FiscalMonth GetByFiscalMonthSortOrder(int sortOrder)
|
|
{
|
|
return AllFiscalMonths.FirstOrDefault(afm => afm.SortOrder == sortOrder);
|
|
}
|
|
|
|
public DateTime ConvertToFiscalYearMonth(DateTime calendarYearMonth)
|
|
{
|
|
var fiscalYearID = calendarYearMonth.Month <= LastFiscalMonth.FiscalMonthID
|
|
? calendarYearMonth.Year
|
|
: calendarYearMonth.Year + 1;
|
|
return new DateTime(fiscalYearID, calendarYearMonth.Month, 1);
|
|
}
|
|
}
|
|
|
|
public class FiscalMonth
|
|
{
|
|
internal Guid MemberGUID { get; set; }
|
|
public byte FiscalMonthID { get; set; }
|
|
public byte SortOrder { get; set; }
|
|
public int FiscalMonthCode { get; set; }
|
|
public String Name { get; set; }
|
|
public String Abbreviation { get; set; }
|
|
public Int32 DaysInMonth { get; set; }
|
|
public Int32 YTDDaysInMonth { get; set; }
|
|
public byte QuarterID { get; set; }
|
|
public String MonthColumnName { get; set; }
|
|
internal String Version { get; set; }
|
|
internal Guid HistoryItemGUID { get; set; }
|
|
internal Int32 TransactionID { get; set; }
|
|
public Int32 DaysInLeapYearMonth { get; set; }
|
|
public Int32 YTDDaysInLeapYearMonth { get; set; }
|
|
public Boolean IsReal { get; set; }
|
|
|
|
}
|
|
|
|
public class YearMonth
|
|
{
|
|
public readonly int FiscalYear;
|
|
public readonly int CalendarYear;
|
|
|
|
internal readonly FiscalMonth FiscalMonth;
|
|
|
|
public readonly DateTime FiscalYearMonth;
|
|
|
|
public readonly DateTime CalendarYearMonth;
|
|
|
|
#region Helper Properties to corelate to database column names
|
|
|
|
public int FiscalYearID => FiscalYear;
|
|
|
|
/// <summary>
|
|
/// This is calendar month and FiscalMonth.SortOrder is the real FiscalMonth
|
|
/// we put it here so that we don't frikin confuse ourself between fiscalmonth and calendarmonth :x
|
|
/// </summary>
|
|
public int FiscalMonthID => FiscalMonth.FiscalMonthID;
|
|
|
|
#endregion
|
|
|
|
internal YearMonth(int fiscalYear, int calendarYear, FiscalMonth fiscalMonth)
|
|
{
|
|
FiscalYear = fiscalYear;
|
|
CalendarYear = calendarYear;
|
|
FiscalMonth = fiscalMonth;
|
|
|
|
FiscalYearMonth = new DateTime(fiscalYear, fiscalMonth.SortOrder, 1);
|
|
CalendarYearMonth = new DateTime(calendarYear, fiscalMonth.FiscalMonthID, 1);
|
|
}
|
|
} |