Files
Guna_Carta_Porte/B4A/Subs.bas
cvaldes1201 970144b626 ...
2024-07-22 16:28:01 -06:00

307 lines
10 KiB
QBasic

B4A=true
Group=Default Group
ModulesStructureVersion=1
Type=StaticCode
Version=11
@EndOfDesignText@
'Code module
'Subs in this code module will be accessible from all modules.
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
' Public GZip As GZipStrings 'Usa la libreria CompressStrings
Dim devModel As String
Dim errorLog As SQL 'Requiere la libreria "SQL"
' Dim wifi As MLwifi
Dim ssid As String 'ignore
Dim rutaMaxPoints As Int = 3000
Dim rutaHrsAtras As Int = 48
' Dim rutaInicioHoy As String = ""
End Sub
'Convierte una fecha al formato yyMMddHHmmss
Sub fechaKMT(fecha As String) As String 'ignore
' if starter.logger then Log(fecha)
Dim OrigFormat As String = DateTime.DateFormat 'save orig date format
DateTime.DateFormat="yyMMddHHmmss"
Dim nuevaFecha As String=DateTime.Date(fecha)
DateTime.DateFormat = OrigFormat 'return to orig date format
' if starter.logger then Log(nuevaFecha)
Return nuevaFecha
End Sub
'Genera una notificacion con importancia alta
Sub notiHigh(title As String, body As String, activity As Object) 'ignore
Private notif As Notification
notif.Initialize2(notif.IMPORTANCE_HIGH)
notif.Icon = "icon"
notif.Vibrate = False
notif.Sound = False
notif.AutoCancel = True
Log("notiHigh: "&title)
notif.SetInfo(title, body, activity)
' if starter.logger then Log("notiHigh SetInfo")
notif.Notify(777)
End Sub
'Regresa el objeto de una notificacion con importancia baja
Sub notiLowReturn(title As String, Body As String, id As Int) As Notification 'ignore
Private notification As Notification
notification.Initialize2(notification.IMPORTANCE_LOW)
' If Starter.logger Then Log("notiLowReturn: "&title)
notification.Icon = "icon"
notification.Sound = False
notification.Vibrate = False
notification.SetInfo(title, Body, Main)
notification.Notify(id)
' if starter.logger then Log("notiLowReturn SetInfo")
Return notification
End Sub
'Regresa la fecha y hora de hoy a las 00:00 en el formato "yyMMddHHMMSS"
Sub fechaInicioHoy As String 'ignore
Dim OrigFormat As String = DateTime.DateFormat 'save orig date format
DateTime.DateFormat = "yyMMdd"
Private h As String = DateTime.Date(DateTime.Now)&"000000"
DateTime.DateFormat = OrigFormat 'return to orig date format
' If Starter.logger Then Log("Hoy="&h)
Return h
End Sub
'Regresa verdadero si ya pasaron XX minutos de la fecha dada
Sub masDeXXMins(hora As Int, mins As Int) As Boolean 'ignore
If (hora + mins * DateTime.TicksPerMinute) < DateTime.Now Then
Return True
Else
Return False
End If
End Sub
'Regresa verdadero si ya pasaron XX minutos de la fechaKMT dada
Sub masDeXXMinsKMT(hora As String, mins As Int) As Boolean 'ignore
Try
' if starter.logger then LogColor($"Hora=${fechaKMT(fechaKMT2Ticks(hora) + mins * DateTime.TicksPerMinute)}, Mins=${mins}, Actual=${fechaKMT(DateTime.Now)}"$,Colors.red)
If fechaKMT2Ticks(hora) + mins * DateTime.TicksPerMinute < DateTime.Now Then
' if starter.logger then Log("+++ +++ "&fechaKMT(fechaKMT2Ticks(hora) + mins * DateTime.TicksPerMinute) & " < " & fechaKMT(DateTime.Now))
Return True
Else
' if starter.logger then Log("+++ +++ "&fechaKMT(fechaKMT2Ticks(hora) + mins * DateTime.TicksPerMinute) & " > " & fechaKMT(DateTime.Now))
Return False
End If
Catch
Log(LastException)
End Try
End Sub
'Convierte una fecha en formato YYMMDDHHMMSS a Ticks
Sub fechaKMT2Ticks(fKMT As String) As Long 'ignore
Try
If fKMT.Length = 12 Then
Private parteFecha As String = fKMT.SubString2(0,6)
Private parteHora As String = fKMT.SubString(6)
Private OrigFormat As String = DateTime.DateFormat 'save original date format
DateTime.DateFormat="yyMMdd"
DateTime.TimeFormat="HHmmss"
Private ticks As Long = DateTime.DateTimeParse(parteFecha,parteHora)
' Log(" +++ +++ pFecha:"&parteFecha&" | pHora:"&parteHora)
DateTime.DateFormat = OrigFormat 'return to original date format
Return ticks
Else
Log("Formato de fecha incorrecto, debe de ser 'yyMMddHHmmss', no '"&fKMT&"' largo="&fKMT.Length)
Return 0
End If
Catch
Log(LastException)
' If Starter.logger Then LogColor($"Fecha dada: ${fKMT}, Parte Fecha: ${parteFecha}, Parte Hora: ${parteHora}"$, Colors.Red)
Return 0
End Try
End Sub
'Hace visible el panel con los parametros "Top" y "Left" dados
Sub panelVisible(panel As Panel, top As Int, left As Int) 'ignore
panel.BringToFront
panel.Visible = True
panel.Top = top
panel.Left = left
End Sub
'Hace visible el panel usando toda la pantalla
Sub panelVisibleCompleto(panel As Panel, a As Activity) 'ignore
panel.BringToFront
panel.Visible = True
panel.Top = 0
panel.Left = 0
panel.Width = a.Width
panel.Height = a.Height
End Sub
'Oculta el panel especificado y lo manda al fondo
Sub panelOculto(panel As Panel) 'ignore
panel.SendToBack
panel.Visible = False
End Sub
'Centra una etiqueta dentro de un elemento superior
Sub centraEtiqueta(elemento As Label, anchoElementoSuperior As Int) 'ignore
elemento.Left = Round(anchoElementoSuperior/2)-(elemento.Width/2)
End Sub
'Centra un panel dentro de un elemento superior
Sub centraPanel(elemento As Panel, anchoElementoSuperior As Int) 'ignore
elemento.Left = Round(anchoElementoSuperior/2)-(elemento.Width/2)
End Sub
Sub centraEditText(elemento As EditText, anchoElementoSuperior As Int) 'ignore
elemento.Left = Round(anchoElementoSuperior/2)-(elemento.Width/2)
End Sub
'Centra una barra de progreso dentro de un elemento superior
Sub centraProgressBar(elemento As ProgressBar, anchoElementoSuperior As Int) 'ignore
elemento.Left = Round(anchoElementoSuperior/2)-(elemento.Width/2)
End Sub
'Modifica el ancho y alto de un panel dado con el ancho y alto proporcionados y los pone en top 0 y left 0.
Sub panelAnchoAlto(p As Panel, w As Int, h As Int)
' If Starter.logger Then Log($"panel:${p}, alncho=${w}, alto=${h}"$)
p.Top = 0
p.Left = 0
p.Width = w
p.Height = h
End Sub
'Regresa el dia de HOY como string, en español y mayusculas.
Sub dameDiaSemana As String
Private ds As String = DateTime.GetDayOfWeek(DateTime.Now)
If ds = "1" Then
ds = "DOMINGO"
else if ds = "2" Then
ds = "LUNES"
else if ds = "3" Then
ds = "MARTES"
else if ds = "4" Then
ds = "MIERCOLES"
else if ds = "5" Then
ds = "JUEVES"
else if ds = "6" Then
ds = "VIERNES"
Else
ds = "SABADO"
End If
Return ds
End Sub
'Regresa el almacen actual de la base de datos.
'Sub traeAlmacen As String 'ignore
' Private c As Cursor
' Private a As String
' c = Starter.skmt.ExecQuery("select ID_ALMACEN from CAT_ALMACEN")
' c.Position = 0
' a = C.GetString("ID_ALMACEN")
' c.Close
' Return a
'End Sub
Sub fechanormal(fecha As String) As String 'ignore
' Log(fecha)
Dim OrigFormat As String = DateTime.DateFormat 'save orig date format
DateTime.DateFormat = "YYYY/MM/dd HH:mm:ss"
Dim nuevaFecha As String=DateTime.Date(fecha)
DateTime.DateFormat = OrigFormat 'return to orig date format
' Log(nuevaFecha)
Return nuevaFecha
End Sub
Sub horaNormal(fecha As String) As String 'ignore
' Log(fecha)
Dim OrigFormat As String = DateTime.DateFormat 'save orig date format
DateTime.TimeFormat = "HH:mm"
Dim nuevaFecha As String=DateTime.time(fecha)
DateTime.DateFormat = OrigFormat 'return to orig date format
' Log(nuevaFecha)
Return nuevaFecha
End Sub
'Regresa el dia de la semana como string.
Sub traeDiaSemana As String
Private hoyNum As Int = DateTime.GetDayOfWeek(DateTime.Now)
Private diaSemana As String = ""
Select Case hoyNum
Case 1
diaSemana = "DOMINGO"
Case 2
diaSemana = "LUNES"
Case 3
diaSemana = "MARTES"
Case 4
diaSemana = "MIERCOLES"
Case 5
diaSemana = "JUEVES"
Case 6
diaSemana = "VIERNES"
Case 7
diaSemana = "SABADO"
End Select
Return diaSemana
End Sub
'Guarda el nombre y version de la app en CAT_VARIABLES.
Sub guardaAppInfo 'ignore
B4XPages.MainPage.skmt.ExecNonQuery("delete from CAT_VARIABLES where CAT_VA_DESCRIPCION = 'EMPRESA' or CAT_VA_DESCRIPCION = 'APP_NAME' or CAT_VA_DESCRIPCION = 'APP_VERSION'")
B4XPages.MainPage.skmt.ExecNonQuery($"insert into CAT_VARIABLES (CAT_VA_DESCRIPCION, CAT_VA_VALOR) values ('APP_NAME', '${Application.LabelName}')"$)
B4XPages.MainPage.skmt.ExecNonQuery($"insert into CAT_VARIABLES (CAT_VA_DESCRIPCION, CAT_VA_VALOR) values ('APP_VERSION', '${Application.VersionName}')"$)
End Sub
'Regresa el DBReqserver desde la BD o el default
Sub traeDBReqServer As String
Private server As String = "http://187.189.244.154:1782"
Private c As ResultSet = B4XPages.MainPage.skmt.ExecQuery($"select * from CAT_VARIABLES where CAT_VA_DESCRIPCION = 'DBReqServer'"$)
Do While c.NextRow
If c.GetString("CAT_VA_VALOR") <> "null" And c.GetString("CAT_VA_VALOR") <> "" Then
server = c.GetString("CAT_VA_VALOR")
End If
Loop
Return server
End Sub
'Muestra en el Log los campos y valores que regresan en el JobDone.
Sub logJobDoneResultados(resultado As DBResult)
For Each records() As Object In resultado.Rows
LogColor($"====== ${resultado.Tag} - REGISTROS = ${resultado.Rows.Size}"$, Colors.RGB(215,37,0))
For Each k As String In resultado.Columns.Keys
LogColor(k & " = " & records(resultado.Columns.Get(k)), Colors.RGB(215,37,0))
Next
Next
End Sub
'Regresa el almacen actual de la base de datos.
Sub traeAlmacen As String 'ignore
Private c As Cursor
Private a As String
c = B4XPages.MainPage.skmt.ExecQuery("select ID_ALMACEN from CAT_ALMACEN")
c.Position = 0
a = C.GetString("ID_ALMACEN")
c.Close
Return a
End Sub
'Agrega una columna a la tabla especificada.
'Hay que indicar el "tipo" de la columna (TEXT, INTEGER, ETC)
'Ej. agregaColumna("TABLA", "COLUMNA", "TIPO")
Sub agregaColumna(tabla As String, columna As String, tipo As String) 'ignore
Try 'Intentamos usar "pragma_table_info" para revisar si existe la columna en la tabla
Private c As Cursor = B4XPages.MainPage.skmt.ExecQuery($"SELECT COUNT(*) AS fCol FROM pragma_table_info('${tabla}') WHERE name='${columna}'"$)
c.Position = 0
If c.GetString("fCol") = 0 Then 'Si no esta la columna la agregamos
B4XPages.MainPage.skmt.ExecNonQuery($"ALTER TABLE ${tabla} ADD COLUMN ${columna} ${tipo}"$)
Log($"Columna "${columna} ${tipo}", agregada a "${tabla}"."$)
End If
Catch 'Si no funciona "pragma_table_info" lo hacemos con try/catch
Try
B4XPages.MainPage.skmt.ExecNonQuery($"ALTER TABLE ${tabla} ADD COLUMN ${columna} ${tipo}"$)
Log($"Columna "${columna} ${tipo}", agregada a "${tabla}".."$)
Catch
Log(LastException)
End Try
End Try
End Sub