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
	Private l_promoDesc As Label
	Private lv_prodsFijos As ListView
	Private clv_prodsVariabes, clv_prodsVariabes2 As CustomListView
	Private l_prodsFijos As Label
	Private l_prodsVariables, l_prodsVariables2 As Label
	Private i_prod As ImageView
	Private l_prodX As Label
	Private p_prods As Panel
	Private b_prodMenos, b_prodMenos2 As Button
	Private et_pCant As EditText
	Private b_prodMas, b_prodMas2 As Button
	Private l_pCant As Label
	Dim totalProds As Int = 0
	Dim prodsVar1 As Int = 0
	Dim prodsVar2 As Int = 0
	Dim prodsVarReq As Int = 0
	Dim prodsVarReq_ As Int = 0
	Dim prodsVarReq2 As Int = 0
	Dim prodsVarReq2_ As Int = 0
	Dim totalCompra As Float = 0
	Dim prodsFijosTot As Float = 0
	Dim tpf As Int = 0
	Dim tpf2 As Int = 0
	Dim maxCantPromos As Int = 1
	Private l_totProds As Label
	Private l_total As Label
	Private b_terminar1 As Button
	Private p_promociones As Panel
	Dim prodsIds, prodsCants, prodsPrecios, prodsIds2, prodsCants2, prodsPrecios2 As List
	Dim estaPromo, esteCliente As String
	Private b_promoMas As Button
	Private b_promoMenos As Button
	Private et_promoCant As EditText
	Private l_promosCant As Label
	Private b_continuar As Button
	Private p_prodsVariables, p_prodsVariables2 As Panel
	Dim laPromo = "", elCliente = "" As String
	Dim prodsPedidoActual As String
	Dim montoPedidoActual As String
	Dim promosMap As Map
	Dim su As StringUtils
End Sub

'You can add more parameters here.
Public Sub Initialize As Object
	Return Me
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
	Root = Root1
	'load the layout to Root
	Root.LoadLayout("promociones")
	
	p_promociones.Height= Root.Height
	p_promociones.Width= Root.Width

	promosMap.Initialize
	p_promociones.Top=0
	p_promociones.Left=0
	b_terminar1.Left = Root.Width - (b_terminar1.Width + 10)
	b_continuar.Left = Root.Width - b_terminar1.Width - (b_continuar.Width + 20)
End Sub

Sub B4XPage_Appear
'	LogColor(B4XPages.MainPage.productos.prodsMap, Colors.blue)
	prodsIds.Initialize
	prodsCants.Initialize
	prodsPrecios.Initialize
	prodsIds2.Initialize
	prodsCants2.Initialize
	prodsPrecios2.Initialize
	Dim m As Map = Subs.traeTotalesClienteActual
	prodsPedidoActual = m.Get("productos")
	montoPedidoActual = m.Get("monto")
'	Log($"laPromo=${laPromo}, el cliente=${elCliente}"$)
	If laPromo <> "" Then muestraPromo(laPromo, elCliente)
End Sub
'You can see the list of page related events in the B4XPagesManager object. The event name is B4XPage.

Sub muestraPromo(promo As String, cliente As String)
	Private thisLog As Boolean = False
'	If thisLog Then Log(promo)
	estaPromo = promo
	esteCliente = cliente
	Private mp As Map = Subs.traePromo(promo, cliente)
	Private prodsPromo As Map = mp.Get("productos") 'Los productos de la promoción.
	Private invDispPromo As Map = Subs.traemosInventarioDisponibleParaPromo(promo)
'	If thisLog Then Log($"invDispPromo=${invDispPromo}"$)
	If thisLog Then Log("Max promos por inv de prods FIJOS: " & Subs.revisaMaxPromosProdsFijosPorInventario(mp))
	If thisLog Then Log("Max promos por inv de prods VARIABLES: " & Subs.revisaMaxPromosProdsVariablesPorInventario(mp))
	maxCantPromos = Subs.revisaMaxPromosProdsVariablesPorInventario(mp)
	et_promoCant.Text = 1
	l_promosCant.text = "Max promos: " & maxCantPromos
	Private cs As CSBuilder
	cs.Initialize
	If thisLog Then Log(mp)
	If thisLog Then Log(prodsPromo)
	Private listaPV As List = mp.Get("prodsVariables")
	Private listaPreciosPV As List = mp.Get("prodsVariablesPrecios")
	Private listaPV2 As List = mp.Get("prodsVariables2")
	Private listaPreciosPV2 As List = mp.Get("prodsVariablesPrecios2")
'	Log("============  " & CRLF & listaPV)
	'Ponemos la promo y descripción.
'	Dim desc As String = $"Promoción: ${promo}${CRLF}${mp.Get("descripcion")}"$
'	Dim desc As String = cs.append("Promocion: ").Color(Colors.RGB(100,149,237)).Append(promo).pop.append(CRLF).Append(mp.Get("descripcion")).Popall
	l_promoDesc.Text = cs.Color(Colors.RGB(100,149,237)).append("Promocion: ").pop.append(CRLF).Append(promo).append(CRLF).Append(mp.Get("descripcion")).Popall
	prodsFijosTot = 0
	'Ponemos el texto en las etiquetas de la cantidad de productos.
'	Dim pf As List = mp.Get("prodsFijos")
	Dim pv As List = mp.Get("prodsVariables")
	Dim pv2 As List = mp.Get("prodsVariables2")
	If thisLog Then Log(mp)
	tpf = 0
	For Each pfp As Int In mp.Get("prodsFijosPiezas").As(List)
		tpf = tpf + pfp
	Next
	tpf2 = tpf
'	tpf = tpf * maxCantPromos
	l_prodsFijos.Text = $"Productos fijos (${tpf})"$
	prodsVarReq = mp.Get("prodsVariablesRequeridos")
	prodsVarReq_ = prodsVarReq
	prodsVarReq2 = mp.Get("prodsVariables2Requeridos")
	prodsVarReq2_ = prodsVarReq2
'	Log("=========  " & prodsVarReq2)
	l_prodsVariables.Text = $"Productos variables requeridos: ${prodsVarReq}"$
	l_prodsVariables2.Text = $"Productos variables2 requeridos: ${prodsVarReq2}"$
	If prodsVarReq = 0 Then
		p_prodsVariables.Visible = False
	Else
		p_prodsVariables.Visible = True
	End If
	'Llenamos el listview con los productos fijos.
	Dim label1 As Label
	label1 = lv_prodsFijos.SingleLineLayout.Label
	label1.TextSize = 13
	label1.TextColor = Colors.black
	lv_prodsFijos.SingleLineLayout.ItemHeight = 30dip
	lv_prodsFijos.Clear
'	If pf.Size < 1 Then pf.Add("Sin productos fijos.")
	lv_prodsFijos.Clear
	agregaFijosALista(mp, True)
	If thisLog Then Log("Total prods fijos = " & prodsFijosTot)
'	If thisLog Then Log("======="&prodsMap)
	l_totProds.text = $"Productos: ${tpf}"$
	l_total.Text = $"Total: $$1.2{prodsFijosTot}"$
'	Dim ins As InputStream
'	Dim bmp As Bitmap
'	Dim jpeg() As Byte

	'Llenamos el listview con los productos variables.
	If thisLog Then Log($"PV1: ${pv.Size}, ${pv}"$)
	If thisLog Then Log($"PV2: ${pv2.Size}, ${pv2}"$)
	If thisLog Then Log("invDisp=" & invDispPromo)
	clv_prodsVariabes.Clear
	clv_prodsVariabes2.Clear
	For Each p As String In pv
		If invDispPromo.ContainsKey(p) Then 'Si no esta en la lista de productos con inventario no lo mostramos.
			Private indicePV = listaPV.IndexOf(p)
'			Log(p & "|" & indicePV & "|" & listaPreciosPV.Get(indicePV))
			Private estePrecio = listaPreciosPV.Get(indicePV)
'			c2.Position=i
'			jpeg = c2.GetBlob("CAT_GP_IMG")
'			ins.InitializeFromBytesArray(jpeg, 0, jpeg.Length)
'			bmp.Initialize2(ins)
			Private thisProd As Map = prodsPromo.Get(p)
			If thisLog Then Log(thisProd)
			clv_prodsVariabes.Add(CreateListItem(Subs.traeProdNombre(p)&CRLF&"Hay " & invDispPromo.Get(p) & " $" & estePrecio, estePrecio, invDispPromo.Get(p), clv_prodsVariabes.AsView.Width, 50dip, Null, p), p)
		End If
	Next
	For Each p As String In pv2
		If invDispPromo.ContainsKey(p) Then 'Si no esta en la lista de productos con inventario no lo mostramos.
'			Log(p)
'			Log($"${invDispPromo}$"$)
'			Log($"${listaPV2}"$)
'			Log("listaPrecios:"&listaPreciosPV2)
			Private indicePV2 = listaPV2.IndexOf(p)
'			Log($"${indicePV2}"$)
'			Log(p & "|" & indicePV & "|" & listaPreciosPV2.Get(indicePV2))
			Private estePrecio = listaPreciosPV2.Get(indicePV2)
'		c2.Position=i
'		jpeg = c2.GetBlob("CAT_GP_IMG")
'		ins.InitializeFromBytesArray(jpeg, 0, jpeg.Length)
'		bmp.Initialize2(ins)
			Private thisProd As Map = prodsPromo.Get(p)
			If thisLog Then Log(thisProd)
			clv_prodsVariabes2.Add(CreateListItem2(Subs.traeProdNombre(p)&CRLF&"Hay " & invDispPromo.Get(p) & " $" & estePrecio, estePrecio, invDispPromo.Get(p), clv_prodsVariabes2.AsView.Width, 50dip, Null, p), p)
		End If
	Next
	If pv2.Size = 0 Then
'		Log("PV2 = 0")
		l_prodsVariables2.Visible = False
		p_prodsVariables2.Visible = False
	
		p_prodsVariables.Height = 410dip
		clv_prodsVariabes.GetBase.Height = p_prodsVariables.Height - 20
'		p_prodsVariables.Height = 380dip
'		clv_prodsVariabes.GetBase.Height = 380dip 'Cambiamos el tamaño y posición de la lista de productos
''		clv_prodsVariabes.GetBase.Width = 
		clv_prodsVariabes.Base_Resize(clv_prodsVariabes.GetBase.Width, p_prodsVariables.Height - 20) 'Cambiamos el tamaño del panel interno de la lista para que ajuste al nuevo tamaño.
	Else
		l_prodsVariables2.Visible = True
		p_prodsVariables2.Visible = True
		p_prodsVariables.Height = 210dip
		clv_prodsVariabes.GetBase.Height = p_prodsVariables.Height - 20
	End If
	b_terminar1.Visible = False
	b_continuar.Visible = False
	cuentaProds
End Sub

'Agregamos los productos fijos a una lista para despues meterlos en el pedido.
Sub agregaFijosALista(mp As Map, addListItem As Boolean)
	Private thisLog As Boolean = False
	Private cont As Int = 0
	prodsFijosTot = 0
	Dim pf As List = mp.Get("prodsFijos")
	If pf.Size < 1 Then pf.Add("Sin productos fijos.")
'	Private m As Map
'	prodsMap.Put(estaPromo, CreateMap("cant":1, "precio":0))
'	prodsIds.Add(estaPromo)
	prodsIds.clear
	prodsCants.clear
	prodsPrecios.clear
	prodsIds.Add(estaPromo)
	prodsCants.Add(et_promoCant.text.As(Int))
	prodsPrecios.Add(0)
	For Each p As String In pf
'		Log($"cont=${cont}, mp=${mp}"$)
		If mp.Get("prodsFijosPiezas").As(List).Size > 0 Then
			Private tpi As String = mp.Get("prodsFijosPiezas").As(List).Get(cont)
		Else
			Private tpi As String = 0
		End If
		If mp.Get("prodsFijosPrecios").As(List).Size > 0 Then
			Private tpr As String = mp.Get("prodsFijosPrecios").As(List).Get(cont)
		Else
			Private tpr As String = 0
		End If
		prodsFijosTot = prodsFijosTot + (tpi * tpr)
		If thisLog Then Log("|"&tpi&"|"&p)
		Private ntpi As Int = tpi * et_promoCant.text
		If addListItem And tpi > 0 Then lv_prodsFijos.AddSingleLine(ntpi & " " & Subs.traeProdNombre(p) & " - $" & tpr)
		cont = cont+1
'		m=CreateMap("cant":tpi, "precio":tpr)
'		prodsMap.Put(p, m)
		prodsIds.Add(p)
		prodsCants.Add(tpi.As(Int)*et_promoCant.text.As(Int))
		prodsPrecios.Add(tpr) '*et_promoCant.text.As(Int)
	Next
End Sub

Sub CreateListItem(Text As String, precioU As String, inv As Int, Width As Int, Height As Int, img As Bitmap, prodId As String) As Panel 'ignore
	Dim p As B4XView = xui.CreatePanel("")
	p.SetLayoutAnimated(0, 0, 0, Width, Height)
	p.LoadLayout("prodItem")
	l_prodX.TextSize = 15
	l_prodX.Text = Text
	l_prodX.TextSize = 15
'	p_prods.Height = Height + 70
'	l_prodX.Height = Height
	p_prods.height = su.MeasureMultilineTextHeight(l_prodX, l_prodX.Text) + 20 'Calculamos la altura del panel de acuerdo al texto contenido.
	If p_prods.Height < Height Then p_prods.Height = Height
	l_prodX.Height = p_prods.Height
	l_pCant.Tag = precioU&"|"&inv&"|"&prodId
	et_pCant.Tag = precioU&"|"&inv&"|"&prodId
	et_pCant.BringToFront
'	i_prod.Bitmap = img
	p.Height = p_prods.Height
	Return p
End Sub

Sub CreateListItem2(Text As String, precioU As String, inv As Int, Width As Int, Height As Int, img As Bitmap, prodId As String) As Panel 'ignore
	Dim p As B4XView = xui.CreatePanel("")
	p.SetLayoutAnimated(0, 0, 0, Width, Height)
	p.LoadLayout("prodItem2")
	l_prodX.TextSize = 15
	l_prodX.Text = Text
	l_prodX.TextSize = 15
'	p_prods.Height = Height
'	l_prodX.Height = Height
	p_prods.height = su.MeasureMultilineTextHeight(l_prodX, l_prodX.Text) + 20 'Calculamos la altura del panel de acuerdo al texto contenido.
	If p_prods.Height < Height Then p_prods.Height = Height
	l_prodX.Height = p_prods.Height
'	l_pCant.Text = 0
	l_pCant.Tag = precioU&"|"&inv&"|"&prodId
	et_pCant.Tag = precioU&"|"&inv&"|"&prodId
	et_pCant.BringToFront
'	i_prod.Bitmap = img
	p.Height = p_prods.Height
	Return p
End Sub

Sub b_prodMenos_Click
	Dim index As Int = clv_prodsVariabes.GetItemFromView(Sender)
	Dim pnl0 As B4XView = clv_prodsVariabes.GetPanel(index)
	Dim pnl As B4XView = pnl0.GetView(0)
'	Dim laCant  As B4XView = pnl.GetView(5)
	Dim laCant As B4XView = pnl.GetView(2).GetView(4)
'	Log($"precio|stock:${laCant.tag}"$)
	laCant.Text = $"$1.0{laCant.Text-1}"$
	If laCant.Text < 0 Then laCant.Text = 0
	
'	Log(Regex.Split("\|", laCant.tag)(0))
	Private precio As String = Regex.Split("\|", laCant.tag)(0)
'	Private inv As String = Regex.Split("\|", laCant.tag)(1)
	Private prodId As String = Regex.Split("\|", laCant.tag)(2)
	Private tmpMap As Map = CreateMap("precio":precio, "cant":laCant.Text, "almacen":estaPromo)
'	Log(tmpMap)
	promosMap.Put(prodId, tmpMap)
	If laCant.Text = 0 Then promosMap.Remove(prodId)
'	LogColor(promosMap, Colors.Magenta)
	cuentaProds
'	l_total.Text = $"Total Compra: $$1.2{totalCompra}"$
'	l_totProds.text = $"Total Productos: ${totalProds}"$
End Sub

Sub b_prodMas_Click
	If prodsVar1 < prodsVarReq Then
		Dim index As Int = clv_prodsVariabes.GetItemFromView(Sender)
		Dim pnl0 As B4XView = clv_prodsVariabes.GetPanel(index)
		Dim pnl As B4XView = pnl0.GetView(0)
'		Dim laCant  As B4XView = pnl.GetView(5)
		Dim laCant As B4XView = pnl.GetView(2).GetView(4)
'		Log($"precio|stock:${laCant.tag}"$)
'		Log($"Indice: ${index}, cant:${laCant.Text+1}, precioU: ${laCant.tag}"$)
		Dim esteTag As List = Regex.Split("\|", laCant.Tag)
'		Log(esteTag)
		If laCant.Text + 1 <= esteTag.get(1) Then
			laCant.Text = $"$1.0{laCant.Text+1}"$
			cuentaProds
		End If

'		Log(Regex.Split("\|", laCant.tag)(0))
		Private precio As String = Regex.Split("\|", laCant.tag)(0)
'		Private inv As String = Regex.Split("\|", laCant.tag)(1)
		Private prodId As String = Regex.Split("\|", laCant.tag)(2)
		Private tmpMap As Map = CreateMap("precio":precio, "cant":laCant.Text, "almacen":estaPromo)
'		Log(tmpMap)
		promosMap.Put(prodId, tmpMap)
'		LogColor(promosMap, Colors.Magenta)
	End If
'	Log($"Total Prods: ${totalProds}, Total Compra: $$1.2{totalCompra}"$)
'	l_total.Text = $"Total Compra: $$1.2{totalCompra}"$
'	l_totProds.text = $"Total Productos: ${totalProds}"$
End Sub


Sub b_prodMenos2_Click
	Dim index As Int = clv_prodsVariabes2.GetItemFromView(Sender)
	Dim pnl0 As B4XView = clv_prodsVariabes2.GetPanel(index)
	Dim pnl As B4XView = pnl0.GetView(0)
'	Dim laCant  As B4XView = pnl.GetView(5)
	Dim laCant As B4XView = pnl.GetView(2).GetView(4)
'	Log($"precio|stock:${laCant.tag}"$)
	laCant.Text = $"$1.0{laCant.Text-1}"$
	If laCant.Text < 0 Then laCant.Text = 0
	
'	Log(Regex.Split("\|", laCant.tag)(0))
	Private precio As String = Regex.Split("\|", laCant.tag)(0)
'	Private inv As String = Regex.Split("\|", laCant.tag)(1)
	Private prodId As String = Regex.Split("\|", laCant.tag)(2)
	Private tmpMap As Map = CreateMap("precio":precio, "cant":laCant.Text, "almacen":estaPromo)
'	Log(tmpMap)
	promosMap.Put(prodId, tmpMap)
	If laCant.Text = 0 Then promosMap.Remove(prodId)
'	LogColor(promosMap, Colors.Magenta)
	cuentaProds
'	l_total.Text = $"Total Compra: $$1.2{totalCompra}"$
'	l_totProds.text = $"Total Productos: ${totalProds}"$
End Sub

Sub b_prodMas2_Click
'	Log(prodsVarReq & "|" & prodsVarReq2 & "|" & tpf & "|" & prodsVar2)
	If prodsVar2 < prodsVarReq2 Then
		Dim index As Int = clv_prodsVariabes2.GetItemFromView(Sender)
		Dim pnl0 As B4XView = clv_prodsVariabes2.GetPanel(index)
		Dim pnl As B4XView = pnl0.GetView(0)
'		Dim laCant  As B4XView = pnl.GetView(5)
		Dim laCant As B4XView = pnl.GetView(2).GetView(4)
'		Log($"precio|stock:${laCant.tag}"$)
'		Log($"Indice: ${index}, cant:${laCant.Text+1}, precioU: ${laCant.tag}"$)
		Dim esteTag As List = Regex.Split("\|", laCant.Tag)
'		Log(esteTag)
		If laCant.Text + 1 <= esteTag.get(1) Then
			laCant.Text = $"$1.0{laCant.Text+1}"$
			cuentaProds
		End If

'		Log(Regex.Split("\|", laCant.tag)(0))
		Private precio As String = Regex.Split("\|", laCant.tag)(0)
'		Private inv As String = Regex.Split("\|", laCant.tag)(1)
		Private prodId As String = Regex.Split("\|", laCant.tag)(2)
		Private tmpMap As Map = CreateMap("precio":precio, "cant":laCant.Text, "almacen":estaPromo)
'		Log(tmpMap)
		promosMap.Put(prodId, tmpMap)
'		LogColor(promosMap, Colors.Magenta)
	End If
'	Log($"Total Prods: ${totalProds}, Total Compra: $$1.2{totalCompra}"$)
'	l_total.Text = $"Total Compra: $$1.2{totalCompra}"$
'	l_totProds.text = $"Total Productos: ${totalProds}"$
End Sub


Private Sub et_pCant_TextChanged (Old As String, New As String)
'	LogColor("txt changed",Colors.Magenta)
	cuentaProds
End Sub

Sub cuentaProds
'	Log("=====================================================")
'	Log("Inicia cuentaProds")
	Private thisLog As Boolean = False
'	LogColor($"ProdsFijos=${tpf}, totalProds=${totalProds}, totalCompra=${totalCompra}"$, Colors.Red)
	prodsIds2.Clear
	prodsCants2.Clear
	prodsPrecios2.Clear
	lv_prodsFijos.Clear
	prodsVar1 = 0
	prodsVar2 = 0
	agregaFijosALista(Subs.traePromo(estaPromo, esteCliente), True)
'	Log(prodsIds)
	Private tcpf As Float = 0 'Total de compra de productos fijos.
	For pf = 0 To prodsIds.Size - 1
		If thisLog Then Log($"${tcpf} + ${prodsCants.Get(pf)} * ${prodsPrecios.Get(pf)} = ${(prodsPrecios.Get(pf) * prodsCants.Get(pf))}"$)
		tcpf = tcpf + (prodsPrecios.Get(pf) * prodsCants.Get(pf))
	Next
'	Log("Total compra prodsFijos = " & tcpf)
	totalCompra = tcpf
	totalProds = tpf 'Cantidad total de productos fijos.
	'Variables1
	For i = 0 To clv_prodsVariabes.GetSize - 1
		Private p0 As B4XView = clv_prodsVariabes.GetPanel(i)
		Private p As B4XView = p0.GetView(0)
		Private cant1 As B4XView = p.GetView(2).GetView(4)
		If cant1.Text = "" Then cant1.Text = 0
		totalProds = totalProds + cant1.Text
		prodsVar1 = prodsVar1 + cant1.Text
		Private esteTag As List = Regex.Split("\|", cant1.Tag)
		If cant1.Text > esteTag.Get(1) Then cant1.Text = esteTag.Get(1)
		If cant1.Text > 0 Then
'			Log(esteTag.Get(2) & "|" & cant1.Text)
			totalCompra = totalCompra + (esteTag.get(0) * cant1.text)
			If thisLog Then Log($"Cant: ${cant1.Text}, Suma: ${totalCompra}"$)
			prodsIds2.Add(esteTag.Get(2))
			prodsCants2.Add(cant1.Text)
			prodsPrecios2.Add(esteTag.get(0))
		End If
		l_total.Text = $"Total: $$1.2{totalCompra}"$
		l_totProds.text = $"Productos: ${totalProds}"$
	Next
	'Variables2
	For i = 0 To clv_prodsVariabes2.GetSize - 1
		Private p0 As B4XView = clv_prodsVariabes2.GetPanel(i)
		Private p As B4XView = p0.GetView(0)
		Private cant2 As B4XView = p.GetView(2).GetView(4)
		totalProds = totalProds + cant2.Text
		prodsVar2 = prodsVar2 + cant2.Text
		Private esteTag As List = Regex.Split("\|", cant2.Tag)
		If cant2.Text > esteTag.Get(1) Then cant2.Text = esteTag.Get(1)
		If cant2.Text > 0 Then
'			Log(esteTag.Get(2) & "|" & cant2.Text)
			totalCompra = totalCompra + (esteTag.get(0) * cant2.text)
			If thisLog Then Log($"Cant: ${cant2.Text}, Suma: ${totalCompra}"$)
			prodsIds2.Add(esteTag.Get(2))
			prodsCants2.Add(cant2.Text)
			prodsPrecios2.Add(esteTag.get(0))
		End If
		l_total.Text = $"Total: $$1.2{totalCompra}"$
		l_totProds.text = $"Productos: ${totalProds}"$
	Next

	If thisLog Then Log($"Total Prods: ${totalProds}, Total: $$1.2{totalCompra}"$)
	l_total.Text = $"Total: $$1.2{totalCompra}"$
	l_totProds.text = $"Productos: ${totalProds}"$
	If totalProds < prodsVarReq + prodsVarReq2 + tpf Or totalProds > prodsVarReq + prodsVarReq2 + tpf Then
'		Log("INCOMPLETA")
		b_terminar1.Visible = False
		b_continuar.Visible = False
	Else
'		Log("COMPLETA")
		b_terminar1.Visible = True
		b_continuar.Visible = True
	End If
	If thisLog Then LogColor($"${prodsIds}${CRLF}${prodsCants}${CRLF}${prodsPrecios}"$, Colors.blue)
	If thisLog Then LogColor($"${prodsIds2}${CRLF}${prodsCants2}${CRLF}${prodsPrecios2}"$, Colors.Magenta)
'	LogColor($"Total Prods: ${totalProds}, Total: $$1.2{totalCompra}"$, Colors.Blue)
End Sub

Private Sub et_pCant_FocusChanged (HasFocus As Boolean)
	LogColor("focus changed", Colors.Magenta)
	cuentaProds
End Sub

Private Sub b_promoMenos_Click
	If et_promoCant.Text > 1 Then
		et_promoCant.Text = et_promoCant.Text.As(Int) - 1
	End If
	l_prodsFijos.Text = $"Productos fijos (${tpf2 * et_promoCant.Text.As(Int)})"$
	l_prodsVariables.Text = $"Productos variables requeridos: ${prodsVarReq_ * et_promoCant.Text.As(Int)}"$
	l_prodsVariables2.Text = $"Productos variables2 requeridos: ${prodsVarReq2_ * et_promoCant.Text.As(Int)}"$
	tpf = tpf2 * et_promoCant.Text.As(Int)
	prodsVarReq = prodsVarReq_ * et_promoCant.Text.As(Int)
	prodsVarReq2 = prodsVarReq2_ * et_promoCant.Text.As(Int)
	Log($"Max promos=${et_promoCant.Text.As(Int)}, Prods fijos=${tpf},  "$)
	cuentaProds
End Sub

Private Sub b_promoMas_Click
	If et_promoCant.Text < maxCantPromos Then
		et_promoCant.Text = et_promoCant.Text.As(Int) + 1
		l_prodsFijos.Text = $"Productos fijos (${tpf2 * et_promoCant.Text.As(Int)})"$
		l_prodsVariables.Text = $"Productos variables requeridos: ${prodsVarReq_ * et_promoCant.Text.As(Int)}"$
		l_prodsVariables2.Text = $"Productos variables2 requeridos: ${prodsVarReq2_ * et_promoCant.Text.As(Int)}"$
		tpf = tpf2 * et_promoCant.Text.As(Int)
		prodsVarReq = prodsVarReq_ * et_promoCant.Text.As(Int)
		prodsVarReq2 = prodsVarReq2_ * et_promoCant.Text.As(Int)
		Log($"Max promos=${et_promoCant.Text.As(Int)}, Prods fijos=${tpf},  "$)
	End If
	If et_promoCant.text > maxCantPromos Then et_promoCant.Text = maxCantPromos
	cuentaProds
End Sub

Private Sub b_terminar1_Click
	cuentaProds
	Log("====================================================================")
	For t = 0 To prodsIds.Size - 1 'Guardamos los productos fijos de la promocion en pedido.
		Private pn As String = Subs.traeProdNombre(prodsIds.Get(t))
		If prodsCants.Get(t) > 0 Then Subs.guardaProductoSinGestion(estaPromo, prodsPrecios.Get(t), prodsCants.Get(t), pn, prodsIds.Get(t), Subs.traeCliente, Subs.traeFecha, Subs.traeUsuarioDeBD, Subs.traeRuta, 0, B4XPages.MainPage.tipo_venta)
	Next
	For t = 0 To prodsIds2.Size - 1 'Guardamos los productos variables de la promocion en pedido.
		Private pn As String = Subs.traeProdNombre(prodsIds2.Get(t))
		Subs.guardaProductoSinGestion(estaPromo, prodsPrecios2.Get(t), prodsCants2.Get(t), pn, prodsIds2.Get(t), Subs.traeCliente, Subs.traeFecha, Subs.traeUsuarioDeBD, Subs.traeRuta, 0, B4XPages.MainPage.tipo_venta)
	Next
'	Log(B4XPages.MainPage.productos.prodsMap)
'	For o=0 To prodsIds.Size - 1 'Ponemos los productos fijos en el mapa de la compra.
'		Private tmpMap As Map = CreateMap("precio":prodsPrecios.Get(o), "cant":prodsCants.Get(o), "almacen":estaPromo)
''		Log(tmpMap)
'		If prodsCants.Get(o) > 0 Then B4XPages.MainPage.productos.prodsMap.Put(estaPromo&"|"&prodsIds.Get(o), tmpMap)
'	Next
'	For o=0 To prodsIds2.Size - 1 'Ponemos los productos variables en el mapa de la compra.
'		Private tmpMap As Map = CreateMap("precio":prodsPrecios2.Get(o), "cant":prodsCants2.Get(o), "almacen":estaPromo)
''		Log(tmpMap)
'		B4XPages.MainPage.productos.prodsMap.Put(estaPromo&"|"&prodsIds2.Get(o), tmpMap)
'	Next
'	LogColor(B4XPages.MainPage.productos.prodsMap, Colors.red)
	lv_prodsFijos.Clear
	B4XPages.MainPage.bTerminarClicked = True
	B4XPages.ShowPage("productos")
	B4XPages.MainPage.productos.b_terminar1_Click
End Sub

Private Sub b_continuar_Click
	cuentaProds
	Log("====================================================================")
	For t = 0 To prodsIds.Size - 1 'Guardamos los productos fijos de la promocion en pedido.
		Private pn As String = Subs.traeProdNombre(prodsIds.Get(t))
		Subs.guardaProductoSinGestion(estaPromo, prodsPrecios.Get(t), prodsCants.Get(t), pn, prodsIds.Get(t), Subs.traeCliente, Subs.traeFecha, Subs.traeUsuarioDeBD, Subs.traeRuta, 0, B4XPages.MainPage.tipo_venta)
	Next
	For t = 0 To prodsIds2.Size - 1 'Guardamos los productos variables de la promocion en pedido.
		Private pn As String = Subs.traeProdNombre(prodsIds2.Get(t))
		Subs.guardaProductoSinGestion(estaPromo, prodsPrecios2.Get(t), prodsCants2.Get(t), pn, prodsIds2.Get(t), Subs.traeCliente, Subs.traeFecha, Subs.traeUsuarioDeBD, Subs.traeRuta, 0, B4XPages.MainPage.tipo_venta)
	Next
'	Log(B4XPages.MainPage.productos.prodsMap)
'	For o=0 To prodsIds.Size - 1 'Ponemos los productos fijos en el mapa de la compra.
'		Private tmpMap As Map = CreateMap("precio":prodsPrecios.Get(o), "cant":prodsCants.Get(o), "almacen":estaPromo)
''		Log(tmpMap)
'		If prodsCants.Get(o) > 0 Then B4XPages.MainPage.productos.prodsMap.Put(estaPromo&"|"&prodsIds.Get(o), tmpMap)
'	Next
'	For o=0 To prodsIds2.Size - 1 'Ponemos los productos variables en el mapa de la compra.
'		Private tmpMap As Map = CreateMap("precio":prodsPrecios2.Get(o), "cant":prodsCants2.Get(o), "almacen":estaPromo)
''		Log(tmpMap)
'		B4XPages.MainPage.productos.prodsMap.Put(estaPromo&"|"&prodsIds2.Get(o), tmpMap)
'	Next
'	LogColor(B4XPages.MainPage.productos.prodsMap, Colors.red)
	lv_prodsFijos.Clear
'	Activity_KeyPress(KeyCodes.KEYCODE_BACK)
'	CallSubDelayed2(productos, "Activity_KeyPress", KeyCodes.KEYCODE_BACK)
	Private prodspage As C_Productos
	If prodspage.IsInitialized Then
		prodspage.lv_catalogos.Visible = True
		prodspage.lv_promos.Visible = False
		prodspage.clv_productos.AsView.Visible = False
	End If
	B4XPages.MainPage.bTerminarClicked = True
	B4XPages.ShowPage("productos")
End Sub

'Sub Activity_KeyPress (key As Int) As Boolean 'ignore
'	' BACK key pressed
'	Log("Keypress")
'	If key=KeyCodes.KEYCODE_BACK Then
'		Log("BACK")
'		Return False
'	End If
'	Return False
'	' Returning False signals the system to handle the key
'End Sub