Advertisement
Guest User

BDCollections-Totals.aspx.cs

a guest
Feb 25th, 2011
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 20.01 KB | None | 0 0
  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Collections;
  5. using System.Web;
  6. using System.Web.Security;
  7. using System.Web.UI;
  8. using System.Web.UI.WebControls;
  9. using System.Web.UI.WebControls.WebParts;
  10. using System.Web.UI.HtmlControls;
  11.  
  12. public partial class Totals : System.Web.UI.Page
  13. {
  14. private string DateTruncUnit = "day";
  15.  
  16. protected void Page_Load(object sender, EventArgs e)
  17. {
  18. if (Request.HttpMethod == "POST")
  19. Response.Redirect("Totals.aspx");
  20. if (Request["qDate"]!=null)
  21. {
  22. DateSelector.SelectedDate = System.DateTime.Parse(Request["qDate"].ToString());
  23. }
  24. else
  25. {
  26. DateSelector.DayStyle.BackColor = System.Drawing.ColorTranslator.FromHtml("#660000");
  27. DateSelector.DayStyle.ForeColor = System.Drawing.ColorTranslator.FromHtml("#efefe7");
  28. if (DateSelector.SelectedDate <= DateTime.MinValue)
  29. DateSelector.SelectedDate = DateTime.Today;
  30. }
  31.  
  32. LoadTicker();
  33. }
  34.  
  35. private void LoadTicker()
  36. {
  37. DBFunctions DBc = new DBFunctions(Context.User.Identity.Name);
  38. Npgsql.NpgsqlDataReader DBcTickers = DBc.SelectTickers();
  39. string TickerPrefix = "";
  40. string TickerText = "";
  41. while (DBcTickers.Read())
  42. {
  43. TickerText += TickerPrefix+DBcTickers.GetValue(1).ToString();
  44. TickerPrefix = ConfigurationManager.AppSettings["TickerSeparator"].Replace(" ","&nbsp;");
  45. }
  46. ClientScript.RegisterStartupScript(typeof(Page), "TickerScript", TickerScript(TickerText));
  47. }
  48.  
  49. protected override void OnPreRender(EventArgs e)
  50. {
  51.  
  52. if (Request["PeriodUnit"]!=null)
  53. this.DateTruncUnit = Request["PeriodUnit"];
  54.  
  55. base.OnPreRender(e);
  56.  
  57. //Check if Final Totals are supposed to be displayed at Graph by default
  58. if (Request.UrlReferrer==null ||(Request.UrlReferrer.AbsolutePath.Contains("Totals.aspx") != true && ConfigurationManager.AppSettings["GraphFinalTotalsByDefault"].ToString() == "true"))
  59. this.chkFinalTotalsOnGraph.Checked = true;
  60.  
  61. //Update Chart Image Element according to date selected on the Calendar
  62. this.ChartIMG.ImageUrl = "Chart.aspx?FinalTotals="+chkFinalTotalsOnGraph.Checked.ToString()+"&TotalsDate=" + DateSelector.SelectedDate.ToLongDateString() + "&PeriodUnit=" + this.DateTruncUnit;
  63. this.ChartIMG.Attributes.Add("onload", "document.getElementById('ChartIMG').tags=document.getElementById('ChartIMG').src; document.getElementById('ChartIMG').src+='\\u0026width='+(document.body.clientWidth-document.getElementById('LoginInfoTable').offsetWidth-20)+'\\u0026height='+(document.body.clientHeight-document.getElementById('tickerdiv').offsetHeight-15);document.getElementById('ChartIMG').onload=null;");
  64.  
  65. //Determine if graph should be displayed in a new window
  66. if (ConfigurationManager.AppSettings["GraphNewWindow"] == "true")
  67. {
  68. if (ConfigurationManager.AppSettings["GraphDisplayTicker"] != "true")
  69. GraphB.OnClientClick = "window.open('" + Microsoft.JScript.GlobalObject.encodeURI(this.ChartIMG.ImageUrl) + "\\u0026width='+(document.body.clientWidth-44)+'\\u0026height='+(document.body.clientHeight-40),null,'width='+(document.body.clientWidth)+',status=no,left=0,top=0,scrollbars=no,resizable=yes,height='+(document.body.clientHeight)); return false;";
  70. else
  71. GraphB.OnClientClick = "window.open('" + Microsoft.JScript.GlobalObject.encodeURI("Totals.aspx?PeriodUnit="+this.DateTruncUnit+"&graphMode=true&qDate=" + DateSelector.SelectedDate.ToShortDateString()) + "',null,'width='+(document.body.clientWidth)+',status=no,left=0,top=0,scrollbars=no,resizable=yes,height='+(document.body.clientHeight)); return false;";
  72. this.MultiView1.ActiveViewIndex = 1;
  73. this.GraphB.Text = "Graph";
  74. GraphB.UseSubmitBehavior = false;
  75. }
  76. else
  77. {
  78. GraphB.OnClientClick = null;
  79. ChartIMG.Attributes.Add("onclick", "window.open('" + Microsoft.JScript.GlobalObject.encodeURI(this.ChartIMG.ImageUrl) + "\\u0026width='+(document.body.clientWidth-44)+'\\u0026height='+(document.body.clientHeight-40),null,'width='+(document.body.clientWidth)+',status=no,left=0,top=0,scrollbars=no,resizable=yes,height='+(document.body.clientHeight)); return false;");
  80. }
  81.  
  82. if (Request["graphMode"] != null && Request["graphMode"].ToString() == "true")
  83. {
  84. this.MultiView1.ActiveViewIndex = 0;
  85. LoginInfoTD.Style.Add("display","none");
  86. }
  87.  
  88. chkFinalTotalsOnGraph.Attributes.Add("onclick", "submit();");
  89.  
  90. //Check if user should see the Admin button
  91. if (!Roles.IsUserInRole("Admin"))
  92. this.AdminB.Visible = false;
  93.  
  94.  
  95. if (!Roles.IsUserInRole("Manager"))
  96. { FormsAuthentication.SignOut(); Response.Redirect("default.aspx"); }
  97.  
  98. //Get user groups
  99. string adPath = ConfigurationManager.AppSettings["ActiveDirectoryUserGroupsPath"];
  100. FormsAuth.LdapGroups adAuth = new FormsAuth.LdapGroups(adPath);
  101. SortedList groups = adAuth.GroupList();
  102.  
  103. //Check how to present usernames
  104. bool userDisplayName=false;
  105. if (ConfigurationManager.AppSettings["TotalsUsernameDisplayType"].ToString() == "displayname")
  106. userDisplayName = true;
  107.  
  108. //Create DB connection
  109. DBFunctions DBc = new DBFunctions(Context.User.Identity.Name);
  110.  
  111. //Select Periods from DB
  112. Npgsql.NpgsqlDataReader DBcPeriods = DBc.SelectPeriods(true);
  113.  
  114. //Select totals from DB
  115. Hashtable DBcTotals = DBc.GetCollectionsTotals(DateSelector.SelectedDate.ToLongDateString(),this.DateTruncUnit);
  116. Hashtable DBcGrandTotalsRecord;
  117. Hashtable DBcTotalsRows=new Hashtable();
  118. int PeriodsCount=0;
  119. foreach (DictionaryEntry userkey in DBcTotals)
  120. {
  121. DBcTotalsRows.Add(userkey.Key.ToString(), "");
  122. }
  123.  
  124. //Put Periods to the PeriodsHeaderPlaceholder and get Grand Totals to GrandTotalsPlaceholder
  125. Literal lGT = new Literal();
  126. DBcGrandTotalsRecord = (Hashtable)DBcTotals["*"];
  127. while (DBcPeriods.Read())
  128. {
  129. Literal l1 = new Literal();
  130. l1.Text = "<td class='TdTotalsHeadersCell'>" + DBcPeriods.GetValue(1).ToString() + "</td>";
  131. PeriodsHeadersPlaceholder.Controls.Add(l1);
  132. lGT.Text += "<td class='TdGrandTotalsCell'>";
  133. if (DBcGrandTotalsRecord[DBcPeriods.GetValue(0).ToString()]!=null)
  134. lGT.Text += DBcGrandTotalsRecord[DBcPeriods.GetValue(0).ToString()];
  135. else
  136. lGT.Text += 0;
  137. lGT.Text += "</td>";
  138. foreach (DictionaryEntry userkey in DBcTotals)
  139. {
  140. Hashtable DBcRecord = (Hashtable)DBcTotals[userkey.Key.ToString()];
  141. DBcTotalsRows[userkey.Key.ToString()] += "<td class=\"TdTotalsCell\">" + (DBcRecord[DBcPeriods.GetValue(0).ToString()] == null ? "0" : DBcRecord[DBcPeriods.GetValue(0).ToString()].ToString()) + "</td>";
  142. }
  143. foreach (DictionaryEntry groupEl in groups)
  144. {
  145. SortedList groupUsers = (SortedList)groupEl.Value;
  146. ArrayList groupTotalPeriods;
  147. if (groupUsers["*Totals"] == null)
  148. {
  149. groupTotalPeriods = new ArrayList();
  150. groupUsers.Add("*Totals", groupTotalPeriods);
  151. }
  152. else
  153. {
  154. groupTotalPeriods = (ArrayList) groupUsers["*Totals"];
  155. }
  156. foreach (DictionaryEntry groupUserEl in groupUsers)
  157. {
  158. Hashtable DBcRecord = (Hashtable)DBcTotals[groupUserEl.Key.ToString()];
  159. if (DBcTotalsRows[groupUserEl.Key.ToString()] != null)
  160. {
  161. if (groupTotalPeriods.Count == PeriodsCount)
  162. {
  163. groupTotalPeriods.Add(Int32.Parse(DBcRecord[DBcPeriods.GetValue(0).ToString()] == null ? "0" : DBcRecord[DBcPeriods.GetValue(0).ToString()].ToString()));
  164. }
  165. else
  166. {
  167. int groupTotalPeriodsVal = (int)groupTotalPeriods[PeriodsCount];
  168. groupTotalPeriodsVal += Int32.Parse(DBcRecord[DBcPeriods.GetValue(0).ToString()] == null ? "0" : DBcRecord[DBcPeriods.GetValue(0).ToString()].ToString());
  169. groupTotalPeriods[PeriodsCount] = groupTotalPeriodsVal;
  170. }
  171. }
  172. }
  173. }
  174. PeriodsCount++;
  175. }
  176. lGT.Text += "<td class='TdGrandTotalsCell'>";
  177. if (DBcGrandTotalsRecord["-1"].ToString().Length>0 )
  178. lGT.Text += DBcGrandTotalsRecord["-1"];
  179. else
  180. lGT.Text += 0;
  181. lGT.Text+="</td>";
  182. GrandTotalsPlaceholder.Controls.Add(lGT);
  183. Literal lT = new Literal();
  184.  
  185. foreach (DictionaryEntry groupEl in groups)
  186. {
  187. lT.Text += "<tr><td class=\"TdGroupCell\">"+groupEl.Key.ToString()+"</td><td class=\"TdTotalsCell\" colspan=\""+(PeriodsCount+1)+"\">&nbsp;</td></tr>";
  188. SortedList groupUsers = (SortedList)groupEl.Value;
  189. int GroupFinalTotal = 0;
  190. foreach (DictionaryEntry groupUserEl in groupUsers)
  191. {
  192. if (groupUserEl.Key.ToString() != "*Totals")
  193. {
  194. Hashtable DBcRecord = (Hashtable)DBcTotals[groupUserEl.Key.ToString()];
  195. try
  196. {
  197. if (DBcTotalsRows[groupUserEl.Key.ToString()] != null)
  198. {
  199. if (DBcRecord["-1"] != null)
  200. {
  201. GroupFinalTotal += Int32.Parse(DBcRecord["-1"].ToString());
  202. if (userDisplayName==true)
  203. lT.Text += "<tr><td class='TdUserTotalsCell'>" + groupUserEl.Value.ToString() + "</td>" + DBcTotalsRows[groupUserEl.Key.ToString()].ToString() + "<td class=\"TdFinalTotalsCell\">" + DBcRecord["-1"] + "</td></tr>";
  204. else
  205. lT.Text += "<tr><td class='TdUserTotalsCell'>" + groupUserEl.Key.ToString() + "</td>" + DBcTotalsRows[groupUserEl.Key.ToString()].ToString() + "<td class=\"TdFinalTotalsCell\">" + DBcRecord["-1"] + "</td></tr>";
  206. }
  207. else
  208. {
  209. GroupFinalTotal += 0;
  210. if (userDisplayName == true)
  211. lT.Text += "<tr><td class='TdUserTotalsCell'>" + groupUserEl.Value.ToString() + "</td>" + DBcTotalsRows[groupUserEl.Key.ToString()].ToString() + "<td class=\"TdFinalTotalsCell\">" + 0 + "</td></tr>";
  212. else
  213. lT.Text += "<tr><td class='TdUserTotalsCell'>" + groupUserEl.Key.ToString() + "</td>" + DBcTotalsRows[groupUserEl.Key.ToString()].ToString() + "<td class=\"TdFinalTotalsCell\">" + 0 + "</td></tr>";
  214. }
  215. }
  216. else
  217. {
  218. if (userDisplayName == true)
  219. lT.Text += "<tr><td class='TdUserTotalsCell'>" + groupUserEl.Value.ToString() + "</td>";
  220. else
  221. lT.Text += "<tr><td class='TdUserTotalsCell'>" + groupUserEl.Key.ToString() + "</td>";
  222. for (int i = 0; i < PeriodsCount; i++)
  223. lT.Text += "<td class=\"TdTotalsCell\">0</td>";
  224. lT.Text += "<td class=\"TdFinalTotalsCell\">0</td></tr>";
  225. }
  226. }
  227. catch (Exception exc)
  228. {
  229. Response.Write(exc);
  230. }
  231. }
  232. }
  233. lT.Text += "<tr><td class=\"TdGroupTotalsDescriptionCell\">" + groupEl.Key.ToString() + " Totals</td>";
  234.  
  235. ArrayList groupTotals = (ArrayList)groupUsers["*Totals"];
  236. for (int i = 0; i < PeriodsCount;i++ )
  237. {
  238. lT.Text += "<td class=\"TdGroupTotalsCell\">";
  239. try
  240. {
  241. lT.Text += groupTotals[i].ToString();
  242. }
  243. catch (Exception ex1)
  244. {
  245.  
  246. lT.Text += "&nbsp;<!-- " + ex1.Message.ToString() + " -->";
  247. }
  248. lT.Text += "</td>";
  249. }
  250. lT.Text += "<td class=\"TdGroupTotalsCell\">"+GroupFinalTotal+"</td></tr><tr><td class=\"TdTotalsCell\" colspan=\""+(PeriodsCount+2)+"\">&nbsp;</td></tr>";
  251.  
  252. }
  253.  
  254. TotalsTablePlaceHolder.Controls.Add(lT);
  255. }
  256.  
  257.  
  258.  
  259. override protected void OnInit(EventArgs e)
  260. {
  261. InitializeComponent();
  262. base.OnInit(e);
  263. }
  264.  
  265. private void InitializeComponent()
  266. {
  267. this.LogoutB.Click += new System.EventHandler(this.LogoutB_Click);
  268. this.AdminB.Click += new System.EventHandler(this.AdminB_Click);
  269. this.CollectionsB.Click += new System.EventHandler(this.CollectionsB_Click);
  270. this.MonthlyReportB.Click += new System.EventHandler(this.MonthlyReportB_Click);
  271. if (ConfigurationManager.AppSettings["GraphNewWindow"].ToString()!="true")
  272. this.GraphB.Click += new System.EventHandler(this.GraphB_Click);
  273. }
  274.  
  275. protected void LogoutB_Click(object sender, EventArgs e)
  276. {
  277. FormsAuthentication.SignOut();
  278. Response.Redirect("Totals.aspx");
  279. FormsAuthentication.RedirectToLoginPage();
  280. }
  281.  
  282. protected void GraphB_Click(object sender, EventArgs e)
  283. {
  284. if (this.MultiView1.ActiveViewIndex == 1)
  285. {
  286. this.MultiView1.ActiveViewIndex = 0;
  287. GraphB.Text = "Table";
  288. }
  289. else
  290. {
  291. this.MultiView1.ActiveViewIndex = 1;
  292. GraphB.Text = "Graph";
  293. }
  294. }
  295.  
  296. protected string TickerScript(string content)
  297. {
  298. IFormatProvider formatprovider = System.Globalization.CultureInfo.GetCultureInfo("en-US");
  299. int lowestTimeToRefresh=int.MaxValue;
  300. int currentRefreshInterval;
  301. System.TimeSpan RefreshHourCurrentEl;
  302. if (ConfigurationManager.AppSettings["TotalsRefreshHours"].ToString().Length > 4)
  303. {
  304. foreach (string RefreshHour in ConfigurationManager.AppSettings["TotalsRefreshHours"].ToString().Split(';'))
  305. {
  306. RefreshHourCurrentEl = System.DateTime.ParseExact(RefreshHour, "HH:mm", formatprovider).Subtract(System.DateTime.Now);
  307. currentRefreshInterval = (int)RefreshHourCurrentEl.TotalSeconds;
  308. if (currentRefreshInterval <= 0)
  309. {
  310. currentRefreshInterval += 86399;
  311. }
  312. if (currentRefreshInterval < lowestTimeToRefresh)
  313. {
  314. lowestTimeToRefresh = currentRefreshInterval;
  315. }
  316.  
  317. }
  318. }
  319.  
  320. int tickerRefreshRate = int.MaxValue;
  321.  
  322. string _str = "<script>";
  323. try
  324. {
  325. currentRefreshInterval=Int32.Parse(ConfigurationManager.AppSettings["TotalsRefreshRate"].ToString());
  326. if (currentRefreshInterval > 0 && currentRefreshInterval < lowestTimeToRefresh)
  327. lowestTimeToRefresh = currentRefreshInterval;
  328. }
  329. catch
  330. {
  331. }
  332.  
  333. try
  334. {
  335. tickerRefreshRate = Int32.Parse(ConfigurationManager.AppSettings["TickerRefreshRate"].ToString());
  336. }
  337. catch
  338. {
  339. }
  340.  
  341. _str+="var refreshInterval=setInterval('window.location.reload()'," + lowestTimeToRefresh.ToString() + @"000);";
  342. _str += "var content='" +content+ @"';
  343. var aw, mq, tWidth, lefttime;
  344. var x=setInterval('AJAXtickerLoad()'," + tickerRefreshRate + @"000);
  345.  
  346.  
  347. function AJAXtickerLoad()
  348. {
  349. try
  350. {
  351. var xmlHttpReq = false;
  352. var self = this;
  353. var strURL= 'ticker.aspx';
  354. // Mozilla/Safari
  355. if (window.XMLHttpRequest) {
  356. self.xmlHttpReq = new XMLHttpRequest();
  357. }
  358. // IE
  359. else if (window.ActiveXObject) {
  360. self.xmlHttpReq = new ActiveXObject('Microsoft.XMLHTTP');
  361. }
  362. self.xmlHttpReq.open('POST', strURL, true);
  363. self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  364. self.xmlHttpReq.onreadystatechange = function() {
  365. if (self.xmlHttpReq.readyState == 4) {
  366. if (content!=self.xmlHttpReq.responseText)
  367. {
  368. content=self.xmlHttpReq.responseText;
  369. if (lefttime)
  370. clearInterval(lefttime);
  371. startticker();
  372. }
  373. }
  374. }
  375. self.xmlHttpReq.send('type=all');
  376. }
  377. catch (err)
  378. {
  379. }
  380. }
  381.  
  382. function startticker()
  383. {
  384. if (document.getElementById) {
  385. tWidth=document.getElementById('tickerdiv').offsetWidth;
  386. var tHeight=document.getElementById('tickerdiv').offsetHeight;
  387. var tick = '<div style=\u0022position:relative;width:'+tWidth+';height:'+tHeight+';overflow:hidden;\u0022';
  388. tick +='><div id=\u0022mq\u0022 style=\u0022position:absolute;left:0px;top:0px;white-space:nowrap;\u0022><\/div><\/div>';
  389. document.getElementById('tickerdiv').innerHTML = tick; mq = document.getElementById('mq');
  390. mq.style.left=(parseInt(tWidth)+10)+'px'; mq.innerHTML='<span id=\u0022tx\u0022>'+unescape(content)+'<\/span>';
  391. aw = document.getElementById('tx').offsetWidth;
  392. if (content.length>0)
  393. lefttime=setInterval('scrollticker()',50);
  394. }
  395. }
  396.  
  397. function scrollticker()
  398. {
  399. mq.style.left = parseInt(mq.style.left)-" + ConfigurationManager.AppSettings["TickerSpeed"].ToString() + @"+'px';
  400. if (parseInt(mq.style.left)<tWidth-aw)
  401. {
  402. mq.innerHTML+='<span id=\u0022tx3\u0022>" + ConfigurationManager.AppSettings["TickerSeparator"].Replace(" ", "&nbsp;") +@"'+unescape(content)+'<\/span>';
  403. aw+=(document.getElementById('tx').offsetWidth+40);
  404. }
  405. }
  406.  
  407. startticker();
  408.  
  409. </script>";
  410.  
  411. return _str;
  412. }
  413.  
  414.  
  415.  
  416. protected void MonthlyReportB_Click(object sender, EventArgs e)
  417. {
  418. this.DateTruncUnit="month";
  419. DateSelector.DayStyle.BackColor = System.Drawing.ColorTranslator.FromHtml("#efefe7");
  420. DateSelector.DayStyle.ForeColor = System.Drawing.ColorTranslator.FromHtml("#000000");
  421. DateSelector.OtherMonthDayStyle.BackColor = System.Drawing.ColorTranslator.FromHtml("#660000");
  422. DateSelector.VisibleDate = DateSelector.SelectedDate;
  423. }
  424.  
  425. protected void AdminB_Click(object sender, EventArgs e)
  426. {
  427. if (Roles.IsUserInRole("Admin")) Response.Redirect("admin.aspx");
  428. }
  429.  
  430. protected void CollectionsB_Click(object sender, EventArgs e)
  431. {
  432. Response.Redirect("default.aspx");
  433. }
  434.  
  435. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement