SiteDataModel.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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.Status != null && r.Status.IsZeroValue
  128. ? 0
  129. : r.Value.HasValue && r.Percentage.HasValue
  130. ? r.Value * r.Percentage.Value
  131. : 0),
  132. AppendixValues =
  133. siteEntity.Appendices
  134. .Select(r => r.Id)
  135. .ToList(),
  136. AppendixDescriptions =
  137. siteEntity.Appendices
  138. .Select(r => r.CustomNumber)
  139. .ToList(),
  140. Appendices =
  141. siteEntity.Appendices
  142. .Select(r => AppendixDataModel.FromAppendix(r, false, configurationService))
  143. .ToList(),
  144. UserValues =
  145. siteEntity.Users
  146. .Select(r => r.Id)
  147. .ToList(),
  148. UserDescriptions =
  149. siteEntity.Users
  150. .Select(r => new
  151. {
  152. LastName = r.Lastname,
  153. Roles = String.Join(", ", r.Roles.Select(u => u.Description))
  154. })
  155. .OrderBy(r => r.Roles)
  156. .Select(r => String.Format("{0}|({1})", r.LastName, r.Roles))
  157. .ToList(),
  158. AppendixValueRemaining =
  159. siteEntity.Appendices
  160. .Where(r => remainingAppendixStatuses.Contains(r.StateId ?? 0))
  161. .Sum(r =>
  162. r.Value.HasValue
  163. ? r.Value.Value
  164. : 0),
  165. AppendixValueNegotiated =
  166. siteEntity.Appendices
  167. .Where(r => !remainingAppendixStatuses.Contains(r.StateId ?? 0))
  168. .Sum(r =>
  169. r.NegotiationValue.HasValue
  170. ? r.NegotiationValue.Value
  171. : 0),
  172. AppendixValueCalculated =
  173. siteEntity.Appendices
  174. .Where(r => remainingAppendixStatuses.Contains(r.StateId ?? 0))
  175. .Sum(r =>
  176. r.Value.HasValue && r.Percentage.HasValue
  177. ? r.Value.Value * r.Percentage.Value
  178. : 0),
  179. UserDescription =
  180. String.Join(", ",
  181. siteEntity.Users
  182. .Select(r => new
  183. {
  184. LastName = r.Lastname,
  185. Roles = String.Join(", ", r.Roles.Select(u => u.Description))
  186. })
  187. .Select(u => String.Format("{0} ({1})", u.LastName, u.Roles ))),
  188. Users =
  189. siteEntity.Users
  190. .Select(r => UserDataModel.FromUser(r, false))
  191. .ToList(),
  192. };
  193. siteModel.AppendixValueCleared = siteModel.AppendixValueRemaining - siteModel.AppendixValueCalculated;
  194. return siteModel;
  195. }
  196. public Core.Domain.Site.Site ToSite()
  197. {
  198. return new Core.Domain.Site.Site
  199. {
  200. Id = this.Id,
  201. CustomNumber = this.CustomNumber,
  202. Description = Description,
  203. Comment = this.Comment,
  204. Start = this.Start,
  205. End = this.End
  206. };
  207. }
  208. }
  209. }