B4A=true
Group=Default Group
ModulesStructureVersion=1
Type=Activity
Version=6.8
@EndOfDesignText@
#Region  Activity Attributes 
	#FullScreen: False
	#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 skmt As SQL
	 Dim g As GPS
	Dim clie_id As String
    Dim sDate,sTime As String
	Dim usuario As String
    
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 d As Cursor
	Dim ruta As String
	Dim Regresar As Button
	

	Dim ListView1 As ListView
	Dim L_CANT As Label
	Dim L_TOTAL As Label
	Dim borra As Button
	Dim Existe As String
	Dim result As String
'    Dim lat_gps, lon_gps As String
    Dim DESC As String

	Private Titulo As Label
	Private b_desc As Button
	Dim DESCUENTO As String
	Dim folio As String
	Dim TIPO As String
End Sub

Sub Activity_Create(FirstTime As Boolean)
	If(FirstTime) Then
		g.Initialize("GPS")
	End If
	ruta = File.DirInternal
	Activity.LoadLayout("tarjeta")
	borra.Visible = True
	Titulo.Text = "Pedido Actual"
	' 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)
	skmt.ExecNonQuery("update pedido set pe_folio = 0 where pe_folio is null")
	   c=skmt.ExecQuery("select count(*) as EXISTE from pedido_cliente WHERE pc_cliente IN (Select CUENTA from cuentaa)")
	   C.Position=0
	   Existe = C.GetString("EXISTE")
	   C.Close
	   c=skmt.ExecQuery("select PE_PRONOMBRE,PE_COSTO_TOT, PE_CANT, PE_FOLIO FROM PEDIDO WHERE PE_CLIENTE IN (Select CUENTA from cuentaa) order by PE_PRONOMBRE asc")
	   ListView1.Clear
	  
	   If c.RowCount>0 Then
	      For i=0 To c.RowCount -1
		     c.Position=i
			 Dim label1 As Label
			 label1 = ListView1.TwoLinesLayout.Label
			 label1.TextSize = 15
			 label1.TextColor = Colors.Black
			 Dim label2 As Label
			 label2 = ListView1.TwoLinesLayout.SecondLabel
			 label2.TextSize = 10
			 label2.TextColor = Colors.Blue
			 ListView1.AddTwoLines(c.GetString("PE_PRONOMBRE"),"Cantidad #"& c.GetString("PE_CANT")& " SubTotal $"& c.GetString("PE_COSTO_TOT")& " Folio "& c.GetString("PE_FOLIO"))
  		  	folio = c.GetString("PE_FOLIO")	      
		  Next
	   End If
	If Existe <> 0 Then
	c=skmt.ExecQuery("select pc_noart, pc_monto from pedido_cliente where pc_cliente in (Select CUENTA from cuentaa)")
	C.Position=0
	L_CANT.Text = c.GetString("PC_NOART")
	L_TOTAL.Text = c.GetString("PC_MONTO")
	End If
		 'la_no_ird.Text = c.GetString("PR_CF_SALDO_ACORT")
		
			 
			 'ListView1.AddTwoLines(c.GetString("pr_cd_nombre"),c.GetString("pr_cd_colonia"))
	    '  Next
	   'End If

End Sub

Sub Activity_Resume
If g.GPSEnabled=False Then
	  ToastMessageShow("Habilitar el GPS", True)
	  StartActivity(g.LocationSettingsIntent)
	 Else
	  g.Start(0,0)
End If

L_CANT.Text =""
L_TOTAL.Text=""
	skmt.ExecNonQuery("update pedido set pe_folio = 0 where pe_folio is null")

 c=skmt.ExecQuery("select count(*) as EXISTE from pedido_cliente WHERE pc_cliente IN (Select CUENTA from cuentaa)")
	   C.Position=0
	   Existe = C.GetString("EXISTE")
	   C.Close
    c=skmt.ExecQuery("select PE_PRONOMBRE,PE_COSTO_TOT, PE_CANT, PE_FOLIO FROM PEDIDO WHERE PE_CLIENTE IN (Select CUENTA from cuentaa) order by PE_PRONOMBRE asc")
	   ListView1.Clear
	  
	   If c.RowCount>0 Then
	      For i=0 To c.RowCount -1
		     c.Position=i
			 Dim label1 As Label
			 label1 = ListView1.TwoLinesLayout.Label
			 label1.TextSize = 15
			 label1.TextColor = Colors.Black
			 Dim label2 As Label
			 label2 = ListView1.TwoLinesLayout.SecondLabel
			 label2.TextSize = 10
			 label2.TextColor = Colors.Blue
			 ListView1.AddTwoLines(c.GetString("PE_PRONOMBRE"),"Cantidad #"& c.GetString("PE_CANT")& " SubTotal $"& c.GetString("PE_COSTO_TOT")& " Folio "& c.GetString("PE_FOLIO"))
		  	folio = c.GetString("PE_FOLIO")	      
	      Next
	   End If
	If Existe <> 0 Then
	c=skmt.ExecQuery("select pc_noart, pc_monto from pedido_cliente where pc_cliente in (Select CUENTA from cuentaa)")
	C.Position=0
	L_CANT.Text = c.GetString("PC_NOART")
	L_TOTAL.Text = c.GetString("PC_MONTO")
	c.Close
    c=skmt.ExecQuery("select SUM(IFNULL(PE_DESC,0)) AS DESCUENTO FROM PEDIDO WHERE PE_CLIENTE IN (Select CUENTA from cuentaa)")
    c.Position=0
	DESC = c.GetString("DESCUENTO")	
	'If L_TOTAL.Text > 1000 And DESC = 0 Then 
	
	'b_desc.Visible = True
	'b_desc.Text = "- 5% DESC"
	'DESCUENTO = "MENOS"
	'Else if L_TOTAL.Text > 1000 Then
	'b_desc.Visible = True
	'b_desc.Text = "Cancela Desc"
	'DESCUENTO =  "MAS"
    'Else
	b_desc.Visible = False
		
	'End If
	End If
			 'la_no_ird.Text = c.GetString("PR_CF_SALDO_ACORT")
		

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

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



Sub Regresar_Click
	StartActivity(fila)
End Sub
Sub Activity_KeyPress (key As Int) As Boolean
    ' BACK key pressed
    If key=KeyCodes.KEYCODE_BACK Then
            ' I want to capture the key here so I return True
			StartActivity(seleccion)
            'Return True
    End If
    ' Returning False signals the system to handle the key
    Return False
End Sub
Sub borra_Click
If folio <> "" Then	
	result =  Msgbox2("Seguro que desa borrar el pedido?","Cancelar pedido", "Si", "", "No",LoadBitmap(File.DirAssets,"alert2.png"))
	If result = DialogResponse.POSITIVE Then
	
			c=skmt.ExecQuery("select PE_PROID,PE_CANT, PE_TIPO FROM PEDIDO where pe_cliente in (Select CUENTA from cuentaa) ")
	   If c.RowCount>0 Then
	      For i=0 To c.RowCount -1
		     c.Position=i
			 
					TIPO = c.GetString("PE_TIPO")
					If c.GetString("PE_TIPO") = "VENTA" Then
						skmt.ExecNonQuery2("update cat_gunaprod2 set cat_gp_almacen = cat_gp_almacen + ? where cat_gp_id = ?", Array As Object(c.GetString("PE_CANT"),c.GetString("PE_PROID")))
					Else
						skmt.ExecNonQuery2("update cat_gunaprod set cat_gp_almacen = cat_gp_almacen + ? where cat_gp_id = ?", Array As Object(c.GetString("PE_CANT"),c.GetString("PE_PROID")))
					End If
	      Next
	   End If
	
	
	skmt.ExecNonQuery("delete from pedido_cliente where pc_cliente in (Select CUENTA from cuentaa)")
	skmt.ExecNonQuery("delete from pedido where pe_cliente in (Select CUENTA from cuentaa)")
	skmt.ExecNonQuery("UPDATE kmt_info set gestion = 0  where CAT_CL_CODIGO In (select cuenta from cuentaa)")		
   Activity_Resume
	End If
Else
Msgbox("CLIENTE YA SE TRANSMITIO, FAVOR DE LLAMAR A SOPORTE PARA ASISTENCIA","AVISO")
End If

End Sub
Sub ListView1_ItemLongClick (Position As Int, Value As Object)
	result =  Msgbox2("Seguro que desa borrar este articulo?","Borrar Articulo", "Si", "", "No",LoadBitmap(File.DirAssets,"alert2.png"))
	
	If result = DialogResponse.POSITIVE Then
		
'		If TIPO = "VENTA" Then
'			c=skmt.ExecQuery2("select count(*) AS CUANTOS from cat_gunaprod2 where  CAT_GP_ID in (select pe_cedis from pedido where pe_pronombre = ? and pe_cliente in (Select CUENTA from cuentaa)) and CAT_GP_CLASIF = 'PROMOS' AND CAT_GP_TIPO = 'PROMOS' AND CAT_GP_SUBTIPO = 'PROMOS'", Array As String(Value))
'		Else
'			c=skmt.ExecQuery2("select count(*) AS CUANTOS from cat_gunaprod where  CAT_GP_ID in (select pe_cedis from pedido where pe_pronombre = ? and pe_cliente in (Select CUENTA from cuentaa)) and CAT_GP_CLASIF = 'PROMOS' AND CAT_GP_TIPO = 'PROMOS' AND CAT_GP_SUBTIPO = 'PROMOS'", Array As String(Value))
'		End If
		c=skmt.ExecQuery2("SELECT COUNT(*) as CUANTOS FROM PEDIDO WHERE pe_cedis in (select pe_cedis from pedido where pe_pronombre = ?) and pe_cliente in (Select CUENTA from cuentaa) and length(pe_cedis) > 3 ", Array As String(Value))
		c.Position=0
		If c.GetString("CUANTOS") > 0 Then
			
			d=skmt.ExecQuery2("select PE_PROID,PE_CANT, PE_TIPO FROM PEDIDO where pe_cedis in (select pe_cedis from pedido where pe_pronombre = ?) and pe_cliente in (Select CUENTA from cuentaa)  and length(pe_cedis) > 3", Array As String(Value))
	  
			If d.RowCount>0 Then
				For i=0 To d.RowCount -1
					d.Position=i
					TIPO = d.GetString("PE_TIPO")
					If d.GetString("PE_TIPO") = "VENTA" Then
						skmt.ExecNonQuery2("update cat_gunaprod2 set cat_gp_almacen = cat_gp_almacen + ? where cat_gp_id = ?", Array As Object(d.GetString("PE_CANT"),d.GetString("PE_PROID")))
					Else
						skmt.ExecNonQuery2("update cat_gunaprod set cat_gp_almacen = cat_gp_almacen + ? where cat_gp_id = ?", Array As Object(d.GetString("PE_CANT"),d.GetString("PE_PROID")))
					End If
				Next
			End If
			d.Close

			
			skmt.ExecNonQuery2("delete from pedido where pe_cedis in (select pe_cedis from pedido where pe_pronombre = ?) and pe_cliente in (Select CUENTA from cuentaa)", Array As Object(Value))
		Else
			d=skmt.ExecQuery2("select PE_PROID,PE_CANT, PE_TIPO FROM PEDIDO where pe_pronombre = ?  and pe_cliente in (Select CUENTA from cuentaa) ", Array As String(Value))
			d.Position = 0
			
					TIPO = d.GetString("PE_TIPO")
					If d.GetString("PE_TIPO") = "VENTA" Then
						skmt.ExecNonQuery2("update cat_gunaprod2 set cat_gp_almacen = cat_gp_almacen + ? where cat_gp_id = ?", Array As Object(d.GetString("PE_CANT"),d.GetString("PE_PROID")))
					Else
						skmt.ExecNonQuery2("update cat_gunaprod set cat_gp_almacen = cat_gp_almacen + ? where cat_gp_id = ?", Array As Object(d.GetString("PE_CANT"),d.GetString("PE_PROID")))
					End If
			d.Close
			
			
			skmt.ExecNonQuery2("delete from pedido where pe_pronombre = ?  and pe_cliente in (Select CUENTA from cuentaa)", Array As Object(Value))
		End If
		
		
	   
	
	 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")
	
	c=skmt.ExecQuery("select sum(pe_costo_tot) as TOTAL_CLIE, SUM(PE_CANT) AS CANT_CLIE, count(*) as CUANTOS FROM PEDIDO WHERE PE_CLIENTE IN (Select CUENTA from cuentaa)")
	c.Position=0
	If c.GetString("CUANTOS") > 0 Then
	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) VALUES (?,?,?,?,?,?,?)", Array As Object(clie_id, sDate & sTime, usuario, c.GetString("CANT_CLIE"),c.GetString("TOTAL_CLIE"), Main.lon_gps, Main.lat_gps))
	skmt.ExecNonQuery("UPDATE kmt_info set gestion = 2  where CAT_CL_CODIGO In (select cuenta from cuentaa)")		
	Else
	skmt.ExecNonQuery("delete from pedido_cliente where PC_CLIENTE In (select cuenta from cuentaa)")
	skmt.ExecNonQuery("UPDATE kmt_info set gestion = 0  where CAT_CL_CODIGO In (select cuenta from cuentaa)")		
	End If
    Activity_Resume


	 End If
End Sub
Sub b_desc_Click
	If DESCUENTO = "MENOS" Then
	DateTime.DateFormat = "MM/dd/yyyy"
	sDate=DateTime.Date(DateTime.Now)
	sTime=DateTime.Time(DateTime.Now)
    c=skmt.ExecQuery("Select CUENTA from cuentaa")
	c.Position=0
	clie_id = c.GetString("CUENTA")
	c.Close
	c=skmt.ExecQuery("select USUARIO from usuarioa")
	c.Position=0
	usuario = c.GetString("USUARIO")
    c.Close
	result =  Msgbox2("Seguro que desa dar descuento del 5%?","Dar descuento", "Si", "", "No",LoadBitmap(File.DirAssets,"alert2.png"))
	If result = DialogResponse.POSITIVE Then
	skmt.ExecNonQuery("UPDATE pedido SET PE_COSTO_TOT = PE_COSTO_TOT * .95, PE_DESC = 5 WHERE PE_CLIENTE In (select cuenta from cuentaa)")		
	c=skmt.ExecQuery("select sum(pe_costo_tot) as TOTAL_CLIE, SUM(PE_CANT) AS CANT_CLIE, count(*) as CUANTOS FROM PEDIDO WHERE PE_CLIENTE IN (Select CUENTA from cuentaa)")
	c.Position=0
	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) VALUES (?,?,?,?,?,?,?)", Array As Object(clie_id, sDate & sTime, usuario, c.GetString("CANT_CLIE"),c.GetString("TOTAL_CLIE"), Main.lon_gps, Main.lat_gps))
    c.Close
	Activity_Resume
	End If
	ELSE IF DESCUENTO = "MAS" Then
	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 USUARIO from usuarioa")
	c.Position=0
	usuario = c.GetString("USUARIO")
    c.Close
	result =  Msgbox2("Seguro que desa CANCELAR el descuento?","Cancelar descuento", "Si", "", "No",LoadBitmap(File.DirAssets,"alert2.png"))
	If result = DialogResponse.POSITIVE Then
	skmt.ExecNonQuery("UPDATE pedido SET PE_COSTO_TOT = PE_COSTO_TOT / .95, PE_DESC = 0 WHERE PE_CLIENTE In (select cuenta from cuentaa)")		
	c=skmt.ExecQuery("select sum(pe_costo_tot) as TOTAL_CLIE, SUM(PE_CANT) AS CANT_CLIE, count(*) as CUANTOS FROM PEDIDO WHERE PE_CLIENTE IN (Select CUENTA from cuentaa)")
	c.Position=0
	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) VALUES (?,?,?,?,?,?,?)", Array As Object(clie_id, sDate & sTime, usuario, c.GetString("CANT_CLIE"),c.GetString("TOTAL_CLIE"),Main.lon_gps, Main.lat_gps))
    c.Close
	Activity_Resume
	End If
	End If
End Sub