Skip to content

Instantly share code, notes, and snippets.

@kiquenet
Last active June 19, 2018 19:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kiquenet/9085b84f716294a13a46a8204ade9248 to your computer and use it in GitHub Desktop.
Save kiquenet/9085b84f716294a13a46a8204ade9248 to your computer and use it in GitHub Desktop.
Generar Informe
Public Class WebForm1
Inherits System.Web.UI.Page
Dim Usuario As String
Dim ConexionSqlServer As String = "Data Source=xxxxx"
Dim Conexion As String = ConexionSqlServer & "Provider=SQLOLEDB;"
'Dim Fecha As Date
Dim FechaDesde As Date
Dim FechaHasta As Date
'Dim FechaString As String
Dim FechaDesdeString As String
Dim FechaHastaString As String
Dim NombreTabla As String
Dim Sede As String = "BCN','SEV','DGT','TA','MAD"
Dim Tabla As String = ""
Dim NumDiasSel As Integer = 0
Dim registrosAfectados As Integer = 0
Dim CommandTimeoutQueriesSlow As Integer = 180
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
DeFechaDesde.Date = Date.Now.Date.AddDays(-1)
DeFechaHasta.Date = Date.Now.Date.AddDays(-1)
CmbServicios.Value = ""
CargaSedes()
CargaComboServicios()
End If
Usuario = Session("login")
Dim isLocal As Boolean = HttpContext.Current.Request.IsLocal
If isLocal Then
tbLog.Visible = True
End If
End Sub
Protected Sub CmbSedes_SelectedIndexChanged(sender As Object, e As EventArgs) Handles CmbSedes.SelectedIndexChanged
CargaSedes()
CargaComboServicios()
End Sub
Public Sub CargaSedes()
Sede = CmbSedes.Value
If Sede = "TODAS" Then
Sede = "BCN','SEV','DGT','TA','MAD"
TxtSedeLoginHermes.Text = ""
End If
Select Case Sede
Case "TODAS"
Sede = "BCN','SEV','DGT','TA','MAD"
TxtSedeLoginHermes.Text = ""
Case "BCN"
TxtSedeLoginHermes.Text = "4"
Case "MAD','DGT"
TxtSedeLoginHermes.Text = "1"
Case "MAD"
TxtSedeLoginHermes.Text = "1"
Case "DGT"
TxtSedeLoginHermes.Text = "1"
Case "SEV"
TxtSedeLoginHermes.Text = "3"
End Select
End Sub
Public Sub CargaComboServicios()
Dim cn As New System.Data.OleDb.OleDbConnection(Conexion)
Dim cmd As New System.Data.OleDb.OleDbCommand
cmd.Connection = cn
Dim dr As OleDb.OleDbDataReader
CmbServicios.Items.Clear()
CmbServicios.Items.Add("TODOS")
Try
cn.Open()
cmd.CommandText = "SELECT distinct Cliente FROM BBDD_Costes.dbo.CostesInforme where Sede in ('" & Sede & "')"
dr = cmd.ExecuteReader
While dr.Read
CmbServicios.Items.Add(dr(0))
End While
If Not dr.IsClosed Then
dr.Close()
End If
Catch ex As Exception
TratarError("CargaComboServicios", ex)
Finally
cn.Close()
End Try
End Sub
Protected Sub BtnGenerar_Click(sender As Object, e As EventArgs) Handles BtnGenerar.Click
LblError.Text = ""
'Fecha = DeFecha.Value
FechaDesde = DeFechaDesde.Value
FechaHasta = DeFechaHasta.Value
'FechaString = DevuelveString(Fecha.Date)
FechaDesdeString = DevuelveString(FechaDesde.Date)
FechaHastaString = DevuelveString(FechaHasta.Date)
NumDiasSel = DateDiff(DateInterval.Day, FechaDesde, FechaHasta) + 1
'If FechaHasta.Date < Date.Now And FechaDesde.Date > Date.Now.AddMonths(-2) Then
If FechaDesde.Date <= FechaHasta.Date Then
LblError.Text = ""
If CmbServicios.Value = "" Then
LblError.Text = "Es necesario seleccionar un servicio."
Else
CargarDatos()
End If
Else
LblError.Text = "La fecha de fin no puede ser menor a la de inicio."
End If
'Else
' LblError.Text = "La fecha debe ser inferior al día actual y dentro de los dos últimos meses."
'End If
End Sub
Public Function DevuelveString(ByVal FechaOri As Date)
Dim FechaResul As String
FechaResul = FechaOri.Date.Year.ToString
If Len(FechaOri.Date.Month.ToString) = 1 Then
FechaResul = FechaResul & "0" & FechaOri.Date.Month.ToString
Else
FechaResul = FechaResul & FechaOri.Date.Month.ToString
End If
If Len(FechaOri.Date.Day.ToString) = 1 Then
FechaResul = FechaResul & "0" & FechaOri.Date.Day.ToString
Else
FechaResul = FechaResul & FechaOri.Date.Day.ToString
End If
Return FechaResul
End Function
Public Sub CargarDatos()
CompruebaTabla()
VaciarInforme()
ActualizarHorasContrato()
InsertarAgentesConHermes()
InsertarAgentesConHermesSinLlamadas()
InsertarAgentesSinHermes()
RellenaInfoAgentes()
DatosPortal()
DatosHermesOdActions()
DatosHermesOdCalls()
RealizaCalculos()
RellenaCampos()
IndicaSiFestivo()
CalculaHorasNocturnas()
AgentesMasDeUnServicio()
DsInforme.SelectCommand = "SELECT * FROM " & NombreTabla & " ORDER BY [NumEmp]"
DsInforme.DataBind()
GvInforme.DataSource = DsInforme
GvInforme.DataBind()
tbLog.Text = "Filas: " & GvInforme.VisibleRowCount & " Columnas: " & GvInforme.Columns.Count & _
vbCrLf & vbCrLf & vbCrLf & tbLog.Text
End Sub
Private Sub TratarError(ByVal ident As String, ByVal ex As Exception)
LblError.Text &= "ERROR " & ident & ": " & ex.Message & vbCrLf & "<br />"
End Sub
Private Sub Log(ByVal ident As String, ByVal msg As String)
tbLog.Text &= ident & ": " & msg & vbCrLf
End Sub
#Region "Prepara tablas y fechas para generar informe"
Public Sub CompruebaTabla()
NombreTabla = "InformeAsistencia" & Usuario
TxtNombreTabla.Text = NombreTabla
Dim ExisteTabla = CompruebaSiExiste()
If Not ExisteTabla Then
CrearTabla()
End If
End Sub
Public Sub CrearTabla()
Dim cn As New System.Data.OleDb.OleDbConnection(Conexion)
Dim cmd As New System.Data.OleDb.OleDbCommand
cmd.Connection = cn
Try
cn.Open()
cmd.CommandText = "CREATE TABLE [dbo].[" & NombreTabla & "]([NumEmp] [varchar](50) NULL, [NomEmp] [varchar](200) NULL, [LoginHermes] [varchar](50) NULL, [LoginPortal] [varchar](50) NULL, [CodCamp] [varchar](50) NULL, [DescCamp] [varchar](50) NULL, [Dia] [varchar](50) NULL, [HorasContr] [varchar](50) NULL, [MinutosDia] [varchar](50) NULL, [HoraInPortal] [varchar](50) NULL, [HoraOutPortal] [varchar](50) NULL, [TiempoPortal] [varchar](50) NULL, [PausaComidaP] [varchar](50) NULL, [PausaPVDP] [varchar](50) NULL, [PausaDescansoP] [varchar](50) NULL, [PausaFormacionP] [varchar](50) NULL, [PausaMedicoP] [varchar](50) NULL, [PausaSindicatoP] [varchar](50) NULL, [PausaReunionP] [varchar](50) NULL, [TotalPausasP] [varchar](50) NULL, [HoraInHermes] [varchar](50) NULL, [HoraOutHermes] [varchar](50) NULL, [TiempoHermes] [varchar](50) NULL, [PausaComidaH] [varchar](50) NULL, [PausaPVDH] [varchar](50) NULL, [PausaDescansoH] [varchar](50) NULL, [PausaFormacionH] [varchar](50) NULL, [PausaSindicatoH] [varchar](50) NULL, [PausaReunionH] [varchar](50) NULL, [PausaMedicoH] [varchar](50) NULL, [TotalPausasH] [varchar](50) NULL, [AfterCall] [varchar](50) NULL, [TiempoEspera] [varchar](50) NULL, [NumLlamadasGes] [varchar](50) NULL, [TiempoConv] [varchar](50) NULL, [Tmo] [varchar](50) NULL, [Alarma] [varchar](50) NULL, [TiempoPorServ] [varchar](50) NULL, [TipoDia] [varchar](50) NULL, [MinutosNocturnos] [varchar](50) NULL, [Transporte] [varchar](50) NULL) "
cmd.ExecuteNonQuery()
Catch ex As Exception
TratarError("CrearTabla", ex)
Finally
cn.Close()
End Try
End Sub
Public Function CompruebaSiExiste()
Dim Existe As Boolean = True
Dim cn As New System.Data.OleDb.OleDbConnection(Conexion)
Dim cmd As New System.Data.OleDb.OleDbCommand
cmd.Connection = cn
Try
cn.Open()
cmd.CommandText = "select count(*) FROM BBDD_ControlAsistencia.dbo." & NombreTabla & ""
cmd.ExecuteNonQuery()
Catch ex As Exception
Existe = False
Finally
cn.Close()
End Try
Return Existe
End Function
Public Sub VaciarInforme()
Dim cn As New System.Data.OleDb.OleDbConnection(Conexion)
Dim cmd As New System.Data.OleDb.OleDbCommand
cmd.Connection = cn
Try
cn.Open()
cmd.CommandText = "DELETE FROM BBDD_ControlAsistencia.dbo." & NombreTabla & ""
cmd.ExecuteNonQuery()
Catch ex As Exception
TratarError("VaciarInforme", ex)
Finally
cn.Close()
End Try
End Sub
Public Sub ActualizarHorasContrato()
'Dim cn As New System.Data.OleDb.OleDbConnection(Conexion)
'Dim cmd As New System.Data.OleDb.OleDbCommand
Dim cn As New System.Data.SqlClient.SqlConnection(ConexionSqlServer)
Dim cmd As New System.Data.SqlClient.SqlCommand
cmd.Connection = cn
Try
cn.Open()
'cmd.CommandText = "UPDATE CRM_INARI.dbo.Usuarios set HorasContrato = cast([Horas trabajo] as int) FROM CRM_INARI.dbo.Usuarios a, [NAVISION\SQL2014].LABOR.dbo.[Registro afiliacion] b WHERE a.NumEmpleado = b.[Cod_ Trabajador] COLLATE Modern_Spanish_CI_AS and enabled = 'True' and b.[Horas trabajo] > 0 and b.[Fecha Real] = (SELECT max(c.[Fecha Real]) FROM [NAVISION\SQL2014].LABOR.dbo.[Registro afiliacion] c WHERE c.[Horas trabajo] > 0 and b.[Cod_ Trabajador] = c.[Cod_ Trabajador]) and b.[Fecha Envio] = (SELECT max(d.[Fecha Envio]) FROM [NAVISION\SQL2014].LABOR.dbo.[Registro afiliacion] d WHERE d.[Horas trabajo] > 0 and b.[Cod_ Trabajador] = d.[Cod_ Trabajador])"
cmd.CommandText = "UPDATE CRM_INARI.dbo.Usuarios " & _
"SET HorasContrato = cast([Horas trabajo] as int) " & _
"FROM CRM_INARI.dbo.Usuarios a, [NAVISION\SQL2014].LABOR.dbo.[Dato Laboral] b " & _
"WHERE a.NumEmpleado = b.[Cod_ Trabajador] COLLATE Modern_Spanish_CI_AS " & _
"AND enabled = 'True' AND b.[Horas trabajo] > 0 " & _
"AND [Fecha Alta] in " & _
"(select max(c.[Fecha Alta]) from [NAVISION\SQL2014].LABOR.dbo.[Dato Laboral] c where b.[Cod_ Trabajador] = c.[Cod_ Trabajador])"
registrosAfectados = cmd.ExecuteNonQuery()
Log("ActualizarHorasContrato", "Actualizados " & registrosAfectados & ". CRM_INARI.dbo.Usuarios.HorasContrato ")
Catch ex As Exception
TratarError("ActualizarHorasContrato", ex)
Finally
cn.Close()
End Try
End Sub
Public Sub InsertarAgentesConHermes()
'Dim cn As New System.Data.OleDb.OleDbConnection(Conexion)
'Dim cmd As New System.Data.OleDb.OleDbCommand
Dim cn As New System.Data.SqlClient.SqlConnection(ConexionSqlServer)
Dim cmd As New System.Data.SqlClient.SqlCommand
cmd.Connection = cn
cmd.CommandTimeout = CommandTimeoutQueriesSlow
'CARGAMOS TODOS LOS REGISTROS QUE NO SON DE SERVICIOSMSB
Dim CadenaFirstCamp = ""
CadenaFirstCamp = CargaFirstCamp()
Try
cn.Open()
'SOLO 1 DIA: cmd.CommandText = "INSERT BBDD_ControlAsistencia.dbo." & NombreTabla & " (LoginHermes, CodCamp, DescCamp,NumEmp,NomEmp,LoginPortal,Dia,HorasContr,HoraInPortal,HoraOutPortal,TiempoPortal,PausaComidaP,PausaPVDP,PausaDescansoP,PausaFormacionP,PausaMedicoP,PausaSindicatoP,PausaReunionP,TotalPausasP,HoraInHermes,HoraOutHermes,TiempoHermes,PausaComidaH,PausaPVDH,PausaDescansoH,PausaFormacionH,PausaSindicatoH,PausaReunionH,PausaMedicoH,TotalPausasH,NumLlamadasGes,TiempoConv,Tmo,Alarma,TipoDia,MinutosNocturnos,Transporte,TiempoPorServ) (select distinct firstagent, CodCamp, Cliente,'0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','LABORABLE','0','0','0' from BBDD_Informes.dbo.DatosODCalls, BBDD_Costes.dbo.CostesInforme where Firstagent like '" & TxtSedeLoginHermes.Text & "%' and FirstCamp = Firstcampaign and substring(CallLocalTimeString,1,8) ='" & FechaString & "' and firstagent > 0 and firstcamp not like 'servicios%' and firstagent in (select firstagent from BBDD_Informes.dbo.DatosODCalls where " & CadenaFirstCamp & " and substring(CallLocalTimeString,1,8) ='" & FechaString & "' and firstcamp not like 'servicios%'))"
cmd.CommandText = "INSERT BBDD_ControlAsistencia.dbo." & NombreTabla & _
" (LoginHermes, CodCamp, DescCamp,NumEmp,NomEmp,LoginPortal,Dia,HorasContr,HoraInPortal,HoraOutPortal,TiempoPortal,PausaComidaP,PausaPVDP,PausaDescansoP,PausaFormacionP,PausaMedicoP,PausaSindicatoP,PausaReunionP,TotalPausasP,HoraInHermes,HoraOutHermes,TiempoHermes,PausaComidaH,PausaPVDH,PausaDescansoH,PausaFormacionH,PausaSindicatoH,PausaReunionH,PausaMedicoH,TotalPausasH,NumLlamadasGes,TiempoConv,Tmo,Alarma,TipoDia,MinutosNocturnos,Transporte,TiempoPorServ) " & _
" (select distinct firstagent, CodCamp, Cliente,'0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','LABORABLE','0','0','0' " & _
" FROM BBDD_Informes.dbo.DatosODCalls, BBDD_Costes.dbo.CostesInforme " & _
" WHERE " & _
" FirstCamp = Firstcampaign " & _
" and (CodCamp IS NOT NULL and Cliente IS NOT NULL)" & _
" and Firstagent like '" & TxtSedeLoginHermes.Text & "%' " & _
" and (substring(CallLocalTimeString,1,8) >= '" & FechaDesdeString & "' and substring(CallLocalTimeString,1,8) <= '" & FechaHastaString & "') " & _
" and firstagent > 0 and firstcamp not like 'servicios%' " & _
" and firstagent in " & _
" (select firstagent from BBDD_Informes.dbo.DatosODCalls where " & CadenaFirstCamp & _
" and (substring(CallLocalTimeString,1,8) >= '" & FechaDesdeString & "' and substring(CallLocalTimeString,1,8) <= '" & FechaHastaString & "') " & _
" and firstcamp not like 'servicios%') " & _
" )"
registrosAfectados = cmd.ExecuteNonQuery()
Log("InsertarAgentesConHermes", "Actualizados " & registrosAfectados & ". BBDD_ControlAsistencia.dbo." & NombreTabla)
Log("InsertarAgentesConHermes", "SQL: " & vbCrLf & cmd.CommandText & vbCrLf & vbCrLf)
Catch ex As Exception
TratarError("InsertarAgentesConHermes", ex)
If ex.GetType = GetType(SqlException) Then
Dim sqlEx As SqlException
sqlEx = CType(ex, SqlException)
Log("InsertarAgentesConHermes", "SQL: " & vbCrLf & cmd.CommandText & vbCrLf & vbCrLf)
End If
Finally
cn.Close()
End Try
End Sub
Public Sub InsertarAgentesConHermesSinLlamadas()
'Dim cn As New System.Data.OleDb.OleDbConnection(Conexion)
'Dim cmd As New System.Data.OleDb.OleDbCommand
Dim cn As New System.Data.SqlClient.SqlConnection(ConexionSqlServer)
Dim cmd As New System.Data.SqlClient.SqlCommand
cmd.Connection = cn
cmd.CommandTimeout = CommandTimeoutQueriesSlow
registrosAfectados = 0
'CARGAMOS TODOS LOS REGISTROS QUE SE LOGAN EN HERMES PERO NO REALIZAN LLAMADAS
Try
cn.Open()
If CmbServicios.Value = "TELEFONICA" Or CmbServicios.Value = "TODOS" Then
cmd.CommandText = "INSERT BBDD_ControlAsistencia.dbo." & NombreTabla & " (LoginHermes, CodCamp, DescCamp,NumEmp,NomEmp,LoginPortal,Dia,HorasContr,HoraInPortal,HoraOutPortal,TiempoPortal,PausaComidaP,PausaPVDP,PausaDescansoP,PausaFormacionP,PausaMedicoP,PausaSindicatoP,PausaReunionP,TotalPausasP,HoraInHermes,HoraOutHermes,TiempoHermes,PausaComidaH,PausaPVDH,PausaDescansoH,PausaFormacionH,PausaSindicatoH,PausaReunionH,PausaMedicoH,TotalPausasH,NumLlamadasGes,TiempoConv,Tmo,Alarma,TipoDia,MinutosNocturnos,Transporte,TiempoPorServ) (select distinct AgentId, '025', 'Telefonica','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','LABORABLE','0','0','0' from BBDD_Informes.dbo.DatosODActions a, CRM_INARI.dbo.Usuarios b where agentid like '" & TxtSedeLoginHermes.Text & "%' and agentid = UsuHermes and (substring(ActionLocalTimeString,1,8) >= '" & FechaDesdeString & "' and substring(ActionLocalTimeString,1,8) <= '" & FechaHastaString & "') and agentid not in (select LoginHermes from BBDD_ControlAsistencia.dbo." & NombreTabla & ") and b.Id in (select IdUsuario from CRM_INARI.dbo.Usuarios_GruposSup where IdGrupoSup in (1,2,3,8,12,31,34,35,36,37,38,39,40,41,42) ) )"
registrosAfectados = registrosAfectados + cmd.ExecuteNonQuery()
Log("InsertarAgentesConHermesSinLlamadas", "Actualizados " & registrosAfectados & ". BBDD_ControlAsistencia.dbo." & NombreTabla)
End If
If CmbServicios.Value = "JURIDICOS" Or CmbServicios.Value = "TODOS" Then
cmd.CommandText = "INSERT BBDD_ControlAsistencia.dbo." & NombreTabla & " (LoginHermes, CodCamp, DescCamp,NumEmp,NomEmp,LoginPortal,Dia,HorasContr,HoraInPortal,HoraOutPortal,TiempoPortal,PausaComidaP,PausaPVDP,PausaDescansoP,PausaFormacionP,PausaMedicoP,PausaSindicatoP,PausaReunionP,TotalPausasP,HoraInHermes,HoraOutHermes,TiempoHermes,PausaComidaH,PausaPVDH,PausaDescansoH,PausaFormacionH,PausaSindicatoH,PausaReunionH,PausaMedicoH,TotalPausasH,NumLlamadasGes,TiempoConv,Tmo,Alarma,TipoDia,MinutosNocturnos,Transporte,TiempoPorServ) (select distinct AgentId, '116', 'Juridicos','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','LABORABLE','0','0','0' from BBDD_Informes.dbo.DatosODActions a, CRM_INARI.dbo.Usuarios b where agentid like '" & TxtSedeLoginHermes.Text & "%' and agentid = UsuHermes and (substring(ActionLocalTimeString,1,8) >= '" & FechaDesdeString & "' and substring(ActionLocalTimeString,1,8) <= '" & FechaHastaString & "') and agentid not in (select LoginHermes from BBDD_ControlAsistencia.dbo." & NombreTabla & ") and b.Id in (select IdUsuario from CRM_INARI.dbo.Usuarios_GruposSup where IdGrupoSup in (80) ) )"
registrosAfectados = registrosAfectados + cmd.ExecuteNonQuery()
Log("InsertarAgentesConHermesSinLlamadas", "Actualizados " & registrosAfectados & ". BBDD_ControlAsistencia.dbo." & NombreTabla)
End If
If CmbServicios.Value = "VENTANILLA UNICA" Or CmbServicios.Value = "TODOS" Then
cmd.CommandText = "INSERT BBDD_ControlAsistencia.dbo." & NombreTabla & " (LoginHermes, CodCamp, DescCamp,NumEmp,NomEmp,LoginPortal,Dia,HorasContr,HoraInPortal,HoraOutPortal,TiempoPortal,PausaComidaP,PausaPVDP,PausaDescansoP,PausaFormacionP,PausaMedicoP,PausaSindicatoP,PausaReunionP,TotalPausasP,HoraInHermes,HoraOutHermes,TiempoHermes,PausaComidaH,PausaPVDH,PausaDescansoH,PausaFormacionH,PausaSindicatoH,PausaReunionH,PausaMedicoH,TotalPausasH,NumLlamadasGes,TiempoConv,Tmo,Alarma,TipoDia,MinutosNocturnos,Transporte,TiempoPorServ) (select distinct AgentId, '043', 'Ventanilla Unica','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','LABORABLE','0','0','0' from BBDD_Informes.dbo.DatosODActions a, CRM_INARI.dbo.Usuarios b where agentid like '" & TxtSedeLoginHermes.Text & "%' and agentid = UsuHermes and (substring(ActionLocalTimeString,1,8) >= '" & FechaDesdeString & "' and substring(ActionLocalTimeString,1,8) <= '" & FechaHastaString & "') and agentid not in (select LoginHermes from BBDD_ControlAsistencia.dbo." & NombreTabla & ") and b.Id in (select IdUsuario from CRM_INARI.dbo.Usuarios_GruposSup where IdGrupoSup in (66) ) )"
registrosAfectados = registrosAfectados + cmd.ExecuteNonQuery()
End If
Log("InsertarAgentesConHermesSinLlamadas", "Actualizados " & registrosAfectados & ". BBDD_ControlAsistencia.dbo." & NombreTabla)
Log("InsertarAgentesConHermesSinLlamadas", "SQL: " & vbCrLf & cmd.CommandText & vbCrLf & vbCrLf)
Catch ex As Exception
TratarError("InsertarAgentesConHermesSinLlamadas", ex)
If ex.GetType = GetType(SqlException) Then
Dim sqlEx As SqlException
sqlEx = CType(ex, SqlException)
Log("InsertarAgentesConHermesSinLlamadas", "SQL: " & vbCrLf & cmd.CommandText & vbCrLf & vbCrLf)
End If
Finally
cn.Close()
End Try
End Sub
Public Function CargaFirstCamp()
Dim Servicio As String = ""
'Dim cn As New System.Data.OleDb.OleDbConnection(Conexion)
'Dim cmd As New System.Data.OleDb.OleDbCommand
Dim cn As New System.Data.SqlClient.SqlConnection(ConexionSqlServer)
Dim cmd As New System.Data.SqlClient.SqlCommand
cmd.Connection = cn
'Dim dr As OleDb.OleDbDataReader
Dim dr As System.Data.SqlClient.SqlDataReader
Try
cn.Open()
If CmbServicios.Value = "TODOS" Then
cmd.CommandText = "SELECT distinct(FirstCamp) FROM BBDD_Costes.dbo.CostesInforme where firstcamp not like 'servicios%' and FirstCamp <> ''"
Else
cmd.CommandText = "SELECT distinct(FirstCamp), Estado, Detalle, Cola FROM BBDD_Costes.dbo.CostesInforme where firstcamp not like 'servicios%' and Cliente = '" & CmbServicios.Value & "'"
End If
dr = cmd.ExecuteReader
While dr.Read
If Servicio = "" Then
Servicio = "'" & dr(0) & "'"
Else
Servicio = Servicio & ",'" & dr(0) & "'"
End If
End While
If Not dr.IsClosed Then
dr.Close()
End If
Catch ex As Exception
TratarError("CargaFirstCamp", ex)
Finally
cn.Close()
End Try
If Servicio = "" Then
Servicio = " firstcampaign = 'SIN SERVICIO' "
Else
Servicio = " firstcampaign in ( " & Servicio & ") "
End If
Return Servicio
End Function
Public Sub InsertarAgentesSinHermes()
'Dim cn As New System.Data.OleDb.OleDbConnection(Conexion)
'Dim cmd As New System.Data.OleDb.OleDbCommand
Dim cn As New System.Data.SqlClient.SqlConnection(ConexionSqlServer)
Dim cmd As New System.Data.SqlClient.SqlCommand
cmd.Connection = cn
CargaSedes()
Try
cn.Open()
If CmbServicios.Value = "TODOS" Then
'SOLO 1 DIA: cmd.CommandText = "INSERT BBDD_ControlAsistencia.dbo." & NombreTabla & " (LoginHermes, CodCamp, DescCamp,NumEmp,NomEmp,LoginPortal,Dia,HorasContr,HoraInPortal,HoraOutPortal,TiempoPortal,PausaComidaP,PausaPVDP,PausaDescansoP,PausaFormacionP,PausaMedicoP,PausaSindicatoP,PausaReunionP,TotalPausasP,HoraInHermes,HoraOutHermes,TiempoHermes,PausaComidaH,PausaPVDH,PausaDescansoH,PausaFormacionH,PausaSindicatoH,PausaReunionH,PausaMedicoH,TotalPausasH,NumLlamadasGes,TiempoConv,Tmo,Alarma,TipoDia,MinutosNocturnos,Transporte,TiempoPorServ) (select '0', b.CodCamp, '0','0','0',a.Usuario,'0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','LABORABLE','0','0','0' from CRM_INARI.dbo.Usuarios a, CRM_Inari.dbo.GruposSupervision b, CRM_Inari.dbo.Usuarios_GruposSup c where a.Id = c.IdUsuario and c.IdGrupoSup = b.ID and a.Sede in ('" & Sede & "') and b.CodCamp <> '0' and a.Usuario in (select d.Usuario from CRM_Inari.dbo.HistoricoUser d where fechaIni = '" & FechaString & "') and b.CodCamp in (select e.CodCamp from BBDD_Costes.dbo.CostesInforme e where CTI = 'NO'))"
cmd.CommandText = "INSERT BBDD_ControlAsistencia.dbo." & NombreTabla & " (LoginHermes, CodCamp, DescCamp,NumEmp,NomEmp,LoginPortal,Dia,HorasContr,HoraInPortal,HoraOutPortal,TiempoPortal,PausaComidaP,PausaPVDP,PausaDescansoP,PausaFormacionP,PausaMedicoP,PausaSindicatoP,PausaReunionP,TotalPausasP,HoraInHermes,HoraOutHermes,TiempoHermes,PausaComidaH,PausaPVDH,PausaDescansoH,PausaFormacionH,PausaSindicatoH,PausaReunionH,PausaMedicoH,TotalPausasH,NumLlamadasGes,TiempoConv,Tmo,Alarma,TipoDia,MinutosNocturnos,Transporte,TiempoPorServ) (select '0', b.CodCamp, '0','0','0',a.Usuario,'0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','LABORABLE','0','0','0' from CRM_INARI.dbo.Usuarios a, CRM_Inari.dbo.GruposSupervision b, CRM_Inari.dbo.Usuarios_GruposSup c where a.Id = c.IdUsuario and c.IdGrupoSup = b.ID and a.Sede in ('" & Sede & "') and b.CodCamp <> '0' and a.Usuario in (select d.Usuario from CRM_Inari.dbo.HistoricoUser d where fechaIni >= '" & FechaDesdeString & "' and fechaIni <= '" & FechaHastaString & "') and b.CodCamp in (select e.CodCamp from BBDD_Costes.dbo.CostesInforme e where CTI = 'NO'))"
Else
'SOLO 1 DIA: cmd.CommandText = "INSERT BBDD_ControlAsistencia.dbo." & NombreTabla & " (LoginHermes, CodCamp, DescCamp,NumEmp,NomEmp,LoginPortal,Dia,HorasContr,HoraInPortal,HoraOutPortal,TiempoPortal,PausaComidaP,PausaPVDP,PausaDescansoP,PausaFormacionP,PausaMedicoP,PausaSindicatoP,PausaReunionP,TotalPausasP,HoraInHermes,HoraOutHermes,TiempoHermes,PausaComidaH,PausaPVDH,PausaDescansoH,PausaFormacionH,PausaSindicatoH,PausaReunionH,PausaMedicoH,TotalPausasH,NumLlamadasGes,TiempoConv,Tmo,Alarma,TipoDia,MinutosNocturnos,Transporte,TiempoPorServ) (select '0', b.CodCamp, '0','0','0',a.Usuario,'0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','LABORABLE','0','0','0' from CRM_INARI.dbo.Usuarios a, CRM_Inari.dbo.GruposSupervision b, CRM_Inari.dbo.Usuarios_GruposSup c where a.Id = c.IdUsuario and c.IdGrupoSup = b.ID and a.Sede in ('" & Sede & "') and b.CodCamp <> '0' and a.Usuario in (select d.Usuario from CRM_Inari.dbo.HistoricoUser d where fechaIni = '" & FechaString & "') and b.CodCamp in (select e.CodCamp from BBDD_Costes.dbo.CostesInforme e where CTI = 'NO' and Cliente = '" & CmbServicios.Value & "'))"
cmd.CommandText = "INSERT BBDD_ControlAsistencia.dbo." & NombreTabla & _
" (LoginHermes, CodCamp, DescCamp,NumEmp,NomEmp,LoginPortal,Dia,HorasContr,HoraInPortal,HoraOutPortal,TiempoPortal,PausaComidaP,PausaPVDP,PausaDescansoP,PausaFormacionP,PausaMedicoP,PausaSindicatoP,PausaReunionP,TotalPausasP,HoraInHermes,HoraOutHermes,TiempoHermes,PausaComidaH,PausaPVDH,PausaDescansoH,PausaFormacionH,PausaSindicatoH,PausaReunionH,PausaMedicoH,TotalPausasH,NumLlamadasGes,TiempoConv,Tmo,Alarma,TipoDia,MinutosNocturnos,Transporte,TiempoPorServ) " & _
" (select '0', b.CodCamp, '0','0','0',a.Usuario,'0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','LABORABLE','0','0','0' " & _
" FROM CRM_INARI.dbo.Usuarios a, CRM_Inari.dbo.GruposSupervision b, CRM_Inari.dbo.Usuarios_GruposSup c " & _
" WHERE a.Id = c.IdUsuario and c.IdGrupoSup = b.ID and b.CodCamp <> '0' " & _
" and a.Usuario in " & _
" (select d.Usuario from CRM_Inari.dbo.HistoricoUser d where fechaIni >= '" & FechaDesdeString & "' and fechaIni <= '" & FechaHastaString & "') " & _
" and b.CodCamp in " & _
" (select e.CodCamp from BBDD_Costes.dbo.CostesInforme e where CTI = 'NO' and Cliente = '" & CmbServicios.Value & "')" & _
" )"
End If
registrosAfectados = cmd.ExecuteNonQuery()
Log("InsertarAgentesSinHermes", "INSERTados " & registrosAfectados & ". BBDD_ControlAsistencia.dbo." & NombreTabla)
Log("InsertarAgentesSinHermes", "SQL: " & vbCrLf & cmd.CommandText & vbCrLf & vbCrLf)
cmd.CommandText = "UPDATE BBDD_ControlAsistencia.dbo." & NombreTabla & _
" SET DESCCAMP = b.Cliente " & _
" FROM BBDD_ControlAsistencia.dbo." & NombreTabla & " a, BBDD_Costes.dbo.costesinforme b where a.CodCamp = b.CodCamp"
registrosAfectados = cmd.ExecuteNonQuery()
Log("InsertarAgentesSinHermes", "Actualizados " & registrosAfectados & ". BBDD_ControlAsistencia.dbo." & NombreTabla)
Log("InsertarAgentesSinHermes", "SQL: " & vbCrLf & cmd.CommandText & vbCrLf & vbCrLf)
Catch ex As Exception
TratarError("InsertarAgentesSinHermes", ex)
Finally
cn.Close()
End Try
End Sub
Public Sub RellenaInfoAgentes()
'Dim cn As New System.Data.OleDb.OleDbConnection(Conexion)
'Dim cmd As New System.Data.OleDb.OleDbCommand
Dim cn As New System.Data.SqlClient.SqlConnection(ConexionSqlServer)
Dim cmd As New System.Data.SqlClient.SqlCommand
cmd.Connection = cn
Try
cn.Open()
'SOLO 1 DIA: cmd.CommandText = "update BBDD_ControlAsistencia.dbo." & NombreTabla & " set NumEmp = NumEmpleado, NomEmp = Operador, LoginPortal = Usuario, LoginHermes = UsuHermes, Dia = '" & Fecha & "', HorasContr = HorasContrato from BBDD_ControlAsistencia.dbo." & NombreTabla & ", CRM_INARI.dbo.Usuarios where LoginHermes = UsuHermes or LoginPortal = Usuario"
' " OUTPUT inserted.NumEmp, inserted.NomEmp, inserted.LoginPortal, inserted.LoginHermes, inserted.Dia, inserted.HorasContr " & _
If FechaDesde = FechaHasta Then
cmd.CommandText = "update BBDD_ControlAsistencia.dbo." & NombreTabla & _
" SET NumEmp = NumEmpleado, NomEmp = Operador, LoginPortal = Usuario, LoginHermes = UsuHermes, Dia = '" & FechaDesde & "', HorasContr = HorasContrato " & _
" FROM BBDD_ControlAsistencia.dbo." & NombreTabla & ", CRM_INARI.dbo.Usuarios where LoginHermes = UsuHermes or LoginPortal = Usuario"
Else
cmd.CommandText = "update BBDD_ControlAsistencia.dbo." & NombreTabla & _
" SET NumEmp = NumEmpleado, NomEmp = Operador, LoginPortal = Usuario, LoginHermes = UsuHermes, HorasContr = HorasContrato " & _
" FROM BBDD_ControlAsistencia.dbo." & NombreTabla & ", CRM_INARI.dbo.Usuarios where LoginHermes = UsuHermes or LoginPortal = Usuario"
End If
registrosAfectados = cmd.ExecuteNonQuery()
Log("RellenaInfoAgentes", "Actualizados " & registrosAfectados & ". BBDD_ControlAsistencia.dbo." & NombreTabla)
Catch ex As Exception
TratarError("RellenaInfoAgentes", ex)
Finally
cn.Close()
End Try
End Sub
Public Sub DatosPortal()
'Dim cn As New System.Data.OleDb.OleDbConnection(Conexion)
'Dim cmd As New System.Data.OleDb.OleDbCommand
Dim cn As New System.Data.SqlClient.SqlConnection(ConexionSqlServer)
Dim cmd As New System.Data.SqlClient.SqlCommand
cmd.Connection = cn
cmd.CommandTimeout = CommandTimeoutQueriesSlow
Try
cn.Open()
If FechaDesde = FechaHasta Then
cmd.CommandText = "update BBDD_ControlAsistencia.dbo." & NombreTabla & " set " & _
"HoraInPortal = (select isnull(min(HoraIni),0) from CRM_INARI.dbo.HistoricoUser where Evento = 'LOGIN' and fecha = '" & FechaDesdeString & "' and Usuario = LoginPortal), " & _
"HoraOutPortal = (select isnull(max(HoraIni),0) from CRM_INARI.dbo.HistoricoUser where Evento = 'LOGOUT' and fecha = '" & FechaDesdeString & "' and Usuario = LoginPortal), " & _
"PausaComidaP = (select isnull(Round(SUM(Duracion*1)/60,2),0) from CRM_INARI.dbo.HistoricoUser where Evento = 'COMIDA' and fecha = '" & FechaDesdeString & "' and Usuario = LoginPortal), " & _
"PausaPVDP = (select isnull(Round(SUM(Duracion*1)/60,2),0) from CRM_INARI.dbo.HistoricoUser where Evento = 'PVD' and fecha = '" & FechaDesdeString & "' and Usuario = LoginPortal), " & _
"PausaDescansoP = (select isnull(Round(SUM(Duracion*1)/60,2),0) from CRM_INARI.dbo.HistoricoUser where Evento = 'DESCANSO' and fecha = '" & FechaDesdeString & "' and Usuario = LoginPortal), " & _
"PausaFormacionP = (select isnull(Round(SUM(Duracion*1)/60,2),0) from CRM_INARI.dbo.HistoricoUser where Evento = 'FORMACION' and fecha = '" & FechaDesdeString & "' and Usuario = LoginPortal), " & _
"PausaMedicoP = (select isnull(Round(SUM(Duracion*1)/60,2),0) from CRM_INARI.dbo.HistoricoUser where Evento = 'MEDICO' and fecha = '" & FechaDesdeString & "' and Usuario = LoginPortal), " & _
"PausaSindicatoP = (select isnull(Round(SUM(Duracion*1)/60,2),0) from CRM_INARI.dbo.HistoricoUser where Evento = 'SINDICATO' and fecha = '" & FechaDesdeString & "' and Usuario = LoginPortal), " & _
"PausaReunionP = (select isnull(Round(SUM(Duracion*1)/60,2),0) from CRM_INARI.dbo.HistoricoUser where Evento = 'REUNION' and fecha = '" & FechaDesdeString & "' and Usuario = LoginPortal), " & _
"TotalPausasP = isnull((PausaComidaP*1 + PausaPVDP*1 + PausaDescansoP*1 + PausaFormacionP*1 + PausaMedicoP*1 + PausaSindicatoP*1 + PausaReunionP*1),0) " & _
"from BBDD_ControlAsistencia.dbo." & NombreTabla & ", CRM_INARI.dbo.HistoricoUser " & _
"where Usuario = LoginPortal"
Else
cmd.CommandText = "update BBDD_ControlAsistencia.dbo." & NombreTabla & " set " & _
"PausaComidaP = (select isnull(Round(SUM(Duracion*1)/60,2),0) from CRM_INARI.dbo.HistoricoUser where Evento = 'COMIDA' and fecha >= '" & FechaDesdeString & "' and fecha <= '" & FechaHastaString & "' and Usuario = LoginPortal), " & _
"PausaPVDP = (select isnull(Round(SUM(Duracion*1)/60,2),0) from CRM_INARI.dbo.HistoricoUser where Evento = 'PVD' and fecha >= '" & FechaDesdeString & "' and fecha <= '" & FechaHastaString & "' and Usuario = LoginPortal), " & _
"PausaDescansoP = (select isnull(Round(SUM(Duracion*1)/60,2),0) from CRM_INARI.dbo.HistoricoUser where Evento = 'DESCANSO' and fecha >= '" & FechaDesdeString & "' and fecha <= '" & FechaHastaString & "' and Usuario = LoginPortal), " & _
"PausaFormacionP = (select isnull(Round(SUM(Duracion*1)/60,2),0) from CRM_INARI.dbo.HistoricoUser where Evento = 'FORMACION' and fecha >= '" & FechaDesdeString & "' and fecha <= '" & FechaHastaString & "' and Usuario = LoginPortal), " & _
"PausaMedicoP = (select isnull(Round(SUM(Duracion*1)/60,2),0) from CRM_INARI.dbo.HistoricoUser where Evento = 'MEDICO' and fecha >= '" & FechaDesdeString & "' and fecha <= '" & FechaHastaString & "' and Usuario = LoginPortal), " & _
"PausaSindicatoP = (select isnull(Round(SUM(Duracion*1)/60,2),0) from CRM_INARI.dbo.HistoricoUser where Evento = 'SINDICATO' and fecha >= '" & FechaDesdeString & "' and fecha <= '" & FechaHastaString & "' and Usuario = LoginPortal), " & _
"PausaReunionP = (select isnull(Round(SUM(Duracion*1)/60,2),0) from CRM_INARI.dbo.HistoricoUser where Evento = 'REUNION' and fecha >= '" & FechaDesdeString & "' and fecha <= '" & FechaHastaString & "' and Usuario = LoginPortal), " & _
"TotalPausasP = isnull((PausaComidaP*1 + PausaPVDP*1 + PausaDescansoP*1 + PausaFormacionP*1 + PausaMedicoP*1 + PausaSindicatoP*1 + PausaReunionP*1),0) " & _
"from BBDD_ControlAsistencia.dbo." & NombreTabla & ", CRM_INARI.dbo.HistoricoUser " & _
"where Usuario = LoginPortal"
End If
registrosAfectados = cmd.ExecuteNonQuery()
Log("DatosPortal", "Actualizados " & registrosAfectados & ". BBDD_ControlAsistencia.dbo." & NombreTabla)
Log("DatosPortal", "SQL: " & vbCrLf & cmd.CommandText & vbCrLf & vbCrLf)
Catch ex As Exception
TratarError("DatosPortal", ex)
If ex.GetType = GetType(SqlException) Then
Dim sqlEx As SqlException
sqlEx = CType(ex, SqlException)
Log("DatosPortal", "SQL: " & vbCrLf & cmd.CommandText & vbCrLf & vbCrLf)
End If
Finally
cn.Close()
End Try
End Sub
Public Sub DatosHermesOdActions()
'Dim cn As New System.Data.OleDb.OleDbConnection(Conexion)
'Dim cmd As New System.Data.OleDb.OleDbCommand
Dim cn As New System.Data.SqlClient.SqlConnection(ConexionSqlServer)
Dim cmd As New System.Data.SqlClient.SqlCommand
cmd.Connection = cn
cmd.CommandTimeout = CommandTimeoutQueriesSlow
Try
cn.Open()
If FechaDesde = FechaHasta Then
cmd.CommandText = "update BBDD_ControlAsistencia.dbo." & NombreTabla & " set " & _
"HoraInHermes = (select isnull(min(SUBSTRING(ActionLocalTimeString,9,4)),0) from BBDD_Informes.dbo.DatosODActions where SUBSTRING(ActionLocalTimeString,1,8) = '" & FechaDesdeString & "' and AgentId = LoginHermes), " & _
"HoraOutHermes = (select isnull(max(SUBSTRING(ActionLocalTimeString,9,4)),0) from BBDD_Informes.dbo.DatosODActions where SUBSTRING(ActionLocalTimeString,1,8) = '" & FechaDesdeString & "' and AgentId = LoginHermes), " & _
"PausaComidaH = (select isnull(Round(sum(Duration)/100/60,2),0) from BBDD_Informes.dbo.DatosODActions where State = '3' and StateDetail = 7 and SUBSTRING(ActionLocalTimeString,1,8) = '" & FechaDesdeString & "' and AgentId = LoginHermes), " & _
"PausaPVDH = (select isnull(Round(sum(Duration)/100/60,2),0) from BBDD_Informes.dbo.DatosODActions where State = '3' and StateDetail = 0 and SUBSTRING(ActionLocalTimeString,1,8) = '" & FechaDesdeString & "' and AgentId = LoginHermes), " & _
"PausaDescansoH = (select isnull(Round(sum(Duration)/100/60,2),0) from BBDD_Informes.dbo.DatosODActions where State = '3' and StateDetail = 3 and SUBSTRING(ActionLocalTimeString,1,8) = '" & FechaDesdeString & "' and AgentId = LoginHermes), " & _
"PausaFormacionH = (select isnull(Round(sum(Duration)/100/60,2),0) from BBDD_Informes.dbo.DatosODActions where State = '3' and StateDetail = 2 and SUBSTRING(ActionLocalTimeString,1,8) = '" & FechaDesdeString & "' and AgentId =LoginHermes), " & _
"PausaSindicatoH = (select isnull(Round(sum(Duration)/100/60,2),0) from BBDD_Informes.dbo.DatosODActions where State = '3' and StateDetail = 5 and SUBSTRING(ActionLocalTimeString,1,8) = '" & FechaDesdeString & "' and AgentId = LoginHermes), " & _
"PausaReunionH = (select isnull(Round(sum(Duration)/100/60,2),0) from BBDD_Informes.dbo.DatosODActions where State = '3' and StateDetail = 1 and SUBSTRING(ActionLocalTimeString,1,8) = '" & FechaDesdeString & "' and AgentId = LoginHermes), " & _
"PausaMedicoH = (select isnull(Round(sum(Duration)/100/60,2),0) from BBDD_Informes.dbo.DatosODActions where State = '3' and StateDetail = 4 and SUBSTRING(ActionLocalTimeString,1,8) = '" & FechaDesdeString & "' and AgentId = LoginHermes), " & _
"AfterCall = (select isnull(Round(sum(Duration)/100/60,2),0) from BBDD_Informes.dbo.DatosODActions where State = '7' and SUBSTRING(ActionLocalTimeString,1,8) = '" & FechaDesdeString & "' and AgentId = LoginHermes) " & _
"from BBDD_ControlAsistencia.dbo." & NombreTabla & ", BBDD_Informes.dbo.DatosODActions " & _
"where LoginHermes = AgentId"
Else
cmd.CommandText = "update BBDD_ControlAsistencia.dbo." & NombreTabla & " set " & _
"PausaComidaH = (select isnull(Round(sum(Duration)/100/60,2),0) from BBDD_Informes.dbo.DatosODActions where State = '3' and StateDetail = 7 and SUBSTRING(ActionLocalTimeString,1,8) >= '" & FechaDesdeString & "' and SUBSTRING(ActionLocalTimeString,1,8) <= '" & FechaHastaString & "' and AgentId = LoginHermes), " & _
"PausaPVDH = (select isnull(Round(sum(Duration)/100/60,2),0) from BBDD_Informes.dbo.DatosODActions where State = '3' and StateDetail = 0 and SUBSTRING(ActionLocalTimeString,1,8) >= '" & FechaDesdeString & "' and SUBSTRING(ActionLocalTimeString,1,8) <= '" & FechaHastaString & "' and AgentId = LoginHermes), " & _
"PausaDescansoH = (select isnull(Round(sum(Duration)/100/60,2),0) from BBDD_Informes.dbo.DatosODActions where State = '3' and StateDetail = 3 and SUBSTRING(ActionLocalTimeString,1,8) >= '" & FechaDesdeString & "' and SUBSTRING(ActionLocalTimeString,1,8) <= '" & FechaHastaString & "' and AgentId = LoginHermes), " & _
"PausaFormacionH = (select isnull(Round(sum(Duration)/100/60,2),0) from BBDD_Informes.dbo.DatosODActions where State = '3' and StateDetail = 2 and SUBSTRING(ActionLocalTimeString,1,8) >= '" & FechaDesdeString & "' and SUBSTRING(ActionLocalTimeString,1,8) <= '" & FechaHastaString & "' and AgentId =LoginHermes), " & _
"PausaSindicatoH = (select isnull(Round(sum(Duration)/100/60,2),0) from BBDD_Informes.dbo.DatosODActions where State = '3' and StateDetail = 5 and SUBSTRING(ActionLocalTimeString,1,8) >= '" & FechaDesdeString & "' and SUBSTRING(ActionLocalTimeString,1,8) <= '" & FechaHastaString & "' and AgentId = LoginHermes), " & _
"PausaReunionH = (select isnull(Round(sum(Duration)/100/60,2),0) from BBDD_Informes.dbo.DatosODActions where State = '3' and StateDetail = 1 and SUBSTRING(ActionLocalTimeString,1,8) >= '" & FechaDesdeString & "' and SUBSTRING(ActionLocalTimeString,1,8) <= '" & FechaHastaString & "' and AgentId = LoginHermes), " & _
"PausaMedicoH = (select isnull(Round(sum(Duration)/100/60,2),0) from BBDD_Informes.dbo.DatosODActions where State = '3' and StateDetail = 4 and SUBSTRING(ActionLocalTimeString,1,8) >= '" & FechaDesdeString & "' and SUBSTRING(ActionLocalTimeString,1,8) <= '" & FechaHastaString & "' and AgentId = LoginHermes), " & _
"AfterCall = (select isnull(Round(sum(Duration)/100/60,2),0) from BBDD_Informes.dbo.DatosODActions where State = '7' and SUBSTRING(ActionLocalTimeString,1,8) >= '" & FechaDesdeString & "' and SUBSTRING(ActionLocalTimeString,1,8) <= '" & FechaHastaString & "' and AgentId = LoginHermes) " & _
"from BBDD_ControlAsistencia.dbo." & NombreTabla & ", BBDD_Informes.dbo.DatosODActions " & _
"where LoginHermes = AgentId"
End If
registrosAfectados = cmd.ExecuteNonQuery()
Log("DatosHermesOdActions", "Actualizados " & registrosAfectados & ". BBDD_ControlAsistencia.dbo." & NombreTabla)
Log("DatosHermesOdActions", "SQL: " & vbCrLf & cmd.CommandText & vbCrLf & vbCrLf)
Catch ex As Exception
TratarError("DatosHermesOdActions", ex)
If ex.GetType = GetType(SqlException) Then
Dim sqlEx As SqlException
sqlEx = CType(ex, SqlException)
Log("DatosHermesOdActions", "SQL: " & vbCrLf & cmd.CommandText & vbCrLf & vbCrLf)
End If
Finally
cn.Close()
End Try
End Sub
Public Sub DatosHermesOdCalls()
'Dim cn As New System.Data.OleDb.OleDbConnection(Conexion)
'Dim cmd As New System.Data.OleDb.OleDbCommand
Dim cn As New System.Data.SqlClient.SqlConnection(ConexionSqlServer)
Dim cmd As New System.Data.SqlClient.SqlCommand
'Dim dr As OleDb.OleDbDataReader
Dim dr As System.Data.SqlClient.SqlDataReader
Dim Agente As String = ""
Dim Servicio As String = ""
cmd.Connection = cn
Try
cn.Open()
cmd.CommandText = "select LoginHermes, CodCamp from BBDD_ControlAsistencia.dbo." & NombreTabla & " where LoginHermes <> ''"
dr = cmd.ExecuteReader
Agente = ""
Servicio = ""
While dr.Read
Agente = dr(0)
If IsDBNull(dr(1)) Then
Log("DatosHermesOdCalls", "Agente: " & Agente & " Servicio NULO ")
Continue While
End If
Servicio = dr(1)
RellenaLlamGes(Agente, Servicio)
End While
If Not dr.IsClosed Then
dr.Close()
End If
Catch ex As Exception
TratarError("DatosHermesOdCalls", ex)
Finally
cn.Close()
End Try
End Sub
Public Sub RellenaLlamGes(ByVal Agente, Servicio)
'Dim cn As New System.Data.OleDb.OleDbConnection(Conexion)
'Dim cmd As New System.Data.OleDb.OleDbCommand
Dim cn As New System.Data.SqlClient.SqlConnection(ConexionSqlServer)
Dim cmd As New System.Data.SqlClient.SqlCommand
cmd.Connection = cn
Try
cn.Open()
cmd.CommandText = "update BBDD_ControlAsistencia.dbo." & NombreTabla & _
" SET " & _
" NumLlamadasGes = (select isnull(COUNT(*),0) from BBDD_Informes.dbo.DatosODCalls a where Substring(a.CallLocalTimeString,1,8) >= '" & FechaDesdeString & "' and Substring(a.CallLocalTimeString,1,8) <= '" & FechaHastaString & "' and a.FirstAgent = '" & Agente & "' " & _
" and ConvDuration > 0 and a.FirstCampaign in (select b.FirstCamp from BBDD_Costes.dbo.CostesInforme b where b.CodCamp = '" & Servicio & "' )), " & _
" TiempoConv = (select isnull(SUM(ConvDuration),0) from BBDD_Informes.dbo.DatosODCalls a where Substring(a.CallLocalTimeString,1,8) >= '" & FechaDesdeString & "' and Substring(a.CallLocalTimeString,1,8) <= '" & FechaHastaString & "' and a.FirstAgent = '" & Agente & "' " & _
" and a.FirstCampaign in (select b.FirstCamp from BBDD_Costes.dbo.CostesInforme b where b.CodCamp = '" & Servicio & "' )) " & _
"FROM BBDD_ControlAsistencia.dbo." & NombreTabla & " e, BBDD_Informes.dbo.DatosODCalls c, BBDD_Costes.dbo.CostesInforme d " & _
"WHERE e.CodCamp = d.CodCamp and c.Firstcampaign = d.Firstcamp and e.CodCamp = '" & Servicio & "' and e.LoginHermes = '" & Agente & "'"
registrosAfectados = cmd.ExecuteNonQuery()
Log("RellenaLlamGes", "Actualizados " & registrosAfectados & ". BBDD_ControlAsistencia.dbo." & NombreTabla)
Catch ex As Exception
TratarError("RellenaLlamGes", ex)
Finally
cn.Close()
End Try
End Sub
Public Sub RealizaCalculos()
If FechaDesde = FechaHasta Then
'Dim cn As New System.Data.OleDb.OleDbConnection(Conexion)
'Dim cmd As New System.Data.OleDb.OleDbCommand
Dim cn As New System.Data.SqlClient.SqlConnection(ConexionSqlServer)
Dim cmd As New System.Data.SqlClient.SqlCommand
'Dim dr As OleDb.OleDbDataReader
Dim dr As System.Data.SqlClient.SqlDataReader
Dim Agente As String = ""
Dim HoraIni As String = ""
Dim HoraFin As String = ""
cmd.Connection = cn
Try
cn.Open()
cmd.CommandText = "select LoginPortal, HoraInPortal, HoraOutPortal, HoraInHermes, HoraOutHermes from BBDD_ControlAsistencia.dbo." & NombreTabla & ""
dr = cmd.ExecuteReader
While dr.Read
Agente = dr(0)
HoraIni = dr(1)
HoraFin = dr(2)
If Len(HoraIni) > 2 And Len(HoraFin) > 2 Then
CalculaLogins(Agente, HoraIni, HoraFin, "TiempoPortal")
End If
HoraIni = dr(3)
HoraFin = dr(4)
If Len(HoraIni) > 2 And Len(HoraFin) > 2 Then
CalculaLogins(Agente, HoraIni, HoraFin, "TiempoHermes")
End If
End While
If Not dr.IsClosed Then
dr.Close()
End If
Catch ex As Exception
TratarError("RealizaCalculos", ex)
Finally
cn.Close()
End Try
Else
CalculaTiempoLogadoVariosDias()
End If
End Sub
Public Sub CalculaTiempoLogadoVariosDias()
'Dim cn As New System.Data.OleDb.OleDbConnection(Conexion)
'Dim cmd As New System.Data.OleDb.OleDbCommand
Dim cn As New System.Data.SqlClient.SqlConnection(ConexionSqlServer)
Dim cmd As New System.Data.SqlClient.SqlCommand
'Dim dr2 As OleDb.OleDbDataReader
'Dim dr As OleDb.OleDbDataReader
Dim dr2 As System.Data.SqlClient.SqlDataReader
Dim dr As System.Data.SqlClient.SqlDataReader
Dim FechaRecorrer As Date = DeFechaDesde.Date
Dim FechaRecorrerString As String
FechaRecorrerString = DevuelveString(FechaRecorrer)
Dim Agente As String = ""
Dim HoraIniHermes As String = ""
Dim HoraFinHermes As String = ""
Dim HoraIniPortal As String = ""
Dim HoraFinPortal As String = ""
cmd.Connection = cn
While FechaRecorrer <= DeFechaHasta.Date
Try
cn.Open()
cmd.CommandText = "select LoginPortal, isnull(min(SUBSTRING(ActionLocalTimeString,9,4)),0), isnull(max(SUBSTRING(ActionLocalTimeString,9,4)),0) from BBDD_Informes.dbo.DatosODActions, BBDD_ControlAsistencia.dbo." & NombreTabla & " where SUBSTRING(ActionLocalTimeString,1,8) = '" & FechaRecorrerString & "' and AgentId = LoginHermes group by LoginPortal"
dr = cmd.ExecuteReader
While dr.Read
Agente = dr(0)
HoraIniHermes = dr(1)
HoraFinHermes = dr(2)
If Len(HoraIniHermes) > 2 And Len(HoraFinHermes) > 2 Then
CalculaLogins(Agente, HoraIniHermes, HoraFinHermes, "TiempoHermes")
End If
End While
If Not dr.IsClosed Then
dr.Close()
End If
cmd.CommandText = "select LoginPortal, isnull(min(HoraIni),0), isnull(max(HoraIni),0) from CRM_INARI.dbo.HistoricoUser, BBDD_ControlAsistencia.dbo." & NombreTabla & " where Evento in ('LOGIN', 'LOGOUT') and fecha = '" & FechaRecorrerString & "' and Usuario = LoginPortal group by LoginPortal"
dr2 = cmd.ExecuteReader
While dr2.Read
Agente = dr2(0)
HoraIniPortal = dr2(1)
HoraFinPortal = dr2(2)
If Len(HoraIniPortal) > 2 And Len(HoraFinPortal) > 2 Then
CalculaLogins(Agente, HoraIniPortal, HoraFinPortal, "TiempoPortal")
End If
End While
If Not dr2.IsClosed Then
dr2.Close()
End If
Catch ex As Exception
TratarError("CalculaTiempoLogadoVariosDias", ex)
Finally
cn.Close()
End Try
FechaRecorrer = FechaRecorrer.Date.AddDays(1)
FechaRecorrerString = DevuelveString(FechaRecorrer)
End While
End Sub
Public Sub CalculaLogins(ByVal Agente, ByVal HoraIni, ByVal HoraFin, ByVal Campo)
Dim TotalMinIni As String
TotalMinIni = Left(HoraIni, 2) * 60 * 60
TotalMinIni = TotalMinIni * 1 + Mid(HoraIni, 3, 2) * 60
TotalMinIni = TotalMinIni * 1 + Right(HoraIni, 2)
Dim TotalMinFin As String
TotalMinFin = Left(HoraFin, 2) * 60 * 60
TotalMinFin = TotalMinFin * 1 + Mid(HoraFin, 3, 2) * 60
TotalMinFin = TotalMinFin * 1 + Right(HoraFin, 2)
Dim TotalMin As String = Math.Round((TotalMinFin - TotalMinIni) / 60)
'Dim cn As New System.Data.OleDb.OleDbConnection(Conexion)
'Dim cmd As New System.Data.OleDb.OleDbCommand
Dim cn As New System.Data.SqlClient.SqlConnection(ConexionSqlServer)
Dim cmd As New System.Data.SqlClient.SqlCommand
cmd.Connection = cn
Try
cn.Open()
cmd.CommandText = "update BBDD_ControlAsistencia.dbo." & NombreTabla & _
" SET " & Campo & " = " & Campo & " *1 + '" & TotalMin & "' *1 " & _
" WHERE LoginPortal = '" & Agente & "'"
registrosAfectados = cmd.ExecuteNonQuery()
Log("CalculaLogins", "Actualizados " & registrosAfectados & ". BBDD_ControlAsistencia.dbo." & NombreTabla)
Catch ex As Exception
TratarError("CalculaLogins", ex)
Finally
cn.Close()
End Try
End Sub
Public Sub RellenaCampos()
Dim updateTable As String = "UPDATE BBDD_ControlAsistencia.dbo." & NombreTabla
'Dim cn As New System.Data.OleDb.OleDbConnection(Conexion)
'Dim cmd As New System.Data.OleDb.OleDbCommand
Dim cn As New System.Data.SqlClient.SqlConnection(ConexionSqlServer)
Dim cmd As New System.Data.SqlClient.SqlCommand
cmd.Connection = cn
Try
registrosAfectados = 0
cn.Open()
cmd.CommandText = updateTable & _
" SET HoraInPortal = SUBSTRING(HoraInPortal,1,2) + ':' + SUBSTRING(HoraInPortal,3,2) where len(HoraInPortal) > 1"
registrosAfectados = registrosAfectados + cmd.ExecuteNonQuery()
cmd.CommandText = updateTable & _
" SET HoraOutPortal = SUBSTRING(HoraOutPortal,1,2) + ':' + SUBSTRING(HoraOutPortal,3,2) where len(HoraOutPortal) > 1"
registrosAfectados = registrosAfectados + cmd.ExecuteNonQuery()
cmd.CommandText = updateTable & _
" SET HoraInHermes = SUBSTRING(HoraInHermes,1,2) + ':' + SUBSTRING(HoraInHermes,3,2) where len(HoraInHermes) > 1"
registrosAfectados = registrosAfectados + cmd.ExecuteNonQuery()
cmd.CommandText = updateTable & _
" SET HoraOutHermes = SUBSTRING(HoraOutHermes,1,2) + ':' + SUBSTRING(HoraOutHermes,3,2) where len(HoraOutHermes) > 1"
registrosAfectados = registrosAfectados + cmd.ExecuteNonQuery()
cmd.CommandText = updateTable & _
" SET TotalPausasP = PausaPVDP*1 + PausaDescansoP*1 + PausaFormacionP*1 + PausaMedicoP*1 + PausaSindicatoP*1 + PausaReunionP*1, " & _
"TotalPausasH = PausaPVDH*1 + PausaDescansoH*1 + PausaFormacionH*1 + PausaMedicoH*1 + PausaSindicatoH*1 + PausaReunionH*1, " & _
"TiempoPortal = TiempoPortal*1 - PausaComidaP*1, TiempoHermes = TiempoHermes*1 - PausaComidaH*1, " & _
"MinutosDia = Round((HorasContr)*60/5,2) * " & NumDiasSel & ""
registrosAfectados = registrosAfectados + cmd.ExecuteNonQuery()
cmd.CommandText = updateTable & _
" SET Tmo = (TiempoConv*1 / NumLlamadasGes*1) where TiempoConv > 0"
registrosAfectados = registrosAfectados + cmd.ExecuteNonQuery()
cmd.CommandText = updateTable & _
" SET Tmo = cast(((Tmo % (24 * 60 * 60)) % (60 * 60)) / 60 as varchar) + ':' + cast(((Tmo % (24 * 60 * 60)) % (60 * 60)) % 60 as varchar) where Tmo > 0"
registrosAfectados = registrosAfectados + cmd.ExecuteNonQuery()
cmd.CommandText = updateTable & _
" SET TiempoConv = Round(TiempoConv/60,2) where TiempoConv > 0"
registrosAfectados = registrosAfectados + cmd.ExecuteNonQuery()
cmd.CommandText = updateTable & _
" SET Alarma = TiempoHermes*1 - MinutosDia*1 where TiempoHermes > 0"
registrosAfectados = registrosAfectados + cmd.ExecuteNonQuery()
cmd.CommandText = updateTable & _
" SET Alarma = TiempoPortal*1 - MinutosDia*1 where TiempoHermes <= 0"
registrosAfectados = registrosAfectados + cmd.ExecuteNonQuery()
cmd.CommandText = updateTable & _
" SET Transporte = 'SI' where HoraInHermes <> '0' and substring(HoraInHermes,1,2)*1 < 6"
registrosAfectados = registrosAfectados + cmd.ExecuteNonQuery()
cmd.CommandText = updateTable & _
" SET Transporte = 'SI' where HoraOutHermes <> '0' and substring(HoraOutHermes,1,2)*1 < 6"
registrosAfectados = registrosAfectados + cmd.ExecuteNonQuery()
cmd.CommandText = updateTable & _
" SET Transporte = 'SI' where HoraInPortal <> '0' and substring(HoraInPortal,1,2)*1 < 6"
registrosAfectados = registrosAfectados + cmd.ExecuteNonQuery()
cmd.CommandText = updateTable & _
" SET Transporte = 'SI' where HoraOutPortal <> '0' and substring(HoraOutPortal,1,2)*1 < 6"
registrosAfectados = registrosAfectados + cmd.ExecuteNonQuery()
cmd.CommandText = updateTable & _
" SET Transporte = 'NO' where Transporte = '0'"
registrosAfectados = registrosAfectados + cmd.ExecuteNonQuery()
cmd.CommandText = updateTable & _
" SET TMO = REPLACE(TMO,':',':0') where LEN(TMO) = 3"
registrosAfectados = registrosAfectados + cmd.ExecuteNonQuery()
cmd.CommandText = updateTable & _
" SET TiempoEspera = ISNULL(TiempoHermes,0)*1 - ISNULL(TotalPausasH,0)*1 - ISNULL(TiempoConv,0)*1 - ISNULL(AfterCall,0)*1 where TiempoHermes > 0"
registrosAfectados = registrosAfectados + cmd.ExecuteNonQuery()
Log("RellenaCampos", "Actualizados " & registrosAfectados & ". BBDD_ControlAsistencia.dbo." & NombreTabla)
Catch ex As Exception
TratarError("RellenaCampos", ex)
Finally
cn.Close()
End Try
End Sub
Public Sub IndicaSiFestivo()
If FechaDesde = FechaHasta Then
'Dim cn As New System.Data.OleDb.OleDbConnection(Conexion)
'Dim cmd As New System.Data.OleDb.OleDbCommand
Dim cn As New System.Data.SqlClient.SqlConnection(ConexionSqlServer)
Dim cmd As New System.Data.SqlClient.SqlCommand
'Dim dr As OleDb.OleDbDataReader
Dim dr As System.Data.SqlClient.SqlDataReader
Dim SwEsFEstivo As Boolean = False
Dim TipoFestivo As String = ""
Dim anio As Integer = FechaDesde.Year
Dim mes As Integer = FechaDesde.Month
Dim dia As Integer = FechaDesde.Day
cmd.Connection = cn
Try
cn.Open()
cmd.CommandText = "select count(*) FROM [NAVISION\SQL2014].LABOR.dbo.[Calendario General] " & _
"WHERE ano = '" & anio & "' and Mes = '" & mes & "' and ([dia festivo 1] = '" & dia & "' or [dia festivo 2] = '" & dia & "' or [dia festivo 3] = '" & dia & "' or [dia festivo 4] = '" & dia & "' or [dia festivo 5] = '" & dia & "' or [dia festivo 6] = '" & dia & "' or [dia festivo 7] = '" & dia & "' or [dia festivo 8] = '" & dia & "' or [dia festivo 9] = '" & dia & "' or [dia festivo 10] = '" & dia & "')"
dr = cmd.ExecuteReader
If dr.Read Then
If dr(0) > 0 Then
ActualizaTipoDia("", "('MAD','DGT','TA','SEV','BCN')")
SwEsFEstivo = True
End If
End If
If Not dr.IsClosed Then
dr.Close()
End If
If SwEsFEstivo = False Then
cmd.CommandText = "select [Cod_ comunid_ auton_],count(*) FROM [NAVISION\SQL2014].LABOR.dbo.[Calendario Comunidad Autonoma] " & _
"WHERE [Cod_ comunid_ auton_] in ('01','09','12') and ano = '" & anio & "' and Mes = '" & mes & "' and ([dia festivo 1] = '" & dia & "' or [dia festivo 2] = '" & dia & "' or [dia festivo 3] = '" & dia & "' or [dia festivo 4] = '" & dia & "' or [dia festivo 5] = '" & dia & "' or [dia festivo 6] = '" & dia & "' or [dia festivo 7] = '" & dia & "' or [dia festivo 8] = '" & dia & "' or [dia festivo 9] = '" & dia & "' or [dia festivo 10] = '" & dia & "') group by [Cod_ comunid_ auton_]"
dr = cmd.ExecuteReader
While dr.Read
If dr(1) > 0 Then
If dr(0) = "12" Then
ActualizaTipoDia("1", "('MAD','DGT','TA')")
ElseIf dr(0) = "01" Then
ActualizaTipoDia("3", "('SEV')")
ElseIf dr(0) = "09" Then
ActualizaTipoDia("4", "('BCN')")
End If
End If
End While
If Not dr.IsClosed Then
dr.Close()
End If
End If
Catch ex As Exception
TratarError("IndicaSiFestivo", ex)
Finally
cn.Close()
End Try
End If
End Sub
Public Sub ActualizaTipoDia(ByVal Hermes, Portal)
'Dim cn As New System.Data.OleDb.OleDbConnection(Conexion)
'Dim cmd As New System.Data.OleDb.OleDbCommand
Dim cn As New System.Data.SqlClient.SqlConnection(ConexionSqlServer)
Dim cmd As New System.Data.SqlClient.SqlCommand
cmd.Connection = cn
Try
cn.Open()
cmd.CommandText = "UPDATE BBDD_ControlAsistencia.dbo." & NombreTabla & _
" SET TipoDia = 'FESTIVO' " & _
"WHERE LoginHermes like '" & Hermes & "%' or LoginPortal in (select Usuario from CRM_Inari.dbo.Usuarios where Sede in " & Portal & " )"
registrosAfectados = registrosAfectados + cmd.ExecuteNonQuery()
Log("RellenaCampos", "Actualizados " & registrosAfectados & ". BBDD_ControlAsistencia.dbo." & NombreTabla)
Catch ex As Exception
TratarError("ActualizaTipoDia", ex)
Finally
cn.Close()
End Try
End Sub
Public Sub CalculaHorasNocturnas()
If FechaDesde = FechaHasta Then
'Dim cn As New System.Data.OleDb.OleDbConnection(Conexion)
'Dim cmd As New System.Data.OleDb.OleDbCommand
Dim cn As New System.Data.SqlClient.SqlConnection(ConexionSqlServer)
Dim cmd As New System.Data.SqlClient.SqlCommand
'Dim dr As OleDb.OleDbDataReader
Dim dr As System.Data.SqlClient.SqlDataReader
Dim Agente As String = ""
Dim HoraIni As String = ""
Dim HoraFin As String = ""
Dim MinutosNoct As Integer = 0
cmd.Connection = cn
Try
cn.Open()
cmd.CommandText = "select LoginPortal, HoraInPortal, HoraOutPortal, HoraInHermes, HoraOutHermes " & _
"FROM BBDD_ControlAsistencia.dbo." & NombreTabla & ""
dr = cmd.ExecuteReader
While dr.Read
Agente = dr(0)
If Len(dr(3)) > 1 Then
HoraIni = dr(3)
HoraFin = dr(4)
ElseIf Len(dr(1)) > 1 Then
HoraIni = dr(1)
HoraFin = dr(2)
End If
Dim i As Integer
If HoraIni > "0" And HoraFin > "0" Then
i = (Left(HoraIni, 2)) * 1
If i >= 0 And i < 6 Then
MinutosNoct = 60 - (Right(HoraIni, 2) * 1)
i += 1
End If
While i <> (Left(HoraFin, 2)) * 1
If i >= 0 And i < 6 Then
If i + 1 >= 0 And i + 1 < 6 Then
MinutosNoct += 60
Else
MinutosNoct += Right(HoraFin, 2)
End If
End If
i += 1
If i = 24 Then i = 0
End While
If (Left(HoraFin, 2)) * 1 < 6 Then
MinutosNoct += Right(HoraFin, 2)
End If
ActualizaCampo(Agente, MinutosNoct)
MinutosNoct = 0
Else
MinutosNoct = 0
ActualizaCampo(Agente, MinutosNoct)
End If
End While
If Not dr.IsClosed Then
dr.Close()
End If
Catch ex As Exception
TratarError("CalculaHorasNocturnas", ex)
Finally
cn.Close()
End Try
End If
End Sub
Public Sub ActualizaCampo(ByVal Agente, ByVal Minutos)
'Dim cn As New System.Data.OleDb.OleDbConnection(Conexion)
'Dim cmd As New System.Data.OleDb.OleDbCommand
Dim cn As New System.Data.SqlClient.SqlConnection(ConexionSqlServer)
Dim cmd As New System.Data.SqlClient.SqlCommand
cmd.Connection = cn
Try
cn.Open()
cmd.CommandText = "UPDATE BBDD_ControlAsistencia.dbo." & NombreTabla & _
" SET MinutosNocturnos = '" & Minutos & "' WHERE LoginPortal = '" & Agente & "'"
registrosAfectados = cmd.ExecuteNonQuery()
Log("ActualizaCampo", "Actualizados " & registrosAfectados & ". BBDD_ControlAsistencia.dbo." & NombreTabla)
Catch ex As Exception
TratarError("ActualizaCampo", ex)
Finally
cn.Close()
End Try
End Sub
Public Sub AgentesMasDeUnServicio()
'Dim cn As New System.Data.OleDb.OleDbConnection(Conexion)
'Dim cmd As New System.Data.OleDb.OleDbCommand
Dim cn As New System.Data.SqlClient.SqlConnection(ConexionSqlServer)
Dim cmd As New System.Data.SqlClient.SqlCommand
'Dim dr As OleDb.OleDbDataReader
Dim dr As System.Data.SqlClient.SqlDataReader
Dim Agente As String = ""
Dim CodCampPpal As String = ""
Dim LoginPortal As String = ""
Dim TiempoConvPpal As Integer = 0
cmd.Connection = cn
Try
cn.Open()
cmd.CommandText = "select Distinct a.CodCamp, a.LoginPortal, a.TiempoConv " & _
"FROM BBDD_ControlAsistencia.dbo." & NombreTabla & " a, BBDD_ControlAsistencia.dbo." & NombreTabla & " b " & _
"WHERE a.LoginPortal = b.LoginPortal and a.CodCamp <> b.CodCamp " & _
"and a.TiempoConv =(select MAX(c.tiempoConv*1) from BBDD_ControlAsistencia.dbo." & NombreTabla & " c where a.LoginPortal = c.LoginPortal)"
dr = cmd.ExecuteReader
While dr.Read
CodCampPpal = dr(0)
Agente = dr(1)
TiempoConvPpal = dr(2)
TiempoServSecundario(CodCampPpal, Agente, TiempoConvPpal)
TiempoServPrincipal(CodCampPpal, Agente, TiempoConvPpal)
End While
If Not dr.IsClosed Then
dr.Close()
End If
RestoTiemposServ()
Catch ex As Exception
TratarError("AgentesMasDeUnServicio", ex)
Finally
cn.Close()
End Try
End Sub
Public Sub TiempoServSecundario(ByVal CodCampPpal, ByVal Agente, ByVal TiempoConvPpal)
'Dim cn As New System.Data.OleDb.OleDbConnection(Conexion)
'Dim cmd As New System.Data.OleDb.OleDbCommand
Dim cn As New System.Data.SqlClient.SqlConnection(ConexionSqlServer)
Dim cmd As New System.Data.SqlClient.SqlCommand
cmd.Connection = cn
Try
cn.Open()
cmd.CommandText = "UPDATE BBDD_ControlAsistencia.dbo." & NombreTabla & _
" SET TiempoPorServ = TiempoConv, MinutosDia = '0' " & _
",PausaComidaP = '0',PausaPVDP = '0',PausaDescansoP = '0',PausaFormacionP = '0',PausaMedicoP = '0',PausaSindicatoP = '0',PausaReunionP = '0' " & _
",TotalPausasP = '0',PausaComidaH = '0',PausaPVDH = '0',PausaDescansoH = '0',PausaFormacionH = '0',PausaSindicatoH = '0',PausaReunionH = '0' " & _
",PausaMedicoH = '0',TotalPausasH = '0',Alarma = '0', MinutosNocturnos = '0',Transporte = 'NO' " & _
" WHERE CodCamp <> '" & CodCampPpal & "' and LoginPortal = '" & Agente & "' and TiempoConv <> '" & TiempoConvPpal & "'"
registrosAfectados = cmd.ExecuteNonQuery()
Log("TiempoServSecundario", "Actualizados " & registrosAfectados & ". BBDD_ControlAsistencia.dbo." & NombreTabla)
Catch ex As Exception
TratarError("TiempoServSecundario", ex)
Finally
cn.Close()
End Try
End Sub
Public Sub TiempoServPrincipal(ByVal CodCampPpal, ByVal Agente, ByVal TiempoConvPpal)
Dim tabla As String = "BBDD_ControlAsistencia.dbo." & NombreTabla
'Dim cn As New System.Data.OleDb.OleDbConnection(Conexion)
'Dim cmd As New System.Data.OleDb.OleDbCommand
Dim cn As New System.Data.SqlClient.SqlConnection(ConexionSqlServer)
Dim cmd As New System.Data.SqlClient.SqlCommand
cmd.Connection = cn
Try
cn.Open()
cmd.CommandText = "update " & tabla & _
" SET TiempoPorServ = TiempoHermes*1 - " & _
"(Select SUM(TiempoConv*1) from " & tabla & _
" WHERE CodCamp <> '" & CodCampPpal & "' and LoginPortal = '" & Agente & "' and TiempoConv <> '" & TiempoConvPpal & "'" & _
") " & _
"WHERE CodCamp = '" & CodCampPpal & "' and LoginPortal = '" & Agente & "' and TiempoConv = '" & TiempoConvPpal & "' and TiempoHermes > 0"
registrosAfectados = cmd.ExecuteNonQuery()
cmd.CommandText = "update " & tabla & _
" SET TiempoPorServ = TiempoPortal*1 - " & _
"(Select SUM(TiempoConv*1) from " & tabla & _
" WHERE CodCamp <> '" & CodCampPpal & "' and LoginPortal = '" & Agente & "' and TiempoConv <> '" & TiempoConvPpal & "'" & _
") " & _
"WHERE CodCamp = '" & CodCampPpal & "' and LoginPortal = '" & Agente & "' and TiempoConv = '" & TiempoConvPpal & "' and TiempoHermes = 0"
registrosAfectados = registrosAfectados + cmd.ExecuteNonQuery()
Log("TiempoServPrincipal", "Actualizados " & registrosAfectados & ". BBDD_ControlAsistencia.dbo." & NombreTabla)
Catch ex As Exception
TratarError("TiempoServPrincipal", ex)
Finally
cn.Close()
End Try
End Sub
Public Sub RestoTiemposServ()
'Dim cn As New System.Data.OleDb.OleDbConnection(Conexion)
'Dim cmd As New System.Data.OleDb.OleDbCommand
Dim cn As New System.Data.SqlClient.SqlConnection(ConexionSqlServer)
Dim cmd As New System.Data.SqlClient.SqlCommand
cmd.Connection = cn
Try
cn.Open()
cmd.CommandText = "update BBDD_ControlAsistencia.dbo." & NombreTabla & _
" SET TiempoPorServ = TiempoHermes*1 WHERE Tiempohermes > 0 and TiempoPorServ = 0 and tiempoconv > 0"
registrosAfectados = cmd.ExecuteNonQuery()
cmd.CommandText = "update BBDD_ControlAsistencia.dbo." & NombreTabla & _
" SET TiempoPorServ = TiempoPortal*1 WHERE Tiempohermes = 0 and TiempoPorServ = 0 and tiempoconv > 0"
registrosAfectados = registrosAfectados + cmd.ExecuteNonQuery()
Log("RestoTiemposServ", "Actualizados " & registrosAfectados & ". BBDD_ControlAsistencia.dbo." & NombreTabla)
Catch ex As Exception
TratarError("RestoTiemposServ", ex)
Finally
cn.Close()
End Try
End Sub
#End Region
Protected Sub BtnExportar_Click(sender As Object, e As EventArgs) Handles BtnExportar.Click
NombreTabla = "InformeAsistencia" & Session("login")
DsInforme.SelectCommand = "SELECT * FROM " & NombreTabla & " ORDER BY [NumEmp]"
DsInforme.DataBind()
GvInforme.DataSource = DsInforme
GvInforme.DataBind()
GvExportar.WriteXlsxToResponse("Informe Asistencia " & Date.Now)
End Sub
End Class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment