B4A=true
Group=Default Group
ModulesStructureVersion=1
Type=Class
Version=11.5
@EndOfDesignText@
Sub Class_Globals
	Private Root As B4XView 'ignore
	Private xui As XUI 'ignore
	'// Process Globals
	Dim ruta As String
	Dim q_buscar As String
	Dim MAC_IMPRESORA As String
	'// Globals
	Dim c As Cursor
	Dim c2 As Cursor
	Dim s As Cursor
	Dim ListView1 As ListView
	Dim entro As String
	Dim gest As Button
	Dim lfila As Label
	Dim busca As EditText
	Dim colonia As String 'ignore
	Private b_qr As Button
	Private qr As QRCode
	Dim sc As Zxing_scanner
	Dim CODIGO As String
	Private b_bdd As Button
	Private SEMANA As String
	Private B_IMP As Button
	Dim logger As Boolean = True
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("Clientes")
	entro ="2"
	ruta = File.DirInternal
	qr.initialize
	c=Starter.skmt.ExecQuery2("select CAT_VA_VALOR from CAT_VARIABLES WHERE CAT_VA_DESCRIPCION = ?", Array As String ("MACIMP"))
	c.Position =0
	MAC_IMPRESORA = c.GetString("CAT_VA_VALOR")
	Starter.MAC_IMPRESORA = MAC_IMPRESORA
	If Not(Starter.Logger) Then logger = False
End Sub

'You can see the list of page related events in the B4XPagesManager object. The event name is B4XPage.

Sub B4XPage_Appear
	B_IMP.Visible = False
'	If Logger Then Log(Me)
'	If Logger Then LogColor("Ultima actividad guardada=" & Subs.traeUltimaActividadBD, Colors.Blue)
'	If Subs.traeUltimaActividadBD <> "colonia" Then Subs.iniciaActividad(Subs.traeUltimaActividadBD)
	c=Starter.skmt.ExecQuery2("select count(*) AS CUANTOS from CAT_VARIABLES WHERE CAT_VA_DESCRIPCION = ?", Array As String ("SEMANA"))
	c.Position =0
	SEMANA = c.GetString("CUANTOS")
	c.Close
	If SEMANA > 0 Then
		c=Starter.skmt.ExecQuery2("select CAT_VA_VALOR from CAT_VARIABLES WHERE CAT_VA_DESCRIPCION = ?", Array As String ("SEMANA"))
		c.Position =0
		SEMANA = c.GetString("CAT_VA_VALOR")
		c.Close
	End If
	b_bdd.Visible = True
'	b_qr.Visible = True
	busca.Text = ""
	entro = "2"
	colonia = 0
	c = Starter.skmt.ExecQuery2("select CAT_CL_NOMBRE, CAT_CL_CALLE, CAT_CL_CODIGO, CAT_CL_NUM_SERIEFISICO from kmt_info where CAT_CL_DIAS_VISITA = (SELECT CAT_VA_VALOR FROM CAT_VARIABLES WHERE CAT_VA_DESCRIPCION = ?) and gestion = 0 and cat_cl_codigo not in (select pe_cliente from pedido) and cat_cl_codigo <> 0 AND CAT_CL_TIPO_RUTA IN (?,?) and CAT_CL_CODIGO not in (select NV_CLIENTE from NOVENTA) ORDER BY CAT_CL_NUM_SERIEFISICO", Array As String("DIA_VISITA", SEMANA, "SEMANAL"))
	ListView1.Clear
	lfila.Text = "NOMBRE"
	If c.RowCount > 0 Then
		For i = 0 To c.RowCount - 1
			c.Position = i
			Dim label1 As Label
			label1 = ListView1.TwoLinesLayout.Label
			label1.TextSize = 14
			label1.TextColor = Colors.Black
			Dim label2 As Label
			label2 = ListView1.TwoLinesLayout.SecondLabel
			label2.TextSize = 14
			label2.TextColor = Colors.Black
			ListView1.AddTwoLines(c.GetString("CAT_CL_CODIGO"), c.GetString("CAT_CL_NUM_SERIEFISICO") & " " & c.GetString("CAT_CL_NOMBRE")  )
		Next
	End If
	entro = "4"
End Sub

Private Sub B4XPage_CloseRequest As ResumableSub
	' BACK key pressed
	Subs.iniciaActividad("principal")
'	B4XPages.ShowPage("Principal")
	Return True
End Sub

Sub ListView1_ItemClick (Position As Int, Value As Object)
'	If Logger Then Log(Regex.Split("\.",Me)(Regex.Split("\.",Me).Length-1))
	Private Subrutina As Map =CreateMap("nombre":"ListView1_ItemClick", "param1":Position, "param2":Value)
	Starter.sesion = CreateMap("actividad":Me, "sub":Subrutina)
	If colonia = 0 Then
		colonia = Value
	End If
	If entro = "2" Then
		c2=Starter.skmt.ExecQuery2("select CAT_CL_CALLE, CAT_CL_COLONIA, count(*) as cuantos from kmt_info where gestion = 0 and CAT_CL_COLONIA = ? GROUP BY CAT_CL_CALLE, CAT_CL_COLONIA order by CAT_CL_CALLE ",  Array As String(Value))
		ListView1.Clear
		lfila.text = "Calle"
		If c2.RowCount>0 Then
			For i=0 To c2.RowCount -1
				c2.Position=i
				ListView1.AddTwoLines(c2.GetString("CAT_CL_CALLE"),c2.GetString("cuantos") )
			Next
		End If
		entro = "3"
	Else If entro = "3" Then
		c2 = Starter.skmt.ExecQuery2("select CAT_CL_NOMBRE, CAT_CL_CALLE, CAT_CL_CODIGO from kmt_info where gestion = 0 and CAT_CL_CALLE = ? AND CAT_CL_COLONIA = ? order by CAT_CL_NOMBRE ", Array As String(Value, colonia))
		ListView1.Clear
		lfila.text = "Nombre"
		If c2.RowCount > 0 Then
			For i = 0 To c2.RowCount - 1
				c2.Position = i
				ListView1.AddTwoLines(c2.GetString("CAT_CL_CODIGO"), c2.GetString("CAT_CL_NOMBRE"))
			Next
		End If
		entro = "4"
	Else If entro = "4" Then
		Starter.skmt.ExecNonQuery("delete from CUENTAA")
		Starter.skmt.ExecNonQuery2("INSERT INTO CUENTAA VALUES (?)", Array As Object(Value))
		Subs.iniciaActividad("Cliente")
	End If
End Sub

Sub Activity_KeyPress (key As Int) As Boolean 'ignore
	' BACK key pressed
	If key=KeyCodes.KEYCODE_BACK Then
		Subs.iniciaActividad("Principal")
		Return False
		'End If
	End If
	' Returning False signals the system to handle the key
End Sub

Sub BUSCA_TextChanged (Old As String, New As String)
'	Log($"${Old}|${New}"$)
	q_buscar = "%" & busca.Text & "%"
	c2 = Starter.skmt.ExecQuery2("select CAT_CL_NOMBRE, CAT_CL_CALLE, CAT_CL_CODIGO from kmt_info where (CAT_CL_NOMBRE like ? OR CAT_CL_CODIGO LIKE ?) and CAT_CL_DIAS_VISITA = (SELECT CAT_VA_VALOR FROM CAT_VARIABLES WHERE CAT_VA_DESCRIPCION = ?) and gestion = 0 order by CAT_CL_NOMBRE ", Array As String(q_buscar,q_buscar,"DIA_VISITA"))
	ListView1.Clear
	lfila.text = "Nombre y Calle"
	If c2.RowCount > 0 Then
		For i = 0 To c2.RowCount - 1
			c2.Position = i
			Dim label1 As Label
			label1 = ListView1.TwoLinesLayout.Label
			label1.TextSize = 14
			label1.TextColor = Colors.Black
			Dim label2 As Label
			label2 = ListView1.TwoLinesLayout.SecondLabel
			label2.TextSize = 14
			label2.TextColor = Colors.Black
			ListView1.AddTwoLines(c2.GetString("CAT_CL_CODIGO"), c2.GetString("CAT_CL_NOMBRE") &" CALLE:  "& c2.GetString("CAT_CL_CALLE"))
		Next
	End If
	entro = "4"
	c2.Close
End Sub

Sub b_qr_Click
	Dim scan_width As Int
	Dim scan_height As Int
	scan_width = 400
	scan_height = 400
	sc.BeginScan("sc","CODIGO CLIENTE",scan_width,scan_height)
End Sub

Sub sc_result(atype As String,Values As String)
	'If Logger Then Log("Type : " & atype)
	'If Logger Then Log("Value : " & Values)
	CODIGO = Values
	c2 = Starter.skmt.ExecQuery2("select COUNT(*) AS ENCONTRADO from kmt_info where CAT_CL_NUM_SERIEFISICO = ? order by CAT_CL_NOMBRE ", Array As String(CODIGO))
	c2.Position = 0
	If c2.GetString("ENCONTRADO") > 0 Then
		s = Starter.skmt.ExecQuery2("select CAT_CL_CODIGO from kmt_info where CAT_CL_NUM_SERIEFISICO = ? order by CAT_CL_NOMBRE ", Array As String(CODIGO))
		s.Position = 0
		Starter.skmt.ExecNonQuery("delete from CUENTAA")
		Starter.skmt.ExecNonQuery2("INSERT INTO CUENTAA VALUES (?)", Array As Object(s.GetString("CAT_CL_CODIGO")))
		s.Close
		b_qr.Visible = False
		Subs.iniciaActividad("Cliente")
	Else
		Msgbox("CODIGO " & CODIGO & " NO ENCONTRADO","AVISO") 'ignore
	End If
End Sub

Sub sc_noScan
	If logger Then Log("nothing returned from the scan !!!!!")
End Sub

Sub calc_ean_checksum(number As String) As String           'this has now become redundant as I am only interested in QR Codes
	Dim i As Int
'	Dim cO As Char
	Dim soma As Int
	Dim n As Int
	Dim digit As Float
	soma = 0
	For i=0 To number.Length - 1
		digit = number.SubString2(i,i+1)
		n= digit * ((i Mod 2) * 2 + 1)
		soma=soma+n
	Next
	Return number & ( ( 10 - ( soma Mod 10 )) Mod 10 )
End Sub

Sub b_bdd_Click
	q_buscar = "%" & busca.Text & "%"
	c2 = Starter.skmt.ExecQuery2("select CAT_CL_NOMBRE, CAT_CL_CALLE, CAT_CL_CODIGO from kmt_info where (CAT_CL_NOMBRE like ? OR CAT_CL_CODIGO LIKE ?) and gestion = 0 order by CAT_CL_NOMBRE ", Array As String(q_buscar,q_buscar))
	ListView1.Clear
	lfila.text = "Nombre y Calle"
	If c2.RowCount > 0 Then
		For i = 0 To c2.RowCount - 1
			c2.Position = i
			Dim label1 As Label
			label1 = ListView1.TwoLinesLayout.Label
			label1.TextSize = 14
			label1.TextColor = Colors.Black
			Dim label2 As Label
			label2 = ListView1.TwoLinesLayout.SecondLabel
			label2.TextSize = 14
			label2.TextColor = Colors.Black
			ListView1.AddTwoLines(c2.GetString("CAT_CL_CODIGO"), c2.GetString("CAT_CL_NOMBRE") &" CALLE:  "& c2.GetString("CAT_CL_CALLE"))
		Next
	End If
	entro = "4"
	c2.Close
End Sub

'Imprimimos clientes.
Sub B_IMP_Click
'	If Printer1.IsInitialized And Printer1.IsConnected = False Then
'		Printer1.Connect
'		ToastMessageShow("Conectando impresora ...", False)
'	End If
'	Try
'		c=Starter.skmt.ExecQuery2("select  CAT_CL_NOMBRE, CAT_CL_CALLE, CAT_CL_CODIGO, CAT_CL_NUM_SERIEFISICO from kmt_info where  CAT_CL_DIAS_VISITA = (SELECT CAT_VA_VALOR FROM CAT_VARIABLES WHERE CAT_VA_DESCRIPCION = ?) and gestion = 0 and cat_cl_codigo not in (select pe_cliente from pedido) and cat_cl_codigo <> 0 AND CAT_CL_TIPO_RUTA IN (?,?)  ORDER BY CAT_CL_NUM_SERIEFISICO", Array As String("DIA_VISITA",SEMANA, "SEMANAL"))
	''	c=skmt.ExecQuery("select  CAT_CL_NOMBRE, CAT_CL_CALLE, CAT_CL_CODIGO,CAT_CL_DIAS_VISITA from kmt_info where   gestion = 0 and cat_cl_codigo <> 0 ORDER BY CAT_CL_CODIGO")
'		If c.RowCount>0 Then
'			For i=0 To c.RowCount -1
'				c.Position=i
'				Printer1.WriteString("ID.  " & c.GetString("CAT_CL_CODIGO") & CRLF)
'				Printer1.WriteString("Nombre " & c.GetString("CAT_CL_NUM_SERIEFISICO") & " " & c.GetString("CAT_CL_NOMBRE")  & CRLF)
'				Printer1.WriteString("------------------------------"  & CRLF)
'			Next
'		End If
'	Catch
'		Log(LastException)
'	End Try
End Sub

Sub revisaImpresora (mode As String) 'ignore
'	If Not(Starter.btAdmin.IsEnabled) Then Starter.btAdmin.Enable 'Si no esta prendido el BT lo prendemos.
'	If Not(Printer1.IsInitialized) Then Printer1.Initialize(Me, "Printer1") 'Si no esta inicializada la inicializamos.
'	If Not(Starter.revisandoImpresora) Then Return
'	Starter.revisandoImpresora = True
'	If mode = "BT" Then
	''		If Logger Then LogColor("reiniciamos BT", Colors.Blue)
	''		Starter.btAdmin.Disable
	''		Sleep(500)
	''		Starter.btAdmin.Enable
	''		Sleep(1000)
	''		If Starter.btAdmin.IsEnabled Then revisaImpresora("")
'	Else
	''		ToastMessageShow("Conectando impresora.", False)
'		If Not(Printer1.IsConnected) Then 'Si no estamos conectados, nos conectamos.
'			If Logger Then Log("Connect")
'			Printer1.Connect
'		Else 'Si ya estamos conectados
'			If Logger Then Log("Disc-Conn")
'			Printer1.DisConnect
'			Sleep(1000)
'			Printer1.Connect
'		End If
'	End If
End Sub