VERSION 5.00
Begin VB.Form frmAjuste 
   BorderStyle     =   1  'Fixed Single
   Caption         =   "Ajuste"
   ClientHeight    =   2265
   ClientLeft      =   45
   ClientTop       =   330
   ClientWidth     =   4620
   Icon            =   "frmAjuste.frx":0000
   LinkTopic       =   "Form1"
   MaxButton       =   0   'False
   ScaleHeight     =   2265
   ScaleWidth      =   4620
   StartUpPosition =   2  'CenterScreen
   Begin VB.CommandButton cmdEstandar 
      Caption         =   "&Aceptar"
      Default         =   -1  'True
      Height          =   375
      Index           =   0
      Left            =   1680
      TabIndex        =   2
      Top             =   1800
      Width           =   1335
   End
   Begin VB.CommandButton cmdEstandar 
      Cancel          =   -1  'True
      Caption         =   "&Cancelar"
      Height          =   375
      Index           =   1
      Left            =   3120
      TabIndex        =   3
      Top             =   1800
      Width           =   1335
   End
   Begin VB.Frame fraAjuste 
      Height          =   1695
      Index           =   0
      Left            =   120
      TabIndex        =   6
      Top             =   0
      Width           =   4335
      Begin VB.TextBox txtCantidad 
         Height          =   315
         Left            =   2880
         TabIndex        =   1
         Top             =   720
         Width           =   1215
      End
      Begin VB.Frame fraAjuste 
         Height          =   1215
         Index           =   1
         Left            =   240
         TabIndex        =   7
         Top             =   240
         Width           =   1455
         Begin VB.OptionButton optAjuste 
            Caption         =   "Positivo"
            Height          =   255
            Index           =   1
            Left            =   240
            TabIndex        =   5
            Top             =   720
            Width           =   975
         End
         Begin VB.OptionButton optAjuste 
            Caption         =   "Negativo"
            Height          =   255
            Index           =   0
            Left            =   240
            TabIndex        =   4
            Top             =   360
            Width           =   1095
         End
      End
      Begin VB.Label lblLabels 
         Caption         =   "Ca&ntidad"
         Height          =   375
         Index           =   0
         Left            =   1920
         TabIndex        =   0
         Top             =   840
         Width           =   855
      End
   End
End
Attribute VB_Name = "frmAjuste"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit

Const kOPT_NEGATIVO = 0

Private Sub Form_Load()

  'Cargar Form
  
  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
  
  On Error GoTo LineaError
  
  optAjuste(kOPT_NEGATIVO) = True
  
  Select Case psForm
    
  Case kFORM_PREMIO_TICKET_LISTA
      
    Me.Caption = "Ajuste - Premio: " + frmPremioTicketLista.lvwPremioTicket.SelectedItem.Text
    
  Case kFORM_ACUMULACION
    
    If psLista = kPUNTO Then
    
      Me.Caption = "Ajuste - Acumulación de Puntos - Cliente: " + frmAcumulacion.lblCliente.Caption
    
    Else
    
      Me.Caption = "Ajuste - Acumulación de Tickets - Cliente: " + frmAcumulacion.lblCliente.Caption
    
    End If
  
  End Select
  
  Exit Sub
  
LineaError:
  MsgBox Err.Description, vbOKOnly + vbInformation
  
End Sub

Private Sub optAjuste_Click(Index As Integer)
  
  'da el foco al texto

  On Error Resume Next
  txtCantidad.SetFocus

End Sub

Private Sub txtCantidad_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 txtCantidad_LostFocus()

  'Formatea el texto
  
  On Error GoTo LineaError
  
  txtCantidad.Text = Format(txtCantidad.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
  'las tablas respectivas

  On Error GoTo LineaError

  Screen.MousePointer = vbHourglass
  
  Select Case Index
  
  Case kCMD_ACEPTAR
  
    If Not ValidarData Then Exit Sub
  
    Select Case psForm
    
    Case kFORM_PREMIO_TICKET_LISTA
      
      ActualizarPremioTicket
      
    Case kFORM_ACUMULACION
    
      If psLista = kPUNTO Then
      
        ActualizarAcumulacionPunto
      
      Else
      
        ActualizarAcumulacionTicket
      
      End If
      
    
    End Select
  
  Case kCMD_CANCELAR
  
    Unload Me
  
  End Select
  
  Screen.MousePointer = vbDefault
  
  Exit Sub
  
LineaError:
  MsgBox Err.Description, vbOKOnly + vbInformation
  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(txtCantidad.Text) = "" Then
    MsgBox "Ingresar cantidad", vbOKOnly + vbInformation
    txtCantidad.SetFocus
    Exit Function
  End If

  ValidarData = True

  Exit Function

LineaError:
  MsgBox Err.Description, vbOKOnly + vbInformation

End Function

Private Sub ActualizarPremioTicket()

  'Inserta Ajuste en Ingreso_Premio_Ticket

  On Error GoTo LineaError
  
  Dim sSQL As String
  Dim ADORS As ADODB.Recordset
  
  Dim bConnFlag As Boolean
  Dim sCodigo As String
  Dim sPremio As String
  
  Dim itemX As ListItem
  Dim lStock As Long
    
  sPremio = Mid(frmPremioTicketLista.lvwPremioTicket.SelectedItem.Key, 2)
  
  sSQL = "SELECT MAX(cIngrCode) AS Codigo FROM Ingreso_Premio_Ticket " + _
         "WHERE cPremCode = $"
  sSQL = VBsprintf(sSQL, sPremio)
  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
  
  ADOCnn.BeginTrans
  bConnFlag = True
  
  sSQL = "INSERT INTO Ingreso_Premio_Ticket (cIngrCode, cPremCode, dIngrDate, " + _
         "nIngrCant, sUserDesc, cAjusFlag) " + _
         "VALUES($, $, '$', $, '$', $)"
  sSQL = VBsprintf(sSQL, sCodigo, sPremio, Format(Now, "yyyy/mm/dd hh:mm"), _
         IIf(optAjuste(kOPT_NEGATIVO).Value, "-", "") + Format(txtCantidad.Text, "####0"), ReemplazarComilla(psUsuario_Descripcion), kSI)
         
  If Not oDBAdmin.EjecutarSentencia(ADOCnn, sSQL) Then GoTo LineaError1
    
  sSQL = "UPDATE Premio_Ticket SET nPremStoc = nPremStoc $ " + _
         "WHERE cPremCode = $"
  sSQL = VBsprintf(sSQL, IIf(optAjuste(kOPT_NEGATIVO).Value, " - ", " + ") + Format(txtCantidad.Text, "####0"), _
         sPremio)

  If Not oDBAdmin.EjecutarSentencia(ADOCnn, sSQL) Then GoTo LineaError1
  
  ADOCnn.CommitTrans
  bConnFlag = False
  
  lStock = CLng(frmPremioTicketLista.lvwPremioTicket.SelectedItem.SubItems(1))
  
  frmPremioTicketLista.lvwPremioTicket.SelectedItem.SubItems(1) = IIf(optAjuste(kOPT_NEGATIVO).Value, Format(lStock - CLng(txtCantidad.Text), "##,##0"), Format(lStock + CLng(txtCantidad.Text), "##,##0"))
  
  If Date >= frmPremioTicketLista.dFInicio And Date <= frmPremioTicketLista.dFFin Then
  
    Set itemX = frmPremioTicketLista.lvwIngresoPremioTicket.ListItems.Add(, "k" + sCodigo, Format(Now, "dd/mm/yyyy hh:mm"))
    itemX.SubItems(1) = IIf(optAjuste(kOPT_NEGATIVO).Value, "-", "") + Format(txtCantidad.Text, "##,##0")
    itemX.SubItems(2) = psUsuario_Descripcion
  
    itemX.Icon = 3
    itemX.SmallIcon = 3
  
    itemX.Selected = True
    
  Else
  
    frmPremioTicketLista.dtpFInicio.Value = Date - 15
    frmPremioTicketLista.dtpFFin.Value = Date
    
    Call frmPremioTicketLista.cmdBuscar_Click
    
  End If
  
  Unload Me
  
  Exit Sub
  
LineaError:
  MsgBox Err.Description, vbOKOnly + vbInformation
  
LineaError1:
  Set ADORS = Nothing
  If bConnFlag Then ADOCnn.RollbackTrans

End Sub

Private Sub ActualizarAcumulacionPunto()

  'Inserta Ajuste en AcumulacionPunto

  On Error GoTo LineaError
  
  Dim sSQL As String
  Dim ADORS As ADODB.Recordset
  
  Dim bConnFlag As Boolean
  Dim sCodigo As String
  Dim sCliente As String
  Dim sTarjeta As String
  
  Dim itemX As ListItem
    
  sCliente = Mid(frmPrincipal.lvwCliente.SelectedItem.Key, 2)
  sTarjeta = frmPrincipal.lvwCliente.SelectedItem.SubItems(2)
  
  sSQL = "SELECT MAX(cAcumCode) AS Codigo FROM Acumulacion_Punto " + _
         "WHERE cClieCode = $"
  sSQL = VBsprintf(sSQL, sCliente)
  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
  
  ADOCnn.BeginTrans
  bConnFlag = True

  sSQL = "INSERT INTO Acumulacion_Punto (cClieCode, cAcumCode, nTarjNumb, " + _
         "nAcumImpo, nPuntCant, sUserDesc, dAcumDate, cAjusFlag) " + _
         "VALUES($, $, '$', $, $, '$', '$', $)"
  sSQL = VBsprintf(sSQL, sCliente, sCodigo, sTarjeta, Null, _
         IIf(optAjuste(kOPT_NEGATIVO).Value, "-", "") + Format(txtCantidad.Text, "####0"), _
         ReemplazarComilla(psUsuario_Descripcion), Format(Now, "yyyy/mm/dd hh:mm"), kSI)

  If Not oDBAdmin.EjecutarSentencia(ADOCnn, sSQL) Then GoTo LineaError1

  sSQL = "UPDATE Cliente SET nPuntCant = nPuntCant + $ " + _
         "WHERE cClieCode = $"
  sSQL = VBsprintf(sSQL, IIf(optAjuste(kOPT_NEGATIVO).Value, " - ", " + ") + Format(txtCantidad.Text, "####0"), _
         sCliente)
  
  If Not oDBAdmin.EjecutarSentencia(ADOCnn, sSQL) Then GoTo LineaError1
  
  ADOCnn.CommitTrans
  bConnFlag = False
  
  frmAcumulacion.lblPuntos.Caption = IIf(optAjuste(kOPT_NEGATIVO).Value, Format(CLng(frmAcumulacion.lblPuntos.Caption) - CLng(txtCantidad.Text), "##,##0"), Format(CLng(frmAcumulacion.lblPuntos.Caption) + CLng(txtCantidad.Text), "##,##0"))
    
  If Date >= frmAcumulacion.dFInicio And Date <= frmAcumulacion.dFFin Then
  
    Set itemX = frmAcumulacion.lvwPunto.ListItems.Add(, "k" + sCodigo, Format(Now, "dd/mm/yyyy hh:mm"))
    itemX.SubItems(1) = sTarjeta
    itemX.SubItems(2) = ""
    itemX.SubItems(3) = IIf(optAjuste(kOPT_NEGATIVO).Value, "-", "") + Format(txtCantidad.Text, "##,##0")
    itemX.SubItems(4) = psUsuario_Descripcion
    
    itemX.Icon = 3
    itemX.SmallIcon = 3
    
    itemX.Selected = True
      
  Else
    
    frmAcumulacion.dtpFInicio.Value = Date - 15
    frmAcumulacion.dtpFFin.Value = Date
      
    Call frmAcumulacion.cmdBuscar_Click
      
  End If
  
  Unload Me
  
  Exit Sub
  
LineaError:
  MsgBox Err.Description, vbOKOnly + vbInformation
  
LineaError1:
  Set ADORS = Nothing
  If bConnFlag Then ADOCnn.RollbackTrans

End Sub



Private Sub ActualizarAcumulacionTicket()

  'Inserta Ajuste en AcumulacionTicket

  On Error GoTo LineaError
  
  Dim sSQL As String
  Dim ADORS As ADODB.Recordset
  
  Dim bConnFlag As Boolean
  Dim sCodigo As String
  Dim sCliente As String
  Dim sTarjeta As String
  
  Dim itemX As ListItem
    
  sCliente = Mid(frmPrincipal.lvwCliente.SelectedItem.Key, 2)
  sTarjeta = frmPrincipal.lvwCliente.SelectedItem.SubItems(2)
  
  sSQL = "SELECT MAX(cAcumCode) AS Codigo FROM Acumulacion_Ticket " + _
         "WHERE cClieCode = $"
  sSQL = VBsprintf(sSQL, sCliente)
  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
  
  ADOCnn.BeginTrans
  bConnFlag = True

  sSQL = "INSERT INTO Acumulacion_Ticket (cClieCode, cAcumCode, nTarjNumb, " + _
         "nTickCant, sUserDesc, dAcumDate, cAjusFlag) " + _
         "VALUES($, $, '$', $, '$', '$', $)"
  sSQL = VBsprintf(sSQL, sCliente, sCodigo, sTarjeta, _
         IIf(optAjuste(kOPT_NEGATIVO).Value, "-", "") + Format(txtCantidad.Text, "####0"), _
         ReemplazarComilla(psUsuario_Descripcion), Format(Now, "yyyy/mm/dd hh:mm"), kSI)

  If Not oDBAdmin.EjecutarSentencia(ADOCnn, sSQL) Then GoTo LineaError1

  sSQL = "UPDATE Cliente SET nTickCant = nTickCant + $ " + _
         "WHERE cClieCode = $"
  sSQL = VBsprintf(sSQL, IIf(optAjuste(kOPT_NEGATIVO).Value, " - ", " + ") + Format(txtCantidad.Text, "####0"), _
         sCliente)
  
  If Not oDBAdmin.EjecutarSentencia(ADOCnn, sSQL) Then GoTo LineaError1
  
  ADOCnn.CommitTrans
  bConnFlag = False
  
  frmAcumulacion.lblTickets.Caption = IIf(optAjuste(kOPT_NEGATIVO).Value, Format(CLng(frmAcumulacion.lblTickets.Caption) - CLng(txtCantidad.Text), "##,##0"), Format(CLng(frmAcumulacion.lblTickets.Caption) + CLng(txtCantidad.Text), "##,##0"))
    
  If Date >= frmAcumulacion.dFInicio And Date <= frmAcumulacion.dFFin Then
  
    Set itemX = frmAcumulacion.lvwTicket.ListItems.Add(, "k" + sCodigo, Format(Now, "dd/mm/yyyy hh:mm"))
    itemX.SubItems(1) = sTarjeta
    itemX.SubItems(2) = IIf(optAjuste(kOPT_NEGATIVO).Value, "-", "") + Format(txtCantidad.Text, "##,##0")
    itemX.SubItems(3) = psUsuario_Descripcion
    
    itemX.Icon = 3
    itemX.SmallIcon = 3
    
    itemX.Selected = True
      
  Else
    
    frmAcumulacion.dtpFInicio.Value = Date - 15
    frmAcumulacion.dtpFFin.Value = Date
      
    Call frmAcumulacion.cmdBuscar_Click
      
  End If
  
  Unload Me
  
  Exit Sub
  
LineaError:
  MsgBox Err.Description, vbOKOnly + vbInformation
  
LineaError1:
  Set ADORS = Nothing
  If bConnFlag Then ADOCnn.RollbackTrans

End Sub


