Files
SIG/GPS/DispositivosControles.aspx.vb

468 lines
20 KiB
VB.net

Option Strict On
Imports Extensiones
Imports System.Data
Imports System.IO
Imports GemBox.Spreadsheet
Partial Class GPS_DispositivosControles
Inherits ClsPaginas
#Region "Propiedades"
Public Property CveControl As Integer
Get
Return ViewState("CveControl").ToString.ToEntero
End Get
Set(value As Integer)
ViewState("CveControl") = value
End Set
End Property
Public Property EsEditar As Integer
Get
Return ViewState("EsEditar").ToString.ToEntero
End Get
Set(value As Integer)
ViewState("EsEditar") = value
End Set
End Property
#End Region
Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
CveControl = -1
EsEditar = 0
CargaDatos(-1)
CargaDdls()
End If
End Sub
Private Sub CargaDdls()
DdlEquipo.DataSource = GlobalDTDispositivos(-1)
DdlEquipo.DataBind()
DdlEquipo.Items.Insert(0, New ListItem("Seleccione", "-1"))
DdlEquipoTemp.DataSource = GlobalDTDispositivos(-1)
DdlEquipoTemp.DataBind()
DdlEquipoTemp.Items.Insert(0, New ListItem("Seleccione", "-1"))
Dim GPS As New ClsGPS
If GPS.UnidadMedidaGet() Then
DdlUnidadMedida.DataSource = GPS.DtResultados
DdlUMTemp.DataSource = GPS.DtResultados
Else
DdlUnidadMedida.DataSource = Nothing
DdlUMTemp.DataSource = Nothing
End If
GPS.Dispose()
DdlUnidadMedida.DataBind()
DdlUnidadMedida.Items.Insert(0, New ListItem("Seleccione", "-1"))
DdlUMTemp.DataBind()
DdlUMTemp.Items.Insert(0, New ListItem("Seleccione", "-1"))
End Sub
Private Sub CargaDatos(CveCliente As Integer)
Dim GPS As New ClsGPS
If GPS.ControlesGet(CveCliente) Then
GvDatos.DataSource = GPS.DtResultados.FiltraParametros($"CveEmpresa ={UsuarioActual.CveEmpresa}")
Else
GvDatos.DataSource = Nothing
End If
GPS.Dispose()
GvDatos.DataBind()
UpDatos.Update()
End Sub
Protected Sub imgE_Click(sender As Object, e As EventArgs)
Dim Boton = CType(sender, LinkButton)
CveControl = Boton.CommandArgument.ToEntero
Dim GvRow As GridViewRow = DirectCast(Boton.NamingContainer, GridViewRow)
DivTanque.Visible = True
EsEditar = 1
If GvRow.DataKey("CveTipoControl").ToString.ToEntero = 1 Then
HTitulo.InnerText = "Guardar Compuesto"
DdlUnidadMedida.SelectedValueExist(GvRow.DataKey("CveUnidadMedida"))
DdlEquipo.SelectedValueExist(GvRow.DataKey("CveDispositivo"))
TxtAlto.Text = GvRow.DataKey("Alto")
TxtAncho.Text = GvRow.DataKey("Ancho")
TxtCalibración.Text = "[]"
TxtCapacidad.Text = GvRow.DataKey("Capacidad")
TxtControl.Text = GvRow.DataKey("Control")
TxtLargo.Text = GvRow.DataKey("Largo")
TxtLongitud.Text = GvRow.DataKey("Longitud")
TxtSensor.Text = GvRow.DataKey("Sensor")
DdlNumero.SelectedValueExist(GvRow.DataKey("CveMetrica"))
BtnAgregarCatalogo.Text = "Actualizar"
UpAgregar.Update()
UpBodyCompuesto.Update()
UpFooter.Update()
RegistraScript("$('#divAgregar').modal()")
ElseIf GvRow.DataKey("CveTipoControl").ToString.ToEntero = 2 Then
DdlUMTemp.SelectedValueExist(GvRow.DataKey("CveUnidadMedida"))
DdlEquipoTemp.SelectedValueExist(GvRow.DataKey("CveDispositivo"))
TxtCapacidadTemp.Text = GvRow.DataKey("Capacidad")
TxtIDSensor.Text = GvRow.DataKey("Sensor")
DdlNoSensor.SelectedValueExist(GvRow.DataKey("CveMetrica"))
LbAgregarTemp.Text = "Actualizar"
RegistraScript("$('#dvAddTemp').modal()")
UpAddTemp.Update()
UPTempBody.Update()
UpdatePanel4.Update()
ElseIf GvRow.DataKey("CveTipoControl").ToString.ToEntero = 3 Then
HTitulo.InnerText = "Guardar Sensor de Pasajeros"
DdlUnidadMedida.SelectedValueExist(GvRow.DataKey("CveUnidadMedida"))
DdlEquipo.SelectedValueExist(GvRow.DataKey("CveDispositivo"))
TxtAlto.Text = GvRow.DataKey("Alto")
TxtAncho.Text = GvRow.DataKey("Ancho")
TxtCalibración.Text = "[]"
TxtCapacidad.Text = GvRow.DataKey("Capacidad")
TxtControl.Text = GvRow.DataKey("Control")
TxtLargo.Text = GvRow.DataKey("Largo")
TxtLongitud.Text = GvRow.DataKey("Longitud")
TxtSensor.Text = GvRow.DataKey("Sensor")
DdlNumero.SelectedValueExist(GvRow.DataKey("CveMetrica"))
BtnAgregarCatalogo.Text = "Actualizar"
DivTanque.Visible = False
UpBodyCompuesto.Update()
UpAgregar.Update()
UpFooter.Update()
RegistraScript("$('#divAgregar').modal()")
End If
End Sub
Protected Sub BtnAgregarCatalogo_Click(sender As Object, e As EventArgs) Handles BtnAgregarCatalogo.Click
Dim GPS As New ClsGPS
GPS.IniciaTransacSQL()
Dim huboerror As Boolean = False
If EsEditar = 0 Then
If TxtCalibración.getfiles().Archivos.Count > 0 Then
Dim Archivo As Archivo = TxtCalibración.getfiles().Archivos(0)
If Archivo.Extencion <> "xls" AndAlso Archivo.Extencion <> "xlsx" Then
GPS.RollBack()
GPS.Dispose()
Alert("El archivo de calibración debe de ser un archivo excel.")
Exit Sub
End If
End If
Dim CveTipoControl As Integer = If(DivTanque.Visible, 1, 3)
If GPS.ControlAdd(TxtControl.Text, DdlUnidadMedida.SelectedValue.ToEntero, TxtCapacidad.Text.ToDouble, TxtLargo.Text.ToDouble, TxtAncho.Text.ToDouble, TxtAlto.Text.ToDouble, TxtLongitud.Text.ToDouble, TxtSensor.Text, DdlEquipo.SelectedValue.ToEntero, DdlNumero.SelectedValue.ToEntero, CveTipoControl) Then
CveControl = GPS.CveControl
Else
huboerror = True
End If
If TxtCalibración.getfiles().Archivos.Count > 0 AndAlso CveControl > 0 AndAlso Not huboerror Then
Try
Dim Archivo As Archivo = TxtCalibración.getfiles().Archivos(0)
Dim Path As String = $"{ModExtensiones.DirectorioBaseEmpresas}TablasCalibracion"
Dim dirSonido As String = String.Format("{0}\{1}.{2}", Path, FormatearNombreArchivo(Archivo.filenameO), Archivo.Extencion)
If File.Exists(dirSonido) Then
My.Computer.FileSystem.DeleteFile(dirSonido)
End If
My.Computer.FileSystem.MoveFile(Archivo.FileUrl, dirSonido)
SpreadsheetInfo.SetLicense(SerialGemBoxSpreadsheet())
Dim EF As ExcelFile = ExcelFile.Load(dirSonido)
Dim Hoja As ExcelWorksheet = EF.Worksheets(0)
Dim Opciones As New CreateDataTableOptions()
Opciones.ColumnHeaders = True
Opciones.StartRow = 0
Opciones.NumberOfColumns = 3
Opciones.NumberOfRows = Hoja.Rows.Count
Opciones.Resolution = ColumnTypeResolution.AutoPreferStringInvariantCulture
Dim TempDT As DataTable = Hoja.CreateDataTable(Opciones)
If Not TempDT.Columns(0).ColumnName = "Nivel Varilla" Then
Alert("El nombre de la primera columna debe ser 'Nivel Varilla'.Favor de corregirlo", TipoMensaje.MsgError)
huboerror = True
End If
If Not TempDT.Columns(1).ColumnName = "Valor" Then
Alert("El nombre de la segunda columna debe ser 'Valor'.Favor de corregirlo", TipoMensaje.MsgError)
huboerror = True
End If
If Not huboerror Then
For Each row As DataRow In TempDT.Rows
Dim Nivel As Double = row("Nivel Varilla").ToString.ToDouble
Dim Valor As Double = row("Valor").ToString.ToDouble
If Not GPS.CalibracionAdd(Nivel, Valor, CveControl) Then
huboerror = True
Exit For
End If
Next
End If
If Not huboerror Then
huboerror = Not GPS.EjecutaCalibracion()
End If
Catch ex As Exception
huboerror = True
End Try
End If
If Not huboerror Then
GPS.Commit()
Alert("El Control se agregó correctamente")
Else
GPS.RollBack()
Alert("Ocurrió un error al realizar la calibración", TipoMensaje.MsgError)
End If
ElseIf EsEditar = 1 Then
If TxtCalibración.getfiles().Archivos.Count > 0 Then
Dim Archivo As Archivo = TxtCalibración.getfiles().Archivos(0)
If Archivo.Extencion <> "xls" AndAlso Archivo.Extencion <> "xlsx" Then
GPS.RollBack()
GPS.Dispose()
Alert("El archivo de calibración debe de ser un archivo excel.")
Exit Sub
End If
End If
If Not GPS.ControlUpdate(TxtControl.Text, DdlUnidadMedida.SelectedValue.ToEntero, TxtCapacidad.Text.ToDouble, TxtLargo.Text.ToDouble, TxtAncho.Text.ToDouble, TxtAlto.Text.ToDouble, TxtLongitud.Text.ToDouble, TxtSensor.Text, DdlEquipo.SelectedValue.ToEntero, CveControl, DdlNumero.SelectedValue.ToEntero,-1) Then
huboerror = True
End If
If TxtCalibración.getfiles().Archivos.Count > 0 AndAlso Not huboerror Then
Try
huboerror = Not GPS.CalibracionDelete(CveControl)
If Not huboerror Then
Dim Archivo As Archivo = TxtCalibración.getfiles().Archivos(0)
Dim Path As String = $"{ModExtensiones.DirectorioBaseEmpresas}TablasCalibracion"
Dim dirSonido As String = String.Format("{0}\{1}.{2}", Path, FormatearNombreArchivo(Archivo.filenameO), Archivo.Extencion)
If File.Exists(dirSonido) Then
My.Computer.FileSystem.DeleteFile(dirSonido)
End If
My.Computer.FileSystem.MoveFile(Archivo.FileUrl, dirSonido)
SpreadsheetInfo.SetLicense(SerialGemBoxSpreadsheet())
Dim EF As ExcelFile = ExcelFile.Load(dirSonido)
Dim Hoja As ExcelWorksheet = EF.Worksheets(0)
Dim Opciones As New CreateDataTableOptions()
Opciones.ColumnHeaders = True
Opciones.StartRow = 0
Opciones.NumberOfColumns = 3
Opciones.NumberOfRows = Hoja.Rows.Count
Opciones.Resolution = ColumnTypeResolution.AutoPreferStringInvariantCulture
Dim TempDT As DataTable = Hoja.CreateDataTable(Opciones)
If Not TempDT.Columns(0).ColumnName = "Nivel Varilla" Then
Alert("El nombre de la primera columna debe ser 'Nivel Varilla'.Favor de corregirlo", TipoMensaje.MsgError)
huboerror = True
End If
If Not TempDT.Columns(1).ColumnName = "Valor" Then
Alert("El nombre de la segunda columna debe ser 'Valor'.Favor de corregirlo", TipoMensaje.MsgError)
huboerror = True
End If
If Not huboerror Then
For Each row As DataRow In TempDT.Rows
Dim Nivel As Double = row("Nivel Varilla").ToString.ToDouble
Dim Valor As Double = row("Valor").ToString.ToDouble
If Not GPS.CalibracionAdd(Nivel, Valor, CveControl) Then
huboerror = True
Exit For
End If
Next
End If
If Not huboerror Then
huboerror = Not GPS.EjecutaCalibracion()
End If
End If
Catch ex As Exception
huboerror = True
End Try
End If
If Not huboerror Then
GPS.Commit()
Alert("El Control se actualizó correctamente")
Else
GPS.RollBack()
Alert("Ocurrió un error al realizar la calibración", TipoMensaje.MsgError)
End If
End If
GPS.Dispose()
TxtAlto.Text = ""
TxtAncho.Text = ""
TxtCalibración.Text = "[]"
TxtCapacidad.Text = ""
TxtControl.Text = ""
TxtLargo.Text = ""
TxtLongitud.Text = ""
TxtSensor.Text = ""
DdlEquipo.SelectedValueExist("-1")
DdlUnidadMedida.SelectedValueExist("-1")
CargaDatos(-1)
RegistraScript("$('#divAgregar').modal('hide')")
UpBodyCompuesto.Update()
UpFooter.Update()
End Sub
Protected Sub BtnAgregarTemp_Click(sender As Object, e As EventArgs) Handles LbAgregarTemp.Click
Dim GPS As New ClsGPS
Dim huboerror As Boolean = False
Dim CveTipoControl As Integer = 2
If EsEditar = 0 Then
If GPS.ControlAdd(TxtControlTemperatura.Text, DdlUMTemp.SelectedValue.ToEntero, TxtCapacidadTemp.Text.ToDouble, -1, -1, -1, -1, TxtIDSensor.Text, DdlEquipoTemp.SelectedValue.ToEntero, DdlNoSensor.SelectedValue.ToEntero, CveTipoControl) Then
CveControl = GPS.CveControl
Alert("El Control se agregó correctamente")
Else
huboerror = True
End If
ElseIf EsEditar = 1 Then
If Not GPS.ControlUpdate(TxtControlTemperatura.Text, DdlUMTemp.SelectedValue.ToEntero, TxtCapacidadTemp.Text.ToDouble, -1, -1, -1, -1, TxtIDSensor.Text, DdlEquipoTemp.SelectedValue.ToEntero, CveControl, DdlNoSensor.SelectedValue.ToEntero,CveTipoControl) Then
huboerror = True
Else
Alert("El Control se actualizó correctamente")
End If
End If
TxtControlTemperatura.Text = ""
TxtIDSensor.Text = ""
TxtCapacidadTemp.Text = ""
TxtControlTemperatura.Text = ""
DdlUnidadMedida.SelectedValueExist("-1")
DdlNoSensor.SelectedValueExist("-1")
GPS.Dispose()
CargaDatos(-1)
RegistraScript("$('#dvAddTemp').modal('hide')")
UpBodyCompuesto.Update()
UpFooter.Update()
End Sub
Protected Sub GvDatos_RowDeleting(sender As Object, e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles GvDatos.RowDeleting
Dim GPS As New ClsGPS
Dim CveControl As Integer = GvDatos.Rows(e.RowIndex).DataKey("CveControl").ToEntero
If GPS.ControlesDelete(CveControl) Then
Alert("El Control se eliminó correctamente", TipoMensaje.MsgInformacion)
End If
GPS.Dispose()
CargaDatos(-1)
UpDatos.Update()
End Sub
Protected Sub GvDatos_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GvDatos.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
e.Row.VerticalAlign = VerticalAlign.Middle
e.Row.FCLinkButton("ImgE").CommandArgument = e.Row.DataKey("CveControl").ToString
e.Row.FCLinkButton("ImgC").CommandArgument = e.Row.DataKey("CveControl").ToString
End If
End Sub
Protected Sub btnCancelar_Click(sender As Object, e As EventArgs) Handles BtnOpenDiv.Click
EsEditar = 0
BtnAgregarCatalogo.Text = "Agregar Compuesto"
TxtControl.Text = ""
TxtCapacidad.Text = ""
TxtLargo.Text = ""
TxtAncho.Text = ""
TxtAlto.Text = ""
TxtLongitud.Text = ""
TxtSensor.Text = ""
TxtCalibración.Text = "[]"
DdlEquipo.SelectedValueExist("-1")
DdlUnidadMedida.SelectedValueExist("-1")
DivTanque.Visible = True
UpBodyCompuesto.Update()
HTitulo.InnerText = "Guardar Compuesto"
UpAgregar.Update()
UpFooter.Update()
RegistraScript("$('#divAgregar').modal()")
End Sub
Public Function FormatearNombreArchivo(nombreArchivoOriginal As String) As String
Dim nombreArchivoFormateado As String
nombreArchivoFormateado = nombreArchivoOriginal.Replace(" ", "_")
nombreArchivoFormateado = nombreArchivoFormateado.Replace("'", "")
nombreArchivoFormateado = nombreArchivoFormateado.Replace(Chr(34), "")
nombreArchivoFormateado = nombreArchivoFormateado.Replace("$", "_")
nombreArchivoFormateado = nombreArchivoFormateado.Replace("#", "_")
nombreArchivoFormateado = nombreArchivoFormateado.Replace("%", "_")
nombreArchivoFormateado = nombreArchivoFormateado.Replace("!", "_")
nombreArchivoFormateado = nombreArchivoFormateado.Replace("&", "_")
nombreArchivoFormateado = nombreArchivoFormateado.Replace("?", "_")
nombreArchivoFormateado = nombreArchivoFormateado.Replace("¿", "_")
nombreArchivoFormateado = nombreArchivoFormateado.Replace("¡", "_")
nombreArchivoFormateado = nombreArchivoFormateado.Replace("*", "_")
nombreArchivoFormateado = nombreArchivoFormateado.Replace("~", "_")
nombreArchivoFormateado = nombreArchivoFormateado.Replace("^", "_")
Return nombreArchivoFormateado
End Function
Protected Sub imgC_Click(sender As Object, e As EventArgs)
Dim Boton = CType(sender, LinkButton)
CveControl = Boton.CommandArgument.ToEntero
Dim GvRow As GridViewRow = DirectCast(Boton.NamingContainer, GridViewRow)
myModalLabel2.InnerText = $"Tabla de calibración de {GvRow.DataKey("Control")}"
Dim GPS As New ClsGPS
If GPS.CalibracionGet(0, CveControl) Then
GvCalibracion.DataSource = GPS.DtResultados
End If
GPS.Dispose()
GvCalibracion.DataBind()
EsEditar = 1
UPCalibracion.Update()
RegistraScript("$('#dvConsulta').modal()")
End Sub
Private Sub LbAgregarTemp_Click(sender As Object, e As EventArgs) Handles LbTemp.Click
EsEditar = 0
LbAgregarTemp.Text = "Agregar"
TxtControlTemperatura.Text = ""
TxtCapacidadTemp.Text = ""
TxtIDSensor.Text = ""
DdlEquipoTemp.SelectedValueExist("-1")
DdlUMTemp.SelectedValueExist("-1")
UPTempBody.Update()
UpFooter.Update()
RegistraScript("$('#dvAddTemp').modal()")
End Sub
Private Sub LbAgregarPasajeros_Click(sender As Object, e As EventArgs) Handles LnkPasajeros.Click
EsEditar = 0
BtnAgregarCatalogo.Text = "Agregar"
TxtControl.Text = ""
TxtCapacidad.Text = ""
TxtLargo.Text = ""
TxtAncho.Text = ""
TxtAlto.Text = ""
TxtLongitud.Text = ""
TxtSensor.Text = ""
TxtCalibración.Text = "[]"
DdlEquipo.SelectedValueExist("-1")
DdlUnidadMedida.SelectedValueExist("-1")
DivTanque.Visible = False
HTitulo.InnerText = "Guardar Sensor de Pasajeros"
UpAgregar.Update()
UpBodyCompuesto.Update()
UpFooter.Update()
RegistraScript("$('#divAgregar').modal()")
End Sub
Protected Sub LbDescargaFormato_Click(sender As Object, e As EventArgs)
Dim Directorio As String = ModExtensiones.DirectorioBaseEmpresas & "Empresas\Grales\FormatoCalibracion.xlsx"
Dim Archivo As New IO.FileInfo(Directorio)
If Archivo.Exists Then
Response.Clear()
Response.ClearHeaders()
Response.ClearContent()
Response.AddHeader("Content-Disposition", "attachment; filename=" + Archivo.Name)
Response.AddHeader("Content-Length", Archivo.Length.ToString())
Response.ContentType = "text/plain"
Response.Flush()
Response.TransmitFile(Archivo.FullName)
Response.End()
End If
End Sub
End Class