AppendixDataModel.cs 7.4 KB

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