AppendixDataModel.cs 9.6 KB

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