public override void OnPublished(PSLibrary.PSContextInfo contextInfo, ProjectPostPublishEventArgs e)
 {
     base.OnPublished(contextInfo, e);
     try
     {
         SPSecurity.RunWithElevatedPrivileges(delegate
                                                  {
                                                      using (var Site = new SPSite(contextInfo.SiteGuid))
                                                      {
                                                          MyUtilities.EnableSQlServerCLR(Site.ID);
                                                          //MyUtilities.InstallStoredProcedure(Site.ID, false);
                                                          string ConnectionString = Utilities.GetProjectServerSQLDatabaseConnectionString(Site.ID, Utilities.DatabaseType.PublishedDatabase);
                                                          using (var sqlConnection = new SqlConnection(ConnectionString.Replace("15", "180")))
                                                          {
                                                              sqlConnection.Open();
                                                              var cmd = new SqlCommand();
                                                              cmd.Connection = sqlConnection;
                                                              cmd.CommandType = CommandType.StoredProcedure;
                                                              cmd.Parameters.Add(new SqlParameter("ProjectUID", e.ProjectGuid.ToString()));
                                                              cmd.Parameters.Add(new SqlParameter("ModifiedBy", contextInfo.UserName));
                                                              cmd.CommandText = "ITXTaskAuditTrail";
                                                              DateTime starttime = DateTime.Now;
                                                              cmd.ExecuteNonQuery();
                                                              MyUtilities.ErrorLog("Time taken for execution : " + starttime.Subtract(DateTime.Now).Seconds, EventLogEntryType.SuccessAudit);
                                                          }
                                                      }
                                                  });
     }
     catch (Exception ex)
     {
         if (string.IsNullOrEmpty(ex.StackTrace))
             MyUtilities.ErrorLog("Error on Published Event due to : " + ex.Message, EventLogEntryType.Error);
         else
             MyUtilities.ErrorLog("Error on Published Event due to : " + ex.Message + Environment.NewLine + "Stack Trace :" + ex.StackTrace, EventLogEntryType.Error);
     }
 }
        public override void OnSubmitted(PSLib.PSContextInfo contextInfo, TimesheetPostEventArgs e)
        {
            try
            {
                WriteLogEntries("", Guid.NewGuid(), "Timesheet Event Handler Fired");
                WriteLogEntries("", Guid.NewGuid(), "User Executing onSubmitted = " + System.Security.Principal.WindowsIdentity.GetCurrent().Name);

                //changes in the additional Logging branch.
                base.OnSubmitted(contextInfo, e);

                SetClientEndpoint(contextInfo.SiteGuid);
                Guid pwaGuid = contextInfo.SiteGuid;
                using (OperationContextScope scope = new OperationContextScope(timesheetClient.InnerChannel))
                {
                    //Read all Timesheet Periods
                    var periods = adminClient.ReadPeriods(SvcAdmin.PeriodState.All).TimePeriods.OrderBy(t => t.WPRD_START_DATE).ToList();
                    WriteLogEntries("", Guid.NewGuid(), "Admin Read periods done with " + System.Security.Principal.WindowsIdentity.GetCurrent().Name);
                    //Set Impersonation
                    SetImpersonation(contextInfo.UserGuid, contextInfo.UserName);
                    var timesheetCopy = timesheetClient.ReadTimesheet(e.TsUID);
                    var timesheet = timesheetCopy;

                    //Get current period ID
                    var currentGuid = timesheet.Headers[0].WPRD_UID;

                    //Find the index of next period
                    int index = periods.FindIndex(t => t.WPRD_UID == currentGuid);
                    var nextPeriod = (index == periods.Count() - 1) ? periods.ElementAt(index) : periods.ElementAt(index + 1);
                    //If the current period id is not the last period id
                    if (periods[index].WPRD_UID != nextPeriod.WPRD_UID)
                    {
                        //Read timesheet for next period
                        var nextTimesheet = timesheetClient.ReadTimesheetByPeriod(contextInfo.UserGuid, nextPeriod.WPRD_UID, SvcTimeSheet.Navigation.Current);

                        // If next timesheet is not yet created only then
                        if (nextTimesheet.Headers.Count == 0)
                        {
                            Guid TSUID = Guid.Empty;
                            //no prepopulationa
                            CreateTimesheet(contextInfo.UserGuid,contextInfo.UserName, nextPeriod.WPRD_UID, ref TSUID, ref nextTimesheet);
                            //for each line that was present in the current timesheet
                            nextTimesheet.Lines.Clear();
                            foreach (var line in timesheet.Lines)
                            {

                                //add the line to the next timesheet
                                try
                                {

                                var lineRow = nextTimesheet.Lines.AddLinesRow(Guid.NewGuid(), nextTimesheet.Headers[0], line.ASSN_UID, line.TASK_UID, line.PROJ_UID,
                                    line.TS_LINE_CLASS_UID, line.TS_LINE_COMMENT, line.TS_LINE_VALIDATION_TYPE, line.TS_LINE_CACHED_ASSIGN_NAME,
                                   line.TS_LINE_CACHED_PROJ_NAME, line.TS_LINE_CACHED_PROJ_REVISION_COUNTER, line.TS_LINE_CACHED_PROJ_REVISION_RANK, line.TS_LINE_IS_CACHED, 0,
                                   line.TS_LINE_STATUS,
                                   0, line.TS_LINE_TASK_HIERARCHY);
                                var date = nextPeriod.WPRD_START_DATE;
                                    if(nextTimesheet.Lines.Any(t=>t.ASSN_UID == lineRow.ASSN_UID))
                                    {
                                        continue;
                                    }
                                Guid[] uids = new Guid[] { lineRow.TS_LINE_UID };
                                timesheetClient.PrepareTimesheetLine(TSUID, ref nextTimesheet, uids);
                                var actuals = lineRow.GetActualsRows();
                                foreach (var actual in actuals)
                                {
                                    actual.SetTS_ACT_NON_BILLABLE_OVT_VALUENull();
                                    actual.SetTS_ACT_NON_BILLABLE_VALUENull();
                                    actual.SetTS_ACT_OVT_VALUENull();
                                    actual.SetTS_ACT_VALUENull();
                                }
                                }
                                catch (Exception)
                                {
                                    continue;
                                }
                            }
                            //Save next timesheet
                            SaveTimesheet(contextInfo.UserGuid,contextInfo.UserName, nextTimesheet, TSUID);

                        }
                    }

                }
                WriteLogEntries(contextInfo.UserName, e.TsUID, "Successfully done updating next timesheet");
            }
            catch (Exception ex)
            {
                WriteLogEntries(contextInfo.UserName, e.TsUID, ex.Message);
            }
        }