/Web/BSCMFF.Core/BusinessLayer/BSCTreeListManager.cs

# · C# · 298 lines · 203 code · 40 blank · 55 comment · 19 complexity · 1e5840fa46582be8c47998911c59aae3 MD5 · raw file

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using BSCMFF.Core.DataAccessLayer;
  6. using System.Collections;
  7. namespace BSCMFF.Core.BusinessLayer
  8. {
  9. /// <summary>
  10. /// Class for managing BSCTreeList items.
  11. /// </summary>
  12. /// <remarks></remarks>
  13. public class BSCTreeListManager
  14. {
  15. /// <summary>
  16. /// Gets the data by date.
  17. /// </summary>
  18. /// <param name="date">The date.</param>
  19. /// <returns></returns>
  20. /// <remarks></remarks>
  21. public static List<BSCTreeListItem> GetDataByDate(DateTime date)
  22. {
  23. List<BSCTreeListItem> list = new List<BSCTreeListItem>();
  24. BSCEntities context = new BSCEntities();
  25. AddKPIs(date, list, context);
  26. AddPerspectives(list, context);
  27. // -- add Main TreeListItem
  28. BSCTreeListItem bli = new BSCTreeListItem();
  29. bli.ID = "0";
  30. bli.Name = "Balanced Scorecard";
  31. bli.ItemType = "bsc";
  32. Hashtable calculatedValues = CalculateValuesForBSC(list);
  33. bli.Progress = calculatedValues["progress"].ToString();
  34. bli.Performance = calculatedValues["performance"].ToString();
  35. bli.Start = calculatedValues["start"].ToString();
  36. bli.Current = calculatedValues["current"].ToString();
  37. bli.Target = calculatedValues["target"].ToString();
  38. bli.Min = "0";
  39. bli.Max = "100";
  40. //list.Add(new BSCTreeListItem(0, "Balanced Scorecard", "bsc", 0, 0));
  41. list.Add(bli);
  42. return list;
  43. }
  44. /// <summary>
  45. /// Calculates the values for BSCTreeList item.
  46. /// </summary>
  47. /// <param name="list">The list.</param>
  48. /// <returns></returns>
  49. /// <remarks></remarks>
  50. private static Hashtable CalculateValuesForBSC(List<BSCTreeListItem> list)
  51. {
  52. int weights = 0;
  53. double progress = 0;
  54. double performance = 0;
  55. double start = 0;
  56. double target = 0;
  57. double current = 0;
  58. foreach (BSCTreeListItem l in list)
  59. {
  60. //if (l.ParentID != "0")
  61. if(l.ItemType == "KPI")
  62. {
  63. weights += Convert.ToInt32(l.Weight);
  64. progress += Convert.ToDouble(l.Progress) * Convert.ToInt32(l.Weight);
  65. performance += Convert.ToDouble(l.Performance) * Convert.ToInt32(l.Weight);
  66. start += Convert.ToDouble(l.Start) * Convert.ToInt32(l.Weight);
  67. target += Convert.ToDouble(l.Target) * Convert.ToInt32(l.Weight);
  68. current += Convert.ToDouble(l.Current) * Convert.ToInt32(l.Weight);
  69. }
  70. }
  71. Hashtable ht = new Hashtable();
  72. if (weights == 0)
  73. {
  74. ht["progress"] = "0";
  75. ht["performance"] = "0";
  76. ht["start"] = "0";
  77. ht["target"] = "0";
  78. ht["current"] = "0";
  79. }
  80. else
  81. {
  82. ht["progress"] = Convert.ToDecimal(progress / weights).ToString("#0.00");
  83. ht["performance"] = Convert.ToDecimal(performance / weights).ToString("#0.00");
  84. ht["start"] = Convert.ToDecimal(start / weights).ToString("#0.00");
  85. ht["target"] = Convert.ToDecimal(target / weights).ToString("#0.00");
  86. ht["current"] = Convert.ToDecimal(current / weights).ToString("#0.00");
  87. }
  88. return ht;
  89. }
  90. /// <summary>
  91. /// Adds the perspectives.
  92. /// </summary>
  93. /// <param name="list">The list.</param>
  94. /// <param name="context">The context.</param>
  95. /// <remarks></remarks>
  96. private static void AddPerspectives(List<BSCTreeListItem> list, BSCEntities context)
  97. {
  98. /// -- add perspectives
  99. List<Perspective> persps = new List<Perspective>();
  100. persps = (from k in context.Perspectives
  101. select k).ToList();
  102. foreach (Perspective p in persps)
  103. {
  104. BSCTreeListItem bli = new BSCTreeListItem();
  105. bli.ID = "perspective" + p.Perspective_ID.ToString();
  106. bli.OrigID = p.Perspective_ID;
  107. bli.Name = p.Name;
  108. bli.ItemType = "Perspective";
  109. bli.ParentID = "0"; //root - BSC
  110. // "perspective" + p.Perspective_ID.ToString();
  111. //bli.Weight = p.Weight.ToString();
  112. //bli.Progress = CalculateProgressForPerspective(bli.ID, list); //kp.Progress.ToString();
  113. Hashtable calculatedValues = CalculateValuesForPerspective(bli.ID, list);
  114. bli.Progress = calculatedValues["progress"].ToString();
  115. bli.Performance = calculatedValues["performance"].ToString();
  116. bli.Start = calculatedValues["start"].ToString();
  117. bli.Current = calculatedValues["current"].ToString();
  118. bli.Target = calculatedValues["target"].ToString();
  119. bli.Min = "0";
  120. bli.Max = "100";
  121. //bli.Performance = kp.Performance.ToString();
  122. list.Add(bli);
  123. }
  124. }
  125. /// <summary>
  126. /// Adds the KPIs.
  127. /// </summary>
  128. /// <param name="date">The date.</param>
  129. /// <param name="list">The list of BSCTreeList item of type KPI.</param>
  130. /// <param name="context">The context.</param>
  131. /// <remarks></remarks>
  132. private static void AddKPIs(DateTime date, List<BSCTreeListItem> list, BSCEntities context)
  133. {
  134. var kpis = (from k in context.KPIs
  135. where k.DateCreated <= date
  136. select k).ToList();
  137. //List<KPI> kpiss = (from k in context.KPIs.in
  138. foreach (KPI k in kpis)
  139. {
  140. if (date == DateTime.Today) //values are in KPI
  141. {
  142. BSCTreeListItem bli = new BSCTreeListItem();
  143. bli.ID = k.KPI_ID.ToString();
  144. bli.OrigID = k.KPI_ID;
  145. bli.Name = k.Name;
  146. bli.Description = k.Description;
  147. bli.ItemType = "KPI";
  148. bli.ParentID = "perspective" + k.Perspective_FK.ToString();
  149. bli.Weight = k.Weight.ToString();
  150. bli.Progress = k.Progress.ToString("#0.00");
  151. bli.Performance = k.Performance.ToString("#0.00");
  152. // bli.Progress = kp.Progress.ToString();
  153. bli.Start = k.Start_Value.ToString();
  154. bli.Current = k.Current_Value.ToString();
  155. bli.Target = k.Target_Value.ToString();
  156. bli.Min = k.Min.ToString();
  157. bli.Max = k.Max.ToString();
  158. list.Add(bli);
  159. }
  160. else // values are archived in KPI_Progress
  161. {
  162. KPI_Progress kp;// = new KPI_Progress();
  163. kp = (from progress in context.KPI_Progress
  164. where progress.KPI_FK == k.KPI_ID && progress.Date == date //progress is archived every day automatically
  165. select progress).SingleOrDefault();
  166. //}
  167. BSCTreeListItem bli = new BSCTreeListItem();
  168. bli.ID = k.KPI_ID.ToString();
  169. bli.OrigID = k.KPI_ID;
  170. bli.Name = k.Name;
  171. bli.Description = k.Description;
  172. bli.ItemType = "KPI";
  173. bli.ParentID = "perspective" + k.Perspective_FK.ToString();
  174. if (kp != null)
  175. {
  176. bli.Weight = kp.Weight.ToString();
  177. bli.Progress = kp.Progress.ToString("#0.00");
  178. bli.Performance = kp.Performance.ToString("#0.00");
  179. // bli.Progress = kp.Progress.ToString();
  180. bli.Start = kp.Start_Value.ToString();
  181. bli.Current = kp.Current_Value.ToString();
  182. bli.Target = kp.Target_Value.ToString();
  183. bli.Min = kp.Min.ToString();
  184. bli.Max = kp.Max.ToString();
  185. }
  186. list.Add(bli);
  187. }
  188. }
  189. }
  190. /// <summary>
  191. /// Calculates the progress for perspective.
  192. /// </summary>
  193. /// <param name="p">The parent id of KPIs = ID of perspective.</param>
  194. /// <param name="list">The list.</param>
  195. /// <returns></returns>
  196. /// <remarks></remarks>
  197. private static string CalculateProgressForPerspective(string p, List<BSCTreeListItem> list)
  198. {
  199. int weights = 0;
  200. double progress = 0;
  201. foreach (BSCTreeListItem l in list)
  202. {
  203. if (l.ParentID == p)
  204. {
  205. weights += Convert.ToInt32(l.Weight);
  206. progress += Convert.ToDouble(l.Progress) * Convert.ToInt32(l.Weight);
  207. }
  208. }
  209. return weights == 0 ? "0" : Convert.ToDecimal(progress / weights).ToString("#0.00");
  210. }
  211. /// <summary>
  212. /// Calculates the values for perspective.
  213. /// </summary>
  214. /// <param name="p">The p.</param>
  215. /// <param name="list">The list.</param>
  216. /// <returns></returns>
  217. /// <remarks></remarks>
  218. private static Hashtable CalculateValuesForPerspective(string p, List<BSCTreeListItem> list)
  219. {
  220. int weights = 0;
  221. double progress = 0;
  222. double performance = 0;
  223. double start = 0;
  224. double target = 0;
  225. double current = 0;
  226. foreach (BSCTreeListItem l in list)
  227. {
  228. if (l.ParentID == p)
  229. {
  230. weights += Convert.ToInt32(l.Weight);
  231. progress += Convert.ToDouble(l.Progress) * Convert.ToInt32(l.Weight);
  232. performance += Convert.ToDouble(l.Performance) * Convert.ToInt32(l.Weight);
  233. start += Convert.ToDouble(l.Start) * Convert.ToInt32(l.Weight);
  234. target += Convert.ToDouble(l.Target) * Convert.ToInt32(l.Weight);
  235. current += Convert.ToDouble(l.Current) * Convert.ToInt32(l.Weight);
  236. }
  237. }
  238. Hashtable ht = new Hashtable();
  239. if(weights == 0)
  240. {
  241. ht["progress"] = "0";
  242. ht["performance"] = "0";
  243. ht["start"] = "0";
  244. ht["target"] = "0";
  245. ht["current"] = "0";
  246. }
  247. else
  248. {
  249. ht["progress"] = Convert.ToDecimal(progress / weights).ToString("#0.00");
  250. ht["performance"] = Convert.ToDecimal(performance / weights).ToString("#0.00");
  251. ht["start"] = Convert.ToDecimal(start / weights).ToString("#0.00");
  252. ht["target"] = Convert.ToDecimal(target / weights).ToString("#0.00");
  253. ht["current"] = Convert.ToDecimal(current / weights).ToString("#0.00");
  254. }
  255. return ht;
  256. }
  257. }
  258. }