using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace GreenTree.Nachtragsmanagement.Web.Models.Site { public class SiteTreeDataModel { public int Id { get; set; } public string TreeKey { get; set; } public string TreeParentKey { get; set; } public string CustomNumber { get; set; } public string Description { get; set; } public DateTime? ReceiptDate { get; set; } public decimal? Value { get; set; } public int? StatusId { get; set; } public string StatusDescription { get; set; } public string StatusColor { get; set; } public ICollection DistCatValues { get; set; } public ICollection DistCatDescriptions { get; set; } public string DistCatDescription { get { if (DistCatDescriptions == null) return String.Empty; else return String.Join(", ", DistCatDescriptions); } } public string Comment { get; set; } public SiteTreeDataModel() { DistCatValues = new List(); DistCatDescriptions = new List(); } public static List TreeFromSite(Core.Domain.Site.Site siteEntity) { var result = new List(); if (siteEntity == null) return result; var emptyAppendix = new SiteTreeDataModel { Id = 0, TreeKey = "a_0", TreeParentKey = String.Empty, CustomNumber = "Offene VA" }; result.Add(emptyAppendix); var openDeviations = siteEntity.Deviations .Where(d => d.Appendix == null); foreach (var openDeviation in openDeviations) { result.Add(new SiteTreeDataModel { Id = openDeviation.Id, TreeKey = String.Format("d_{0}", openDeviation.Id), TreeParentKey = emptyAppendix.TreeKey, CustomNumber = openDeviation.CustomNumber, Description = openDeviation.Description, ReceiptDate = openDeviation.ReceiptDate, DistCatValues = openDeviation.DisturbanceValues .Select(d => d.Id) .ToList(), DistCatDescriptions = openDeviation.DisturbanceValues .Select(r => r.Disturbance == null ? String.Empty : r.Disturbance.Description) .ToList(), StatusId = openDeviation.StatusId, StatusDescription = openDeviation.Status == null ? String.Empty : openDeviation.Status.Description, Value = openDeviation.Value.Value * openDeviation.Percentage.Value, Comment = openDeviation.Comment }); } foreach (var appendix in siteEntity.Appendices) { var parentAppendix = new SiteTreeDataModel { Id = appendix.Id, TreeKey = String.Format("a_{0}", appendix.Id), TreeParentKey = String.Empty, CustomNumber = appendix.CustomNumber, Description = appendix.Description, ReceiptDate = appendix.OfferingDate, DistCatValues = appendix.CategoryValues .Select(d => d.Id) .ToList(), DistCatDescriptions = appendix.CategoryValues .Select(r => r.Category == null ? String.Empty : r.Category.Description) .ToList(), StatusId = appendix.StateId, StatusDescription = appendix.State == null ? String.Empty : appendix.State.Description, StatusColor = appendix.State == null ? "#FFFFFF" : appendix.State.HexColor, Value = appendix.NegotiationValue, Comment = appendix.Comment }; result.Add(parentAppendix); foreach (var deviation in appendix.Deviations) { var childDeviation = new SiteTreeDataModel { Id = deviation.Id, TreeKey = String.Format("d_{0}", deviation.Id), TreeParentKey = parentAppendix.TreeKey, CustomNumber = deviation.CustomNumber, Description = deviation.Description, ReceiptDate = deviation.ReceiptDate, DistCatValues = deviation.DisturbanceValues .Select(d => d.Id) .ToList(), DistCatDescriptions = deviation.DisturbanceValues .Select(r => r.Disturbance == null ? String.Empty : r.Disturbance.Description) .ToList(), StatusId = deviation.StatusId, StatusDescription = deviation.Status == null ? String.Empty : deviation.Status.Description, Value = deviation.Value.Value * deviation.Percentage.Value, Comment = deviation.Comment }; result.Add(childDeviation); } } return result; } } }