B4A=true
Group=Default Group
ModulesStructureVersion=1
Type=Class
Version=11.5
@EndOfDesignText@
Sub Mods 'ignore
'	Lineas 709 y 1041 para corregir lo del almacen pachuca y rmi's
End Sub

Sub Class_Globals
	Private Root As B4XView 'ignore
	Private xui As XUI 'ignore
	'// Process Gloals
	Dim g As GPS
	Dim ruta As String
	Dim q_buscar As String
	'// Globals
	Dim c As Cursor
	Dim c2 As Cursor
	Dim C7 As Cursor
	Dim ListView1 As ListView
	Dim entro As String = 0
	Dim lfila As Label
	Dim marca As String
	Dim tipo As String
	Private BUSCA As EditText
	Dim ya_entro As String
	Dim bmp As Bitmap
	Dim c As Cursor
	Dim e As Cursor
	Dim f As Cursor
	Dim h As Cursor
	Dim CC As Cursor
	Dim DD As Cursor
	Dim l_tipo As Label
	Dim l_sub As Label
	Dim l_marca As Label
	Dim l_desc As Label
	Dim l_precio As Label
	Dim l_bodega As Label
	Dim cantidad As EditText
	Dim guardar As Button
	Dim Terminar As Button
	Dim img_prod As ImageView
	Dim IMG_PASO() As Byte
	Dim L_PROID As Label
	Dim clie_id As String
	Dim sDate,sTime As String
	Dim usuario As String
	Dim lat_gps, lon_gps As String
	Dim t_venta As Label
	Dim PROMO_C As String
	Dim i_fotol As ImageView
	Private DESC_PROMO As Button
	Private nopromo As Button
	Dim TOT_ART_PROMO As String
	Dim cambio As String
	Dim COSTO_TOT As String
	Dim ALMACEN As String
	Dim preciosin As String
	Dim cl_ruta As String
	Dim d As Cursor
	Dim TOT_ART_PROMOR As String
	Dim PROMO_CR As String
	Dim TOTAL_PROMO As String
	Dim HCCP_CANT As String
	Private Panel1 As Panel
	Dim query As String
	Private t_precio As Label
	Private LV_PRECIOS As ListView
	Private PERFIL As String
	Private EXISTE_CAMBIO As String
	Private QUERY2 As String
	Dim CAMBIOS As String
	Dim precio2 As String
	Private B_MENOS As Button
	Private B_MAS As Button
	Private E_CAMBIO As EditText
	Private B_MENOSC As Button
	Private B_MASC As Button
	Private limite_abordo As String
	Private limite_anterior As String
	Private cuenta As String
	Private limite_actual As String
	Private l_invact As Label
	Private L_LINV As Label
	Private L_CANT As Label
	Private V_ANT_SALIR As String
	Private L_CARGA As Label
	Private DESC_SS As String
	Private DESC_PING As String
	Private DESC_RTEC As String
	Private TIPO_CLIENTE As String
	Private PROMOS_VENTAS As String
	Private Label3 As Label
	Private ImageView4 As ImageView
	Dim PRECIO_CERO As String
	Private SI_APLICA_PROMO As String
	Private PROMO_ESP As String
	Private TOTAL_PROMO_CLIE As String
	Private INV_TOTAL_PROMO As String
	Private YA_IMPRIMIO As String
	Private HCCP_CANT_E As String
	Dim lvClic As Map
	Private TECLA_BACK As String = 0
	Private lv_producto2 As ListView
	Private lv_subtipo As ListView
	Dim logger As Boolean = True
	Private clv_rmi As CustomListView
	Private Panel5 As Panel
	Private l_prodX As Label
	Private p_prods As Panel
	Private et_pCant As EditText
	Private l_pCant As Label
	Dim r As Cursor
	Private b_rmi As Button
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)
	If Not(Starter.Logger) Then logger = False
	Root = Root1
	'load the layout to Root
	Root.LoadLayout("productos")
	ruta = File.DirInternal
	Panel1.Visible= False
	lvClic.Initialize
	Panel5.Visible = False
End Sub

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

Sub B4XPage_Appear
'	If Subs.traeUltimaActividadBD <> "Productos" Then Subs.iniciaActividad(Subs.traeUltimaActividadBD)
	CARGA_PRODUCTOS
	
	
	B_MASC.Enabled = True
	B_MENOSC.Enabled = True
	E_CAMBIO.Enabled = True
	
	If Subs.revisaimpreso(Subs.traeCliente) And l_sub.Text = "PROMOS" Then
		Msgbox ("YA SE IMPRIMIO EL TICKET.", "AVISO") 'ignore
		Panel1.Visible= False
		l_bodega.Text = "0"
		Subs.iniciaActividad("Cliente")
		l_sub.Text = ""
	End If
		
	If l_sub.Text = "PROMOS" Or l_sub.Text = "EXHIBIDOR/POP" Then
		B_MASC.Enabled = False
		B_MENOSC.Enabled = False
		E_CAMBIO.Enabled = False
		Log("Botones NO activos")
	Else
		B_MASC.Enabled = True
		B_MENOSC.Enabled = True
		E_CAMBIO.Enabled = True
	End If
End Sub

Sub CARGA_PRODUCTOS
	Terminar.Enabled = True
	guardar.Enabled = True
	If Subs.clienteActual <> "0" Then Starter.tipov = "VENTA" Else Starter.tipov = "ABORDO"
	If logger Then Log(Subs.clienteActual  & "|" &Starter.tipov)
	If logger Then Log("Entro="&entro)
	If logger Then Log("ya_entro="&ya_entro)
	If Starter.tipov = "PREVENTA" Then
		query = "cat_gunaprod"
		QUERY2 = "COUNT_GUNAPROD"
	Else IF Starter.tipov = "VENTA" Then
		query = "cat_gunaprod2"
		QUERY2= " COUNT_GUNAPROD2"
	ELSE IF Starter.tipov = "ABORDO" Then
		query = "cat_gunaprod"
		QUERY2 = "COUNT_GUNAPROD"
	Else
		query = "cat_gunaprod"
		QUERY2 = "COUNT_GUNAPROD"
	End If
	E_CAMBIO.Text = "0"
	cantidad.Text = "0"
	If ya_entro <> "1" Then
		If entro=0 Then entro ="2"
		ya_entro ="1"
		Dim label1 As Label
		label1 = ListView1.TwoLinesLayout.Label
		label1.TextSize = 14
		label1.TextColor = Colors.Black
		Dim label2 As Label
		label2 = ListView1.TwoLinesLayout.SecondLabel
		label2.TextSize = 14
		label2.TextColor = Colors.Black
		c2=Starter.skmt.ExecQuery("select  COUNT(*) AS CUANTOS, CAT_GP_TIPO from " & query & " where CAT_GP_ALMACEN > 0 AND CAT_GP_TIPOPROD <> 1 and (length(CAT_GP_CODPROMO) = 1 OR CAT_GP_CODPROMO = CAT_GP_ID) GROUP BY CAT_GP_TIPO ORDER BY CAT_GP_TIPO ASC")
		ListView1.Clear
		lfila.text = "TIPO"
		lvClic.Put("PASO", "TIPO")
		If c2.RowCount>0 Then
			For i=0 To c2.RowCount -1
				c2.Position=i
				ListView1.AddTwoLines(c2.GetString("CAT_GP_TIPO"),c2.GetString("CUANTOS"))
			Next
		End If
		entro = "2"
	Else
		entro = "3"
		If logger Then LogColor($"Entro=${entro}"$, Colors.Blue)
		ListView1_ItemClick(1,V_ANT_SALIR)
	End If
	c=Starter.skmt.ExecQuery2("select CAT_VA_VALOR from CAT_VARIABLES WHERE CAT_VA_DESCRIPCION = ?", Array As String ("PERFIL"))
	c.Position =0
	PERFIL = c.GetString("CAT_VA_VALOR")
	c.Close
	If PERFIL = "V-COSTO" Then
		Starter.skmt.ExecNonQuery("DELETE FROM CAT_GUNAPROD WHERE CAT_GP_ID IN (SELECT CAT_GP_ID FROM LISTA_PRECIOS WHERE NLISTA = '9' AND PRECIO = '0')")
	End If
	ListView1.Visible = True
	If cuenta  = "0" Then
		B4XPages.mainpage.Cliente.CREDITO_DISPONIBLE  = 10000000
	End If
	
	LogColor(ListView1.Visible,Colors.Red)
End Sub

Sub ListView1_ItemClick (Position As Int, Value As Object)
	Log(Value)
	Private l As List
	l.Initialize
	l.Add(Position)
	l.Add(Value)
	If lvClic.ContainsKey("ultimo") Then lvClic.Put("anterior", lvClic.get("ultimo"))
	lvClic.Put("ultimo", l)
'	If Logger Then Log($"lvClic=${lvClic}"$)
	Private Subrutina As Map =CreateMap("nombre":"ListView1_ItemClick", "param1":Position, "param2":Value)
	Starter.sesion = CreateMap("actividad":Me, "sub":Subrutina)
'	If Logger Then LogColor($"Entro=${entro}, Value=${Value}"$, Colors.Blue)
	Log("MARCA=" & l_marca.Text & "|" & entro)
	B_MASC.Enabled = True
	B_MENOSC.Enabled = True
	E_CAMBIO.Enabled = True
	Log("Botones activos")
	If Value = "PROMOS" And B4XPages.mainpage.Cliente.cuenta <> "N" Then
		entro = "3"
		marca = "PROMOS"
		tipo = "PROMOS"
		Log($"MARCA=${marca}, tipo=${tipo}"$)
	Else if Value = "PROMOS" And B4XPages.mainpage.Cliente.cuenta = "N"  Then
		entro = "5"
		Subs.iniciaActividad("Cliente")
	End If
	If entro = "1" Then
		marca = Value
		c2=Starter.skmt.ExecQuery2("select  COUNT(*) AS CUANTOS, CAT_GP_TIPO from " & query & " where CAT_GP_ALMACEN > 0 and CAT_GP_CLASIF = ? AND CAT_GP_TIPOPROD <> 1 and (length(CAT_GP_CODPROMO) = 1 OR CAT_GP_CODPROMO = CAT_GP_ID) GROUP BY CAT_GP_TIPO ORDER BY CAT_GP_TIPO ASC", Array As String(Value))
		ListView1.Clear
		lfila.text = "TIPO"
		lvClic = CreateMap("PASO":0, "TIPO":0, "SUB-TIPO":0)
		lvClic.Put("TIPO", l)
		lvClic.Put("PASO", lfila.Text)
		If logger Then Log("lvClic="&lvClic)
		If c2.RowCount>0 Then
			For i=0 To c2.RowCount -1
				c2.Position=i
				ListView1.AddTwoLines(c2.GetString("CAT_GP_TIPO"),c2.GetString("CUANTOS"))
			Next
		End If
		entro = "3"
	Else If entro = "2" Then 'Or lvClic.Get("PASO") = "TIPO" Then
		tipo = Value
		c2=Starter.skmt.ExecQuery2("select  COUNT(*) AS CUANTOS, CAT_GP_SUBTIPO from " & query & " where CAT_GP_ALMACEN > 0 and CAT_GP_TIPO = ? AND CAT_GP_TIPOPROD <> 1 and (length(CAT_GP_CODPROMO) = 1 OR CAT_GP_CODPROMO = CAT_GP_ID) GROUP BY CAT_GP_SUBTIPO ORDER BY CAT_GP_SUBTIPO ASC ", Array As String( Value))
		ListView1.Clear
		lfila.text = "SUB-TIPO"
		lvClic.Put("PASO", lfila.Text)
		lvClic.Put("SUB-TIPO", l)
'		If Logger Then Log("lvClic="&lvClic)
		If c2.RowCount>0 Then
			For i=0 To c2.RowCount -1
				c2.Position=i
				ListView1.AddTwoLines(c2.GetString("CAT_GP_SUBTIPO"),c2.GetString("CUANTOS"))
			Next
		End If
		entro = "3"
'		If Logger Then LogColor($"Entro=${entro}, Value=${Value}"$, Colors.Blue)
	Else If entro = "3" Then
		Log(tipo)
		TECLA_BACK = 0
		If tipo = "PROMOS" Then TECLA_BACK = 1
'		If Logger Then LogColor($"Entro=${entro}, Value=${Value}"$, Colors.Blue)
		V_ANT_SALIR = Value
		c2=Starter.skmt.ExecQuery2("select  CAT_GP_NOMBRE, CAT_GP_PRECIO, CAT_GP_ALMACEN, CAT_GP_IMG from " & query & " where CAT_GP_ALMACEN > 0  AND CAT_GP_TIPO = ? AND CAT_GP_SUBTIPO =? AND CAT_GP_TIPOPROD <> 1  and (length(CAT_GP_CODPROMO) = 1 OR CAT_GP_CODPROMO = CAT_GP_ID) and CAT_GP_ID NOT IN (SELECT PE_PROID FROM PEDIDO WHERE PE_CEDIS = PE_PROID AND pe_cliente in (Select CUENTA from cuentaa)) ", Array As String( tipo,  Value))
		ListView1.Clear
		lfila.text = "PRODUCTO"
		lvClic.Put("PASO", lfila.Text)
		lvClic.Put("PRODUCTO", l)
'		If Logger Then Log("lvClic="&lvClic)
		Dim ins As InputStream
		Dim bmp As Bitmap
		Dim jpeg() As Byte
		If c2.RowCount>0 Then
			For i=0 To c2.RowCount -1
				c2.Position=i
				jpeg = c2.GetBlob("CAT_GP_IMG")
				ins.InitializeFromBytesArray(jpeg, 0, jpeg.Length)
				bmp.Initialize2(ins)
				ListView1.AddTwoLines(c2.GetString("CAT_GP_NOMBRE"),"# " & c2.GetString("CAT_GP_ALMACEN") & " $ " & c2.GetString("CAT_GP_PRECIO"))
			Next
		End If
		entro = "4"
'		LogColor($"Entro=${entro}, Value=${Value}"$, Colors.Blue)
	Else If entro = "4" Then
'		If Logger Then LogColor($"Entro=${entro}, Value=${Value}"$, Colors.Blue)
		Starter.skmt.ExecNonQuery("delete from PROID")
		Starter.skmt.ExecNonQuery2("INSERT INTO PROID VALUES (?)", Array As Object(Value))
		Log($"MARCA=${marca}, tipo=${tipo}"$)
		Log("*************  MARCA=" & l_marca.Text & "|" & l_sub.text & "|" & marca & "|" & Value)
		HACER_PEDIDO
		If tipo = "PROMOS" Or tipo = "EXHIBIDOR/POP" Then
			B_MASC.Enabled = False
			B_MENOSC.Enabled = False
			E_CAMBIO.Enabled = False
			Log("Botones NO activos")
		Else
			B_MASC.Enabled = True
	B_MENOSC.Enabled = True
	E_CAMBIO.Enabled = True
		End If

		
	End If
End Sub

Private Sub lv_subtipo_ItemClick (Position As Int, Value As Object)
	
End Sub

Private Sub lv_producto2_ItemClick (Position As Int, Value As Object)
	
End Sub

Sub B4XPage_CloseRequest As ResumableSub'ignore
	' BACK key pressed
		Starter.ultima_back_productos = 1
		If logger Then LogColor("BACK", Colors.Red)
		If TECLA_BACK = 1 Then entro ="2"
	If Panel5.Visible Then
		Panel5.Visible = False
	Else
	End If
		ya_entro ="0"

		If entro = "4"  Or entro = "3" Then
			entro = "2"
			TECLA_BACK = 1
			ListView1_ItemClick(1,tipo)
			Return True
		Else If entro = "2" Then    'Antes 3
			'B4XPage_Appear
			CARGA_PRODUCTOS
			Return False
		Else
			If logger Then LogColor($"Entro=${entro} - BACK"$, Colors.Blue)
			Return False
		End If
	' Returning False signals the system to handle the key
End Sub

Sub BUSCA_TextChanged (Old As String, New As String)

	q_buscar = "%" & BUSCA.Text & "%"
	
		
	c=Starter.skmt.ExecQuery2("select CAT_GP_NOMBRE, CAT_GP_ALMACEN, CAT_GP_PRECIO,CAT_GP_IMG, CAT_GP_TIPO from " & query & " where CAT_GP_PRECIO > 0 AND CAT_GP_ALMACEN > 0 and CAT_GP_NOMBRE like ? and CAT_GP_CLASIF <>  ?  AND CAT_GP_TIPOPROD <> 1 and (length(CAT_GP_CODPROMO) = 1 OR CAT_GP_CODPROMO = CAT_GP_ID) order by CAT_GP_NOMBRE asc", Array As String(q_buscar,"PROMOS"))
			
	ListView1.Clear
	Dim ins As InputStream
	Dim bmp As Bitmap
	Dim jpeg() As Byte
	If c.RowCount>0 Then
		For i=0 To c.RowCount -1
			c.Position=i
			jpeg = c.GetBlob("CAT_GP_IMG")
			ins.InitializeFromBytesArray(jpeg, 0, jpeg.Length)
			bmp.Initialize2(ins)
			tipo = c.GetString("CAT_GP_TIPO")
			ListView1.AddTwoLines(c.GetString("CAT_GP_NOMBRE"),"Existencia " & c.GetString("CAT_GP_ALMACEN") & " Precio $" & c.GetString("CAT_GP_PRECIO"))
		
		Next
	End If
	entro= "4"	
	c.Close
End Sub

Sub HACER_PEDIDO
	ListView1.Visible = False
	c=Starter.skmt.ExecQuery("Select SUBSTR(CUENTA,1,1) AS CUENTA from cuentaa")
	c.Position=0
	clie_id = c.GetString("CUENTA")
	c.Close
	c=Starter.skmt.ExecQuery("Select CUENTA from cuentaa")
'	If Logger Then Log("+ + + "&c.RowCount)
	c.Position=0
	cuenta = c.GetString("CUENTA")
	c.Close
	Panel1.Visible= True
	cambio = "0"
	cantidad.Text = "0"
	c=Starter.skmt.ExecQuery("select  ID_ALMACEN from CAT_ALMACEN")
'	If Logger Then Log("+ + + "&c.RowCount)
	C.Position =0
	ALMACEN = C.GetString("ID_ALMACEN")
	C.Close
	i_fotol.Visible = False
	c=Starter.skmt.ExecQuery("select CAT_GP_ID,CAT_GP_NOMBRE,CAT_GP_IMP1,CAT_GP_IMP2,CAT_GP_PRECIO,CAT_GP_CLASIF,CAT_GP_STS,CAT_GP_TIPO,CAT_GP_SUBTIPO,CAT_GP_IMG,CAT_GP_ALMACEN,CAT_GP_DEV  from " & query & " where CAT_GP_NOMBRE In (Select PDESC from PROID)")
	c.Position=0
	L_PROID.Text = c.GetString("CAT_GP_ID")
	l_tipo.Text = c.GetString("CAT_GP_TIPO")
	l_sub.Text = c.GetString("CAT_GP_SUBTIPO")
	l_marca.Text = c.GetString("CAT_GP_CLASIF")
	l_desc.Text = c.GetString("CAT_GP_NOMBRE")
	
	
	If l_sub.Text = "PROMOS" Or l_sub.Text = "EXHIBIDOR/POP" Then
		B_MASC.Enabled = False
		B_MENOSC.Enabled = False
		E_CAMBIO.Enabled = False
		Log("Botones NO activos")
	Else
		B_MASC.Enabled = True
		B_MENOSC.Enabled = True
		E_CAMBIO.Enabled = True
	End If
	
	If PERFIL = "V-SUPER" Then
		c2=Starter.skmt.ExecQuery("select PRECIO, NLISTA FROM LISTA_PRECIOS WHERE NLISTA = '10' AND  CAT_GP_ID =" & "'" &L_PROID.TEXT&"' ORDER BY NLISTA ASC")
		If logger Then Log("+ + + "&c.RowCount)
		c2.Position=0
		l_precio.Text = c2.GetString("PRECIO")
		c2.Close
	Else
		l_precio.Text = c.GetDouble("CAT_GP_PRECIO")
	End If
	l_bodega.Text = c.GetString("CAT_GP_ALMACEN")
	PROMOS_VENTAS =  c.GetString("CAT_GP_ALMACEN")
	IMG_PASO = c.GetBlob("CAT_GP_IMG")
	PROMO_C = c.GetString("CAT_GP_STS")
	cambio = c.GetString("CAT_GP_DEV")
	preciosin = c.GetString("CAT_GP_IMP2")
	PROMO_CR = c.GetString("CAT_GP_IMP1")
	precio2 = l_precio.Text
	c=Starter.skmt.ExecQuery("select  CAT_VA_VALOR from CAT_VARIABLES where CAT_VA_DESCRIPCION = 'LIMITE'")
'	If Logger Then Log("+ + + "&c.RowCount)
	If c.RowCount > 0 Then
		C.Position = 0
		limite_abordo = C.GetString("CAT_VA_VALOR")
	End If
	C.Close
	c=Starter.skmt.ExecQuery2("select sum(cat_gp_precio * cat_gp_almacen) as MONTO from cat_gunaprod2 where cat_gp_almacen > 0 and cat_gp_clasif <> ? ", Array As String("PROMOS"))
'	If Logger Then Log("+ + + "&c.RowCount)
	c.Position = 0
	limite_anterior =  c.GetString("MONTO")
	c.Close
	If clie_id = "0"  Then
		l_invact.Visible=True
		L_LINV.Visible=True
		L_CARGA.Visible = True
		L_CANT.Text ="PEDIDO"
		c2=Starter.skmt.ExecQuery("select count(*) as CUANTOS  from cat_gunaprod2 where CAT_GP_NOMBRE In (Select PDESC from PROID)")
'		If Logger Then Log("+ + + "&c2.RowCount)
		c2.Position =0
		If c2.GetString("CUANTOS") > 0 Then
			c=Starter.skmt.ExecQuery("select CAT_GP_ALMACEN  from cat_gunaprod2 where CAT_GP_NOMBRE In (Select PDESC from PROID)")
			C.Position =0
			l_invact.text = C.GetString("CAT_GP_ALMACEN")
			C.Close
		Else
			l_invact.text = 0
		End If
		Label3.Visible = False
		ImageView4.Visible = False
		E_CAMBIO.Visible = False
		B_MENOSC.Visible = False
		B_MASC.Visible = False
	Else
		l_invact.Visible=False
		L_LINV.Visible=False
		L_CARGA.Visible = False
		L_CANT.Text ="VENTA"
		Label3.Visible = True
		ImageView4.Visible = True
		E_CAMBIO.Visible = True
		B_MENOSC.Visible = True
		B_MASC.Visible = True
	End If
	DESC_SS = 0
	DESC_PING = 0
	DESC_RTEC = 0
	CC=Starter.skmt.ExecQuery("SELECT COUNT(*) AS CUANTOS FROM DESC_CLIENTES WHERE DC_CL_CLIENTE IN (Select cuenta from cuentaa) ")
'	If Logger Then Log("+ + + "&CC.RowCount)
	CC.Position =0
	If CC.GetString("CUANTOS")  > 0 Then
		DD=Starter.skmt.ExecQuery("SELECT DC_CL_CLIENTE , CAT_CL_DESCUENTO_SS , CAT_CL_DESCUENTO_RTEC , CAT_CL_DESCUENTO_PING, CAT_CL_TIPOCLIENTE FROM DESC_CLIENTES WHERE DC_CL_CLIENTE IN (Select cuenta from cuentaa) ")
'		If Logger Then Log("+ + + "&DD.RowCount)
		DD.Position =0
		DESC_SS = DD.GetString("CAT_CL_DESCUENTO_SS")
		DESC_RTEC = DD.GetString("CAT_CL_DESCUENTO_RTEC")
		DESC_PING = DD.GetString("CAT_CL_DESCUENTO_PING")
		TIPO_CLIENTE = DD.GetString("CAT_CL_TIPOCLIENTE")
		DD.Close
	Else
		DESC_SS = 0
		DESC_PING = 0
		DESC_RTEC = 0
		TIPO_CLIENTE = 0
	End If
	CC.Close
	'' esto es de las promos para activarlas despues
	If l_marca.text = "PROMOS" And l_tipo.Text ="PROMOS" Then
		DESC_PROMO.Visible = True
		CC=Starter.skmt.ExecQuery2("select count(*) as CUANTOS FROM PROMOS_COMP WHERE CAT_PA_ID = ?", Array As String(L_PROID.Text))
		CC.Position =0
		If CC.GetString("CUANTOS") = 0 Then
			l_bodega.Text = "1"
		Else
			CC.Close
			CC=Starter.skmt.ExecQuery2("select CAT_PA_MAXPROM, CAT_PA_MAXPROMREC, CAT_PA_MAXPROMCLIE FROM PROMOS_COMP WHERE CAT_PA_ID = ?", Array As String(L_PROID.Text))
			CC.Position =0
				
			DD=Starter.skmt.ExecQuery2("SELECT COUNT(*) AS CUANTOS FROM HIST_CLIENTE_CANT_PROMOS WHERE HCCP_CLIENTE IN (Select cuenta from cuentaa) AND HCCP_PROMO =?", Array As String(L_PROID.Text))
			DD.Position =0
			If DD.GetString("CUANTOS")  = 0 Then
				HCCP_CANT_E = 0
			Else
				DD.Close
				DD=Starter.skmt.ExecQuery2("SELECT HCCP_CANT FROM HIST_CLIENTE_CANT_PROMOS WHERE HCCP_CLIENTE IN (Select cuenta from cuentaa) AND HCCP_PROMO =?", Array As String(L_PROID.Text))
				DD.Position =0
				HCCP_CANT_E = DD.GetString("HCCP_CANT")
				DD.Close
			End If
			DD=Starter.skmt.ExecQuery2("SELECT COUNT(*) AS CUANTOS FROM HIST_VENTAS WHERE HVD_CLIENTE IN (Select cuenta from cuentaa) AND HVD_PRONOMBRE =?", Array As String(l_desc.Text))
			DD.Position =0
			If DD.GetString("CUANTOS")  = 0 Then
				HCCP_CANT = 0
			Else
				DD.Close
				DD=Starter.skmt.ExecQuery2("SELECT SUM(HVD_CANT) AS HCCP_CANT FROM HIST_VENTAS WHERE HVD_CLIENTE IN (Select cuenta from cuentaa) AND HVD_PRONOMBRE =?", Array As String(l_desc.Text))
				DD.Position =0
				HCCP_CANT = DD.GetString("HCCP_CANT")
				DD.Close
			End If
			HCCP_CANT_E = HCCP_CANT_E - HCCP_CANT
			TOTAL_PROMO = CC.GetString("CAT_PA_MAXPROMCLIE") - HCCP_CANT
			If HCCP_CANT = 0 Then
				PROMOS_VENTAS = CC.GetString("CAT_PA_MAXPROM")
			Else IF TOTAL_PROMO > CC.GetString("CAT_PA_MAXPROMREC") Then
				PROMOS_VENTAS = 	CC.GetString("CAT_PA_MAXPROMREC")
			Else
				PROMOS_VENTAS = TOTAL_PROMO
			End If
		End If
	Else
		DESC_PROMO.Visible = False
	End If
	c.Close
	c=Starter.skmt.ExecQuery("select CASE WHEN pe_costo_tot is null then 0 else  sum( pe_costo_tot ) end as TOTAL_CLIE FROM PEDIDO WHERE PE_CLIENTE IN (Select CUENTA from cuentaa)")
	c.Position=0
	t_venta.Text = c.GetString("TOTAL_CLIE")
	c.Close
	Terminar.Visible = True
	guardar.Visible = True
	cantidad.Visible = True
	If cambio = "1" Then
		nopromo.Visible = True
	Else
		nopromo.Visible = False
	End If
	Dim out As OutputStream
	out = File.OpenOutput(ruta, "1.jpg", False)
	out.WriteBytes(IMG_PASO, 0, IMG_PASO.Length)
	out.Close
	img_prod.Bitmap = LoadBitmap(ruta,"1.jpg")
	TOT_ART_PROMO = "0"
	TOT_ART_PROMOR = "0"
	If l_marca.text = "PROMOS" And l_tipo.Text ="PROMOS" And l_bodega.Text = "0" Then
		MsgboxAsync("ALERTA","EL CLIENTE YA NO PUEDE TENER ESTA PROMOCION") 'ignore
		Panel1.Visible= False
	End If
	If marca = "PROMOS" Then
		DD=Starter.skmt.ExecQuery2("SELECT COUNT(*) AS CUANTOS  FROM PROMOS_COMP WHERE CAT_PA_ID =?", Array As String(L_PROID.Text))
		DD.Position =0
		If  DD.GetString("CUANTOS") > 0 Then
			C=Starter.skmt.ExecQuery2("SELECT CAT_PA_MAXPROMCLIE  FROM PROMOS_COMP WHERE CAT_PA_ID =?", Array As String(L_PROID.Text))
			C.Position =0
			TOTAL_PROMO_CLIE = c.GetString("CAT_PA_MAXPROMCLIE")
			c.Close
		Else
			TOTAL_PROMO_CLIE = 0
		End If
		DD.Close
	Else
		If TIPO_CLIENTE = "TDCK" Then
	
			c=Starter.skmt.ExecQuery("select PRECIO, NLISTA FROM LISTA_PRECIOS WHERE NLISTA ='10' AND  CAT_GP_ID =" & "'" &L_PROID.TEXT&"' ORDER BY NLISTA ASC")
			c.Position=0
			l_precio.Text = c.GetString("PRECIO")
			c.Close
		End If
	End If
	If l_tipo.Text = "SALTY SNACKS" Then
		l_precio.Text =  Round2(l_precio.Text -(l_precio.Text * DESC_SS/100),2)
	ELSE IF l_tipo.Text = "PING" Then
		l_precio.Text = Round2(l_precio.Text - (l_precio.Text * DESC_PING/100),2)
	ELSE IF l_tipo.Text = "RTEC" Then
		l_precio.Text = Round2(l_precio.Text - (l_precio.Text * DESC_RTEC/100),2)
	End If

	If l_marca.text = "PROMOS" And l_tipo.Text ="PROMOS" And l_bodega.Text = "0"  Then
		MsgboxAsync("ALERTA","EL CLIENTE NO PUEDE TENER ESTA PROMOCION") 'ignore
		Panel1.Visible= False
	End If
	DD=Starter.skmt.ExecQuery2("SELECT COUNT(*) AS CUANTOS  FROM HIST_CLIENTE_CANT_PROMOS WHERE HCCP_CLIENTE IN (Select cuenta from cuentaa) AND HCCP_PROMO =?", Array As String(L_PROID.Text))
	DD.Position =0
	SI_APLICA_PROMO = DD.GetString("CUANTOS")
	DD.Close
	DD=Starter.skmt.ExecQuery2("SELECT COUNT(*) AS CUANTOS  FROM HIST_CLIENTE_CANT_PROMOS WHERE HCCP_PROMO =?", Array As String(L_PROID.Text))
	DD.Position =0
	PROMO_ESP = DD.GetString("CUANTOS")
	DD.Close

	
	If PROMO_ESP > 0 And SI_APLICA_PROMO = 0 Then
		MsgboxAsync("ALERTA","EL CLIENTE NO PUEDE TENER ESTA PROMOCION 1") 'ignore
		Panel1.Visible= False
	End If
	If L_PROID.Text = "PROMO58" Then
		l_bodega.Text = 1
	End If
	If 	L_PROID.Text = "PROMO58" And HCCP_CANT_E < 1 Then
		MsgboxAsync("ALERTA","EL CLIENTE NO PUEDE TENER ESTA PROMOCION <")
		Panel1.Visible= False
	End If
	If 	 HCCP_CANT_E = 0  And ( L_PROID.Text = "1000000001" Or L_PROID.Text = "PROMO243" Or L_PROID.Text = "PROMO244" Or L_PROID.Text = "PROMO245" )Then
		MsgboxAsync("ALERTA","EL CLIENTE NO PUEDE TENER ESTA PROMOCION 0 " &HCCP_CANT & " "& L_PROID.Text) 'ignore
		Panel1.Visible= False
	Else IF L_PROID.Text = "1000000001" Or L_PROID.Text = "PROMO243" Or L_PROID.Text = "PROMO244" Or L_PROID.Text = "PROMO245"  Then
		l_bodega.Text = 1
	End If
	If 	L_PROID.Text = "PROMO76" And HCCP_CANT = 0 Then
		MsgboxAsync("ALERTA","EL CLIENTE NO PUEDE TENER ESTA PROMOCION 2") 'ignore
		Panel1.Visible= False
	Else IF L_PROID.Text = "PROMO76" Then
		l_bodega.Text = 1
	End If
	C=Starter.skmt.ExecQuery("SELECT COUNT(*) AS CUANTOS  FROM CAT_DESCUENTOS_SKU WHERE CAT_DS_CLIENTE IN (Select cuenta from cuentaa)  AND CAT_DS_PRODID  = " & "'" &L_PROID.TEXT&"' ")
	C.Position =0
	If c.GetString("CUANTOS") > 0 Then
		DD=Starter.skmt.ExecQuery("SELECT CAT_DS_PORCENTAJE  FROM CAT_DESCUENTOS_SKU WHERE CAT_DS_CLIENTE IN (Select cuenta from cuentaa)  AND CAT_DS_PRODID  = " & "'" &L_PROID.TEXT&"' ")
		DD.Position =0
		l_precio.Text = Round2(l_precio.Text - (l_precio.Text * DD.GetString("CAT_DS_PORCENTAJE")/100),2)
	End If
	c.Close
	'' ESTO ES PARA NO PASAR DE LAS PROMOS ABORDO Y MANDARLAS AL MINIMO INICIO
	If l_marca.text = "PROMOS" And l_tipo.Text ="PROMOS" Then
		e=Starter.skmt.ExecQuery2("select count(*) as CUANTOS FROM pedido WHERE PE_PROID = ? AND PE_CLIENTE  IN (Select cuenta from cuentaa) ", Array As String(L_PROID.Text))
		e.Position =0
		If e.GetString("CUANTOS") = 0 Then
			l_bodega.Text = PROMOS_VENTAS
			e=Starter.skmt.ExecQuery2("select count(*) as CUANTOS FROM CAT_DETALLES_PAQ WHERE CAT_DP_ID  = ? and cat_dp_tipo = 0", Array As String(L_PROID.Text) )
			e.Position = 0
			If e.GetString("CUANTOS") > 0 Then
				F=Starter.skmt.ExecQuery2("select CAT_DP_IDPROD, sum(CAT_DP_PZAS) as CAT_DP_PZAS FROM CAT_DETALLES_PAQ WHERE CAT_DP_ID = ? and cat_dp_tipo = 0 GROUP BY CAT_DP_IDPROD", Array As String(L_PROID.Text) )
				'se coloco el if por la mala promocion.
				For i=0 To F.RowCount -1
					F.Position=i
					c=Starter.skmt.ExecQuery2("select count(*) as CUANTOS from cat_gunaprod2 where CAT_GP_ID = ? and cat_gp_almacen > 0 ", Array As String(f.GetString("CAT_DP_IDPROD")))
					c.Position=0
					If c.GetString("CUANTOS") > 0 Then
						h=Starter.skmt.ExecQuery2("select CAT_GP_NOMBRE, CAT_GP_ALMACEN from  cat_gunaprod2 where CAT_GP_ID = ? ", Array As String(f.GetString("CAT_DP_IDPROD")))
						h.Position=0
						If TOTAL_PROMO_CLIE > l_bodega.Text Then
							INV_TOTAL_PROMO = l_bodega.Text
						Else
							INV_TOTAL_PROMO = TOTAL_PROMO_CLIE
						End If
						If h.GetString("CAT_GP_ALMACEN")  < INV_TOTAL_PROMO * f.GetString("CAT_DP_PZAS") Then
							If h.GetString("CAT_GP_ALMACEN")  < f.GetString("CAT_DP_PZAS") Then
								l_bodega.Text = "0"
							Else
								l_bodega.Text = Round(h.GetString("CAT_GP_ALMACEN") / f.GetString("CAT_DP_PZAS"))
							End If
						End If
						h.Close
					else if c.GetString("CUANTOS") = 0 Then
						l_bodega.Text = "0"
					End If
					c.Close
				Next
				f.Close
			End If
			e.Close
			If l_bodega.Text = "0" Then
				Msgbox ("PROMO LLEGO AL LIMITE PARA ESTE CLIENTE", "AVISO") 'ignore
				Panel1.Visible= False
				l_bodega.Text = "0"
				Subs.iniciaActividad("Cliente")
			End If
		Else
			Msgbox ("YA SE VENDIO LA PROMO.", "AVISO") 'ignore
			Panel1.Visible= False
			l_bodega.Text = "0"
			Subs.iniciaActividad("Cliente")
		End If
	End If
	If l_marca.text = "PROMOS" And B4XPages.MainPage.cliente.YA_IMPRIMIO2 = 1 Then
		Msgbox ("YA SE IMPRIMIO EL TICKET.", "AVISO") 'ignore
		Panel1.Visible= False
		l_bodega.Text = "0"
		Subs.iniciaActividad("Cliente")
		l_sub.Text = ""
	End If
	
	If l_marca.text = "PROMOS" Then
		c=Starter.skmt.ExecQuery("SELECT COUNT(*) AS CUANTOS FROM CLIENTE_CARGA_DIA WHERE CCD_CUENTA IN (Select CUENTA from cuentaa)")
		c.Position = 0
		If c.GetString("CUANTOS") > 0 Then
			Msgbox ("YA SE IMPRIMIO EL TICKET.", "AVISO") 'ignore
			Panel1.Visible= False
			l_bodega.Text = "0"
			Subs.iniciaActividad("Cliente")
			l_sub.Text = ""
		End If
		c.Close
	End If
	If l_marca.text = "PROMOS" Then
		C7=Starter.skmt.ExecQuery2("SELECT COUNT(*) AS CUANTOS FROM PEDIDO  WHERE PE_CLIENTE IN (Select cuenta from cuentaa) AND PE_PROID  = ? ", Array As String(L_PROID.Text))
		C7.Position=0
		If C7.GetString("CUANTOS") > 0 Then
			Msgbox ("YA SE VENDIO LA PROMO.", "AVISO") 'ignore
			Panel1.Visible= False
		End If
	End If
End Sub

Sub guardar_Click
	B_MASC.Enabled = True
	B_MENOSC.Enabled = True
	E_CAMBIO.Enabled = True
	guardar.Enabled = False
	Panel1.Visible=False
	If cantidad.Text = "" Then
		cantidad.Text = 0
	End If
	limite_actual = t_venta.Text + (cantidad.Text * l_precio.Text)
	Log($"Limite actual=${limite_actual}"$)
	If cuenta = "0" Then
		limite_actual = limite_actual + limite_anterior
	End If
	Log($"Limite actual=${limite_actual}"$)
	If Starter.lat_gps = 0 Or Starter.lat_gps = Null Then
		Private coords As List = Subs.traeCoordsDeBD
		Starter.lat_gps = coords.Get(0)
		Starter.lon_gps = coords.Get(1)
	End If
'	If Logger Then Log($"cuenta: ${cuenta}, limite_actual:${limite_actual}, limite_abordo:${limite_abordo}"$)
	If limite_abordo = "" Then limite_abordo = 0 'Mod por CHV - 211213 - Para que si limite_abordo no tiene valor, no nos saque de la app.
	If cuenta ="0" And limite_actual > limite_abordo And cantidad.Text <> 0 And cantidad.Text <> "" Then
		Log($"${cuenta},${limite_actual > limite_abordo},${cantidad.text}"$)
		Msgbox("Excede el limite abordo","limite") 'ignore
	Else IF (cuenta <> "0" And limite_actual < B4XPages.MainPage.cliente.CREDITO_DISPONIBLE) Or (cuenta ="0" And limite_actual < limite_abordo) Then
		If E_CAMBIO.Text = "" Or E_CAMBIO.Text = "0" Then
			Panel1.Visible= False
			entro = "3"
			If BUSCA.Text = "" Then
				ListView1_ItemClick(1,V_ANT_SALIR)
			Else
				BUSCA_TextChanged(1, BUSCA.Text)
			End If
		Else
			If E_CAMBIO.Text = "" Then E_CAMBIO.Text = "0"
			If E_CAMBIO.Text * l_precio.Text  > t_venta.Text Then
				Msgbox("Excede EL MONTO DE LA VENTA", "ADVERTENCIA") 'ignore
			else if  E_CAMBIO.Text + cantidad.Text > l_bodega.Text Then
				Msgbox("Excede existencia", "ADVERTENCIA") 'ignore
			Else
				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")
				'' para el rmi con costo se quita el comentado de abajo FL 11-05-2023
				Starter.skmt.ExecNonQuery2("INSERT INTO PEDIDO (PE_CEDIS,PE_COSTO_TOT,PE_COSTOU,PE_CANT,PE_PRONOMBRE,PE_PROID,PE_CLIENTE,PE_FECHA,PE_USUARIO,PE_TIPO,PE_PRECIO2) VALUES(?,?,?,?,?,?,?,?,?,?,?) ", Array As Object ("DUR", E_CAMBIO.text * l_precio.Text * (-1),l_precio.Text, E_CAMBIO.text,"CAMBIO" & l_desc.text, L_PROID.Text,clie_id, sDate & sTime, usuario,Starter.tipov,precio2))
				''				Starter.skmt.ExecNonQuery2("INSERT INTO PEDIDO (PE_CEDIS,PE_COSTO_TOT,PE_COSTOU,PE_CANT,PE_PRONOMBRE,PE_PROID,PE_CLIENTE,PE_FECHA,PE_USUARIO,PE_TIPO,PE_PRECIO2) VALUES(?,?,?,?,?,?,?,?,?,?,?) ", Array As Object ("DUR", 0,0, E_CAMBIO.text,"CAMBIO" & l_desc.text, L_PROID.Text,clie_id, sDate & sTime, usuario,Starter.tipov,precio2))
				
				'Starter.skmt.ExecNonQuery2("update " & query & " set cat_gp_almacen = cat_gp_almacen - ? where cat_gp_id = ? ", Array As Object(E_CAMBIO.Text, L_PROID.Text))
				E_CAMBIO.Text = ""
				Panel1.Visible= False
				entro = "3"
				If BUSCA.Text = "" Then
					ListView1_ItemClick(1,V_ANT_SALIR)
				Else
					BUSCA_TextChanged(1, BUSCA.Text)
				End If
			End If
		End If
		If cantidad.Text = "" Or  cantidad.Text = "0.0" Or cantidad.Text = "0" Then
			Panel1.Visible= False
			entro = "3"
			If BUSCA.Text = "" Then
				ListView1_ItemClick(1,V_ANT_SALIR)
			Else
				BUSCA_TextChanged(1, BUSCA.Text)
			End If
		ELSE If cantidad.Text > l_bodega.Text Then
			Msgbox("Excede la existencia", "ADVERTENCIA") 'ignore
		Else If l_marca.text = "PROMOS" And l_tipo.Text ="PROMOS" And PROMO_C > 0 And cantidad.Text > 0 Then
			Starter.skmt.ExecNonQuery("delete from PROIDID")
			Starter.skmt.ExecNonQuery2("INSERT INTO PROIDID VALUES (?)", Array As Object(L_PROID.text))
			c=Starter.skmt.ExecQuery("Select count(*) as CUANTOSD from pedido where PE_CLIENTE IN (Select CUENTA from cuentaa) and length(pe_cedis) < 3  and  PE_COSTOU > 0 and pe_proid In (Select cat_dp_idprod from CAT_DETALLES_PAQ WHERE CAT_DP_ID In (Select PROIDID from PROIDID) AND CAT_DP_TIPO = 1)")
			c.Position =0
			If c.GetString("CUANTOSD") = 0 Then
				Msgbox("No tiene venta el cliente para la promo", "ADVERTENCIA") 'ignore
			Else
				c=Starter.skmt.ExecQuery("Select sum(pe_cant) As TOT_ART_PROMO, sum(PE_COSTO_TOT) as COSTO_TOT from pedido where PE_CLIENTE IN (Select CUENTA from cuentaa) and length(pe_cedis) < 3 and  PE_COSTOU > 0 and pe_proid In (Select cat_dp_idprod from CAT_DETALLES_PAQ WHERE CAT_DP_ID In (Select PROIDID from PROIDID) AND CAT_DP_TIPO = 1)")
				C.Position =0
				TOT_ART_PROMO = C.GetString("TOT_ART_PROMO")
				COSTO_TOT = C.GetString("COSTO_TOT")
				C.Close
				' se agrego and  PE_COSTOU > 0 para que no tome los cambios
				c=Starter.skmt.ExecQuery("Select count(*) as CUANTOSD from pedido where PE_CLIENTE IN (Select CUENTA from cuentaa) and length(pe_cedis) < 3 and  PE_COSTOU > 0 and pe_proid In (Select cat_dp_idprod from CAT_DETALLES_PAQ WHERE CAT_DP_ID In (Select PROIDID from PROIDID) AND (CAT_DP_PRECIO = 0.01 or CAT_DP_PRECIO = 1 OR CAT_DP_PRECIO = 0)  AND CAT_DP_TIPO = 1)")
				c.Position =0
				If c.GetString("CUANTOSD") = 0 Then
					TOT_ART_PROMOR = "0" 'Msgbox("No tiene venta el cliente para la promo", "ADVERTENCIA") 'ignore
				Else
					c=Starter.skmt.ExecQuery("Select sum(pe_cant) As TOT_ART_PROMO, sum(PE_COSTO_TOT) as COSTO_TOT from pedido where PE_CLIENTE IN (Select CUENTA from cuentaa) and length(pe_cedis) < 3  and  PE_COSTOU > 0 and pe_proid In (Select cat_dp_idprod from CAT_DETALLES_PAQ WHERE CAT_DP_ID In (Select PROIDID from PROIDID) AND (CAT_DP_PRECIO = 0.01 or CAT_DP_PRECIO = 1 OR CAT_DP_PRECIO = 0) AND CAT_DP_TIPO = 1)")
					C.Position =0
					TOT_ART_PROMOR = C.GetString("TOT_ART_PROMO")
				End If
				C.Close
				If PROMO_C = "" Then PROMO_C = "0"
				PROMO_C = PROMO_C * cantidad.Text
				If PROMO_CR = "" Then PROMO_CR = "0"
				PROMO_CR = PROMO_CR * cantidad.Text
				If  TOT_ART_PROMO = PROMO_C And TOT_ART_PROMOR = PROMO_CR Then
					Starter.skmt.ExecNonQuery2("UPDATE PEDIDO SET PE_CEDIS = (?) WHERE PE_CEDIS  and length(pe_cedis) < 3  and PE_CLIENTE IN (Select CUENTA from cuentaa) and pe_proid In (Select cat_dp_idprod from CAT_DETALLES_PAQ WHERE CAT_DP_ID In (Select PROIDID from PROIDID))", Array As Object(L_PROID.text))
					D=Starter.skmt.ExecQuery("select CAT_DP_ID, CAT_DP_PRECIO, CAT_DP_PRECIO_SIMPTOS, CAT_DP_IDPROD FROM CAT_DETALLES_PAQ WHERE CAT_DP_ID IN (Select PROIDID from PROIDID) AND CAT_DP_PRECIO > 1 AND CAT_DP_TIPO = 1") ' MODIFCADO 05/05/2021 PARA QUE NO TOME LOS CAMBIOS
					If D.RowCount>0 Then
						For i=0 To D.RowCount -1
							D.Position=i
							' se agrega esta linea para que no tome los cambios
							Starter.skmt.ExecNonQuery2("UPDATE PEDIDO SET PE_COSTOU = (?), PE_COSTO_TOT = PE_CANT * (?) , PE_COSTO_SIN = (?) WHERE PE_PROID = (?) And  PE_CLIENTE IN (Select CUENTA from cuentaa) and length(pe_cedis) < 3  and  PE_COSTOU > 0 and pe_proid In (Select cat_dp_idprod from CAT_DETALLES_PAQ WHERE CAT_DP_ID In (Select PROIDID from PROIDID) AND CAT_DP_TIPO = 1 AND CAT_DP_PRECIO > 1) ",Array As Object( D.GetString("CAT_DP_PRECIO"), D.GetString("CAT_DP_PRECIO"), D.GetString("CAT_DP_PRECIO_SIMPTOS"),D.GetString("CAT_DP_IDPROD")))
						Next
					End If
					D.Close
					D=Starter.skmt.ExecQuery("select CAT_DP_ID, CAT_DP_PRECIO, CAT_DP_PRECIO_SIMPTOS, CAT_DP_IDPROD FROM CAT_DETALLES_PAQ WHERE CAT_DP_ID IN (Select PROIDID from PROIDID) AND (CAT_DP_PRECIO = 0.01 or CAT_DP_PRECIO = 1 OR CAT_DP_PRECIO = 0) AND CAT_DP_TIPO = 1")
					If D.RowCount>0 Then
						For i=0 To D.RowCount -1
							D.Position=i
							Starter.skmt.ExecNonQuery2("UPDATE PEDIDO SET PE_COSTOU = (?), PE_COSTO_TOT = PE_CANT * (?) , PE_COSTO_SIN = (?) WHERE PE_PROID = (?) AND  PE_CLIENTE IN (Select CUENTA from cuentaa) and length(pe_cedis) < 3 and  PE_COSTOU > 0 and pe_proid In (Select cat_dp_idprod from CAT_DETALLES_PAQ WHERE CAT_DP_ID In (Select PROIDID from PROIDID) AND CAT_DP_TIPO = 1",Array As Object( D.GetString("CAT_DP_PRECIO"), D.GetString("CAT_DP_PRECIO"), D.GetString("CAT_DP_PRECIO_SIMPTOS"),D.GetString("CAT_DP_IDPROD")))
						Next
					End If
					D.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.Close
					c=Starter.skmt.ExecQuery("select CAT_CL_RUTA from kmt_info where CAT_CL_CODIGO In (Select cuenta from cuentaa)")
					cl_ruta = "0"
					If c.RowCount > 0 Then
						c.Position=0
						cl_ruta = c.GetString("CAT_CL_RUTA")
					End If
					c.Close
					c=Starter.skmt.ExecQuery("select USUARIO from usuarioa")
					usuario = ""
					If c.RowCount > 0 Then
						c.Position=0
						usuario = c.GetString("USUARIO")
					End If
					'para insertar la promo de ades el de regalo en 0
					e=Starter.skmt.ExecQuery("select count(*) as CUANTOS FROM CAT_DETALLES_PAQ WHERE CAT_DP_ID IN (Select PROIDID from PROIDID) and CAT_DP_TIPO = 0 ")
					e.Position =0
					If e.GetString("CUANTOS") > 0 Then
						F=Starter.skmt.ExecQuery("select CAT_DP_IDPROD, CAT_DP_PZAS,CAT_DP_PRECIO FROM CAT_DETALLES_PAQ WHERE CAT_DP_ID IN (Select PROIDID from PROIDID) and CAT_DP_TIPO = 0 ")
						'meter el otro
						If f.RowCount>0 Then
							For i=0 To f.RowCount -1
								f.Position=i
								'F.Position =0
								PRECIO_CERO = f.GetString("CAT_DP_PRECIO")
								h=Starter.skmt.ExecQuery2("select CAT_GP_NOMBRE from CAT_GUNAPROD2 where CAT_GP_ID = ? ", Array As String(f.GetString("CAT_DP_IDPROD")))
								h.Position=0
								Starter.skmt.ExecNonQuery2("INSERT INTO  PEDIDO (PE_CEDIS,PE_COSTO_TOT,PE_COSTOU,PE_CANT,PE_PRONOMBRE,PE_PROID,PE_CLIENTE,PE_FECHA,PE_USUARIO,PE_RUTA,PE_TIPO,PE_PRECIO2) VALUES(?,?,?,?,?,?,?,?,?,?,?,?) ", Array As Object (L_PROID.text,PRECIO_CERO *  f.GetString("CAT_DP_PZAS") * cantidad.text, PRECIO_CERO, f.GetString("CAT_DP_PZAS") * cantidad.text, h.GetString("CAT_GP_NOMBRE"), f.GetString("CAT_DP_IDPROD"),clie_id, sDate & sTime, usuario,cl_ruta,Starter.tipov,precio2))
								'ESTO LO CAMBIE POR QUE NO DESCUENTA EL INVENTARIO ABORDO CON LAS PROMOS
								Starter.skmt.ExecNonQuery2("update " & query & " set cat_gp_almacen = cat_gp_almacen - ? where cat_gp_id = ? ", Array As Object(f.GetString("CAT_DP_PZAS") * cantidad.text, f.GetString("CAT_DP_IDPROD")))
								h.Close
							Next
						End If
						f.Close
					End If
					e.Close
					Starter.skmt.ExecNonQuery2("INSERT INTO  PEDIDO (PE_CEDIS,PE_COSTO_TOT,PE_COSTOU,PE_CANT,PE_PRONOMBRE,PE_PROID,PE_CLIENTE,PE_FECHA,PE_USUARIO,PE_RUTA,PE_TIPO,PE_PRECIO2) VALUES(?,?,?,?,?,?,?,?,?,?,?,?) ", Array As Object (L_PROID.text,0, 0, cantidad.text, l_desc.text, L_PROID.Text,clie_id, sDate & sTime, usuario,cl_ruta,Starter.tipov,precio2))
					Starter.skmt.ExecNonQuery2("update " & query & " set cat_gp_almacen = cat_gp_almacen - ? where cat_gp_id = ? ", Array As Object(cantidad.Text, L_PROID.Text))
				Else
					Msgbox("Tiene " & TOT_ART_PROMO & " " & TOT_ART_PROMOR & " y necesita " & PROMO_C & " " & PROMO_CR & " Para la Promo. " , "ADVERTENCIA") 'ignore
				End If
			End If
			cantidad.Text = ""
			Panel1.Visible= False
			entro = "3"
			If BUSCA.Text = "" Then
				ListView1_ItemClick(1,V_ANT_SALIR)
			Else
				BUSCA_TextChanged(1, BUSCA.Text)
			End If
		
			''''' PROMOS QUE NO SON VARIABLE
		else If l_marca.text = "PROMOS" And l_tipo.Text ="PROMOS" And PROMO_C = 0 Then
			Starter.skmt.ExecNonQuery("delete from PROIDID")
			Starter.skmt.ExecNonQuery2("INSERT INTO PROIDID VALUES (?)", Array As Object(L_PROID.text))
			c=Starter.skmt.ExecQuery("Select CUENTA from cuentaa")
			c.Position=0
			clie_id = c.GetString("CUENTA")
			c.Close
			DateTime.DateFormat = "MM/dd/yyyy"
			sDate=DateTime.Date(DateTime.Now)
			sTime=DateTime.Time(DateTime.Now)
			c=Starter.skmt.ExecQuery("select CAT_CL_RUTA from kmt_info where CAT_CL_CODIGO In (Select cuenta from cuentaa)")
			c.Position=0
			cl_ruta = c.GetString("CAT_CL_RUTA")
			c.Close
			c=Starter.skmt.ExecQuery("select USUARIO from usuarioa")
			c.Position=0
			usuario = c.GetString("USUARIO")
			'para insertar la promo de ades el de regalo en 0
			e=Starter.skmt.ExecQuery("select count(*) as CUANTOS FROM CAT_DETALLES_PAQ WHERE CAT_DP_ID IN (Select PROIDID from PROIDID) ")
			e.Position =0
			If e.GetString("CUANTOS") > 0 Then
				F=Starter.skmt.ExecQuery("select CAT_DP_IDPROD, CAT_DP_PZAS, CAT_DP_PRECIO FROM CAT_DETALLES_PAQ WHERE CAT_DP_ID IN (Select PROIDID from PROIDID)")
				For i =0 To f.RowCount -1
					F.Position =i
					h=Starter.skmt.ExecQuery2("select CAT_GP_NOMBRE from " & query & " where CAT_GP_ID = ? ", Array As String(f.GetString("CAT_DP_IDPROD")))
					h.Position=0
					Starter.skmt.ExecNonQuery2("INSERT INTO  PEDIDO (PE_CEDIS,PE_COSTO_TOT,PE_COSTOU,PE_CANT,PE_PRONOMBRE,PE_PROID,PE_CLIENTE,PE_FECHA,PE_USUARIO,PE_RUTA,PE_TIPO,PE_PRECIO2) VALUES(?,?,?,?,?,?,?,?,?,?,?,?) ", Array As Object (L_PROID.text,f.GetString("CAT_DP_PRECIO") * cantidad.text * f.GetString("CAT_DP_PZAS"), f.GetString("CAT_DP_PRECIO"), f.GetString("CAT_DP_PZAS") * cantidad.text, h.GetString("CAT_GP_NOMBRE"), f.GetString("CAT_DP_IDPROD"),clie_id, sDate & sTime, usuario,cl_ruta,Starter.tipov,precio2))
					'ESTO LO CAMBIE POR QUE NO DESCUENTA EL INVENTARIO ABORDO CON LAS PROMOS
					Starter.skmt.ExecNonQuery2("update " & query & " set cat_gp_almacen = cat_gp_almacen - ? where cat_gp_id = ? ", Array As Object(f.GetString("CAT_DP_PZAS") * cantidad.text, f.GetString("CAT_DP_IDPROD")))
					h.Close
				Next
				f.Close
			End If
			e.Close
			Starter.skmt.ExecNonQuery2("INSERT INTO  PEDIDO (PE_CEDIS,PE_COSTO_TOT,PE_COSTOU,PE_CANT,PE_PRONOMBRE,PE_PROID,PE_CLIENTE,PE_FECHA,PE_USUARIO,PE_RUTA,PE_TIPO,PE_PRECIO2) VALUES(?,?,?,?,?,?,?,?,?,?,?,?) ", Array As Object (L_PROID.text,0, 0, cantidad.text, l_desc.text, L_PROID.Text,clie_id, sDate & sTime, usuario,cl_ruta,Starter.tipov,precio2))
			Starter.skmt.ExecNonQuery2("update " & query & " set cat_gp_almacen = cat_gp_almacen - ? where cat_gp_id = ? ", Array As Object(cantidad.Text, L_PROID.Text))
			cantidad.Text = ""
			c=Starter.skmt.ExecQuery("select sum(pe_costo_tot) as TOTAL_CLIE, SUM(PE_CANT) AS CANT_CLIE, SUM(PE_COSTO_SIN) AS TOTAL_CLIE_SIN 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,PC_ALMACEN,PC_RUTA,PC_COSTO_SIN) VALUES (?,?,?,?,?,?,?,?,?,?)", Array As Object(clie_id, sDate & sTime, usuario, c.GetString("CANT_CLIE"),c.GetString("TOTAL_CLIE"), Starter.lon_gps, Starter.lat_gps,ALMACEN,cl_ruta,c.GetString("TOTAL_CLIE_SIN")))
			Starter.skmt.ExecNonQuery("UPDATE kmt_info set gestion = 2  where CAT_CL_CODIGO In (select cuenta from cuentaa)")
			Panel1.Visible= False
			entro = "3"
			If BUSCA.Text = "" Then
				ListView1_ItemClick(1,V_ANT_SALIR)
			Else
				BUSCA_TextChanged(1, BUSCA.Text)
			End If
		Else
			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 CAT_CL_RUTA from kmt_info where CAT_CL_CODIGO In (Select cuenta from cuentaa)")
			c.Position=0
			cl_ruta = c.GetString("CAT_CL_RUTA")
			c.Close
			c=Starter.skmt.ExecQuery("select USUARIO from usuarioa")
			c.Position=0
			usuario = c.GetString("USUARIO")
			Starter.skmt.ExecNonQuery2("INSERT INTO  PEDIDO (PE_CEDIS,PE_COSTO_TOT,PE_COSTOU,PE_CANT,PE_PRONOMBRE,PE_PROID,PE_CLIENTE,PE_FECHA,PE_USUARIO,PE_RUTA,PE_COSTO_SIN,PE_TIPO,PE_PRECIO2) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?) ", Array As Object (ALMACEN,cantidad.Text * l_precio.Text, l_precio.text, cantidad.text, l_desc.text, L_PROID.Text,clie_id, sDate & sTime, usuario,cl_ruta,preciosin,Starter.tipov,precio2))
			Starter.skmt.ExecNonQuery2("update " & query & " set cat_gp_almacen = cat_gp_almacen - ? where cat_gp_id = ? ", Array As Object(cantidad.Text, L_PROID.Text))
			c=Starter.skmt.ExecQuery("select sum(pe_costo_tot) as TOTAL_CLIE, SUM(PE_CANT) AS CANT_CLIE, SUM(PE_COSTO_SIN) AS TOTAL_CLIE_SIN 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,PC_ALMACEN,PC_RUTA,PC_COSTO_SIN) VALUES (?,?,?,?,?,?,?,?,?,?)", Array As Object(clie_id, sDate & sTime, usuario, c.GetString("CANT_CLIE"),c.GetString("TOTAL_CLIE"), Starter.lon_gps, Starter.lat_gps, ALMACEN,cl_ruta,c.GetString("TOTAL_CLIE_SIN")))
			Starter.skmt.ExecNonQuery("UPDATE kmt_info set gestion = 2  where CAT_CL_CODIGO In (select cuenta from cuentaa)")
			Panel1.Visible= False
			entro = "3"
			If BUSCA.Text = "" Then
				ListView1_ItemClick(1,V_ANT_SALIR)
			Else
				BUSCA_TextChanged(1, BUSCA.Text)
			End If
			Starter.skmt.ExecNonQuery("delete from pedido3")
			Starter.skmt.ExecNonQuery("UPDATE PEDIDO SET PE_FOLIO = 0 where PE_FOLIO IS NULL")
			c=Starter.skmt.ExecQuery("SELECT SUM(PE_FOLIO) AS FOLIO FROM PEDIDO WHERE PE_CLIENTE IN (Select CUENTA from cuentaa)")
			c.Position = 0
			If c.GetString("FOLIO") <> Null And c.GetString("FOLIO") > 0 Then
				YA_IMPRIMIO = 1
			Else
				YA_IMPRIMIO = 0
			End If
			c.Close
			Starter.skmt.ExecNonQuery("INSERT INTO PEDIDO3 (PE_PRECIO2, PE_TIPO, PE_FECHA, PE_DESC, PE_COSTO_SIN, PE_RUTA, PE_CEDIS, PE_COSTO_TOT, PE_COSTOU, PE_CANT, PE_PRONOMBRE, PE_PROID, PE_CLIENTE, PE_USUARIO ) SELECT PE_PRECIO2, PE_TIPO, PE_FECHA, PE_DESC, PE_COSTO_SIN, PE_RUTA, PE_CEDIS, SUM(PE_COSTO_TOT) AS PE_COSTO_TOT, PE_COSTOU, SUM(PE_CANT) As PE_CANT, PE_PRONOMBRE, PE_PROID, PE_CLIENTE, PE_USUARIO FROM PEDIDO WHERE PE_CLIENTE IN (Select CUENTA from cuentaa) GROUP BY PE_PRECIO2, PE_TIPO, PE_DESC, PE_COSTO_SIN,	PE_RUTA, PE_CEDIS, PE_COSTOU, PE_PRONOMBRE,	PE_PROID, PE_CLIENTE ") 'Mod por CHV - 220302 - Agregué PE_FECHA al select/insert
			c=Starter.skmt.ExecQuery("select PE_PRECIO2,PE_TIPO,PE_DESC,PE_COSTO_SIN,PE_RUTA,PE_CEDIS,PE_COSTO_TOT,PE_COSTOU,PE_CANT,PE_PRONOMBRE,PE_PROID,PE_CLIENTE,	PE_FECHA,PE_USUARIO 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
					Starter.skmt.ExecNonQuery2("UPDATE PEDIDO3 SET PE_FECHA= ? WHERE PE_CLIENTE = ? AND PE_PRONOMBRE = ? AND PE_PROID = ?  AND PE_CEDIS = ? ", Array As Object(c.GetString("PE_FECHA"), c.GetString("PE_CLIENTE"),c.GetString("PE_PRONOMBRE"),c.GetString("PE_PROID"),c.GetString("PE_CEDIS")))
				Next
			End If
			c.Close
			Starter.skmt.ExecNonQuery2("UPDATE PEDIDO3 SET PE_FOLIO = ? WHERE PE_CLIENTE IN (Select CUENTA from cuentaa)", Array As Object(YA_IMPRIMIO))
			Starter.skmt.ExecNonQuery("delete from pedido WHERE PE_CLIENTE IN (Select CUENTA from cuentaa) ")
			Starter.skmt.ExecNonQuery("INSERT INTO PEDIDO SELECT * FROM PEDIDO3")
			'StartActivity(fila)
		End If
		Starter.skmt.ExecNonQuery("delete from pedido3")
		Starter.skmt.ExecNonQuery("UPDATE PEDIDO SET PE_FOLIO = 0 where PE_FOLIO IS NULL")
		c=Starter.skmt.ExecQuery("SELECT SUM(PE_FOLIO) AS FOLIO FROM PEDIDO WHERE PE_CLIENTE IN (Select CUENTA from cuentaa)")
		c.Position = 0
		If c.GetString("FOLIO") <> Null And c.GetString("FOLIO") > 0 Then
			YA_IMPRIMIO = 1
		Else
			YA_IMPRIMIO = 0
		End If
		c.Close
		Starter.skmt.ExecNonQuery("INSERT INTO PEDIDO3 (PE_PRECIO2, PE_TIPO, PE_FECHA, PE_DESC, PE_COSTO_SIN, PE_RUTA, PE_CEDIS, PE_COSTO_TOT, PE_COSTOU, PE_CANT, PE_PRONOMBRE,	PE_PROID, PE_CLIENTE, PE_USUARIO ) SELECT PE_PRECIO2, PE_TIPO, PE_FECHA, PE_DESC, PE_COSTO_SIN, PE_RUTA, PE_CEDIS, SUM(PE_COSTO_TOT) AS PE_COSTO_TOT, PE_COSTOU, SUM(PE_CANT) As PE_CANT, PE_PRONOMBRE,	PE_PROID, PE_CLIENTE, PE_USUARIO FROM PEDIDO WHERE PE_CLIENTE IN (Select CUENTA from cuentaa) GROUP BY PE_PRECIO2, PE_TIPO,	PE_DESC, PE_COSTO_SIN, PE_RUTA, PE_CEDIS, PE_COSTOU, PE_PRONOMBRE, PE_PROID, PE_CLIENTE ") 'Mod por CHV - 220302 - Agregue PE_FECHA al select/insert
		c=Starter.skmt.ExecQuery("select PE_PRECIO2,PE_TIPO,PE_DESC,PE_COSTO_SIN,PE_RUTA,PE_CEDIS,PE_COSTO_TOT,PE_COSTOU,PE_CANT,PE_PRONOMBRE,PE_PROID,PE_CLIENTE,	PE_FECHA,PE_USUARIO 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
				Starter.skmt.ExecNonQuery2("UPDATE PEDIDO3 SET PE_FECHA= ? WHERE PE_CLIENTE = ? AND PE_PRONOMBRE = ? AND PE_PROID = ?  AND PE_CEDIS = ? ", Array As Object(c.GetString("PE_FECHA"), c.GetString("PE_CLIENTE"),c.GetString("PE_PRONOMBRE"),c.GetString("PE_PROID"),c.GetString("PE_CEDIS")))
			Next
		End If
		c.Close
		Starter.skmt.ExecNonQuery2("UPDATE PEDIDO3 SET PE_FOLIO = ? WHERE PE_CLIENTE IN (Select CUENTA from cuentaa)", Array As Object(YA_IMPRIMIO))
		Starter.skmt.ExecNonQuery("delete from pedido WHERE PE_CLIENTE IN (Select CUENTA from cuentaa) ")
		Starter.skmt.ExecNonQuery("INSERT INTO PEDIDO SELECT * FROM PEDIDO3")
	Else
		Log($"${cuenta},${limite_actual > limite_abordo},${cantidad.text},${B4XPages.MainPage.cliente.CREDITO_DISPONIBLE},${limite_actual},${limite_abordo}"$)
		Msgbox("EXCEDE EL LIMITE DE CREDITO","AVISO") 'ignore
	End If
	Starter.skmt.ExecNonQuery("delete from pedido3")
	Starter.skmt.ExecNonQuery("UPDATE PEDIDO SET PE_FOLIO = 0 where PE_FOLIO IS NULL")
	If cuenta <> "0" Then
		c=Starter.skmt.ExecQuery("SELECT SUM(PE_FOLIO) AS FOLIO FROM PEDIDO WHERE PE_CLIENTE IN (Select CUENTA from cuentaa)")
		c.Position = 0
		If c.GetString("FOLIO") <> Null And c.GetString("FOLIO") > 0 Then
			YA_IMPRIMIO = 1
		Else
			YA_IMPRIMIO = 0
		End If
		c.Close
	End If
	Starter.skmt.ExecNonQuery("INSERT INTO PEDIDO3 (PE_PRECIO2, PE_TIPO, PE_FECHA, PE_DESC, PE_COSTO_SIN, PE_RUTA, PE_CEDIS, PE_COSTO_TOT, PE_COSTOU, PE_CANT, PE_PRONOMBRE, PE_PROID, PE_CLIENTE, PE_USUARIO ) SELECT PE_PRECIO2, 	PE_TIPO, PE_FECHA, PE_DESC, PE_COSTO_SIN, PE_RUTA, PE_CEDIS, SUM(PE_COSTO_TOT) AS PE_COSTO_TOT, PE_COSTOU, SUM(PE_CANT) As PE_CANT, PE_PRONOMBRE, PE_PROID,	PE_CLIENTE, PE_USUARIO FROM PEDIDO WHERE PE_CLIENTE IN (Select CUENTA from cuentaa) GROUP BY PE_PRECIO2,	PE_TIPO,	PE_DESC,	PE_COSTO_SIN,	PE_RUTA,	PE_CEDIS,	PE_COSTOU, PE_PRONOMBRE,	PE_PROID, PE_CLIENTE ") 'Mod por CHV - 220302 - Agregué PE_FECHA al select/insert
	c=Starter.skmt.ExecQuery("select PE_PRECIO2,PE_TIPO,PE_DESC,PE_COSTO_SIN,PE_RUTA,PE_CEDIS,PE_COSTO_TOT,PE_COSTOU,PE_CANT,PE_PRONOMBRE,PE_PROID,PE_CLIENTE,	PE_FECHA,PE_USUARIO 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
			Starter.skmt.ExecNonQuery2("UPDATE PEDIDO3 SET PE_FECHA= ? WHERE PE_CLIENTE = ? AND PE_PRONOMBRE = ? AND PE_PROID = ?  AND PE_CEDIS = ? ", Array As Object(c.GetString("PE_FECHA"), c.GetString("PE_CLIENTE"),c.GetString("PE_PRONOMBRE"),c.GetString("PE_PROID"),c.GetString("PE_CEDIS")))
		Next
	End If
	c.Close
	Starter.skmt.ExecNonQuery2("UPDATE PEDIDO3 SET PE_FOLIO = ? WHERE PE_CLIENTE IN (Select CUENTA from cuentaa)", Array As Object(YA_IMPRIMIO))
	Starter.skmt.ExecNonQuery("delete from pedido WHERE PE_CLIENTE IN (Select CUENTA from cuentaa) ")
	Starter.skmt.ExecNonQuery("INSERT INTO PEDIDO SELECT * FROM PEDIDO3")
	ListView1.Visible = True
	Starter.skmt.ExecNonQuery2("UPDATE PEDIDO SET PE_FECHA = ?  WHERE PE_CLIENTE IN (Select CUENTA from cuentaa) and PE_FECHA IS NULL", Array As Object(sDate & sTime))
	guardar.Enabled = True
End Sub

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

Sub Terminar_Click
	B_MASC.Enabled = True
	B_MENOSC.Enabled = True
	E_CAMBIO.Enabled = True
	Terminar.Enabled = False
	Panel1.Visible=False
	If cantidad.Text = "" Then
		cantidad.Text = 0
	End If
	If Starter.lat_gps = 0 Or Starter.lat_gps = Null Then
		Private coords As List = Subs.traeCoordsDeBD
		Starter.lat_gps = coords.Get(0)
		Starter.lon_gps = coords.Get(1)
	End If
	limite_actual = t_venta.Text + (cantidad.Text * l_precio.Text)
	Log($"Limite actual=${limite_actual}"$)
	If cuenta = "0" Then
		limite_actual = limite_actual + limite_anterior
	End If
	Log($"Limite actual=${limite_actual}"$)
	If limite_abordo = "" Then limite_abordo = 0 'Mod por CHV - 211213 - Para que si limite_abordo no tiene valor, no nos saque de la app.
	If cuenta ="0" And limite_actual > limite_abordo And cantidad.Text <> 0 And cantidad.Text <> "" Then
		Msgbox("excede el limite abordo","limite") 'ignore
	Else IF (cuenta <> "0" And limite_actual < B4XPages.MainPage.cliente.CREDITO_DISPONIBLE) Or (cuenta ="0" And limite_actual < limite_abordo) Then
		If E_CAMBIO.Text = "" Or E_CAMBIO.Text = "0" Then
			Panel1.Visible= False
		Else
			If E_CAMBIO.Text = "" Then E_CAMBIO.Text = "0"
			If E_CAMBIO.Text * l_precio.Text  > t_venta.Text Then
				Msgbox("Excede EL MONTO DE LA VENTA", "ADVERTENCIA") 'ignore
			else if  E_CAMBIO.Text + cantidad.Text > l_bodega.Text Then
				Msgbox("Excede existencia", "ADVERTENCIA") 'ignore
			Else
				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")
				'' cambio para el rmi con costo fl 11-05-2023
				Starter.skmt.ExecNonQuery2("INSERT INTO PEDIDO (PE_CEDIS,PE_COSTO_TOT,PE_COSTOU,PE_CANT,PE_PRONOMBRE,PE_PROID,PE_CLIENTE,PE_FECHA,PE_USUARIO,PE_TIPO,PE_PRECIO2) VALUES(?,?,?,?,?,?,?,?,?,?,?) ", Array As Object ("DUR",E_CAMBIO.text * l_precio.Text * (-1),l_precio.Text, E_CAMBIO.text,"CAMBIO" & l_desc.text, L_PROID.Text,clie_id, sDate & sTime, usuario,Starter.tipov,precio2))
				''				Starter.skmt.ExecNonQuery2("INSERT INTO PEDIDO (PE_CEDIS,PE_COSTO_TOT,PE_COSTOU,PE_CANT,PE_PRONOMBRE,PE_PROID,PE_CLIENTE,PE_FECHA,PE_USUARIO,PE_TIPO,PE_PRECIO2) VALUES(?,?,?,?,?,?,?,?,?,?,?) ", Array As Object ("DUR",0,0, E_CAMBIO.text,"CAMBIO" & l_desc.text, L_PROID.Text,clie_id, sDate & sTime, usuario,Starter.tipov,precio2))
				
				'Starter.skmt.ExecNonQuery2("update " & query & " set cat_gp_almacen = cat_gp_almacen - ? where cat_gp_id = ? ", Array As Object(E_CAMBIO.Text, L_PROID.Text))
				E_CAMBIO.Text = ""
				Panel1.Visible= False
			End If
		End If
		If cantidad.Text = "" Or  cantidad.Text = "0.0" Or cantidad.Text = "0" Then
			B4XPages.ShowPage("Cliente")
		ELSE If cantidad.Text > l_bodega.Text Then
			Msgbox("Excede la existencia", "ADVERTENCIA") 'ignore
		Else If l_marca.text = "PROMOS" And l_tipo.Text ="PROMOS" And PROMO_C > 0 And cantidad.Text > 0 Then
			Starter.skmt.ExecNonQuery("delete from PROIDID")
			Starter.skmt.ExecNonQuery2("INSERT INTO PROIDID VALUES (?)", Array As Object(L_PROID.text))
			c=Starter.skmt.ExecQuery("Select count(*) as CUANTOSD from pedido where PE_CLIENTE IN (Select CUENTA from cuentaa) and length(pe_cedis) < 3  and  PE_COSTOU > 0 and pe_proid In (Select cat_dp_idprod from CAT_DETALLES_PAQ WHERE CAT_DP_ID In (Select PROIDID from PROIDID) AND CAT_DP_TIPO = 1)")
			c.Position =0
			If c.GetString("CUANTOSD") = 0 Then
				Msgbox("No tiene venta el cliente para la promo", "ADVERTENCIA") 'ignore
			Else
				c=Starter.skmt.ExecQuery("Select sum(pe_cant) As TOT_ART_PROMO, sum(PE_COSTO_TOT) as COSTO_TOT from pedido where PE_CLIENTE IN (Select CUENTA from cuentaa) and length(pe_cedis) < 3  and  PE_COSTOU > 0 and pe_proid In (Select cat_dp_idprod from CAT_DETALLES_PAQ WHERE CAT_DP_ID In (Select PROIDID from PROIDID) AND CAT_DP_TIPO = 1)")
				TOT_ART_PROMO = 0
				COSTO_TOT = 0
				If c.RowCount > 0 Then
					c.Position =0
					TOT_ART_PROMO = c.GetString("TOT_ART_PROMO")
					COSTO_TOT = c.GetString("COSTO_TOT")
				End If
				C.Close
				' Se agrego and  PE_COSTOU > 0 para que no tome los cambios
				c=Starter.skmt.ExecQuery("Select count(*) as CUANTOSD from pedido where PE_CLIENTE IN (Select CUENTA from cuentaa) and length(pe_cedis) < 3  and  PE_COSTOU > 0 and pe_proid In (Select cat_dp_idprod from CAT_DETALLES_PAQ WHERE CAT_DP_ID In (Select PROIDID from PROIDID) AND (CAT_DP_PRECIO = 0.01 or CAT_DP_PRECIO = 1 OR CAT_DP_PRECIO = 0)  AND CAT_DP_TIPO = 1)")
				c.Position =0
				If c.GetString("CUANTOSD") = 0 Then
					TOT_ART_PROMOR = "0" 'Msgbox("No tiene venta el cliente para la promo", "ADVERTENCIA") 'ignore
				Else
					c=Starter.skmt.ExecQuery("Select sum(pe_cant) As TOT_ART_PROMO, sum(PE_COSTO_TOT) as COSTO_TOT from pedido where PE_CLIENTE IN (Select CUENTA from cuentaa) and length(pe_cedis) < 3  and  PE_COSTOU > 0 and pe_proid In (Select cat_dp_idprod from CAT_DETALLES_PAQ WHERE CAT_DP_ID In (Select PROIDID from PROIDID) AND (CAT_DP_PRECIO = 0.01 or CAT_DP_PRECIO = 1 OR CAT_DP_PRECIO = 0) AND CAT_DP_TIPO = 1)")
					TOT_ART_PROMOR = 0
					If c.RowCount > 0 Then
						C.Position =0
						TOT_ART_PROMOR = C.GetString("TOT_ART_PROMO")
					End If
				End If
				C.Close
				If PROMO_C = "" Then PROMO_C = "0"
				PROMO_C = PROMO_C * cantidad.Text
				If PROMO_CR = "" Then PROMO_CR = "0"
				PROMO_CR = PROMO_CR * cantidad.Text
				If  TOT_ART_PROMO = PROMO_C And TOT_ART_PROMOR = PROMO_CR Then
					' anterior a 29/06/2015
					Starter.skmt.ExecNonQuery2("UPDATE PEDIDO SET PE_CEDIS = (?) WHERE length(pe_cedis) < 3  and PE_CLIENTE IN (Select CUENTA from cuentaa) and pe_proid In (Select cat_dp_idprod from CAT_DETALLES_PAQ WHERE CAT_DP_ID In (Select PROIDID from PROIDID))", Array As Object(L_PROID.text))
					D=Starter.skmt.ExecQuery("select CAT_DP_ID, CAT_DP_PRECIO, CAT_DP_PRECIO_SIMPTOS, CAT_DP_IDPROD FROM CAT_DETALLES_PAQ WHERE CAT_DP_ID IN (Select PROIDID from PROIDID) AND CAT_DP_PRECIO > 1 AND CAT_DP_TIPO = 1") ' MODIFCADO 05/05/2021 PARA QUE NO TOME LOS CAMBIOS
					If D.RowCount>0 Then
						For i=0 To D.RowCount -1
							D.Position=i
							' se agrega esta linea para que no tome los cambios
							Starter.skmt.ExecNonQuery2("UPDATE PEDIDO SET PE_COSTOU = (?), PE_COSTO_TOT = PE_CANT * (?) , PE_COSTO_SIN = (?) WHERE PE_PROID = (?) And  PE_CLIENTE IN (Select CUENTA from cuentaa) and length(pe_cedis) < 3  and  PE_COSTOU > 0 and pe_proid In (Select cat_dp_idprod from CAT_DETALLES_PAQ WHERE CAT_DP_ID In (Select PROIDID from PROIDID) AND CAT_DP_TIPO = 1 AND CAT_DP_PRECIO > 1) ",Array As Object( D.GetString("CAT_DP_PRECIO"), D.GetString("CAT_DP_PRECIO"), D.GetString("CAT_DP_PRECIO_SIMPTOS"),D.GetString("CAT_DP_IDPROD")))
						Next
					End If
					D.Close
					D=Starter.skmt.ExecQuery("select CAT_DP_ID, CAT_DP_PRECIO, CAT_DP_PRECIO_SIMPTOS, CAT_DP_IDPROD FROM CAT_DETALLES_PAQ WHERE CAT_DP_ID IN (Select PROIDID from PROIDID) AND (CAT_DP_PRECIO = 0.01 or CAT_DP_PRECIO = 1 OR CAT_DP_PRECIO = 0) AND CAT_DP_TIPO = 1")
					If D.RowCount>0 Then
						For i=0 To D.RowCount -1
							D.Position=i
							Starter.skmt.ExecNonQuery2("UPDATE PEDIDO SET PE_COSTOU = (?), PE_COSTO_TOT = PE_CANT * (?) , PE_COSTO_SIN = (?) WHERE PE_PROID = (?) AND  PE_CLIENTE IN (Select CUENTA from cuentaa) and length(pe_cedis) < 3 and  PE_COSTOU > 0 and pe_proid In (Select cat_dp_idprod from CAT_DETALLES_PAQ WHERE CAT_DP_ID In (Select PROIDID from PROIDID) AND CAT_DP_TIPO = 1",Array As Object( D.GetString("CAT_DP_PRECIO"), D.GetString("CAT_DP_PRECIO"), D.GetString("CAT_DP_PRECIO_SIMPTOS"),D.GetString("CAT_DP_IDPROD")))
						Next
					End If
					D.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.Close
					c=Starter.skmt.ExecQuery("select CAT_CL_RUTA from kmt_info where CAT_CL_CODIGO In (Select cuenta from cuentaa)")
					cl_ruta = 0
					If c.RowCount > 0 Then
						c.Position=0
						cl_ruta = c.GetString("CAT_CL_RUTA")
					End If
					c.Close
					c=Starter.skmt.ExecQuery("select USUARIO from usuarioa")
					usuario = "0"
					If c.RowCount > 0 Then
						c.Position=0
						usuario = c.GetString("USUARIO")
					End If
					'para insertar la promo de ades el de regalo en 0
					e=Starter.skmt.ExecQuery("select count(*) as CUANTOS FROM CAT_DETALLES_PAQ WHERE CAT_DP_ID IN (Select PROIDID from PROIDID) and CAT_DP_TIPO = 0 ")
					e.Position =0
					If e.GetString("CUANTOS") > 0 Then
						F=Starter.skmt.ExecQuery("select CAT_DP_IDPROD, CAT_DP_PZAS,CAT_DP_PRECIO FROM CAT_DETALLES_PAQ WHERE CAT_DP_ID IN (Select PROIDID from PROIDID) and CAT_DP_TIPO = 0 ")
						'meter el otro
						If f.RowCount>0 Then
							For i=0 To f.RowCount -1
								f.Position=i
								PRECIO_CERO = f.GetString("CAT_DP_PRECIO")
								h=Starter.skmt.ExecQuery2("select CAT_GP_NOMBRE from CAT_GUNAPROD2 where CAT_GP_ID = ? ", Array As String(f.GetString("CAT_DP_IDPROD")))
								h.Position=0
								Starter.skmt.ExecNonQuery2("INSERT INTO  PEDIDO (PE_CEDIS,PE_COSTO_TOT,PE_COSTOU,PE_CANT,PE_PRONOMBRE,PE_PROID,PE_CLIENTE,PE_FECHA,PE_USUARIO,PE_RUTA,PE_TIPO,PE_PRECIO2) VALUES(?,?,?,?,?,?,?,?,?,?,?,?) ", Array As Object (L_PROID.text,PRECIO_CERO *  f.GetString("CAT_DP_PZAS") * cantidad.text, PRECIO_CERO, f.GetString("CAT_DP_PZAS") * cantidad.text, h.GetString("CAT_GP_NOMBRE"), f.GetString("CAT_DP_IDPROD"),clie_id, sDate & sTime, usuario,cl_ruta,Starter.tipov,precio2))
								'ESTO LO CAMBIE POR QUE NO DESCUENTA EL INVENTARIO ABORDO CON LAS PROMOS
								Starter.skmt.ExecNonQuery2("update " & query & " set cat_gp_almacen = cat_gp_almacen - ? where cat_gp_id = ? ", Array As Object(f.GetString("CAT_DP_PZAS") * cantidad.text, f.GetString("CAT_DP_IDPROD")))
								h.Close
							Next
						End If
						f.Close
					End If
					e.Close
					Starter.skmt.ExecNonQuery2("INSERT INTO  PEDIDO (PE_CEDIS,PE_COSTO_TOT,PE_COSTOU,PE_CANT,PE_PRONOMBRE,PE_PROID,PE_CLIENTE,PE_FECHA,PE_USUARIO,PE_RUTA,PE_TIPO,PE_PRECIO2) VALUES(?,?,?,?,?,?,?,?,?,?,?,?) ", Array As Object (L_PROID.text,0, 0, cantidad.text, l_desc.text, L_PROID.Text,clie_id, sDate & sTime, usuario,cl_ruta,Starter.tipov,precio2))
					Starter.skmt.ExecNonQuery2("update " & query & " set cat_gp_almacen = cat_gp_almacen - ? where cat_gp_id = ? ", Array As Object(cantidad.Text, L_PROID.Text))
				Else
					Msgbox("Tiene " & TOT_ART_PROMO & " " & TOT_ART_PROMOR & " y necesita " & PROMO_C & " " & PROMO_CR & " Para la Promo. " , "ADVERTENCIA") 'ignore
				End If
			End If
			cantidad.Text = ""
			Panel1.Visible= False
			Subs.iniciaActividad("Cliente")
		else If l_marca.text = "PROMOS" And l_tipo.Text ="PROMOS" And PROMO_C = 0 Then
			Starter.skmt.ExecNonQuery("delete from PROIDID")
			Starter.skmt.ExecNonQuery2("INSERT INTO PROIDID VALUES (?)", Array As Object(L_PROID.text))
			c=Starter.skmt.ExecQuery("Select CUENTA from cuentaa")
			clie_id = "0"
			If c.RowCount > 0 Then
				c.Position=0
				clie_id = c.GetString("CUENTA")
			End If
			c.Close
			DateTime.DateFormat = "MM/dd/yyyy"
			sDate=DateTime.Date(DateTime.Now)
			sTime=DateTime.Time(DateTime.Now)
 
			c=Starter.skmt.ExecQuery("select CAT_CL_RUTA from kmt_info where CAT_CL_CODIGO In (Select cuenta from cuentaa)")
			c.Position=0
			cl_ruta = c.GetString("CAT_CL_RUTA")
			c.Close
		
			c=Starter.skmt.ExecQuery("select USUARIO from usuarioa")
			c.Position=0
			usuario = c.GetString("USUARIO")
			'para insertar la promo de ades el de regalo en 0
			e=Starter.skmt.ExecQuery("select count(*) as CUANTOS FROM CAT_DETALLES_PAQ WHERE CAT_DP_ID IN (Select PROIDID from PROIDID) ")
			e.Position =0
			If e.GetString("CUANTOS") > 0 Then
				F=Starter.skmt.ExecQuery("select CAT_DP_IDPROD, CAT_DP_PZAS, CAT_DP_PRECIO FROM CAT_DETALLES_PAQ WHERE CAT_DP_ID IN (Select PROIDID from PROIDID)")
				For i =0 To f.RowCount -1
					F.Position =i
					h=Starter.skmt.ExecQuery2("select CAT_GP_NOMBRE from " & query & " where CAT_GP_ID = ? ", Array As String(f.GetString("CAT_DP_IDPROD")))
					h.Position=0
					Starter.skmt.ExecNonQuery2("INSERT INTO  PEDIDO (PE_CEDIS,PE_COSTO_TOT,PE_COSTOU,PE_CANT,PE_PRONOMBRE,PE_PROID,PE_CLIENTE,PE_FECHA,PE_USUARIO,PE_RUTA,PE_TIPO,PE_PRECIO2) VALUES(?,?,?,?,?,?,?,?,?,?,?,?) ", Array As Object (L_PROID.text,f.GetString("CAT_DP_PRECIO") * cantidad.text * f.GetString("CAT_DP_PZAS"), f.GetString("CAT_DP_PRECIO"), f.GetString("CAT_DP_PZAS") * cantidad.text, h.GetString("CAT_GP_NOMBRE"), f.GetString("CAT_DP_IDPROD"),clie_id, sDate & sTime, usuario,cl_ruta,Starter.tipov,precio2))
					'ESTO LO CAMBIE POR QUE NO DESCUENTA EL INVENTARIO ABORDO CON LAS PROMOS
					Starter.skmt.ExecNonQuery2("update " & query & " set cat_gp_almacen = cat_gp_almacen - ? where cat_gp_id = ? ", Array As Object(f.GetString("CAT_DP_PZAS") * cantidad.text, f.GetString("CAT_DP_IDPROD")))
					h.Close
				Next
				f.Close
			End If
			e.Close
			Starter.skmt.ExecNonQuery2("INSERT INTO  PEDIDO (PE_CEDIS,PE_COSTO_TOT,PE_COSTOU,PE_CANT,PE_PRONOMBRE,PE_PROID,PE_CLIENTE,PE_FECHA,PE_USUARIO,PE_RUTA,PE_TIPO,PE_PRECIO2) VALUES(?,?,?,?,?,?,?,?,?,?,?,?) ", Array As Object (L_PROID.text,0, 0, cantidad.text, l_desc.text, L_PROID.Text,clie_id, sDate & sTime, usuario,cl_ruta,Starter.tipov,precio2))
			Starter.skmt.ExecNonQuery2("update " & query & " set cat_gp_almacen = cat_gp_almacen - ? where cat_gp_id = ? ", Array As Object(cantidad.Text, L_PROID.Text))
			cantidad.Text = ""
			c=Starter.skmt.ExecQuery("select sum(pe_costo_tot) as TOTAL_CLIE, SUM(PE_CANT) AS CANT_CLIE, SUM(PE_COSTO_SIN) AS TOTAL_CLIE_SIN 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,PC_ALMACEN,PC_RUTA,PC_COSTO_SIN) VALUES (?,?,?,?,?,?,?,?,?,?)", Array As Object(clie_id, sDate & sTime, usuario, c.GetString("CANT_CLIE"),c.GetString("TOTAL_CLIE"), Starter.lon_gps, Starter.lat_gps, ALMACEN,cl_ruta,c.GetString("TOTAL_CLIE_SIN")))
			Starter.skmt.ExecNonQuery("UPDATE kmt_info set gestion = 2  where CAT_CL_CODIGO In (select cuenta from cuentaa)")
			Subs.iniciaActividad("Cliente")
		Else
			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 CAT_CL_RUTA from kmt_info where CAT_CL_CODIGO In (Select cuenta from cuentaa)")
			c.Position=0
			cl_ruta = c.GetString("CAT_CL_RUTA")
			c.Close
		
			c=Starter.skmt.ExecQuery("select USUARIO from usuarioa")
			c.Position=0
			usuario = c.GetString("USUARIO")
	
			Starter.skmt.ExecNonQuery2("INSERT INTO  PEDIDO (PE_CEDIS,PE_COSTO_TOT,PE_COSTOU,PE_CANT,PE_PRONOMBRE,PE_PROID,PE_CLIENTE,PE_FECHA,PE_USUARIO,PE_RUTA,PE_COSTO_SIN,PE_TIPO,PE_PRECIO2) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?) ", Array As Object (ALMACEN,cantidad.Text * l_precio.Text, l_precio.text, cantidad.text, l_desc.text, L_PROID.Text,clie_id, sDate & sTime, usuario,cl_ruta,preciosin,Starter.tipov,precio2))
			Starter.skmt.ExecNonQuery2("update " & query & " set cat_gp_almacen = cat_gp_almacen - ? where cat_gp_id = ? ", Array As Object(cantidad.Text, L_PROID.Text))
			c=Starter.skmt.ExecQuery("select sum(pe_costo_tot) as TOTAL_CLIE, SUM(PE_CANT) AS CANT_CLIE, SUM(PE_COSTO_SIN) AS TOTAL_CLIE_SIN 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,PC_ALMACEN,PC_RUTA,PC_COSTO_SIN) VALUES (?,?,?,?,?,?,?,?,?,?)", Array As Object(clie_id, sDate & sTime, usuario, c.GetString("CANT_CLIE"),c.GetString("TOTAL_CLIE"), Starter.lon_gps, Starter.lat_gps, ALMACEN,cl_ruta,c.GetString("TOTAL_CLIE_SIN")))
			Starter.skmt.ExecNonQuery("UPDATE kmt_info set gestion = 2  where CAT_CL_CODIGO In (select cuenta from cuentaa)")
			Panel1.Visible= False
		
			Starter.skmt.ExecNonQuery("delete from pedido3")
			Starter.skmt.ExecNonQuery("UPDATE PEDIDO SET PE_FOLIO = 0 where PE_FOLIO IS NULL")
			c=Starter.skmt.ExecQuery("SELECT SUM(PE_FOLIO) AS FOLIO FROM PEDIDO WHERE PE_CLIENTE IN (Select CUENTA from cuentaa)")
			c.Position = 0
			If c.GetString("FOLIO") <> Null And c.GetString("FOLIO") > 0 Then
				YA_IMPRIMIO = 1
			Else
				YA_IMPRIMIO = 0
			End If
			c.Close
			Starter.skmt.ExecNonQuery("INSERT INTO PEDIDO3 (PE_PRECIO2,	PE_TIPO,	PE_DESC,	PE_COSTO_SIN,	PE_RUTA,	PE_CEDIS,	 PE_COSTO_TOT,	 PE_COSTOU,	 PE_CANT,	PE_PRONOMBRE,	PE_PROID,	PE_CLIENTE,	PE_USUARIO ) SELECT PE_PRECIO2,	PE_TIPO, PE_DESC, PE_COSTO_SIN,	PE_RUTA, PE_CEDIS,	SUM(PE_COSTO_TOT) AS PE_COSTO_TOT,	 PE_COSTOU,	SUM(PE_CANT) As PE_CANT,	PE_PRONOMBRE,	PE_PROID,	PE_CLIENTE,		PE_USUARIO FROM PEDIDO WHERE PE_CLIENTE IN (Select CUENTA from cuentaa) GROUP BY PE_PRECIO2,	PE_TIPO,	PE_DESC,	PE_COSTO_SIN,	PE_RUTA,	PE_CEDIS,	PE_COSTOU, PE_PRONOMBRE,	PE_PROID, PE_CLIENTE ")
			c=Starter.skmt.ExecQuery("select PE_PRECIO2,PE_TIPO,PE_DESC,PE_COSTO_SIN,PE_RUTA,PE_CEDIS,PE_COSTO_TOT,PE_COSTOU,PE_CANT,PE_PRONOMBRE,PE_PROID,PE_CLIENTE,	PE_FECHA,PE_USUARIO 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
					Starter.skmt.ExecNonQuery2("UPDATE PEDIDO3 SET PE_FECHA= ? WHERE PE_CLIENTE = ? AND PE_PRONOMBRE = ? AND PE_PROID = ?  AND PE_CEDIS = ? ", Array As Object(c.GetString("PE_FECHA"), c.GetString("PE_CLIENTE"),c.GetString("PE_PRONOMBRE"),c.GetString("PE_PROID"),c.GetString("PE_CEDIS")))
				Next
			End If
			c.Close
			
			Starter.skmt.ExecNonQuery2("UPDATE PEDIDO3 SET PE_FOLIO = ? WHERE PE_CLIENTE IN (Select CUENTA from cuentaa)", Array As Object(YA_IMPRIMIO))
			Starter.skmt.ExecNonQuery("delete from pedido WHERE PE_CLIENTE IN (Select CUENTA from cuentaa) ")
			Starter.skmt.ExecNonQuery("INSERT INTO PEDIDO SELECT * FROM PEDIDO3")
			Subs.iniciaActividad("Cliente")
		End If
		Starter.skmt.ExecNonQuery("delete from pedido3")
		Starter.skmt.ExecNonQuery("UPDATE PEDIDO SET PE_FOLIO = 0 where PE_FOLIO IS NULL")
		c=Starter.skmt.ExecQuery("SELECT SUM(PE_FOLIO) AS FOLIO FROM PEDIDO WHERE PE_CLIENTE IN (Select CUENTA from cuentaa)")
		c.Position = 0
		If c.GetString("FOLIO") <> Null And c.GetString("FOLIO") > 0 Then
			YA_IMPRIMIO = 1
		Else
			YA_IMPRIMIO = 0
		End If
		c.Close
		Starter.skmt.ExecNonQuery("INSERT INTO PEDIDO3 (PE_PRECIO2,	PE_TIPO,	PE_DESC,	PE_COSTO_SIN,	PE_RUTA,	PE_CEDIS,	 PE_COSTO_TOT,	 PE_COSTOU,	 PE_CANT,	PE_PRONOMBRE,	PE_PROID,	PE_CLIENTE,	PE_USUARIO ) SELECT PE_PRECIO2,	PE_TIPO, PE_DESC, PE_COSTO_SIN,	PE_RUTA, PE_CEDIS,	SUM(PE_COSTO_TOT) AS PE_COSTO_TOT,	 PE_COSTOU,	SUM(PE_CANT) As PE_CANT,	PE_PRONOMBRE,	PE_PROID,	PE_CLIENTE,		PE_USUARIO FROM PEDIDO WHERE PE_CLIENTE IN (Select CUENTA from cuentaa) GROUP BY PE_PRECIO2,	PE_TIPO,	PE_DESC,	PE_COSTO_SIN,	PE_RUTA,	PE_CEDIS,	PE_COSTOU, PE_PRONOMBRE,	PE_PROID, PE_CLIENTE ")
		c=Starter.skmt.ExecQuery("select PE_PRECIO2,PE_TIPO,PE_DESC,PE_COSTO_SIN,PE_RUTA,PE_CEDIS,PE_COSTO_TOT,PE_COSTOU,PE_CANT,PE_PRONOMBRE,PE_PROID,PE_CLIENTE,	PE_FECHA,PE_USUARIO 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
				Starter.skmt.ExecNonQuery2("UPDATE PEDIDO3 SET PE_FECHA= ? WHERE PE_CLIENTE = ? AND PE_PRONOMBRE = ? AND PE_PROID = ?  AND PE_CEDIS = ? ", Array As Object(c.GetString("PE_FECHA"), c.GetString("PE_CLIENTE"),c.GetString("PE_PRONOMBRE"),c.GetString("PE_PROID"),c.GetString("PE_CEDIS")))
			Next
		End If
		c.Close
		Starter.skmt.ExecNonQuery2("UPDATE PEDIDO3 SET PE_FOLIO = ? WHERE PE_CLIENTE IN (Select CUENTA from cuentaa)", Array As Object(YA_IMPRIMIO))
		Starter.skmt.ExecNonQuery("delete from pedido WHERE PE_CLIENTE IN (Select CUENTA from cuentaa) ")
		Starter.skmt.ExecNonQuery("INSERT INTO PEDIDO SELECT * FROM PEDIDO3")
	Else
		Log($"${cuenta},${limite_actual > limite_abordo},${cantidad.text},${B4XPages.MainPage.cliente.CREDITO_DISPONIBLE},${limite_actual},${limite_abordo}"$)
		Msgbox("EXCEDE EL LIMITE DE CREDITO","AVISO") 'ignore
	End If
	Starter.skmt.ExecNonQuery("delete from pedido3")
	Starter.skmt.ExecNonQuery("UPDATE PEDIDO SET PE_FOLIO = 0 where PE_FOLIO IS NULL")
	If cuenta <> "0" Then
		c=Starter.skmt.ExecQuery("SELECT SUM(PE_FOLIO) AS FOLIO FROM PEDIDO WHERE PE_CLIENTE IN (Select CUENTA from cuentaa)")
		c.Position = 0
		If c.GetString("FOLIO") <> Null And c.GetString("FOLIO") > 0 Then
			YA_IMPRIMIO = 1
		Else
			YA_IMPRIMIO = 0
		End If
		c.Close
	End If
	Starter.skmt.ExecNonQuery("INSERT INTO PEDIDO3 (PE_PRECIO2,	PE_TIPO,	PE_DESC,	PE_COSTO_SIN,	PE_RUTA,	PE_CEDIS,	 PE_COSTO_TOT,	 PE_COSTOU,	 PE_CANT,	PE_PRONOMBRE,	PE_PROID,	PE_CLIENTE,	PE_USUARIO ) SELECT PE_PRECIO2,	PE_TIPO, PE_DESC, PE_COSTO_SIN,	PE_RUTA, PE_CEDIS,	SUM(PE_COSTO_TOT) AS PE_COSTO_TOT,	 PE_COSTOU,	SUM(PE_CANT) As PE_CANT,	PE_PRONOMBRE,	PE_PROID,	PE_CLIENTE,		PE_USUARIO FROM PEDIDO WHERE PE_CLIENTE IN (Select CUENTA from cuentaa) GROUP BY PE_PRECIO2,	PE_TIPO,	PE_DESC,	PE_COSTO_SIN,	PE_RUTA,	PE_CEDIS,	PE_COSTOU, PE_PRONOMBRE,	PE_PROID, PE_CLIENTE ")
	c=Starter.skmt.ExecQuery("select PE_PRECIO2,PE_TIPO,PE_DESC,PE_COSTO_SIN,PE_RUTA,PE_CEDIS,PE_COSTO_TOT,PE_COSTOU,PE_CANT,PE_PRONOMBRE,PE_PROID,PE_CLIENTE,	PE_FECHA,PE_USUARIO 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
			Starter.skmt.ExecNonQuery2("UPDATE PEDIDO3 SET PE_FECHA= ? WHERE PE_CLIENTE = ? AND PE_PRONOMBRE = ? AND PE_PROID = ?  AND PE_CEDIS = ? ", Array As Object(c.GetString("PE_FECHA"), c.GetString("PE_CLIENTE"),c.GetString("PE_PRONOMBRE"),c.GetString("PE_PROID"),c.GetString("PE_CEDIS")))
		Next
	End If
	c.Close
	Starter.skmt.ExecNonQuery2("UPDATE PEDIDO3 SET PE_FOLIO = ? WHERE PE_CLIENTE IN (Select CUENTA from cuentaa)", Array As Object(YA_IMPRIMIO))
	Starter.skmt.ExecNonQuery("delete from pedido WHERE PE_CLIENTE IN (Select CUENTA from cuentaa) ")
	Starter.skmt.ExecNonQuery("INSERT INTO PEDIDO SELECT * FROM PEDIDO3")
	ListView1.Visible = True
	Starter.skmt.ExecNonQuery2("UPDATE PEDIDO SET PE_FECHA = ? where  PE_CLIENTE IN (Select CUENTA from cuentaa) and PE_FECHA IS NULL", Array As Object(sDate & sTime))
	Terminar.Enabled = True
	Starter.ultima_back_productos = 0
	Subs.iniciaActividad("Cliente")
End Sub

Sub i_fotol_Click
	i_fotol.Visible = False
End Sub

Sub DESC_PROMO_Click
	Starter.skmt.ExecNonQuery("delete from PROIDID")
	Starter.skmt.ExecNonQuery2("INSERT INTO PROIDID VALUES (?)", Array As Object(L_PROID.text))
	Subs.iniciaActividad("detalle_promo")
End Sub

Sub nopromo_Click
	If cantidad.Text = "" Then
		Panel1.Visible= False
	Else
		If cantidad.Text * l_precio.Text  > t_venta.Text Then
			Msgbox("Excede EL MONTO DE LA VENTA", "ADVERTENCIA") 'ignore
'		Else
'			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.ExecNonQuery2("INSERT INTO  PEDIDO (PE_CEDIS,PE_COSTO_TOT,PE_COSTOU,PE_CANT,PE_PRONOMBRE,PE_PROID,PE_CLIENTE,PE_FECHA,PE_USUARIO,PE_TIPO,PE_PRECIO2) VALUES(?,?,?,?,?,?,?,?,?,?,?) ", Array As Object ("DUR",0,0, cantidad.text,"CAMBIO" & l_desc.text, L_PROID.Text,clie_id, sDate & sTime, usuario,Starter.tipov,precio2))
'			Starter.skmt.ExecNonQuery2("update " & query & " set cat_gp_almacen = cat_gp_almacen - ? where cat_gp_id = ? ", Array As Object(cantidad.Text, L_PROID.Text))
'			cantidad.Text = ""
'			Panel1.Visible= False
		End If
	End If
End Sub

Sub LV_PRECIOS_ItemClick (Position As Int, Value As Object)
	c=Starter.skmt.ExecQuery("select PRECIO, NLISTA FROM LISTA_PRECIOS WHERE NLISTA = " & "'" & Value &"' AND  CAT_GP_ID =" & "'" &L_PROID.TEXT&"' ORDER BY NLISTA ASC")
	c.Position=0
	l_precio.Text = c.GetString("PRECIO")
	c.Close
End Sub

Sub B_MENOS_Click
	If cantidad.Text = "" Then
		cantidad.Text = ""
	Else IF cantidad.Text > 0 Then
		cantidad.Text = cantidad.Text -1
	End If
End Sub

Sub B_MAS_Click
	If cantidad.Text = "" Then
		cantidad.Text = 1
	Else
		cantidad.Text = cantidad.Text + 1
	End If
End Sub

Sub B_MENOSC_Click
	If E_CAMBIO.Text = "" Then
		E_CAMBIO.Text = ""
	Else IF E_CAMBIO.Text > 0 Then
		E_CAMBIO.Text = E_CAMBIO.Text -1
	End If
End Sub

Sub B_MASC_Click
	If E_CAMBIO.Text = "" Then
		E_CAMBIO.Text = 1
	Else
		E_CAMBIO.Text = E_CAMBIO.Text + 1
	End If
End Sub


Private Sub b_rmi_Click
	Panel5.Visible = True
	
	r = Starter.skmt.ExecQuery("select CAT_ID, CAT_DESCRIPCION, CAT_MONTO FROM CAT_RMI")
	clv_rmi.Clear
	For i=0 To r.RowCount -1
		r.Position = i
		clv_rmi.Add(CreateListItem(i &" ," &r.GetString("CAT_DESCRIPCION"), r.GetString("CAT_MONTO"), clv_rmi.AsView.Width , 50dip),i)
	Next
	r.Close
End Sub


Sub CreateListItem(descripcion As String, monto As String, Width As Int, Height As Int) As Panel
	Dim p As B4XView = xui.CreatePanel("")
	p.SetLayoutAnimated(0, 0, 0, Width, Height)
	p.LoadLayout("proitem")
	l_prodX.Text = descripcion& CRLF & " $ " & monto
	Return p
End Sub