mirror of
https://github.com/KeymonSoft/ADM2.git
synced 2026-04-17 19:36:33 +00:00
- Se cambia e envio de las fotos a una formma para guardarlas en disco y no en BD - Se agrega la hora de inico a las NO Ventas de la Bitacora!
1629 lines
79 KiB
QBasic
1629 lines
79 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
|
|
Private su As StringUtils 'Usa la libreria StringUtils
|
|
Dim phn As Phone
|
|
Dim devModel As String
|
|
Dim kmt, 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 = ""
|
|
Private subsLogs As Boolean = False
|
|
Dim in As Intent
|
|
Dim intentUsado As Boolean = False
|
|
End Sub
|
|
|
|
'Pone el valor de phn.Model en la variable global "devModel"
|
|
Sub getPhnId As String 'ignore
|
|
'Requiere la libreria "Phone"
|
|
devModel = phn.Model
|
|
If devModel.Length <= 3 Then 'Si phn.Model esta en blanco ...
|
|
Dim t As String = phn.GetSettings("android_id") 'Intentamos con "android_id"
|
|
devModel = t
|
|
End If
|
|
If devModel.Length >= 3 Then 'Si tenemos valor para phn.Model
|
|
File.WriteString(File.DirInternal, "phnId.txt", devModel) 'Sobreescribimos archivo phnId.txt with deviceId
|
|
' Log("Tenemos phnId: "&devModel&" "&File.DirInternal&"/phn.txt sobreescrito")
|
|
Else If devModel.Length < 3 Then ' Si no tenemos valor, lo leemos de phnId.txt
|
|
Dim s As String = File.ReadString(File.DirInternal, "phnId.txt")
|
|
devModel = s
|
|
' Log("Leemos id de "&File.DirInternal&"/phnId.txt")
|
|
' Log(devModel)
|
|
End If
|
|
Return devModel
|
|
End Sub
|
|
|
|
'Comprime y regresa un texto (str) en base64
|
|
Sub compress(str As String) As String 'ignore
|
|
'Requiere la libreria "CompressStrings"
|
|
Dim compressed() As Byte = GZip.compress(str)
|
|
' Log($"UncompressedBytesLength: ${str.Length}"$)
|
|
' Log($"CompressedBytesLength: ${compressed.Length}"$)
|
|
Dim base64 As String = su.EncodeBase64(compressed)
|
|
Log($"Comprimido: ${base64.Length}"$)
|
|
' Log($"CompressedBytes converted to base64: ${base64}"$)
|
|
Return base64
|
|
End Sub
|
|
|
|
'Descomprime y regresa un texto en base64
|
|
Sub decompress(base64 As String) As String 'ignore
|
|
Dim decompressedbytes() As Byte = su.DecodeBase64(base64)
|
|
' Log($"decompressedbytesLength: ${decompressedbytes.Length}"$)
|
|
Dim bc As ByteConverter
|
|
Dim uncompressed As String = bc.StringFromBytes(decompressedbytes,"UTF8")
|
|
Log($"Descomprimido: ${uncompressed.Length}"$)
|
|
' Log($"Decompressed String = ${uncompressed}"$)
|
|
Return uncompressed
|
|
End Sub
|
|
|
|
'Convierte una fecha al formato yyMMddHHmmss
|
|
Sub fechaKMT(fecha As String) As String 'ignore
|
|
' 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
|
|
' 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)
|
|
' 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)
|
|
Log("notiLowReturn: "&title)
|
|
notification.Icon = "icon"
|
|
notification.Sound = False
|
|
notification.Vibrate = False
|
|
notification.SetInfo(title, Body, Main)
|
|
notification.Notify(id)
|
|
' Log("notiLowReturn SetInfo")
|
|
Return notification
|
|
End Sub
|
|
|
|
'Escribimos las coordenadas y fecha a un archivo de texto
|
|
Sub guardaInfoEnArchivo(coords As String) 'ignore
|
|
' Cambiamos el formato de la hora
|
|
Dim OrigFormat As String=DateTime.DateFormat 'save orig date format
|
|
DateTime.DateFormat="MMM-dd HH:mm:ss"
|
|
Dim lastUpdate As String=DateTime.Date(DateTime.Now)
|
|
DateTime.DateFormat=OrigFormat 'return to orig date format
|
|
|
|
Dim ubic As String = coords&","&lastUpdate
|
|
Dim out As OutputStream = File.OpenOutput(File.DirInternal, "gps.txt", True)
|
|
Dim s As String = ubic & CRLF
|
|
Dim t() As Byte = s.GetBytes("UTF-8")
|
|
out.WriteBytes(t, 0, t.Length)
|
|
out.Close
|
|
End Sub
|
|
|
|
'Escribimos las coordenadas (latitud, longitud, fecha) y fecha a una BD
|
|
Sub guardaInfoEnBD(coords As String) 'ignore
|
|
Log("Guardamos ubicacion en BD - "&coords)
|
|
Try
|
|
Dim latlon() As String = Regex.Split("\|", coords)
|
|
If latlon.Length < 2 Then latlon = Regex.Split(",", coords) 'Si son menos de 2, entonces estan separadas por comas y no por "|"
|
|
If subsLogs Then Log("LatLon="&latlon)
|
|
kmt.ExecNonQuery2("INSERT INTO RUTA_GPS(FECHA, LAT, LON) VALUES (?,?,?)", Array As Object (latlon(2),latlon(0),latlon(1)))
|
|
Catch
|
|
LogColor(LastException, Colors.red)
|
|
End Try
|
|
End Sub
|
|
|
|
'Regresa la ruta solicitada comprimida y en base64
|
|
Sub dameRuta(inicioRuta As String, origenRuta As String) As String 'ignore
|
|
'Requiere la libreria "SQL"
|
|
Dim fechaInicio As String
|
|
Try 'incioRuta es numero
|
|
inicioRuta = inicioRuta * 1
|
|
' Log("fechaInicio numerica="&fechaInicio)
|
|
fechaInicio = fechaKMT(DateTime.Now - (DateTime.TicksPerHour * inicioRuta))
|
|
Catch 'inicioRuta es string
|
|
fechaInicio = fechaInicioHoy
|
|
' Log("fechaInicio string="&fechaInicio)
|
|
End Try
|
|
If subsLogs Then Log("fechaInicio: "&fechaInicio&" | rutaHrsAtras="&rutaHrsAtras) 'fechaKMT(DateTime.Now)
|
|
Dim c As Cursor
|
|
If kmt.IsInitialized = False Then kmt.Initialize(Starter.ruta, "kmt.db", True)
|
|
If subsLogs Then Log("select FECHA, LAT, LON from "& origenRuta &" where FECHA > " & fechaInicio & " order by FECHA desc limit " & rutaMaxPoints)
|
|
c = kmt.ExecQuery("select FECHA, LAT, LON from "& origenRuta &" where FECHA > " & fechaInicio & " order by FECHA desc limit " & rutaMaxPoints)
|
|
c.Position = 0
|
|
Dim ruta2 As String = ""
|
|
If c.RowCount>0 Then
|
|
For i=0 To c.RowCount -1
|
|
c.Position=i
|
|
ruta2=ruta2&CRLF&c.GetString("LAT")&","&c.GetString("LON")&","&c.GetString("FECHA")
|
|
B4XPages.MainPage.fechaRuta = c.GetString("FECHA")
|
|
Next
|
|
End If
|
|
c.Close
|
|
Return compress(ruta2)
|
|
End Sub
|
|
|
|
'Limpiamos la tabla RUTA_GPS de la BD
|
|
Sub deleteGPS_DB 'ignore
|
|
kmt.ExecNonQuery("delete from RUTA_GPS")
|
|
kmt.ExecNonQuery("vacuum;")
|
|
ToastMessageShow("Borramos BD Coords GPS", False)
|
|
End Sub
|
|
|
|
'Limpiamos la tabla errorLog de la BD
|
|
Sub deleteErrorLog_DB 'ignore
|
|
errorLog.ExecNonQuery("delete from errores")
|
|
errorLog.ExecNonQuery("vacuum;")
|
|
ToastMessageShow("BD Errores Borrada", False)
|
|
End Sub
|
|
|
|
'Borramos el archio "gps.txt"
|
|
Sub borramosArchivoGPS 'ignore
|
|
Dim out As OutputStream = File.OpenOutput(File.DirInternal, "gps.txt", False)
|
|
Dim s As String = ""
|
|
Dim t() As Byte = s.GetBytes("UTF-8")
|
|
out.WriteBytes(t, 0, t.Length)
|
|
out.Close
|
|
End Sub
|
|
|
|
'Revisa que exista la BD y si es necesario crea algunans tablas dentro de ella
|
|
Sub revisaBD 'ignore
|
|
' Main.ruta = File.DirInternal
|
|
' Log(Starter.ruta)
|
|
' Log(File.Exists(Starter.ruta, "kmt.db"))
|
|
If Not(File.Exists(Starter.ruta, "kmt.db")) Then
|
|
File.Copy(File.DirAssets, "kmt.db", Starter.ruta, "kmt.db")
|
|
LogColor("copiamos kmt.db de "&File.DirAssets & " a " & Starter.ruta,Colors.Green)
|
|
End If
|
|
If Not(kmt.IsInitialized) Then kmt.Initialize(Starter.ruta, "kmt.db", True)
|
|
kmt.ExecNonQuery("CREATE TABLE IF NOT EXISTS RUTA_GPS(FECHA INTEGER, LAT TEXT, LON TEXT)")
|
|
' kmt.ExecNonQuery("CREATE TABLE IF NOT EXISTS UUC(fecha INTEGER, lat TEXT, lon TEXT)") 'LastKnownLocation
|
|
kmt.ExecNonQuery("CREATE TABLE IF NOT EXISTS bitacora(fecha INTEGER, texto TEXT)") 'Bitacora
|
|
kmt.ExecNonQuery("CREATE TABLE IF NOT EXISTS HIST_AVANCE(HA_MARCA TEXT, HA_AVANCE TEXT, HA_OBJETIVO TEXT, HA_PORCENTAJE TEXT)") 'Historico avance mes actual
|
|
kmt.ExecNonQuery("CREATE TABLE IF NOT EXISTS ABONOSP(NOTA TEXT, CLIENTE TEXT, SALDO_PENDIENTE TEXT)")
|
|
kmt.ExecNonQuery("CREATE TABLE IF NOT EXISTS CLIENTE_NUEVO(CN_ID_CLIENTE TEXT, CN_NOMBRE TEXT, CN_enviado text)")
|
|
kmt.ExecNonQuery("CREATE TABLE IF NOT EXISTS RUTA_SUPLENCIA(RS_RUTA TEXT)")
|
|
Try 'Si no existe la columna PC_ENVIO_OK la agregamos.
|
|
kmt.ExecQuery("select count(PC_ENVIO_OK) from PEDIDO_CLIENTE")
|
|
Catch
|
|
Try
|
|
kmt.ExecNonQuery("ALTER TABLE PEDIDO_CLIENTE ADD COLUMN PC_ENVIO_OK INTEGER")
|
|
Catch
|
|
LogColor("No pudimos agregar la columna PC_ENVIO_OK.", Colors.Red)
|
|
LogColor(LastException, Colors.Red)
|
|
End Try
|
|
End Try
|
|
|
|
Try 'Si no existe la columna CAT_CL_DIAS_VISITA la agregamos.
|
|
kmt.ExecQuery("select count(CAT_CL_DIAS_VISITA) from kmt_info2")
|
|
Catch
|
|
Try
|
|
kmt.ExecNonQuery("ALTER TABLE kmt_info2 ADD COLUMN CAT_CL_DIAS_VISITA TEXT")
|
|
Catch
|
|
LogColor("No pudimos agregar la columna CAT_CL_DIAS_VISITA.", Colors.Red)
|
|
LogColor(LastException, Colors.Red)
|
|
End Try
|
|
End Try
|
|
|
|
Try 'Si no existe la columna PC_ENVIO_OK la agregamos.
|
|
kmt.ExecQuery("select count(PC_TIEMPO_TIENDA) from PEDIDO_CLIENTE")
|
|
Catch
|
|
Try
|
|
kmt.ExecNonQuery("ALTER TABLE PEDIDO_CLIENTE ADD COLUMN PC_TIEMPO_TIENDA FLOAT")
|
|
Catch
|
|
LogColor("No pudimos agregar la columna PC_TIEMPO_TIENDA.", Colors.Red)
|
|
LogColor(LastException, Colors.Red)
|
|
End Try
|
|
End Try
|
|
Try 'Si no existe la columna PC_FACTURA la agregamos.
|
|
kmt.ExecQuery("select count(PC_FACTURA) from PEDIDO_CLIENTE")
|
|
Catch
|
|
Try
|
|
kmt.ExecNonQuery("ALTER TABLE PEDIDO_CLIENTE ADD COLUMN PC_FACTURA INTEGER")
|
|
Catch
|
|
LogColor("No pudimos agregar la columna PC_FACTURA.", Colors.Red)
|
|
LogColor(LastException, Colors.Red)
|
|
End Try
|
|
End Try
|
|
kmt.ExecNonQuery("CREATE TABLE IF NOT EXISTS bitacora(fecha INTEGER, texto TEXT)") 'Bitacora
|
|
' kmt.ExecNonQuery("drop table PEDIDO_INICIO_FINAL") 'Bitacora
|
|
kmt.ExecNonQuery("CREATE TABLE IF NOT EXISTS PEDIDO_INICIO_FINAL(PIF_CLIENTE TEXT, PIF_HORA_INICIO LONG, PIF_HORA_FINAL LONG)") 'Bitacora
|
|
|
|
'Tabla para la bitacora de errores
|
|
If Not(errorLog.IsInitialized) Then errorLog.Initialize(Starter.ruta, "errorLog.db", True)
|
|
errorLog.ExecNonQuery("CREATE TABLE IF NOT EXISTS errores(fecha INTEGER, error TEXT)")
|
|
End Sub
|
|
|
|
'Obtiene el ssid al que esta conectado el telefono
|
|
Sub getSSID 'ignore
|
|
' 'Requiere la libreria "MLWifi400"
|
|
' If wifi.isWifiConnected Then
|
|
' ssid = wifi.WifiSSID
|
|
' End If
|
|
End Sub
|
|
|
|
'Convierte un texto en formato JSON a un objeto "Map"
|
|
Sub JSON2Map(theJson As String) As Map 'ignore
|
|
'Requiere la libreria "JSON"
|
|
Try
|
|
Private json As JSONParser
|
|
json.Initialize(theJson)
|
|
Return json.NextObject
|
|
Catch
|
|
Log(LastException)
|
|
log2DB("JSON2Map: "&LastException)
|
|
Private m As Map = CreateMap("title":"Error generating JSON", "t":"Error", "Message":LastException, "text" : LastException)
|
|
Return m
|
|
End Try
|
|
End Sub
|
|
|
|
'Convierte un mapa a formato JSON
|
|
Sub map2JSON(m As Map) As String 'ignore
|
|
'Requiere la libreria "JSON"
|
|
'Convierte un objecto "Map" a JSON
|
|
Dim jg As JSONGenerator
|
|
jg.Initialize(m)
|
|
Dim t As String = jg.ToString
|
|
Return t
|
|
End Sub
|
|
|
|
'Mandamos "coords" en un mensaje a "Sprvsr"
|
|
'Sub mandamosLoc(coords As String) 'ignore
|
|
'' Log("Iniciamos mandamosLoc "&coords)
|
|
'' Log("locRequest="&Tracker.locRequest)
|
|
' guardaInfoEnBD(coords)'Escribimos coordenadas y fecha a una bd
|
|
' Dim t As String
|
|
' If Tracker.locRequest="Activa" Then
|
|
' If PushService.au = 1 Then
|
|
' t = "au" ' es una actualizacion
|
|
' Else
|
|
' t = "u" ' es una peticion
|
|
' End If
|
|
' Dim params As Map = CreateMap("topic":"Sprvsr", "coords":coords, "t":t, "b":PushService.battery, "mt":Main.montoActual)
|
|
' CallSub2(PushService, "mandaMensaje",params)
|
|
' Tracker.locRequest="Enviada"
|
|
' CallSubDelayed(Tracker,"CreateLocationRequest")
|
|
' End If
|
|
'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
|
|
Log("Hoy="&h)
|
|
Return h
|
|
End Sub
|
|
|
|
'Guardamos "texto" a la bitacora
|
|
Sub log2DB(texto As String) 'ignore
|
|
LogColor(fechaKMT(DateTime.Now)&" - log2BD: '"&texto&"'", Colors.LightGray)
|
|
kmt.ExecNonQuery2("INSERT INTO bitacora(fecha, texto) VALUES (?,?)", Array As Object (fechaKMT(DateTime.now), texto))
|
|
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
|
|
' 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
|
|
' Log("+++ +++ "&fechaKMT(fechaKMT2Ticks(hora) + mins * DateTime.TicksPerMinute) & " < " & fechaKMT(DateTime.Now))
|
|
Return True
|
|
Else
|
|
' Log("+++ +++ "&fechaKMT(fechaKMT2Ticks(hora) + mins * DateTime.TicksPerMinute) & " > " & fechaKMT(DateTime.Now))
|
|
Return False
|
|
End If
|
|
Catch
|
|
Log(LastException)
|
|
End Try
|
|
End Sub
|
|
|
|
'Limpiamos la tabla "bitacora" de la BD
|
|
Sub borraLogDB 'ignore
|
|
LogColor("Borramos BD de log", Colors.Magenta)
|
|
kmt.ExecNonQuery("delete from bitacora")
|
|
kmt.ExecNonQuery("vacuum;")
|
|
End Sub
|
|
|
|
'Monitoreamos los servicios para ver si estan activos (No pausados), y si no, los reniciamos
|
|
'Sub Monitor 'ignore
|
|
' Private monitorStatus As Boolean = True
|
|
' LogColor("Corriendo Subs.Monitor", Colors.RGB(161,150,0))
|
|
' If IsPaused(Tracker) Then
|
|
' log2DB("Reiniciando 'Tracker Pausado' desde Subs.Monitor")
|
|
' StartService(Tracker)
|
|
' monitorStatus = False
|
|
' Else
|
|
' CallSubDelayed(Tracker, "revisaFLP")
|
|
' End If
|
|
' If IsPaused(PushService) Then
|
|
' log2DB("Reiniciando 'PushService Pausado' desde Subs.Monitor")
|
|
' StartService(PushService)
|
|
' monitorStatus = False
|
|
' Else
|
|
' revisaPushService
|
|
' End If
|
|
' If monitorStatus Then LogColor(" +++ +++ Servicios Activos", Colors.Green)
|
|
'End Sub
|
|
|
|
'Compara la UUG (Ultima Ubicacion Guardada) con FLP.LastKnowLocation y si
|
|
'cumple con los requisitos de distancia y precision la guardamos en la BD.
|
|
Sub revisaUUG 'ignore
|
|
Try
|
|
' revisaFLP
|
|
If Tracker.FLP.GetLastKnownLocation.IsInitialized Then
|
|
Dim daa As Int = Tracker.UUGCoords.DistanceTo(Tracker.FLP.GetLastKnownLocation) 'Distancia de la UUG a la actual de Tracker.FLP.GetLastKnownLocation
|
|
If Starter.Logger Then LogColor($"**** UUC "${fechaKMT(Tracker.FLP.GetLastKnownLocation.Time)}|$0.2{Tracker.FLP.GetLastKnownLocation.Accuracy}|$0.8{Tracker.FLP.GetLastKnownLocation.Latitude}|$0.8{Tracker.FLP.GetLastKnownLocation.Longitude}|$0.2{Tracker.FLP.GetLastKnownLocation.Speed}|"$, Colors.RGB(255,112,35))
|
|
If daa > 40 And Tracker.FLP.GetLastKnownLocation.Accuracy < 35 Then 'Si la distancia de la ubicacion anterior es mayor de XX y la precision es menor de XX, la guardamos ...
|
|
kmt.ExecNonQuery2("INSERT INTO RUTA_GPS(fecha, lat, lon) VALUES (?,?,?)", Array As Object (fechaKMT(Tracker.FLP.GetLastKnownLocation.Time),Tracker.FLP.GetLastKnownLocation.Latitude,Tracker.FLP.GetLastKnownLocation.Longitude))
|
|
If Starter.Logger Then Log("++++ Distancia a anterior="&daa&"|"&"Precision="&Tracker.FLP.GetLastKnownLocation.Accuracy)
|
|
End If
|
|
Tracker.UUGCoords = Tracker.FLP.GetLastKnownLocation
|
|
End If
|
|
Catch
|
|
LogColor("If Tracker.FLP.GetLastKnownLocation.IsInitialized --> "&LastException, Colors.Red)
|
|
End Try
|
|
End Sub
|
|
|
|
'Revisamos que el FLP (FusedLocationProvider) este inicializado y activo
|
|
Sub revisaFLP 'ignore
|
|
LogColor("**** **** Revisamos FLP **** ****", Colors.RGB(78,0,227))
|
|
Private todoBienFLP As Boolean = True
|
|
Try
|
|
If Not(Tracker.FLP.IsInitialized) Then
|
|
log2DB("revisaFLP: No esta inicializado ... 'Reinicializando FLP'")
|
|
Tracker.FLP.Initialize("flp")
|
|
todoBienFLP = False
|
|
End If
|
|
Catch
|
|
LogColor("If Not(Tracker.FLP.IsInitialized) --- "&LastException, Colors.Red)
|
|
End Try
|
|
Try
|
|
If Tracker.FLP.IsInitialized Then
|
|
Try
|
|
If Not(Tracker.FLP.IsConnected) Then
|
|
log2DB("revisaFLP: No esta conectado ... 'Reconectando FLP'")
|
|
' Tracker.FLP.Connect
|
|
CallSubDelayed(Tracker,"StartFLP")
|
|
todoBienFLP = False
|
|
End If
|
|
Catch
|
|
LogColor("If Not(Tracker.FLP.IsConnected) --> "&LastException, Colors.Red)
|
|
End Try
|
|
Try
|
|
If Tracker.FLP.IsConnected And _
|
|
Tracker.FLP.GetLastKnownLocation.IsInitialized And _
|
|
Tracker.FLP.GetLastKnownLocation.DistanceTo(Tracker.UUGCoords) > 500 Then
|
|
log2DB("revisaFLP: 'No se esta actualizando, lo reiniciamos ...'")
|
|
StartService(Tracker)
|
|
todoBienFLP = False
|
|
End If
|
|
Catch
|
|
LogColor("If FLP.IsConnectctd and FLP.getLKL.IsInitialized --> "&LastException, Colors.Red)
|
|
End Try
|
|
End If
|
|
If todoBienFLP Then LogColor(" +++ +++ Sin errores en FLP", Colors.Green)
|
|
Catch
|
|
LogColor("If Tracker.FLP.IsInitialized --> "&LastException, Colors.Red)
|
|
End Try
|
|
' revisar hora de lastKnownlocation y si es mayor de 10 minutos llamar StartFLP
|
|
End Sub
|
|
|
|
'Revisamos que el servicio "PushService" este inicializado y activo
|
|
'Sub revisaPushService 'ignore
|
|
' Private todoBienPS As Boolean = True
|
|
' LogColor("**** **** Revisamos PushService **** ****", Colors.RGB(78,0,227))
|
|
' If Not(PushService.wsh.IsInitialized) Then 'Si no esta inicializado ...
|
|
' log2DB("revisaPushService: No esta inicializado ... 'Reinicializando PushService'")
|
|
' CallSubDelayed(PushService, "Connect")
|
|
' todoBienPS = False
|
|
' End If
|
|
' If Not(PushService.wsh.ws.Connected) Then 'Si no esta conectado ...
|
|
' log2DB("revisaPushService: No esta conectado ... 'Reconectando PushService'")
|
|
' CallSubDelayed(PushService, "Connect")
|
|
' todoBienPS = False
|
|
' End If
|
|
' If masDeXXMinsKMT(Starter.pushServiceActividad, 5) Then 'Si mas de xx minutos de la ultima actividad entonces ...
|
|
' PushService.wsh.Close
|
|
' CallSubDelayed(PushService, "Connect")
|
|
'' StartService(PushService)
|
|
' log2DB("revisaPushService: 'Reconectamos 'PushService' por inactividad")
|
|
' Starter.pushServiceActividad = fechaKMT(DateTime.Now)
|
|
' todoBienPS = False
|
|
' End If
|
|
' If todoBienPS Then LogColor(" +++ +++ Sin errores en PushService", Colors.Green)
|
|
'End Sub
|
|
|
|
'Borramos renglones extra de la tabla de errores
|
|
Sub borraArribaDe100Errores 'ignore
|
|
revisaBD
|
|
LogColor("Borramos BD de log", Colors.Magenta)
|
|
errorLog.ExecNonQuery("DELETE FROM errores WHERE fecha NOT in (SELECT fecha FROM errores ORDER BY fecha desc LIMIT 99 )")
|
|
errorLog.ExecNonQuery("vacuum;")
|
|
Log("Borramos mas de 100 de errorLog")
|
|
End Sub
|
|
|
|
'Borramos renglones extra de la tabla de bitacora
|
|
Sub borraArribaDe600RenglonesBitacora 'ignore
|
|
revisaBD
|
|
LogColor("Borramos BD de log", Colors.Magenta)
|
|
kmt.ExecNonQuery("DELETE FROM bitacora WHERE fecha NOT in (SELECT fecha FROM bitacora ORDER BY fecha desc LIMIT 599 )")
|
|
kmt.ExecNonQuery("vacuum;")
|
|
Log("Borramos mas de 600 de bitacora")
|
|
End Sub
|
|
|
|
'Inserta 50 renglones de prueba a la tabla "errores"
|
|
Sub insertaRenglonesPruebaEnErrorLog 'ignore
|
|
revisaBD
|
|
Log("insertamos 50 renglones a errorLog")
|
|
For x = 1 To 50
|
|
errorLog.ExecNonQuery2("INSERT INTO errores(fecha, error) VALUES (?,?)", Array As Object (fechaKMT(DateTime.now), "abc"))
|
|
Log(x)
|
|
Next
|
|
End Sub
|
|
|
|
'Regresa la tabla "errores" en una lista de mapas convertida a JSON
|
|
Sub dameErroresJSON(SQL As SQL, maxErrores As Int, comprimido As Boolean) As String 'ignore
|
|
Log("dameErroresJSON")
|
|
Private j As JSONGenerator
|
|
Private lim As String
|
|
Private cur As ResultSet
|
|
Private l As List
|
|
Private i As Int = 0
|
|
l.Initialize
|
|
Dim m, m2 As Map
|
|
m2.Initialize
|
|
If maxErrores = 0 Then lim = "" Else lim = "limit "&maxErrores
|
|
cur = SQL.ExecQuery("select * from errores order by fecha desc "&lim)
|
|
Do While cur.NextRow
|
|
m.Initialize
|
|
m.Put("fecha", cur.GetString("fecha"))
|
|
m.Put("error", cur.GetString("error"))
|
|
m2.Put(i,m)
|
|
i = i + 1
|
|
Loop
|
|
cur.Close
|
|
j.Initialize(m2)
|
|
Log(j.ToString)
|
|
If comprimido Then
|
|
Return compress(j.ToString)
|
|
Else
|
|
Return j.ToString
|
|
End If
|
|
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)
|
|
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)
|
|
LogColor($"Fecha dada: ${fKMT}, Parte Fecha: ${parteFecha}, Parte Hora: ${parteHora}"$, Colors.Red)
|
|
Return 0
|
|
End Try
|
|
End Sub
|
|
|
|
Sub InstallAPK(dir As String, apk As String) 'ignore
|
|
If File.Exists(dir, apk) Then
|
|
Dim i As Intent
|
|
i.Initialize(i.ACTION_VIEW, "file://" & File.Combine(dir, apk))
|
|
i.SetType("application/vnd.android.package-archive")
|
|
StartActivity(i)
|
|
End If
|
|
End Sub
|
|
|
|
'Copia la base de datos del almacenamiento interno al externo en el directorio kmts
|
|
Sub copiaDB(result As Boolean) 'ignore
|
|
ToastMessageShow("copiaDB", False)
|
|
If result Then
|
|
Dim p As String
|
|
If File.ExternalWritable Then
|
|
p = File.DirInternal
|
|
' Log("Externo")
|
|
Else
|
|
p = File.DirInternal
|
|
' Log("Interno")
|
|
End If
|
|
Dim theDir As String
|
|
Try
|
|
File.MakeDir(File.DirInternal,"kmts")
|
|
theDir = "/kmts"
|
|
Catch
|
|
theDir = ""
|
|
End Try
|
|
Try
|
|
File.Copy(File.DirInternal,"kmt.db",File.DirInternal&theDir,"cedex_kmt.db")
|
|
File.Copy(File.DirInternal,"errorLog.db",File.DirInternal&theDir,"cedex_errorLog.db")
|
|
ToastMessageShow("BD copiada!", False)
|
|
Catch
|
|
ToastMessageShow("No se pudo hacer la copia: "&LastException, True)
|
|
End Try
|
|
Log("rootExternal="&p)
|
|
Log("File.DirInternal="&File.DirInternal)
|
|
Log("File.DirInternal="&File.DirInternal)
|
|
Else
|
|
ToastMessageShow("Sin permisos", False)
|
|
End If
|
|
End Sub
|
|
|
|
'Hace visible y trae al frente 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
|
|
|
|
'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 horizontalmente dentro de un elemento superior
|
|
Sub centraPanel(elemento As Panel, anchoElementoSuperior As Int) 'ignore
|
|
elemento.Left = Round(anchoElementoSuperior/2)-(elemento.Width/2)
|
|
End Sub
|
|
|
|
'Centra un panel verticalmente dentro de un elemento superior
|
|
Sub centraPanelV(elemento As Panel, altoElementoSuperior As Int) 'ignore
|
|
elemento.Top = Round(altoElementoSuperior/2)-(elemento.Height/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
|
|
|
|
'Regresa el usuario de la tabla USUARIOA si es que existe, si no existe, regresa "SinUsuario".
|
|
Sub buscaDBUsuario As String 'ignore
|
|
Private c As Cursor
|
|
Private usuario As String = "SinUsuario"
|
|
c=kmt.ExecQuery("select USUARIO from usuarioa")
|
|
c.Position=0
|
|
If c.RowCount > 0 Then usuario = c.GetString("USUARIO")
|
|
Return usuario
|
|
End Sub
|
|
|
|
'Saca el usuario de la tabla USUARIOA
|
|
Sub dameUsuarioDeDB As String 'ignore
|
|
Private c As Cursor
|
|
Private u As String = "SinUsuario"
|
|
If Not(kmt.IsInitialized) Then revisaBD
|
|
c=kmt.ExecQuery("select USUARIO from usuarioa")
|
|
c.Position=0
|
|
If c.RowCount > 0 Then u = c.GetString("USUARIO")
|
|
c.Close
|
|
Return u
|
|
End Sub
|
|
|
|
''Inserta un producto en la tabla "PEDIDO"
|
|
'Sub guardaProductoX(cedis As String, costoTot As String, costoU As String, cant As String, nombre As String, prodId As String, clienteId As String, fecha As String, usuario As String, tipoV As String, precio2 As String, query As String) 'ignore
|
|
'' LogColor("guardaProducto", Colors.Magenta)
|
|
'' Log($"Guardamos producto ${prodId}"$)
|
|
'' B4XPages.MainPage.skmt.ExecNonQuery2("INSERT INTO PEDIDO (PE_CEDIS, PE_COSTO_TOT, PE_COSTOU, PE_CANT, PE_PRONOMBRE, PE_PROID, PE_CLIENTE, PE_FECHA, PE_USUARIO, PE_TIPO, PE_PRECIO2, PE_RUTA) VALUES(?,?,?,?,?,?,?,?,?,?,?,?) ", Array As Object (cedis, costoTot, costoU, cant, nombre, prodId, clienteId, fecha, usuario, tipoV, precio2, Starter.rutaV))
|
|
'' B4XPages.MainPage.skmt.ExecNonQuery2("update " & query & " set cat_gp_almacen = cat_gp_almacen - ? where cat_gp_id = ? ", Array As Object(cant, prodId))
|
|
'' ToastMessageShow("guardaProd", False)
|
|
'End Sub
|
|
|
|
'Sub guardaProductoSin(cedis As String, costoTot As String, costoU As String, cant As String, nombre As String, prodId As String, clienteId As String, fecha As String, usuario As String, rutaV As String, precioSin As String, tipoV As String, precio2 As String, query As String) 'ignore
|
|
'' LogColor("guardaProductoSin", Colors.Magenta)
|
|
'' B4XPages.MainPage.skmt.ExecNonQuery2("INSERT INTO PEDIDO (PE_CEDIS, PE_COSTO_TOT, PE_COSTOU, PE_CANT, PE_PRONOMBRE, PE_PROID, PE_CLIENTE, PE_FECHA, PE_USUARIO, PE_RUTA, PE_COSTO_SIN, PE_TIPO, PE_PRECIO2) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?) ", Array As Object (cedis, costoTot,costoU, cant, nombre, prodId, clienteId, fecha, usuario, rutaV, precioSin, tipoV, precio2))
|
|
'' B4XPages.MainPage.skmt.ExecNonQuery2("update " & query & " set cat_gp_almacen = cat_gp_almacen - ? where cat_gp_id = ? ", Array As Object(cant, prodId))
|
|
'' DateTime.DateFormat = "MM/dd/yyyy"
|
|
'' Private sDate As String =DateTime.Date(DateTime.Now)
|
|
'' Private sTime As String =DateTime.Time(DateTime.Now)
|
|
'' Private c As Cursor = B4XPages.MainPage.skmt.ExecQuery("select sum(pe_costo_tot) as TOTAL_CLIE, SUM(PE_CANT) AS CANT_CLIE, SUM(PE_COSTO_SIN) AS TOTAL_CLIE_SIN FROM PEDIDO WHERE PE_CLIENTE IN (Select CUENTA from cuentaa)")
|
|
'' c.Position=0
|
|
'' B4XPages.MainPage.skmt.ExecNonQuery("delete from pedido_cliente where PC_CLIENTE In (select cuenta from cuentaa)")
|
|
'' B4XPages.MainPage.skmt.ExecNonQuery2("insert into pedido_cliente(PC_CLIENTE, PC_FECHA, PC_USER, PC_NOART, PC_MONTO,PC_LON, PC_LAT,PC_ALMACEN,PC_RUTA,PC_COSTO_SIN) VALUES (?,?,?,?,?,?,?,?,?,?)", Array As Object(clienteId, sDate & sTime, usuario, c.GetString("CANT_CLIE"),c.GetString("TOTAL_CLIE"), B4XPages.MainPage.lon_gps, B4XPages.MainPage.lat_gps, cedis, rutaV, c.GetString("TOTAL_CLIE_SIN")))
|
|
'' B4XPages.MainPage.skmt.ExecNonQuery("UPDATE kmt_info set gestion = 2 where CAT_CL_CODIGO In (select cuenta from cuentaa)")
|
|
'' c.Close
|
|
' ToastMessageShow("guardaProdSin", False)
|
|
'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
|
|
|
|
'Regresa el nombre del producto desde CAT_GUNAPROD
|
|
Sub traeProdNombre(id As String) As String
|
|
Private h As Cursor
|
|
Private n As String
|
|
h=B4XPages.MainPage.skmt.ExecQuery2($"select CAT_GP_NOMBRE from ${Starter.tabla} where CAT_GP_ID = ? "$, Array As String(id.Trim))
|
|
If h.RowCount > 0 Then
|
|
h.Position = 0
|
|
n = h.GetString("CAT_GP_NOMBRE")
|
|
' Log(h.RowCount&"|"&id&"|"&n&"|")
|
|
End If
|
|
h.Close
|
|
If n = Null Or n="" Then n = "N/A"
|
|
' Log(h.RowCount&"|"&id&"|"&n&"|")
|
|
Return n
|
|
End Sub
|
|
|
|
'Regresa el total del pedido en la tabla "PEDIDO" del cliente actual.
|
|
Sub totalPedido As String
|
|
Private cT As Cursor = Starter.skmt.ExecQuery($"select sum(PE_COSTO_TOT) as total from PEDIDO where PE_CLIENTE = '${traeCliente}'"$)
|
|
Private pTotal As String = "0"
|
|
If cT.RowCount > 0 Then
|
|
cT.Position = 0
|
|
' Log("|"&cT.GetLong("total")&"|"&pTotal)
|
|
Private tempT As String = cT.GetLong("total")
|
|
If tempT <> "null" And tempT <> Null Then
|
|
' Log("|"&cT.GetLong("total")&"|")
|
|
pTotal = tempT
|
|
End If
|
|
' Log($"Cliente actual=${traeCliente}, hayPedido=${hay}"$)
|
|
End If
|
|
cT.Close
|
|
Return pTotal
|
|
End Sub
|
|
|
|
'Regresa la ruta actual de la base de datos.
|
|
Sub traeRuta As String 'ignore
|
|
Private c As Cursor
|
|
Private r As String
|
|
c=B4XPages.MainPage.skmt.ExecQuery("select CAT_CL_RUTA from kmt_info where CAT_CL_CODIGO In (Select cuenta from cuentaa)")
|
|
r = "0"
|
|
If c.RowCount > 0 Then
|
|
c.Position=0
|
|
r = c.GetString("CAT_CL_RUTA")
|
|
End If
|
|
c.Close
|
|
Return r
|
|
End Sub
|
|
|
|
'Regresa la ruta actual de la base de datos.
|
|
Sub traeRuta2 (cliente As String) As String 'ignore
|
|
Private c As Cursor
|
|
Private r As String
|
|
c=B4XPages.MainPage.skmt.ExecQuery($"select CAT_CL_RUTA from kmt_info where CAT_CL_CODIGO = '${cliente}' UNION ALL select CAT_CL_RUTA from kmt_info2 where CAT_CL_CODIGO = '${cliente}'"$)
|
|
r = "0"
|
|
If c.RowCount > 0 Then
|
|
c.Position=0
|
|
r = c.GetString("CAT_CL_RUTA")
|
|
End If
|
|
c.Close
|
|
Return r
|
|
End Sub
|
|
|
|
Sub traeCliente As String 'ignore
|
|
Private c As Cursor
|
|
Private cl As String
|
|
c=B4XPages.MainPage.skmt.ExecQuery("Select CUENTA from cuentaa")
|
|
c.Position=0
|
|
cl = c.GetString("CUENTA")
|
|
c.Close
|
|
Return cl
|
|
End Sub
|
|
|
|
Sub traeFecha As String 'ignore
|
|
DateTime.DateFormat = "MM/dd/yyyy"
|
|
Private sDate As String =DateTime.Date(DateTime.Now)
|
|
Private sTime As String =DateTime.Time(DateTime.Now)
|
|
Return sDate & sTime
|
|
End Sub
|
|
|
|
'Regresa el usuario de la tabla USUARIOA
|
|
Sub traeUsuarioDeBD As String 'ignore
|
|
Private c As Cursor
|
|
Private u As String = "SinUsuario"
|
|
If Not(kmt.IsInitialized) Then revisaBD
|
|
c=kmt.ExecQuery("select USUARIO from usuarioa")
|
|
c.Position=0
|
|
If c.RowCount > 0 Then u = c.GetString("USUARIO")
|
|
c.Close
|
|
Return u
|
|
End Sub
|
|
|
|
'Inserta un producto en la tabla "pedido" y "pedido_cliente".
|
|
'Actualiza "cat_gunaprod" y la columna "gestion" en la tabla "kmt_info".
|
|
Sub guardaProducto(cedis As String, costoU As String, cant As String, nombre As String, prodId As String, clienteId As String, fecha As String, usuario As String, rutaV As String, precioSin As String, tipoVenta As String)
|
|
' LogColor("guardaProducto: "&prodId&", cant="&cant, Colors.Magenta)
|
|
Private c As Cursor
|
|
B4XPages.MainPage.skmt.ExecNonQuery2("INSERT INTO PEDIDO (PE_CEDIS,PE_COSTO_TOT,PE_COSTOU,PE_CANT,PE_PRONOMBRE,PE_PROID,PE_CLIENTE,PE_FECHA,PE_USUARIO,PE_RUTA,PE_COSTO_SIN,PE_FOLIO) VALUES(?,?,?,?,?,?,?,?,?,?,?,?) ", Array As Object (cedis, (cant * costoU), costoU, cant, nombre, prodId, clienteId, fecha, usuario, rutaV, precioSin, tipoVenta))
|
|
B4XPages.MainPage.skmt.ExecNonQuery2($"update ${Starter.tabla} set cat_gp_almacen = cat_gp_almacen - ? where cat_gp_id = ? "$, Array As Object(cant, prodId))
|
|
c=B4XPages.MainPage.skmt.ExecQuery("select sum(pe_costo_tot) as TOTAL_CLIE, SUM(PE_CANT) AS CANT_CLIE, SUM(PE_COSTO_SIN) AS TOTAL_CLIE_SIN FROM PEDIDO WHERE PE_CLIENTE IN (Select CUENTA from cuentaa)")
|
|
c.Position=0
|
|
B4XPages.MainPage.skmt.ExecNonQuery("delete from pedido_cliente where PC_CLIENTE In (select cuenta from cuentaa)")
|
|
B4XPages.MainPage.skmt.ExecNonQuery2("insert into pedido_cliente(PC_CLIENTE, PC_FECHA, PC_USER, PC_NOART, PC_MONTO,PC_LON, PC_LAT,PC_ALMACEN,PC_RUTA,PC_COSTO_SIN) VALUES (?,?,?,?,?,?,?,?,?,?)", Array As Object(clienteId, fecha, usuario, c.GetString("CANT_CLIE"), c.GetString("TOTAL_CLIE"), B4XPages.MainPage.lon_gps, B4XPages.MainPage.lat_gps, cedis, c.GetString("TOTAL_CLIE_SIN")))
|
|
B4XPages.MainPage.skmt.ExecNonQuery("UPDATE kmt_info set gestion = 2 where CAT_CL_CODIGO In (select cuenta from cuentaa)")
|
|
End Sub
|
|
|
|
'Inserta un producto en la tabla "pedido" y "pedido_cliente" y actualiza "cat_gunaprod".
|
|
'NO ACTUALIZA LA BANDERA DE GESTION EN LA TABLA "kmt_info".
|
|
'Si "gestion=2" entonces el sistema considera que el pedido ya se guardó y ya no se debe modificar.
|
|
Sub guardaProductoSinGestion(cedis As String, costoU As String, cant As String, nombre As String, prodId As String, clienteId As String, fecha As String, usuario As String, rutaV As String, precioSin As String, tipoVenta As String, cantc As String, bcajas As String)
|
|
' LogColor("guardaProducto: "&prodId&", cant="&cant, Colors.Magenta)
|
|
Private c As Cursor
|
|
B4XPages.MainPage.skmt.ExecNonQuery2("INSERT INTO PEDIDO (PE_CEDIS,PE_COSTO_TOT,PE_COSTOU,PE_CANT,PE_PRONOMBRE,PE_PROID,PE_CLIENTE,PE_FECHA,PE_USUARIO,PE_RUTA,PE_COSTO_SIN,PE_FOLIO,PE_ENVIO_OK,PE_CANTC,PE_BCAJAS) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,0,?,?) ", Array As Object (cedis, (cant * costoU), costoU, cant, nombre, prodId, clienteId, fecha, usuario, rutaV, precioSin, tipoVenta,cantc,bcajas))
|
|
B4XPages.MainPage.skmt.ExecNonQuery2($"update ${Starter.tabla} set cat_gp_almacen = cat_gp_almacen - ? where cat_gp_id = ? "$, Array As Object(cant, prodId))
|
|
c=B4XPages.MainPage.skmt.ExecQuery("select sum(pe_costo_tot) as TOTAL_CLIE, SUM(PE_CANT) AS CANT_CLIE, SUM(PE_COSTO_SIN) AS TOTAL_CLIE_SIN FROM PEDIDO WHERE PE_CLIENTE IN (Select CUENTA from cuentaa)")
|
|
c.Position=0
|
|
B4XPages.MainPage.skmt.ExecNonQuery("delete from pedido_cliente where PC_CLIENTE In (select cuenta from cuentaa)")
|
|
B4XPages.MainPage.skmt.ExecNonQuery2("insert into pedido_cliente(PC_CLIENTE, PC_FECHA, PC_USER, PC_NOART, PC_MONTO,PC_LON, PC_LAT,PC_ALMACEN,PC_RUTA,PC_COSTO_SIN) VALUES (?,?,?,?,?,?,?,?,?,?)", Array As Object(clienteId, fecha, usuario, c.GetString("CANT_CLIE"), c.GetString("TOTAL_CLIE"), B4XPages.MainPage.lon_gps, B4XPages.MainPage.lat_gps, cedis, c.GetString("TOTAL_CLIE_SIN")))
|
|
End Sub
|
|
|
|
Sub actualizaProducto(cedis As String, costoU As String, cant As Int, nombre As String, prodId As String, clienteId As String, fecha As String, usuario As String, rutaV As String, precioSin As String, tipoVenta As String, cantc As String, bcajas As String)
|
|
Private c As Cursor=B4XPages.MainPage.skmt.ExecQuery($"select * from pedido where pe_cedis = '${cedis}' and pe_proid = '${prodId}' and pe_cliente = '${clienteId}' AND PE_FOLIO = '${B4XPages.MainPage.tipo_venta}'"$)
|
|
' Log($"ROWCOUNT: ${c.RowCount}, ${cant}"$)
|
|
LogColor($"actualizaProducto, c=${clienteId}, p=${prodId}, nombre=${nombre}, cant=${cant}, cedis=${cedis}, tipo=${tipoVenta}"$, Colors.Magenta)
|
|
|
|
If c.RowCount > 0 Then
|
|
c.Position=0
|
|
Private antCant As Int = 0
|
|
If IsNumber(c.GetInt("PE_CANT")) Then antCant=c.GetInt("PE_CANT")
|
|
Private difCant As Int = cant - antCant
|
|
B4XPages.MainPage.skmt.ExecNonQuery($"update pedido set pe_cant = ${cant}, pe_costo_tot = ${(cant*c.GetString("PE_COSTOU"))}, PE_CANTC = ${cantc}, PE_BCAJAS = ${bcajas} where pe_cedis = '${cedis}' and pe_proid = '${prodId}' and pe_cliente = '${clienteId}' AND PE_FOLIO = '${B4XPages.MainPage.tipo_venta}'"$)
|
|
B4XPages.MainPage.skmt.ExecNonQuery($"update ${Starter.tabla} set cat_gp_almacen = cat_gp_almacen - (${difCant}) where cat_gp_id = '${prodId}' "$)
|
|
' Log($"CANT=${cant}"$)
|
|
If cant = 0 Then
|
|
Log("BORRAMOS PROD")
|
|
B4XPages.MainPage.skmt.ExecNonQuery($"delete from pedido where pe_cedis = '${cedis}' and pe_proid = '${prodId}' and pe_cliente = '${clienteId}' AND PE_FOLIO = '${B4XPages.MainPage.tipo_venta}'"$)
|
|
Private pe As Cursor = B4XPages.MainPage.skmt.ExecQuery("select count(pe_cliente) as cuantosPedidos from pedido where pe_cliente In (select cuenta from cuentaa)")
|
|
pe.Position=0
|
|
If pe.GetString("cuantosPedidos") = 0 Then B4XPages.MainPage.skmt.ExecNonQuery("delete from pedido_cliente where PC_CLIENTE In (select cuenta from cuentaa)")
|
|
End If
|
|
Else
|
|
'INSERTAMOS
|
|
If cant <> 0 Then guardaProductoSinGestion(cedis, costoU, cant, nombre, prodId, clienteId, fecha, usuario, rutaV, precioSin, tipoVenta,cantc,bcajas)
|
|
End If
|
|
c.Close
|
|
End Sub
|
|
|
|
Sub guardaProductoSinGestion2(cedis As String, costoU As String, cant As String, nombre As String, prodId As String, clienteId As String, fecha As String, usuario As String, rutaV As String, precioSin As String, tipoVenta As String, cantc As String, bcajas As String)
|
|
' LogColor("guardaProducto: "&prodId&", cant="&cant, Colors.Magenta)
|
|
Private c As Cursor
|
|
B4XPages.MainPage.skmt.ExecNonQuery2("INSERT INTO PEDIDO (PE_CEDIS,PE_COSTO_TOT,PE_COSTOU,PE_CANT,PE_PRONOMBRE,PE_PROID,PE_CLIENTE,PE_FECHA,PE_USUARIO,PE_RUTA,PE_COSTO_SIN,PE_FOLIO,PE_ENVIO_OK,PE_CANTC,PE_BCAJAS) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,0,?,?) ", Array As Object (cedis, (cantc * costoU), costoU, cant, nombre, prodId, clienteId, fecha, usuario, rutaV, precioSin, tipoVenta,cantc,bcajas))
|
|
B4XPages.MainPage.skmt.ExecNonQuery2($"update ${Starter.tabla} set cat_gp_almacen = cat_gp_almacen - ? where cat_gp_id = ? "$, Array As Object(cant, prodId))
|
|
c=B4XPages.MainPage.skmt.ExecQuery("select sum(pe_costo_tot) as TOTAL_CLIE, SUM(PE_CANT) AS CANT_CLIE, SUM(PE_COSTO_SIN) AS TOTAL_CLIE_SIN FROM PEDIDO WHERE PE_CLIENTE IN (Select CUENTA from cuentaa)")
|
|
c.Position=0
|
|
B4XPages.MainPage.skmt.ExecNonQuery("delete from pedido_cliente where PC_CLIENTE In (select cuenta from cuentaa)")
|
|
B4XPages.MainPage.skmt.ExecNonQuery2("insert into pedido_cliente(PC_CLIENTE, PC_FECHA, PC_USER, PC_NOART, PC_MONTO,PC_LON, PC_LAT,PC_ALMACEN,PC_RUTA,PC_COSTO_SIN) VALUES (?,?,?,?,?,?,?,?,?,?)", Array As Object(clienteId, fecha, usuario, c.GetString("CANT_CLIE"), c.GetString("TOTAL_CLIE"), B4XPages.MainPage.lon_gps, B4XPages.MainPage.lat_gps, cedis, c.GetString("TOTAL_CLIE_SIN")))
|
|
End Sub
|
|
|
|
Sub actualizaProducto2(cedis As String, costoU As String, cant As Int, nombre As String, prodId As String, clienteId As String, fecha As String, usuario As String, rutaV As String, precioSin As String, tipoVenta As String, cantc As String, bcajas As String)
|
|
Private c As Cursor=B4XPages.MainPage.skmt.ExecQuery($"select * from pedido where pe_cedis = '${cedis}' and pe_proid = '${prodId}' and pe_cliente = '${clienteId}' AND PE_FOLIO = '${B4XPages.MainPage.tipo_venta}'"$)
|
|
' Log($"ROWCOUNT: ${c.RowCount}, ${cant}"$)
|
|
LogColor($"actualizaProducto, c=${clienteId}, p=${prodId}, nombre=${nombre}, cant=${cant}, cedis=${cedis}, tipo=${tipoVenta}"$, Colors.Magenta)
|
|
|
|
If c.RowCount > 0 Then
|
|
c.Position=0
|
|
Private antCant As Int = 0
|
|
If IsNumber(c.GetInt("PE_CANT")) Then antCant=c.GetInt("PE_CANT")
|
|
Private difCant As Int = cant - antCant
|
|
B4XPages.MainPage.skmt.ExecNonQuery($"update pedido set pe_cant = ${cant}, pe_costo_tot = ${(cantc*c.GetString("PE_COSTOU"))}, PE_CANTC = ${cantc}, PE_BCAJAS = ${bcajas} where pe_cedis = '${cedis}' and pe_proid = '${prodId}' and pe_cliente = '${clienteId}' AND PE_FOLIO = '${B4XPages.MainPage.tipo_venta}'"$)
|
|
B4XPages.MainPage.skmt.ExecNonQuery($"update ${Starter.tabla} set cat_gp_almacen = cat_gp_almacen - (${difCant}) where cat_gp_id = '${prodId}' "$)
|
|
' Log($"CANT=${cant}"$)
|
|
If cant = 0 Then
|
|
Log("BORRAMOS PROD")
|
|
B4XPages.MainPage.skmt.ExecNonQuery($"delete from pedido where pe_cedis = '${cedis}' and pe_proid = '${prodId}' and pe_cliente = '${clienteId}' AND PE_FOLIO = '${B4XPages.MainPage.tipo_venta}'"$)
|
|
Private pe As Cursor = B4XPages.MainPage.skmt.ExecQuery("select count(pe_cliente) as cuantosPedidos from pedido where pe_cliente In (select cuenta from cuentaa)")
|
|
pe.Position=0
|
|
If pe.GetString("cuantosPedidos") = 0 Then B4XPages.MainPage.skmt.ExecNonQuery("delete from pedido_cliente where PC_CLIENTE In (select cuenta from cuentaa)")
|
|
End If
|
|
Else
|
|
'INSERTAMOS
|
|
If cant <> 0 Then guardaProductoSinGestion2(cedis, costoU, cant, nombre, prodId, clienteId, fecha, usuario, rutaV, precioSin, tipoVenta,cantc,bcajas)
|
|
End If
|
|
c.Close
|
|
End Sub
|
|
|
|
Sub traeTotalCliente As Double
|
|
Private sumaTotal As Double
|
|
Private cursorprueba As Cursor = B4XPages.MainPage.skmt.ExecQuery("Select PE_COSTO_TOT from pedido where PE_CLIENTE <> 0 ")
|
|
For i= 0 To cursorprueba.RowCount -1
|
|
cursorprueba.Position = i
|
|
' LogColor(cursorprueba.GetString("PE_COSTO_TOT"),Colors.Red)
|
|
sumaTotal = sumaTotal + cursorprueba.GetString("PE_COSTO_TOT")
|
|
sumaTotal = NumberFormat2(sumaTotal, 0, 2, 2, False)
|
|
' Log(NumberFormat2(sumaTotal, 0, 2, 2, False))
|
|
Next
|
|
cursorprueba.Close
|
|
Return sumaTotal
|
|
End Sub
|
|
|
|
Sub traeTotalClienteabordo As Double
|
|
Private sumaTotal As Double
|
|
Private cursorprueba As Cursor = B4XPages.MainPage.skmt.ExecQuery2("Select PE_COSTO_TOT from pedido where PE_FOLIO = ? AND PE_CLIENTE = 0", Array As String ("ABORDO"))
|
|
For i= 0 To cursorprueba.RowCount -1
|
|
cursorprueba.Position = i
|
|
' LogColor(cursorprueba.GetString("PE_COSTO_TOT"),Colors.Red)
|
|
sumaTotal = sumaTotal + cursorprueba.GetString("PE_COSTO_TOT")
|
|
sumaTotal = NumberFormat2(sumaTotal, 0, 2, 2, False)
|
|
' Log(NumberFormat2(sumaTotal, 0, 2, 2, False))
|
|
Next
|
|
cursorprueba.Close
|
|
Return sumaTotal
|
|
End Sub
|
|
|
|
Sub traeTotalClientepreventa As Double
|
|
Private sumaTotal As Double
|
|
Private cursorprueba As Cursor = B4XPages.MainPage.skmt.ExecQuery2("Select PE_COSTO_TOT from pedido where PE_FOLIO = ? AND PE_CLIENTE <> 0", Array As String ("PREVENTA"))
|
|
For i= 0 To cursorprueba.RowCount -1
|
|
cursorprueba.Position = i
|
|
' LogColor(cursorprueba.GetString("PE_COSTO_TOT"),Colors.Red)
|
|
sumaTotal = sumaTotal + cursorprueba.GetString("PE_COSTO_TOT")
|
|
sumaTotal = NumberFormat2(sumaTotal, 0, 2, 2, False)
|
|
' Log(NumberFormat2(sumaTotal, 0, 2, 2, False))
|
|
Next
|
|
cursorprueba.Close
|
|
Return sumaTotal
|
|
End Sub
|
|
|
|
Sub traeTotalClientepreventaparacredito As Double
|
|
Private sumaTotal As Double
|
|
Private cursorprueba As Cursor = B4XPages.MainPage.skmt.ExecQuery2("Select PE_COSTO_TOT from pedido where PE_FOLIO = ? AND PE_CLIENTE <> 0 AND PE_CLIENTE IN (SELECT CUENTA FROM CUENTAA)", Array As String ("PREVENTA"))
|
|
For i= 0 To cursorprueba.RowCount -1
|
|
cursorprueba.Position = i
|
|
' LogColor(cursorprueba.GetString("PE_COSTO_TOT"),Colors.Red)
|
|
sumaTotal = sumaTotal + cursorprueba.GetString("PE_COSTO_TOT")
|
|
sumaTotal = NumberFormat2(sumaTotal, 0, 2, 2, False)
|
|
' Log(NumberFormat2(sumaTotal, 0, 2, 2, False))
|
|
Next
|
|
cursorprueba.Close
|
|
Return sumaTotal
|
|
End Sub
|
|
|
|
Sub traeTotalClienteventa As Double
|
|
Private sumaTotal As Double
|
|
Private cursorprueba As Cursor = B4XPages.MainPage.skmt.ExecQuery2("Select PE_COSTO_TOT from pedido where PE_FOLIO = ? AND PE_CLIENTE <> 0", Array As String ("VENTA"))
|
|
For i= 0 To cursorprueba.RowCount -1
|
|
cursorprueba.Position = i
|
|
' LogColor(cursorprueba.GetString("PE_COSTO_TOT"),Colors.Red)
|
|
sumaTotal = sumaTotal + cursorprueba.GetString("PE_COSTO_TOT")
|
|
sumaTotal = NumberFormat2(sumaTotal, 0, 2, 2, False)
|
|
' Log(NumberFormat2(sumaTotal, 0, 2, 2, False))
|
|
Next
|
|
cursorprueba.Close
|
|
Return sumaTotal
|
|
End Sub
|
|
|
|
'Regresa un mapa con la información de la promo.
|
|
'Regresa: {id, maxXcliente, maxRecurrente, maxPromos, historico,
|
|
' productos={idProducto={idProducto, preciosimptos, precio, almacen, tipo, piezas, usuario, fecha, regalo, clasif}} 'Mapa con los productos de la promo y los datos de cada uno.
|
|
' tipos={idProducto=tipo} 'Mapa con id y tipo del producto, 0 si es fijo y 1 si es variable.
|
|
' prodsFijos={idProducto,idProducto} 'Lista con los ids de los productos fijos.
|
|
' prodsVariables={idProducto,idProducto} 'Lista con los ids de los productos variables.
|
|
' resultado="OK" 'Ok si existe la promocion.
|
|
' prodsVariablesRequeridos=5} 'Cantidad de productos variables requeridos para la promoción.
|
|
Sub traePromo(promo As String, cliente As String) As Map
|
|
Private inicioContador As String = DateTime.Now
|
|
Private c As Cursor = B4XPages.MainPage.skmt.ExecQuery("Select * from promos_comp where cat_pa_id = '"& promo&"'") 'Obtenemos las el maximo de promocioones a otorgar.
|
|
Private siHistorico As String = 0
|
|
Private promoMap As Map
|
|
Private prodsFijos, prodsFijosPrecios, prodsFijosPiezas, prodsVariables, prodsVariables2, prodsVariablesPrecios, prodsVariablesPrecios2 As List
|
|
promoMap.Initialize
|
|
prodsFijos.Initialize
|
|
prodsFijosPrecios.Initialize
|
|
prodsFijosPiezas.Initialize
|
|
prodsVariables.Initialize
|
|
prodsVariables2.Initialize
|
|
prodsVariablesPrecios.Initialize
|
|
prodsVariablesPrecios2.Initialize
|
|
c.Position = 0
|
|
If c.RowCount > 0 Then promoMap = CreateMap("id":promo, "maxXcliente":c.GetString("CAT_PA_MAXPROMCLIE"), "maxRecurrente":c.GetString("CAT_PA_MAXPROMREC"), "maxPromos":c.GetString("CAT_PA_MAXPROM"))
|
|
c = B4XPages.MainPage.skmt.ExecQuery("Select count(*) as hist from HIST_PROMOS where HP_CLIENTE = '"& cliente & "' and HP_CODIGO_PROMOCION = '" & promo & "'") 'Revisamos si hay historico de la promoción.
|
|
c.Position = 0
|
|
If c.GetString("hist") > 0 Then siHistorico = 1
|
|
promoMap.Put("historico", siHistorico)
|
|
c = B4XPages.MainPage.skmt.ExecQuery("Select * from CAT_DETALLES_PAQ where CAT_DP_ID = '"& promo & "'") 'Obtenemos los detalles de la promoción.
|
|
c.Position = 0
|
|
If c.RowCount > 0 Then
|
|
Private prods, tipos As Map
|
|
prods.Initialize
|
|
tipos.Initialize
|
|
For i=0 To c.RowCount -1
|
|
c.Position=i
|
|
prods.Put(c.GetString("CAT_DP_IDPROD"), CreateMap("idProducto":c.GetString("CAT_DP_IDPROD"), "precioSimptos":c.GetString("CAT_DP_PRECIO_SIMPTOS"), "precio":c.GetString("CAT_DP_PRECIO"), "almacen":c.GetString("CAT_DP_ALMACEN"), "tipo":c.GetString("CAT_DP_TIPO"), "piezas":c.GetString("CAT_DP_PZAS"), "usuario":c.GetString("CAT_DP_USUARIO"), "regalo":c.GetString("CAT_DP_REGALO"), "clasif":c.GetString("CAT_DP_CLASIF")))
|
|
tipos.Put(c.GetString("CAT_DP_IDPROD"), c.GetString("CAT_DP_TIPO"))
|
|
If c.GetString("CAT_DP_TIPO") = "0" Then
|
|
prodsFijos.Add(c.GetString("CAT_DP_IDPROD"))
|
|
prodsFijosPrecios.Add(c.GetString("CAT_DP_PRECIO"))
|
|
prodsFijosPiezas.Add(c.GetString("CAT_DP_PZAS"))
|
|
End If
|
|
If c.GetString("CAT_DP_TIPO") = "1" Then
|
|
prodsVariables.Add(c.GetString("CAT_DP_IDPROD"))
|
|
prodsVariablesPrecios.Add(c.GetString("CAT_DP_PRECIO"))
|
|
End If
|
|
If c.GetString("CAT_DP_TIPO") = "2" Then
|
|
' LogColor(c.GetString("CAT_DP_IDPROD") & "|" & c.GetString("CAT_DP_TIPO"), Colors.Blue)
|
|
prodsVariables2.Add(c.GetString("CAT_DP_IDPROD"))
|
|
prodsVariablesPrecios2.Add(c.GetString("CAT_DP_PRECIO"))
|
|
End If
|
|
' Log($"id:${c.GetString("CAT_DP_IDPROD")}, tipo:${c.GetString("CAT_DP_TIPO")}"$)
|
|
' If prodsVariables2.Size > 0 Then LogColor(c.GetString("CAT_DP_ID") & "|" & prodsVariables2, Colors.red)
|
|
Next
|
|
promoMap.Put("productos", prods) 'Mapa con los productos de la promocion (id, precio, almacen, tipo, piezas, etc.)
|
|
promoMap.Put("tipos", tipos) 'Mapa con los productos de la promoción y su tipo (fijo o variable).
|
|
promoMap.Put("prodsFijos", prodsFijos) 'Lista de los productos fijos de la promoción.
|
|
promoMap.Put("prodsVariables", prodsVariables) 'Lista de los productos variables de la promoción.
|
|
promoMap.Put("prodsVariables2", prodsVariables2)
|
|
promoMap.Put("prodsFijosCant", prodsFijos.Size)
|
|
promoMap.Put("prodsFijosPrecios", prodsFijosPrecios)
|
|
promoMap.Put("prodsFijosPiezas", prodsFijosPiezas)
|
|
promoMap.Put("prodsVariablesCant", prodsVariables.Size)
|
|
promoMap.Put("prodsVariables2Cant", prodsVariables2.Size)
|
|
promoMap.Put("prodsVariablesPrecios", prodsVariablesPrecios)
|
|
promoMap.Put("prodsVariablesPrecios2", prodsVariablesPrecios2)
|
|
promoMap.Put("resultado", "ok")
|
|
Else
|
|
promoMap.Put("resultado", "No hay datos de la promoción.")
|
|
End If
|
|
c = B4XPages.MainPage.skmt.ExecQuery2($"Select CAT_GP_STS, CAT_GP_IMP1, CAT_GP_NOMBRE from ${Starter.tabla} where CAT_GP_ID = ?"$,Array As String(promo)) 'Obtenemos las piezas requeridas de productos variables para la promoción.
|
|
c.Position = 0
|
|
Private pvr As String = 0
|
|
Private pvr2 As String = 0
|
|
If c.RowCount > 0 Then
|
|
c.Position = 0
|
|
pvr = c.GetString("CAT_GP_STS")
|
|
pvr2 = c.GetString("CAT_GP_IMP1")
|
|
If pvr = Null Or pvr = "null" Then pvr = 0
|
|
If pvr2 = Null Or pvr2 = "null" Then pvr2 = 0
|
|
promoMap.Put("prodsVariablesRequeridos", pvr) 'Cantidad de productos variables requeridos para la promoción.
|
|
promoMap.Put("prodsVariables2Requeridos", pvr2)
|
|
promoMap.put("descripcion", c.GetString("CAT_GP_NOMBRE"))
|
|
End If
|
|
c.Close
|
|
' Log($"Inv variables: ${cuantosVariablesTengoBD(promo)}"$)
|
|
' Log($"Inv dispo: ${traemosInventarioDisponibleParaPromo(promo)}"$)
|
|
' LogColor($"Promo ${promo}: ${promoMap}"$, Colors.Green)
|
|
' LogColor("TIEMPO para traePromo -=" & promo & "=- : " & ((DateTime.Now-inicioContador)/1000), Colors.Red)
|
|
Return promoMap
|
|
End Sub
|
|
|
|
'Regresa un mapa con el inventario disponible por producto para la promoción (desde la base de datos).
|
|
Sub traemosInventarioDisponibleParaPromo(promo As String) As Map 'ignore
|
|
Private c As Cursor
|
|
Log("ESTAMOS VIENDO INVENTARIO")
|
|
c = B4XPages.MainPage.skmt.ExecQuery2($"SELECT CAT_GP_ID, CAT_GP_ALMACEN FROM ${Starter.tabla} WHERE CAT_GP_ID IN (select CAT_DP_IDPROD FROM CAT_DETALLES_PAQ WHERE CAT_DP_ID = ?)"$, Array As String(promo))
|
|
' Private prodInv As Map
|
|
' prodInv.Initialize
|
|
Private prods As Map
|
|
prods.Initialize
|
|
If c.RowCount > 0 Then
|
|
|
|
For i=0 To c.RowCount -1
|
|
c.Position=i
|
|
prods.Put(c.GetString("CAT_GP_ID"), c.GetString("CAT_GP_ALMACEN"))
|
|
' Log($"prod:${c.GetString("CAT_GP_ID")}, inventario:${c.GetString("CAT_GP_ALMACEN")}"$)
|
|
Next
|
|
' prodInv.Put("inventarios", prods)
|
|
End If
|
|
Return prods
|
|
End Sub
|
|
|
|
'Resta los productos fijos del inventario de la promoción (mapa) y regresa un mapa con el nuevo inventario.
|
|
'Hay que darle como parametro un mapa (traePromo(promo)) con toda la informacion de la promocion.
|
|
'Regresa en el mapa la llave "resultado" que nos da "ok" o "No hay suficiente producto para la promocion".
|
|
Sub restaFijosPromo(promoMap As Map) As Map 'ignore
|
|
Private thisLog As Boolean = True 'Si es verdadero, muestra los logs de este sub.
|
|
Private inventariosDisponiblesParaEstaPromo As Map = traemosInventarioDisponibleParaPromo(promoMap.Get("id")) 'Obtenemos un mapa con el inventario disponible para cada producto de la promocion desde la base de datos.
|
|
If thisLog Then LogColor("InvDisponible: " & inventariosDisponiblesParaEstaPromo, Colors.red)
|
|
If thisLog Then LogColor("Inventario inicial antes de FIJOS: "&inventariosDisponiblesParaEstaPromo, Colors.Gray) 'Inventario inicial.
|
|
inventariosDisponiblesParaEstaPromo.Put("resultado", "ko")
|
|
Private i As Int
|
|
Private prodsmap As Map = promoMap.Get("productos") 'Obtenemos un mapa con todos los productos de la promoción.
|
|
Private prodsFijos As List = promoMap.get("prodsFijos") 'Obtenemos una lista con los productos fijos de la promoción.
|
|
For p = 0 To prodsFijos.Size - 1
|
|
Private t As String = prodsFijos.Get(p) 'Obtenemos el Id de este producto desde la lista de productos fijos.
|
|
Private p2 As Map = prodsmap.Get(t) 'Obtenemos un mapa con los datos de este producto (id, precio, almacen, tipo, piezas, etc.)
|
|
If thisLog Then Log($"T: ${t}, prod ${p2.Get("idProducto")}, piezas: ${p2.Get("piezas")}"$) 'Producto y piezas requeridas
|
|
If thisLog Then Log("inventariosDisponiblesParaEstaPromo="&inventariosDisponiblesParaEstaPromo)
|
|
If inventariosDisponiblesParaEstaPromo.ContainsKey(t) Then 'Si el mapa del inventario contiene el id del producto entonces ...
|
|
i = inventariosDisponiblesParaEstaPromo.get(t) 'Obtenemos del mapa el inventario de este producto.
|
|
If thisLog Then Log($"Nuevo inventario de ${t}: ${i}-${promoMap.Get("prodsFijosPiezas").As(List).get(p)} = $1.0{i - promoMap.Get("prodsFijosPiezas").As(List).get(p)}"$) 'El nuevo inventario.
|
|
inventariosDisponiblesParaEstaPromo.Put(t, $"${i - promoMap.Get("prodsFijosPiezas").As(List).get(p)}"$) 'Restamos del inventario las piezas requeridas para la promoción y guardamos el nuevo inventario en el mapa.
|
|
inventariosDisponiblesParaEstaPromo.Put("resultado", "ok")
|
|
Else 'Si en el mapa no esta el id del producto, entonces no tenemos inventario.
|
|
inventariosDisponiblesParaEstaPromo.Put("resultado", "No hay suficiente producto para la promocion.")
|
|
LogColor("Sin suficiente inventario fijo: " & t, Colors.Blue)
|
|
Exit
|
|
End If
|
|
If i - p2.Get("piezas") < 0 Then
|
|
inventariosDisponiblesParaEstaPromo.Put("resultado", "No hay suficiente producto para la promocion.") 'Si el inventario de este producto sale negativo, quiere decir que no tenemos suficiente inventario para la promoción.
|
|
Exit
|
|
End If
|
|
Next
|
|
If prodsFijos.Size = 0 Then inventariosDisponiblesParaEstaPromo.Put("resultado", "ok") 'No hay productos fijos.
|
|
If thisLog Then LogColor("Inventario final depues de FIJOS: "&inventariosDisponiblesParaEstaPromo, Colors.blue) 'Inventario final.
|
|
Return inventariosDisponiblesParaEstaPromo
|
|
End Sub
|
|
|
|
'Revisa si tenemos los productos variables requeridos para la promoción (mapa).
|
|
'Hay que darle como parametro un mapa (traePromo(promo)) con toda la informacion de la promocion.
|
|
Sub alcanzanLosVariablesParaPromo(promoMap As Map, inventarioSinFijos As Map) As Boolean 'ignore
|
|
Private thisLog As Boolean = False 'Si es verdadero, muestra los logs de este sub.
|
|
If thisLog Then LogColor("Inventario inicial: "&inventarioSinFijos, Colors.Gray) 'Inventario inicial.
|
|
Private totalProdsVariables As Int = 0
|
|
Private totalProdsVariables2 As Int = 0
|
|
' Private prodsmap As Map = promoMap.Get("productos") 'Obtenemos un mapa con todos los productos de la promoción.
|
|
Private prodsVariables As List = promoMap.get("prodsVariables") 'Obtenemos una lista con los productos variables de la promoción.
|
|
Private prodsVariables2 As List = promoMap.get("prodsVariables2")
|
|
For p = 0 To prodsVariables.Size - 1
|
|
Private t As String = prodsVariables.Get(p) 'Obtenemos el Id de este producto desde la lista de productos fijos.
|
|
' Log("inventarioSinFijos: " & inventarioSinFijos)
|
|
If inventarioSinFijos.ContainsKey(t) Then 'Si existe el producto en la lista del inventario, entonces ...
|
|
Private p2 As String = inventarioSinFijos.Get(t) 'Obtenemos el inventario disponible este producto.
|
|
' Log(p2)
|
|
totalProdsVariables = totalProdsVariables + p2
|
|
' Log(totalProdsVariables)
|
|
If thisLog Then Log($"prod ${t}, hay: ${p2}"$) 'Producto y piezas requeridas
|
|
End If
|
|
Next
|
|
For p = 0 To prodsVariables2.Size - 1
|
|
Private t As String = prodsVariables2.Get(p) 'Obtenemos el Id de este producto desde la lista de productos fijos.
|
|
If inventarioSinFijos.ContainsKey(t) Then 'Si existe el producto en la lista del inventario, entonces ...
|
|
Private p2 As String = inventarioSinFijos.Get(t) 'Obtenemos el inventario disponible este producto.
|
|
totalProdsVariables2 = totalProdsVariables2 + p2
|
|
If thisLog Then Log($"prod ${t}, hay: ${p2}"$) 'Producto y piezas requeridas
|
|
End If
|
|
Next
|
|
If thisLog Then Log("Total prods variables=" & totalProdsVariables & ", requeridos=" & promoMap.Get("prodsVariablesRequeridos"))
|
|
If thisLog Then Log("Total prods variables2=" & totalProdsVariables2 & ", requeridos2=" & promoMap.Get("prodsVariables2Requeridos"))
|
|
Private res As Boolean = False
|
|
Private res1 As Boolean = False
|
|
Private res2 As Boolean = False
|
|
|
|
' Log($"${totalProdsVariables} >= ${promoMap.Get("prodsVariablesRequeridos")}"$)
|
|
' Log($"${totalProdsVariables2} >= ${promoMap.Get("prodsVariables2Requeridos")}"$)
|
|
|
|
If totalProdsVariables > 0 And totalProdsVariables >= promoMap.Get("prodsVariablesRequeridos") Then
|
|
res1 = True 'Si el total de inventario de productos variables (totalProdsVariables) es mayor o igual a los productos requeridos entonces regresamos TRUE
|
|
End If
|
|
If promoMap.Get("prodsVariablesRequeridos").As (Int) = 0 Then res1 = True
|
|
If totalProdsVariables2 > 0 And totalProdsVariables2 >= promoMap.Get("prodsVariables2Requeridos") Then
|
|
res2 = True 'Si el total de inventario de productos variables (totalProdsVariables) es mayor o igual a los productos requeridos entonces regresamos TRUE
|
|
End If
|
|
If promoMap.Get("prodsVariables2Requeridos").As(Int) = 0 Then res2 = True
|
|
If res1 And res2 Then 'Solo si son verdadero los dos el resultado es verdadero.
|
|
res = True
|
|
Else
|
|
res = False
|
|
End If
|
|
' Log(res)
|
|
Return res
|
|
End Sub
|
|
|
|
'Regresa el numero máximo de promociones permitidas, tomando en cuenta recurrentes, clientes y maxPromos.
|
|
Sub traeMaxPromos(pm As Map) As Int
|
|
Private thisLog As Boolean = False 'Si es verdadero, muestra los logs de este sub.
|
|
Private maxPromos As List
|
|
Private vendidas As Int = 0
|
|
maxPromos.Initialize
|
|
' If Starter.promosLog Then Log("==== HISTORICO:"&pm.Get("historico"))
|
|
If thisLog Then Log(pm)
|
|
If pm.Get("historico") = "1" Then maxPromos.Add(pm.Get("maxRecurrente")) 'Si hay historico, agregamos maxRecurrente
|
|
maxPromos.Add(pm.Get("maxPromos")) 'Agregamos maxPromos
|
|
maxPromos.Add(pm.Get("maxXcliente")) 'Agregamos maxXcliente
|
|
maxPromos.Sort(True)
|
|
|
|
' Log($"|${pm.Get("id").As(String).trim}|${traeCliente.Trim}|"$)
|
|
Private c As Cursor = B4XPages.MainPage.skmt.ExecQuery2("select sum(PE_CANT) as vendidas from PEDIDO where PE_PROID = ? and PE_CLIENTE = ? ", Array As String(pm.Get("id").As(String).trim, traeCliente.Trim))
|
|
If c.RowCount > 0 Then
|
|
c.Position = 0
|
|
vendidas = c.GetInt("vendidas")
|
|
' Log(vendidas)
|
|
End If
|
|
|
|
' If Starter.promosLog Then Log(maxPromos)
|
|
' If Starter.promosLog Then Log("Max Promos="&maxPromos.Get(0))
|
|
' LogColor($"maxPromos=${maxPromos.Get(0)} - vendidas=${vendidas}"$, Colors.red)
|
|
Return maxPromos.Get(0) - vendidas 'Regresamos el numero mas pequeño de las opciones.
|
|
End Sub
|
|
|
|
'Regresa la cantidad de promos que se le han vendido al cliente.
|
|
Sub traePromosVendidas(promo As String, cliente As String) As Int
|
|
Private c As Cursor
|
|
Private pv As Int = 0
|
|
c=B4XPages.MainPage.skmt.ExecQuery($"select PE_CANT from PEDIDO where PE_PROID = '${promo}' and PE_CLIENTE = '${cliente}'"$)
|
|
If c.RowCount > 0 Then
|
|
c.Position = 0
|
|
pv = c.GetInt("PE_CANT")
|
|
End If
|
|
Return pv
|
|
End Sub
|
|
|
|
Sub procesaPromocion(idPromo As String, cliente As String) As Map 'ignore
|
|
Private thisLog As Boolean = True 'Si es verdadero, muestra los logs de este sub.
|
|
Private inicioContador As String = DateTime.Now
|
|
If thisLog Then LogColor($"********* Iniciamos revision de Promo ${idPromo} *********"$, Colors.Magenta)
|
|
'Obtenemos el mapa con toda la info de la promoción.
|
|
Private pm As Map = traePromo(idPromo, cliente)
|
|
If thisLog Then LogColor(pm, Colors.Blue)
|
|
If pm.Get("resultado") = "ok" Then 'Si encontramos la promoción, entonces ...
|
|
'Buscamos el máximo de promociones permitidas.
|
|
If thisLog Then LogColor($"Promociones permitidas=${traeMaxPromos(pm)}"$, Colors.Blue)
|
|
If thisLog Then Log("Promos vendidas: " & traePromosVendidas(idPromo, cliente))
|
|
If traePromosVendidas(idPromo, cliente) >= traeMaxPromos(pm) Then
|
|
If thisLog Then LogColor("Ya se vendieron las promos PERMITIDAS para el cliente", Colors.red)
|
|
Return CreateMap("status":"ko", "mp":pm)
|
|
End If
|
|
'Restamos del inventario (mapa) las piezas necesarias para los productos fijos.
|
|
Private inventarioSinFijos As Map = restaFijosPromo(pm)
|
|
If thisLog Then LogColor("inventariosfijos="&inventarioSinFijos, Colors.Green)
|
|
|
|
If inventarioSinFijos.Get("resultado") = "ok" Then
|
|
'Revisamos que los productos variables requeridos sean menos que el inventario total (mapa).
|
|
Private pv As Boolean = alcanzanLosVariablesParaPromo(pm, inventarioSinFijos)
|
|
If thisLog Then Log("Alcanzan los variables? --> " & pv)
|
|
If pv Then Return CreateMap("status":"ok", "mp":pm) Else Return CreateMap("status":"ko", "mp":pm)
|
|
Else
|
|
If thisLog Then LogColor("NO HAY INVENTARIO SUFICIENTE " & idPromo, Colors.red)
|
|
Return CreateMap("status":"ko", "mp":pm)
|
|
End If
|
|
End If
|
|
' Si tenemos suficiente inventario para los variables mostramos la promocion, si no ...
|
|
' break 'NO HAY INVENTARIO SUFICIENTE PARA LA PROMOCION.
|
|
|
|
LogColor("TIEMPO DE PROCESO ESTA PROMO: " & ((DateTime.Now-inicioContador)/1000), Colors.Red)
|
|
End Sub
|
|
|
|
'Regresa cuantas promos alcanzan con los productos FIJOS que hay en inventario.
|
|
Sub revisaMaxPromosProdsFijosPorInventario2(pm As Map) As Int
|
|
Private thisLog As Boolean = False
|
|
If thisLog Then Log($"pm=${pm}"$)
|
|
' Private prodsFijos As List = pm.get("prodsFijos")
|
|
Private invDispParaPromo As Map = traemosInventarioDisponibleParaPromo(pm.Get("id"))
|
|
If thisLog Then Log($"invDispParaPromo=${invDispParaPromo}"$)
|
|
Private maxPromos As String = traeMaxPromos(pm)
|
|
Private maxPromosFijosXinv As Int = 1
|
|
Private fpf2, pdp2 As Int
|
|
Private salir As Boolean = False
|
|
Private pf As List = pm.Get("prodsFijos")
|
|
Private pfp As List = pm.Get("prodsFijosPiezas")
|
|
If thisLog Then Log($"maxPromos=${maxPromos}, prodsFijos=${pf}, piezas=${pfp}"$)
|
|
If thisLog Then LogColor($"InvFijo disponible=${invDispParaPromo}"$, Colors.Blue)
|
|
Private invFijoXpromo As Map
|
|
invFijoXpromo.Initialize
|
|
For p = 0 To pf.Size -1 'Generamos mapa con los productos fijo y piezas requeridos por promo.
|
|
invFijoXpromo.Put(pf.Get(p), pfp.Get(p))
|
|
Next
|
|
If thisLog Then LogColor("Inv req. de prods fijos x promo" & invFijoXpromo, Colors.Green)
|
|
For i = 1 To maxPromos 'Revisamos cuantas promociones alcanzan, hasta llegar al máximo de promos permitadas.
|
|
If thisLog Then LogColor("Prods para promo " & (i+1), Colors.Magenta)
|
|
For q = 0 To pf.Size - 1
|
|
Private q2 As String = pf.Get(q)
|
|
If thisLog Then Log("q="&q2)
|
|
' fpf2 = invFijoXpromo.Get(q2) * i 'Multiplicamos las piezas requeridas por la cantidad de promos.
|
|
fpf2 = pfp.Get(q) * i 'Multiplicamos las piezas requeridas por la cantidad de promos.
|
|
pdp2 = invDispParaPromo.Get(q2)
|
|
If thisLog Then Log($"pf=${q2}, Actual=${(i)}, max promos: ${pdp2}-${fpf2}=${pdp2 - fpf2}"$)
|
|
If pdp2 - fpf2 < 0 Then 'Si el inventario es negativo, entonces ya no alcanza para este producto.
|
|
salir=True
|
|
Exit
|
|
End If
|
|
Next
|
|
If salir Then Exit
|
|
maxPromosFijosXinv = i
|
|
Next
|
|
If thisLog Then LogColor("InvFijo requerido x promo="&invFijoXpromo, Colors.blue)
|
|
LogColor("Maximo de promociones de prodsFijos POR inventario = " & maxPromosFijosXinv, Colors.Red)
|
|
Return maxPromosFijosXinv
|
|
End Sub
|
|
|
|
'Regresa cuantas promos alcanzan con los productos FIJOS que hay en inventario.
|
|
Sub revisaMaxPromosProdsFijosPorInventario(pm As Map) As Int
|
|
Private thisLog As Boolean = False
|
|
Private invFijoXpromo As Map
|
|
Private t As List
|
|
t.Initialize
|
|
t.Add(traeMaxPromos(pm)) ' Agregamos a la lista las promos maximas permitidas (recurrente, cliente y promo).
|
|
invFijoXpromo.Initialize
|
|
If thisLog Then LogColor($"pm=${pm}"$, Colors.Blue)
|
|
Private invDispParaPromo As Map = traemosInventarioDisponibleParaPromo(pm.Get("id"))
|
|
If thisLog Then Log($"invDispParaPromo=${invDispParaPromo}"$)
|
|
Private prodsFijosPiezas As List = pm.Get("prodsFijosPiezas")
|
|
Private idProdsFijos As List = pm.Get("prodsFijos")
|
|
For p = 0 To idProdsFijos.Size -1 'Generamos una lista con las promos disponibles por producto (dividimos el inventario total entre las piezas requeridas).
|
|
If thisLog Then Log($"id=${idProdsFijos.Get(p)}, inv=${invDispParaPromo.Get(idProdsFijos.Get(p))}, pzas=${prodsFijosPiezas.Get(p)}"$)
|
|
If thisLog Then Log($"${(invDispParaPromo.Get(idProdsFijos.Get(p)) / prodsFijosPiezas.Get(p))}"$)
|
|
Private x() As String = Regex.Split("\.", $"${(invDispParaPromo.Get(idProdsFijos.Get(p)) / prodsFijosPiezas.Get(p))}"$) 'Separamos el resultado de la division por el punto decimal.
|
|
If thisLog Then Log(x(0))
|
|
t.Add(x(0).As(Int)) 'Solo guardamos la parte del entero de la division.
|
|
Next
|
|
t.Sort(True) 'Ordenamos la lista para que en el lugar 0 este el resultao mas pequeño.
|
|
If thisLog Then LogColor($"prodsFijos=${idProdsFijos}"$, Colors.Blue)
|
|
If thisLog Then LogColor($"prodsFijosPiezasReq=${prodsFijosPiezas}"$, Colors.Blue)
|
|
If thisLog Then LogColor($"invFijoXpromo=${invFijoXpromo}"$, Colors.Blue)
|
|
' LogColor("Max promos de prodsFijos POR inventario = " & t.Get(0), Colors.red)
|
|
Return t.Get(0) 'Regresamos el resultado mas pequeño.
|
|
End Sub
|
|
|
|
'Regresa cuantas promos alcanzan con los productos VARIABLES que hay en inventario.
|
|
'La cantidad de promos disponibles se calcula DESPUES de descontar los productos fijos, y si las
|
|
'promos por productos fijos llega al maximo, aunque se puedan mas de producos variables, solo se
|
|
'regresa el maximo por productos fijos. Ej. si las promos por variables es 10, pero el maximo por
|
|
'fijos es 5, entonces regresamos 5.
|
|
Sub revisaMaxPromosProdsVariablesPorInventario(pm As Map) As Int 'ignore
|
|
Private thisLog As Boolean = False
|
|
If thisLog Then Log("======================================================")
|
|
If thisLog Then Log("======================================================")
|
|
Private invFijoXpromo As Map
|
|
invFijoXpromo.Initialize
|
|
Private totalProdsVariablesDisponibles As Int = 0
|
|
Private totalProdsVariables2Disponibles As Int = 0
|
|
If thisLog Then LogColor($"pm=${pm}"$, Colors.Blue)
|
|
Private invDispParaPromo As Map = traemosInventarioDisponibleParaPromo(pm.Get("id"))
|
|
If thisLog Then Log($"invDispParaPromo=${invDispParaPromo}"$)
|
|
Private maxPromos As String = traeMaxPromos(pm)
|
|
Private maxPromosXFijos As Int = revisaMaxPromosProdsFijosPorInventario(pm)
|
|
Private idProdsVariables As List = pm.Get("prodsVariables")
|
|
Private idProdsVariables2 As List = pm.Get("prodsVariables2")
|
|
Private prodsVariablesRequeridos As Int = pm.Get("prodsVariablesRequeridos")
|
|
Private prodsVariables2Requeridos As Int = pm.Get("prodsVariables2Requeridos")
|
|
Private prodsFijosPiezas As List = pm.Get("prodsFijosPiezas")
|
|
Private idProdsFijos As List = pm.Get("prodsFijos")
|
|
For p = 0 To idProdsFijos.Size -1 'Generamos mapa con los productos fijos y piezas requeridas por promo.
|
|
invFijoXpromo.Put(idProdsFijos.Get(p), prodsFijosPiezas.Get(p))
|
|
Private idEsteProd As String = idProdsFijos.Get(p)
|
|
Private invEsteProd As Int = invDispParaPromo.Get(idEsteProd)
|
|
Private pzasReqEsteProd As Int = prodsFijosPiezas.Get(p)
|
|
If thisLog Then Log($"id=${idEsteProd}, inv=${invEsteProd}, pzas=${pzasReqEsteProd}"$)
|
|
' invDispParaPromo.Put( idEsteProd, (invEsteProd - (1)) )
|
|
Next
|
|
If thisLog Then LogColor($"MaxPromos=${maxPromos}, promosXFijos=${maxPromosXFijos}"$, Colors.Blue)
|
|
If thisLog Then LogColor($"prodsFijos=${idProdsFijos}"$, Colors.Blue)
|
|
If thisLog Then LogColor($"prodsFijosPiezasReq=${prodsFijosPiezas}"$, Colors.Blue)
|
|
If thisLog Then LogColor($"prodsVariables=${idProdsVariables}${CRLF}Variables Req=${prodsVariablesRequeridos} "$, Colors.Blue)
|
|
If thisLog Then LogColor($"prodsVariables2=${idProdsVariables2}${CRLF}Variables2 Req=${prodsVariables2Requeridos} "$, Colors.Blue)
|
|
If thisLog Then LogColor($"invFijoXpromo=${invFijoXpromo}"$, Colors.Blue)
|
|
If thisLog Then Log($"Prods variables disponibles = ${totalProdsVariablesDisponibles}"$)
|
|
If thisLog Then Log($"Prods variables2 disponibles = ${totalProdsVariables2Disponibles}"$)
|
|
Private maxPromosXVariables As Int = 0
|
|
Private maxPromosXVariables2 As Int = 0
|
|
For x = 1 To maxPromosXFijos
|
|
If thisLog Then Log("=====================================================")
|
|
If thisLog Then Log("=====================================================")
|
|
For i = 0 To idProdsFijos.Size - 1
|
|
If thisLog Then Log($"FIJO - ${idProdsFijos.Get(i)}, ${invDispParaPromo.Get(idProdsFijos.Get(i))} - ${prodsFijosPiezas.Get(i).As(Int)*(i+1)}"$)
|
|
invDispParaPromo.Put(idProdsFijos.Get(i), invDispParaPromo.Get(idProdsFijos.Get(i)).As(Int) - prodsFijosPiezas.Get(i).As(Int)*(i+1)) 'Restamos las piezas de los productos fijos del inventario disponible.
|
|
Next
|
|
If thisLog Then LogColor("Inv disponible despues de restar fijos = " & invDispParaPromo, Colors.Blue)
|
|
|
|
totalProdsVariablesDisponibles = 0
|
|
totalProdsVariables2Disponibles = 0
|
|
For i = 0 To idProdsVariables.Size - 1 'Obtenemos total de productos variables disponibes.
|
|
If invDispParaPromo.ContainsKey(idProdsVariables.Get(i)) Then
|
|
totalProdsVariablesDisponibles = totalProdsVariablesDisponibles + invDispParaPromo.Get(idProdsVariables.Get(i))
|
|
End If
|
|
Next
|
|
For i = 0 To idProdsVariables2.Size - 1 'Obtenemos total de productos variables disponibes.
|
|
If invDispParaPromo.ContainsKey(idProdsVariables2.Get(i)) Then
|
|
totalProdsVariables2Disponibles = totalProdsVariables2Disponibles + invDispParaPromo.Get(idProdsVariables2.Get(i))
|
|
End If
|
|
Next
|
|
'Revisamos variables.
|
|
If thisLog Then Log($"Var disponibles - var requeridos : ${totalProdsVariablesDisponibles} - ${prodsVariablesRequeridos*x}"$)
|
|
totalProdsVariablesDisponibles = totalProdsVariablesDisponibles - (prodsVariablesRequeridos*x)
|
|
totalProdsVariables2Disponibles = totalProdsVariables2Disponibles - (prodsVariables2Requeridos*x)
|
|
If thisLog Then Log("prodsVariables disponibles despues de promo = " & totalProdsVariablesDisponibles)
|
|
If thisLog Then Log("prodsVariables2 disponibles despues de promo = " & totalProdsVariables2Disponibles)
|
|
If totalProdsVariablesDisponibles < 0 Then Exit 'Ya no hay inventario disponible.
|
|
If totalProdsVariables2Disponibles < 0 Then Exit 'Ya no hay inventario disponible.
|
|
maxPromosXVariables = x
|
|
Next
|
|
'Restamos fijos.
|
|
If thisLog Then LogColor("Max promos de prodsVariables POR inventario = " & maxPromosXVariables, Colors.red)
|
|
Return maxPromosXVariables
|
|
End Sub
|
|
|
|
'Regresa la suma del inventario de los productos variables de la promoción dada desde la base de datos.
|
|
Sub cuantosVariablesTengoBD(promo As String) As String 'ignore
|
|
' Private x As String = "0"
|
|
' If promo <> "" Then
|
|
' Private c As Cursor
|
|
' c = Starter.skmt.ExecQuery2("Select SUM(CAT_GP_ALMACEN) as variables FROM CAT_GUNAPROD2 WHERE CAT_GP_ID IN (Select CAT_DP_IDPROD FROM CAT_DETALLES_PAQ WHERE CAT_DP_ID = ? and cat_dp_tipo = 1 GROUP BY CAT_DP_IDPROD)", Array As String (promo))
|
|
' If c.RowCount > -1 Then
|
|
' c.Position = 0
|
|
' If c.GetString("variables") <> Null Then x = c.GetString("variables")
|
|
' End If
|
|
' End If
|
|
' Return x
|
|
End Sub
|
|
|
|
'Regresa un mapa con los datos del producto desde la base de datos.
|
|
'el mapa incluye: Id, nombre, tipo y subtipo del producto.
|
|
Sub traeProdIdDeBD As Map 'ignore
|
|
Private c As Cursor
|
|
Private m As Map
|
|
c=B4XPages.MainPage.skmt.ExecQuery($"select CAT_GP_ID,CAT_GP_NOMBRE,CAT_GP_TIPO,CAT_GP_SUBTIPO from ${Starter.tabla} where CAT_GP_NOMBRE In (Select PDESC from PROID)"$)
|
|
If c.RowCount > 0 Then
|
|
c.Position = 0
|
|
m = CreateMap("id":c.GetString("CAT_GP_ID"), "nombre":c.GetString("CAT_GP_NOMBRE"), "tipo":c.GetString("CAT_GP_TIPO"), "subtipo":c.GetString("CAT_GP_SUBTIPO"))
|
|
Else
|
|
m = CreateMap("id":"N/A", "nombre":"N/A", "tipo":"N/A", "subtipo":"N/A")
|
|
End If
|
|
c.Close
|
|
Return m
|
|
End Sub
|
|
|
|
'Guarda en la base de datos la hora inicial y final de la vista.
|
|
Sub guardaClienteHoraInicio(cliente As String)
|
|
B4XPages.MainPage.skmt.ExecNonQuery2("insert into PEDIDO_INICIO_FINAL(PIF_CLIENTE, PIF_HORA_INICIO, PIF_HORA_FINAL) VALUES(?,?,?) ", Array As Object (cliente, DateTime.Now, 0))
|
|
' LogColor($"insertamos ${cliente}, hora_inicio=${DateTime.Now}, hora_final=0"$,Colors.Red)
|
|
End Sub
|
|
|
|
'Actualizamos el tiempo que el vendedor estuvo en la tienda, desde que entra a la pantalla del cliente hasta que hace clic en "Guardar".
|
|
Sub actualizaTET(cliente As String)
|
|
Dim c As Cursor = B4XPages.MainPage.skmt.Execquery2("select * from PEDIDO_INICIO_FINAL where PIF_CLIENTE = ?", Array As String(cliente))
|
|
Dim total As Long = 0
|
|
If c.RowCount > 0 Then
|
|
c.Position=0
|
|
For i = 0 To c.RowCount-1
|
|
c.Position=i
|
|
' LogColor($"cliente=${c.GetString("PIF_CLIENTE")}, inicio=${c.GetString("PIF_HORA_INICIO")}, final=${c.GetString("PIF_HORA_FINAL")}"$, Colors.Magenta)
|
|
Dim subtotal As Long = c.GetString("PIF_HORA_FINAL") - c.GetString("PIF_HORA_INICIO")
|
|
total = total + subtotal
|
|
' Log($" Subtotal=${subtotal}, total=${total}"$)
|
|
Next
|
|
' Log($"Total=${(total/1000)/60}"$)
|
|
B4XPages.MainPage.skmt.ExecNonQuery2("update PEDIDO_CLIENTE set PC_TIEMPO_TIENDA = ? where PC_CLIENTE = ?", Array As String(((total/1000)/60), cliente))
|
|
End If
|
|
End Sub
|
|
|
|
'Regresa el total de productos y monto del pedido del cliente actual.
|
|
Sub traeTotalesClienteActual As Map
|
|
Private m As Map
|
|
m.Initialize
|
|
Private c_prodsX As Cursor=B4XPages.MainPage.skmt.ExecQuery("select ifnull(sum(PE_CANT), 0) as cantProds, ifnull(sum(PE_COSTO_TOT), 0) as costoTotal FROM PEDIDO WHERE PE_CLIENTE IN (Select CUENTA from cuentaa) order by PE_PRONOMBRE asc")
|
|
c_prodsX.Position=0
|
|
' LogColor($"Productos de la orden: ${c_prodsX.GetString("cantProds")}, Total: ${c_prodsX.GetString("costoTotal")}"$, Colors.red)
|
|
m = CreateMap("productos": c_prodsX.GetString("cantProds"), "monto" : c_prodsX.GetString("costoTotal"))
|
|
Return m
|
|
End Sub
|
|
|
|
'Borra el pedido del cliente actual.
|
|
'Borra los registros de la tabla "pedido" y "pedido_cliente"
|
|
'Actualiza las tablas "cat_gunaprod" y "kmt_info".
|
|
Sub borraPedidoClienteActual As String
|
|
' Private thisC As Cursor
|
|
' thisC=B4XPages.MainPage.skmt.ExecQuery("select PE_PROID,PE_CANT FROM PEDIDO where pe_cliente in (Select CUENTA from cuentaa) ")
|
|
' If thisC.RowCount>0 Then
|
|
' For i=0 To thisC.RowCount -1
|
|
' thisC.Position=i
|
|
' B4XPages.MainPage.skmt.ExecNonQuery2($"update ${Starter.tabla} set cat_gp_almacen = cat_gp_almacen + ? where cat_gp_id = ?"$, Array As Object(thisC.GetString("PE_CANT"),thisC.GetString("PE_PROID")))
|
|
' B4XPages.MainPage.skmt.ExecNonQuery2("INSERT INTO INVENT_X_ENVIAR (ALMACEN , PROID , CANTIDAD ) VALUES(?,?,?) ", Array As Object (traeAlmacen, thisC.GetString("PE_PROID"),thisC.GetString("PE_CANT")* -1))
|
|
' Next
|
|
' End If
|
|
' B4XPages.MainPage.skmt.ExecNonQuery("delete from pedido_cliente where pc_cliente in (Select CUENTA from cuentaa)")
|
|
' B4XPages.MainPage.skmt.ExecNonQuery("delete from pedido where pe_cliente in (Select CUENTA from cuentaa)")
|
|
' B4XPages.MainPage.skmt.ExecNonQuery("UPDATE kmt_info set gestion = 0 where CAT_CL_CODIGO In (select cuenta from cuentaa)")
|
|
' Return 1
|
|
Private thisC As Cursor
|
|
' Private tablaProds As String = "cat_gunaprod2"
|
|
thisC=Starter.skmt.ExecQuery("select PE_PROID, PE_CANT, PE_FOLIO FROM PEDIDO where pe_cliente in (Select CUENTA from cuentaa) ")
|
|
If thisC.RowCount>0 Then
|
|
For i=0 To thisC.RowCount -1
|
|
thisC.Position = i
|
|
' Log(thisC.GetString("PE_TIPO") & "|" & traeTablaProds(thisC.GetString("PE_TIPO")))
|
|
Starter.skmt.ExecNonQuery($"update ${traeTablaProds(thisC.GetString("PE_FOLIO"))} set cat_gp_almacen = cat_gp_almacen + ${thisC.GetString("PE_CANT")} where cat_gp_id = '${thisC.GetString("PE_PROID")}'"$)
|
|
LogColor($"update ${traeTablaProds(thisC.GetString("PE_FOLIO"))} set cat_gp_almacen = cat_gp_almacen + ${thisC.GetString("PE_CANT")} where cat_gp_id = '${thisC.GetString("PE_PROID")}'"$, Colors.red)
|
|
' Starter.skmt.ExecNonQuery2("INSERT INTO INVENT_X_ENVIAR (ALMACEN , PROID , CANTIDAD ) VALUES(?,?,?) ", Array As Object (traeAlmacen, thisC.GetString("PE_PROID"),thisC.GetString("PE_CANT")* -1))
|
|
Next
|
|
End If
|
|
Starter.skmt.ExecNonQuery("delete from pedido_cliente where pc_cliente in (Select CUENTA from cuentaa)")
|
|
Starter.skmt.ExecNonQuery("delete from pedido where pe_cliente in (Select CUENTA from cuentaa)")
|
|
Starter.skmt.ExecNonQuery("UPDATE kmt_info set gestion = 0 where CAT_CL_CODIGO In (select cuenta from cuentaa)")
|
|
Return 1
|
|
End Sub
|
|
|
|
Sub traeTablaProds(tipoventa As String) As String
|
|
Private tablaProds As String = "cat_gunaprod2"
|
|
If tipoventa = "ABORDO" Or tipoventa = "PREVENTA" Or tipoventa = "RECARGA" Then tablaProds = "cat_gunaprod"
|
|
' LogColor($"Tipo= ${tipoventa}, tabla=${tablaProds}"$, Colors.RGB(200,136,0))
|
|
Return tablaProds
|
|
End Sub
|
|
|
|
'Regresa verdadero si la columna gestion en la tabla "kmt_info" tene valor 2.
|
|
'si no, entonces regresa falso.
|
|
Sub pedidoGuardado As Boolean
|
|
Private guardado As Boolean = False
|
|
Private g As Cursor = B4XPages.MainPage.skmt.ExecQuery("select gestion from kmt_info where CAT_CL_CODIGO in (Select CUENTA from cuentaa)")
|
|
If g.RowCount > 0 Then
|
|
g.Position=0
|
|
If g.GetString("gestion") = "2" Or g.GetString("gestion") = "3" Then guardado = True
|
|
End If
|
|
' Log($"Guardado=${guardado}"$)
|
|
Return guardado
|
|
End Sub
|
|
|
|
'Regresa verdadero si hay pedido en la tabla "PEDIDO" del cliente actual.
|
|
Sub hayPedido As Boolean
|
|
Private thisC As Cursor=B4XPages.MainPage.skmt.ExecQuery($"select count(PE_CLIENTE) as hayPedido from PEDIDO where PE_CLIENTE = '${traeCliente}'"$)
|
|
thisC.Position=0
|
|
Private hay As Boolean = False
|
|
If thisC.GetInt("hayPedido") > 0 Then hay = True
|
|
' Log($"Cliente actual=${traeCliente}, hayPedido=${hay}"$)
|
|
Return hay
|
|
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)
|
|
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
|
|
|
|
'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
|
|
|
|
'Guarda el nombre y version de la app en CAT_VARIABLES.
|
|
Sub guardaAppInfo(skmt As SQL)
|
|
skmt.ExecNonQuery("delete from CAT_VARIABLES where CAT_VA_DESCRIPCION = 'EMPRESA' or CAT_VA_DESCRIPCION = 'APP_NAME' or CAT_VA_DESCRIPCION = 'APP_VERSION'")
|
|
skmt.ExecNonQuery($"insert into CAT_VARIABLES (CAT_VA_DESCRIPCION, CAT_VA_VALOR) values ('APP_NAME', '${Application.LabelName}')"$)
|
|
skmt.ExecNonQuery($"insert into CAT_VARIABLES (CAT_VA_DESCRIPCION, CAT_VA_VALOR) values ('APP_VERSION', '${Application.VersionName}')"$)
|
|
End Sub
|
|
|
|
Sub traeinventario(id As String) As String
|
|
Dim c As Cursor
|
|
Dim inventario As String = "0"
|
|
c=B4XPages.MainPage.skmt.ExecQuery($"select CAT_GP_ALMACEN from ${Starter.tabla} where CAT_GP_ID = '${id}'"$)
|
|
' Log($"select CAT_GP_ALMACEN from ${Starter.tabla} where CAT_GP_NOMBRE = '${id}'"$)
|
|
If c.RowCount > 0 Then
|
|
c.Position = 0
|
|
inventario = c.GetString("CAT_GP_ALMACEN")
|
|
End If
|
|
c.Close
|
|
Return inventario
|
|
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 traeRutaBitacora As String 'ignore
|
|
Private c As Cursor
|
|
Private r As String
|
|
c=B4XPages.MainPage.skmt.ExecQuery("select CAT_CL_RUTA from kmt_info WHERE CAT_CL_CODIGO IN (SELECT CUENTA FROM CUENTAA)")
|
|
r = "0"
|
|
If c.RowCount > 0 Then
|
|
c.Position=0
|
|
r = c.GetString("CAT_CL_RUTA")
|
|
End If
|
|
c.Close
|
|
Return r
|
|
End Sub
|
|
|
|
' Se revisa si hay una intención (intent) de abrir una base de datos y si es así, entonces se importa esa base de datos.
|
|
Sub importaBDDesdeWhatsApp
|
|
' Private tmpBDWA As Boolean = traeUsarIntentBDWA
|
|
Log("Revisamos intent de importar desde whatsapp")
|
|
Log(B4XPages.MainPage.intentUsado)
|
|
Log(in)
|
|
If Not(in.IsInitialized) Then in = B4XPages.GetNativeParent(B4XPages.MainPage).GetStartingIntent ' Si se usa esta funcion en Mainpage, se pone "Me" en lugar de B4XPages.MainPage.
|
|
If Not(B4XPages.MainPage.intentUsado) And in <> Null Then
|
|
' Log(in)
|
|
LogColor("Importamos base de datos desde Whatsapp.", Colors.blue)
|
|
B4XPages.MainPage.intentUsado = True
|
|
' Log(in.As(String))
|
|
If in.GetData <> Null Then
|
|
Dim XmlData As String
|
|
XmlData = in.GetData
|
|
Try
|
|
Dim OutStr As OutputStream = File.OpenOutput(File.DirInternal,"kmt.db",False)
|
|
Dim InStr As InputStream = File.OpenInput("ContentDir",XmlData)
|
|
File.Copy2(InStr,OutStr)
|
|
LogColor("BD copiada a interna.", Colors.Blue)
|
|
OutStr.Close
|
|
If in.As(String).Contains("whatsapp") Then ToastMessageShow("BD cargada desde Whatsapp", False)
|
|
Catch
|
|
Log(LastException)
|
|
End Try
|
|
' ExitApplication
|
|
' Starter.skmt.ExecNonQuery("delete from CAT_VARIABLES where CAT_VA_DESCRIPCION = 'IMPORTAR_BD_WA'")
|
|
' Starter.skmt.ExecNonQuery($"insert into CAT_VARIABLES (CAT_VA_DESCRIPCION, CAT_VA_VALOR) values ('IMPORTAR_BD_WA', '${tmpBDWA}')"$)
|
|
Private a As Cursor = Starter.skmt.ExecQuery($"select CAT_VA_VALOR from CAT_VARIABLES where CAT_VA_DESCRIPCION = 'APP_NAME'"$)
|
|
If a.RowCount > 0 Then
|
|
a.Position = 0
|
|
ToastMessageShow($"BD de "${a.GetString("CAT_VA_VALOR")}" cargada."$, True)
|
|
End If
|
|
a = Starter.skmt.ExecQuery($"select * from usuarioa"$)
|
|
If a.RowCount > 0 Then
|
|
a.Position = 0
|
|
B4XPages.MainPage.user.Text = a.GetString("USUARIO")
|
|
B4XPages.MainPage.pass.Text = a.GetString("PASS")
|
|
End If
|
|
End If
|
|
End If
|
|
End Sub
|
|
|
|
'Regresa si se debe de usar el intent de importar la base d datos desde Whatsapp.
|
|
Sub traeUsarIntentBDWA As Boolean 'ignore
|
|
Private BDWA As Boolean = False
|
|
Private x As Cursor = Starter.skmt.ExecQuery($"select CAT_VA_VALOR from CAT_VARIABLES where CAT_VA_DESCRIPCION = 'IMPORTAR_BD_WA'"$)
|
|
If x.RowCount > 0 Then
|
|
x.Position = 0
|
|
If x.GetString("CAT_VA_VALOR") = "true" Then BDWA = True
|
|
End If
|
|
' Log($"cb_importarBDWA = ${BDWA}"$)
|
|
Return BDWA
|
|
End Sub
|
|
|
|
' Función de ayuda para crear el objeto MultipartFileData de forma más limpia
|
|
Sub CreateMultipartFileData(Dir As String, FileName As String, KeyName As String, ContentType As String) As MultipartFileData
|
|
Dim mfd As MultipartFileData
|
|
mfd.Initialize
|
|
mfd.Dir = Dir
|
|
mfd.FileName = FileName
|
|
mfd.KeyName = KeyName
|
|
mfd.ContentType = ContentType
|
|
Return mfd
|
|
End Sub |