AppendixDataModel.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. using GreenTree.Nachtragsmanagement.Services.Configuration;
  2. using Newtonsoft.Json;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Web;
  7. namespace GreenTree.Nachtragsmanagement.Web.Models.Appendix
  8. {
  9. public class AppendixDataModel : IRequireStateDataModel
  10. {
  11. public int Id { get; set; }
  12. public string CustomNumber { get; set; }
  13. public int CustomNumberSort
  14. {
  15. get
  16. {
  17. Int32.TryParse(CustomNumber, out int val);
  18. return val;
  19. }
  20. }
  21. public string Description { get; set; }
  22. public decimal? Probability { get; set; }
  23. public int? OfferingNumber { get; set; }
  24. public DateTime? OfferingDate { get; set; }
  25. public decimal OfferingValue { get; set; }
  26. public decimal? RelationOfferingToDeviations { get; set; }
  27. public decimal Percentage { get; set; }
  28. public decimal PercentageValue { get; set; }
  29. public DateTime? NegotiationDate { get; set; }
  30. public TimeSpan? OfferingToNegotiationDate { get; set; }
  31. public decimal? NegotiationValue { get; set; }
  32. public decimal? RelationOfferingToNegotiation { get; set; }
  33. public bool ProtocolExists { get; set; }
  34. public string OrderNumber { get; set; }
  35. public DateTime? OrderDate { get; set; }
  36. public bool OrderInvoiceCreated { get; set; }
  37. public string OrderInvoiceCreatedDescription { get; set; }
  38. public string Comment { get; set; }
  39. public int? StateId { get; set; }
  40. public StateDataModel State { get; set; }
  41. public string IsRemaining { get; set; }
  42. public string StateDescription { get; set; }
  43. public string SiteDescription { get; set; }
  44. public string SiteCustomNumber { get; set; }
  45. public int? SiteId { get; set; }
  46. public ICollection<string> UserDescriptions { get; set; }
  47. public string UserDescription { get; set; }
  48. public ICollection<int> DeviationValues { get; set; }
  49. public ICollection<string> DeviationDescriptions { get; set; }
  50. public string DeviationDescription { get; set; }
  51. public ICollection<string> CategoryEntities { get; set; }
  52. public ICollection<CategoryValueDataModel> CategoryValueEntities { get; set; }
  53. public string CategoryValuesDescription
  54. {
  55. get
  56. {
  57. if (CategoryValueEntities == null)
  58. return String.Empty;
  59. else
  60. return String.Join(", ", CategoryValueEntities.Select(d => d.Description));
  61. }
  62. }
  63. public string InvoiceDescription { get; set; }
  64. public ICollection<InvoiceDataModel> Invoices { get; set; }
  65. public AppendixDataModel()
  66. {
  67. DeviationValues = new List<int>();
  68. DeviationDescriptions = new List<string>();
  69. CategoryEntities = new List<string>();
  70. CategoryValueEntities = new List<CategoryValueDataModel>();
  71. Invoices = new List<InvoiceDataModel>();
  72. }
  73. public static AppendixDataModel FromAppendix(Core.Domain.Appendix.Appendix appendixEntity, bool newWhenIsNull,
  74. IConfigurationService configurationService)
  75. {
  76. if (appendixEntity == null && newWhenIsNull)
  77. return new AppendixDataModel
  78. {
  79. Id = -1,
  80. Percentage = (decimal)1.0
  81. };
  82. if (appendixEntity == null && !newWhenIsNull)
  83. throw new ArgumentNullException("appendixEntity", "Cannot create AppendixDataModel from NULL appendix entity.");
  84. var remainingAppendixStatuses = configurationService.TryGetConfigItemValue<int[]>(
  85. "GreenTree.Nachtragsmanagement.SiteGrid.RemainingAppendixStatuses", new[] { 1, 5, 6, 7, 9, 12, 13 });
  86. var model = new AppendixDataModel
  87. {
  88. Id = appendixEntity.Id,
  89. CustomNumber = appendixEntity.CustomNumber,
  90. Description = appendixEntity.Description,
  91. Percentage = appendixEntity.Percentage.HasValue
  92. ? appendixEntity.Percentage.Value
  93. : (decimal)0.5,
  94. PercentageValue = appendixEntity.Value.HasValue && appendixEntity.Percentage.HasValue
  95. ? appendixEntity.Value.Value * appendixEntity.Percentage.Value
  96. : 0,
  97. OfferingNumber = appendixEntity.OfferingNumber,
  98. OfferingDate = appendixEntity.OfferingDate,
  99. OfferingValue = appendixEntity.Value.Value,
  100. RelationOfferingToDeviations =
  101. appendixEntity.Value.HasValue && appendixEntity.Deviations.Any()
  102. ? appendixEntity.Deviations
  103. .Sum(r => r.Value.HasValue && r.Percentage.HasValue ? r.Value.Value * r.Percentage.Value : 0) != 0
  104. ? (decimal?)Convert.ToDecimal(
  105. (appendixEntity.Value.Value * appendixEntity.Percentage.Value) /
  106. (appendixEntity.Deviations
  107. .Sum(r => r.Value.HasValue && r.Percentage.HasValue
  108. ? r.Value.Value * r.Percentage.Value
  109. : 0) == 0
  110. ? 1
  111. : appendixEntity.Deviations
  112. .Sum(r => r.Value.HasValue && r.Percentage.HasValue
  113. ? r.Value.Value * r.Percentage.Value
  114. : 0)))
  115. : null
  116. : null,
  117. NegotiationDate = appendixEntity.NegotiationDate,
  118. OfferingToNegotiationDate = appendixEntity.NegotiationDate.HasValue && appendixEntity.OfferingDate.HasValue
  119. ? appendixEntity.NegotiationDate - appendixEntity.OfferingDate
  120. : null,
  121. NegotiationValue = appendixEntity.NegotiationValue,
  122. RelationOfferingToNegotiation =
  123. appendixEntity.Percentage.HasValue && appendixEntity.NegotiationValue.HasValue
  124. ? appendixEntity.NegotiationValue /
  125. ((appendixEntity.Value.Value * appendixEntity.Percentage.Value) == 0
  126. ? 1
  127. : (appendixEntity.Value.Value * appendixEntity.Percentage.Value))
  128. : null,
  129. ProtocolExists = appendixEntity.ProtocolExists,
  130. OrderNumber = appendixEntity.OrderNumber,
  131. OrderDate = appendixEntity.OrderDate,
  132. Comment = appendixEntity.Comment,
  133. OrderInvoiceCreated = appendixEntity.OrderInvoiceCreated,
  134. OrderInvoiceCreatedDescription =
  135. appendixEntity.OrderInvoiceCreated
  136. ? "Ja"
  137. : "Nein",
  138. StateId = appendixEntity.StateId,
  139. State = appendixEntity.State == null
  140. ? null
  141. : StateDataModel.FromState(appendixEntity.State, false),
  142. IsRemaining = remainingAppendixStatuses.Contains(appendixEntity.StateId ?? 0)
  143. ? "Ja"
  144. : "Nein",
  145. StateDescription = appendixEntity.State == null
  146. ? null
  147. : appendixEntity.State.Description,
  148. SiteId = appendixEntity.SiteId,
  149. SiteDescription = appendixEntity.Site == null
  150. ? null
  151. : appendixEntity.Site.Description,
  152. SiteCustomNumber = appendixEntity.Site == null
  153. ? null
  154. : appendixEntity.Site.CustomNumber,
  155. UserDescriptions = appendixEntity.Site == null
  156. ? null
  157. : appendixEntity.Site.Users
  158. .Select(r => new
  159. {
  160. LastName = r.Lastname,
  161. Roles = String.Join(", ", r.Roles.Select(u => u.Description))
  162. })
  163. .OrderBy(r => r.Roles)
  164. .Select(r => String.Format("{0}|({1})", r.LastName, r.Roles))
  165. .ToList(),
  166. UserDescription = appendixEntity.Site == null
  167. ? String.Empty
  168. : String.Join(", ",
  169. appendixEntity.Site.Users
  170. .Select(r => new
  171. {
  172. LastName = r.Lastname,
  173. Roles = String.Join(", ", r.Roles.Select(u => u.Description))
  174. })
  175. .Select(u => String.Format("{0} ({1})", u.LastName, u.Roles))),
  176. CategoryValueEntities =
  177. appendixEntity.CategoryValues
  178. .Select(r =>
  179. new CategoryValueDataModel
  180. {
  181. CategoryId = r.CategoryId,
  182. Description = String.Format("{0} - {1:c2}", r.Category.Description, r.Value),
  183. Value = r.Value
  184. })
  185. .ToList(),
  186. DeviationValues =
  187. appendixEntity.Deviations
  188. .Select(r => r.Id)
  189. .ToList(),
  190. DeviationDescriptions =
  191. appendixEntity.Deviations
  192. .Select(r => r.CustomNumber)
  193. .ToList(),
  194. DeviationDescription =
  195. String.Join(", ",
  196. appendixEntity.Deviations
  197. .Select(d => d.CustomNumber)),
  198. Invoices =
  199. appendixEntity.Invoices
  200. .Select(i => InvoiceDataModel.FromInvoice(i, false))
  201. .ToList()
  202. };
  203. foreach (var disturbance in model.CategoryValueEntities)
  204. {
  205. var json = JsonConvert.SerializeObject(disturbance);
  206. disturbance.Json = json;
  207. model.CategoryEntities.Add(json);
  208. }
  209. return model;
  210. }
  211. public Core.Domain.Appendix.Appendix ToAppendix()
  212. {
  213. return new Core.Domain.Appendix.Appendix
  214. {
  215. Id = this.Id,
  216. CustomNumber = this.CustomNumber,
  217. Description = this.Description,
  218. Percentage = this.Percentage,
  219. StateId = this.StateId,
  220. Value = this.OfferingValue,
  221. OfferingNumber = this.OfferingNumber,
  222. OfferingDate = this.OfferingDate,
  223. NegotiationDate = this.NegotiationDate,
  224. NegotiationValue = this.NegotiationValue,
  225. ProtocolExists = this.ProtocolExists,
  226. OrderNumber = this.OrderNumber,
  227. OrderDate = this.OrderDate,
  228. Comment = this.Comment,
  229. SiteId = this.SiteId
  230. };
  231. }
  232. }
  233. }