using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Globalization; using System.Linq; using System.Web; using EnVisage.Code; namespace EnVisage.Models { public class FiscalCalendarModel : IBaseModel { #region Classes and enums /// /// Status of the Calendar Settings record /// public enum FiscalCalendarSettingStatus { /// /// NoValue = 0. /// [DisplayValue("No Value")] NoValue = 0, /// /// Inactive = 0. /// [DisplayValue("Inactive")] Inactive = 1, /// /// Active = 1. /// [DisplayValue("Active")] Active = 2, /// /// Inactive = 2. /// [DisplayValue("Future")] Future = 3, } /// /// Represents a fiscal calendar type. /// public enum FiscalCalendarType { /// /// Calendar Year = 0. /// [DisplayValue("Calendar Year")] CalendarYear = 0, /// /// 4-4-5 weeks calendar where each quarter contains of 3 months with 4, 4 and 5 weeks. /// [DisplayValue("4,4,5 weeks Calendar")] Calendar445 = 1, /// /// 4-5-4 weeks calendar where each quarter contains of 3 months with 4, 5 and 4 weeks. /// [DisplayValue("4,5,4 weeks Calendar")] Calendar454 = 2, /// /// 5-4-4 weeks calendar where each quarter contains of 3 months with 5, 4 and 4 weeks. /// [DisplayValue("5,4,4 weeks Calendar")] Calendar544 = 3, } /// /// Represents a way of ending the calendar year and distribution of weeks between quarters. /// public enum CalendarYearType { [DisplayValue("52 weeks only")] Only52Weeks = 0, [DisplayValue("Last Weekending day of the month")] LastWeekDay = 1, [DisplayValue("Standard Year")] StandardYear = 2 } /// /// Represents a fiscal year record type. /// public enum FiscalYearType { Week = 0, Month = 1, Quarter = 2, Year = 3 } #endregion #region Constructors public FiscalCalendarModel() { Id = Guid.Empty; Status = FiscalCalendarSettingStatus.NoValue; CreatedAt = DateTime.UtcNow; } #endregion #region Properties public Guid Id { get; set; } // TypeSettingId [Required] [Display(Name = "Calendar Type")] public FiscalCalendarType? CalendarType { get; set; } // Type [Required] [DataType(DataType.Date), DisplayFormat(DataFormatString = "{0:MM/dd/yy}", ApplyFormatInEditMode = true)] [Display(Name = "First Calendar Day")] public DateTime? StartingPoint { get; set; } // CurrentYearStartDate [Required] [Display(Name = "Week Ending")] public DayOfWeek? WeekendingDay { get; set; } // WeekEnding [Required] [Display(Name = "Year Type")] public CalendarYearType? YearType { get; set; } // WeekEndingType [Display(Name = "Year Type")] public string YearTypeAsText { get; set; } [Required] [Display(Name = "Adjust Period")] public bool UseAdjustingPeriod { get; set; } // Adjustingperiod [Required] [DataType(DataType.Date), DisplayFormat(DataFormatString = "{0:MM/dd/yy}", ApplyFormatInEditMode = true)] [Display(Name = "Effective Date of Change")] public DateTime? EffectiveChangeDate { get; set; } [DataType(DataType.Date), DisplayFormat(DataFormatString = "{0:MM/dd/yy}", ApplyFormatInEditMode = true)] [Display(Name = "Valid To")] public DateTime? ValidTo { get; set; } public DateTime CreatedAt { get; set; } [Display(Name = "Status")] public FiscalCalendarSettingStatus Status { get; set; } #endregion #region Methods /// /// Validates an object and returns a collection that holds failed-validation information. /// /// /// A collection that holds failed-validation information. /// /// The validation context. public IEnumerable Validate(ValidationContext validationContext) { switch (CalendarType) { case FiscalCalendarType.CalendarYear: yield break; default: if (this.YearType == null) yield return new ValidationResult(string.Format(Constants.ERROR_TEMPLATE_REQUIRED, "Year Type"), new[] { "YearType" }); if (this.StartingPoint == null) yield return new ValidationResult(string.Format(Constants.ERROR_TEMPLATE_REQUIRED, "Current Year Start Date"), new[] { "StartDate" }); break; } } #endregion #region Conversions /// /// Casts a obect to the object of type . /// /// A object. /// A object filled with data from db. public static explicit operator FiscalCalendarModel(FiscalCalendarSetting obj) { if (obj == null) return null; var model = new FiscalCalendarModel { Id = obj.Id, CalendarType = (FiscalCalendarType)obj.CalendarType, WeekendingDay = (DayOfWeek)obj.WeekendingDay, YearType = (CalendarYearType)obj.YearType, StartingPoint = obj.StartingPoint, EffectiveChangeDate = obj.EffectiveChangeDate, UseAdjustingPeriod = obj.UseAdjustingPeriod, CreatedAt = obj.CreatedAt, }; model.TrimStringProperties(); if (model.YearType.HasValue) { if ((model.YearType.Value == FiscalCalendarModel.CalendarYearType.LastWeekDay) && (model.WeekendingDay.HasValue)) model.YearTypeAsText = FiscalCalendarModel.CalendarYearType.LastWeekDay.ToDisplayValue().Replace("Weekending day", model.WeekendingDay.Value.ToDisplayValue()); else model.YearTypeAsText = model.YearType.Value.ToDisplayValue(); } else { model.YearTypeAsText = String.Empty; } return model; } /// /// Copies data from model to DAL object. /// /// A target DAL object. public void CopyTo(FiscalCalendarSetting obj) { if (obj == null) throw new ArgumentNullException(); if (!EffectiveChangeDate.HasValue) throw new Exception("Effective Change Date must be set"); if (!CalendarType.HasValue) throw new Exception("Calendar Type must be set"); if (!YearType.HasValue) throw new Exception("Year Type must be set"); obj.EffectiveChangeDate = EffectiveChangeDate.Value; obj.CalendarType = (short)CalendarType.Value; obj.YearType = (short)YearType.Value; obj.UseAdjustingPeriod = UseAdjustingPeriod; obj.CreatedAt = CreatedAt; if (CalendarType == FiscalCalendarModel.FiscalCalendarType.CalendarYear) { obj.StartingPoint = new DateTime(EffectiveChangeDate.Value.Year, 1, 1); obj.WeekendingDay = (short)DayOfWeek.Sunday; } else { if (!StartingPoint.HasValue) throw new Exception("Starting Point must be set"); if (!WeekendingDay.HasValue) throw new Exception("Weekending Day must be set"); obj.StartingPoint = StartingPoint.Value; obj.WeekendingDay = (short)WeekendingDay.Value; } } public static FiscalCalendarModel GetModelFor(FiscalCalendarSetting obj) { return (FiscalCalendarModel)obj; } #endregion } public class FiscalCalendarListModel { public FiscalCalendarListModel() { Items = new List(); } public List Items { get; set; } } }