- VERSION 4.10.02

- Se agregó el almacen a todas las tablas necesarias y a los queries de envio de información.
This commit is contained in:
Jose Alberto Guerra Ugalde
2024-10-03 15:24:11 -06:00
parent e85f953e73
commit ead56c8758
16 changed files with 2900 additions and 358 deletions

View File

@@ -35,6 +35,7 @@ Sub Class_Globals
Private b_limpiarRuta As Button
Dim secuenciao As Int
Dim SECIENDA As String
Dim rutaGenerada As Boolean = False
End Sub
'You can add more parameters here.
@@ -84,7 +85,7 @@ Sub B4XPage_Appear
' Private t1 As Map
ListView1.Clear
Dim cs, cs2 As CSBuilder
entro = 3
' entro = 3
' Log("Generamos ListView1 en Activity_Resume")
For i=0 To c.RowCount -1 'Generamos el listView con la lista ordenada.
c.Position=i
@@ -92,7 +93,7 @@ Sub B4XPage_Appear
cs2.Initialize
' t1 = Starter.waypointsOrdered.Get(k)
' c.GetString("codigo")
Log(c.GetString("indice"))
' Log(c.GetString("indice"))
ListView1.AddTwoLines(cs.Color(Colors.RGB(100,149,237)).Append(c.GetString("codigo")).PopAll, cs2.append(c.GetString("CAT_CL_NOMBRE")).Color(Colors.RGB(100,149,237)).Append(" Calle: ").Pop.Append(c.GetString("CAT_CL_CALLE").Trim & " " & c.GetString("CAT_CL_NOEXT")).PopAll )
Next
Else
@@ -107,11 +108,12 @@ Sub B4XPage_Appear
Subs.centraEtiqueta(lfila, Root.Width)
b_getRutaInfo.Visible = True
b_getRutaInfo.BringToFront
laRuta = ""
End Sub
Sub ListView1_ItemClick (Position As Int, Value As Object)
' Log($"Entro= ${entro}"$)
Log($"Entro= ${entro}, Cliente: ${Value}"$)
ListView1.Clear
Sleep(50)
Subs.SetDivider(ListView1, Colors.LightGray, 2)
@@ -121,11 +123,16 @@ Sub ListView1_ItemClick (Position As Int, Value As Object)
ListView1.Top = lv1Top + 100
End If
l_rutaInfo.Visible = False
b_GetDirs.Visible = False
' b_GetDirs.Visible = False
If Not(Value.As(String).StartsWith("Ruta:")) Then 'Si el valor del clic no es la ruta, entonces es el cliente y lo mandamos a el.
entro = "3"
Log("Es cliente")
End If
If entro = "2" Then
b_GetDirs.Left = Root.Width - b_GetDirs.Width - 10dip
b_GetDirs.Visible = True
img_getDirs.Visible = True
b_getRutaInfo.Visible = False
' b_getRutaInfo.Visible = False
Private lrt As String
lrt = Value
laRuta = lrt.SubString(6) 'Quitamos el texto "Ruta: " para obtener el numero de la ruta.
@@ -147,9 +154,14 @@ Sub ListView1_ItemClick (Position As Int, Value As Object)
distOrderedMap.Initialize
If c2.RowCount>0 Then
For i=0 To c2.RowCount -1 'Generamos mapa de clientes
c2.Position=i
thisLoc.Latitude = c2.GetString("CAT_CL_LAT")
thisLoc.Longitude = c2.GetString("CAT_CL_LONG")
c2.Position = i
If c2.GetString("CAT_CL_LAT") = "null" Then
thisLoc.Latitude = 0
thisLoc.Longitude = 0
Else
thisLoc.Latitude = c2.GetString("CAT_CL_LAT")
thisLoc.Longitude = c2.GetString("CAT_CL_LONG")
End If
' Log(Tracker.UUGCoords)
Private distancia As Int = Tracker.UUGCoords.DistanceTo(thisLoc) 'Calculamos la distancia de la posicion ACTUAL a la tienda.
Private esteCliente As Map = CreateMap("distancia": distancia, "ubicacion": thisLoc.Longitude&","&thisLoc.Latitude, "codigo": c2.GetString("CAT_CL_CODIGO"), "nomDirDist": $"${c2.GetString("CAT_CL_NOMBRE")} CALLE: ${c2.GetString("CAT_CL_CALLE")} ${CRLF}Distancia: $1.1{(distancia/1000)} kms"$)
@@ -410,6 +422,7 @@ Sub traeRutaDia(aVisitar As B4XOrderedMap)
Private r As Int = 1
Private r1, wps As Map
Starter.skmt.ExecNonQuery("delete from waypoints")
Starter.skmt.ExecNonQuery("update kmt_info set secuencia = '150'") ' Ponemos la secuencia en 150 (que es el ultimo globito que tenemos) y luego en secuencia ponemos el orden segun el ruteo.
For Each k As Object In clientesMapaO.Keys 'Guardamos en la BD el orden de los waypoints para luego generar el listview.
r1 = clientesMapaO.Get(k)
r1.Get("codigo")
@@ -418,6 +431,18 @@ Sub traeRutaDia(aVisitar As B4XOrderedMap)
Starter.skmt.ExecNonQuery2("insert into waypoints values (?,?)", Array As Object(r1.Get("codigo"), wps.get("waypoint_index")))
r = r + 1
Next
Private no As Cursor = Starter.skmt.ExecQuery("select * from waypoints order by indice") ' Traemos el orden segun el ruteo dinamico.
If no.RowCount > 0 Then
Private nuevaSec As Int = 1
For i = 0 To no.RowCount - 1
no.Position = i
Starter.skmt.ExecNonQuery($"update kmt_info set secuencia = '${nuevaSec}' where cat_cl_codigo = '${no.Getstring("codigo")}'"$) ' Ponemos en la secuencia el orden segun el resultado del ruteo.
' Log($"${no.Getstring("codigo")},${no.getstring("indice")}, ${nuevaSec}"$)
nuevaSec = nuevaSec + 1
Next
End If
ListView1.Clear
Sleep(100)
Dim label2 As Label
@@ -430,7 +455,7 @@ Sub traeRutaDia(aVisitar As B4XOrderedMap)
Log("Generamos ListView1 en traeRutaDia")
'Traemos las visitas restantes ordenadas por el indice de waypoints (este indice nos indica el orden en la ruta calculada).
c=Starter.skmt.ExecQuery("select codigo, indice, CAT_CL_NOMBRE, CAT_CL_CALLE, CAT_CL_NOEXT from waypoints inner join kmt_info on waypoints.codigo = kmt_info.CAT_CL_CODIGO where gestion = 0 order by indice")
c=Starter.skmt.ExecQuery("select codigo, indice, CAT_CL_NOMBRE, CAT_CL_CALLE, CAT_CL_NOEXT from waypoints inner join kmt_info on waypoints.codigo = kmt_info.CAT_CL_CODIGO where gestion = 0 order by indice")
If c.RowCount > 0 Then
For i=0 To c.RowCount -1 'Generamos el listView con la lista ordenada.
secuenciao = i + 1
@@ -438,7 +463,7 @@ Sub traeRutaDia(aVisitar As B4XOrderedMap)
cs.Initialize
cs2.Initialize
ListView1.AddTwoLines(cs.Color(Colors.RGB(100,149,237)).Append(c.GetString("codigo")).PopAll, cs2.append(c.GetString("CAT_CL_NOMBRE")).Color(Colors.RGB(100,149,237)).Append(" Calle: ").Pop.Append(c.GetString("CAT_CL_CALLE").Trim & " " & c.GetString("CAT_CL_NOEXT")).PopAll )
Starter.skmt.ExecNonQuery2("UPDATE kmt_info SET SECUENCIA = ? where CAT_CL_NOMBRE = ? ", Array As String(secuenciao, c.GetString("CAT_CL_NOMBRE")))
' Starter.skmt.ExecNonQuery2("UPDATE kmt_info SET SECUENCIA = ? where CAT_CL_NOMBRE = ? ", Array As String(secuenciao, c.GetString("CAT_CL_NOMBRE")))
Next
End If
c.Close
@@ -550,7 +575,8 @@ End Sub
'Traemos la ruta de visitas via el API de OSRM usando el sub "traeRutaDia(traeTodosAVisitar)".
Private Sub b_getRutaInfo_Click
traeRutaDia(traeTodosAVisitar)
' traeRutaDia(traeTodosAVisitar)
caculaRutaGPS(todosAVisitar)
End Sub
'Mostramos u ocultamos el boton para borrar los waypoints de la ruta.
@@ -567,4 +593,128 @@ Private Sub b_limpiarRuta_Click
Starter.skmt.ExecNonQuery("delete from waypoints")
b_limpiarRuta.Visible = False
B4XPage_Appear
End Sub
End Sub
Sub caculaRutaGPS(coords As List)
Log("caculaRutaGPS")
Private tmpList As List
Dim cs, cs2 As CSBuilder
tmpList.Initialize
tmpList.AddAll(Array As String("ACTUAL", Tracker.UUGCoords.Longitude, Tracker.UUGCoords.Latitude)) ' Ponemos la ubicacion actual en tmpList.
Log($"STARTER.INICIOMAPA = ${Starter.iniciomapa}"$)
If Starter.inicioMapa Then
tmpList.Initialize
tmpList.AddAll(Array As String("ACTUAL", Starter.inicioLon, Starter.inicioLat)) ' Ponemos la ubicacion de la tienda seleccionada en tmpList.
Starter.inicioMapa = False
Starter.inicioLon = 0
Starter.inicioLat = 0
End If
Log($">>>> TMPLIST = ${tmpList}"$)
coords.InsertAt(0,tmpList) ' Agregamos tmpList (ubicacion actual) al principio de la lista de coordenadas, para que ese sea el inicio de la ruta.
Log("=================================")
Log(coords)
Private coordsStr As String = ""
For Each tienda As List In coords
' Log(tienda)
If coordsStr = "" Then
coordsStr = $"${tienda.get(0)},${tienda.get(1)},${tienda.get(2)}"$
Else
coordsStr = $"${coordsStr};${tienda.get(0)},${tienda.get(1)},${tienda.get(2)}"$
End If
Next
Log(coordsStr)
ProgressDialogShow2("Calculando distancia y tiempo, un momento por favor.", False)
Dim j As HttpJob
j.Initialize("trip", Me) 'Calculamos el resto de la ruta.
j.Download($"http://keymon.lat:9002/ruteador?algoritmo=${Subs.traeAlgoritmoRuteo}&m=${Subs.traeMatrizRuteo}&r=${Subs.traeRutaReparto}&a=${Subs.traeAlmacen}&f=CEDIS,${Starter.cedisLocation.Longitude},${Starter.cedisLocation.Latitude}&c=${coordsStr}"$)
Log($"http://keymon.lat:9002/ruteador?algoritmo=${Subs.traeAlgoritmoRuteo}&m=${Subs.traeMatrizRuteo}&r=${Subs.traeRutaReparto}&a=${Subs.traeAlmacen}&f=CEDIS,${Starter.cedisLocation.Longitude},${Starter.cedisLocation.Latitude}&c=${coordsStr}"$)
Wait For (j) JobDone(j As HttpJob)
If j.Success Then
Dim jp As JSONParser
jp.Initialize(j.GetString)
Dim m As Map = jp.NextObject
Log(m)
If m.Get("code") = "OK" Then
Log("===== OK")
rutaGenerada = True
Dim puntos As Int = m.Get("puntos")
Private coords As List = m.Get("coords")
Private duration As String = m.Get("duration")
Private distance As String = m.Get("distance")
Private mensaje As String = m.Get("mensaje")
Starter.skmt.ExecNonQuery("delete from waypoints")
Starter.skmt.ExecNonQuery("update kmt_info set SECUENCIA = 150")
' Log(">>> " & coords.Size)
For i = 0 To coords.Size - 1
Private ti As Map = coords.Get(i)
' Starter.skmt.ExecNonQuery($"update kmt_info set SECUENCIA = ${ti.Get("pos")} where CAT_CL_CODIGO = '${ti.Get("id").As(String).SubString(1)}'"$) ' Ponemos la secuencia con el orden del ruteo para poder verla en el mapa.
Starter.skmt.ExecNonQuery($"insert into waypoints (codigo, indice) values ('${ti.Get("id").As(String).SubString(1)}', '${ti.Get("pos")}')"$)
Next
Private no As Cursor = Starter.skmt.ExecQuery("select * from waypoints order by indice") ' Traemos el orden segun el ruteo dinamico.
Log("ROWCOUNT: " & no.RowCount)
If no.RowCount > 0 Then
Private nuevaSec As Int = 1
For i = 0 To no.RowCount - 1
no.Position = i
Starter.skmt.ExecNonQuery($"update kmt_info set secuencia = '${nuevaSec}' where cat_cl_codigo = '${no.Getstring("codigo")}'"$) ' Ponemos en la secuencia el orden segun el resultado del ruteo.
Log($"${no.Getstring("codigo")},${no.getstring("indice")}, ${nuevaSec}"$)
nuevaSec = nuevaSec + 1
Next
End If
c = Starter.skmt.ExecQuery("select codigo, indice, CAT_CL_NOMBRE, CAT_CL_CALLE, CAT_CL_NOEXT from waypoints inner join kmt_info on waypoints.codigo = kmt_info.CAT_CL_CODIGO where gestion = 0 order by indice")
If c.RowCount > 0 Then 'Ya hay waypoints en la base de datos.
c.Position = 0
conMapa = True
ListView1.Clear
Dim cs, cs2 As CSBuilder
entro = 3
For i=0 To c.RowCount -1 'Generamos el listView con la lista ordenada.
c.Position = i
cs.Initialize
cs2.Initialize
ListView1.AddTwoLines(cs.Color(Colors.RGB(100,149,237)).Append(c.GetString("codigo")).PopAll, cs2.append(c.GetString("CAT_CL_NOMBRE")).Color(Colors.RGB(100,149,237)).Append(" Calle: ").Pop.Append(c.GetString("CAT_CL_CALLE").Trim & " " & c.GetString("CAT_CL_NOEXT")).PopAll )
Next
End If
c.Close
Log($"Distancia total: $1.1{distance/1000} kms, tiempo aprox: $1.1{duration/60} mins. ($1.1{duration/60/60} hrs)"$)
l_rutaInfo.Text = $"Distancia: $1.1{distance/1000} kms, tiempo aprox: $1.1{duration/60/60} hrs${CRLF}Visitas restantes: ${puntos - 1}"$
l_rutaInfo.Width = Root.Width * 0.9
Subs.centraEtiqueta(l_rutaInfo, Root.Width)
l_rutaInfo.Visible = True
l_rutaInfo.BringToFront
ListView1.Top = lv1Top + 100
End If
Else
LogColor("**************** Error! ******************", Colors.red)
End If
j.Release
ProgressDialogHide
End Sub
'Regresa lista de los clientes a visitar y sus coordenadas.
Sub todosAVisitar As List
Log("|" & laRuta &"|")
Log(entro)
Private sqlDeRuta As String = ""
' If entro = 3 Then sqlDeRuta = $"and CAT_CL_RUTA = '${laRuta}'"$
If laRuta <> "" Then sqlDeRuta = $"and CAT_CL_RUTA = '${laRuta}'"$
Private aVisitar, tmpList As List
aVisitar.Initialize
Private coords As ResultSet = Starter.skmt.ExecQuery($"select distinct CAT_CL_CODIGO, CAT_CL_LAT, CAT_CL_LONG from kmt_info where gestion = 0 ${sqlDeRuta}"$)
Log("ROWCOUNT 2: " & coords.RowCount)
Do While coords.NextRow
tmpList.Initialize
tmpList.AddAll(Array As String(coords.GetString("CAT_CL_CODIGO"), coords.GetString("CAT_CL_LONG"), coords.GetString("CAT_CL_LAT")))
aVisitar.Add(tmpList)
Loop
Log(aVisitar)
Return aVisitar
End Sub
Private Sub b_GetDirs_longClick
caculaRutaGPS(todosAVisitar)
End Sub