B4A=true
Group=Default Group
ModulesStructureVersion=1
Type=Class
Version=12.2
@EndOfDesignText@
Sub Class_Globals
	Private Root As B4XView 'ignore
	Private xui As XUI 'ignore
	Dim reqManager As DBRequestManager
	Dim conectado As String
	Dim t1 As Timer
	Dim sb As StringBuilder
	Dim PHONE As Phone
	Dim trabajar As Button
	Dim c As Cursor
	Dim d As Cursor
	Dim b As Cursor
	Dim e As Cursor
	Dim f As Cursor
	Dim f1 As Cursor
	Dim f2 As Cursor
	Dim f3 As Cursor
	Dim F4 As Cursor
	Dim lv As ListView
	Dim cargar As Button
	Dim pendiente As Button
	Dim foto1() As Byte
	Dim usuario As String
	Dim connecta As Button
	Dim conn As String
	Dim Subir As Button
	Dim cmd As DBCommand
	Dim TOPE As Int
	Dim COUNT_CLIE As Int
	Dim fecha As String
	Dim drop As Double
	Dim efectiva As Double
	Dim L As Cursor
	Dim l_aLiquidar As Label
	Dim l_cuantosc As Label
	Dim l_montoSalida As Label
'	Dim l_efectiva As Label
	Dim l_ctast As Label
	Dim BUSCA As Button
	Dim Cuantos As String
	Dim l_cuantosn As Label
	Dim e_ruta As EditText
	Dim l_porvisitar As Label
	Dim b_mapa As Button
	Dim l_ruta As Label
	Dim ALMACEN As String
	Private img2 As ImageView
	Dim Listo1 As String
	Dim Listo2 As String
	Dim Listo3 As String
	Dim Listo4 As String
	Dim Listo5 As String
	Dim EJECUTANDO As String
	Dim RES As String
	Dim inve As String
	Dim cuantos_pedidosc As String
	Dim cuantos_pedido As String
	Dim cuantos_noventa As String
	Dim datos_iguales As String
	Dim p As PhoneId
	Dim imei As String
	
	Private inv As Button
'	Private cambios As Label
	Private L_MONTOE As Label
	Private B_CLIEPROD As Button
	Private ListView1 As ListView
	Private B_REGRESA As Button
	Private Panel1 As Panel
	Dim ESTATUS As String
	Dim batterystatus(11) As Int
	Private bu As BatteryUtilities
	Dim RUTA_T As String
	Dim reqs As List
	'''''' MAPA RUTAS
	Private Btn_Ubicar_Ru As Button
	Dim cargo As String
	Private p_principal As Panel
	Dim ime As IME
	Private b_tabulador As Button
	
	Private l_rutasP As Label
	Private l_rechazo As Label
	Private Label9 As Label
	Dim cp As Cursor
	Private b_check As Button
End Sub

'You can add more parameters here.
Public Sub Initialize As Object
	Return Me
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
	Root = Root1
	'load the layout to Root
	Root.LoadLayout("principal")
	EJECUTANDO=1
	reqManager.Initialize(Me, B4XPages.MainPage.server)
'	LogColor($"ReqServer = ${B4XPages.MainPage.server}"$, Colors.red)
'	Dim ruta As String
	img2.Visible =False

	DateTime.DateFormat = "MM/dd/yyyy"
	fecha=DateTime.Date(DateTime.Now)
'	skmt.Initialize(Starter.ruta,"kmt.db", True)
	b=Starter.skmt.ExecQuery("Select count(*) as CUANTOS from pedido_cliente")
	b.Position=0
	Cuantos = b.GetString("CUANTOS")
	d=Starter.skmt.ExecQuery("select count(*) as TOTAL_VISITAR from kmt_info where gestion = 0")
	D.Position=0
	b.Close
	e=Starter.skmt.ExecQuery("select count(*) as POR_VISITAR from kmt_info")
	e.Position=0
	If Cuantos = 0 Then
		l_aLiquidar.Text = 0
		l_cuantosc.Text = 0
		l_cuantosn.Text = 0
		drop = 0
		l_montoSalida.Text = 0
		efectiva = 0
'		l_efectiva.text = 0
		l_ctast.Text = d.GetString("TOTAL_VISITAR")
		l_porvisitar.Text = e.GetString("POR_VISITAR")
	Else
'		c=Starter.skmt.ExecQuery("select sum(pc_monto) as MONTO_DIA, count(pc_cliente) AS CLIENTES_DIA from pedido_cliente")
'		b=Starter.skmt.ExecQuery("select count(*) as CUANTOS from noventa")
'		'where pc_fecha = ?", Array As String(fecha)
'		C.Position=0
'		b.Position=0
'		l_aLiquidar.Text = c.GetString("MONTO_DIA")
'		l_cuantosc.Text = c.GetString("CLIENTES_DIA")
'		l_cuantosn.Text = b.GetString("CUANTOS")
''		drop = c.GetString("MONTO_DIA") / c.GetString("CLIENTES_DIA")
		''		l_montoSalida.Text = Round2(drop,2)
''		efectiva = c.GetString("CLIENTES_DIA") / e.GetString("POR_VISITAR")
''		l_efectiva.text = Round2(efectiva * 100, 2)
'		l_ctast.Text = d.GetString("TOTAL_VISITAR")
'		l_porvisitar.Text = e.GetString("POR_VISITAR")
'		b.Close
'		c.Close
	End If
	b=Starter.skmt.ExecQuery("select id_almacen from CAT_ALMACEN")
	If b.RowCount > 0 Then
'		Log("Traemos almacen ..." & B4XPages.MainPage.server)
		b.Position = 0
'		Log(b.GetString("ID_ALMACEN"))
		Dim cmd As DBCommand
		cmd.Initialize
		cmd.Name = "select_coords_almacen_marquez"
		cmd.Parameters = Array As Object(b.GetString("ID_ALMACEN"))
		reqManager.ExecuteQuery(cmd , 0, "coords_almacen")
	End If
	B4XPages.MainPage.usuario=Subs.dameUsuarioDeDB
End Sub

Sub B4XPage_Appear
	b_check.visible = False
	Subs.centraPanel(p_principal, Root.Width)
'	c=Starter.skmt.ExecQuery("select noversion from version")
'	c.Position=0
'	If c.RowCount > 0 And c.GetString("NOVERSION") <> "2.1" Then
'		ToastMessageShow("EXISTE UNA NUEVA VERSION, ACTUALIZA POR FAVOR." , True)
'		cargar.Visible = False
'		Subir.Visible = False
'		connecta.Visible = False
'		trabajar.Visible = False
'		BUSCA.Visible = False
'		inv.Visible = False
'	End If
'	c.Close
	If EJECUTANDO = 0 Then
		Listo1 = 0
		Listo2 = 0
		Listo3 = 0
		Listo4 = 0
		Listo5 = 0
		inve = 0
		img2.Visible =False
	End If
	l_ruta.Text = 0
	f=Starter.skmt.ExecQuery("select rutaa from rutaa")
	If f.RowCount > 0 Then
		f.Position = 0
		l_ruta.Text = f.GetString("RUTAA")
	End If
	f.Close
	If l_ruta.Text = 0 Then
		l_ruta.Text = 0
		l_rutasP.Text = 0
	Else
		f=Starter.skmt.ExecQuery("select distinct CAT_CL_RUTA from kmt_info order by CAT_CL_RUTA")
'		f.Position=0
		l_rutasP.Text = ""
		For i = 0 To f.RowCount - 1
			f.Position = i
			If i = 0 Then
				l_rutasP.Text = f.GetString("CAT_CL_RUTA")
			Else
				l_rutasP.Text = l_rutasP.Text & ", " & f.GetString("CAT_CL_RUTA")
			End If
		Next
		f.Close
	End If
	DateTime.DateFormat = "MM/dd/yyyy"
	fecha=DateTime.Date(DateTime.Now)
	b=Starter.skmt.ExecQuery("Select count(*) as CUANTOS from kmt_info")
	b.Position=0
	If b.RowCount > 0 Then Cuantos = b.GetString("CUANTOS")
	d=Starter.skmt.ExecQuery("select count(*) as TOTAL_VISITAR from kmt_info where gestion = 0")
	D.Position=0
	b.Close
	e=Starter.skmt.ExecQuery("select count(*) as POR_VISITAR from kmt_info")
	e.Position=0
'	f=Starter.skmt.ExecQuery("select count(*) as CAT_CL_RUTA from kmt_info")
'	f.Position=0
'	l_ruta.Text = 0
'	If f.RowCount > 0 Then l_ruta.Text = f.GetString("CAT_CL_RUTA")
'	f.Close
'	f=Starter.skmt.ExecQuery("select CAT_CL_RUTA from kmt_info")
'	f.Position=0
'	If f.RowCount > 0 Then l_ruta.Text = f.GetString("CAT_CL_RUTA")
'	f.Close

	cargar.Visible = False
	Subir.Visible = False
	connecta.Visible = True

	If Cuantos = 0 Then
		l_aLiquidar.Text = 0
		l_cuantosc.Text = 0
		l_cuantosn.Text = 0
		drop = 0
		l_montoSalida.Text = 0
		efectiva = 0
'		l_efectiva.text = 0
'		cambios.Text = 0
		L_MONTOE.Text = 0
		l_ctast.Text = 0
		If d.RowCount > 0 Then l_ctast.Text = d.GetString("TOTAL_VISITAR")
		l_porvisitar.Text = 0
		If e.RowCount > 0 Then l_porvisitar.Text = e.GetString("POR_VISITAR")
		d.Close
		e.Close
	Else
		c=Starter.skmt.ExecQuery("select sum(hvd_costo_tot) as MONTO_DIA  from hist_ventas where hvd_cliente in (select cat_cl_codigo from kmt_info ) AND HVD_ESTATUS = 1 ")
		f1=Starter.skmt.ExecQuery("select sum(hvd_costo_tot) as MONTO_TOTAL_DIA from hist_ventas2 where hvd_cliente in (select cat_cl_codigo from kmt_info ) AND HVD_RECHAZO = 0")
		f2=Starter.skmt.ExecQuery("select sum(REP_COSTO_TOT) as MONTO_REC_DIA FROM REPARTO where REP_cliente in (Select cat_cl_codigo from kmt_info)")
'		b=Starter.skmt.ExecQuery("select count(*) as CUANTOS from noventa where nv_cliente in (select cat_cl_codigo from kmt_info where gestion = 3)")
		b=Starter.skmt.ExecQuery("select count(*) as CUANTOS from noventa where nv_motivo <> 'ENTREGA'")
'		L=Starter.skmt.ExecQuery("select count(*) as CLIENTES_DIA from noventa where nv_cliente in (select cat_cl_codigo from kmt_info where gestion = 2)")
		L=Starter.skmt.ExecQuery("select count(*) as CLIENTES_DIA from noventa where nv_motivo = 'ENTREGA'")
		f2.Position=0
'		F4=Starter.skmt.execqUery("select count(*) as CUANTOS FROM PEDIDO")
'		F4.Position = 0
		Private MONTO_REC_DIA = 0
		If f2.GetString("MONTO_REC_DIA") <> Null Then MONTO_REC_DIA = f2.GetString("MONTO_REC_DIA")
'		If F4.GetString("CUANTOS") > 0 Then
'			f3=Starter.skmt.execqUery("select sum(pe_costo_tot) as MONTO_VENTA FROM PEDIDO")
'			f3.Position=0
''			Log(MONTO_REC_DIA & " - " & f3.GetString("MONTO_VENTA"))
'			l_efectiva.text = to_number_vb(MONTO_REC_DIA - f3.GetString("MONTO_VENTA")) & "..."
'			f3.Close
'		Else
'			l_efectiva.text = to_number_vb(MONTO_REC_DIA) & ".."
'		End If
'		F4.Close
		C.Position = 0
		b.Position = 0
		L.Position = 0
		f1.Position = 0
		l_aLiquidar.Text = 0
		If f1.RowCount > 0 Then l_aLiquidar.Text = to_number_vb (f1.GetString("MONTO_TOTAL_DIA"))
		L_MONTOE.Text = 0
'		If C.RowCount > 0 Then L_MONTOE.Text = to_number_vb (C.GetString("MONTO_DIA"))
		l_cuantosc.Text = 0
		If L.RowCount > 0 Then l_cuantosc.Text = L.GetString("CLIENTES_DIA")
		l_cuantosn.Text = 0
		If b.RowCount > 0 Then l_cuantosn.Text = b.GetString("CUANTOS")
		'drop = c.GetString("MONTO_DIA") / c.GetString("CLIENTES_DIA")
		If f1.GetString("MONTO_TOTAL_DIA") <> Null Then l_montoSalida.Text = Round2(f1.GetString("MONTO_TOTAL_DIA"), 2)
		'efectiva = c.GetString("CLIENTES_DIA") / e.GetString("POR_VISITAR")
		l_ctast.Text = 0
		If d.RowCount > 0 Then l_ctast.Text = d.GetString("TOTAL_VISITAR")
		l_porvisitar.Text = 0
		If e.RowCount > 0 Then l_porvisitar.Text = e.GetString("POR_VISITAR")
		c.Close
		b.Close
'		c=Starter.skmt.ExecQuery2("select count(*) as CUANTOS from CAT_VARIABLES where CAT_VA_DESCRIPCION = ? ", Array As String("CAMBIOS"))
'		c.Position = 0
'		If c.GetString("CUANTOS") = 0 Then
''			cambios.Text = 0
'		Else
'			B=Starter.skmt.ExecQuery("select CAT_VA_VALOR from CAT_VARIABLES where CAT_VA_DESCRIPCION = 'CAMBIOS'")
'			B.Position = 0
''			cambios.Text = b.GetString("CAT_VA_VALOR")
'			b.Close
'		End If
		c.Close
	End If
	c=Starter.skmt.ExecQuery("select  ID_ALMACEN from CAT_ALMACEN")
	C.Position =0
	ALMACEN = 0
	If C.RowCount > 0 Then ALMACEN = C.GetString("ID_ALMACEN")
	B4XPages.MainPage.almacen = ALMACEN
	C.Close
	c=Starter.skmt.ExecQuery("select HGDATE, HGLAT, HGLON from HIST_GPS")
	' HACER ESTO PARA EL FORANEO
	If c.RowCount>0 Then
		For i=0 To c.RowCount -1
			c.Position=i
			Dim cmd As DBCommand
			cmd.Initialize
			cmd.Name = "insert_gps_actual_REP"
			cmd.Parameters = Array As Object(c.GetString("hgdate"),c.GetString("hglat"),c.GetString("hglon"),usuario )
		Next
	End If
	'Para traer la bateria
	bu.Initialize
	batterystatus = bu.BatteryInformation
'	LogColor($"Bateria STATUS=${batterystatus(0)}, ${batterystatus(1)}, ${batterystatus(4)}, BATT: ${Starter.batt}"$, Colors.Red)
	B4XPages.MainPage.batt = batterystatus(0)
	c=Starter.skmt.ExecQuery("select usuario from usuarioa")
	c.Position = 0
	usuario = c.GetString("USUARIO")
	c.Close
	c=Starter.skmt.ExecQuery("select RUTAA from RUTAA")
	c.Position = 0
	RUTA_T = 0
	If c.RowCount > 0 Then RUTA_T = c.GetString("RUTAA")
	B4XPages.MainPage.rutaPreventa = RUTA_T
	c.Close
	reqs.Initialize

	Private rc As Cursor = Starter.skmt.ExecQuery($"select * from REPARTO where REP_RECHAZO = "1" and REP_CANT > 0"$)
'	Log($"RECHAZOS=${rc.RowCount}"$)
	If rc.RowCount > 0 Then
		Starter.skmt.ExecNonQuery($"delete from RECHAZOS"$)
		For i3=0 To rc.RowCount -1 'Copiamos los rechazos de REPARTO a RECHAZOS.
			rc.Position = i3
'			Log($"delete from RECHAZOS where R_CLIENTE = '${rc.GetString("REP_CLIENTE")}' and R_CLI_ORIG = '${rc.GetString("REP_CLI_ORIG")}' and R_RECHAZO = '1' and R_PRODID = '${rc.GetString("REP_PRODID")}'"$)
'			Starter.skmt.ExecNonQuery($"delete from RECHAZOS where R_CLIENTE = '${rc.GetString("REP_CLIENTE")}' and R_CLI_ORIG = '${rc.GetString("REP_CLI_ORIG")}' and R_RECHAZO = '1' and R_PRODID = '${rc.GetString("REP_PRODID")}'"$)
			Starter.skmt.ExecNonQuery($"insert into RECHAZOS(R_FECHA, R_CLIENTE, R_CLI_ORIG, R_PRODID, R_CANT, R_RECHAZO, R_PRECIO) select REP_FECHA, REP_CLIENTE, REP_CLI_ORIG, REP_PRODID, REP_CANT, REP_RECHAZO, REP_PRECIO from REPARTO where REP_CLIENTE = '${rc.GetString("REP_CLIENTE")}' and REP_CLI_ORIG = '${rc.GetString("REP_CLI_ORIG")}' and REP_PRODID = '${rc.GetString("REP_PRODID")}' and REP_RECHAZO = '1' and REP_CANT > 0"$)
		Next
	End If
	Private vc As Cursor = Starter.skmt.ExecQuery($"select * from REPARTO where REP_RECHAZO = "0" and REP_CANT > 0"$)
'	Log($"VENTAS=${vc.RowCount}"$)
	If vc.RowCount > 0 Then
		Starter.skmt.ExecNonQuery($"delete from VENTAS"$)
		For i4=0 To vc.RowCount -1 'Copiamos las ventas de REPARTO a VENTAS.
			vc.Position = i4
'			Log($"delete from VENTAS where V_CLIENTE = '${vc.GetString("REP_CLIENTE")}' and V_CLI_ORIG = '${vc.GetString("REP_CLI_ORIG")}' and V_RECHAZO = '1' and V_PRODID = '${vc.GetString("REP_PRODID")}'"$)
'			Starter.skmt.ExecNonQuery($"delete from VENTAS where V_CLIENTE = '${vc.GetString("REP_CLIENTE")}' and V_CLI_ORIG = '${vc.GetString("REP_CLI_ORIG")}' and V_RECHAZO = '0' and V_PRODID = '${vc.GetString("REP_PRODID")}'"$)
			Starter.skmt.ExecNonQuery($"insert into VENTAS(V_FECHA, V_CLIENTE, V_CLI_ORIG, V_PRODID, V_CANT, V_RECHAZO, V_PRECIO) select REP_FECHA, REP_CLIENTE, REP_CLI_ORIG, REP_PRODID, REP_CANT, REP_RECHAZO, REP_PRECIO from REPARTO where REP_CLIENTE = '${vc.GetString("REP_CLIENTE")}' and REP_CLI_ORIG = '${vc.GetString("REP_CLI_ORIG")}' and REP_PRODID = '${vc.GetString("REP_PRODID")}' and REP_RECHAZO = '0' and REP_CANT > 0"$)
		Next
	End If
	Starter.ENVIA_ULTIMA_GPS

'	Log($"ENTREGADOS: ${Subs.traeEntregados}"$)
'	Log($"RECHAZADOS: ${Subs.traeRechazados}"$)
'	Log($"VENDIDOS: ${Subs.traeVendidos}"$)
	Dim entMap As Map = Subs.traeEntregados
	Dim rechMap As Map = Subs.traeRechazados
	Dim vendMap As Map = Subs.traeVendidos
	l_aLiquidar.Text = entMap.Get("monto") + vendMap.Get("monto")
	l_rechazo.text = rechMap.Get("monto")
	L_MONTOE.Text = entMap.Get("monto") + vendMap.Get("monto")
	B4XPages.MainPage.clientesVisitados = L_MONTOE.Text
	B4XPages.MainPage.clientesVenta = l_cuantosc.Text
	B4XPages.MainPage.clientesRechazo = l_cuantosn.Text
	B4XPages.MainPage.clientestotal = l_porvisitar.Text
	B4XPages.MainPage.montoActual = Round2(l_montoSalida.Text - rechMap.Get("monto"), 2)
	B4XPages.MainPage.porVisitar = l_ctast.text
	B4XPages.MainPage.entregas = l_cuantosc.text
	B4XPages.MainPage.rechazos = l_cuantosn.text
	B4XPages.MainPage.montoEntregado = L_MONTOE.text
	B4XPages.MainPage.montoRechazado = l_rechazo.text
	
	reqManager.Initialize(Me, B4XPages.MainPage.server)
'	LogColor($"ReqServer = ${B4XPages.MainPage.server}"$, Colors.red)
	
	If l_ctast.Text = 0 And l_porvisitar.Text <> 0 Then
		Label9.Visible = True
		L_MONTOE.Visible = True
	Else
		Label9.Visible = False
		L_MONTOE.Visible = False
	End If


'checachek
End Sub

Sub checachek
		b = Starter.skmt.ExecQuery("SELECT CHECADO FROM CHECADO_CHECK")
		If b.RowCount > 0 Then
		b.Position = 0
		If b.GetString("CHECADO") = 0 Then
			b_check.Visible = True
		Else If b.GetString("CHECADO") = 1 Then
			b_check.Visible =False
		End If
		b.Close
		End If
End Sub

Sub IsConnectedToInternet As Boolean 'ignore
	Dim r As Reflector
	r.Target = r.GetContext
	r.Target = r.RunMethod2("getSystemService", "connectivity", "java.lang.String")
	r.Target = r.RunMethod("getActiveNetworkInfo")
	If r.Target <> Null Then
		Return r.RunMethod("isConnectedOrConnecting")
	End If
	Return False
End Sub

Sub to_number_vb (valor As Object) As String
	Dim valstring As Int
	Dim valstring2 As String = valor
	valstring2.Replace(":","")
	Try
		valstring =valstring2.Replace(":","")
		Return valstring
	Catch
		Return 0
	End Try
End Sub

Sub trabajar_Click
	If b_check.Visible = True Then
	MsgboxAsync("Favor de hacer el checklist.","Atención")
	Else
		
	B4XPages.ShowPage("Clientes")
	End If
	
End Sub

Sub Subir_Click
	
	
	
	If Not(IsConnectedToInternet) Then MsgboxAsync("No hay conexión a conexión a Internet", "Revisa tu conexión")
	c=Starter.skmt.ExecQuery("select usuario from usuarioa")
	c.Position = 0
	usuario = c.GetString("USUARIO")
	c.Close
	' NOVENTA
	c = Starter.skmt.ExecQuery("SELECT NV_CLIENTE,NV_FECHA,NV_USER,NV_MOTIVO,NV_COMM,NV_LAT,NV_LON, NV_FOTO FROM NOVENTA")
	D=Starter.skmt.ExecQuery("SELECT COUNT(*) AS CUANTOS_NOVENTA FROM NOVENTA")
	If c.RowCount>0 Then
		For i=0 To c.RowCount -1
			c.Position=i
			foto1 = c.GetBlob("NV_FOTO")
			Dim cmd As DBCommand
			cmd.Initialize
			cmd.Name = "insert_reparto_noventa_MARQUEZ_REPG"
			cmd.Parameters = Array As Object(C.GetString("NV_CLIENTE"),C.GetString("NV_FECHA"),C.GetString("NV_USER"),C.GetString("NV_MOTIVO"),C.GetString("NV_COMM"),C.GetString("NV_LAT"),C.GetString("NV_LON"), ALMACEN, l_ruta.Text)
			reqManager.ExecuteQuery(cmd , 0, "inst_noventa")
		Next
		c.Close
	End If
	d.Position=0
	cuantos_noventa = D.GetString("CUANTOS_NOVENTA")
	d.Close
	c=Starter.skmt.ExecQuery("SELECT HVD_CLIENTE,HVD_PRONOMBRE,HVD_CANT,HVD_COSTO_TOT, HVD_FECHA, HVD_CODPROMO, HVD_PROID, HVD_NUM_TICKET, HVD_NUM_REGISTRO, HVD_RECHAZO, HVD_PARCIAL FROM hist_ventas WHERE HVD_RECHAZO = 1 OR HVD_PARCIAL = 1")
	'
	If c.RowCount>0 Then
		For i=0 To c.RowCount -1
			c.Position=i
			Dim cmd As DBCommand
			cmd.Initialize
			cmd.Name = "insert_rep_hist_ventas_marquez_REPG"
			cmd.Parameters = Array As Object(C.GetString("HVD_CLIENTE"),C.GetString("HVD_PRONOMBRE"),C.GetString("HVD_CANT"),C.GetString("HVD_COSTO_TOT"),C.GetString("HVD_CODPROMO"),C.GetString("HVD_PROID"),C.GetString("HVD_NUM_TICKET"),C.GetString("HVD_NUM_REGISTRO"),C.GetString("HVD_RECHAZO"),C.GetString("HVD_PARCIAL"), ALMACEN)
			reqManager.ExecuteQuery(cmd , 0, "inst_noventa")
		Next
		c.Close
	End If
'	pedido
	c=Starter.skmt.ExecQuery("SELECT PE_CEDIS, PE_COSTO_TOT, PE_COSTOU, PE_CANT, PE_PRONOMBRE, PE_PROID, PE_CLIENTE, PE_FECHA, PE_USUARIO, PE_COSTO_SIN, PE_RUTA, 1 as PE_DESC FROM PEDIDO")
	If c.RowCount>0 Then
		For i=0 To c.RowCount -1
			c.Position=i
			Dim cmd As DBCommand
			cmd.Initialize
			cmd.Name = "insert_pedido_REPG"
			cmd.Parameters = Array As Object(C.GetString("PE_CEDIS"),ALMACEN,C.GetString("PE_COSTO_TOT"),C.GetString("PE_COSTOU"),C.GetString("PE_CANT"),C.GetString("PE_PRONOMBRE"),C.GetString("PE_PROID"),C.GetString("PE_CLIENTE"),C.GetString("PE_FECHA"),C.GetString("PE_USUARIO"),C.GetString("PE_RUTA"), C.GetString("PE_COSTO_SIN"),C.GetString("PE_DESC"),"0","VENTA")
			reqManager.ExecuteQuery(cmd , 0, "ins_pedido")
		Next
		c.Close
	End If
'	reqManager.Initialize(Me, "http://187.189.244.154:1787") 'Para el servidor de pruebas.
	c = Starter.skmt.ExecQuery("SELECT * from reparto")
	If c.RowCount > 0 Then
		Private folio As String = Subs.hmsKMT
		For i=0 To c.RowCount -1
			c.Position=i
			Dim cmd As DBCommand
			cmd.Initialize
			cmd.Name = "insertReparto_marquez"
			cmd.Parameters = Array As Object(folio, C.GetString("REP_FECHA"), C.GetString("REP_CLIENTE"), C.GetString("REP_CLI_ORIG"), C.GetString("REP_PRODID"), C.GetString("REP_PRONOMBRE"), C.GetString("REP_PRODREGISTRO"), C.GetString("REP_CANT"), C.GetString("REP_PRECIO"), C.GetString("REP_COSTO_TOT"), C.GetString("REP_RECHAZO"),ALMACEN,l_ruta.Text)
			reqManager.ExecuteCommand(cmd , "ins_reparto")
		Next
		c.Close
	End If
	c = Starter.skmt.ExecQuery("SELECT * from rechazos")
	If c.RowCount > 0 Then
		Private folio As String = Subs.hmsKMT
		For i=0 To c.RowCount -1
			c.Position=i
			Dim cmd As DBCommand
			cmd.Initialize
			cmd.Name = "insertRechazos_marquez"
			cmd.Parameters = Array As Object(folio, C.GetString("R_FECHA"), C.GetString("R_CLIENTE"), C.GetString("R_CLI_ORIG"), C.GetString("R_PRODID"), C.GetString("R_CANT"), C.GetString("R_RECHAZO"),ALMACEN,l_ruta.Text)
			reqManager.ExecuteCommand(cmd , "ins_rechazos")
		Next
		c.Close
	End If
	c = Starter.skmt.ExecQuery("SELECT * from ventas")
	If c.RowCount > 0 Then
		Private folio As String = Subs.hmsKMT
		For i=0 To c.RowCount -1
			c.Position=i
			Dim cmd As DBCommand
			cmd.Initialize
			cmd.Name = "insertVentas_marquez"
			cmd.Parameters = Array As Object(folio, C.GetString("V_FECHA"), C.GetString("V_CLIENTE"), C.GetString("V_CLI_ORIG"), C.GetString("V_PRODID"), C.GetString("V_CANT"), C.GetString("V_RECHAZO"),ALMACEN,l_ruta.Text)
			reqManager.ExecuteCommand(cmd , "ins_ventas")
		Next
		c.Close
	End If
	'TABULADOR
	c = Starter.skmt.ExecQuery("SELECT VEINTE, DIEZ, CINCO, DOS, PESO, CENTAVO, TOTAL FROM TABULADOR_MONEDAS")
	d = Starter.skmt.ExecQuery("SELECT RUTAA FROM RUTAA")
	b = Starter.skmt.ExecQuery("SELECT ID_ALMACEN FROM CAT_ALMACEN")
	f = Starter.skmt.ExecQuery("SELECT HVD_FECHA FROM HIST_VENTAS")
	d.Position = 0
	b.Position = 0
	f.Position = 0
	If c.RowCount > 0 Then
		For i = 0 To c.RowCount - 1
			c.Position=i
			Dim cmd As DBCommand
			Dim fechatabulador() As String = Regex.Split(" ", f.GetString("HVD_FECHA"))
			Dim fechatabulador2() As String = Regex.Split("-", fechatabulador(0))
			Dim fechatabulador3 As String = fechatabulador2(1)&"/"&fechatabulador2(2)&"/"&fechatabulador2(0)
			cmd.Initialize
			cmd.Name = "insert_tabulador_monedas_MARQUEZ"
			cmd.Parameters = Array As Object(d.GetString("RUTAA"), b.GetString("ID_ALMACEN"),c.GetString("VEINTE"),c.GetString("DIEZ"),c.GetString("CINCO"),c.GetString("DOS"),c.GetString("PESO"),c.GetString("CENTAVO"),fechatabulador3)
			reqManager.ExecuteCommand(cmd , "ins_tabulador_mon")
		Next
		c.Close
		d.Close
		b.Close
		f.Close
	End If

	c = Starter.skmt.ExecQuery("SELECT MIL, QUINIENTOS, DOCIENTOS, CIEN, CINCUENTA, VEINTE FROM TABULADOR_BILLETES")
	d = Starter.skmt.ExecQuery("SELECT RUTAA FROM RUTAA")
	b = Starter.skmt.ExecQuery("SELECT ID_ALMACEN FROM CAT_ALMACEN")
	f = Starter.skmt.ExecQuery("SELECT HVD_FECHA FROM HIST_VENTAS")
	d.Position = 0
	b.Position = 0
	f.Position = 0
	If c.RowCount > 0 Then
		For i = 0 To c.RowCount - 1
			c.Position=i
			Dim cmd As DBCommand
			Dim fechatabulador() As String = Regex.Split(" ", f.GetString("HVD_FECHA"))
			Dim fechatabulador2() As String = Regex.Split("-", fechatabulador(0))
			Dim fechatabulador3 As String = fechatabulador2(1)&"/"&fechatabulador2(2)&"/"&fechatabulador2(0)
			cmd.Initialize
			cmd.Name = "insert_tabulador_billetes_MARQUEZ"
			cmd.Parameters = Array As Object(b.GetString("ID_ALMACEN"), d.GetString("RUTAA"),c.GetString("MIL"),c.GetString("QUINIENTOS"),c.GetString("DOCIENTOS"),c.GetString("CIEN"),c.GetString("CINCUENTA"),c.GetString("VEINTE"),fechatabulador3)
			reqManager.ExecuteCommand(cmd , "ins_tabulador_bill")
		Next
		c.Close
		d.Close
		b.Close
		f.Close
	End If
	'FIN TABULADOR
	
	Dim cmd As DBCommand
	cmd.Initialize
	cmd.Name = "insert_drop_rep_marquez"
	cmd.Parameters = Array As Object(Subs.dameUsuarioDeDB, l_ruta.Text, ALMACEN, l_ctast.text, Subs.traeEntregados, Subs.traeRechazados, "ENVIO")
	reqManager.ExecuteCommand(cmd , "inst_drop")
	
	ToastMessageShow("Se Actualizaran los datos, Este proceso podria tardar hasta un minuto, gracias "& l_ruta.text , True)
End Sub

Sub cargar_Click
	Dim pedidodiaanterior As Boolean = False
	Dim cp As Cursor = Starter.skmt.ExecQuery("select * from NOVENTA")
	If cp.RowCount > 0 Then
		cp.Position = 0
		Private fecha As String = cp.GetString("NV_FECHA")
		Log(cp.GetString("NV_FECHA"))
		Dim fecha2() As String = Regex.Split(" ",fecha)
		Log(fecha2(0))
		Dim fecha3() As String = Regex.Split("/",fecha2(0))
		DateTime.DateFormat = "yyyyMMdd"
		DateTime.TimeFormat = ""
		Dim dia As String = fecha3(2)&fecha3(0)&fecha3(1)
		Log(dia)
		Dim dia2 As String = DateTime.Date(DateTime.Now)
		Log(dia & " , " & dia2)
		If dia < dia2 Then
			pedidodiaanterior = True
		End If
	End If
	If Not (pedidodiaanterior) Then
	Log("CUMPLE")
		EJECUTANDO = 1
		reqs.Initialize
		TOPE = 0
		LogColor($"ReqServer = ${B4XPages.MainPage.server}"$, Colors.red)
		reqManager.Initialize(Me, B4XPages.MainPage.server)
	
		Dim cmd As DBCommand
		cmd.Initialize
		cmd.Name = "insert_drop_rep_marquez"
		cmd.Parameters = Array As Object(Subs.dameUsuarioDeDB, l_ruta.Text, ALMACEN, l_ctast.text, Subs.traeEntregados, Subs.traeRechazados, "CARGA")
		reqManager.ExecuteCommand(cmd , "inst_drop")
'	reqs.Add("inst_noventa")

		Starter.skmt.ExecNonQuery("delete from kmt_info")
		Starter.skmt.ExecNonQuery("delete from cod_result")
		Starter.skmt.ExecNonQuery("delete from hist_gest")
		Starter.skmt.ExecNonQuery("delete from CAT_DETALLES_PAQ")
		Starter.skmt.ExecNonQuery("delete from telefonos")
'	Starter.skmt.ExecNonQuery("delete from VENTAS")
		Starter.skmt.ExecNonQuery("delete from PUNTEO_GPS")
		If l_ruta.Text = "0" Then '
			Log("*********************  No hay ruta, BORRAMOS TODO")
			Starter.skmt.ExecNonQuery("delete from cat_gunaprod")
			Starter.skmt.ExecNonQuery("delete from REPARTO")
			Starter.skmt.ExecNonQuery("delete from HIST_VENTAS")
			Starter.skmt.ExecNonQuery("delete from HIST_VENTAS2")
			Starter.skmt.ExecNonQuery("delete from NOVENTA")
		Else
			Log("*********************  Hay ruta, NO BORRAMOS TODO")
		End If

		' NUEVOS CLIENTES
		c=Starter.skmt.ExecQuery("select usuario from usuarioa")
		c.Position = 0
		usuario = c.GetString("USUARIO")

		cmd.Initialize
		cmd.Name = "select_punteo_marquez_re_REPG"
		cmd.Parameters = Array As Object(e_ruta.text, ALMACEN,ALMACEN)
		reqManager.ExecuteQuery(cmd , 0, "punteo")
		Log($"Pedimos Punteo"$)
		reqs.Add("punteo")

		If l_ruta.Text = "0" Then
			cmd.Initialize
			cmd.Name = "select_almacen_marquez_re_REPG"
			cmd.Parameters = Array As Object(ALMACEN, e_ruta.text, ALMACEN)
			reqManager.ExecuteQuery(cmd , 0, "gunaprod")
			Log($"Pedimos gunaprod - ${ALMACEN}, ${e_ruta.text}"$)
			reqs.Add("gunaprod")
		End If


		cmd.Initialize
		cmd.Name = "select_cliente_marquez_re_REPG"
		cmd.Parameters = Array As Object(e_ruta.text, ALMACEN,ALMACEN,e_ruta.text, ALMACEN)
		reqManager.ExecuteQuery(cmd , 0, "kmt_datos")
		Log($"Pedimos kmt_datos"$)
		reqs.Add("kmt_datos")

		cmd.Initialize
		cmd.Name = "select_ventad_marquez_re_REPG"
		cmd.Parameters = Array As Object(e_ruta.text, ALMACEN)
		reqManager.ExecuteQuery(cmd , 0, "hist_datos")
		Log($"Pedimos hist_datos"$)
		reqs.Add("hist_datos")

		cmd.Initialize
		cmd.Name = "select_coords_almacen_Marquez"
		cmd.Parameters = Array As Object(ALMACEN)
		reqManager.ExecuteQuery(cmd , 0, "coords_almacen")

'	c=Starter.skmt.ExecQuery("SELECT REP_CLIENTE, REP_PRONOMBRE, REP_CANT, REP_COSTO_TOT, REP_FECHA FROM REPARTO")
'	If c.RowCount > 0 Then
'		For i=0 To c.RowCount -1
'			c.Position=i
'			Dim cmd As DBCommand
'			cmd.Initialize
'			cmd.Name = "insert_reparto_rechazo_REPG"
'			cmd.Parameters = Array As Object(C.GetString("REP_CLIENTE"),C.GetString("REP_PRONOMBRE"),C.GetString("REP_CANT"),C.GetString("REP_COSTO_TOT"),C.GetString("REP_FECHA"), ALMACEN)
'			reqManager.ExecuteCommand(cmd , "inst_noventa")
'			reqs.Add("inst_noventa")
'		Next
'	End If
'	c.Close

		' NOVENTA
'	c=Starter.skmt.ExecQuery("SELECT NV_CLIENTE, NV_FECHA, NV_USER,NV_MOTIVO,NV_COMM,NV_LAT,NV_LON, NV_FOTO FROM NOVENTA")
'	If c.RowCount > 0 Then
'		For i=0 To c.RowCount -1
'			c.Position=i
'			foto1 = c.GetBlob("NV_FOTO")
'			Dim cmd As DBCommand
'			cmd.Initialize
'			cmd.Name = "insert_reparto_REPG"
'			cmd.Parameters = Array As Object(C.GetString("NV_CLIENTE"),C.GetString("NV_FECHA"),C.GetString("NV_USER"),C.GetString("NV_MOTIVO"),C.GetString("NV_COMM"),C.GetString("NV_LAT"),C.GetString("NV_LON"), ALMACEN, l_ruta.Text, foto1)
'			reqManager.ExecuteCommand(cmd , "inst_noventa")
'			reqs.Add("inst_noventa")
'		Next
'	End If
'	c.Close	

		D=Starter.skmt.ExecQuery("SELECT COUNT(*) AS CUANTOS_NOVENTA FROM NOVENTA")
		d.Position=0
		cuantos_noventa = D.GetString("CUANTOS_NOVENTA")
		d.Close
		cargar.Visible = False
		Subir.Visible = False
		connecta.Visible = True
		inv.Visible = False
		ToastMessageShow("Se actualizarán los datos, este proceso podria tardar hasta un minuto, gracias" , True)
		ProgressDialogShow2("Descargando rutas, espere un momento.", False)
		If Not(IsConnectedToInternet) Then MsgboxAsync("No hay conexión a Internet", "Revisa tu conexión")
	
	
	Else
		Log("XXXX")
		MsgboxAsync("Para poder cargar día no puedes tener venta del día anterior. Por favor haz FIN DIA e intenta nuevamente.","Atención")
	End If
	
End Sub

Sub JobDone(Job As HttpJob)
'	Private r As DBResult = reqManager.HandleJob(Job)
	If Job.Success = False Then
		LogColor("** " & Job.Tag & " Error: " & Job.ErrorMessage, Colors.Red) ' Mod by CHV - 211023
		'ToastMessageShow("Error: " & Job.ErrorMessage, True)
	Else
		LogColor("JobDone: '" & reqManager.HandleJob(Job).tag & "' - Registros: " & reqManager.HandleJob(Job).Rows.Size, Colors.Green)  'Mod por CHV - 211023
		If Job.JobName = "DBRequest" Then
			Dim result As DBResult = reqManager.HandleJob(Job)
			If result.Tag = "kmt_datos" Then 'query tag
				If reqs.IndexOf("kmt_datos") > -1 Then
					reqs.RemoveAt(reqs.IndexOf("kmt_datos"))
'					Log(reqs.Size)
				End If
				Starter.skmt.BeginTransaction
				For Each records() As Object In result.Rows
					Dim CAT_CL_CODIGO As String = records(result.Columns.Get("CAT_CL_CODIGO"))
					Dim CAT_CL_RUTA As String = records(result.Columns.Get("CAT_CL_RUTA"))
					Dim CAT_CL_NOMBRE As String = records(result.Columns.Get("CAT_CL_NOMBRE"))
					Dim CAT_CL_ATIENDE1 As String = records(result.Columns.Get("CAT_CL_ATIENDE1"))
					Dim CAT_CL_ATIENTE2 As String = records(result.Columns.Get("CAT_CL_ATIENTE2"))
					Dim CAT_CL_TELEFONO As String = records(result.Columns.Get("CAT_CL_TELEFONO"))
					Dim CAT_CL_EMAIL As String = records(result.Columns.Get("CAT_CL_EMAIL"))
					Dim CAT_CL_CALLE As String = records(result.Columns.Get("CAT_CL_CALLE"))
					Dim CAT_CL_NOEXT As String = records(result.Columns.Get("CAT_CL_NOEXT"))
					Dim CAT_CL_NOINT As String = records(result.Columns.Get("CAT_CL_NOINT"))
					Dim CAT_CL_CALLE1 As String = records(result.Columns.Get("CAT_CL_CALLE1"))
					Dim CAT_CL_CALLE2 As String = records(result.Columns.Get("CAT_CL_CALLE2"))
					Dim CAT_CL_COLONIA As String = records(result.Columns.Get("CAT_CL_COLONIA"))
					Dim CAT_CL_MUNI As String = records(result.Columns.Get("CAT_CL_MUNI"))
					Dim CAT_CL_EDO As String = records(result.Columns.Get("CAT_CL_EDO"))
					Dim CAT_CL_CP As String = records(result.Columns.Get("CAT_CL_CP"))
					Dim CAT_CL_LONG As String = records(result.Columns.Get("CAT_CL_LONG"))
					Dim CAT_CL_LAT As String = records(result.Columns.Get("CAT_CL_LAT"))
					Dim CAT_CL_BCREDITO As String = records(result.Columns.Get("CAT_CL_BCREDITO"))
					Dim CAT_CL_SECUENCIA As String = records(result.Columns.Get("CAT_CL_SECUENCIA"))
'					Log("  ++  insert into kmt_info: "&CAT_CL_RUTA&","&CAT_CL_CODIGO&","&CAT_CL_RUTA&","&CAT_CL_NOMBRE)
					Starter.skmt.ExecNonQuery2("INSERT INTO kmt_info(CAT_CL_CODIGO,CAT_CL_RUTA,CAT_CL_NOMBRE,CAT_CL_ATIENDE1,CAT_CL_ATIENTE2,CAT_CL_TELEFONO,CAT_CL_EMAIL,CAT_CL_CALLE,CAT_CL_NOEXT,CAT_CL_NOINT,CAT_CL_CALLE1,CAT_CL_CALLE2,CAT_CL_COLONIA,CAT_CL_MUNI,CAT_CL_EDO,CAT_CL_CP,CAT_CL_LONG,CAT_CL_LAT,CAT_CL_BCREDITO, gestion,SECUENCIA) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,0,?)", Array As Object (CAT_CL_CODIGO,CAT_CL_RUTA,CAT_CL_NOMBRE,CAT_CL_ATIENDE1,CAT_CL_ATIENTE2,CAT_CL_TELEFONO,CAT_CL_EMAIL,CAT_CL_CALLE,CAT_CL_NOEXT,CAT_CL_NOINT,CAT_CL_CALLE1,CAT_CL_CALLE2,CAT_CL_COLONIA,CAT_CL_MUNI,CAT_CL_EDO,CAT_CL_CP,CAT_CL_LONG,CAT_CL_LAT,CAT_CL_BCREDITO,CAT_CL_SECUENCIA))
				Next
				Starter.skmt.TransactionSuccessful
				Starter.skmt.EndTransaction
				Listo1 = 1
				If Listo1 = 1  And Listo3 = 1  Then
					ToastMessageShow("Datos Actualizados." , True)
					B4XPage_Appear
					img2.Visible = False
					EJECUTANDO=0
				End If
			End If
		End If

		If Job.JobName = "DBRequest" Then
			Dim result As DBResult = reqManager.HandleJob(Job)
			If result.Tag = "gunaprod" Then 'query tag
				If reqs.IndexOf("gunaprod") > -1 Then
					reqs.RemoveAt(reqs.IndexOf("gunaprod"))
					Log(reqs.Size)
				End If
				Starter.skmt.BeginTransaction
				For Each records() As Object In result.Rows
					Dim CAT_GP_ID As String = records(result.Columns.Get("CAT_DP_ID"))
					Dim CAT_GP_NOMBRE As String = records(result.Columns.Get("CAT_DP_NOMBRE"))
					Dim CAT_GP_IMP1 As String = records(result.Columns.Get("CAT_DP_IMP1"))
					Dim CAT_GP_IMP2 As String = records(result.Columns.Get("CAT_DP_IMP2"))
					Dim CAT_GP_PRECIO As String = records(result.Columns.Get("CAT_DP_PRECIO"))
					Dim CAT_GP_CLASIF As String = records(result.Columns.Get("CAT_DP_CLASIF"))
					Dim CAT_GP_STS As String = records(result.Columns.Get("CAT_DP_STS"))
					Dim CAT_GP_TIPO As String = records(result.Columns.Get("CAT_DP_TIPO"))
					Dim CAT_GP_SUBTIPO As String = records(result.Columns.Get("CAT_DP_SUBTIPO"))
					Dim CONVERSION As String = records(result.Columns.Get("CAT_DP_CONVERSION1"))
					Dim PRECIOCONVER As String = records(result.Columns.Get("CAT_DP_PRECIO4"))
					Dim CAT_GP_IMG() As Byte = records(result.Columns.Get("CAT_DP_IMG"))
					'Dim CAT_GP_ALMACEN As Int = records(RESULT.Columns.Get("CAT_GP_ALMACEN"))
					'	Dim CAT_GP_DEV As String = records(RESULT.Columns.Get("CAT_GP_DEV"))
					'  skmt.ExecNonQuery2("INSERT INTO CAT_GUNAPROD(CAT_GP_ID,CAT_GP_NOMBRE,CAT_GP_IMP1,CAT_GP_IMP2,CAT_GP_PRECIO,CAT_GP_CLASIF,CAT_GP_STS,CAT_GP_TIPO,CAT_GP_SUBTIPO,CAT_GP_IMG,CAT_GP_ALMACEN,CAT_GP_DEV) VALUES (?,?,?,?,?,?,?,?,?,?,?,?)", Array As Object (CAT_GP_ID,CAT_GP_NOMBRE,CAT_GP_IMP1,CAT_GP_IMP2,CAT_GP_PRECIO,CAT_GP_CLASIF,CAT_GP_STS,CAT_GP_TIPO,CAT_GP_SUBTIPO,CAT_GP_IMG, CAT_GP_ALMACEN,CAT_GP_DEV))
'					Log("  ++  insert into CAT_GUNAPROD: "&CAT_GP_ID&","&CAT_GP_NOMBRE&","&CAT_GP_IMP1)
					Starter.skmt.ExecNonQuery2("INSERT INTO CAT_GUNAPROD(CAT_GP_ID,CAT_GP_NOMBRE,CAT_GP_IMP1,CAT_GP_IMP2,CAT_GP_PRECIO,CAT_GP_CLASIF,CAT_GP_STS,CAT_GP_TIPO,CAT_GP_SUBTIPO,CAT_GP_IMG,CAT_GP_ALMACEN,CONVERSION,PRECIOCONVER) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)", Array As Object (CAT_GP_ID,CAT_GP_NOMBRE,CAT_GP_IMP1,CAT_GP_IMP2,CAT_GP_PRECIO,CAT_GP_CLASIF,CAT_GP_STS,CAT_GP_TIPO,CAT_GP_SUBTIPO,CAT_GP_IMG, 0,CONVERSION,PRECIOCONVER))
				Next
				Starter.skmt.TransactionSuccessful
				Starter.skmt.EndTransaction
				Listo2=1
				If Listo1 = 1 And Listo2 =1 And Listo3 = 1 And Listo4 = 1 Then
					ToastMessageShow("Datos Actualizados." , True)
					B4XPage_Appear
					img2.Visible = False
					EJECUTANDO=0
				End If
			End If
		End If

		If Job.JobName = "DBRequest" Then
			Dim result As DBResult = reqManager.HandleJob(Job)
			If result.Tag = "gunaprodp" Then 'query tag
				If reqs.IndexOf("gunaprodp") > -1 Then
					reqs.RemoveAt(reqs.IndexOf("gunaprodp"))
					Log(reqs.Size)
				End If
				For Each records() As Object In result.Rows
					Dim CAT_GP_ID As String = records(result.Columns.Get("CAT_GP_ID"))
					Dim CAT_GP_NOMBRE As String = records(result.Columns.Get("CAT_GP_NOMBRE"))
					Dim CAT_GP_IMP1 As String = records(result.Columns.Get("CAT_GP_IMP1"))
					Dim CAT_GP_IMP2 As String = records(result.Columns.Get("CAT_GP_IMP2"))
					Dim CAT_GP_PRECIO As String = records(result.Columns.Get("CAT_GP_PRECIO"))
					Dim CAT_GP_CLASIF As String = records(result.Columns.Get("CAT_GP_CLASIF"))
					Dim CAT_GP_STS As String = records(result.Columns.Get("CAT_GP_STS"))
					Dim CAT_GP_TIPO As String = records(result.Columns.Get("CAT_GP_TIPO"))
					Dim CAT_GP_SUBTIPO As String = records(result.Columns.Get("CAT_GP_SUBTIPO"))
					Dim CAT_GP_IMG() As Byte = records(result.Columns.Get("CAT_GP_IMG"))
					'Dim CAT_GP_ALMACEN As Int = records(RESULT.Columns.Get("CAT_GP_ALMACEN"))
'					Log("  ++  insert into CAT_GUNAPROD: "&CAT_GP_ID&","&CAT_GP_NOMBRE&","&CAT_GP_IMP1)
					Starter.skmt.ExecNonQuery2("INSERT INTO CAT_GUNAPROD(CAT_GP_ID,CAT_GP_NOMBRE,CAT_GP_IMP1,CAT_GP_IMP2,CAT_GP_PRECIO,CAT_GP_CLASIF,CAT_GP_STS,CAT_GP_TIPO,CAT_GP_SUBTIPO,CAT_GP_IMG,CAT_GP_ALMACEN) VALUES (?,?,?,?,?,?,?,?,?,?,?)", Array As Object (CAT_GP_ID,CAT_GP_NOMBRE,CAT_GP_IMP1,CAT_GP_IMP2,CAT_GP_PRECIO,CAT_GP_CLASIF,CAT_GP_STS,CAT_GP_TIPO,CAT_GP_SUBTIPO,CAT_GP_IMG, 0))
				Next
				Listo4=1
				If Listo1 = 1 And Listo2 =1 And Listo3 = 1 And Listo4 = 1 Then
					ToastMessageShow("Datos Actualizados." , True)
					B4XPage_Appear
					img2.Visible = False
					EJECUTANDO=0
				Else If Listo4 = 1 And Listo3 = 1 And inve = 1 Then
					ToastMessageShow("Datos Actualizados." , True)
					B4XPage_Appear
					img2.Visible = False
					EJECUTANDO=0
				End If
			End If
		End If

		If Job.JobName = "DBRequest" Then
			Dim result As DBResult = reqManager.HandleJob(Job)
			If result.Tag = "detallepaq" Then 'query tag
				If reqs.IndexOf("detallepaq") > -1 Then
					reqs.RemoveAt(reqs.IndexOf("detallepaq"))
					Log(reqs.Size)
				End If
				For Each records() As Object In result.Rows
					Dim CAT_DP_ALMACEN  As String = records(result.Columns.Get("CAT_DP_ALMACEN"))
					Dim CAT_DP_ID As String = records(result.Columns.Get("CAT_DP_ID"))
					Dim CAT_DP_IDPROD As String = records(result.Columns.Get("CAT_DP_IDPROD"))
					Dim CAT_DP_TIPO As String = records(result.Columns.Get("CAT_DP_TIPO"))
					Dim CAT_DP_PZAS As String = records(result.Columns.Get("CAT_DP_PZAS"))
					Dim CAT_DP_USUARIO  As String = records(result.Columns.Get("CAT_DP_USUARIO"))
					Dim CAT_DP_FECHA As String = records(result.Columns.Get("CAT_DP_FECHA"))
					Dim CAT_DP_REGALO  As String = records(result.Columns.Get("CAT_DP_REGALO"))
					Dim CAT_DP_CLASIF  As String = records(result.Columns.Get("CAT_DP_CLASIF"))
					Dim CAT_DP_PRECIO As String = records(result.Columns.Get("CAT_DP_PRECIO"))
					Dim CAT_DP_PRECIO_SIMPTOS As String = records(result.Columns.Get("CAT_DP_PRECIO_SIMPTOS"))
'					Log("  ++  insert into CAT_DETALLES_PAQ: "&CAT_DP_ALMACEN&","&CAT_DP_ID&","&CAT_DP_IDPROD)
					Starter.skmt.ExecNonQuery2("INSERT INTO CAT_DETALLES_PAQ(CAT_DP_ALMACEN,CAT_DP_ID,CAT_DP_IDPROD,CAT_DP_TIPO,CAT_DP_PZAS,CAT_DP_USUARIO,CAT_DP_FECHA,CAT_DP_REGALO,CAT_DP_CLASIF,CAT_DP_PRECIO,CAT_DP_PRECIO_SIMPTOS) VALUES (?,?,?,?,?,?,?,?,?,?,?)", Array As Object (CAT_DP_ALMACEN,CAT_DP_ID,CAT_DP_IDPROD,CAT_DP_TIPO,CAT_DP_PZAS,CAT_DP_USUARIO,CAT_DP_FECHA,CAT_DP_REGALO,CAT_DP_CLASIF,CAT_DP_PRECIO,CAT_DP_PRECIO_SIMPTOS))
				Next
				Listo3 =1
				If Listo1 = 1 And Listo2 = 1 And Listo3 = 1 And Listo4 = 1 Then
					ToastMessageShow("Datos Actualizados." , True)
					B4XPage_Appear
					img2.Visible=False
					EJECUTANDO = 0
				Else If Listo4 = 1 And Listo3 = 1 And inve = 1 Then
					ToastMessageShow("Datos Actualizados." , True)
					B4XPage_Appear
					img2.Visible = False
					EJECUTANDO = 0
				End If
			End If
		End If

		If Job.JobName = "DBRequest" Then
			Dim result As DBResult = reqManager.HandleJob(Job)
			If result.Tag = "punteo" Then 'query tag
				If reqs.IndexOf("punteo") > -1 Then
					reqs.RemoveAt(reqs.IndexOf("punteo"))
'					Log(reqs.Size)
				End If
				If reqs.IndexOf("punteo") > 0 Then
					reqs.RemoveAt(reqs.IndexOf("punteo"))
					Log(reqs.Size)
				End If
				Starter.skmt.BeginTransaction
				For Each records() As Object In result.Rows
					Dim HV_CLIENTE As String = records(result.Columns.Get("HV_CLIENTE"))
					Dim HV_LONG As String = records(result.Columns.Get("HV_LONG"))
					Dim HV_LAT As String = records(result.Columns.Get("HV_LAT"))
'					Log("  ++  insert into PUNTEO_GPS: "&HV_CLIENTE&","&HV_LONG)
					Starter.skmt.ExecNonQuery2("INSERT INTO PUNTEO_GPS(HV_CLIENTE, HV_LONG, HV_LAT) VALUES (?,?,?)", Array As Object (HV_CLIENTE, HV_LONG, HV_LAT))
				Next
				Starter.skmt.TransactionSuccessful
				Starter.skmt.EndTransaction
			End If
		End If

		If Job.JobName = "DBRequest" Then
			Dim result As DBResult = reqManager.HandleJob(Job)
			If result.Tag = "hist_datos" Then 'query tag
				If reqs.IndexOf("hist_datos") > -1 Then
					reqs.RemoveAt(reqs.IndexOf("hist_datos"))
'					Log(reqs.Size)
				End If
				Log(l_ruta.text)
				Log($"********  Insertamos HV - ${result.Rows.Size}"$)
				Starter.skmt.BeginTransaction
				For Each records() As Object In result.Rows
					Dim HVD_CLIENTE As String = records(result.Columns.Get("HVD_CLIENTE"))
					Dim HVD_PRONOMBRE As String = records(result.Columns.Get("HVD_PRONOMBRE"))
					Dim HVD_CANT As String = records(result.Columns.Get("HVD_CANT"))
					Dim HVD_COSTO_TOT As String = records(result.Columns.Get("HVD_COSTO_TOT"))
					Dim HVD_FECHA As String = records(result.Columns.Get("HVD_FECHA"))
					Dim HVD_CODPROMO As String = records(result.Columns.Get("HVD_CODPROMO"))
					Dim HVD_PROID As String = records(result.Columns.Get("HVD_PROID"))
					Dim HVD_NUM_TICKET As String = records(result.Columns.Get("HVD_NUM_TICKET"))
					Dim HVD_NUM_REGISTRO As String = records(result.Columns.Get("HVD_NUM_REGISTRO"))
					Dim HVD_RECHAZO As String = records(result.Columns.Get("HVD_RECHAZO"))
					Dim HVD_PLACA As String = records(result.Columns.Get("HVD_PLACA"))
					Dim bcajas As String = records(result.Columns.Get("HVD_BCAJAS"))
					Dim cantc As String = records(result.Columns.Get("HVD_CANTC"))
'					Log("  ++  insert into HIST_VENTAS: "&HVD_CLIENTE&","&HVD_PRONOMBRE&","&HVD_CANT)

					If l_ruta.Text = "0" Then 'Si no hay ruta cargada, guardamos todos los clientes
						Log("No hay ruta, guardamos todos")
						Starter.skmt.ExecNonQuery2("INSERT INTO HIST_VENTAS(HVD_CLIENTE,HVD_PRONOMBRE,HVD_CANT,HVD_COSTO_TOT, HVD_FECHA, HVD_CODPROMO, HVD_PROID,HVD_NUM_TICKET, HVD_NUM_REGISTRO, HVD_RECHAZO, HVD_ESTATUS, HVD_PARCIAL, PLACA,BCAJAS,CANTC) VALUES (?,?,?,?,?,?,?,?,?,?,0,0,?,?,?)", Array As Object (HVD_CLIENTE,HVD_PRONOMBRE,HVD_CANT,HVD_COSTO_TOT,HVD_FECHA, HVD_CODPROMO, HVD_PROID,HVD_NUM_TICKET, HVD_NUM_REGISTRO, HVD_RECHAZO, HVD_PLACA,bcajas,cantc))
						Starter.skmt.ExecNonQuery2("INSERT INTO HIST_VENTAS2(HVD_CLIENTE,HVD_PRONOMBRE,HVD_CANT,HVD_COSTO_TOT, HVD_FECHA, HVD_CODPROMO, HVD_PROID,HVD_NUM_TICKET, HVD_NUM_REGISTRO, HVD_RECHAZO, HVD_ESTATUS, HVD_PARCIAL) VALUES (?,?,?,?,?,?,?,?,?,?,0,0)", Array As Object (HVD_CLIENTE,HVD_PRONOMBRE,HVD_CANT,HVD_COSTO_TOT,HVD_FECHA, HVD_CODPROMO, HVD_PROID,HVD_NUM_TICKET, HVD_NUM_REGISTRO, HVD_RECHAZO))
					Else 'Si ya hay ruta cargada, solo guardamos los clientes nuevos.
'						Log("Hay ruta, guardamos los nuevos")
						Private tc As Cursor = Starter.skmt.ExecQuery($"select HVD_CLIENTE from HIST_VENTAS where HVD_CLIENTE = '${HVD_CLIENTE}' and HVD_PROID = '${HVD_PROID}'"$)
						If tc.RowCount = 0 Then
							Log($"Guardamos ${HVD_CLIENTE}"$)
							Starter.skmt.ExecNonQuery2("INSERT INTO HIST_VENTAS(HVD_CLIENTE,HVD_PRONOMBRE,HVD_CANT,HVD_COSTO_TOT, HVD_FECHA, HVD_CODPROMO, HVD_PROID,HVD_NUM_TICKET, HVD_NUM_REGISTRO, HVD_RECHAZO, HVD_ESTATUS, HVD_PARCIAL, PLACA,BCAJAS,CANTC) VALUES (?,?,?,?,?,?,?,?,?,?,0,0,?,?,?)", Array As Object (HVD_CLIENTE,HVD_PRONOMBRE,HVD_CANT,HVD_COSTO_TOT,HVD_FECHA, HVD_CODPROMO, HVD_PROID,HVD_NUM_TICKET, HVD_NUM_REGISTRO, HVD_RECHAZO, HVD_PLACA,bcajas,cantc))
							Starter.skmt.ExecNonQuery2("INSERT INTO HIST_VENTAS2(HVD_CLIENTE,HVD_PRONOMBRE,HVD_CANT,HVD_COSTO_TOT, HVD_FECHA, HVD_CODPROMO, HVD_PROID,HVD_NUM_TICKET, HVD_NUM_REGISTRO, HVD_RECHAZO, HVD_ESTATUS, HVD_PARCIAL) VALUES (?,?,?,?,?,?,?,?,?,?,0,0)", Array As Object (HVD_CLIENTE,HVD_PRONOMBRE,HVD_CANT,HVD_COSTO_TOT,HVD_FECHA, HVD_CODPROMO, HVD_PROID,HVD_NUM_TICKET, HVD_NUM_REGISTRO, HVD_RECHAZO))							
						End If
					End If
				Next
				Starter.skmt.TransactionSuccessful
				Starter.skmt.EndTransaction
				Log($"********  Terminamos HV - ${result.Rows.Size}"$)
'				f2=Starter.skmt.ExecQuery2("select COUNT(*) as CAMBIOS from hist_ventas where SUBSTR(HVD_PRONOMBRE,1,6) =?", Array As String("CAMBIO"))
'				f2.Position = 0
'				If f2.GetString("CAMBIOS") > 0 Then
'					f1=Starter.skmt.ExecQuery2("select  sum(HVD_CANT) as CAMBIOS  from hist_ventas where SUBSTR(HVD_PRONOMBRE,1,6) =?", Array As String("CAMBIO"))
'					f1.Position = 0
'					Starter.skmt.ExecNonQuery2("delete from CAT_VARIABLES WHERE CAT_VA_DESCRIPCION =?", Array As Object("CAMBIOS"))
'					Starter.skmt.ExecNonQuery2("INSERT INTO CAT_VARIABLES VALUES(?,?)", Array As Object("CAMBIOS",f1.GetString("CAMBIOS")))
'					f1.Close
'				Else
'					Starter.skmt.ExecNonQuery2("delete from CAT_VARIABLES WHERE CAT_VA_DESCRIPCION =?", Array As Object("CAMBIOS"))
'					Starter.skmt.ExecNonQuery2("INSERT INTO CAT_VARIABLES VALUES(?,?)", Array As Object("CAMBIOS","0"))
'				End If
'				f2.Close
				Listo3 =1
				If Listo1 = 1  And Listo3 = 1  Then
					ToastMessageShow("Datos Actualizados." , True)
					B4XPage_Appear
					img2.Visible = False
					EJECUTANDO = 0
				End If
			End If
		End If

		If Job.JobName = "DBRequest" Then
			Dim result As DBResult = reqManager.HandleJob(Job)
			If result.Tag = "variables" Then 'query tag
				For Each records() As Object In result.Rows
					Dim Cat_Va_Descripcion As String = records(result.Columns.Get("CAT_VA_DESCRIPCION"))
					Dim Cat_Va_Valor As String = records(result.Columns.Get("CAT_VA_VALOR"))
'					Log("  ++  insert into CAT_VARIABLES: "&Cat_Va_Descripcion&","&Cat_Va_Valor)
					Starter.skmt.ExecNonQuery2("INSERT INTO CAT_VARIABLES(CAT_VA_DESCRIPCION, CAT_VA_VALOR) VALUES (?,?)", Array As Object (Cat_Va_Descripcion, Cat_Va_Valor))
				Next
			End If
		End If

		If Job.JobName = "DBRequest" Then
			Dim result As DBResult = reqManager.HandleJob(Job)
			If result.Tag = "valida_pedido" Then 'query tag
				For Each records() As Object In result.Rows
					Dim CUANTOSP As Int = records(result.Columns.Get("CUANTOSP"))
					If cuantos_pedido = CUANTOSP Then
						' ToastMessageShow("rojo val ok 1 cuantosp." , True)
						datos_iguales = "ok"
					Else
						ToastMessageShow("No se cargo bien la info P. Sync Nuevamente" & CUANTOSP & " " & cuantos_pedido, True)
					End If
				Next
			End If
		End If

		If Job.JobName = "DBRequest" Then
			Dim result As DBResult = reqManager.HandleJob(Job)
			If result.Tag = "valida_pedidoc" Then 'query tag
				For Each records() As Object In result.Rows
					Dim CUANTOSC As Int = records(result.Columns.Get("CUANTOSC"))
					If cuantos_pedidosc = CUANTOSC Then
						' ToastMessageShow("rojo val ok 1 cuantosp." , True)
						datos_iguales = "ok"
					Else
						ToastMessageShow("No se cargo bien la info C. Sync Nuevamente"  & CUANTOSC & " " & cuantos_pedidosc, True)
					End If
				Next
			End If
		End If

		If Job.JobName = "DBRequest" Then
			Dim result As DBResult = reqManager.HandleJob(Job)
			If result.Tag = "valida_noventa" Then 'query tag
				For Each records() As Object In result.Rows
					Dim CUANTOSN As Int = records(result.Columns.Get("CUANTOSN"))
					If cuantos_noventa = CUANTOSN Then
						' ToastMessageShow("rojo val ok 1 cuantosp." , True)
						datos_iguales = "ok"
					Else
						ToastMessageShow("No se cargo bien la info N. Sync Nuevamente"  & CUANTOSN & " " & cuantos_noventa, True)
					End If
				Next
			End If
		End If

		If Job.JobName = "DBRequest" Then
			Dim result As DBResult = reqManager.HandleJob(Job)
			If result.Tag = "insert_pedido" Then 'query tag
				ToastMessageShow("Datos Actualizados." , True)
			End If
		End If
		 
		If Job.JobName = "DBRequest" Then
			Dim result As DBResult = reqManager.HandleJob(Job)
			If result.Tag = "version" Then 'query tag
				For Each records() As Object In result.Rows
					Starter.skmt.ExecNonQuery("delete from VERSION")
					Dim CAT_VE_VERSION As String = records(result.Columns.Get("CAT_VE_VERSION"))
'					Log("  ++  insert into VERSION: "&CAT_VE_VERSION)
					Starter.skmt.ExecNonQuery2("INSERT INTO VERSION(NOVERSION) VALUES (?)", Array As Object (CAT_VE_VERSION))
				Next
			End If
		End If

		If Job.JobName = "DBRequest" Then
			Dim result As DBResult = reqManager.HandleJob(Job)
			If result.Tag = "count_cli" Then 'query tag
				For Each records() As Object In result.Rows
					COUNT_CLIE = records(result.Columns.Get("COUNT_CLIE"))
				Next
			End If
		End If

		If Job.JobName = "DBRequest" Then
			Dim result As DBResult = reqManager.HandleJob(Job)
			If result.Tag = "ruta" Then 'query tag
				If reqs.IndexOf("ruta") > -1 Then
					reqs.RemoveAt(reqs.IndexOf("ruta"))
					Log(reqs.Size)
				End If
				For Each records() As Object In result.Rows
					Dim VALIDO As String = records(result.Columns.Get("VALIDO"))
'					Log("VALIDO= " & VALIDO)
					If VALIDO = "OK" Then
						If l_ctast.Text = "0" Then
							Subir.Visible = True
						End If
						cargar.Visible = True
						If Subs.dameUsuarioDeDB = "ROOT" Then Subir.Visible = True
						connecta.Visible = False
						Starter.skmt.ExecNonQuery("DELETE FROM RUTAA")
'						Log("  ++  insert into RUTAA: "&e_ruta.Text)
						Starter.skmt.ExecNonQuery2("INSERT INTO RUTAA(RUTAA) VALUES (?)", Array As Object (e_ruta.TEXT))
						B4XPages.MainPage.rutaPreventa = e_ruta.TEXT
						If conn = "1" Then
							ToastMessageShow("Existe Conexión con el Servidor." , True)
						End If
					Else
						cargar.Visible = False
						Subir.Visible = False
						inv.Visible = False
						connecta.Visible = False
					End If
				Next
			End If
		End If

		If Job.JobName = "DBRequest" Then 'Traemos las coordenadas del CEDIS. (Parte de la funcionalidad OSRM)
			Dim result As DBResult = reqManager.HandleJob(Job)
			If result.Tag = "coords_almacen" Then 'query tag
				For Each records() As Object In result.Rows
					Log("Coordenadas del almacen: " & records(result.Columns.Get("CAT_AL_LATITUD")) & "," & records(result.Columns.Get("CAT_AL_LONGITUD")))
					Private lat, lon As Double
					lat = 0.0
					lon = 0.0
					If IsNumber(records(result.Columns.Get("CAT_AL_LATITUD"))) And IsNumber(records(result.Columns.Get("CAT_AL_LONGITUD"))) Then
						lat = records(result.Columns.Get("CAT_AL_LATITUD"))
						lon = records(result.Columns.Get("CAT_AL_LONGITUD"))
					End If
					Starter.cedisLocation.Latitude = lat
					Starter.cedisLocation.Longitude = lon
					Starter.skmt.ExecNonQuery2("delete from CAT_VARIABLES where CAT_VA_DESCRIPCION = ?", Array As Object ("COORDS_ALMACEN"))
					Starter.skmt.ExecNonQuery2("INSERT INTO CAT_VARIABLES(CAT_VA_DESCRIPCION, CAT_VA_VALOR) VALUES (?,?)", Array As Object ("COORDS_ALMACEN",lon&","&lat))
					'19.48118148992086,-99.15295579261536
				Next
			End If
		End If

		If Job.JobName = "DBRequest" Then 
			Dim result As DBResult = reqManager.HandleJob(Job)
			If result.Tag = "ins_reparto" Then 'query tag
				For Each records() As Object In result.Rows
					For Each k As String In result.Columns.Keys
						Log(k & ": " & records(result.Columns.Get(k)))
					Next
				Next
			End If
		End If

		If Job.JobName = "DBRequest" Then
			Dim result As DBResult = reqManager.HandleJob(Job)
			If result.Tag = "ins_rechazos" Then 'query tag
				For Each records() As Object In result.Rows
					For Each k As String In result.Columns.Keys
						Log(k & ": " & records(result.Columns.Get(k)))
					Next
				Next
			End If
		End If
		Job.Release
	End If
	If reqs.Size = 0 Then
		
		
		ProgressDialogHide
			
		c = Starter.skmt.ExecQuery("SELECT COUNT(*) AS CUANTOS FROM CHECADO_CHECK")
		c.Position = 0
		If c.GetString("CUANTOS") = 0 Then
			Starter.skmt.ExecNonQuery2("insert into CHECADO_CHECK (CHECADO) VALUES(?)", Array As String("0"))
			
		Else If c.GetString("CUANTOS") = 1 Then

		End If
		c.Close
'		Log(reqs.size)
'		Log("Terminamos lo descarga de ruta!")
	End If
End Sub

Private Sub B4XPage_CloseRequest As ResumableSub
	' BACK key pressed
	If Panel1.visible Then
		Log("PANEL1 VISIBLE")
		B_REGRESA_Click
	Else
		B4XPages.ShowPage("Login")
	End If
	' Returning False signals the system to handle the key
	Return False
End Sub

Sub connecta_Click
	LogColor($"ReqServer = ${B4XPages.MainPage.server}"$, Colors.red)
	If Not(IsConnectedToInternet) Then MsgboxAsync("No hay conexión a conexión a Internet", "Revisa tu conexión")
	If e_ruta.Text <> "" Then
		imei = "" 'p.GetDeviceId
		conn = "1"
		ime.HideKeyboard
		Dim cmd As DBCommand
		cmd.Initialize
		cmd.Name = "select_version_MARQUEZ"
		reqManager.ExecuteQuery(cmd , 0, "version")
		Dim cmd As DBCommand
		cmd.Initialize
		cmd.Name = "select_ruta_re_REPG_marquez"
		cmd.Parameters = Array As Object(ALMACEN, e_ruta.text, imei)
'		Log($"${ALMACEN}, ${e_ruta.text}, ${imei}"$)
		reqManager.ExecuteQuery(cmd , 0, "ruta")
		ToastMessageShow("Validando Conexión." , True)
		Starter.skmt.ExecNonQuery("DELETE FROM RUTAA")
		Starter.skmt.ExecNonQuery2("INSERT INTO RUTAA(RUTAA) VALUES (?)", Array As Object (e_ruta.TEXT))
		B4XPages.MainPage.rutaPreventa = e_ruta.TEXT
	End If
End Sub

Sub BUSCA_Click
	If b_check.Visible = True Then
		MsgboxAsync("Favor de hacer el checklist.","Atención")
	Else
'	StartActivity(BUSCAR)
	B4XPages.ShowPage("Buscar")
	End If
End Sub

Sub b_mapa_Click
'	StartActivity(mapas)
	B4XPages.ShowPage("mapas")
End Sub

Sub e_ruta_EnterPressed
	If e_ruta.Text = "FIN DIA" Then
		RES =  Msgbox2("Seguro que desea hacer el cierre todos los datos se borraran?","Cierre", "Si", "", "No",LoadBitmap(File.DirAssets,"alert2.png")) 'ignore
		If RES = DialogResponse.POSITIVE Then
			e_ruta.Text = ""
			Starter.skmt.ExecNonQuery("delete from pedido_cliente")
			Starter.skmt.ExecNonQuery("delete from CHECADO_CHECK")
			Starter.skmt.ExecNonQuery("delete from CHECKLIST2")
			Starter.skmt.ExecNonQuery("delete from pedido")
			Starter.skmt.ExecNonQuery("delete from noventa")
			Starter.skmt.ExecNonQuery("delete from clie_act")
			Starter.skmt.ExecNonQuery("delete from kmt_info")
			Starter.skmt.ExecNonQuery("delete from cat_gunaprod")
			Starter.skmt.ExecNonQuery("delete from CAT_DETALLES_PAQ")
			Starter.skmt.ExecNonQuery("delete from HIST_VENTAS")
			Starter.skmt.ExecNonQuery("delete from REPARTO")
			Starter.skmt.ExecNonQuery("DELETE FROM TABULADOR_MONEDAS")
			Starter.skmt.ExecNonQuery("DELETE FROM TABULADOR_BILLETES")
			Starter.skmt.ExecNonQuery("DELETE FROM RUTAA")
			B4XPage_Appear
		End If
'		Starter.waypointsOrdered.Clear
		Starter.skmt.ExecNonQuery("delete from waypoints")
	End If
End Sub

Sub inv_Click
	ToastMessageShow("Se Actualizaran los datos, Este proceso podria tardar hasta un minuto, gracias" , True)
End Sub

Sub t1_tick
	cmd.Initialize
	cmd.Name = "select_cuantos_pedido"
	cmd.Parameters = Array As Object(ALMACEN,l_ruta.text)
	reqManager.ExecuteQuery(cmd , 0, "valida_pedido")
	
	cmd.Initialize
	cmd.Name = "select_cuantos_noventa_mards"
	cmd.Parameters = Array As Object(ALMACEN,l_ruta.text)
	reqManager.ExecuteQuery(cmd , 0, "valida_noventa")
	
	cmd.Initialize
	cmd.Name = "select_cuantos_pedidoc"
	cmd.Parameters = Array As Object(ALMACEN,l_ruta.text)
	reqManager.ExecuteQuery(cmd , 0, "valida_pedidoc")
	img2.Visible=False
	t1.Enabled = False
End Sub

Sub B_CLIEPROD_Click
	If b_check.Visible = True Then
		MsgboxAsync("Favor de hacer el checklist.","Atención")
	Else
	BUSCA.Visible = False
	connecta.Visible = False
	trabajar.Visible = False
	B_CLIEPROD.Visible = False
	Panel1.Visible = True
	Panel1.BringToFront
	Btn_Ubicar_Ru.Visible=False
	c=Starter.skmt.ExecQuery("select HVD_PRONOMBRE, SUM(HVD_CANT) AS HVD_CANT, HVD_RECHAZO from HIST_VENTAS GROUP BY HVD_PRONOMBRE, HVD_RECHAZO ORDER BY HVD_PRONOMBRE")
	Subs.SetDivider(ListView1, Colors.LightGray, 2)
	ListView1.Clear
	Dim label1 As Label
	label1 = ListView1.TwoLinesLayout.Label
	label1.TextSize = 10
	label1.TextColor = Colors.Black
	Dim label2 As Label
	label2 = ListView1.TwoLinesLayout.SecondLabel
	label2.TextSize = 10
	label2.TextColor = Colors.Black
			
	If c.RowCount>0 Then
		For i=0 To c.RowCount -1
			c.Position=i
			If c.GetString("HVD_RECHAZO") = "0" Then
				ESTATUS = " "
			ELSE IF c.GetString("HVD_RECHAZO") = "1" Then
				ESTATUS = "RECHAZADO"
			Else
				ESTATUS = "SIN ESTATUS"
			End If
			ListView1.AddTwoLines(c.GetString("HVD_PRONOMBRE") , c.GetString("HVD_CANT") & "   " & ESTATUS )
		Next
	End If
	
	

'	c=skmt.ExecQuery("select HVD_CLIENTE, HVD_PRONOMBRE, HVD_CANT, HVD_ESTATUS  from HIST_VENTAS ORDER BY HVD_PRONOMBRE")
'		
'	ListView1.Clear
'	Dim label1 As Label
'	label1 = ListView1.TwoLinesLayout.Label
'	label1.TextSize = 10
'	label1.TextColor = Colors.Black
'	Dim label2 As Label
'	label2 = ListView1.TwoLinesLayout.SecondLabel
'	label2.TextSize = 10
'	label2.TextColor = Colors.Black
'			
'	If c.RowCount>0 Then
'		For i=0 To c.RowCount -1
'			c.Position=i
'			If c.GetString("HVD_ESTATUS") = "0" Then
'				ESTATUS = "SIN ENTREGAR"
'			ELSE IF c.GetString("HVD_ESTATUS") = "1" Then
'				ESTATUS = "ENTREGADO"
'			ELSE IF c.GetString("HVD_ESTATUS") = "2" Then
'				ESTATUS = "RECHAZADO"
'			Else 
'				ESTATUS = "SIN ESTATUS"
'			End If
'			ListView1.AddTwoLines(c.GetString("HVD_PRONOMBRE") & "   " & c.GetString("HVD_CLIENTE") , c.GetString("HVD_CANT") & "   " & ESTATUS )
'		Next
'	End If
End If
End Sub

Sub B_REGRESA_Click
	Log("B_REGRESA_Click")
	BUSCA.Visible = True
	connecta.Visible = True
	trabajar.Visible = True
	B_CLIEPROD.Visible = True
	Panel1.Visible = False
	Btn_Ubicar_Ru.Visible=True
End Sub

''''''''''''''''''''''''''''MAPA RUTAS
Sub Btn_Ubicar_Ru_Click
	If b_check.Visible = True Then
		MsgboxAsync("Favor de hacer el checklist.","Atención")
	Else
	
	StartActivity(MAPA_RUTAS)
	End If
End Sub
'You can see the list of page related events in the B4XPagesManager object. The event name is B4XPage.

Private Sub b_tabulador_Click
	If b_check.Visible = True Then
		MsgboxAsync("Favor de hacer el checklist.","Atención")
	Else
	B4XPages.ShowPage("tabulador")
	End If
End Sub

Private Sub b_check_Click
	
	B4XPages.ShowPage("checklist")
	
End Sub