AppendixDataModel.cs 8.1 KB

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