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

103 lines
3.5 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using EnVisage.Code;
using System.Linq;
namespace EnVisage.Models
{
public class ClientModel : IBaseModel<Client>
{
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<ContactModel> Contacts
{
get
{
List<ContactModel> result = new List<ContactModel>();
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<CompanyModel> Companies
{
get
{
List<CompanyModel> result = new List<CompanyModel>();
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<Guid> CompanyId { get; set; }
public IList<string> Company { get; set; }
/// <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 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;
}
/// <summary>
/// Copies data from model to DAL object.
/// </summary>
/// <param name="dbObj">A target DAL object.</param>
public void CopyTo(Client dbObj)
{
if (dbObj == null)
throw new ArgumentNullException();
dbObj.Name = Name;
dbObj.GLAccountId = GLAccountId;
dbObj.ClientNumber = ClientNumber;
}
}
/// <summary>
/// An UI representation of client to be displayed as list items
/// </summary>
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<string> Companies { get; set; }
public string GlAccount { get; set; }
}
}