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 skmt As SQL
	Dim g As GPS
	Dim clie_id As String
	Dim sDate,sTime As String
	Dim usuario As String
	
	Dim c As Cursor
	Dim ruta As String
	Dim Regresar As Button
	
	Dim ListView1 As ListView
	Dim L_CANT As Label
	Dim L_TOTAL As Label
	Dim borra As Button
	Dim Existe As String
	Dim result As String
	'    Dim lat_gps, lon_gps As String
	Dim DESC As String
	Dim c2, j3 As Cursor
	Private Titulo As Label
	Private b_desc As Button
	Dim DESCUENTO As String
	Dim folio As String
	Dim tgl As Toggle
	Private almacen As String
	Private p_nota As Panel
	Dim tipo_venta As String = Subs.traeTipoVentaDeBD
	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
	ruta = File.DirInternal
	Root.LoadLayout("nota")
	borra.Visible = True
	Titulo.Text = "Pedido Actual"
'	If Existe <> 0 Then
'		c=B4XPages.MainPage.skmt.ExecQuery("select pc_noart, pc_monto from pedido_cliente where pc_cliente in (Select CUENTA from cuentaa)")
'		C.Position=0
'		L_CANT.Text = c.GetString("PC_NOART")
'		L_TOTAL.Text = c.GetString("PC_MONTO")
'	End If
End Sub

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

Sub B4XPage_Appear
	If Not(Starter.gps.GPSEnabled) Then
		ToastMessageShow("Es necesario tener el GPS encendido", True)
		StartActivity(Starter.gps.LocationSettingsIntent)
	End If
	p_nota.Height = Root.Height * 0.9
	p_nota.Width = Root.Width * 0.9
	Panel1.Visible = True
	c=B4XPages.MainPage.skmt.ExecQuery("select  ID_ALMACEN from CAT_ALMACEN")
	C.Position =0
	almacen = C.GetString("ID_ALMACEN")
	C.Close
	L_CANT.Text =""
	L_TOTAL.Text=""
	c=B4XPages.MainPage.skmt.ExecQuery("select count(*) as EXISTE from pedido_cliente WHERE pc_cliente IN (Select CUENTA from cuentaa)")
	C.Position=0
	Existe = C.GetString("EXISTE")
	C.Close
	c=B4XPages.MainPage.skmt.ExecQuery("select PE_PRONOMBRE,PE_COSTO_TOT, PE_CANT, PE_FOLIO, PE_CEDIS FROM PEDIDO WHERE PE_CLIENTE IN (Select CUENTA from cuentaa) order by PE_CEDIS desc")
	ListView1.Clear
	ListView1.Height = Root.Height * 0.65
	Private cs As CSBuilder
	If c.RowCount>0 Then
		For i=0 To c.RowCount -1
			cs.Initialize
			c.Position=i
			Dim label1 As Label
			label1 = ListView1.TwoLinesLayout.Label
			label1.TextSize = 20
			label1.TextColor = Colors.Black
			label1.color = Colors.White
			Private textColor As Int = Colors.White
			If c.GetString("PE_CEDIS").Contains("PRO") Then textColor = Colors.Blue 'Si es promo, cambiamos el color del texto.
			If Not(IsNumber(c.GetString("PE_CEDIS"))) Then textColor = Colors.Blue 'Si es promo, cambiamos el color del texto.
			Dim label2 As Label
			label2 = ListView1.TwoLinesLayout.SecondLabel
			label2.TextSize = 16
			label2.TextColor = Colors.White
			
			ListView1.AddSingleLine(cs.Color(textColor).Size(20).append(c.GetString("PE_PRONOMBRE") & " ").pop.Append(CRLF).Size(16).Color(Colors.White).Append("Cantidad #"& c.GetString("PE_CANT")).Append(" SubTotal $"& c.GetString("PE_COSTO_TOT")).Append(" "  & c.GetString("PE_FOLIO")& " " &c.GetString("PE_CEDIS")).PopAll)
 			label1.Height = 90dip
'			label2.Height = 60dip
'			
'			ListView1.TwoLinesLayout.ItemHeight = 120dip
			ListView1.SingleLineLayout.ItemHeight = 90dip
			label1.Typeface = Typeface.CreateNew(Typeface.DEFAULT_BOLD, Typeface.STYLE_BOLD)
			label2.Typeface = Typeface.CreateNew(Typeface.DEFAULT_BOLD, Typeface.STYLE_BOLD)
			folio = c.GetString("PE_FOLIO")
		Next
	End If
	If Existe <> 0 Then
		c=B4XPages.MainPage.skmt.ExecQuery("select SUM(PE_CANT) AS PE_CANT from pedido where pe_cliente in (Select CUENTA from cuentaa) AND PE_PROID NOT IN (SELECT CAT_PA_ID FROM PROMOS_COMP ) ")
		C.Position=0
		L_CANT.Text = c.GetString("PE_CANT")
'		L_TOTAL.Text = NumberFormat2(c.GetString("PC_MONTO"), 1, 2, 2, True)
		c.Close
		c=B4XPages.MainPage.skmt.ExecQuery("select SUM(IFNULL(PE_DESC,0)) AS DESCUENTO FROM PEDIDO WHERE PE_CLIENTE IN (Select CUENTA from cuentaa)")
		c.Position=0
		DESC = c.GetString("DESCUENTO")
		b_desc.Visible = False
		
		'AQUI CAMBIAR
		Private s3 As Cursor=B4XPages.MainPage.skmt.ExecQuery2("select * FROM PEDIDO WHERE PE_CLIENTE IN (Select CUENTA from cuentaa) and PE_RECALCULO <> ? OR PE_RECALCULO <> ? OR PE_RECALCULO IS NOT NULL",Array As String("","null"))
		LogColor(s3.RowCount,Colors.Yellow)
		If s3.RowCount > 0 Then
				
			Private s As Cursor=B4XPages.MainPage.skmt.ExecQuery2("select iFNULL(sum(pe_costo_tot),0) as TOTAL_CLIE, SUM(PE_CANT) AS CANT_CLIE FROM PEDIDO WHERE PE_CLIENTE IN (Select CUENTA from cuentaa) and (PE_RECALCULO = ? or PE_RECALCULO = ? or PE_RECALCULO IS NULL)",Array As String("","null"))
			s.Position=0
			Private s5 As Cursor = B4XPages.MainPage.skmt.ExecQuery2("select iFNULL(sum(PE_RECALCULOTOT),0) as TOTAL_CLIE FROM PEDIDO WHERE PE_CLIENTE IN (Select CUENTA from cuentaa) and (PE_RECALCULO <> ? or PE_RECALCULO <> ? OR PE_RECALCULO IS NOT NULL)",Array As String("","null"))
			s5.Position = 0
			
				
			L_TOTAL.Text =NumberFormat2(s.GetString("TOTAL_CLIE") + s5.GetString("TOTAL_CLIE"),0,2,2,True)
				
			s.Close
		
		
		Else If s3.RowCount = 0 Then
			Private s As Cursor=B4XPages.MainPage.skmt.ExecQuery("select iFNULL(sum(pe_costo_tot),0) as TOTAL_CLIE, SUM(PE_CANT) AS CANT_CLIE FROM PEDIDO WHERE PE_CLIENTE IN (Select CUENTA from cuentaa)")
			s.Position=0
			
			L_TOTAL.Text =NumberFormat2(s.GetString("TOTAL_CLIE"),0,2,2,True)
				
			s.Close
		End If
		'AQUI TERMINA
		
	End If
End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

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

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

Private Sub B4XPage_CloseRequest As ResumableSub
'	BACK key pressed
'	Return True To close, False To cancel
	B4XPages.ShowPage("Cliente")
	Return False
End Sub

Sub borra_Click
'	c = B4XPages.MainPage.skmt.ExecQuery("SELECT HABILITADA FROM IMPRESORA")
'	If c.RowCount >  0 Then
'		c.Position = 0
'		If c.GetString("HABILITADA") = 1 Then
			If Not(Subs.revisaImpreso) Then
				If folio <> "" Then
					result =  Msgbox2("Seguro que desa borrar el pedido?","Cancelar pedido", "Si", "", "No",LoadBitmap(File.DirAssets,"alert2.png")) 'ignore
					If result = DialogResponse.POSITIVE Then
						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")))
								B4XPages.MainPage.skmt.ExecNonQuery2("INSERT INTO  INVENT_X_ENVIAR (ALMACEN , PROID , CANTIDAD ) VALUES(?,?,?) ", Array As Object (almacen,c.GetString("PE_PROID"),c.GetString("PE_CANT")* -1))
							Next
						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.ExecNonQuery("UPDATE kmt_info3 set gestion = 0  where CAT_CL_CODIGO In (select cuenta from cuentaa)")
						B4XPage_Appear
					End If
				Else
					Msgbox("CLIENTE YA SE TRANSMITIO, FAVOR DE LLAMAR A SOPORTE PARA ASISTENCIA","AVISO") 'ignore
				End If
			Else
				ToastMessageShow("La venta ya esta impresa, no se puede modificar!!", True)
				B4XPages.MainPage.cliente.p_transparenteTicketImpreso.Visible = True
				B4XPages.ShowPage("cliente")
			End If
'		Else If c.GetString("HABILITADA") = 0 Then
'			If folio <> "" Then
'				result =  Msgbox2("Seguro que desa borrar el pedido?","Cancelar pedido", "Si", "", "No",LoadBitmap(File.DirAssets,"alert2.png")) 'ignore
'				If result = DialogResponse.POSITIVE Then
'					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")))
'							B4XPages.MainPage.skmt.ExecNonQuery2("INSERT INTO  INVENT_X_ENVIAR (ALMACEN , PROID , CANTIDAD ) VALUES(?,?,?) ", Array As Object (almacen,c.GetString("PE_PROID"),c.GetString("PE_CANT")* -1))
'						Next
'					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.ExecNonQuery("UPDATE kmt_info3 set gestion = 0  where CAT_CL_CODIGO In (select cuenta from cuentaa)")
'					B4XPage_Appear
'				End If
'			Else
'				Msgbox("CLIENTE YA SE TRANSMITIO, FAVOR DE LLAMAR A SOPORTE PARA ASISTENCIA","AVISO") 'ignore
'			End If
'		End If
'	Else If c.RowCount =  0 Then
'		If folio <> "" Then
'			result =  Msgbox2("Seguro que desa borrar el pedido?","Cancelar pedido", "Si", "", "No",LoadBitmap(File.DirAssets,"alert2.png")) 'ignore
'			If result = DialogResponse.POSITIVE Then
'				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")))
'						B4XPages.MainPage.skmt.ExecNonQuery2("INSERT INTO INVENT_X_ENVIAR (ALMACEN , PROID , CANTIDAD ) VALUES(?,?,?) ", Array As Object (almacen,c.GetString("PE_PROID"),c.GetString("PE_CANT")* -1))
'					Next
'				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.ExecNonQuery("UPDATE kmt_info3 set gestion = 0  where CAT_CL_CODIGO In (select cuenta from cuentaa)")
'				B4XPage_Appear
'			End If
'		Else
'			Msgbox("CLIENTE YA SE TRANSMITIO, FAVOR DE LLAMAR A SOPORTE PARA ASISTENCIA","AVISO") 'ignore
'		End If
'	End If
End Sub

Sub ListView1_ItemLongClick (Position As Int, Value As Object)
	Log(Subs.traeTablaProds(tipo_venta))
	Log(Value)
	Private X() As String = Regex.Split(" ", Value)
	Log(X.Length)
	Private nom As String = ""
	For i = 0 To X.Length -1
		If X(i).Contains(CRLF) Then
		End If
		If Not(X(i).Contains(CRLF)) Then
			nom = nom & " " & X(i)
		Else
			Exit
		End If
	Next
	nom = nom.Trim
	Private cedis As String = X(X.Length-1)
	Log(nom)
	Dim cx As Cursor = B4XPages.MainPage.skmt.ExecQuery($"select PE_PROID,PE_CANT, PE_FOLIO, PE_CEDIS, PE_PRONOMBRE, PE_CEDIS/1 AS is_numeric FROM PEDIDO where (pe_pronombre = '${nom}' or pe_pronombre = '${nom} ') AND PE_CEDIS = '${cedis}' and is_numeric > 0 and pe_cliente in (Select CUENTA from cuentaa) and PE_RECALCULO > 0"$)
	Log("Recalculados: " & cx.RowCount)
	If cx.RowCount > 0 Then ' Si hay recalculo (producto con promocion), al borrar el produto hay que borrar TODO el pedido.
		result =  Msgbox2("Si se borra este producto, se va a borrar el pedido COMPLETO, ¿desea continuar?","Borrar pedido completo", "Si", "", "No",LoadBitmap(File.DirAssets,"alert2.png")) 'ignore
		If result = DialogResponse.NEGATIVE Then
			Return
		Else
			Starter.skmt.ExecNonQuery("delete from pedido where pe_cliente in (Select CUENTA from cuentaa)")
			Starter.skmt.ExecNonQuery("delete from pedido_cliente where pc_cliente in (Select CUENTA from cuentaa)")
			B4XPage_Appear
			Return
		End If
	End If
	c = B4XPages.MainPage.skmt.ExecQuery("SELECT HABILITADA FROM IMPRESORA")
	If c.RowCount >  0 Then
		c.Position = 0
		If c.GetString("HABILITADA") = 1 Then
			If Not(Subs.revisaImpreso) Then
				result =  Msgbox2("Seguro que desea borrar este articulo?","Borrar Articulo", "Si", "", "No",LoadBitmap(File.DirAssets,"alert2.png")) 'ignore
				If result = DialogResponse.POSITIVE Then
					c=B4XPages.MainPage.skmt.ExecQuery($"select PE_PROID,PE_CANT, PE_FOLIO, PE_CEDIS, PE_PRONOMBRE FROM PEDIDO where (pe_pronombre = '${nom}' or pe_pronombre = '${nom} ') AND PE_CEDIS = '${cedis}' and pe_cliente in (Select CUENTA from cuentaa)"$)
					Log($"select PE_PROID,PE_CANT, PE_FOLIO, PE_CEDIS FROM PEDIDO where pe_pronombre = '${nom}' AND PE_CEDIS = '${cedis}' and pe_cliente in (Select CUENTA from cuentaa)"$)
					Log(c.RowCount)
					c.Position=0
					'		If c.RowCount > 0 Then
					'		B4XPages.MainPage.skmt.ExecNonQuery2("update cat_gunaprod set cat_gp_almacen = cat_gp_almacen + ? where cat_gp_id = ?", Array As Object(c.GetString("PE_CANT"),c.GetString("PE_PROID")))
					B4XPages.MainPage.skmt.ExecNonQuery2("INSERT INTO INVENT_X_ENVIAR (ALMACEN , PROID , CANTIDAD) VALUES(?,?,?) ", Array As Object (almacen,c.GetString("PE_PROID"),c.GetString("PE_CANT")* -1))
					'		c2=B4XPages.MainPage.skmt.ExecQuery($"select count(*) AS CUANTOS from CAT_GUNAPROD where CAT_GP_ID in (select pe_cedis from pedido where pe_pronombre = '${nom}' and pe_cliente in (Select CUENTA from cuentaa)) and CAT_GP_CLASIF = 'PROMOS' AND CAT_GP_TIPO = 'PROMOS' AND CAT_GP_SUBTIPO = 'PROMOS'"$)'Con esto revisamos si el nombre es de una promo
					'		c2=B4XPages.MainPage.skmt.ExecQuery($"select ISNUMERIC(pe_cedis) AS noEsPromo from pedido where pe_pronombre = '${nom}' and pe_cliente in (Select CUENTA from cuentaa) and pe_cedis = '${cedis}'"$)'Con esto revisamos si el nombre es de una promo
					'		Log($"select count(*) AS CUANTOS from CAT_GUNAPROD where CAT_GP_ID in (select pe_cedis from pedido where pe_pronombre = '${nom}' and pe_cliente in (Select CUENTA from cuentaa)) and CAT_GP_CLASIF = 'PROMOS' AND CAT_GP_TIPO = 'PROMOS' AND CAT_GP_SUBTIPO = 'PROMOS'"$)
					'		Log($"select ISNUMERIC(pe_cedis) AS noEsPromo from pedido where pe_pronombre = '${nom}' and pe_cliente in (Select CUENTA from cuentaa) and pe_cedis = '${cedis}'"$)
					'		c2.Position=0
					'		Log("Algo "&c2.GetString("noEsPromo"))
					If Not(IsNumber(cedis)) Then
						'			j3 = B4XPages.MainPage.skmt.ExecQuery2("SELECT PE_CEDIS, PE_CANT,PE_PROID, PE_PRONOMBRE FROM PEDIDO WHERE PE_CEDIS IN (SELECT PE_CEDIS FROM PEDIDO WHERE PE_PRONOMBRE = ?)",Array As String(nom))
						'			Log(j3.RowCount)
						'			If j3.RowCount > 0 Then
						Log("Soy promo")
						'				For j = 0 To c.RowCount -1
						'					Log("aqui tronare?")
						''					c.Position = j
						B4XPages.MainPage.skmt.ExecNonQuery($"update ${Subs.traeTablaProds(tipo_venta)} set cat_gp_almacen = cat_gp_almacen + ${c.GetString("PE_CANT")} where cat_gp_id = '${c.GetString("PE_PROID")}'"$)
						B4XPages.MainPage.skmt.ExecNonQuery2("delete from pedido where pe_cedis= ? and pe_cliente in (Select CUENTA from cuentaa) AND PE_FOLIO = ?", Array As Object(cedis, c.GetString("PE_FOLIO")))
						'				Next
						'			End If
						'			j3.Close
					Else
						B4XPages.MainPage.skmt.ExecNonQuery($"update ${Subs.traeTablaProds(tipo_venta)} set cat_gp_almacen = cat_gp_almacen + ${c.GetString("PE_CANT")} where cat_gp_id = '${c.Getstring("PE_PROID")}'"$)
						B4XPages.MainPage.skmt.ExecNonQuery2("delete from pedido where pe_pronombre = ? and pe_cedis = ? and pe_cliente in (Select CUENTA from cuentaa) AND PE_FOLIO = ?", Array As Object(nom, cedis, c.GetString("PE_FOLIO")))
					End If
					c.Close
'					c2.Close
					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
					clie_id = 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 sum(pe_costo_tot) as TOTAL_CLIE, SUM(PE_CANT) AS CANT_CLIE, count(*) as CUANTOS FROM PEDIDO WHERE PE_CLIENTE IN (Select CUENTA from cuentaa)")
					c.Position=0
					If c.GetString("CUANTOS") > 0 Then
						B4XPages.MainPage.skmt.ExecNonQuery("delete from pedido_cliente where PC_CLIENTE In (select cuenta from cuentaa)")
						B4XPages.MainPage.skmt.ExecNonQuery2("insert into pedido_cliente(PC_CLIENTE, PC_FECHA, PC_USER, PC_NOART, PC_MONTO,PC_LON, PC_LAT) VALUES (?,?,?,?,?,?,?)", Array As Object(clie_id, sDate & sTime, usuario, c.GetString("CANT_CLIE"),c.GetString("TOTAL_CLIE"), B4XPages.MainPage.lon_gps, B4XPages.MainPage.lat_gps))
						B4XPages.MainPage.skmt.ExecNonQuery("UPDATE kmt_info3 set gestion = 2  where CAT_CL_CODIGO In (select cuenta from cuentaa)")
					Else
						B4XPages.MainPage.skmt.ExecNonQuery("delete from pedido_cliente where PC_CLIENTE In (select cuenta from cuentaa)")
						B4XPages.MainPage.skmt.ExecNonQuery("UPDATE kmt_info3 set gestion = 0  where CAT_CL_CODIGO In (select cuenta from cuentaa)")
					End If
					B4XPage_Appear
				End If
			Else
				B4XPages.MainPage.cliente.p_transparenteTicketImpreso.Visible = True
				B4XPages.ShowPage("cliente")
				ToastMessageShow("La venta ya esta impresa, no se puede modificar!!", True)
			End If
		Else If c.GetString("HABILITADA") = 0 Then
			If Not(Subs.revisaImpreso) Then
				result =  Msgbox2("Seguro que desea borrar este articulo?","Borrar Articulo", "Si", "", "No",LoadBitmap(File.DirAssets,"alert2.png")) 'ignore
				If result = DialogResponse.POSITIVE Then
					Private X() As String = Regex.Split(" ", Value)
					Log(X)
					Log(X.Length)
					Private nom As String = ""
					For i = 0 To X.Length -1
'						Log(X(i))
						If X(i).Contains(CRLF) Then
'							Log("Retorno")
						End If
						If Not(X(i).Contains(CRLF)) Then
							nom = nom & " " & X(i)
						Else
							Exit
						End If
'						Log(nom)
					Next
'					Log(nom)
					nom = nom.Trim
					If X(X.Length-1) <> ".01" Then
						Private cedis As String = X(X.Length-1)
					Else
						Private cedis As String = (X(X.Length-2) &" " & X(X.Length-1))
					End If
					Log(nom)
					Log(cedis)
					c=B4XPages.MainPage.skmt.ExecQuery($"select PE_PROID,PE_CANT, PE_FOLIO, PE_CEDIS, PE_PRONOMBRE FROM PEDIDO where (pe_pronombre = '${nom}' or pe_pronombre = '${nom} ') AND PE_CEDIS = '${cedis}' and pe_cliente in (Select CUENTA from cuentaa)"$) ' Se agregó el "pe_pronombre = '${nom} '" porque al nombre de los productos de promo se les agrega un "espacio al final" para que cuando exiiste en el pedido un mismo producto fuera de promocion, el "quitaduplicados" no los elimine.
					Log($"select PE_PROID,PE_CANT, PE_FOLIO, PE_CEDIS FROM PEDIDO where (pe_pronombre = '${nom}' or pe_pronombre = '${nom} ') AND PE_CEDIS = '${cedis}' and pe_cliente in (Select CUENTA from cuentaa)"$)
					Log(c.RowCount)
					c.Position=0
'					If c.RowCount > 0 Then
'						B4XPages.MainPage.skmt.ExecNonQuery2("update cat_gunaprod set cat_gp_almacen = cat_gp_almacen + ? where cat_gp_id = ?", Array As Object(c.GetString("PE_CANT"),c.GetString("PE_PROID")))
'						B4XPages.MainPage.skmt.ExecNonQuery2("INSERT INTO INVENT_X_ENVIAR (ALMACEN , PROID , CANTIDAD) VALUES(?,?,?) ", Array As Object (almacen,c.GetString("PE_PROID"),c.GetString("PE_CANT")* -1))
'						c2=B4XPages.MainPage.skmt.ExecQuery($"select count(*) AS CUANTOS from CAT_GUNAPROD where CAT_GP_ID in (select pe_cedis from pedido where pe_pronombre = '${nom}' and pe_cliente in (Select CUENTA from cuentaa)) and CAT_GP_CLASIF = 'PROMOS' AND CAT_GP_TIPO = 'PROMOS' AND CAT_GP_SUBTIPO = 'PROMOS'"$)'Con esto revisamos si el nombre es de una promo
'						c2=B4XPages.MainPage.skmt.ExecQuery($"select ISNUMERIC(pe_cedis) AS noEsPromo from pedido where pe_pronombre = '${nom}' and pe_cliente in (Select CUENTA from cuentaa) and pe_cedis = '${cedis}'"$)'Con esto revisamos si el nombre es de una promo
'						Log($"select count(*) AS CUANTOS from CAT_GUNAPROD where CAT_GP_ID in (select pe_cedis from pedido where pe_pronombre = '${nom}' and pe_cliente in (Select CUENTA from cuentaa)) and CAT_GP_CLASIF = 'PROMOS' AND CAT_GP_TIPO = 'PROMOS' AND CAT_GP_SUBTIPO = 'PROMOS'"$)
'						Log($"select ISNUMERIC(pe_cedis) AS noEsPromo from pedido where pe_pronombre = '${nom}' and pe_cliente in (Select CUENTA from cuentaa) and pe_cedis = '${cedis}'"$)
'						c2.Position=0
'						Log("Algo "&c2.GetString("noEsPromo"))
					If Not(IsNumber(cedis)) Then
'					j3 = B4XPages.MainPage.skmt.ExecQuery2("SELECT PE_CEDIS, PE_CANT,PE_PROID, PE_PRONOMBRE FROM PEDIDO WHERE PE_CEDIS IN (SELECT PE_CEDIS FROM PEDIDO WHERE PE_PRONOMBRE = ?)",Array As String(nom))
'					Log(j3.RowCount)
'					If j3.RowCount > 0 Then
						Log("Soy promo")
'						For j = 0 To c.RowCount -1
'						Log("aqui tronare?")
'						'					c.Position = j
						B4XPages.MainPage.skmt.ExecNonQuery($"update ${Subs.traeTablaProds(tipo_venta)} set cat_gp_almacen = cat_gp_almacen + ${c.GetString("PE_CANT")} where cat_gp_id = '${c.GetString("PE_PROID")}'"$)
						B4XPages.MainPage.skmt.ExecNonQuery2("delete from pedido where pe_cedis= ? and pe_cliente in (Select CUENTA from cuentaa) AND PE_FOLIO = ?", Array As Object(cedis, c.GetString("PE_FOLIO")))
						'				Next
						'			End If
						'			j3.Close
					Else
						B4XPages.MainPage.skmt.ExecNonQuery($"update ${Subs.traeTablaProds(tipo_venta)} set cat_gp_almacen = cat_gp_almacen + ${c.GetString("PE_CANT")} where cat_gp_id = '${c.Getstring("PE_PROID")}'"$)
						B4XPages.MainPage.skmt.ExecNonQuery2("delete from pedido where pe_pronombre = ? and pe_cedis = ? and pe_cliente in (Select CUENTA from cuentaa) AND PE_FOLIO = ?", Array As Object(nom, cedis, c.GetString("PE_FOLIO")))
					End If
					c.Close
'					c2.Close
					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
					clie_id = 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 sum(pe_costo_tot) as TOTAL_CLIE, SUM(PE_CANT) AS CANT_CLIE, count(*) as CUANTOS FROM PEDIDO WHERE PE_CLIENTE IN (Select CUENTA from cuentaa)")
					c.Position=0
					If c.GetString("CUANTOS") > 0 Then
						B4XPages.MainPage.skmt.ExecNonQuery("delete from pedido_cliente where PC_CLIENTE In (select cuenta from cuentaa)")
						B4XPages.MainPage.skmt.ExecNonQuery2("insert into pedido_cliente(PC_CLIENTE, PC_FECHA, PC_USER, PC_NOART, PC_MONTO,PC_LON, PC_LAT) VALUES (?,?,?,?,?,?,?)", Array As Object(clie_id, sDate & sTime, usuario, c.GetString("CANT_CLIE"),c.GetString("TOTAL_CLIE"), B4XPages.MainPage.lon_gps, B4XPages.MainPage.lat_gps))
						B4XPages.MainPage.skmt.ExecNonQuery("UPDATE kmt_info3 set gestion = 2  where CAT_CL_CODIGO In (select cuenta from cuentaa)")
					Else
						B4XPages.MainPage.skmt.ExecNonQuery("delete from pedido_cliente where PC_CLIENTE In (select cuenta from cuentaa)")
						B4XPages.MainPage.skmt.ExecNonQuery("UPDATE kmt_info3 set gestion = 0  where CAT_CL_CODIGO In (select cuenta from cuentaa)")
					End If
					B4XPage_Appear
				End If
			Else
				B4XPages.MainPage.cliente.p_transparenteTicketImpreso.Visible = True
				B4XPages.ShowPage("cliente")
				ToastMessageShow("La venta ya esta impresa, no se puede modificar!!", True)
			End If
		End If
	Else If c.RowCount =  0 Then
		If Not(Subs.revisaImpreso) Then
			result =  Msgbox2("Seguro que desea borrar este articulo?","Borrar Articulo", "Si", "", "No",LoadBitmap(File.DirAssets,"alert2.png")) 'ignore
			If result = DialogResponse.POSITIVE Then
				Private X() As String = Regex.Split(" ", Value)
				Log(X)
				Log(X.Length)
				Private nom As String = ""
				For i = 0 To X.Length -1
'					Log(X(i))
					If X(i).Contains(CRLF) Then
'						Log("Retorno")
					End If
					If Not(X(i).Contains(CRLF)) Then
						nom = nom & " " & X(i)
					Else
						Exit
					End If
'					Log(nom)
				Next
'				Log(nom)
				nom = nom.Trim
				Private cedis As String = X(X.Length-1)
				c=B4XPages.MainPage.skmt.ExecQuery($"select PE_PROID,PE_CANT, PE_FOLIO, PE_CEDIS, PE_PRONOMBRE FROM PEDIDO where (pe_pronombre = '${nom}' or pe_pronombre = '${nom} ') AND PE_CEDIS = '${cedis}' and pe_cliente in (Select CUENTA from cuentaa)"$)
				Log($"select PE_PROID,PE_CANT, PE_FOLIO, PE_CEDIS FROM PEDIDO where pe_pronombre = '${nom}' AND PE_CEDIS = '${cedis}' and pe_cliente in (Select CUENTA from cuentaa)"$)
				Log(c.RowCount)
				c.Position=0
				'		If c.RowCount > 0 Then
				'		B4XPages.MainPage.skmt.ExecNonQuery2("update cat_gunaprod set cat_gp_almacen = cat_gp_almacen + ? where cat_gp_id = ?", Array As Object(c.GetString("PE_CANT"),c.GetString("PE_PROID")))
				B4XPages.MainPage.skmt.ExecNonQuery2("INSERT INTO INVENT_X_ENVIAR (ALMACEN , PROID , CANTIDAD) VALUES(?,?,?) ", Array As Object (almacen,c.GetString("PE_PROID"),c.GetString("PE_CANT")* -1))
				'		c2=B4XPages.MainPage.skmt.ExecQuery($"select count(*) AS CUANTOS from CAT_GUNAPROD where CAT_GP_ID in (select pe_cedis from pedido where pe_pronombre = '${nom}' and pe_cliente in (Select CUENTA from cuentaa)) and CAT_GP_CLASIF = 'PROMOS' AND CAT_GP_TIPO = 'PROMOS' AND CAT_GP_SUBTIPO = 'PROMOS'"$)'Con esto revisamos si el nombre es de una promo
				'		c2=B4XPages.MainPage.skmt.ExecQuery($"select ISNUMERIC(pe_cedis) AS noEsPromo from pedido where pe_pronombre = '${nom}' and pe_cliente in (Select CUENTA from cuentaa) and pe_cedis = '${cedis}'"$)'Con esto revisamos si el nombre es de una promo
				'		Log($"select count(*) AS CUANTOS from CAT_GUNAPROD where CAT_GP_ID in (select pe_cedis from pedido where pe_pronombre = '${nom}' and pe_cliente in (Select CUENTA from cuentaa)) and CAT_GP_CLASIF = 'PROMOS' AND CAT_GP_TIPO = 'PROMOS' AND CAT_GP_SUBTIPO = 'PROMOS'"$)
				'		Log($"select ISNUMERIC(pe_cedis) AS noEsPromo from pedido where pe_pronombre = '${nom}' and pe_cliente in (Select CUENTA from cuentaa) and pe_cedis = '${cedis}'"$)
				'		c2.Position=0
				'		Log("Algo "&c2.GetString("noEsPromo"))
				If Not(IsNumber(cedis)) Then
					'			j3 = B4XPages.MainPage.skmt.ExecQuery2("SELECT PE_CEDIS, PE_CANT,PE_PROID, PE_PRONOMBRE FROM PEDIDO WHERE PE_CEDIS IN (SELECT PE_CEDIS FROM PEDIDO WHERE PE_PRONOMBRE = ?)",Array As String(nom))
					'			Log(j3.RowCount)
					'			If j3.RowCount > 0 Then
					Log("Soy promo")
					'				For j = 0 To c.RowCount -1
					'					Log("aqui tronare?")
					''					c.Position = j
					B4XPages.MainPage.skmt.ExecNonQuery($"update ${Subs.traeTablaProds(tipo_venta)} set cat_gp_almacen = cat_gp_almacen + ${c.GetString("PE_CANT")} where cat_gp_id = '${c.GetString("PE_PROID")}'"$)
					B4XPages.MainPage.skmt.ExecNonQuery2("delete from pedido where pe_cedis= ? and pe_cliente in (Select CUENTA from cuentaa) AND PE_FOLIO = ?", Array As Object(cedis, c.GetString("PE_FOLIO")))
					'				Next
					'			End If
					'			j3.Close
				Else
					B4XPages.MainPage.skmt.ExecNonQuery($"update ${Subs.traeTablaProds(tipo_venta)} set cat_gp_almacen = cat_gp_almacen + ${c.GetString("PE_CANT")} where cat_gp_id = '${c.Getstring("PE_PROID")}'"$)
					B4XPages.MainPage.skmt.ExecNonQuery2("delete from pedido where pe_pronombre = ? and pe_cedis = ? and pe_cliente in (Select CUENTA from cuentaa) AND PE_FOLIO = ?", Array As Object(nom, cedis, c.GetString("PE_FOLIO")))
				End If
				c.Close
'				c2.Close
				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
				clie_id = 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 sum(pe_costo_tot) as TOTAL_CLIE, SUM(PE_CANT) AS CANT_CLIE, count(*) as CUANTOS FROM PEDIDO WHERE PE_CLIENTE IN (Select CUENTA from cuentaa)")
				c.Position=0
				If c.GetString("CUANTOS") > 0 Then
					B4XPages.MainPage.skmt.ExecNonQuery("delete from pedido_cliente where PC_CLIENTE In (select cuenta from cuentaa)")
					B4XPages.MainPage.skmt.ExecNonQuery2("insert into pedido_cliente(PC_CLIENTE, PC_FECHA, PC_USER, PC_NOART, PC_MONTO,PC_LON, PC_LAT) VALUES (?,?,?,?,?,?,?)", Array As Object(clie_id, sDate & sTime, usuario, c.GetString("CANT_CLIE"),c.GetString("TOTAL_CLIE"), B4XPages.MainPage.lon_gps, B4XPages.MainPage.lat_gps))
					B4XPages.MainPage.skmt.ExecNonQuery("UPDATE kmt_info3 set gestion = 2  where CAT_CL_CODIGO In (select cuenta from cuentaa)")
				Else
					B4XPages.MainPage.skmt.ExecNonQuery("delete from pedido_cliente where PC_CLIENTE In (select cuenta from cuentaa)")
					B4XPages.MainPage.skmt.ExecNonQuery("UPDATE kmt_info3 set gestion = 0  where CAT_CL_CODIGO In (select cuenta from cuentaa)")
				End If
				B4XPage_Appear
			End If
		Else
			B4XPages.MainPage.cliente.p_transparenteTicketImpreso.Visible = True
			B4XPages.ShowPage("cliente")
			ToastMessageShow("La venta ya esta impresa, no se puede modificar!!", True)
		End If
	End If
End Sub

Sub b_desc_Click
	If DESCUENTO = "MENOS" Then
		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
		clie_id = c.GetString("CUENTA")
		c.Close
		c=B4XPages.MainPage.skmt.ExecQuery("select USUARIO from usuarioa")
		c.Position=0
		usuario = c.GetString("USUARIO")
		c.Close
		result =  Msgbox2("Seguro que desa dar descuento del 5%?","Dar descuento", "Si", "", "No",LoadBitmap(File.DirAssets,"alert2.png")) 'ignore
		If result = DialogResponse.POSITIVE Then
			B4XPages.MainPage.skmt.ExecNonQuery("UPDATE pedido SET PE_COSTO_TOT = PE_COSTO_TOT * .95, PE_DESC = 5 WHERE PE_CLIENTE In (select cuenta from cuentaa)")
			c=B4XPages.MainPage.skmt.ExecQuery("select sum(pe_costo_tot) as TOTAL_CLIE, SUM(PE_CANT) AS CANT_CLIE, count(*) as CUANTOS FROM PEDIDO WHERE PE_CLIENTE IN (Select CUENTA from cuentaa)")
			c.Position=0
			B4XPages.MainPage.skmt.ExecNonQuery("delete from pedido_cliente where PC_CLIENTE In (select cuenta from cuentaa)")
			B4XPages.MainPage.skmt.ExecNonQuery2("insert into pedido_cliente(PC_CLIENTE, PC_FECHA, PC_USER, PC_NOART, PC_MONTO,PC_LON, PC_LAT) VALUES (?,?,?,?,?,?,?)", Array As Object(clie_id, sDate & sTime, usuario, c.GetString("CANT_CLIE"),c.GetString("TOTAL_CLIE"), B4XPages.MainPage.lon_gps, B4XPages.MainPage.lat_gps))
			c.Close
			B4XPage_Appear
		End If
	ELSE IF DESCUENTO = "MAS" Then
		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
		clie_id = c.GetString("CUENTA")
		c.Close
		c=B4XPages.MainPage.skmt.ExecQuery("select USUARIO from usuarioa")
		c.Position=0
		usuario = c.GetString("USUARIO")
		c.Close
		result =  Msgbox2("Seguro que desa CANCELAR el descuento?","Cancelar descuento", "Si", "", "No",LoadBitmap(File.DirAssets,"alert2.png")) 'ignore
		If result = DialogResponse.POSITIVE Then
			B4XPages.MainPage.skmt.ExecNonQuery("UPDATE pedido SET PE_COSTO_TOT = PE_COSTO_TOT / .95, PE_DESC = 0 WHERE PE_CLIENTE In (select cuenta from cuentaa)")
			c=B4XPages.MainPage.skmt.ExecQuery("select sum(pe_costo_tot) as TOTAL_CLIE, SUM(PE_CANT) AS CANT_CLIE, count(*) as CUANTOS FROM PEDIDO WHERE PE_CLIENTE IN (Select CUENTA from cuentaa)")
			c.Position=0
			B4XPages.MainPage.skmt.ExecNonQuery("delete from pedido_cliente where PC_CLIENTE In (select cuenta from cuentaa)")
			B4XPages.MainPage.skmt.ExecNonQuery2("insert into pedido_cliente(PC_CLIENTE, PC_FECHA, PC_USER, PC_NOART, PC_MONTO,PC_LON, PC_LAT) VALUES (?,?,?,?,?,?,?)", Array As Object(clie_id, sDate & sTime, usuario, c.GetString("CANT_CLIE"),c.GetString("TOTAL_CLIE"), B4XPages.MainPage.lon_gps, B4XPages.MainPage.lat_gps))
			c.Close
			B4XPage_Appear
		End If
	End If
End Sub

Private Sub p_nota_Click
	
End Sub