132 lines
4.8 KiB
C#
132 lines
4.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel.DataAnnotations;
|
|
using System.Linq;
|
|
using EnVisage.Code;
|
|
|
|
namespace EnVisage.Models
|
|
{
|
|
public class TrainingModel : IBaseModel<Training>, IValidatableObject
|
|
{
|
|
public Guid Id { get; set; }
|
|
[Required]
|
|
[Display(Name = "Start Date")]
|
|
[DataType(DataType.Date), DisplayFormat(DataFormatString = "{0:MM/dd/yy}", ApplyFormatInEditMode = true)]
|
|
public DateTime TrainingStartDate { get; set; }
|
|
[Required]
|
|
[Display(Name = "End Date")]
|
|
[DataType(DataType.Date), DisplayFormat(DataFormatString = "{0:MM/dd/yy}", ApplyFormatInEditMode = true)]
|
|
public DateTime TrainingEndDate { get; set; }
|
|
[Required]
|
|
[Display(Name = "Name")]
|
|
public string TrainingName { get; set; }
|
|
[Required]
|
|
[Display(Name = "Type")]
|
|
public Guid TrainingTypeId { get; set; }
|
|
[Display(Name = "Type")]
|
|
public string TrainingTypeName { get; set; }
|
|
[Required]
|
|
[Display(Name = "Cost")]
|
|
public decimal TrainingCost { get; set; }
|
|
[Required]
|
|
[Display(Name = "% Time Allocated")]
|
|
[Range(0, 100)]
|
|
[UIHint("Slider")]
|
|
public short TrainingTimeAllocated { get; set; }
|
|
//public decimal UOMValue { get; set; }
|
|
[Display(Name = "Duration")]
|
|
public int? TrainingDuration
|
|
{
|
|
get { return (TrainingEndDate.Subtract(TrainingStartDate).Days + 1); }
|
|
}
|
|
public List<PeopleResourceTrainingModel> PeopleResourceTrainings { get; set; }
|
|
[Required]
|
|
[Display(Name = "Resources")]
|
|
[UIHint("Resources")]
|
|
public List<Guid> Resources { get; set; }
|
|
[Required]
|
|
[Display(Name = "Weekends")]
|
|
[UIHint("Weekends")]
|
|
public List<DateTime> Weekends { get; set; }
|
|
public int WeekendingDay { get; set; }
|
|
public TrainingModel()
|
|
{
|
|
PeopleResourceTrainings = new List<PeopleResourceTrainingModel>();
|
|
Weekends = new List<DateTime>();
|
|
}
|
|
|
|
public void CopyTo(Training obj)
|
|
{
|
|
if (obj == null)
|
|
throw new ArgumentNullException();
|
|
|
|
obj.StartDate = TrainingStartDate;
|
|
obj.EndDate = TrainingEndDate;
|
|
obj.Name = TrainingName;
|
|
obj.TrainingTypeId = TrainingTypeId;
|
|
obj.Cost = TrainingCost;
|
|
obj.PercentAllocated = TrainingTimeAllocated;
|
|
obj.PeopleResourceTrainings = PeopleResourceTrainings.Select(t =>
|
|
{
|
|
var o = new PeopleResourceTraining();
|
|
t.CopyTo(o);
|
|
return o;
|
|
}).ToList();
|
|
obj.Weekends = string.Join(",", Weekends.Select(t => t.ToShortDateString()));
|
|
}
|
|
public static explicit operator TrainingModel(Training obj)
|
|
{
|
|
if (obj == null)
|
|
return null;
|
|
var model = new TrainingModel
|
|
{
|
|
Id = obj.Id,
|
|
TrainingStartDate = obj.StartDate,
|
|
TrainingEndDate = obj.EndDate,
|
|
TrainingName = obj.Name,
|
|
TrainingCost = obj.Cost,
|
|
TrainingTimeAllocated = obj.PercentAllocated,
|
|
Resources = obj.PeopleResourceTrainings.Select(x=>x.PeopleResourceId).ToList(),
|
|
Weekends = obj.Weekends.Split(new []{','}, StringSplitOptions.RemoveEmptyEntries).Select(c =>
|
|
{
|
|
DateTime dt;
|
|
return DateTime.TryParse(c, out dt) ? dt : DateTime.MinValue;
|
|
}).Where(t=>t != DateTime.MinValue).ToList()
|
|
};
|
|
var str = string.Empty;
|
|
foreach (var dateTime in model.Weekends)
|
|
{
|
|
if (!string.IsNullOrEmpty(str))
|
|
str += ",";
|
|
str += "'" + dateTime.ToShortDateString() + "'";
|
|
}
|
|
|
|
model.TrimStringProperties();
|
|
return model;
|
|
}
|
|
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
|
|
{
|
|
if (TrainingStartDate > TrainingEndDate)
|
|
yield return new ValidationResult("End Date should be greater than Start Date", new[] { "StartDate", "EndDate" });
|
|
}
|
|
}
|
|
public class PeopleResourceTrainingModel : IBaseModel<PeopleResourceTraining>
|
|
{
|
|
public Guid Id { get; set; }
|
|
public Guid PeopleResourceId { get; set; }
|
|
public int HoursOff { get; set; }
|
|
public Guid TrainingId { get; set; }
|
|
public DateTime WeekEndingDate { get; set; }
|
|
|
|
public void CopyTo(PeopleResourceTraining obj)
|
|
{
|
|
if (obj == null)
|
|
throw new ArgumentNullException();
|
|
|
|
obj.HoursOff = HoursOff;
|
|
obj.PeopleResourceId = PeopleResourceId;
|
|
obj.TrainingId = TrainingId;
|
|
obj.WeekEndingDate = WeekEndingDate;
|
|
}
|
|
}
|
|
} |