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

88 lines
3.0 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;
namespace EnVisage.Models
{
public class RateModel : IBaseModel<Rate>
{
#region enums and subclasses
public enum RateType
{
Global = 0,
Derived = 1
}
#endregion
public Guid Id { get; set; }
public Guid? ParentId { get; set; }
public Guid ExpenditureCategoryId { get; set; }
public RateType Type { get; set; }
[Required]
[Display(Name = "Rate")]
[DisplayFormat(DataFormatString = "{0:0.00}", ApplyFormatInEditMode = true)]
[Range(typeof(Decimal), "0.01", "9999999.99", ErrorMessage = "{0} must be a decimal/number between {1} and {2}.")]
public decimal Rate1 { get; set; }
[Display(Name = "Start Date")]
[DataType(DataType.Date), DisplayFormat(DataFormatString = "{0:MM/dd/yy}", ApplyFormatInEditMode = true)]
public DateTime StartDate { get; set; }
[Display(Name = "End Date")]
[DataType(DataType.Date), DisplayFormat(DataFormatString = "{0:MM/dd/yy}", ApplyFormatInEditMode = true)]
[DateGreaterThanOrEqualAttribute("StartDate", "Rate End Date should not be less than Start Date")]
public DateTime EndDate { get; set; }
public Guid? DerivedObjectId { get; set; }
public int FreezeRate { get; set; }
//public virtual ExpenditureCategory ExpenditureCategory { get; set; }
/// <summary>
/// Casts a <see cref="Rate"/> obect to the object of type <see cref="RateModel"/>.
/// </summary>
/// <param name="obj">A <see cref="Rate"/> object.</param>
/// <returns>A <see cref="RateModel"/> object filled with data from db.</returns>
public static explicit operator RateModel(Rate obj)
{
if (obj == null)
return null;
var model = new RateModel
{
Id = obj.Id,
ParentId = obj.ParentId,
ExpenditureCategoryId = obj.ExpenditureCategoryId,
Type = (RateType)obj.Type,
Rate1 = obj.Rate1,
StartDate = obj.StartDate,
EndDate = obj.EndDate,
DerivedObjectId = obj.DerivedId,
FreezeRate = obj.FreezeRate,
};
return model;
}
/// <summary>
/// Copies data from model to DAL object.
/// </summary>
/// <param name="dbObj">A target DAL object.</param>
public void CopyTo(Rate dbObj)
{
if (dbObj == null)
throw new ArgumentNullException();
dbObj.ParentId = ParentId;
dbObj.ExpenditureCategoryId = ExpenditureCategoryId;
dbObj.Type = (short)Type;
dbObj.Rate1 = Rate1;
dbObj.StartDate = StartDate;
dbObj.EndDate = EndDate;
dbObj.DerivedId = DerivedObjectId;
dbObj.FreezeRate = FreezeRate;
}
}
}