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
	Private p_ticketsdia As Panel
	
	Dim c As Cursor
	Dim S As Cursor
	Dim ruta As String
	Dim ListView1 As ListView
	Dim b_noventa0 As Button
	Dim nombre_boton As String
	Dim STIME As String
	Private p_transparenciaIniciaInicioFinVenta As Panel
	Private p_inicioFinVenta As Panel
	Private b_finVenta As Button
	Private l_finVenta As Label
	Private p_guardarPeso As Panel
	Private l_guardarPeso As Label
	Private et_guardarPeso As EditText
	Private b_noVenta As Button
	Private b_guardarPeso As Button
	Private p_botonesGuardarPeso As Panel
	Private p_noVenta As Panel
	Private p_botonesNoVenta As Panel
	Private b_cancelaNoVenta As Button
	Private b_guardaNoVenta As Button
	Private Panel5 As Panel
	Private et_comentarioNoVenta As EditText
	Private Label3 As Label
	Private r_opcion4 As RadioButton
	Private r_opcion3 As RadioButton
	Private r_opcion2 As RadioButton
	Private r_opcion1 As RadioButton
	Private l_NoVenta As Label
	Private p_camara As Panel
	Private p_botonesCam As Panel
	Private b_cam As Button
	Private b_foto_inci As Button
	Private p_cam As Panel
	Private camEx As CameraExClass
	Private frontCamera As Boolean = False
	Dim laCam As C_Camara
	Dim fototomada As String = "0"
	Dim sDate As String
	Dim STIME As String
	Private nombreFoto As String
	Dim fotoCerrado As String = ""
	Private p_botonesCam As Panel
	Dim motivo As String
	Dim reqManager As DBRequestManager
	Private p_camara2 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
	ruta = File.DirInternal
	Root.LoadLayout("ticketsdia")
	If File.Exists(ruta, "kmt.db") = False Then
		File.Copy(File.DirAssets, "kmt.db", ruta, "kmt.db")
	End If
	c = B4XPages.MainPage.skmt.ExecQuery("select PC_CLIENTE, PC_MONTO, PC_NOART FROM PEDIDO_CLIENTE ORDER BY PC_FECHA asc")

	ListView1.Clear
	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 = 10
			label1.TextColor = Colors.Black
			Dim label2 As Label
			label2 = ListView1.TwoLinesLayout.SecondLabel
			label2.TextSize = 13
			label2.TextColor = Colors.Blue
			ListView1.AddTwoLines(c.GetString("PC_CLIENTE"),"Cantidad #"& c.GetString("PC_NOART")& " SubTotal $"& c.GetString("PC_MONTO"))
		Next
	End If
	p_transparenciaIniciaInicioFinVenta.top = 0 : p_transparenciaIniciaInicioFinVenta.Left = 0
	p_transparenciaIniciaInicioFinVenta.Height = Root.Height : p_transparenciaIniciaInicioFinVenta.Width = Root.Width
	Subs.centraPanel(p_inicioFinVenta, Root.Width)
	p_guardarPeso.Top = p_inicioFinVenta.top : p_guardarPeso.left = p_inicioFinVenta.left
	Subs.centraPanel(p_noVenta, Root.Width)
	reqManager.Initialize(Me, Starter.DBReqServer)
End Sub

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

Sub B4XPage_Appear
	nombre_boton = "NOVENTA"
'	c = B4XPages.MainPage.skmt.ExecQuery("select PC_CLIENTE, PC_MONTO, PC_NOART,(select CAT_CL_NOMBRE from kmt_info where cat_cl_codigo = pc_cliente UNION select CAT_CL_NOMBRE from kmt_info2 where cat_cl_codigo = pc_cliente ) as NOMBRE FROM PEDIDO_CLIENTE ORDER BY PC_FECHA asc")
	c = B4XPages.MainPage.skmt.ExecQuery("select PE_CLIENTE as PC_CLIENTE, PE_PESO AS PC_MONTO, 1 as PC_NOART,(select CAT_CL_NOMBRE from kmt_info where cat_cl_codigo = pe_cliente UNION select CAT_CL_NOMBRE from kmt_info2 where cat_cl_codigo = pe_cliente ) as NOMBRE FROM PESOS where PE_TIPO = 'VENTA' ORDER BY PE_FECHA asc")
	Dim noArticulos As Int = 0
	fotoCerrado = ""
	ListView1.Clear
	If c.RowCount>0 Then
		For i=0 To c.RowCount -1
			c.Position=i
'			Dim x As Cursor = B4XPages.MainPage.skmt.ExecQuery($"select sum(PE_CANT) as PC_NOART from PEDIDO where PE_CLIENTE = '${c.GetString("PC_CLIENTE")}' AND PE_PROID NOT IN (SELECT CAT_PA_ID FROM PROMOS_COMP)"$)
			Dim x As Cursor = B4XPages.MainPage.skmt.ExecQuery($"select 1 as PC_NOART from PESOS where PE_CLIENTE = '${c.GetString("PC_CLIENTE")}'"$)
			If x.RowCount > 0 Then
				x.Position = 0
'				Log(x.GetString("PC_NOART"))
				noArticulos = x.GetString("PC_NOART")
			End If
			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 = 13
			label2.TextColor = Colors.Blue
			ListView1.AddTwoLines(c.GetString("PC_CLIENTE"),c.GetString("NOMBRE") &"   Cantidad #"& noArticulos & " SubTotal $"& c.GetString("PC_MONTO"))
		Next
	End If
	c.Close
	p_camara.Top = 0 : p_camara.Left = 0
	p_camara.Width = Root.Width : p_camara.Height = Root.Height
	Subs.centraPanel(p_camara2, Root.Width)
End Sub

Sub Regresar_Click
	B4XPages.ShowPage("cliente")
End Sub

Sub Activity_KeyPress (key As Int) As Boolean
	' BACK key pressed
	If key=KeyCodes.KEYCODE_BACK Then
		' I want to capture the key here so I return True
		B4XPages.ShowPage("principal")
		'Return True
	End If
	' Returning False signals the system to handle the key
	Return False
End Sub

Sub ListView1_ItemLongClick (Position As Int, Value As Object)
	Private cliente As String = Value
	Starter.skmt.ExecNonQuery($"delete from cuentaa"$)
	Starter.skmt.ExecNonQuery($"insert into cuentaa (cuenta) values ('${cliente}')"$)
	Log($"insert into cuentaa (cuenta) values ('${cliente}')"$)
	c = Starter.skmt.ExecQuery("SELECT * FROM FOTOINICIAL")
	If c.RowCount = 0 Then
		p_camara.Visible = True
		InitializeCamera
		p_camara.Left = 0
		p_camara.Visible = True
		nombreFoto = "inicio"
		b_foto_inci.Text = "Foto tienda"
	Else
		p_transparenciaIniciaInicioFinVenta.Visible = True
		b_finVenta.Enabled = True
		Subs.bitacora(Subs.fechanormal(DateTime.Now), Subs.traeUsuarioDeBD, Subs.traeAlmacen, Subs.traeRuta, "Inicia Venta", cliente, Subs.fechanormal(DateTime.Now), "", Starter.lat_gps, Starter.lon_gps, "2", "", "", "Tickets Día")
		Starter.enVenta = True
		Subs.enVenta(1, "CLIENTES")
		LogColor($">>>>>> INICIA VENTA: ${Starter.enVenta} - ${cliente}"$, Colors.red)
		Log(Subs.traeRuta2(Subs.traeCliente))
	End If
	c = Starter.skmt.ExecQuery($"select PE_FOTO_CERRADO from PESOS where PE_CLIENTE = '${cliente}' and PE_FOTO_CERRADO = '1'"$)
	If c.RowCount > 0 Then
		fotoCerrado = "1"
	Else
		fotoCerrado = ""
	End If
	c.Close
	Log("CLIENTE: " & cliente)
	
	
	
'	B4XPages.MainPage.skmt.ExecNonQuery("delete from CUENTAA")
'	B4XPages.MainPage.skmt.ExecNonQuery2("INSERT INTO CUENTAA VALUES (?)", Array As Object(Value))
'	DateTime.TimeFormat = "HHmmss"
'	STIME=DateTime.Time(DateTime.Now)
'	Starter.pre_viejo = Subs.traeTotalClientepreventaparacredito
'	s=B4XPages.MainPage.skmt.ExecQuery("SELECT COUNT(*) AS CUANTOS FROM HIST_STAY_STORE WHERE HSS_IN = 0 AND HSS_CODIGO In (select cuenta from cuentaa)")
'	s.Position = 0
'	If s.GetString("CUANTOS") = 1 Then
'		B4XPages.MainPage.skmt.ExecNonQuery2("UPDATE HIST_STAY_STORE set HSS_IN = ?  where HSS_IN = 0 AND HSS_CODIGO In (select cuenta from cuentaa)", Array As Object(STIME))
'	Else
'		B4XPages.MainPage.skmt.ExecNonQuery2("INSERT INTO HIST_STAY_STORE(HSS_CODIGO,  HSS_IN , HSS_OUT , HSS_TOT) VALUES (?,0,0,0)", Array As Object (Value))
'		B4XPages.MainPage.skmt.ExecNonQuery2("UPDATE HIST_STAY_STORE set HSS_IN = ?  where HSS_IN = 0 AND HSS_CODIGO In (select cuenta from cuentaa)", Array As Object(STIME))
'	End If
'	DateTime.TimeFormat = "HH:mm:ss"
'	Subs.guardaClienteHoraInicio(Value)
'	B4XPages.MainPage.cliente.venimosDeTicketsDia = True
'	B4XPages.ShowPage("cliente")
End Sub

Sub b_noventa0_Click
	If nombre_boton = "NOVENTA" Then
		nombre_boton = "VENTA"
		b_noventa0.Text ="VENTA"
'		c = B4XPages.MainPage.skmt.ExecQuery("select NV_CLIENTE,NV_MOTIVO,NV_COMM, (select CAT_CL_NOMBRE from kmt_info where cat_cl_codigo = NV_CLIENTE ) as NOMBRE FROM NOVENTA ORDER BY NV_CLIENTE asc")
		c = B4XPages.MainPage.skmt.ExecQuery("select PE_CLIENTE as NV_CLIENTE, PE_MOTIVO as NV_MOTIVO, PE_COMENTARIO as NV_COMM, (select CAT_CL_NOMBRE from kmt_info where cat_cl_codigo = pe_cliente UNION select CAT_CL_NOMBRE from kmt_info2 where cat_cl_codigo = pe_cliente ) as NOMBRE FROM PESOS where PE_TIPO = 'NO VENTA' ORDER BY PE_FECHA asc")
		ListView1.Clear
		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 = 15
				label1.TextColor = Colors.Black
				Dim label2 As Label
				label2 = ListView1.TwoLinesLayout.SecondLabel
				label2.TextSize = 10
				label2.TextColor = Colors.Blue
				ListView1.AddTwoLines(c.GetString("NV_CLIENTE"),c.GetString("NOMBRE") &"  Motivo #"& c.GetString("NV_MOTIVO")& " Comentario: "& c.GetString("NV_COMM"))
			Next
		End If
	Else
		nombre_boton = "NOVENTA"
		b_noventa0.Text ="NO VENTA"
'		c = B4XPages.MainPage.skmt.ExecQuery("select PC_CLIENTE,PC_MONTO,PC_NOART,(select CAT_CL_NOMBRE from kmt_info where cat_cl_codigo = pc_cliente ) as NOMBRE FROM PEDIDO_CLIENTE ORDER BY PC_FECHA asc")
		c = B4XPages.MainPage.skmt.ExecQuery("select PE_CLIENTE as PC_CLIENTE, PE_PESO AS PC_MONTO, 1 as PC_NOART,(select CAT_CL_NOMBRE from kmt_info where cat_cl_codigo = pe_cliente UNION select CAT_CL_NOMBRE from kmt_info2 where cat_cl_codigo = pe_cliente ) as NOMBRE FROM PESOS where PE_TIPO = 'VENTA' ORDER BY PE_FECHA asc")
		ListView1.Clear
	  
		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 = 15
				label1.TextColor = Colors.Black
				Dim label2 As Label
				label2 = ListView1.TwoLinesLayout.SecondLabel
				label2.TextSize = 10
				label2.TextColor = Colors.Blue
				ListView1.AddTwoLines(c.GetString("PC_CLIENTE"),c.GetString("NOMBRE") &"   Cantidad #"& c.GetString("PC_NOART")& " SubTotal $"& c.GetString("PC_MONTO"))
			Next
		End If
		c.Close
	End If
End Sub

Private Sub p_ticketsdia_Click
	
End Sub

Private Sub b_guardaNoVenta_Click
	If r_opcion1.Checked Then
		motivo = "CERRADO"
	Else If r_opcion2.Checked Then
		motivo = "NO COMPRA"
	Else If r_opcion3.Checked Then
		motivo = "CON PRODUCTO"
	Else If r_opcion4.Checked Then
		motivo = "NO ESTA EL ENCARGADO"
	Else
		motivo = "CANCELACION"
	End If
	If motivo = "CERRADO" And fotoCerrado = "" Then
		nombreFoto = Subs.traeCliente & "_cerrado"
		p_camara.Visible = True
		InitializeCamera
		p_camara.Left = 0
		p_camara.Visible = True
		p_camara.BringToFront
		b_foto_inci.Text = "Foto tienda"
	Else
		DateTime.DateFormat="yy-MM-dd HH:mm:ss"
		Dim fecha As String=DateTime.Date(DateTime.Now)
		Subs.guardaPeso("NO VENTA", Subs.traeAlmacen, Subs.traeRuta2(Subs.traeCliente), Subs.traeUsuarioDeBD, Subs.traeCliente, "0", motivo, et_comentarioNoVenta.text, fotoCerrado)
		Subs.bitacora(Subs.fechanormal(DateTime.Now), Subs.traeUsuarioDeBD, Subs.traeAlmacen, Subs.traeRuta, "No Venta", Subs.traeCliente, "", Subs.fechanormal(DateTime.Now), Starter.lat_gps, Starter.lon_gps, 2, motivo, et_comentarioNoVenta.text, $"Tickets Día Venta:${et_guardarPeso.text}"$)
		Starter.enVenta = False
		Subs.enVenta(0, "")
		Starter.skmt.ExecNonQuery("UPDATE kmt_info set gestion = 3  where CAT_CL_CODIGO In (select cuenta from cuentaa)")
		p_noVenta.Visible = False
		p_guardarPeso.Visible = False
		p_transparenciaIniciaInicioFinVenta.Visible = False
	End If
End Sub

Private Sub b_cancelaNoVenta_Click
	p_noVenta.Visible = False
End Sub

Private Sub b_guardarPeso_Click
	If et_guardarPeso.Text <> "" And et_guardarPeso.Text <> "0" And IsNumber(et_guardarPeso.Text) Then
		Private peso As Double = et_guardarPeso.Text
		et_guardarPeso.Text = peso

		Subs.guardaPeso("VENTA", Subs.traeAlmacen, Subs.traeRuta2(Subs.traeCliente), Subs.traeUsuarioDeBD, Subs.traeCliente, peso, "", "", "")
		Subs.bitacora(Subs.fechanormal(DateTime.Now), Subs.traeUsuarioDeBD, Subs.traeAlmacen, Subs.traeRuta2(Subs.traeCliente), "Termina Venta", Subs.traeCliente, "", Subs.fechanormal(DateTime.Now), Starter.lat_gps, Starter.lon_gps, 2, "", "", $"Tickets Día Venta:${et_guardarPeso.text}"$)
		Starter.skmt.ExecNonQuery("UPDATE kmt_info set gestion = 2 where CAT_CL_CODIGO In (select cuenta from cuentaa)")
		Starter.enVenta = False
		Subs.enVenta(0, "")
		p_guardarPeso.Visible = False
		p_transparenciaIniciaInicioFinVenta.Visible = False
'		b_inicioVenta.Enabled = True
		b_finVenta.Enabled = False
		B4XPages.MainPage.ticketsDia.B4XPage_Appear
		mandaPendientes
	End If
End Sub

Private Sub b_noVenta_Click
	Log("No Venta")
	p_noVenta.Visible = True
	r_opcion1.Checked = True
	r_opcion2.Checked = False
	r_opcion3.Checked = False
	r_opcion4.Checked = False
	et_comentarioNoVenta.Text = ""
End Sub

Private Sub et_guardarPeso_TextChanged (Old As String, New As String)
	If Old <> New And IsNumber(New.trim) And New.trim <> "0" Then
		b_guardarPeso.Enabled = True
	Else
		b_guardarPeso.Enabled = False
	End If
End Sub

Sub mandaPendientes
'	PEDIDO_CLIENTE (Pendientes)
	Log(">>>>>>>  MANDA PENDIENTES")
	Dim PCNoArts As String
	Dim PCMonto As String
	If Subs.traeUsuarioDeBD <> "KMTS1"  Then
		'PESOS (Pendientes)
		c = Starter.skmt.ExecQuery($"SELECT * FROM PESOS where PE_ENVIO_OK <> 1"$)
		Log($"PESOS PENDIENTE: ${c.RowCount}"$)
		If c.RowCount>0 Then
			For i = 0 To c.RowCount -1
				c.Position = i
				Dim cmd As DBCommand
				cmd.Initialize
'				cmd.Name = "insert_pedido2_ADM"
'				cmd.Parameters = Array As Object(c.GetString("PE_CEDIS"),c.GetString("PE_CEDIS"),c.GetString("PE_IDALMACEN"),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"),Subs.traeRuta2(c.GetString("PE_CLIENTE")), c.GetString("PE_COSTO_SIN"),c.GetString("PE_DESC"),c.GetString("PE_FOLIO"),c.GetString("PE_CANTC"),c.GetString("PE_BCAJAS"),c.GetString("PE_TIPOPAGO"),c.GetString("PE_COMENTARIO"))
'				reqManager.ExecuteCommand(cmd , $"ins_pedidosPendientes_prods_${c.GetString("PE_CLIENTE")}_${c.GetString("PE_PROID")}"$)
'				Log($">>>>  INS_PEDIDO= ${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")}, ${c.GetString("PE_FOLIO")}, ${PCNoArts}, ${PCMonto}"$)
'				sql.insert_pedido_INTMEX=INSERT INTO INTMEX.PEDIDO (PE_CODPROMO,PE_FECHA_SINC,PE_IDALMACEN,PE_COSTO_TOT,PE_COSTOU,PE_CANT,PE_PRONOMBRE,PE_PROID,PE_CLIENTE,PE_FECHA,PE_USUARIO,PE_RUTA,PE_COSTO_SIN, PE_DESCUENTO, PE_TIPO) VALUES ((?),SYSDATE,(?),(?),(?),(?),(?),(?),(?),(?),(?),(?),(?),nvl((?),0),(?))
'				sql.insert_pedido2_INTMEX=INSERT INTO INTMEX.PEDIDO (PE_CODPROMO,PE_FECHA_SINC,PE_IDALMACEN,PE_COSTO_TOT,PE_COSTOU,PE_CANT,PE_PRONOMBRE,PE_PROID,PE_CLIENTE,PE_FECHA,PE_USUARIO,PE_RUTA,PE_COSTO_SIN, PE_DESCUENTO, PE_TIPO, PE_CANT_TOTAL, PE_VENTA_TOTAL) VALUES ((?),SYSDATE,(?),(?),(?),(?),(?),(?),(?),(?),(?),(?),(?),nvl((?),0),(?),(?),(?))

				Dim cmd As DBCommand
				cmd.Initialize
				cmd.Name = "insertPesos"
'				Log(c.GetString("PE_IDALMACEN")&", "&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")&", "&Subs.traeRuta2(c.GetString("PE_CLIENTE"))&", "& c.GetString("PE_COSTO_SIN")&", "&c.GetString("PE_DESC")&", "&c.GetString("PE_FOLIO")&", "& ALMACEN)
				cmd.Parameters = Array As Object(c.GetString("PE_IDALMACEN"),c.GetString("PE_RUTA"),c.GetString("PE_USUARIO"),c.GetString("PE_CLIENTE"),c.GetString("PE_PESO"),c.GetString("PE_FECHA"),c.GetString("PE_TIPO"),c.GetString("PE_COMENTARIO"),c.GetString("PE_FOTO_CERRADO"))
'				Log($">>>>  insertPesos= ${C.GetString("PE_IDALMACEN")},${c.GetString("PE_RUTA")},${C.GetString("PE_USUARIO")},${C.GetString("PE_CLIENTE")},${C.GetString("PE_PESO")}, ${C.GetString("PE_FECHA")}"$)
				reqManager.ExecuteCommand(cmd , $"insertPesos_${c.GetString("PE_CLIENTE")}"$)
			Next
		End If
		c.Close
	End If
End Sub

Private Sub b_finVenta_Click
	et_guardarPeso.Text = ""
	p_guardarPeso.Visible = True
'	b_inicioVenta.Enabled = True
'	b_finVenta.Enabled = False
End Sub

Private Sub b_foto_inci_Click
	camEx.TakePicture
End Sub

Private Sub InitializeCamera
	Starter.rp.CheckAndRequest(Starter.rp.PERMISSION_CAMERA)
	Wait For B4XPage_PermissionResult (Permission As String, resulta As Boolean)
	If resulta Then
		camEx.Initialize(p_cam, frontCamera, Me, "Camera1")
		frontCamera = camEx.Front
	Else
		ToastMessageShow("No permission!!!", True)
	End If
End Sub

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

Sub Camera1_PictureTaken (Data() As Byte)
	If b_foto_inci.Text = "Foto tienda" Then
		DateTime.DateFormat="ddMMyyyyHHmmss"
		fototomada = DateTime.Now & $"_${nombreFoto}.jpg"$
		Dim filename As String = fototomada
		Dim dir As String = File.DirInternal
		camEx.SavePictureToFile(Data, dir, filename)
		camEx.StartPreview 'restart preview
		ToastMessageShow("Picture saved." & CRLF  & "File size: " & File.Size(dir, filename), True)
		Dim Dirp As String = File.DirInternal
		Dim dir As String
		Dim Dir2 As String
		Try
			File.MakeDir(Dirp,"/adm")
			dir = "/adm"
			Log("creado en promotoria " & Dirp & dir)
		Catch
			dir = ""
			Log("creado en raiz")
		End Try
		Try
			File.MakeDir(Dirp & dir,"/reduccion")
			Dir2 = "/reduccion"
			Log("creado en promotoria " & Dirp & dir & Dir2)
		Catch
			Dir2 = ""
			Log("creado en raiz")
		End Try
		DateTime.DateFormat = "MM/dd/yyyy"
		DateTime.TimeFormat = "HH:mm:ss"
		sDate = DateTime.Date(DateTime.Now)
		STIME = DateTime.Time(DateTime.Now)
		camEx.SavePictureToFile(Data, Dirp&dir, filename)
		camEx.StartPreview 'restart preview
		ToastMessageShow("Picture saved." & CRLF  & "File size: " & File.Size(dir, filename) & dir &"," & filename, True)
		Log("Picture saved." & CRLF  & "File size: " & File.Size(dir, filename) & dir &"," & filename)
		
		Dim img As B4XBitmap = xui.LoadBitmapResize(File.DirInternal & dir, filename, 450, 600, True)
		Dim out As OutputStream = File.OpenOutput(File.DirInternal & dir & Dir2, filename, False)
		img.WriteToStream(out, 100, "PNG")
		out.Close
		If nombreFoto = "inicio" Then
			Starter.skmt.ExecNonQuery2("INSERT INTO FOTOINICIAL (RUTAA, ALMACEN, LATITUD, LONGITUD, FECHAHORA, FOTO, USUARIO) VALUES (?,?,?,?,?,?,?)",Array As Object(Subs.traeRuta, Subs.traeAlmacen, B4XPages.MainPage.lat_gps, B4XPages.MainPage.lon_gps, sDate & " " & STIME, File.ReadBytes(File.DirInternal & dir & Dir2, fototomada), Subs.traeUsuarioDeBD))
			b_foto_inci.Text = "Foto usuario"
			ChangeCamera_Click
		Else
			fotoCerrado = fototomada
			p_camara.Visible = False
			StopCamera
		End If
	Else If b_foto_inci.Text = "Foto usuario" Then
		DateTime.DateFormat="ddMMyyyyHHmmss"
		fototomada = fototomada = DateTime.Now & $"_${nombreFoto}.jpg"$
		Dim filename As String = fototomada
		Dim dir As String = File.DirInternal
		camEx.SavePictureToFile(Data, dir, filename)
		camEx.StartPreview 'restart preview
		ToastMessageShow("Picture saved." & CRLF  & "File size: " & File.Size(dir, filename), True)
		Dim Dirp As String = File.DirInternal
		Dim dir As String
		Dim Dir2 As String
		Try
			File.MakeDir(Dirp,"/adm")
			dir = "/adm"
			Log("creado en promotoria " & Dirp & dir)
		Catch
			dir = ""
			Log("creado en raiz")
		End Try
		Try
			File.MakeDir(Dirp & dir,"/reduccion")
			Dir2 = "/reduccion"
			Log("creado en promotoria " & Dirp & dir & Dir2)
		Catch
			dir = ""
			Log("creado en raiz")
		End Try
		DateTime.DateFormat = "MM/dd/yyyy"
		DateTime.TimeFormat = "HH:mm:ss"
		sDate = DateTime.Date(DateTime.Now)
		STIME = DateTime.Time(DateTime.Now)
		camEx.SavePictureToFile(Data, Dirp&dir, filename)
		camEx.StartPreview 'restart preview
		ToastMessageShow("Picture saved." & CRLF  & "File size: " & File.Size(dir, filename) & dir &"," & filename, True)
		Log("Picture saved." & CRLF  & "File size: " & File.Size(dir, filename) & dir &"," & filename)
		Dim img As B4XBitmap = xui.LoadBitmapResize(File.DirInternal & dir, filename, 450, 600, True)
		Dim out As OutputStream = File.OpenOutput(File.DirInternal & dir & Dir2, filename, False)
		img.WriteToStream(out, 100, "PNG")
		out.Close
		B4XPages.MainPage.skmt.ExecNonQuery2("UPDATE FOTOINICIAL set FOTO2 = ? where USUARIO = ? and RUTAA = ? and ALMACEN = ? ", Array As Object (File.ReadBytes(File.DirInternal & dir & Dir2, fototomada), Subs.traeUsuarioDeBD,Subs.traeRuta, Subs.traeAlmacen ))
		b_foto_inci.Text = "Foto tienda"
		ChangeCamera_Click
		p_camara.Visible = False
		StopCamera
		If Subs.traeCliente = "0" Then
			B4XPages.MainPage.tipo_venta = "ABORDO"
		Else If Subs.traeCliente = "1" Then
			B4XPages.MainPage.tipo_venta = "RECARGA"
		Else
			B4XPages.MainPage.tipo_venta = "PREVENTA"
		End If
'		B4XPages.ShowPage("productos")
'		p_transparentePidePeso.Visible = True
	End If
End Sub

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

Private Sub StopCamera
'	Capturing = False
	If camEx.IsInitialized Then
		camEx.Release
	End If
End Sub

Private Sub p_camara_Click
	
End Sub