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 { 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 UserId { get; set; } public IList ViewId { get; set; } public IEnumerable Users { get { EnVisageEntities DbContext = new EnVisageEntities(); return DbContext.AspNetUsers; } } public List 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(new UserStore(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; } } /// /// Casts a obect to the object of type . /// /// A object. /// A object filled with data from db. 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; } /// /// Copies data from model to DAL object. /// /// A target DAL object. 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 GetWeeklyTeamCapacity(Dictionary expCats = null, Dictionary uoms = null, bool? isUOMHours = null) { EnVisageEntities dbContext = new EnVisageEntities(); var dict = new Dictionary(); var resourceManager = new PeopleResourcesManager(dbContext); IEnumerable 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> globalRates = new Dictionary>(); 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()); globalRates[rate.ExpenditureCategoryId].Add(rate); } foreach (var resource in resources) { Dictionary 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; } } }