AppendixDataModel.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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
  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)))
  84. : null,
  85. NegotiationDate = appendixEntity.NegotiationDate,
  86. NegotiationValue = appendixEntity.NegotiationValue,
  87. RelationOfferingToNegotiation =
  88. appendixEntity.Value.HasValue && appendixEntity.NegotiationValue.HasValue
  89. ? appendixEntity.NegotiationValue / appendixEntity.Value
  90. : null,
  91. ProtocolExists = appendixEntity.ProtocolExists,
  92. OrderNumber = appendixEntity.OrderNumber,
  93. OrderDate = appendixEntity.OrderDate,
  94. Comment = appendixEntity.Comment,
  95. OrderInvoiceCreated = appendixEntity.OrderInvoiceCreated,
  96. StateId = appendixEntity.StateId,
  97. StateDescription = appendixEntity.State == null
  98. ? null
  99. : appendixEntity.State.Description,
  100. SiteId = appendixEntity.SiteId,
  101. SiteDescription = appendixEntity.Site == null
  102. ? null
  103. : appendixEntity.Site.Description,
  104. CategoryValueEntities =
  105. appendixEntity.CategoryValues
  106. .Select(r =>
  107. new CategoryValueDataModel
  108. {
  109. CategoryId = r.CategoryId,
  110. Description = String.Format("{0} - {1:c2}", r.Category.Description, r.Value),
  111. Value = r.Value
  112. })
  113. .ToList(),
  114. DeviationValues =
  115. appendixEntity.Deviations
  116. .Select(r => r.Id)
  117. .ToList(),
  118. DeviationDescriptions =
  119. appendixEntity.Deviations
  120. .Select(r => r.CustomNumber)
  121. .ToList(),
  122. DeviationDescription =
  123. String.Join(", ",
  124. appendixEntity.Deviations
  125. .Select(d => d.CustomNumber))
  126. };
  127. foreach (var disturbance in model.CategoryValueEntities)
  128. {
  129. var json = JsonConvert.SerializeObject(disturbance);
  130. disturbance.Json = json;
  131. model.CategoryEntities.Add(json);
  132. }
  133. return model;
  134. }
  135. public Core.Domain.Appendix.Appendix ToAppendix()
  136. {
  137. return new Core.Domain.Appendix.Appendix
  138. {
  139. Id = this.Id,
  140. CustomNumber = this.CustomNumber,
  141. Description = this.Description,
  142. Percentage = this.Percentage,
  143. StateId = this.StateId,
  144. Value = this.OfferingValue,
  145. OfferingNumber = this.OfferingNumber,
  146. OfferingDate = this.OfferingDate,
  147. NegotiationDate = this.NegotiationDate,
  148. NegotiationValue = this.NegotiationValue,
  149. ProtocolExists = this.ProtocolExists,
  150. OrderNumber = this.OrderNumber,
  151. OrderDate = this.OrderDate,
  152. Comment = this.Comment,
  153. SiteId = this.SiteId
  154. };
  155. }
  156. }
  157. }