AppendixDataModel.cs 6.3 KB

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