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
	Dim g As GPS
	Dim ruta As String
	Dim c As Cursor	
	Dim CANCELA As Button
	Dim GUARDA As Button
	Dim cuenta As String
	Dim usuario As String
	Dim sDate,sTime As String
	Dim sDate2,sTime2 As String
	Dim no_cliente As String
	Dim no_ruta As String	
	Dim r_4 As RadioButton
	Dim E_NOMBRE As EditText
	Dim tgl As Toggle
	Private l_sinUbicacion As Label
	Private p_nuevoCliente As Panel
	Private Label1 As Label
	Private p_botones As Panel
	Private p_cam As Panel
	Private p_camara As Panel
	Private b_foto_inci As Button
	Dim frontCamera As Boolean = False
	Dim fototomada As String = "0"
	Dim ALMACEN As String
	Private et_direccion As EditText
	Private Label2 As Label
	Dim lat As Double = 0
	Dim lon As Double = 0
	Private b_ubicacion As Label
	Private Label3 As Label
	Private cb_giro As B4XComboBox
	Dim giros As String
	Dim hr_ini As String
	Dim hr_fin As String
	Private L_Atiende As Label
	Private ET_Atiende As EditText
	Private Label5 As Label
	Private et_colonia As EditText
	Private et_telefono As EditText
	Private et_referencia As EditText
	Private l_referecia As Label
	Private l_hora_final As Label
	Private l_hora_inicial As Label
	Private cb_hora_ini As B4XComboBox
	Private cb_hora_fin As B4XComboBox
	Private l_fin As Label
	Private l_ini As Label
End Sub

Public Sub Initialize As Object
	Return Me
End Sub

Private Sub B4XPage_Created (Root1 As B4XView)
	Root = Root1
	Root.LoadLayout("nuevocliente")
	ruta = File.DirInternal
	If File.Exists(ruta, "kmt.db") = False Then
		File.Copy(File.DirAssets, "kmt.db", ruta, "kmt.db")
	End If
	Starter.rp.CheckAndRequest(Starter.rp.PERMISSION_WRITE_EXTERNAL_STORAGE)
	Wait For B4XPage_PermissionResult (Permission As String, Result As Boolean)
	If Result Then
		Log("Con permisos para escritura")
	Else
		ToastMessageShow("Sin permisos de escritura!!!", True)
	End If
	Starter.rp.CheckAndRequest(Starter.rp.PERMISSION_READ_EXTERNAL_STORAGE)
	Wait For B4XPage_PermissionResult (Permission As String, Result As Boolean)
	If Result Then
		Log("Con permisos para escritura")
	Else
		ToastMessageShow("Sin permisos de lectura!!!", True)
	End If
End Sub

Sub B4XPage_Appear
	et_direccion.Text = ""
	E_NOMBRE.Text =  ""
	ET_Atiende.Text = ""
	et_colonia.Text = ""
	et_telefono.Text = ""
	et_referencia.Text = ""
	If Not(Starter.gps.GPSEnabled) Then
		ToastMessageShow("Es necesario tener el GPS encendido", True)
		StartActivity(Starter.gps.LocationSettingsIntent)
	End If
	GUARDA.Visible = False
	Subs.panelVisible(p_nuevoCliente, 0, 0)
	p_nuevoCliente.Height = Root.Height
	p_nuevoCliente.Width = Root.Width
	Subs.centraEtiqueta(Label1, Root.Width)
	Subs.centraEtiqueta(Label5, Root.Width)
	Subs.centraEtiqueta(Label2, Root.Width)
	Subs.centraEtiqueta(Label3, Root.Width)
	Subs.centraEtiqueta(l_sinUbicacion, Root.Width)
	Subs.centraEtiqueta(b_ubicacion, Root.Width)
	Subs.centraPanel(p_botones, Root.Width)
	E_NOMBRE.Left = Round(Root.Width/2)-(E_NOMBRE.Width/2)
	et_colonia.Left = Round(Root.Width/2)-(et_colonia.Width/2)
	et_direccion.Left = Round(Root.Width/2)-(et_direccion.Width/2)
	If B4XPages.MainPage.lat_gps <> "0.0" Then
		GUARDA.Visible = True 'Si hay ubicaccion, mostramos el boton de guardar.
		l_sinUbicacion.Visible = True
	End If
	
	If Not(Starter.GPS.GPSEnabled) Then
		ToastMessageShow("Debe Activar el GPS del Equipo.", True)
		StartActivity(Starter.GPS.LocationSettingsIntent)
	Else
		Starter.GPS.Start(0, 0)
	End If
	CallSubDelayed(Tracker, "StartFLPSmall")
	
	c = Starter.skmt.ExecQuery("SELECT GIRO FROM CAT_GIRO order by 1")
	Dim Items As List
	Items.Initialize
	Items.Add("SELECCIONA UNA OPCIÓN")
	If c.RowCount > 0 Then
		For i = 0 To c.RowCount-1
			c.Position = i
			Items.Add(c.GetString("GIRO"))
		Next
		cb_giro.SetItems(Items)
	End If
	giros = "SELECCIONA UNA OPCIÓN"
	c.Close

	Dim Items2 As List
	Items2.Initialize
	Items2.Add("SELECCIONA UNA OPCIÓN")
	For i = 7 To 23
		Dim hora As String
		hora = i
		Items2.Add(hora)
	Next
	cb_hora_ini.SetItems(Items2)
	
	hr_ini = ("SELECCIONA UNA OPCIÓN")

	Dim Item3 As List
	Item3.Initialize
	Item3.Add("SELECCIONA UNA OPCIÓN")
	cb_hora_fin.SetItems(Item3)
	hr_fin = "SELECCIONA UNA OPCIÓN"
	l_ini.Text=""
	l_fin.Text=""
	
End Sub

Sub GPS_LocationChanged (Location1 As Location)
	If B4XPages.MainPage.lat_gps <> "0.0" Then
		GUARDA.Visible = True 'Si hay ubicaccion, mostramos el boton de guardar.
		l_sinUbicacion.Visible = True
		lat = Location1.Latitude
		lon = Location1.Longitude
		l_sinUbicacion.Text = ("Latitud: " & lat & ", Longotud: "& lon)
	End If
	
	b_ubicacion.TextSize = 13
	b_ubicacion.Text = $"Precisión GPS $1.0{Location1.Accuracy} m"$
	If Location1.Accuracy > 200 Then
		b_ubicacion.TextColor = Colors.Red
		b_ubicacion.TextSize = 16
		b_ubicacion.Text = $"Mala señal $1.0{Location1.Accuracy} m"$
	End If
End Sub

Sub CANCELA_Click
	B4XPages.ShowPage("Principal")
	CallSubDelayed(Tracker, "StartFLP")
End Sub

Sub GUARDA_Click
	If E_NOMBRE.Text = "" Then
		MsgboxAsync("Por favor captura el nombre de la Tienda","Atención")
	Else If et_direccion.Text.Length > 3 Then
		If lat <> 0 Then
			If giros <> "SELECCIONA UNA OPCIÓN" Or cb_giro.SelectedItem <> "SELECCIONA UNA OPCIÓN" Then
				If ET_Atiende.Text <> "" Then
					If et_telefono.Text <> "" Then
						If et_colonia.Text <> "" Then
							If et_referencia.Text <> "" Then
								If cb_hora_fin.SelectedItem <> "SELECCIONA UNA OPCIÓN" And cb_hora_ini.SelectedItem <> "SELECCIONA UNA OPCIÓN" Then
								DateTime.DateFormat = "MM/dd/yyyy"
								DateTime.Timeformat = "HHmmss"
								sDate = DateTime.Date(DateTime.Now)
								sTime = DateTime.Time(DateTime.Now)
								Log($" ////////////   Date: ${sDate} - Time: ${sTime} ////////////////"$)
								Private hora As String = sTime.SubString2(0,2)
								Private mins As String = sTime.SubString2(2,4)
								Private segs As String = sTime.SubString(4)
								sTime = hora & ":" & mins & ":" & segs
								Log("////////////// sTime: "&sTime&" ////////////////")
								c = Starter.skmt.ExecQuery("select CAT_CL_RUTA FROM kmt_info")
								c.Position = 0
								no_ruta = c.GetString("CAT_CL_RUTA")
								no_cliente = "N" & sTime & no_ruta
								Log("++ ++ no_cliente = "&no_cliente)
								c.Close
								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_MTOCOMPRA,CAT_CL_NUM_SERIEFISICO, gestion, CAT_CL_GIRO) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,0,0,0,?) ", Array As Object (no_cliente,no_ruta, E_NOMBRE.Text,"null","null","null","null","null","null","null","null","null","null","null","null","null",B4XPages.MainPage.lon_gps,B4XPages.MainPage.lat_gps, cb_giro.SelectedItem))
								Starter.skmt.ExecNonQuery2("INSERT INTO HIST_STAY_STORE(HSS_CODIGO,  HSS_IN , HSS_OUT , HSS_TOT) VALUES (?,0,0,0)", Array As Object (no_cliente))
								Starter.skmt.ExecNonQuery("delete from CUENTAA")
								Starter.skmt.ExecNonQuery2("INSERT INTO CUENTAA VALUES (?)", Array As Object(no_cliente))
								c = Starter.skmt.ExecQuery("select ID_ALMACEN from CAT_ALMACEN")
								If c.RowCount > 0 Then
									c.Position = 0
									ALMACEN = c.GetString("ID_ALMACEN")
								End If
								c.Close
								c = Starter.skmt.ExecQuery("select usuario from usuarioa")
								c.Position = 0
								usuario = c.GetString("USUARIO")
								c.Close
								DateTime.DateFormat = "dd/MM/yyyy"
								DateTime.Timeformat = "HH:mm:ss"
								sDate2 = DateTime.Date(DateTime.Now)
								sTime2 = DateTime.Time(DateTime.Now)
        
								Starter.skmt.ExecNonQuery2("INSERT INTO CLIENTES_NUEVOS(CN_ID, CN_FECHA, CN_USER, CN_LAT, CN_LON, CN_NOMBRE, CN_DIRECCION, CN_FOTO, CN_RUTA, CN_GIRO, CN_SOLICITA,CN_COLONIA, CN_TELEFONO, CN_REFERENCIA, CN_HR_INICIO, CN_HR_FIN) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)", Array As String(no_cliente, sDate2 &" "&sTime2, usuario, lat, lon, E_NOMBRE.Text, et_direccion.Text, ALMACEN, no_ruta, cb_giro.SelectedItem, ET_Atiende.Text,et_colonia.Text,et_telefono.Text, et_referencia.Text, cb_hora_ini.SelectedItem, cb_hora_fin.SelectedItem))
								B4XPages.ShowPage("Principal")
								CallSubDelayed(Tracker, "StartFLP")
								Else
									MsgboxAsync("Por favor, debe capturar la apertura o el cierre del negocio.","Atención")
								End If
							Else
								MsgboxAsync("Por favor, debe capturar la referencia del negocio.","Atención")
							End If
						Else
							MsgboxAsync("Por favor, debe capturar la colonia el negocio.","Atención")
						End If
					Else
						MsgboxAsync("Por favor, debe capturar el telfono.","Atención")
					End If
				Else
					MsgboxAsync("Por favor, debe capturar al encargado que atiende el negocio.","Atención")
				End If
			Else
				MsgboxAsync("Por favor captura el giro del cliente","Atención")
			End If
		Else
			MsgboxAsync("Por favor revisa que tengas señal GPS","Atención")
		End If
	Else
		MsgboxAsync("Por favor captura la dirección del cliente","Atención")
	End If
End Sub

Private Sub p_nuevoCliente_Click
	
End Sub

Private Sub cb_giro_SelectedIndexChanged (Index As Int)
	giros = cb_giro.SelectedItem
End Sub


Private Sub cb_hora_ini_SelectedIndexChanged(Index As Int)
	Dim Item3 As List
	Item3.Initialize
	Item3.Add("SELECCIONA UNA OPCIÓN")
    
	If Index = 0 Then
		l_ini.Text = ""
		cb_hora_fin.SetItems(Item3) 
		Return
	End If

	Dim horaInicio As Int = cb_hora_ini.SelectedItem + 1
	For i = horaInicio To 23
		Dim hora As String
		hora = i
		Item3.Add(hora)
	Next
	cb_hora_fin.SetItems(Item3)

	If cb_hora_ini.SelectedItem >= 7 And cb_hora_ini.SelectedItem <= 11 Then
		l_ini.Text = "AM"
	Else If cb_hora_ini.SelectedItem >= 12 And cb_hora_ini.SelectedItem <= 23 Then
		l_ini.Text = "PM"
	End If
End Sub

Private Sub cb_hora_fin_SelectedIndexChanged(Index As Int)
	If Index = 0 Then
		l_fin.Text = ""
		Return
	End If

	If cb_hora_fin.SelectedItem >= 7 And cb_hora_fin.SelectedItem <= 11 Then
		l_fin.Text = "AM"
	Else If cb_hora_fin.SelectedItem >= 12 And cb_hora_fin.SelectedItem <= 23 Then
		l_fin.Text = "PM"
	End If
End Sub