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
	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 b_regresar As Button
	Dim ListView1 As ListView
	Dim L_CANT As Label
	Dim L_TOTAL As Label
	Dim b_borra As Button
	Dim Existe As String
	Dim result As String
	'    Dim lat_gps, lon_gps As String
	Dim cuantos As String
	Dim nombre_prod As String
	Dim cuantos_pedido As String
	Private Titulo As Label
	Private P1 As Panel
	Private lv2 As ListView
	Private p_principal As Panel
	Dim clv_pedido As CustomListView
	Private p_prods As Panel
	Private l_prodX As Label
	Private l_pCant As Label
	Private et_pCant As EditText
	Dim bmp As Bitmap
	Dim etCantHasFocus As Boolean = False
	Dim totalProds As Int = 0
	Dim totalCompra As Int = 0
	Dim prodsMap As Map
	Dim reconstruirPedido As Boolean = False
	Dim promoABorrar As String
	Private b_guardar As Button
	Private b_prodMas As Button
	Private b_prodMenos As Button
	Private b_prodMenosC As Button
	Private l_pCantC As Label
	Private et_pCantC As EditText
	Private b_prodMasC As Button
	Private p_botMasMenC As Panel
	Private p_botMasMen As Panel
	Private b_revversar As Button
	Private Label2 As Label
	Private Label3 As Label
	Private l_piezas As Label
	Private l_cajas As Label
	
	Private l_cajas As Label
	Private l_piezas As Label
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
	B4XPages.SetTitle(Me, "Productos")
	'load the layout to Root
	g.Initialize("GPS")
	Root.LoadLayout("detalleVenta")
	b_borra.Visible = False
	Titulo.Text = "Pedido"
End Sub

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

Sub B4XPage_Appear
	Label2.Visible = False
	Label3.Visible = False
	L_TOTAL.Visible = False
	L_CANT.Visible = False
	b_revversar.Visible = False
	Subs.centraPanel(p_principal, Root.Width)
'	b_guardar.Visible = False
	
'	Titulo.Left = Round(p_principal.Width/2)-(Titulo.Width/2)
	L_CANT.Text =""
	L_TOTAL.Text=""
	c=Starter.skmt.ExecQuery("select count(*) as EXISTE from HIST_VENTAS WHERE HVD_CLIENTE IN (Select CUENTA from cuentaa) and HVD_RECHAZO = 0 ")
	C.Position=0
	Existe = C.GetString("EXISTE")
	C.Close
'	c=Starter.skmt.ExecQuery("select distinct hist_ventas.hvd_num_registro, HIST_VENTAS.HVD_CLIENTE, HIST_VENTAS.HVD_PRONOMBRE, HIST_VENTAS.HVD_PROID, HIST_VENTAS.HVD_CANT, HIST_VENTAS2.HVD_CANT as HVD_CANT2, HIST_VENTAS.HVD_COSTO_TOT, HIST_VENTAS.HVD_CODPROMO from HIST_VENTAS inner join HIST_VENTAS2 on HIST_VENTAS.HVD_PROID = HIST_VENTAS2.HVD_PROID and HIST_VENTAS.HVD_CLIENTE = HIST_VENTAS2.HVD_CLIENTE WHERE HIST_VENTAS.HVD_CLIENTE IN (Select CUENTA from cuentaa) and HIST_VENTAS.HVD_RECHAZO = 0 order by HIST_VENTAS.HVD_CODPROMO desc, HIST_VENTAS.HVD_PRONOMBRE asc")
	
	
	c=Starter.skmt.ExecQuery("select * from HIST_VENTAS WHERE hvd_rechazo <> 2 and HVD_RECHAZOCANT > 0 order by HVD_CODPROMO desc, HVD_PRONOMBRE asc")
'	Private c2 As Cursor=Starter.skmt.ExecQuery("select  from PEDIDO WHERE PE_CLIENTE IN (SELECT CUENTA FROM CUENTAA)")
	
	ListView1.Clear
	clv_pedido.Clear
	Subs.SetDivider(ListView1, Colors.LightGray, 2)
	clv_pedido.GetBase.SetLayoutAnimated(100, 0dip, 60dip, Root.Width - 50, Root.Height * 0.62) 'Cambiamos el tamaño y posición de la lista de productos
	clv_pedido.Base_Resize(clv_pedido.GetBase.Width, clv_pedido.GetBase.Height) 'Cambiamos el tamaño del panel interno de la lista para que ajuste al nuevo tamaño.
'	Log("RC1:"&c.RowCount)
	If c.RowCount>0 Then
'		Log("Usamos HV")

		For i=0 To c.RowCount -1
			c.Position=i
'			Log(c.GetString("HVD_PROID"))
			If c.GetString("BCAJAS") = "0" Then
'				Log("AQUI1")
'				If c.GetString("HVD_CANT") <> c.GetString("HVD_RECHAZOCANT") Then
'					'Traemos cantidad de hvd2
'					Private oc As Cursor = Starter.skmt.ExecQuery($"select HVD_CANT from HIST_VENTAS2 WHERE HVD_CLIENTE IN (Select CUENTA from cuentaa) and hvd_proid = '${c.GetString("HVD_PROID")}' and HVD_NUM_REGISTRO = '${c.GetString("HVD_NUM_REGISTRO")}'"$)
'					Private cant2 As Int = 0
'					If oc.RowCount > 0 Then
'						oc.Position = 0
'						cant2 = oc.GetString("HVD_CANT")
'					End If
'					oc.Close
				Dim label1 As Label
				label1 = ListView1.TwoLinesLayout.Label
				label1.TextSize = 12
				label1.TextColor = Colors.black
				Dim Label2 As Label
				Label2 = ListView1.TwoLinesLayout.SecondLabel
				Label2.TextSize = 12
				Label2.TextColor = Colors.black
				Private bgColor, textColor As Int
				bgColor = Colors.RGB(177, 200, 249)'azul
				textColor = Colors.black
				If c.GetString("HVD_CODPROMO") <> Subs.traeAlmacen Then
'				bgColor = Colors.RGB(255, 212, 163) 'naranja
					bgColor = Colors.White
				End If
'			Log($"Agregamos prod a lista1 | ${c.GetString("HVD_CANT")} - ${c.GetString("HVD_CANT2")}"$) 'HVD_CANT2 es la original de la orden.
'			ListView1.AddTwoLines(c.GetString("HVD_PRONOMBRE"),"Cantidad #"& c.GetString("HVD_CANT")& " SubTotal $"& c.GetString("HVD_COSTO_TOT"))
				
				'aqui selecciono lo que esta en pedido del cliente actual
				Private c2 As Cursor=Starter.skmt.ExecQuery($"select IFNULL(PE_CANT,0) AS PE_CANT, CONSECUTIVO, PE_BCAJAS from PEDIDO WHERE PE_CLIENTE IN (SELECT CUENTA FROM CUENTAA) AND PE_PROID ='${c.GetString("HVD_PROID")}' AND CONSECUTIVO = '${c.GetString("CONSECUTIVO")}'"$)
				
				'aqui veo cuanto se ha vendido del rechazo del cliente original en el hist_ventas 
				Private d5 As Cursor =  Starter.skmt.ExecQuery($"SELECT IFNULL(Sum(PE_CANT),0) AS PE_CANT FROM PEDIDO WHERE PE_CLIENTEOR = '${c.GetString("HVD_CLIENTE")}' AND PE_PROID = '${c.GetString("HVD_PROID")}' AND CONSECUTIVO = '${c.GetString("CONSECUTIVO")}'"$)
				d5.Position = 0
				Private restacant As String = d5.GetString("PE_CANT")
				d5.Close
				
				If (c.GetString("HVD_RECHAZOCANT")-restacant) <> "0" Then				
					If c2.RowCount > 0 Then
						c2.Position = 0
					
						Private c3 As Cursor=Starter.skmt.ExecQuery($"select Ifnull(sum(PE_CANT),0) AS PE_CANT from PEDIDO WHERE PE_CLIENTE NOT IN (SELECT CUENTA FROM CUENTAA) AND PE_CLIENTEOR = '${c.GetString("HVD_CLIENTE")}' AND PE_PROID ='${c.GetString("HVD_PROID")}' AND CONSECUTIVO = '${c.GetString("CONSECUTIVO")}'"$)
						Private resta As String = 0
					
						If c3.RowCount > 0 Then
'						For i = 0 To c3.RowCount - 1
							c3.Position = 0
							resta =  c3.GetString("PE_CANT")
'						Next
						Else
							resta = 0
						End If
					
						Dim preciocat As Cursor = Starter.skmt.ExecQuery($"select IFNULL(CAT_GP_PRECIO,0) AS CAT_GP_PRECIO from CAT_GUNAPROD WHERE CAT_GP_ID = '${c.GetString("HVD_PROID")}'"$)
						If preciocat.RowCount > 0 Then
							preciocat.Position = 0
							Dim preciofull As String = preciocat.GetString("CAT_GP_PRECIO")
						End If
						preciocat.Close
						Log(c.GetString("HVD_CODPROMO"))
						Log(Subs.traeAlmacen)
						
						
						If c.GetString("HVD_CODPROMO") = Subs.traeAlmacen Then
							clv_pedido.Add(CreateListItem(c.GetString("HVD_PRONOMBRE"), Subs.traePrecio2(c.GetString("HVD_PROID"), c.GetString("HVD_CODPROMO"),c.GetString("CONSECUTIVO")), c2.GetString("PE_CANT"), c.GetString("HVD_RECHAZOCANT")-c2.GetString("PE_CANT") -resta, clv_pedido.AsView.Width, 118dip, bmp, c.GetString("HVD_PROID"), bgColor, textColor, c.GetString("HVD_CODPROMO"),c.GetString("HVD_CLIENTE"),c.GetString("CONSECUTIVO")),i)
						Else
							Log("aqui arriba")
							clv_pedido.Add(CreateListItem(c.GetString("HVD_PRONOMBRE"), preciofull, c2.GetString("PE_CANT"), c.GetString("HVD_RECHAZOCANT")-c2.GetString("PE_CANT") -resta, clv_pedido.AsView.Width, 118dip, bmp, c.GetString("HVD_PROID"), bgColor, textColor, c.GetString("HVD_CODPROMO"),c.GetString("HVD_CLIENTE"),c.GetString("CONSECUTIVO")),i)
						End If
'						clv_pedido.Add(CreateListItem(c.GetString("HVD_PRONOMBRE"), Subs.traePrecio2(c.GetString("HVD_PROID"), c.GetString("HVD_CODPROMO"),c.GetString("CONSECUTIVO")), c2.GetString("PE_CANT"), c.GetString("HVD_RECHAZOCANT")-c2.GetString("PE_CANT") -resta, clv_pedido.AsView.Width, 118dip, bmp, c.GetString("HVD_PROID"), bgColor, textColor, c.GetString("HVD_CODPROMO"),c.GetString("HVD_CLIENTE"),c.GetString("CONSECUTIVO")),i)
'						Log("ENTRE A PEDIDO")
					Else iF c2.RowCount = 0 Then
						Private c3 As Cursor=Starter.skmt.ExecQuery($"select Ifnull(sum(PE_CANT),0) AS PE_CANT from PEDIDO WHERE PE_CLIENTE NOT IN (SELECT CUENTA FROM CUENTAA) AND PE_CLIENTEOR = '${c.GetString("HVD_CLIENTE")}' AND PE_PROID ='${c.GetString("HVD_PROID")}' AND CONSECUTIVO = '${c.GetString("CONSECUTIVO")}'"$)
						Private resta As String = 0
					
						If c3.RowCount > 0 Then
'						For i = 0 To c3.RowCount - 1
							c3.Position = 0
							resta =  c3.GetString("PE_CANT")
'						Next
						Else
							resta = 0
						
						End If
						
						Dim preciocat As Cursor = Starter.skmt.ExecQuery($"select IFNULL(CAT_GP_PRECIO,0) AS CAT_GP_PRECIO from CAT_GUNAPROD WHERE CAT_GP_ID = '${c.GetString("HVD_PROID")}'"$)
						If preciocat.RowCount > 0 Then
							preciocat.Position = 0
							Dim preciofull As String = preciocat.GetString("CAT_GP_PRECIO")
						End If
						preciocat.Close
						Log(c.GetString("HVD_CODPROMO"))
						Log(Subs.traeAlmacen)
						
						If c.GetString("HVD_CODPROMO") = Subs.traeAlmacen Then
							clv_pedido.Add(CreateListItem(c.GetString("HVD_PRONOMBRE"), Subs.traePrecio2(c.GetString("HVD_PROID"), c.GetString("HVD_CODPROMO"),c.GetString("CONSECUTIVO")), 0, c.GetString("HVD_RECHAZOCANT") -resta, clv_pedido.AsView.Width, 118dip, bmp, c.GetString("HVD_PROID"), bgColor, textColor, c.GetString("HVD_CODPROMO"),c.GetString("HVD_CLIENTE"),c.GetString("CONSECUTIVO")),i)
						Else
							Log("aqui abajo")
							clv_pedido.Add(CreateListItem(c.GetString("HVD_PRONOMBRE"), preciofull, 0, c.GetString("HVD_RECHAZOCANT") -resta, clv_pedido.AsView.Width, 118dip, bmp, c.GetString("HVD_PROID"), bgColor, textColor, c.GetString("HVD_CODPROMO"),c.GetString("HVD_CLIENTE"),c.GetString("CONSECUTIVO")),i)
							
						End If
'							clv_pedido.Add(CreateListItem(c.GetString("HVD_PRONOMBRE"), Subs.traePrecio2(c.GetString("HVD_PROID"), c.GetString("HVD_CODPROMO"),c.GetString("CONSECUTIVO")), 0, c.GetString("HVD_RECHAZOCANT") -resta, clv_pedido.AsView.Width, 118dip, bmp, c.GetString("HVD_PROID"), bgColor, textColor, c.GetString("HVD_CODPROMO"),c.GetString("HVD_CLIENTE"),c.GetString("CONSECUTIVO")),i)
'						Log("ENTRE A HIST")
					End If
				End If
				c2.Close
'					Log(c.GetString("HVD_CODPROMO"))
'				End If
			Else If c.GetString("BCAJAS") = "1" Then
				Log("AQUI1")
'				If c.GetString("HVD_CANT") <> c.GetString("HVD_RECHAZOCANT") Then
				'Traemos cantidad de hvd2
				Private oc As Cursor = Starter.skmt.ExecQuery($"select HVD_CANT from HIST_VENTAS WHERE HVD_CLIENTE IN (Select CUENTA from cuentaa) and hvd_proid = '${c.GetString("HVD_PROID")}' and HVD_NUM_REGISTRO = '${c.GetString("HVD_NUM_REGISTRO")}'"$)
				Private cant2 As Int = 0
				If oc.RowCount > 0 Then
					oc.Position = 0
					cant2 = oc.GetString("HVD_CANT")
				End If
				oc.Close
				Dim label1 As Label
				label1 = ListView1.TwoLinesLayout.Label
				label1.TextSize = 12
				label1.TextColor = Colors.black
				Dim Label2 As Label
				Label2 = ListView1.TwoLinesLayout.SecondLabel
				Label2.TextSize = 12
				Label2.TextColor = Colors.black
				Private bgColor, textColor As Int
				bgColor = Colors.RGB(177, 200, 249)'azul
				textColor = Colors.black
				If c.GetString("HVD_CODPROMO") <> Subs.traeAlmacen Then
'				bgColor = Colors.RGB(255, 212, 163) 'naranja
					bgColor = Colors.White
				End If
'			Log($"Agregamos prod a lista1 | ${c.GetString("HVD_CANT")} - ${c.GetString("HVD_CANT2")}"$) 'HVD_CANT2 es la original de la orden.
'			ListView1.AddTwoLines(c.GetString("HVD_PRONOMBRE"),"Cantidad #"& c.GetString("HVD_CANT")& " SubTotal $"& c.GetString("HVD_COSTO_TOT"))

				Private c2 As Cursor=Starter.skmt.ExecQuery($"select * from PEDIDO WHERE PE_CLIENTE IN (SELECT CUENTA FROM CUENTAA)"$)
				
				Private d5 As Cursor =  Starter.skmt.ExecQuery($"SELECT IFNULL(Sum(PE_CANT),0) AS PE_CANT FROM PEDIDO WHERE PE_CLIENTEOR = '${c.GetString("HVD_CLIENTE")}' AND PE_PROID = '${c.GetString("HVD_PROID")}' AND CONSECUTIVO = '${c.GetString("CONSECUTIVO")}'"$)
				d5.Position = 0
				Private restacant As String = d5.GetString("PE_CANT")
				d5.Close
				
				If (c.GetString("HVD_RECHAZOCANT")-restacant) <> "0" Then
					If c2.RowCount > 0 Then
						c2.Position = 0
						Log("SI ENTRO A PONER CAJAS BIEN ")
						Log(c2.GetString("PE_CAJAS"))
						Private c3 As Cursor=Starter.skmt.ExecQuery($"select Ifnull(sum(PE_CANT),0) AS PE_CANT from PEDIDO WHERE PE_CLIENTE NOT IN (SELECT CUENTA FROM CUENTAA) AND PE_CLIENTEOR = '${c.GetString("HVD_CLIENTE")}' AND PE_PROID ='${c.GetString("HVD_PROID")}' AND CONSECUTIVO = '${c.GetString("CONSECUTIVO")}'"$)
						Private resta As String = 0
					
						If c3.RowCount > 0 Then
'						For i = 0 To c3.RowCount - 1
							c3.Position = 0
							resta =  c3.GetString("PE_CANT")
'						Next
						Else
							resta = 0
						End If
						Private puc As String = c.GetString("HVD_COSTO_TOT")/c.GetString("CANTC")
						LogColor((restacant/Subs.traeConversion(c.GetString("HVD_PROID"))),Colors.Green)
						clv_pedido.Add(CreateListItem2(c.GetString("HVD_PRONOMBRE"), puc, (restacant/Subs.traeConversion(c.GetString("HVD_PROID"))), ((c.GetString("HVD_RECHAZOCANT")- restacant-resta)/Subs.traeConversion(c.GetString("HVD_PROID"))), clv_pedido.AsView.Width, 118dip, bmp, c.GetString("HVD_PROID"), bgColor, textColor, c.GetString("HVD_CODPROMO"),c.GetString("HVD_CLIENTE"),c.GetString("CONSECUTIVO")),i)
					Else
						Log("NO ENTRO A PONER CAJAS BIEN ")
						Private c3 As Cursor=Starter.skmt.ExecQuery($"select Ifnull(sum(PE_CANT),0) AS PE_CANT from PEDIDO WHERE PE_CLIENTE NOT IN (SELECT CUENTA FROM CUENTAA) AND PE_CLIENTEOR = '${c.GetString("HVD_CLIENTE")}' AND PE_PROID ='${c.GetString("HVD_PROID")}' AND CONSECUTIVO = '${c.GetString("CONSECUTIVO")}'"$)
						Private resta As String = 0
					
						If c3.RowCount > 0 Then
'						For i = 0 To c3.RowCount - 1
							c3.Position = 0
							resta =  c3.GetString("PE_CANT")
'						Next
						Else
							resta = 0
						End If
						LogColor(resta,Colors.Green)
						Private puc As String = c.GetString("HVD_COSTO_TOT")/c.GetString("CANTC")
'						clv_pedido.Add(CreateListItem(c.GetString("HVD_PRONOMBRE"), Subs.traePrecio(c.GetString("HVD_PROID"), c.GetString("HVD_CODPROMO")), 0, c.GetString("HVD_RECHAZOCANT") , clv_pedido.AsView.Width, 118dip, bmp, c.GetString("HVD_PROID"), bgColor, textColor, c.GetString("HVD_CODPROMO"),c.GetString("HVD_CLIENTE")),i)
						clv_pedido.Add(CreateListItem2(c.GetString("HVD_PRONOMBRE"), puc, 0, ((c.GetString("HVD_RECHAZOCANT")-resta)/Subs.traeConversion(c.GetString("HVD_PROID"))), clv_pedido.AsView.Width, 118dip, bmp, c.GetString("HVD_PROID"), bgColor, textColor, c.GetString("HVD_CODPROMO"),c.GetString("HVD_CLIENTE"),c.GetString("CONSECUTIVO")),i)
					End If
				End If
				c2.Close
'				End If
			End If
		Next
		
'		cuentaProds("")
	End If
	
'	Private vc As Cursor = Starter.skmt.ExecQuery($"select HVD_PROID, HVD_CANT, IFNULL(HVD_RECHAZOCANT, 0) AS HVD_RECHAZOCANT, CAT_GP_PRECIO from HIST_VENTAS LEFT JOIN CAT_GUNAPROD ON CAT_GP_ID = HVD_PROID where BCAJAS = 0 and HVD_CODPROMO ='${Subs.traealmacen}' and HVD_CLIENTE in (select cuenta from cuentaa)"$)
'	
'	Private Cuenta As String = 0
'	Private arti As String = 0
'	If vc.RowCount > 0 Then
'		For i = 0 To vc.RowCount - 1
'			vc.Position = i
''			Log(vc.GetString("HVD_CANT")&","&vc.GetString("HVD_RECHAZOCANT"))
'			arti = arti + (vc.GetString("HVD_CANT") - vc.GetString("HVD_RECHAZOCANT"))
'			Cuenta = Cuenta + ((vc.GetString("HVD_CANT") - vc.GetString("HVD_RECHAZOCANT"))*vc.GetString("CAT_GP_PRECIO"))
'		Next
'	End If
''	Log(Cuenta)
'	
'	Private vc2 As Cursor = Starter.skmt.ExecQuery("select HVD_PROID, HVD_CANT, IFNULL(HVD_RECHAZOCANT, 0) AS HVD_RECHAZOCANT, CANTC,HVD_COSTO_TOT from HIST_VENTAS where HVD_CLIENTE in (select cuenta from cuentaa) AND BCAJAS = 1")
'	Private Cuenta1 As String = 0
'	Private arti1 As String = 0
'	If vc2.RowCount > 0 Then
'		For i = 0 To vc2.RowCount - 1
'			vc2.Position = i
'			Dim preu As Float = NumberFormat2(vc2.GetString("HVD_COSTO_TOT")/vc2.GetString("CANTC"),0,2,2,False)
'			Private vc3 As Cursor = Starter.skmt.ExecQuery($"select PRECIOCONVER, CONVERSION from CAT_GUNAPROD where CAT_GP_ID = '${vc2.GetString("HVD_PROID")}'"$)
'			If vc3.RowCount > 0 Then
'				vc3.Position = 0
'				Cuenta1 = Cuenta1 + ((vc2.GetString("CANTC")-(vc2.GetString("HVD_RECHAZOCANT")/vc3.GetString("CONVERSION")))*preu)
'				arti1 = arti1 + (vc2.GetString("HVD_CANT") - vc2.GetString("HVD_RECHAZOCANT"))
'			End If
'		Next
'	End If
''	Log(Cuenta1)
'	
'	Private vc4 As Cursor = Starter.skmt.ExecQuery($"select HVD_PROID, HVD_CANT, IFNULL(HVD_RECHAZOCANT, 0) AS HVD_RECHAZOCANT, CANTC,HVD_COSTO_TOT, HVD_RECHAZO, HVD_CODPROMO from HIST_VENTAS where HVD_CLIENTE in (select cuenta from cuentaa) AND BCAJAS = 0 and HVD_CODPROMO <> '${Subs.traeAlmacen}'"$)
'	Private Cuenta2 As String = 0
'	Private arti2 As String = 0
'	If vc4.RowCount > 0 Then
'		
'		For i = 0 To vc4.RowCount - 1
'			vc4.Position = i
'			If vc4.GetString("HVD_PROID") <> vc4.GetString("HVD_CODPROMO") Then
'				If vc4.GetString("HVD_RECHAZO") = 0 Then
'					Cuenta2 =  vc4.GetString("HVD_COSTO_TOT")
'					arti2 = vc4.GetString("HVD_CANT")
'				Else
'					Private vc5 As Cursor = Starter.skmt.ExecQuery($"select CAT_GP_PRECIO from CAT_GUNAPROD where CAT_GP_ID = '${vc4.GetString("HVD_PROID")}'"$)
'					If vc5.RowCount > 0 Then
'						vc5.Position= 0
''						Log(vc5.GetString("CAT_GP_PRECIO"))
'						Cuenta2 = Cuenta2 +((vc4.GetString("HVD_CANT")- vc4.GetString("HVD_RECHAZOCANT"))*vc5.GetString("CAT_GP_PRECIO"))
'						arti2 = arti2 +(vc4.GetString("HVD_CANT")- vc4.GetString("HVD_RECHAZOCANT"))
'					End If
'				End If
'			End If
'			
'		Next
'	End If
''	Log(Cuenta)
'	
'	L_TOTAL.Text = Cuenta + Cuenta1 + Cuenta2
'	L_CANT.Text = arti + arti1 +arti2
	

End Sub

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

Sub b_regresar_Click
	B4XPages.ShowPage("Cliente")
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

Sub ListView1_ItemLongClick (Position As Int, Value As Object) 'BORRAR
'	nombre_prod = Value
'	result =  Msgbox2("Seguro que desa borrar este articulo?","Borrar Articulo", "Si", "", "No",LoadBitmap(File.DirAssets,"alert2.png")) 'ignore
'	If result = DialogResponse.POSITIVE Then
'		c=Starter.skmt.ExecQuery2("select HVD_CANT FROM hist_ventas where HVD_pronombre = ? and HVD_cliente in (Select CUENTA from cuentaa) and HVD_RECHAZO = 0  ", Array As String(Value))
'		c.Position=0
'		If c.GetString("HVD_CANT") > 1 Then
'			b_regresar.Visible =False
'			b_borra.Visible=False
'			P1.Visible =True
'			cuantos_pedido = c.GetString("HVD_CANT")
'			lv2.Clear
'			lv2.SingleLineLayout.Label.TextColor = Colors.Black
'			For i=1 To c.GetString("HVD_CANT")
'				lv2.AddSingleLine(i)
'			Next
'		Else
'			'	 skmt.ExecNonQuery2("update cat_gunaprod set cat_gp_almacen = cat_gp_almacen + ? where cat_gp_nombre = ?", Array As Object(c.GetString("HVD_CANT"),Value))
'			c.Close
'			Starter.skmt.ExecNonQuery2("insert into reparto(REP_CLIENTE, REP_PRONOMBRE, REP_CANT, REP_COSTO_TOT,REP_FECHA, REP_CLI_ORIG)  select  HVD_CLIENTE, HVD_PRONOMBRE, HVD_CANT, HVD_COSTO_TOT, HVD_FECHA, HVD_CLIENTE from hist_ventas where HVD_PRONOMBRE = ? and HVD_cliente in (Select CUENTA from cuentaa) ", Array As String(Value))
'			c=Starter.skmt.ExecQuery2("select HVD_CANT FROM hist_ventas where HVD_pronombre = ? and HVD_cliente in (Select CUENTA from cuentaa) and HVD_RECHAZO = 0 ", Array As String(Value))
'			c.Position=0
'			Starter.skmt.ExecNonQuery2("update cat_gunaprod set cat_gp_almacen = cat_gp_almacen + ? where cat_gp_nombre = ?", Array As Object(c.GetString("HVD_CANT"),Value))
'			c.Close
'	
'			'skmt.ExecNonQuery2("delete FROM HIST_VENTAS WHERE HVD_PRONOMBRE = ? and HVD_cliente in (Select CUENTA from cuentaa) ", Array As String(Value))
'			' se cambia por update para no borrarlo y tener todos los registros.
'			Starter.skmt.ExecNonQuery2("update HIST_VENTAS set HVD_RECHAZO = 1 WHERE HVD_PRONOMBRE = ? and HVD_cliente in (Select CUENTA from cuentaa) ", Array As String(Value))
	'
'			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")
'			'quitar esta mamada no es ahi
'			Starter.skmt.ExecNonQuery("UPDATE kmt_info set gestion = 3  where CAT_CL_CODIGO In (select cuenta from cuentaa)")
'			B4XPage_Appear
'		End If
'			 
'	End If
End Sub

Sub lv2_ItemClick (Position As Int, Value As Object) 'BORRAR
'	'  If Value = "Todos" Then
'	' cuantos = c.GetString("HVD_CANT")
'	'Else
'	cuantos = Value
'	' End If
'	If cuantos = cuantos_pedido Then
'		'Starter.skmt.ExecNonQuery2("update cat_gunaprod set cat_gp_almacen = cat_gp_almacen + ? where cat_gp_nombre = ?", Array As Object(c.GetString("HVD_CANT"),nombre_prod))
'		c.Close
'		Starter.skmt.ExecNonQuery2("insert into reparto(REP_CLIENTE, REP_PRONOMBRE, REP_CANT, REP_COSTO_TOT, REP_FECHA, REP_CLI_ORIG)  select  HVD_CLIENTE, HVD_PRONOMBRE, HVD_CANT, HVD_COSTO_TOT, HVD_FECHA, HVD_CLIENTE from hist_ventas where HVD_PRONOMBRE = ? and HVD_cliente in (Select CUENTA from cuentaa) ", Array As String(nombre_prod))
'		c=Starter.skmt.ExecQuery2("select HVD_CANT FROM hist_ventas where HVD_pronombre = ? and HVD_cliente in (Select CUENTA from cuentaa) and HVD_RECHAZO = 0  ", Array As String(nombre_prod))
'		c.Position=0
'		Starter.skmt.ExecNonQuery2("update cat_gunaprod set cat_gp_almacen = cat_gp_almacen + ? where cat_gp_nombre = ?", Array As Object(c.GetString("HVD_CANT"),nombre_prod))
'		c.Close
'		'Starter.skmt.ExecNonQuery2("delete FROM HIST_VENTAS WHERE HVD_PRONOMBRE = ? and HVD_cliente in (Select CUENTA from cuentaa) ", Array As String(Value))
'		' se cambia por update para no borrarlo y tener todos los registros.
'		Starter.skmt.ExecNonQuery2("update HIST_VENTAS set HVD_RECHAZO = 1 WHERE HVD_PRONOMBRE = ? and HVD_cliente in (Select CUENTA from cuentaa) and HVD_RECHAZO = 0  ", Array As String(Value))
'		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")
'		'quitar esta m*m*d* no es ahi
'		Starter.skmt.ExecNonQuery("UPDATE kmt_info set gestion = 3  where CAT_CL_CODIGO In (select cuenta from cuentaa)")
'		Starter.skmt.ExecNonQuery2("update hist_Ventas set  HVD_RECHAZO = 1, HVD_PARCIAL = 0, HVD_CANT = HVD_CANT - ? where HVD_pronombre = ? and HVD_cliente in (Select CUENTA from cuentaa) and HVD_RECHAZO = 0 ", Array As Object(cuantos, nombre_prod))
'		b_regresar.Visible =True
'		b_borra.Visible=True
'		P1.Visible =False
'		B4XPage_Appear
'	Else
'		'	skmt.ExecNonQuery2("update cat_gunaprod set cat_gp_almacen = cat_gp_almacen + ? where cat_gp_nombre = ?", Array As Object(cuantos,nombre_prod))
'		c.Close
	'
'		'modificar tambien esto
'		'Modificaciones para que solo quite una parte.
'		Starter.skmt.ExecNonQuery2("insert into reparto(REP_CLIENTE, REP_PRONOMBRE, REP_CANT, REP_COSTO_TOT, REP_FECHA, REP_CLI_ORIG) select HVD_CLIENTE, HVD_PRONOMBRE, ?, ? * (HVD_COSTO_TOT/?), HVD_FECHA, HVD_CLIENTE from hist_ventas where HVD_PRONOMBRE = ? and HVD_cliente in (Select CUENTA from cuentaa) ", Array As Object(cuantos,cuantos,cuantos_pedido, nombre_prod))
'		Starter.skmt.ExecNonQuery2("insert into hist_ventas(HVD_CLIENTE,HVD_PRONOMBRE,HVD_CANT,HVD_COSTO_TOT, HVD_FECHA, HVD_CODPROMO, HVD_PROID,HVD_NUM_TICKET, HVD_NUM_REGISTRO, HVD_RECHAZO, HVD_ESTATUS, HVD_PARCIAL) select HVD_CLIENTE,HVD_PRONOMBRE,?,HVD_COSTO_TOT, HVD_FECHA, HVD_CODPROMO, HVD_PROID,HVD_NUM_TICKET, HVD_NUM_REGISTRO, 1, HVD_ESTATUS, 1 from hist_ventas where HVD_PRONOMBRE = ? and HVD_cliente in (Select CUENTA from cuentaa) ", Array As Object(cuantos, nombre_prod))
'		Starter.skmt.ExecNonQuery2("update hist_Ventas set  HVD_RECHAZO = 0, HVD_PARCIAL = 1, HVD_CANT = HVD_CANT - ? where HVD_pronombre = ? and HVD_cliente in (Select CUENTA from cuentaa) and HVD_RECHAZO = 0  ", Array As Object(cuantos, nombre_prod))
'		Starter.skmt.ExecNonQuery2("update hist_Ventas set  HVD_COSTO_TOT = HVD_CANT * (HVD_COSTO_TOT/?) where HVD_pronombre = ? and HVD_cliente in (Select CUENTA from cuentaa) and HVD_RECHAZO = 0  ", Array As Object(cuantos_pedido, nombre_prod))
'		Starter.skmt.ExecNonQuery2("update cat_gunaprod set cat_gp_almacen = cat_gp_almacen + ? where cat_gp_nombre = ?", Array As Object(cuantos,nombre_prod))
'		'crear nueva tabla para que guarde el resto.
	''		skmt.ExecNonQuery2("delete FROM HIST_VENTAS WHERE HVD_PRONOMBRE = ? and HVD_cliente in (Select CUENTA from cuentaa) ", Array As String(Value))
'		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")
'		Starter.skmt.ExecNonQuery("UPDATE kmt_info set gestion = 3  where CAT_CL_CODIGO In (select cuenta from cuentaa)")
'		B4XPage_Appear
'		b_regresar.Visible =True
'		b_borra.Visible=True
'		P1.Visible =False
'	End If
End Sub

'****************************************************************************
'***************** PARA EL MAS/MENOS *************************************
'****************************************************************************

Sub CreateListItem(Text As String, precioU As String, inv As Int, inv2 As Int, Width As Int, Height As Int, img As Bitmap, prodId As String, bc As Int, tc As Int, promo As String, cliente_original As String,consecutivo As Int) As Panel
	Dim p As B4XView = xui.CreatePanel("")
	Private cs As CSBuilder
	cs.Initialize
	p.SetLayoutAnimated(0, 0, 0, Width, Height)
	p.LoadLayout("prodItem2")
	p_prods.Color = bc
	l_prodX.TextColor = tc
	precioU = $"$1.2{precioU}"$
	l_prodX.Text = Text&CRLF&"Cant: " & inv2 & "   $" & precioU
	If promo <> "1" And precioU = 0 Then
		l_prodX.Text = cs.Color(Colors.RGB(123,0,0)).append(Text&CRLF&"Cant: " & inv2 & "   $" & precioU).PopAll
	End If
	l_prodX.Tag = $"ID: ${prodId}${CRLF}${Text}${CRLF}Precio: $$1.2{precioU}${CRLF}Inv: ${inv} pzs"$
'	l_pCant.Text = 0
'	Log("PRECIO " & Round2(precioU,2)&"| INV "&inv&"| ID "&prodId&"| PROMO "&promo&"| INV2? "&inv2&"| CLIENTE OR NONONO YA NO VA Y UN ESPACIO POR FAVOR "&cliente_original)
	l_pCant.Tag = Round2(precioU.Replace(",",""),2)&"|"&inv&"|"&prodId&"|"&promo&"|"&inv2&"|"&cliente_original&"|"&consecutivo
	et_pCant.Tag = Round2(precioU.Replace(",",""),2)&"|"&inv&"|"&prodId&"|"&promo&"|"&inv2&"|"&cliente_original&"|"&consecutivo
	p_prods.Tag = l_pCant.tag
'	Log($"Ponemos la cant en ${inv}"$)
	l_pCant.Text = inv
'	et_pCant.Text = inv
'	et_pCant.BringToFront

	l_pCant.BringToFront
	et_pCant.SendToBack
	p_botMasMenC.Visible = False
	l_cajas.Visible = False
'	i_prod.Bitmap = img
	Return p
End Sub

Sub CreateListItem2(Text As String, precioU As String, inv As Int, inv2 As Int, Width As Int, Height As Int, img As Bitmap, prodId As String, bc As Int, tc As Int, promo As String, cliente_original As String,consecutivo As Int) As Panel
	Dim p As B4XView = xui.CreatePanel("")
	Private cs As CSBuilder
	cs.Initialize
	p.SetLayoutAnimated(0, 0, 0, Width, Height)
	p.LoadLayout("prodItem2")
	p_prods.Color = bc
	l_prodX.TextColor = tc
	precioU = $"$1.2{precioU}"$
	l_prodX.Text = Text&CRLF&"Cant: " & inv2 & "   $" & precioU
	If promo <> "1" And precioU = 0 Then
		l_prodX.Text = cs.Color(Colors.RGB(123,0,0)).append(Text&CRLF&"Cant: " & inv2 & "   $" & precioU).PopAll
	End If
	
	Log(inv&" inventario 1")
	Log(inv2&" inventario 2")
	l_prodX.Tag = $"ID: ${prodId}${CRLF}${Text}${CRLF}Precio: $$1.2{precioU}${CRLF}Inv: ${inv} pzs"$
'	l_pCant.Text = 0
	l_pCantC.Tag = Round2(precioU.Replace(",",""),2)&"|"&inv&"|"&prodId&"|"&promo&"|"&inv2&"|"&cliente_original&"|"&consecutivo
	et_pCantC.Tag = Round2(precioU.Replace(",",""),2)&"|"&inv&"|"&prodId&"|"&promo&"|"&inv2&"|"&cliente_original&"|"&consecutivo
	p_prods.Tag = l_pCantC.tag
'	Log($"Ponemos la cant en ${inv}"$)
	l_pCantC.Text = NumberFormat2(inv,0,0,0,False)
'	et_pCant.Text = inv
'	et_pCant.BringToFront

	l_pCantC.BringToFront
	et_pCantC.SendToBack
	p_botMasMen.Visible = False
	l_piezas.Visible = False
'	i_prod.Bitmap = img
	Return p
End Sub

Sub b_prodMenos_Click
	etCantHasFocus = False
	Log("etCantHasFocus=" & etCantHasFocus)
	LogColor("b_prodMenos_Click", Colors.Magenta)
	Dim index As Int = clv_pedido.GetItemFromView(Sender)
	Dim pnl0 As B4XView = clv_pedido.GetPanel(index)
	Dim pnl As B4XView = pnl0.GetView(0)
	Dim laCant As B4XView = pnl.GetView(2).GetView(3)
'	Log(pnl.GetView(2).GetView(0) & "|" & pnl.GetView(2).GetView(1) & "|" & pnl.GetView(2).GetView(2))
	Dim esteTag As List = Regex.Split("\|", laCant.Tag)
	Log("LC_TEXT:"&laCant.Text&"|PROMO:"&esteTag.Get(3)&"|LC_TAG:"&laCant.Tag&"|ET:"&esteTag)
	
	Private minimoadesc As String
	Dim minimos As Cursor = Starter.skmt.ExecQuery2("SELECT * FROM CAT_GUNAPROD3 WHERE CAT_GP_ID = ?", Array As String(esteTag.Get(2)))
	If minimos.RowCount > 0 Then
		minimos.Position = 0
		minimoadesc = minimos.GetString("CAT_DP_CANT_MIN_VENTA")
	Else
		minimoadesc = "1"
	End If
	minimos.Close
	'*'${minimoadesc}')
	minimoadesc = "1"
	If laCant.Text > 0 Then
'		If esteTag.Get(3) <> Subs.traeAlmacen Then 'Si es PROMO entonces ...
'			Private c2 As Cursor =Starter.skmt.ExecQuery($"select HVD_RECHAZO FROM HIST_VENTAS WHERE HVD_PROID = '${esteTag.Get(2)}' AND HVD_CLIENTE IN (Select CUENTA from cuentaa)"$)
'			c2.Position = 0
'			If c2.GetString("HVD_RECHAZO") = "0" Then
'			
'			
'				Log("ES PROMO")
'				result =  Msgbox2("Si se modifica una promoción, la promoción se rompe y solo quedarán los productos sueltos, ¿seguro que desea continuar?","Modificar Promoción", "Si", "", "No",LoadBitmap(File.DirAssets,"alert2.png")) 'ignore
'				If result = DialogResponse.POSITIVE Then 'Quitamos esta promo
'					prodsMap.Remove(esteTag.Get(3))
''				c=Starter.skmt.ExecQuery($"select HVD_CODPROMO FROM HIST_VENTAS WHERE HVD_CODPROMO = '${esteTag.Get(2)}' AND HVD_cliente IN (Select CUENTA from cuentaa)"$)
''				For i = 0 To c.RowCount -1
''					c.Position = i
'					LogColor(esteTag.Get(3),Colors.Blue)
'					Starter.skmt.ExecNonQuery($"update HIST_VENTAS set HVD_RECHAZO = 1,  HVD_RECHAZOCANT = HVD_RECHAZOCANT + 1  WHERE  HVD_CODPROMO = '${esteTag.Get(3)}' and HVD_CLIENTE =  '${esteTag.Get(5)}'"$)
'					Starter.skmt.ExecNonQuery($"update HIST_VENTAS set HVD_RECHAZO = 2,  HVD_RECHAZOCANT = HVD_RECHAZOCANT + 1  WHERE HVD_PROID = '${esteTag.Get(3)}' and HVD_CODPROMO = '${esteTag.Get(3)}' and HVD_CLIENTE in (Select CUENTA from cuentaa)"$)
''				Starter.skmt.ExecNonQuery($"update HIST_VENTAS set HVD_CODPROMO = '${Subs.traeAlmacen}' WHERE HVD_cliente in (Select CUENTA from cuentaa) AND HVD_CODPROMO = '${esteTag.Get(3)}'"$)
''				Next
'					c.Close
'					LogColor(prodsMap, Colors.red)
'					reconstruirPedido = True
'					promoABorrar = esteTag.Get(3)
'					B4XPage_Appear
'				End If
'			Else
'				If laCant.Text = "" Then laCant.Text = 0
'				laCant.Text = $"$1.0{laCant.Text-1}"$
'				If laCant.Text < 0 Then laCant.Text = 0
'				Log("NO ES promo")
'				Starter.skmt.ExecNonQuery($"update HIST_VENTAS set HVD_RECHAZO = 1,  HVD_RECHAZOCANT = IFNULL(HVD_RECHAZOCANT,0) + 1  WHERE HVD_PROID = '${esteTag.Get(2)}' and HVD_cliente in (Select CUENTA from cuentaa)"$)
'				Starter.skmt.ExecNonQuery2("update cat_gunaprod set cat_gp_almacen = cat_gp_almacen + 1 where cat_gp_id = ?", Array As Object(esteTag.Get(2)))
'				B4XPage_Appear
'			End If
'		Else
		If laCant.Text = "" Then laCant.Text = 0
		laCant.Text = $"$1.0{laCant.Text-(1*minimoadesc)}"$
		If laCant.Text < 0 Then laCant.Text = 0
		Log("NO ES promo")
'		Starter.skmt.ExecNonQuery($"update HIST_VENTAS set HVD_RECHAZO = 1,  HVD_RECHAZOCANT = IFNULL(HVD_RECHAZOCANT,0) + 1  WHERE HVD_PROID = '${esteTag.Get(2)}' and HVD_cliente = '${esteTag.Get(5)}'"$)
'			Starter.skmt.ExecNonQuery2("update cat_gunaprod set cat_gp_almacen = cat_gp_almacen + 1 where cat_gp_id = ?", Array As Object(esteTag.Get(2)))
'		Starter.skmt.ExecNonQuery2("update PEDIDO set cat_gp_almacen = cat_gp_almacen + 1 where cat_gp_id = ?", Array As Object(esteTag.Get(2)))

		Private c3 As Cursor = Starter.skmt.ExecQuery2("SELECT PE_CANT FROM PEDIDO WHERE PE_PROID = ? AND PE_CLIENTE IN (SELECT CUENTA FROM CUENTAA) AND CONSECUTIVO = ? ", Array As String(esteTag.Get(2), esteTag.Get(6)))
		If c3.RowCount > 0 Then
			c3.Position = 0
			If c3.GetString("PE_CANT")> minimoadesc Then
				Private c2 As Cursor = Starter.skmt.ExecQuery2("SELECT * FROM CAT_GUNAPROD WHERE CAT_GP_ID = ?", Array As String(esteTag.Get(2)))
				If c2.RowCount > 0 Then
					c2.Position = 0
'			Starter.skmt.ExecNonQuery($"update HIST_VENTAS set HVD_RECHAZO = 1,  HVD_RECHAZOCANT = HVD_RECHAZOCANT - 1  WHERE HVD_PROID = '${esteTag.Get(2)}' and HVD_cliente =  '${esteTag.Get(5)}'"$)
					Starter.skmt.ExecNonQuery($"update PEDIDO set PE_CANT = IFNULL(PE_CANT,0)-(1*'${minimoadesc}'), PE_COSTO_TOT= ((IFNULL(PE_CANT,0) -(1*'${minimoadesc}'))*'${c2.GetString("CAT_GP_PRECIO")}') WHERE PE_PROID = '${esteTag.Get(2)}' and PE_CLIENTE in (Select CUENTA from cuentaa) AND CONSECUTIVO = '${esteTag.Get(6)}'"$)
				End If
			Else If c3.GetString("PE_CANT") = minimoadesc Then
				Private c2 As Cursor = Starter.skmt.ExecQuery2("SELECT * FROM CAT_GUNAPROD WHERE CAT_GP_ID = ?", Array As String(esteTag.Get(2)))
				If c2.RowCount > 0 Then
					c2.Position = 0
'			Starter.skmt.ExecNonQuery($"update HIST_VENTAS set HVD_RECHAZO = 1,  HVD_RECHAZOCANT = HVD_RECHAZOCANT - 1  WHERE HVD_PROID = '${esteTag.Get(2)}' and HVD_cliente =  '${esteTag.Get(5)}'"$)
'					Starter.skmt.ExecNonQuery($"update PEDIDO set PE_CANT = IFNULL(PE_CANT,0)-1, PE_COSTO_TOT= ((IFNULL(PE_CANT,0) -1)*'${c2.GetString("CAT_GP_PRECIO")}') WHERE PE_PROID_PROID = '${esteTag.Get(2)}' and PE_CLIENTE in (Select CUENTA from cuentaa)"$)
					Starter.skmt.ExecNonQuery($"delete from PEDIDO where PE_PROID = '${esteTag.Get(2)}' and PE_CLIENTE in (Select CUENTA from cuentaa) AND CONSECUTIVO = '${esteTag.Get(6)}'"$)
				End If
			End If
		
		
		
		End If
		B4XPage_Appear
''		End If
	End If
	Dim preciou As Float = esteTag.Get(0)/esteTag.Get(1)
	Log("PU: " & preciou)
'	LogColor("estamos aqui mmenos " & laCant.Text& " , " & (preciou * laCant.Text),Colors.Green )
'	If L_CANT.Text - 1 >= 0  Then 
'		L_CANT.Text = L_CANT.Text - 1
'	End If
'	b_guardar.Visible = True
'	Subs.prodRechazo(esteTag.Get(5), esteTag.Get(2))
'	Private cym As Map = Subs.traemosCantYMonto(clv_pedido)
'	L_CANT.Text = cym.Get("cantidad")
'	L_TOTAL.Text = Round2(cym.Get("monto"), 2)
'	cuentaProds("-")
End Sub

Sub b_prodMas_Click
	etCantHasFocus = False
	Dim sDate, sTime As String
	DateTime.DateFormat = "dd/MM/yyyy"
	DateTime.TimeFormat = "HH:mm:ss"
	sDate = DateTime.Date(DateTime.Now)
	sTime = DateTime.Time(DateTime.Now)
'	Log("etCantHasFocus=" & etCantHasFocus)
	LogColor("b_prodMas_Click", Colors.Magenta)
	Dim index As Int = clv_pedido.GetItemFromView(Sender)
	Dim pnl0 As B4XView = clv_pedido.GetPanel(index)
	Dim pnl As B4XView = pnl0.GetView(0)
	Dim laCant As B4XView = pnl.GetView(2).GetView(3)
'	Log(pnl.GetView(2).GetView(1).text&"|"&pnl.GetView(2).GetView(2)&"|"&pnl.GetView(2).GetView(4).text)
'	Log($"precio|stock:${laCant.tag}"$)
'	Log($"Indice: ${index}, cant:${laCant.Text+1}, precioU: ${laCant.tag}"$)
	Dim esteTag As List = Regex.Split("\|", laCant.Tag)
	Log("LC_TEXT:"&laCant.Text&"|LC_TAG:"&laCant.Tag&"|ET:"&esteTag)
	If laCant.Text = "" Then laCant.Text = 0
	
	Private maxProds = esteTag.Get(1) + esteTag.Get(4) 'Prods disponibles + prods comprados
	
	Private minimoadesc As String
	Dim minimos As Cursor = Starter.skmt.ExecQuery2("SELECT * FROM CAT_GUNAPROD3 WHERE CAT_GP_ID = ?", Array As String(esteTag.Get(2)))
	If minimos.RowCount > 0 Then
		minimos.Position = 0
		minimoadesc = minimos.GetString("CAT_DP_CANT_MIN_VENTA")
	Else
		minimoadesc = "1"
	End If
	minimos.Close
	minimoadesc = "1"
	'*'${minimoadesc}')
	
	LogColor($"++++++++++++++++++++++++++   ${esteTag}"$, Colors.Green)
	Log(esteTag.get(4))
	If esteTag.Get(4) = "0" Then
		
	Else
		If laCant.Text + 1 <= maxProds Then
			Log($"NuevaCant = ${laCant.Text + 1}"$)
			laCant.Text = $"$1.0{laCant.Text + 1}"$
			Starter.skmt.ExecNonQuery2("update cat_gunaprod set cat_gp_almacen = cat_gp_almacen - 1 where cat_gp_id = ?", Array As String(esteTag.Get(2)))
			c = Starter.skmt.ExecQuery2("SELECT * FROM PEDIDO WHERE PE_PROID = ? and PE_CLIENTE IN (SELECT CUENTA FROM CUENTAA) AND CONSECUTIVO = ? ", Array As String(esteTag.Get(2), esteTag.Get(6)))
			If c.RowCount > 0 Then
				Private c2 As Cursor = Starter.skmt.ExecQuery2("SELECT * FROM HIST_VENTAS WHERE HVD_PROID = ? AND CONSECUTIVO = ? ", Array As String(esteTag.Get(2), esteTag.Get(6)))
				If c2.RowCount > 0 Then
					c2.Position = 0
					
					Dim preciocat As Cursor = Starter.skmt.ExecQuery($"select IFNULL(CAT_GP_PRECIO,0) AS CAT_GP_PRECIO from CAT_GUNAPROD WHERE CAT_GP_ID = '${c2.GetString("HVD_PROID")}'"$)
					If preciocat.RowCount > 0 Then
						preciocat.Position = 0
						Dim preciofull As String = preciocat.GetString("CAT_GP_PRECIO")
					End If
					preciocat.Close
					
					If c2.GetString("HVD_CODPROMO") = Subs.traeAlmacen Then
						Private pup As String = c2.GetString("HVD_COSTO_TOT")/c2.GetString("HVD_CANT")
'						Starter.skmt.ExecNonQuery($"update HIST_VENTAS set HVD_RECHAZO = 1,  HVD_RECHAZOCANT = HVD_RECHAZOCANT - 1  WHERE HVD_PROID = '${esteTag.Get(2)}' and HVD_cliente =  '${esteTag.Get(5)}'"$)
						Starter.skmt.ExecNonQuery($"update PEDIDO set PE_CANT = IFNULL(PE_CANT,0)+(1*'${minimoadesc}'), PE_COSTO_TOT= ((IFNULL(PE_CANT,0)+(1*'${minimoadesc}'))*'${pup}') WHERE PE_PROID = '${esteTag.Get(2)}' and PE_CLIENTE in (Select CUENTA from cuentaa) AND CONSECUTIVO = '${esteTag.Get(6)}'"$)
					Else
						Starter.skmt.ExecNonQuery($"update PEDIDO set PE_CANT = IFNULL(PE_CANT,0)+(1*'${minimoadesc}'), PE_COSTO_TOT= ((IFNULL(PE_CANT,0)+(1*'${minimoadesc}'))*'${preciofull}') WHERE PE_PROID = '${esteTag.Get(2)}' and PE_CLIENTE in (Select CUENTA from cuentaa) AND CONSECUTIVO = '${esteTag.Get(6)}'"$)
					End If
					
				End If
			Else
				Private c2 As Cursor = Starter.skmt.ExecQuery2("SELECT * FROM HIST_VENTAS WHERE HVD_PROID = ? AND CONSECUTIVO = ? ", Array As String(esteTag.Get(2), esteTag.Get(6)))
				If c2.RowCount > 0 Then
					c2.Position = 0
					
					Dim preciocat As Cursor = Starter.skmt.ExecQuery($"select IFNULL(CAT_GP_PRECIO,0) AS CAT_GP_PRECIO from CAT_GUNAPROD WHERE CAT_GP_ID = '${c2.GetString("HVD_PROID")}'"$)
					If preciocat.RowCount > 0 Then
						preciocat.Position = 0
						Dim preciofull As String = preciocat.GetString("CAT_GP_PRECIO")
					End If
					preciocat.Close
					
					Dim precioreg As Cursor = Starter.skmt.ExecQuery($"select IFNULL(HVD_COSTO_TOT,0) AS HVD_COSTO_TOT, HVD_CANT from HIST_VENTAS WHERE HVD_PROID = '${c2.GetString("HVD_PROID")}' AND CONSECUTIVO = '${esteTag.Get(6)}'"$)
					If precioreg.RowCount > 0 Then
						precioreg.Position = 0
						Dim precioregalo As String = precioreg.GetString("HVD_COSTO_TOT")/precioreg.GetString("HVD_CANT")
						If precioregalo < 1 Then
							Dim esregalo As String = 1
						Else
							Dim esregalo As String = 0
						End If
					End If
					precioreg.Close
					
					If c2.GetString("HVD_CODPROMO") = Subs.traeAlmacen Then
						Private pup As String = c2.GetString("HVD_COSTO_TOT")/c2.GetString("HVD_CANT")
						
'						Starter.skmt.ExecNonQuery($"update HIST_VENTAS set HVD_RECHAZO = 1,  HVD_RECHAZOCANT = HVD_RECHAZOCANT - 1  WHERE HVD_PROID = '${esteTag.Get(2)}' and HVD_cliente =  '${esteTag.Get(5)}'"$)
						Log("algo1")
						Starter.skmt.ExecNonQuery2("INSERT INTO PEDIDO(PE_PROID,PE_PRONOMBRE,PE_CANT,PE_CLIENTEOR,PE_CLIENTE,PE_COSTOU,PE_COSTO_TOT,PE_CAJAS,PE_BCAJAS,CONSECUTIVO,PE_RUTA, PE_TIPO,PE_CEDIS,PE_FECHA,PE_USUARIO,FECHA_PREV,RUTA_REP,PE_REGALO) VALUES (?,?,?,?,?,?,?,0,0,?,?,?,?,?,?,?,?,?)", Array As String(esteTag.Get(2),c2.GetString("HVD_PRONOMBRE"),1*minimoadesc,esteTag.Get(5),Subs.traeCliente,(pup*minimoadesc),(pup*minimoadesc),esteTag.Get(6),Subs.traerUTAPREV,"PREVENTA",Subs.traeAlmacen, sDate & " " & sTime, Subs.traeUsuario,Subs.traefECHAPREV, Subs.traeRutaReparto,esregalo))
					Else
						Log("|" & preciofull& "|")
						Log("|" & minimoadesc& "|")
'						Log("minimoadesc")
						
						
						Log(esteTag.Get(2))
						Log(c2.GetString("HVD_PRONOMBRE"))
						Log((1*minimoadesc))
						Log(esteTag.Get(5))
						Log(Subs.traeCliente)
						Log((preciofull*minimoadesc))
						Log(esteTag.Get(6))
						Log(Subs.traerUTAPREV)
						Log(Subs.traeAlmacen)
						Log(sDate & " " & sTime)
						Log(Subs.traeUsuario)
						Log(Subs.traefECHAPREV)
						Log(Subs.traeRutaReparto)
						Log(esregalo)

'						Log(esteTag.Get(2)&"|"& c2.GetString("HVD_PRONOMBRE")&"|"& (1*minimoadesc) &"|"& esteTag.Get(5) &"|"& Subs.traeCliente &"|"& (preciofull*minimoadesc) &"|"& (preciofull*minimoadesc) &"|"& esteTag.Get(6) &"|"& Subs.traerUTAPREV &"|"& "PREVENTA" &"|"& Subs.traeAlmacen &"|"& sDate & " " & sTime &"|"& Subs.traeUsuario &"|"& Subs.traefECHAPREV &"|"& Subs.traeRutaReparto &"|"& esregalo)
						Starter.skmt.ExecNonQuery2("INSERT INTO PEDIDO(PE_PROID,PE_PRONOMBRE,PE_CANT,PE_CLIENTEOR,PE_CLIENTE,PE_COSTOU,PE_COSTO_TOT,PE_CAJAS,PE_BCAJAS,CONSECUTIVO,PE_RUTA, PE_TIPO,PE_CEDIS,PE_FECHA,PE_USUARIO,FECHA_PREV,RUTA_REP,PE_REGALO) VALUES (?,?,?,?,?,?,?,0,0,?,?,?,?,?,?,?,?,?)", Array As String(esteTag.Get(2),c2.GetString("HVD_PRONOMBRE"),1*minimoadesc,esteTag.Get(5),Subs.traeCliente,(preciofull*minimoadesc),(preciofull*minimoadesc),esteTag.Get(6),Subs.traerUTAPREV,"PREVENTA",Subs.traeAlmacen, sDate & " " & sTime, Subs.traeUsuario,Subs.traefECHAPREV, Subs.traeRutaReparto,esregalo))
					End If
					
				End If
			End If
		

			Subs.prodVenta(esteTag.Get(5), esteTag.Get(2))
'		Private cym As Map = Subs.traemosCantYMonto(clv_pedido)
'		L_CANT.Text = cym.Get("cantidad")
'		L_TOTAL.Text = Round2(cym.Get("monto"), 2)
'		cuentaProds("+")
			B4XPage_Appear
		End If
	End If
'	Dim preciou As Float = esteTag.Get(0)/esteTag.Get(1)
'	Log(preciou)
'	LogColor("estamos aquii mas " & laCant.Text& " , " & (preciou * laCant.Text) ,Colors.Green )
'	b_guardar.Visible = True
'	Log($"Total Prods: ${totalProds}, Total Compra: $$1.2{totalCompra}"$)
'	c = Starter.skmt.ExecQuery($"SELECT HVD_CANT FROM HIST_VENTAS2 WHERE HVD_CLIENTE IN (SELECT CUENTA FROM CUENTAA) AND HVD_PROID = '${esteTag.Get(2)}' "$)
'	c.Position = 0
'	Dim cantoriginal As String = 0
'	If c.RowCount > 0 Then
'		cantoriginal = c.GetString("HVD_CANT")
'	End If
'	If cantoriginal <= esteTag.Get(1)  Then
'		L_CANT.Text = L_CANT.Text + 1
'	End If
End Sub


Private Sub b_prodMasC_Click
	etCantHasFocus = False
'	Log("etCantHasFocus=" & etCantHasFocus)
	LogColor("b_prodMas_Click", Colors.Magenta)
	Dim index As Int = clv_pedido.GetItemFromView(Sender)
	Dim pnl0 As B4XView = clv_pedido.GetPanel(index)
	Dim pnl As B4XView = pnl0.GetView(0)
	Dim laCantc As B4XView = pnl.GetView(3).GetView(3)
	LogColor(laCantc.Text&"-----------------------------",Colors.Blue)
'	Log(pnl.GetView(2).GetView(1).text&"|"&pnl.GetView(2).GetView(2)&"|"&pnl.GetView(2).GetView(4).text)
'	Log($"precio|stock:${laCant.tag}"$)
'	Log($"Indice: ${index}, cant:${laCant.Text+1}, precioU: ${laCant.tag}"$)
	Dim esteTag As List = Regex.Split("\|", laCantc.Tag)
	Log("LC_TEXT:"&laCantc.Text&"|LC_TAG:"&laCantc.Tag&"|ET:"&esteTag)
	If laCantc.Text = "" Then laCantc.Text = 0
	
	Private maxProds As String = (esteTag.Get(1) + esteTag.Get(4))'Prods disponibles + prods comprados
	LogColor(esteTag.Get(1),Colors.Red)
	LogColor(esteTag.Get(4),Colors.Red)
	LogColor($"++++++++++++++++++++++++++   ${esteTag}"$, Colors.Green)
'	Log(esteTag.get(4))
	Log(maxProds)
	
'	l_pCantC.Tag = Round2(precioU,2)&"|"&inv&"|"&prodId&"|"&promo&"|"&inv2&"|"&cliente_original
	Log(esteTag.Get(4))
	If esteTag.Get(4) = "0" Then
		
	Else
		If laCantc.Text + 1 <= maxProds Then
			Log($"NuevaCant = ${laCantc.Text + 1}"$)
'			laCantc.Text = NumberFormat2(laCantc.Text + 1,0,0,0,False)
			laCantc.Text = $"$1.0{laCantc.Text + 1}"$
			LogColor(esteTag,Colors.Magenta)
		
			LogColor(laCantc.Text,Colors.Red)
'			Starter.skmt.ExecNonQuery($"update HIST_VENTAS set HVD_RECHAZO = 1,  HVD_RECHAZOCANT = IFNULL(HVD_RECHAZOCANT,0) -(1 * ${Subs.traeConversion(esteTag.Get(2))})  WHERE HVD_PROID = '${esteTag.Get(2)}' and HVD_cliente in (Select CUENTA from cuentaa)"$)
		
			c = Starter.skmt.ExecQuery2("SELECT * FROM PEDIDO WHERE PE_PROID = ? and PE_CLIENTE IN (SELECT CUENTA FROM CUENTAA) AND CONSECUTIVO = ? ", Array As String(esteTag.Get(2), esteTag.Get(6)))
			If c.RowCount > 0 Then
				Private c2 As Cursor = Starter.skmt.ExecQuery2("SELECT * FROM HIST_VENTAS WHERE HVD_PROID = ? AND CONSECUTIVO = ? ", Array As String(esteTag.Get(2), esteTag.Get(6)))
				If c2.RowCount > 0 Then
					c2.Position = 0
					Private puc As String = c2.GetString("HVD_COSTO_TOT")/c2.GetString("CANTC")
'					Starter.skmt.ExecNonQuery($"update HIST_VENTAS set HVD_RECHAZO = 1,  HVD_RECHAZOCANT =(IFNULL(HVD_RECHAZOCANT,0) -(1 * ${Subs.traeConversion(esteTag.Get(2))}) ) WHERE HVD_PROID = '${esteTag.Get(2)}' and HVD_cliente =  '${esteTag.Get(5)}'"$)
					Starter.skmt.ExecNonQuery($"update PEDIDO set PE_CANT = (IFNULL(PE_CANT,0)+(1 * '${Subs.traeConversion(esteTag.Get(2))}')), PE_COSTO_TOT= (IFNULL(PE_CAJAS,0)+1)*'${puc}', PE_CAJAS= IFNULL(PE_CAJAS,0) + 1 WHERE PE_PROID = '${esteTag.Get(2)}' and PE_CLIENTE in (Select CUENTA from cuentaa) AND CONSECUTIVO = '${esteTag.Get(6)}'"$)
					B4XPage_Appear
					Log("aqui2")
				End If
			Else
				Private c2 As Cursor = Starter.skmt.ExecQuery2("SELECT * FROM HIST_VENTAS WHERE HVD_PROID = ? AND CONSECUTIVO = ? ", Array As String(esteTag.Get(2), esteTag.Get(6)))
				If c2.RowCount > 0 Then
					c2.Position = 0
					Private puc As String = c2.GetString("HVD_COSTO_TOT")/c2.GetString("CANTC")
					Log(puc)
				
'				Starter.skmt.ExecNonQuery($"update HIST_VENTAS set HVD_RECHAZO = 1,  HVD_RECHAZOCANT = IFNULL(HVD_RECHAZOCANT,0) -(1 * ${Subs.traeConversion(esteTag.Get(2))})  WHERE HVD_PROID = '${esteTag.Get(2)}' and HVD_cliente =  '${esteTag.Get(5)}'"$)
					Starter.skmt.ExecNonQuery2("INSERT INTO PEDIDO(PE_PROID,PE_PRONOMBRE,PE_CANT,PE_CLIENTEOR,PE_CLIENTE,PE_COSTOU,PE_COSTO_TOT,PE_CAJAS,PE_BCAJAS,CONSECUTIVO,PE_RUTA, PE_TIPO,PE_CEDIS,PE_FECHA,PE_USUARIO,FECHA_PREV,RUTA_REP,PE_REGALO) VALUES (?,?,?,?,?,?,?,1,1,?,?,?,?,?,?,?,?,?)", Array As String(esteTag.Get(2),c2.GetString("HVD_PRONOMBRE"),Subs.traeConversion(esteTag.Get(2)),esteTag.Get(5),Subs.traeCliente,puc,puc,esteTag.Get(6),Subs.traerUTAPREV,"PREVENTA",Subs.traeAlmacen, sDate & " " & sTime, Subs.traeUsuario,Subs.traefECHAPREV, Subs.traeRutaReparto,0))
					B4XPage_Appear
				End If
			End If
		
		End If
	End If
End Sub

Private Sub b_prodMenosC_Click
	etCantHasFocus = False
	Log("etCantHasFocus=" & etCantHasFocus)
	LogColor("b_prodMenos_Click", Colors.Magenta)
	Dim index As Int = clv_pedido.GetItemFromView(Sender)
	Dim pnl0 As B4XView = clv_pedido.GetPanel(index)
	Dim pnl As B4XView = pnl0.GetView(0)
	Dim laCantc As B4XView = pnl.GetView(3).GetView(3)
'	Log(pnl.GetView(2).GetView(0) & "|" & pnl.GetView(2).GetView(1) & "|" & pnl.GetView(2).GetView(2))
	Dim esteTag As List = Regex.Split("\|", laCantc.Tag)
	Log("LC_TEXT:"&laCantc.Text&"|PROMO:"&esteTag.Get(3)&"|LC_TAG:"&laCantc.Tag&"|ET:"&esteTag)
	
	
	
	If laCantc.Text > 0 Then
		
'		Starter.skmt.ExecNonQuery($"update HIST_VENTAS set HVD_RECHAZO = 1,  HVD_RECHAZOCANT = (IFNULL(HVD_RECHAZOCANT,0) +(1 * ${Subs.traeConversion(esteTag.Get(2))}))  WHERE HVD_PROID = '${esteTag.Get(2)}' and HVD_cliente = '${esteTag.Get(5)}'"$)
		Private c3 As Cursor = Starter.skmt.ExecQuery2("SELECT PE_CAJAS FROM PEDIDO WHERE PE_PROID = ? AND PE_CLIENTE IN (SELECT CUENTA FROM CUENTAA) AND CONSECUTIVO = ? ", Array As String(esteTag.Get(2), esteTag.Get(6)))
		If c3.RowCount > 0 Then
			c3.Position = 0
			If c3.GetString("PE_CAJAS") > 1 Then
				Starter.skmt.ExecNonQuery($"update PEDIDO set PE_CANT = (IFNULL(PE_CANT,0)-(1 * '${Subs.traeConversion(esteTag.Get(2))}')), PE_COSTO_TOT= ((IFNULL(PE_CAJAS,0)-1) *'${Subs.traePrecioC(esteTag.Get(2))}'), PE_CAJAS = IFNULL(PE_CAJAS,0) - 1 WHERE PE_PROID = '${esteTag.Get(2)}' and PE_CLIENTE in (Select CUENTA from cuentaa) AND CONSECUTIVO = '${esteTag.Get(6)}'"$)
			Else If c3.GetString("PE_CAJAS") = 1 Then
				Starter.skmt.ExecNonQuery($"delete from PEDIDO where PE_PROID = '${esteTag.Get(2)}' and PE_CLIENTE in (Select CUENTA from cuentaa) AND CONSECUTIVO = '${esteTag.Get(6)}'"$)
			End If
		End If
		Log("entre al if o algo asi ")
		If laCantc.Text = "" Then laCantc.Text = 0
		laCantc.Text = laCantc.Text - 1
		If laCantc.Text < 0 Then laCantc.Text = 0
		B4XPage_Appear
'		Starter.skmt.ExecNonQuery2("update cat_gunaprod set cat_gp_almacen = (cat_gp_almacen + (1 * ?)) where cat_gp_id = ?", Array As Object(esteTag.Get(2), Subs.traeConversion(esteTag.Get(2))))
	Else If laCantc.Text = 0 Then
		
	End If
	
	Dim preciou As Float = esteTag.Get(0)/esteTag.Get(1)
	Log("PU: " & preciou)
End Sub

Private Sub l_pCantC_Click
	
End Sub

Private Sub b_revversar_Click
	Starter.skmt.ExecNonQuery($"update HIST_VENTAS set HVD_RECHAZO = 0,  HVD_RECHAZOCANT = 0  WHERE HVD_cliente in (Select CUENTA from cuentaa)"$)
	B4XPage_Appear
End Sub

Sub cuentaProds(accion As String)
	''	Log("Entramos a cuentaProds")
'	Log("*******************************************************")
'	Private cantRechazada As Int = 0
'	If Not(reconstruirPedido) Then
'		Log("***********  CUENTAPRODS - NO RECONSTRUIR")
'		prodsMap.Initialize
'		For i = 0 To clv_pedido.GetSize - 1
'			Private p0 As B4XView = clv_pedido.GetPanel(i)
'			Private p As B4XView = p0.GetView(0)
'			Private cant1 As B4XView = p.GetView(2).GetView(3)
'			If cant1.Text = "" Then cant1.Text = 0
	''			totalProds = totalProds + cant1.Text
'			Private esteTag As List = Regex.Split("\|", cant1.Tag)
'			Log("-------------------------------------")
'			Log($"ET: ${esteTag}"$)
'			Private esteProd As String = esteTag.Get(2)
'			Private estaCant As Int = cant1.Text
'			Private estaCantOriginal As Int = esteTag.Get(1)
'			Private esteCliente = Subs.traeCliente
'			Private esteProdNombre = Subs.traeNombre(esteProd)
'			Private esteClienteOriginal As Int = esteTag.Get(5)
'			Private fechaReparto As String = Subs.traeFechaReparto
'			Private maxProds = Subs.traeMaxCantidad(esteTag.Get(5), esteTag.Get(2))
'			
'			If cant1.Text > maxProds Then cant1.Text = maxProds
'			
'			cantRechazada = esteTag.Get(4) - cant1.Text
	''			If accion = "-" Then cantRechazada = Subs.traeCantidadRechazada(esteClienteOriginal, esteProd) + 1
	''			If accion = "+" Then cantRechazada = Subs.traeCantidadRechazada(esteClienteOriginal, esteProd) - 1
'			Log($"${Subs.traeCantidadRechazada(esteClienteOriginal, esteProd)}, ${Subs.traeCantidadVendida(esteClienteOriginal, esteProd)}"$)
'			
	''			LogColor($"cantRechazada = ${esteTag.Get(4)} - ${cant1.Text}"$, Colors.Magenta)
'			LogColor($"cantRechazada = ${cantRechazada}"$, Colors.Magenta)
'			Private estePrecio As String = 0
'			If Subs.traePrecio(esteProd, 1) <> Null Then estePrecio = Subs.traePrecio(esteProd, 1) * cantRechazada
'			
	''			LogColor(esteProd & "|" & cant1.Text & "|" & estaCantOriginal & "|" & cantRechazada & "|" & estePrecio, Colors.red)
	''			LogColor($"cantRechazada=${cantRechazada} | ${esteProdNombre}"$, Colors.Blue)
	''			Log($"Ponemos HVD_CANT ${esteProd} en ${cant1.text}"$)
'			Starter.skmt.ExecNonQuery2("update HIST_VENTAS set HVD_PARCIAL = 1, HVD_CANT = ? WHERE HVD_PROID = ? and HVD_CLIENTE in (Select CUENTA from cuentaa)", Array As String(cant1.Text ,esteProd))
'			Private rr As Cursor = Starter.skmt.ExecQuery($"select count(REP_CLIENTE) as hayRechazo from REPARTO where rep_prodid = '${esteProd}' and REP_CLIENTE in (Select CUENTA from cuentaa)"$)
'			rr.Position = 0
	''			Log($"HayRechazo=${rr.GetString("hayRechazo")}"$)
'			If rr.GetString("hayRechazo") = 0 Then
'				Log("INSERTAMOS EN REPARTO")
'				Starter.skmt.ExecNonQuery2("insert into reparto(REP_CLIENTE, REP_PRONOMBRE, REP_CANT, REP_COSTO_TOT, REP_FECHA, REP_RECHAZO, REP_PRODID, REP_PRECIO, REP_CLI_ORIG)  VALUES (?,?,?,?,?,1,?,?,?) ", Array As String(esteCliente, esteProdNombre, 0, estePrecio, fechaReparto, esteProd, estePrecio, esteCliente))
'				Starter.skmt.ExecNonQuery2("insert into reparto(REP_CLIENTE, REP_PRONOMBRE, REP_CANT, REP_COSTO_TOT, REP_FECHA, REP_RECHAZO, REP_PRODID, REP_PRECIO, REP_CLI_ORIG)  VALUES (?,?,?,?,?,0,?,?,?) ", Array As String(esteCliente, esteProdNombre, cantRechazada, 0, fechaReparto, esteProd, estePrecio, esteCliente))
'			Else if esteClienteOriginal = "0" Then 'Actualizamos el rechazo en el cliente actual.
	''				Log($"Actualizamos REP_CANT=${cantRechazada}"$)
	''				Starter.skmt.ExecNonQuery($"update reparto set REP_CANT = '${cantRechazada}', REP_PRECIO = '${estePrecio}', REP_COSTO_TOT = '${estePrecio * cantRechazada}' where REP_CLIENTE in (Select CUENTA from cuentaa) and REP_CLI_ORIG = '${esteClienteOriginal}' and REP_RECHAZO = 1 and REP_PRODID = '${esteProd}'"$)
'			Else 'Cliente original <> 0 entonces actualizamos el rechazo en el cliente original y NO en el actual.
'				Log($"Actualizamos REP_CANT RECHAZO = ${cantRechazada}"$)
	''				Starter.skmt.ExecNonQuery($"update reparto set REP_CANT = ${cantRechazada}, REP_PRECIO = '${estePrecio}', REP_COSTO_TOT = '${estePrecio * cantRechazada}' where REP_CLIENTE = '${esteClienteOriginal}' and REP_CLI_ORIG = '${esteClienteOriginal}' and REP_RECHAZO = 1 and REP_PRODID = '${esteProd}'"$)
	''				Starter.skmt.ExecNonQuery($"update reparto set REP_CANT = '${cant1.Text}', REP_PRECIO = '${estePrecio}', REP_COSTO_TOT = '${estePrecio * cantRechazada}' where REP_CLIENTE in (Select CUENTA from cuentaa) and REP_CLI_ORIG = '${esteClienteOriginal}' and REP_RECHAZO = 0 and REP_PRODID = '${esteProd}'"$)
'			End If
'			
'			If cant1.Text > 0 Then
	''				totalCompra = totalCompra + (esteTag.get(0) * cant1.text)
	''				Log($"Cant: ${cant1.Text}, Suma: ${totalCompra}"$)
'				Private m As Map
'				m=CreateMap("cant":cant1.Text, "cant2":esteTag.get(4), "precio":esteTag.get(0), "promo":esteTag.get(3), "nombre":Subs.traeNombre(esteTag.Get(2)), "cli_orig":esteTag.get(5))
	''				LogColor("PUT:" & esteTag.Get(2), Colors.Red)
'				prodsMap.Put(esteTag.Get(2), m)
'			End If
'			Log("-------------------------------------")
'		Next
'	End If
'	Private bgColor, textColor As Int
	'
'	If reconstruirPedido Then
'		Log("***********  CUENTAPRODS - SI RECONSTRUIR")
'		Private newPromo, newPrecio As String
'		clv_pedido.Clear
'		Log("Usamos PRODSMAP")
'		For Each pr As String In prodsMap.Keys
'			Private pr1 As Map = prodsMap.Get(pr)
'			Log("PPP: " & pr & "|PromoABorrar=" & promoABorrar & "|" & pr1)
'			bgColor = Colors.RGB(177, 200, 249)'azul
'			textColor = Colors.black
'			newPromo = pr1.Get("promo")
'			newPrecio = pr1.Get("precio")
'			Log("AAA: " & newPromo & "|" & promoABorrar)
'			If newPromo = promoABorrar Then
'				newPromo = 1
'				newPrecio = Subs.traePrecio(pr, False) * pr1.Get("cant")
'				'Ponemos precio SIN promo y sacamos el producto de la promo.
'				Starter.skmt.ExecNonQuery($"update HIST_VENTAS set HVD_COSTO_TOT = ${newPrecio}, HVD_CODPROMO ='${Subs.traeAlmacen}' WHERE HVD_PRONOMBRE = '${pr1.Get("nombre")}' and HVD_cliente in (Select CUENTA from cuentaa)"$)
'				LogColor("CAMBIAMOS PROMO: " & newPromo, Colors.Magenta)
'			End If
'			If newPromo <> "1" Then
	''				bgColor = Colors.RGB(255, 212, 163) 'naranja
'				bgColor = Colors.White
'			End If
'			Log($"CLI: ${pr1.Get("nombre")}, ${newPrecio}, ${pr1.Get("cant")}, ${pr1.Get("cant2")}"$)
'			clv_pedido.Add(CreateListItem(pr1.Get("nombre"), newPrecio, pr1.Get("cant"), pr1.Get("cant2"), clv_pedido.AsView.Width, 118dip, bmp, pr, bgColor, textColor, newPromo, pr1.Get("cli_orig")), pr1.Get("nombre"))
'			reconstruirPedido = False
'		Next
'		promoABorrar = ""
'	End If
'	Log($"Total Prods: ${totalProds}, Total Compra: $$1.2{totalCompra}"$)
'	LogColor("prodsMap=" & prodsMap, Colors.Green)
'	
	'
'	LogColor(prodsMap, Colors.Blue)
'	c = Starter.skmt.ExecQuery($"SELECT sum(HVD_CANT) as CANTIDAD FROM HIST_VENTAS WHERE HVD_CLIENTE IN (SELECT CUENTA FROM CUENTAA)"$)
'	Dim cantidad1 As String = 0
'	If c.RowCount > 0 Then
'		c.Position = 0
'		cantidad1 = c.GetString("CANTIDAD")
'		L_CANT.Text = Round(cantidad1)
'	End If
'	c.Close
'	
'	Dim totalfinal As String = 0
'	For Each pr As String In prodsMap.Keys
'		Private pr1 As Map = prodsMap.Get(pr)
	''		LogColor(pr,Colors.Red)
'		Dim x As Cursor = Starter.skmt.ExecQuery($"SELECT CAT_GP_PRECIO FROM CAT_GUNAPROD WHERE CAT_GP_ID = '${pr}'"$)
'		c = Starter.skmt.ExecQuery($"SELECT HVD_CANT FROM HIST_VENTAS WHERE HVD_CLIENTE IN (SELECT CUENTA FROM CUENTAA) AND HVD_PROID ='${pr}' "$)
'		Dim cantidady As String = 0
'		If c.RowCount >0 Then
'			c.Position = 0
'			cantidady = c.GetString("HVD_CANT")
'		End If
	''		Log(x.RowCount)
'		If x.RowCount > 0 Then
'			x.Position = 0
	''			Log(x.GetString("CAT_GP_PRECIO"))
'			Dim preciou As String = x.GetString("CAT_GP_PRECIO")
'			Dim costototalhist  As String = preciou * cantidady
	''			Log("Total ==== "&costototalhist)
'		End If
'		totalfinal = totalfinal + costototalhist
'		x.Close
'	Next
'	L_TOTAL.Text = Round2(totalfinal, 2)
'	c.Close
'	
'	c = Starter.skmt.ExecQuery("select COUNT(*) AS CUANTOS from REPARTO where REP_CLIENTE in (select cuenta from cuentaa) and REP_RECHAZO = '0' and REP_CANT > 0 order by REP_PRONOMBRE")
'	If c.RowCount > 0 Then
'		c.Position = 0
'		If c.GetString("CUANTOS") > 0 Then
'			
'			Dim z As Cursor = Starter.skmt.ExecQuery($"select SUM(REP_CANT) AS CANTIDAD , SUM(REP_PRECIO) AS PRECIO from REPARTO where REP_CLIENTE in (select cuenta from cuentaa) and REP_RECHAZO = '0' and REP_CANT > 0 order by REP_PRONOMBRE"$)
'			Dim cantidad2 As String = 0
	''			Log(z.RowCount)
'			If z.RowCount > 0 Then
'				z.Position = 0
'				cantidad2= z.GetString("CANTIDAD")
	''				LogColor(z.GetString("PRECIO")&" , " & z.GetString("CANTIDAD"), Colors.Magenta)
	''				Log(cantidad1 &" , "& cantidad2)
'				L_CANT.Text = Round((cantidad1 + cantidad2))
'			End If
'			z.Close
'			
'			
'		End If
'	End If
'	
'	
'	
End Sub

Private Sub b_guardar_Click
	
End Sub


Private Sub l_pCant_Click
	
End Sub

Sub p_prods_Click
	Log(Sender.As(Panel).tag)
End Sub