AppendixDataModel.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 string 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 = (decimal)1.0
  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)0.5,
  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. ? appendixEntity.Deviations
  89. .Sum(r => r.Value.HasValue && r.Percentage.HasValue ? r.Value.Value * r.Percentage.Value : 0) != 0
  90. ? (decimal?)Convert.ToDecimal(
  91. (appendixEntity.Value.Value * appendixEntity.Percentage.Value) / appendixEntity.Deviations
  92. .Sum(r => r.Value.HasValue && r.Percentage.HasValue
  93. ? r.Value.Value * r.Percentage.Value
  94. : 0))
  95. : null
  96. : null,
  97. NegotiationDate = appendixEntity.NegotiationDate,
  98. NegotiationValue = appendixEntity.NegotiationValue,
  99. RelationOfferingToNegotiation =
  100. appendixEntity.Percentage.HasValue && appendixEntity.NegotiationValue.HasValue
  101. ? appendixEntity.NegotiationValue /
  102. (appendixEntity.Value == 0
  103. ? 1
  104. : (appendixEntity.Value.Value * appendixEntity.Percentage.Value))
  105. : null,
  106. ProtocolExists = appendixEntity.ProtocolExists,
  107. OrderNumber = appendixEntity.OrderNumber,
  108. OrderDate = appendixEntity.OrderDate,
  109. Comment = appendixEntity.Comment,
  110. OrderInvoiceCreated = appendixEntity.OrderInvoiceCreated,
  111. OrderInvoiceCreatedDescription =
  112. appendixEntity.OrderInvoiceCreated
  113. ? "Ja"
  114. : "Nein",
  115. StateId = appendixEntity.StateId,
  116. State = appendixEntity.State == null
  117. ? null
  118. : StateDataModel.FromState(appendixEntity.State, false),
  119. StateDescription = appendixEntity.State == null
  120. ? null
  121. : appendixEntity.State.Description,
  122. SiteId = appendixEntity.SiteId,
  123. SiteDescription = appendixEntity.Site == null
  124. ? null
  125. : appendixEntity.Site.Description,
  126. SiteCustomNumber = appendixEntity.Site == null
  127. ? null
  128. : appendixEntity.Site.CustomNumber,
  129. UserDescriptions = appendixEntity.Site == null
  130. ? null
  131. : appendixEntity.Site.Users
  132. .Select(r => new
  133. {
  134. LastName = r.Lastname,
  135. Roles = String.Join(", ", r.Roles.Select(u => u.Description))
  136. })
  137. .OrderBy(r => r.Roles)
  138. .Select(r => String.Format("{0}|({1})", r.LastName, r.Roles))
  139. .ToList(),
  140. UserDescription = appendixEntity.Site == null
  141. ? String.Empty
  142. : String.Join(", ",
  143. appendixEntity.Site.Users
  144. .Select(r => new
  145. {
  146. LastName = r.Lastname,
  147. Roles = String.Join(", ", r.Roles.Select(u => u.Description))
  148. })
  149. .Select(u => String.Format("{0} ({1})", u.LastName, u.Roles))),
  150. CategoryValueEntities =
  151. appendixEntity.CategoryValues
  152. .Select(r =>
  153. new CategoryValueDataModel
  154. {
  155. CategoryId = r.CategoryId,
  156. Description = String.Format("{0} - {1:c2}", r.Category.Description, r.Value),
  157. Value = r.Value
  158. })
  159. .ToList(),
  160. DeviationValues =
  161. appendixEntity.Deviations
  162. .Select(r => r.Id)
  163. .ToList(),
  164. DeviationDescriptions =
  165. appendixEntity.Deviations
  166. .Select(r => r.CustomNumber)
  167. .ToList(),
  168. DeviationDescription =
  169. String.Join(", ",
  170. appendixEntity.Deviations
  171. .Select(d => d.CustomNumber)),
  172. Invoices =
  173. appendixEntity.Invoices
  174. .Select(i => InvoiceDataModel.FromInvoice(i, false))
  175. .ToList()
  176. };
  177. foreach (var disturbance in model.CategoryValueEntities)
  178. {
  179. var json = JsonConvert.SerializeObject(disturbance);
  180. disturbance.Json = json;
  181. model.CategoryEntities.Add(json);
  182. }
  183. return model;
  184. }
  185. public Core.Domain.Appendix.Appendix ToAppendix()
  186. {
  187. return new Core.Domain.Appendix.Appendix
  188. {
  189. Id = this.Id,
  190. CustomNumber = this.CustomNumber,
  191. Description = this.Description,
  192. Percentage = this.Percentage,
  193. StateId = this.StateId,
  194. Value = this.OfferingValue,
  195. OfferingNumber = this.OfferingNumber,
  196. OfferingDate = this.OfferingDate,
  197. NegotiationDate = this.NegotiationDate,
  198. NegotiationValue = this.NegotiationValue,
  199. ProtocolExists = this.ProtocolExists,
  200. OrderNumber = this.OrderNumber,
  201. OrderDate = this.OrderDate,
  202. Comment = this.Comment,
  203. SiteId = this.SiteId
  204. };
  205. }
  206. }
  207. }