Option Strict On Imports Extensiones Imports System.Data Partial Class GPS_RepGeoReferencias Inherits ClsPaginas Public Property CveGeoReferencia As Integer Get Return ViewState("CveGeoReferencia").ToString.ToEntero End Get Set(value As Integer) ViewState("CveGeoReferencia") = value End Set End Property Private Property DtPosiciones As DataTable Get Return CType(ViewState("DtPosiciones"), DataTable) End Get Set(value As DataTable) ViewState("DtPosiciones") = value End Set End Property Public Property DtDetalle() As Data.DataTable Get Return DirectCast(ViewState("DtDetalle"), DataTable) End Get Set(ByVal value As Data.DataTable) ViewState("DtDetalle") = value End Set End Property Public Property DtDetalleEliminar() As Data.DataTable Get Return DirectCast(ViewState("DtDetalleEliminar"), DataTable) End Get Set(ByVal value As Data.DataTable) ViewState("DtDetalleEliminar") = value End Set End Property Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load If Not Page.IsPostBack Then CargaGeoReferencias() CargaDispositivos() CreaTmpDTDetalle() CargaGrupos() ' RegistraScript("$('#dvConsulta').modal()") End If End Sub Private Sub CargaGeoReferencias() Dim Estatus = If(ChkEstatusGeocerca.Checked, 1, 0) Dim GPS As New ClsGPS If GPS.GeoReferenciaGet(-1, Estatus, "-1", UsuarioActual.CveCliente) Then GVGeoreferencias.DataSource = GPS.DtResultados Else GVGeoreferencias.DataSource = Nothing End If GPS.Dispose() GVGeoreferencias.DataBind() End Sub Private Sub CargaDispositivos() DdlEquipoD.DataSource = GlobalDTDispositivos(UsuarioActual.CveCliente) DdlEquipoD.DataBind() DdlEquipoD.Items.Insert(0, New ListItem("Seleccione", "-1")) GvDispositivos.DataSource = GlobalDTDispositivosUsuarios(UsuarioActual.CveCliente, UsuarioActual.CveUsuario).FiltraParametros("Estatus = 1") GvDispositivos.DataBind() End Sub Private Sub CargaGrupos() Using ObjGPS As New ClsGPS If ObjGPS.GruposGet(-1, UsuarioActual.CveCliente) Then DdlGrupo.DataSource = ObjGPS.DtResultados DdlGrupo.DataBind() End If End Using DdlGrupo.Items.Insert(0, New ListItem("Seleccione", "-1")) End Sub Protected Sub CreaTmpDTDetalle() DtDetalle = New DataTable Dim pk = DtDetalle.Columns.Add("CveDispositivo", GetType(Integer)) DtDetalle.Columns.Add("NoEconomico", GetType(String)) DtDetalle.Columns.Add("CveDispositivoGeoReferenciaDetalle", GetType(Integer)) DtDetalle.PrimaryKey = New DataColumn() {pk} GvEquiposDetalle.DataSource = DtDetalle GvEquiposDetalle.DataBind() UpAgregar.Update() End Sub Protected Sub CreaTmpDTDetalleEliminar() DtDetalleEliminar = New DataTable Dim pk2 = DtDetalleEliminar.Columns.Add("CveDispositivo", GetType(Integer)) DtDetalleEliminar.Columns.Add("NoEconomico", GetType(String)) Dim pk3 = DtDetalleEliminar.Columns.Add("CveDispositivoGeoReferenciaDetalle", GetType(Integer)) DtDetalleEliminar.PrimaryKey = New DataColumn() {pk2, pk3} UpAgregar.Update() End Sub Protected Sub GvGeoreferencias_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GVGeoreferencias.RowDataBound If e.Row.RowType = DataControlRowType.DataRow Then e.Row.FCLinkButton("imgColor").Style.Add("background-color", e.Row.DBCampo("Color").ToString) e.Row.FCLinkButton("imgE").CommandArgument = e.Row.DBCampo("CveGeoReferencia").ToString If e.Row.DBCampo("Estatus").ToString = "True" Then e.Row.FCLabel("LblEstatus").Text = "Activo" e.Row.FCLabel("LblEstatus").ForeColor = Drawing.Color.Green Else e.Row.FCLabel("LblEstatus").Text = "Inactivo" e.Row.FCLabel("LblEstatus").ForeColor = Drawing.Color.Red End If End If End Sub Protected Sub BtnConsultar_Click(sender As Object, e As EventArgs) Handles BtnConsultar.Click Dim Coord As String = "" Dim count = 0 CreaDTPosiciones() For Each row As GridViewRow In GVGeoreferencias.Rows If row.FCCheckBox("chkGR").Checked Then If row.DataKey("Tipo") = "Geocerca" Then Coord = SQLToGmaps(row.DataKey("Coordenadas").ToString, "POLYGON") RegistraScript($"creaPoligono({Coord}, '{row.DataKey("Color").ToString}', '{row.DataKey("GeoReferencia")}')", count.ToString()) AddRowPosiciones(row.DataKey("GeoReferencia").ToString, Coord) ElseIf row.DataKey("Tipo") = "Ruta" Then Coord = SQLToGmaps(row.DataKey("Coordenadas").ToString, "LINESTRING") RegistraScript($"creaPolyline({Coord}, '{row.DataKey("Color").ToString}', '{row.DataKey("GeoReferencia")}')", count.ToString()) AddRowPosiciones(row.DataKey("GeoReferencia").ToString, Coord) End If End If count += 1 Next If chkUp.Checked Then Dim gps As New ClsGPS Dim Fecha, Vehiculo, Icono, CoordDis, Info As String For Each GVRow As GridViewRow In GvDispositivos.Rows If GVRow.FCCheckBox("chkDis").Checked Then If gps.UltimaPosicionEquipoGet(GVRow.DataKey("CveDispositivo").ToEntero, MinFecha, MaxFecha) Then If gps.DtResultados.Rows.Count > 0 Then CoordDis = gps.DtResultados.Rows(0)("Posicion").ToString.SQLToGmaps("POINT") Fecha = gps.DtResultados.Rows(0)("FechaTrama").ToString Vehiculo = gps.DtResultados.Rows(0)("Vehiculo").ToString Info = InfoWindow($"Vehículo:{Vehiculo}", "-1", GVRow.DataKey("CveDispositivo").ToString.ToEntero, gps.DtResultados.Rows(0)) If gps.DtResultados.Rows(0)("SPD").ToString.ToDouble > 1 Then Icono = $"/Imagenes/{GlobalDTDispositivos(UsuarioActual.CveCliente).FiltroPrimeroValor("Icono", $"CveDispositivo = {GVRow.DataKey("CveDispositivo").ToString}")}green.png" Else Icono = $"/Imagenes/{GlobalDTDispositivos(UsuarioActual.CveCliente).FiltroPrimeroValor("Icono", $"CveDispositivo = {GVRow.DataKey("CveDispositivo").ToString}")}.png" End If RegistraScript($"CreaMarcador({CoordDis},'{GVRow.DataKey("NombreVehiculo")}', '{Info}', '{Icono}');", $"{GVRow.DataKey("CveDispositivo").ToString}D") Else Alert($"No se encontraron resultados del vehículo con serie: {GVRow.DataKey("SerieVehiculo").ToString}", TipoMensaje.MsgAdvertencia) End If End If End If Next gps.Dispose() End If chkUp.Checked = False UpUp.Update() UPModalBusqueda.Update() GvBarra.DataSource = DtPosiciones GvBarra.DataBind() UPGV.Update() RegistraScript($"FitBounds();", "XBounds") End Sub Protected Sub imgE_Click(sender As Object, e As EventArgs) Dim Boton = CType(sender, LinkButton) CveGeoReferencia = Boton.CommandArgument.ToEntero DtDetalle = Nothing DtDetalleEliminar = Nothing Try Dim GvRow As GridViewRow = DirectCast(Boton.NamingContainer, GridViewRow) TxtNombre.Text = GvRow.DataKey("GeoReferencia").ToString TxtTolerancia.Text = GvRow.DataKey("Tolerancia").ToString 'DdlEquipo.SelectedValueExist(GvRow.DataKey("CveDispositivo").ToString) txtFechaI.Text = GvRow.DataKey("FechaInicio").ToString.ToDate.ToString("dd/MM/yyyy") txtFechaF.Text = GvRow.DataKey("FechaFin").ToString.ToDate.ToString("dd/MM/yyyy") chkLunes.Checked = CBool(GvRow.DataKey("AplicaLunes").ToString) chkMartes.Checked = CBool(GvRow.DataKey("AplicaMartes").ToString) chkMiercoles.Checked = CBool(GvRow.DataKey("AplicaMiercoles").ToString) chkJueves.Checked = CBool(GvRow.DataKey("AplicaJueves").ToString) chkViernes.Checked = CBool(GvRow.DataKey("AplicaViernes").ToString) chkSabado.Checked = CBool(GvRow.DataKey("AplicaSabado").ToString) chkDomingo.Checked = CBool(GvRow.DataKey("AplicaDomingo").ToString) chkEstatus.Checked = CBool(GvRow.DataKey("Estatus").ToString) TxtHoraI.Text = GvRow.DataKey("HoraInicio").ToString.ToDateTime.ToString("HH:mm") TxtHoraF.Text = GvRow.DataKey("HoraFin").ToString.ToDateTime.ToString("HH:mm") Using ObjGPS As New ClsGPS If ObjGPS.GetReferenciaDetalle(-1, CveGeoReferencia, -1, UsuarioActual.CveCliente) Then If ObjGPS.DtResultados.Rows.Count > 0 Then DtDetalle = ObjGPS.DtResultados GvEquiposDetalle.DataSource = DtDetalle Else DtDetalle = Nothing DtDetalleEliminar = Nothing GvEquiposDetalle.DataSource = Nothing End If End If End Using GvEquiposDetalle.DataBind() Catch ex As Exception OnError("Ocurrió un error al cargar la GeoReferencia.", ex) End Try RegistraScript("CargaModal();") UpBodyGeoReferencia.Update() End Sub Private Function SQLToGmaps(CoordsSQL As String, Tipo As String) As String Dim CoordsPoligono As String = CoordsSQL.Replace($"{Tipo}", "").Replace(")", "").Replace("(", "") Dim CoordsPuntos() As String = CoordsPoligono.Split(","c) Dim Var As String = If(Tipo = "POINT", "", "[") For Each coordenada As String In CoordsPuntos If coordenada Is CoordsPuntos.Last Then Dim latlng() As String = coordenada.Trim.Split(" "c) Var = $"{Var}{{lat: {latlng(1)}, lng: {latlng(0)} }}" Else Dim latlng() As String = coordenada.Trim.Split(" "c) Var = $"{Var}{{lat: {latlng(1)}, lng: {latlng(0)} }}," End If Next Var = If(Tipo = "POINT", Var, $"{Var}]") Return Var End Function Protected Sub BtnActualizarGeoreferencia_Click(sender As Object, e As EventArgs) If DtDetalle IsNot Nothing AndAlso DtDetalle.Rows.Count > 0 Then Dim ObjGPS As New ClsGPS ObjGPS.IniciaTransacSQL() Dim TieneError As Boolean = False Try Dim HoraI As Date = ($"{Date.Now.ToString("dd/MM/yyyy")} {TxtHoraI.Text}").ToDate Dim HoraF As Date = ($"{Date.Now.ToString("dd/MM/yyyy")} {TxtHoraF.Text}").ToDate If ObjGPS.GeoReferenciaUpdate(CveGeoReferencia, TxtNombre.Text, -1, chkEstatus.Checked, txtFechaI.Text.ToDate, txtFechaF.Text.ToDate, chkLunes.Checked, chkMartes.Checked, chkMiercoles.Checked, chkJueves.Checked, chkViernes.Checked, chkSabado.Checked, chkDomingo.Checked, HoraI, HoraF, TxtTolerancia.Text.ToEntero, UsuarioActual.CveCliente) Then For Each Dr As DataRow In DtDetalle.Rows Dim CveGeoReferenciaDetalle = Dr("CveDispositivoGeoreferenciaDetalle").ToString.ToEntero If CveGeoReferenciaDetalle > 0 Then If Not ObjGPS.UpdateReferenciaDetalle(CveGeoReferenciaDetalle, CveGeoReferencia, Dr("CveDispositivo").ToString.ToEntero) Then TieneError = True Exit For End If Else If Not ObjGPS.AddGeoReferenciaDetalle(CveGeoReferencia, Dr("CveDispositivo").ToString.ToEntero) Then TieneError = True Exit For End If End If Next If Not TieneError Then For Each DrE As DataRow In DtDetalleEliminar.Rows TieneError = Not ObjGPS.RemoveferenciaDetalle(DrE("CveDispositivoGeoreferenciaDetalle").ToString.ToEntero) Next End If Else TieneError = True End If Catch ex As Exception GuardaLog(ex.ToString()) Finally If TieneError Then ObjGPS.RollBack() Alert("Ocurrió un error al guardar la georeferencia.", TipoMensaje.MsgError) Else ObjGPS.Commit() Alert("La georeferencia se actualizó correctamente.", TipoMensaje.MsgInformacion) End If ObjGPS.Dispose() End Try Else Alert("No hay vehiculos asignados a esta georeferencia", TipoMensaje.MsgAdvertencia) End If End Sub Protected Sub gvMarcas_RowDeleting(sender As Object, e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles GVGeoreferencias.RowDeleting Dim GPS As New ClsGPS Dim CveGeoReferencia As Integer = GVGeoreferencias.Rows(e.RowIndex).DataKey("CveGeoReferencia").ToEntero If GPS.GeoReferenciaDelete(CveGeoReferencia, UsuarioActual.CveCliente) Then Alert("La GeoReferencia se eliminó correctamente", TipoMensaje.MsgInformacion) End If GPS.Dispose() CargaGeoReferencias() RegistraScript("$('#divAgregarTipo').modal('hide')") End Sub #Region "DATA TABLES" Private Sub CreaDTPosiciones() ViewState("DtPosiciones") = Nothing Dim dt As New DataTable dt.Columns.Add("Nombre", Type.GetType("System.String")) dt.Columns.Add("Posicion", Type.GetType("System.String")) ViewState("DtPosiciones") = dt End Sub Private Sub AddRowPosiciones(Nombres As String, Posicion As String) Dim dr As DataRow = DtPosiciones.NewRow() dr("Nombre") = Nombres dr("Posicion") = Posicion DtPosiciones.Rows.Add(dr) End Sub Protected Sub GvBarra_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GvBarra.RowDataBound If e.Row.RowType = DataControlRowType.DataRow Then e.Row.Attributes.Add("onClick", $" CenterPoligono({e.Row.DBCampo("Posicion")});") End If End Sub #End Region 'Protected Sub LnkAgregarDisp_Click(sender As Object, e As EventArgs) 'End Sub 'Protected Sub LnkAgregarGrupo_Click(sender As Object, e As EventArgs) 'End Sub 'Protected Sub GvEquiposDetalle_RowDeleting(sender As Object, e As GridViewDeleteEventArgs) 'End Sub Protected Sub GvEquiposDetalle_RowDeleting(sender As Object, e As GridViewDeleteEventArgs) If DtDetalleEliminar Is Nothing Then CreaTmpDTDetalleEliminar() End If If DtDetalle.Rows.Count > 0 Then Dim Dr As DataRow = DtDetalle.Rows(e.RowIndex) Dim CveGeoReferenciaDetalle = Dr("CveDispositivoGeoreferenciaDetalle").ToString.ToEntero If CveGeoReferenciaDetalle > 0 Then Dim DrEliminar = DtDetalleEliminar.NewRow DrEliminar("CveDispositivoGeoreferenciaDetalle") = CveGeoReferenciaDetalle DrEliminar("CveDispositivo") = Dr("CveDispositivo").ToString.ToEntero DrEliminar("NoEconomico") = Dr("NoEconomico").ToString DtDetalleEliminar.Rows.Add(DrEliminar) DtDetalleEliminar.AcceptChanges() End If DtDetalle.Rows.RemoveAt(e.RowIndex) GvEquiposDetalle.DataSource = DtDetalle GvEquiposDetalle.DataBind() End If UpAgregar.Update() End Sub Protected Sub LnkAgregarDisp_Click(sender As Object, e As EventArgs) Dim CveDispositivo As Integer = DdlEquipoD.SelectedValue.ToEntero If CveDispositivo > 0 Then Dim NoEconomico As String = DdlEquipoD.SelectedItem.Text If DtDetalle IsNot Nothing Then If Not DtDetalle.Select($"CveDispositivo={CveDispositivo}").Any() Then Dim Dr As DataRow = DtDetalle.NewRow Dr("CveDispositivo") = CveDispositivo Dr("NoEconomico") = NoEconomico Dr("CveDispositivoGeoReferenciaDetalle") = -1 DtDetalle.Rows.Add(Dr) DtDetalle.AcceptChanges() Else Alert($"El dispositivo '{NoEconomico} ya existe en la tabla.'", TipoMensaje.MsgAdvertencia) End If Else CreaTmpDTDetalle() CreaTmpDTDetalleEliminar() Dim Dr As DataRow = DtDetalle.NewRow Dr("CveDispositivo") = CveDispositivo Dr("NoEconomico") = NoEconomico Dr("CveDispositivoGeoReferenciaDetalle") = -1 DtDetalle.Rows.Add(Dr) DtDetalle.AcceptChanges() End If GvEquiposDetalle.DataSource = DtDetalle GvEquiposDetalle.DataBind() End If UpAgregar.Update() CargaGeoReferencias() RegistraScript("CargaPickersHora();", "1") End Sub Protected Sub LnkAgregarGrupo_Click(sender As Object, e As EventArgs) Dim CveGrupo As Integer = DdlGrupo.SelectedValue.ToEntero Using ObjGPS As New ClsGPS If ObjGPS.GruposDetallesGet(CveGrupo) Then Dim Query = From dr In ObjGPS.DtResultados Select CveDispositivo = dr("CveDispositivo").ToString.ToEntero, NoEconomico = dr("NoEconomico").ToString, CveDispositivoGeoReferenciaDetalle = -1 Dim dt As DataTable = Query.QueryToDataTable(True) dt.PrimaryKey = New DataColumn() {dt.Columns(0)} DtDetalle.Merge(dt, True, MissingSchemaAction.Ignore) End If End Using GvEquiposDetalle.DataSource = DtDetalle GvEquiposDetalle.DataBind() UpAgregar.Update() RegistraScript("CargaPickersHora();", "1") End Sub Protected Sub ChkEstatusGeocerca_CheckedChanged(sender As Object, e As EventArgs) CargaGeoReferencias() End Sub End Class