VERSION 5.00
Object = "{5E9E78A0-531B-11CF-91F6-C2863C385E30}#1.0#0"; "MSFLXGRD.OCX"
Object = "{831FDD16-0C5C-11D2-A9FC-0000F8754DA1}#2.2#0"; "MSCOMCTL.OCX"
Begin VB.Form Form_ordena_alumnos 
   BorderStyle     =   1  'Fixed Single
   Caption         =   "Ordenar Alumnos En Cualquier Orden"
   ClientHeight    =   9615
   ClientLeft      =   45
   ClientTop       =   375
   ClientWidth     =   11640
   Icon            =   "Form_ordena_alumnos.frx":0000
   LinkTopic       =   "Form1"
   MaxButton       =   0   'False
   MDIChild        =   -1  'True
   MinButton       =   0   'False
   ScaleHeight     =   9615
   ScaleWidth      =   11640
   Begin VB.CommandButton cmd_salir 
      Caption         =   "Salir"
      Height          =   855
      Left            =   10080
      Picture         =   "Form_ordena_alumnos.frx":2A8B2
      Style           =   1  'Graphical
      TabIndex        =   14
      Top             =   240
      Width           =   1335
   End
   Begin VB.CommandButton btn_Ver 
      Caption         =   "Ver"
      Height          =   855
      Left            =   8520
      Picture         =   "Form_ordena_alumnos.frx":2AED8
      Style           =   1  'Graphical
      TabIndex        =   10
      Top             =   240
      Width           =   1335
   End
   Begin VB.Frame Frame2 
      Caption         =   "Seleccione Curso"
      BeginProperty Font 
         Name            =   "Arial"
         Size            =   8.25
         Charset         =   0
         Weight          =   700
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      ForeColor       =   &H00C00000&
      Height          =   1095
      Left            =   120
      TabIndex        =   1
      Top             =   120
      Width           =   8175
      Begin VB.CheckBox Check_Letra 
         Caption         =   "Letra"
         ForeColor       =   &H00000000&
         Height          =   195
         Left            =   6840
         TabIndex        =   2
         Top             =   240
         Value           =   1  'Checked
         Visible         =   0   'False
         Width           =   1095
      End
      Begin MSComctlLib.ImageCombo IC_Ensenanza 
         Height          =   345
         Left            =   1440
         TabIndex        =   3
         Top             =   480
         Width           =   4095
         _ExtentX        =   7223
         _ExtentY        =   609
         _Version        =   393216
         ForeColor       =   -2147483640
         BackColor       =   -2147483643
         BeginProperty Font {0BE35203-8F91-11CE-9DE3-00AA004BB851} 
            Name            =   "Arial"
            Size            =   8.25
            Charset         =   0
            Weight          =   400
            Underline       =   0   'False
            Italic          =   0   'False
            Strikethrough   =   0   'False
         EndProperty
         Locked          =   -1  'True
         Text            =   "IC_Ensenanza"
      End
      Begin MSComctlLib.ImageCombo IC_Nivel 
         Height          =   345
         Left            =   5640
         TabIndex        =   4
         Top             =   480
         Width           =   1095
         _ExtentX        =   1931
         _ExtentY        =   609
         _Version        =   393216
         ForeColor       =   -2147483640
         BackColor       =   -2147483643
         BeginProperty Font {0BE35203-8F91-11CE-9DE3-00AA004BB851} 
            Name            =   "Arial"
            Size            =   8.25
            Charset         =   0
            Weight          =   400
            Underline       =   0   'False
            Italic          =   0   'False
            Strikethrough   =   0   'False
         EndProperty
         Locked          =   -1  'True
         Text            =   "IC_Nivel"
      End
      Begin MSComctlLib.ImageCombo IC_Letra 
         Height          =   345
         Left            =   6840
         TabIndex        =   5
         Top             =   480
         Width           =   1095
         _ExtentX        =   1931
         _ExtentY        =   609
         _Version        =   393216
         ForeColor       =   -2147483640
         BackColor       =   -2147483643
         BeginProperty Font {0BE35203-8F91-11CE-9DE3-00AA004BB851} 
            Name            =   "Arial"
            Size            =   8.25
            Charset         =   0
            Weight          =   400
            Underline       =   0   'False
            Italic          =   0   'False
            Strikethrough   =   0   'False
         EndProperty
         Locked          =   -1  'True
         Text            =   "IC_Letra"
      End
      Begin MSComctlLib.ImageCombo IC_Agno 
         Height          =   345
         Left            =   240
         TabIndex        =   6
         Top             =   480
         Width           =   1095
         _ExtentX        =   1931
         _ExtentY        =   609
         _Version        =   393216
         ForeColor       =   -2147483640
         BackColor       =   -2147483643
         BeginProperty Font {0BE35203-8F91-11CE-9DE3-00AA004BB851} 
            Name            =   "Arial"
            Size            =   8.25
            Charset         =   0
            Weight          =   400
            Underline       =   0   'False
            Italic          =   0   'False
            Strikethrough   =   0   'False
         EndProperty
         Locked          =   -1  'True
         Text            =   "IC_Agno"
      End
      Begin VB.Label Label1 
         Alignment       =   2  'Center
         Caption         =   "Año"
         BeginProperty Font 
            Name            =   "Arial"
            Size            =   8.25
            Charset         =   0
            Weight          =   400
            Underline       =   0   'False
            Italic          =   0   'False
            Strikethrough   =   0   'False
         EndProperty
         ForeColor       =   &H00000000&
         Height          =   255
         Left            =   120
         TabIndex        =   9
         Top             =   240
         Width           =   495
      End
      Begin VB.Label Label2 
         Alignment       =   2  'Center
         Caption         =   "Enseñanza"
         BeginProperty Font 
            Name            =   "Arial"
            Size            =   8.25
            Charset         =   0
            Weight          =   400
            Underline       =   0   'False
            Italic          =   0   'False
            Strikethrough   =   0   'False
         EndProperty
         ForeColor       =   &H00000000&
         Height          =   255
         Left            =   1440
         TabIndex        =   8
         Top             =   240
         Width           =   975
      End
      Begin VB.Label Label3 
         Alignment       =   2  'Center
         Caption         =   "Nivel"
         BeginProperty Font 
            Name            =   "Arial"
            Size            =   8.25
            Charset         =   0
            Weight          =   400
            Underline       =   0   'False
            Italic          =   0   'False
            Strikethrough   =   0   'False
         EndProperty
         ForeColor       =   &H00000000&
         Height          =   255
         Left            =   5520
         TabIndex        =   7
         Top             =   240
         Width           =   615
      End
   End
   Begin VB.Frame Frame1 
      Caption         =   "Listado de Alumnos"
      BeginProperty Font 
         Name            =   "Arial"
         Size            =   8.25
         Charset         =   0
         Weight          =   700
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      ForeColor       =   &H00C00000&
      Height          =   8055
      Left            =   120
      TabIndex        =   0
      Top             =   1440
      Width           =   11295
      Begin VB.CommandButton cmd_bajar 
         Caption         =   "Bajar"
         BeginProperty Font 
            Name            =   "Arial"
            Size            =   8.25
            Charset         =   0
            Weight          =   400
            Underline       =   0   'False
            Italic          =   0   'False
            Strikethrough   =   0   'False
         EndProperty
         Height          =   795
         Index           =   0
         Left            =   10080
         Picture         =   "Form_ordena_alumnos.frx":2B44F
         Style           =   1  'Graphical
         TabIndex        =   13
         Top             =   240
         Width           =   855
      End
      Begin VB.CommandButton cmd_subir 
         Caption         =   "Subir"
         BeginProperty Font 
            Name            =   "Arial"
            Size            =   8.25
            Charset         =   0
            Weight          =   400
            Underline       =   0   'False
            Italic          =   0   'False
            Strikethrough   =   0   'False
         EndProperty
         Height          =   795
         Index           =   0
         Left            =   9120
         Picture         =   "Form_ordena_alumnos.frx":2BA65
         Style           =   1  'Graphical
         TabIndex        =   12
         Top             =   240
         Width           =   855
      End
      Begin MSFlexGridLib.MSFlexGrid MSFlexGrid1 
         Height          =   6615
         Left            =   120
         TabIndex        =   11
         Top             =   1200
         Width           =   10935
         _ExtentX        =   19288
         _ExtentY        =   11668
         _Version        =   393216
         Cols            =   7
         FixedCols       =   0
         AllowBigSelection=   -1  'True
         FocusRect       =   0
         SelectionMode   =   1
      End
   End
End
Attribute VB_Name = "Form_ordena_alumnos"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Private Sub btn_Ver_Click()
    Dim var_sql As String
    Dim rec As ADODB.Recordset
    Set rec = New ADODB.Recordset
    
    var_sql = "SELECT orden_libro,p.ape_pat_alu Paterno, p.ape_mat_alu Materno, p.nom_alu Nombres, p.ci_alu,a.fec_mat as fec_matricula,num_mat Num_Matricula" _
            & " FROM postulantes p, alu_x_cur a, matriculas m" _
            & " WHERE p.ci_alu = a.ci_alu AND a.ci_alu=m.ci_alu AND a.fec_mat=m.fec_mat" _
            & " AND a.cod_ens = '" & Mid(IC_Ensenanza.SelectedItem.Key, 2) & "' AND a.num_gra = '" & val(IC_Nivel.Text) & "'" _
            & " AND a.let_cur ='" & IC_Letra.Text & "' AND a.agno = " & IC_Agno.Text & " AND a.agno=m.anno_mat" _
            & " ORDER BY orden_libro,a.fec_ing_curso, m.num_mat, p.ape_pat_alu, p.ape_mat_alu, p.nom_alu"
    rec.Open var_sql, conn
    Call Rec2Grid(rec, MSFlexGrid1, True)
    rec.Close

End Sub

Private Sub cmd_bajar_Click(Index As Integer)
    Dim rut As String
    Dim accion_realizada As String
    Dim rec As ADODB.Recordset
    Set rec = New ADODB.Recordset
    Dim var_sql As String
    Dim log As Boolean
    rut = MSFlexGrid1.TextMatrix(MSFlexGrid1.Row, 5)
    QRY = "SELECT orden_libro FROM matriculas WHERE anno_mat = '" & val(IC_Agno.Text) & "' AND fec_mat_r IS NOT NULL AND ci_alu = '" & rut & "'"
    rec.Open QRY, conn
    If rec("orden_libro") = 0 Then
        ' Llamo funcion que coloca numero de orden
        Call asigna_numero_orden
    'REC.Close
    'Exit Sub
    End If
        
    ' Subo el numero de orden
    If MSFlexGrid1.Row <> (MSFlexGrid1.Rows - 1) Then
        var_sql = "UPDATE matriculas SET orden_libro = orden_libro + 1 WHERE anno_mat = '" & val(IC_Agno.Text) & "' AND fec_mat_r IS NOT NULL AND ci_alu = '" & rut & "'"
        conn.Execute var_sql
        var_sql = "UPDATE matriculas SET orden_libro = orden_libro - 1 WHERE anno_mat = '" & val(IC_Agno.Text) & "' AND fec_mat_r IS NOT NULL AND ci_alu = '" & MSFlexGrid1.TextMatrix((MSFlexGrid1.Row + 1), 5) & "'"
        conn.Execute var_sql
        
        i = MSFlexGrid1.Row
        Me.MSFlexGrid1.Row = i + 1
        Me.MSFlexGrid1.RowSel = i + 1
        Me.MSFlexGrid1.Col = 0
        Me.MSFlexGrid1.ColSel = Me.MSFlexGrid1.Cols - 1
    End If
     
    'Asignar log
    Select Case Mid(IC_Ensenanza.SelectedItem.Key, 2)
    Case "996"
    grado = "PK"
    Case "010"
    grado = "K"
    Case "110"
    grado = "EB"
    Case "310"
    grado = "EM"
    End Select
    curso = CStr(val(IC_Nivel.Text)) & "º " & IC_Letra.Text & " " & grado
    accion_realizada = "Se cambio de orden a alumno con rut " & rut & " del curso " & curso
    log = Inserta_Vitacora_Alumno(rut, accion_realizada)
     
    'Refresco Grilla
    btn_Ver_Click
    
    rec.Close
End Sub

Private Sub cmd_salir_Click()
    Unload Me
End Sub

Private Sub cmd_subir_Click(Index As Integer)
    Dim log As Boolean
    Dim accion_realizada As String
    Dim rut As String
    Dim rec As ADODB.Recordset
    Set rec = New ADODB.Recordset
    Dim var_sql As String
    rut = MSFlexGrid1.TextMatrix(MSFlexGrid1.Row, 5)
    QRY = "SELECT orden_libro FROM matriculas WHERE anno_mat = '" & val(IC_Agno.Text) & "' AND fec_mat_r IS NOT NULL AND ci_alu = '" & rut & "'"
    rec.Open QRY, conn
    If rec("orden_libro") = 0 Then
        ' Llamo funcion que coloca numero de orden
        Call asigna_numero_orden
'        REC.Close
'        Exit Sub
    End If
        
    ' Subo el numero de orden
    If MSFlexGrid1.Row <> 1 Then
        var_sql = "UPDATE matriculas SET orden_libro = orden_libro - 1 WHERE anno_mat = '" & val(IC_Agno.Text) & "' AND fec_mat_r IS NOT NULL AND ci_alu = '" & rut & "'"
        conn.Execute var_sql
        var_sql = "UPDATE matriculas SET orden_libro = orden_libro + 1 WHERE anno_mat = '" & val(IC_Agno.Text) & "' AND fec_mat_r IS NOT NULL AND ci_alu = '" & MSFlexGrid1.TextMatrix((MSFlexGrid1.Row - 1), 5) & "'"
        conn.Execute var_sql
        
        i = MSFlexGrid1.Row
        Me.MSFlexGrid1.Row = i - 1
        Me.MSFlexGrid1.RowSel = i - 1
        Me.MSFlexGrid1.Col = 0
        Me.MSFlexGrid1.ColSel = Me.MSFlexGrid1.Cols - 1
        
    End If
       
    
    'Asignar log
    Select Case Mid(IC_Ensenanza.SelectedItem.Key, 2)
    Case "996"
    grado = "PK"
    Case "010"
    grado = "K"
    Case "110"
    grado = "EB"
    Case "310"
    grado = "EM"
    End Select
    curso = CStr(val(IC_Nivel.Text)) & "º " & IC_Letra.Text & " " & grado
    accion_realizada = "Se cambio de orden a alumno con rut " & rut & " del curso " & curso
    log = Inserta_Vitacora_Alumno(rut, accion_realizada)
    
     ' Refresco Grilla
    btn_Ver_Click
    
    rec.Close
End Sub

Private Sub Form_Load()
    Me.Top = 0
    Me.Left = 0
    IC_Letra.Text = ""
    IC_Letra.ComboItems.Clear
    Call Set_IC_Agno
End Sub

Private Sub IC_Agno_Click()
    IC_Ensenanza.Enabled = True
    Set_IC_Ensenanza
End Sub

Private Sub Set_IC_Ensenanza()
    Dim rec As ADODB.Recordset
    Dim QRY As String
    
    QRY = "SELECT DISTINCT m.cod_ens, ce.des_ens FROM matriculas as m, codigos_ensenanza as ce WHERE m.cod_ens=ce.cod_ens AND m.anno_mat=" & IC_Agno.SelectedItem.Text & " ORDER BY ce.ord_ens;"
    Set rec = New ADODB.Recordset
    rec.Open QRY, conn
    
    IC_Ensenanza.ComboItems.Clear
    If rec.RecordCount > 0 Then
        rec.MoveFirst
        For i = 1 To rec.RecordCount
            IC_Ensenanza.ComboItems.Add i, "K" & Format(rec!Cod_Ens, "000"), rec!des_ens
            rec.MoveNext
        Next i
        Set IC_Ensenanza.SelectedItem = IC_Ensenanza.ComboItems.Item(1)
        ErrorStatus = False
        ' Llenar Siguiente Combo.
        IC_Nivel.Enabled = True
        IC_Letra.Enabled = False
        Call Set_IC_Nivel
    Else
        ErrorStatus = True
        IC_Ensenanza.Enabled = False
        IC_Nivel.Enabled = False
        IC_Letra.Enabled = False
    End If
    rec.Close
End Sub



Private Sub IC_Ensenanza_Click()
    IC_Nivel.Enabled = True
    Call Set_IC_Nivel
End Sub

Private Sub Set_IC_Nivel()
    Dim rec As ADODB.Recordset
    Dim QRY As String

    QRY = "SELECT DISTINCT m.num_gra FROM matriculas as m WHERE m.cod_ens='" & Mid(IC_Ensenanza.SelectedItem.Key, 2) & "' AND m.anno_mat=" & IC_Agno.SelectedItem.Text & " ORDER BY m.num_gra;"
    Set rec = New ADODB.Recordset
    rec.Open QRY, conn
    
    IC_Nivel.ComboItems.Clear
    If rec.RecordCount > 0 Then
        rec.MoveFirst
        For i = 1 To rec.RecordCount
            IC_Nivel.ComboItems.Add i, , str(rec!Num_Gra)
            rec.MoveNext
        Next i
        Set IC_Nivel.SelectedItem = IC_Nivel.ComboItems.Item(1)
        ErrorStatus = False
        ' Llenar Siguiente Combo.
        If Check_Letra.Value = 1 Then
            IC_Letra.Enabled = True
            Call Set_IC_Letra
        End If
    Else
        ErrorStatus = True
        IC_Nivel.Enabled = False
        IC_Letra.Enabled = False
    End If
    rec.Close
End Sub

Private Sub IC_Nivel_Click()
  '  If Check_Letra.Value = 1 Then
        IC_Letra.Enabled = True
        Call Set_IC_Letra
   ' End If
End Sub

Private Sub Set_IC_Letra()
    Dim rec As ADODB.Recordset
    Dim QRY As String
    
    'If Check_Letra.Value = 1 Then
        IC_Letra.ComboItems.Clear
        IC_Letra.Text = ""
        QRY = "SELECT DISTINCT m.let_cur FROM alu_x_cur as m WHERE m.cod_ens='" & Mid(IC_Ensenanza.SelectedItem.Key, 2) & "' AND m.agno=" & Trim(IC_Agno.SelectedItem.Text) & " AND m.num_gra='" & Trim(IC_Nivel.SelectedItem.Text) & "' ORDER BY m.num_gra;"
        Set rec = New ADODB.Recordset
        rec.Open QRY, conn
        If rec.RecordCount > 0 Then
            rec.MoveFirst
            For i = 1 To rec.RecordCount
                IC_Letra.ComboItems.Add i, , rec!Let_Cur
                rec.MoveNext
            Next i
            Set IC_Letra.SelectedItem = IC_Letra.ComboItems.Item(1)
            ErrorStatus = False
            ' Llenar Siguiente Combo.
        Else
            ErrorStatus = True
            Check_Letra.Value = 0
            IC_Letra.Enabled = False
        End If
        rec.Close
'    Else
'        ErrorStatus = False
'        IC_Letra.Enabled = False
'    End If
End Sub

Private Sub Check_Letra_Click()
    If Check_Letra.Value = 0 Then
        IC_Letra.Enabled = False
    ElseIf Check_Letra.Value = 1 And Not (ErrorStatus) And IC_Ensenanza.Enabled And IC_Nivel.Enabled Then
        IC_Letra.Enabled = True
        Call Set_IC_Letra
    Else
        Check_Letra.Value = 0
        IC_Letra.Enabled = False
    End If
End Sub

Private Sub Set_IC_Agno()
    Dim agno As Integer
    
  '  agno = IIf(Year(Date) < 2005, 2005, Year(Date))
  '  IC_Agno.ComboItems.Clear
'    For i = 2005 To (Anno)
'        IC_Agno.ComboItems.Add i, , str(i + 2004)
'    Next i
'    Set IC_Agno.SelectedItem = IC_Agno.ComboItems.Item(i - 1)
'    ErrorStatus = False
    
    agno = IIf(Year(Date) < 2005, 2005, Year(Date))
    IC_Agno.ComboItems.Clear
    For i = 1 To (agno - 2004)
        IC_Agno.ComboItems.Add i, , str(i + 2004)
    Next i
    Set IC_Agno.SelectedItem = IC_Agno.ComboItems.Item(i - 1)
    ErrorStatus = False
    
    ' Llenar combo enseñanza
    IC_Ensenanza.Enabled = True
    IC_Nivel.Enabled = False
    IC_Letra.Enabled = False
    Call Set_IC_Ensenanza
End Sub

Private Sub Rec2Grid(ByRef rec As ADODB.Recordset, ByRef grid As Object, Optional ByVal Numeracion As Boolean = True)
    grid.Clear
    If rec.RecordCount > 0 Then
        grid.Cols = rec.Fields.count + 1
        grid.Rows = rec.RecordCount + 1
        grid.FixedCols = 1
        grid.FixedRows = 1
        grid.TextMatrix(0, 0) = "Nº"
        grid.ColWidth(0) = IIf(Numeracion, TextWidth(grid.TextMatrix(0, 0)) + 100, 0)
        For i = 1 To rec.Fields.count
            grid.TextMatrix(0, i) = UCase(rec.Fields(i - 1).Name)
            grid.ColWidth(i) = TextWidth(grid.TextMatrix(0, i)) + 100
        Next i
        rec.MoveFirst
        For i = 1 To rec.RecordCount
            'progreso.Value = i
            grid.TextMatrix(i, 0) = i
            grid.ColWidth(0) = IIf(Numeracion, IIf(TextWidth(grid.TextMatrix(i, 0)) + 100 > grid.ColWidth(0), TextWidth(grid.TextMatrix(i, 0)) + 100, grid.ColWidth(0)), 0)
            For j = 1 To rec.Fields.count
                grid.TextMatrix(i, j) = IIf(IsNull(rec(j - 1)), "Null", rec(j - 1))
                grid.ColWidth(j) = IIf(TextWidth(grid.TextMatrix(i, j)) + 100 > grid.ColWidth(j), TextWidth(grid.TextMatrix(i, j)) + 100, grid.ColWidth(j))
            Next j
            rec.MoveNext
        Next i
    Else
        grid.Cols = 2
        grid.Rows = 2
    End If
End Sub

Private Sub asigna_numero_orden()
    Dim i As Integer
'    Dim REC As ADODB.Recordset
'    Set REC = New ADODB.Recordset
    Dim var_sql As String
'    REC.Open var_sql, conn
'    REC.Close
    For i = 1 To MSFlexGrid1.Rows - 1
        rut = MSFlexGrid1.TextMatrix(i, 5)
        var_sql = "UPDATE matriculas SET orden_libro = " & i & " WHERE anno_mat = '" & val(IC_Agno.Text) & "' AND fec_mat_r IS NOT NULL AND ci_alu = '" & rut & "'"
        conn.Execute var_sql
    Next i
    
End Sub
