B4A=true
Group=Default Group
ModulesStructureVersion=1
Type=Activity
Version=6.8
@EndOfDesignText@
#Region  Activity Attributes 
	#FullScreen: True
	#IncludeTitle: True
#End Region

Sub Process_Globals
	'These global variables will be declared once when the application starts.
	'These variables can be accessed from all modules.
	'    Dim g As GPS
	Dim ruta As String
	Dim q_buscar As String
	Dim skmt As SQL
End Sub

Sub Globals
	'These global variables will be redeclared each time the activity is created.
	'These variables can only be accessed from this module.
	Dim c As Cursor
	Dim c2 As Cursor
	Dim ListView1 As ListView
	Dim entro As String
	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
	Dim PROMOS_VENTAS As String
	
End Sub

Sub Activity_Create(FirstTime As Boolean)
	If(FirstTime) Then
'		g.Initialize("GPS")
	End If
	'Do not forget to load the layout file created with the visual designer. For example:
	'Activity.LoadLayout("Layout1")
	Activity.RemoveAllViews
    Activity.LoadLayout("PRODUCTOS")
	'Dim ruta As String
	' valido donde escribo el archivo de la base de datos de kmt
	ruta = File.DirInternal
	Panel1.Visible= False
	' se crea o no el archivo de la base de ddatos de kmt
	'NOTAS SI SE MODIFICA LA ESTRUCTURA SE QUITA EL IF Y SE VA DIRECTO A LA SENTENCIA FILE.COPY PARA QUE 
	'TOME LA NUEVA ESTRUCTURA ES MUY IMPORTANTE TENER EL IF DE LO CONTRARIO SOLO LO ESCRIBE UNA VEZ Y LO BORRA
	'SI SE REGRESA A ESTE ACTIVIDAD.
	If File.Exists(ruta, "kmt.db") = False Then
		File.Copy(File.DirAssets, "kmt.db", ruta, "kmt.db")
	End If
	skmt.Initialize(ruta,"kmt.db", True)
End Sub

Sub Activity_Resume
	If Main.tipov = "PREVENTA" Then
		query = "cat_gunaprod"
		QUERY2 = "COUNT_GUNAPROD"
	Else IF Main.tipov = "VENTA" Then
		query = "cat_gunaprod2"
		QUERY2= " COUNT_GUNAPROD2"
	ELSE IF Main.tipov = "ABORDO" Then
		query = "cat_gunaprod"
		QUERY2 = "COUNT_GUNAPROD"
	Else
		query = "cat_gunaprod"
		QUERY2 = "COUNT_GUNAPROD"
		ya_entro = 0
	End If
	
	Log(Main.tipov)
	
	If ya_entro <> "1" Then

		'BUSCA.Text =""
		'SE COMENTO LA LINEA DE ARRIBA YA QUE DISPARA EL EVENTO DEL TEXTCHANGE Y PUEDE HACER LENTO EL SISTEMA
		skmt.Initialize(ruta,"kmt.db", True)
		entro ="1"
		ya_entro ="1"
		c=skmt.ExecQuery("select CAT_GP_CLASIF, COUNT(*) AS CUANTOS from " & query & " where (length(CAT_GP_CODPROMO) = 1 OR CAT_GP_CODPROMO = CAT_GP_ID) GROUP BY CAT_GP_CLASIF order by CAT_GP_CLASIF asc")
		
		'ESTO ES PARA NO VER PROCTER
		'c=skmt.ExecQuery2("select CAT_GP_CLASIF, count(*) as cuantos from cat_gunaprod where CAT_GP_ALMACEN > 0 and cat_gp_clasif NOT IN (?,?,?,?,?,?,?,?,?,?) group by CAT_GP_CLASIF order by CAT_GP_CLASIF asc", Array As String("PROCTER","PROMOS","TRES MONTES","CAMPARI","CAMPARI-PRO","CLOROX DE MEXICO","MARS","CONAGRA","VERDE VALLE","MEAD JOHNSON"))
		ListView1.Clear
		lfila.Text = "MARCA"
		Dim label1 As Label
		label1 = ListView1.TwoLinesLayout.Label
		label1.TextSize = 10
		label1.TextColor = Colors.Black
		Dim label2 As Label
		label2 = ListView1.TwoLinesLayout.SecondLabel
		label2.TextSize = 10
		label2.TextColor = Colors.Black
			
		If c.RowCount>0 Then
			For i=0 To c.RowCount -1
				c.Position=i
		'		bmp = LoadBitmap(File.DirAssets, "palomita_verde.png")

				ListView1.AddTwoLines(c.GetString("CAT_GP_CLASIF"),c.GetString("CUANTOS"))
			
			Next
		End If
	End If
 
	c=skmt.ExecQuery2("select CAT_VA_VALOR from CAT_VARIABLES WHERE CAT_VA_DESCRIPCION = ?", Array As String ("PERFIL"))
	c.Position =0
	If c.RowCount > 0 Then
		PERFIL = c.GetString("CAT_VA_VALOR")
	Else
		PERFIL = 0
	End If
	c.Close
 
If PERFIL = "V-COSTO" Then
	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
End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub



Sub ListView1_ItemClick (Position As Int, Value As Object)
	'Sub ListView1_ItemLongClick (Position As Int, Value As Object)
	'que_colonia = Value
	'StartActivity(fila)
	
	
	If Value = "PROMOS" And fila.cuenta <> "N" Then
		entro = "3"
		marca = "PROMOS"
		tipo = "PROMOS"
	Else if Value = "PROMOS"  And fila.cuenta = "N"  Then
		entro = "5"
		StartActivity(fila)
	End If
	If entro = "1" Then
		marca = Value
		skmt.Initialize(ruta,"kmt.db", True)
		c2=skmt.ExecQuery2("select  COUNT(*) AS CUANTOS, CAT_GP_TIPO from " & query & " where CAT_GP_ALMACEN > 0 and CAT_GP_CLASIF = ? 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"
		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
		tipo = Value
		skmt.Initialize(ruta,"kmt.db", True)
		c2=skmt.ExecQuery2("select  COUNT(*) AS CUANTOS, CAT_GP_SUBTIPO from " & query & " where CAT_GP_ALMACEN > 0 and CAT_GP_CLASIF = ? and CAT_GP_TIPO = ? 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(marca, Value))
		ListView1.Clear
		lfila.text = "SUB-TIPO"
		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"
	   
	Else If entro = "3" Then
		skmt.Initialize(ruta,"kmt.db", True)
		c2=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_CLASIF = ? AND CAT_GP_TIPO = ?  and (length(CAT_GP_CODPROMO) = 1 OR CAT_GP_CODPROMO = CAT_GP_ID) ", Array As String(marca,  Value))
		ListView1.Clear
		lfila.text = "PRODUCTO"
		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"
	Else If entro = "4" Then
	  
		skmt.ExecNonQuery("delete from PROID")
		skmt.ExecNonQuery2("INSERT INTO PROID VALUES (?)", Array As Object(Value))
		'StartActivity(pedidos)
		HACER_PEDIDO
	End If
End Sub

Sub Activity_KeyPress (key As Int) As Boolean
    ' BACK key pressed
    If key=KeyCodes.KEYCODE_BACK Then
       	 ya_entro ="0"

	   If entro = "4" Then
	   entro = "3"
	   Activity_Resume
	   Return True
	   Else If entro = "3" Then
	 '  entro = "2"
	  ' Activity_Resume
	  ' Return True
	  ' Else If entro = "2" Then
	  ' entro = "1"
	  ' Activity_Resume
	  ' Return True
	   'Else If entro = "1" Then
	   
			' I want to capture the key here so I return True
			
			StartActivity(fila)
            Return False
	   End If
    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 & "%"
	skmt.Initialize(ruta,"kmt.db", True)
	 ' c=skmt.ExecQuery2("select CAT_GP_NOMBRE from cat_gunaprod where CAT_GP_ALMACEN > 0 and CAT_GP_NOMBRE like ? AND cat_gp_clasif NOT IN (?,?,?,?,?,?,?,?,?,?)  order by CAT_GP_NOMBRE asc", Array As String(q_buscar, "PROCTER","PROMOS","TRES MONTES","CAMPARI","CAMPARI-PRO","CLOROX DE MEXICO","MARS","CONAGRA","VERDE VALLE","MEAD JOHNSON"))
	c=skmt.ExecQuery2("select CAT_GP_NOMBRE, CAT_GP_ALMACEN, CAT_GP_PRECIO,CAT_GP_IMG from " & query & " where CAT_GP_PRECIO > 0 AND CAT_GP_ALMACEN > 0 and CAT_GP_NOMBRE like ? and CAT_GP_CLASIF <>  ?  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"))
	'and CAT_GP_CLASIF <>  ? group by CAT_GP_CLASIF order by CAT_GP_CLASIF asc", Array As String("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)

			 ListView1.AddTwoLines(c.GetString("CAT_GP_NOMBRE"),"Existencia " & c.GetString("CAT_GP_ALMACEN") & " Precio $" & c.GetString("CAT_GP_PRECIO"))
	      Next
	   End If

	 
	 
'	   If c.RowCount>0 Then
'	      For i=0 To c.RowCount -1
'		     c.Position=i
'			  Dim label1 As Label
'			 label1 = ListView1.SingleLineLayout.Label
'			 label1.TextSize = 13
	'			 ListView1.AddSingleLine(c.GetString("CAT_GP_NOMBRE"))
	'	      Next
	'	   End If
	c.Close
	entro= "4"
End Sub

Sub HACER_PEDIDO
	c=skmt.ExecQuery("Select SUBSTR(CUENTA,1,1) AS CUENTA from cuentaa")
	c.Position=0
	clie_id = c.GetString("CUENTA")
	c.Close

	Panel1.Visible= True
	cambio = "0"
'	If g.GPSEnabled=False Then
'		ToastMessageShow("Habilitar el GPS", True)
'		StartActivity(g.LocationSettingsIntent)
'	Else
'		g.Start(0,0)
'	End If
	cantidad.Text = ""
	c=skmt.ExecQuery("select  ID_ALMACEN from CAT_ALMACEN")
	C.Position =0
	ALMACEN = C.GetString("ID_ALMACEN")
	C.Close
	c=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
	i_fotol.Visible = False

	skmt.Initialize(ruta,"kmt.db", True)
	c=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)")
	' ListView1.Clear
	   
	'If c.RowCount>0 Then
	'  For i=0 To c.RowCount -1
	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 PERFIL = "V-SUPER" Then
	c2=skmt.ExecQuery("select PRECIO, NLISTA FROM LISTA_PRECIOS WHERE NLISTA = '10' AND  CAT_GP_ID =" & "'" &L_PROID.TEXT&"' ORDER BY NLISTA ASC")
	c2.Position=0
	l_precio.Text = c2.GetString("PRECIO")
	c2.Close
	
	Else IF ALMACEN = "6" And cl_ruta = "16" Then
		c2=skmt.ExecQuery("select PRECIO, NLISTA FROM LISTA_PRECIOS WHERE NLISTA = '11' AND  CAT_GP_ID =" & "'" &L_PROID.TEXT&"' ORDER BY NLISTA ASC")
		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
			 

	

		
	If l_marca.text = "PROMOS" And l_tipo.Text ="PROMOS" Then
		DESC_PROMO.Visible = True
		CC=skmt.ExecQuery2("select count(*) as CUANTOS FROM PROMOS_COMP WHERE CAT_PA_ID = ?", Array As String(c.GetString("CAT_GP_ID")))
		CC.Position =0
		If CC.GetString("CUANTOS") = 0 Then
			l_bodega.Text = "4"
		Else
			CC.Close
			CC=skmt.ExecQuery2("select CAT_PA_MAXPROM, CAT_PA_MAXPROMREC, CAT_PA_MAXPROMCLIE FROM PROMOS_COMP WHERE CAT_PA_ID = ?", Array As String(c.GetString("CAT_GP_ID")))
			CC.Position =0
				
			DD=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(c.GetString("CAT_GP_ID")))
			DD.Position =0
			If DD.GetString("CUANTOS")  = 0 Then
				HCCP_CANT = 0
			
			Else
				DD.Close
				
				DD=skmt.ExecQuery2("SELECT HCCP_CANT FROM HIST_CLIENTE_CANT_PROMOS WHERE HCCP_CLIENTE IN (Select cuenta from cuentaa) AND HCCP_PROMO =?", Array As String(c.GetString("CAT_GP_ID")))
				DD.Position =0
				HCCP_CANT = DD.GetString("HCCP_CANT")
				DD.Close
			End If
			TOTAL_PROMO = CC.GetString("CAT_PA_MAXPROMCLIE") - HCCP_CANT
			If HCCP_CANT = 0 Then
				
				l_bodega.Text = CC.GetString("CAT_PA_MAXPROM")
			Else IF TOTAL_PROMO > CC.GetString("CAT_PA_MAXPROMREC") Then
				l_bodega.Text = 	CC.GetString("CAT_PA_MAXPROMREC")
			Else
				l_bodega.Text = TOTAL_PROMO
			End If
		End If
	Else
		DESC_PROMO.Visible = False
	End If
            	
	c.Close
	c=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
	' If t_venta.Text < 100  AND l_tipo.Text = "PROMOS" Then
	'Terminar.Visible = False
	'guardar.Visible = False
	'cantidad.Visible = False
	'nopromo.Visible = True
	'Else
	'cambio = 0
	Terminar.Visible = True
	guardar.Visible = True
	cantidad.Visible = True
	If cambio = "1" Then
		nopromo.Visible = True
		If clie_id = "N" Then
			nopromo.Visible = False
		End If
	Else
		nopromo.Visible = False
	End If
	'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
		Msgbox("ALERTA","EL CLIENTE YA NO PUEDE TENER ESTA PROMOCION")
		Panel1.Visible= False

	End If

If marca <> "PROMOS" Then

If PERFIL = "PREVENTA" Then
	
t_precio.Visible = False
LV_PRECIOS.Visible = False

ELSE IF PERFIL = "V-ESPECIAL" Then
	c=skmt.ExecQuery("select PRECIO, NLISTA FROM LISTA_PRECIOS WHERE NLISTA IN ('5','6','7','8','10') AND  CAT_GP_ID =" & "'" &L_PROID.TEXT&"' ORDER BY NLISTA ASC")
	LV_PRECIOS.Clear
	Dim label1 As Label
	label1 = LV_PRECIOS.TwoLinesLayout.Label
	label1.TextSize = 10
	label1.TextColor = Colors.Black
	Dim label2 As Label
	label2 = LV_PRECIOS.TwoLinesLayout.SecondLabel
	label2.TextSize = 10
	label2.TextColor = Colors.Black
			
	If c.RowCount>0 Then
		For i=0 To c.RowCount -1
			c.Position=i
			LV_PRECIOS.AddTwoLines(c.GetString("NLISTA"),c.GetString("PRECIO"))
		Next
	End If
ELSE IF PERFIL = "V-MINI" Then
		t_precio.Visible = False
		LV_PRECIOS.Visible = False

		c=skmt.ExecQuery("select PRECIO, NLISTA FROM LISTA_PRECIOS WHERE NLISTA = '2' AND  CAT_GP_ID =" & "'" &L_PROID.TEXT&"' ORDER BY NLISTA ASC")
		c.Position=0
		l_precio.Text = c.GetString("PRECIO")
		c.Close
ELSE IF PERFIL = "V-COSTO" Then

		t_precio.Visible = False
		LV_PRECIOS.Visible = False

		c=skmt.ExecQuery("select PRECIO, NLISTA FROM LISTA_PRECIOS WHERE NLISTA = '9' AND  CAT_GP_ID =" & "'" &L_PROID.TEXT&"' ORDER BY NLISTA ASC")
		c.Position=0
		l_precio.Text = c.GetString("PRECIO")
			c.Close
ELSE IF PERFIL = "V-SUPER" Then

			t_precio.Visible = False
			LV_PRECIOS.Visible = False

			c=skmt.ExecQuery("select PRECIO, NLISTA FROM LISTA_PRECIOS WHERE NLISTA ='9' 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


'' 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
	
	If query = "cat_gunaprod2" Then
		l_bodega.Text = PROMOS_VENTAS
'	Msgbox("1","alerta")
			
			e=skmt.ExecQuery2("select count(*) as CUANTOS FROM CAT_DETALLES_PAQ WHERE CAT_DP_ID  = ? ", Array As String(L_PROID.Text) )
		e.Position = 0
			 
	If e.GetString("CUANTOS") > 0 Then
'			Msgbox("2","alerta")
		
				F=skmt.ExecQuery2("select CAT_DP_IDPROD, CAT_DP_PZAS FROM CAT_DETALLES_PAQ WHERE CAT_DP_ID = ? ", Array As String(L_PROID.Text) )
		'se coloco el if por la mala promocion.
		
		For i=0 To F.RowCount -1
			F.Position=i
			'		Msgbox(f.GetString("CAT_DP_IDPROD"),"alerta")
	
			c=skmt.ExecQuery2("select count(*) as CUANTOS from cat_gunaprod2 where CAT_GP_ID = ? ", Array As String(f.GetString("CAT_DP_IDPROD")))
			c.Position=0
			If c.GetString("CUANTOS") > 0 Then
'					Msgbox("4","alerta")
	
			h=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 h.GetString("CAT_GP_ALMACEN")  < l_bodega.Text * f.GetString("CAT_DP_PZAS") Then
					l_bodega.Text = "1"
				End If
			'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, f.GetString("CAT_DP_PZAS") * cantidad.text, h.GetString("CAT_GP_NOMBRE"), f.GetString("CAT_DP_IDPROD"),clie_id, sDate & sTime, usuario,cl_ruta,Main.tipov,precio2))
			h.Close
			else if c.GetString("CUANTOS") = 0 Then
				
				l_bodega.Text = "0"
			End If
			c.Close
			
		Next
	    
		
		
		f.Close
	End If
		e.Close
	End If
	'' ESTO ES PARA NO PASAR DE LAS PROMOS ABORDO Y MANDARLAS AL MINIMO  FIN
	End If
	
	If clie_id="0" Then
		nopromo.Visible = False
	End If
End Sub




Sub guardar_Click
	If cantidad.Text = "" Then
		Panel1.Visible= False

		
	ELSE If cantidad.Text > l_bodega.Text Then
		Msgbox("Exede la existencia", "ADVERTENCIA")
	Else If l_marca.text = "PROMOS" And l_tipo.Text ="PROMOS" And PROMO_C > 0 And cantidad.Text > 0 Then
		skmt.ExecNonQuery("delete from PROIDID")
		
		skmt.ExecNonQuery2("INSERT INTO PROIDID VALUES (?)", Array As Object(L_PROID.text))
		c=skmt.ExecQuery("Select count(*) as CUANTOSD from pedido where 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) AND CAT_DP_PRECIO > 0)")
		c.Position =0
		If c.GetString("CUANTOSD") = 0 Then
			Msgbox("No tiene venta el cliente para la promo", "ADVERTENCIA")
		Else
			c=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 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)")
			C.Position =0
			TOT_ART_PROMO = C.GetString("TOT_ART_PROMO")
			COSTO_TOT = C.GetString("COSTO_TOT")
			C.Close
		
			c=skmt.ExecQuery("Select count(*) as CUANTOSD from pedido where 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) AND 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")
			Else
				c=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 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 AND CAT_DP_TIPO = 1)")
				C.Position =0
				TOT_ART_PROMOR = C.GetString("TOT_ART_PROMO")
			End If
			C.Close
		
		
			PROMO_C = PROMO_C * cantidad.Text
			PROMO_CR = PROMO_CR * cantidad.Text
		
			If  TOT_ART_PROMO = PROMO_C And TOT_ART_PROMOR = PROMO_CR Then
				' anterior a 29/06/2015
				'skmt.ExecNonQuery2("UPDATE PEDIDO SET PE_COSTOU = (?), PE_COSTO_TOT = PE_CANT * (?) WHERE 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_precio.text/(TOT_ART_PROMO/cantidad.Text),l_precio.text/(TOT_ART_PROMO/cantidad.Text)))
				'en pe_cedis meto el codigo de promocion para hacer esta validacion.
				skmt.ExecNonQuery2("UPDATE PEDIDO SET PE_CEDIS = (?) WHERE 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=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")
				If D.RowCount>0 Then
					For i=0 To D.RowCount -1
						D.Position=i
			 
						skmt.ExecNonQuery2("UPDATE PEDIDO SET PE_COSTOU = (?), PE_COSTO_TOT = PE_CANT * (?) , PE_COSTO_SIN = (?) WHERE PE_PROID = (?) And PE_CEDIS In (Select PROIDID from PROIDID) AND  PE_CLIENTE IN (Select CUENTA from cuentaa) ",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=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 AND CAT_DP_TIPO = 1")
				If D.RowCount>0 Then
					For i=0 To D.RowCount -1
						D.Position=i
			 
						skmt.ExecNonQuery2("UPDATE PEDIDO SET PE_COSTOU = (?), PE_COSTO_TOT = PE_CANT * (?) , PE_COSTO_SIN = (?) WHERE PE_PROID = (?) And PE_CEDIS In (Select PROIDID from PROIDID) AND  PE_CLIENTE IN (Select CUENTA from cuentaa) ",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
	    
		
	    
				'skmt.ExecNonQuery("UPDATE PEDIDO SET PE_COSTO_TOT = 0.01 WHERE 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))")
        
				DateTime.DateFormat = "MM/dd/yyyy"
				sDate=DateTime.Date(DateTime.Now)
				sTime=DateTime.Time(DateTime.Now)
   		
				c=skmt.ExecQuery("Select CUENTA from cuentaa")
				c.Position=0
				clie_id = c.GetString("CUENTA")
		
				c.Close
		
				If Main.tipov = "ABORDO" Then
					c=skmt.ExecQuery("select CAT_CL_RUTA from kmt_info")
					c.Position=0
					cl_ruta = c.GetString("CAT_CL_RUTA")
					c.Close
				
				Else	
				c=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
				End If
				
				c=skmt.ExecQuery("select USUARIO from usuarioa")
				c.Position=0
				usuario = c.GetString("USUARIO")
				'skmt.ExecNonQuery2("INSERT INTO  PEDIDO (PE_CEDIS,PE_COSTO_TOT,PE_COSTOU,PE_CANT,PE_PRONOMBRE,PE_PROID,PE_CLIENTE,PE_FECHA,PE_USUARIO) VALUES(?,?,?,?,?,?,?,?,?) ", Array As Object ("COACALCO",cantidad.Text * l_precio.Text, l_precio.text, cantidad.text, l_desc.text, L_PROID.Text,clie_id, sDate & sTime, usuario))
				'para insertar la promo de ades el de regalo en 0
				e=skmt.ExecQuery("select count(*) as CUANTOS FROM CAT_DETALLES_PAQ WHERE CAT_DP_ID IN (Select PROIDID from PROIDID) and CAT_DP_TIPO = 0 AND CAT_DP_PRECIO = 0")
		
			 
				If e.GetString("CUANTOS") > 0 Then
		
					F=skmt.ExecQuery("select CAT_DP_IDPROD, CAT_DP_PZAS FROM CAT_DETALLES_PAQ WHERE CAT_DP_ID IN (Select PROIDID from PROIDID) and CAT_DP_TIPO = 0 AND CAT_DP_PRECIO = 0")
					'se coloco el if por la mala promocion.
		
					For i=0 To F.RowCount -1
						F.Position=i
		
						h=skmt.ExecQuery2("select CAT_GP_NOMBRE from " & query & " where CAT_GP_ID = ? ", Array As String(f.GetString("CAT_DP_IDPROD")))
						h.Position=0
						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, f.GetString("CAT_DP_PZAS") * cantidad.text, h.GetString("CAT_GP_NOMBRE"), f.GetString("CAT_DP_IDPROD"),clie_id, sDate & sTime, usuario,cl_ruta,Main.tipov,precio2))
						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
		
		
				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,Main.tipov,precio2))
				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")
			End If
		End If
		
		cantidad.Text = ""
		Panel1.Visible= False
		
		skmt.ExecNonQuery("delete from pedido3")
		skmt.ExecNonQuery("UPDATE PEDIDO SET PE_FOLIO = 0 where PE_FOLIO IS NULL")

		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, PE_FOLIO ) 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, PE_FOLIO " & _
	" 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, PE_FOLIO ")
		c=skmt.ExecQuery("select PE_PRECIO2,PE_TIPO,PE_FOLIO,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
				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
		skmt.ExecNonQuery("delete from pedido WHERE PE_CLIENTE IN (Select CUENTA from cuentaa) ")
		skmt.ExecNonQuery("INSERT INTO PEDIDO SELECT * FROM PEDIDO3")
	

	else If l_marca.text = "PROMOS" And l_tipo.Text ="PROMOS" And PROMO_C = 0 Then
		skmt.ExecNonQuery("delete from PROIDID")
		
		skmt.ExecNonQuery2("INSERT INTO PROIDID VALUES (?)", Array As Object(L_PROID.text))
		c=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=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=skmt.ExecQuery("select USUARIO from usuarioa")
		c.Position=0
		usuario = c.GetString("USUARIO")
		'skmt.ExecNonQuery2("INSERT INTO  PEDIDO (PE_CEDIS,PE_COSTO_TOT,PE_COSTOU,PE_CANT,PE_PRONOMBRE,PE_PROID,PE_CLIENTE,PE_FECHA,PE_USUARIO) VALUES(?,?,?,?,?,?,?,?,?) ", Array As Object ("COACALCO",cantidad.Text * l_precio.Text, l_precio.text, cantidad.text, l_desc.text, L_PROID.Text,clie_id, sDate & sTime, usuario))
		'para insertar la promo de ades el de regalo en 0
		e=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=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=skmt.ExecQuery2("select CAT_GP_NOMBRE from " & query & " where CAT_GP_ID = ? ", Array As String(f.GetString("CAT_DP_IDPROD")))
				h.Position=0
				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,Main.tipov,precio2))
				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
		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,Main.tipov,precio2))
		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=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
		skmt.ExecNonQuery("delete from pedido_cliente where PC_CLIENTE In (select cuenta from cuentaa)")
		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"), Main.lon_gps, Main.lat_gps,ALMACEN,cl_ruta,c.GetString("TOTAL_CLIE_SIN")))
		If Main.tipov <> "ABORDO" Then
			skmt.ExecNonQuery("UPDATE kmt_info set gestion = 2  where CAT_CL_CODIGO In (select cuenta from cuentaa)")
		End If
		
		Panel1.Visible= False

		skmt.ExecNonQuery("delete from pedido3")
		skmt.ExecNonQuery("UPDATE PEDIDO SET PE_FOLIO = 0 where PE_FOLIO IS NULL")

		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, PE_FOLIO ) 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, PE_FOLIO " & _
	" 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, PE_FOLIO ")
		c=skmt.ExecQuery("select PE_PRECIO2,PE_TIPO,PE_FOLIO,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
				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
		skmt.ExecNonQuery("delete from pedido WHERE PE_CLIENTE IN (Select CUENTA from cuentaa) ")
		skmt.ExecNonQuery("INSERT INTO PEDIDO SELECT * FROM PEDIDO3")
	
   	
	Else
	
		DateTime.DateFormat = "MM/dd/yyyy"
		sDate=DateTime.Date(DateTime.Now)
		sTime=DateTime.Time(DateTime.Now)
		c=skmt.ExecQuery("Select CUENTA from cuentaa")
		c.Position=0
		clie_id = c.GetString("CUENTA")
		c.Close
		If Main.tipov = "ABORDO" Then
			c=skmt.ExecQuery("select CAT_CL_RUTA from kmt_info")
			c.Position=0
			cl_ruta = c.GetString("CAT_CL_RUTA")
			c.Close
				
		Else
			c=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
		End If
				
		
		c=skmt.ExecQuery("select USUARIO from usuarioa")
		c.Position=0
		usuario = c.GetString("USUARIO")
	
		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,Main.tipov,precio2))
		skmt.ExecNonQuery2("update " & query & " set cat_gp_almacen = cat_gp_almacen - ? where cat_gp_id = ? ", Array As Object(cantidad.Text, L_PROID.Text))
		c=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
		skmt.ExecNonQuery("delete from pedido_cliente where PC_CLIENTE In (select cuenta from cuentaa)")
		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"), Main.lon_gps, Main.lat_gps,ALMACEN,cl_ruta,c.GetString("TOTAL_CLIE_SIN")))
		If Main.tipov <> "ABORDO" Then
			skmt.ExecNonQuery("UPDATE kmt_info set gestion = 2  where CAT_CL_CODIGO In (select cuenta from cuentaa)")
		
			End If
		Panel1.Visible= False

		skmt.ExecNonQuery("delete from pedido3")
		skmt.ExecNonQuery("UPDATE PEDIDO SET PE_FOLIO = 0 where PE_FOLIO IS NULL")

		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, PE_FOLIO ) 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, PE_FOLIO " & _
	" 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, PE_FOLIO ")
		c=skmt.ExecQuery("select PE_PRECIO2,PE_TIPO,PE_FOLIO,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
				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
		skmt.ExecNonQuery("delete from pedido WHERE PE_CLIENTE IN (Select CUENTA from cuentaa) ")
		skmt.ExecNonQuery("INSERT INTO PEDIDO SELECT * FROM PEDIDO3")
	
	End If
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
	
	If cantidad.Text = "" Then
		StartActivity(fila)
		
	ELSE If cantidad.Text > l_bodega.Text Then
		Msgbox("Exede la existencia", "ADVERTENCIA")
	Else If l_marca.text = "PROMOS" And l_tipo.Text ="PROMOS" And PROMO_C > 0 And cantidad.Text > 0 Then
		skmt.ExecNonQuery("delete from PROIDID")
		
		skmt.ExecNonQuery2("INSERT INTO PROIDID VALUES (?)", Array As Object(L_PROID.text))
		c=skmt.ExecQuery("Select count(*) as CUANTOSD from pedido where 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) AND CAT_DP_PRECIO > 0)")
		c.Position =0
		If c.GetString("CUANTOSD") = 0 Then
			Msgbox("No tiene venta el cliente para la promo", "ADVERTENCIA")
		Else
			c=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 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)")
			C.Position =0
			TOT_ART_PROMO = C.GetString("TOT_ART_PROMO")
			COSTO_TOT = C.GetString("COSTO_TOT")
			C.Close
		
			c=skmt.ExecQuery("Select count(*) as CUANTOSD from pedido where 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) AND 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")
			Else
				c=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 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 AND CAT_DP_TIPO = 1)")
				C.Position =0
				TOT_ART_PROMOR = C.GetString("TOT_ART_PROMO")
			End If
			C.Close
		
		
			PROMO_C = PROMO_C * cantidad.Text
			PROMO_CR = PROMO_CR * cantidad.Text
		
			If  TOT_ART_PROMO = PROMO_C And TOT_ART_PROMOR = PROMO_CR Then
				' anterior a 29/06/2015
				'skmt.ExecNonQuery2("UPDATE PEDIDO SET PE_COSTOU = (?), PE_COSTO_TOT = PE_CANT * (?) WHERE 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_precio.text/(TOT_ART_PROMO/cantidad.Text),l_precio.text/(TOT_ART_PROMO/cantidad.Text)))
				'en pe_cedis meto el codigo de promocion para hacer esta validacion.
				skmt.ExecNonQuery2("UPDATE PEDIDO SET PE_CEDIS = (?) WHERE 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=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")
				If D.RowCount>0 Then
					For i=0 To D.RowCount -1
						D.Position=i
			 
						skmt.ExecNonQuery2("UPDATE PEDIDO SET PE_COSTOU = (?), PE_COSTO_TOT = PE_CANT * (?) , PE_COSTO_SIN = (?) WHERE PE_PROID = (?) And PE_CEDIS In (Select PROIDID from PROIDID) AND  PE_CLIENTE IN (Select CUENTA from cuentaa) ",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=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 AND CAT_DP_TIPO = 1")
				If D.RowCount>0 Then
					For i=0 To D.RowCount -1
						D.Position=i
			 
						skmt.ExecNonQuery2("UPDATE PEDIDO SET PE_COSTOU = (?), PE_COSTO_TOT = PE_CANT * (?) , PE_COSTO_SIN = (?) WHERE PE_PROID = (?) And PE_CEDIS In (Select PROIDID from PROIDID) AND  PE_CLIENTE IN (Select CUENTA from cuentaa) ",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
	    
		
	    
				'skmt.ExecNonQuery("UPDATE PEDIDO SET PE_COSTO_TOT = 0.01 WHERE 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))")
        
				DateTime.DateFormat = "MM/dd/yyyy"
				sDate=DateTime.Date(DateTime.Now)
				sTime=DateTime.Time(DateTime.Now)
   		
				c=skmt.ExecQuery("Select CUENTA from cuentaa")
				c.Position=0
				clie_id = c.GetString("CUENTA")
		
				c.Close
		
				c=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=skmt.ExecQuery("select USUARIO from usuarioa")
				c.Position=0
				usuario = c.GetString("USUARIO")
				'skmt.ExecNonQuery2("INSERT INTO  PEDIDO (PE_CEDIS,PE_COSTO_TOT,PE_COSTOU,PE_CANT,PE_PRONOMBRE,PE_PROID,PE_CLIENTE,PE_FECHA,PE_USUARIO) VALUES(?,?,?,?,?,?,?,?,?) ", Array As Object ("COACALCO",cantidad.Text * l_precio.Text, l_precio.text, cantidad.text, l_desc.text, L_PROID.Text,clie_id, sDate & sTime, usuario))
				'para insertar la promo de ades el de regalo en 0
				e=skmt.ExecQuery("select count(*) as CUANTOS FROM CAT_DETALLES_PAQ WHERE CAT_DP_ID IN (Select PROIDID from PROIDID) and CAT_DP_TIPO = 0 AND CAT_DP_PRECIO = 0")
				e.Position =0
				If e.GetString("CUANTOS") > 0 Then
					F=skmt.ExecQuery("select CAT_DP_IDPROD, CAT_DP_PZAS FROM CAT_DETALLES_PAQ WHERE CAT_DP_ID IN (Select PROIDID from PROIDID) and CAT_DP_TIPO = 0 AND CAT_DP_PRECIO = 0")
					'meter el otro
					F.Position =0
					h=skmt.ExecQuery2("select CAT_GP_NOMBRE from " & query & " where CAT_GP_ID = ? ", Array As String(f.GetString("CAT_DP_IDPROD")))
					h.Position=0
					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, f.GetString("CAT_DP_PZAS") * cantidad.text, h.GetString("CAT_GP_NOMBRE"), f.GetString("CAT_DP_IDPROD"),clie_id, sDate & sTime, usuario,cl_ruta,Main.tipov,precio2))
					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
					f.Close
				End If
				e.Close
				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,Main.tipov,precio2))
				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")
			End If
		End If
		
		cantidad.Text = ""
		Panel1.Visible= False
		
		skmt.ExecNonQuery("delete from pedido3")
		skmt.ExecNonQuery("UPDATE PEDIDO SET PE_FOLIO = 0 where PE_FOLIO IS NULL")

		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, PE_FOLIO ) 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, PE_FOLIO " & _
	" 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, PE_FOLIO ")
		c=skmt.ExecQuery("select PE_PRECIO2,PE_TIPO,PE_FOLIO,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
				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
		skmt.ExecNonQuery("delete from pedido WHERE PE_CLIENTE IN (Select CUENTA from cuentaa) ")
		skmt.ExecNonQuery("INSERT INTO PEDIDO SELECT * FROM PEDIDO3")
	
		StartActivity(fila)
	else If l_marca.text = "PROMOS" And l_tipo.Text ="PROMOS" And PROMO_C = 0 Then
		skmt.ExecNonQuery("delete from PROIDID")
		
		skmt.ExecNonQuery2("INSERT INTO PROIDID VALUES (?)", Array As Object(L_PROID.text))
		c=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=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=skmt.ExecQuery("select USUARIO from usuarioa")
		c.Position=0
		usuario = c.GetString("USUARIO")
		'skmt.ExecNonQuery2("INSERT INTO  PEDIDO (PE_CEDIS,PE_COSTO_TOT,PE_COSTOU,PE_CANT,PE_PRONOMBRE,PE_PROID,PE_CLIENTE,PE_FECHA,PE_USUARIO) VALUES(?,?,?,?,?,?,?,?,?) ", Array As Object ("COACALCO",cantidad.Text * l_precio.Text, l_precio.text, cantidad.text, l_desc.text, L_PROID.Text,clie_id, sDate & sTime, usuario))
		'para insertar la promo de ades el de regalo en 0
		e=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=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=skmt.ExecQuery2("select CAT_GP_NOMBRE from " & query & " where CAT_GP_ID = ? ", Array As String(f.GetString("CAT_DP_IDPROD")))
				h.Position=0
				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,Main.tipov,precio2))
				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
		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,Main.tipov,precio2))
		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=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
		skmt.ExecNonQuery("delete from pedido_cliente where PC_CLIENTE In (select cuenta from cuentaa)")
		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"), Main.lon_gps, Main.lat_gps,ALMACEN,cl_ruta,c.GetString("TOTAL_CLIE_SIN")))
		skmt.ExecNonQuery("UPDATE kmt_info set gestion = 2  where CAT_CL_CODIGO In (select cuenta from cuentaa)")
	
	
		skmt.ExecNonQuery("delete from pedido3")
		skmt.ExecNonQuery("UPDATE PEDIDO SET PE_FOLIO = 0 where PE_FOLIO IS NULL")

		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, PE_FOLIO ) 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, PE_FOLIO " & _
	" 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, PE_FOLIO ")
		c=skmt.ExecQuery("select PE_PRECIO2,PE_TIPO,PE_FOLIO,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
				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
		skmt.ExecNonQuery("delete from pedido WHERE PE_CLIENTE IN (Select CUENTA from cuentaa) ")
		skmt.ExecNonQuery("INSERT INTO PEDIDO SELECT * FROM PEDIDO3")
	
		StartActivity(fila)
   
	Else
	
		DateTime.DateFormat = "MM/dd/yyyy"
		sDate=DateTime.Date(DateTime.Now)
		sTime=DateTime.Time(DateTime.Now)
		c=skmt.ExecQuery("Select CUENTA from cuentaa")
		c.Position=0
		clie_id = c.GetString("CUENTA")
		c.Close
		c=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=skmt.ExecQuery("select USUARIO from usuarioa")
		c.Position=0
		usuario = c.GetString("USUARIO")
	
		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,Main.tipov,precio2))
		skmt.ExecNonQuery2("update " & query & " set cat_gp_almacen = cat_gp_almacen - ? where cat_gp_id = ? ", Array As Object(cantidad.Text, L_PROID.Text))
		c=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
		skmt.ExecNonQuery("delete from pedido_cliente where PC_CLIENTE In (select cuenta from cuentaa)")
		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"), Main.lon_gps, Main.lat_gps,ALMACEN,cl_ruta,c.GetString("TOTAL_CLIE_SIN")))
		skmt.ExecNonQuery("UPDATE kmt_info set gestion = 2  where CAT_CL_CODIGO In (select cuenta from cuentaa)")
		Panel1.Visible= False
		
		skmt.ExecNonQuery("delete from pedido3")
		skmt.ExecNonQuery("UPDATE PEDIDO SET PE_FOLIO = 0 where PE_FOLIO IS NULL")

		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, PE_FOLIO ) 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, PE_FOLIO " & _
	" 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, PE_FOLIO ")
		c=skmt.ExecQuery("select PE_PRECIO2,PE_TIPO,PE_FOLIO,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
				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
		skmt.ExecNonQuery("delete from pedido WHERE PE_CLIENTE IN (Select CUENTA from cuentaa) ")
		skmt.ExecNonQuery("INSERT INTO PEDIDO SELECT * FROM PEDIDO3")
	
		StartActivity(fila)
	End If
End Sub
Sub i_fotol_Click
i_fotol.Visible = False

End Sub

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

	StartActivity(detalle_promo)
End Sub
Sub nopromo_Click
	c=skmt.ExecQuery2("select CAT_VA_VALOR from CAT_VARIABLES WHERE CAT_VA_DESCRIPCION = ?", Array As String ("CAMBIOS"))
	c.Position =0
	CAMBIOS = c.GetString("CAT_VA_VALOR")
	c.Close
	If CAMBIOS = "1" Then
	c=skmt.ExecQuery("select COUNT(*) AS CUANTOS from HIST_RMI WHERE HR_CLIENTE IN (Select CUENTA from cuentaa) and  HR_PROID = '" & L_PROID.Text & "'")
	c.Position =0
	EXISTE_CAMBIO = c.GetString("CUANTOS")
	c.Close
	If EXISTE_CAMBIO = 0 Then
		EXISTE_CAMBIO = 0
	Else
		c=skmt.ExecQuery("select SUM(HR_CANT) AS CUANTOS from HIST_RMI WHERE HR_CLIENTE IN (Select CUENTA from cuentaa) and  HR_PROID = '" & L_PROID.Text & "'")
		c.Position =0
		EXISTE_CAMBIO = c.GetString("CUANTOS")
		c.Close
		
	End If
	If EXISTE_CAMBIO = 0 Then
		Msgbox("NO SE VENDIO ESTE PRODUCTO, NO SE PUEDE CAMBIAR","ALERTA")
	ELSE If cantidad.Text = "" Then
	Msgbox("Cantidad no puede ser 0","Cantidad")
	Else If cantidad.Text > EXISTE_CAMBIO Then
			Msgbox("Exede la existencia", "ADVERTENCIA")
		Else
			DateTime.DateFormat = "MM/dd/yyyy"
			sDate=DateTime.Date(DateTime.Now)
			sTime=DateTime.Time(DateTime.Now)
    		c=skmt.ExecQuery("Select CUENTA from cuentaa")
			c.Position=0
			clie_id = c.GetString("CUENTA")
			c=skmt.ExecQuery("select USUARIO from usuarioa")
			c.Position=0
			usuario = c.GetString("USUARIO")
	
			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,Main.tipov,precio2))
			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
		
	Else
		If cantidad.Text > 15 Then
			Msgbox("Exede la existencia", "ADVERTENCIA")
		Else
			DateTime.DateFormat = "MM/dd/yyyy"
			sDate=DateTime.Date(DateTime.Now)
			sTime=DateTime.Time(DateTime.Now)
			c=skmt.ExecQuery("Select CUENTA from cuentaa")
			c.Position=0
			clie_id = c.GetString("CUENTA")
			c=skmt.ExecQuery("select USUARIO from usuarioa")
			c.Position=0
			usuario = c.GetString("USUARIO")
	
			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,Main.tipov,precio2))
			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=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