VERSION 5.00
Object = "{3B7C8863-D78F-101B-B9B5-04021C009402}#1.2#0"; "richtx32.ocx"
Object = "{CDE57A40-8B86-11D0-B3C6-00A0C90AEA82}#1.0#0"; "msdatgrd.ocx"
Begin VB.Form Form1 
   BackColor       =   &H00FFFFFF&
   Caption         =   "Empaque"
   ClientHeight    =   11895
   ClientLeft      =   525
   ClientTop       =   1065
   ClientWidth     =   17655
   LinkTopic       =   "Form1"
   ScaleHeight     =   11895
   ScaleWidth      =   17655
   WindowState     =   2  'Maximized
   Begin VB.CommandButton Command12 
      BackColor       =   &H00FFFFFF&
      Caption         =   "Inventario"
      BeginProperty Font 
         Name            =   "Impact"
         Size            =   14.25
         Charset         =   0
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   735
      Left            =   10440
      Style           =   1  'Graphical
      TabIndex        =   29
      Top             =   10080
      Width           =   1695
   End
   Begin VB.OptionButton Option2 
      Caption         =   "Entrada"
      BeginProperty Font 
         Name            =   "Impact"
         Size            =   14.25
         Charset         =   0
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   345
      Left            =   5760
      TabIndex        =   28
      Top             =   360
      Width           =   1695
   End
   Begin VB.OptionButton Option1 
      Caption         =   "Salida"
      BeginProperty Font 
         Name            =   "Impact"
         Size            =   14.25
         Charset         =   0
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   345
      Left            =   5760
      TabIndex        =   27
      Top             =   0
      Value           =   -1  'True
      Width           =   1695
   End
   Begin VB.CommandButton Command11 
      BackColor       =   &H00FFFFFF&
      Caption         =   "Salir"
      BeginProperty Font 
         Name            =   "Impact"
         Size            =   14.25
         Charset         =   0
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   735
      Left            =   12240
      Style           =   1  'Graphical
      TabIndex        =   25
      Top             =   10080
      Width           =   1335
   End
   Begin VB.CommandButton Command6 
      BackColor       =   &H00FFFFFF&
      Height          =   1575
      Left            =   9240
      Picture         =   "Form1.frx":0000
      Style           =   1  'Graphical
      TabIndex        =   9
      Top             =   4200
      Width           =   975
   End
   Begin VB.CommandButton Command10 
      BackColor       =   &H00FFFFFF&
      Height          =   1095
      Left            =   7800
      Picture         =   "Form1.frx":29E2
      Style           =   1  'Graphical
      TabIndex        =   24
      Top             =   4800
      Width           =   1335
   End
   Begin VB.CommandButton Command9 
      BackColor       =   &H00FFFFFF&
      Height          =   1095
      Left            =   7800
      Picture         =   "Form1.frx":308C
      Style           =   1  'Graphical
      TabIndex        =   23
      Top             =   3720
      Width           =   1335
   End
   Begin VB.CommandButton Command8 
      BackColor       =   &H00FFFFFF&
      Caption         =   "CONSULTA"
      BeginProperty Font 
         Name            =   "Impact"
         Size            =   14.25
         Charset         =   0
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   735
      Left            =   14760
      Style           =   1  'Graphical
      TabIndex        =   22
      Top             =   1080
      Width           =   2295
   End
   Begin VB.CommandButton Command7 
      BackColor       =   &H00FFFFFF&
      Caption         =   "Re-Imprmir"
      BeginProperty Font 
         Name            =   "Impact"
         Size            =   15.75
         Charset         =   0
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   855
      Left            =   14760
      Style           =   1  'Graphical
      TabIndex        =   21
      Top             =   240
      Width           =   2295
   End
   Begin VB.TextBox Text2 
      Height          =   495
      Left            =   9240
      TabIndex        =   0
      Top             =   960
      Width           =   975
   End
   Begin RichTextLib.RichTextBox RichTextBox1 
      Height          =   375
      Left            =   12840
      TabIndex        =   19
      Top             =   240
      Visible         =   0   'False
      Width           =   615
      _ExtentX        =   1085
      _ExtentY        =   661
      _Version        =   393217
      TextRTF         =   $"Form1.frx":36E5
   End
   Begin VB.CommandButton Command4 
      BackColor       =   &H00FFFFFF&
      Caption         =   "Finalizar"
      Enabled         =   0   'False
      BeginProperty Font 
         Name            =   "Impact"
         Size            =   15.75
         Charset         =   0
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   1575
      Left            =   12960
      Style           =   1  'Graphical
      TabIndex        =   18
      Top             =   240
      Width           =   1695
   End
   Begin VB.PictureBox Picture2 
      Height          =   375
      Left            =   17400
      ScaleHeight     =   315
      ScaleWidth      =   315
      TabIndex        =   16
      Top             =   600
      Visible         =   0   'False
      Width           =   375
   End
   Begin VB.CommandButton Command5 
      BackColor       =   &H00FFFFFF&
      Height          =   1575
      Left            =   9240
      Picture         =   "Form1.frx":3770
      Style           =   1  'Graphical
      TabIndex        =   10
      Top             =   2640
      Width           =   975
   End
   Begin VB.TextBox Text10 
      Alignment       =   2  'Center
      BeginProperty Font 
         Name            =   "Impact"
         Size            =   15.75
         Charset         =   0
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   540
      Left            =   9240
      TabIndex        =   8
      Text            =   "1"
      Top             =   1980
      Width           =   975
   End
   Begin VB.CommandButton Command3 
      Caption         =   "Exportar Excel"
      Height          =   375
      Left            =   17880
      TabIndex        =   6
      Top             =   360
      Visible         =   0   'False
      Width           =   1215
   End
   Begin VB.CommandButton Command2 
      BackColor       =   &H00FFFFFF&
      Caption         =   "Agregar Bulto o Procesar"
      BeginProperty Font 
         Name            =   "Impact"
         Size            =   15.75
         Charset         =   0
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   1095
      Left            =   10320
      Style           =   1  'Graphical
      TabIndex        =   5
      Top             =   720
      Width           =   2535
   End
   Begin VB.TextBox Text1 
      BeginProperty Font 
         Name            =   "Impact"
         Size            =   15.75
         Charset         =   0
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   540
      Left            =   1680
      ScrollBars      =   3  'Both
      TabIndex        =   4
      Top             =   120
      Width           =   2175
   End
   Begin RichTextLib.RichTextBox RichTextBox_Dis 
      Height          =   255
      Left            =   0
      TabIndex        =   3
      Top             =   0
      Visible         =   0   'False
      Width           =   615
      _ExtentX        =   1085
      _ExtentY        =   450
      _Version        =   393217
      Enabled         =   -1  'True
      TextRTF         =   $"Form1.frx":6152
   End
   Begin MSDataGridLib.DataGrid DataGrid1 
      Height          =   4935
      Index           =   0
      Left            =   0
      TabIndex        =   2
      Top             =   5880
      Width           =   10215
      _ExtentX        =   18018
      _ExtentY        =   8705
      _Version        =   393216
      AllowUpdate     =   0   'False
      Appearance      =   0
      HeadLines       =   1
      RowHeight       =   30
      BeginProperty HeadFont {0BE35203-8F91-11CE-9DE3-00AA004BB851} 
         Name            =   "Impact"
         Size            =   14.25
         Charset         =   0
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      BeginProperty Font {0BE35203-8F91-11CE-9DE3-00AA004BB851} 
         Name            =   "Impact"
         Size            =   15.75
         Charset         =   0
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      ColumnCount     =   2
      BeginProperty Column00 
         DataField       =   ""
         Caption         =   ""
         BeginProperty DataFormat {6D835690-900B-11D0-9484-00A0C91110ED} 
            Type            =   0
            Format          =   ""
            HaveTrueFalseNull=   0
            FirstDayOfWeek  =   0
            FirstWeekOfYear =   0
            LCID            =   3082
            SubFormatType   =   0
         EndProperty
      EndProperty
      BeginProperty Column01 
         DataField       =   ""
         Caption         =   ""
         BeginProperty DataFormat {6D835690-900B-11D0-9484-00A0C91110ED} 
            Type            =   0
            Format          =   ""
            HaveTrueFalseNull=   0
            FirstDayOfWeek  =   0
            FirstWeekOfYear =   0
            LCID            =   3082
            SubFormatType   =   0
         EndProperty
      EndProperty
      SplitCount      =   1
      BeginProperty Split0 
         MarqueeStyle    =   3
         BeginProperty Column00 
         EndProperty
         BeginProperty Column01 
         EndProperty
      EndProperty
   End
   Begin VB.CommandButton Command1 
      BackColor       =   &H00FFFFFF&
      Caption         =   "Buscar"
      BeginProperty Font 
         Name            =   "Impact"
         Size            =   15.75
         Charset         =   0
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   615
      Left            =   3960
      Style           =   1  'Graphical
      TabIndex        =   1
      Top             =   80
      Width           =   1695
   End
   Begin MSDataGridLib.DataGrid DataGrid1 
      Height          =   8055
      Index           =   1
      Left            =   10320
      TabIndex        =   13
      Top             =   1920
      Width           =   6735
      _ExtentX        =   11880
      _ExtentY        =   14208
      _Version        =   393216
      AllowUpdate     =   0   'False
      Appearance      =   0
      HeadLines       =   1
      RowHeight       =   27
      BeginProperty HeadFont {0BE35203-8F91-11CE-9DE3-00AA004BB851} 
         Name            =   "Impact"
         Size            =   14.25
         Charset         =   0
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      BeginProperty Font {0BE35203-8F91-11CE-9DE3-00AA004BB851} 
         Name            =   "Impact"
         Size            =   14.25
         Charset         =   0
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      ColumnCount     =   2
      BeginProperty Column00 
         DataField       =   ""
         Caption         =   ""
         BeginProperty DataFormat {6D835690-900B-11D0-9484-00A0C91110ED} 
            Type            =   0
            Format          =   ""
            HaveTrueFalseNull=   0
            FirstDayOfWeek  =   0
            FirstWeekOfYear =   0
            LCID            =   3082
            SubFormatType   =   0
         EndProperty
      EndProperty
      BeginProperty Column01 
         DataField       =   ""
         Caption         =   ""
         BeginProperty DataFormat {6D835690-900B-11D0-9484-00A0C91110ED} 
            Type            =   0
            Format          =   ""
            HaveTrueFalseNull=   0
            FirstDayOfWeek  =   0
            FirstWeekOfYear =   0
            LCID            =   3082
            SubFormatType   =   0
         EndProperty
      EndProperty
      SplitCount      =   1
      BeginProperty Split0 
         MarqueeStyle    =   3
         BeginProperty Column00 
         EndProperty
         BeginProperty Column01 
         EndProperty
      EndProperty
   End
   Begin MSDataGridLib.DataGrid DataGrid1 
      Height          =   3015
      Index           =   2
      Left            =   0
      TabIndex        =   17
      Top             =   720
      Width           =   9135
      _ExtentX        =   16113
      _ExtentY        =   5318
      _Version        =   393216
      AllowUpdate     =   0   'False
      Appearance      =   0
      HeadLines       =   1
      RowHeight       =   30
      BeginProperty HeadFont {0BE35203-8F91-11CE-9DE3-00AA004BB851} 
         Name            =   "Impact"
         Size            =   14.25
         Charset         =   0
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      BeginProperty Font {0BE35203-8F91-11CE-9DE3-00AA004BB851} 
         Name            =   "Impact"
         Size            =   15.75
         Charset         =   0
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      ColumnCount     =   2
      BeginProperty Column00 
         DataField       =   ""
         Caption         =   ""
         BeginProperty DataFormat {6D835690-900B-11D0-9484-00A0C91110ED} 
            Type            =   0
            Format          =   ""
            HaveTrueFalseNull=   0
            FirstDayOfWeek  =   0
            FirstWeekOfYear =   0
            LCID            =   3082
            SubFormatType   =   0
         EndProperty
      EndProperty
      BeginProperty Column01 
         DataField       =   ""
         Caption         =   ""
         BeginProperty DataFormat {6D835690-900B-11D0-9484-00A0C91110ED} 
            Type            =   0
            Format          =   ""
            HaveTrueFalseNull=   0
            FirstDayOfWeek  =   0
            FirstWeekOfYear =   0
            LCID            =   3082
            SubFormatType   =   0
         EndProperty
      EndProperty
      SplitCount      =   1
      BeginProperty Split0 
         MarqueeStyle    =   3
         SizeMode        =   1
         Size            =   10
         BeginProperty Column00 
         EndProperty
         BeginProperty Column01 
         EndProperty
      EndProperty
   End
   Begin VB.Label Label7 
      BackColor       =   &H00FFFFFF&
      Caption         =   "FACT NRO: "
      BeginProperty Font 
         Name            =   "Impact"
         Size            =   14.25
         Charset         =   0
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   375
      Left            =   3600
      TabIndex        =   32
      Top             =   5400
      Width           =   2415
   End
   Begin VB.Label Label6 
      BackColor       =   &H00FFFFFF&
      Caption         =   "CANTIDAD:"
      BeginProperty Font 
         Name            =   "Impact"
         Size            =   14.25
         Charset         =   0
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   375
      Left            =   3600
      TabIndex        =   31
      Top             =   5040
      Width           =   2415
   End
   Begin VB.Label Label5 
      BackColor       =   &H00FFFFFF&
      Caption         =   "CODIGO:  "
      BeginProperty Font 
         Name            =   "Impact"
         Size            =   14.25
         Charset         =   0
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   375
      Left            =   120
      TabIndex        =   30
      Top             =   5040
      Width           =   3495
   End
   Begin VB.Label Label4 
      BackColor       =   &H00FFFFFF&
      Caption         =   "OEM: "
      BeginProperty Font 
         Name            =   "Impact"
         Size            =   14.25
         Charset         =   0
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   375
      Left            =   120
      TabIndex        =   26
      Top             =   5400
      Width           =   3495
   End
   Begin VB.Image Image1 
      Height          =   645
      Left            =   13680
      Picture         =   "Form1.frx":61D4
      Top             =   10080
      Width           =   3015
   End
   Begin VB.Label Label9 
      Alignment       =   2  'Center
      BackColor       =   &H00FFFFFF&
      Caption         =   "Barra"
      BeginProperty Font 
         Name            =   "Impact"
         Size            =   14.25
         Charset         =   0
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   375
      Index           =   1
      Left            =   9120
      TabIndex        =   20
      Top             =   480
      Width           =   1215
   End
   Begin VB.Label LBNBULTO 
      BackColor       =   &H00FFFFFF&
      Caption         =   "0"
      BeginProperty Font 
         Name            =   "Impact"
         Size            =   15.75
         Charset         =   0
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   375
      Left            =   12000
      TabIndex        =   15
      Top             =   240
      Width           =   1455
   End
   Begin VB.Label Label3 
      BackColor       =   &H00FFFFFF&
      Caption         =   "BULTO Nº:"
      BeginProperty Font 
         Name            =   "Impact"
         Size            =   15.75
         Charset         =   0
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   375
      Left            =   10560
      TabIndex        =   14
      Top             =   240
      Width           =   1455
   End
   Begin VB.Label Label2 
      BackColor       =   &H00FFFFFF&
      Caption         =   "CLIENTE: "
      BeginProperty Font 
         Name            =   "Impact"
         Size            =   14.25
         Charset         =   0
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   1215
      Left            =   120
      TabIndex        =   12
      Top             =   3840
      Width           =   7575
   End
   Begin VB.Label Label9 
      Alignment       =   2  'Center
      BackColor       =   &H00FFFFFF&
      Caption         =   "Cantidad"
      BeginProperty Font 
         Name            =   "Impact"
         Size            =   14.25
         Charset         =   0
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   375
      Index           =   0
      Left            =   9165
      TabIndex        =   11
      Top             =   1560
      Width           =   1095
   End
   Begin VB.Label Label1 
      BackColor       =   &H00FFFFFF&
      Caption         =   "FACTURA Nº"
      BeginProperty Font 
         Name            =   "Impact"
         Size            =   15.75
         Charset         =   0
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   375
      Left            =   120
      TabIndex        =   7
      Top             =   120
      Width           =   2775
   End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
'Call Exportar_DbGrid_Excel("Hoja1")


Private Sub Command1_Click()
If rs4.BOF = True And rs4.BOF = True Then
    If LBNBULTO.Caption = "1" Then
        'vBloquearCliente = False
        'DataGrid1(2).Enabled = True
        
        'Exit Sub
    End If
Else
    
End If
If Option1 = True Then
    'V_Vista = "SELECT 'FAC' AS TIPO, MAX(FECHAE) AS FECHAE, CODCLIE, DESCRIP, SUM(CREDITO) AS CREDITO, SUM(CONTADO) AS CONTADO, 'A' AS TIPOFAC FROM SAFACT WHERE  TIPOFAC='A' AND TIPOFAC+NUMEROD IN(SELECT   A.TipoFac+A.NumeroD FROM SAITEMFAC AS A INNER JOIN SAFACT AS B ON A.TipoFac = B.TipoFac AND A.NumeroD = B.NumeroD INNER JOIN SACLIE AS C ON B.CodClie = C.CodClie WHERE (A.TipoFac = 'A') AND (A.Cantidad - ISNULL ((SELECT   SUM(CANTIDAD) AS Expr1 FROM _TABLE_DESPACHO AS _TABLE_DESPACHO_1 WHERE   nbulto<>0 and TIPOFAC=A.TIPOFAC AND (NPRESUPUESTO = A.NumeroD) AND (CODIGO = A.CodItem)), 0) > 0)) GROUP BY CODCLIE, DESCRIP "
    'V_Vista = "SELECT 'CAR' AS TIPO, MAX(FECHAE) AS FECHAE, codusua as CODCLIE, MAX(Autori) as DESCRIP, 0 AS CREDITO, 0 AS CONTADO FROM SAOPEI WHERE  TIPOOPI='P' AND TIPOOPI+NUMEROD IN(SELECT   A.TipoOPI+A.NumeroD FROM SAITEMOPI AS A INNER JOIN SAOPEI AS B ON A.TipoOPI = B.TipoOPI AND A.NumeroD = B.NumeroD  WHERE (A.TipoOPI = 'P') AND (A.Cantidad - ISNULL ((SELECT   SUM(CANTIDAD) AS Expr1 FROM _TABLE_DESPACHO AS _TABLE_DESPACHO_1 WHERE   nbulto<>0 and TIPOFAC=A.TIPOOPI AND  (NPRESUPUESTO = A.NumeroD) AND (CODIGO = A.CodItem)), 0) > 0)) GROUP BY  codusua "
    V_Vista = "SELECT 'FAC' AS TIPO, MAX(FECHAE) AS FECHAE, CODCLIE, DESCRIP, SUM(CREDITO) AS CREDITO, SUM(CONTADO) AS CONTADO, 'A' AS TIPOFAC FROM SAFACT WHERE convert(varchar,fechae,112)>=20250812 and  TIPOFAC='A' AND TIPOFAC+NUMEROD IN(SELECT   A.TipoFac+A.NumeroD FROM SAITEMFAC AS A INNER JOIN SAFACT AS B ON A.TipoFac = B.TipoFac AND A.NumeroD = B.NumeroD INNER JOIN SACLIE AS C ON B.CodClie = C.CodClie WHERE (A.TipoFac = 'A') AND (A.Cantidad - ISNULL ((SELECT   SUM(CANTIDAD) AS Expr1 FROM _TABLE_DESPACHO AS _TABLE_DESPACHO_1 WHERE   nbulto<>0 and TIPOFAC=A.TIPOFAC AND (NPRESUPUESTO = A.NumeroD) AND (CODIGO = A.CodItem)), 0) > 0)) GROUP BY CODCLIE, DESCRIP " & _
    " UNION " & _
    " SELECT 'DES' AS TIPO, MAX(FECHAE) AS FECHAE, codusua as CODCLIE, MAX(Autori) as DESCRIP, 0 AS CREDITO, 0 AS CONTADO, 'P' AS TIPOOPI FROM SAOPEI WHERE  TIPOOPI='P' AND TIPOOPI+NUMEROD IN(SELECT   A.TipoOPI+A.NumeroD FROM SAITEMOPI AS A INNER JOIN SAOPEI AS B ON A.TipoOPI = B.TipoOPI AND A.NumeroD = B.NumeroD  WHERE (A.TipoOPI = 'P') AND (A.Cantidad - ISNULL ((SELECT   SUM(CANTIDAD) AS Expr1 FROM _TABLE_DESPACHO AS _TABLE_DESPACHO_1 WHERE   nbulto<>0 and TIPOFAC=A.TIPOOPI AND  (NPRESUPUESTO = A.NumeroD) AND (CODIGO = A.CodItem)), 0) > 0)) GROUP BY  codusua "
Else
'    V_Vista = "SELECT 'DEV' AS TIPO, MAX(FECHAE) AS FECHAE, CODCLIE, DESCRIP, SUM(CREDITO) AS CREDITO, SUM(CONTADO) AS CONTADO, 'B' AS TIPOFAC FROM SAFACT WHERE  TIPOFAC='B' AND TIPOFAC+NUMEROD IN(SELECT   A.TipoFac+A.NumeroD FROM SAITEMFAC AS A INNER JOIN SAFACT AS B ON A.TipoFac = B.TipoFac AND A.NumeroD = B.NumeroD INNER JOIN SACLIE AS C ON B.CodClie = C.CodClie WHERE (A.TipoFac = 'B') AND (A.Cantidad - ISNULL ((SELECT   SUM(CANTIDAD*-1) AS Expr1 FROM _TABLE_DESPACHO AS _TABLE_DESPACHO_1 WHERE   nbulto<>0 and TIPOFAC=A.TIPOFAC AND (NPRESUPUESTO = A.NumeroD) AND (CODIGO = A.CodItem)), 0) > 0)) GROUP BY CODCLIE, DESCRIP "
    V_Vista = "SELECT 'DEV' AS TIPO, MAX(FECHAE) AS FECHAE, CODCLIE, DESCRIP, SUM(CREDITO) AS CREDITO, SUM(CONTADO) AS CONTADO, 'B' AS TIPOFAC FROM SAFACT WHERE  TIPOFAC='B' AND TIPOFAC+NUMEROD IN(SELECT   A.TipoFac+A.NumeroD FROM SAITEMFAC AS A INNER JOIN SAFACT AS B ON A.TipoFac = B.TipoFac AND A.NumeroD = B.NumeroD INNER JOIN SACLIE AS C ON B.CodClie = C.CodClie WHERE (A.TipoFac = 'B') AND (A.Cantidad - ISNULL ((SELECT   SUM(CANTIDAD*-1) AS Expr1 FROM _TABLE_DESPACHO AS _TABLE_DESPACHO_1 WHERE   nbulto<>0 and TIPOFAC=A.TIPOFAC AND (NPRESUPUESTO = A.NumeroD) AND (CODIGO = A.CodItem)), 0) > 0)) GROUP BY CODCLIE, DESCRIP " & _
    " UNION " & _
    " SELECT 'CAR' AS TIPO, MAX(FECHAE) AS FECHAE, codusua as CODCLIE, MAX(Autori) as DESCRIP, 0 AS CREDITO, 0 AS CONTADO, 'O' AS TIPOOPI FROM SAOPEI WHERE  TIPOOPI='O' AND TIPOOPI+NUMEROD IN(SELECT   A.TipoOPI+A.NumeroD FROM SAITEMOPI AS A INNER JOIN SAOPEI AS B ON A.TipoOPI = B.TipoOPI AND A.NumeroD = B.NumeroD  WHERE (A.TipoOPI = 'O') AND (A.Cantidad - ISNULL ((SELECT   SUM(CANTIDAD*-1) AS Expr1 FROM _TABLE_DESPACHO AS _TABLE_DESPACHO_1 WHERE   nbulto<>0 and TIPOFAC=A.TIPOOPI AND  (NPRESUPUESTO = A.NumeroD) AND (CODIGO = A.CodItem)), 0) > 0)) GROUP BY  codusua "

End If
'MEN123 = InputBox("", "", V_Vista)
ActualizarRs1
dbindEx = 2
Set DataGrid1(dbindEx).DataSource = rs1
vTabla = "FACTURA_TABLE_DESPACHO"
FormatoGrid
'''''''''''''''''formato
If rs1.BOF = True And rs1.EOF = True Then
    V_Vista = "SELECT    ID, NPRESUPUESTO, FECHA, CODIGO, DESCRIPCION, CANTIDAD, NBULTO " & _
    " FROM         _TABLE_DESPACHO AS _TABLE_DESPACHO_1 WHERE     NBULTO=0"
    ActualizarRs4
    
    V_Vista = "SELECT   MAX(NBULTO) + 1 AS PROXN FROM  _TABLE_DESPACHO  WHERE nbulto=0"
    ActualizarRs5
    If rs5.BOF = True And rs5.EOF = True Then
        LBNBULTO.Caption = 1
    Else
        If IsNull(rs5.Fields("PROXN").Value) = True Then
            LBNBULTO.Caption = "1"
        Else
            LBNBULTO.Caption = rs5.Fields("PROXN").Value
        End If
    End If
        '''''''''''''''''formato
        dbindEx = 1
        Set DataGrid1(dbindEx).DataSource = rs4
        vTabla = "TABLE_DESPACHO"
        FormatoGrid
        '''''''''''''''''fin formato
    'Exit Sub
End If

'''''''''''''''''fin formato
'V_Vista = "SELECT   TipoFac, NumeroD, FechaE, CodItem, Descrip1, Cantidad - ISNULL  ((SELECT   SUM(CANTIDAD) AS Expr1 FROM         _TABLE_DESPACHO WHERE     (NPRESUPUESTO = A.NumeroD) AND (CODIGO = A.CodItem)), 0) AS CANTIDAD " & _
'" FROM         SAITEMFAC AS A WHERE     (TipoFac = 'F') AND (NumeroD = '" & Text1.Text & "') AND (Cantidad - ISNULL ((SELECT   SUM(CANTIDAD) AS Expr1 FROM         _TABLE_DESPACHO AS _TABLE_DESPACHO_1  WHERE     (NPRESUPUESTO = A.NumeroD) AND (CODIGO = A.CodItem)), 0) > 0)"

ActualizarCampos

Text2.SetFocus

End Sub

Sub ActualizarCampos()

If rs1.BOF = True Or rs1.EOF = True Then
    V_Vista = "SELECT TOP 0 '' AS TipoFac, '' as Numerod"
    ActualizarRs3
    dbindEx = 0
    Set DataGrid1(dbindEx).DataSource = rs3
    'vTabla = "DET_FACTURA_TABLE_DESPACHO"
    Text10.Text = "0"
    Label2.Caption = ""
    Label4.Caption = ""
    Exit Sub
End If
If Option1 = True Then
    If rs1.Fields("TIPO").Value = "FAC" Then
        V_Vista = "SELECT   A.TipoFac,  A.NumeroD, A.FechaE, A.NroLinea, A.CodItem, A.Descrip1, A.Cantidad - ISNULL ((SELECT   SUM(CANTIDAD) AS Expr1 FROM _TABLE_DESPACHO WHERE TIPOFAC=A.TIPOFAC AND (NPRESUPUESTO = A.NumeroD) AND (CODIGO = A.CodItem)), 0) AS CANTIDAD, ISNULL((SELECT C07 FROM SAPROD WHERE SAPROD.CODPROD=A.CODITEM ),'') AS UBI, ISNULL((SELECT C01 FROM SAPROD WHERE SAPROD.CODPROD=A.CODITEM ),'') AS OEM, C.CodClie, C.Descrip, C.Direc1, C.Direc2, C.Telef, B.CODVEND, B.CODUSUA, ISNULL(B.NUMEROR,'') AS NUMEROR " & _
        " FROM         SAITEMFAC AS A INNER JOIN SAFACT AS B ON A.TipoFac = B.TipoFac AND A.NumeroD = B.NumeroD INNER JOIN SACLIE AS C ON B.CodClie = C.CodClie " & _
        " WHERE     (A.TipoFac = 'A') AND (A.NumeroD IN(SELECT NUMEROD FROM SAFACT WHERE TIPOFAC='A' AND CODCLIE='" & rs1.Fields("CODCLIE").Value & "')) AND (A.Cantidad - ISNULL ((SELECT   SUM(CANTIDAD) AS Expr1 FROM _TABLE_DESPACHO AS _TABLE_DESPACHO_1 WHERE  TIPOFAC='A' AND   (NPRESUPUESTO = A.NumeroD) AND (CODIGO = A.CodItem)), 0) > 0)  ORDER BY A.NUMEROD, A.CODITEM"
    Else
        V_Vista = "SELECT   A.TIPOOPI AS TipoFac, A.NumeroD, A.FechaE, A.NroLinea, A.CodItem, A.Descrip1, A.Cantidad - ISNULL ((SELECT   SUM(CANTIDAD) AS Expr1 FROM _TABLE_DESPACHO WHERE A.TIPOOPI=TIPOFAC AND (NPRESUPUESTO = A.NumeroD) AND (CODIGO = A.CodItem)), 0) AS CANTIDAD, ISNULL((SELECT C07 FROM SAPROD WHERE SAPROD.CODPROD=A.CODITEM ),'') AS UBI, ISNULL((SELECT C01 FROM SAPROD WHERE SAPROD.CODPROD=A.CODITEM ),'') AS OEM, B.AUTORI AS CodClie, B.Respon AS Descrip, '' AS Direc1, '' AS Direc2, '' AS Telef, '' AS CODVEND, B.CODUSUA, '' AS NUMEROR " & _
        " FROM         SAITEMOPI AS A INNER JOIN SAOPEI AS B ON A.TipoOPI = B.TipoOPI AND A.NumeroD = B.NumeroD  " & _
        " WHERE     (A.TipoOPI = 'P') AND (A.NumeroD IN(SELECT NUMEROD FROM SAOPEI WHERE TIPOOPI='P' AND CODUSUA='" & rs1.Fields("CODCLIE").Value & "')) AND (A.Cantidad - ISNULL ((SELECT   SUM(CANTIDAD) AS Expr1 FROM _TABLE_DESPACHO AS _TABLE_DESPACHO_1 WHERE  TIPOFAC='P' AND   (NPRESUPUESTO = A.NumeroD) AND (CODIGO = A.CodItem)), 0) > 0) ORDER BY A.NUMEROD, A.CODITEM"
    End If

Else
    If rs1.Fields("TIPO").Value = "DEV" Then
        V_Vista = "SELECT   A.TipoFac, A.NumeroD, A.FechaE, A.NroLinea, A.CodItem, A.Descrip1, A.Cantidad - ISNULL ((SELECT   SUM(CANTIDAD*-1) AS Expr1 FROM _TABLE_DESPACHO WHERE TIPOFAC=A.TIPOFAC AND (NPRESUPUESTO = A.NumeroD) AND (CODIGO = A.CodItem)), 0) AS CANTIDAD, ISNULL((SELECT C07 FROM SAPROD WHERE SAPROD.CODPROD=A.CODITEM ),'') AS UBI, ISNULL((SELECT C01 FROM SAPROD WHERE SAPROD.CODPROD=A.CODITEM ),'') AS OEM, C.CodClie, C.Descrip, C.Direc1, C.Direc2, C.Telef, B.CODVEND, B.CODUSUA, ISNULL(B.NUMEROR,'') AS NUMEROR " & _
        " FROM         SAITEMFAC AS A INNER JOIN SAFACT AS B ON A.TipoFac = B.TipoFac AND A.NumeroD = B.NumeroD INNER JOIN SACLIE AS C ON B.CodClie = C.CodClie " & _
        " WHERE     (A.TipoFac = 'B')  AND (A.NumeroD IN(SELECT NUMEROD FROM SAFACT WHERE TIPOFAC='B' AND CODCLIE='" & rs1.Fields("CODCLIE").Value & "')) AND (A.Cantidad - ISNULL ((SELECT   SUM(CANTIDAD*-1) AS Expr1 FROM _TABLE_DESPACHO AS _TABLE_DESPACHO_1 WHERE  TIPOFAC='B' AND   (NPRESUPUESTO = A.NumeroD) AND (CODIGO = A.CodItem)), 0) > 0)  ORDER BY A.NUMEROD, A.CODITEM"
    Else
        V_Vista = "SELECT   A.TIPOOPI AS TipoFac, A.NumeroD, A.FechaE, A.NroLinea, A.CodItem, A.Descrip1, A.Cantidad - ISNULL ((SELECT   SUM(CANTIDAD*-1) AS Expr1 FROM _TABLE_DESPACHO WHERE A.TIPOOPI=TIPOFAC AND (NPRESUPUESTO = A.NumeroD) AND (CODIGO = A.CodItem)), 0) AS CANTIDAD, ISNULL((SELECT C07 FROM SAPROD WHERE SAPROD.CODPROD=A.CODITEM ),'') AS UBI, ISNULL((SELECT C01 FROM SAPROD WHERE SAPROD.CODPROD=A.CODITEM ),'') AS OEM, B.AUTORI AS CodClie, B.Respon AS Descrip, '' AS Direc1, '' AS Direc2, '' AS Telef, '' AS CODVEND, B.CODUSUA, '' AS NUMEROR " & _
        " FROM         SAITEMOPI AS A INNER JOIN SAOPEI AS B ON A.TipoOPI = B.TipoOPI AND A.NumeroD = B.NumeroD  " & _
        " WHERE     (A.TipoOPI = 'O') AND (A.NumeroD IN(SELECT NUMEROD FROM SAOPEI WHERE TIPOOPI='O' AND CODUSUA='" & rs1.Fields("CODCLIE").Value & "')) AND (A.Cantidad - ISNULL ((SELECT   SUM(CANTIDAD*-1) AS Expr1 FROM _TABLE_DESPACHO AS _TABLE_DESPACHO_1 WHERE  TIPOFAC='O' AND   (NPRESUPUESTO = A.NumeroD) AND (CODIGO = A.CodItem)), 0) > 0)  ORDER BY A.NUMEROD, A.CODITEM"
    
    End If
End If
ActualizarRs3
'MEN123 = InputBox("", "", V_Vista)
'''''''''''''''''formato
dbindEx = 0
Set DataGrid1(dbindEx).DataSource = rs3
vTabla = "DET_FACTURA_TABLE_DESPACHO"
FormatoGrid
DataGrid1(0).Columns(1).Alignment = dbgRight
'DataGrid1(0).Columns(4).Alignment = dbgRight
'''''''''''''''''fin formato
If rs3.EOF = True And rs3.BOF = True Then
    Text10.Text = "0"
    Label2.Caption = ""
    Label4.Caption = ""
Else
    'Text10.Text = rs3.Fields("CANTIDAD").Value
    Text10.Text = 1
    Label2.Caption = rs3.Fields("CODCLIE").Value & " " & rs3.Fields("DESCRIP").Value & vbCrLf & rs3.Fields("DIREC1").Value & " " & rs3.Fields("DIREC2").Value & " " & rs3.Fields("TELEF").Value
    Label4.Caption = "OEM: " & rs3.Fields("OEM").Value
End If
If Option1 = True Then
    If rs1.Fields("TIPO").Value = "FAC" Then
        'V_Vista = "SELECT    MAX(ID) AS ID, NPRESUPUESTO, MAX(FECHA) AS FECHA, CODIGO, DESCRIPCION, SUM(CANTIDAD) AS CANTIDAD, NBULTO " & _
        '" FROM         _TABLE_DESPACHO AS _TABLE_DESPACHO_1 WHERE     (NPRESUPUESTO IN(SELECT NUMEROD FROM SAFACT WHERE TIPOFAC='" & rs1.Fields("TIPOFAC").Value & "' AND CODCLIE= '" & rs1.Fields("CODCLIE").Value & "')) AND NBULTO=0 GROUP BY NPRESUPUESTO, CODIGO, DESCRIPCION, NBULTO"
        V_Vista = "SELECT     MAX(A.ID) AS ID, A.NPRESUPUESTO, MAX(A.FECHA) AS FECHA, A.CODIGO, B.C01 AS OEM, A.DESCRIPCION, SUM(A.CANTIDAD) AS Cantidad, A.NPRESUPUESTO AS DOC, A.CODBULTO FROM _TABLE_DESPACHO AS A LEFT OUTER JOIN SAPROD AS B ON A.CODIGO = B.CodProd " & _
        " WHERE (A.NPRESUPUESTO IN(SELECT NUMEROD FROM SAFACT WHERE TIPOFAC='" & rs1.Fields("TIPOFAC").Value & "' AND CODCLIE= '" & rs1.Fields("CODCLIE").Value & "')) AND A.NBULTO=0 GROUP BY A.CODIGO, A.DESCRIPCION, B.C01, A.CODBULTO, A.NPRESUPUESTO "
        ActualizarRs4
        'MEN123 = InputBox("", "", V_Vista)
        V_Vista = "SELECT   MAX(NBULTO) + 1 AS PROXN FROM  _TABLE_DESPACHO  WHERE ultbulto is null and (NPRESUPUESTO IN(SELECT NUMEROD FROM SAFACT WHERE TIPOFAC='" & rs1.Fields("TIPOFAC").Value & "' AND CODCLIE= '" & rs1.Fields("CODCLIE").Value & "'))"
        ActualizarRs5
    Else
'        V_Vista = "SELECT    MAX(ID) AS ID, NPRESUPUESTO, MAX(FECHA) AS FECHA, CODIGO, DESCRIPCION, SUM(CANTIDAD) AS CANTIDAD, NBULTO " & _
'        " FROM         _TABLE_DESPACHO AS _TABLE_DESPACHO_1 WHERE     (NPRESUPUESTO IN(SELECT NUMEROD FROM SAOPEI WHERE TIPOOPI='P' AND CODUSUA= '" & rs1.Fields("CODCLIE").Value & "')) AND NBULTO=0  GROUP BY NPRESUPUESTO, CODIGO, DESCRIPCION, NBULTO"
        V_Vista = "SELECT     MAX(A.ID) AS ID, A.NPRESUPUESTO, MAX(A.FECHA) AS FECHA, A.CODIGO, B.C01 AS OEM, A.DESCRIPCION, SUM(A.CANTIDAD) AS Cantidad, A.NPRESUPUESTO AS DOC, A.CODBULTO FROM _TABLE_DESPACHO AS A LEFT OUTER JOIN SAPROD AS B ON A.CODIGO = B.CodProd " & _
        " WHERE  (NPRESUPUESTO IN(SELECT NUMEROD FROM SAOPEI WHERE TIPOOPI='P' AND CODUSUA= '" & rs1.Fields("CODCLIE").Value & "')) AND NBULTO=0  GROUP BY  A.CODIGO, A.DESCRIPCION, B.C01, A.CODBULTO, A.NPRESUPUESTO "
        
        ActualizarRs4
        
        V_Vista = "SELECT   MAX(NBULTO) + 1 AS PROXN FROM  _TABLE_DESPACHO  WHERE ultbulto is null and (NPRESUPUESTO IN(SELECT NUMEROD FROM SAOPEI WHERE TIPOOPI='P' AND CODUSUA= '" & rs1.Fields("CODCLIE").Value & "'))"
        ActualizarRs5
    
    End If
Else
    If rs1.Fields("TIPO").Value = "DEV" Then
        'V_Vista = "SELECT    MAX(ID) AS ID, NPRESUPUESTO, MAX(FECHA) AS FECHA, CODIGO, DESCRIPCION, SUM(CANTIDAD) AS CANTIDAD, NBULTO " & _
        '" FROM         _TABLE_DESPACHO AS _TABLE_DESPACHO_1 WHERE     (NPRESUPUESTO IN(SELECT NUMEROD FROM SAFACT WHERE TIPOFAC='" & rs1.Fields("TIPOFAC").Value & "' AND CODCLIE= '" & rs1.Fields("CODCLIE").Value & "')) AND NBULTO=0 GROUP BY NPRESUPUESTO, CODIGO, DESCRIPCION, NBULTO"
        V_Vista = "SELECT     MAX(A.ID) AS ID, A.NPRESUPUESTO, MAX(A.FECHA) AS FECHA, A.CODIGO, B.C01 AS OEM, A.DESCRIPCION, SUM(A.CANTIDAD) AS Cantidad, A.NPRESUPUESTO AS DOC, A.CODBULTO FROM _TABLE_DESPACHO AS A LEFT OUTER JOIN SAPROD AS B ON A.CODIGO = B.CodProd " & _
        " WHERE (A.NPRESUPUESTO IN(SELECT NUMEROD FROM SAFACT WHERE TIPOFAC='" & rs1.Fields("TIPOFAC").Value & "' AND CODCLIE= '" & rs1.Fields("CODCLIE").Value & "')) AND A.NBULTO=0 GROUP BY A.CODIGO, A.DESCRIPCION, B.C01, A.CODBULTO, A.NPRESUPUESTO "
        ActualizarRs4
        'MEN123 = InputBox("", "", V_Vista)
        V_Vista = "SELECT   MAX(NBULTO) + 1 AS PROXN FROM  _TABLE_DESPACHO  WHERE ultbulto is null and (NPRESUPUESTO IN(SELECT NUMEROD FROM SAFACT WHERE TIPOFAC='" & rs1.Fields("TIPOFAC").Value & "' AND CODCLIE= '" & rs1.Fields("CODCLIE").Value & "'))"
        ActualizarRs5
    Else
'        V_Vista = "SELECT    MAX(ID) AS ID, NPRESUPUESTO, MAX(FECHA) AS FECHA, CODIGO, DESCRIPCION, SUM(CANTIDAD) AS CANTIDAD, NBULTO " & _
'        " FROM         _TABLE_DESPACHO AS _TABLE_DESPACHO_1 WHERE     (NPRESUPUESTO IN(SELECT NUMEROD FROM SAOPEI WHERE TIPOOPI='P' AND CODUSUA= '" & rs1.Fields("CODCLIE").Value & "')) AND NBULTO=0  GROUP BY NPRESUPUESTO, CODIGO, DESCRIPCION, NBULTO"
        V_Vista = "SELECT     MAX(A.ID) AS ID, A.NPRESUPUESTO, MAX(A.FECHA) AS FECHA, A.CODIGO, B.C01 AS OEM, A.DESCRIPCION, SUM(A.CANTIDAD) AS Cantidad, A.NPRESUPUESTO AS DOC, A.CODBULTO FROM _TABLE_DESPACHO AS A LEFT OUTER JOIN SAPROD AS B ON A.CODIGO = B.CodProd " & _
        " WHERE  (NPRESUPUESTO IN(SELECT NUMEROD FROM SAOPEI WHERE TIPOOPI='O' AND CODUSUA= '" & rs1.Fields("CODCLIE").Value & "')) AND NBULTO=0  GROUP BY  A.CODIGO, A.DESCRIPCION, B.C01, A.CODBULTO, A.NPRESUPUESTO "
        
        ActualizarRs4
        
        V_Vista = "SELECT   MAX(NBULTO) + 1 AS PROXN FROM  _TABLE_DESPACHO  WHERE ultbulto is null and (NPRESUPUESTO IN(SELECT NUMEROD FROM SAOPEI WHERE TIPOOPI='P' AND CODUSUA= '" & rs1.Fields("CODCLIE").Value & "'))"
        ActualizarRs5
    
    End If
End If
If rs5.BOF = True And rs5.EOF = True Then

Else

End If
If rs5.BOF = True And rs5.EOF = True Then
        vBloquearCliente = False
        Command1.Enabled = True
        DataGrid1(2).Enabled = True
    LBNBULTO.Caption = 1
Else

    If IsNull(rs5.Fields("PROXN").Value) = True Then
        LBNBULTO.Caption = "1"
        vBloquearCliente = False
        Command1.Enabled = True
        DataGrid1(2).Enabled = True
    Else
        LBNBULTO.Caption = rs5.Fields("PROXN").Value
        vBloquearCliente = True
        Command1.Enabled = False
        DataGrid1(2).Enabled = False
    End If
End If
'''''''''''''''''formato
dbindEx = 1
Set DataGrid1(dbindEx).DataSource = rs4
vTabla = "TABLE_DESPACHO"
FormatoGrid


'''''''''''''''''fin formato
End Sub



Private Sub Command10_Click()

Select Case vSubirBajar
    Case 0
        If rs3.EOF = True And rs3.BOF = True Then
            Exit Sub
        End If
        If rs3.EOF = False Then
            rs3.MoveNext
        End If
        If rs3.EOF = True Then
            rs3.MoveLast
        End If
    Case 1
        If rs4.EOF = True And rs4.BOF = True Then
            Exit Sub
        End If
        If rs4.EOF = False Then
            rs4.MoveNext
        End If
        If rs4.EOF = True Then
            rs4.MoveLast
        End If
    Case 2
        If rs1.EOF = True And rs1.BOF = True Then
            Exit Sub
        End If
        If rs1.EOF = False Then
            rs1.MoveNext
        End If
        If rs1.EOF = True Then
            rs1.MoveLast
        End If
End Select

End Sub

Private Sub Command11_Click()
End
End Sub

Private Sub Command2_Click()
Dim VCOD As String

If rs4.BOF = True And rs4.EOF = True Then
Else
    Command4.Enabled = True
    V_Vista = "SELECT     ValueInt FROM         SACORRELSIS WHERE     (FieldName = 'PrxBulto')"
    ActualizarRs6
    If Option1.Value = True Then
        V_Vista = "select codclie, descrip, isnull(direc1,'')+isnull(direc2,'') as direc, isnull(telef,'') as telef, codvend, codusua from safact where tipofac='a' and numerod in(select NPRESUPUESTO FROM _TABLE_DESPACHO WHERE NBULTO=0)"
    Else
        V_Vista = "select codclie, descrip, isnull(direc1,'')+isnull(direc2,'') as direc, isnull(telef,'') as telef, codvend, codusua from safact where tipofac='B' and numerod in(select NPRESUPUESTO FROM _TABLE_DESPACHO WHERE NBULTO=0)"
    End If
    ActualizarRs7
    'MEN123 = InputBox("", "", V_Vista)
    vCodclie = rs7.Fields("codclie").Value
    vNomClie = rs7.Fields("descrip").Value
    vDirecCliente = rs7.Fields("direc").Value
    vTelefCliente = rs7.Fields("telef").Value
    vCodVend = rs7.Fields("codvend").Value
    'vCodUsua = rs7.Fields("codusua").Value
    
    vNroBulto = rs6.Fields("VALUEINT").Value
    CN.Execute "UPDATE  _TABLE_DESPACHO SET CODBULTO=" & vNroBulto & " , NBULTO=" & LBNBULTO.Caption & " WHERE NBULTO=0"
    
    Nombre_Impresora = "Xprinter XP-236B"
    ' Establece la impresora que se utilizará para imprimir
    For Each Prt In Printers
        If Prt.DeviceName = Nombre_Impresora Then
            Set Printer = Prt
        End If
    Next
    ActualizarCampos
        RichTextBox1.Text = App.Path & "\DETALLE.rpt@" & vbCrLf & "@BULTOS@"
        RichTextBox1.SaveFile App.Path & "\REPORTE.INI", 1
        RichTextBox1.Text = " {_TABLE_DESPACHO.CODBULTO} = " & vNroBulto & "  AND {_TABLE_DESPACHO.NBULTO} = " & LBNBULTO.Caption - 1
        RichTextBox1.SaveFile App.Path & "\FILTRO.INI", 1
        RichTextBox1.Text = "SI"
        RichTextBox1.SaveFile App.Path & "\IMPRIMIR_DIRECTO.INI", 1
        MEN123 = Shell(App.Path & "\REPORTE.EXE", vbNormalFocus)
    Exit Sub
    
    Picture2.Picture = LoadPicture(App.Path & "\LOGO.JPG")
    Call Imprimir_Imagen(Picture2.Picture, 1, 0, 0)
    Printer.Print " "
    Printer.Print " "
    Printer.Print " "
    Printer.Print " "
    Printer.Print " "
    
    VCOD = Format(vNroBulto, "000000") & "-" & LBNBULTO.Caption
    FastQRCode VCOD, App.Path & "\Ejemplo.bmp"
        Printer.FontSize = 12
        Printer.FontName = "Lucida Console"
        Picture2.Picture = LoadPicture(App.Path & "\Ejemplo.bmp")

        Printer.Print "" & VCOD & "  "
        Printer.FontSize = 11
        Printer.Print "Vendedor:" & vCodVend
        Printer.Print "Usuario: " & vCodUsua
        Printer.FontSize = 8
        Printer.Print ""
        Printer.FontBold = True
        Printer.Print "" & "RIF: " & vCodclie
        
        Printer.FontSize = 7
        Printer.Print "" & Mid(vNomClie, 1, 32)
        Printer.Print "" & Mid(vNomClie, 33, 32)
        Printer.FontBold = False
        
        Printer.Print "" & Mid(vDirecCliente, 1, 35)
        Printer.Print "" & Mid(vDirecCliente, 36, 35)
        Printer.Print "" & Mid(vDirecCliente, 71, 35)
        Printer.Print "" & vTelefCliente
        'Printer.Print "" & Mid(Spreadsheet1.Cells(Spreadsheet1.ActiveCell.Row, 2), 1, 16)
        'Printer.Print "" & Trim(Mid(Spreadsheet1.Cells(Spreadsheet1.ActiveCell.Row, 2), 17, 16))
        'Printer.Print "" & Trim(Mid(Spreadsheet1.Cells(Spreadsheet1.ActiveCell.Row, 2), 34, 16))
        Call Imprimir_Imagen(Picture2.Picture, 1 * 0.25, 1950, 800)
        Printer.EndDoc
    
    
    
    'Command1 = True
    ActualizarCampos
End If
Text2.SetFocus
End Sub
Public Sub Imprimir_Imagen(pic As StdPicture, ZOOM As Double, pleft As Double, ptop As Double)
  Printer.Print
  
  'Dibujamos la imagen multiplicando el ancho y alto por el valor del ZOOM
  Printer.PaintPicture pic, pleft, ptop, pic.Width * ZOOM, _
                       pic.Height * ZOOM, 0, 0, pic.Width, _
                       pic.Height
  
  'Mandamos a imprimir la imagen
  'Printer.EndDoc
End Sub
Private Sub Command3_Click()
Call Exportar_DbGrid_Excel("Hoja1")
End Sub

Private Sub Command4_Click()
    CN.Execute "update sacorrelsis set valueint=valueint+1 where fieldname='PrxBulto'"
    CN.Execute "UPDATE _TABLE_DESPACHO SET ULTBULTO= 0" & LBNBULTO.Caption - 1 & " WHERE ULTBULTO IS NULL"

        RichTextBox1.Text = App.Path & "\RESUMEN.rpt@" & vbCrLf & "@BULTOS@"
        RichTextBox1.SaveFile App.Path & "\REPORTE.INI", 1
        RichTextBox1.Text = " {_TABLE_DESPACHO.CODBULTO} = " & vNroBulto & " "
        RichTextBox1.SaveFile App.Path & "\FILTRO.INI", 1
        RichTextBox1.Text = "SI"
        RichTextBox1.SaveFile App.Path & "\IMPRIMIR_DIRECTO.INI", 1
        MEN123 = Shell(App.Path & "\REPORTE.EXE", vbNormalFocus)
        Command4.Enabled = False
        'vBloquearCliente = False
        'Command1.Enabled = True
        'DataGrid1(2).Enabled = True
Text2.SetFocus
End Sub

Private Sub Command5_Click()
Dim VCAN As Double
Dim VCANS As String

If rs3.BOF = True And rs3.EOF = True Then
Else
    'vBloquearCliente = True
    'Command1.Enabled = False
    'DataGrid1(2).Enabled = False
    vPosicionProd = rs3.AbsolutePosition
    If rs3.EOF = True Then
        rs3.MoveLast
    End If
    VCAN = Text10.Text
    VCANS = Replace(VCAN, ",", ".")
    If Option1 = True Then
        CN.Execute " INSERT INTO  _TABLE_DESPACHO (TIPOFAC, NPRESUPUESTO, FECHA, CODIGO, DESCRIPCION, CANTIDAD, NBULTO, NROLINEA, codusua, codesta) VALUES " & _
        "('" & rs3.Fields("TIPOFAC").Value & "','" & rs3.Fields("NUMEROD").Value & "', GETDATE(), '" & rs3.Fields("CODITEM").Value & "', '" & rs3.Fields("DESCRIP1").Value & "', 0" & VCANS & ", 0, 0" & rs3.Fields("NROLINEA").Value & ", '" & vCodUsua & "','" & vCodEsta & "')"
        'Command1 = True
    Else
        CN.Execute " INSERT INTO  _TABLE_DESPACHO (TIPOFAC, NPRESUPUESTO, FECHA, CODIGO, DESCRIPCION, CANTIDAD, NBULTO, NROLINEA, codusua, codesta) VALUES " & _
        "('" & rs3.Fields("TIPOFAC").Value & "','" & rs3.Fields("NUMEROD").Value & "', GETDATE(), '" & rs3.Fields("CODITEM").Value & "', '" & rs3.Fields("DESCRIP1").Value & "', -1*0" & VCANS & ", 0, 0" & rs3.Fields("NROLINEA").Value & ", '" & vCodUsua & "','" & vCodEsta & "')"
    End If
    
    ActualizarCampos
    On Error Resume Next
    rs3.AbsolutePosition = vPosicionProd
End If
Text2.SetFocus
End Sub

Private Sub Command6_Click()
If rs4.BOF = True And rs4.EOF = True Then
Else
    CN.Execute "DELETE FROM  _TABLE_DESPACHO WHERE ID= " & rs4.Fields("ID").Value
    ActualizarCampos
End If
Text2.SetFocus
End Sub

Private Sub Command7_Click()
        RichTextBox1.Text = "NO"
        RichTextBox1.SaveFile App.Path & "\IMPRIMIR_DIRECTO.INI", 1
        MEN123 = Shell(App.Path & "\REPORTE.EXE", vbNormalFocus)
End Sub

Private Sub Command8_Click()
Form2.Show
End Sub

Private Sub Command9_Click()
Select Case vSubirBajar
    Case 0
        If rs3.EOF = True And rs3.BOF = True Then
            Exit Sub
        End If
        If rs3.BOF = False Then
            rs3.MovePrevious
        End If
        If rs3.BOF = True Then
            rs3.MoveFirst
        End If
    Case 1
        If rs4.EOF = True And rs4.BOF = True Then
            Exit Sub
        End If
        If rs4.BOF = False Then
            rs4.MovePrevious
        End If
        If rs4.BOF = True Then
            rs4.MoveFirst
        End If
    Case 2
        If rs1.EOF = True And rs1.BOF = True Then
            Exit Sub
        End If
        If rs1.BOF = False Then
            rs1.MovePrevious
        End If
        If rs1.BOF = True Then
            rs1.MoveFirst
        End If
End Select
End Sub

Private Sub DataGrid1_ColResize(Index As Integer, ByVal ColIndex As Integer, Cancel As Integer)
dbindEx = Index
GuardarFormatoGrid
End Sub



Private Sub DataGrid1_GotFocus(Index As Integer)
vSubirBajar = Index
End Sub

Private Sub DataGrid1_KeyUp(Index As Integer, KeyCode As Integer, Shift As Integer)
If Index = 0 Then
    If rs3.EOF = True And rs3.BOF = True Then
        Text10.Text = "0"
        Label2.Caption = ""
        Label4.Caption = ""
    Else
        'Text10.Text = rs3.Fields("CANTIDAD").Value
        Text10.Text = 1
        Label2.Caption = rs3.Fields("CODCLIE").Value & " " & rs3.Fields("DESCRIP").Value & vbCrLf & rs3.Fields("DIREC1").Value & " " & rs3.Fields("DIREC2").Value & " " & rs3.Fields("TELEF").Value
        Label4.Caption = "OEM: " & rs3.Fields("OEM").Value
        Label5.Caption = "CODIGO: " & " " & rs3.Fields("CODITEM").Value
        Label6.Caption = "CANTIDAD: " & " " & rs3.Fields("CANTIDAD").Value
        Label7.Caption = "FACT NRO: " & " " & rs3.Fields("NUMEROR").Value
    End If
End If
If Index = 2 Then
    ActualizarCampos
End If
    Text2.SetFocus
End Sub

Private Sub DataGrid1_RowColChange(Index As Integer, LastRow As Variant, ByVal LastCol As Integer)
If Index = 0 Then
    If rs3.EOF = True And rs3.BOF = True Then
        Text10.Text = "0"
        Label2.Caption = ""
        Label4.Caption = ""
    Else
        'Text10.Text = rs3.Fields("CANTIDAD").Value
        Text10.Text = 1
        If rs3.EOF = True Then
            rs3.MoveLast
        End If
        If rs3.BOF = True Then
            rs3.MoveFirst
        End If
        Label2.Caption = rs3.Fields("CODCLIE").Value & " " & rs3.Fields("DESCRIP").Value & vbCrLf & rs3.Fields("DIREC1").Value & " " & rs3.Fields("DIREC2").Value & " " & rs3.Fields("TELEF").Value
        Label4.Caption = "OEM: " & rs3.Fields("OEM").Value
        Label5.Caption = "CODIGO: " & rs3.Fields("CODITEM").Value
        Label6.Caption = "CANTIDAD: " & rs3.Fields("CANTIDAD").Value
        Label7.Caption = "FACT NRO: " & " " & rs3.Fields("NUMEROR").Value
    End If
End If
If Index = 2 Then
    ActualizarCampos
End If
Text2.SetFocus
End Sub

Private Sub Form_Activate()
Command1 = True
End Sub

Private Sub Form_Load()
'CONEXION_D = "FILE NAME=" & App.Path & "\CONEXION.dsn"
'CN.Open CONEXION_D

V_Vista = "SELECT TOP 1 * FROM SACORRELSIS where FieldName='PrxBulto' "
ActualizarRs3

If rs3.BOF = True Or rs3.EOF = True Then
    CN.Execute "INSERT INTO SACORRELSIS (  CodSucu, FieldName, ValueStr, ValueInt, ValueDec) VALUES  ('00000', 'PrxBulto',   '', 1, 0 )"
    CN.Execute "CREATE TABLE [dbo].[_TABLE_DESPACHO]([ID] [int] IDENTITY(1,1) NOT NULL, [NPRESUPUESTO] [nvarchar](50) NULL, [FECHA] [datetime] NULL," & _
    "[NroLinea] [int] NULL,  [CODIGO] [nvarchar](50) NULL,[DESCRIPCION] [nvarchar](250) NULL,    [CANTIDAD] [float] NULL, " & _
    "[NBULTO] [float] NULL,  [CODBULTO] [int] NULL, [ULTBULTO] [int] NULL, CONSTRAINT [PK__TABLE_DESPACHO] PRIMARY KEY CLUSTERED " & _
    " ([ID] ASC )WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY] ) ON [PRIMARY]"
End If
End Sub

Sub GuardarFormatoGrid()
Dim I_G As Integer
Dim V_Star As Integer
RichTextBox_Dis.Text = ""
vTabla = DataGrid1(dbindEx).Tag
    For I_G = 0 To DataGrid1(dbindEx).Columns.Count - 1
        RichTextBox_Dis.Text = RichTextBox_Dis.Text & Format(DataGrid1(dbindEx).Columns(I_G).Width, "00000")
        RichTextBox_Dis.Text = RichTextBox_Dis.Text & "-"
    Next I_G

    RichTextBox_Dis.SaveFile App.Path & "\" & vTabla & ".TXT", 1
End Sub

Sub FormatoGrid()
On Error Resume Next
RichTextBox_Dis.LoadFile (App.Path & "\" & vTabla & ".TXT")
DataGrid1(dbindEx).Tag = vTabla
If Err.Number = 75 Then
    For I_G = 0 To DataGrid1(dbindEx).Columns.Count - 1
        RichTextBox_Dis.Text = RichTextBox_Dis.Text & Format(DataGrid1(dbindEx).Columns(I_G).Width, "00000")
        RichTextBox_Dis.Text = RichTextBox_Dis.Text & "-"
    Next I_G

    RichTextBox_Dis.SaveFile App.Path & "\" & vTabla & ".TXT", 1
Else
    For I_G = 0 To DataGrid1(dbindEx).Columns.Count - 1
        
            If I_G = 0 Then
                V_Star = 1
                DataGrid1(dbindEx).Columns(I_G).Width = Mid(RichTextBox_Dis.Text, V_Star, 5)
            Else
                V_Star = V_Star + 6
                DataGrid1(dbindEx).Columns(I_G).Width = Mid(RichTextBox_Dis.Text, V_Star, 5)
            End If
        
    Next I_G
End If
End Sub



Private Sub Form_Resize()
If Form1.Width > 5000 Then
    DataGrid1(1).Width = Form1.Width - 300 - DataGrid1(1).Left
End If
If Form1.Height > 3000 Then
    DataGrid1(0).Height = Form1.Height - DataGrid1(0).Top - 500
End If
End Sub

Private Sub Form_Unload(Cancel As Integer)
End
End Sub

Private Sub Option1_Click()
Command1 = True
Form1.BackColor = RGB(255, 255, 255)
End Sub

Private Sub Option2_Click()
Command1 = True
Form1.BackColor = &H8080FF
End Sub

Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyPress = 13 Then
    KeyAscii = 0
    Command1 = True
End If
End Sub

Private Sub Text2_KeyPress(KeyAscii As Integer)
Dim VCAN As Double
Dim VCANS As String


If KeyAscii = 13 Then
    KeyAscii = 0
    If rs1.BOF = True Or rs1.EOF = True Then
        Exit Sub
    End If
    If Option1 = True Then
        If rs1.Fields("TIPO").Value = "FAC" Then
            V_Vista = "SELECT   A.TipoFac, A.NumeroD, A.FechaE, A.NroLinea, A.CodItem, A.Descrip1, A.Cantidad - ISNULL ((SELECT   SUM(CANTIDAD) AS Expr1 FROM _TABLE_DESPACHO WHERE (NPRESUPUESTO = A.NumeroD) AND (CODIGO = A.CodItem)), 0) AS CANTIDAD, C.CodClie, C.Descrip, C.Direc1, C.Direc2, C.Telef, B.CODVEND, B.CODUSUA " & _
            " FROM         SAITEMFAC AS A INNER JOIN SAFACT AS B ON A.TipoFac = B.TipoFac AND A.NumeroD = B.NumeroD INNER JOIN SACLIE AS C ON B.CodClie = C.CodClie " & _
            " WHERE  A.CODITEM='" & Text2.Text & "' AND   (A.TipoFac = 'A') AND (A.NumeroD = '" & rs1.Fields("NUMEROD").Value & "') AND (A.Cantidad - ISNULL ((SELECT   SUM(CANTIDAD) AS Expr1 FROM _TABLE_DESPACHO AS _TABLE_DESPACHO_1 WHERE    (NPRESUPUESTO = A.NumeroD) AND (CODIGO = A.CodItem)), 0) > 0)"
            
            ActualizarRs4
            
            V_Vista = "SELECT   MAX(NBULTO) + 1 AS PROXN FROM  _TABLE_DESPACHO  WHERE ultbulto is null and (NPRESUPUESTO IN(SELECT NUMEROD FROM SAFACT WHERE TIPOFAC='" & rs1.Fields("TIPOFAC").Value & "' AND CODCLIE= '" & rs1.Fields("CODCLIE").Value & "'))"
            ActualizarRs5
        Else
            V_Vista = "SELECT   A.TIPOOPI AS TipoFac, A.NumeroD, A.FechaE, A.NroLinea, A.CodItem, A.Descrip1, A.Cantidad - ISNULL ((SELECT   SUM(CANTIDAD) AS Expr1 FROM _TABLE_DESPACHO WHERE (NPRESUPUESTO = A.NumeroD) AND (CODIGO = A.CodItem)), 0) AS CANTIDAD,  B.AUTORI AS CodClie, B.Respon AS Descrip, '' AS Direc1, '' AS Direc2, '' AS Telef, '' AS CODVEND, B.CODUSUA " & _
            " FROM         SAITEMOPI AS A INNER JOIN SAOPEI AS B ON A.TipoOPI = B.TipoOPI AND A.NumeroD = B.NumeroD " & _
            " WHERE  A.CODITEM='" & Text2.Text & "' AND   (A.TipoOPI = 'P') AND (A.NumeroD = '" & rs1.Fields("NUMEROD").Value & "') AND (A.Cantidad - ISNULL ((SELECT   SUM(CANTIDAD) AS Expr1 FROM _TABLE_DESPACHO AS _TABLE_DESPACHO_1 WHERE    (NPRESUPUESTO = A.NumeroD) AND (CODIGO = A.CodItem)), 0) > 0)"
            
            ActualizarRs4
            
            V_Vista = "SELECT   MAX(NBULTO) + 1 AS PROXN FROM  _TABLE_DESPACHO  WHERE ultbulto is null and (NPRESUPUESTO IN(SELECT NUMEROD FROM SAOPEI WHERE TIPOOPI='P' AND CODUSUA= '" & rs1.Fields("CODCLIE").Value & "'))"
            ActualizarRs5
        
        End If
    Else
        V_Vista = "SELECT   A.TipoFac, A.NumeroD, A.FechaE, A.NroLinea, A.CodItem, A.Descrip1, A.Cantidad - ISNULL ((SELECT   SUM(CANTIDAD) AS Expr1 FROM _TABLE_DESPACHO WHERE (NPRESUPUESTO = A.NumeroD) AND (CODIGO = A.CodItem)), 0) AS CANTIDAD, C.CodClie, C.Descrip, C.Direc1, C.Direc2, C.Telef, B.CODVEND, B.CODUSUA " & _
        " FROM         SAITEMFAC AS A INNER JOIN SAFACT AS B ON A.TipoFac = B.TipoFac AND A.NumeroD = B.NumeroD INNER JOIN SACLIE AS C ON B.CodClie = C.CodClie " & _
        " WHERE  A.CODITEM='" & Text2.Text & "' AND   (A.TipoFac = 'B') AND (A.NumeroD = '" & rs1.Fields("NUMEROD").Value & "') AND (A.Cantidad - ISNULL ((SELECT   SUM(CANTIDAD) AS Expr1 FROM _TABLE_DESPACHO AS _TABLE_DESPACHO_1 WHERE    (NPRESUPUESTO = A.NumeroD) AND (CODIGO = A.CodItem)), 0) > 0)"
    
    End If
    ActualizarRs3
    
    
    
    If rs3.BOF = True And rs3.EOF = True Then
        Form1.BackColor = RGB(255, 0, 0)
    Else
        Form1.BackColor = RGB(255, 255, 255)
        
        VCAN = Text10.Text
        VCANS = Replace(VCAN, ",", ".")
        If Option1 = True Then
        CN.Execute " INSERT INTO  _TABLE_DESPACHO (TIPOFAC, NPRESUPUESTO, FECHA, CODIGO, DESCRIPCION, CANTIDAD, NBULTO, NROLINEA, codusua, codesta) VALUES " & _
        "('" & rs3.Fields("TIPOFAC").Value & "','" & rs3.Fields("NUMEROD").Value & "', GETDATE(), '" & rs3.Fields("CODITEM").Value & "', '" & rs3.Fields("DESCRIP1").Value & "', 0" & VCANS & ", 0, 0" & rs3.Fields("NROLINEA").Value & ", '" & vCodUsua & "','" & vCodEsta & "')"
        'Command1 = True
        Else
        CN.Execute " INSERT INTO  _TABLE_DESPACHO (TIPOFAC, NPRESUPUESTO, FECHA, CODIGO, DESCRIPCION, CANTIDAD, NBULTO, NROLINEA, codusua, codesta) VALUES " & _
        "('" & rs3.Fields("TIPOFAC").Value & "','" & rs3.Fields("NUMEROD").Value & "', GETDATE(), '" & rs3.Fields("CODITEM").Value & "', '" & rs3.Fields("DESCRIP1").Value & "', -1*0" & VCANS & ", 0, 0" & rs3.Fields("NROLINEA").Value & ", '" & vCodUsua & "','" & vCodEsta & "')"
        
        End If
        
        ActualizarCampos
    End If
    Text2.Text = ""
End If
End Sub

Private Sub Text2_KeyUp(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyDown Then
    Command10 = True
End If
If KeyCode = vbKeyUp Then
    Command9 = True
End If
End Sub
