AppendixDataModel.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 int? SiteId { get; set; }
  33. public string SiteDescription { get; set; }
  34. public string SiteCustomNumber { get; set; }
  35. public ICollection<int> DeviationValues { get; set; }
  36. public ICollection<string> DeviationDescriptions { get; set; }
  37. public string DeviationDescription { get; set; }
  38. public ICollection<string> CategoryEntities { get; set; }
  39. public ICollection<CategoryValueDataModel> CategoryValueEntities { get; set; }
  40. public string CategoryValuesDescription
  41. {
  42. get
  43. {
  44. if (CategoryValueEntities == null)
  45. return String.Empty;
  46. else
  47. return String.Join(", ", CategoryValueEntities.Select(d => d.Description));
  48. }
  49. }
  50. public string InvoiceDescription { get; set; }
  51. public ICollection<InvoiceDataModel> Invoices { get; set; }
  52. public AppendixDataModel()
  53. {
  54. DeviationValues = new List<int>();
  55. DeviationDescriptions = new List<string>();
  56. CategoryEntities = new List<string>();
  57. CategoryValueEntities = new List<CategoryValueDataModel>();
  58. Invoices = new List<InvoiceDataModel>();
  59. }
  60. public static AppendixDataModel FromAppendix(Core.Domain.Appendix.Appendix appendixEntity, bool newWhenIsNull)
  61. {
  62. if (appendixEntity == null && newWhenIsNull)
  63. return new AppendixDataModel
  64. {
  65. Id = -1,
  66. Percentage = 100
  67. };
  68. if (appendixEntity == null && !newWhenIsNull)
  69. throw new ArgumentNullException("appendixEntity", "Cannot create AppendixDataModel from NULL appendix entity.");
  70. var model = new AppendixDataModel
  71. {
  72. Id = appendixEntity.Id,
  73. CustomNumber = appendixEntity.CustomNumber,
  74. Description = appendixEntity.Description,
  75. Percentage = appendixEntity.Percentage.HasValue
  76. ? appendixEntity.Percentage.Value
  77. : (decimal)0.5,
  78. PercentageValue = appendixEntity.Value.HasValue && appendixEntity.Percentage.HasValue
  79. ? appendixEntity.Value.Value * appendixEntity.Percentage.Value
  80. : 0,
  81. OfferingNumber = appendixEntity.OfferingNumber,
  82. OfferingDate = appendixEntity.OfferingDate,
  83. OfferingValue = appendixEntity.Value.Value,
  84. RelationOfferingToDeviations =
  85. appendixEntity.Value.HasValue && appendixEntity.Deviations.Any()
  86. ? (decimal?)Convert.ToDecimal(
  87. appendixEntity.Value /
  88. (appendixEntity.Deviations
  89. .Sum(r => r.Value.HasValue && r.Percentage.HasValue ? r.Value.Value * r.Percentage.Value : 0) != 0
  90. ? appendixEntity.Deviations
  91. .Sum(r => r.Value.HasValue && r.Percentage.HasValue ? r.Value.Value * r.Percentage.Value : 0)
  92. : 1))
  93. : null,
  94. NegotiationDate = appendixEntity.NegotiationDate,
  95. NegotiationValue = appendixEntity.NegotiationValue,
  96. RelationOfferingToNegotiation =
  97. appendixEntity.Value.HasValue && appendixEntity.NegotiationValue.HasValue
  98. ? appendixEntity.NegotiationValue / appendixEntity.Value
  99. : null,
  100. ProtocolExists = appendixEntity.ProtocolExists,
  101. OrderNumber = appendixEntity.OrderNumber,
  102. OrderDate = appendixEntity.OrderDate,
  103. Comment = appendixEntity.Comment,
  104. OrderInvoiceCreated = appendixEntity.OrderInvoiceCreated,
  105. OrderInvoiceCreatedDescription =
  106. appendixEntity.OrderInvoiceCreated
  107. ? "Ja"
  108. : "Nein",
  109. StateId = appendixEntity.StateId,
  110. State = appendixEntity.State == null
  111. ? null
  112. : StateDataModel.FromState(appendixEntity.State, false),
  113. StateDescription = appendixEntity.State == null
  114. ? null
  115. : appendixEntity.State.Description,
  116. SiteId = appendixEntity.SiteId,
  117. SiteDescription = appendixEntity.Site == null
  118. ? null
  119. : appendixEntity.Site.Description,
  120. SiteCustomNumber = appendixEntity.Site == null
  121. ? null
  122. : appendixEntity.Site.CustomNumber,
  123. CategoryValueEntities =
  124. appendixEntity.CategoryValues
  125. .Select(r =>
  126. new CategoryValueDataModel
  127. {
  128. CategoryId = r.CategoryId,
  129. Description = String.Format("{0} - {1:c2}", r.Category.Description, r.Value),
  130. Value = r.Value
  131. })
  132. .ToList(),
  133. DeviationValues =
  134. appendixEntity.Deviations
  135. .Select(r => r.Id)
  136. .ToList(),
  137. DeviationDescriptions =
  138. appendixEntity.Deviations
  139. .Select(r => r.CustomNumber)
  140. .ToList(),
  141. DeviationDescription =
  142. String.Join(", ",
  143. appendixEntity.Deviations
  144. .Select(d => d.CustomNumber)),
  145. Invoices =
  146. appendixEntity.Invoices
  147. .Select(i => InvoiceDataModel.FromInvoice(i, false))
  148. .ToList()
  149. };
  150. foreach (var disturbance in model.CategoryValueEntities)
  151. {
  152. var json = JsonConvert.SerializeObject(disturbance);
  153. disturbance.Json = json;
  154. model.CategoryEntities.Add(json);
  155. }
  156. return model;
  157. }
  158. public Core.Domain.Appendix.Appendix ToAppendix()
  159. {
  160. return new Core.Domain.Appendix.Appendix
  161. {
  162. Id = this.Id,
  163. CustomNumber = this.CustomNumber,
  164. Description = this.Description,
  165. Percentage = this.Percentage,
  166. StateId = this.StateId,
  167. Value = this.OfferingValue,
  168. OfferingNumber = this.OfferingNumber,
  169. OfferingDate = this.OfferingDate,
  170. NegotiationDate = this.NegotiationDate,
  171. NegotiationValue = this.NegotiationValue,
  172. ProtocolExists = this.ProtocolExists,
  173. OrderNumber = this.OrderNumber,
  174. OrderDate = this.OrderDate,
  175. Comment = this.Comment,
  176. SiteId = this.SiteId
  177. };
  178. }
  179. }
  180. }