AppendixDataModel.cs 7.8 KB

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