VERSION 5.00
Object = "{831FDD16-0C5C-11D2-A9FC-0000F8754DA1}#2.0#0"; "mscomctl.ocx"
Begin VB.Form frmPrincipal 
   BorderStyle     =   1  'Fixed Single
   Caption         =   "Control de Caja Registradora"
   ClientHeight    =   7995
   ClientLeft      =   150
   ClientTop       =   615
   ClientWidth     =   11820
   Icon            =   "frmPrincipal.frx":0000
   LinkTopic       =   "Form1"
   MaxButton       =   0   'False
   ScaleHeight     =   8280
   ScaleMode       =   0  'User
   ScaleWidth      =   11820
   StartUpPosition =   2  'CenterScreen
   Begin VB.TextBox txtsupervisor 
      Height          =   285
      Left            =   7455
      TabIndex        =   4
      Top             =   7335
      Visible         =   0   'False
      Width           =   1215
   End
   Begin VB.Timer tmrBDCliente 
      Interval        =   25000
      Left            =   300
      Top             =   5820
   End
   Begin VB.CommandButton Command1 
      Caption         =   "Actualizar Codigos Catalogo"
      Height          =   705
      Left            =   2880
      TabIndex        =   3
      Top             =   7035
      Visible         =   0   'False
      Width           =   1050
   End
   Begin VB.CommandButton cmdActualizaPrecios 
      Caption         =   "Actualiza Precios y Stock"
      Height          =   615
      Left            =   1410
      TabIndex        =   2
      Top             =   7080
      Visible         =   0   'False
      Width           =   1140
   End
   Begin VB.CommandButton cmdActualizaPuntajes 
      Caption         =   "Actualiza Puntajes"
      Height          =   615
      Left            =   150
      TabIndex        =   1
      Top             =   7080
      Visible         =   0   'False
      Width           =   1140
   End
   Begin MSComctlLib.ImageList Imagenes 
      Left            =   3000
      Top             =   5760
      _ExtentX        =   1005
      _ExtentY        =   1005
      BackColor       =   -2147483643
      ImageWidth      =   32
      ImageHeight     =   32
      MaskColor       =   12632256
      _Version        =   393216
      BeginProperty Images {2C247F25-8591-11D1-B16A-00C0F0283628} 
         NumListImages   =   11
         BeginProperty ListImage1 {2C247F27-8591-11D1-B16A-00C0F0283628} 
            Picture         =   "frmPrincipal.frx":030A
            Key             =   "Productos"
         EndProperty
         BeginProperty ListImage2 {2C247F27-8591-11D1-B16A-00C0F0283628} 
            Picture         =   "frmPrincipal.frx":0BE4
            Key             =   "TipoCambio"
         EndProperty
         BeginProperty ListImage3 {2C247F27-8591-11D1-B16A-00C0F0283628} 
            Picture         =   "frmPrincipal.frx":14BE
            Key             =   "ConfiguracionBotones"
         EndProperty
         BeginProperty ListImage4 {2C247F27-8591-11D1-B16A-00C0F0283628} 
            Picture         =   "frmPrincipal.frx":1D98
            Key             =   "CabeceraImpresion"
         EndProperty
         BeginProperty ListImage5 {2C247F27-8591-11D1-B16A-00C0F0283628} 
            Picture         =   "frmPrincipal.frx":2672
            Key             =   "CambioTurno"
         EndProperty
         BeginProperty ListImage6 {2C247F27-8591-11D1-B16A-00C0F0283628} 
            Picture         =   "frmPrincipal.frx":2F4C
            Key             =   "CajaRegistradora"
         EndProperty
         BeginProperty ListImage7 {2C247F27-8591-11D1-B16A-00C0F0283628} 
            Picture         =   "frmPrincipal.frx":3826
            Key             =   "ReporteX"
         EndProperty
         BeginProperty ListImage8 {2C247F27-8591-11D1-B16A-00C0F0283628} 
            Picture         =   "frmPrincipal.frx":4100
            Key             =   "CuadreCaja"
         EndProperty
         BeginProperty ListImage9 {2C247F27-8591-11D1-B16A-00C0F0283628} 
            Picture         =   "frmPrincipal.frx":441A
            Key             =   "ReporteZ"
         EndProperty
         BeginProperty ListImage10 {2C247F27-8591-11D1-B16A-00C0F0283628} 
            Picture         =   "frmPrincipal.frx":4CF4
            Key             =   "Salir"
         EndProperty
         BeginProperty ListImage11 {2C247F27-8591-11D1-B16A-00C0F0283628} 
            Picture         =   "frmPrincipal.frx":5D46
            Key             =   ""
         EndProperty
      EndProperty
   End
   Begin MSComctlLib.Toolbar Toolbar1 
      Align           =   1  'Align Top
      Height          =   870
      Left            =   0
      TabIndex        =   0
      Top             =   0
      Width           =   11820
      _ExtentX        =   20849
      _ExtentY        =   1535
      ButtonWidth     =   1931
      ButtonHeight    =   1376
      Appearance      =   1
      ImageList       =   "Imagenes"
      _Version        =   393216
      BeginProperty Buttons {66833FE8-8583-11D1-B16A-00C0F0283628} 
         NumButtons      =   18
         BeginProperty Button1 {66833FEA-8583-11D1-B16A-00C0F0283628} 
            Object.Visible         =   0   'False
            Caption         =   "Productos"
            Key             =   "Productos"
            ImageKey        =   "Productos"
         EndProperty
         BeginProperty Button2 {66833FEA-8583-11D1-B16A-00C0F0283628} 
            Caption         =   "Tipo Cambio"
            Key             =   "TipoCambio"
            ImageKey        =   "TipoCambio"
         EndProperty
         BeginProperty Button3 {66833FEA-8583-11D1-B16A-00C0F0283628} 
            Object.Visible         =   0   'False
            Caption         =   "Botones"
            Key             =   "ConfiguracionBotones"
            ImageKey        =   "ConfiguracionBotones"
         EndProperty
         BeginProperty Button4 {66833FEA-8583-11D1-B16A-00C0F0283628} 
            Object.Visible         =   0   'False
            Caption         =   "Cabecera"
            Key             =   "CabeceraImpresion"
            ImageKey        =   "CabeceraImpresion"
         EndProperty
         BeginProperty Button5 {66833FEA-8583-11D1-B16A-00C0F0283628} 
            Style           =   3
         EndProperty
         BeginProperty Button6 {66833FEA-8583-11D1-B16A-00C0F0283628} 
            Caption         =   "Caja"
            Key             =   "CajaRegistradora"
            ImageKey        =   "CajaRegistradora"
         EndProperty
         BeginProperty Button7 {66833FEA-8583-11D1-B16A-00C0F0283628} 
            Style           =   3
         EndProperty
         BeginProperty Button8 {66833FEA-8583-11D1-B16A-00C0F0283628} 
            Caption         =   "Cambio Turno"
            Key             =   "CambioTurno"
            ImageKey        =   "CambioTurno"
         EndProperty
         BeginProperty Button9 {66833FEA-8583-11D1-B16A-00C0F0283628} 
            Caption         =   "Reporte X"
            Key             =   "ReporteX"
            ImageKey        =   "ReporteX"
         EndProperty
         BeginProperty Button10 {66833FEA-8583-11D1-B16A-00C0F0283628} 
            Style           =   3
         EndProperty
         BeginProperty Button11 {66833FEA-8583-11D1-B16A-00C0F0283628} 
            Caption         =   "Reporte Z"
            Key             =   "ReporteZ"
            ImageKey        =   "ReporteZ"
         EndProperty
         BeginProperty Button12 {66833FEA-8583-11D1-B16A-00C0F0283628} 
            Enabled         =   0   'False
            Caption         =   "Cuadre Caja"
            Key             =   "CuadreCaja"
            ImageKey        =   "CuadreCaja"
         EndProperty
         BeginProperty Button13 {66833FEA-8583-11D1-B16A-00C0F0283628} 
            Style           =   3
         EndProperty
         BeginProperty Button14 {66833FEA-8583-11D1-B16A-00C0F0283628} 
            Caption         =   "Salir"
            Key             =   "Salir"
            ImageKey        =   "Salir"
         EndProperty
         BeginProperty Button15 {66833FEA-8583-11D1-B16A-00C0F0283628} 
            Style           =   3
         EndProperty
         BeginProperty Button16 {66833FEA-8583-11D1-B16A-00C0F0283628} 
            Caption         =   "Z Fecha"
            Key             =   "ZFecha"
            ImageIndex      =   9
         EndProperty
         BeginProperty Button17 {66833FEA-8583-11D1-B16A-00C0F0283628} 
            Caption         =   "Z1 Fecha"
            Key             =   "Z1Fecha"
            ImageIndex      =   9
         EndProperty
         BeginProperty Button18 {66833FEA-8583-11D1-B16A-00C0F0283628} 
            Object.Visible         =   0   'False
            Caption         =   "Sincronizar"
            Key             =   "SFecha"
            ImageIndex      =   1
         EndProperty
      EndProperty
   End
   Begin VB.Menu mnuMantenimiento 
      Caption         =   "&Mantenimiento"
      Begin VB.Menu mnuMantenimiento_Hijo 
         Caption         =   "&Productos"
         Index           =   1
         Visible         =   0   'False
      End
      Begin VB.Menu mnuMantenimiento_Hijo 
         Caption         =   "&Tipo de Cambio"
         Index           =   2
      End
      Begin VB.Menu mnuMantenimiento_Hijo 
         Caption         =   "-"
         Index           =   3
      End
      Begin VB.Menu mnuMantenimiento_Hijo 
         Caption         =   "&Configuración del Teclado"
         Index           =   4
      End
      Begin VB.Menu mnuMantenimiento_Hijo 
         Caption         =   "Cabecera de &Impresión"
         Index           =   5
      End
      Begin VB.Menu mnuMantenimiento_Hijo 
         Caption         =   "-"
         Index           =   6
      End
      Begin VB.Menu mnuMantenimiento_Hijo 
         Caption         =   "Configuración Impresora"
         Index           =   7
      End
      Begin VB.Menu mnuMantenimiento_Hijo 
         Caption         =   "Iniciar Caja"
         Index           =   8
      End
      Begin VB.Menu mnuraya 
         Caption         =   "-"
      End
      Begin VB.Menu mnu_sincronizar_DiaAnt 
         Caption         =   "Sincronizar Datos (Dia Anterior)"
         Enabled         =   0   'False
      End
      Begin VB.Menu mnu_1 
         Caption         =   "-"
      End
      Begin VB.Menu mnu_sincronizar 
         Caption         =   "Sincronizar Datos"
      End
      Begin VB.Menu mnulin 
         Caption         =   "-"
      End
      Begin VB.Menu mnuDepurarTablas 
         Caption         =   "Depurar Tablas"
      End
      Begin VB.Menu mnuUsuarios 
         Caption         =   "&Usuarios"
      End
      Begin VB.Menu mnuConfNumeracion 
         Caption         =   "Configuración Numeración"
         Visible         =   0   'False
      End
      Begin VB.Menu mnuConfAlmacen 
         Caption         =   "Configuración Almacen"
         Visible         =   0   'False
      End
      Begin VB.Menu mnuConfigParam 
         Caption         =   "Configuración Parámetros"
      End
      Begin VB.Menu mnu_rayita 
         Caption         =   "-"
      End
      Begin VB.Menu mnu_tiptar 
         Caption         =   "Tipo de Tarjeta"
      End
   End
   Begin VB.Menu mnuOperaciones 
      Caption         =   "Operaciones"
      Begin VB.Menu mnuOperaciones_Hijo 
         Caption         =   "Cambio de Turno"
         Index           =   1
      End
      Begin VB.Menu mnuOperaciones_Hijo 
         Caption         =   "-"
         Index           =   2
      End
      Begin VB.Menu mnuOperaciones_Hijo 
         Caption         =   "Caja Registradora"
         Index           =   3
      End
      Begin VB.Menu mnuOperaciones_Hijo 
         Caption         =   "Caja Registradora (Versión Full)"
         Index           =   4
      End
      Begin VB.Menu mnuOperaciones_Hijo 
         Caption         =   "-"
         Index           =   5
      End
      Begin VB.Menu mnuOperaciones_Hijo 
         Caption         =   "Reporte X"
         Index           =   6
      End
      Begin VB.Menu mnuOperaciones_Hijo 
         Caption         =   "Cuadre de Caja"
         Index           =   7
         Visible         =   0   'False
      End
      Begin VB.Menu mnuOperaciones_Hijo 
         Caption         =   "Reporte Z"
         Index           =   8
      End
      Begin VB.Menu mnuOperaciones_Hijo 
         Caption         =   "-"
         Index           =   9
         Visible         =   0   'False
      End
      Begin VB.Menu mnuOperaciones_Hijo 
         Caption         =   "Reporte O/C"
         Index           =   10
         Visible         =   0   'False
      End
      Begin VB.Menu mnuOperaciones_Hijo 
         Caption         =   "Reporte N/C"
         Index           =   11
         Visible         =   0   'False
      End
      Begin VB.Menu mnuOperaciones_Hijo 
         Caption         =   "Reporte PADE"
         Index           =   12
         Visible         =   0   'False
      End
      Begin VB.Menu mnuOperaciones_Hijo 
         Caption         =   "Reporte PADV"
         Index           =   13
         Visible         =   0   'False
      End
      Begin VB.Menu mnuOperaciones_Hijo 
         Caption         =   "Reporte LIQPAD"
         Index           =   14
         Visible         =   0   'False
      End
      Begin VB.Menu mnuOperaciones_Hijo 
         Caption         =   "Consulta OP"
         Index           =   15
      End
      Begin VB.Menu mnuOperaciones_Hijo 
         Caption         =   "Reporte X-1"
         Index           =   16
      End
      Begin VB.Menu mnuOperaciones_Hijo 
         Caption         =   "Reporte X-1 formato  Excel"
         Index           =   17
      End
      Begin VB.Menu mnuRptStock 
         Caption         =   "Reporte Stock"
      End
   End
   Begin VB.Menu mnuCtaCte 
      Caption         =   "Venta Futura"
      Begin VB.Menu mnuOperCtaCte 
         Caption         =   "Operaciones"
      End
      Begin VB.Menu mnuConsCtaCte 
         Caption         =   "Consultas"
      End
   End
   Begin VB.Menu mnuAcercaDe 
      Caption         =   "Acerca de..."
   End
   Begin VB.Menu mnuSalir 
      Caption         =   "&Salir"
   End
End
Attribute VB_Name = "frmPrincipal"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Private Type t_productos
    Codigo  As Long
    Cantidad As Long
    Producto As String
    Catalogo As String
End Type

Private Type DetalleZ
    IdCab As Long
    CodProd As Integer
    DscProd As String
    Cantidad As Double
End Type

Option Explicit

Public bCerrar As Boolean
Public bAcceso As Boolean
Public iMnuReporte As Integer
Dim sTexto As String
Dim intentos As Integer

Dim sImprimir() As String
Dim iNroFilas As Integer
Dim iNroBloques As Integer

Private Sub cmdActualizaPuntajes_Click()
  On Error Resume Next

  Dim sSQL As String
  Dim ADORS As ADODB.Recordset
  Dim ADORS2 As ADODB.Recordset
  Dim ADORS3 As ADODB.Recordset
  Dim intPuntaje As Double
  Dim strCodigo As String
  Dim strFechaHoy As String
  Dim i As Long

  ConectarBDCliente
  Dim codigocliente As String
  Dim telefono As String
  Dim tarjeta As String
  Dim puntos As Double
  Dim fechanac As String
  Dim fechaulttra As String
  Dim fechatarjeta As String
  Dim distrito As String
  Dim direccion As String
  Dim nombre As String

  sSQL = " SELECT * " & " FROM Acumulacion"

  If Not oDBAdmin.EjecutarConsulta(ADOCnn2, sSQL, ADORS) Then Exit Sub

  Do While Not ADORS.EOF
    'sw FALTA IdLocal
    codigocliente = LeerCampo(ADORS!cClieCode)

    'sw To SQL Server era >>> codigooriginal
    sSQL = " SELECT * " & _
           "   FROM CLIENTE " & _
           " where  sCodigoOriginal=" & codigocliente

    If Not oDBAdmin.EjecutarConsulta(ADOCnn2, sSQL, ADORS2) Then Exit Sub

    strCodigo = LeerCampo(ADORS2!cClieCode)
    'Insertamos en la tabla de acumulacion de puntos
    'sw FALTA IdLocal
    sSQL = " INSERT INTO Acumulacion_Punto" & _
           " (cClieCode, nTarjNumb, nAcumImpo, nPuntCant, sUserDesc, dAcumDate)" & _
           " Select " & strCodigo & ",'" & Right("00000000" & strCodigo, 8) & "',nAcumImpo, nPuntCant, sUserDesc, dAcumDate " & _
           " from Acumulacion where cCliecode= " & strCodigo
    oDBAdmin.EjecutarSentencia ADOCnn2, sSQL

    'sw FALTA IdLocal
    sSQL = " SELECT sum(nPuntCant) as puntos " & _
           "   FROM acumulacion_punto " & _
           "  WHERE cClieCode = " & strCodigo
    If oDBAdmin.EjecutarConsulta(ADOCnn2, sSQL, ADORS2) Then
      MsgBox "Error"
    End If

    puntos = "0" & ADORS2!puntos

    'sw FALTA IdLocal
    sSQL = " SELECT sum(nPuntCant) as usados " & _
           "   FROM Canje_punto " & _
           "  WHERE cClieCode = " & strCodigo
    If oDBAdmin.EjecutarConsulta(ADOCnn2, sSQL, ADORS2) Then
      MsgBox "Error"
    End If
    puntos = puntos - ("0" & (ADORS2!usados))

    'sw FALTA IdLocal
    sSQL = " UPDATE CLIENTE" & _
           "    SET nPuntCant = " & LeerCampo(ADORS2!puntos) & _
           "  WHERE cClieCode = " & strCodigo
    If oDBAdmin.EjecutarSentencia(ADOCnn2, sSQL) Then
       MsgBox "Error"
    End If

    ADORS.MoveNext
  Loop

  MsgBox "Proceso terminado"
  Screen.MousePointer = vbDefault
  '----------------------------------------------------------------------
  'sw ¿Canje? NO UBIQUÉ ESTA TABLA EN OMNICLIENT.MDB
  sSQL = " SELECT * " & _
         "   FROM Canje "

  If Not oDBAdmin.EjecutarConsulta(ADOCnn2, sSQL, ADORS) Then Exit Sub

  Do While Not ADORS.EOF
    codigocliente = LeerCampo(ADORS!cClieCode)

    'sw To SQL Server era >>> codigooriginal
    sSQL = " SELECT * " & _
           "   FROM CLIENTE " & _
           " where  sCodigoOriginal=" & codigocliente

    If Not oDBAdmin.EjecutarConsulta(ADOCnn2, sSQL, ADORS2) Then Exit Sub

    strCodigo = LeerCampo(ADORS2!cClieCode)
    'Insertamos en la tabla de acumulacion de puntos
    'sw FALTA IdLocal
    sSQL = " INSERT INTO Canje_Punto" & _
           " (cClieCode, cCanjCode, nTarjNumb, nPuntCant, cPremCode, nPremCant, sUserDesc, dCanjDate, dExtoDate, sUseEDesc)" & _
           " Select " & strCodigo & ",cCanjCode, '" & Right("00000000" & strCodigo, 8) & "', nPuntCant, cPremCode, nPremCant, sUserDesc, dCanjDate, dExtoDate, sUseEDesc " & _
           " from Canje where cCliecode= " & strCodigo
    oDBAdmin.EjecutarSentencia ADOCnn2, sSQL

    'sw FALTA IdLocal
    sSQL = " SELECT sum(nPuntCant) as puntos " & _
           "   FROM acumulacion_punto " & _
           "  WHERE cClieCode = " & strCodigo
    If oDBAdmin.EjecutarConsulta(ADOCnn2, sSQL, ADORS2) Then
      MsgBox "Error"
    End If

    puntos = "0" & ADORS2!puntos

    'sw FALTA IdLocal
    sSQL = " SELECT sum(nPuntCant) as usados " & _
           "   FROM Canje_punto " & _
           "  WHERE cClieCode = " & strCodigo
    If oDBAdmin.EjecutarConsulta(ADOCnn2, sSQL, ADORS2) Then
      MsgBox "Error"
    End If
    puntos = puntos - ("0" & (ADORS2!usados))

    'sw FALTA IdLocal
    sSQL = " UPDATE CLIENTE" & _
           "    SET nPuntCant = " & LeerCampo(ADORS2!puntos) & _
           "  WHERE cClieCode = " & strCodigo
    If oDBAdmin.EjecutarSentencia(ADOCnn2, sSQL) Then
      MsgBox "Error"
    End If

    ADORS.MoveNext
  Loop

  MsgBox "Proceso terminado"
  Screen.MousePointer = vbDefault

  GoTo Salir

LineaError:
  MsgBox Err.Description, vbOKOnly + vbInformation
Salir:
  Screen.MousePointer = vbDefault
End Sub

Private Sub Command1_Click()
  Funciones.actualizar
End Sub

Private Sub Command3_Click()
frmConsultaCtaCteCliente.Show 1
End Sub

Private Sub Command4_Click()
    frmConsCtaCteCliente.Show 1
End Sub

Private Sub Form_Load()
  intentos = 0
  If piPerfil_Codigo = 3 Then mnuUsuarios.Visible = False
  
  'Cargar el formulario
  On Error GoTo LineaError
  Screen.MousePointer = vbHourglass
  ManejarControles (True)
  
  'Abre cierra caja
  If kPAR_TURNO = "" Then
    ProcesoCaja False
    
    Toolbar1.Buttons(2).Enabled = False
    Toolbar1.Buttons(6).Enabled = False
    Toolbar1.Buttons(8).Enabled = False
    Toolbar1.Buttons(9).Enabled = False
    Toolbar1.Buttons(11).Enabled = False
    
    MsgBox "La caja se encuentra cerrada, debe aperturarla para empezar " + vbCrLf + "registrar operaciones."
  Else
    Toolbar1.Buttons(2).Enabled = True
    Toolbar1.Buttons(6).Enabled = True
    Toolbar1.Buttons(8).Enabled = True
    Toolbar1.Buttons(9).Enabled = True
    Toolbar1.Buttons(11).Enabled = True
  
    ProcesoCaja True
  End If
  
  If LCase(psUsuario_Descripcion) = "admin" And piPerfil_Codigo = "1" Then
    Toolbar1.Buttons(15).Visible = True
    Toolbar1.Buttons(16).Visible = True
    Toolbar1.Buttons(17).Visible = True
    'Toolbar1.Buttons(18).Visible = True
  End If
  
  Dim rsConfiguracion As ADODB.Recordset
  Dim NumiCierreCaja As Integer
  Dim sSQL As String
  sSQL = "select icierrecaja from configuracion"
  
  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, rsConfiguracion) Then MsgBox ""
  
  NumiCierreCaja = rsConfiguracion!iCierreCaja
  
  If NumiCierreCaja > 0 Then
    MsgBox "Se ha habilitado la Opcion Reporte Z, porque algunas operaciones no han concluido"
    Toolbar1.Visible = True
    
    Dim i As Integer
    
    For i = 1 To 17
        Toolbar1.Buttons(i).Enabled = False
    Next i
    
    Toolbar1.Buttons(11).Enabled = True
    mnuOperaciones_Hijo(8).Enabled = True
    
  End If
  
  
  'mnuOperaciones_Hijo.Item.Enabled = True
  
  Screen.MousePointer = vbDefault
  Exit Sub
 
LineaError:
  MsgBox Err.Description, vbOKOnly + vbInformation
  Screen.MousePointer = vbDefault
End Sub

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
  'UnHookKeyB
End Sub

Private Sub Form_Unload(Cancel As Integer)
  
  On Error GoTo LineaError
   
  'Cierra la conexión a la BD y la aplicación
  
  Call oDBAdmin.CerrarBD(ADOCnn)
  End
  Exit Sub
  
LineaError:
  MsgBox Err.Description, vbOKOnly + vbInformation
End Sub

Private Sub InicializarForm()
  On Error GoTo LineaError

  Me.Caption = Me.Caption + " - Hoy: " + Format(Date, "dd/mm/yyyy")
  Exit Sub
  
LineaError:
  MsgBox Err.Description, vbOKOnly + vbInformation
End Sub

Public Sub ManejarControles(bTodoFlag As Boolean)
'  'Maneja el uso de los controles
'  'Si bTodoFlag = True  --> la totalidad de controles del formulario
'  'Si bTodoFlag = False --> solo aquellas relacionadas con Cliente
'  On Error GoTo LineaError
'
'  If bTodoFlag Then
''    Call DeshabilitarControl(mnuUsuario_Hijo(kMNU_USUARIO_NUEVO), False)
''    Call DeshabilitarControl(mnuUsuario_Hijo(kMNU_USUARIO_MANTENIMIENTO), False)
''    Call DeshabilitarControl(mnuPremioTicket_Hijo(kMNU_PREMIO_TICKET_NUEVO), False)
''    Call DeshabilitarControl(mnuOperaciones, False)
'
'    If piPerfil_Codigo = kPERFIL_SUPERVISOR Then
'      'Call HabilitarControl(mnuUsuario_Hijo(kMNU_USUARIO_NUEVO), False)
'      'Call HabilitarControl(mnuUsuario_Hijo(kMNU_USUARIO_MANTENIMIENTO), False)
'    End If
'
'    If piPerfil_Codigo = kPERFIL_ADMINISTRADOR Or piPerfil_Codigo = kPERFIL_SUPERVISOR Then
'      'Call HabilitarControl(mnuPremioTicket_Hijo(kMNU_PREMIO_TICKET_NUEVO), False)
'      Call HabilitarControl(mnuReporte, False)
'    End If
'
'  End If
'  Exit Sub
'
'LineaError:
'  MsgBox Err.Description, vbOKOnly + vbInformation

End Sub

Private Sub mnu_dbf_Click()
  Form1.Show 1
End Sub

Private Sub mnu_rptz1_Click()
  
End Sub

Private Sub mnu_sincronizar_Click()
  boolSincronizarTodo = False
  If MsgBox("¿Desea Sincronizar también las tablas maestras?", vbYesNo + vbQuestion, "Seleccione") = vbYes Then
    boolSincronizarTodo = True
  End If
  
  boolSincronizarCierre = False
  
  Sincronizar
  
  boolSincronizarTodo = True
End Sub

Private Sub mnu_sincronizar_DiaAnt_Click()
  If MsgBox("Seguro de Sincronizar Datos del dia anterior?", vbYesNo + vbQuestion, "Seleccione") = vbYes Then
    boolSincronizarTodo = False
    boolSincronizarCierre = False
    
    Sincronizar True
    
    boolSincronizarTodo = True
  End If
End Sub

Private Sub mnu_tiptar_Click()
  FrmTablas.Show vbModal
End Sub

Private Sub mnuConfAlmacen_Click()

        Load frmAutorizarOperacion
        Set frmAutorizarOperacion.frmPadre = Me
        frmAutorizarOperacion.Show vbModal
        
        If Not bAcceso Then
            Exit Sub
        Else
            frmConfigAlmacen.Show vbModal
        End If
End Sub

Private Sub mnuConfigParam_Click()
  Load frmAutorizarOperacion
  Set frmAutorizarOperacion.frmPadre = Me
  frmAutorizarOperacion.Show vbModal
        
  If Not bAcceso Then
    Exit Sub
  Else
    frmParametros.Show vbModal
  End If
End Sub

Private Sub mnuConfNumeracion_Click()
  frmConfNumeracion.Show vbModal
End Sub

Private Sub mnuConsCtaCte_Click()
frmConsCtaCteCliente.Show vbModal
End Sub

Private Sub mnuDepurarTablas_Click()
  frmDepurarTablas.Show vbModal
End Sub

Private Sub mnuMantenimiento_Hijo_Click(Index As Integer)
  'Invoca a los formularios de reportes
  On Error GoTo LineaError
  
  Select Case Index
    Case kMNU_MANTENIMIENTO_PRODUCTO
      frmProductoLista.Show vbModal
    Case kMNU_MANTENIMIENTO_CONFIGURACION_TECLADO
      frmConfiguraBotonProducto.Show vbModal
    Case kMNU_MANTENIMIENTO_TIPO_CAMBIO
      frmTipoCambioLista.Show vbModal
    Case kMNU_MANTENIMIENTO_CABECERA_IMPRESION
      frmCabeceraImpresionLista.Show vbModal
    Case kMNU_MANTENIMIENTO_CONFIGURA_IMPRESORA
      frmConfiguraImpresora.Show vbModal
    Case kMNU_MANTENIMIENTO_INICIAR_CAJA
      FrmApertura.Show
    End Select
  Exit Sub
  
LineaError:
  MsgBox Err.Description, vbOKOnly + vbInformation
End Sub

Private Sub mnuOperaciones_Hijo_Click(Index As Integer)
  'Invoca a los formularios de reportes
  Dim sSQL As String
  Dim ADORS As ADODB.Recordset
  
  On Error GoTo LineaError
  bAcceso = False
  intentos = 0
  If nTipoCambio = 0 Then
    Call CapturaTipoCambio
  End If
  Select Case Index
    Case kMNU_OPERACIONES_CAMBIO_TURNO
      If MsgBox("Esta Operación Cambiará el Turno" + Chr(13) + " Desea Continuar?", vbQuestion + vbYesNo + vbDefaultButton2, "Confirmar Cambio de Turno") = vbYes Then
        Load frmAutorizarOperacion
        Set frmAutorizarOperacion.frmPadre = Me
        frmAutorizarOperacion.Show vbModal
        If Not bAcceso Then Exit Sub
        'bAcceso = False
        Call CambioTurno
      End If
    Case kMNU_OPERACIONES_CAJA_REGISTRADORA
      If nTipoCambio <> 0 Then frmCajaRegistradora.Show vbModal
    Case kMNU_OPERACIONES_CAJA_REGISTRADORA_FULL
      If nTipoCambio <> 0 Then
        frmCajaRegistradoraFull.Inicio
        frmCajaRegistradoraFull.Show vbModal
      End If
    Case kMNU_REPORTE_X
      Load frmAutorizarOperacion
      Set frmAutorizarOperacion.frmPadre = Me
      frmAutorizarOperacion.Show vbModal
      If Not bAcceso Then Exit Sub
      'bAcceso = False
      Call ReportesEspeciales(10, "X", True)
    Case kMNU_REPORTE_X1
      Load frmAutorizarOperacion
      Set frmAutorizarOperacion.frmPadre = Me
      frmAutorizarOperacion.Show vbModal
      If Not bAcceso Then Exit Sub
      'bAcceso = False
      Call ReportesEspeciales1(10, "X", True)
    Case 17
      Load frmAutorizarOperacion
      Set frmAutorizarOperacion.frmPadre = Me
      frmAutorizarOperacion.Show vbModal
      If Not bAcceso Then Exit Sub
      'bAcceso = False
      ExportaX1aExcel
    Case kMNU_REPORTE_Z
      
      If MsgBox("Esta Operación Cerrará Caja" + Chr(13) + " Desea Continuar?", vbQuestion + vbYesNo + vbDefaultButton2, "Confirmar Cierre de Caja") = vbYes Then
        Load frmAutorizarOperacion
        Set frmAutorizarOperacion.frmPadre = Me
        frmAutorizarOperacion.Show vbModal
        
        If Not bAcceso Then Exit Sub
        If Not ValidaCierre Then Exit Sub
        
        Dim NumiCierreCaja As Integer
        Dim rsConfiguracion As New ADODB.Recordset
        
        'CAMBIAR ESTA CONSULTA. UTILIZAR EL STANDAR DEL SISTEMA
        sSQL = "select icierrecaja from configuracion"
        
        If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, rsConfiguracion) Then MsgBox "Error"
                
        NumiCierreCaja = rsConfiguracion!iCierreCaja
        
        '1 Inicio de Cierre de Caja
        If NumiCierreCaja = 0 Or NumiCierreCaja = 6 Then
          ADOCnn.Execute "update configuracion set icierrecaja=1"
          NumiCierreCaja = 1
        End If
        
        '2 Creacion de Z1 en SQL SERVER
        If NumiCierreCaja = 1 Then
          Call RegistrarZ1(-1)
          ADOCnn.Execute "update configuracion set icierrecaja=2"
          NumiCierreCaja = 2
        End If
            
        '3 ReportesEspeciales1 Terminado  (Z1)
        If NumiCierreCaja = 2 Then
          bCerrar = False
          intentos = 1
          Call ReportesEspeciales1(11, "Z")
          ADOCnn.Execute "update configuracion set icierrecaja=3"
          NumiCierreCaja = 3
        End If
    
        '4 ReportesEspeciales Terminado   (Z)
        If NumiCierreCaja = 3 Then
          intentos = 1
          Call ReportesEspeciales(11, "Z")
          ADOCnn.Execute "update configuracion set icierrecaja=4"
          NumiCierreCaja = 4
        End If
        
        'NOTA: MODIFICAR LOS QUERYS DEL CUADRE PARA OPERACION Y OPERACIONPARALELA
        '5 Cuadre de Caja
        If NumiCierreCaja = 4 Then
          frmCuadre.Inicio 1
          frmCuadre.Show vbModal
          ADOCnn.Execute "update configuracion set icierrecaja=5"
          NumiCierreCaja = 5
        End If
    
        '6 Sincronizacion de Transacciones
        If NumiCierreCaja = 5 Then
          Call BlanqueaTransacciones
          ProcesoCaja False
        End If
      End If
    Case kMNU_CUADRE_CAJA
      bCerrar = False
      frmCuadre.Inicio 2
      frmCuadre.Show vbModal
    Case kMNU_REPORTE_OC
      Call ReporteParalelo(20)
    Case kMNU_REPORTE_NC
      Call ReporteParalelo(21)
    Case kMNU_REPORTE_PADE
      Call ReporteParalelo(22)
    Case kMNU_REPORTE_PADV
      Call ReporteParalelo(23)
    Case kMNU_REPORTE_LIQPAD
      Call ReporteParalelo(24)
    Case kMNU_OPERACIONES_CONSULTAOP
      frmOperacionParalelaConsulta.Inicio
      frmOperacionParalelaConsulta.Show
  End Select
  
  Exit Sub
  
LineaError:
  MsgBox Err.Description, vbOKOnly + vbInformation
End Sub

Private Sub mnuAcercaDe_Click()
  'Invoca a Form AcercaDe
  On Error GoTo LineaError

  frmAcercaDe.Show vbModal
  Exit Sub
  
LineaError:
  MsgBox Err.Description, vbOKOnly + vbInformation
End Sub

Private Sub mnuOperCtaCte_Click()
CapturaTipoCambio
If nTipoCambio <> 0 Then
    frmCajaFacturacionPagos.Show vbModal
End If
End Sub

Private Sub mnuRptStock_Click()

  On Error GoTo LineaError

  Screen.MousePointer = vbHourglass
  
  Dim STRSQL As String
  Dim i As Integer
  Dim strCodigoFamilia As String
  Dim strFechaInicio As String
  Dim strFechaFin As String
  Dim strCodigoAlmacen As String
  Dim strDescripAlmacen As String
  
  'sw Add
  Dim bFlagDesc1 As String
  Dim bFlagDesc2 As String
  Dim Texto_imprimir As String
  'Obtenemos parametros
  Dim dFechaIni As Date
  Dim dFechaFin As Date
  
  strCodigoFamilia = "*"
  
  

  strCodigoAlmacen = kPAR_ALMACEN
  Dim rsAlmacen As ADODB.Recordset
  If Not oDBAdmin.EjecutarConsulta(ADOCnnSQL, "select vdscalmac  from almacen where ccodalmac='" & strCodigoAlmacen & "'", rsAlmacen) Then GoTo Salir
  
  If Not rsAlmacen.EOF Then
    strDescripAlmacen = rsAlmacen!vDscAlmac
  End If
  
  
  dFechaFin = Now
  dFechaIni = DateAdd("m", -1, Now)
  
  'sw Add
  bFlagDesc1 = "0"
  bFlagDesc2 = "0"
  
  strFechaInicio = Format(dFechaIni, "YYYYMMDD")
  strFechaFin = Format(dFechaFin, "YYYYMMDD")
  


  Dim rs As ADODB.Recordset
  'sw ORIGINAL strSQL = " usp_Log_Saldos '" & strFechaInicio & "'," & _
           "                '" & strFechaFin & "'," & _
           "                '" & strCodigoFamilia & "'," & _
           "                '" & psUsuario_Nombre & "'"
  STRSQL = " usp_Log_Saldos '" & strFechaInicio & "'," & _
           "                '" & strFechaFin & "'," & _
           "                '" & strCodigoFamilia & "'," & _
           "                ''," & _
           "                " & bFlagDesc1 & "," & _
            "               " & bFlagDesc2 & "," & _
           "                '" & strCodigoAlmacen & "'"
  
  
  If Not oDBAdmin.EjecutarConsulta(ADOCnnSQL, STRSQL, rs) Then GoTo Salir
        
  If Not rs.EOF Then

          'sMovimiento = ADORS!nTipMov
          Texto_imprimir = ""
          Call CabeceraImpresion
          
            For i = 0 To UBound(sCabecera) - 1
              If Not Trim(sCabecera(i)) = "" Then
                Texto_imprimir = Texto_imprimir + Right(Space(60) + sCabecera(i), Int(Len(Space(40) + sCabecera(i)) / 2)) + vbCrLf
              End If
            Next
        
        
        'Texto_imprimir = "-----------------------------" + vbCrLf
        Texto_imprimir = Texto_imprimir + "---------------------------------------" + vbCrLf
        Texto_imprimir = Texto_imprimir + Space(10) + "LISTADO DE SALDOS" + vbCrLf
        Texto_imprimir = Texto_imprimir + "---------------------------------------" + vbCrLf
        Texto_imprimir = Texto_imprimir + "Fecha Inicio: " + CStr(dFechaIni) + vbCrLf
        Texto_imprimir = Texto_imprimir + "Fecha Fin   : " + CStr(dFechaFin) + vbCrLf
        Texto_imprimir = Texto_imprimir + "Almacen     : " + strDescripAlmacen + vbCrLf
        
        
        Texto_imprimir = Texto_imprimir + vbCrLf
        
        Texto_imprimir = Texto_imprimir + "Codigo " + Space(5) + "Descripcion " + Space(3) + "UM" + Space(2) + "Cantidad" + vbCrLf
        Do While Not rs.EOF
            Texto_imprimir = Texto_imprimir + rs!ccodidart + Space(2) + Left(rs!descarticulo + Space(16), 16) + Space(2) + Left(rs!unidmed, 3) + Space(2) + CStr(rs!saldoanterior - rs!saldos) + vbCrLf
            rs.MoveNext
        Loop
        
        Texto_imprimir = Texto_imprimir + "---------------------------------------"



        printer.FontSize = 8 '11
        printer.FontName = "Courier New"

        printer.Print Texto_imprimir
        printer.Print " "
        printer.Print " "
        printer.EndDoc



  Else

    MsgBox "No existen datos para su consulta."
  End If
  
  GoTo Salir
  
LineaError:
  MsgBox Err.Description, vbOKOnly + vbInformation
  
Salir:
  Set rs = Nothing
  Screen.MousePointer = vbDefault



End Sub

Private Sub mnuSalir_Click()
  'Salir de la aplicación
  On Error GoTo LineaError

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

Sub ReportesEspeciales(iTipoReporte As Integer, sLetraReporte As String, Optional bParcial As Boolean = False, Optional bOC As Boolean = False)
  On Error GoTo LineaError

  Screen.MousePointer = vbHourglass
  Dim a As String
  Dim efec As Double
  Dim sSQL As String
  Dim ADORS As ADODB.Recordset
  Dim ADORS1 As ADODB.Recordset
  Dim i As Long, j As Long
  Dim iInicio As Long, iFin As Long
  Dim sTecla1 As String, sTecla2 As String, sTecla3 As String
  Dim cnSQL As ADODB.Connection
  Dim nTipoCambio As Double
  Dim nTotal As Double
  Dim nTotalItems As Long
  Dim nTotalPrecUnit As Double
  Dim nTotalTransac As Double
  Dim nprectransac As Double
  Dim nSubtotal As Double
  Dim nPorcTotal As Double
  Dim nItems As Long
  Dim nPrecUnit As Double
  Dim nPrecUnitOrig As Double
  Dim sProdDescCrta As String
  Dim sTeclDesc As String

  Dim sUsuario As String
  Dim iCodOper As Long
  Dim sFecha As String
  Dim iCodOperAutogenerado As Long
  'totales
  ConfiguraImpresora kPAR_IMPRESORA
  
  'Montos por transaccion - Numero transac
  Dim iCashMont As Double, iCashNume As Long 'Transaccion Cash
  Dim iVoucMont As Double, iVoucNume As Long 'Transaccion Voucher
  Dim iDolaMont As Double, iDolaNume As Long 'Transaccion Dolares
  Dim iAnulMont As Double, iAnulNume As Long 'Transaccion Anulado
  Dim iPagoMont As Double, iPagoNume As Long 'Transaccion Pago
  Dim iVOPOMont As Double, iVOPONume As Long 'Transaccion VO-POSTN
  Dim TotalVenta As Double
  Dim cOperId As String
  Dim cOperIdP As String
  Dim sCondicionOpe As String
  Dim sCondicionOpeP As String
  
  ReDim sImprimir(20)
  iNroFilas = 0
  iNroBloques = 0
  
  'Captura la mayor transaccion de las permitidas
  sSQL = " SELECT MAX(cOperCode) AS maximo FROM Operacion"
  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo LineaError

  If ADORS.RecordCount = 1 Then
    If IsNull(ADORS("maximo")) Then
      iCodOper = 1
    Else
      iCodOper = LeerCampo(ADORS("maximo")) + 1
    End If
  End If


  'SE OBTIENE FECHA DEL SISTEMA DE LA BASE DE DATOS DE SQL - version 2.0.5
   
   Dim cnFECHASQL As ADODB.Connection
   Dim sqlFECHASIS As String
   Dim rsFEchaSis As ADODB.Recordset
      
   If oDBAdmin.AbrirBD(cnFECHASQL, psConnectString, False) = True Then
     
     sqlFECHASIS = "select convert(varchar,getdate(),120) as fechasis"
     If oDBAdmin.EjecutarConsulta(cnFECHASQL, sqlFECHASIS, rsFEchaSis) = True Then
         
         If rsFEchaSis!fechasis <> "" Then
             sFecha = rsFEchaSis!fechasis
         End If

     Else
         'no pudo abrir el rs: que consulte al access
         'MsgBox "No se pudo obtener la fecha, hay problemas de conexión de la red con la Base de Datos Principal", vbOKOnly + vbExclamation, "Aviso"
         sFecha = Format(Now(), "DD/MM/YYYY hh:mm")
         'Unload Me
     End If

     Set cnFECHASQL = Nothing

   Else
     'no pudo conectarse al sql: que consulte al access
      'MsgBox "No se pudo obtener la fecha, hay problemas de conexión de la red con la Base de Datos Principal", vbOKOnly + vbExclamation, "Aviso"
      sFecha = Format(Now(), "DD/MM/YYYY hh:mm")
      'Unload Me
   End If
   
   'FIN FECHA DEL SISTEMA DE LA BASE DE DATOS DE SQL


  'sFecha = Format(Now(), "DD/MM/YYYY hh:mm")

  Set ADORS = Nothing
  
  'Inserta esta operacion
  sSQL = "INSERT INTO OPERACION (cOperCode, cUserDesc, dOperFech, cOperTipo, cCajaCodi, cTurnoCodi) " + _
         "VALUES" + _
         "($, '$', '$', $, '$', '$')"
  sSQL = VBsprintf(sSQL, CStr(iCodOper), psUsuario_Descripcion, sFecha, iTipoReporte, kPAR_CAJA, kPAR_TURNO)
  If Not oDBAdmin.EjecutarSentencia(ADOCnn, sSQL) Then GoTo LineaError
    
  sSQL = "SELECT MAX(cOperID) as MAXIMO FROM OPERACION"
  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo LineaError

  If ADORS.RecordCount = 1 Then
    If IsNull(ADORS("maximo")) Then
      iCodOperAutogenerado = 1
    Else
      iCodOperAutogenerado = LeerCampo(ADORS("maximo")) + 1
    End If
  End If
  Set ADORS = Nothing
  
If Not oDBAdmin.AbrirBD(cnSQL, psConnectString) Then GoTo Salir 'ABRE SQL OMNISYS

  sSQL = "SELECT cOperId, cOperIdP FROM Configuracion"
  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then
    cOperId = ""
    cOperIdP = ""
  Else
    cOperId = LeerCampo(ADORS("cOperId"))
    cOperIdP = LeerCampo(ADORS("cOperIdP"))
  End If
  
  If cOperId = "" Or cOperIdP = "" Then
    sCondicionOpe = "   and o.cOperflagabierto = 1 "
    sCondicionOpeP = "   and o.cOperflagabierto = 1 "
  Else
    sCondicionOpe = "   and o.cOperId > " + cOperId + " "
    sCondicionOpeP = "   and o.cOperCode > " + cOperIdP + " "
  End If
  
  '------fecha de apertura de caja -----------
  Dim dtfechaaper As Date
  sSQL = "SELECT MIN(o.dOperFech) AS Fecha " & _
         "  FROM Operacion o " & _
         " WHERE o.nMontoDolares > 0 And o.cOperFlagAnula= 0 " & sCondicionOpe
         
  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo LineaError
  If ADORS.RecordCount = 1 Then
    dtfechaaper = FormatDateTime(IIf(IsNull(ADORS!fecha), sFecha, ADORS!fecha), vbGeneralDate)
  Else
    dtfechaaper = sFecha
  End If
  Set ADORS = Nothing
  'Captura el tipo de cambio
  sSQL = " SELECT nTipoCambMont FROM Tipo_Cambio" & _
         " WHERE FORMAT(dTipoCambFech, 'yyyy/mm/dd') = '" & Format(dtfechaaper, "yyyy/mm/dd") & "'"

  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo LineaError
  If ADORS.RecordCount <> 1 Then
    MsgBox "Para registrar ventas en dólares debe registrar el tipo de cambio"
    nTipoCambio = 0
  Else
    nTipoCambio = LeerCampo(ADORS("nTipoCambMont"))
  End If
  
  sSQL = " SELECT SUM(O.nProdCant)AS TotItem, sum ((O.nProdCant * O.nProdPrecUnit) + ((O.nProdCant * O.nProdPrecUnit) * O.nDscto/100))  AS TotMonto " & _
         "   FROM Operacion AS O" & _
         "  WHERE cOperTipo in (1,2,3)  " & sCondicionOpe & _
         "    AND cprodcode <> 0 AND cOperflaganula= 0 "
         
  If bParcial Then sSQL = sSQL + " AND cTurnoCodi = '" & kPAR_TURNO & "'"
     
  nTotal = 0
  nTotalItems = 0
  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo LineaError
  
  Do While Not ADORS.EOF
    If IsNull(LeerCampo(ADORS("TotItem"))) Or LeerCampo(ADORS("TotItem")) = "" Then
      nTotalItems = nTotalItems + 0
    Else
      nTotalItems = nTotalItems + LeerCampo(ADORS("TotItem"))
    End If
    If IsNull(LeerCampo(ADORS("TotMonto"))) Or LeerCampo(ADORS("TotMonto")) = "" Then
      nTotal = nTotal + 0
    Else
      nTotal = nTotal + LeerCampo(ADORS("TotMonto"))
    End If
    ADORS.MoveNext
  Loop
  
  TotalVenta = nTotal
  If nTotalItems = 0 Then
    nTotalPrecUnit = 0
  Else
    nTotalPrecUnit = nTotal / nTotalItems
  End If
  
  'Captura Cabecera
  Call CabeceraImpresion
  
  For i = 0 To UBound(sCabecera) - 1
    If Not Trim(sCabecera(i)) = "" Then
      Call AgregarLineaBloque(Right("                  " + sCabecera(i), Int(Len(sCabecera(i)) / 2) + 20))
    End If
  Next
  Call AgregarLineaBloque(vbCrLf + Format(CStr(Now()), "DD/MM/YYYY HH:MM (DDD)") & " NS:" & kSERIE_DISCO_DURO)
  
  printer.FontSize = 8 ' 11
  printer.FontName = "Courier New"
  
  'PRIMER BLOQUE
  Call AgregarLineaBloque("=======================================")
  Call AgregarLineaBloque(sLetraReporte + "  CHECK                             01")
  Call AgregarLineaBloque("=======================================")
  Call AgregarLineaBloque("TOTAL     " + Right("                             *" + Format(CStr(nTotalItems), "###,###,##0.00"), 29))
  Call AgregarLineaBloque("          " + Right("                             *" + Format(CStr(nTotal), "###,###,##0.00"), 29))
  Call AgregarLineaBloque("=======================================")
    
  sSQL = " SELECT P.cProdCode as codigo, SUM(O.nProdCant) AS TotalItem,  max(P.SProdDescCrta) as Producto, max(P.cCodCatalogo) as Catalogo " & _
         " FROM Producto AS P " & _
         " LEFT outer JOIN Operacion AS O " & _
         "   ON P.cProdCode = O.cProdCode " & _
         " WHERE cOperflagAnula= 0 " & sCondicionOpe
   
  If bParcial Then sSQL = sSQL + " AND cTurnoCodi = '" & kPAR_TURNO & "'"
  sSQL = sSQL + " GROUP BY P.cProdCode Having  SUM(O.nProdCant) > 0 "
  Dim catal As String
  
  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo LineaError
  If ADORS.RecordCount > 0 Then
    Call AgregarLineaBloque("Producto                        Cant.  ")
    Call AgregarLineaBloque("---------------------------------------")
    
    Do While Not ADORS.EOF
      nItems = LeerCampo(ADORS("TotalItem"))
      catal = LeerCampo(ADORS("Catalogo"))
      sProdDescCrta = LeerCampo(ADORS("Producto"))
      nPorcTotal = (nSubtotal * 100) / IIf(nTotal = 0, 1, nTotal)
      
      Call AgregarLineaBloque(Left(catal + Space(10), 10) + " " + Left(sProdDescCrta + Space(17), 17) + " " + Right(Space(10) & Format(CStr(nItems), "###,###,##0.00"), 10))
      
      ADORS.MoveNext
    Loop
    
    Call AgregarLineaBloque("=======================================")
  End If
 
  '----------------------Vouchers---------------------------
  Dim iEfeVoucher As Double
  sSQL = " SELECT SUM((o.nProdCant * o.nProdPrecUnit) + ( (o.nProdCant * o.nProdPrecUnit) * o.nDscto/100)) AS TOTAL " & _
         " FROM Operacion o " & _
         " WHERE o.cOperTipo = 2  " & _
         " AND o.cOperFlagAnula = 0 " & sCondicionOpe
         
  If bParcial Then sSQL = sSQL + " AND cTurnoCodi = '" & kPAR_TURNO & "'"

  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo LineaError
  If Not ADORS.EOF Then
    iVoucMont = "0" & (ADORS!Total)
  Else
    iVoucMont = 0
  End If
  Set ADORS = Nothing
  '----------------------Efectivo en vouchers---------------------------
  sSQL = " SELECT SUM(o.nMontosoles) AS TOTAL" & _
         " FROM Operacion o " & _
         " WHERE o.cOperTipo = 1 AND TRIM(o.cEmisor) <> '' " & _
         " AND o.cDoctipo in ('01', '02') " & _
         " AND o.cOperFlagAnula = 0 " & sCondicionOpe

  If bParcial Then sSQL = sSQL + " AND cTurnoCodi = '" & kPAR_TURNO & "'"
          
  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo LineaError
  If Not ADORS.EOF Then
    iEfeVoucher = "0" & (ADORS!Total)
  Else
    iEfeVoucher = 0
  End If
  Set ADORS = Nothing
  iVoucMont = iVoucMont - iEfeVoucher
  '-------------------------voucher en liquidacion ------------------------
  sSQL = " SELECT SUM(o.nmontosoles +  o.nprodprecunit ) as TOTAL" & _
         "   FROM operacion o " & _
         "  WHERE o.cOperTipo = 2 and o.cDocTipo='14' " & _
         "    AND o.cOperFlagAnula = 0" & sCondicionOpe
         
  If bParcial Then sSQL = sSQL + " AND cTurnoCodi = '" & kPAR_TURNO & "'"
         
  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo LineaError
  iVoucMont = iVoucMont + val("0" & ADORS!Total)
  
  '--------contabilizando tickets---------------------------------------------------------
    
  sSQL = "SELECT distinct(o.cOpercode) " & _
         " FROM operacion o WHERE  (o.cdoctipo='02' or o.cdoctipo='01') " & _
         " AND o.cOperFlaganula=0 "
          
  If bParcial Then sSQL = sSQL + " AND cTurnoCodi = '" & kPAR_TURNO & "'"
   
  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo LineaError
  iCashNume = ADORS.RecordCount
   
  sSQL = "SELECT distinct(o.cOpercode) " & _
         " FROM operacion o WHERE  (o.cemisor <> ''  or o.cOperTipo=2) " & _
         " AND o.cOperFlaganula=0 " & sCondicionOpe
   
  If bParcial Then sSQL = sSQL + " AND cTurnoCodi = '" & kPAR_TURNO & "'"
  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo LineaError
   
  iVoucNume = ADORS.RecordCount
         
  'TERCER BLOQUE
  Call AgregarLineaBloque("=======================================")
  Call AgregarLineaBloque(sLetraReporte + "  CHECK  TOTAL TRANSACCIONES        02")
  Call AgregarLineaBloque("=======================================")
    
  If iCashNume <> 0 Then
    Call AgregarLineaBloque("TICKET      " + Right(Space(27) + "000000" + CStr(iCashNume) + "   ", 27))
    Call AgregarLineaBloque("            " + Right(Space(27) + "*" + Format(CStr(TotalVenta), "###,###,##0.00"), 27))
  End If
  
  Call AgregarLineaBloque("=======================================")
  If iVoucNume <> 0 Then
    Call AgregarLineaBloque("VOUCHER VTA " + Right(Space(27) + "000000" + CStr(iVoucNume) + "   ", 27))
    Call AgregarLineaBloque("            " + Right(Space(27) + "*" + Format(CStr(iVoucMont), "###,###,##0.00"), 27))
  End If
   
  'AQUI VA LO DEL PAGO Y TRANSACCIONES ANULADAS
  sSQL = " SELECT SUM((o.nProdPrecUnit * o.nProdCant) + ((o.nProdPrecUnit * o.nProdCant) * o.nDscto/100)) as TKMontNulos" & _
         "   FROM Operacion o " & _
         "  WHERE o.cOperflaganula= 1 " & _
         "    AND o.cProdcode <>  0 " & _
         "    AND o.cOpercodeanula <> 0 " & sCondicionOpe

  If bParcial Then sSQL = sSQL + " AND cTurnoCodi = '" & kPAR_TURNO & "'"
  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo LineaError
  If Not ADORS.EOF Then
    iVOPOMont = IIf(IsNull(ADORS!TKMontNulos), 0, ADORS!TKMontNulos)
  End If
  
  sSQL = " SELECT distinct o.cOperCode " & _
         "   FROM Operacion o " & _
         "  WHERE o.cOperflaganula= 1 " & _
         "    AND o.cProdcode <>  0 " & _
         "    AND o.cOpercodeanula <> 0 " & sCondicionOpe
        
  If bParcial Then sSQL = sSQL + " AND cTurnoCodi = '" & kPAR_TURNO & "'"
  
  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo LineaError
  If Not ADORS.EOF Then
    iVOPONume = IIf(IsNull(ADORS.RecordCount), 0, ADORS.RecordCount)
  End If
  
  '-----Recaudaciones
  sSQL = " SELECT SUM((o.nProdPrecUnit * o.nProdCant)  + ((o.nProdPrecUnit * o.nProdCant)  * o.nDscto/100)) as TKMontPago, COUNT(*) as TKPago " & _
         "   FROM Operacion o " & _
         "  WHERE o.cOperflaganula= 0 " & _
         "    AND o.cOpercodeanula = 0 " & _
         "    AND o.cOpertipo= 5 " & sCondicionOpe
    
  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo LineaError
  If Not ADORS.EOF Then
    iPagoMont = IIf(IsNull(ADORS!TKMontPago), 0, ADORS!TKMontPago)
    iPagoNume = IIf(IsNull(ADORS!TKPago), 0, ADORS!TKPago)
  End If
  
  '---------------Datos de Apertura de caja
  Dim iAperturasoles As Double
  Dim iAperturadolares As Double
  sSQL = " SELECT  nImporApertSol, nImporApertDol " & _
         " FROM Configuracion "

  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo LineaError
  iAperturasoles = (ADORS!nImporApertSol)
  iAperturadolares = (ADORS!nImporApertDol)
  
  'DEL CUADRE
  Dim iCashMontdol As Double
  iCashMont = 0
  iCashMontdol = 0
  
 'Total de Dolares obtenidos por ventas
  sSQL = " SELECT SUM(o.nMontoDolares-o.nVueltoDolares) AS TOTAL" & _
         " FROM Operacion o" & _
         " WHERE o.cOperTipo in (1, 3) " & _
         " AND o.cDoctipo in ('01','02') " & _
         " AND o.cOperFlaganula= 0 " & sCondicionOpe
         
  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo LineaError
  
  Do While Not ADORS.EOF
    If IsNull(LeerCampo(ADORS!Total)) Or (LeerCampo(ADORS!Total)) = "" Then
      iCashMontdol = 0
    Else
      iCashMontdol = iCashMontdol + LeerCampo(ADORS!Total)
    End If
    ADORS.MoveNext
  Loop
  
  'Calculo del Cash Soles
  sSQL = " SELECT SUM(o.nMontoSoles + o.nProdPrecUnit) AS TOTAL" & _
         " FROM Operacion o " & _
         " WHERE (o.cOperTipo in( 1, 3)) " & _
         " AND o.cDoctipo in ('01','02') " & _
         " AND o.cProdCode = 0 " & _
         " AND o.cOperFlaganula= 0 " & sCondicionOpe
         
  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo LineaError
  
  Do While Not ADORS.EOF
    If IsNull(LeerCampo(ADORS!Total)) Or (LeerCampo(ADORS!Total)) = "" Then
      iCashMont = 0
    Else
     iCashMont = iCashMont + LeerCampo(ADORS!Total)
    End If
    ADORS.MoveNext
  Loop
    
  Set ADORS = Nothing
  
  '------------------------------------------------
  If iPagoMont > (iAperturasoles + iCashMont) Then
    iPagoMont = iAperturasoles + iCashMont
  End If
  
  If iPagoMont > 0 Then 'Existe Pago
    Call AgregarLineaBloque("RECAUDACION " + Right(Space(27) + "000000" + CStr(iPagoNume) + "   ", 27))
    Call AgregarLineaBloque("            " + Right(Space(27) + "(" + Format(CStr(iPagoMont), "###,###,##0.00") + ")", 27))
  End If
  
 'QUINTO BLOQUE
  Call AgregarLineaBloque("=======================================")
  Call AgregarLineaBloque(sLetraReporte + "  CHECK  TOTALES                    03")
  Call AgregarLineaBloque("=======================================")
   
  '-------FIN DEL CUADRE---------------'
  If iAperturasoles > 0 Then 'Existe monto de apertura
    Call AgregarLineaBloque("APERT.    S/" + Right(Space(27) + "*" + Format(CStr(iAperturasoles), "###,###,##0.00"), 27))
  End If

  If iAperturadolares > 0 Then 'Existe monto de apertura dolares
    Call AgregarLineaBloque("APERT.     $" + Right(Space(27) + "*" + Format(CStr(iAperturadolares), "###,###,##0.00"), 27))
  End If
  
  If (iAperturasoles + iCashMont - iPagoMont) <> 0 Then   'Existe dinero Soles
    Call AgregarLineaBloque("---------------------------------------")
    Call AgregarLineaBloque("CASH-DRW S/." + Right(Space(27) + "*" + Format(CStr(iAperturasoles + iCashMont - iPagoMont), "###,###,##0.00"), 27))
  End If
    
  If (iAperturadolares + iCashMontdol) <> 0 Then 'Existe dinero Dolares
    Call AgregarLineaBloque("CASH-DRW$(TC S/." & Right(Space(5) & CStr(nTipoCambio), 5) & ")" & Right(Space(17) & "*" & Format(CStr(iAperturadolares + iCashMontdol), "###,###,##0.00"), 17))
  End If
  
  If iVoucNume <> 0 Then 'Existe VOUCHERS
    Call AgregarLineaBloque("VOUCHER     " + Right(Space(27) + "*" + Format(CStr(iVoucMont), "###,###,##0.00"), 27))
  End If
  
  If iVOPONume <> 0 Then 'Existe anulada
    Call AgregarLineaBloque("NULOS  TK   " + Right(Space(27) + "000000" + CStr(iVOPONume) + "   ", 27))
    Call AgregarLineaBloque("            " + Right(Space(27) + "*" + Format(CStr(iVOPOMont), "###,###,##0.00"), 27))
  End If

  Call AgregarLineaBloque("")
  
  Select Case sLetraReporte
    Case "Z" 'Cambiar a reporte Z
      'BLOQUE TICKETS EN GENERAL (B Y F)
      Call AgregarLineaBloque("=======================================")
      Call AgregarLineaBloque("TICKETS ")
      Call AgregarLineaBloque("=======================================")
      Call AgregarLineaBloque("      " + Format(CStr(Now()), "DD/MM/YYYY (DDD)"))
      
      sSQL = " SELECT min(o.cdoccodigo) as mini,max(o.cdoccodigo) as maxi" & _
             " FROM Operacion o WHERE o.cOperTipo in (1,2,3,4,5) and o.cdoctipo IN ('01','02')" & sCondicionOpe
         
      If bParcial Then sSQL = sSQL + " AND cTurnoCodi = '" & kPAR_TURNO & "'"
      
      If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS1) Then GoTo LineaError
      
      Do While Not ADORS1.EOF
        Call AgregarLineaBloque("RANGO [ " + CStr(IIf(IsNull(ADORS1.Fields("mini")), "", ADORS1.Fields("mini"))) + "  ---  " + CStr(IIf(IsNull(ADORS1.Fields("maxi")), "", ADORS1.Fields("maxi"))) + " ]")
        
        ADORS1.MoveNext
      Loop
      Set ADORS1 = Nothing
    
      'CUARTO BLOQUE
      Call AgregarLineaBloque("=======================================")
      Call AgregarLineaBloque("=======================================")
      Call AgregarLineaBloque("TICKETS -- BOLETA TOTAL TRANSACC.")
      Call AgregarLineaBloque("=======================================")
      Call AgregarLineaBloque("      " + Format(CStr(Now()), "DD/MM/YYYY (DDD)"))
  
      sSQL = "SELECT COUNT(*)AS TotDoc, sum ((O.nProdCant * O.nProdPrecUnit)+ ((O.nProdCant * O.nProdPrecUnit) * O.nDscto/100))  AS TotMonto " _
           & "FROM Operacion AS O " _
           & "WHERE cOperTipo in (1,2,3) " _
           & "AND cprodcode <> 0 AND cOperflaganula= 0 AND cdoctipo='02' " & sCondicionOpe

      If bParcial Then sSQL = sSQL + " AND cTurnoCodi = '" & kPAR_TURNO & "'"
      
      If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS1) Then GoTo LineaError
      
      Do While Not ADORS1.EOF
        Call AgregarLineaBloque("TICKET      " + Right(Space(27) + "000000" + CStr(ADORS1.Fields("TotDoc")) + "   ", 27))
        Call AgregarLineaBloque("            " + Right(Space(27) + "*" + Format(CStr(IIf(IsNull(ADORS1.Fields("TotMonto")), "0", ADORS1.Fields("TotMonto"))), "###,###,##0.00"), 27))
        
        ADORS1.MoveNext
      Loop
      Set ADORS1 = Nothing
    
      'QUINTO BLOQUE
      Call AgregarLineaBloque("=======================================")
      Call AgregarLineaBloque("=======================================")
      Call AgregarLineaBloque("TICKETS -- FACTURA TOTAL TRANSACC.")
      Call AgregarLineaBloque("=======================================")
      Call AgregarLineaBloque("      " + Format(CStr(Now()), "DD/MM/YYYY (DDD)"))
     
      sSQL = "SELECT COUNT(*)AS TotDoc, sum ((O.nProdCant * O.nProdPrecUnit)+ ((O.nProdCant * O.nProdPrecUnit) * O.nDscto/100))  AS TotMonto " _
           & "FROM Operacion AS O " _
           & "WHERE cOperTipo in (1,2,3) " _
           & "AND cprodcode <> 0 AND cOperflaganula= 0 AND cdoctipo='01' " & sCondicionOpe
         
      If bParcial Then sSQL = sSQL + " AND cTurnoCodi = '" & kPAR_TURNO & "'"
      
      If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS1) Then GoTo LineaError
      
      Do While Not ADORS1.EOF
        Call AgregarLineaBloque("TICKET      " + Right(Space(27) + "000000" + CStr(ADORS1.Fields("TotDoc")) + "   ", 27))
        Call AgregarLineaBloque("            " + Right(Space(27) + "*" + Format(CStr(IIf(IsNull(ADORS1.Fields("TotMonto")), "0", ADORS1.Fields("TotMonto"))), "###,###,##0.00"), 27))
        
        ADORS1.MoveNext
      Loop
      Set ADORS1 = Nothing
    
      Call AgregarLineaBloque("=======================================")
  End Select
  
  ReportesConsolidado bParcial, 1
  
  Call AgregarLineaBloque("")
  Call AgregarLineaBloque(" " + Left(psUsuario_Descripcion, 10) + " " + Format(CStr(Now()), "hh:mm") + " ") 'Hora: 6 + 1
  
  '----------------------------------------------'
  If sLetraReporte = "Z" Then
    Dim iCodZ  As Integer
    sSQL = "SELECT idz + 1 as cod, maxid , Imac " & _
           "  FROM _tmp1 " & _
           " WHERE prn=1"
           
    If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo LineaError
    If Not ADORS.EOF Then
      iCodZ = ADORS!cod
      If iCodZ > ADORS!maxid Then iCodZ = 1
    Else
      iCodZ = 1
    End If
   
    Call AgregarLineaBloque("Código Rep. Z: " + Right("000000" + CStr(iCodZ), 6))  'Codigo Z
    Call AgregarLineaBloque("Total acumulado: *" + Format(CStr(ADORS!imac + nTotal), "###,###,##0.00"))
    Set ADORS = Nothing
  End If
  
  Call AgregarLineaBloque("CAJA: " + kPAR_CAJA + "   TURNO: " + kPAR_TURNO)
  Call AgregarLineaBloque("Versión " & App.Major & "." & App.Minor & "." & App.Revision)
  Call AgregarLineaBloque("Correlativo: " & ObtenerCorrelativo(1))
  
  ConfiguraImpresora kPAR_IMPRESORA
  
  Select Case KImpresora
    Case 1
      Call CortePapelGrande(sImprimir, 1, iNroBloques, "RptXOZ")
    Case 0
      For iNroFilas = 0 To iNroBloques
        printer.Print sImprimir(iNroFilas)
      Next
        
      printer.Print " "
      printer.Print " "
      printer.EndDoc
  End Select
 
  bCerrar = True
  
  Select Case sLetraReporte
    Case "X"
    Case "Z"
      kPAR_TURNO = ""
      ProcesoCaja False
  End Select
  
  'ACTUALIZA LOS DATOS EN CONFIGURACION
  sSQL = "UPDATE Configuracion SET cCodiTurno = '" + kPAR_TURNO + "'"
  Call oDBAdmin.EjecutarSentencia(ADOCnn, sSQL)
  
  If sLetraReporte = "Z" And intentos = 1 Then
    sSQL = " UPDATE _Tmp1 SET idz=" & CStr(iCodZ) & _
           " , Imac= Imac + " & CStr(nTotal) & _
           " WHERE prn=1 "
    Call oDBAdmin.EjecutarSentencia(ADOCnn, sSQL)
    
    sSQL = "INSERT INTO _Tmp (idz, tvd, fec, prn)" & _
            " VALUES(" & CStr(iCodZ) & " , " & CStr(nTotal) & ", '" & sFecha & "', 1 )"
        
    Call oDBAdmin.EjecutarSentencia(ADOCnn, sSQL)
  End If
  
  Screen.MousePointer = vbDefault
  
  GoTo Salir
  
LineaError:
  If intentos = 3 Then
    MsgBox "No fue posible imprimir reporte" + sLetraReporte, vbOKOnly + vbInformation, "Error"
    intentos = 0
  Else
    MsgBox Err.Description
    intentos = intentos + 1
    ReportesEspeciales iTipoReporte, sLetraReporte, bParcial, bOC
  End If
  
Salir:
  Set ADORS = Nothing
  Set ADORS1 = Nothing
  Set cnSQL = Nothing
  Screen.MousePointer = vbDefault
End Sub

Sub BlanqueaTransacciones()
  On Error GoTo LineaError
  
  Screen.MousePointer = vbHourglass
  
  Dim sSQL As String
  Dim ADORS As ADODB.Recordset
  Dim cOperId As String
  Dim cOperIdP As String
  
  sSQL = " UPDATE OPERACION SET cOperFlagAbierto = 0" + _
         " WHERE cOperFlagAbierto = 1"
  If Not oDBAdmin.EjecutarSentencia(ADOCnn, sSQL) Then GoTo LineaError
  
  sSQL = " UPDATE OPERACIONPARALELA SET cOperFlagAbierto = 0" + _
         " WHERE cOperFlagAbierto = 1"
  If Not oDBAdmin.EjecutarSentencia(ADOCnn, sSQL) Then GoTo LineaError
  
  sSQL = " UPDATE TCABECERAZ1 SET nidEstado = 0" + _
         " WHERE nidEstado = -1"
  If Not oDBAdmin.EjecutarSentencia(ADOCnnSQL, sSQL) Then GoTo LineaError
  
  'Id de operacion de la tabla OPERACION
  sSQL = "SELECT IIF(ISNULL(MAX(cOperID)),0, MAX(cOperID)) as cOperIDx FROM Operacion Where coperflagabierto = 0 "
  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then
    cOperId = LeerCampo(ADORS("cOperIDx")) = "NULL"
  Else
    cOperId = LeerCampo(ADORS("cOperIDx"))
  End If
   
  'Id de operacion de la tabla OPERACIONPARALELA
  sSQL = "SELECT IIF(ISNULL(MAX(cOperCode)),0, MAX(cOperCode)) as cOperCodex FROM OperacionParalela Where coperflagabierto = 0 "
  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then
    cOperIdP = "NULL"
  Else
    cOperIdP = LeerCampo(ADORS("cOperCodex"))
  End If
  
  sSQL = "UPDATE Configuracion SET cOperId = " + cOperId + ", cOperIdP = " + cOperIdP
    
  Call oDBAdmin.EjecutarSentencia(ADOCnn, sSQL)
  
  ADOCnn.Execute "update configuracion set icierrecaja=6"
  
  boolSincronizarTodo = False
  boolSincronizarCierre = True
  
  Sincronizar
  
  boolSincronizarTodo = True
  boolSincronizarCierre = False
  
    Toolbar1.Buttons(2).Enabled = False
    Toolbar1.Buttons(6).Enabled = False
    Toolbar1.Buttons(8).Enabled = False
    Toolbar1.Buttons(9).Enabled = False
    Toolbar1.Buttons(11).Enabled = False
  
  
  MsgBox "Se han cerrado todas las operaciones."
  Screen.MousePointer = vbDefault
  GoTo Salir
  
LineaError:
  MsgBox Err.Description, vbOKOnly + vbInformation
Salir:
  Screen.MousePointer = vbDefault
End Sub

Public Sub VerificaCierre()
  On Error GoTo LineaError

  Screen.MousePointer = vbHourglass
  
  Dim sSQL As String
  Dim cOperId As String
  Dim sCondicionOpe As String
  Dim ADORS As ADODB.Recordset
  
  sSQL = "SELECT cOperId FROM Configuracion"
  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then
    cOperId = ""
  Else
    cOperId = LeerCampo(ADORS("cOperId"))
  End If
  
  If cOperId = "" Then
    sCondicionOpe = "   and o.cOperflagabierto = 1 "
  Else
    sCondicionOpe = "   and o.cOperId > " + cOperId + " "
  End If

  sSQL = " SELECT count(*) AS TOTAL FROM Operacion o" & _
         " WHERE FORMAT(o.dOperFech, 'yyyy/mm/dd') <> '" & Format(CStr(Now()), "yyyy/mm/dd") & "'" & _
         " AND o.cOperTipo in (1,2,3,4,5)" & sCondicionOpe
  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo Salir
  
  If LeerCampo(ADORS!Total) > 0 Then
    MsgBox "Tiene operaciones abiertas de días anteriores"
  End If
  
LineaError:
  MsgBox Err.Description, vbOKOnly + vbInformation
Salir:
  Screen.MousePointer = vbDefault
End Sub

Private Sub mnuUsuarios_Click()
    frmUsuarioLista.Show vbModal
End Sub

Private Sub ProcesoConectarBDCliente()
  If DateDiff("n", dtFechaUltimaVerificacionConexionBDCliente, Now) >= 1 Then
    dtFechaUltimaVerificacionConexionBDCliente = Now
    ConectarBDCliente False
    If bolExisteConexionBDCliente = True Then
      'Aqui se Sincroniza la informacion de puntos que no esten sincronizadas
      SincronizaDatosCliente
    End If
  End If
End Sub

Private Sub Toolbar1_ButtonClick(ByVal Button As MSComctlLib.Button)
  On Error Resume Next

  Select Case Button.Key
  Case "Productos"
    Call mnuMantenimiento_Hijo_Click(kMNU_MANTENIMIENTO_PRODUCTO)
  Case "TipoCambio"
    Call mnuMantenimiento_Hijo_Click(kMNU_MANTENIMIENTO_TIPO_CAMBIO)
  Case "CajaRegistradora"
    Call CapturaTipoCambio
    If nTipoCambio <> 0 Then Call mnuOperaciones_Hijo_Click(kMNU_OPERACIONES_CAJA_REGISTRADORA)
  Case "CambioTurno"
    Call mnuOperaciones_Hijo_Click(kMNU_OPERACIONES_CAMBIO_TURNO)
  Case "ReporteX"
    Call mnuOperaciones_Hijo_Click(kMNU_REPORTE_X)
  Case "ReporteZ"
    Call mnuOperaciones_Hijo_Click(kMNU_REPORTE_Z)
  Case "CuadreCaja"
    Call mnuOperaciones_Hijo_Click(kMNU_CUADRE_CAJA)
  Case "Salir"
    Call mnuSalir_Click
  Case "ZFecha"
    Call ReportesEspecialesFecha("Z", False)
  Case "Z1Fecha"
    Call ReportesEspeciales1Fecha("Z", False)
  Case "SFecha"
    Call SincronizarFecha
  End Select
End Sub

Sub ReportesEspecialesFecha(sLetraReporte As String, Optional bParcial As Boolean = False)
  On Error GoTo LineaError

  Dim cnSQL As ADODB.Connection
  Dim ADORS As ADODB.Recordset
  Dim ADORS1 As ADODB.Recordset
  Dim a As String
  Dim sSQL As String
  Dim i As Long, j As Long
  Dim nTipoCambio As Double
  Dim nTotal As Double
  Dim nTotalItems As Long
  Dim nTotalPrecUnit As Double
  Dim nSubtotal As Double
  Dim nPorcTotal As Double
  Dim nItems As Long
  Dim sProdDescCrta As String

  'Montos por transaccion - Numero transac
  Dim iCashMont As Double, iCashNume As Long 'Transaccion Cash
  Dim iVoucMont As Double, iVoucNume As Long 'Transaccion Voucher
  Dim iPagoMont As Double, iPagoNume As Long 'Transaccion Pago
  Dim iVOPOMont As Double, iVOPONume As Long 'Transaccion VO-POSTN
  Dim TotalVenta As Double
  
  Dim sCondicionOpe As String
  Dim sCondicionOpeP As String
  Dim fecha As String
  
  ConfiguraImpresora kPAR_IMPRESORA
  
  ReDim sImprimir(20)
  iNroFilas = 0
  iNroBloques = 0
  
  If Not oDBAdmin.AbrirBD(cnSQL, psConnectString) Then GoTo Salir 'ABRE SQL OMNISYS

  fecha = InputBox("Ingrese una fecha", "Fecha de Reporte", "19/07/1979", 500, 700)
  If Not IsDate(fecha) Then
     MsgBox "La fecha debe tener el formato dd/mm/yyyy", vbCritical
     Exit Sub
  End If
  
  Screen.MousePointer = vbHourglass
  
  sCondicionOpe = " and FORMAT(o.dOperFech, 'dd/mm/yyyy') = '" & fecha & "'"
  sCondicionOpeP = " and FORMAT(o.dOperFech, 'dd/mm/yyyy') = '" & fecha & "'"
  
  'Captura el tipo de cambio
  sSQL = " SELECT nTipoCambMont FROM Tipo_Cambio" & _
         " WHERE FORMAT(dTipoCambFech, 'dd/mm/yyyy') = '" & fecha & "'"

  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo LineaError
  If ADORS.RecordCount <> 1 Then
    MsgBox "Para registrar ventas en dólares debe registrar el tipo de cambio"
    nTipoCambio = 0
  Else
    nTipoCambio = LeerCampo(ADORS("nTipoCambMont"))
  End If
  
  sSQL = " SELECT SUM(O.nProdCant)AS TotItem, sum ((O.nProdCant * O.nProdPrecUnit) + ((O.nProdCant * O.nProdPrecUnit) * O.nDscto/100))  AS TotMonto " & _
         "   FROM Operacion AS O" & _
         "  WHERE cOperTipo in (1,2,3)  " & sCondicionOpe & _
         "    AND cprodcode <> 0 AND cOperflaganula= 0 "
         
  If bParcial Then sSQL = sSQL + " AND cTurnoCodi = '" & kPAR_TURNO & "'"
     
  nTotal = 0
  nTotalItems = 0
  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo LineaError
  
  Do While Not ADORS.EOF
    If IsNull(LeerCampo(ADORS("TotItem"))) Or LeerCampo(ADORS("TotItem")) = "" Then
      nTotalItems = nTotalItems + 0
    Else
      nTotalItems = nTotalItems + LeerCampo(ADORS("TotItem"))
    End If
    If IsNull(LeerCampo(ADORS("TotMonto"))) Or LeerCampo(ADORS("TotMonto")) = "" Then
      nTotal = nTotal + 0
    Else
      nTotal = nTotal + LeerCampo(ADORS("TotMonto"))
    End If
    ADORS.MoveNext
  Loop
  
  TotalVenta = nTotal
  If nTotalItems = 0 Then
    nTotalPrecUnit = 0
  Else
    nTotalPrecUnit = nTotal / nTotalItems
  End If
  
  'Captura Cabecera
  Call CabeceraImpresion
  
  For i = 0 To UBound(sCabecera) - 1
    If Not Trim(sCabecera(i)) = "" Then
      Call AgregarLineaBloque(Right("                  " + sCabecera(i), Int(Len(sCabecera(i)) / 2) + 20))
    End If
  Next
  
  Call AgregarLineaBloque("")
  Call AgregarLineaBloque(fecha + " " + Format(CStr(Now()), "HH:MM") + " " + Format(CDate(Format(fecha, "dd/mm/yyyy")), "(DDD)") & " NS:" & kSERIE_DISCO_DURO)
  
  printer.FontSize = 8 ' 11
  printer.FontName = "Courier New"
  
  'PRIMER BLOQUE
  Call AgregarLineaBloque("=======================================")
  Call AgregarLineaBloque(sLetraReporte + "  CHECK                             01")
  Call AgregarLineaBloque("=======================================")
  Call AgregarLineaBloque("TOTAL     " + Right("                             *" + Format(CStr(nTotalItems), "###,###,##0.00"), 29))
  Call AgregarLineaBloque("          " + Right("                             *" + Format(CStr(nTotal), "###,###,##0.00"), 29))
  Call AgregarLineaBloque("=======================================")
  
  sSQL = " SELECT P.cProdCode as codigo, SUM(O.nProdCant) AS TotalItem,  max(P.SProdDescCrta) as Producto, max(P.cCodCatalogo) as Catalogo " & _
         " FROM Producto AS P " & _
         " LEFT outer JOIN Operacion AS O " & _
         "   ON P.cProdCode = O.cProdCode " & _
         " WHERE cOperflagAnula= 0 " & sCondicionOpe
   
  If bParcial Then sSQL = sSQL + " AND cTurnoCodi = '" & kPAR_TURNO & "'"
  sSQL = sSQL + " GROUP BY P.cProdCode Having  SUM(O.nProdCant) > 0 "
  Dim catal As String
  
  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo LineaError
  If ADORS.RecordCount > 0 Then
    Call AgregarLineaBloque("Producto                        Cant.  ")
    Call AgregarLineaBloque("---------------------------------------")
    
    Do While Not ADORS.EOF
      nItems = LeerCampo(ADORS("TotalItem"))
      catal = LeerCampo(ADORS("Catalogo"))
      sProdDescCrta = LeerCampo(ADORS("Producto"))
      nPorcTotal = (nSubtotal * 100) / IIf(nTotal = 0, 1, nTotal)
      
      Call AgregarLineaBloque(Left(catal + Space(10), 10) + " " + Left(sProdDescCrta + Space(17), 17) + " " + Right(Space(10) & Format(CStr(nItems), "###,###,##0.00"), 10))
      
      ADORS.MoveNext
    Loop
    
    Call AgregarLineaBloque("=======================================")
  End If
  
  '----------------------Vouchers---------------------------
  Dim iEfeVoucher As Double
  sSQL = " SELECT SUM((o.nProdCant * o.nProdPrecUnit) + ( (o.nProdCant * o.nProdPrecUnit) * o.nDscto/100)) AS TOTAL " & _
         " FROM Operacion o " & _
         " WHERE o.cOperTipo = 2  " & _
         " AND o.cOperFlagAnula = 0 " & sCondicionOpe
         
  If bParcial Then sSQL = sSQL + " AND cTurnoCodi = '" & kPAR_TURNO & "'"

  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo LineaError
  If Not ADORS.EOF Then
    iVoucMont = "0" & (ADORS!Total)
  Else
    iVoucMont = 0
  End If
  Set ADORS = Nothing
  '----------------------Efectivo en vouchers---------------------------
  sSQL = " SELECT SUM(o.nMontosoles) AS TOTAL" & _
         " FROM Operacion o " & _
         " WHERE o.cOperTipo = 1 AND TRIM(o.cEmisor) <> '' " & _
         " AND o.cDoctipo in ('01', '02') " & _
         " AND o.cOperFlagAnula = 0 " & sCondicionOpe

  If bParcial Then sSQL = sSQL + " AND cTurnoCodi = '" & kPAR_TURNO & "'"
          
  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo LineaError
  If Not ADORS.EOF Then
    iEfeVoucher = "0" & (ADORS!Total)
  Else
    iEfeVoucher = 0
  End If
  Set ADORS = Nothing
  iVoucMont = iVoucMont - iEfeVoucher
  '-------------------------voucher en liquidacion ------------------------
  sSQL = " SELECT SUM(o.nmontosoles +  o.nprodprecunit ) as TOTAL" & _
         "   FROM operacion o " & _
         "  WHERE o.cOperTipo = 2 and o.cDocTipo='14' " & _
         "    AND o.cOperFlagAnula = 0" & sCondicionOpe
         
  If bParcial Then sSQL = sSQL + " AND cTurnoCodi = '" & kPAR_TURNO & "'"
         
  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo LineaError
  iVoucMont = iVoucMont + val("0" & ADORS!Total)
  
  '--------contabilizando tickets---------------------------------------------------------
  sSQL = "SELECT distinct(o.cOpercode) " & _
         " FROM operacion o WHERE  (o.cdoctipo='02' or o.cdoctipo='01') " & _
         " AND o.cOperFlaganula=0 "
          
  If bParcial Then sSQL = sSQL + " AND cTurnoCodi = '" & kPAR_TURNO & "'"
   
  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo LineaError
  iCashNume = ADORS.RecordCount
   
  sSQL = "SELECT distinct(o.cOpercode) " & _
         " FROM operacion o WHERE  (o.cemisor <> ''  or o.cOperTipo=2) " & _
         " AND o.cOperFlaganula=0 " & sCondicionOpe
   
  If bParcial Then sSQL = sSQL + " AND cTurnoCodi = '" & kPAR_TURNO & "'"
  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo LineaError
   
  iVoucNume = ADORS.RecordCount
         
  'TERCER BLOQUE
  Call AgregarLineaBloque("=======================================")
  Call AgregarLineaBloque(sLetraReporte + "  CHECK  TOTAL TRANSACCIONES        02")
  Call AgregarLineaBloque("=======================================")
    
  If iCashNume <> 0 Then
    Call AgregarLineaBloque("TICKET      " + Right(Space(27) + "000000" + CStr(iCashNume) + "   ", 27))
    Call AgregarLineaBloque("            " + Right(Space(27) + "*" + Format(CStr(TotalVenta), "###,###,##0.00"), 27))
  End If
  
  Call AgregarLineaBloque("=======================================")
  If iVoucNume <> 0 Then
    Call AgregarLineaBloque("VOUCHER VTA " + Right(Space(27) + "000000" + CStr(iVoucNume) + "   ", 27))
    Call AgregarLineaBloque("            " + Right(Space(27) + "*" + Format(CStr(iVoucMont), "###,###,##0.00"), 27))
  End If
   
  'AQUI VA LO DEL PAGO Y TRANSACCIONES ANULADAS
  sSQL = " SELECT SUM((o.nProdPrecUnit * o.nProdCant) + ((o.nProdPrecUnit * o.nProdCant) * o.nDscto/100)) as TKMontNulos" & _
         "   FROM Operacion o " & _
         "  WHERE o.cOperflaganula= 1 " & _
         "    AND o.cProdcode <>  0 " & _
         "    AND o.cOpercodeanula <> 0 " & sCondicionOpe

  If bParcial Then sSQL = sSQL + " AND cTurnoCodi = '" & kPAR_TURNO & "'"
  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo LineaError
  If Not ADORS.EOF Then
    iVOPOMont = IIf(IsNull(ADORS!TKMontNulos), 0, ADORS!TKMontNulos)
  End If
  
  sSQL = " SELECT distinct o.cOperCode " & _
         "   FROM Operacion o " & _
         "  WHERE o.cOperflaganula= 1 " & _
         "    AND o.cProdcode <>  0 " & _
         "    AND o.cOpercodeanula <> 0 " & sCondicionOpe
        
  If bParcial Then sSQL = sSQL + " AND cTurnoCodi = '" & kPAR_TURNO & "'"
  
  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo LineaError
  If Not ADORS.EOF Then
    iVOPONume = IIf(IsNull(ADORS.RecordCount), 0, ADORS.RecordCount)
  End If
  
  '-----Recaudaciones
  sSQL = " SELECT SUM((o.nProdPrecUnit * o.nProdCant)  + ((o.nProdPrecUnit * o.nProdCant)  * o.nDscto/100)) as TKMontPago, COUNT(*) as TKPago " & _
         "   FROM Operacion o " & _
         "  WHERE o.cOperflaganula= 0 " & _
         "    AND o.cOpercodeanula = 0 " & _
         "    AND o.cOpertipo= 5 " & sCondicionOpe
    
  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo LineaError
  If Not ADORS.EOF Then
    iPagoMont = IIf(IsNull(ADORS!TKMontPago), 0, ADORS!TKMontPago)
    iPagoNume = IIf(IsNull(ADORS!TKPago), 0, ADORS!TKPago)
  End If
  
  '---------------Datos de Apertura de caja
  Dim iAperturasoles As Double
  Dim iAperturadolares As Double
  sSQL = " SELECT  nImporApertSol, nImporApertDol " & _
         " FROM Configuracion "

  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo LineaError
  iAperturasoles = (ADORS!nImporApertSol)
  iAperturadolares = (ADORS!nImporApertDol)
  
  'DEL CUADRE
  Dim iCashMontdol As Double
  iCashMont = 0
  iCashMontdol = 0
  
 'Total de Dolares obtenidos por ventas
  sSQL = " SELECT SUM(o.nMontoDolares-o.nVueltoDolares) AS TOTAL" & _
         " FROM Operacion o" & _
         " WHERE o.cOperTipo in (1, 3) " & _
         " AND o.cDoctipo in ('01','02') " & _
         " AND o.cOperFlaganula= 0 " & sCondicionOpe
         
  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo LineaError
  
  Do While Not ADORS.EOF
    If IsNull(LeerCampo(ADORS!Total)) Or (LeerCampo(ADORS!Total)) = "" Then
      iCashMontdol = 0
    Else
      iCashMontdol = iCashMontdol + LeerCampo(ADORS!Total)
    End If
    ADORS.MoveNext
  Loop
  
  'Calculo del Cash Soles
  sSQL = " SELECT SUM(o.nMontoSoles + o.nProdPrecUnit) AS TOTAL" & _
         " FROM Operacion o " & _
         " WHERE (o.cOperTipo in( 1, 3)) " & _
         " AND o.cDoctipo in ('01','02') " & _
         " AND o.cProdCode = 0 " & _
         " AND o.cOperFlaganula= 0 " & sCondicionOpe
         
  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo LineaError
  
  Do While Not ADORS.EOF
    If IsNull(LeerCampo(ADORS!Total)) Or (LeerCampo(ADORS!Total)) = "" Then
      iCashMont = 0
    Else
      iCashMont = iCashMont + LeerCampo(ADORS!Total)
    End If
    ADORS.MoveNext
  Loop
    
  Set ADORS = Nothing
  
  '------------------------------------------------
  If iPagoMont > (iAperturasoles + iCashMont) Then
    iPagoMont = iAperturasoles + iCashMont
  End If
  
  If iPagoMont > 0 Then 'Existe Pago
    Call AgregarLineaBloque("RECAUDACION " + Right(Space(27) + "000000" + CStr(iPagoNume) + "   ", 27))
    Call AgregarLineaBloque("            " + Right(Space(27) + "(" + Format(CStr(iPagoMont), "###,###,##0.00") + ")", 27))
  End If
  
 'QUINTO BLOQUE
  Call AgregarLineaBloque("=======================================")
  Call AgregarLineaBloque(sLetraReporte + "  CHECK  TOTALES                    03")
  Call AgregarLineaBloque("=======================================")
   
  '-------FIN DEL CUADRE---------------'
  If iAperturasoles > 0 Then 'Existe monto de apertura
    Call AgregarLineaBloque("APERT.    S/" + Right(Space(27) + "*" + Format(CStr(iAperturasoles), "###,###,##0.00"), 27))
  End If

  If iAperturadolares > 0 Then 'Existe monto de apertura dolares
    Call AgregarLineaBloque("APERT.     $" + Right(Space(27) + "*" + Format(CStr(iAperturadolares), "###,###,##0.00"), 27))
  End If
  
  If (iAperturasoles + iCashMont - iPagoMont) <> 0 Then   'Existe dinero Soles
    Call AgregarLineaBloque("---------------------------------------")
    Call AgregarLineaBloque("CASH-DRW S/." + Right(Space(27) + "*" + Format(CStr(iAperturasoles + iCashMont - iPagoMont), "###,###,##0.00"), 27))
  End If

    
  If (iAperturadolares + iCashMontdol) <> 0 Then 'Existe dinero Dolares
    Call AgregarLineaBloque("CASH-DRW$(TC S/." & Right(Space(5) & CStr(nTipoCambio), 5) & ")" & Right(Space(17) & "*" & Format(CStr(iAperturadolares + iCashMontdol), "###,###,##0.00"), 17))
  End If
  If iVoucNume <> 0 Then 'Existe VOUCHERS
    Call AgregarLineaBloque("VOUCHER     " + Right(Space(27) + "*" + Format(CStr(iVoucMont), "###,###,##0.00"), 27))
  End If
  
  If iVOPONume <> 0 Then 'Existe anulada
    Call AgregarLineaBloque("NULOS  TK   " + Right(Space(27) + "000000" + CStr(iVOPONume) + "   ", 27))
    Call AgregarLineaBloque("            " + Right(Space(27) + "*" + Format(CStr(iVOPOMont), "###,###,##0.00"), 27))
  End If

  Call AgregarLineaBloque("")
  
  Select Case sLetraReporte
    Case "Z" 'Cambiar a reporte Z
      'BLOQUE TICKETS EN GENERAL (B Y F)
      Call AgregarLineaBloque("=======================================")
      Call AgregarLineaBloque("TICKETS ")
      Call AgregarLineaBloque("=======================================")
      Call AgregarLineaBloque("      " + fecha + " " + Format(CDate(Format(fecha, "dd/mm/yyyy")), "(DDD)"))
      
      sSQL = " SELECT min(o.cdoccodigo) as mini,max(o.cdoccodigo) as maxi" & _
             " FROM Operacion o WHERE o.cOperTipo in (1,2,3,4,5) and o.cdoctipo IN ('01','02')" & sCondicionOpe
         
      If bParcial Then sSQL = sSQL + " AND cTurnoCodi = '" & kPAR_TURNO & "'"
      
      If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS1) Then GoTo LineaError
      
      Do While Not ADORS1.EOF
        Call AgregarLineaBloque("RANGO [ " + CStr(IIf(IsNull(ADORS1.Fields("mini")), "", ADORS1.Fields("mini"))) + "  ---  " + CStr(IIf(IsNull(ADORS1.Fields("maxi")), "", ADORS1.Fields("maxi"))) + " ]")
        
        ADORS1.MoveNext
      Loop
      Set ADORS1 = Nothing
    
      'CUARTO BLOQUE
      Call AgregarLineaBloque("=======================================")
      Call AgregarLineaBloque("=======================================")
      Call AgregarLineaBloque("TICKETS -- BOLETA TOTAL TRANSACC.")
      Call AgregarLineaBloque("=======================================")
      Call AgregarLineaBloque("      " + fecha + " " + Format(CDate(Format(fecha, "dd/mm/yyyy")), "(DDD)"))
  
      sSQL = "SELECT COUNT(*)AS TotDoc, sum ((O.nProdCant * O.nProdPrecUnit)+ ((O.nProdCant * O.nProdPrecUnit) * O.nDscto/100))  AS TotMonto " _
           & "FROM Operacion AS O " _
           & "WHERE cOperTipo in (1,2,3) " _
           & "AND cprodcode <> 0 AND cOperflaganula= 0 AND cdoctipo='02' " & sCondicionOpe

      If bParcial Then sSQL = sSQL + " AND cTurnoCodi = '" & kPAR_TURNO & "'"
      
      If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS1) Then GoTo LineaError
      
      Do While Not ADORS1.EOF
        Call AgregarLineaBloque("TICKET      " + Right(Space(27) + "000000" + CStr(ADORS1.Fields("TotDoc")) + "   ", 27))
        Call AgregarLineaBloque("            " + Right(Space(27) + "*" + Format(CStr(IIf(IsNull(ADORS1.Fields("TotMonto")), "0", ADORS1.Fields("TotMonto"))), "###,###,##0.00"), 27))
        
        ADORS1.MoveNext
      Loop
      Set ADORS1 = Nothing
    
      'QUINTO BLOQUE
      Call AgregarLineaBloque("=======================================")
      Call AgregarLineaBloque("=======================================")
      Call AgregarLineaBloque("TICKETS -- FACTURA TOTAL TRANSACC.")
      Call AgregarLineaBloque("=======================================")
      Call AgregarLineaBloque("      " + fecha + " " + Format(CDate(Format(fecha, "dd/mm/yyyy")), "(DDD)"))
     
      sSQL = "SELECT COUNT(*)AS TotDoc, sum ((O.nProdCant * O.nProdPrecUnit)+ ((O.nProdCant * O.nProdPrecUnit) * O.nDscto/100))  AS TotMonto " _
           & "FROM Operacion AS O " _
           & "WHERE cOperTipo in (1,2,3) " _
           & "AND cprodcode <> 0 AND cOperflaganula= 0 AND cdoctipo='01' " & sCondicionOpe
         
      If bParcial Then sSQL = sSQL + " AND cTurnoCodi = '" & kPAR_TURNO & "'"
      
      If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS1) Then GoTo LineaError
      
      Do While Not ADORS1.EOF
        Call AgregarLineaBloque("TICKET      " + Right(Space(27) + "000000" + CStr(ADORS1.Fields("TotDoc")) + "   ", 27))
        Call AgregarLineaBloque("            " + Right(Space(27) + "*" + Format(CStr(IIf(IsNull(ADORS1.Fields("TotMonto")), "0", ADORS1.Fields("TotMonto"))), "###,###,##0.00"), 27))
        
        ADORS1.MoveNext
      Loop
      Set ADORS1 = Nothing
    
      Call AgregarLineaBloque("=======================================")
  End Select
  
  ReportesConsolidadoFecha fecha, bParcial, 1
  
  Call AgregarLineaBloque("")
  Call AgregarLineaBloque(" " + Left(psUsuario_Descripcion, 10) + " " + Format(CStr(Now()), "hh:mm") + " ") 'Hora: 6 + 1
  
  '----------------------------------------------'
  Dim sCodigoRepCabZ As String
  If sLetraReporte = "Z" Then
    sSQL = "SELECT IIF(ISNULL(MAX(CodigoRep)),1, MAX(CodigoRep)) as CodigoRepX " & _
           "  FROM CabeceraZ " & _
           " WHERE FORMAT(Fecha, 'dd/mm/yyyy') = '" & fecha & "' "
           
    If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo LineaError
    If Not ADORS.EOF Then
       sCodigoRepCabZ = ADORS!CodigoRepX
    Else
       sCodigoRepCabZ = "000001"
    End If
   
    Call AgregarLineaBloque(sCodigoRepCabZ)
    Call AgregarLineaBloque("Total acumulado: *" + Format(CStr(nTotal), "###,###,##0.00"))
    
    Set ADORS = Nothing
  End If
  
  Call AgregarLineaBloque("CAJA: " + kPAR_CAJA + "   TURNO: " + kPAR_TURNO)
  Call AgregarLineaBloque("Versión " & App.Major & "." & App.Minor & "." & App.Revision)
  
  
  sSQL = "select max(copercode) as CodigoUltimo from operacion" & _
        " where format(doperfech,'dd/mm/yyyy')='" & fecha & "' and copertipo=10"
  
  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo LineaError
  
  Call AgregarLineaBloque("Correlativo: " & ADORS!CodigoUltimo)
  
  
  ConfiguraImpresora kPAR_IMPRESORA
  
  Select Case KImpresora
    Case 1
      Call CortePapelGrande(sImprimir, 1, iNroBloques, "RptFecXOZ")
    Case 0
      For iNroFilas = 0 To iNroBloques
        printer.Print sImprimir(iNroFilas)
      Next
        
      printer.Print " "
      printer.Print " "
      printer.EndDoc
  End Select
  
  Screen.MousePointer = vbDefault
  
  GoTo Salir
  
LineaError:
  If intentos = 3 Then
    MsgBox "No fue posible imprimir reporte" + sLetraReporte, vbOKOnly + vbInformation, "Error"
    intentos = 0
  Else
    MsgBox Err.Description
    intentos = intentos + 1
    ReportesEspecialesFecha sLetraReporte, bParcial
  End If
  
Salir:
  Set ADORS = Nothing
  Set ADORS1 = Nothing
  Set cnSQL = Nothing
  Screen.MousePointer = vbDefault
End Sub

Sub ReportesEspeciales1Fecha(sLetraReporte As String, Optional bParcial As Boolean = False)
  On Error GoTo LineaError

  Dim a As String
  Dim efec As Double
  Dim sSQL As String
  Dim ADORS As ADODB.Recordset
  Dim ADORS1 As ADODB.Recordset
  Dim i As Long, j As Long
  Dim iInicio As Long, iFin As Long
  Dim sTecla1 As String, sTecla2 As String, sTecla3 As String
  Dim cnSQL As ADODB.Connection
  Dim nTipoCambio As Double
  Dim nTotal As Double
  Dim nTotalItems As Long
  Dim ntotalitemsoc As Long
  Dim nTotalPrecUnit As Double
  Dim nTotalTransac As Double
  Dim nprectransac As Double
  Dim nSubtotal As Double
  Dim nPorcTotal As Double
  Dim nItems As Long
  Dim nPrecUnit As Double
  Dim nPrecUnitOrig As Double
  Dim sProdDescCrta As String
  Dim sTeclDesc As String

  Dim sUsuario As String
  Dim iCodOper As Long
  Dim sFecha As String
  Dim iCodOperAutogenerado As Long
  
  'Montos por transaccion - Numero transac
  Dim iCashMont As Double, iCashNume As Long 'Transaccion Cash
  Dim iVoucMont As Double, iVoucNume As Long 'Transaccion Voucher
  Dim iDolaMont As Double, iDolaNume As Long 'Transaccion Dolares
  Dim iAnulMont As Double, iAnulNume As Long 'Transaccion Anulado
  Dim iPagoMont As Double, iPagoNume As Long 'Transaccion Pago
  Dim iVOPOMont As Double, iVOPONume As Long 'Transaccion VO-POSTN
  Dim TotalVenta As Double
  Dim TotalOC As Double
  Dim catal  As String
  
    '''variables para grabar en la tabla de Z
  Dim nCodCabZAutogenerado As Long
  Dim nCantTotalCabZ As Double
  Dim nMontoTotalCabZ As Double
  Dim dFechaCabZ As String
  Dim sRangoTKBolCabZ As String
  Dim sRangoTKFacCabZ As String
  Dim sCodigoRepCabZ As String
  Dim sCajaCabZ As String
  Dim ArregloZ() As DetalleZ
  
  Dim cOperId As String
  Dim cOperIdP As String
  Dim sCondicionOpe As String
  Dim sCondicionOpeP As String
  Dim fecha As String
  
  ConfiguraImpresora kPAR_IMPRESORA_ALTERNA
  
  fecha = InputBox("Ingrese una fecha", "Fecha de Reporte", "19/07/1979", 500, 700)
  If Not IsDate(fecha) Then
     MsgBox "La fecha debe tener el formato dd/mm/yyyy", vbCritical
     Exit Sub
  End If
  
  Screen.MousePointer = vbHourglass
  
  ReDim sImprimir(20)
  iNroFilas = 0
  iNroBloques = 0
  
  If Not oDBAdmin.AbrirBD(cnSQL, psConnectString) Then GoTo Salir 'ABRE SQL OMNISYS
  
  sCondicionOpe = " AND FORMAT(O.dOperFech, 'dd/mm/yyyy') = '" & fecha & "'"
  sCondicionOpeP = " AND FORMAT(O.dOperFech, 'dd/mm/yyyy') = '" & fecha & "'"
  
  'Captura el tipo de cambio
  sSQL = " SELECT nTipoCambMont FROM Tipo_Cambio" & _
         " WHERE FORMAT(dTipoCambFech, 'dd/mm/yyyy') = '" & fecha & "'"

  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo LineaError
  If ADORS.RecordCount <> 1 Then
    MsgBox "Para registrar ventas en dólares debe registrar el tipo de cambio"
    nTipoCambio = 0
  Else
    nTipoCambio = LeerCampo(ADORS("nTipoCambMont"))
  End If
    
  sSQL = " SELECT SUM(O.nProdCant)AS TotItem, sum ((O.nProdCant * O.nProdPrecUnit)+ ((O.nProdCant * O.nProdPrecUnit) * O.nDscto/100))  AS TotMonto " & _
         "   FROM Operacion AS O" & _
         "  WHERE cOperTipo in (1,2,3)  " & _
         "    AND cprodcode <> 0 AND cOperflaganula= 0 " & sCondicionOpe
         
  If bParcial Then sSQL = sSQL + " AND cTurnoCodi = '" & kPAR_TURNO & "'"
     
  nTotal = 0
  nTotalItems = 0
  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo LineaError
  Do While Not ADORS.EOF
    If IsNull(LeerCampo(ADORS("TotItem"))) Or LeerCampo(ADORS("TotItem")) = "" Then
      nTotalItems = nTotalItems + 0
    Else
      nTotalItems = nTotalItems + LeerCampo(ADORS("TotItem"))
    End If
    If IsNull(LeerCampo(ADORS("TotMonto"))) Or LeerCampo(ADORS("TotMonto")) = "" Then
      nTotal = nTotal + 0
    Else
      nTotal = nTotal + LeerCampo(ADORS("TotMonto"))
    End If
    ADORS.MoveNext
  Loop
  TotalVenta = nTotal
  If nTotalItems = 0 Then
    nTotalPrecUnit = 0
  Else
    nTotalPrecUnit = nTotal / nTotalItems
  End If
    
  sSQL = " Select sum(nProdCant) as items  , sum((nProdPrecUnit *  nProdCant) + ((nProdPrecUnit *  nProdCant) * nDscto/100) ) as precio " & _
         "    FROM OperacionParalelaProducto p , OperacionParalela o " & _
         "   WHERE p.cOpercode = o.cOpercode " & _
         "     AND o.cOpercodeAnul=0 " & _
         "     AND o.cEstado='1' AND o.cOperTipo in (20)  " & sCondicionOpeP
         
  If bParcial Then
    sSQL = sSQL + " AND o.cTurnoCodi = '" & kPAR_TURNO & "' "
  Else
    sSQL = sSQL + " "
  End If
    
  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo LineaError
  ntotalitemsoc = ("0" & (ADORS!Items))
  nTotalItems = nTotalItems + ("0" & (ADORS!Items))
  nTotal = nTotal + ("0" & (ADORS!precio))
  TotalOC = CDbl(("0" & (ADORS!precio)))
  
  'Guardando valores para grabar en tabla Z
  nCantTotalCabZ = nTotalItems
  nMontoTotalCabZ = nTotal
  dFechaCabZ = Now()
  sCajaCabZ = kPAR_CAJA
  
  'Captura Cabecera
  Call CabeceraImpresion
  
  For i = 0 To UBound(sCabecera) - 1
    If Not Trim(sCabecera(i)) = "" Then
        Call AgregarLineaBloque(Right("                  " + sCabecera(i), Int(Len(sCabecera(i)) / 2) + 20))
    End If
  Next
  
  Call AgregarLineaBloque("")
  Call AgregarLineaBloque(fecha + " " + Format(CStr(Now()), "HH:MM") + " " + Format(CDate(Format(fecha, "dd/mm/yyyy")), "(DDD)") & " NS:" & kSERIE_DISCO_DURO)
  
  printer.FontSize = 8 ' 11
  printer.FontName = "Courier New"
  
  'PRIMER BLOQUE
  Call AgregarLineaBloque("=======================================")
  Call AgregarLineaBloque(sLetraReporte + "1 CHECK                             01")
  Call AgregarLineaBloque("=======================================")
  Call AgregarLineaBloque("TOTAL     " + Right("                             *" + Format(CStr(nTotalItems), "###,###,##0.00"), 29))
  Call AgregarLineaBloque("          " + Right("                             *" + Format(CStr(nTotal), "###,###,##0.00"), 29))
  Call AgregarLineaBloque("=======================================")
  
  Dim sSql2 As String
  If bParcial Then sSql2 = " AND cTurnoCodi = '" & kPAR_TURNO & "' "
  
  sSQL = "SELECT cProdcode, sum(cantidad) as Cant ,  max(catalogo) as Catal, max(descripcion) as Descrip " & _
        " FROM  " & _
        "      ( SELECT 1, CProdCode,  sum(cant) as Cantidad , max(catal) as Catalogo, max(Descrip) as Descripcion " & _
        "          FROM    (SELECT  o.cProdCode,  sum(o.nProdCant) as cant ,  max(p.cCodcatalogo)  as catal, max( p.sProdDesc)  as Descrip  FROM Operacion o, Producto p " & _
        "                    WHERE o.cProdcode = p.cProdcode And o.cProdcode <> 0 And o.cOperFlaganula = 0 " & sCondicionOpe & _
        "                    GROUP BY o.cProdCode  ) " & _
        "       GROUP BY CPRODCODE  " & _
        "         UNION  " & _
        "       SELECT 2, d.cProdCode,  sum( d.nProdCant) as cantidad ,  max(p.cCodcatalogo)  as catalogo, max( p.sProdDesc)  as Descripcion " & _
        "         FROM  OperacionParalelaProducto  d, OperacionParalela o,   Producto  p WHERE d.cOpercode = o.cOpercode AND d.cProdCode = p.cProdCode AND d.cProdcode <> 0 AND o.cOperCodeanul = 0 AND cEstado= '1' AND o.cOpertipo in (20, 21) " & sCondicionOpeP & "  GROUP BY d.cProdCode   ) " & _
        " GROUP BY  cProdcode " & _
        " ORDER BY  3"
  
  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo LineaError
  Dim bHayDetalleZ As Boolean
  bHayDetalleZ = False
  
  If ADORS.RecordCount > 0 Then
    bHayDetalleZ = True
    Call AgregarLineaBloque("Producto                        Cant.  ")
    Call AgregarLineaBloque("---------------------------------------")
 
    ReDim ArregloZ(1 To ADORS.RecordCount)
    Dim ncontador As Integer
    ncontador = 1
    
    iNroFilas = iNroFilas + 2
    Do While Not ADORS.EOF
      nItems = LeerCampo(ADORS("Cant"))
      catal = LeerCampo(ADORS("Catal"))
      sProdDescCrta = LeerCampo(ADORS("Descrip"))
         
      Call AgregarLineaBloque(Left(catal + Space(10), 10) + " " + Left(sProdDescCrta + Space(17), 17) + " " + Right(Space(10) & Format(CStr(nItems), "###,###,##0.00"), 10))
      
      ArregloZ(ncontador).IdCab = nCodCabZAutogenerado
      ArregloZ(ncontador).CodProd = LeerCampo(ADORS("cProdCode"))
      ArregloZ(ncontador).DscProd = LeerCampo(ADORS("Descrip"))
      ArregloZ(ncontador).Cantidad = LeerCampo(ADORS("Cant"))
        
      ncontador = ncontador + 1
      
      ADORS.MoveNext
    Loop
    
    Call AgregarLineaBloque("=======================================")
  End If
  
  Dim descuento
  sSQL = " Select sum(p.nProdCant) as items  , sum((p.nProdPrecUnit * p.nProdCant)  + ((p.nProdPrecUnit * p.nProdCant) * nDscto/100) ) as precio " & _
         "    FROM OperacionParalelaProducto p , OperacionParalela o " & _
         "   WHERE p.cOpercode = o.cOpercode " & _
         "     AND o.cOpercodeAnul=0 " & sCondicionOpeP & _
         "     AND o.cEstado='1' AND cOperTipo in (21)  "

  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo LineaError
  
  nTotalItems = "0" & (ADORS!Items)
  nTotal = "0" & (ADORS!precio)
  If nTotalItems > 0 Then
    Call AgregarLineaBloque(sLetraReporte + "1 CHECK NC S/LIQUIDAR                ")
    Call AgregarLineaBloque("=======================================")
    Call AgregarLineaBloque("TOTAL     " + Right("                             *" + Format(CStr(nTotalItems), "###,###,##0.00"), 29))
    Call AgregarLineaBloque("          " + Right("                             *" + Format(CStr(nTotal), "###,###,##0.00"), 29))
    Call AgregarLineaBloque("=======================================")
    
    'Nota de Consumo
    sSQL = " SELECT d.cProdCode , sum(d.nProdCant) as Cantidad ,  max(p.cCodcatalogo) as Catalogo, max(p.sProdDesc) as Descripcion " & _
           " FROM OperacionParalelaProducto  d, OperacionParalela o,   Producto  p " & _
           " WHERE d.cOpercode = o.cOpercode " & _
           "   AND d.cProdCode = p.cProdCode " & _
           "   AND d.cProdcode <> 0 " & sCondicionOpeP & _
           "   AND o.cOperCodeanul =  0 AND cEstado= '1' AND o.cOpertipo in (21) " & _
           " GROUP BY d.cprodcode " & _
           " ORDER BY  4"
         
    If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo LineaError
    If ADORS.RecordCount > 0 Then
       Call AgregarLineaBloque("Producto                        Cant.  ")
       Call AgregarLineaBloque("---------------------------------------")
       
       Do While Not ADORS.EOF
          nItems = LeerCampo(ADORS("Cantidad"))
          catal = LeerCampo(ADORS("Catalogo"))
          sProdDescCrta = LeerCampo(ADORS("Descripcion"))
          
          Call AgregarLineaBloque(Left(catal + Space(10), 10) + " " + Left(sProdDescCrta + Space(17), 17) + " " + Right(Space(10) & Format(CStr(nItems), "###,###,##0.00"), 10))
          
          ADORS.MoveNext
       Loop
          
       Call AgregarLineaBloque("=======================================")
     End If
  End If
  
  'SEGUNDO BLOQUE: PADE
  Dim iTotPADE As Double
  Dim iTotPADES As Double
  Dim iTotPADED As Double
  Dim iTotPADEANU As Double
  Dim iTotPADEANUS As Double
  Dim iTotPADEANUD As Double
  Dim iItemPADEAnulado As Long
  Dim iItemPADE As Long
  Dim sDetallePA As String
  
  sSQL = " SELECT Sum(o.nMontosoles) as PADE, count(*) as items FROM OperacionParalela o " & _
         " WHERE o.cOperTipo in (22) and o.cEstado in ('1') " & _
         " AND o.cOpercodeAnul = 0 " & sCondicionOpeP
         
  If bParcial Then sSQL = sSQL + " AND cTurnoCodi = '" & kPAR_TURNO & "'"
  
  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo LineaError
  If Not ADORS.EOF Then
    iTotPADE = "0" & (ADORS!PADE)
    iItemPADE = "0" & (ADORS!Items)
  Else
    iTotPADE = 0
    iItemPADE = 0
  End If
  
  iTotPADEANU = 0
  iItemPADEAnulado = 0
    
  Call AgregarLineaBloque(sLetraReporte + "1 CHECK PADE                        02")
  Call AgregarLineaBloque("=======================================")
  Call AgregarLineaBloque("TOTAL     " + Right("                             *" + Format(CStr(iItemPADE), "###,###,##0.00"), 29))
  Call AgregarLineaBloque("          " + Right("                         S/. *" + Format(CStr(iTotPADE), "###,###,##0.00"), 29))
  Call AgregarLineaBloque("=======================================")
    
  sSQL = " SELECT o.cOperCode, o.cOperTipo, o.nTarjNumb, u.sUserName + ' ' + u.sUserApel as sGlosa, o.nMontosoles, o.nMontoDolares, o.nMoneda, o.cDocCodigo, o.cDocSerie FROM OperacionParalela o, usuario u " & _
         " WHERE o.cOperTipo in (22) and o.cEstado in ('1')  " & sCondicionOpeP & _
         " AND o.cOpercodeAnul = 0 AND o.cUserDesc = u.sUserDesc "
                 
  If bParcial Then sSQL = sSQL + " AND cTurnoCodi = '" & kPAR_TURNO & "'"
  
  If Not oDBAdmin.AbrirBD(ADOCnnSQL, psConnectString) Then GoTo LineaError
  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo LineaError
  
  If ADORS.RecordCount > 0 Then
    Call AgregarLineaBloque("Glosa              Tarjeta     Importe ")
    Call AgregarLineaBloque("---------------------------------------")
    
    iTotPADES = 0
    iTotPADED = 0
    iTotPADEANUS = 0
    iTotPADEANUD = 0
    
    Do While Not ADORS.EOF
      'Calculando PADE Anulados
      sSQL = "SELECT COUNT(1) AS EXISTE FROM PAGOS WHERE FechaAnulacion is not null and cSerieDocPago='" & LeerCampo(ADORS!cDocSerie) & "' AND convert(int,NroDocPago)=" & CStr(LeerCampo(ADORS!cDocCodigo))
      If oDBAdmin.EjecutarConsulta(ADOCnnSQL, sSQL, ADORS1) Then
         If LeerCampo(ADORS1("EXISTE")) > 0 Then
            iItemPADEAnulado = iItemPADEAnulado + 1
            iTotPADEANU = iTotPADEANU + LeerCampo(ADORS!nMontoSoles)
            If LeerCampo(ADORS!nMoneda) = 2 Then
              iTotPADEANUD = iTotPADEANUD + LeerCampo(ADORS!nMontoDolares)
              sDetallePA = sDetallePA + Left(Replace(LeerCampo(ADORS!sGlosa), vbCrLf, "") + Space(18), 18) + " " + Left(LeerCampo(ADORS!nTarjNumb) & Space(8), 8) + " US$" + Right(Space(8) & Format(CStr(ADORS!nMontoDolares), "###,###,##0.00"), 8) + vbCrLf
            Else
              iTotPADEANUS = iTotPADEANUS + LeerCampo(ADORS!nMontoSoles)
              sDetallePA = sDetallePA + Left(Replace(LeerCampo(ADORS!sGlosa), vbCrLf, "") + Space(18), 18) + " " + Left(LeerCampo(ADORS!nTarjNumb) & Space(8), 8) + " S/." + Right(Space(8) & Format(CStr(ADORS!nMontoSoles), "###,###,##0.00"), 8) + vbCrLf
            End If
         End If
      End If
      
      'Modificacion Dolares
      Dim mon As String
      If LeerCampo(ADORS!nMoneda) = 2 Then
        Call AgregarLineaBloque(Left(Replace(LeerCampo(ADORS!sGlosa), vbCrLf, "") + Space(18), 18) + " " + Left(LeerCampo(ADORS!nTarjNumb) & Space(8), 8) + " US$" + Right(Space(8) & Format(CStr(ADORS!nMontoDolares), "###,###,##0.00"), 8))
        iTotPADED = iTotPADED + LeerCampo(ADORS!nMontoDolares)
      Else
        Call AgregarLineaBloque(Left(Replace(LeerCampo(ADORS!sGlosa), vbCrLf, "") + Space(18), 18) + " " + Left(LeerCampo(ADORS!nTarjNumb) & Space(8), 8) + " S/." + Right(Space(8) & Format(CStr(ADORS!nMontoSoles), "###,###,##0.00"), 8))
        iTotPADES = iTotPADES + LeerCampo(ADORS!nMontoSoles)
      End If
      
      ADORS.MoveNext
    Loop
    
    Call AgregarLineaBloque("=======================================")
  End If
  
  'TERCER BLOQUE: PADE LIQUIDADOS EN EL DIA
  Dim PADLIQDIA As Double
  Dim PADLIQDIAS As Double
  Dim PADLIQDIAD As Double
  
  Dim iItemPADLIQDIA As Long
  sSQL = " SELECT sum(o.nMontosoles) as padliq, Count(*) as item  FROM OperacionParalela o " & _
         " WHERE o.cOperTipo in (22) and o.cEstado in ('2') " & sCondicionOpeP & _
         " AND o.cOpercodeAnul = 0 "
         
  If bParcial Then sSQL = sSQL + " AND cTurnoCodi = '" & kPAR_TURNO & "'"
  
  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo LineaError
  If Not ADORS.EOF Then
     PADLIQDIA = "0" & ADORS!PADLIQ
     iItemPADLIQDIA = "0" & ADORS!Item
  Else
    PADLIQDIA = 0
    iItemPADLIQDIA = 0
  End If
  
  Call AgregarLineaBloque(sLetraReporte + "1 CHECK PADE LIQ  Dia               03")
  Call AgregarLineaBloque("=======================================")
  Call AgregarLineaBloque("TOTAL     " + Right("                             *" + Format(CStr(iItemPADLIQDIA), "###,###,##0.00"), 29))
  Call AgregarLineaBloque("          " + Right("                         S/. *" + Format(CStr(PADLIQDIA), "###,###,##0.00"), 29))
  Call AgregarLineaBloque("=======================================")
    
  sSQL = " SELECT o.cOperCode, o.cOperTipo, o.nTarjNumb, o.sGlosa, o.nMontosoles, o.nMontoDolares, o.nMoneda FROM OperacionParalela o " & _
         " WHERE o.cOperTipo in (22) and o.cEstado in ('2') " & sCondicionOpeP & _
         " AND o.cOpercodeAnul = 0 "
         
  If bParcial Then sSQL = sSQL + " AND cTurnoCodi = '" & kPAR_TURNO & "'"
  
  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo LineaError
  
  If ADORS.RecordCount > 0 Then
    Call AgregarLineaBloque("Glosa              Tarjeta     Importe ")
    Call AgregarLineaBloque("---------------------------------------")
    
    PADLIQDIAS = 0
    PADLIQDIAD = 0
    Do While Not ADORS.EOF
      'Modificacion Dolares
      If LeerCampo(ADORS!nMoneda) = 2 Then
        Call AgregarLineaBloque(Left(Replace(LeerCampo(ADORS!sGlosa), vbCrLf, "") + Space(18), 18) + " " + Left(LeerCampo(ADORS!nTarjNumb) & Space(8), 8) + " US$" + Right(Space(8) & Format(CStr(ADORS!nMontoDolares), "###,###,##0.00"), 8))
        PADLIQDIAD = PADLIQDIAD + LeerCampo(ADORS!nMontoDolares)
      Else
        Call AgregarLineaBloque(Left(Replace(LeerCampo(ADORS!sGlosa), vbCrLf, "") + Space(18), 18) + " " + Left(LeerCampo(ADORS!nTarjNumb) & Space(8), 8) + " S/." + Right(Space(8) & Format(CStr(ADORS!nMontoSoles), "###,###,##0.00"), 8))
        PADLIQDIAS = PADLIQDIAS + LeerCampo(ADORS!nMontoSoles)
      End If
      
      ADORS.MoveNext
    Loop
    Call AgregarLineaBloque("=======================================")
  End If
  
  'CUARTO BLOQUE: PADES LIQUIDADOS y CERRADOS
  Dim PADLIQ As Double
  Dim PADLIQS As Double
  Dim PADLIQD As Double
  Dim iItemPADLIQ As Long
  sSQL = " SELECT sum(nMontosoles) as padliq, count(*) as item FROM OperacionParalela " & _
         " WHERE cOperTipo in (22) and cOperFlagAbierto=0 and cEstado in ('2') " & _
         " AND cOperCodeRela in (SELECT o.cOperCode from OperacionParalela o WHERE o.cOpercodeAnul = 0 " & sCondicionOpeP & ")" & _
         " AND cOpercodeAnul = 0 "
    
  If bParcial Then sSQL = sSQL + " AND cTurnoCodi = '" & kPAR_TURNO & "'"
  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo LineaError
  If Not ADORS.EOF Then
    PADLIQ = "0" & ADORS!PADLIQ
    iItemPADLIQ = "0" & ADORS!Item
  Else
    PADLIQ = 0
    iItemPADLIQ = 0
  End If
  
  Call AgregarLineaBloque(sLetraReporte + "1 CHECK PADE LIQ                    04")
  Call AgregarLineaBloque("=======================================")
  Call AgregarLineaBloque("TOTAL     " + Right("                             *" + Format(CStr(iItemPADLIQ), "###,###,##0.00"), 29))
  Call AgregarLineaBloque("          " + Right("                         S/. *" + Format(CStr(PADLIQ), "###,###,##0.00"), 29))
  Call AgregarLineaBloque("=======================================")
  
  sSQL = " SELECT cOperCode, cOperTipo, nTarjNumb, sGlosa, nMontosoles, nMoneda, nMontoDolares FROM OperacionParalela " & _
         " WHERE cOperTipo in (22) and cOperFlagAbierto=0 and cEstado in ('2') " & _
         " AND cOperCodeRela in (SELECT o.cOperCode from OperacionParalela o WHERE coperCodeanul= 0 " & sCondicionOpeP & ")" & _
         " AND cOpercodeAnul = 0 "
         
  If bParcial Then sSQL = sSQL + " AND cTurnoCodi = '" & kPAR_TURNO & "'"
  
  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo LineaError
  
  If ADORS.RecordCount > 0 Then
    Call AgregarLineaBloque("Glosa              Tarjeta     Importe ")
    Call AgregarLineaBloque("---------------------------------------")
    
    PADLIQS = 0
    PADLIQD = 0
    Do While Not ADORS.EOF
      'Modificacion Dolares
      If LeerCampo(ADORS!nMoneda) = 2 Then
        Call AgregarLineaBloque(Left(Replace(LeerCampo(ADORS!sGlosa), vbCrLf, "") + Space(18), 18) + " " + Left(LeerCampo(ADORS!nTarjNumb) & Space(8), 8) + " US$" + Right(Space(8) & Format(CStr(ADORS!nMontoDolares), "###,###,##0.00"), 8))
        PADLIQD = PADLIQD + LeerCampo(ADORS!nMontoDolares)
      Else
        Call AgregarLineaBloque(Left(Replace(LeerCampo(ADORS!sGlosa), vbCrLf, "") + Space(18), 18) + " " + Left(LeerCampo(ADORS!nTarjNumb) & Space(8), 8) + " S/." + Right(Space(8) & Format(CStr(ADORS!nMontoSoles), "###,###,##0.00"), 8))
        PADLIQS = PADLIQS + LeerCampo(ADORS!nMontoSoles)
      End If
      
      ADORS.MoveNext
    Loop
    Call AgregarLineaBloque("=======================================")
  End If
  
  'QUINTO BLOQUE: PADE ANULADOS
  Call AgregarLineaBloque(sLetraReporte + "1 CHECK PADE ANULADOS               05")
  Call AgregarLineaBloque("=======================================")
  Call AgregarLineaBloque("TOTAL     " + Right("                             *" + Format(CStr(iItemPADEAnulado), "###,###,##0.00"), 29))
  Call AgregarLineaBloque("          " + Right("                         S/. *" + Format(CStr(iTotPADEANU), "###,###,##0.00"), 29))
  Call AgregarLineaBloque("=======================================")
  
  If iItemPADEAnulado > 0 Then
     Call AgregarLineaBloque("Glosa              Tarjeta     Importe ")
     Call AgregarLineaBloque("---------------------------------------")
     Call AgregarLineaBloque(sDetallePA)
     Call AgregarLineaBloque("=======================================")
  End If
  
  'SEXTO BLOQUE: PADV
  Dim iTotPADV As Double
  Dim iTotPADVS As Double
  Dim iTotPADVD As Double
  Dim iItemPADV As Long
  Dim iTotPADVANU As Double
  Dim iTotPADVANUS As Double
  Dim iTotPADVANUD As Double
  Dim iItemPADVAnulado As Long
  Dim sDetallePV As String
  
  sSQL = " SELECT Sum(o.nMontosoles) as PADV, count(*) as items FROM OperacionParalela o " & _
         " WHERE o.cOperTipo in (23) and o.cEstado in ('1') " & sCondicionOpeP & _
         " AND o.cOpercodeAnul = 0 "
  
  If bParcial Then sSQL = sSQL + " AND cTurnoCodi = '" & kPAR_TURNO & "'"
  
  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo LineaError
  
  If Not ADORS.EOF Then
    iTotPADV = "0" & (ADORS!PADV)
    iItemPADV = "0" & (ADORS!Items)
  Else
    iTotPADV = 0
    iItemPADV = 0
  End If
  
  iTotPADVANU = 0
  iItemPADVAnulado = 0
  
  Call AgregarLineaBloque(sLetraReporte + "1 CHECK PADV                        06")
  Call AgregarLineaBloque("=======================================")
  Call AgregarLineaBloque("TOTAL     " + Right("                             *" + Format(CStr(iItemPADV), "###,###,##0.00"), 29))
  Call AgregarLineaBloque("          " + Right("                         S/. *" + Format(CStr(iTotPADV), "###,###,##0.00"), 29))
  Call AgregarLineaBloque("=======================================")
  
  sSQL = " SELECT o.cOperCode, o.cOperTipo, o.nTarjNumb, u.sUserName + ' ' + u.sUserApel as sGlosa, o.nMontosoles, o.nMontoDolares, o.nMoneda, o.cDocCodigo, o.cDocSerie FROM OperacionParalela o, usuario u " & _
         " WHERE o.cOperTipo in (23) and o.cEstado in ('1') " & sCondicionOpeP & _
         " AND o.cOpercodeAnul = 0 AND o.cUserDesc = u.sUserDesc "
         
  If bParcial Then sSQL = sSQL + " AND cTurnoCodi = '" & kPAR_TURNO & "'"
  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo LineaError
  If ADORS.RecordCount > 0 Then
    Call AgregarLineaBloque("Glosa              Tarjeta     Importe ")
    Call AgregarLineaBloque("---------------------------------------")
    
    iTotPADVS = 0
    iTotPADVD = 0
    iTotPADVANUS = 0
    iTotPADVANUD = 0
    
    Do While Not ADORS.EOF
      'Calculando PADV Anulados
      sSQL = "SELECT COUNT(1) AS EXISTE FROM PAGOS WHERE FechaAnulacion is not null and cSerieDocPago='" & LeerCampo(ADORS!cDocSerie) & "' AND convert(int,NroDocPago)=" & CStr(LeerCampo(ADORS!cDocCodigo))
      If oDBAdmin.EjecutarConsulta(ADOCnnSQL, sSQL, ADORS1) Then
         If LeerCampo(ADORS1("EXISTE")) > 0 Then
            iItemPADVAnulado = iItemPADEAnulado + 1
            iTotPADVANU = iTotPADVANU + LeerCampo(ADORS!nMontoSoles)
            If LeerCampo(ADORS!nMoneda) = 2 Then
              iTotPADVANUD = iTotPADEANUD + LeerCampo(ADORS!nMontoDolares)
              sDetallePV = sDetallePV + Left(Replace(LeerCampo(ADORS!sGlosa), vbCrLf, "") + Space(18), 18) + " " + Left(LeerCampo(ADORS!nTarjNumb) & Space(8), 8) + " US$" + Right(Space(8) & Format(CStr(ADORS!nMontoDolares), "###,###,##0.00"), 8) + vbCrLf
            Else
              iTotPADVANUS = iTotPADEANUS + LeerCampo(ADORS!nMontoSoles)
              sDetallePV = sDetallePV + Left(Replace(LeerCampo(ADORS!sGlosa), vbCrLf, "") + Space(18), 18) + " " + Left(LeerCampo(ADORS!nTarjNumb) & Space(8), 8) + " S/." + Right(Space(8) & Format(CStr(ADORS!nMontoSoles), "###,###,##0.00"), 8) + vbCrLf
            End If
         End If
      End If
      
      'Modificacion Dolares
      If LeerCampo(ADORS!nMoneda) = 2 Then
        Call AgregarLineaBloque(Left(Replace(LeerCampo(ADORS!sGlosa), vbCrLf, "") + Space(18), 18) + " " + Left(LeerCampo(ADORS!nTarjNumb) & Space(8), 8) + " US$" + Right(Space(8) & Format(CStr(ADORS!nMontoDolares), "###,###,##0.00"), 8))
        iTotPADVD = iTotPADVD + LeerCampo(ADORS!nMontoDolares)
      Else
        Call AgregarLineaBloque(Left(Replace(LeerCampo(ADORS!sGlosa), vbCrLf, "") + Space(18), 18) + " " + Left(LeerCampo(ADORS!nTarjNumb) & Space(8), 8) + " S/." + Right(Space(8) & Format(CStr(ADORS!nMontoSoles), "###,###,##0.00"), 8))
        iTotPADVS = iTotPADVS + LeerCampo(ADORS!nMontoSoles)
      End If
      
      ADORS.MoveNext
    Loop
    Call AgregarLineaBloque("=======================================")
 End If
  
  'SETIMO BLOQUE: PADVS LIQUIDADOS dia
  Dim PADVLIQDIA As Double
  Dim PADVLIQDIAS As Double
  Dim PADVLIQDIAD As Double
  Dim iItemPADVLIQDIA As Long
  sSQL = " SELECT sum(o.nMontosoles) as padliq, Count(*) as item FROM OperacionParalela o " & _
         " WHERE o.cOperTipo in (23) and o.cEstado in ('2') " & sCondicionOpeP & _
         " AND o.cOpercodeAnul = 0 "
              
  If bParcial Then sSQL = sSQL + " AND cTurnoCodi = '" & kPAR_TURNO & "'"
  
  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo LineaError
  If Not ADORS.EOF Then
     PADVLIQDIA = "0" & ADORS!PADLIQ
     iItemPADVLIQDIA = "0" & ADORS!Item
  Else
    PADVLIQDIA = 0
    iItemPADVLIQDIA = 0
  End If
  
  Call AgregarLineaBloque(sLetraReporte + "1 CHECK PADV LIQ Dia                07")
  Call AgregarLineaBloque("=======================================")
  Call AgregarLineaBloque("TOTAL     " + Right("                             *" + Format(CStr(iItemPADVLIQDIA), "###,###,##0.00"), 29))
  Call AgregarLineaBloque("          " + Right("                         S/. *" + Format(CStr(PADVLIQDIA), "###,###,##0.00"), 29))
  Call AgregarLineaBloque("=======================================")
  
  sSQL = " SELECT o.cOperCode, o.cOperTipo, o.nTarjNumb, o.sGlosa, o.nMontosoles, o.nMontoDolares, o.nMoneda FROM OperacionParalela o " & _
         " WHERE o.cOperTipo in (23) and o.cEstado in ('2') " & sCondicionOpeP & _
         " AND o.cOpercodeAnul = 0 "
         
  If bParcial Then sSQL = sSQL + " AND cTurnoCodi = '" & kPAR_TURNO & "'"
  
  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo LineaError
  If ADORS.RecordCount > 0 Then
     Call AgregarLineaBloque("Glosa              Tarjeta     Importe ")
     Call AgregarLineaBloque("---------------------------------------")
     
     PADVLIQDIAD = 0
     PADVLIQDIAS = 0
     Do While Not ADORS.EOF
       'Modificacion Dolares
       If LeerCampo(ADORS!nMoneda) = 2 Then
         Call AgregarLineaBloque(Left(Replace(LeerCampo(ADORS!sGlosa), vbCrLf, "") + Space(18), 18) + " " + Left(LeerCampo(ADORS!nTarjNumb) & Space(8), 8) + " US$" + Right(Space(8) & Format(CStr(ADORS!nMontoDolares), "###,###,##0.00"), 8))
         PADVLIQDIAD = PADVLIQDIAD + LeerCampo(ADORS!nMontoDolares)
       Else
         Call AgregarLineaBloque(Left(Replace(LeerCampo(ADORS!sGlosa), vbCrLf, "") + Space(18), 18) + " " + Left(LeerCampo(ADORS!nTarjNumb) & Space(8), 8) + " S/." + Right(Space(8) & Format(CStr(ADORS!nMontoSoles), "###,###,##0.00"), 8))
         PADVLIQDIAS = PADVLIQDIAS + LeerCampo(ADORS!nMontoSoles)
       End If
       
       ADORS.MoveNext
     Loop
     Call AgregarLineaBloque("=======================================")
   End If
 
  'OCTAVO BLOQUE: PADVS LIQUIDADOS
  Dim PADVLIQ As Double
  Dim PADVLIQS As Double
  Dim PADVLIQD As Double
  
  Dim iItemPADVLIQ As Long
  sSQL = " SELECT sum(nMontosoles) as padliq, count(*) as item FROM OperacionParalela " & _
         " WHERE cOperTipo in (23) and cOperFlagAbierto=0 and cEstado in ('2') " & _
         " AND cOperCodeRela in (SELECT o.cOperCode from OperacionParalela o WHERE cOpercodeAnul = 0 " & sCondicionOpeP & ") " & _
         " AND cOpercodeAnul = 0 "
             
  If bParcial Then sSQL = sSQL + " AND cTurnoCodi = '" & kPAR_TURNO & "'"
  
  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo LineaError
  If Not ADORS.EOF Then
     PADVLIQ = "0" & ADORS!PADLIQ
     iItemPADVLIQ = "0" & ADORS!Item
  Else
    PADVLIQ = 0
    iItemPADVLIQ = 0
  End If
  
  Call AgregarLineaBloque(sLetraReporte + "1 CHECK PADV LIQ                    08")
  Call AgregarLineaBloque("=======================================")
  Call AgregarLineaBloque("TOTAL     " + Right("                             *" + Format(CStr(iItemPADVLIQ), "###,###,##0.00"), 29))
  Call AgregarLineaBloque("          " + Right("                         S/. *" + Format(CStr(PADVLIQ), "###,###,##0.00"), 29))
  Call AgregarLineaBloque("=======================================")
  
  sSQL = " SELECT cOperCode, cOperTipo, nTarjNumb, sGlosa, nMontosoles, nMontoDolares, nMoneda FROM OperacionParalela " & _
         " WHERE cOperTipo in (23) and cOperFlagAbierto=0 and cEstado in ('2') " & _
         "   AND cOperCodeRela in (SELECT o.cOperCode from OperacionParalela o WHERE cOpercodeAnul = 0 " & sCondicionOpeP & ") " & _
         "   AND cOpercodeAnul = 0 "
         
  If bParcial Then sSQL = sSQL + " AND cTurnoCodi = '" & kPAR_TURNO & "'"
  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo LineaError
  If ADORS.RecordCount > 0 Then
     Call AgregarLineaBloque("Glosa              Tarjeta     Importe ")
     Call AgregarLineaBloque("---------------------------------------")
     
     PADVLIQS = 0
     PADVLIQD = 0
     Do While Not ADORS.EOF
       'Modificacion Dolares
       If LeerCampo(ADORS!nMoneda) = 2 Then
         Call AgregarLineaBloque(Left(Replace(LeerCampo(ADORS!sGlosa), vbCrLf, "") + Space(18), 18) + " " + Left(LeerCampo(ADORS!nTarjNumb) & Space(8), 8) + " US$" + Right(Space(8) & Format(CStr(ADORS!nMontoDolares), "###,###,##0.00"), 8))
         PADVLIQD = PADVLIQD + LeerCampo(ADORS!nMontoDolares)
       Else
         Call AgregarLineaBloque(Left(Replace(LeerCampo(ADORS!sGlosa), vbCrLf, "") + Space(18), 18) + " " + Left(LeerCampo(ADORS!nTarjNumb) & Space(8), 8) + " S/." + Right(Space(8) & Format(CStr(ADORS!nMontoSoles), "###,###,##0.00"), 8))
         PADVLIQS = PADVLIQS + LeerCampo(ADORS!nMontoSoles)
       End If
       
       ADORS.MoveNext
    Loop
    Call AgregarLineaBloque("=======================================")
  End If
  
  'NOVENO BLOQUE: PADV ANULADOS
  Call AgregarLineaBloque(sLetraReporte + "1 CHECK PADV ANULADOS               09")
  Call AgregarLineaBloque("=======================================")
  Call AgregarLineaBloque("TOTAL     " + Right("                             *" + Format(CStr(iItemPADVAnulado), "###,###,##0.00"), 29))
  Call AgregarLineaBloque("          " + Right("                         S/. *" + Format(CStr(iTotPADVANU), "###,###,##0.00"), 29))
  Call AgregarLineaBloque("=======================================")
  
  If iItemPADVAnulado > 0 Then
     Call AgregarLineaBloque("Glosa              Tarjeta     Importe ")
     Call AgregarLineaBloque("---------------------------------------")
     Call AgregarLineaBloque(sDetallePV)
     Call AgregarLineaBloque("=======================================")
  End If
  
  '----------------------Vouchers---------------------------
  Dim iEfeVoucher As Double
  sSQL = " SELECT SUM((o.nProdPrecUnit * o.nProdCant) +  ((o.nProdPrecUnit * o.nProdCant) * o.nDscto/100)) AS TOTAL" & _
         " FROM Operacion o " & _
         " WHERE o.cOperTipo = 2" & _
         " AND o.cOperFlagAnula = 0 " & _
         " AND o.cOpercodeAnula=0 " & sCondicionOpe
         
  If bParcial Then sSQL = sSQL + " AND cTurnoCodi = '" & kPAR_TURNO & "'"

  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo LineaError
  If Not ADORS.EOF Then
    iVoucMont = "0" & (ADORS!Total)
  Else
    iVoucMont = 0
  End If
  Set ADORS = Nothing
  
  'DECIMO BLOQUE: Efectivo en vouchers
   sSQL = " SELECT SUM(o.nMontosoles) AS TOTAL" & _
           " FROM Operacion o" & _
           " WHERE o.cOperTipo = 1 AND TRIM(o.cEmisor) <> '' " & _
           " AND o.cOperFlagAnula = 0 " & sCondicionOpe

  If bParcial Then sSQL = sSQL + " AND cTurnoCodi = '" & kPAR_TURNO & "'"
          
  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo LineaError
  If Not ADORS.EOF Then
    iEfeVoucher = "0" & (ADORS!Total)
  Else
    iEfeVoucher = 0
  End If
  Set ADORS = Nothing
   iVoucMont = iVoucMont - iEfeVoucher
   '-------------------------Voucher en liquidacion ------------------------
  sSQL = " SELECT SUM(o.nmontosoles +  o.nprodprecunit ) as TOTAL" & _
         "   FROM operacion o" & _
         "  WHERE o.cOperTipo = 2 and o.cDocTipo='14' " & _
         "    AND o.cOperFlagAnula = 0" & sCondicionOpe
         
  If bParcial Then sSQL = sSQL + " AND cTurnoCodi = '" & kPAR_TURNO & "'"
         
  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo LineaError
  iVoucMont = iVoucMont + val("0" & ADORS!Total)
  
   '--------Contabilizando tickets---------------------------------------------------------
   sSQL = "SELECT distinct(o.cOpercode) " & _
          " FROM operacion o WHERE  (o.cdoctipo='02' or o.cdoctipo='01') " & _
          " AND o.cOperFlaganula=0 " & sCondicionOpe
          
   If bParcial Then sSQL = sSQL + " AND cTurnoCodi = '" & kPAR_TURNO & "'"
   
   If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo LineaError
   iCashNume = ADORS.RecordCount
   
   sSQL = "SELECT distinct(o.cOpercode) " & _
          " FROM operacion o WHERE  (o.cemisor <> ''  or o.cOperTipo=2) " & _
          " AND o.cOperFlaganula=0 " & sCondicionOpe
   
   If bParcial Then sSQL = sSQL + " AND cTurnoCodi = '" & kPAR_TURNO & "'"
   If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo LineaError
   
   iVoucNume = ADORS.RecordCount
  
  Call AgregarLineaBloque("=======================================")
  Call AgregarLineaBloque(sLetraReporte + "1 CHECK  TOTAL TRANSACCIONES       10")
  Call AgregarLineaBloque("=======================================")
  
  If iCashNume <> 0 Then
    Call AgregarLineaBloque("TICKET      " + Right(Space(27) + "000000" + CStr(iCashNume) + "   ", 27))
    Call AgregarLineaBloque("            " + Right(Space(27) + "*" + Format(CStr(TotalVenta), "###,###,##0.00"), 27))
  End If
  
  If iItemPADE <> 0 Then
    Call AgregarLineaBloque("PADE        " + Right(Space(27) + "000000" + CStr(iItemPADE) + "   ", 27))
    Call AgregarLineaBloque("            " + Right(Space(27) & "S/. *" + Format(CStr(iTotPADES - iTotPADEANUS), "###,###,##0.00"), 27))
    Call AgregarLineaBloque("            " + Right(Space(27) & "US$ *" + Format(CStr(iTotPADED - iTotPADEANUD), "###,###,##0.00"), 27))
  End If
  
  If PADLIQ <> 0 Then
    Call AgregarLineaBloque("PADE LIQ    " + Right(Space(27) & "000000" + CStr(iItemPADLIQ) + "   ", 27))
    Call AgregarLineaBloque("            " + Right(Space(27) & "S/. *(" + Format(CStr(PADLIQS), "###,###,##0.00)"), 27))
    Call AgregarLineaBloque("            " + Right(Space(27) & "US$ *(" + Format(CStr(PADLIQD), "###,###,##0.00)"), 27))
  End If
  
  If iItemPADV <> 0 Then
    Call AgregarLineaBloque("PADV        " + Right(Space(27) & "000000" + CStr(iItemPADV) + "   ", 27))
    Call AgregarLineaBloque("            " + Right(Space(27) & "S/. *" + Format(CStr(iTotPADVS - iTotPADVANUS), "###,###,##0.00"), 27))
    Call AgregarLineaBloque("            " + Right(Space(27) & "US$ *" + Format(CStr(iTotPADVD - iTotPADVANUD), "###,###,##0.00"), 27))
  End If
    
  If PADVLIQ <> 0 Then
    Call AgregarLineaBloque("PADV LIQ    " + Right(Space(27) & "000000" + CStr(iItemPADVLIQ) + "   ", 27))
    Call AgregarLineaBloque("            " + Right(Space(27) & "S/. *(" + Format(CStr(PADVLIQS), "###,###,##0.00)"), 27))
    Call AgregarLineaBloque("            " + Right(Space(27) & "US$ *(" + Format(CStr(PADVLIQD), "###,###,##0.00)"), 27))
  End If
  
  If ntotalitemsoc <> 0 Then '-Operaciones de ORDENES DE CONSUMO -
    Call AgregarLineaBloque("O/C         " + Right(Space(27) & "000000" + CStr(ntotalitemsoc) + "   ", 27))
    Call AgregarLineaBloque("            " + Right(Space(27) & "*" + Format(CStr(TotalOC), "###,###,##0.00"), 27) + vbCrLf)
  End If
   
  Call AgregarLineaBloque("---------------------------------------")
  If iVoucNume <> 0 Then
    Call AgregarLineaBloque("VOUCHER VTA " + Right(Space(27) & "000000" + CStr(iVoucNume) + "   ", 27))
    Call AgregarLineaBloque("            " + Right(Space(27) & "*" + Format(CStr(iVoucMont), "###,###,##0.00"), 27))
  End If
  
  If PADLIQDIA <> 0 Then
    Call AgregarLineaBloque("PADE LQ Día " + Right(Space(27) & "000000" + CStr(iItemPADLIQDIA) + "   ", 27))
    Call AgregarLineaBloque("            " + Right(Space(27) & "S/. *" + Format(CStr(PADLIQDIAS), "###,###,##0.00"), 27))
    Call AgregarLineaBloque("            " + Right(Space(27) & "US$ *" + Format(CStr(PADLIQDIAD), "###,###,##0.00"), 27))
  End If
  
  If PADVLIQDIA <> 0 Then
    Call AgregarLineaBloque("PADV LQ Día " + Right(Space(27) & "000000" + CStr(iItemPADVLIQDIA) + "   ", 27))
    Call AgregarLineaBloque("            " + Right(Space(27) & "S/. *" + Format(CStr(PADVLIQDIAS), "###,###,##0.00"), 27))
    Call AgregarLineaBloque("            " + Right(Space(27) & "US$ *" + Format(CStr(PADVLIQDIAD), "###,###,##0.00"), 27))
  End If
  
  'AQUI VA LO DEL PAGO Y TRANSACCIONES ANULADAS
  sSQL = " SELECT SUM((o.nProdPrecUnit * o.nProdCant)  + ((o.nProdPrecUnit * o.nProdCant) * o.nDscto/100)) as TKMontNulos" & _
        "   FROM Operacion o " & _
        "  WHERE o.cOperflaganula= 1 " & _
        "    AND o.cProdcode <>  0 " & _
        "    AND o.cOpercodeanula <> 0 " & sCondicionOpe

  If bParcial Then sSQL = sSQL + " AND cTurnoCodi = '" & kPAR_TURNO & "'"
  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo LineaError
  If Not ADORS.EOF Then
     iVOPOMont = IIf(IsNull(ADORS!TKMontNulos), 0, ADORS!TKMontNulos)
  End If
  
  sSQL = " SELECT distinct o.cOperCode " & _
        "   FROM Operacion o " & _
        "  WHERE o.cOperflaganula= 1 " & _
        "    AND o.cProdcode <>  0 " & _
        "    AND o.cOpercodeanula <> 0 " & sCondicionOpe
        
  If bParcial Then sSQL = sSQL + " AND cTurnoCodi = '" & kPAR_TURNO & "'"
  
  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo LineaError
  If Not ADORS.EOF Then
     iVOPONume = IIf(IsNull(ADORS.RecordCount), 0, ADORS.RecordCount)
  End If
  '-----Recaudaciones
  sSQL = " SELECT SUM((o.nProdPrecUnit * o.nProdCant)  + ((o.nProdPrecUnit * o.nProdCant) * o.nDscto/100)) as TKMontPago, COUNT(*) as TKPago " & _
        "   FROM Operacion o " & _
        "  WHERE o.cOperflaganula= 0 " & _
        "    AND o.cOpercodeanula = 0 " & _
        "    AND o.cOpertipo= 5 " & sCondicionOpe

    
  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo LineaError
  If Not ADORS.EOF Then
     iPagoMont = IIf(IsNull(ADORS!TKMontPago), 0, ADORS!TKMontPago)
     iPagoNume = IIf(IsNull(ADORS!TKPago), 0, ADORS!TKPago)
  End If
  
  '---------------Datos de Apertura de caja
  Dim iAperturasoles As Double
  Dim iAperturadolares As Double
  sSQL = " SELECT  nImporApertSol, nImporApertDol " & _
         " FROM Configuracion "

  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo LineaError
  iAperturasoles = (ADORS!nImporApertSol)
  iAperturadolares = (ADORS!nImporApertDol)
  
  '------- DEL CUADRE -----------------'
  Dim iCashMontdol As Double
  iCashMont = 0
  iCashMontdol = 0
  
 'Total de Dolares obtenidos por ventas
  sSQL = " SELECT SUM(o.nMontoDolares-o.nVueltoDolares) AS TOTAL" & _
         " FROM Operacion o " & _
         " WHERE o.cOperTipo in (1) " & _
         " AND o.cDoctipo in ('01','02', '14', '10') " & _
         " AND o.cOperFlaganula= 0 " & sCondicionOpe
         
  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo LineaError
  
  Do While Not ADORS.EOF
    If IsNull(LeerCampo(ADORS!Total)) Or (LeerCampo(ADORS!Total)) = "" Then
      iCashMontdol = 0
    Else
      iCashMontdol = iCashMontdol + LeerCampo(ADORS!Total)
    End If
    ADORS.MoveNext
  Loop
  
  'Calculo del Cash Soles
  sSQL = " SELECT SUM(o.nMontoSoles + o.nProdPrecUnit) AS TOTAL" & _
           " FROM Operacion o " & _
           " WHERE (o.cOperTipo in( 1)) " & _
           " AND o.cDoctipo in ('01','02','10', '14') " & _
           " AND o.cProdCode = 0 " & _
           " AND o.cOperFlaganula= 0 " & sCondicionOpe
  
  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo LineaError
  
  Do While Not ADORS.EOF
    If IsNull(LeerCampo(ADORS!Total)) Or (LeerCampo(ADORS!Total)) = "" Then
      iCashMont = 0
    Else
     iCashMont = iCashMont + LeerCampo(ADORS!Total)
    End If
    ADORS.MoveNext
  Loop
    
  Set ADORS = Nothing
  
  '------------------------------------------------
  If iPagoMont > (iAperturasoles + iCashMont) Then
      iPagoMont = iAperturasoles + iCashMont
  End If
  
  If iPagoMont > 0 Then 'Existe Pago
    Call AgregarLineaBloque("RECAUDACION " + Right(Space(29) + "000000" + CStr(iPagoNume) + "   ", 29) + "   ")
    Call AgregarLineaBloque("          " + Right(Space(28) + "(" + Format(CStr(iPagoMont), "###,###,##0.00") + ")", 29))
  End If
  
  'ONCEAVO BLOQUE
  Call AgregarLineaBloque("=======================================")
  Call AgregarLineaBloque(sLetraReporte + "1 CHECK  TOTALES                    11")
  Call AgregarLineaBloque("=======================================")
  
  '-------FIN DEL CUADRE---------------'
  If iAperturasoles > 0 Then 'Existe monto de apertura
    Call AgregarLineaBloque("APERT.  S/  " + Right(Space(27) + "*" + Format(CStr(iAperturasoles), "###,###,##0.00"), 27))
  End If
  
  If iAperturadolares > 0 Then 'Existe monto de apertura dolares
    Call AgregarLineaBloque("APERT. US$  " + Right(Space(27) + "*" + Format(CStr(iAperturadolares), "###,###,##0.00"), 27))
  End If

  If (iAperturasoles + iCashMont - iPagoMont + iTotPADES + PADLIQDIAS) <> 0 Then 'Existe dinero Soles
    Call AgregarLineaBloque("---------------------------------------")
    Call AgregarLineaBloque("CASH-DRW S/." + Right(Space(27) + "*" + Format(CStr(iAperturasoles + iCashMont - iPagoMont + iTotPADES - iTotPADEANUS + PADLIQDIAS), "###,###,##0.00"), 27))
  End If

  If (iAperturadolares + iCashMontdol + iTotPADED + PADLIQDIAD) <> 0 Then 'Existe dinero Dolares
    Call AgregarLineaBloque("CASH-DRW$(TC S/." & Right(Space(5) & CStr(nTipoCambio), 5) & ")" & Right(Space(17) & "*" & Format(CStr(iAperturadolares + iCashMontdol + iTotPADED - iTotPADEANUD + PADLIQDIAD), "###,###,##0.00"), 17))
  End If
  
  If iVoucNume <> 0 Then 'Existe Pago
    Call AgregarLineaBloque("VOUCHER     " + Right(Space(27) + "*" + Format(CStr(iVoucMont), "###,###,##0.00"), 27))
  End If

  If (iTotPADVS + PADVLIQDIAS) <> 0 Then  'Existe Pago Soles
    Call AgregarLineaBloque("PADEV S/.   " + Right(Space(27) + "*" + Format(CStr(iTotPADVS - iTotPADVANUS + PADVLIQDIAS), "###,###,##0.00"), 27))
  End If
  
  If (iTotPADVD + PADVLIQDIAD) <> 0 Then  'Existe Pago Dolares
    Call AgregarLineaBloque("PADEV US$   " + Right(Space(27) + "*" + Format(CStr(iTotPADVD - iTotPADVANUD + PADVLIQDIAD), "###,###,##0.00"), 27))
  End If
  
  'OC ANUULADAS
  Dim ddNuloOC As Double
  Dim dlItemNuloOc As Long
  sSQL = " SELECT sum(o.nmontoSoles) as nulooc, count(*) as item " & _
         "  FROM operacionparalela o WHERE o.copercodeanul <> 0 AND cdoctipo='10' " & sCondicionOpeP

  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo LineaError
  ddNuloOC = "0" & (ADORS!nulooc)
  dlItemNuloOc = "0" & (ADORS!Item)
  '-----------------------------------------------------------------------
  If dlItemNuloOc <> 0 Then 'Existe anulada oc
    Call AgregarLineaBloque("NULOS  OC   " + Right(Space(27) + "000000" + CStr(dlItemNuloOc) + "   ", 27) + "   ")
    Call AgregarLineaBloque("            " + Right(Space(27) + "*-" + Format(CStr(ddNuloOC), "###,###,##0.00"), 27))
  End If

  If iVOPONume <> 0 Then 'Existe anulada
    Call AgregarLineaBloque("NULOS  TK   " + Right(Space(27) + "000000" + CStr(iVOPONume) + "   ", 27) + "   ")
    Call AgregarLineaBloque("            " + Right(Space(27) + "*" + Format(CStr(iVOPOMont), "###,###,##0.00"), 27))
  End If
 
  Call AgregarLineaBloque("")
  Select Case sLetraReporte
    Case "Z" 'Cambiar a reporte Z
      'BLOQUE TICKETS EN GENERAL (B Y  F)
      Call AgregarLineaBloque("=======================================")
      Call AgregarLineaBloque("TICKETS ")
      Call AgregarLineaBloque("=======================================")
      Call AgregarLineaBloque("      " + fecha + " " + Format(CDate(Format(fecha, "dd/mm/yyyy")), "(DDD)"))
 
      sSQL = " SELECT min(o.cdoccodigo) as mini,max(o.cdoccodigo) as maxi" & _
             " FROM Operacion o WHERE o.cOperTipo in (1,2,3,4,5) and o.cdoctipo IN ('01','02')" & sCondicionOpe
         
      If bParcial Then sSQL = sSQL + " AND cTurnoCodi = '" & kPAR_TURNO & "'"
     
      If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS1) Then GoTo LineaError
      
      Do While Not ADORS1.EOF
        Call AgregarLineaBloque("RANGO [ " + CStr(IIf(IsNull(ADORS1.Fields("mini")), "", ADORS1.Fields("mini"))) + "  ---  " + CStr(IIf(IsNull(ADORS1.Fields("maxi")), "", ADORS1.Fields("maxi"))) + " ]")
        sRangoTKBolCabZ = "RANGO [ " + CStr(IIf(IsNull(ADORS1.Fields("mini")), "", ADORS1.Fields("mini"))) + "  ---  " + CStr(IIf(IsNull(ADORS1.Fields("maxi")), "", ADORS1.Fields("maxi"))) + " ]"
        
        ADORS1.MoveNext
     Loop
     Set ADORS1 = Nothing

     'CUARTO BLOQUE
     Call AgregarLineaBloque("=======================================")
     Call AgregarLineaBloque("=======================================")
     Call AgregarLineaBloque("TICKETS -- BOLETA TOTAL TRANSACC.")
     Call AgregarLineaBloque("=======================================")
     Call AgregarLineaBloque("      " + fecha + " " + Format(CDate(Format(fecha, "dd/mm/yyyy")), "(DDD)"))
     
     sSQL = " SELECT COUNT(*)AS TotDoc, sum ((O.nProdCant * O.nProdPrecUnit)+ ((O.nProdCant * O.nProdPrecUnit) * O.nDscto/100))  AS TotMonto " _
          & " FROM Operacion AS O " _
          & " WHERE cOperTipo in (1,2,3) " _
          & "   AND cprodcode <> 0 AND cOperflaganula= 0 AND cdoctipo='02' " & sCondicionOpe

    If bParcial Then sSQL = sSQL + " AND cTurnoCodi = '" & kPAR_TURNO & "'"
      
    If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS1) Then GoTo LineaError
      
    Do While Not ADORS1.EOF
        Call AgregarLineaBloque("TICKET      " + Right(Space(27) + "000000" + CStr(ADORS1.Fields("TotDoc")) + "   ", 27))
        Call AgregarLineaBloque("            " + Right(Space(27) + "*" + Format(CStr(IIf(IsNull(ADORS1.Fields("TotMonto")), "0", ADORS1.Fields("TotMonto"))), "###,###,##0.00"), 27))
        
        sRangoTKBolCabZ = "TICKET      " + Right(Space(27) + "000000" + CStr(ADORS1.Fields("TotDoc")) + "   ", 27) + vbCrLf
        sRangoTKBolCabZ = sRangoTKBolCabZ + "            " + Right(Space(27) + "*" + Format(CStr(IIf(IsNull(ADORS1.Fields("TotMonto")), "0", ADORS1.Fields("TotMonto"))), "###,###,##0.00"), 27) + vbCrLf
        
        ADORS1.MoveNext
    Loop
    Set ADORS1 = Nothing

    'QUINTO BLOQUE
    Call AgregarLineaBloque("=======================================")
    Call AgregarLineaBloque("=======================================")
    Call AgregarLineaBloque("TICKETS -- FACTURA TOTAL TRANSACC.")
    Call AgregarLineaBloque("=======================================")
    Call AgregarLineaBloque("      " + fecha + " " + Format(CDate(Format(fecha, "dd/mm/yyyy")), "(DDD)"))
    
    sSQL = "SELECT COUNT(*)AS TotDoc, sum ((O.nProdCant * O.nProdPrecUnit)+ ((O.nProdCant * O.nProdPrecUnit) * O.nDscto/100))  AS TotMonto " _
    & "FROM Operacion AS O " _
    & "WHERE cOperTipo in (1,2,3) " _
    & "AND cprodcode <> 0 AND cOperflaganula= 0 AND cdoctipo='01' " & sCondicionOpe
         
    If bParcial Then sSQL = sSQL + " AND cTurnoCodi = '" & kPAR_TURNO & "'"
      
    If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS1) Then GoTo LineaError
      
    Do While Not ADORS1.EOF
        Call AgregarLineaBloque("TICKET      " + Right(Space(27) + "000000" + CStr(ADORS1.Fields("TotDoc")) + "   ", 27))
        Call AgregarLineaBloque("            " + Right(Space(27) + "*" + Format(CStr(IIf(IsNull(ADORS1.Fields("TotMonto")), "0", ADORS1.Fields("TotMonto"))), "###,###,##0.00"), 27))
        
        sRangoTKBolCabZ = "TICKET      " + Right(Space(27) + "000000" + CStr(ADORS1.Fields("TotDoc")) + "   ", 27) + vbCrLf
        sRangoTKBolCabZ = sRangoTKBolCabZ + "            " + Right(Space(27) + "*" + Format(CStr(IIf(IsNull(ADORS1.Fields("TotMonto")), "0", ADORS1.Fields("TotMonto"))), "###,###,##0.00"), 27) + vbCrLf
        
        ADORS1.MoveNext
    Loop
    Set ADORS1 = Nothing
 
    '--------- NC X VENTA FUTURA
    Call AgregarLineaBloque("=======================================")
    Call AgregarLineaBloque("=======================================")
    Call AgregarLineaBloque("NOTAS DE CONSUMO X VENTA FUTURA ")
    Call AgregarLineaBloque("=======================================")
    Call AgregarLineaBloque("      " + fecha + " " + Format(CDate(Format(fecha, "dd/mm/yyyy")), "(DDD)"))
  
    sSQL = "SELECT p.cCodCatalogo as Catal, p.sProdDescCrta as Descrip, Sum(d.nProdCant) AS Cant " _
    & "FROM (OperacionParalela o INNER JOIN OperacionParalelaProducto d ON o.cOperCode = d.cOperCode) INNER JOIN Producto p ON d.cProdCode = p.cProdCode " _
    & "Where o.cdocTipo = '11' And o.cOperCodeAnul=0 " & sCondicionOpeP _
    & "GROUP BY p.cCodCatalogo, p.sProdDescCrta; "

    If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo LineaError
    If ADORS.RecordCount > 0 Then
      Call AgregarLineaBloque("Producto                        Cant.  ")
      Call AgregarLineaBloque("---------------------------------------")
      
      Do While Not ADORS.EOF
        nItems = LeerCampo(ADORS("Cant"))
        catal = LeerCampo(ADORS("Catal"))
        sProdDescCrta = LeerCampo(ADORS("Descrip"))
         
        Call AgregarLineaBloque(Left(catal + Space(10), 10) + " " + Left(sProdDescCrta + Space(17), 17) + " " + Right(Space(10) & Format(CStr(nItems), "###,###,##0.00"), 10))
      
        ADORS.MoveNext
      Loop
      Call AgregarLineaBloque("=======================================")
   End If
 End Select
 
 ReportesConsolidadoFecha fecha, True, 2
  
 Call AgregarLineaBloque("")
 Call AgregarLineaBloque(Left(psUsuario_Descripcion, 10) + " " + Format(CStr(Now()), "hh:mm") + " ") 'Hora: 6 + 1
 
 '----------------------------------------------'
 If sLetraReporte = "Z" Then
    sSQL = "SELECT IIF(ISNULL(MAX(CodigoRep)),1, MAX(CodigoRep)) as CodigoRepZ " & _
           "  FROM CabeceraZ " & _
           " WHERE FORMAT(Fecha, 'dd/mm/yyyy') = '" & fecha & "' "
           
    If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo LineaError
    If Not ADORS.EOF Then
       sCodigoRepCabZ = ADORS!CodigoRepZ
    Else
       sCodigoRepCabZ = "000001"
    End If
    
    Call AgregarLineaBloque(sCodigoRepCabZ)
    Call AgregarLineaBloque("Total acumulado: *" + Format(CStr(nTotal), "###,###,##0.00"))
    Set ADORS = Nothing
    '----------------------------------------------'
  End If
  
  Call AgregarLineaBloque("CAJA: " + kPAR_CAJA + "   TURNO: " + kPAR_TURNO)
  Call AgregarLineaBloque("Versión " & App.Major & "." & App.Minor & "." & App.Revision)
  
 ConfiguraImpresora kPAR_IMPRESORA_ALTERNA
 Select Case KImpresoraALT
    Case 1
        Call CortePapelGrande(sImprimir, 2, iNroBloques, "RptX1OZ1")
    Case 0
        For iNroFilas = 0 To iNroBloques
          printer.Print sImprimir(iNroFilas)
        Next
        
        printer.Print " "
        printer.Print " "
        printer.EndDoc
  End Select
  
  Screen.MousePointer = vbDefault
  
  GoTo Salir
  
LineaError:
  If intentos = 3 Then
     MsgBox "No fue posible imprimir reporte" + sLetraReporte, vbOKOnly + vbInformation, "Error"
     intentos = 0
  Else
    intentos = intentos + 1
    ReportesEspeciales1Fecha sLetraReporte, bParcial
  End If
  
Salir:
  Set ADORS = Nothing
  Set ADORS1 = Nothing
  Set cnSQL = Nothing
  Screen.MousePointer = vbDefault
End Sub

Sub SincronizarFecha()
  On Error GoTo LineaError

  Dim ADORS1 As ADODB.Recordset
  Dim smoneda, sMontoSoles, sEfectivoSoles, sVueltoSoles, sGlosa, sEstado, sOperCodeRela _
    , sOperCodeAnul, sDscto, sServicio, sProdCode, sProdPrecUnit, sTurnoCodi _
    , nTarjNumb, sUserDesc, sOperid, sOperCode, sTarjNumb, cDocCodigo _
    , cAlmaCode, sProdCant, sOperFech, sOperTipo, sOperFlagAnula, sOperCodeAnula _
    , sOperFlagAbierto, sMontoDolares, sCajacodi, sFlagSincronizacion, cDocSerie _
    , cdocTipo, cRuc, sIdLocal, sSupervisor As String
  Dim bValor As Boolean
  Dim iTipo As Integer
  Dim i As Integer
  Dim nitem As Long
  Dim ADORS As ADODB.Recordset
  Dim sSQL As String
  Dim cClieCode As String
  Dim sClieNombres As String
  Dim fecha
  
  If Not oDBAdmin.AbrirBD(ADOCnnSQL, psConnectString) Then GoTo Salir 'ABRE SQL OMNISYS

  fecha = InputBox("Ingrese una fecha", "Fecha de Sincronizacion", "19/07/1979", 500, 700)
  If Not IsDate(fecha) Then
     MsgBox "La fecha debe tener el formato dd/mm/yyyy", vbCritical
     Exit Sub
  End If
  
  Screen.MousePointer = vbHourglass
  
  sSQL = "dbo.spr_limpiar_caja_dia '" & kPAR_CAJA & "','" & fecha & "'"
  Call oDBAdmin.EjecutarSentencia(ADOCnnSQL, sSQL)
  
  'Operacion
  EscribirLog ("Sincronizando Operacion...")
  sSQL = " SELECT * FROM Operacion " & _
         " WHERE cProdCode <> 0 " & _
         " AND FORMAT(dOperFech, 'dd/mm/yyyy') = '" & fecha & "'" & _
         " UNION " & _
         " SELECT * FROM Operacion_Historico " & _
         " WHERE cProdCode <> 0 " & _
         " AND FORMAT(dOperFech, 'dd/mm/yyyy') = '" & fecha & "'"

  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo Salir
  
  Do While Not ADORS.EOF
    ADOCnnSQL.BeginTrans
    sCajacodi = kPAR_CAJA
    sOperid = LeerCampo(ADORS("cOperID"))
    sOperCode = LeerCampo(ADORS("cOperCode"))
    sUserDesc = LeerCampo(ADORS("cUserDesc"))
    sTarjNumb = LeerCampo(ADORS("nTarjNumb"))
    sIdLocal = LeerCampo(ADORS("idLocal"))
    Call RecuperaCliente(sTarjNumb, sIdLocal, cClieCode, sClieNombres, iTipo)
    cAlmaCode = LeerCampo(ADORS("cAlmaCode"))
    sProdCode = Right("000000" + LeerCampo(ADORS("cProdCode")), 6)
    sProdCant = LeerCampo(ADORS("nProdCant"))
    sProdPrecUnit = LeerCampo(ADORS("nProdPrecUnit"))
    sOperFech = Format(LeerCampo(ADORS("dOperFech")), "yyyy-mm-dd hh:nn:ss")
    sOperTipo = LeerCampo(ADORS("cOperTipo"))
    sOperFlagAnula = LeerCampo(ADORS("cOperFlagAnula"))
    sOperCodeAnula = LeerCampo(ADORS("cOperCodeAnula"))
    sOperFlagAbierto = LeerCampo(ADORS("cOperFlagAbierto"))
    sMontoDolares = LeerCampo(ADORS("nMontoDolares"))

    cDocCodigo = Right("000000" & CStr(val(ADORS("cDocCodigo"))), 6)
    cDocSerie = LeerCampo(ADORS("cDocSerie"))
    cdocTipo = LeerCampo(ADORS("cDocTipo"))
    cRuc = LeerCampo(ADORS("cRuc"))

    sFlagSincronizacion = LeerCampo(ADORS("cFlagSincronizacion"))
    sTurnoCodi = LeerCampo(ADORS("cTurnoCodi"))
    sDscto = LeerCampo(ADORS("nDscto"))
    sServicio = LeerCampo(ADORS("nServicio"))
    sSupervisor = LeerCampo(ADORS("cSupervisor"))
    
    If sServicio = "" Then sServicio = "0"
    
    'cDocSerie = Left(cDocSerie, 2) & "X" 'Solo para el caso de la caja 2. ESP: ESPECIAL
    
    Dim ADORS2 As ADODB.Recordset
    Dim nroReg As Integer
    
    Set ADORS2 = Nothing
          
    sSQL = "SELECT ISNULL(MAX(cOperId),0) + 1 cOperId " & _
           "FROM operacion WHERE cCajaCodi='" & kPAR_CAJA & "'"
                
    If Not oDBAdmin.EjecutarConsulta(ADOCnnSQL, sSQL, ADORS2) Then GoTo Salir
    sOperid = ADORS2!cOperId
    Set ADORS2 = Nothing
          
    sSQL = " INSERT INTO Operacion (cOperID, cOperCode, cUserDesc, nTarjNumb, cAlmaCode, cProdCode," & _
           " nProdCant, nProdPrecUnit, dOperFech, cOperTipo, cOperFlagAnula, cOperCodeAnula," & _
           " cOperFlagAbierto, nMontoDolares, cCajacodi, cFlagSincronizacion, cTurnoCodi, " & _
           " nDscto, nServicio, cCodiCLie, vDescClie, cDocCodigo, cdocserie, cdoctipo, cruc, iTipoCliente,idlocal) " & _
           " VALUES (" & sOperid & ", " & sOperCode & ", '" & sUserDesc & "', '" & sTarjNumb & "', '" & _
           cAlmaCode & "','" & sProdCode & "', " & sProdCant & ", " & sProdPrecUnit & ", CONVERT(DATETIME,'" & sOperFech & "',120), " & _
           sOperTipo & ", " & sOperFlagAnula & ", " & sOperCodeAnula & ", " & sOperFlagAbierto & ", " & _
           sMontoDolares & " , '" & sCajacodi & "', '1', '" & sTurnoCodi & "', " & sDscto & ", " & sServicio & ", '" & _
           cClieCode & "', '" & sClieNombres & "','" & cDocCodigo & "','" & cDocSerie & "','" & cdocTipo & "','" & cRuc & "'," & Str(iTipo) & ",'" & sIdLocal & "')"
          
    If oDBAdmin.EjecutarSentencia(ADOCnnSQL, sSQL, False) Then
      EscribirLog ("Operacion INSERTADA. sOperid:" & sOperid & ";sOperCode" & sOperCode & ";")
      ADOCnnSQL.CommitTrans
    Else
      EscribirLog ("Error. No se pudo INSERTAR Operacion. sOperid:" & sOperid & ";sOperCode" & sOperCode & ";sSQL:" & sSQL & ";")
      ADOCnnSQL.RollbackTrans
    End If
    
    ADORS.MoveNext
  Loop
  
  '(OPERACION PARALELA)
  frmUpdt.InsertaLinea ("Sincronizando Operaciones Paralelas")
  
  sSQL = " SELECT * FROM OperacionParalela " & _
         " WHERE cOperCodeAnul = 0 " & _
         " AND FORMAT(dOperFech, 'dd/mm/yyyy') = '" & fecha & "'" & _
         " AND NOT cOperCode in  (Select cOperCodeAnul from OperacionParalela ) " & _
         " UNION " & _
         " SELECT * FROM OperacionParalela_Historico " & _
         " WHERE cOperCodeAnul = 0 " & _
         " AND FORMAT(dOperFech, 'dd/mm/yyyy') = '" & fecha & "'" & _
         " AND NOT cOperCode in  (Select cOperCodeAnul from OperacionParalela_Historico ) "

  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo Salir
  
  Do While Not ADORS.EOF
    ADOCnnSQL.BeginTrans
    sOperCode = LeerCampo(ADORS("cOperCode"))
    sOperTipo = LeerCampo(ADORS("cOperTipo"))
    sOperFech = Format(LeerCampo(ADORS("dOperFech")), "yyyy-mm-dd hh:nn:ss")
    sTarjNumb = LeerCampo(ADORS("nTarjNumb"))
    
    Call RecuperaCliente(sTarjNumb, RetornarIdLocalToOPFromO(CStr(sOperCode), CStr(sOperFech)), _
                         cClieCode, sClieNombres, iTipo)
    
    smoneda = LeerCampo(ADORS("nMoneda"))
    sMontoSoles = LeerCampo(ADORS("nMontoSoles"))
    sMontoDolares = LeerCampo(ADORS("nMontoDolares"))
    sEfectivoSoles = LeerCampo(ADORS("nEfectivoSoles"))
    sVueltoSoles = LeerCampo(ADORS("nVueltoSoles"))
    sGlosa = LeerCampo(ADORS("sGlosa"))
    sEstado = LeerCampo(ADORS("cEstado"))
    sCajacodi = kPAR_CAJA
    sFlagSincronizacion = LeerCampo(ADORS("cFlagSincronizacion"))
    sTurnoCodi = LeerCampo(ADORS("cTurnoCodi"))
    sUserDesc = LeerCampo(ADORS("cUserDesc"))
    sOperCodeRela = LeerCampo(ADORS("cOperCodeRela"))
    sOperCodeAnul = LeerCampo(ADORS("cOperCodeAnul"))
    cDocCodigo = Right("000000" & CStr(val(ADORS("cDocCodigo"))), 6)
    cDocSerie = LeerCampo(ADORS("cDocSerie"))
    cdocTipo = LeerCampo(ADORS("cDocTipo"))
    
    'cDocSerie = Left(cDocSerie, 2) & "X" 'Solo para el caso de la caja 2. ESP: ESPECIAL
    
    If sOperCodeAnul = "" Then sOperCodeAnul = "0"
      sSQL = " INSERT INTO OperacionParalela (cCajaCodi, cOpercode , cOperTipo, dOperFech, nTarjNumb, nMoneda," & _
             " nMontoSoles, nMontoDolares, nEfectivoSoles, nVueltoSoles, sGlosa, cEstado, " & _
             " cFlagSincronizacion, cTurnoCodi, cUserDesc, cOperCodeRela, cOperCodeAnul, " & _
             " cCodiCLie, vDescClie, iTipocliente, cdoccodigo,cdocserie,cdoctipo)" & _
             " VALUES ('" & sCajacodi & "', " & sOperCode & ", " & sOperTipo & ", CONVERT(DATETIME,'" & sOperFech & "',120), '" & sTarjNumb & "', '" & _
             smoneda & "'," & sMontoSoles & ", " & sMontoDolares & ", " & sEfectivoSoles & ", " & sVueltoSoles & ", '" & _
             sGlosa & "', '" & sEstado & "', '1', '" & _
             sTurnoCodi & "' , '" & sUserDesc & "', " & sOperCodeRela & ", " & sOperCodeAnul & ", '" & _
             cClieCode & "', '" & sClieNombres & "'," & Str(iTipo) & ",'" & cDocCodigo & "','" & cDocSerie & "','" & cdocTipo & "')"
      
      If oDBAdmin.EjecutarSentencia(ADOCnnSQL, sSQL, False) = True Then
        sSQL = " SELECT * FROM OperacionParalelaProducto WHERE cOperCode = " & sOperCode & " and cProdCode <> 0 " & _
               " UNION " & _
               " SELECT * FROM OperacionParalelaProductoHist WHERE cOperCode = " & sOperCode & " and cProdCode <> 0 "
        Call oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS1)
        bValor = True
        i = 0
        Do While Not ADORS1.EOF
          i = i + 1
          cAlmaCode = LeerCampo(ADORS1("cAlmaCode"))
          sProdCode = Right("000000" + LeerCampo(ADORS1("cProdCode")), 6)
          sProdCant = LeerCampo(ADORS1("nProdCant"))
          sProdPrecUnit = LeerCampo(ADORS1("nProdPrecUnit"))
          sDscto = LeerCampo(ADORS1("nDscto"))
          nitem = "0" & ADORS1("nItem")
          sServicio = "0" & ADORS1("nServicio")
          sSupervisor = LeerCampo(ADORS1("cSupervisor"))
          If i = 1 Then
            sSQL = "DELETE FROM operacionparalelaproducto WHERE cOperCOde = " + sOperCode
            If Not oDBAdmin.EjecutarSentencia(ADOCnnSQL, sSQL) Then
              EscribirLog ("No se puedo DELETE OPP. cOperCOde:" & sOperCode & ";")
              ADOCnnSQL.RollbackTrans
              GoTo Salir
            End If
          End If
                  
          sSQL = " INSERT INTO OperacionParalelaProducto (cCajaCodi, cOpercode , cAlmaCode, cProdCode," & _
                 " nProdCant, nProdPrecUnit, nDscto, nServicio, nitem, cSupervisor)" & _
                 " VALUES ('" & sCajacodi & "', " & sOperCode & ", '" & cAlmaCode & "', '" & sProdCode & "', " & sProdCant & ", " & _
                 sProdPrecUnit & "," & sDscto & ", " & sServicio & "," & nitem & ",'" & sSupervisor & "' )"
                       
          bValor = bValor And oDBAdmin.EjecutarSentencia(ADOCnnSQL, sSQL, False)
          
          ADORS1.MoveNext
        Loop
        
        If bValor Then
          EscribirLog ("Se insertaron OPP con cOperCOde:" & sOperCode & ";")
          ADOCnnSQL.CommitTrans
        Else
          EscribirLog ("No se puedo insertar OPP. cOperCOde:" & sOperCode & ";")
          ADOCnnSQL.RollbackTrans
        End If
      Else
        EscribirLog ("No se pudo insertar OP con cOperCOde:" & sOperCode & ";")
        ADOCnnSQL.RollbackTrans
      End If
    ADORS.MoveNext
  Loop
  
  sSQL = "dbo.OSCMOYMOVIVNTA_REPROCESA '" & fecha & "'"
  Call oDBAdmin.EjecutarSentencia(ADOCnnSQL, sSQL)
  
  MsgBox "Proceso Terminado. Caja:" & kPAR_CAJA & ";Fecha:" & fecha & ";", vbOKOnly + vbInformation
  
  GoTo Salir
  
LineaError:
  MsgBox Err.Description, vbOKOnly + vbInformation
  
Salir:
  Set ADORS = Nothing
  Set ADORS1 = Nothing
  
  Screen.MousePointer = vbDefault
End Sub

Sub ReportesConsolidadoFecha(ByVal pFecha As String, Optional bParcial As Boolean = False, Optional Impres As Integer)
  On Error GoTo LineaError

  Screen.MousePointer = vbHourglass
  Dim a As String
  Dim sSQL As String
  Dim ADORS As ADODB.Recordset
  Dim ADORS1 As ADODB.Recordset
  Dim i As Long, j As Long
  Dim iInicio As Long, iFin As Long
  Dim sTecla1 As String, sTecla2 As String, sTecla3 As String
  Dim cnSQL As ADODB.Connection
  
  Dim nTipoCambio As Double
  Dim nTotal As Double
  Dim nTotal1 As Double
  Dim nTotalItems As Long
  Dim nTotalPrecUnit As Double
  Dim nTotalTransac As Double
  Dim nprectransac As Double
  Dim nSubtotal As Double
  Dim nSubtotal1 As Double
  Dim nPorcTotal As Double
  Dim p1 As Double
  Dim p2 As Double
  Dim nItems As Long
  Dim nPrecUnit As Double
  Dim nPrecUnitOrig As Double
  Dim sProdDescCrta As String
  Dim sTeclDesc As String
 ' Dim sTexto As String
  Dim sUsuario As String
  Dim iCodOper As Long
  Dim sFecha As String
  Dim iCambioDolares As Double
  Dim iCodOperAutogenerado As Long
  Dim iserv_b As Double
  Dim iserv_f As Double
  Dim igv_b As Double
  Dim igv_f As Double
  Dim porc As Double
  Dim igv_b_SQL As Double
  Dim igv_f_SQL As Double
  
  igv_b_SQL = 0
  igv_f_SQL = 0
  
  'Montos por transaccion - Numero transac
  Dim iCashMont As Double, iCashNume As Long 'Transaccion Cash
  Dim iVoucMont As Double, iVoucNume As Long 'Transaccion Voucher
  Dim iDolaMont As Double, iDolaNume As Long 'Transaccion Dolares
  Dim iAnulMont As Double, iAnulNume As Long 'Transaccion Anulado
  Dim iPagoMont As Double, iPagoNume As Long  'Transaccion Pago
  Dim iVOPOMont As Double, iVOPONume As Long 'Transaccion VO-POSTN
  Dim dDscto As Double
  Dim cOperId As String
  Dim cOperIdP As String
  Dim sCondicionOpe As String
  Dim sCondicionOpeP As String
  
  sCondicionOpe = " AND FORMAT(O.dOperFech, 'dd/mm/yyyy') = '" & pFecha & "'"
  sCondicionOpeP = " AND FORMAT(O.dOperFech, 'dd/mm/yyyy') = '" & pFecha & "'"
  
  'Calcula Totales SERVICIO FACTURA
  sSQL = " SELECT SUM(((o.nProdCant * o.nProdPrecUnit) + (o.nProdCant * o.nProdPrecUnit * o.nDscto/100)) / (1 + " & kIGV & " +  (o.nServicio /100) )  * (o.nServicio/100) ) as Servicio,  " & _
         "        SUM(((o.nProdCant * o.nProdPrecUnit) + (o.nProdCant * o.nProdPrecUnit * o.nDscto/100)) / (1 + " & kIGV & " + (o.nServicio / 100)) * " & kIGV & ") As Igv " & _
         "   FROM Operacion o " & _
         "   WHERE o.cDoctipo= '01' " & _
         "   AND o.cOperFlaganula= 0 " & _
         "   AND o.cProdcode <> 0 " & sCondicionOpe
         
  If bParcial Then sSQL = sSQL + " AND o.cTurnoCodi = '" & kPAR_TURNO & "'"
     
  igv_f = 0
  iserv_f = 0
  
  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo Salir
  If Not ADORS.EOF Then
    iserv_f = CDbl(IIf(IsNull(ADORS!servicio), 0, ADORS!servicio))
    igv_f = CDbl(IIf(IsNull(ADORS!Igv), 0, ADORS!Igv))
  End If

  Set ADORS = Nothing

 'Calcula Totales SERVICIO BOLETAS
  sSQL = " SELECT SUM(((o.nProdCant * o.nProdPrecUnit) + (o.nProdCant * o.nProdPrecUnit * o.nDscto/100)) / (1 + " & kIGV & " +  (o.nServicio /100) )  * (o.nServicio/100) ) as Servicio,  " & _
         "       SUM(((o.nProdCant * o.nProdPrecUnit) + (o.nProdCant * o.nProdPrecUnit * o.nDscto/100)) / (1 + " & kIGV & " + (o.nServicio / 100)) * " & kIGV & ") As Igv " & _
         "   FROM Operacion o " & _
         "   WHERE o.cDoctipo= '02' " & _
         "   AND o.cOperFlaganula= 0 " & _
         "   AND o.cProdcode <> 0 " & sCondicionOpe
         
  If bParcial Then sSQL = sSQL + " AND o.cTurnoCodi = '" & kPAR_TURNO & "'"
     
  igv_b = 0
  iserv_b = 0
  
  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo Salir
  If Not ADORS.EOF Then
    iserv_b = CDbl(IIf(IsNull(ADORS!servicio), 0, ADORS!servicio))
    igv_b = CDbl(IIf(IsNull(ADORS!Igv), 0, ADORS!Igv))
  End If

  Set ADORS = Nothing
   
  Dim sBolCaja, sFacCaja As String
  sBolCaja = "B" + Right(Trim(kPAR_CAJA), 2)
  sFacCaja = "F" + Right(Trim(kPAR_CAJA), 2)
   
  If Not oDBAdmin.AbrirBD(cnSQL, psConnectString) Then GoTo Salir 'ABRE SQL OMNISYS
   
  sSQL = "SELECT TipoDoc, sum(igv)  AS SumaIgv FROM VENTA01 WHERE cSerieDoc ='" & sBolCaja & "' or  cSerieDoc ='" & sFacCaja & "'  GROUP BY TipoDoc"
   
  If Not oDBAdmin.EjecutarConsulta(cnSQL, sSQL, ADORS1) Then GoTo LineaError
  Do While Not ADORS1.EOF
    If ADORS1!TipoDoc = "01" Then
      igv_f_SQL = ADORS1!SumaIgv
    ElseIf ADORS1!TipoDoc = "02" Then
      igv_b_SQL = ADORS1!SumaIgv
    End If
    ADORS1.MoveNext
  Loop
  Set ADORS1 = Nothing
  
  'PRIMER BLOQUE
  Call AgregarLineaBloque("=======================================")
  Call AgregarLineaBloque("        IGV FACTURA        ")
  Call AgregarLineaBloque("=======================================")
  Call AgregarLineaBloque(" TOTAL    " + Right(Space(28) + "*" + Format(CStr(igv_f + igv_f_SQL), "###,###,##0.00"), 29))
  
  'SEGUNDO BLOQUE
  Call AgregarLineaBloque("=======================================")
  Call AgregarLineaBloque("        IGV BOLETA         ")
  Call AgregarLineaBloque("=======================================")
  Call AgregarLineaBloque(" TOTAL    " + Right(Space(28) + "*" + Format(CStr(igv_b + igv_b_SQL), "###,###,##0.00"), 29))
  
  p2 = CDbl(Format(CStr(igv_b + igv_b_SQL), "###,###,##0.00"))
  
  'TERCER BLOQUE
  Call AgregarLineaBloque("=======================================")
  Call AgregarLineaBloque("        TOTAL IGV          ")
  Call AgregarLineaBloque("=======================================")
  Call AgregarLineaBloque(" TOTAL    " + Right(Space(28) + "*" + Format(CStr(igv_f + igv_f_SQL + igv_b + igv_b_SQL), "###,###,##0.00"), 29))
  
  Call AgregarLineaBloque("=======================================")
  Call AgregarLineaBloque("     SERVICIOS ( " + kPAR_PORCSERV + " % )       ")
  Call AgregarLineaBloque("=======================================")
  Call AgregarLineaBloque(" TOTAL    " + Right(Space(28) + "*" + Format(CStr(iserv_b + iserv_f), "###,###,##0.00"), 29))
  Call AgregarLineaBloque("=======================================")

  If Impres = 2 Then
    sSQL = "  SELECT SUM(((nProdCant * nProdPrecUnit) + (nProdCant * nProdPrecUnit * nDscto/100)) / (1 + (" & kIGV & " +  (nServicio /100) ) ) * (nServicio/100)) as Servicio  " & _
           "    FROM OperacionParalelaProducto p , OperacionParalela o " & _
           "   WHERE p.cOpercode = o.cOpercode " & _
           "     AND o.cOperTipo= 20 and cProdCode <> 0 " & _
           "     and  o.cOpercodeAnul=0 " & sCondicionOpeP
    
    If bParcial Then sSQL = sSQL + " AND cTurnoCodi = '" & kPAR_TURNO & "'"
    
    nItems = 0
    nSubtotal = 0
    
    If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo Salir
    If Not ADORS.EOF Then
      nSubtotal = CDbl(IIf(IsNull(ADORS!servicio), 0, ADORS!servicio))
    End If

    Set ADORS = Nothing
    
    'CUARTO BLOQUE
    Call AgregarLineaBloque("=======================================")
    Call AgregarLineaBloque("     SERV PARALELO( " + kPAR_PORCSERV + " % )       ")
    Call AgregarLineaBloque("=======================================")
    Call AgregarLineaBloque(" TOTAL    " + Right(Space(28) + "*" + Format(CStr(nSubtotal), "###,###,##0.00"), 29))
    Call AgregarLineaBloque("=======================================")
  End If

  GoTo Salir
  
LineaError:
  MsgBox Err.Description, vbOKOnly + vbInformation
  
Salir:
  Set ADORS = Nothing
  Set ADORS1 = Nothing
  Set cnSQL = Nothing
  Screen.MousePointer = vbDefault
End Sub

Sub CambioTurno()
  Dim sSQL As String
  
  Call mnuOperaciones_Hijo_Click(kMNU_REPORTE_X)
  kPAR_TURNO = Right("00" + CStr(val(kPAR_TURNO) + 1), 2)
  
  'ACTUALIZA LOS DATOS EN CONFIGURACION
  '-------------------------------------
  sSQL = "UPDATE Configuracion SET cCodiTurno = '" + kPAR_TURNO + "'"
  Call oDBAdmin.EjecutarSentencia(ADOCnn, sSQL)
  '--------------------------------------------
  
  boolSincronizarTodo = False
  boolSincronizarCierre = False
  
  Sincronizar
  
  boolSincronizarTodo = True
  
  Me.Hide
  Main

End Sub

Sub CapturaTipoCambio()
  On Error GoTo LineaError
  Dim sSQL As String
  Dim ADORS As ADODB.Recordset
  nTipoCambio = 0

'SE OBTIENE FECHA DEL SISTEMA DE LA BASE DE DATOS DE SQL - version 2.0.5
   
   Dim cnFECHASQL As ADODB.Connection
   Dim sqlFECHASIS As String
   Dim rsFEchaSis As ADODB.Recordset
   Dim sFecha As String
   
   If oDBAdmin.AbrirBD(cnFECHASQL, psConnectString, False) = True Then
     
     sqlFECHASIS = "select convert(varchar,getdate(),120) as fechasis"
     If oDBAdmin.EjecutarConsulta(cnFECHASQL, sqlFECHASIS, rsFEchaSis) = True Then
         
         If rsFEchaSis!fechasis <> "" Then
             sFecha = rsFEchaSis!fechasis
         End If

     Else
         'no pudo abrir el rs: que consulte al access
         sFecha = Now()
     End If

     Set cnFECHASQL = Nothing

   Else
     'no pudo conectarse al sql: que consulte al access
      sFecha = Now()
   End If
   
   'FIN FECHA DEL SISTEMA DE LA BASE DE DATOS DE SQL



  'Captura el tipo de cambio
  sSQL = " SELECT nTipoCambMont FROM Tipo_Cambio" & _
         " WHERE FORMAT(dTipoCambFech, 'yyyy/mm/dd') = '" & Format(CStr(sFecha), "yyyy/mm/dd") & "'"

  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo Salir
  If ADORS.RecordCount <> 1 Then
    MsgBox "Para registrar ventas en dólares debe registrar el tipo de cambio"
    frmTipoCambio.iEditar = kCMD_NUEVO
    frmTipoCambio.Show vbModal
    'Call CapturaTipoCambio
  Else
    nTipoCambio = LeerCampo(ADORS("nTipoCambMont"))
  End If
  GoTo Salir
  
LineaError:
  MsgBox Err.Description, vbOKOnly + vbInformation

Salir:
  Set ADORS = Nothing
  Screen.MousePointer = vbDefault
End Sub
 
 Public Sub ProcesoCaja(bApertura As Boolean)
   Dim i As Long
   
   For i = 1 To mnuOperaciones_Hijo.Count
     If mnuOperaciones_Hijo(i).Caption <> "-" Then mnuOperaciones_Hijo(i).Enabled = bApertura
   Next
      
   For i = 1 To mnuMantenimiento_Hijo.Count - 1
     If mnuMantenimiento_Hijo(i).Caption <> "-" Then mnuMantenimiento_Hijo(i).Enabled = bApertura
   Next
   
   mnuMantenimiento_Hijo(mnuMantenimiento_Hijo.Count).Enabled = Not bApertura
   
   'Toolbar1.Visible = bApertura
 End Sub

Sub ReporteParalelo(aTipoVenta As Integer)
  On Error GoTo LineaError

  Screen.MousePointer = vbHourglass
  Dim a As String
  Dim sSQL As String
  Dim ADORS As ADODB.Recordset
  Dim ADORS1 As ADODB.Recordset
  Dim i As Long, j As Long
  Dim iInicio As Long, iFin As Long
  Dim sTecla1 As String, sTecla2 As String, sTecla3 As String
    
  Dim nTipoCambio As Double
  Dim nTotal As Double
  Dim nTotalItems As Long
  Dim nTotalPrecUnit As Double
  Dim nTotalTransac As Double
  Dim nprectransac As Double
  Dim nSubtotal As Double
  Dim nPorcTotal As Double
  Dim nItems As Long
  Dim nPrecUnit As Double
  Dim nPrecUnitOrig As Double
  Dim sProdDescCrta As String
  Dim sTeclDesc As String
  Dim sTexto As String
  Dim sUsuario As String
  Dim iCodOper As Long
  Dim sFecha As String
  Dim iCambioDolares As Double
  Dim iCodOperAutogenerado As Long
  Dim iTipoReporte As Integer
  Dim sLetraReporte As String
  Dim cOperCode  As Long
  Dim nDscto As Double
  
  Select Case aTipoVenta
    Case 20
      iTipoReporte = 20
      sLetraReporte = "O/C"
    Case 21
      iTipoReporte = 21
      sLetraReporte = "N/C"
    Case 22
      iTipoReporte = 22
      sLetraReporte = "PADE"
    Case 23
      iTipoReporte = 23
      sLetraReporte = "PADV"
    Case 24
      iTipoReporte = 24
      sLetraReporte = "LIQPAD"
    End Select
  
  ConfiguraImpresora kPAR_IMPRESORA
  ConfiguraImpresora kPAR_IMPRESORA_ALTERNA
  
  'Montos por transaccion - Numero transac
  Dim iCashMont As Double, iCashNume As Long 'Transaccion Cash
  Dim iVoucMont As Double, iVoucNume As Long 'Transaccion Voucher
  Dim iDolaMont As Double, iDolaNume As Long 'Transaccion Dolares
  Dim iAnulMont As Double, iAnulNume As Long 'Transaccion Anulado
  Dim iPagoMont As Double, iPagoNume As Long 'Transaccion Pago
  Dim iVOPOMont As Double, iVOPONume As Long 'Transaccion VO-POSTN
  
  Dim cOperId As String
  Dim cOperIdP As String
  Dim sCondicionOpe As String
  Dim sCondicionOpeP As String
  
  sSQL = "SELECT cOperId, cOperIdP FROM Configuracion"
  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then
    cOperId = ""
    cOperIdP = ""
  Else
    cOperId = LeerCampo(ADORS("cOperId"))
    cOperIdP = LeerCampo(ADORS("cOperIdP"))
  End If
  
  If cOperId = "" Or cOperIdP = "" Then
    sCondicionOpe = "   and o.cOperflagabierto = 1 "
    sCondicionOpeP = "   and o.cOperflagabierto = 1 "
  Else
    sCondicionOpe = "   and o.cOperId > " + cOperId + " "
    sCondicionOpeP = "   and o.cOperCode > " + cOperIdP + " "
  End If
  
  sSQL = " SELECT count(*) AS TOTAL FROM OperacionParalela o " & _
         " WHERE o.cOperTipo = " & aTipoVenta & sCondicionOpeP
             
  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo Salir
  
  If LeerCampo(ADORS("total")) < 1 Then
    MsgBox "Debe registrar operaciones."
    GoTo Salir
  End If
  
  Set ADORS = Nothing
  
  'Captura la mayor transaccion de las permitidas
  sSQL = " SELECT MAX(cOperCode) AS maximo FROM OperacionParalela"
  'sSQL = " SELECT MAX(cOperCode) AS maximo FROM Operacion" & _
         " WHERE cOperFlagAbierto= 1 "
  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo Salir

  If ADORS.RecordCount = 1 Then
    If IsNull(ADORS("maximo")) Then
      iCodOper = 1
    Else
      iCodOper = LeerCampo(ADORS("maximo")) + 1
      'EscribirCfg "PARAMETROS", "cOpeB", psArchivoCfg, Str(iCodOper + 1)
    End If
  End If

   'SE OBTIENE FECHA DEL SISTEMA DE LA BASE DE DATOS DE SQL - version 2.0.5
   
   Dim cnFECHASQL As ADODB.Connection
   Dim sqlFECHASIS As String
   Dim rsFEchaSis As ADODB.Recordset
      
   If oDBAdmin.AbrirBD(cnFECHASQL, psConnectString, False) = True Then
     
     sqlFECHASIS = "select convert(varchar,getdate(),120) as fechasis"
     If oDBAdmin.EjecutarConsulta(cnFECHASQL, sqlFECHASIS, rsFEchaSis) = True Then
         
         If rsFEchaSis!fechasis <> "" Then
             sFecha = rsFEchaSis!fechasis
         End If

     Else
         'no pudo abrir el rs: que consulte al access
         'MsgBox "No se pudo obtener la fecha, hay problemas de conexión de la red con la Base de Datos Principal", vbOKOnly + vbExclamation, "Aviso"
         sFecha = Format(Now(), "DD/MM/YYYY hh:mm")
         'Unload Me
     End If

     Set cnFECHASQL = Nothing

   Else
     'no pudo conectarse al sql: que consulte al access
      'MsgBox "No se pudo obtener la fecha, hay problemas de conexión de la red con la Base de Datos Principal", vbOKOnly + vbExclamation, "Aviso"
      sFecha = Format(Now(), "DD/MM/YYYY hh:mm")
      'Unload Me
   End If
   
   'FIN FECHA DEL SISTEMA DE LA BASE DE DATOS DE SQL


  'sFecha = Format(Now(), "DD/MM/YYYY hh:mm")

  Set ADORS = Nothing
  
  If iCodOper = 1 Then
'    kOPERACION = LeerCfg("PARAMETROS", "cOpeB", psArchivoCfg)
'    iCodOper = Val(kOPERACION)
  End If
  
  'Inserta esta operacion
  sSQL = " INSERT INTO OPERACIONPARALELA (cOperCode, cOperTipo, dOperFech, sGlosa," & _
         " cEstado, cCajaCodi, cFlagSincronizacion, cTurnoCodi, cUserDesc)" & _
         " VALUES" + _
         "($, '$', '$', $, '$', '$', '$')"
  sSQL = VBsprintf(sSQL, CStr(iCodOper), 30, sFecha, sLetraReporte, 1, kPAR_CAJA, 0, kPAR_TURNO, psUsuario_Descripcion)
  If Not oDBAdmin.EjecutarSentencia(ADOCnn, sSQL) Then GoTo Salir
    
  'sSQL = "SELECT MAX(cOperID) as MAXIMO FROM OPERACION"
  'If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, adors) Then GoTo Salir

  'If adors.RecordCount = 1 Then
  '  If IsNull(adors("maximo")) Then
  '    iCodOperAutogenerado = 1
  '  Else
  '    iCodOperAutogenerado = LeerCampo(adors("maximo")) + 1
  '  End If
  'End If
  Set ADORS = Nothing
  
   
   
   
   
   
   
   
  
  'Captura el tipo de cambio
  sSQL = " SELECT nTipoCambMont FROM Tipo_Cambio" & _
         " WHERE FORMAT(dTipoCambFech, 'yyyy/mm/dd') = '" & Format(CStr(sFecha), "yyyy/mm/dd") & "'"

  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo Salir
  If ADORS.RecordCount <> 1 Then
    MsgBox "Para registrar ventas en dólares debe registrar el tipo de cambio"
    nTipoCambio = 0
  Else
    nTipoCambio = LeerCampo(ADORS("nTipoCambMont"))
  End If
  
  'Calcula Totales
  
  sSQL = " SELECT SUM(O.nProdCant) AS TotItem," & _
         " SUM(O.nProdCant * O.nProdPrecUnit * (100 - O.nDscto) /100) AS TotMonto" & _
         " FROM (ConfiguracionTeclado AS C RIGHT JOIN Producto as P ON C.cProdCode = P.cProdCode)" & _
         " INNER JOIN Operacion AS O ON P.cProdCode = O.cProdCode AND P.cAlmaCode = O.cAlmaCode" & _
         " WHERE cProdFlagCanje <> 1 " & _
         " AND cOperTipo = 10" & sCondicionOpe
     
  sSQL = " SELECT sum(b.nprodcant), sum()O.cOperCode, O.cUserDesc, O.nTarjNumb, O.dOperFech, O.cOperTipo," & _
         " O.cOperCodeRela, O.nVueltoSoles, O.nEfectivoSoles, B.cAlmaCode, B.cProdCode, B.nProdCant," & _
         " B.nProdPrecUnit, B.nDscto, B.sProdDescCrta" & _
         " FROM OperacionParalela AS O LEFT JOIN" & _
         " (SELECT A.cOperCode, A.cProdCode, A.nProdCant, A.nProdPrecUnit, A.nDscto," & _
         " P.sProdDescCrta FROM OperacionParalelaProducto as A INNER JOIN Producto AS P" & _
         " ON A.cProdCode = P.cProdCode AND A.cAlmaCode = P.cAlmaCode) AS B ON O.cOperCode = B.cOperCode" & _
         " WHERE O.cOperCode = $ AND O.cEstado IN ('1')"
  nTotal = 0
  nTotalItems = 0
  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo Salir
  Do While Not ADORS.EOF
    If IsNull(LeerCampo(ADORS("TotItem"))) Or LeerCampo(ADORS("TotItem")) = "" Then
      nTotalItems = nTotalItems + 0
    Else
      nTotalItems = nTotalItems + LeerCampo(ADORS("TotItem"))
    End If
    If IsNull(LeerCampo(ADORS("TotMonto"))) Or LeerCampo(ADORS("TotMonto")) = "" Then
      nTotal = nTotal + 0
    Else
      nTotal = nTotal + LeerCampo(ADORS("TotMonto"))
    End If
    ADORS.MoveNext
  Loop
  
  If nTotalItems = 0 Then
    nTotalPrecUnit = 0
  Else
    nTotalPrecUnit = nTotal / nTotalItems
  End If
    
  'Captura Cabecera
  Call CabeceraImpresion
  
  sTexto = ""
  For i = 0 To UBound(sCabecera) - 1
    sTexto = sTexto + Right("              " + sCabecera(i), Int(Len(sCabecera(i)) / 2) + 14) + vbCrLf
  Next
  sTexto = sTexto + vbCrLf
  sTexto = sTexto + "      " + Format(CStr(Now()), "DD/MM/YYYY (DDD)") + vbCrLf
  
  printer.FontSize = 8 ' 11
  printer.FontName = "Courier New"
  
  'PRIMER BLOQUE
  sTexto = sTexto + "===========================" + vbCrLf
  sTexto = sTexto + sLetraReporte + " CHECK                10" + vbCrLf
  sTexto = sTexto + "===========================" + vbCrLf
  sTexto = sTexto + "TOTAL     " + Right("                 *" + Format(CStr(nTotalItems), "###,###,##0.00"), 29) + vbCrLf
  sTexto = sTexto + "          " + Right("                 *" + Format(CStr(nTotal), "###,###,##0.00"), 29) + vbCrLf
  sTexto = sTexto + "AV./ITEM  " + Right("                 *" + Format(CStr(nTotalPrecUnit), "###,###,##0.00"), 29) + vbCrLf
  sTexto = sTexto + "===========================" + vbCrLf
    
  printer.Print sTexto
  
  sTexto = ""

  'Barre cada una de las teclas de producto
  sSQL = " SELECT O.cOperCode, O.cProdCode, O.nProdCant, O.nProdPrecUnit, O.nDscto, P.sProdDescCrta," & _
         " O.nProdCant * O.nProdPrecUnit * (100 - O.nDscto) / 100 As TotalMonto," & _
         " c.cTeclCode , c.sTeclDesc FROM Operacion O INNER JOIN" & _
         " (Producto P LEFT JOIN ConfiguracionTeclado AS C ON P.cProdCode = C.cProdCode)" & _
         " ON O.cProdCode = P.cProdCode AND P.cAlmaCode = O.cAlmaCode" & _
         " Where cOperTipo = 10 " & sCondicionOpe & " ORDER BY 1"
  
  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo Salir
  'sTexto = ""
  cOperCode = 0
  Do While Not ADORS.EOF
    If cOperCode = LeerCampo(ADORS("cOperCode")) Then
      nItems = LeerCampo(ADORS("nProdCant"))
      'nSubTotal = LeerCampo(adors("TotalMonto"))
      nPrecUnit = LeerCampo(ADORS("nProdPrecUnit"))
      sProdDescCrta = LeerCampo(ADORS("sProdDescCrta"))
      nDscto = LeerCampo(ADORS("nDscto"))
      sTeclDesc = IIf(LeerCampo(ADORS("sTeclDesc")) = "", "---", LeerCampo(ADORS("sTeclDesc")))
      
      sTexto = sTexto + Left(sTeclDesc + "          ", 10) + Right("                 @" + Format(CStr(nPrecUnit), "###,###,##0.00"), 29) + vbCrLf
      sTexto = sTexto + Left(sProdDescCrta + "          ", 10) + Right("                 *" + Format(CStr(nItems), "###,###,##0.00"), 29) + vbCrLf
      sTexto = sTexto + Left("%Dscto          ", 10) + Right("                 *" + Format(CStr(nDscto), "###,###,##0.00"), 29) + vbCrLf
      nSubtotal = nSubtotal + LeerCampo(ADORS("TotalMonto"))
    Else
      If cOperCode <> 0 Then
        sTexto = sTexto + "  AV./ITEM" + Right("                 *" + Format(CStr(nSubtotal), "###,###,##0.00"), 29) + vbCrLf
      End If
      cOperCode = LeerCampo(ADORS("cOperCode"))
      nSubtotal = 0
      nItems = LeerCampo(ADORS("nProdCant"))
      'nSubTotal = LeerCampo(adors("TotalMonto"))
      nPrecUnit = LeerCampo(ADORS("nProdPrecUnit"))
      sProdDescCrta = LeerCampo(ADORS("sProdDescCrta"))
      nDscto = LeerCampo(ADORS("nDscto"))
      sTeclDesc = IIf(LeerCampo(ADORS("sTeclDesc")) = "---", "", LeerCampo(ADORS("sTeclDesc")))
      nSubtotal = nSubtotal + LeerCampo(ADORS("TotalMonto"))
    
      sTexto = sTexto + "---------------------------" + vbCrLf
      sTexto = sTexto + "TX" + CStr(cOperCode) + vbCrLf
      sTexto = sTexto + Left(sTeclDesc + "          ", 10) + Right("                 @" + Format(CStr(nPrecUnit), "###,###,##0.00"), 29) + vbCrLf
      sTexto = sTexto + Left(sProdDescCrta + "          ", 10) + Right("                 *" + Format(CStr(nItems), "###,###,##0.00"), 29) + vbCrLf
      sTexto = sTexto + Left("%Dscto          ", 10) + Right("                 *" + Format(CStr(nDscto), "###,###,##0.00"), 29) + vbCrLf
    End If
    ADORS.MoveNext
  Loop
  sTexto = sTexto + "  AV./ITEM" + Right("                 *" + Format(CStr(nSubtotal), "###,###,##0.00"), 29) + vbCrLf
  printer.Print sTexto
  
  sTexto = ""
  
  'SEGUNDO BLOQUE
  sTexto = sTexto + "===========================" + vbCrLf
  sTexto = sTexto + sLetraReporte + " CHECK                20" + vbCrLf
  sTexto = sTexto + "===========================" + vbCrLf
  sTexto = sTexto + "TOTAL     " + Right("                 *" + Format(CStr(nTotalItems), "###,###,##0.00"), 29) + vbCrLf
  sTexto = sTexto + "          " + Right("                 *" + Format(CStr(nTotal), "###,###,##0.00"), 29) + vbCrLf
  sTexto = sTexto + "AV./ITEM  " + Right("                 *" + Format(CStr(nTotalPrecUnit), "###,###,##0.00"), 29) + vbCrLf

  'Captura el total de transacciones por tipo
  
    sSQL = " SELECT o.cOperTipo, o.cOperCode, SUM(o.nProdCant * o.nProdPrecUnit * (100 - o.nDscto) / 100) AS TotMonto" & _
           " FROM Operacion o " & _
           " WHERE ((o.cOperTipo = 3 AND o.cProdCode <> 0)" & _
           " OR (o.cOperTipo = 10 AND o.cOperFlagAnula = 1)" & _
           " OR (o.cOperTipo = 10 AND o.cOperFlagAnula = 0 AND o.nProdPrecUnit > 0))" & _
           " AND o.cOperTipo = 10" & sCondicionOpe
             
  sSQL = sSQL + " GROUP BY o.cOperTipo, o.cOperCode ORDER BY 1" 'SUM(nProdCant )

  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo Salir
  nTotalTransac = 0
  iCashMont = 0
  iCashNume = 0
  iVoucMont = 0
  iVoucNume = 0
  iDolaMont = 0
  iDolaNume = 0
  iPagoMont = 0
  iPagoNume = 0
  Do While Not ADORS.EOF
    'nItems = LeerCampo(ADOrs("TotItem"))
    nSubtotal = LeerCampo(ADORS("TotMonto"))
    Select Case LeerCampo(ADORS("cOperTipo"))
      Case 10
        iCashMont = iCashMont + LeerCampo(ADORS("TotMonto"))
        If LeerCampo(ADORS("TotMonto")) < 0 Then
          iCashNume = iCashNume - 1
        Else
          iCashNume = iCashNume + 1
        End If
        
      Case 2
        iVoucMont = iVoucMont + LeerCampo(ADORS("TotMonto"))
        If LeerCampo(ADORS("TotMonto")) < 0 Then
          iVoucNume = iVoucNume - 1
        Else
          iVoucNume = iVoucNume + 1
        End If
        
      Case 3
        iDolaMont = iDolaMont + (LeerCampo(ADORS("TotMonto")) / nTipoCambio)
        If LeerCampo(ADORS("TotMonto")) < 0 Then
          iDolaNume = iDolaNume - 1
        Else
          iDolaNume = iDolaNume + 1
        End If
        
      Case 5
        iPagoMont = iPagoMont + LeerCampo(ADORS("TotMonto"))
        iPagoNume = iPagoNume + 1
    End Select
    ADORS.MoveNext
  Loop
  
  'IF nTotalTransac = 0
  If iCashNume <> 0 Then
    sTexto = sTexto + "   CASH/TL" + Right("        " + "000000" + CStr(iCashNume) + "   ", 29) + vbCrLf
    sTexto = sTexto + "          " + Right("                 *" + Format(CStr(iCashMont), "###,###,##0.00"), 29) + vbCrLf
  End If
  
  If iVoucNume <> 0 Then
    sTexto = sTexto + "   VOUCHER" + Right("        " + "000000" + CStr(iVoucNume) + "   ", 29) + vbCrLf
    sTexto = sTexto + "          " + Right("                 *" + Format(CStr(iVoucMont), "###,###,##0.00"), 29) + vbCrLf
  End If
  
  If iDolaNume <> 0 Then
    sTexto = sTexto + "   EFECT.$" + Right("        " + "000000" + CStr(iDolaNume) + "   ", 29) + vbCrLf
    sTexto = sTexto + "          " + Right("                 *" + Format(CStr(iDolaMont), "###,###,##0.00"), 29) + vbCrLf
  End If
  
  nTotalTransac = iCashNume + iVoucNume + iDolaNume
  
  nprectransac = nTotal / nTotalTransac
  sTexto = sTexto + "  NET-TOTL" + Right("        " + "000000" + CStr(nTotalTransac) + "   ", 29) + vbCrLf
  sTexto = sTexto + "          " + Right("                 *" + Format(CStr(nTotal), "###,###,##0.00"), 29) + vbCrLf
  sTexto = sTexto + "  AV./TRAN" + Right("                 *" + Format(CStr(nprectransac), "###,###,##0.00"), 29) + vbCrLf + vbCrLf

  'FINALIZA EL REPORTE
  sTexto = sTexto + vbCrLf
  sTexto = sTexto + " " + Left(psUsuario_Descripcion, 10) + " " 'Usuario: 10 + 1
  sTexto = sTexto + Format(CStr(Now()), "hh:mm") + " " 'Hora: 6 + 1
  sTexto = sTexto + Right("000000" + CStr(iCodOper), 6) 'Codigo Transaccion: 6
  sTexto = sTexto + vbCrLf + "CAJA: " + kPAR_CAJA + "   TURNO: " + kPAR_TURNO
  'sTexto = sTexto + Right("000000" + CStr(iCodOperAutogenerado), 6) 'Codigo Transaccion: 6
    
  'txtTexto.Text = txtTexto.Text + sTexto
  
  printer.Print sTexto
  
  printer.Print " "
  printer.Print " "
  printer.EndDoc
  'Printer.KillDoc '----------------
  
  If MsgBox("¿Desea cerrar las operaciones de tipo '" & sLetraReporte & "'?", vbYesNo + vbDefaultButton1 + vbInformation) = vbYes Then
    'CIERRA LAS OPERACIONES DE TIPO O/C
    '-------------------------------------
    sSQL = "UPDATE Operacion SET cOperFlagAbierto = 0 WHERE cOperTipo = '" & iTipoReporte & "' and cOperFlagAbierto = 1"
    Call oDBAdmin.EjecutarSentencia(ADOCnn, sSQL)
    '--------------------------------------------
  End If
  
  bCerrar = True
  
  
  
  Screen.MousePointer = vbDefault
  
  GoTo Salir
  
LineaError:
  MsgBox Err.Description, vbOKOnly + vbInformation
  
  
Salir:
  Set ADORS = Nothing
  Set ADORS1 = Nothing
  Screen.MousePointer = vbDefault
  intentos = 0
End Sub

Private Function DevolverMontoTipoDocAccess_RV(ByVal sDocSerie As String, ByRef Cantidad As Integer, ByRef Monto As Double) As Boolean
Dim STRSQL As String
Dim ADORS As ADODB.Recordset
Dim bRpta As Boolean


STRSQL = "SELECT Operacion.cDocSerie, Count(Operacion.cDocSerie) AS Cantidad, Sum(nProdCant*nProdPrecUnit/" & 1 + kIGV & ") AS Monto "
STRSQL = STRSQL + "From Operacion WHERE Year(dOperFech)=Year(Now()) "
STRSQL = STRSQL + "AND Month(dOperFech)=Month(Now()) "
STRSQL = STRSQL + "AND Day(dOperFech)=Day(Now()) "
STRSQL = STRSQL + "AND Operacion.cProdCode<>0 "
STRSQL = STRSQL + "AND Operacion.cDocSerie = '" & sDocSerie & "' "
STRSQL = STRSQL + "AND Operacion.cOperFlagAnula=0 "
STRSQL = STRSQL + "GROUP BY Operacion.cDocSerie"

If Not oDBAdmin.EjecutarConsulta(ADOCnn, STRSQL, ADORS) Then GoTo Salir

Cantidad = ADORS!Cantidad
Monto = ADORS!Monto


DevolverMontoTipoDocAccess_RV = True
Exit Function

Salir:
DevolverMontoTipoDocAccess_RV = False
Cantidad = 0
Monto = 0

End Function
Private Sub CompararCorrelativoDocumentosSQL_RV()
Dim sSQL As String
Dim cnSQL As ADODB.Connection
Dim ADORS As ADODB.Recordset
Dim ADORS2 As ADODB.Recordset
Dim xcadTipDoc As String
Dim nCantDoc As Integer
Dim nMinDoc As Long
Dim nMaxDoc As Long
Dim sCaja As String
Dim cSerieDoc As String

sSQL = "SELECT cSerieDoc FROM Config_Documento WHERE cTipoDoc IN('01','02')"

If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo Salir

If Not oDBAdmin.AbrirBD(cnSQL, psConnectString) Then GoTo Salir 'OMNISYS

Do While Not ADORS.EOF
    
    sCaja = "Caja " & Right(Trim(ADORS!cSerieDoc), 2)
    cSerieDoc = ADORS!cSerieDoc
    
    sSQL = "select count(NroDoc)-1 as cantidad from (select distinct cDocCodigo as NroDoc from operacion where  cDocSerie ='" & cSerieDoc & "' and year(doperfech)=" & Year(Now) & " and month(doperfech)=" & Month(Now) & " and day(doperfech)=" & Day(Now) & " " _
    & "Union select NroDoc from venta01 where cSerieDoc='" & cSerieDoc & "' and year(fechaemision)=" & Year(Now) & " and month(fechaemision)=" & Month(Now) & " and day(fechaemision)=" & Day(Now) & ") o "
    If Not oDBAdmin.EjecutarConsulta(cnSQL, sSQL, ADORS2) Then GoTo Salir
    nCantDoc = ADORS2!Cantidad
    
    
    sSQL = "select cast(min(NroDoc)as int)as nromin from (select distinct cDocCodigo as NroDoc from operacion where  cDocSerie ='" & cSerieDoc & "' and year(doperfech)=" & Year(Now) & " and month(doperfech)=" & Month(Now) & " and day(doperfech)=" & Day(Now) & " " _
    & "Union select NroDoc from venta01 where cSerieDoc='" & cSerieDoc & "' and year(fechaemision)=" & Year(Now) & " and month(fechaemision)=" & Month(Now) & " and day(fechaemision)=" & Day(Now) & ") o "
    If Not oDBAdmin.EjecutarConsulta(cnSQL, sSQL, ADORS2) Then GoTo Salir
    nMinDoc = IIf(IsNull(ADORS2!nromin), 0, ADORS2!nromin)
    

    sSQL = "select cast(max(NroDoc)as int)as nromax from (select distinct cDocCodigo as NroDoc from operacion where  cDocSerie ='" & cSerieDoc & "' and year(doperfech)=" & Year(Now) & " and month(doperfech)=" & Month(Now) & " and day(doperfech)=" & Day(Now) & " " _
    & "Union select NroDoc from venta01 where cSerieDoc='" & cSerieDoc & "' and year(fechaemision)=" & Year(Now) & " and month(fechaemision)=" & Month(Now) & " and day(fechaemision)=" & Day(Now) & ") o "
    If Not oDBAdmin.EjecutarConsulta(cnSQL, sSQL, ADORS2) Then GoTo Salir
    nMaxDoc = IIf(IsNull(ADORS2!nromax), 0, ADORS2!nromax)
    
    If nMinDoc + nCantDoc <> nMaxDoc Then
                sSQL = "sp_send_cdosysmail 'jcure@omnisciencia.com','jcure@omnisciencia.com;jaybar@omnisciencia.com','Error Comparativo RegVta Correlativos SQL - Caja: " & sCaja & "','Error en correlativo SQL Tipo Doc: " & ADORS!cSerieDoc & "'"
                cnSQL.Execute (sSQL)
                
                sSQL = "INSERT INTO MsgErrorRegistroVentas VALUES ('" & Format(Now, "yyyyMMdd") & "','Error Comparativo RegVta Correlativos SQL - Caja: " & sCaja & "','Error en correlativo SQL Tipo Doc: " & ADORS!cSerieDoc & "')"
                cnSQL.Execute (sSQL)
                
                MsgBox "Error en correlativo SQL para el Tipo Doc: " & ADORS!cSerieDoc & " ...Informe al administrador", vbCritical, "Mensaje"

            
    End If
    
    ADORS.MoveNext
Loop



Set ADORS2 = Nothing
Set ADORS = Nothing
Set cnSQL = Nothing

Exit Sub
Salir:
Set ADORS2 = Nothing
Set ADORS = Nothing
Set cnSQL = Nothing

End Sub

Private Sub CompararSumaDeArticulosAccessSQL_RV()

Dim sSQL As String
Dim cnSQL As ADODB.Connection
Dim ADORS As ADODB.Recordset
Dim xcadTipDoc As String
Dim nTotalSumaProdAccess As Integer
Dim nTotalSumaProdSQL As Integer
Dim sCaja As String
sSQL = "SELECT cSerieDoc FROM Config_Documento WHERE cTipoDoc IN('01','02')"

If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo Salir

Do While Not ADORS.EOF
    sCaja = "Caja " & Right(Trim(ADORS!cSerieDoc), 2)
    If xcadTipDoc = "" Then
        xcadTipDoc = "('" & ADORS!cSerieDoc & "'"
    Else
        xcadTipDoc = xcadTipDoc & ",'" & ADORS!cSerieDoc & "')"
    End If
    ADORS.MoveNext
Loop


sSQL = "SELECT Sum(Operacion.nProdCant) AS SumaDeProdCant From Operacion " _
& "WHERE (((Year([dOperFech]))=Year(Now())) AND ((Month([dOperFech]))=Month(Now())) AND ((Day([dOperFech]))=Day(Now())) AND ((Operacion.cProdCode)<>0) AND ((Operacion.cDocSerie) In " & xcadTipDoc & ") AND ((Operacion.cOperFlagAnula)=0)) "

If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo Salir

nTotalSumaProdAccess = ADORS!SumaDeProdCant

If Not oDBAdmin.AbrirBD(cnSQL, psConnectString) Then GoTo Salir 'OMNISYS

sSQL = "SELECT SUM(NPRODCANT) as SumaDeProdCant FROM OPERACION WHERE YEAR(DOPERFECH)=" & Year(Now) & " AND MONTH(DOPERFECH)=" & Month(Now) & " AND DAY(DOPERFECH)=" & Day(Now) & " " _
& "AND COPERFLAGANULA=0 AND CDOCSERIE IN " & xcadTipDoc & " "


If Not oDBAdmin.EjecutarConsulta(cnSQL, sSQL, ADORS) Then GoTo Salir

nTotalSumaProdSQL = ADORS!SumaDeProdCant

If nTotalSumaProdAccess <> nTotalSumaProdSQL Then
                'enviar mensaje al usuario y mail a jaybar y jcure
                sSQL = "sp_send_cdosysmail 'jcure@omnisciencia.com','jcure@omnisciencia.com;jaybar@omnisciencia.com','Error Comparativo RegVta - Caja: " & sCaja & "','La suma de productos vendidos entre Access y SQL para el dia de hoy no coincide'"
                cnSQL.Execute (sSQL)
                
                sSQL = "INSERT INTO MsgErrorRegistroVentas VALUES ('" & Format(Now, "yyyyMMdd") & "','Error Comparativo RegVta - Caja: " & sCaja & "','La suma de productos vendidos entre Access y SQL para el dia de hoy no coincide')"
                cnSQL.Execute (sSQL)
                
                MsgBox "Error No cuadran la suma de productos vendidos en esta caja, entre Access y SQL...Informe al administrador!", vbCritical, "Mensaje"
    
End If


Set ADORS = Nothing
Set cnSQL = Nothing

Exit Sub
Salir:
Set ADORS = Nothing
Set cnSQL = Nothing

End Sub


Private Sub CompararCuentaDeProductosAccessSQL_RV()
Dim sSQL As String
Dim cnSQL As ADODB.Connection
Dim ADORS As ADODB.Recordset
Dim xcadTipDoc As String
Dim nTotalCantProdAccess As Integer
Dim nTotalCantProdSQL As Integer
Dim sCaja As String
sSQL = "SELECT cSerieDoc FROM Config_Documento WHERE cTipoDoc IN('01','02')"

If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo Salir

Do While Not ADORS.EOF
    sCaja = "Caja " & Right(Trim(ADORS!cSerieDoc), 2)
    If xcadTipDoc = "" Then
        xcadTipDoc = "('" & ADORS!cSerieDoc & "'"
    Else
        xcadTipDoc = xcadTipDoc & ",'" & ADORS!cSerieDoc & "')"
    End If
    ADORS.MoveNext
Loop


sSQL = "SELECT Count(Operacion.cProdCode) AS CuentaDeProdCode From Operacion " _
& "WHERE (((Year([dOperFech]))=Year(Now())) AND ((Month([dOperFech]))=Month(Now())) AND ((Day([dOperFech]))=Day(Now())) AND ((Operacion.cProdCode)<>0) AND ((Operacion.cDocSerie) In " & xcadTipDoc & ") AND ((Operacion.cOperFlagAnula)=0)) "

If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo Salir

nTotalCantProdAccess = ADORS!CuentaDeProdCode


If Not oDBAdmin.AbrirBD(cnSQL, psConnectString) Then GoTo Salir 'OMNISYS

sSQL = "SELECT count (CPRODCODE) as CuentaDeProdCode FROM OPERACION WHERE YEAR(DOPERFECH)=" & Year(Now) & " AND MONTH(DOPERFECH)=" & Month(Now) & " AND DAY(DOPERFECH)=" & Day(Now) & " " _
& "AND COPERFLAGANULA=0 AND CDOCSERIE IN " & xcadTipDoc & " "

If Not oDBAdmin.EjecutarConsulta(cnSQL, sSQL, ADORS) Then GoTo Salir

nTotalCantProdSQL = ADORS!CuentaDeProdCode

If nTotalCantProdAccess <> nTotalCantProdSQL Then
                'enviar mensaje al usuario y mail a jaybar y jcure
                sSQL = "sp_send_cdosysmail 'jcure@omnisciencia.com','jcure@omnisciencia.com;jaybar@omnisciencia.com','Error Comparativo RegVta - Caja: " & sCaja & "','La cantidad de productos vendidos entre Access y SQL para el dia de hoy no coincide'"
                cnSQL.Execute (sSQL)
                
                sSQL = "INSERT INTO MsgErrorRegistroVentas VALUES ('" & Format(Now, "yyyyMMdd") & "','Error Comparativo RegVta - Caja: " & sCaja & "','La cantidad de productos vendidos entre Access y SQL para el dia de hoy no coincide')"
                cnSQL.Execute (sSQL)
                
                MsgBox "Error No cuadran la cantidad de productos vendidos en esta caja, entre Access y SQL...Informe al administrador!", vbCritical, "Mensaje"
    
    
End If


Set ADORS = Nothing
Set cnSQL = Nothing

Exit Sub
Salir:
Set ADORS = Nothing
Set cnSQL = Nothing


End Sub
Private Sub CompararAccessSQL_RV()
Dim STRSQL As String
Dim sSQL As String
Dim cnSQL As ADODB.Connection
Dim ADORS As ADODB.Recordset
Dim xcadTipDoc As String
Dim nCantidadAccess As Integer
Dim nMontoAccess As Double
Dim bRespuestaAccess As Boolean
'''solicitado por jaybar 150311
'''Verificar cuadre de totales por Venta en Boletas y Facturas en ambas ubicaciones

sSQL = "SELECT cSerieDoc FROM Config_Documento WHERE cTipoDoc IN('01','02')"

If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo Salir

Do While Not ADORS.EOF
    If xcadTipDoc = "" Then
        xcadTipDoc = "('" & ADORS!cSerieDoc & "'"
    Else
        xcadTipDoc = xcadTipDoc & ",'" & ADORS!cSerieDoc & "')"
    End If
    ADORS.MoveNext
Loop

If Not oDBAdmin.AbrirBD(cnSQL, psConnectString) Then GoTo Salir 'OMNISYS

sSQL = "select cDocSerie as SerieDoc, count(cDocCodigo) as CantNroDoc,"
sSQL = sSQL + "ValVta = Sum(Round(Abs(nProdCant) * nProdPrecUnit / (1 + dbo.fnRetornarIGV()), 2)) "
sSQL = sSQL + "From operacion Where "
sSQL = sSQL + "cDocSerie in " & xcadTipDoc & " "
sSQL = sSQL + "and year(dOperFech)=" & Year(Now) & " and month(dOperFech)=" & Month(Now) & " and day(dOperFech)=" & Day(Now) & " "
sSQL = sSQL + "and   case cOperFlagAnula when 0 then 'N' when 1 then 'S' end='N' "
sSQL = sSQL + "group by cDocSerie "

If Not oDBAdmin.EjecutarConsulta(cnSQL, sSQL, ADORS) Then GoTo Salir

Do While Not ADORS.EOF
    
    bRespuestaAccess = DevolverMontoTipoDocAccess_RV(ADORS!SerieDoc, nCantidadAccess, nMontoAccess)
    If bRespuestaAccess = True Then
        If ADORS!CantNroDoc <> nCantidadAccess Or Round(ADORS!ValVta, 2) <> Round(nMontoAccess, 2) Then
                'enviar mensaje al usuario y mail a jaybar y jcure
                sSQL = "sp_send_cdosysmail 'jcure@omnisciencia.com','jcure@omnisciencia.com;jaybar@omnisciencia.com','Error Comparativo RegVta - Serie: " & ADORS!SerieDoc & "','Monto Access: " & Round(nMontoAccess, 2) & ", Monto SQL=" & Round(ADORS!ValVta, 2) & "'"
                cnSQL.Execute (sSQL)
                
                sSQL = "INSERT INTO MsgErrorRegistroVentas VALUES ('" & Format(Now, "yyyyMMdd") & "','Error Comparativo RegVta - Serie: " & ADORS!SerieDoc & "','Monto Access: " & Round(nMontoAccess, 2) & ", Monto SQL=" & Round(ADORS!ValVta, 2) & "')"
                cnSQL.Execute (sSQL)
                
                MsgBox "Error No cuadran la cantidad y/o totales emitidos en esta caja entre Access y SQL...Informe al administrador!", vbCritical, "Mensaje"
        End If
        
    ElseIf bRespuestaAccess = False Then
        sSQL = "sp_send_cdosysmail 'jcure@omnisciencia.com','jcure@omnisciencia.com;jaybar@omnisciencia.com','Error Comparativo RegVta - Serie: " & ADORS!SerieDoc & "','No hay informacion en Access para el dia de hoy'"
        cnSQL.Execute (sSQL)
        MsgBox "Error No cuadran la cantidad y/o totales emitidos en esta caja entre Access y SQL...Informe al administrador!", vbCritical, "Mensaje"
    End If
    
    ADORS.MoveNext
Loop


Set ADORS = Nothing
Set cnSQL = Nothing
Exit Sub
Salir:
Set ADORS = Nothing
Set cnSQL = Nothing
End Sub

Sub ReportesEspeciales1(iTipoReporte As Integer, sLetraReporte As String, Optional bParcial As Boolean = False, Optional bOC As Boolean = False)
   On Error GoTo LineaError

  Screen.MousePointer = vbHourglass
  Dim a As String
  Dim efec As Double
  Dim sSQL As String
  Dim ADORS As ADODB.Recordset
  Dim ADORS1 As ADODB.Recordset
  Dim i As Long, j As Long
  Dim iInicio As Long, iFin As Long
  Dim sTecla1 As String, sTecla2 As String, sTecla3 As String
  Dim cnSQL As ADODB.Connection
  Dim nTipoCambio As Double
  Dim nTotal As Double
  Dim nTotalItems As Long
  Dim ntotalitemsoc As Long
  Dim nTotalPrecUnit As Double
  Dim nTotalTransac As Double
  Dim nprectransac As Double
  Dim nSubtotal As Double
  Dim nPorcTotal As Double
  Dim nItems As Long
  Dim nPrecUnit As Double
  Dim nPrecUnitOrig As Double
  Dim sProdDescCrta As String
  Dim sTeclDesc As String

  Dim sUsuario As String
  Dim iCodOper As Long
  Dim sFecha As String
  Dim iCodOperAutogenerado As Long
  ConfiguraImpresora kPAR_IMPRESORA_ALTERNA
  
  'Montos por transaccion - Numero transac
  Dim iCashMont As Double, iCashNume As Long 'Transaccion Cash
  Dim iVoucMont As Double, iVoucNume As Long 'Transaccion Voucher
  Dim iDolaMont As Double, iDolaNume As Long 'Transaccion Dolares
  Dim iAnulMont As Double, iAnulNume As Long 'Transaccion Anulado
  Dim iPagoMont As Double, iPagoNume As Long 'Transaccion Pago
  Dim iVOPOMont As Double, iVOPONume As Long 'Transaccion VO-POSTN
  Dim TotalVenta As Double
  Dim TotalOC As Double
  Dim catal  As String
  
    '''variables para grabar en la tabla de Z
  Dim nCodCabZAutogenerado As Long
  Dim nCantTotalCabZ As Double
  Dim nMontoTotalCabZ As Double
  Dim dFechaCabZ As String
  Dim sRangoTKBolCabZ As String
  Dim sRangoTKFacCabZ As String
  Dim sCodigoRepCabZ As String
  Dim sCajaCabZ As String
  Dim ArregloZ() As DetalleZ
  
  Dim cOperId As String
  Dim cOperIdP As String
  Dim sCondicionOpe As String
  Dim sCondicionOpeP As String
  
  ReDim sImprimir(20)
  iNroFilas = 0
  iNroBloques = 0
  
  'Captura la mayor transaccion de las permitidas
  sSQL = " SELECT MAX(cOperCode) AS maximo FROM Operacion"
  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo LineaError

  If ADORS.RecordCount = 1 Then
    If IsNull(ADORS("maximo")) Then
      iCodOper = 1
    Else
      iCodOper = LeerCampo(ADORS("maximo")) + 1
    End If
  End If

  'SE OBTIENE FECHA DEL SISTEMA DE LA BASE DE DATOS DE SQL - version 2.0.5
   
   Dim cnFECHASQL As ADODB.Connection
   Dim sqlFECHASIS As String
   Dim rsFEchaSis As ADODB.Recordset
      
   If oDBAdmin.AbrirBD(cnFECHASQL, psConnectString, False) = True Then
     
     sqlFECHASIS = "select convert(varchar,getdate(),120) as fechasis"
     If oDBAdmin.EjecutarConsulta(cnFECHASQL, sqlFECHASIS, rsFEchaSis) = True Then
         
         If rsFEchaSis!fechasis <> "" Then
             sFecha = rsFEchaSis!fechasis
         End If

     Else
         'no pudo abrir el rs: que consulte al access
         'MsgBox "No se pudo obtener la fecha, hay problemas de conexión de la red con la Base de Datos Principal", vbOKOnly + vbExclamation, "Aviso"
         sFecha = Format(Now(), "DD/MM/YYYY hh:mm")
         'Unload Me
     End If

     Set cnFECHASQL = Nothing

   Else
     'no pudo conectarse al sql: que consulte al access
      'MsgBox "No se pudo obtener la fecha, hay problemas de conexión de la red con la Base de Datos Principal", vbOKOnly + vbExclamation, "Aviso"
      sFecha = Format(Now(), "DD/MM/YYYY hh:mm")
      'Unload Me
   End If
   
   'FIN FECHA DEL SISTEMA DE LA BASE DE DATOS DE SQL


  'sFecha = Format(Now(), "DD/MM/YYYY hh:mm")

  Set ADORS = Nothing
  
  'Inserta esta operacion
  sSQL = "INSERT INTO OPERACION (cOperCode, cUserDesc, dOperFech, cOperTipo, cCajaCodi, cTurnoCodi) " + _
         "VALUES" + _
         "($, '$', '$', $, '$', '$')"
  sSQL = VBsprintf(sSQL, CStr(iCodOper), psUsuario_Descripcion, sFecha, iTipoReporte, kPAR_CAJA, kPAR_TURNO)
  If Not oDBAdmin.EjecutarSentencia(ADOCnn, sSQL) Then GoTo LineaError
    
  sSQL = "SELECT MAX(cOperID) as MAXIMO FROM OPERACION"
  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo LineaError

  If ADORS.RecordCount = 1 Then
    If IsNull(ADORS("maximo")) Then
      iCodOperAutogenerado = 1
    Else
      iCodOperAutogenerado = LeerCampo(ADORS("maximo")) + 1
    End If
  End If
  Set ADORS = Nothing
  
'jpcb
  sSQL = "SELECT MAX(IdZ) as MAXIMO FROM CabeceraZ"
  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo LineaError

  If ADORS.RecordCount = 1 Then
    If IsNull(ADORS("maximo")) Then
      nCodCabZAutogenerado = 1
    Else
      nCodCabZAutogenerado = LeerCampo(ADORS("maximo")) + 1
    End If
  End If
  Set ADORS = Nothing
  
  If Not oDBAdmin.AbrirBD(cnSQL, psConnectString) Then GoTo Salir 'ABRE SQL OMNISYS
  
  sSQL = "SELECT cOperId, cOperIdP FROM Configuracion"
  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then
    cOperId = ""
    cOperIdP = ""
  Else
    cOperId = LeerCampo(ADORS("cOperId"))
    cOperIdP = LeerCampo(ADORS("cOperIdP"))
  End If
  
  If cOperId = "" Or cOperIdP = "" Then
    sCondicionOpe = "   and o.cOperflagabierto = 1 "
    sCondicionOpeP = "   and o.cOperflagabierto = 1 "
  Else
    sCondicionOpe = "   and o.cOperId > " + cOperId + " "
    sCondicionOpeP = "   and o.cOperCode > " + cOperIdP + " "
  End If
  
  '------fecha de apertura de caja -----------
  Dim dtfechaaper As Date
  sSQL = "SELECT MIN(o.dOperFech) AS Fecha " & _
         "  FROM Operacion o " & _
         " WHERE o.nMontoDolares > 0 And o.cOperFlagAnula= 0 " & sCondicionOpe
         
  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo LineaError
  If ADORS.RecordCount = 1 Then
     dtfechaaper = FormatDateTime(IIf(IsNull(ADORS!fecha), sFecha, ADORS!fecha), vbGeneralDate)
  Else
     dtfechaaper = sFecha
  End If
  
  Set ADORS = Nothing
    
  'Captura el tipo de cambio
  sSQL = " SELECT nTipoCambMont FROM Tipo_Cambio" & _
         " WHERE FORMAT(dTipoCambFech, 'yyyy/mm/dd') = '" & Format(dtfechaaper, "yyyy/mm/dd") & "'"

  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo LineaError
  If ADORS.RecordCount <> 1 Then
    MsgBox "Para registrar ventas en dólares debe registrar el tipo de cambio"
    nTipoCambio = 0
  Else
    nTipoCambio = LeerCampo(ADORS("nTipoCambMont"))
  End If
    
  sSQL = " SELECT SUM(O.nProdCant)AS TotItem, sum ((O.nProdCant * O.nProdPrecUnit)+ ((O.nProdCant * O.nProdPrecUnit) * O.nDscto/100))  AS TotMonto " & _
         "   FROM Operacion AS O" & _
         "  WHERE cOperTipo in (1,2,3)  " & _
         "    AND cprodcode <> 0 AND cOperflaganula= 0 " & sCondicionOpe
         
  If bParcial Then sSQL = sSQL + " AND cTurnoCodi = '" & kPAR_TURNO & "'"
     
  nTotal = 0
  nTotalItems = 0
  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo LineaError
  Do While Not ADORS.EOF
    If IsNull(LeerCampo(ADORS("TotItem"))) Or LeerCampo(ADORS("TotItem")) = "" Then
      nTotalItems = nTotalItems + 0
    Else
      nTotalItems = nTotalItems + LeerCampo(ADORS("TotItem"))
    End If
    If IsNull(LeerCampo(ADORS("TotMonto"))) Or LeerCampo(ADORS("TotMonto")) = "" Then
      nTotal = nTotal + 0
    Else
      nTotal = nTotal + LeerCampo(ADORS("TotMonto"))
    End If
    ADORS.MoveNext
  Loop
  TotalVenta = nTotal
  If nTotalItems = 0 Then
    nTotalPrecUnit = 0
  Else
    nTotalPrecUnit = nTotal / nTotalItems
  End If
    
  sSQL = " Select sum(nProdCant) as items  , sum((nProdPrecUnit *  nProdCant) + ((nProdPrecUnit *  nProdCant) * nDscto/100) ) as precio " & _
         "    FROM OperacionParalelaProducto p , OperacionParalela o " & _
         "   WHERE p.cOpercode = o.cOpercode " & _
         "     AND o.cOpercodeAnul=0 " & _
         "     AND o.cEstado='1' AND o.cOperTipo in (20)  " & sCondicionOpeP
         
  If bParcial Then
    sSQL = sSQL + " AND o.cTurnoCodi = '" & kPAR_TURNO & "' "
  Else
    sSQL = sSQL + " "
  End If
       
    
  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo LineaError
  ntotalitemsoc = ("0" & (ADORS!Items))
  nTotalItems = nTotalItems + ("0" & (ADORS!Items))
  nTotal = nTotal + ("0" & (ADORS!precio))
  TotalOC = CDbl(("0" & (ADORS!precio)))
  
  'Guardando valores para grabar en tabla Z
  nCantTotalCabZ = nTotalItems
  nMontoTotalCabZ = nTotal
  dFechaCabZ = Now()
  sCajaCabZ = kPAR_CAJA
  
  'Captura Cabecera
  Call CabeceraImpresion
  
  For i = 0 To UBound(sCabecera) - 1
    If Not Trim(sCabecera(i)) = "" Then
        AgregarLineaBloque (Right("                  " + sCabecera(i), Int(Len(sCabecera(i)) / 2) + 20))
    End If
  Next
  
  Call AgregarLineaBloque(vbCrLf + Format(CStr(Now()), "DD/MM/YYYY HH:MM (DDD)") & " NS:" & kSERIE_DISCO_DURO)
  
  printer.FontSize = 8 ' 11
  printer.FontName = "Courier New"
  
  'PRIMER BLOQUE
  Call AgregarLineaBloque("=======================================")
  Call AgregarLineaBloque(sLetraReporte + "1 CHECK                             01")
  Call AgregarLineaBloque("=======================================")
  Call AgregarLineaBloque("TOTAL     " + Right("                             *" + Format(CStr(nTotalItems), "###,###,##0.00"), 29))
  Call AgregarLineaBloque("          " + Right("                             *" + Format(CStr(nTotal), "###,###,##0.00"), 29))
  Call AgregarLineaBloque("=======================================")

  Dim sSql2 As String
  If bParcial Then sSql2 = " AND cTurnoCodi = '" & kPAR_TURNO & "' "
  
  sSQL = "SELECT cProdcode, sum(cantidad) as Cant ,  max(catalogo) as Catal, max(descripcion) as Descrip " & _
        " FROM  " & _
        "      ( SELECT 1, CProdCode,  sum(cant) as Cantidad , max(catal) as Catalogo, max(Descrip) as Descripcion " & _
        "          FROM    (SELECT  o.cProdCode,  sum(o.nProdCant) as cant ,  max(p.cCodcatalogo)  as catal, max( p.sProdDesc)  as Descrip  FROM Operacion o, Producto p " & _
        "                    WHERE o.cProdcode = p.cProdcode And o.cProdcode <> 0 And o.cOperFlaganula = 0 " & sCondicionOpe & _
        "                    GROUP BY o.cProdCode  ) " & _
        "       GROUP BY CPRODCODE  " & _
        "         UNION  " & _
        "       SELECT 2, d.cProdCode,  sum( d.nProdCant) as cantidad ,  max(p.cCodcatalogo)  as catalogo, max( p.sProdDesc)  as Descripcion " & _
        "         FROM  OperacionParalelaProducto  d, OperacionParalela o,   Producto  p WHERE d.cOpercode = o.cOpercode AND d.cProdCode = p.cProdCode AND d.cProdcode <> 0 AND o.cOperCodeanul = 0 AND cEstado= '1' AND o.cOpertipo in (20, 21) " & sCondicionOpeP & "  GROUP BY d.cProdCode   ) " & _
        " GROUP BY  cProdcode " & _
        " ORDER BY  3"
  
  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo LineaError
  Dim bHayDetalleZ As Boolean
  bHayDetalleZ = False
  
  If ADORS.RecordCount > 0 Then
    bHayDetalleZ = True
    Call AgregarLineaBloque("Producto                        Cant.  ")
    Call AgregarLineaBloque("---------------------------------------")
 
    ReDim ArregloZ(1 To ADORS.RecordCount)
    Dim ncontador As Integer
    ncontador = 1
    
    Do While Not ADORS.EOF
      nItems = LeerCampo(ADORS("Cant"))
      catal = LeerCampo(ADORS("Catal"))
      sProdDescCrta = LeerCampo(ADORS("Descrip"))
         
      Call AgregarLineaBloque(Left(catal + Space(10), 10) + " " + Left(sProdDescCrta + Space(17), 17) + " " + Right(Space(10) & Format(CStr(nItems), "###,###,##0.00"), 10))
      
      ArregloZ(ncontador).IdCab = nCodCabZAutogenerado
      ArregloZ(ncontador).CodProd = LeerCampo(ADORS("cProdCode"))
      ArregloZ(ncontador).DscProd = LeerCampo(ADORS("Descrip"))
      ArregloZ(ncontador).Cantidad = LeerCampo(ADORS("Cant"))
        
      ncontador = ncontador + 1
      
      ADORS.MoveNext
    Loop
    
    Call AgregarLineaBloque("=======================================")
  End If
 
  Dim descuento
  sSQL = " Select sum(p.nProdCant) as items  , sum((p.nProdPrecUnit * p.nProdCant)  + ((p.nProdPrecUnit * p.nProdCant) * nDscto/100) ) as precio " & _
         "    FROM OperacionParalelaProducto p , OperacionParalela o " & _
         "   WHERE p.cOpercode = o.cOpercode " & _
         "     AND o.cOpercodeAnul=0 " & sCondicionOpeP & _
         "     AND o.cEstado='1' AND cOperTipo in (21)  "

  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo LineaError
  
  nTotalItems = "0" & (ADORS!Items)
  nTotal = "0" & (ADORS!precio)
  If nTotalItems > 0 Then
    Call AgregarLineaBloque(sLetraReporte + "1 CHECK NC S/LIQUIDAR                ")
    Call AgregarLineaBloque("=======================================")
    Call AgregarLineaBloque("TOTAL     " + Right("                             *" + Format(CStr(nTotalItems), "###,###,##0.00"), 29))
    Call AgregarLineaBloque("          " + Right("                             *" + Format(CStr(nTotal), "###,###,##0.00"), 29))
    Call AgregarLineaBloque("=======================================")
    
    'Nota de Consumo
    sSQL = " SELECT d.cProdCode , sum(d.nProdCant) as Cantidad ,  max(p.cCodcatalogo) as Catalogo, max(p.sProdDesc) as Descripcion " & _
           " FROM OperacionParalelaProducto  d, OperacionParalela o,   Producto  p " & _
           " WHERE d.cOpercode = o.cOpercode " & _
           "   AND d.cProdCode = p.cProdCode " & _
           "   AND d.cProdcode <> 0 " & sCondicionOpeP & _
           "   AND o.cOperCodeanul =  0 AND cEstado= '1' AND o.cOpertipo in (21) " & _
           " GROUP BY d.cprodcode " & _
           " ORDER BY  4"
         
    If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo LineaError
    If ADORS.RecordCount > 0 Then
       Call AgregarLineaBloque("Producto                        Cant.  ")
       Call AgregarLineaBloque("---------------------------------------")
       
       Do While Not ADORS.EOF
          nItems = LeerCampo(ADORS("Cantidad"))
          catal = LeerCampo(ADORS("Catalogo"))
          sProdDescCrta = LeerCampo(ADORS("Descripcion"))
          
          Call AgregarLineaBloque(Left(catal + Space(10), 10) + " " + Left(sProdDescCrta + Space(17), 17) + " " + Right(Space(10) & Format(CStr(nItems), "###,###,##0.00"), 10))
          
          ADORS.MoveNext
       Loop
          
       Call AgregarLineaBloque("=======================================")
     End If
  End If
  
  'SEGUNDO BLOQUE: PADE
  Dim iTotPADE As Double
  Dim iTotPADES As Double
  Dim iTotPADED As Double
  Dim iTotPADEANU As Double
  Dim iTotPADEANUS As Double
  Dim iTotPADEANUD As Double
  Dim iItemPADEAnulado As Long
  Dim iItemPADE As Long
  Dim sDetallePA As String
  
  sSQL = " SELECT Sum(o.nMontosoles) as PADE, count(*) as items FROM OperacionParalela o " & _
         " WHERE o.cOperTipo in (22) and o.cEstado in ('1') " & _
         " AND o.cOpercodeAnul = 0 " & sCondicionOpeP
         
  If bParcial Then sSQL = sSQL + " AND cTurnoCodi = '" & kPAR_TURNO & "'"
  
  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo LineaError
  If Not ADORS.EOF Then
    iTotPADE = "0" & (ADORS!PADE)
    iItemPADE = "0" & (ADORS!Items)
  Else
    iTotPADE = 0
    iItemPADE = 0
  End If
  
  iTotPADEANU = 0
  iItemPADEAnulado = 0
    
  Call AgregarLineaBloque(sLetraReporte + "1 CHECK PADE                        02")
  Call AgregarLineaBloque("=======================================")
  Call AgregarLineaBloque("TOTAL     " + Right("                             *" + Format(CStr(iItemPADE), "###,###,##0.00"), 29))
  Call AgregarLineaBloque("          " + Right("                         S/. *" + Format(CStr(iTotPADE), "###,###,##0.00"), 29))
  Call AgregarLineaBloque("=======================================")
    
  sSQL = " SELECT o.cOperCode, o.cOperTipo, o.nTarjNumb, u.sUserName + ' ' + u.sUserApel as sGlosa, o.nMontosoles, o.nMontoDolares, o.nMoneda, o.cDocCodigo, o.cDocSerie FROM OperacionParalela o, usuario u " & _
         " WHERE o.cOperTipo in (22) and o.cEstado in ('1')  " & sCondicionOpeP & _
         " AND o.cOpercodeAnul = 0 AND o.cUserDesc = u.sUserDesc "
                 
  If bParcial Then sSQL = sSQL + " AND cTurnoCodi = '" & kPAR_TURNO & "'"
  
  If Not oDBAdmin.AbrirBD(ADOCnnSQL, psConnectString) Then GoTo LineaError
  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo LineaError
  
  If ADORS.RecordCount > 0 Then
    Call AgregarLineaBloque("Glosa              Tarjeta     Importe ")
    Call AgregarLineaBloque("---------------------------------------")
    
    iTotPADES = 0
    iTotPADED = 0
    iTotPADEANUS = 0
    iTotPADEANUD = 0
    
    Do While Not ADORS.EOF
      'Calculando PADE Anulados
      sSQL = "SELECT COUNT(1) AS EXISTE FROM PAGOS WHERE FechaAnulacion is not null and cSerieDocPago='" & LeerCampo(ADORS!cDocSerie) & "' AND convert(int,NroDocPago)=" & CStr(LeerCampo(ADORS!cDocCodigo))
      If oDBAdmin.EjecutarConsulta(ADOCnnSQL, sSQL, ADORS1) Then
         If LeerCampo(ADORS1("EXISTE")) > 0 Then
            iItemPADEAnulado = iItemPADEAnulado + 1
            iTotPADEANU = iTotPADEANU + LeerCampo(ADORS!nMontoSoles)
            If LeerCampo(ADORS!nMoneda) = 2 Then
              iTotPADEANUD = iTotPADEANUD + LeerCampo(ADORS!nMontoDolares)
              sDetallePA = sDetallePA + Left(Replace(LeerCampo(ADORS!sGlosa), vbCrLf, "") + Space(18), 18) + " " + Left(LeerCampo(ADORS!nTarjNumb) & Space(8), 8) + " US$" + Right(Space(8) & Format(CStr(ADORS!nMontoDolares), "###,###,##0.00"), 8) + vbCrLf
            Else
              iTotPADEANUS = iTotPADEANUS + LeerCampo(ADORS!nMontoSoles)
              sDetallePA = sDetallePA + Left(Replace(LeerCampo(ADORS!sGlosa), vbCrLf, "") + Space(18), 18) + " " + Left(LeerCampo(ADORS!nTarjNumb) & Space(8), 8) + " S/." + Right(Space(8) & Format(CStr(ADORS!nMontoSoles), "###,###,##0.00"), 8) + vbCrLf
            End If
         End If
      End If
      
      'Modificacion Dolares
      Dim mon As String
      If LeerCampo(ADORS!nMoneda) = 2 Then
        Call AgregarLineaBloque(Left(Replace(LeerCampo(ADORS!sGlosa), vbCrLf, "") + Space(18), 18) + " " + Left(LeerCampo(ADORS!nTarjNumb) & Space(8), 8) + " US$" + Right(Space(8) & Format(CStr(ADORS!nMontoDolares), "###,###,##0.00"), 8))
        iTotPADED = iTotPADED + LeerCampo(ADORS!nMontoDolares)
      Else
        Call AgregarLineaBloque(Left(Replace(LeerCampo(ADORS!sGlosa), vbCrLf, "") + Space(18), 18) + " " + Left(LeerCampo(ADORS!nTarjNumb) & Space(8), 8) + " S/." + Right(Space(8) & Format(CStr(ADORS!nMontoSoles), "###,###,##0.00"), 8))
        iTotPADES = iTotPADES + LeerCampo(ADORS!nMontoSoles)
      End If
      
      ADORS.MoveNext
    Loop
    
    Call AgregarLineaBloque("=======================================")
  End If
  
  'TERCER BLOQUE: PADE LIQUIDADOS EN EL DIA
  Dim PADLIQDIA As Double
  Dim PADLIQDIAS As Double
  Dim PADLIQDIAD As Double
  
  Dim iItemPADLIQDIA As Long
  sSQL = " SELECT sum(o.nMontosoles) as padliq, Count(*) as item  FROM OperacionParalela o " & _
         " WHERE o.cOperTipo in (22) and o.cEstado in ('2') " & sCondicionOpeP & _
         " AND o.cOpercodeAnul = 0 "
         
  If bParcial Then sSQL = sSQL + " AND cTurnoCodi = '" & kPAR_TURNO & "'"
  
  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo LineaError
  If Not ADORS.EOF Then
     PADLIQDIA = "0" & ADORS!PADLIQ
     iItemPADLIQDIA = "0" & ADORS!Item
  Else
    PADLIQDIA = 0
    iItemPADLIQDIA = 0
  End If
  
  Call AgregarLineaBloque(sLetraReporte + "1 CHECK PADE LIQ  Dia               03")
  Call AgregarLineaBloque("=======================================")
  Call AgregarLineaBloque("TOTAL     " + Right("                             *" + Format(CStr(iItemPADLIQDIA), "###,###,##0.00"), 29))
  Call AgregarLineaBloque("          " + Right("                         S/. *" + Format(CStr(PADLIQDIA), "###,###,##0.00"), 29))
  Call AgregarLineaBloque("=======================================")
    
  sSQL = " SELECT o.cOperCode, o.cOperTipo, o.nTarjNumb, o.sGlosa, o.nMontosoles, o.nMontoDolares, o.nMoneda FROM OperacionParalela o " & _
         " WHERE o.cOperTipo in (22) and o.cEstado in ('2') " & sCondicionOpeP & _
         " AND o.cOpercodeAnul = 0 "
         
  If bParcial Then sSQL = sSQL + " AND cTurnoCodi = '" & kPAR_TURNO & "'"
  
  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo LineaError
  
  If ADORS.RecordCount > 0 Then
    Call AgregarLineaBloque("Glosa              Tarjeta     Importe ")
    Call AgregarLineaBloque("---------------------------------------")
    
    PADLIQDIAS = 0
    PADLIQDIAD = 0
    Do While Not ADORS.EOF
      'Modificacion Dolares
      If LeerCampo(ADORS!nMoneda) = 2 Then
        Call AgregarLineaBloque(Left(Replace(LeerCampo(ADORS!sGlosa), vbCrLf, "") + Space(18), 18) + " " + Left(LeerCampo(ADORS!nTarjNumb) & Space(8), 8) + " US$" + Right(Space(8) & Format(CStr(ADORS!nMontoDolares), "###,###,##0.00"), 8))
        PADLIQDIAD = PADLIQDIAD + LeerCampo(ADORS!nMontoDolares)
      Else
        Call AgregarLineaBloque(Left(Replace(LeerCampo(ADORS!sGlosa), vbCrLf, "") + Space(18), 18) + " " + Left(LeerCampo(ADORS!nTarjNumb) & Space(8), 8) + " S/." + Right(Space(8) & Format(CStr(ADORS!nMontoSoles), "###,###,##0.00"), 8))
        PADLIQDIAS = PADLIQDIAS + LeerCampo(ADORS!nMontoSoles)
      End If
      
      ADORS.MoveNext
    Loop
    Call AgregarLineaBloque("=======================================")
  End If
  
  'CUARTO BLOQUE: PADES LIQUIDADOS y CERRADOS
  Dim PADLIQ As Double
  Dim PADLIQS As Double
  Dim PADLIQD As Double
  Dim iItemPADLIQ As Long
  sSQL = " SELECT sum(nMontosoles) as padliq, count(*) as item FROM OperacionParalela " & _
         " WHERE cOperTipo in (22) and cOperFlagAbierto=0 and cEstado in ('2') " & _
         " AND cOperCodeRela in (SELECT o.cOperCode from OperacionParalela o WHERE o.cOpercodeAnul = 0 " & sCondicionOpeP & ")" & _
         " AND cOpercodeAnul = 0 "
    
  If bParcial Then sSQL = sSQL + " AND cTurnoCodi = '" & kPAR_TURNO & "'"
  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo LineaError
  If Not ADORS.EOF Then
    PADLIQ = "0" & ADORS!PADLIQ
    iItemPADLIQ = "0" & ADORS!Item
  Else
    PADLIQ = 0
    iItemPADLIQ = 0
  End If
  
  Call AgregarLineaBloque(sLetraReporte + "1 CHECK PADE LIQ                    04")
  Call AgregarLineaBloque("=======================================")
  Call AgregarLineaBloque("TOTAL     " + Right("                             *" + Format(CStr(iItemPADLIQ), "###,###,##0.00"), 29))
  Call AgregarLineaBloque("          " + Right("                         S/. *" + Format(CStr(PADLIQ), "###,###,##0.00"), 29))
  Call AgregarLineaBloque("=======================================")

  sSQL = " SELECT cOperCode, cOperTipo, nTarjNumb, sGlosa, nMontosoles, nMoneda, nMontoDolares FROM OperacionParalela " & _
         " WHERE cOperTipo in (22) and cOperFlagAbierto=0 and cEstado in ('2') " & _
         " AND cOperCodeRela in (SELECT o.cOperCode from OperacionParalela o WHERE coperCodeanul= 0 " & sCondicionOpeP & ")" & _
         " AND cOpercodeAnul = 0 "
         
  If bParcial Then sSQL = sSQL + " AND cTurnoCodi = '" & kPAR_TURNO & "'"
  
  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo LineaError
  
  If ADORS.RecordCount > 0 Then
    Call AgregarLineaBloque("Glosa              Tarjeta     Importe ")
    Call AgregarLineaBloque("---------------------------------------")
    
    PADLIQS = 0
    PADLIQD = 0
    Do While Not ADORS.EOF
      'Modificacion Dolares
      If LeerCampo(ADORS!nMoneda) = 2 Then
        Call AgregarLineaBloque(Left(Replace(LeerCampo(ADORS!sGlosa), vbCrLf, "") + Space(18), 18) + " " + Left(LeerCampo(ADORS!nTarjNumb) & Space(8), 8) + " US$" + Right(Space(8) & Format(CStr(ADORS!nMontoDolares), "###,###,##0.00"), 8))
        PADLIQD = PADLIQD + LeerCampo(ADORS!nMontoDolares)
      Else
        Call AgregarLineaBloque(Left(Replace(LeerCampo(ADORS!sGlosa), vbCrLf, "") + Space(18), 18) + " " + Left(LeerCampo(ADORS!nTarjNumb) & Space(8), 8) + " S/." + Right(Space(8) & Format(CStr(ADORS!nMontoSoles), "###,###,##0.00"), 8))
        PADLIQS = PADLIQS + LeerCampo(ADORS!nMontoSoles)
      End If
      
      ADORS.MoveNext
    Loop
    Call AgregarLineaBloque("=======================================")
  End If
  
  'QUINTO BLOQUE: PADE ANULADOS
  Call AgregarLineaBloque(sLetraReporte + "1 CHECK PADE ANULADOS               05")
  Call AgregarLineaBloque("=======================================")
  Call AgregarLineaBloque("TOTAL     " + Right("                             *" + Format(CStr(iItemPADEAnulado), "###,###,##0.00"), 29))
  Call AgregarLineaBloque("          " + Right("                         S/. *" + Format(CStr(iTotPADEANU), "###,###,##0.00"), 29))
  Call AgregarLineaBloque("=======================================")

  If iItemPADEAnulado > 0 Then
     Call AgregarLineaBloque("Glosa              Tarjeta     Importe ")
     Call AgregarLineaBloque("---------------------------------------")
     Call AgregarLineaBloque(sDetallePA)
     Call AgregarLineaBloque("=======================================")
  End If
  
  'SEXTO BLOQUE: PADV
  Dim iTotPADV As Double
  Dim iTotPADVS As Double
  Dim iTotPADVD As Double
  Dim iItemPADV As Long
  Dim iTotPADVANU As Double
  Dim iTotPADVANUS As Double
  Dim iTotPADVANUD As Double
  Dim iItemPADVAnulado As Long
  Dim sDetallePV As String
  
  sSQL = " SELECT Sum(o.nMontosoles) as PADV, count(*) as items FROM OperacionParalela o " & _
         " WHERE o.cOperTipo in (23) and o.cEstado in ('1') " & sCondicionOpeP & _
         " AND o.cOpercodeAnul = 0 "
  
  If bParcial Then sSQL = sSQL + " AND cTurnoCodi = '" & kPAR_TURNO & "'"
  
  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo LineaError
  
  If Not ADORS.EOF Then
    iTotPADV = "0" & (ADORS!PADV)
    iItemPADV = "0" & (ADORS!Items)
  Else
    iTotPADV = 0
    iItemPADV = 0
  End If
  
  iTotPADVANU = 0
  iItemPADVAnulado = 0
  
  Call AgregarLineaBloque(sLetraReporte + "1 CHECK PADV                        06")
  Call AgregarLineaBloque("=======================================")
  Call AgregarLineaBloque("TOTAL     " + Right("                             *" + Format(CStr(iItemPADV), "###,###,##0.00"), 29))
  Call AgregarLineaBloque("          " + Right("                         S/. *" + Format(CStr(iTotPADV), "###,###,##0.00"), 29))
  Call AgregarLineaBloque("=======================================")

  sSQL = " SELECT o.cOperCode, o.cOperTipo, o.nTarjNumb, u.sUserName + ' ' + u.sUserApel as sGlosa, o.nMontosoles, o.nMontoDolares, o.nMoneda, o.cDocCodigo, o.cDocSerie FROM OperacionParalela o, usuario u " & _
         " WHERE o.cOperTipo in (23) and o.cEstado in ('1') " & sCondicionOpeP & _
         " AND o.cOpercodeAnul = 0 AND o.cUserDesc = u.sUserDesc "
         
  If bParcial Then sSQL = sSQL + " AND cTurnoCodi = '" & kPAR_TURNO & "'"
  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo LineaError
  If ADORS.RecordCount > 0 Then
    Call AgregarLineaBloque("Glosa              Tarjeta     Importe ")
    Call AgregarLineaBloque("---------------------------------------")
    
    iTotPADVS = 0
    iTotPADVD = 0
    iTotPADVANUS = 0
    iTotPADVANUD = 0
    
    Do While Not ADORS.EOF
      'Calculando PADV Anulados
      sSQL = "SELECT COUNT(1) AS EXISTE FROM PAGOS WHERE FechaAnulacion is not null and cSerieDocPago='" & LeerCampo(ADORS!cDocSerie) & "' AND convert(int,NroDocPago)=" & CStr(LeerCampo(ADORS!cDocCodigo))
      If oDBAdmin.EjecutarConsulta(ADOCnnSQL, sSQL, ADORS1) Then
         If LeerCampo(ADORS1("EXISTE")) > 0 Then
            iItemPADVAnulado = iItemPADEAnulado + 1
            iTotPADVANU = iTotPADVANU + LeerCampo(ADORS!nMontoSoles)
            If LeerCampo(ADORS!nMoneda) = 2 Then
              iTotPADVANUD = iTotPADEANUD + LeerCampo(ADORS!nMontoDolares)
              sDetallePV = sDetallePV + Left(Replace(LeerCampo(ADORS!sGlosa), vbCrLf, "") + Space(18), 18) + " " + Left(LeerCampo(ADORS!nTarjNumb) & Space(8), 8) + " US$" + Right(Space(8) & Format(CStr(ADORS!nMontoDolares), "###,###,##0.00"), 8) + vbCrLf
            Else
              iTotPADVANUS = iTotPADEANUS + LeerCampo(ADORS!nMontoSoles)
              sDetallePV = sDetallePV + Left(Replace(LeerCampo(ADORS!sGlosa), vbCrLf, "") + Space(18), 18) + " " + Left(LeerCampo(ADORS!nTarjNumb) & Space(8), 8) + " S/." + Right(Space(8) & Format(CStr(ADORS!nMontoSoles), "###,###,##0.00"), 8) + vbCrLf
            End If
         End If
      End If
      
      'Modificacion Dolares
      If LeerCampo(ADORS!nMoneda) = 2 Then
        Call AgregarLineaBloque(Left(Replace(LeerCampo(ADORS!sGlosa), vbCrLf, "") + Space(18), 18) + " " + Left(LeerCampo(ADORS!nTarjNumb) & Space(8), 8) + " US$" + Right(Space(8) & Format(CStr(ADORS!nMontoDolares), "###,###,##0.00"), 8))
        iTotPADVD = iTotPADVD + LeerCampo(ADORS!nMontoDolares)
      Else
        Call AgregarLineaBloque(Left(Replace(LeerCampo(ADORS!sGlosa), vbCrLf, "") + Space(18), 18) + " " + Left(LeerCampo(ADORS!nTarjNumb) & Space(8), 8) + " S/." + Right(Space(8) & Format(CStr(ADORS!nMontoSoles), "###,###,##0.00"), 8))
        iTotPADVS = iTotPADVS + LeerCampo(ADORS!nMontoSoles)
      End If
      
      ADORS.MoveNext
    Loop
    Call AgregarLineaBloque("=======================================")
 End If
  
  'SETIMO BLOQUE: PADVS LIQUIDADOS dia
  Dim PADVLIQDIA As Double
  Dim PADVLIQDIAS As Double
  Dim PADVLIQDIAD As Double
  Dim iItemPADVLIQDIA As Long
  sSQL = " SELECT sum(o.nMontosoles) as padliq, Count(*) as item FROM OperacionParalela o " & _
         " WHERE o.cOperTipo in (23) and o.cEstado in ('2') " & sCondicionOpeP & _
         " AND o.cOpercodeAnul = 0 "
              
  If bParcial Then sSQL = sSQL + " AND cTurnoCodi = '" & kPAR_TURNO & "'"
  
  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo LineaError
  If Not ADORS.EOF Then
     PADVLIQDIA = "0" & ADORS!PADLIQ
     iItemPADVLIQDIA = "0" & ADORS!Item
  Else
    PADVLIQDIA = 0
    iItemPADVLIQDIA = 0
  End If
  
  Call AgregarLineaBloque(sLetraReporte + "1 CHECK PADV LIQ Dia                07")
  Call AgregarLineaBloque("=======================================")
  Call AgregarLineaBloque("TOTAL     " + Right("                             *" + Format(CStr(iItemPADVLIQDIA), "###,###,##0.00"), 29))
  Call AgregarLineaBloque("          " + Right("                         S/. *" + Format(CStr(PADVLIQDIA), "###,###,##0.00"), 29))
  Call AgregarLineaBloque("=======================================")

  sSQL = " SELECT o.cOperCode, o.cOperTipo, o.nTarjNumb, o.sGlosa, o.nMontosoles, o.nMontoDolares, o.nMoneda FROM OperacionParalela o " & _
         " WHERE o.cOperTipo in (23) and o.cEstado in ('2') " & sCondicionOpeP & _
         " AND o.cOpercodeAnul = 0 "
         
  If bParcial Then sSQL = sSQL + " AND cTurnoCodi = '" & kPAR_TURNO & "'"
  
  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo LineaError
  If ADORS.RecordCount > 0 Then
     Call AgregarLineaBloque("Glosa              Tarjeta     Importe ")
     Call AgregarLineaBloque("---------------------------------------")
     
     PADVLIQDIAD = 0
     PADVLIQDIAS = 0
     Do While Not ADORS.EOF
       'Modificacion Dolares
       If LeerCampo(ADORS!nMoneda) = 2 Then
         Call AgregarLineaBloque(Left(Replace(LeerCampo(ADORS!sGlosa), vbCrLf, "") + Space(18), 18) + " " + Left(LeerCampo(ADORS!nTarjNumb) & Space(8), 8) + " US$" + Right(Space(8) & Format(CStr(ADORS!nMontoDolares), "###,###,##0.00"), 8))
         PADVLIQDIAD = PADVLIQDIAD + LeerCampo(ADORS!nMontoDolares)
       Else
         Call AgregarLineaBloque(Left(Replace(LeerCampo(ADORS!sGlosa), vbCrLf, "") + Space(18), 18) + " " + Left(LeerCampo(ADORS!nTarjNumb) & Space(8), 8) + " S/." + Right(Space(8) & Format(CStr(ADORS!nMontoSoles), "###,###,##0.00"), 8))
         PADVLIQDIAS = PADVLIQDIAS + LeerCampo(ADORS!nMontoSoles)
       End If
      
       ADORS.MoveNext
     Loop
     Call AgregarLineaBloque("=======================================")
   End If
 
  'OCTAVO BLOQUE: PADVS LIQUIDADOS
  Dim PADVLIQ As Double
  Dim PADVLIQS As Double
  Dim PADVLIQD As Double
  
  Dim iItemPADVLIQ As Long
  sSQL = " SELECT sum(nMontosoles) as padliq, count(*) as item FROM OperacionParalela " & _
         " WHERE cOperTipo in (23) and cOperFlagAbierto=0 and cEstado in ('2') " & _
         " AND cOperCodeRela in (SELECT o.cOperCode from OperacionParalela o WHERE cOpercodeAnul = 0 " & sCondicionOpeP & ") " & _
         " AND cOpercodeAnul = 0 "
             
  If bParcial Then sSQL = sSQL + " AND cTurnoCodi = '" & kPAR_TURNO & "'"
  
  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo LineaError
  If Not ADORS.EOF Then
     PADVLIQ = "0" & ADORS!PADLIQ
     iItemPADVLIQ = "0" & ADORS!Item
  Else
    PADVLIQ = 0
    iItemPADVLIQ = 0
  End If
  
  Call AgregarLineaBloque(sLetraReporte + "1 CHECK PADV LIQ                    08")
  Call AgregarLineaBloque("=======================================")
  Call AgregarLineaBloque("TOTAL     " + Right("                             *" + Format(CStr(iItemPADVLIQ), "###,###,##0.00"), 29))
  Call AgregarLineaBloque("          " + Right("                         S/. *" + Format(CStr(PADVLIQ), "###,###,##0.00"), 29))
  Call AgregarLineaBloque("=======================================")

  sSQL = " SELECT cOperCode, cOperTipo, nTarjNumb, sGlosa, nMontosoles, nMontoDolares, nMoneda FROM OperacionParalela " & _
         " WHERE cOperTipo in (23) and cOperFlagAbierto=0 and cEstado in ('2') " & _
         "   AND cOperCodeRela in (SELECT o.cOperCode from OperacionParalela o WHERE cOpercodeAnul = 0 " & sCondicionOpeP & ") " & _
         "   AND cOpercodeAnul = 0 "
         
  If bParcial Then sSQL = sSQL + " AND cTurnoCodi = '" & kPAR_TURNO & "'"
  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo LineaError
  If ADORS.RecordCount > 0 Then
     Call AgregarLineaBloque("Glosa              Tarjeta     Importe ")
     Call AgregarLineaBloque("---------------------------------------")
     
     PADVLIQS = 0
     PADVLIQD = 0
     Do While Not ADORS.EOF
       'Modificacion Dolares
     
       If LeerCampo(ADORS!nMoneda) = 2 Then
         Call AgregarLineaBloque(Left(Replace(LeerCampo(ADORS!sGlosa), vbCrLf, "") + Space(18), 18) + " " + Left(LeerCampo(ADORS!nTarjNumb) & Space(8), 8) + " US$" + Right(Space(8) & Format(CStr(ADORS!nMontoDolares), "###,###,##0.00"), 8))
         PADVLIQD = PADVLIQD + LeerCampo(ADORS!nMontoDolares)
       Else
         Call AgregarLineaBloque(Left(Replace(LeerCampo(ADORS!sGlosa), vbCrLf, "") + Space(18), 18) + " " + Left(LeerCampo(ADORS!nTarjNumb) & Space(8), 8) + " S/." + Right(Space(8) & Format(CStr(ADORS!nMontoSoles), "###,###,##0.00"), 8))
         PADVLIQS = PADVLIQS + LeerCampo(ADORS!nMontoSoles)
       End If
       
     ADORS.MoveNext
    Loop
    Call AgregarLineaBloque("=======================================")
  End If
  
  'NOVENO BLOQUE: PADV ANULADOS
  Call AgregarLineaBloque(sLetraReporte + "1 CHECK PADV ANULADOS               09")
  Call AgregarLineaBloque("=======================================")
  Call AgregarLineaBloque("TOTAL     " + Right("                             *" + Format(CStr(iItemPADVAnulado), "###,###,##0.00"), 29))
  Call AgregarLineaBloque("          " + Right("                         S/. *" + Format(CStr(iTotPADVANU), "###,###,##0.00"), 29))
  Call AgregarLineaBloque("=======================================")
  
  If iItemPADVAnulado > 0 Then
     Call AgregarLineaBloque("Glosa              Tarjeta     Importe ")
     Call AgregarLineaBloque("---------------------------------------")
     Call AgregarLineaBloque(sDetallePV)
     Call AgregarLineaBloque("=======================================")
  End If
  
  '----------------------Vouchers---------------------------
  Dim iEfeVoucher As Double
  sSQL = " SELECT SUM((o.nProdPrecUnit * o.nProdCant) +  ((o.nProdPrecUnit * o.nProdCant) * o.nDscto/100)) AS TOTAL" & _
         " FROM Operacion o " & _
         " WHERE o.cOperTipo = 2" & _
         " AND o.cOperFlagAnula = 0 " & _
         " AND o.cOpercodeAnula=0 " & sCondicionOpe
         
  If bParcial Then sSQL = sSQL + " AND cTurnoCodi = '" & kPAR_TURNO & "'"

  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo LineaError
  If Not ADORS.EOF Then
    iVoucMont = "0" & (ADORS!Total)
  Else
    iVoucMont = 0
  End If
  Set ADORS = Nothing
  
  'DECIMO BLOQUE: Efectivo en vouchers
  sSQL = " SELECT SUM(o.nMontosoles) AS TOTAL" & _
           " FROM Operacion o" & _
           " WHERE o.cOperTipo = 1 AND TRIM(o.cEmisor) <> '' " & _
           " AND o.cOperFlagAnula = 0 " & sCondicionOpe

  If bParcial Then sSQL = sSQL + " AND cTurnoCodi = '" & kPAR_TURNO & "'"
          
  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo LineaError
  If Not ADORS.EOF Then
    iEfeVoucher = "0" & (ADORS!Total)
  Else
    iEfeVoucher = 0
  End If
  Set ADORS = Nothing
   iVoucMont = iVoucMont - iEfeVoucher
   '-------------------------Voucher en liquidacion ------------------------
  sSQL = " SELECT SUM(o.nmontosoles +  o.nprodprecunit ) as TOTAL" & _
         "   FROM operacion o" & _
         "  WHERE o.cOperTipo = 2 and o.cDocTipo='14' " & _
         "    AND o.cOperFlagAnula = 0" & sCondicionOpe
         
  If bParcial Then sSQL = sSQL + " AND cTurnoCodi = '" & kPAR_TURNO & "'"
         
  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo LineaError
  iVoucMont = iVoucMont + val("0" & ADORS!Total)
  
   '--------Contabilizando tickets---------------------------------------------------------
   sSQL = "SELECT distinct(o.cOpercode) " & _
          " FROM operacion o WHERE  (o.cdoctipo='02' or o.cdoctipo='01') " & _
          " AND o.cOperFlaganula=0 " & sCondicionOpe
          
   If bParcial Then sSQL = sSQL + " AND cTurnoCodi = '" & kPAR_TURNO & "'"
   
   If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo LineaError
   iCashNume = ADORS.RecordCount
   
   sSQL = "SELECT distinct(o.cOpercode) " & _
          " FROM operacion o WHERE  (o.cemisor <> ''  or o.cOperTipo=2) " & _
          " AND o.cOperFlaganula=0 " & sCondicionOpe
   
   If bParcial Then sSQL = sSQL + " AND cTurnoCodi = '" & kPAR_TURNO & "'"
   If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo LineaError
   
   iVoucNume = ADORS.RecordCount
  
  Call AgregarLineaBloque("=======================================")
  Call AgregarLineaBloque(sLetraReporte + "1 CHECK  TOTAL TRANSACCIONES       10")
  Call AgregarLineaBloque("=======================================")
  
  If iCashNume <> 0 Then
    Call AgregarLineaBloque("TICKET      " + Right(Space(27) + "000000" + CStr(iCashNume) + "   ", 27))
    Call AgregarLineaBloque("            " + Right(Space(27) + "*" + Format(CStr(TotalVenta), "###,###,##0.00"), 27))
  End If
  
  If iItemPADE <> 0 Then
    Call AgregarLineaBloque("PADE        " + Right(Space(27) + "000000" + CStr(iItemPADE) + "   ", 27))
    Call AgregarLineaBloque("            " + Right(Space(27) & "S/. *" + Format(CStr(iTotPADES - iTotPADEANUS), "###,###,##0.00"), 27))
    Call AgregarLineaBloque("            " + Right(Space(27) & "US$ *" + Format(CStr(iTotPADED - iTotPADEANUD), "###,###,##0.00"), 27))
  End If
  
  If PADLIQ <> 0 Then
    Call AgregarLineaBloque("PADE LIQ    " + Right(Space(27) & "000000" + CStr(iItemPADLIQ) + "   ", 27))
    Call AgregarLineaBloque("            " + Right(Space(27) & "S/. *(" + Format(CStr(PADLIQS), "###,###,##0.00)"), 27))
    Call AgregarLineaBloque("            " + Right(Space(27) & "US$ *(" + Format(CStr(PADLIQD), "###,###,##0.00)"), 27))
  End If
  
  If iItemPADV <> 0 Then
    Call AgregarLineaBloque("PADV        " + Right(Space(27) & "000000" + CStr(iItemPADV) + "   ", 27))
    Call AgregarLineaBloque("            " + Right(Space(27) & "S/. *" + Format(CStr(iTotPADVS - iTotPADVANUS), "###,###,##0.00"), 27))
    Call AgregarLineaBloque("            " + Right(Space(27) & "US$ *" + Format(CStr(iTotPADVD - iTotPADVANUD), "###,###,##0.00"), 27))
  End If
    
  If PADVLIQ <> 0 Then
    Call AgregarLineaBloque("PADV LIQ    " + Right(Space(27) & "000000" + CStr(iItemPADVLIQ) + "   ", 27))
    Call AgregarLineaBloque("            " + Right(Space(27) & "S/. *(" + Format(CStr(PADVLIQS), "###,###,##0.00)"), 27))
    Call AgregarLineaBloque("            " + Right(Space(27) & "US$ *(" + Format(CStr(PADVLIQD), "###,###,##0.00)"), 27))
  End If
  
  If ntotalitemsoc <> 0 Then '-Operaciones de ORDENES DE CONSUMO -
    Call AgregarLineaBloque("O/C         " + Right(Space(27) & "000000" + CStr(ntotalitemsoc) + "   ", 27))
    Call AgregarLineaBloque("            " + Right(Space(27) & "*" + Format(CStr(TotalOC), "###,###,##0.00"), 27) + vbCrLf)
  End If
  
  Call AgregarLineaBloque("---------------------------------------")
  If iVoucNume <> 0 Then
    Call AgregarLineaBloque("VOUCHER VTA " + Right(Space(27) & "000000" + CStr(iVoucNume) + "   ", 27))
    Call AgregarLineaBloque("            " + Right(Space(27) & "*" + Format(CStr(iVoucMont), "###,###,##0.00"), 27))
  End If
  
  If PADLIQDIA <> 0 Then
    Call AgregarLineaBloque("PADE LQ Día " + Right(Space(27) & "000000" + CStr(iItemPADLIQDIA) + "   ", 27))
    Call AgregarLineaBloque("            " + Right(Space(27) & "S/. *" + Format(CStr(PADLIQDIAS), "###,###,##0.00"), 27))
    Call AgregarLineaBloque("            " + Right(Space(27) & "US$ *" + Format(CStr(PADLIQDIAD), "###,###,##0.00"), 27))
  End If
  
  If PADVLIQDIA <> 0 Then
    Call AgregarLineaBloque("PADV LQ Día " + Right(Space(27) & "000000" + CStr(iItemPADVLIQDIA) + "   ", 27))
    Call AgregarLineaBloque("            " + Right(Space(27) & "S/. *" + Format(CStr(PADVLIQDIAS), "###,###,##0.00"), 27))
    Call AgregarLineaBloque("            " + Right(Space(27) & "US$ *" + Format(CStr(PADVLIQDIAD), "###,###,##0.00"), 27))
  End If
  
  'AQUI VA LO DEL PAGO Y TRANSACCIONES ANULADAS
  sSQL = " SELECT SUM((o.nProdPrecUnit * o.nProdCant)  + ((o.nProdPrecUnit * o.nProdCant) * o.nDscto/100)) as TKMontNulos" & _
        "   FROM Operacion o " & _
        "  WHERE o.cOperflaganula= 1 " & _
        "    AND o.cProdcode <>  0 " & _
        "    AND o.cOpercodeanula <> 0 " & sCondicionOpe

  If bParcial Then sSQL = sSQL + " AND cTurnoCodi = '" & kPAR_TURNO & "'"
  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo LineaError
  If Not ADORS.EOF Then
     iVOPOMont = IIf(IsNull(ADORS!TKMontNulos), 0, ADORS!TKMontNulos)
  End If
  
  sSQL = " SELECT distinct o.cOperCode " & _
        "   FROM Operacion o " & _
        "  WHERE o.cOperflaganula= 1 " & _
        "    AND o.cProdcode <>  0 " & _
        "    AND o.cOpercodeanula <> 0 " & sCondicionOpe
        
  If bParcial Then sSQL = sSQL + " AND cTurnoCodi = '" & kPAR_TURNO & "'"
  
  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo LineaError
  If Not ADORS.EOF Then
     iVOPONume = IIf(IsNull(ADORS.RecordCount), 0, ADORS.RecordCount)
  End If
  '-----Recaudaciones
  sSQL = " SELECT SUM((o.nProdPrecUnit * o.nProdCant)  + ((o.nProdPrecUnit * o.nProdCant) * o.nDscto/100)) as TKMontPago, COUNT(*) as TKPago " & _
        "   FROM Operacion o " & _
        "  WHERE o.cOperflaganula= 0 " & _
        "    AND o.cOpercodeanula = 0 " & _
        "    AND o.cOpertipo= 5 " & sCondicionOpe

    
  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo LineaError
  If Not ADORS.EOF Then
     iPagoMont = IIf(IsNull(ADORS!TKMontPago), 0, ADORS!TKMontPago)
     iPagoNume = IIf(IsNull(ADORS!TKPago), 0, ADORS!TKPago)
  End If
  
  '---------------Datos de Apertura de caja
  Dim iAperturasoles As Double
  Dim iAperturadolares As Double
  sSQL = " SELECT  nImporApertSol, nImporApertDol " & _
         " FROM Configuracion "

  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo LineaError
  iAperturasoles = (ADORS!nImporApertSol)
  iAperturadolares = (ADORS!nImporApertDol)
  
  '------- DEL CUADRE -----------------'
  Dim iCashMontdol As Double
  iCashMont = 0
  iCashMontdol = 0
  
 'Total de Dolares obtenidos por ventas
  sSQL = " SELECT SUM(o.nMontoDolares-o.nVueltoDolares) AS TOTAL" & _
         " FROM Operacion o " & _
         " WHERE o.cOperTipo in (1) " & _
         " AND o.cDoctipo in ('01','02', '14', '10') " & _
         " AND o.cOperFlaganula= 0 " & sCondicionOpe
         
  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo LineaError
  
  Do While Not ADORS.EOF
    If IsNull(LeerCampo(ADORS!Total)) Or (LeerCampo(ADORS!Total)) = "" Then
      iCashMontdol = 0
    Else
      iCashMontdol = iCashMontdol + LeerCampo(ADORS!Total)
    End If
    ADORS.MoveNext
  Loop
  
  'Calculo del Cash Soles
         
  sSQL = " SELECT SUM(o.nMontoSoles + o.nProdPrecUnit) AS TOTAL" & _
           " FROM Operacion o " & _
           " WHERE (o.cOperTipo in( 1)) " & _
           " AND o.cDoctipo in ('01','02','10', '14') " & _
           " AND o.cProdCode = 0 " & _
           " AND o.cOperFlaganula= 0 " & sCondicionOpe
  
  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo LineaError
  
  Do While Not ADORS.EOF
    If IsNull(LeerCampo(ADORS!Total)) Or (LeerCampo(ADORS!Total)) = "" Then
      iCashMont = 0
    Else
     iCashMont = iCashMont + LeerCampo(ADORS!Total)
    End If
    ADORS.MoveNext
  Loop
    
  Set ADORS = Nothing
  
  '------------------------------------------------
  If iPagoMont > (iAperturasoles + iCashMont) Then
      iPagoMont = iAperturasoles + iCashMont
  End If
  
  If iPagoMont > 0 Then 'Existe Pago
    Call AgregarLineaBloque("RECAUDACION " + Right(Space(29) + "000000" + CStr(iPagoNume) + "   ", 29) + "   ")
    Call AgregarLineaBloque("          " + Right(Space(28) + "(" + Format(CStr(iPagoMont), "###,###,##0.00") + ")", 29))
  End If
  
 'ONCEAVO BLOQUE
  Call AgregarLineaBloque("=======================================")
  Call AgregarLineaBloque(sLetraReporte + "1 CHECK  TOTALES                    11")
  Call AgregarLineaBloque("=======================================")
  
  '-------FIN DEL CUADRE---------------'
  If iAperturasoles > 0 Then 'Existe monto de apertura
    Call AgregarLineaBloque("APERT.  S/  " + Right(Space(27) + "*" + Format(CStr(iAperturasoles), "###,###,##0.00"), 27))
  End If
  If iAperturadolares > 0 Then 'Existe monto de apertura dolares
    Call AgregarLineaBloque("APERT. US$  " + Right(Space(27) + "*" + Format(CStr(iAperturadolares), "###,###,##0.00"), 27))
  End If

  If (iAperturasoles + iCashMont - iPagoMont + iTotPADES + PADLIQDIAS) <> 0 Then 'Existe dinero Soles
      Call AgregarLineaBloque("---------------------------------------")
      Call AgregarLineaBloque("CASH-DRW S/." + Right(Space(27) + "*" + Format(CStr(iAperturasoles + iCashMont - iPagoMont + iTotPADES - iTotPADEANUS + PADLIQDIAS), "###,###,##0.00"), 27))
  End If

  If (iAperturadolares + iCashMontdol + iTotPADED + PADLIQDIAD) <> 0 Then 'Existe dinero Dolares
    Call AgregarLineaBloque("CASH-DRW$(TC S/." & Right(Space(5) & CStr(nTipoCambio), 5) & ")" & Right(Space(17) & "*" & Format(CStr(iAperturadolares + iCashMontdol + iTotPADED - iTotPADEANUD + PADLIQDIAD), "###,###,##0.00"), 17))
  End If
  If iVoucNume <> 0 Then 'Existe Pago
    Call AgregarLineaBloque("VOUCHER     " + Right(Space(27) + "*" + Format(CStr(iVoucMont), "###,###,##0.00"), 27))
  End If

  If (iTotPADVS + PADVLIQDIAS) <> 0 Then  'Existe Pago Soles
    Call AgregarLineaBloque("PADEV S/.   " + Right(Space(27) + "*" + Format(CStr(iTotPADVS - iTotPADVANUS + PADVLIQDIAS), "###,###,##0.00"), 27))
  End If
  
  If (iTotPADVD + PADVLIQDIAD) <> 0 Then  'Existe Pago Dolares
    Call AgregarLineaBloque("PADEV US$   " + Right(Space(27) + "*" + Format(CStr(iTotPADVD - iTotPADVANUD + PADVLIQDIAD), "###,###,##0.00"), 27))
  End If
  
  'OC ANUULADAS
  Dim ddNuloOC As Double
  Dim dlItemNuloOc As Long
  sSQL = " SELECT sum(o.nmontoSoles) as nulooc, count(*) as item " & _
         " FROM operacionparalela o WHERE o.copercodeanul <> 0 AND cdoctipo='10' " & sCondicionOpeP

  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo LineaError
  ddNuloOC = "0" & (ADORS!nulooc)
  dlItemNuloOc = "0" & (ADORS!Item)
  '-----------------------------------------------------------------------
  If dlItemNuloOc <> 0 Then 'Existe anulada oc
    Call AgregarLineaBloque("NULOS  OC   " + Right(Space(27) + "000000" + CStr(dlItemNuloOc) + "   ", 27) + "   ")
    Call AgregarLineaBloque("            " + Right(Space(27) + "*-" + Format(CStr(ddNuloOC), "###,###,##0.00"), 27))
  End If

  If iVOPONume <> 0 Then 'Existe anulada
    Call AgregarLineaBloque("NULOS  TK   " + Right(Space(27) + "000000" + CStr(iVOPONume) + "   ", 27) + "   ")
    Call AgregarLineaBloque("            " + Right(Space(27) + "*" + Format(CStr(iVOPOMont), "###,###,##0.00"), 27))
  End If
 
 Call AgregarLineaBloque("")
 Select Case sLetraReporte
   Case "Z" 'Cambiar a reporte Z
     'BLOQUE TICKETS EN GENERAL (B Y  F)
     Call AgregarLineaBloque("=======================================")
     Call AgregarLineaBloque("TICKETS ")
     Call AgregarLineaBloque("=======================================")
     Call AgregarLineaBloque("      " + Format(CStr(Now()), "DD/MM/YYYY (DDD)"))
 
     sSQL = " SELECT min(o.cdoccodigo) as mini,max(o.cdoccodigo) as maxi" & _
            " FROM Operacion o WHERE o.cOperTipo in (1,2,3,4,5) and o.cdoctipo IN ('01','02')" & sCondicionOpe
      
     If bParcial Then sSQL = sSQL + " AND cTurnoCodi = '" & kPAR_TURNO & "'"
     
     If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS1) Then GoTo LineaError
      
     Do While Not ADORS1.EOF
        Call AgregarLineaBloque("RANGO [ " + CStr(IIf(IsNull(ADORS1.Fields("mini")), "", ADORS1.Fields("mini"))) + "  ---  " + CStr(IIf(IsNull(ADORS1.Fields("maxi")), "", ADORS1.Fields("maxi"))) + " ]")
        sRangoTKBolCabZ = "RANGO [ " + CStr(IIf(IsNull(ADORS1.Fields("mini")), "", ADORS1.Fields("mini"))) + "  ---  " + CStr(IIf(IsNull(ADORS1.Fields("maxi")), "", ADORS1.Fields("maxi"))) + " ]"
        
        ADORS1.MoveNext
     Loop
     Set ADORS1 = Nothing

     'CUARTO BLOQUE
     Call AgregarLineaBloque("=======================================")
     Call AgregarLineaBloque("=======================================")
     Call AgregarLineaBloque("TICKETS -- BOLETA TOTAL TRANSACC.")
     Call AgregarLineaBloque("=======================================")
     Call AgregarLineaBloque("      " + Format(CStr(Now()), "DD/MM/YYYY (DDD)"))
     
     sSQL = " SELECT COUNT(*)AS TotDoc, sum ((O.nProdCant * O.nProdPrecUnit)+ ((O.nProdCant * O.nProdPrecUnit) * O.nDscto/100))  AS TotMonto " _
          & " FROM Operacion AS O " _
          & " WHERE cOperTipo in (1,2,3) " _
          & "   AND cprodcode <> 0 AND cOperflaganula= 0 AND cdoctipo='02' " & sCondicionOpe

    If bParcial Then sSQL = sSQL + " AND cTurnoCodi = '" & kPAR_TURNO & "'"
      
    If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS1) Then GoTo LineaError
      
    Do While Not ADORS1.EOF
        Call AgregarLineaBloque("TICKET      " + Right(Space(27) + "000000" + CStr(ADORS1.Fields("TotDoc")) + "   ", 27))
        Call AgregarLineaBloque("            " + Right(Space(27) + "*" + Format(CStr(IIf(IsNull(ADORS1.Fields("TotMonto")), "0", ADORS1.Fields("TotMonto"))), "###,###,##0.00"), 27))
        
        sRangoTKBolCabZ = "TICKET      " + Right(Space(27) + "000000" + CStr(ADORS1.Fields("TotDoc")) + "   ", 27) + vbCrLf
        sRangoTKBolCabZ = sRangoTKBolCabZ + "            " + Right(Space(27) + "*" + Format(CStr(IIf(IsNull(ADORS1.Fields("TotMonto")), "0", ADORS1.Fields("TotMonto"))), "###,###,##0.00"), 27) + vbCrLf
        
        ADORS1.MoveNext
    Loop
    Set ADORS1 = Nothing

    'QUINTO BLOQUE
    Call AgregarLineaBloque("=======================================")
    Call AgregarLineaBloque("=======================================")
    Call AgregarLineaBloque("TICKETS -- FACTURA TOTAL TRANSACC.")
    Call AgregarLineaBloque("=======================================")
    Call AgregarLineaBloque("      " + Format(CStr(Now()), "DD/MM/YYYY (DDD)"))
    
    sSQL = "SELECT COUNT(*)AS TotDoc, sum ((O.nProdCant * O.nProdPrecUnit)+ ((O.nProdCant * O.nProdPrecUnit) * O.nDscto/100))  AS TotMonto " _
    & "FROM Operacion AS O " _
    & "WHERE cOperTipo in (1,2,3) " _
    & "AND cprodcode <> 0 AND cOperflaganula= 0 AND cdoctipo='01' " & sCondicionOpe
         
    If bParcial Then sSQL = sSQL + " AND cTurnoCodi = '" & kPAR_TURNO & "'"
      
    If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS1) Then GoTo LineaError
    
    Do While Not ADORS1.EOF
        Call AgregarLineaBloque("TICKET      " + Right(Space(27) + "000000" + CStr(ADORS1.Fields("TotDoc")) + "   ", 27))
        Call AgregarLineaBloque("            " + Right(Space(27) + "*" + Format(CStr(IIf(IsNull(ADORS1.Fields("TotMonto")), "0", ADORS1.Fields("TotMonto"))), "###,###,##0.00"), 27))
        
        sRangoTKBolCabZ = "TICKET      " + Right(Space(27) + "000000" + CStr(ADORS1.Fields("TotDoc")) + "   ", 27) + vbCrLf
        sRangoTKBolCabZ = sRangoTKBolCabZ + "            " + Right(Space(27) + "*" + Format(CStr(IIf(IsNull(ADORS1.Fields("TotMonto")), "0", ADORS1.Fields("TotMonto"))), "###,###,##0.00"), 27) + vbCrLf
        
        ADORS1.MoveNext
    Loop
    Set ADORS1 = Nothing
 
    '--------- NC X VENTA FUTURA
    Call AgregarLineaBloque("=======================================")
    Call AgregarLineaBloque("=======================================")
    Call AgregarLineaBloque("NOTAS DE CONSUMO X VENTA FUTURA ")
    Call AgregarLineaBloque("=======================================")
    Call AgregarLineaBloque("      " + Format(CStr(Now()), "DD/MM/YYYY (DDD)"))
  
    sSQL = "SELECT p.cCodCatalogo as Catal, p.sProdDescCrta as Descrip, Sum(d.nProdCant) AS Cant " _
    & "FROM (OperacionParalela o INNER JOIN OperacionParalelaProducto d ON o.cOperCode = d.cOperCode) INNER JOIN Producto p ON d.cProdCode = p.cProdCode " _
    & "Where o.cdocTipo = '11' And o.cOperCodeAnul=0 " & sCondicionOpeP _
    & "GROUP BY p.cCodCatalogo, p.sProdDescCrta; "

    If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo LineaError
    If ADORS.RecordCount > 0 Then
      Call AgregarLineaBloque("Producto                        Cant.  ")
      Call AgregarLineaBloque("---------------------------------------")
 
      Do While Not ADORS.EOF
        nItems = LeerCampo(ADORS("Cant"))
        catal = LeerCampo(ADORS("Catal"))
        sProdDescCrta = LeerCampo(ADORS("Descrip"))
         
        Call AgregarLineaBloque(Left(catal + Space(10), 10) + " " + Left(sProdDescCrta + Space(17), 17) + " " + Right(Space(10) & Format(CStr(nItems), "###,###,##0.00"), 10))
      
        ADORS.MoveNext
      Loop
      Call AgregarLineaBloque("=======================================")
    End If
 End Select
  
 ReportesConsolidado True, 2
  
 Call AgregarLineaBloque("")
 Call AgregarLineaBloque(" " + Left(psUsuario_Descripcion, 10) + " " + Format(CStr(Now()), "hh:mm") + " ") 'Hora: 6 + 1
 '----------------------------------------------'
 If sLetraReporte = "Z" Then
    Dim iCodZ  As Integer
    sSQL = "SELECT idz + 1 as cod, maxid , Imac " & _
           "  FROM _tmp1 " & _
           " WHERE prn=2"
           
    If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo LineaError
    If Not ADORS.EOF Then
       iCodZ = ADORS!cod
       If iCodZ > ADORS!maxid Then iCodZ = 1
    Else
       iCodZ = 1
    End If
   
    Call AgregarLineaBloque("Código Rep. Z1: " + Right("000000" + CStr(iCodZ), 6))  'Codigo Z
    Call AgregarLineaBloque("Total acumulado: *" + Format(CStr(ADORS!imac + nTotal), "###,###,##0.00"))
    
    Set ADORS = Nothing
    
    sCodigoRepCabZ = "Código Rep. Z1: " + Right("000000" + CStr(iCodZ), 6)
  End If
  
  Call AgregarLineaBloque("CAJA: " + kPAR_CAJA + "   TURNO: " + kPAR_TURNO)
  Call AgregarLineaBloque("Versión " & App.Major & "." & App.Minor & "." & App.Revision)
  Call AgregarLineaBloque("Correlativo: " & ObtenerCorrelativo(2))
  
  Dim rsOpinicio As New ADODB.Recordset
  Dim InicioOP As String

  Dim rsOpPinicio As New ADODB.Recordset
  Dim InicioOPP As String

  Dim rsOpFin As New ADODB.Recordset
  Dim FinOP As String

  Dim rsOpPFin As New ADODB.Recordset
  Dim FinOPP As String

  ' Se Obtiene el IdOperacionInicio y el IdOperacionParalelaInicio
  rsOpinicio.Open "select coperid from Configuracion", ADOCnn, adOpenStatic, adLockOptimistic
  rsOpPinicio.Open "select coperidP from Configuracion", ADOCnn, adOpenStatic, adLockOptimistic

  InicioOP = val(rsOpinicio(0).Value)
  InicioOPP = val(rsOpPinicio(0).Value)

  ' Se Obtiene el IdOperacionFin y el Id OperacionParalelaFin
  rsOpFin.Open "select max(coperid) from operacion", ADOCnn, adOpenStatic, adLockOptimistic
  rsOpPFin.Open "select max(copercode) from operacionParalela", ADOCnn, adOpenStatic, adLockOptimistic

  FinOP = IIf(IsNull(rsOpFin(0).Value), "0", rsOpFin(0).Value)
  FinOPP = IIf(IsNull(rsOpPFin(0).Value), "0", rsOpPFin(0).Value)
  
  'GRABANDO EL Z
  If sLetraReporte = "Z" Then
    Dim STRSQL As String
    'STRSQL = "INSERT INTO CabeceraZ(IdZ,Caja,CantidadTotal,MontoTotal,Fecha,RangoTKBOL,RangoTKFAC,CodigoRep) VALUES (" & nCodCabZAutogenerado & ",'" & sCajaCabZ & "'," & nCantTotalCabZ & "," & nMontoTotalCabZ & ",'" & Format(dFechaCabZ, "yyyy-mm-dd") & "','" & sRangoTKBolCabZ & "','" & sRangoTKFacCabZ & "','" & sCodigoRepCabZ & "')"
    STRSQL = "INSERT INTO CabeceraZ(IdZ,Caja,CantidadTotal,MontoTotal,Fecha,RangoTKBOL,RangoTKFAC,CodigoRep,cOperIdIni,cOperIdFin,cOperIdPIni,cOperIdPFin) VALUES (" & nCodCabZAutogenerado & ",'" & sCajaCabZ & "'," & nCantTotalCabZ & "," & nMontoTotalCabZ & ",'" & Format(dFechaCabZ, "yyyy-mm-dd") & "','" & sRangoTKBolCabZ & "','" & sRangoTKFacCabZ & "','" & sCodigoRepCabZ & "'," & InicioOP & ", " & FinOP & ", " & InicioOPP & ", " & FinOPP & ")"
    ADOCnn.Execute STRSQL
     
    If bHayDetalleZ = True Then
       Dim indice As Integer
       For indice = 1 To UBound(ArregloZ)
          STRSQL = "INSERT INTO DetalleZ(IdZ,Caja,nItem,CodProd,DscProd,Cantidad) VALUES (" & nCodCabZAutogenerado & ",'" & sCajaCabZ & "'," & indice & "," & ArregloZ(indice).CodProd & ",'" & ArregloZ(indice).DscProd & "'," & ArregloZ(indice).Cantidad & ")"
          ADOCnn.Execute STRSQL
       Next
    End If
  End If
  
 ConfiguraImpresora kPAR_IMPRESORA_ALTERNA
 Select Case KImpresoraALT
    Case 1
        Call CortePapelGrande(sImprimir, 2, iNroBloques, "RptX1OZ1")
    Case 0
        For iNroFilas = 0 To iNroBloques
          printer.Print sImprimir(iNroFilas)
        Next
        
        printer.Print " "
        printer.Print " "
        printer.EndDoc
  End Select
 
  bCerrar = True
  
  Select Case sLetraReporte
    Case "X"
    Case "Z"
'      kPAR_TURNO = ""
'        ProcesoCaja False
    End Select
    'ACTUALIZA LOS DATOS EN CONFIGURACION
  '-------------------------------------
  
  If sLetraReporte = "Z" And intentos = 1 Then
    sSQL = " UPDATE _Tmp1 SET idz=" & CStr(iCodZ) & _
           " , Imac= Imac + " & CStr(nTotal) & _
           " WHERE prn=2 "
    Call oDBAdmin.EjecutarSentencia(ADOCnn, sSQL)
    
    sSQL = "INSERT INTO _Tmp (idz, tvd, fec, prn)" & _
            " VALUES(" & CStr(iCodZ) & " , " & CStr(nTotal) & ", '" & sFecha & "', 2 )"

    Call oDBAdmin.EjecutarSentencia(ADOCnn, sSQL)
  End If
  '-------------------------------------------
  Screen.MousePointer = vbDefault
  
  GoTo Salir
  
LineaError:
  If intentos = 3 Then
     MsgBox "No fue posible imprimir reporte" + sLetraReporte, vbOKOnly + vbInformation, "Error"
     intentos = 0
  Else
    intentos = intentos + 1
    ReportesEspeciales1 iTipoReporte, sLetraReporte, bParcial, bOC
    
  End If
  
Salir:
  Set ADORS = Nothing
  Set ADORS1 = Nothing
  Set cnSQL = Nothing
  Screen.MousePointer = vbDefault
End Sub

Sub ReportesConsolidado(Optional bParcial As Boolean = False, Optional Impres As Integer)
  On Error GoTo LineaError

  Screen.MousePointer = vbHourglass
  Dim a As String
  Dim sSQL As String
  Dim ADORS As ADODB.Recordset
  Dim ADORS1 As ADODB.Recordset
  Dim i As Long, j As Long
  Dim iInicio As Long, iFin As Long
  Dim sTecla1 As String, sTecla2 As String, sTecla3 As String
  Dim cnSQL As ADODB.Connection
  
  Dim nTipoCambio As Double
  Dim nTotal As Double
  Dim nTotal1 As Double
  Dim nTotalItems As Long
  Dim nTotalPrecUnit As Double
  Dim nTotalTransac As Double
  Dim nprectransac As Double
  Dim nSubtotal As Double
  Dim nSubtotal1 As Double
  Dim nPorcTotal As Double
  Dim p1 As Double
  Dim p2 As Double
  Dim nItems As Long
  Dim nPrecUnit As Double
  Dim nPrecUnitOrig As Double
  Dim sProdDescCrta As String
  Dim sTeclDesc As String
 ' Dim sTexto As String
  Dim sUsuario As String
  Dim iCodOper As Long
  Dim sFecha As String
  Dim iCambioDolares As Double
  Dim iCodOperAutogenerado As Long
  Dim iserv_b As Double
  Dim iserv_f As Double
  Dim igv_b As Double
  Dim igv_f As Double
  Dim porc As Double
  Dim igv_b_SQL As Double
  Dim igv_f_SQL As Double
  
  igv_b_SQL = 0
  igv_f_SQL = 0
  
  'Montos por transaccion - Numero transac
  Dim iCashMont As Double, iCashNume As Long 'Transaccion Cash
  Dim iVoucMont As Double, iVoucNume As Long 'Transaccion Voucher
  Dim iDolaMont As Double, iDolaNume As Long 'Transaccion Dolares
  Dim iAnulMont As Double, iAnulNume As Long 'Transaccion Anulado
  Dim iPagoMont As Double, iPagoNume As Long  'Transaccion Pago
  Dim iVOPOMont As Double, iVOPONume As Long 'Transaccion VO-POSTN
  Dim dDscto As Double
  Dim cOperId As String
  Dim cOperIdP As String
  Dim sCondicionOpe As String
  Dim sCondicionOpeP As String
  
  sSQL = "SELECT cOperId, cOperIdP FROM Configuracion"
  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then
    cOperId = ""
    cOperIdP = ""
  Else
    cOperId = LeerCampo(ADORS("cOperId"))
    cOperIdP = LeerCampo(ADORS("cOperIdP"))
  End If
  
  If cOperId = "" Or cOperIdP = "" Then
    sCondicionOpe = "   and o.cOperflagabierto = 1 "
    sCondicionOpeP = "   and o.cOperflagabierto = 1 "
  Else
    sCondicionOpe = "   and o.cOperId > " + cOperId + " "
    sCondicionOpeP = "   and o.cOperCode > " + cOperIdP + " "
  End If
  
  'Calcula Totales SERVICIO FACTURA
  sSQL = " SELECT SUM(((o.nProdCant * o.nProdPrecUnit) + (o.nProdCant * o.nProdPrecUnit * o.nDscto/100)) / (1 + " & kIGV & " +  (o.nServicio /100) )  * (o.nServicio/100) ) as Servicio,  " & _
         "        SUM(((o.nProdCant * o.nProdPrecUnit) + (o.nProdCant * o.nProdPrecUnit * o.nDscto/100)) / (1 + " & kIGV & " + (o.nServicio / 100)) * " & kIGV & ") As Igv " & _
         "   FROM Operacion o " & _
         "   WHERE o.cDoctipo= '01' " & _
         "   AND o.cOperFlaganula= 0 " & _
         "   AND o.cProdcode <> 0 " & sCondicionOpe
         
  If bParcial Then sSQL = sSQL + " AND o.cTurnoCodi = '" & kPAR_TURNO & "'"
     
  igv_f = 0
  iserv_f = 0
  
  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo Salir
  If Not ADORS.EOF Then
    iserv_f = CDbl(IIf(IsNull(ADORS!servicio), 0, ADORS!servicio))
    igv_f = CDbl(IIf(IsNull(ADORS!Igv), 0, ADORS!Igv))
  End If

    Set ADORS = Nothing

 'Calcula Totales SERVICIO BOLETAS
 sSQL = " SELECT SUM(((o.nProdCant * o.nProdPrecUnit) + (o.nProdCant * o.nProdPrecUnit * o.nDscto/100)) / (1 + " & kIGV & " +  (o.nServicio /100) )  * (o.nServicio/100) ) as Servicio,  " & _
         "       SUM(((o.nProdCant * o.nProdPrecUnit) + (o.nProdCant * o.nProdPrecUnit * o.nDscto/100)) / (1 + " & kIGV & " + (o.nServicio / 100)) * " & kIGV & ") As Igv " & _
         "   FROM Operacion o " & _
         "   WHERE o.cDoctipo= '02' " & _
         "   AND o.cOperFlaganula= 0 " & _
         "   AND o.cProdcode <> 0 " & sCondicionOpe
         
  If bParcial Then sSQL = sSQL + " AND o.cTurnoCodi = '" & kPAR_TURNO & "'"
     
  igv_b = 0
  iserv_b = 0
  
  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo Salir
  If Not ADORS.EOF Then
    iserv_b = CDbl(IIf(IsNull(ADORS!servicio), 0, ADORS!servicio))
    igv_b = CDbl(IIf(IsNull(ADORS!Igv), 0, ADORS!Igv))
  End If

   Set ADORS = Nothing
   
   Dim sBolCaja, sFacCaja As String
   sBolCaja = "B" + Right(Trim(kPAR_CAJA), 2)
   sFacCaja = "F" + Right(Trim(kPAR_CAJA), 2)
   
   If Not oDBAdmin.AbrirBD(cnSQL, psConnectString) Then GoTo Salir 'ABRE SQL OMNISYS
   
   sSQL = "SELECT TipoDoc, sum(igv)  AS SumaIgv FROM VENTA01 WHERE cSerieDoc ='" & sBolCaja & "' or  cSerieDoc ='" & sFacCaja & "'  GROUP BY TipoDoc"
   
   If Not oDBAdmin.EjecutarConsulta(cnSQL, sSQL, ADORS1) Then GoTo LineaError
   Do While Not ADORS1.EOF
        If ADORS1!TipoDoc = "01" Then
            igv_f_SQL = ADORS1!SumaIgv
        ElseIf ADORS1!TipoDoc = "02" Then
            igv_b_SQL = ADORS1!SumaIgv
        End If
        ADORS1.MoveNext
   Loop
     Set ADORS1 = Nothing
  'PRIMER BLOQUE
  Call AgregarLineaBloque("=======================================")
  Call AgregarLineaBloque("        IGV FACTURA        ")
  Call AgregarLineaBloque("=======================================")
  Call AgregarLineaBloque(" TOTAL    " + Right(Space(28) + "*" + Format(CStr(igv_f + igv_f_SQL), "###,###,##0.00"), 29))
  
   
  'SEGUNDO BLOQUE
  Call AgregarLineaBloque("=======================================")
  Call AgregarLineaBloque("        IGV BOLETA         ")
  Call AgregarLineaBloque("=======================================")
  Call AgregarLineaBloque(" TOTAL    " + Right(Space(28) + "*" + Format(CStr(igv_b + igv_b_SQL), "###,###,##0.00"), 29))
  
  p2 = CDbl(Format(CStr(igv_b + igv_b_SQL), "###,###,##0.00"))
  
  'TERCER BLOQUE
  Call AgregarLineaBloque("=======================================")
  Call AgregarLineaBloque("        TOTAL IGV          ")
  Call AgregarLineaBloque("=======================================")
  Call AgregarLineaBloque(" TOTAL    " + Right(Space(28) + "*" + Format(CStr(igv_f + igv_f_SQL + igv_b + igv_b_SQL), "###,###,##0.00"), 29))
 
  
  Call AgregarLineaBloque("=======================================")
  Call AgregarLineaBloque("     SERVICIOS ( " + kPAR_PORCSERV + " % )       ")
  Call AgregarLineaBloque("=======================================")
  Call AgregarLineaBloque(" TOTAL    " + Right(Space(28) + "*" + Format(CStr(iserv_b + iserv_f), "###,###,##0.00"), 29))
  Call AgregarLineaBloque("=======================================")

  If Impres = 2 Then
    sSQL = "  SELECT SUM(((nProdCant * nProdPrecUnit) + (nProdCant * nProdPrecUnit * nDscto/100)) / (1 + (" & kIGV & " +  (nServicio /100) ) ) * (nServicio/100)) as Servicio  " & _
           "    FROM OperacionParalelaProducto p , OperacionParalela o " & _
           "   WHERE p.cOpercode = o.cOpercode " & _
           "     AND o.cOperTipo= 20 and cProdCode <> 0 " & _
           "     and  o.cOpercodeAnul=0 " & sCondicionOpeP
         
    If bParcial Then sSQL = sSQL + " AND cTurnoCodi = '" & kPAR_TURNO & "'"
     
    nItems = 0
    nSubtotal = 0
    If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo Salir
    If Not ADORS.EOF Then
      nSubtotal = CDbl(IIf(IsNull(ADORS!servicio), 0, ADORS!servicio))
    End If

    Set ADORS = Nothing
  
    'CUARTO BLOQUE
    Call AgregarLineaBloque("=======================================")
    Call AgregarLineaBloque("     SERV PARALELO( " + kPAR_PORCSERV + " % )       ")
    Call AgregarLineaBloque("=======================================")
    Call AgregarLineaBloque(" TOTAL    " + Right(Space(28) + "*" + Format(CStr(nSubtotal), "###,###,##0.00"), 29))
    Call AgregarLineaBloque("=======================================")
  End If
  
  GoTo Salir
  
LineaError:
  MsgBox Err.Description, vbOKOnly + vbInformation
  
Salir:
  Set ADORS = Nothing
  Set ADORS1 = Nothing
  Set cnSQL = Nothing
  Screen.MousePointer = vbDefault
End Sub

Public Function ReemplazarComilla(sCadena) As String

  On Error GoTo LineaError

  Dim i As Integer
  Dim sValor As String
  
  sValor = sCadena
  
  If Trim(sValor) <> "" Then
    sValor = Replace(sCadena, "'", "' + Chr(39) + '")
  End If
  
  ReemplazarComilla = sValor
  
  Exit Function
  
LineaError:
  MsgBox Err.Description, vbOKOnly + vbInformation
    
End Function
Public Sub ExportaX1aExcel()

  Dim lRegistros As Long
  Dim l As Long

  Dim sSQL As String
  Dim ADORS As ADODB.Recordset

  Dim xlApp As Excel.Application
  Dim xlBook As Excel.Workbook
  Dim xlSheet As Excel.Worksheet
  Dim xlSheetTemp As Excel.Worksheet
  
  Dim lFila As Long
  Dim i As Long
  '------------------------------
  Dim descuento As Double
  Dim nTotalTransac  As Double
  Dim iCashMont As Double
  Dim iCashNume As Double
  Dim iVoucMont As Double
  Dim iVoucNume As Double
  Dim iDolaMont As Double
  Dim iDolaNume As Double
  Dim iPagoMont As Double
  Dim iPagoNume As Double
  Dim nSubtotal As Double
  Dim nTotal As Double
  Dim descuentos As Double
  Dim npreciounit As Double
  Dim cOperId As String
  Dim cOperIdP As String
  Dim sCondicionOpe As String
  Dim sCondicionOpeP As String
  '------------------------------
  Screen.MousePointer = vbHourglass
  
  Set xlApp = CreateObject("excel.application")
  Set xlBook = xlApp.Workbooks.Add
  Set xlSheet = xlBook.Worksheets.Add
  
  xlSheet.Name = "Reporte X - Detallado "
  
  xlBook.Application.DisplayAlerts = False
  
  For Each xlSheetTemp In xlBook.Worksheets
    If xlSheetTemp.Name <> xlSheet.Name Then
      xlSheetTemp.Delete
    End If
  Next xlSheetTemp
  
  xlBook.Application.DisplayAlerts = True
  
  'Formato Página
  xlSheet.PageSetup.Orientation = xlPortrait
  xlSheet.PageSetup.LeftMargin = Application.InchesToPoints(0.3)
  xlSheet.PageSetup.RightMargin = Application.InchesToPoints(0.3)
  xlSheet.PageSetup.TopMargin = Application.InchesToPoints(0.5)
  xlSheet.PageSetup.BottomMargin = Application.InchesToPoints(0.5)
  xlSheet.PageSetup.HeaderMargin = Application.InchesToPoints(0.1)
  xlSheet.PageSetup.FooterMargin = Application.InchesToPoints(0.1)
  xlSheet.PageSetup.PrintTitleRows = xlSheet.Rows("1:5").Address
    
  'Cabecera / Pie de Página
  xlSheet.PageSetup.RightHeader = "&6" + "OmniPos - Caja Registradora"
  xlSheet.PageSetup.RightFooter = "&6página &P"
  
  'Columna
  xlSheet.Columns(1).ColumnWidth = 5
  xlSheet.Columns(2).ColumnWidth = 5
  xlSheet.Columns(3).ColumnWidth = 8
  xlSheet.Columns(4).ColumnWidth = 25
  xlSheet.Columns(5).ColumnWidth = 10
  xlSheet.Columns(6).ColumnWidth = 10
  xlSheet.Columns(7).ColumnWidth = 15
  
  lFila = 1
  xlSheet.Cells(lFila, 4).Value = "Operaciones de Venta "
  lFila = lFila + 1
  
'----------------Adicionando Hoja de detalle ------------------------
  xlSheet.Cells(lFila, 1).Value = "Tipo Doc."
  xlSheet.Cells(lFila, 2).Value = "Serie"
  xlSheet.Cells(lFila, 3).Value = "Número"
  xlSheet.Cells(lFila, 4).Value = "Articulo"
  xlSheet.Cells(lFila, 5).Value = "Cantidad"
  xlSheet.Cells(lFila, 6).Value = "Precio Unit."
  xlSheet.Cells(lFila, 7).Value = "Sub-Total"
  xlSheet.Cells(lFila, 8).Value = "Operación"
  lFila = lFila + 1
  
  sSQL = "SELECT cOperId, cOperIdP FROM Configuracion"
  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then
    cOperId = ""
    cOperIdP = ""
  Else
    cOperId = LeerCampo(ADORS("cOperId"))
    cOperIdP = LeerCampo(ADORS("cOperIdP"))
  End If
  
  If cOperId = "" Or cOperIdP = "" Then
    sCondicionOpe = "   and o.cOperflagabierto = 1 "
    sCondicionOpeP = "   and o.cOperflagabierto = 1 "
  Else
    sCondicionOpe = "   and o.cOperId > " + cOperId + " "
    sCondicionOpeP = "   and o.cOperCode > " + cOperIdP + " "
  End If
    
 '-----------------Operaciones ------------------------------
 sSQL = " SELECT  cOperTipo,  p.sProdDescCrta, P.nProdPrecUnit," & _
       " o.nProdCant, o.cDocSerie, o.cDocCodigo, o.cDocTipo, o.cOpercodeAnula, " & _
       " o.nProdCant * o.nProdPrecUnit AS TotalMonto, cemisor " & _
       " FROM operacion o " & _
       " Left Outer Join producto p " & _
       "  on o.cprodcode= p.cprodcode " & _
       " WHERE cdoctipo in ('01', '02') and o.cprodcode <> 0 " & sCondicionOpe & _
       " AND not copercode in (select copercodeanula from operacion where cOperflaganula=1 )"
   
    
  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then Exit Sub
    
  Dim TipoOperacion As String
  
   Do While Not ADORS.EOF
      If val("0" & (ADORS!cOperCodeAnula)) <> 0 And LeerCampo(ADORS!cOperTipo) <> 3 Then
        TipoOperacion = "ANULACION"
      Else
        Select Case LeerCampo(ADORS!cOperTipo)
          Case 1
              TipoOperacion = "VTA EN EFECTIVO"
          Case 2
              TipoOperacion = "VOUCHER"
          Case 3
              If Trim(LeerCampo(ADORS!cEmisor)) <> "" Then
                TipoOperacion = "LIQUIDACION con voucher "
              Else
                TipoOperacion = "LIQUIDACION  "
              End If
          Case 5
              TipoOperacion = "PAGO"
          Case Else
              TipoOperacion = "OTROS"
        End Select
     End If
     
      xlSheet.Cells(lFila, 1) = IIf(LeerCampo(ADORS!cdocTipo) = "01", "FAC", "BOL")
      xlSheet.Cells(lFila, 2) = LeerCampo(ADORS!cDocSerie)
      xlSheet.Cells(lFila, 3) = Right("00000000" & LeerCampo(ADORS!cDocCodigo), 7)
      xlSheet.Cells(lFila, 4) = LeerCampo(ADORS!sProdDescCrta)
      xlSheet.Cells(lFila, 5) = (ADORS!nProdCant)
      xlSheet.Cells(lFila, 6) = (ADORS!nProdPrecUnit)
      xlSheet.Cells(lFila, 7) = (ADORS!TotalMonto)
      xlSheet.Cells(lFila, 8) = TipoOperacion
      lFila = lFila + 1
      ADORS.MoveNext
   Loop
   
 '-------------Operaciones Paralelas-------------------------
  lFila = lFila + 1
  xlSheet.Cells(lFila, 4).Value = "Operaciones Paralelas "
  lFila = lFila + 1
  xlSheet.Cells(lFila, 1).Value = "Tipo Doc."
  xlSheet.Cells(lFila, 2).Value = "Serie"
  xlSheet.Cells(lFila, 3).Value = "Número"
  xlSheet.Cells(lFila, 4).Value = "Articulo"
  xlSheet.Cells(lFila, 5).Value = "Cantidad"
  xlSheet.Cells(lFila, 6).Value = "Precio Unit."
  xlSheet.Cells(lFila, 7).Value = "Sub-Total"
  xlSheet.Cells(lFila, 8).Value = "Operación"
  lFila = lFila + 1
    
   
sSQL = "SELECT a.cEstado, a.cOperTipo, a.cDocSerie, a.cDocCodigo, a.cDocTipo,a.cOperCodeAnul, " & _
     "d.nProdCant AS  sProdCant ,  d.nProdPrecUnit  AS Precio, d.nDscto, " & _
     "p.sproddesccrta as producto ,  p.cprodcode,  p.ccodcatalogo AS Codigo " & _
     "FROM  operacionparalelaproducto d, operacionparalela a , producto p " & _
     "WHERE d.copercode in " & _
     "      (select o.copercode  FROM OperacionParalela o " & _
     "       WHERE 1=1 " & sCondicionOpeP & " ) " & _
     "AND d.cOpercode=a.COperCode and   d.cProdCode=p.cProdCode and  a.cdoccodigo <> 0 " & _
     "AND a.cOpertipo in (20,21,24) "
        
If oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then
  
  Do While Not ADORS.EOF
      If val("0" & (ADORS!copercodeanul)) <> 0 Then
        TipoOperacion = "ANULACION"
      Else
        Select Case LeerCampo(ADORS!cOperTipo)
          Case 20
              Select Case LeerCampo(ADORS!cEstado)
               Case "0"
                 TipoOperacion = "O/C ANULADA"
               Case Else
                 TipoOperacion = "ORDEN DE CONSUMO"
               End Select
              
          Case 21
              Select Case LeerCampo(ADORS!cEstado)
               Case "0"
                 TipoOperacion = "NC ANULADA"
               Case "1"
                 TipoOperacion = "NC SIN LIQUIDAR "
               Case "2"
                 TipoOperacion = "NC LIQUIDADAS"
                Case Else
                 TipoOperacion = "NOTA CONSUMO"
              End Select
          Case Else
              TipoOperacion = "OTROS"
        End Select
     End If
       descuento = val(LeerCampo(ADORS("Precio"))) * val(("0" & ADORS("nDscto"))) / 100
       npreciounit = val(LeerCampo(ADORS("Precio"))) - descuento
       xlSheet.Cells(lFila, 1) = IIf(LeerCampo(ADORS!cdocTipo) = "11", "NC", "")
       xlSheet.Cells(lFila, 2) = LeerCampo(ADORS!cDocSerie)
       xlSheet.Cells(lFila, 3) = Right("0000000" & LeerCampo(ADORS!cDocCodigo), 7)
       xlSheet.Cells(lFila, 4) = LeerCampo(ADORS!Producto)
       If LeerCampo(ADORS!cEstado) = "0" Then
        xlSheet.Cells(lFila, 5) = "0"
        xlSheet.Cells(lFila, 6) = "0"
        xlSheet.Cells(lFila, 7) = "0"
        xlSheet.Cells(lFila, 8) = TipoOperacion
       Else
        xlSheet.Cells(lFila, 5) = (ADORS!sProdCant)
        xlSheet.Cells(lFila, 6) = npreciounit
        xlSheet.Cells(lFila, 7) = npreciounit * ("0" & (ADORS!sProdCant))
        xlSheet.Cells(lFila, 8) = TipoOperacion
       End If
       lFila = lFila + 1
       ADORS.MoveNext
    Loop
  lFila = lFila + 1
  End If
      '--------------------------PADE Y PADV----------------------------------
   sSQL = " SELECT o.cDocserie, o.cDoccodigo, o.cOperCodeAnul, o.cOperCode, o.cOperTipo, o.cestado, o.nTarjNumb, o.sGlosa, o.nMontosoles FROM OperacionParalela o " & _
         " WHERE o.cOperTipo in (22,23) and o.cEstado in ('1') " & sCondicionOpeP
             
    
  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then Exit Sub
  
   
  If ADORS.RecordCount > 0 Then
    xlSheet.Cells(lFila, 2) = LeerCampo(ADORS!cDocSerie)
    xlSheet.Cells(lFila, 3) = Right("0000000" & LeerCampo(ADORS!cDocCodigo), 7)
    xlSheet.Cells(lFila, 4).Value = "Glosa"
    xlSheet.Cells(lFila, 5).Value = "Tarjeta"
    xlSheet.Cells(lFila, 6).Value = "Importe"
    lFila = lFila + 1
    Do While Not ADORS.EOF
         If val("0" & (ADORS!copercodeanul)) <> 0 Then
            TipoOperacion = "ANULACION"
         Else
          Select Case LeerCampo(ADORS!cOperTipo)
          Case 22
              Select Case LeerCampo(ADORS!cEstado)
               Case "0"
                 TipoOperacion = "PADE ANULADO"
               Case 1
                 TipoOperacion = "PADE "
               Case 2
                 TipoOperacion = "PADE LIQUIDADO"
               End Select
              
          Case 23
              Select Case LeerCampo(ADORS!cEstado)
               Case "0"
                 TipoOperacion = "PADV ANULADA"
               Case "1"
                 TipoOperacion = "PADV "
               Case "2"
                 TipoOperacion = "PADV LIQUIDADO"
                Case Else
                 TipoOperacion = "PADV"
              End Select
          Case Else
              TipoOperacion = "OTROS"
        End Select
     End If
      xlSheet.Cells(lFila, 2) = LeerCampo(ADORS!cDocSerie)
      xlSheet.Cells(lFila, 3) = Right("0000000" & LeerCampo(ADORS!cDocCodigo), 7)
      xlSheet.Cells(lFila, 4) = LeerCampo(ADORS!sGlosa)
      xlSheet.Cells(lFila, 5) = LeerCampo(ADORS!nTarjNumb)
      xlSheet.Cells(lFila, 6) = val("0" & (ADORS!nMontoSoles))
      xlSheet.Cells(lFila, 8) = TipoOperacion
      lFila = lFila + 1
      ADORS.MoveNext
   Loop
   End If
   '------------------------PADE LIQUIDADOS EN EL DIA ------------------------------------------
  sSQL = " SELECT o.cDocserie, o.cDoccodigo, o.cOperCode, o.cOperTipo, o.nTarjNumb, o.sGlosa, o.nMontosoles FROM OperacionParalela o " & _
         " WHERE o.cOperTipo in (22) and o.cEstado in ('2') " & _
         " AND o.cOpercodeAnul = 0 " & sCondicionOpeP
       
  
  If oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then
  lFila = lFila + 1
  Do While Not ADORS.EOF
     xlSheet.Cells(lFila, 2) = LeerCampo(ADORS!cDocSerie)
     xlSheet.Cells(lFila, 3) = Right("0000000" & LeerCampo(ADORS!cDocCodigo), 7)
     xlSheet.Cells(lFila, 4) = LeerCampo(ADORS!sGlosa)
     xlSheet.Cells(lFila, 5) = LeerCampo(ADORS!nTarjNumb)
     xlSheet.Cells(lFila, 6) = Format(CStr(ADORS!nMontoSoles), "###,###,##0.00")
     xlSheet.Cells(lFila, 8) = "PADE LIQ Dia"
     
     lFila = lFila + 1
     ADORS.MoveNext
  Loop
  End If
  
  '---------------PADES LIQUIDADOS y CERRADOS--------------------------------------------------------
  sSQL = " SELECT cDocserie, cDoccodigo, cOperCode, cOperTipo, nTarjNumb, sGlosa, nMontosoles FROM OperacionParalela " & _
         " WHERE cOperTipo in (22) and cOperFlagAbierto=0 and cEstado in ('2') " & _
         " AND cOperCodeRela in (SELECT o.cOperCode from OperacionParalela o WHERE 1 = 1 " & sCondicionOpeP & ")" & _
         " AND cOpercodeAnul = 0 "
     
  If oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then
  lFila = lFila + 1
  Do While Not ADORS.EOF
     xlSheet.Cells(lFila, 2) = LeerCampo(ADORS!cDocSerie)
     xlSheet.Cells(lFila, 3) = Right("0000000" & LeerCampo(ADORS!cDocCodigo), 7)
     xlSheet.Cells(lFila, 4) = LeerCampo(ADORS!sGlosa)
     xlSheet.Cells(lFila, 5) = LeerCampo(ADORS!nTarjNumb)
     xlSheet.Cells(lFila, 6) = Format(CStr(ADORS!nMontoSoles), "###,###,##0.00")
     xlSheet.Cells(lFila, 8) = "PADE LIQ Dias anteriores"
    
     lFila = lFila + 1
     ADORS.MoveNext
  Loop
  End If
  
  '------------------------PADV LIQUIDADOS EN EL DIA ------------------------------------------
  sSQL = " SELECT o.cDocserie, o.cDoccodigo, o.cOperCode, o.cOperTipo, o.nTarjNumb, o.sGlosa, o.nMontosoles FROM OperacionParalela o " & _
         " WHERE o.cOperTipo in (23) and o.cEstado in ('2') " & _
         " AND o.cOpercodeAnul = 0 " & sCondicionOpeP
       
  
  If oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then
  
  Do While Not ADORS.EOF
     xlSheet.Cells(lFila, 2) = LeerCampo(ADORS!cDocSerie)
     xlSheet.Cells(lFila, 3) = Right("0000000" & LeerCampo(ADORS!cDocCodigo), 7)
     xlSheet.Cells(lFila, 4) = LeerCampo(ADORS!sGlosa)
     xlSheet.Cells(lFila, 5) = LeerCampo(ADORS!nTarjNumb)
     xlSheet.Cells(lFila, 6) = Format(CStr(ADORS!nMontoSoles), "###,###,##0.00")
     xlSheet.Cells(lFila, 8) = "PADV LIQ Dia"
     
     lFila = lFila + 1
     ADORS.MoveNext
  Loop
  End If
   
  '---------------PADVS LIQUIDADOS y CERRADOS--------------------------------------------------------
  sSQL = " SELECT cDocserie, cDoccodigo, cOperCode, cOperTipo, nTarjNumb, sGlosa, nMontosoles FROM OperacionParalela " & _
         " WHERE cOperTipo in (23) and cOperFlagAbierto=0 and cEstado in ('2') " & _
         " AND cOperCodeRela in (SELECT o.cOperCode from OperacionParalela o WHERE 1 = 1 " & sCondicionOpeP & ")" & _
         " AND cOpercodeAnul = 0 "
  If oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then
  
  Do While Not ADORS.EOF
     xlSheet.Cells(lFila, 2) = LeerCampo(ADORS!cDocSerie)
     xlSheet.Cells(lFila, 3) = Right("0000000" & LeerCampo(ADORS!cDocCodigo), 7)
     xlSheet.Cells(lFila, 4) = LeerCampo(ADORS!sGlosa)
     xlSheet.Cells(lFila, 5) = LeerCampo(ADORS!nTarjNumb)
     xlSheet.Cells(lFila, 6) = Format(CStr(ADORS!nMontoSoles), "###,###,##0.00")
     xlSheet.Cells(lFila, 8) = "PADV LIQ Dias anteriores"
    
     lFila = lFila + 1
     ADORS.MoveNext
  Loop
   
  End If
  
 '---------------------------------------- ANULADOS ------------------------------
  sSQL = " SELECT  cOperTipo,  p.sProdDescCrta, P.nProdPrecUnit," & _
       " o.nProdCant, o.cDocSerie, o.cDocCodigo, o.cDocTipo, o.cOpercodeAnula, " & _
       " o.nProdCant * o.nProdPrecUnit AS TotalMonto, cemisor " & _
       " FROM operacion o " & _
       " Left Outer Join producto p " & _
       "  on o.cprodcode= p.cprodcode " & _
       " WHERE cdoctipo in ('01', '02') and o.cprodcode <> 0 " & sCondicionOpe & _
       " AND copercode in (select copercodeanula from operacion where cOperflaganula=1 )"
       
If oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then
   Do While Not ADORS.EOF
      If val("0" & (ADORS!cOperCodeAnula)) <> 0 And LeerCampo(ADORS!cOperTipo) <> 3 Then
        TipoOperacion = "ANULACION"
      Else
        Select Case LeerCampo(ADORS!cOperTipo)
          Case 1
              TipoOperacion = "VTA EN EFECTIVO Anulada"
          Case 2
              TipoOperacion = "VOUCHER Anulada "
          Case 3
              If Trim(LeerCampo(ADORS!cEmisor)) <> "" Then
                TipoOperacion = "LIQUIDACION con voucher Anulada"
              Else
                TipoOperacion = "LIQUIDACION  Anulada"
              End If
          Case 5
              TipoOperacion = "PAGO Anulado"
          Case Else
              TipoOperacion = "OTROS Anulados"
        End Select
     End If
     
      xlSheet.Cells(lFila, 1) = IIf(LeerCampo(ADORS!cdocTipo) = "01", "FAC", "BOL")
      xlSheet.Cells(lFila, 2) = LeerCampo(ADORS!cDocSerie)
      xlSheet.Cells(lFila, 3) = Right("00000000" & LeerCampo(ADORS!cDocCodigo), 7)
      xlSheet.Cells(lFila, 4) = LeerCampo(ADORS!sProdDescCrta)
      xlSheet.Cells(lFila, 5) = (ADORS!nProdCant)
      xlSheet.Cells(lFila, 6) = (ADORS!nProdPrecUnit)
      xlSheet.Cells(lFila, 7) = (ADORS!TotalMonto)
      xlSheet.Cells(lFila, 8) = TipoOperacion
      lFila = lFila + 1
      ADORS.MoveNext
   Loop
End If

  '---------------- Anulados paralelos ----------------------------------------------------------
sSQL = " SELECT o.cEstado, o.cOperTipo, o.cDocSerie, o.cDocCodigo, o.cDocTipo,o.cOperCodeAnul, o.sglosa, o.nmontosoles" & _
       "   FROM Operacionparalela o " & _
       " WHERE o.copercodeanul <>  0 AND not o.cOpertipo in (20,21,24) " & sCondicionOpeP
     
    If oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then
    Do While Not ADORS.EOF
    
       Select Case LeerCampo(ADORS!cOperTipo)
          Case 22
               TipoOperacion = "PADE ANULADO"
              
          Case 23
                TipoOperacion = "PADV   ANULADO"
           Case Else
              TipoOperacion = "OTROS"
        End Select
          
      xlSheet.Cells(lFila, 2) = LeerCampo(ADORS!cDocSerie)
      xlSheet.Cells(lFila, 3) = Right("00000000" & LeerCampo(ADORS!cDocCodigo), 7)
      xlSheet.Cells(lFila, 4) = LeerCampo(ADORS!sGlosa)
      xlSheet.Cells(lFila, 6) = (ADORS!nMontoSoles)
      xlSheet.Cells(lFila, 8) = TipoOperacion
      lFila = lFila + 1
      ADORS.MoveNext
     Loop
   End If
  '-------------Adicionando hoja Consolidada - Operacion-----------------------------------------------
  lFila = 1
  Set xlSheet = xlBook.Worksheets.Add
  
  xlSheet.Name = "Reporte X - Consolidado "
 
  'Formato Página
  xlSheet.PageSetup.Orientation = xlPortrait
  xlSheet.PageSetup.LeftMargin = Application.InchesToPoints(0.3)
  xlSheet.PageSetup.RightMargin = Application.InchesToPoints(0.3)
  xlSheet.PageSetup.TopMargin = Application.InchesToPoints(0.5)
  xlSheet.PageSetup.BottomMargin = Application.InchesToPoints(0.5)
  xlSheet.PageSetup.HeaderMargin = Application.InchesToPoints(0.1)
  xlSheet.PageSetup.FooterMargin = Application.InchesToPoints(0.1)
  xlSheet.PageSetup.PrintTitleRows = xlSheet.Rows("1:5").Address
    
  'Cabecera / Pie de Página
  xlSheet.PageSetup.RightHeader = "&6" + "OmniPos - Caja Registradora"
  xlSheet.PageSetup.RightFooter = "&6página &P"
  
  'Columna
  xlSheet.Columns(1).ColumnWidth = 30
  xlSheet.Columns(2).ColumnWidth = 10
  xlSheet.Columns(3).ColumnWidth = 10
  xlSheet.Columns(4).ColumnWidth = 10
  xlSheet.Columns(5).ColumnWidth = 13
  xlSheet.Columns(6).ColumnWidth = 10
  xlSheet.Columns(7).ColumnWidth = 15
  
  lFila = 1
  xlSheet.Cells(lFila, 1).Value = "Operaciones de Venta "
  lFila = lFila + 1
  
  xlSheet.Cells(lFila, 1).Value = "Articulo"
  xlSheet.Cells(lFila, 2).Value = "Cantidad"
  xlSheet.Cells(lFila, 3).Value = "Precio Unit."
  xlSheet.Cells(lFila, 4).Value = "Sub-Total"
  
  lFila = lFila + 1
  sSQL = " SELECT  C.sTeclDesc, P.sProdDescCrta, MAX(P.nProdPrecUnit) AS PrecioUnit," & _
         " SUM(O.nProdCant) AS TotalItem," & _
         " SUM(O.nProdCant * O.nProdPrecUnit) AS TotalMonto" & _
         " FROM (ConfiguracionTeclado AS C RIGHT JOIN Producto as P ON C.cProdCode = P.cProdCode)" & _
         " INNER JOIN Operacion AS O ON P.cProdCode = O.cProdCode " & _
         " WHERE cOperTipo in (1,2,3,4,5)" & sCondicionOpe
   
  sSQL = sSQL + " GROUP BY C.cTeclCode, C.sTeclDesc, P.sProdDescCrta" & _
         " ORDER BY 1"
   
  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then Exit Sub
  lRegistros = ADORS.RecordCount
  
   Do While Not ADORS.EOF
      xlSheet.Cells(lFila, 1) = LeerCampo(ADORS!sProdDescCrta)
      xlSheet.Cells(lFila, 2) = "0" & (ADORS!TotalItem)
      xlSheet.Cells(lFila, 3) = "0" & (ADORS!PrecioUnit)
      xlSheet.Cells(lFila, 4) = "0" & (ADORS!TotalMonto)
      lFila = lFila + 1
      ADORS.MoveNext
   Loop
   
 '-------------O/C---------------------------------------------------
  sSQL = "Select a.cprodcode,sum(a.nProdCant) as  sProdCant , max(a.nProdPrecUnit)  as Precio,a.nDscto, max(p.sproddesccrta) as producto , max(p.ccodcatalogo) as Codigo  from operacionparalelaproducto a " & _
        " left outer join producto p on  a.cProdCode=p.cProdCode " & _
        " where copercode in (select o.copercode  from operacionparalela o " & _
        " where o.cestado='1' and o.cOperTipo in (20) " & sCondicionOpeP & " )"
        
 sSQL = sSQL + " group by a.cprodcode, a.nDscto " & _
         " ORDER BY  max(p.sproddesccrta)"

 If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then Exit Sub
 
 If ADORS.RecordCount > 0 Then
  lFila = lFila + 1
  xlSheet.Cells(lFila, 1).Value = "Ordenes de Consumo "
  lFila = lFila + 1
  xlSheet.Cells(lFila, 1).Value = "Articulo"
  xlSheet.Cells(lFila, 2).Value = "Cantidad"
  xlSheet.Cells(lFila, 3).Value = "Precio Unit."
  xlSheet.Cells(lFila, 4).Value = "Sub-Total"
  lFila = lFila + 1
 End If
    Do While Not ADORS.EOF
       descuento = val(LeerCampo(ADORS("Precio"))) * val(("0" & ADORS("nDscto"))) / 100
       npreciounit = val(LeerCampo(ADORS("Precio"))) - descuento
       xlSheet.Cells(lFila, 1) = LeerCampo(ADORS!Producto)
       xlSheet.Cells(lFila, 2) = "0" & (ADORS!sProdCant)
       xlSheet.Cells(lFila, 3) = npreciounit
       xlSheet.Cells(lFila, 4) = npreciounit * ("0" & (ADORS!sProdCant))
       lFila = lFila + 1
       ADORS.MoveNext
    Loop
'----------Articulos en N/C sin Liquidar-----------------------------
  lFila = lFila + 1
  sSQL = "Select o.cprodcode, sum(o.nProdCant) as  sProdCant , max(o.nProdPrecUnit)  as Precio,o.nDscto, max(p.sproddesccrta) as producto , max(p.ccodcatalogo) as Codigo  from operacionparalelaproducto o " & _
        " left outer join producto p on  o.cProdCode=p.cProdCode and  o.cAlmaCode=p.cAlmaCode " & _
        " where copercode in (select copercode  from operacionparalela " & _
        " where cestado='1' and cOperTipo in (21) )"
        
  sSQL = sSQL + " group by o.cprodcode, o.nDscto " & _
         " ORDER BY  max(p.sproddesccrta)"
        

 If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then Exit Sub
 If ADORS.RecordCount > 0 Then
  xlSheet.Cells(lFila, 1).Value = "Articulos en N/C sin Liquidar "
  lFila = lFila + 1
  xlSheet.Cells(lFila, 1).Value = "Articulo"
  xlSheet.Cells(lFila, 2).Value = "Cantidad"
  xlSheet.Cells(lFila, 3).Value = "Precio Unit."
  xlSheet.Cells(lFila, 4).Value = "Sub-Total"
  lFila = lFila + 1
 End If
    
  Do While Not ADORS.EOF
       descuento = val(LeerCampo(ADORS("Precio"))) * val(("0" & ADORS("nDscto"))) / 100
       npreciounit = val(LeerCampo(ADORS("Precio"))) - descuento
       xlSheet.Cells(lFila, 1) = LeerCampo(ADORS!Producto)
       xlSheet.Cells(lFila, 2) = "0" & (ADORS!sProdCant)
       xlSheet.Cells(lFila, 3) = npreciounit
       xlSheet.Cells(lFila, 4) = npreciounit * ("0" & (ADORS!sProdCant))
       lFila = lFila + 1
       ADORS.MoveNext
  Loop
'-----------------------------PADE  -------------------------------
Dim iTotPADE As Double
  Dim iItemPADE As Long
  sSQL = " SELECT Sum(o.nMontosoles) as PADE, count(*) as items FROM OperacionParalela o " & _
         " WHERE o.cOperTipo in (22) and o.cEstado in ('1') " & sCondicionOpeP
                
        
    
  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then Exit Sub
  If Not ADORS.EOF Then
    iTotPADE = "0" & (ADORS!PADE)
    iItemPADE = "0" & (ADORS!Items)
  Else
    iTotPADE = 0
    iItemPADE = 0
  End If
  
  '-----------------------------PADV  -------------------------------
  Dim iTotPADV As Double
  Dim iItemPADV As Long
  sSQL = " SELECT Sum(o.nMontosoles) as PADV, count(*) as items FROM OperacionParalela o " & _
         " WHERE o.cOperTipo in (23) and cEstado in ('1') " & sCondicionOpeP
                
   
  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then Exit Sub
  If Not ADORS.EOF Then
    iTotPADV = "0" & (ADORS!PADV)
    iItemPADV = "0" & (ADORS!Items)
  Else
    iTotPADV = 0
    iItemPADV = 0
  End If
  
  
'----------------------TOTALES-----------------------------------------
lFila = lFila + 1
xlSheet.Cells(lFila, 1) = "TOTALES "

lFila = lFila + 1

 Dim ntotalitemsoc As Long
 Dim ntotalOC As Double
'-------------Total OC ------------------
  sSQL = " Select sum(nProdCant) as items  , sum(nProdCant * (nProdPrecUnit * ( ( 100 - nDscto)/100)) ) as precio " & _
         " From operacionparalelaproducto " & _
         " where copercode in (select o.copercode  from operacionparalela o " & _
         " where o.cestado='1' and o.cOperTipo in (20) " & sCondicionOpeP & " ) "
  
  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then Exit Sub
  ntotalitemsoc = "0" & (ADORS!Items)
  ntotalOC = "0" & (ADORS!precio)
  xlSheet.Cells(lFila, 2) = "O/C"
  xlSheet.Cells(lFila, 3) = Right("0000000" + CStr(ntotalitemsoc), 7)
  lFila = lFila + 1
  xlSheet.Cells(lFila, 3) = ntotalOC
  lFila = lFila + 1
'-----------Efectivo en voucher -------------
 Dim iEfeVoucher As Double
   sSQL = " SELECT SUM(o.nProdCant * o.nProdPrecUnit * (100 - o.nDscto) /100) AS TOTAL " & _
         " FROM Operacion o " & _
         " WHERE o.cOperTipo = 2  " & sCondicionOpe & _
         " AND NOT o.cOperFlagAnula = 1 " & _
         " AND o.cOperCode Not In (Select cOperCodeAnula " & _
         "                         From Operacion " & _
         "                        Where cOperFlagAnula = 1 " & Replace(sCondicionOpe, "o.", "") & " )"
  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then Exit Sub
  If Not ADORS.EOF Then
    iVoucMont = "0" & (ADORS!Total)
  Else
    iVoucMont = 0
  End If
  xlSheet.Cells(lFila, 2) = "Total en vouchers tickets"
  xlSheet.Cells(lFila, 3) = iVoucMont
  lFila = lFila + 1
  Set ADORS = Nothing
  '----------------------Efectivo en vouchers---------------------------
    sSQL = " SELECT SUM(o.nMontosoles) AS TOTAL" & _
         " FROM Operacion o" & _
         " WHERE o.cOperTipo = 1 AND TRIM(o.cEmisor) <> '' " & sCondicionOpe & _
         " AND NOT o.cOperFlagAnula = 1" & _
         " AND o.cOperCode Not In (Select cOperCodeAnula " & _
         "                         From Operacion " & _
         "                        Where cOperFlagAnula = 1 " & Replace(sCondicionOpe, "o.", "") & " )"
          
  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then Exit Sub
  If Not ADORS.EOF Then
    iEfeVoucher = "0" & (ADORS!Total)
  Else
    iEfeVoucher = 0
  End If
  Set ADORS = Nothing
  xlSheet.Cells(lFila, 2) = "Total en efectivo en vouchers"
  xlSheet.Cells(lFila, 3) = iEfeVoucher
  lFila = lFila + 1
  iVoucMont = iVoucMont - iEfeVoucher
  xlSheet.Cells(lFila, 2) = "Total solo voucher"
  xlSheet.Cells(lFila, 3) = iVoucMont
   
   '------------------------voucher en liquidacion -------------------------
   sSQL = "SELECT SUM(o.nmontosoles + o.nprodprecunit ) as TOTAL" & _
         "   FROM operacion o " & _
         " WHERE o.cOperTipo = 2 and o.cDocTipo='14' " & _
         " AND NOT o.cOperFlagAnula = 1" & sCondicionOpe
         
  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then Exit Sub
  iVoucMont = iVoucMont + val("0" & ADORS!Total)
  xlSheet.Cells(lFila, 2) = "Total en voucher en liquidacion"
  xlSheet.Cells(lFila, 3) = iVoucMont
  '---------------------------------------------
'Total de Dolares obtenidos por ventas
  sSQL = " SELECT SUM(o.nMontoDolares-o.nVueltoDolares) AS TOTAL" & _
         " FROM Operacion o " & _
         " WHERE o.cOperTipo in (1) " & sCondicionOpe & _
         " AND ( o.coperCode not in (SELECT cOperCodeanula " & _
         "                          FROM Operacion WHERE cOperFlagAnula=1 " & Replace(sCondicionOpe, "o.", "") & " ) " & _
         "                          OR  cdocTipo NOT in ('02', '01')) " & _
         " AND (o.cOperCode NOT in (Select cOperCode FROM OperacionParalela WHERE cOperCodeAnul <> 0 ) OR  cDocTipo in ('02', '01')) "
         
         
  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then Exit Sub
  Dim iCashMontdol As Double
  Do While Not ADORS.EOF
    If IsNull(LeerCampo(ADORS!Total)) Or (LeerCampo(ADORS!Total)) = "" Then
      iCashMontdol = 0
    Else
      iCashMontdol = iCashMontdol + LeerCampo(ADORS!Total)
    End If
    ADORS.MoveNext
  Loop
  xlSheet.Cells(lFila, 2) = "Total en dolares"
  xlSheet.Cells(lFila, 3) = iCashMontdol
  
  'Calculo del Cash Soles
         
  sSQL = " SELECT SUM((o.nProdPrecUnit * o.nProdCant)+o.nMontoSoles) AS TOTAL" & _
         " FROM Operacion o" & _
         " WHERE (o.cOperTipo in (1)) " & sCondicionOpe & _
         " AND o.cProdCode = 0" & _
           " AND ( o.coperCode not in (SELECT cOperCodeanula " & _
         "                          FROM Operacion WHERE cOperFlagAnula=1 " & Replace(sCondicionOpe, "o.", "") & " ) " & _
         "                          OR  cdocTipo NOT in ('02', '01')) " & _
         " AND (o.cOperCode NOT in (Select cOperCode FROM OperacionParalela WHERE cOperCodeAnul <> 0  ) OR  cDocTipo in ('02', '01')) "
         

              
  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then Exit Sub
  
  Do While Not ADORS.EOF
    If IsNull(LeerCampo(ADORS!Total)) Or (LeerCampo(ADORS!Total)) = "" Then
      iCashMont = 0
    Else
     iCashMont = iCashMont + LeerCampo(ADORS!Total)
    End If
    ADORS.MoveNext
  Loop
  xlSheet.Cells(lFila, 2) = "Total en soles"
  xlSheet.Cells(lFila, 3) = iCashMont
    
  Set ADORS = Nothing
  
  
'--------------------------------------------------------------------------
 xlApp.WindowState = xlMaximized
 xlApp.Visible = True
 Screen.MousePointer = vbDefault
   
End Sub

Private Function ValidaCierre() As Boolean
  Dim sSQL As String
  Dim ADORS As ADODB.Recordset
  Dim cOperId As String
  Dim sCondicionOpe As String
  
  sSQL = "SELECT cOperId FROM Configuracion"
  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then
    cOperId = ""
  Else
    cOperId = LeerCampo(ADORS("cOperId"))
  End If
  
  If cOperId = "" Then
    sCondicionOpe = "   and o.cOperflagabierto = 1 "
  Else
    sCondicionOpe = "   and o.cOperId > " + cOperId + " "
  End If
  
  ValidaCierre = True
  'Verifica si hay operaciones
  sSQL = " SELECT count(*) AS TOTAL FROM Operacion o" & _
         " WHERE o.cOperTipo in (1,2,3,4,5)" & sCondicionOpe
           
  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then Exit Function
  
  If LeerCampo(ADORS("total")) < 1 Then
    MsgBox "Debe registrar operaciones."
    ValidaCierre = False
  End If
  
  Set ADORS = Nothing
  
  '-----------Consistenciando que no existan Notas de Consumo sin Liquidar----------'
  'se quito esta validacion 020311
'  sSQL = " Select count(cOpercode) as total from operacionparalela " & _
'         " where cestado='1' and cOperTipo in (21) AND cOperflagAbierto=1 AND " & _
'         " NOT coperCode in (Select cOperCodeAnul from OperacionParalela)"
'
'  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then Exit Function
'
'  If LeerCampo(ADORS("total")) > 0 Then
'    MsgBox "Debe liquidar las Notas de Consumo emitidas."
'    ValidaCierre = False
'  End If
'
'  Set ADORS = Nothing

  '------------------------------------------------------------------------------------
End Function

Public Sub AgregarLineaBloque(sTexto As String)
  If iNroFilas >= 100 Then
    sImprimir(iNroBloques) = sImprimir(iNroBloques) & sTexto
    iNroBloques = iNroBloques + 1
    iNroFilas = 0
  Else
    sTexto = sTexto + vbCrLf
    sImprimir(iNroBloques) = sImprimir(iNroBloques) & sTexto
    iNroFilas = iNroFilas + 1
  End If
End Sub
