using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using EnVisage.Code; using System.Linq; namespace EnVisage.Models { public class ClientModel : IBaseModel { public Guid Id { get; set; } [Required] [MaxLength(100, ErrorMessage = "Client name should not exceed 100 characters")] public string Name { get; set; } public Guid? GLAccountId { get; set; } [Display(Name = "GL Account")] public GLAccountModel GLAccount { get; set; } [Display(Name = "Number")] public string ClientNumber { get; set; } [Display(Name = "Number of Projects")] public int ProjectsCount { get; set; } public List Contacts { get { List result = new List(); EnVisageEntities DbContext = new EnVisageEntities(); var contacts = (from c in DbContext.Contacts where c.ParentId == Id select c).ToList(); foreach (var contact in contacts) result.Add((ContactModel)contact); return result; } set { } } public List Companies { get { List result = new List(); EnVisageEntities DbContext = new EnVisageEntities(); var companies = (from c in DbContext.Companies where c.ParentCompanyId != null select c).ToList(); foreach (var company in companies) result.Add((CompanyModel)company); return result.OrderBy(x => x.ParentCompanyName).ToList(); } set { } } public IList CompanyId { get; set; } public IList Company { get; set; } /// /// Casts a obect to the object of type . /// /// A object. /// A object filled with data from db. public static explicit operator ClientModel(Client obj) { if (obj == null) return null; var model = new ClientModel { Id = obj.Id, Name = obj.Name, GLAccountId = obj.GLAccountId, ClientNumber = obj.ClientNumber, GLAccount = (GLAccountModel)obj.GLAccount, ProjectsCount = obj.Projects.Count }; model.TrimStringProperties(); return model; } /// /// Copies data from model to DAL object. /// /// A target DAL object. public void CopyTo(Client dbObj) { if (dbObj == null) throw new ArgumentNullException(); dbObj.Name = Name; dbObj.GLAccountId = GLAccountId; dbObj.ClientNumber = ClientNumber; } } /// /// An UI representation of client to be displayed as list items /// public class ClientListModel { public Guid Id { get; set; } public string Name { get; set; } public string ClientNumber { get; set; } public int ProjectsCount { get; set; } public IList Companies { get; set; } public string GlAccount { get; set; } } }