using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace GreenTree.Nachtragsmanagement.Web.Models.Appendix { public class AppendixDataModel : IRequireStateDataModel { public int Id { get; set; } public string CustomNumber { get; set; } public string Description { get; set; } public decimal? Probability { get; set; } public int? OfferingNumber { get; set; } public DateTime? OfferingDate { get; set; } public decimal? OfferingValue { get; set; } public DateTime? NegotiationDate { get; set; } public decimal? NegotiationValue { get; set; } public bool ProtocolExists { get; set; } public int? OrderNumber { get; set; } public DateTime? OrderDate { get; set; } public bool OrderInvoiceCreated { get; set; } public string Comment { get; set; } public int? StateId { get; set; } public string StateDescription { get; set; } public int? SiteId { get; set; } public string SiteDescription { get; set; } public ICollection DeviationValues { get; set; } public ICollection DeviationDescriptions { get; set; } public string DeviationDescription { get; set; } public ICollection CategoryEntities { get; set; } public ICollection CategoryValueEntities { get; set; } public string CategoryValuesDescription { get { if (CategoryValueEntities == null) return String.Empty; else return String.Join(", ", CategoryValueEntities.Select(d => d.Description)); } } public AppendixDataModel() { DeviationValues = new List(); DeviationDescriptions = new List(); CategoryEntities = new List(); CategoryValueEntities = new List(); } public static AppendixDataModel FromAppendix(Core.Domain.Appendix.Appendix appendixEntity, bool newWhenIsNull) { if (appendixEntity == null && newWhenIsNull) return new AppendixDataModel { Id = -1 }; if (appendixEntity == null && !newWhenIsNull) throw new ArgumentNullException("appendixEntity", "Cannot create AppendixDataModel from NULL appendix entity."); return new AppendixDataModel { Id = appendixEntity.Id, CustomNumber = appendixEntity.CustomNumber, Description = appendixEntity.Description, Probability = appendixEntity.Probability, OfferingNumber = appendixEntity.OfferingNumber, OfferingDate = appendixEntity.OfferingDate, OfferingValue = appendixEntity.Value, NegotiationDate = appendixEntity.NegotiationDate, NegotiationValue = appendixEntity.NegotiationValue, ProtocolExists = appendixEntity.ProtocolExists, OrderNumber = appendixEntity.OrderNumber, OrderDate = appendixEntity.OrderDate, Comment = appendixEntity.Comment, OrderInvoiceCreated = appendixEntity.OrderInvoiceCreated, StateId = appendixEntity.StateId, StateDescription = appendixEntity.State == null ? null : appendixEntity.State.Description, SiteId = appendixEntity.SiteId, SiteDescription = appendixEntity.Site == null ? null : appendixEntity.Site.Description, CategoryValueEntities = appendixEntity.CategoryValues .Select(r => new CategoryValueDataModel { CategoryId = r.CategoryId, Description = String.Format("{0} - {1:c2}", r.Category.Description, r.Value), Value = r.Value }) .ToList(), DeviationValues = appendixEntity.Deviations .Select(r => r.Id) .ToList(), DeviationDescriptions = appendixEntity.Deviations .Select(r => r.CustomNumber) .ToList(), DeviationDescription = String.Join(", ", appendixEntity.Deviations .Select(d => d.CustomNumber)) }; } public Core.Domain.Appendix.Appendix ToAppendix() { return new Core.Domain.Appendix.Appendix { Id = this.Id, CustomNumber = this.CustomNumber, Description = this.Description, Probability = this.Probability, OfferingNumber = this.OfferingNumber, OfferingDate = this.OfferingDate, NegotiationDate = this.NegotiationDate, NegotiationValue = this.NegotiationValue, ProtocolExists = this.ProtocolExists, OrderNumber = this.OrderNumber, OrderDate = this.OrderDate, Comment = this.Comment, SiteId = this.SiteId }; } } }