AppendixDataModel.cs 10 KB

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