/// <summary> /// Bind all timesheet of current user /// </summary> private void BindAllTimesheet() { try { using (SPSite site = new SPSite(SPContext.Current.Site.Url)) { using (SPWeb web = site.OpenWeb()) { SPList lstTimesheet = web.Lists[Constants.TimesheetListName]; if (lstTimesheet != null) { TimesheetRespository objRespository = new TimesheetRespository(); SPListItemCollection itemColl = objRespository.GetListItemsByAuthor(lstTimesheet, SPContext.Current.Web.CurrentUser.ID); if (itemColl != null && itemColl.Count > 0) { DataTable dtRecords = itemColl.GetDataTable(); DataView view = new DataView(dtRecords.DefaultView.ToTable()); view.Sort = "TimesheetDate Desc"; gvTimesheet.DataSource = view.ToTable(); gvTimesheet.DataBind(); } else { gvTimesheet.DataSource = null; gvTimesheet.DataBind(); } } else { ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Notify", "alert('" + Constants.TimesheetListName + " list not found.');", true); } } } } catch (Exception ex) { Common.HandleException(ex, SPContext.Current.Web.Url, "BindTimesheet", "ManageTimesheetUserControl", SPContext.Current.Web.CurrentUser.Name); lblMsg.Text = Constants.ErrorMsg; } }
/// <summary> /// Bind timsheet details /// </summary> /// <param name="timesheetId"></param> private void BindTimesheetData(int timesheetId) { try { using (SPSite site = new SPSite(SPContext.Current.Site.Url)) { using (SPWeb web = site.OpenWeb()) { TimesheetRespository objRepository = new TimesheetRespository(); SPListItem item = objRepository.GetListItemById(web, Constants.TimesheetListName, timesheetId); if (item != null) { lblCategory.Text = Convert.ToString(item["Category"]); lblDescription.Text = Convert.ToString(item["Description"]); lblDate.Text = Convert.ToDateTime(item["TimesheetDate"]).ToString("yyyy-MM-dd"); lblHours.Text = Convert.ToString(item["Hours"]); SPFieldMultiLineText multilineField = item.Fields.GetField("Description") as SPFieldMultiLineText; if (multilineField != null) { txtDescription.Text = multilineField.GetFieldValueAsText(item["Description"]); } ddlCategory.ClearSelection(); if (ddlCategory.Items.FindByValue(Convert.ToString(item["Category"])) != null) { ddlCategory.Items.FindByValue(Convert.ToString(item["Category"])).Selected = true; } dtDate.SelectedDate = Convert.ToDateTime(item["TimesheetDate"]); txtHours.Text = Convert.ToString(item["Hours"]); } } } } catch (Exception ex) { Common.HandleException(ex, SPContext.Current.Web.Url, "BindTimesheetData", "ManageTimesheetUserControl", SPContext.Current.Web.CurrentUser.Name); lblMsg.Text = Constants.ErrorMsg; } }
/// <summary> /// Delete button click event /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void lnkDelete_Click(object sender, EventArgs e) { try { bool dataSave = false; LinkButton btn = (LinkButton)sender; using (SPSite site = new SPSite(SPContext.Current.Site.Url)) { using (SPWeb web = site.OpenWeb()) { bool allowUnsafeUpdate = web.AllowUnsafeUpdates; try { web.AllowUnsafeUpdates = true; TimesheetRespository objRepository = new TimesheetRespository(); dataSave = objRepository.DeleteListItemById(web, Constants.TimesheetListName, Convert.ToInt32(btn.CommandArgument)); BindAllTimesheet(); lblMsg.Text = string.Empty; } finally { web.AllowUnsafeUpdates = allowUnsafeUpdate; } } } if (dataSave) { ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Notify", "alert('" + Constants.TimesheetDeletedMsg + "');", true); } } catch (Exception ex) { Common.HandleException(ex, SPContext.Current.Web.Url, "btnDelete_Click", "ManageTimesheetUserControl", SPContext.Current.Web.CurrentUser.Name); lblMsg.Text = Constants.ErrorMsg; } }
/// <summary> /// Submit button event /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void btnSubmit_Click(object sender, EventArgs e) { try { LinkButton btn = (LinkButton)sender; int timesheetId = 0; if (btn.CommandArgument != string.Empty) { timesheetId = Convert.ToInt32(btn.CommandArgument); } bool validHours = false; bool dataSave = false; using (SPSite site = new SPSite(SPContext.Current.Site.Url)) { using (SPWeb web = site.OpenWeb()) { bool allowUnsafeUpdate = web.AllowUnsafeUpdates; try { web.AllowUnsafeUpdates = true; SPList lstTimesheet = web.Lists[Constants.TimesheetListName]; if (lstTimesheet != null) { Timesheet objTimesheet = new Timesheet() { ID = timesheetId, TimesheetDate = dtDate.SelectedDate, Category = ddlCategory.SelectedItem.Text, Description = txtDescription.Text.Trim(), Hours = Convert.ToDouble(txtHours.Text.Trim()) }; TimesheetRespository objRepository = new TimesheetRespository(); validHours = Common.ValidHours(lstTimesheet, dtDate.SelectedDate, Convert.ToDouble(txtHours.Text.Trim()), objTimesheet.ID, objRepository); if (validHours) { dataSave = objRepository.SaveTimesheet(lstTimesheet, objTimesheet); BindAllTimesheet(); lblPageTitle.Text = "Timesheet"; lblMsg.Text = string.Empty; ClearControls(); } } else { validHours = true; ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Notify", "alert('" + Constants.TimesheetListName + " list not found.');", true); } } finally { web.AllowUnsafeUpdates = allowUnsafeUpdate; } } } if (!validHours) { ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Notify", "alert('Total hours per day should not be greater than 8.');", true); } if (dataSave) { if (timesheetId > 0) { ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Notify", "alert('" + Constants.TimesheetUpdatedMsg + "');", true); } else { ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Notify", "alert('" + Constants.TimesheetAddedMsg + "');", true); } trGrid.Visible = true; trForm.Visible = false; } } catch (Exception ex) { Common.HandleException(ex, SPContext.Current.Web.Url, "btnSubmit_Click", "ManageTimesheetUserControl", SPContext.Current.Web.CurrentUser.Name); lblMsg.Text = Constants.ErrorMsg; } }
/// <summary> /// Validate timesheet hours - should not be greater than 8 per day /// </summary> /// <param name="list"></param> /// <param name="timesheetDate"></param> /// <param name="enteredHours"></param> /// <param name="timesheetId"></param> /// <param name="objRepository"></param> /// <returns></returns> public static bool ValidHours(SPList list, DateTime timesheetDate, double enteredHours, int timesheetId, TimesheetRespository objRepository) { bool isValid = true; SPListItemCollection itemColl = objRepository.GetTimesheetsByUserAndDate(list, SPContext.Current.Web.CurrentUser.ID, timesheetDate); if (itemColl != null && itemColl.Count > 0) { double totalHours = 0; foreach (SPListItem item in itemColl) { if (!item.ID.Equals(timesheetId)) { totalHours += Convert.ToDouble(item["Hours"]); } } if (totalHours >= 8) { isValid = false; } else { totalHours += enteredHours; if (totalHours > 8) { isValid = false; } } } return(isValid); }