TimeEntry.aspx.cs
上传用户:autodoor
上传日期:2022-08-04
资源大小:9973k
文件大小:17k
源码类别:

.net编程

开发平台:

Others

  1. using System;
  2. using System.Collections;
  3. using System.Data;
  4. using System.Text;
  5. using System.Web.UI.WebControls;
  6. using qminoa.BLL;
  7. using qminoa.Common.Data;
  8. using qminoa.BLL.PM;
  9. using qminoa.DA;
  10. namespace qminoa.Webs.PM
  11. {
  12. public class TimeEntry : qminoa.Webs.PageBase
  13. {
  14. protected qminoa.BLL.PM.TimeEntry _userInput = new qminoa.BLL.PM.TimeEntry(0, 0, 0, 0, DateTime.MinValue, null, -1M);
  15. protected DataTable _dayListTable = null;
  16. private PMUser  _user;
  17. private DateTime _weekEndingDate = DateTime.Today;
  18. private DateTime _weekStartingDate = DateTime.Today;
  19. protected System.Web.UI.WebControls.Button AddEntry;
  20. protected System.Web.UI.WebControls.CompareValidator CompareValidator1;
  21. protected System.Web.UI.WebControls.Button Cancel;
  22. protected System.Web.UI.WebControls.DropDownList CategoryList;
  23. protected System.Web.UI.WebControls.DropDownList Days;
  24. protected System.Web.UI.WebControls.TextBox Description;
  25. protected System.Web.UI.WebControls.TextBox Hours;
  26. protected System.Web.UI.WebControls.DropDownList ProjectList;
  27. protected System.Web.UI.WebControls.RangeValidator RangeValidator1;
  28. protected System.Web.UI.WebControls.RequiredFieldValidator Requiredfieldvalidator1;
  29. protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator2;
  30. protected System.Web.UI.WebControls.DataGrid TimeEntryGrid;
  31. protected System.Web.UI.WebControls.DropDownList UserList;
  32. protected System.Web.UI.WebControls.TextBox WeekEnding;
  33. protected System.Web.UI.WebControls.DropDownList BranchDropList;
  34. protected System.Web.UI.WebControls.DropDownList DepartmentDropList;
  35. private void Page_Load(object sender, System.EventArgs e)
  36. {
  37. this.PageBegin("项目管理",true);
  38. _user = new PMUser(Convert.ToInt16(this.Empid));
  39. if (!Page.IsPostBack)
  40. {
  41. BLL.PM.TimeEntry.FillCorrectStartEndDates(DateTime.Today, 
  42. ref _weekStartingDate, ref _weekEndingDate);
  43. WeekEnding.Text = _weekEndingDate.ToShortDateString();
  44. BindUserList();
  45. BindEntryFields();
  46. BindTimeSheet(_user.UserID, _weekStartingDate, _weekEndingDate);
  47. }
  48. else
  49. {
  50. BLL.PM.TimeEntry.FillCorrectStartEndDates(Convert.ToDateTime(WeekEnding.Text), 
  51. ref _weekStartingDate, ref _weekEndingDate);
  52. _dayListTable = BLL.PM.TimeEntry.GetWeek(Convert.ToDateTime(WeekEnding.Text));
  53. }
  54. }
  55. private void BindCategoryList()
  56. {
  57. if (ProjectList.SelectedItem != null) 
  58. {
  59. CategoryList.DataSource = Project.GetCategories(Convert.ToInt32(ProjectList.SelectedItem.Value));
  60. CategoryList.DataValueField = "CategoryID";
  61. CategoryList.DataTextField = "Abbreviation";
  62. CategoryList.DataBind();
  63. }
  64. }
  65. private void BindDates()
  66. {
  67. _dayListTable = BLL.PM.TimeEntry.GetWeek(Convert.ToDateTime(WeekEnding.Text));
  68. Days.DataSource = _dayListTable;
  69. Days.DataValueField = "Date";
  70. Days.DataTextField = "Day";
  71. Days.DataBind();
  72. Days.Items.FindByText(Convert.ToDateTime(DateTime.Today).ToString("ddd")).Selected= true;
  73. }
  74. private void BindEntryFields()
  75. {
  76. BindDates();
  77. BindProjectList();
  78. BindCategoryList();
  79. }
  80. private void BindProjectList()
  81. {
  82. if (ProjectList.SelectedItem != null)
  83. _userInput.ProjectID = Convert.ToInt32(ProjectList.SelectedItem.Value);
  84. ProjectList.DataSource = ListUserProjects();
  85. ProjectList.DataTextField = "Name";
  86. ProjectList.DataValueField = "ProjectID";
  87. ProjectList.DataBind();
  88. if (ProjectList.Items.FindByValue(_userInput.ProjectID.ToString()) != null)
  89. ProjectList.Items.FindByValue(_userInput.ProjectID.ToString()).Selected = true;
  90. }
  91. private void BindTimeSheet(int userID, DateTime start, DateTime end)
  92. {
  93. BindTimeSheet(userID, userID, start, end);
  94. }
  95. private void BindTimeSheet(int queryUserID, int userID, DateTime start, DateTime end)
  96. {
  97. TimeEntriesCollection entryList = BLL.PM.TimeEntry.GetEntries(queryUserID, userID, start, end);
  98. if (entryList != null) 
  99. {
  100. SortGridData(entryList, SortField, SortAscending);
  101. }
  102. TimeEntryGrid.DataSource = entryList;
  103. TimeEntryGrid.DataBind();
  104. }
  105. private void BindUserList()
  106. {
  107. DataTable empTB = (new EmpSystem()).GetEmpTB_By_EmpID(_user.UserID);
  108. DataRow row = empTB.Rows[0];
  109. int depID = (int)row[DepData.DEPID_FIELD];
  110. DataTable depTB = (new DepSystem()).GetDepTB_By_DepID(depID);
  111. row = depTB.Rows[0];
  112. int branchID = (int)row[DepData.BRANCHID_FIELD];
  113. AdminDB admin = new AdminDB();
  114. BranchDropList.DataSource=admin.GetAllBranch();
  115. BranchDropList.DataBind();
  116. BranchDropList.Items.FindByValue(branchID.ToString()).Selected=true;
  117. DepartmentDropList.DataSource=admin.GetDepByBranch(Int32.Parse(BranchDropList.SelectedItem.Value));
  118. DepartmentDropList.DataBind();
  119. DepartmentDropList.Items.FindByValue(depID.ToString()).Selected=true;
  120. UserList.DataSource = PMUser.GetUsers(_user.UserID, _user.Role,Int32.Parse(DepartmentDropList.SelectedItem.Value));
  121. UserList.DataBind();
  122. UserList.Items.FindByValue(_user.UserID.ToString()).Selected=true;
  123. }
  124. private void ClearEntryFields()
  125. {
  126. BindEntryFields();
  127. TimeEntryGrid.EditItemIndex = -1;
  128. BindTimeSheet(_user.UserID, Convert.ToInt32(UserList.SelectedItem.Value), _weekStartingDate, _weekEndingDate);
  129. Description.Text = string.Empty;
  130. Hours.Text = string.Empty;
  131. }
  132. protected CategoriesCollection ListGridCategories(int projectID)
  133. {
  134. return BLL.PM.Project.GetCategories(projectID);
  135. }
  136. protected ProjectsCollection ListUserProjects()
  137. {
  138. return BLL.PM.Project.GetProjects(_user.UserID, Convert.ToInt32(UserList.SelectedItem.Value));
  139. }
  140. private void SortGridData(TimeEntriesCollection list, string sortField, bool asc)
  141. {
  142. TimeEntriesCollection.TimeEntryFields sortCol = TimeEntriesCollection.TimeEntryFields.InitValue;
  143. switch(sortField)
  144. {
  145. case "EntryDate":
  146. sortCol = TimeEntriesCollection.TimeEntryFields.Day;
  147. break;
  148. case "ProjectName":
  149. sortCol = TimeEntriesCollection.TimeEntryFields.Project;
  150. break;
  151. case "CategoryName":
  152. sortCol = TimeEntriesCollection.TimeEntryFields.Category;
  153. break;
  154. case "Duration":
  155. sortCol = TimeEntriesCollection.TimeEntryFields.Hours;
  156. break;
  157. case "Description":
  158. sortCol = TimeEntriesCollection.TimeEntryFields.Description;
  159. break;
  160. default:
  161. break;
  162. }
  163. list.Sort(sortCol, asc);
  164. }
  165. #region Web Form Designer generated code
  166. override protected void OnInit(EventArgs e)
  167. {
  168. //
  169. // CODEGEN: This call is required by the ASP.NET Web Form Designer.
  170. //
  171. InitializeComponent();
  172. base.OnInit(e);
  173. }
  174. /// <summary>
  175. /// Required method for Designer support - do not modify
  176. /// the contents of this method with the code editor.
  177. /// </summary>
  178. private void InitializeComponent()
  179. {    
  180. this.WeekEnding.TextChanged += new System.EventHandler(this.WeekEnding_TextChanged);
  181. this.ProjectList.SelectedIndexChanged += new System.EventHandler(this.ProjectList_SelectedIndexChanged);
  182. this.AddEntry.Click += new System.EventHandler(this.AddEntry_Click);
  183. this.Cancel.Click += new System.EventHandler(this.Cancel_Click);
  184. this.BranchDropList.SelectedIndexChanged += new System.EventHandler(this.BranchDropList_SelectedIndexChanged);
  185. this.DepartmentDropList.SelectedIndexChanged += new System.EventHandler(this.DepartmentDropList_SelectedIndexChanged);
  186. this.UserList.SelectedIndexChanged += new System.EventHandler(this.UserList_OnChange);
  187. this.TimeEntryGrid.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.TimeEntryGrid_OnCancel);
  188. this.TimeEntryGrid.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.TimeEntryGrid_OnEdit);
  189. this.TimeEntryGrid.SortCommand += new System.Web.UI.WebControls.DataGridSortCommandEventHandler(this.TimeEntryGrid_Sort);
  190. this.TimeEntryGrid.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.TimeEntryGrid_OnUpdate);
  191. this.TimeEntryGrid.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.TimeEntryGrid_OnDelete);
  192. this.TimeEntryGrid.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.TimeEntryGrid_Itembound);
  193. this.Load += new System.EventHandler(this.Page_Load);
  194. }
  195. #endregion
  196. private void AddEntry_Click(object sender, System.EventArgs e)
  197. {
  198. Requiredfieldvalidator1.Validate();
  199. RequiredFieldValidator2.Validate();
  200. RangeValidator1.Validate();
  201. CompareValidator1.Validate();
  202. if (Requiredfieldvalidator1.IsValid && RequiredFieldValidator2.IsValid && CompareValidator1.IsValid && RangeValidator1.IsValid) 
  203. {
  204. BLL.PM.TimeEntry te = new BLL.PM.TimeEntry(0, 
  205. Convert.ToInt32(UserList.SelectedItem.Value), 
  206. Convert.ToInt32(ProjectList.SelectedItem.Value),
  207. Convert.ToInt32(CategoryList.SelectedItem.Value), 
  208. Convert.ToDateTime(Days.SelectedItem.Value), 
  209. Description.Text, Convert.ToDecimal(Hours.Text));
  210. te.Save(); 
  211. ClearEntryFields();
  212. }
  213. }
  214. private void Cancel_Click(object sender, System.EventArgs e)
  215. {
  216. ClearEntryFields();
  217. }
  218. private void ProjectList_SelectedIndexChanged(object sender, System.EventArgs e)
  219. {
  220. BindCategoryList();
  221. }
  222. private void TimeEntryGrid_Itembound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
  223. {
  224. if (e.Item.ItemType == ListItemType.EditItem) 
  225. {
  226. DropDownList currentCbo = (DropDownList) e.Item.FindControl("EntryDays");
  227. currentCbo.SelectedIndex = currentCbo.Items.IndexOf(currentCbo.Items.FindByText(_userInput.Day));
  228. currentCbo = (DropDownList) e.Item.FindControl("EntryProjects");
  229. currentCbo.SelectedIndex = currentCbo.Items.IndexOf(currentCbo.Items.FindByText(_userInput.ProjectName));
  230. currentCbo = (DropDownList) e.Item.FindControl("EntryCategories");
  231. currentCbo.SelectedIndex = currentCbo.Items.IndexOf(currentCbo.Items.FindByText(_userInput.CategoryName));
  232. }
  233. }
  234. protected void TimeEntryGrid_OnCancel(Object sender, DataGridCommandEventArgs e)
  235. {
  236. TimeEntryGrid.EditItemIndex = -1;
  237. BindTimeSheet(_user.UserID, Convert.ToInt32(UserList.SelectedItem.Value), _weekStartingDate, _weekEndingDate);
  238. }
  239. protected void TimeEntryGrid_OnDelete(Object sender, DataGridCommandEventArgs e)
  240. {
  241. int entryLogID = Convert.ToInt32(TimeEntryGrid.DataKeys[(int)e.Item.ItemIndex]);
  242. BLL.PM.TimeEntry.Remove(entryLogID);
  243. BindTimeSheet(_user.UserID, Convert.ToInt32(UserList.SelectedItem.Value), _weekStartingDate, _weekEndingDate);
  244. }
  245. protected void TimeEntryGrid_OnEdit(Object sender, DataGridCommandEventArgs e)
  246. {
  247. TimeEntryGrid.EditItemIndex = e.Item.ItemIndex;
  248. _userInput.Day = ((Label) e.Item.FindControl("EntryDay")).Text;
  249. _userInput.ProjectName = ((Label) e.Item.FindControl("EntryProject")).Text.Trim();
  250. _userInput.ProjectID = Convert.ToInt32(((Label) e.Item.FindControl("EntryProjectID")).Text);
  251. _userInput.CategoryName = ((Label) e.Item.FindControl("EntryCategory")).Text.Trim();
  252. _userInput.Duration = Convert.ToDecimal(((Label) e.Item.FindControl("EntryDuration")).Text);
  253. _userInput.Description = ((Label) e.Item.FindControl("GridDescription")).Text.Trim();
  254. BindTimeSheet(_user.UserID, Convert.ToInt32(UserList.SelectedItem.Value), _weekStartingDate, _weekEndingDate);
  255. }
  256. protected void TimeEntryGrid_OnUpdate(Object sender, DataGridCommandEventArgs e)
  257. {
  258. DataGridItem item = TimeEntryGrid.Items[TimeEntryGrid.EditItemIndex];
  259. RequiredFieldValidator required = (RequiredFieldValidator) item.FindControl("RequiredFieldValidatorGridHours");
  260. CompareValidator comparer = (CompareValidator) item.FindControl("CompareValidatorGridHours");
  261. required.Validate();
  262. comparer.Validate();
  263. if (required.IsValid && comparer.IsValid) 
  264. {
  265. int entryLogID;
  266. int userID;
  267. int projectID;
  268. int categoryID;
  269. DateTime taskDate;
  270. string description;
  271. decimal duration;
  272. BLL.PM.TimeEntry te = null;
  273. entryLogID = Convert.ToInt32(TimeEntryGrid.DataKeys[(int)e.Item.ItemIndex]);
  274. userID = Convert.ToInt32(UserList.SelectedItem.Value);
  275. projectID = Convert.ToInt32(((DropDownList) e.Item.FindControl("EntryProjects")).SelectedItem.Value);
  276. categoryID = Convert.ToInt32(((DropDownList) e.Item.FindControl("EntryCategories")).SelectedItem.Value);
  277. taskDate = Convert.ToDateTime(((DropDownList) e.Item.FindControl("EntryDays")).SelectedItem.Value);
  278. description = ((TextBox) e.Item.FindControl("EntryDescription")).Text;//TTSecurity.CleanStringRegex(((TextBox) e.Item.FindControl("EntryDescription")).Text);
  279. duration = Convert.ToDecimal(((TextBox) e.Item.FindControl("EntryHours")).Text);
  280. te = new BLL.PM.TimeEntry(entryLogID, userID, projectID, categoryID, taskDate, description, duration);
  281. te.Save();
  282. TimeEntryGrid.EditItemIndex = -1;
  283. BindTimeSheet(_user.UserID, Convert.ToInt32(UserList.SelectedItem.Value), _weekStartingDate, _weekEndingDate);
  284. }
  285. }
  286. private void TimeEntryGrid_Sort(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
  287. {
  288. TimeEntryGrid.EditItemIndex = -1;
  289. SortField = e.SortExpression;
  290. BindTimeSheet(_user.UserID, Convert.ToInt32(UserList.SelectedItem.Value), _weekStartingDate, _weekEndingDate);
  291. }
  292. protected void UserProjects_OnChange(object sender, System.EventArgs e)
  293. {
  294. DataGridItem item = TimeEntryGrid.Items[TimeEntryGrid.EditItemIndex];
  295. if (item != null)
  296. {
  297. TextBox txt = (TextBox) item.FindControl("EntryHours");
  298. _userInput.Duration = Convert.ToDecimal(txt.Text);
  299. TextBox txtDesc = (TextBox) item.FindControl("EntryDescription");
  300. _userInput.Description = txtDesc.Text;
  301. DropDownList EntryDays = (DropDownList)item.FindControl("EntryDays");
  302. _userInput.EntryDate = Convert.ToDateTime(EntryDays.SelectedItem.Value);
  303. _userInput.Day = EntryDays.SelectedItem.Text;
  304. }
  305. DropDownList EntryProjects = (DropDownList) sender;
  306. _userInput.ProjectID = Convert.ToInt32(EntryProjects.SelectedItem.Value);
  307. _userInput.ProjectName = EntryProjects.SelectedItem.Text;
  308. BindTimeSheet(_user.UserID, Convert.ToInt32(UserList.SelectedItem.Value), _weekStartingDate, _weekEndingDate);
  309. }
  310. private void UserList_OnChange(object sender, System.EventArgs e)
  311. {
  312. TimeEntryGrid.EditItemIndex = -1;
  313. BindTimeSheet(_user.UserID, Convert.ToInt32(UserList.SelectedItem.Value), _weekStartingDate, _weekEndingDate);
  314. BindEntryFields();
  315. }
  316. private void WeekEnding_TextChanged(object sender, System.EventArgs e)
  317. {
  318. TimeEntryGrid.EditItemIndex = -1;
  319. BLL.PM.TimeEntry.FillCorrectStartEndDates(Convert.ToDateTime(WeekEnding.Text), 
  320. ref _weekStartingDate, ref _weekEndingDate);
  321. WeekEnding.Text = _weekEndingDate.ToShortDateString();
  322. BindEntryFields();
  323. BindTimeSheet(_user.UserID, Convert.ToInt32(UserList.SelectedItem.Value), _weekStartingDate, _weekEndingDate);
  324. }
  325. private void BranchDropList_SelectedIndexChanged(object sender, System.EventArgs e)
  326. {
  327. AdminDB admin = new AdminDB();
  328. DepartmentDropList.DataSource=admin.GetDepByBranch(Int32.Parse(BranchDropList.SelectedItem.Value));
  329. DepartmentDropList.DataBind();
  330. if(DepartmentDropList.Items.Count !=0)
  331. {
  332. UserList.DataSource = PMUser.GetUsers(_user.UserID, _user.Role,Int32.Parse(DepartmentDropList.SelectedItem.Value));
  333. UserList.DataBind();
  334. UserList.Items[0].Selected = true;
  335. TimeEntryGrid.EditItemIndex = -1;
  336. BindTimeSheet(_user.UserID, Convert.ToInt32(UserList.SelectedItem.Value), _weekStartingDate, _weekEndingDate);
  337. BindEntryFields();
  338. }
  339. else
  340. {
  341. BindTimeSheet(_user.UserID, 0, _weekStartingDate, _weekEndingDate);
  342. UserList.Items.Clear();
  343. ProjectList.Items.Clear();
  344. }
  345. }
  346. private void DepartmentDropList_SelectedIndexChanged(object sender, System.EventArgs e)
  347. {
  348. if(DepartmentDropList.Items.Count !=0)
  349. {
  350. UserList.DataSource = PMUser.GetUsers(_user.UserID, _user.Role,Int32.Parse(DepartmentDropList.SelectedItem.Value));
  351. UserList.DataBind();
  352. UserList.Items[0].Selected = true;
  353. TimeEntryGrid.EditItemIndex = -1;
  354. BindTimeSheet(_user.UserID, Convert.ToInt32(UserList.SelectedItem.Value), _weekStartingDate, _weekEndingDate);
  355. BindEntryFields();
  356. }
  357. else
  358. {
  359. BindTimeSheet(_user.UserID, 0, _weekStartingDate, _weekEndingDate);
  360. UserList.Items.Clear();
  361. ProjectList.Items.Clear();
  362. }
  363. }
  364. string SortField 
  365. {
  366. get 
  367. {
  368. object o = ViewState["SortField"];
  369. if (o == null) 
  370. {
  371. return String.Empty;
  372. }
  373. return (string)o;
  374. }
  375. set 
  376. {
  377. if (value == SortField) 
  378. {
  379. SortAscending = !SortAscending;
  380. }
  381. ViewState["SortField"] = value;
  382. }
  383. }
  384. bool SortAscending 
  385. {
  386. get 
  387. {
  388. object o = ViewState["SortAscending"];
  389. if (o == null) 
  390. {
  391. return true;
  392. }
  393. return (bool)o;
  394. }
  395. set 
  396. {
  397. ViewState["SortAscending"] = value;
  398. }
  399. }
  400. }
  401. }