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 skmt As SQL
	Dim c As Cursor
	
	Dim CANCELA As Button
	Dim GUARDA As Button
	Dim r_1 As RadioButton
	Dim r_2 As RadioButton
	Dim r_3 As RadioButton
	Dim e_comm As EditText
	Dim motivo As String
	Dim cuenta As String
	Dim usuario As String
	Dim sDate,sTime As String
'	Dim lat_gps, lon_gps As String
	Dim tgl As Toggle
	Dim r_4 As RadioButton
	
	Private stay_hh As String
	Private stay_mi As String
	Private stay_ss As String
	Private p_NoVenta As Panel
	Dim tipo_venta = Subs.traeTipoVentaDeBD
	Dim bitacora As C_Bitacora
	Private Panel1 As Panel
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("noventa")
	
	ruta = File.DirInternal
'	End If
	If File.Exists(ruta, "kmt.db") = False Then
		File.Copy(File.DirAssets, "kmt.db", ruta, "kmt.db")
	End If
'	skmt.Initialize(ruta,"kmt.db", True)
	bitacora.Initialize("bitacora", "bitacora", Root, B4XPages.MainPage.skmt, Starter.reqManager)
End Sub

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

Sub B4XPage_Appear
	Panel1.Height = Root.Height
	Panel1.Width = Root.Width
	Subs.centraPanel(Panel1, Root.Width)
	Subs.centraPanelV(Panel1, Root.Height)
	e_comm.Text=""
	tgl.Initialize
	If Not(Starter.gps.GPSEnabled) Then
		ToastMessageShow("Es necesario tener el GPS encendido", True)
		StartActivity(Starter.gps.LocationSettingsIntent)
	End If
	r_1.Checked = False
	r_2.Checked = False
	r_3.Checked = False
	r_4.Checked = False
End Sub

Sub GPS_LocationChanged (Location1 As Location)
	'lat_gps=Location1.ConvertToSeconds(Location1.Latitude)
	'lon_gps=Location1.ConvertToSeconds(Location1.Longitude)
End Sub

Sub CANCELA_Click
	B4XPages.ShowPage("Cliente")
End Sub

Sub GUARDA_Click
	If r_1.Checked Then
		motivo = "CERRADO"
	Else If r_2.Checked Then
		motivo = "NO COMPRA"
	Else If r_3.Checked Then
		motivo = "CON PRODUCTO"
	Else
		motivo = "NO ESTA EL ENCARGADO"
	End If
	DateTime.DateFormat = "MM/dd/yyyy"
	sDate=DateTime.Date(DateTime.Now)
	sTime=DateTime.Time(DateTime.Now)
	c=B4XPages.MainPage.skmt.ExecQuery("select CUENTA from cuentaa")
	c.Position = 0
	cuenta = c.GetString("CUENTA")
	c=B4XPages.MainPage.skmt.ExecQuery("select usuario from usuarioa")
	c.Position = 0
	usuario = c.GetString("USUARIO")
	c=B4XPages.MainPage.skmt.ExecQuery("select PE_PROID,PE_CANT FROM PEDIDO where pe_cliente in (Select CUENTA from cuentaa) ")
	If c.RowCount>0 Then
		For i=0 To c.RowCount -1
			c.Position=i
			B4XPages.MainPage.skmt.ExecNonQuery2($"update ${Subs.traeTablaProds(tipo_venta)} set cat_gp_almacen = cat_gp_almacen + ? where cat_gp_id = ?"$, Array As Object(c.GetString("PE_CANT"),c.GetString("PE_PROID")))
		Next
	End If
	
	If B4XPages.MainPage.cliente.IsInitialized Then
		B4XPages.MainPage.cliente.motivoNoVenta = motivo
'		Subs.bitacora(Subs.fechanormal(DateTime.Now), usuario, Subs.traeAlmacen, Subs.traeRuta, "No Venta", Subs.traeCliente, "", Subs.fechanormal(DateTime.Now), B4XPages.MainPage.lat_gps, B4XPages.MainPage.lon_gps, B4XPages.MainPage.cliente.precision, motivo, e_comm.text)
		Starter.enVenta = False
	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.ExecNonQuery2("INSERT INTO  NOVENTA (NV_CLIENTE,NV_FECHA,NV_USER,NV_MOTIVO,NV_COMM,NV_LAT,NV_LON) VALUES(?,?,?,?,?,?,?) ", Array As Object (cuenta,sDate & sTime, usuario, motivo,e_comm.text, B4XPages.MainPage.lat_gps, B4XPages.MainPage.lon_gps))
	B4XPages.MainPage.skmt.ExecNonQuery("UPDATE kmt_info3 set gestion = 3  where CAT_CL_CODIGO In (select cuenta from cuentaa)")
	DateTime.TimeFormat = "HHmmss"
	sTime=DateTime.Time(DateTime.Now)
	B4XPages.MainPage.skmt.ExecNonQuery2("UPDATE HIST_STAY_STORE set HSS_OUT = ?  where HSS_CODIGO In (select cuenta from cuentaa)", Array As Object(sTime))
	'  B4XPages.MainPage.skmt.ExecNonQuery("UPDATE HIST_STAY_STORE set HSS_TOT = HSS_OUT - HSS_IN where HSS_CODIGO In (select cuenta from cuentaa)")

	c=B4XPages.MainPage.skmt.ExecQuery(" Select (substr(HSS_OUT,1,2) - substr(HSS_IN,1,2)) - Case when  (substr(HSS_OUT,3,2) - substr(HSS_IN,3,2)) < 0 Then 1 Else 0 end HORAS_TOTALES, " & _
	"Case when  (substr(HSS_OUT,3,2) - substr(HSS_IN,3,2)) < 0 Then (substr(HSS_OUT,3,2) + 60 - substr(HSS_IN,3,2)) " & _
	"	Else (substr(HSS_OUT,3,2) - substr(HSS_IN,3,2)) End - Case when  (substr(HSS_OUT,5,2) - substr(HSS_IN,5,2)) < 0 Then 1 Else 0 end  MINUTOS_TOTALES, " & _
	"Case when  (substr(HSS_OUT,5,2) - substr(HSS_IN,5,2)) < 0 Then (substr(HSS_OUT,5,2) + 60 - substr(HSS_IN,5,2)) " & _
	"	Else (substr(HSS_OUT,5,2) - substr(HSS_IN,5,2)) end SEGUNDOS_TOTALES " & _
	"	from HIST_STAY_STORE where HSS_CODIGO In (Select cuenta from cuentaa) ")
	c.Position = 0
	stay_hh = c.GetString("HORAS_TOTALES") * 60 * 60
	stay_mi = c.GetString("MINUTOS_TOTALES") * 60
	stay_ss = c.GetString("SEGUNDOS_TOTALES")
	B4XPages.MainPage.skmt.ExecNonQuery2("UPDATE HIST_STAY_STORE set HSS_TOT = ?  where HSS_CODIGO In (select cuenta from cuentaa)", Array As Object(stay_hh + stay_mi + stay_ss))
	B4XPages.MainPage.skmt.ExecNonQuery2("UPDATE HIST_STAY_OUT set HSO_FIN = ?  ", Array As Object(sTime))
	DateTime.TimeFormat = "HH:mm:ss"
	bitacora.mandaBitacora(Subs.fechanormal(DateTime.Now), usuario, Subs.traeAlmacen, Subs.traeRutaBitacora, "NO VENTA", Subs.traeCliente,Subs.fechanormal(DateTime.Now), Subs.fechanormal(DateTime.Now), B4XPages.MainPage.lat_gps, B4XPages.MainPage.lon_gps, 2, motivo, r_1.Checked, B4XPages.MainPage.cliente.bitacora.banderaGeoCerca)
	B4XPages.ShowPage("Principal")
End Sub