SiteDataModel.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. using GreenTree.Nachtragsmanagement.Services.Configuration;
  2. using GreenTree.Nachtragsmanagement.Web.Models.Admin.User;
  3. using GreenTree.Nachtragsmanagement.Web.Models.Appendix;
  4. using GreenTree.Nachtragsmanagement.Web.Models.Deviation;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Web;
  9. namespace GreenTree.Nachtragsmanagement.Web.Models.Site
  10. {
  11. public class SiteDataModel
  12. {
  13. public int Id { get; set; }
  14. public string CustomNumber { get; set; }
  15. public string Description { get; set; }
  16. public DateTime? Start { get; set; }
  17. public DateTime? End { get; set; }
  18. public string Finished { get; set; }
  19. public string Comment { get; set; }
  20. public ICollection<int> DeviationValues { get; set; }
  21. public ICollection<string> DeviationDescriptions { get; set; }
  22. public ICollection<DeviationDataModel> Deviations { get; set; }
  23. public string DeviationDescription
  24. {
  25. get
  26. {
  27. if (DeviationDescriptions == null)
  28. return String.Empty;
  29. else
  30. return String.Join(" - ", DeviationDescriptions);
  31. }
  32. }
  33. public string HtmlDeviationDescription
  34. {
  35. get
  36. {
  37. if (DeviationDescriptions == null)
  38. return String.Empty;
  39. else
  40. return String.Join("</br>--------------</br>",
  41. Deviations
  42. .Select(d => String.Format("{0} - {1}", d.CustomNumber, d.Description)));
  43. }
  44. }
  45. public decimal? DeviationValue { get; set; }
  46. public decimal? SiteDeviationValue { get; set; }
  47. public ICollection<int> AppendixValues { get; set; }
  48. public ICollection<string> AppendixDescriptions { get; set; }
  49. public ICollection<AppendixDataModel> Appendices { get; set; }
  50. public string AppendixDescription
  51. {
  52. get
  53. {
  54. if (AppendixDescriptions == null)
  55. return String.Empty;
  56. else
  57. return String.Join(", ", AppendixDescriptions);
  58. }
  59. }
  60. public decimal? AppendixValueRemaining { get; set; }
  61. public decimal? AppendixValueCalculated { get; set; }
  62. public decimal? AppendixValueCleared { get; set; }
  63. public decimal? AppendixValueNegotiated { get; set; }
  64. public ICollection<int> UserValues { get; set; }
  65. public ICollection<string> UserDescriptions { get; set; }
  66. public ICollection<UserDataModel> Users { get; set; }
  67. public string UserDescription { get; set; }
  68. public ICollection<SiteTreeDataModel> SiteTreeData { get; set; }
  69. public SiteDataModel()
  70. {
  71. DeviationValues = new List<int>();
  72. DeviationDescriptions = new List<string>();
  73. AppendixValues = new List<int>();
  74. AppendixDescriptions = new List<string>();
  75. UserValues = new List<int>();
  76. UserDescriptions = new List<string>();
  77. }
  78. public static SiteDataModel FromSite(Core.Domain.Site.Site siteEntity, bool newWhenIsNull,
  79. IConfigurationService configurationService)
  80. {
  81. if (siteEntity == null && newWhenIsNull)
  82. return new SiteDataModel
  83. {
  84. Id = -1,
  85. };
  86. if (siteEntity == null && !newWhenIsNull)
  87. throw new ArgumentNullException("siteEntity", "Cannot create SiteDataModel from NULL site entity.");
  88. var remainingAppendixStatuses = configurationService.TryGetConfigItemValue<int[]>(
  89. "GreenTree.Nachtragsmanagement.SiteGrid.RemainingAppendixStatuses", new[] { 1, 5, 6, 7, 9, 12, 13 });
  90. var siteModel = new SiteDataModel
  91. {
  92. Id = siteEntity.Id,
  93. CustomNumber = siteEntity.CustomNumber,
  94. Description = siteEntity.Description,
  95. Start = siteEntity.Start,
  96. End = siteEntity.End,
  97. Finished = siteEntity.End.HasValue
  98. ? siteEntity.End <= DateTime.Now
  99. ? "Ja"
  100. : "Nein"
  101. : "Nein",
  102. Comment = siteEntity.Comment,
  103. DeviationValues =
  104. siteEntity.Deviations
  105. .Select(r => r.Id)
  106. .ToList(),
  107. DeviationDescriptions =
  108. siteEntity.Deviations
  109. .Select(r => r.Description)
  110. .ToList(),
  111. Deviations =
  112. siteEntity.Deviations
  113. .Select(r => DeviationDataModel.FromDeviation(r, false, configurationService))
  114. .ToList(),
  115. DeviationValue =
  116. siteEntity.Deviations
  117. .Sum(r => r.Value.HasValue && r.Percentage.HasValue
  118. ? r.Value * r.Percentage.Value
  119. : 0) +
  120. siteEntity.Appendices
  121. .SelectMany(r => r.Deviations)
  122. .Sum(r => r.Value.HasValue && r.Percentage.HasValue
  123. ? r.Value * r.Percentage.Value
  124. : 0),
  125. SiteDeviationValue =
  126. siteEntity.Deviations
  127. .Sum(r => r.Value.HasValue && r.Percentage.HasValue
  128. ? r.Value * r.Percentage.Value
  129. : 0),
  130. AppendixValues =
  131. siteEntity.Appendices
  132. .Select(r => r.Id)
  133. .ToList(),
  134. AppendixDescriptions =
  135. siteEntity.Appendices
  136. .Select(r => r.CustomNumber)
  137. .ToList(),
  138. Appendices =
  139. siteEntity.Appendices
  140. .Select(r => AppendixDataModel.FromAppendix(r, false, configurationService))
  141. .ToList(),
  142. UserValues =
  143. siteEntity.Users
  144. .Select(r => r.Id)
  145. .ToList(),
  146. UserDescriptions =
  147. siteEntity.Users
  148. .Select(r => new
  149. {
  150. LastName = r.Lastname,
  151. Roles = String.Join(", ", r.Roles.Select(u => u.Description))
  152. })
  153. .OrderBy(r => r.Roles)
  154. .Select(r => String.Format("{0}|({1})", r.LastName, r.Roles))
  155. .ToList(),
  156. AppendixValueRemaining =
  157. siteEntity.Appendices
  158. .Where(r => remainingAppendixStatuses.Contains(r.StateId ?? 0))
  159. .Sum(r =>
  160. r.Value.HasValue
  161. ? r.Value.Value
  162. : 0),
  163. AppendixValueCalculated =
  164. siteEntity.Appendices
  165. .Where(r => remainingAppendixStatuses.Contains(r.StateId ?? 0))
  166. .Sum(r =>
  167. r.Value.HasValue && r.Percentage.HasValue
  168. ? r.Value.Value * r.Percentage.Value
  169. : 0),
  170. UserDescription =
  171. String.Join(", ",
  172. siteEntity.Users
  173. .Select(r => new
  174. {
  175. LastName = r.Lastname,
  176. Roles = String.Join(", ", r.Roles.Select(u => u.Description))
  177. })
  178. .Select(u => String.Format("{0} ({1})", u.LastName, u.Roles ))),
  179. Users =
  180. siteEntity.Users
  181. .Select(r => UserDataModel.FromUser(r, false))
  182. .ToList(),
  183. };
  184. siteModel.AppendixValueCleared = siteModel.AppendixValueRemaining - siteModel.AppendixValueCalculated;
  185. return siteModel;
  186. }
  187. public Core.Domain.Site.Site ToSite()
  188. {
  189. return new Core.Domain.Site.Site
  190. {
  191. Id = this.Id,
  192. CustomNumber = this.CustomNumber,
  193. Description = Description,
  194. Comment = this.Comment,
  195. Start = this.Start,
  196. End = this.End
  197. };
  198. }
  199. }
  200. }