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 kh As kms_helperSubs
	'//Process Globals
	Dim clie_id As String
	Dim sDate,sTime As String
	Dim usuario As String
	'//Globals
	Dim c, C_DOE As Cursor
	Dim C1 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
	Private Titulo As Label
	Private b_desc As Button
	Dim DESCUENTO As String
	Dim folio As String
	Dim TIPO As String
	Private CAMBIOS As String
	Dim logger As Boolean = True
	Private B_PEDIDO_DOE As Button
	Dim list_prodsPedido As List
	Private clv_pedidos As CustomListView
	Private p_pedido As Panel
	Private l_pedido As Label
	Private lv_prodsDOE As ListView
	Private L_CANT_D As Label
	Private L_TOTAL_D As Label
	Private l_tipoPedido As Label
	Dim pedidoMostrado As String = "NORMAL"
End Sub

'You can add more parameters here.
Public Sub Initialize As Object
	kh.Initialize(Me, "kh", Starter.skmt)
	Return Me
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
	If Not(Starter.Logger) Then logger = False
	Root = Root1
	'load the layout to Root
	ruta = File.DirInternal
	Root.LoadLayout("nota")
	borra.Visible = True
	Titulo.Text = "Pedido Actual"
	Starter.skmt.ExecNonQuery("update pedido set pe_folio = 0 where pe_folio is null")
	c = Starter.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=Starter.skmt.ExecQuery("select PE_PRONOMBRE,PE_COSTO_TOT, PE_CANT, PE_FOLIO FROM PEDIDO WHERE PE_CLIENTE IN (Select CUENTA from cuentaa) order by PE_PRONOMBRE asc")
'	ListView1.Clear
'	ListView1.TwoLinesLayout.ItemHeight = 90dip
'	If c.RowCount>0 Then
'		For i = 0 To c.RowCount -1
'			c.Position=i
'			Dim label1 As Label
'			label1 = ListView1.TwoLinesLayout.Label
''			label1.Color = Colors.Cyan
'			label1.TextSize = 15
'			label1.TextColor = Colors.Black
''			label1.Height = 50dip
'			Dim label2 As Label
'			label2 = ListView1.TwoLinesLayout.SecondLabel
'			label2.TextSize = 12
'			label2.TextColor = Colors.Black
''			label2.Color = Colors.green
'			ListView1.AddTwoLines(c.GetString("PE_PRONOMBRE"),"Cantidad #"& c.GetString("PE_CANT")& " SubTotal $"& c.GetString("PE_COSTO_TOT")& " Folio "& c.GetString("PE_FOLIO"))
'			folio = c.GetString("PE_FOLIO")
'		Next
'	End If
	list_prodsPedido.Initialize
'	Log("list_prodsPedido.Initialize")
	If Existe <> 0 Then
		c = Starter.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 kh.clienteConDOE And l_tipoPedido.IsInitialized Then
		l_tipoPedido.Visible = True
		Else
			If l_tipoPedido.IsInitialized Then l_tipoPedido.Visible = False
	End If
	pedidoMostrado = "NORMAL"
	Dim sumatotal As Double = 0
	c=Starter.skmt.ExecQuery2("select count(*) as CUANTOS FROM PEDIDO WHERE PE_CLIENTE IN (Select CUENTA from cuentaa) AND PE_CEDIS = ?", Array As String("DOE"))
	c.Position = 0
	ListView1.Visible = True
	L_CANT.Visible = True
	L_TOTAL.Visible = True
	lv_prodsDOE.Visible = False
	L_CANT_D.Visible = False
	L_TOTAL_D.Visible = False
	l_tipoPedido.Text = "PEDIDO NORMAL"
	If c.GetString("CUANTOS") > 0 Then
		B_PEDIDO_DOE.Text = "IR A PEDIDO DON TOÑO"
		B_PEDIDO_DOE.VISIBLE = True
		C_DOE=Starter.skmt.ExecQuery2("select PE_PRONOMBRE, PE_COSTO_TOT, PE_CANT, PE_FOLIO FROM PEDIDO WHERE PE_CEDIS = ? AND PE_CLIENTE IN (Select CUENTA from cuentaa) order by PE_PRONOMBRE asc", Array As String("DOE"))
		lv_prodsDOE.Clear
		If C_DOE.RowCount>0 Then
			For i=0 To C_DOE.RowCount -1
				C_DOE.Position = i
				Dim cs As CSBuilder
				cs.Initialize
				Private txtPromo As String = ""
				If C_DOE.GetString("PE_COSTO_TOT") < 0.02 And C_DOE.GetString("PE_COSTO_TOT") >= 0 Then txtPromo = "P - "
				If C_DOE.GetString("PE_COSTO_TOT") < 0 Then txtPromo = "RMI - "
				Dim label1 As Label
				label1 = lv_prodsDOE.TwoLinesLayout.Label
				label1.TextSize = 15
				label1.TextColor = Colors.Black
				Dim label2 As Label
				label2 = lv_prodsDOE.TwoLinesLayout.SecondLabel
				label2.TextSize = 12
				label2.TextColor = Colors.Black
				lv_prodsDOE.AddTwoLines(cs.Color(Colors.RGB(10,153,10)).append(C_DOE.GetString("PE_PRONOMBRE")).PopAll,txtPromo & "Cantidad #"& C_DOE.GetString("PE_CANT")& " SubTotal $"& C_DOE.GetString("PE_COSTO_TOT")& " Folio "& C_DOE.GetString("PE_FOLIO"))
				folio = C_DOE.GetString("PE_FOLIO")
			Next
		End If
		C_DOE.Close
		C_DOE=Starter.skmt.ExecQuery2("select sum(pe_cant) AS CANTIDAD, sum(pe_costo_tot) AS MONTO from  pedido where PE_CEDIS <> PE_PROID and pe_cliente in (Select CUENTA from cuentaa)  AND PE_CEDIS = ?", Array As String("DOE"))
		C_DOE.Position=0
		L_CANT_D.Text = C_DOE.GetString("CANTIDAD")
		L_TOTAL_D.Text = numberformat2(kh.traeTotalCliente, 1, 2, 2, True)
		c.Close
	Else
		B_PEDIDO_DOE.VISIBLE = False
	End If
	c=Starter.skmt.ExecQuery("select count(*) as CUANTOS from kmt_info where (impresion = 1 or cat_cl_codigo in (select ci_cuenta from cliente_impreso)) and CAT_CL_CODIGO in (Select CUENTA from cuentaa)")
	c.Position = 0
'	If logger Then Log($"Impreso: ${c.GetString("CUANTOS")}"$)
	If c.GetString("CUANTOS") > 0 Then
		borra.Visible = False
	Else
		borra.Visible = True
	End If
'	If Subs.clienteActual = "0" Then borra.Visible = True 'Si el cliente actual es "abordo" entonces SIEMPRE lo dejamos modificar la venta.
	c.Close
	L_CANT.Text =""
	L_TOTAL.Text=""
	Starter.skmt.ExecNonQuery("update pedido set pe_folio = 0 where pe_folio is null")
	c = Starter.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=Starter.skmt.ExecQuery("select PE_PRONOMBRE, PE_COSTO_TOT, PE_CANT, PE_FOLIO, PE_CEDIS, PE_PROID FROM PEDIDO WHERE PE_CLIENTE IN (Select CUENTA from cuentaa) and PE_CEDIS <> 'DOE' order by PE_PRONOMBRE asc")
	Private cs As CSBuilder
	ListView1.Clear
	clv_pedidos.Clear
	ListView1.TwoLinesLayout.ItemHeight = 90dip
	list_prodsPedido.Clear
	If c.RowCount  >0 Then
		For i = 0 To c.RowCount - 1
			cs.Initialize
			c.Position=i
			Private txtPromo As String = ""
			If Not(IsNumber(c.GetString("PE_CEDIS"))) Then txtPromo = "P - "
			If c.GetString("PE_CEDIS") = "DUR" Then txtPromo = "C - "
			Dim label1 As Label
			label1 = ListView1.TwoLinesLayout.Label
			label1.TextSize = 15
			label1.Height = 30dip
			label1.TextColor = Colors.Black
'			label1.Height = 50dip
			Dim label2 As Label
			label2 = ListView1.TwoLinesLayout.SecondLabel
			label2.TextSize = 12
			label2.TextColor = Colors.Black
			Private textColor As Int = Colors.black
			If Not(IsNumber(c.GetString("PE_CEDIS"))) Then textColor = Colors.RGB(210,105,30) 'Si es promo, cambiamos el color del texto.
			If c.GetString("PE_CEDIS") = "DUR" Then textColor = Colors.Red 'Si es promo, cambiamos el color del texto.
'			ListView1.AddTwoLines(c.GetString("PE_PRONOMBRE"), cs.Color(textColor).append(txtPromo).Append("Cantidad #" & c.GetString("PE_CANT") & " SubTotal $" & c.GetString("PE_COSTO_TOT") & " Folio " & c.GetString("PE_FOLIO")).PopAll)
			clv_pedidos.Add(CreateListItemPedidos(c.GetString("PE_PRONOMBRE"), textColor, txtPromo, c.GetString("PE_CANT"), c.GetString("PE_COSTO_TOT"), c.GetString("PE_FOLIO")), c.GetString("PE_PRONOMBRE"))
			list_prodsPedido.Add(CreateMap("prodNom":c.GetString("PE_PRONOMBRE"), "cedis":c.GetString("PE_CEDIS"), "prodId": c.GetString("PE_PROID"), "cant": c.GetString("PE_CANT")))
			folio = c.GetString("PE_FOLIO")
		Next
	End If
	If Existe <> 0 Then
		c = Starter.skmt.ExecQuery("select sum(pe_cant) AS CANTIDAD, sum(pe_costo_tot) AS MONTO from pedido where PE_CEDIS <> PE_PROID and pe_cliente in (Select CUENTA from cuentaa)")
		C.Position = 0
		L_CANT.Text = c.GetString("CANTIDAD")
		L_TOTAL.Text = kh.traeTotalCliente
		c.Close
		c = Starter.skmt.ExecQuery("select SUM(IFNULL(PE_DESC,0)) AS DESCUENTO FROM PEDIDO WHERE PE_CLIENTE IN (Select CUENTA from cuentaa)")
		c.Position = 0
		b_desc.Visible = False
	End If
	c=Starter.skmt.ExecQuery("select count(*) As CUANTOS FROM PEDIDO WHERE PE_CLIENTE IN (Select CUENTA from cuentaa)")
	c.Position = 0
	If c.GetString("CUANTOS") = 0 Then
		folio = 0
	Else
		C1=Starter.skmt.ExecQuery("SELECT SUM(ifnull(PE_FOLIO,0)) AS FOLIO FROM PEDIDO WHERE PE_CLIENTE IN (Select CUENTA from cuentaa)")
		C1.Position = 0
		folio = C1.GetString("FOLIO")
		If C1.GetString("FOLIO") > 0 Then
			folio = 100
		End If
	End If
	c.Close
End Sub

Sub CreateListItemPedidos(Texto1 As String, txtColor As Int, txtPromo As String, cantp As String, costoTotP As String, folioP As String) As Panel 'ignore
	Dim p As B4XView = xui.CreatePanel("")
	Dim su As StringUtils
	Dim cs As CSBuilder
	cs.Initialize
	p.LoadLayout("pedidoItem")
	l_pedido.Text = cs.append(Texto1).Append(CRLF).Color(txtColor).append(txtPromo).Append("Cantidad #" & cantp & " SubTotal $" & NumberFormat2(costoTotP, 1, 2, 2, True) & " Folio " & folioP).PopAll
	Dim alto As Int = su.MeasureMultilineTextHeight(l_pedido, l_pedido.Text)
	p.SetLayoutAnimated(0, 0, 0, clv_pedidos.AsView.Width, alto + 20)
'	i_prod.Bitmap = img
	Return p
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
	Subs.iniciaActividad("Cliente")
End Sub

Private Sub B4XPage_CloseRequest As ResumableSub
	' BACK key pressed
	Subs.iniciaActividad("Cliente")
'	B4XPages.ShowPage("Principal")
	Return True
End Sub

Sub borra_Click
	Log(pedidoMostrado)
	Log("CON DOE: " & kh.clienteConDOE)
	Private textoExtra As String = ""
	If pedidoMostrado = "DOE" Then textoExtra = " DOE"
	If kh.clienteConDOE And pedidoMostrado = "NORMAL" Then textoExtra = " (Sin DOE)"
	If folio = "0" Or 1 = 1 Then 'Si el folio es 0 o el cliente actual es "abordo", los deja borrar. - Mod por CHV 220512  'Para que el abordo borre siempre agregar -> " Or Subs.clienteActual = "0""
		result =  Msgbox2($"¿Seguro que desea borrar el pedido${textoExtra}?"$,"Cancelar pedido", "Si", "", "No", LoadBitmap(File.DirAssets,"alert2.png")) 'ignore
		If result = DialogResponse.POSITIVE Then
'			c = Starter.skmt.ExecQuery("select PE_PROID, PE_CANT, PE_TIPO, PE_CEDIS 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
'					TIPO = c.GetString("PE_TIPO")
'					If c.GetString("PE_CEDIS") <> "DUR" Then Starter.skmt.ExecNonQuery2($"update ${Subs.traeTablaProds(TIPO)} 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
'			Starter.skmt.ExecNonQuery("delete from pedido_cliente where pc_cliente in (Select CUENTA from cuentaa)")
'			Starter.skmt.ExecNonQuery("delete from pedido where pe_cliente in (Select CUENTA from cuentaa)")
'			Starter.skmt.ExecNonQuery("UPDATE kmt_info set gestion = 0  where CAT_CL_CODIGO In (select cuenta from cuentaa)")
			If pedidoMostrado = "DOE" Then Subs.borraPedidoDOEClienteActual Else Subs.borraPedidoClienteActual
'			B4XPages.MainPage.productos.prodsMap.Initialize 'inicializamos mapa de productos para que no aparezcan como vendidos en la lista
			B4XPages.MainPage.promos.promosMap.Initialize
'			Log("Llamamos LlenaProdsLL")
			If B4XPages.MainPage.productos.PCLV.IsInitialized Then B4XPages.MainPage.productos.LlenaProdsLL(Null, Null)
			B4XPage_Appear
		End If
	Else
		Msgbox("CLIENTE YA SE TRANSMITIO, FAVOR DE LLAMAR A SOPORTE PARA ASISTENCIA","AVISO") 'ignore
	End If
End Sub

Sub ListView1_ItemLongClick (Position As Int, Value As Object)
'	Private thisLog As Boolean = False
'	c=Starter.skmt.ExecQuery("select count(*) as CUANTOS from CLIENTE_IMPRESO where CI_CUENTA in  (Select CUENTA from cuentaa)")
'	c.Position = 0
'	If thisLog Then Log("lista prods="&list_prodsPedido.Get(Position))
'	Private m As Map = list_prodsPedido.Get(Position)
'	If c.GetString("CUANTOS") = 0 Or Subs.clienteActual = "0" Then 'Si la venta no está impresa o es abordo entonces continuamos ...
'		c=Starter.skmt.ExecQuery2("select count(*) as CUANTOS FROM PEDIDO WHERE PE_CEDIS = ?	 AND PE_CLIENTE IN (Select CUENTA from cuentaa)", Array As String("DUR"))
'		c.Position = 0
'		CAMBIOS = c.GetString("CUANTOS")
'		c.Close
'		c=Starter.skmt.ExecQuery2("select PE_PRONOMBRE,PE_COSTO_TOT, PE_CANT, PE_FOLIO FROM PEDIDO WHERE PE_CLIENTE IN (Select CUENTA from cuentaa) AND PE_PRONOMBRE = ? order by PE_PRONOMBRE asc", Array As String(Value) )
'		c.Position = 0
'		If c.GetString("PE_FOLIO")  = "0" Or Subs.clienteActual = "0" 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 prod As Cursor=Starter.skmt.ExecQuery2("select PE_PROID, PE_CANT, PE_CEDIS FROM PEDIDO where pe_pronombre = ? and pe_cliente in (Select CUENTA from cuentaa) ", Array As String(Value))
'				prod.Position=0
'				If thisLog Then Log("m2= " & m)
'				c.Close
'				If thisLog Then Log($"Buscamos: ${Value}, ${m.Get("cedis")}"$)
'				c=Starter.skmt.ExecQuery2("select count(*) AS CUANTOS from cat_gunaprod2 where CAT_GP_ID in (select pe_cedis from pedido where pe_pronombre = ? and pe_cedis = ? and pe_cliente in (Select CUENTA from cuentaa)) and CAT_GP_CLASIF = 'PROMOS' AND CAT_GP_TIPO = 'PROMOS' AND CAT_GP_SUBTIPO = 'PROMOS'", Array As String(Value, m.Get("cedis")))
'				c.Position=0
'				If c.GetString("CUANTOS") > 0 Then
'					If thisLog Then Log($"Borramos de PROMO ${Value}"$)
'					Private pPromos As Cursor = Starter.skmt.ExecQuery($"select pe_proid, pe_cant from pedido where pe_cedis = '${m.Get("cedis")}'"$)
'					For tp = 0 To pPromos.RowCount - 1
'						pPromos.Position = tp
'						If thisLog Then Log($"SUMAMOS ${pPromos.GetString("PE_CANT")} a ${pPromos.GetString("PE_PROID")} "$)
'						Starter.skmt.ExecNonQuery($"update ${Subs.traeTablaProds(Starter.tipov)} set cat_gp_almacen = cat_gp_almacen + ${pPromos.GetString("PE_CANT")} where cat_gp_id = '${pPromos.Getstring("PE_PROID")}'"$)
'					Next
'					Starter.skmt.ExecNonQuery2("delete from pedido where pe_cedis in (select pe_cedis from pedido where PE_CEDIS = ? and pe_pronombre = ?) and pe_cliente in (Select CUENTA from cuentaa)", Array As Object(m.Get("cedis"), Value))
'				Else
'					If thisLog Then Log($"SUMAMOS ${m.get("cant")} a ${m.get("prodId")} "$)
'					Starter.skmt.ExecNonQuery2($"update ${Subs.traeTablaProds(Starter.tipov)} set cat_gp_almacen = cat_gp_almacen + ? where cat_gp_id = ?"$, Array As Object(m.get("cant"), m.get("prodId")))
'					If thisLog Then Log($"BORRAMOS ${Value}, ${m.Get("cedis")}"$)
'					Starter.skmt.ExecNonQuery2("delete from pedido where pe_pronombre = ?  and pe_cedis = ? and pe_cant = ? and pe_cliente in (Select CUENTA from cuentaa)", Array As Object(Value, m.Get("cedis"), m.Get("cant")))
'				End If
'				prod.Close
'				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
'				clie_id = c.GetString("CUENTA")
'				c=Starter.skmt.ExecQuery("select USUARIO from usuarioa")
'				c.Position=0
'				usuario = c.GetString("USUARIO")
'				c=Starter.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
'					
'					Log($"==============   TOTAL = ${c.GetString("TOTAL_CLIE")}"$)
'					If c.GetString("TOTAL_CLIE") < 1 Then
'						Starter.skmt.ExecNonQuery("delete from pedido where PE_PRONOMBRE like 'CAMBIO%' and PE_CLIENTE In (select cuenta from cuentaa)"$)
'					End If
'					
'					Starter.skmt.ExecNonQuery("delete from pedido_cliente where PC_CLIENTE In (select cuenta from cuentaa)")
'					If thisLog Then LogColor($"Insertamos coordenadas en pedido: ${Starter.lon_gps}, ${Starter.lat_gps}"$, Colors.Magenta)
'					Starter.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"), Starter.lon_gps, Starter.lat_gps))
'					Starter.skmt.ExecNonQuery("UPDATE kmt_info set gestion = 2  where CAT_CL_CODIGO In (select cuenta from cuentaa)")
'				Else
'					Starter.skmt.ExecNonQuery("delete from pedido_cliente where PC_CLIENTE In (select cuenta from cuentaa)")
'					Starter.skmt.ExecNonQuery("UPDATE kmt_info set gestion = 0  where CAT_CL_CODIGO In (select cuenta from cuentaa)")
'				End If
'				If thisLog Then Log("Llamamos LlenaProdsLL")
'				If B4XPages.MainPage.productos.PCLV.IsInitialized Then B4XPages.MainPage.productos.LlenaProdsLL(Null, Null)
'				B4XPage_Appear
'			End If
'		Else
'			Msgbox("CLIENTE YA SE TRANSMITIO, FAVOR DE LLAMAR A SOPORTE PARA ASISTENCIA","AVISO") 'ignore
'		End If
'	Else
'		ToastMessageShow("Esta venta ya esta impresa, no se puede editar ni borrar.", False)
'	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 = Starter.skmt.ExecQuery("Select CUENTA from cuentaa")
		c.Position = 0
		clie_id = c.GetString("CUENTA")
		c.Close
		c = Starter.skmt.ExecQuery("select USUARIO from usuarioa")
		c.Position = 0
		usuario = c.GetString("USUARIO")
		c.Close
		result =  Msgbox2("Seguro que desea dar descuento del 5%?","Dar descuento", "Si", "", "No",LoadBitmap(File.DirAssets,"alert2.png")) 'ignore
		If result = DialogResponse.POSITIVE Then
			Starter.skmt.ExecNonQuery("UPDATE pedido SET PE_COSTO_TOT = PE_COSTO_TOT * .95, PE_DESC = 5 WHERE PE_CLIENTE In (select cuenta from cuentaa)")
			c = Starter.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
			Starter.skmt.ExecNonQuery("delete from pedido_cliente where PC_CLIENTE In (select cuenta from cuentaa)")
			If logger Then LogColor($"Insertamos coordenadas en pedido: ${Starter.lon_gps}, ${Starter.lat_gps}"$, Colors.Magenta)
			Starter.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"), Starter.lon_gps, Starter.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 = Starter.skmt.ExecQuery("Select CUENTA from cuentaa")
		c.Position = 0
		clie_id = c.GetString("CUENTA")
		c.Close
		c = Starter.skmt.ExecQuery("select USUARIO from usuarioa")
		c.Position = 0
		usuario = c.GetString("USUARIO")
		c.Close
		result =  Msgbox2("Seguro que desea CANCELAR el descuento?","Cancelar descuento", "Si", "", "No",LoadBitmap(File.DirAssets,"alert2.png")) 'ignore
		If result = DialogResponse.POSITIVE Then
			Starter.skmt.ExecNonQuery("UPDATE pedido SET PE_COSTO_TOT = PE_COSTO_TOT / .95, PE_DESC = 0 WHERE PE_CLIENTE In (select cuenta from cuentaa)")
			c = Starter.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
			Starter.skmt.ExecNonQuery("delete from pedido_cliente where PC_CLIENTE In (select cuenta from cuentaa)")
			If logger Then LogColor($"Insertamos coordenadas en pedido: ${Starter.lon_gps}, ${Starter.lat_gps}"$, Colors.Magenta)
			Starter.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"), Starter.lon_gps, Starter.lat_gps))
			c.Close
			B4XPage_Appear
		End If
	End If
End Sub

Private Sub B_PEDIDO_DOE_Click
	If B_PEDIDO_DOE.Text = "IR A PEDIDO DON TOÑO" Then
		pedidoMostrado = "DOE"
		l_tipoPedido.Text = "PEDIDO DON TOÑO"
		ListView1.Visible = False
		L_CANT.Visible = False
		L_TOTAL.Visible = False
		B_PEDIDO_DOE.Text = "IR A PEDIDO NORMAL"
		lv_prodsDOE.Visible = True
		L_CANT_D.Visible = True
		L_TOTAL_D.Visible = True
	Else
		pedidoMostrado = "NORMAL"
		l_tipoPedido.Text = "PEDIDO NORMAL"
		ListView1.Visible = True
		B_PEDIDO_DOE.Text = "IR A PEDIDO DON TOÑO"
		lv_prodsDOE.Visible = False
		L_CANT_D.Visible = False
		L_TOTAL_D.Visible = False
		L_CANT.Visible = True
		L_TOTAL.Visible = True
	End If
End Sub

Private Sub clv_pedidos_ItemLongClick (position As Int, Value As Object)
	Private thisLog As Boolean = False
	c = Starter.skmt.ExecQuery("select count(*) as hayImpresos from CLIENTE_IMPRESO where CI_CUENTA in  (Select CUENTA from cuentaa)")
	c.Position = 0
	If thisLog Then Log("lista prods="&list_prodsPedido.Get(position))
	Private m As Map = list_prodsPedido.Get(position)
	If c.GetString("hayImpresos") = 0 Then 'Si la venta no está impresa o es abordo entonces continuamos ...   'Para que el abordo borre siempre " Or Subs.clienteActual = "0""
		c=Starter.skmt.ExecQuery2("select count(*) as CUANTOS FROM PEDIDO WHERE PE_CEDIS = ? AND PE_CLIENTE IN (Select CUENTA from cuentaa)", Array As String("DUR"))
		c.Position = 0
		CAMBIOS = c.GetString("CUANTOS")
		c.Close
		c = Starter.skmt.ExecQuery2("select PE_PRONOMBRE, PE_COSTO_TOT, PE_CANT, PE_FOLIO FROM PEDIDO WHERE PE_CLIENTE IN (Select CUENTA from cuentaa) AND PE_PRONOMBRE = ? order by PE_PRONOMBRE asc", Array As String(Value) )
		c.Position = 0
		If c.GetString("PE_FOLIO") = "0" Or 1 = 1 Then   'Para que el abordo borre siempre agregar -> " Or Subs.clienteActual = "0" "
			result =  Msgbox2("Seguro que desea borrar este articulo?","Borrar Articulo", "Si", "", "No",LoadBitmap(File.DirAssets,"alert2.png")) 'ignore
			If result = DialogResponse.POSITIVE Then
				Private prod As Cursor = Starter.skmt.ExecQuery2("select PE_PROID, PE_CANT, PE_CEDIS FROM PEDIDO where pe_pronombre = ? and pe_cliente in (Select CUENTA from cuentaa) ", Array As String(Value))
				prod.Position = 0
				If thisLog Then Log("m2= " & m)
				c.Close
				If thisLog Then Log($"Buscamos: ${Value}, ${m.Get("cedis")}"$)
				c = Starter.skmt.ExecQuery2("select count(*) AS CUANTOS from cat_gunaprod2 where CAT_GP_ID in (select pe_cedis from pedido where pe_pronombre = ? and pe_cedis = ? and pe_cliente in (Select CUENTA from cuentaa)) and CAT_GP_CLASIF = 'PROMOS' AND CAT_GP_TIPO = 'PROMOS' AND CAT_GP_SUBTIPO = 'PROMOS'", Array As String(Value, m.Get("cedis")))
				c.Position = 0
				If c.GetString("CUANTOS") > 0 Then 'Si es promo, borramos la promo completa del pedido.
					If thisLog Then Log($"Borramos de PROMO ${Value}"$)
					Private pPromos As Cursor = Starter.skmt.ExecQuery($"select pe_proid, pe_cant from pedido where pe_cedis = '${m.Get("cedis")}'"$)
					For tp = 0 To pPromos.RowCount - 1
						pPromos.Position = tp
						If thisLog Then Log($"SUMAMOS ${pPromos.GetString("PE_CANT")} a ${pPromos.GetString("PE_PROID")} "$)
						Starter.skmt.ExecNonQuery($"update ${Subs.traeTablaProds(Starter.tipov)} set cat_gp_almacen = cat_gp_almacen + ${pPromos.GetString("PE_CANT")} where cat_gp_id = '${pPromos.Getstring("PE_PROID")}'"$)
					Next
					Starter.skmt.ExecNonQuery2("delete from pedido where pe_cedis in (select pe_cedis from pedido where PE_CEDIS = ? and pe_pronombre = ?) and pe_cliente in (Select CUENTA from cuentaa)", Array As Object(m.Get("cedis"), Value))
				Else
					If thisLog Then Log($"SUMAMOS ${m.get("cant")} a ${m.get("prodId")} "$)
					'Si no es RMI, actualizamos el inventario.
					If prod.GetString("PE_CEDIS") <> "DUR" Then Starter.skmt.ExecNonQuery2($"update ${Subs.traeTablaProds(Starter.tipov)} set cat_gp_almacen = cat_gp_almacen + ? where cat_gp_id = ?"$, Array As Object(m.get("cant"), m.get("prodId")))
					If thisLog Then Log($"BORRAMOS ${Value}, ${m.Get("cedis")}"$)
					Starter.skmt.ExecNonQuery2("delete from pedido where pe_pronombre = ?  and pe_cedis = ? and pe_cant = ? and pe_cliente in (Select CUENTA from cuentaa)", Array As Object(Value, m.Get("cedis"), m.Get("cant")))
				End If
				prod.Close
				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
				clie_id = c.GetString("CUENTA")
				c = Starter.skmt.ExecQuery("select USUARIO from usuarioa")
				c.Position = 0
				usuario = c.GetString("USUARIO")
				c = Starter.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
'					Log($"==============   TOTAL = ${c.GetString("TOTAL_CLIE")}"$)
					If c.GetString("TOTAL_CLIE") < 1 Then
						Starter.skmt.ExecNonQuery("delete from pedido where PE_PRONOMBRE like 'CAMBIO%' and PE_CLIENTE In (select cuenta from cuentaa)"$)
					End If
					Starter.skmt.ExecNonQuery("delete from pedido_cliente where PC_CLIENTE In (select cuenta from cuentaa)")
					If thisLog Then LogColor($"Insertamos coordenadas en pedido: ${Starter.lon_gps}, ${Starter.lat_gps}"$, Colors.Magenta)
					Starter.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"), Starter.lon_gps, Starter.lat_gps))
					Starter.skmt.ExecNonQuery("UPDATE kmt_info set gestion = 2  where CAT_CL_CODIGO In (select cuenta from cuentaa)")
				Else
					Starter.skmt.ExecNonQuery("delete from pedido_cliente where PC_CLIENTE In (select cuenta from cuentaa)")
					Starter.skmt.ExecNonQuery("UPDATE kmt_info set gestion = 0  where CAT_CL_CODIGO In (select cuenta from cuentaa)")
				End If
				If thisLog Then Log("Llamamos LlenaProdsLL")
				If B4XPages.MainPage.productos.PCLV.IsInitialized Then B4XPages.MainPage.productos.LlenaProdsLL(Null, Null)
				B4XPage_Appear
			End If
		Else
			Msgbox("CLIENTE YA SE TRANSMITIO, FAVOR DE LLAMAR A SOPORTE PARA ASISTENCIA","AVISO") 'ignore
		End If
	Else
		ToastMessageShow("Esta venta ya esta impresa, no se puede editar ni borrar.", False)
	End If
End Sub

Private Sub lv_prodsDOE_ItemLongClick (Position As Int, Value As Object)
	If kh.revisaImpresa Then
		ToastMessageShow("Esta venta ya esta impresa, no se puede editar ni borrar.", False)
	Else
		If folio = "0" Or 1 = 1 Then 'Si el folio es 0 o el cliente actual es "abordo", los deja borrar. - Mod por CHV 220512 poner esto para borrar abordo "Or Subs.clienteActual = "0""
			result =  Msgbox2("Seguro que desea borrar el pedido?","Cancelar pedido", "Si", "", "No",LoadBitmap(File.DirAssets,"alert2.png")) 'ignore
			If result = DialogResponse.POSITIVE Then
				c=Starter.skmt.ExecQuery2("select PE_PROID,PE_CANT, PE_TIPO FROM PEDIDO where pe_cliente in (Select CUENTA from cuentaa) AND PE_CEDIS = ?", Array As String("DOE"))
				If c.RowCount > 0 Then
					For i = 0 To c.RowCount - 1
						c.Position = i
						TIPO = c.GetString("PE_TIPO")
						If c.GetString("PE_TIPO") = "VENTA" Or c.GetString("PE_TIPO") = "DOE" Then
							Log($"update cat_gunaprod2 set cat_gp_almacen = cat_gp_almacen + ${c.GetString("PE_CANT")} where cat_gp_id = 4{c.GetString("PE_PROID")}"$)
							Starter.skmt.ExecNonQuery2("update cat_gunaprod2 set cat_gp_almacen = cat_gp_almacen + ? where cat_gp_id = ?", Array As Object(c.GetString("PE_CANT"),c.GetString("PE_PROID")))
						Else
							Log("rrrr")
							Starter.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")))
						End If
					Next
				End If
				Starter.skmt.ExecNonQuery2("delete from pedido where pe_cliente in (Select CUENTA from cuentaa) AND PE_CEDIS = ?", Array As Object("DOE"))
				Starter.skmt.ExecNonQuery("delete from pedido_cliente where pc_cliente in (Select CUENTA from cuentaa)")
				Starter.skmt.ExecNonQuery("UPDATE kmt_info 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