SiteDataModel.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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? AppendixValueNegotiated { get; set; }
  62. public ICollection<int> UserValues { get; set; }
  63. public ICollection<string> UserDescriptions { get; set; }
  64. public ICollection<UserDataModel> Users { get; set; }
  65. public string UserDescription { get; set; }
  66. public ICollection<SiteTreeDataModel> SiteTreeData { get; set; }
  67. public SiteDataModel()
  68. {
  69. DeviationValues = new List<int>();
  70. DeviationDescriptions = new List<string>();
  71. AppendixValues = new List<int>();
  72. AppendixDescriptions = new List<string>();
  73. UserValues = new List<int>();
  74. UserDescriptions = new List<string>();
  75. }
  76. public static SiteDataModel FromSite(Core.Domain.Site.Site siteEntity, bool newWhenIsNull,
  77. IConfigurationService configurationService)
  78. {
  79. if (siteEntity == null && newWhenIsNull)
  80. return new SiteDataModel
  81. {
  82. Id = -1,
  83. };
  84. if (siteEntity == null && !newWhenIsNull)
  85. throw new ArgumentNullException("siteEntity", "Cannot create SiteDataModel from NULL site entity.");
  86. var remainingAppendixStatuses = configurationService.TryGetConfigItemValue<int[]>(
  87. "GreenTree.Nachtragsmanagement.SiteGrid.RemainingAppendixStatuses", new[] { 1, 5, 6, 7, 11, 12, 13 });
  88. return new SiteDataModel
  89. {
  90. Id = siteEntity.Id,
  91. CustomNumber = siteEntity.CustomNumber,
  92. Description = siteEntity.Description,
  93. Start = siteEntity.Start,
  94. End = siteEntity.End,
  95. Finished = siteEntity.End.HasValue
  96. ? siteEntity.End <= DateTime.Now
  97. ? "Ja"
  98. : "Nein"
  99. : "Nein",
  100. Comment = siteEntity.Comment,
  101. DeviationValues =
  102. siteEntity.Deviations
  103. .Select(r => r.Id)
  104. .ToList(),
  105. DeviationDescriptions =
  106. siteEntity.Deviations
  107. .Select(r => r.Description)
  108. .ToList(),
  109. Deviations =
  110. siteEntity.Deviations
  111. .Select(r => DeviationDataModel.FromDeviation(r, false, configurationService))
  112. .ToList(),
  113. DeviationValue =
  114. siteEntity.Deviations
  115. .Sum(r => r.Value.HasValue && r.Percentage.HasValue
  116. ? r.Value * r.Percentage.Value
  117. : 0) +
  118. siteEntity.Appendices
  119. .SelectMany(r => r.Deviations)
  120. .Sum(r => r.Value.HasValue && r.Percentage.HasValue
  121. ? r.Value * r.Percentage.Value
  122. : 0),
  123. SiteDeviationValue =
  124. siteEntity.Deviations
  125. .Sum(r => r.Value.HasValue && r.Percentage.HasValue
  126. ? r.Value * r.Percentage.Value
  127. : 0),
  128. AppendixValues =
  129. siteEntity.Appendices
  130. .Select(r => r.Id)
  131. .ToList(),
  132. AppendixDescriptions =
  133. siteEntity.Appendices
  134. .Select(r => r.CustomNumber)
  135. .ToList(),
  136. Appendices =
  137. siteEntity.Appendices
  138. .Select(r => AppendixDataModel.FromAppendix(r, false))
  139. .ToList(),
  140. UserValues =
  141. siteEntity.Users
  142. .Select(r => r.Id)
  143. .ToList(),
  144. UserDescriptions =
  145. siteEntity.Users
  146. .Select(r => new
  147. {
  148. LastName = r.Lastname,
  149. Roles = String.Join(", ", r.Roles.Select(u => u.Description))
  150. })
  151. .OrderBy(r => r.Roles)
  152. .Select(r => String.Format("{0}|({1})", r.LastName, r.Roles))
  153. .ToList(),
  154. AppendixValueRemaining =
  155. siteEntity.Appendices
  156. .Where(r => remainingAppendixStatuses.Contains(r.StateId ?? 0))
  157. .Sum(r =>
  158. r.Value.HasValue && r.Percentage.HasValue
  159. ? r.Value.Value * r.Percentage.Value
  160. : 0),
  161. AppendixValueNegotiated =
  162. siteEntity.Appendices
  163. .Where(r => !remainingAppendixStatuses.Contains(r.StateId ?? 0))
  164. .Sum(r =>
  165. r.Value.HasValue && r.Percentage.HasValue
  166. ? r.Value.Value * r.Percentage.Value
  167. : 0),
  168. UserDescription =
  169. String.Join(", ",
  170. siteEntity.Users
  171. .Select(r => new
  172. {
  173. LastName = r.Lastname,
  174. Roles = String.Join(", ", r.Roles.Select(u => u.Description))
  175. })
  176. .Select(u => String.Format("{0} ({1})", u.LastName, u.Roles ))),
  177. Users =
  178. siteEntity.Users
  179. .Select(r => UserDataModel.FromUser(r, false))
  180. .ToList(),
  181. };
  182. }
  183. public Core.Domain.Site.Site ToSite()
  184. {
  185. return new Core.Domain.Site.Site
  186. {
  187. Id = this.Id,
  188. CustomNumber = this.CustomNumber,
  189. Description = Description,
  190. Comment = this.Comment,
  191. Start = this.Start,
  192. End = this.End
  193. };
  194. }
  195. }
  196. }