EnVisageOnline/Main/Source/EnVisage/Models/FiscalCalendarExModel.cs

275 lines
7.4 KiB
C#

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<FiscalCalendarSetting>
{
#region Classes and enums
/// <summary>
/// Status of the Calendar Settings record
/// </summary>
public enum FiscalCalendarSettingStatus
{
/// <summary>
/// NoValue = 0.
/// </summary>
[DisplayValue("No Value")]
NoValue = 0,
/// <summary>
/// Inactive = 0.
/// </summary>
[DisplayValue("Inactive")]
Inactive = 1,
/// <summary>
/// Active = 1.
/// </summary>
[DisplayValue("Active")]
Active = 2,
/// <summary>
/// Inactive = 2.
/// </summary>
[DisplayValue("Future")]
Future = 3,
}
/// <summary>
/// Represents a fiscal calendar type.
/// </summary>
public enum FiscalCalendarType
{
/// <summary>
/// Calendar Year = 0.
/// </summary>
[DisplayValue("Calendar Year")]
CalendarYear = 0,
/// <summary>
/// 4-4-5 weeks calendar where each quarter contains of 3 months with 4, 4 and 5 weeks.
/// </summary>
[DisplayValue("4,4,5 weeks Calendar")]
Calendar445 = 1,
/// <summary>
/// 4-5-4 weeks calendar where each quarter contains of 3 months with 4, 5 and 4 weeks.
/// </summary>
[DisplayValue("4,5,4 weeks Calendar")]
Calendar454 = 2,
/// <summary>
/// 5-4-4 weeks calendar where each quarter contains of 3 months with 5, 4 and 4 weeks.
/// </summary>
[DisplayValue("5,4,4 weeks Calendar")]
Calendar544 = 3,
}
/// <summary>
/// Represents a way of ending the calendar year and distribution of weeks between quarters.
/// </summary>
public enum CalendarYearType
{
[DisplayValue("52 weeks only")]
Only52Weeks = 0,
[DisplayValue("Last Weekending day of the month")]
LastWeekDay = 1,
[DisplayValue("Standard Year")]
StandardYear = 2
}
/// <summary>
/// Represents a fiscal year record type.
/// </summary>
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
/// <summary>
/// Validates an object and returns a collection that holds failed-validation information.
/// </summary>
/// <returns>
/// A collection that holds failed-validation information.
/// </returns>
/// <param name="validationContext">The validation context.</param>
public IEnumerable<ValidationResult> 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
/// <summary>
/// Casts a <see cref="FiscalCalendarSetting"/> obect to the object of type <see cref="FiscalCalendarModel"/>.
/// </summary>
/// <param name="obj">A <see cref="FiscalCalendarSetting"/> object.</param>
/// <returns>A <see cref="FiscalCalendarModel"/> object filled with data from db.</returns>
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;
}
/// <summary>
/// Copies data from model to DAL object.
/// </summary>
/// <param name="obj">A target DAL object.</param>
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<FiscalCalendarModel>();
}
public List<FiscalCalendarModel> Items { get; set; }
}
}