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
	Private frontCamera As Boolean = False
	Dim g As GPS
	Private p_camara As Panel
	Private camEx As CameraExClass
	Dim btnTakePicture As Button
	Dim c As Cursor
	Dim cuenta As String
	Dim lat_gps As String
	Dim USUARIO As String
	Dim MOTIVO As String
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
	g.Initialize("GPS")
	Root.LoadLayout("foto")
	c=Starter.skmt.ExecQuery("select cuenta from cuentaa")
	c.Position = 0
	cuenta = c.GetString("CUENTA")
End Sub

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

Sub B4XPage_Appear
	InitializeCamera
End Sub

Private Sub InitializeCamera
	camEx.Initialize(p_camara, frontCamera, Me, "Camera1")
	frontCamera = camEx.Front
End Sub

Sub Activity_Pause (UserClosed As Boolean)
'	camEx.Release
End Sub

Sub GPS_LocationChanged (Location1 As Location)

End Sub

Sub Camera1_Ready (Success As Boolean)
	If Success Then
		camEx.SetJpegQuality(90)
		camEx.CommitParameters
		camEx.StartPreview
		Log(camEx.GetPreviewSize)
	Else
		ToastMessageShow("Cannot open camera.", True)
	End If
End Sub

Sub btnTakePicture_Click
	Dim ps As CameraSize
	ps.Width =640
	ps.Height =480
	camEx.SetPictureSize(ps.Width, ps.Height)
	'ToastMessageShow(ps.Width & "x" & ps.Height, False)
	camEx.CommitParameters
	camEx.TakePicture
End Sub

Sub btnFocus_Click
	camEx.FocusAndTakePicture
End Sub

Sub Camera1_PictureTaken (Data() As Byte)
	Dim filename As String = "1.jpg"
	Dim dir As String = File.DirInternal
	
	camEx.SavePictureToFile(Data, dir, filename)
	camEx.StartPreview 'restart preview
	'Dim out As OutputStream
	Dim sDate,sTime As String
	DateTime.DateFormat = "MM/dd/yyyy"
	sDate=DateTime.Date(DateTime.Now)
	sTime=DateTime.Time(DateTime.Now)
	c=Starter.skmt.ExecQuery("select CUENTA from cuentaa")
	c.Position = 0
	cuenta = c.GetString("CUENTA")
	c=Starter.skmt.ExecQuery("select USUARIO from usuarioa")
	c.Position = 0
	USUARIO = c.GetString("USUARIO")
	c.Close
	Starter.skmt.ExecNonQuery("DELETE FROM NOVENTA WHERE NV_CLIENTE IN (select cuenta from cuentaa)")
	c=Starter.skmt.ExecQuery("select HVD_CLIENTE,HVD_PRONOMBRE,HVD_CANT,HVD_COSTO_TOT from HIST_VENTAS WHERE HVD_CLIENTE IN (Select CUENTA from cuentaa) order by HVD_PRONOMBRE asc")
	If c.RowCount>0 Then
		For i=0 To c.RowCount -1
			c.Position=i
			Starter.skmt.ExecNonQuery2("insert into reparto(REP_CLIENTE, REP_PRONOMBRE, REP_CANT, REP_COSTO_TOT) select HVD_CLIENTE, HVD_PRONOMBRE, HVD_CANT, HVD_COSTO_TOT from hist_ventas where HVD_PRONOMBRE = ? and HVD_cliente in (Select CUENTA from cuentaa) ", Array As String(c.GetString("HVD_PRONOMBRE")))
			Starter.skmt.ExecNonQuery2("update cat_gunaprod set cat_gp_almacen = cat_gp_almacen + ? where cat_gp_nombre = ?", Array As Object(c.GetString("HVD_CANT"),c.GetString("HVD_PRONOMBRE")))
			' ANTES DE MODIFCAR
		Next
	End If
	Starter.skmt.ExecNonQuery2("INSERT INTO  NOVENTA (NV_CLIENTE,NV_FECHA,NV_USER,NV_MOTIVO,NV_COMM,NV_LAT,NV_LON, NV_FOTO) VALUES(?,?,?,?,?,?,?,?) ", Array As Object (cuenta,sDate & sTime, USUARIO, "CERRADO", B4XPages.MainPage.noVenta.COMENTARIO, B4XPages.MainPage.lat_gps, B4XPages.MainPage.lon_gps, Data))
	Starter.skmt.ExecNonQuery($"UPDATE kmt_info set gestion = 3, HORAENT = '${sDate & " " & sTime}' where CAT_CL_CODIGO In (select cuenta from cuentaa)"$)
	Starter.skmt.ExecNonQuery("update HIST_VENTAS SET HVD_RECHAZO = 1 WHERE HVD_CLIENTE IN (SELECT CUENTA FROM CUENTAA)")
	B4XPages.ShowPage("Cliente")
End Sub

Sub ChangeCamera_Click
	camEx.Release
	frontCamera = Not(frontCamera)
	InitializeCamera
End Sub

Sub btnEffect_Click
	Dim effects As List = camEx.GetSupportedColorEffects
	If effects.IsInitialized = False Then
		ToastMessageShow("Effects not supported.", False)
		Return
	End If
	Dim effect As String = effects.Get((effects.IndexOf(camEx.GetColorEffect) + 1) Mod effects.Size)
	camEx.SetColorEffect(effect)
	ToastMessageShow(effect, False)
	camEx.CommitParameters
End Sub

Sub btnFlash_Click
	Dim f() As Float = camEx.GetFocusDistances
	Log(f(0) & ", " & f(1) & ", " & f(2))
	Dim flashModes As List = camEx.GetSupportedFlashModes
	If flashModes.IsInitialized = False Then
		ToastMessageShow("Flash not supported.", False)
		Return
	End If
	Dim flash As String = flashModes.Get((flashModes.IndexOf(camEx.GetFlashMode) + 1) Mod flashModes.Size)
	camEx.SetFlashMode(flash)
	ToastMessageShow(flash, False)
	camEx.CommitParameters
End Sub

Sub btnPictureSize_Click
	Dim pictureSizes() As CameraSize = camEx.GetSupportedPicturesSizes
	Dim current As CameraSize = camEx.GetPictureSize
	For i = 0 To pictureSizes.Length - 1
		If pictureSizes(i).Width = current.Width And pictureSizes(i).Height = current.Height Then Exit
	Next
	Dim ps As CameraSize = pictureSizes((i + 1) Mod pictureSizes.Length)
	camEx.SetPictureSize(ps.Width, ps.Height)
	ToastMessageShow(ps.Width & "x" & ps.Height & i, False)
	camEx.CommitParameters
End Sub

Private Sub B4XPage_CloseRequest As ResumableSub
	' BACK key pressed
	' I want to capture the key here so I return True
	B4XPages.ShowPage("Cliente")
	' Returning False signals the system to handle the key
	Return False
End Sub

