EnVisageOnline/Beta/Source/EnVisage/Models/TeamModel.cs

160 lines
5.8 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using EnVisage.Code;
using System.Linq;
using EnVisage.Code.BLL;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
namespace EnVisage.Models
{
public class TeamModel : IBaseModel<Team>
{
public Guid Id { get; set; }
[Required]
[MaxLength(100, ErrorMessage = "Team name should not exceed 100 characters")]
[Display(Name = "Team name")]
public string Name { get; set; }
public Guid? CompanyId { get; set; }
public Guid? CostCenterId { get; set; }
public Guid? ReportToId { get; set; }
public Guid? PlannedCapacityScenarioId { get; set; }
public CompanyModel Company { get; set; }
public CreditDepartmentModel CostCenter { get; set; }
public GLAccountModel GLAccount { get; set; }
public ContactModel ReportTo { get; set; }
public IList<Guid> UserId { get; set; }
public IList<Guid> ViewId { get; set; }
public IEnumerable<AspNetUser> Users
{
get
{
EnVisageEntities DbContext = new EnVisageEntities();
return DbContext.AspNetUsers;
}
}
public List<ViewModel> AllViews
{
get
{
var dbContext = new EnVisageEntities();
var views = (from c in dbContext.Views select c).ToList();
var result = views.Select(t => (ViewModel)t).ToList();
return result.OrderBy(x => x.Name).ToList();
}
set { }
}
public String TeamUsers
{
get
{
string result = "";
EnVisageEntities DbContext = new EnVisageEntities();
var ac = new ApplicationDbContext();
var usermanager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(ac));
var users = DbContext.User2Team.Where(x => x.TeamId == Id).ToList();
foreach (var user in users)
{
ApplicationUser u = usermanager.FindById(user.UserId);
if (u != null)
{
if (!string.IsNullOrEmpty(result))
result += ", ";
result += u.UserName;
}
}
return result;
}
}
/// <summary>
/// Casts a <see cref="Client"/> obect to the object of type <see cref="ClientModel"/>.
/// </summary>
/// <param name="obj">A <see cref="Client"/> object.</param>
/// <returns>A <see cref="ClientModel"/> object filled with data from db.</returns>
public static explicit operator TeamModel(Team obj)
{
if (obj == null)
return null;
var model = new TeamModel
{
Id = obj.Id,
Name = obj.Name,
CompanyId = obj.CompanyId,
CostCenterId = obj.CostCenterId,
ReportToId = obj.ReportsTo,
Company = (CompanyModel)obj.Company,
CostCenter = (CreditDepartmentModel)obj.CreditDepartment,
ReportTo = (ContactModel)obj.Contact,
PlannedCapacityScenarioId = obj.PlannedCapacityScenarioId
};
model.TrimStringProperties();
return model;
}
/// <summary>
/// Copies data from model to DAL object.
/// </summary>
/// <param name="dbObj">A target DAL object.</param>
public void CopyTo(Team dbObj)
{
if (dbObj == null)
throw new ArgumentNullException();
dbObj.Name = Name;
dbObj.CostCenterId = CostCenterId;
dbObj.CompanyId = CompanyId;
dbObj.ReportsTo = ReportToId;
}
public Dictionary<DateTime, PeopleResourceModel.CapacityValues> GetWeeklyTeamCapacity(Dictionary<Guid, ExpenditureCategory> expCats = null, Dictionary<Guid, UOM> uoms = null, bool? isUOMHours = null)
{
EnVisageEntities dbContext = new EnVisageEntities();
var dict = new Dictionary<DateTime, PeopleResourceModel.CapacityValues>();
var resourceManager = new PeopleResourcesManager(dbContext);
IEnumerable<PeopleResourceModel> resources = null;
Guid[] expCatIds = null;
if(expCats != null)
{
expCatIds = expCats.Keys.ToArray();
resources = resourceManager.GetResources().Where(x => x.TeamId == Id && expCatIds.Contains(x.ExpenditureCategoryId) && x.IsActiveEmployee);
}
if (resources != null)
{
Dictionary<Guid, List<Rate>> globalRates = new Dictionary<Guid, List<Rate>>();
var ungroupedrates = dbContext.Rates.Where(r => r.Type == (short)EnVisage.Models.RateModel.RateType.Global && expCatIds.Contains(r.ExpenditureCategoryId));
foreach(var rate in ungroupedrates)
{
if (!globalRates.ContainsKey(rate.ExpenditureCategoryId))
globalRates.Add(rate.ExpenditureCategoryId, new List<Rate>());
globalRates[rate.ExpenditureCategoryId].Add(rate);
}
foreach (var resource in resources)
{
Dictionary<DateTime, PeopleResourceModel.CapacityValues> weeklyCapacities = resource.GetResourceWeeklyCapacity(globalRates);
foreach (var week in resource.WeeklyCapacities)
{
if (dict.ContainsKey(week.Key))
{
dict[week.Key].Quantity += week.Value.Quantity *
Utils.GetUOMMultiplier(expCats, uoms, week.Value.ExpCatId, isUOMHours);
dict[week.Key].Cost += week.Value.Cost;
}
else
{
dict.Add(week.Key, week.Value);
dict[week.Key].Quantity = week.Value.Quantity * Utils.GetUOMMultiplier(expCats, uoms, week.Value.ExpCatId, isUOMHours);
}
}
}
}
return dict;
}
}
}