201 lines
8.7 KiB
C#
201 lines
8.7 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data.Entity;
|
|
using System.Linq;
|
|
using System.Web;
|
|
using EnVisage.Models;
|
|
|
|
namespace EnVisage.Code.BLL
|
|
{
|
|
public class TrainingManager : ManagerBase<Training, TrainingModel>
|
|
{
|
|
public TrainingManager(EnVisageEntities dbContext)
|
|
: base(dbContext)
|
|
{
|
|
}
|
|
|
|
protected override Training InitInstance()
|
|
{
|
|
return new Training { Id = Guid.NewGuid() };
|
|
}
|
|
|
|
protected override Training RetrieveReadOnlyById(Guid key)
|
|
{
|
|
return DataTable.AsNoTracking().FirstOrDefault(t => t.Id == key);
|
|
}
|
|
|
|
public override DbSet<Training> DataTable
|
|
{
|
|
get
|
|
{
|
|
return DbContext.Trainings;
|
|
}
|
|
}
|
|
|
|
public override Training Save(TrainingModel model)
|
|
{
|
|
//if (!model.traStartDate.HasValue)
|
|
// throw new ArgumentNullException("model.StartDate");
|
|
//if (!model.EndDate.HasValue)
|
|
// throw new ArgumentNullException("model.EndDate");
|
|
Dictionary<Guid, decimal> uomValue = new Dictionary<Guid, decimal>();
|
|
using (var db = new EnVisageEntities())
|
|
{
|
|
(from p in db.PeopleResources
|
|
join ec in db.ExpenditureCategory on p.ExpenditureCategoryId equals ec.Id
|
|
join uom in db.UOMs on ec.UOMId equals uom.Id
|
|
where model.Resources.Contains(p.Id)
|
|
select new { p.Id, uom.UOMValue })
|
|
.ToList().ForEach(x => uomValue.Add(x.Id, x.UOMValue));
|
|
}
|
|
var obj = base.Save(model);
|
|
|
|
#region Save PeopleResourceTrainings
|
|
if (!Guid.Empty.Equals(model.Id))
|
|
{
|
|
var items2Delete = DbContext.PeopleResourceTrainings.Where(t => t.TrainingId == model.Id);
|
|
foreach (var peopleResourceTraining in items2Delete)
|
|
{
|
|
DbContext.Entry(peopleResourceTraining).State = EntityState.Deleted;
|
|
}
|
|
}
|
|
|
|
var weekEndingSetting = DbContext.SystemSettings.FirstOrDefault(t => t.Type == (int)SystemSettingType.FiscalCalendarWeekEnding);
|
|
var weekEndingDay = weekEndingSetting != null
|
|
? (DayOfWeek)Convert.ToInt16(weekEndingSetting.Value)
|
|
: DayOfWeek.Saturday;
|
|
var weekEndingDate = model.TrainingStartDate;
|
|
if (weekEndingDate.DayOfWeek != weekEndingDay)
|
|
weekEndingDate = Utils.GetNextDateByWeekDate(weekEndingDate, (short)weekEndingDay, 1, true);
|
|
var rangeEndDate = model.TrainingEndDate;
|
|
if (rangeEndDate.DayOfWeek != weekEndingDay)
|
|
rangeEndDate = Utils.GetNextDateByWeekDate(rangeEndDate, (short)weekEndingDay, 1, true);
|
|
var dt = weekEndingDate.AddDays(-6);
|
|
var businessDays = 0;
|
|
var wkEndDts = 0;
|
|
while (dt <= rangeEndDate)
|
|
{
|
|
if (dt > weekEndingDate)
|
|
{
|
|
foreach (var res in model.Resources)
|
|
{
|
|
var hoursOff = Convert.ToInt32(businessDays * uomValue[res] * model.TrainingTimeAllocated / ((7 - wkEndDts) * 100));
|
|
if (hoursOff >= 0)
|
|
{
|
|
DbContext.PeopleResourceTrainings.Add(new PeopleResourceTraining
|
|
{
|
|
Id = Guid.NewGuid(),
|
|
TrainingId = obj.Id,
|
|
PeopleResourceId = res,
|
|
WeekEndingDate = weekEndingDate,
|
|
HoursOff = hoursOff
|
|
|
|
});
|
|
}
|
|
}
|
|
weekEndingDate = weekEndingDate.AddDays(7);
|
|
businessDays = 0;
|
|
wkEndDts = 0;
|
|
}
|
|
if (model.TrainingStartDate <= dt && dt <= model.TrainingEndDate)
|
|
businessDays++;
|
|
if (model.Weekends.Any(t => dt == t))
|
|
{
|
|
wkEndDts++;
|
|
if (model.TrainingStartDate <= dt && dt <= model.TrainingEndDate)
|
|
businessDays--;
|
|
}
|
|
dt = dt.AddDays(1);
|
|
}
|
|
if (wkEndDts != 7)
|
|
{
|
|
foreach (var res in model.Resources)
|
|
{
|
|
var hoursOff = Convert.ToInt32(businessDays * uomValue[res] * model.TrainingTimeAllocated / ((7 - wkEndDts) * 100));
|
|
if (hoursOff > 0)
|
|
{
|
|
DbContext.PeopleResourceTrainings.Add(new PeopleResourceTraining
|
|
{
|
|
Id = Guid.NewGuid(),
|
|
TrainingId = obj.Id,
|
|
PeopleResourceId = res,
|
|
WeekEndingDate = weekEndingDate,
|
|
HoursOff = hoursOff
|
|
});
|
|
}
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
return obj;
|
|
}
|
|
|
|
public static List<TrainingModel> GetTrainings(Guid peopleResourceId)
|
|
{
|
|
if (Guid.Empty.Equals(peopleResourceId))
|
|
return null;
|
|
var Trainings = new Dictionary<Guid, TrainingModel>();
|
|
var prevDate = DateTime.MinValue;
|
|
TrainingModel item = null;
|
|
using (var db = new EnVisageEntities())
|
|
{
|
|
var uomValue = (from p in db.PeopleResources
|
|
join ec in db.ExpenditureCategory on p.ExpenditureCategoryId equals ec.Id
|
|
join uom in db.UOMs on ec.UOMId equals uom.Id
|
|
where p.Id == peopleResourceId
|
|
select new {uom.Id, uom.UOMValue}).FirstOrDefault();
|
|
if (uomValue == null || uomValue.UOMValue == 0)
|
|
throw new UOMNotExistsException();
|
|
|
|
var records = (from t in db.Trainings
|
|
join prv in db.PeopleResourceTrainings on t.Id equals prv.TrainingId into g
|
|
from sub in g.DefaultIfEmpty()
|
|
where t.PeopleResourceTrainings.Select(x=>x.PeopleResourceId).Contains(peopleResourceId)
|
|
select
|
|
new
|
|
{
|
|
t.Id,
|
|
t.StartDate,
|
|
t.EndDate,
|
|
t.Name,
|
|
t.Cost,
|
|
t.PercentAllocated,
|
|
t.TrainingType,
|
|
itemId = sub == null ? (Guid?)null : sub.Id,
|
|
HoursOff = sub == null ? (int?)null : sub.HoursOff,
|
|
WeekEndingDate = sub == null ? (DateTime?)null : sub.WeekEndingDate,
|
|
});
|
|
foreach (var training in records.OrderBy(o => o.WeekEndingDate))
|
|
{
|
|
if (!Trainings.ContainsKey(training.Id))
|
|
{
|
|
item = new TrainingModel
|
|
{
|
|
Id = training.Id,
|
|
TrainingStartDate = training.StartDate,
|
|
TrainingEndDate = training.EndDate,
|
|
TrainingName = training.Name,
|
|
TrainingCost = training.Cost,
|
|
TrainingTypeId = training.TrainingType.Id,
|
|
TrainingTypeName = training.TrainingType.Name,
|
|
TrainingTimeAllocated = training.PercentAllocated,
|
|
};
|
|
Trainings.Add(item.Id, item);
|
|
}
|
|
|
|
if (training.itemId.HasValue)
|
|
{
|
|
item.PeopleResourceTrainings.Add(new PeopleResourceTrainingModel
|
|
{
|
|
Id = training.itemId.Value,
|
|
WeekEndingDate = training.WeekEndingDate.Value,
|
|
HoursOff = training.HoursOff.Value,
|
|
PeopleResourceId = peopleResourceId
|
|
});
|
|
}
|
|
}
|
|
}
|
|
return Trainings.Values.ToList();
|
|
}
|
|
}
|
|
} |