VERSION 5.00
Begin VB.Form frmPremioTicket 
   BorderStyle     =   1  'Fixed Single
   Caption         =   "Premio (Ticket)"
   ClientHeight    =   3045
   ClientLeft      =   45
   ClientTop       =   330
   ClientWidth     =   6660
   Icon            =   "frmPremioTicket.frx":0000
   LinkTopic       =   "Form1"
   MaxButton       =   0   'False
   ScaleHeight     =   3045
   ScaleWidth      =   6660
   StartUpPosition =   2  'CenterScreen
   Begin VB.CommandButton cmdEstandar 
      Cancel          =   -1  'True
      Caption         =   "&Cancelar"
      Height          =   375
      Index           =   1
      Left            =   5160
      TabIndex        =   13
      Top             =   2520
      Width           =   1335
   End
   Begin VB.CommandButton cmdEstandar 
      Caption         =   "&Aceptar"
      Default         =   -1  'True
      Height          =   375
      Index           =   0
      Left            =   3720
      TabIndex        =   11
      Top             =   2520
      Width           =   1335
   End
   Begin VB.Frame fraPremioTicket 
      Height          =   2295
      Left            =   120
      TabIndex        =   0
      Top             =   120
      Width           =   6375
      Begin VB.Frame fraActivo 
         Height          =   735
         Left            =   3000
         TabIndex        =   12
         Top             =   1320
         Width           =   3135
         Begin VB.OptionButton optActivo 
            Caption         =   "Inactivo"
            Height          =   255
            Index           =   0
            Left            =   240
            TabIndex        =   9
            Top             =   240
            Width           =   1215
         End
         Begin VB.OptionButton optActivo 
            Caption         =   "Activo"
            Height          =   255
            Index           =   1
            Left            =   1800
            TabIndex        =   10
            Top             =   240
            Width           =   1215
         End
      End
      Begin VB.TextBox txtTicket 
         Height          =   315
         Left            =   1560
         TabIndex        =   8
         Top             =   1680
         Width           =   1215
      End
      Begin VB.TextBox txtStock 
         Height          =   315
         Left            =   1560
         TabIndex        =   6
         Top             =   1320
         Width           =   1215
      End
      Begin VB.TextBox txtComentario 
         Height          =   675
         Left            =   1560
         MaxLength       =   255
         TabIndex        =   4
         Top             =   600
         Width           =   4575
      End
      Begin VB.TextBox txtDescripcion 
         Height          =   315
         Left            =   1560
         MaxLength       =   100
         TabIndex        =   2
         Top             =   240
         Width           =   4575
      End
      Begin VB.Label lblLabels 
         Caption         =   "Tickets"
         Height          =   375
         Index           =   3
         Left            =   240
         TabIndex        =   7
         Top             =   1800
         Width           =   1455
      End
      Begin VB.Label lblLabels 
         Caption         =   "&Stock"
         Height          =   375
         Index           =   2
         Left            =   240
         TabIndex        =   5
         Top             =   1320
         Width           =   1455
      End
      Begin VB.Label lblLabels 
         Caption         =   "Co&mentario"
         Height          =   375
         Index           =   1
         Left            =   240
         TabIndex        =   3
         Top             =   720
         Width           =   1455
      End
      Begin VB.Label lblLabels 
         Caption         =   "&Descripción"
         Height          =   375
         Index           =   0
         Left            =   240
         TabIndex        =   1
         Top             =   360
         Width           =   1455
      End
   End
End
Attribute VB_Name = "frmPremioTicket"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit

Public iEditar As Integer
Dim sCodigo As String

Private Sub Form_Load()

  'Cargar formulario
  
  On Error GoTo LineaError

  Screen.MousePointer = vbHourglass
  
  InicializarForm
  
  Screen.MousePointer = vbDefault
  
  Exit Sub
  
LineaError:
  MsgBox Err.Description, vbOKOnly + vbInformation
  Screen.MousePointer = vbDefault

End Sub


Private Sub InicializarForm()

  'Inicializa Form según opción

  On Error GoTo LineaError

  Select Case iEditar
  
  Case kCMD_NUEVO
    Me.Caption = "Nuevo Premio (Ticket)"
    optActivo(kSI).Value = True
  
  Case kCMD_EDITAR
    Me.Caption = "Editar Premio (Ticket)"
    TraerData
    DeshabilitarControles
  
  Case kCMD_BORRAR
    Me.Caption = "Borrar Premio (Ticket)"
    TraerData
    DeshabilitarControles
    
  Case kCMD_VER
    Me.Caption = "Premio (Ticket)"
    TraerData
    DeshabilitarControles
    
  End Select
  
  Exit Sub
  
LineaError:
  MsgBox Err.Description, vbOKOnly + vbInformation

End Sub

Private Sub TraerData()

  'Trae la data de la tabla Premio_Ticket
  
  On Error GoTo LineaError

  Dim sSQL As String
  Dim ADORS As ADODB.Recordset
  
  sSQL = "SELECT * FROM Premio_Ticket " + _
         "WHERE cPremCode = $"
  sSQL = VBsprintf(sSQL, Mid(frmPremioTicketLista.lvwPremioTicket.SelectedItem.Key, 2))
  
  If oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then
  
    If Not ADORS.EOF Then
    
      sCodigo = LeerCampo(ADORS!cPremCode)
      
      txtDescripcion.Text = LeerCampo(ADORS!sPremDesc)
      txtComentario.Text = LeerCampo(ADORS!sPremCome)
      txtStock.Text = Format(LeerCampo(ADORS!nPremStoc), "##,##0")
      txtTicket.Text = Format(LeerCampo(ADORS!nTickCant), "##,##0")
      
      If LeerCampo(ADORS!cPremActi) <> kSI Then
        optActivo(kNO).Value = True
      Else
        optActivo(kSI).Value = True
      End If
      
    End If
    
  End If
  
  Exit Sub
  
LineaError:
  MsgBox Err.Description, vbOKOnly + vbInformation
  
End Sub

Private Sub DeshabilitarControles()

  'Deshabilita controles
  
  On Error GoTo LineaError
  
  Call DeshabilitarControl(txtStock, True)
  
  If (iEditar = kCMD_EDITAR And Not frmPremioTicketLista.bTodoFlag) Or iEditar <> kCMD_EDITAR Then
    Call DeshabilitarControl(txtDescripcion, True)
  End If
  
  If iEditar <> kCMD_EDITAR Then
    Call DeshabilitarControl(txtComentario, True)
    Call DeshabilitarControl(txtTicket, True)
    Call DeshabilitarControl(optActivo(kNO), False)
    Call DeshabilitarControl(optActivo(kSI), False)
  End If
  
  Exit Sub
  
LineaError:
  MsgBox Err.Description, vbOKOnly + vbInformation
  
End Sub

Private Sub txtDescripcion_KeyPress(KeyAscii As Integer)
  
  'Convierte cada caracter digitado a mayúsculas
  
  On Error GoTo LineaError
  
  Call ValidKeyPress(KeyAscii, kVKP_UPPER_CASE)
  
  Exit Sub
  
LineaError:
  MsgBox Err.Description, vbOKOnly + vbInformation
  
End Sub

Private Sub txtComentario_KeyPress(KeyAscii As Integer)
  
  'Convierte cada caracter digitado a mayúsculas
  
  On Error GoTo LineaError
  
  Call ValidKeyPress(KeyAscii, kVKP_UPPER_CASE)
  
  Exit Sub
  
LineaError:
  MsgBox Err.Description, vbOKOnly + vbInformation
  
End Sub

Private Sub txtStock_KeyPress(KeyAscii As Integer)

  'Valida q se ingrese solo valores numéricos (enteros)

  On Error GoTo LineaError

  Call ValidKeyPress(KeyAscii, kVKP_NUMERIC_INTEGER_ONLY)

  Exit Sub

LineaError:
  MsgBox Err.Description, vbOKOnly + vbInformation

End Sub

Private Sub txtStock_LostFocus()

  'Formatea el texto
  
  On Error GoTo LineaError
  
  txtStock.Text = Format(txtStock.Text, "##,##0")
  
  Exit Sub

LineaError:
  MsgBox Err.Description, vbOKOnly + vbInformation
  
End Sub

Private Sub txtTicket_KeyPress(KeyAscii As Integer)

  'Valida q se ingrese solo valores numéricos (enteros)

  On Error GoTo LineaError

  Call ValidKeyPress(KeyAscii, kVKP_NUMERIC_INTEGER_ONLY)

  Exit Sub

LineaError:
  MsgBox Err.Description, vbOKOnly + vbInformation

End Sub

Private Sub txtTicket_LostFocus()

  'Formatea el texto
  
  On Error GoTo LineaError
  
  txtTicket.Text = Format(txtTicket.Text, "##,##0")
  
  Exit Sub

LineaError:
  MsgBox Err.Description, vbOKOnly + vbInformation
  
End Sub

Private Sub cmdEstandar_Click(Index As Integer)

  'Permite descargar el formulario, previamente haciendo cambios o no en
  'la tabla Premio_Ticket
  
  On Error GoTo LineaError

  Screen.MousePointer = vbHourglass
  
  Select Case Index
  
  Case kCMD_ACEPTAR
        
    If iEditar = kCMD_VER Then
      Unload Me
      GoTo Salir
    End If
  
    If ValidarData Then
    
      If ActualizarTabla Then
    
        If psForm = kFORM_PREMIO_TICKET_LISTA Then
    
          ActualizarLista
          Call frmPremioTicketLista.cmdBuscar_Click
           
        End If
        
        Unload Me
        
      End If
    
    End If
  
  Case kCMD_CANCELAR
  
    Unload Me
    
  End Select
  
  GoTo Salir
  
LineaError:
  MsgBox Err.Description, vbOKOnly + vbInformation
  
Salir:
  Screen.MousePointer = vbDefault

End Sub

Private Function ValidarData() As Boolean

  'Valida que la información ingresada sea correcta y completa
  
  On Error GoTo LineaError

  ValidarData = False
  
  If Trim(txtDescripcion.Text) = "" Then
    MsgBox "Ingresar Descripción, vbOKOnly + vbInformation"
    txtDescripcion.SetFocus
    Exit Function
  End If
  
  If Trim(txtTicket.Text) = "" Then
    MsgBox "Ingresar Tickets", vbOKOnly + vbInformation
    txtTicket.SetFocus
    Exit Function
  End If
  
  ValidarData = True
  
  Exit Function
  
LineaError:
  MsgBox Err.Description, vbOKOnly + vbInformation

End Function

Private Function ActualizarTabla() As Boolean

  'Inserta/Actualiza/Elimina el registro de Premio_Ticket

  On Error GoTo LineaError

  Dim sSQL As String
  Dim ADORS As ADODB.Recordset
  
  Dim bConnFlag As Boolean

  ActualizarTabla = False
  
  ADOCnn.BeginTrans
  bConnFlag = True

  Select Case iEditar

  Case kCMD_NUEVO
  
    sSQL = "SELECT MAX(cPremCode) AS Codigo FROM Premio_Ticket"
    If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo LineaError1

    sCodigo = "1"

    If Not ADORS.EOF Then

      If LeerCampo(ADORS!Codigo) <> "" Then
        sCodigo = CStr(ADORS!Codigo + 1)
      End If

    End If

    Set ADORS = Nothing
    
    sSQL = "INSERT INTO Premio_Ticket (cPremCode, sPremDesc, sPremCome, nPremStoc, " + _
           "nTickCant, cPremActi) " + _
           "VALUES ($, '$', '$', $, $, $)"
    sSQL = VBsprintf(sSQL, sCodigo, _
           ReemplazarComilla(Trim(txtDescripcion.Text)), _
           ReemplazarComilla(Trim(txtComentario.Text)), _
           IIf(Trim(txtStock.Text) = "", "0", Format(txtStock.Text, "####0")), _
           Format(txtTicket.Text, "####0"), _
           IIf(optActivo(kSI).Value, kSI, kNO))
           
    If Not oDBAdmin.EjecutarSentencia(ADOCnn, sSQL) Then GoTo LineaError1
    
    If Trim(txtStock.Text) <> "" Then
    
      sSQL = "INSERT INTO Ingreso_Premio_Ticket (cIngrCode, cPremCode, dIngrDate, " + _
             "nIngrCant, sUserDesc) " + _
             "VALUES($, $, '$', $, '$')"
      sSQL = VBsprintf(sSQL, "1", sCodigo, Format(Date, "yyyy/mm/dd hh:mm"), _
             Format(txtStock.Text, "####0"), ReemplazarComilla(psUsuario_Descripcion))
             
    Else
      GoTo Actualizar
             
    End If
    
  Case kCMD_EDITAR

    sSQL = "UPDATE Premio_Ticket SET sPremDesc = '$', sPremCome = '$', nTickCant = $, cPremActi = $ " + _
           "WHERE cPremCode = $"
    sSQL = VBsprintf(sSQL, ReemplazarComilla(Trim(txtDescripcion.Text)), _
           ReemplazarComilla(Trim(txtComentario.Text)), _
           Format(txtTicket.Text, "####0"), _
           IIf(optActivo(kSI).Value, kSI, kNO), _
           sCodigo)

  Case kCMD_BORRAR
  
    sSQL = "DELETE * FROM Ingreso_Premio_Ticket " + _
           "WHERE cPremCode = $"
    sSQL = VBsprintf(sSQL, sCodigo)
    
    If Not oDBAdmin.EjecutarSentencia(ADOCnn, sSQL) Then Exit Function
    
    sSQL = "DELETE * FROM Premio_Ticket " + _
           "WHERE cPremCode = $"
    sSQL = VBsprintf(sSQL, sCodigo)

  End Select
  
  If oDBAdmin.EjecutarSentencia(ADOCnn, sSQL) Then
  
Actualizar:
    ADOCnn.CommitTrans
    bConnFlag = False
    ActualizarTabla = True
    
  Else
    GoTo LineaError1
    
  End If

  Exit Function

LineaError:
  MsgBox Err.Description, vbOKOnly + vbInformation
  
LineaError1:
  Set ADORS = Nothing
  If bConnFlag Then ADOCnn.RollbackTrans
  
End Function

Private Sub ActualizarLista()

  'Actualiza la lista de frmPremioTicketLista

  On Error GoTo LineaError
  
  Dim itemX As ListItem
  
  Select Case iEditar
  
  Case kCMD_NUEVO, kCMD_EDITAR
  
    If frmPremioTicketLista.optActivos(kSI).Value = optActivo(kSI).Value Or frmPremioTicketLista.optActivos(kNO).Value = optActivo(kNO).Value Then
  
      If iEditar = kCMD_NUEVO Then
        Set itemX = frmPremioTicketLista.lvwPremioTicket.ListItems.Add(, "k" + sCodigo, Trim(txtDescripcion.Text))
      Else
        Set itemX = frmPremioTicketLista.lvwPremioTicket.SelectedItem
        itemX.Text = Trim(txtDescripcion.Text)
      End If
    
      If Trim(txtStock.Text) = "" Then
        itemX.SubItems(1) = "0"
      Else
        itemX.SubItems(1) = Format(txtStock.Text, "##,##0")
      End If
      
      itemX.SubItems(2) = Format(txtTicket.Text, "##,##0")
    
      If optActivo(kSI).Value Then
        itemX.Icon = 1
        itemX.SmallIcon = 1
      Else
        itemX.Icon = 2
        itemX.SmallIcon = 2
      End If
    
      itemX.Selected = True
      
    Else
    
      If iEditar = kCMD_NUEVO Then
        
        If optActivo(kSI).Value Then
          frmPremioTicketLista.optActivos(kSI).Value = True
        Else
          frmPremioTicketLista.optActivos(kNO).Value = True
        End If
        
      ElseIf iEditar = kCMD_EDITAR Then
        frmPremioTicketLista.lvwPremioTicket.ListItems.Remove (frmPremioTicketLista.lvwPremioTicket.SelectedItem.Index)
      End If
      
    End If
    
  Case kCMD_BORRAR
  
    frmPremioTicketLista.lvwPremioTicket.ListItems.Remove (frmPremioTicketLista.lvwPremioTicket.SelectedItem.Index)
    
  End Select
      
  Exit Sub
  
LineaError:
  MsgBox Err.Description, vbOKOnly + vbInformation
  
End Sub


