Imports Williams.Lea.Symphony.Web.UIObjects Imports Telerik.Web.UI Imports Williams.Lea.Symphony.Web.CustomControls Imports Telerik.Web.UI.Calendar Imports Williams.Lea.Symphony.Web.Domain.Interfaces Imports Williams.Lea.Symphony.Web.Domain.Models Imports Williams.Lea.Symphony.Web.Domain.Validation Imports System.Linq Imports Williams.Lea.Symphony.Web.Domain.Helpers Public Class SYS_SiteTimesheet Inherits SitePage Public Property _timeclocksService As ITimeclocksService Public Property _personnelService As IPersonnelService 'Page load Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'Do nothing End Sub ' Main need source Protected Sub rgMain_NeedSource(ByVal source As Object, ByVal e As Telerik.Web.UI.GridNeedDataSourceEventArgs) Handles rgMain.NeedDataSource LoadData() End Sub ' Load main source Protected Sub LoadData() Dim data = _personnelService.GetAll(Me.SITENAME, GetRadGridSettings(rgMain)) If Not data Is Nothing Then rgMain.DataSource = data.ToTimeAdjustedList() rgMain.VirtualItemCount = data.TotalItemCount End If End Sub ' Main item bound Protected Sub rgMain_ItemDataBound(ByVal sender As Object, ByVal e As GridItemEventArgs) Handles rgMain.ItemDataBound Select Case e.Item.ItemType Case GridItemType.AlternatingItem, GridItemType.Item Dim item As GridItem = DirectCast(e.Item, GridItem) If Not item.DataItem.ENABLED Then If e.Item.ItemType = GridItemType.AlternatingItem Then item.CssClass = "rgAltRow rgDisabled" Else item.CssClass = "rgRow rgDisabled" End If End If Case GridItemType.EditFormItem If e.Item.IsInEditMode Then If TypeOf (e.Item) Is GridEditFormInsertItem Or TypeOf (e.Item) Is GridDataInsertItem Then ' Do nothing Else Dim id As Integer = CInt(e.Item.DataItem.ID) Dim payrollID As String = e.Item.DataItem.PAYROLL_ID Dim guid As Guid? = DirectCast(e.Item.DataItem.RGUID, Guid) Dim rcCalendar As RadCalendar = DirectCast(e.Item.FindControl("rcCalendar"), RadCalendar) If Not rcCalendar Is Nothing Then rcCalendar.SelectedDate = TimeProvider.Current.UtcNow.ConvertToLocalTime End If 'Set hours grid Dim rgHours As RadGrid = DirectCast(e.Item.FindControl("rgHours"), RadGrid) If Not rgHours Is Nothing Then rgHours.Rebind() End If End If Page.Validate("vgSave") End If End Select End Sub ' Main binding Protected Sub rgMain_Binding(ByVal sender As Object, ByVal e As EventArgs) Handles rgMain.DataBinding For Each c As GridColumn In rgMain.Columns If TypeOf (c) Is GridDateTimeColumn Then Dim dateTimeColumn As GridDateTimeColumn = TryCast(c, GridDateTimeColumn) dateTimeColumn.DataFormatString = String.Format("{{0:{0}}}", _session.DATE_TIME_FORMAT) dateTimeColumn.FilterDateFormat = _session.DATE_TIME_FORMAT End If Next End Sub ' Main item created Protected Sub rgMain_ItemCreated(ByVal sender As Object, ByVal e As GridItemEventArgs) Handles rgMain.ItemCreated Select Case e.Item.ItemType Case GridItemType.CommandItem Dim btnClearFilters As Button = DirectCast(e.Item.FindControl("btnClearFilters"), Button) Dim lbClearFilters As LinkButton = DirectCast(e.Item.FindControl("lbClearFilters"), LinkButton) Dim isFiltered = IsRadGridFiltered(rgMain) btnClearFilters.Enabled = isFiltered lbClearFilters.Enabled = isFiltered Case GridItemType.EditFormItem If e.Item.IsInEditMode Then Dim rgHours As RadGrid = DirectCast(e.Item.FindControl("rgHours"), RadGrid) If Not rgHours Is Nothing Then AddHandler rgHours.NeedDataSource, AddressOf rgHours_NeedDataSource AddHandler rgHours.DeleteCommand, AddressOf rgHours_DeleteCommand AddHandler rgHours.InsertCommand, AddressOf rgHours_InsertCommand AddHandler rgHours.UpdateCommand, AddressOf rgHours_UpdateCommand AddHandler rgHours.ItemDataBound, AddressOf rgHours_ItemDataBound AddHandler rgHours.ItemCommand, AddressOf rgHours_ItemCommand AddHandler rgHours.DataBinding, AddressOf rgHours_DataBinding AddHandler rgHours.PreRender, AddressOf rgHours_PreRender End If Dim rcCalendar As RadCalendar = DirectCast(e.Item.FindControl("rcCalendar"), RadCalendar) If Not rcCalendar Is Nothing Then AddHandler rcCalendar.SelectionChanged, AddressOf rcCalendar_SelectionChanged End If End If End Select End Sub ' Main item command Protected Sub rgMain_ItemCommand(ByVal sender As Object, ByVal e As GridCommandEventArgs) Handles rgMain.ItemCommand If e.CommandName.ToUpper = "EDIT" Then rgMain.MasterTableView.EditFormSettings.CaptionDataField = "PAYROLL_ID" rgMain.MasterTableView.EditFormSettings.CaptionFormatString = "Edit Hours ({0})" Else rgMain.MasterTableView.EditFormSettings.CaptionDataField = "" rgMain.MasterTableView.EditFormSettings.CaptionFormatString = "Add Hours" End If If e.CommandName.ToUpper = "CLEARFILTERS" Then For Each column As GridColumn In rgMain.MasterTableView.OwnerGrid.Columns column.CurrentFilterFunction = GridKnownFunction.NoFilter column.CurrentFilterValue = String.Empty Next rgMain.MasterTableView.FilterExpression = String.Empty rgMain.Rebind() End If End Sub ' Calendar date changed event Protected Sub rcCalendar_SelectionChanged(ByVal sender As Object, ByVal e As SelectedDatesEventArgs) Dim rc As RadCalendar = DirectCast(sender, RadCalendar) If Not rc Is Nothing Then Dim gefi As GridEditFormItem = DirectCast(rc.NamingContainer, GridEditFormItem) If Not gefi Is Nothing Then Dim rg As RadGrid = DirectCast(gefi.FindControl("rgHours"), RadGrid) If Not rg Is Nothing Then rg.CurrentPageIndex = 0 rg.Rebind() End If End If End If End Sub ' Handles time grid Pre render event Protected Sub rgHours_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Dim rg As RadGrid = DirectCast(sender, RadGrid) Dim levelGuid As New Guid? If Not rg Is Nothing Then Dim gefi As GridEditFormItem = DirectCast(rg.NamingContainer, GridEditFormItem) If Not gefi Is Nothing Then Dim gdi As GridDataItem = DirectCast(gefi.ParentItem, GridDataItem) If Not gdi Is Nothing Then rg.MasterTableView.GetColumn("SITELOCATION").Visible = gdi.GetDataKeyValue("LOCATION_TRACKING") End If End If End If End Sub ' Hours need source Protected Sub rgHours_NeedDataSource(ByVal sender As Object, ByVal e As GridNeedDataSourceEventArgs) Dim rg As RadGrid = DirectCast(sender, RadGrid) If Not rg Is Nothing Then Dim guid As New Guid? Dim day As New Date? If Not rg Is Nothing Then Dim gefi As GridEditFormItem = DirectCast(rg.NamingContainer, GridEditFormItem) If Not gefi Is Nothing Then Dim rcCalendar As RadCalendar = DirectCast(gefi.FindControl("rcCalendar"), RadCalendar) Dim gdi As GridDataItem = DirectCast(gefi.ParentItem, GridDataItem) If Not gdi Is Nothing And Not rcCalendar Is Nothing Then guid = gdi.GetDataKeyValue("RGUID") day = rcCalendar.SelectedDate End If End If End If If Not guid Is Nothing And Not day Is Nothing Then LoadHours(rg, guid, day) End If End If End Sub ' Load hours Protected Sub LoadHours(ByVal rg As RadGrid, ByVal guid As Guid?, ByVal day As Date) Dim dayStart As Date = day.ConvertToServerTime Dim dayEnd As Date = DateAdd(DateInterval.Day, 1, day).ConvertToServerTime Dim data = _timeclocksService.GetHours(guid, dayStart, dayEnd, GetRadGridSettings(rg)) If Not data Is Nothing Then rg.DataSource = data.ToTimeAdjustedList() rg.VirtualItemCount = data.TotalItemCount End If End Sub ' Hours item command Protected Sub rgHours_ItemCommand(ByVal sender As Object, ByVal e As GridCommandEventArgs) Dim rg As RadGrid = DirectCast(sender, RadGrid) If e.CommandName.ToUpper = "EDIT" Then rg.MasterTableView.EditFormSettings.CaptionFormatString = "Edit Hours" Else rg.MasterTableView.EditFormSettings.CaptionFormatString = "Add Hours" End If If e.CommandName.ToUpper = "CLEARFILTERS" Then For Each column As GridColumn In rgMain.MasterTableView.OwnerGrid.Columns column.CurrentFilterFunction = GridKnownFunction.NoFilter column.CurrentFilterValue = String.Empty Next rg.MasterTableView.FilterExpression = String.Empty rg.Rebind() End If End Sub 'Hours binding Protected Sub rgHours_DataBinding(ByVal sender As Object, ByVal e As EventArgs) Dim rg As RadGrid = DirectCast(sender, RadGrid) For Each c As GridColumn In rg.Columns If TypeOf (c) Is GridDateTimeColumn Then Dim dateTimeColumn As GridDateTimeColumn = TryCast(c, GridDateTimeColumn) dateTimeColumn.DataFormatString = String.Format("{{0:{0}}}", _session.DATE_TIME_FORMAT) dateTimeColumn.FilterDateFormat = _session.DATE_TIME_FORMAT End If Next End Sub ' Hours insert command Protected Sub rgHours_InsertCommand(ByVal sender As Object, ByVal e As GridCommandEventArgs) If rgMain.EditItems.Count > 0 Then Dim gdi As GridDataItem = DirectCast(rgMain.EditItems(0), GridDataItem) Dim rg As RadGrid = DirectCast(sender, RadGrid) Dim siteLocationId As Integer? = Nothing If Not gdi Is Nothing Then Dim guid As Guid? = gdi.GetDataKeyValue("RGUID") Me.Validate("vgSave") If Not Me.IsValid Then Me.ErrorMessage(GetValidatorErrors(Me.Validators)) e.Canceled = True Return End If Dim rcbRole As RadComboBox = DirectCast(e.Item.FindControl("rcbRole"), RadComboBox) Dim rcbType As RadComboBox = DirectCast(e.Item.FindControl("rcbType"), RadComboBox) Dim dtpStartTime As DateTimePicker = DirectCast(e.Item.FindControl("dtpStartTime"), DateTimePicker) Dim dtpEndTime As DateTimePicker = DirectCast(e.Item.FindControl("dtpEndTime"), DateTimePicker) Dim rntbHours As RadNumericTextBox = DirectCast(e.Item.FindControl("rntbHours"), RadNumericTextBox) Dim rcbLocation As RadComboBox = DirectCast(e.Item.FindControl("rcbLocation"), RadComboBox) Dim rtbComments As RadTextBox = DirectCast(e.Item.FindControl("rtbComments"), RadTextBox) Dim hours As Decimal = rntbHours.Value If Not String.IsNullOrEmpty(rcbLocation.SelectedValue) Then siteLocationId = CInt(rcbLocation.SelectedValue) End If Dim tci = _timeclocksService.GetItem(New Guid(rcbType.SelectedValue)) If tci Is Nothing OrElse Not New String() {"T", "H"}.Contains(tci.TCITYPE.ToUpper()) Then If Not dtpStartTime.SelectedDate Is Nothing AndAlso dtpStartTime.SelectedDate.HasValue AndAlso Not dtpEndTime.SelectedDate Is Nothing AndAlso dtpEndTime.SelectedDate.HasValue Then hours = dtpStartTime.SelectedDate.Difference(dtpEndTime.SelectedDate) End If End If Try Dim id = _timeclocksService.Create(New TimeclocksDTO() With { .PERSONNELGUID = guid, .LEVELGUID = New Guid(rcbRole.SelectedValue), .TCIGUID = New Guid(rcbType.SelectedValue), .START_TIME = dtpStartTime.SelectedDate.ConvertToServerTime, .END_TIME = dtpEndTime.SelectedDate.ConvertToServerTime, .HOURS = hours, .SITETIMECLOCKLOCATIONSID = siteLocationId, .COMMENTS = rtbComments.Text, .SITE = Me.SITENAME }) LogAction(String.Format("Timeclock {0} has been created", id), ActionType.Create, True) rg.Rebind() Catch ex As ValidationException Me.ErrorMessage(GetValidationExceptionErrors(ex)) e.Canceled = True Return Catch ex As Exception Me.ErrorMessage(Domain.Localization.Resources.Resources.ErrorInsertingRecord) LogError(ex.ToString(), ActionType.Create, False) e.Canceled = True Return End Try End If End If End Sub ' Hours update command Protected Sub rgHours_UpdateCommand(ByVal sender As Object, ByVal e As GridCommandEventArgs) Dim id As Integer = CInt(e.Item.OwnerTableView.DataKeyValues(e.Item.ItemIndex)("ID")) Dim siteLocationId As Integer? = Nothing Me.Validate("vgSave") If Not Me.IsValid Then Me.ErrorMessage(GetValidatorErrors(Me.Validators)) e.Canceled = True Return End If Dim rcbRole As RadComboBox = DirectCast(e.Item.FindControl("rcbRole"), RadComboBox) Dim rcbType As RadComboBox = DirectCast(e.Item.FindControl("rcbType"), RadComboBox) Dim dtpStartTime As DateTimePicker = DirectCast(e.Item.FindControl("dtpStartTime"), DateTimePicker) Dim dtpEndTime As DateTimePicker = DirectCast(e.Item.FindControl("dtpEndTime"), DateTimePicker) Dim rntbHours As RadNumericTextBox = DirectCast(e.Item.FindControl("rntbHours"), RadNumericTextBox) Dim rcbLocation As RadComboBox = DirectCast(e.Item.FindControl("rcbLocation"), RadComboBox) Dim rtbComments As RadTextBox = DirectCast(e.Item.FindControl("rtbComments"), RadTextBox) Dim hours As Decimal = rntbHours.Value If Not String.IsNullOrEmpty(rcbLocation.SelectedValue) Then siteLocationId = CInt(rcbLocation.SelectedValue) End If Dim tci = _timeclocksService.GetItem(New Guid(rcbType.SelectedValue)) If tci Is Nothing OrElse Not New String() {"T", "H"}.Contains(tci.TCITYPE.ToUpper()) Then If Not dtpStartTime.SelectedDate Is Nothing AndAlso dtpStartTime.SelectedDate.HasValue AndAlso Not dtpEndTime.SelectedDate Is Nothing AndAlso dtpEndTime.SelectedDate.HasValue Then hours = dtpStartTime.SelectedDate.Difference(dtpEndTime.SelectedDate) End If End If Try _timeclocksService.Update(New TimeclocksDTO() With { .ID = id, .LEVELGUID = New Guid(rcbRole.SelectedValue), .TCIGUID = New Guid(rcbType.SelectedValue), .START_TIME = dtpStartTime.SelectedDate.ConvertToServerTime, .END_TIME = dtpEndTime.SelectedDate.ConvertToServerTime, .HOURS = hours, .SITETIMECLOCKLOCATIONSID = siteLocationId, .COMMENTS = rtbComments.Text, .SITE = Me.SITENAME }) LogAction(String.Format("Timeclock {0} has been updated", id), ActionType.Update, True) Catch ex As ValidationException Me.ErrorMessage(GetValidationExceptionErrors(ex)) e.Canceled = True Return Catch ex As KeyNotFoundException Me.ErrorMessage(Domain.Localization.Resources.Resources.ItemNotFound) e.Canceled = True Return Catch ex As Exception Me.ErrorMessage(Domain.Localization.Resources.Resources.ErrorUpdatingRecord) LogError(ex.ToString(), ActionType.Update, False) e.Canceled = True Return End Try End Sub ' Hours delete command Protected Sub rgHours_DeleteCommand(ByVal sender As Object, ByVal e As GridCommandEventArgs) Dim id As Integer = CInt(e.Item.OwnerTableView.DataKeyValues(e.Item.ItemIndex)("ID")) Dim rtbCode As RadTextBox = DirectCast(e.Item.FindControl("rtbCode"), RadTextBox) Try _timeclocksService.Delete(New TimeclocksDTO() With {.ID = id, .SITE = Me.SITENAME}) LogAction(String.Format("Timeclock {0} has been deleted", id), ActionType.Delete, True) Catch ex As KeyNotFoundException Me.ErrorMessage(Domain.Localization.Resources.Resources.ItemNotFound) e.Canceled = True Return Catch ex As Exception Me.ErrorMessage(Domain.Localization.Resources.Resources.ErrorDeletingRecord) LogError(ex.ToString(), ActionType.Delete, False) e.Canceled = True Return End Try Dim rg = DirectCast(sender, RadGrid) rg.MasterTableView.ClearEditItems() End Sub ' Hours item bound Protected Sub rgHours_ItemDataBound(ByVal sender As Object, ByVal e As GridItemEventArgs) Dim rg As RadGrid = DirectCast(sender, RadGrid) Dim levelGuid As New Guid? Select Case e.Item.ItemType Case GridItemType.EditFormItem If e.Item.IsInEditMode Then Dim rcbRole As RadComboBox = DirectCast(e.Item.FindControl("rcbRole"), RadComboBox) If Not rcbRole Is Nothing Then Dim roles = _timeclocksService.GetAllLevels(Me.SITENAME, True, Nothing) If Not roles Is Nothing Then For Each r In roles rcbRole.Items.Add(New RadComboBoxItem(r.DESCRIPTION, r.LEVELGUID.ToString)) Next End If End If Dim rcbType As RadComboBox = DirectCast(e.Item.FindControl("rcbType"), RadComboBox) If Not rcbType Is Nothing Then Dim types = _timeclocksService.GetItems(Me.SITENAME) If Not types Is Nothing Then For Each t In types rcbType.Items.Add(New RadComboBoxItem(t.DESCRIPTION, t.TCIGUID.ToString)) Next End If End If Dim gefi As GridEditFormItem = DirectCast(rg.NamingContainer, GridEditFormItem) If Not gefi Is Nothing Then Dim gdi As GridDataItem = DirectCast(gefi.ParentItem, GridDataItem) If Not gdi Is Nothing Then e.Item.FindControl("siteLocation").Visible = gdi.GetDataKeyValue("LOCATION_TRACKING") End If End If Dim rcbLocation As RadComboBox = DirectCast(e.Item.FindControl("rcbLocation"), RadComboBox) If Not rcbLocation Is Nothing Then Dim siteLocations = _timeclocksService.GetAllSiteLocations(Me.SITENAME, True, Nothing) If Not siteLocations Is Nothing Then For Each siteLocation In siteLocations rcbLocation.Items.Add(New RadComboBoxItem(siteLocation.DESCRIPTION, siteLocation.ID.ToString)) Next End If End If If TypeOf (e.Item) Is GridEditFormInsertItem Or TypeOf (e.Item) Is GridDataInsertItem Then ' Do nothing Else 'Set role If Not rcbRole Is Nothing AndAlso Not rcbRole.Items.FindItemByValue(e.Item.DataItem.LEVELGUID.ToString) Is Nothing Then rcbRole.Items.FindItemByValue(e.Item.DataItem.LEVELGUID.ToString).Selected = True End If 'Set type If Not rcbType Is Nothing AndAlso Not rcbType.Items.FindItemByValue(e.Item.DataItem.TCIGUID.ToString) Is Nothing Then rcbType.Items.FindItemByValue(e.Item.DataItem.TCIGUID.ToString).Selected = True End If 'Set site location If Not rcbLocation Is Nothing AndAlso Not rcbLocation.Items.FindItemByText(e.Item.DataItem.SITELOCATION) Is Nothing Then rcbLocation.Items.FindItemByText(e.Item.DataItem.SITELOCATION).Selected = True End If End If Page.Validate("vgHoursSave") End If End Select End Sub End Class