VERSION 5.00
Object = "{3B7C8863-D78F-101B-B9B5-04021C009402}#1.2#0"; "RICHTX32.OCX"
Object = "{F9043C88-F6F2-101A-A3C9-08002B2F49FB}#1.2#0"; "COMDLG32.OCX"
Object = "{CDE57A40-8B86-11D0-B3C6-00A0C90AEA82}#1.0#0"; "MSDATGRD.OCX"
Object = "{48E59290-9880-11CF-9754-00AA00C00908}#1.0#0"; "MSINET.OCX"
Begin VB.Form FrmWebPedidos 
   BackColor       =   &H00FFFFFF&
   Caption         =   "Pedidos Web Ver 1.1"
   ClientHeight    =   1230
   ClientLeft      =   120
   ClientTop       =   720
   ClientWidth     =   4335
   LinkTopic       =   "Form7"
   MaxButton       =   0   'False
   ScaleHeight     =   1230
   ScaleWidth      =   4335
   Begin VB.CommandButton Command8 
      Caption         =   "Subir Productos"
      BeginProperty Font 
         Name            =   "Impact"
         Size            =   14.25
         Charset         =   0
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   735
      Left            =   1920
      TabIndex        =   11
      Top             =   0
      Width           =   2415
   End
   Begin MSComDlg.CommonDialog CommonDialog1 
      Left            =   3120
      Top             =   840
      _ExtentX        =   847
      _ExtentY        =   847
      _Version        =   393216
   End
   Begin RichTextLib.RichTextBox RichTextBox1 
      Height          =   375
      Left            =   120
      TabIndex        =   12
      Top             =   1080
      Visible         =   0   'False
      Width           =   855
      _ExtentX        =   1508
      _ExtentY        =   661
      _Version        =   393217
      Enabled         =   -1  'True
      TextRTF         =   $"FrmWebPedidos.frx":0000
   End
   Begin VB.Timer Timer2 
      Interval        =   1000
      Left            =   12120
      Top             =   480
   End
   Begin VB.Timer Timer1 
      Enabled         =   0   'False
      Interval        =   60000
      Left            =   12960
      Top             =   600
   End
   Begin VB.CommandButton Command3 
      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            =   14640
      Style           =   1  'Graphical
      TabIndex        =   2
      Top             =   0
      Visible         =   0   'False
      Width           =   1335
   End
   Begin VB.CommandButton Command5 
      BackColor       =   &H00FFFFFF&
      Caption         =   "Eliminar"
      BeginProperty Font 
         Name            =   "Impact"
         Size            =   14.25
         Charset         =   0
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   735
      Left            =   13200
      Style           =   1  'Graphical
      TabIndex        =   8
      Top             =   0
      Visible         =   0   'False
      Width           =   1455
   End
   Begin VB.CommandButton Command4 
      BackColor       =   &H00FFFFFF&
      Caption         =   "Aceptar"
      BeginProperty Font 
         Name            =   "Impact"
         Size            =   14.25
         Charset         =   0
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   735
      Left            =   11400
      Style           =   1  'Graphical
      TabIndex        =   3
      Top             =   0
      Visible         =   0   'False
      Width           =   1815
   End
   Begin VB.CommandButton Command6 
      Caption         =   "CONFIGURAR"
      BeginProperty Font 
         Name            =   "Impact"
         Size            =   14.25
         Charset         =   0
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   735
      Left            =   4320
      TabIndex        =   9
      Top             =   0
      Visible         =   0   'False
      Width           =   1575
   End
   Begin VB.CommandButton Command7 
      Caption         =   "Importar JSON"
      BeginProperty Font 
         Name            =   "Impact"
         Size            =   14.25
         Charset         =   0
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   735
      Left            =   0
      TabIndex        =   10
      Top             =   0
      Width           =   1935
   End
   Begin VB.CommandButton Command1 
      Caption         =   "Importar Web"
      BeginProperty Font 
         Name            =   "Impact"
         Size            =   14.25
         Charset         =   0
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   735
      Left            =   12720
      TabIndex        =   0
      Top             =   120
      Visible         =   0   'False
      Width           =   2175
   End
   Begin InetCtlsObjects.Inet Inet1 
      Left            =   18000
      Top             =   0
      _ExtentX        =   1005
      _ExtentY        =   1005
      _Version        =   393216
   End
   Begin VB.TextBox Text1 
      Height          =   375
      Left            =   120
      TabIndex        =   4
      Top             =   120
      Visible         =   0   'False
      Width           =   2535
   End
   Begin VB.CommandButton Command2 
      BackColor       =   &H00FFFFFF&
      Caption         =   "Buscar"
      BeginProperty Font 
         Name            =   "Impact"
         Size            =   14.25
         Charset         =   0
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   735
      Left            =   2760
      Style           =   1  'Graphical
      TabIndex        =   1
      Top             =   0
      Visible         =   0   'False
      Width           =   1455
   End
   Begin MSDataGridLib.DataGrid DataGrid1 
      Height          =   5655
      Index           =   0
      Left            =   0
      TabIndex        =   5
      Top             =   2880
      Visible         =   0   'False
      Width           =   7575
      _ExtentX        =   13361
      _ExtentY        =   9975
      _Version        =   393216
      AllowUpdate     =   0   'False
      HeadLines       =   1
      RowHeight       =   23
      BeginProperty HeadFont {0BE35203-8F91-11CE-9DE3-00AA004BB851} 
         Name            =   "MS Sans Serif"
         Size            =   8.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            =   12
         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 RichTextLib.RichTextBox RichTextBox_Dis 
      Height          =   255
      Left            =   0
      TabIndex        =   6
      Top             =   0
      Visible         =   0   'False
      Width           =   255
      _ExtentX        =   450
      _ExtentY        =   450
      _Version        =   393217
      TextRTF         =   $"FrmWebPedidos.frx":008B
   End
   Begin MSDataGridLib.DataGrid DataGrid1 
      Height          =   5655
      Index           =   1
      Left            =   7560
      TabIndex        =   7
      Top             =   2880
      Visible         =   0   'False
      Width           =   7695
      _ExtentX        =   13573
      _ExtentY        =   9975
      _Version        =   393216
      AllowUpdate     =   0   'False
      HeadLines       =   1
      RowHeight       =   23
      BeginProperty HeadFont {0BE35203-8F91-11CE-9DE3-00AA004BB851} 
         Name            =   "MS Sans Serif"
         Size            =   8.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            =   12
         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 Command9 
      Caption         =   "Enviar SQL"
      Height          =   375
      Left            =   1920
      TabIndex        =   13
      Top             =   800
      Width           =   2415
   End
End
Attribute VB_Name = "FrmWebPedidos"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Public Contador As Long ' Variable para contar el tiempo acumulado
Public Contador2 As Long




Private Sub Command9_Click()
Me.CommonDialog1.ShowOpen
RichTextBox1.LoadFile Me.CommonDialog1.FileName
vMySqlQuery = RichTextBox1.text
SubirQueryHTTP
End Sub

Private Sub Form_Unload(Cancel As Integer)
Cancel = True
FrmWebPedidos.WindowState = 1
End Sub

Private Sub Timer1_Timer()
    Contador = Contador + 1 ' Incrementamos el contador cada minuto
    If Contador >= 60 Then ' 240 minutos son 4 horas
        EjecutarFuncion ' Llamamos a la función cuando pasan 4 horas
        Contador = 0 ' Reiniciamos el contador
    End If
    Contador2 = Contador2 + 1 ' Incrementamos el contador cada minuto
    If Contador2 >= 5 Then ' 240 minutos son 4 horas
        SubirCierreDia
        SubirVentasDia
        
        Contador2 = 0 ' Reiniciamos el contador
    End If
End Sub

Private Sub EjecutarFuncion()
    ' Aquí va el código de la función que deseas ejecutar
    Command8 = True
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 Command1_Click()
Dim strURL As String
Dim vDataUrl As String
Dim vFilaURL() As String
Dim vCampoUrl() As String
Command1.Enabled = False
    'CN.Execute "truncate table _PAGINAWEB_PEDIDO"
    'Inet1.OpenURL "http://appsnac.com/a_clientes/" & vRutaFTP & "/pedidos.php"
    'MEN123 = MsgBox("BAJANDO PEDIDO", vbInformation)
    strURL = "http://appsnac.com/a_clientes/" & vRutaFTP & "/pedidos.php"
    vDataUrl = Inet1.OpenURL(strURL)
    vFilaURL = Split(vDataUrl, "<br>")
    While x < UBound(vFilaURL)
        vCampoUrl = Split(vFilaURL(x), ";")
        On Error Resume Next
        CN.Execute "insert into _PAGINAWEB_PEDIDO (nombre, rif, codigo, cantidad, cantidad2, cantidad1) values ('" & vCampoUrl(1) & "','" & vCampoUrl(2) & "','" & vCampoUrl(3) & "',0" & vCampoUrl(4) & ",0" & vCampoUrl(5) & ",0" & vCampoUrl(6) & ") "
        x = x + 1
    Wend
    '''''''''''''''BULTO
    x = 0
    'CN.Execute "truncate table _PAGINAWEB_PEDIDOE"
    'Inet1.OpenURL "http://appsnac.com/a_clientes/" & vRutaFTP & "/pedidose.php"
    'MEN123 = MsgBox("BAJANDO PEDIDO", vbInformation)
    strURL = "http://appsnac.com/a_clientes/" & vRutaFTP & "/pedidose.php"
    vDataUrl = Inet1.OpenURL(strURL)
    vFilaURL = Split(vDataUrl, "<br>")
    While x < UBound(vFilaURL)
        vCampoUrl = Split(vFilaURL(x), ";")
        On Error Resume Next
        CN.Execute "insert into _PAGINAWEB_PEDIDOE (nombre, rif, codigo,unidad, cantemp, cantidad, cantidad2, cantidad1) values ('" & vCampoUrl(1) & "','" & vCampoUrl(2) & "','" & vCampoUrl(3) & "','" & vCampoUrl(4) & "',0" & vCampoUrl(5) & ",0" & vCampoUrl(6) & ",0" & vCampoUrl(7) & ",0" & vCampoUrl(8) & ") "
        x = x + 1
    Wend

Command2 = True
Command1.Enabled = True
End Sub


Private Sub Command2_Click()
'V_Vista = "select RIF, MAX(NOMBRE) AS NOMBRE FROM _PAGINAWEB_PEDIDO WHERE NOMBRE+RIF LIKE '%" & Replace(Text1.text, "*", "%") & "%' group by rif ORDER BY NOMBRE"
V_Vista = "SELECT    a.rif,  a.nombre, SUM((a.cantidad * b.PRECIOI3 + a.cantidad2 * b.PRECIOI2) + a.cantidad1 * b.PRECIOI1) AS TOTAL FROM _PAGINAWEB_PEDIDO AS a INNER JOIN SAPROD AS b ON a.codigo = b.CodProd GROUP BY a.nombre, a.rif " & _
" UNION  " & _
"SELECT      rif , nombre, 0 AS total  FROM         _PAGINAWEB_PEDIDOE"
ActualizarRs3
'MEN123 = InputBox("", "", V_Vista)
'''''''''''''''''formato
dbindEx = 0
Set DataGrid1(dbindEx).DataSource = rs3
vTabla = "PEDIDOS WEB"
FormatoGrid
ActualizarDetalleWeb
'''''''''''''''''fin formato
DataGrid1(0).Columns(2).NumberFormat = "#,##0.00"
DataGrid1(0).Columns(2).Alignment = dbgRight

End Sub
Sub ActualizarDetalleWeb()
If rs3.BOF = True Or rs3.EOF = True Then
    V_Vista = "select top 0 '' as rif, '' as nombre, '' as codprod"
        ActualizarRs4
    dbindEx = 1
    Set DataGrid1(dbindEx).DataSource = rs4
    Exit Sub
Else
    'V_Vista = "SELECT  _PAGINAWEB_PEDIDO.rif, _PAGINAWEB_PEDIDO.nombre, SAPROD.CodProd, SAPROD.Descrip, _PAGINAWEB_PEDIDO.cantidad, _PAGINAWEB_PEDIDO.cantidad2, _PAGINAWEB_PEDIDO.cantidad1, SAPROD.PRECIOI3, SAPROD.Precio3, SAPROD.Refere FROM  _PAGINAWEB_PEDIDO INNER JOIN SAPROD ON _PAGINAWEB_PEDIDO.codigo = SAPROD.CodProd where _PAGINAWEB_PEDIDO.rif='" & rs3.Fields("rif").Value & "'"
'    V_Vista = "SELECT     a.rif, a.nombre, c.CODPROD, c.DESCRIP, a.unidad, a.cantemp, a.cantidad, a.cantidad2, a.cantidad1, c.PRECIOI3 AS Precioi3, c.PRECIO3, c.PRECIOI2 AS Precioi2, c.PRECIO2, c.PRECIOI1 AS Precioi1,  c.PRECIO1, c.barra as Referencia FROM         _PAGINAWEB_PEDIDOE AS a INNER JOIN _PROD_UND AS c ON a.codigo = c.CODPROD AND a.unidad = c.UNIDAD WHERE     (a.rif = '" & rs3.Fields("rif").Value & "') union " & _
'    "SELECT     a.rif, a.nombre, b.CodProd, b.Descrip, '' as unidad, 0 as cantemp, a.cantidad, a.cantidad2, a.cantidad1, b.PRECIOI3 AS Precioi3, b.Precio3, b.PRECIOI2 AS Precioi2, b.Precio2, b.PRECIOI1 AS Precioi1, b.Precio1, b.Refere FROM _PAGINAWEB_PEDIDO AS a INNER JOIN SAPROD AS b ON a.codigo = b.CodProd WHERE (a.rif = '" & rs3.Fields("rif").Value & "')"
V_Vista = "SELECT     a.rif, a.nombre, c.CODPROD, c.DESCRIP, a.unidad, a.cantemp, 'TipoPrecio' = CASE WHEN a.cantidad > 0 THEN 'Precio3' WHEN a.Cantidad2> 0 THEN 'Precio2' WHEN a.Cantidad1> 0 THEN 'Precio1' END, a.cantidad+ a.cantidad2+ a.cantidad1 as CantTot, 'Precio' = CASE WHEN a.cantidad > 0 THEN c.PRECIOI3 WHEN a.Cantidad2> 0 THEN c.PRECIOI2 WHEN a.Cantidad1> 0 THEN c.PRECIOI1 END, " & _
" 'Total' = CASE WHEN a.cantidad > 0 THEN c.PRECIOI3*a.cantidad WHEN a.Cantidad2> 0 THEN c.PRECIOI2*a.cantidad2 WHEN a.Cantidad1> 0 THEN c.PRECIOI1*a.cantidad1 END, a.cantidad, a.cantidad2, a.cantidad1 , c.PRECIOI3 AS Precioi3, c.PRECIO3, c.PRECIOI2 AS Precioi2, c.PRECIO2, c.PRECIOI1 AS Precioi1,  c.PRECIO1, c.barra as Referencia FROM _PAGINAWEB_PEDIDOE AS a INNER JOIN _PROD_UND AS c ON a.codigo = c.CODPROD AND a.unidad = c.UNIDAD WHERE     (a.rif = 'J303442366') " & _
" union SELECT     a.rif, a.nombre, b.CodProd, b.Descrip, '' as unidad, 0 as cantemp, 'TipoPrecio' = CASE WHEN a.cantidad > 0 THEN 'Precio3' WHEN a.Cantidad2> 0 THEN 'Precio2' WHEN a.Cantidad1> 0 THEN 'Precio1' END, a.cantidad+ a.cantidad2+ a.cantidad1 as CantTot, 'Precio' = CASE WHEN a.cantidad > 0 THEN b.PRECIOI3  WHEN a.Cantidad2> 0 THEN b.PRECIOI2 WHEN a.Cantidad1> 0 THEN b.PRECIOI1 END, 'Total' = CASE WHEN a.cantidad > 0 THEN b.PRECIOI3*a.cantidad WHEN a.Cantidad2> 0 THEN b.PRECIOI2*a.cantidad2 " & _
" WHEN a.Cantidad1> 0 THEN b.PRECIOI1*a.cantidad1 END, a.cantidad, a.cantidad2, a.cantidad1 , b.PRECIOI3 AS Precioi3, b.Precio3, b.PRECIOI2 AS Precioi2, b.Precio2, b.PRECIOI1 AS Precioi1, b.Precio1, b.Refere FROM _PAGINAWEB_PEDIDO AS a INNER JOIN SAPROD AS b ON a.codigo = b.CodProd WHERE (a.rif = '" & rs3.Fields("RIF").Value & "')"
    ActualizarRs4
    dbindEx = 1
    Set DataGrid1(dbindEx).DataSource = rs4
    vTabla = "PEDIDOS WEB_pedidoe"
    FormatoGrid
End If
If rs4.BOF = True Or rs4.EOF = True Then
Else
DataGrid1(1).Columns(8).NumberFormat = "#,##0.00"
DataGrid1(1).Columns(8).Alignment = dbgRight
DataGrid1(1).Columns(9).NumberFormat = "#,##0.00"
DataGrid1(1).Columns(9).Alignment = dbgRight
End If
End Sub

Private Sub Command3_Click()

FrmWebPedidos.Visible = False
End Sub

Private Sub Command4_Click()
Dim i As Long
Dim vLineaPedido As Long
Dim vTipPrecioWeb As String
If rs3.BOF = True Or rs3.EOF = True Then
Else
    If rs4.BOF = True Or rs4.EOF = True Then
        Exit Sub
    End If
    rs4.MoveFirst
    Me.Inet1.OpenURL "http://" & vServidorFTP & "/a_clientes/" & vRutaFTP & "/limpiar.php?rif=" & rs3.Fields("RIF").Value
    If FrmWebPedidos.Visible = True Then
        DataGrid1(0).SetFocus
    End If
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    FRMFAC.Spreadsheet1.Range("A2:A10000", "OK2:OK10000") = ""
    i = 2
    While rs4.EOF = False And vLineaPedido < vLimiteLineas
        vLineaPedido = vLineaPedido + 1

        If FRMFAC.Check1.Value = 1 Then
            V_Vista = "SELECT CODPROD, DESCRIP,  PRECIOI3, PRECIOI2, PRECIOI1, 0 AS ESSERV, ESEXENTO, DOLAR AS COSTO, (SELECT EXISTEN FROM SAEXIS WHERE CODUBIC='" & FRMFAC.Text2(2).text & "' AND SAEXIS.CODPROD=SAPROD.CODPROD) AS EXISTEN, DESCRIP3 FROM SAPROD WHERE CODPROD='" & rs4.Fields("CODPROD").Value & "' UNION " & _
            "SELECT CODSERV, DESCRIP,  PRECIOI3, PRECIOI2, PRECIOI1, 1 AS ESSERV, ESEXENTO, COSTO, 999999 AS EXISTEN, DESCRIP3 FROM SASERV WHERE CODSERV='" & rs4.Fields("CODPROD").Value & "'"
        Else
            V_Vista = "SELECT CODPROD, DESCRIP, PRECIO3 AS PRECIOI3, PRECIOI2, PRECIOI1, 0 AS ESSERV, ESEXENTO, DOLAR AS COSTO, (SELECT EXISTEN FROM SAEXIS WHERE CODUBIC='" & FRMFAC.Text2(2).text & "' AND SAEXIS.CODPROD=SAPROD.CODPROD) AS EXISTEN, DESCRIP3 FROM SAPROD WHERE CODPROD='" & rs4.Fields("CODPROD").Value & "' UNION " & _
            "SELECT CODSERV, DESCRIP, PRECIO3 AS PRECIOI3, PRECIOI2, PRECIOI1, 1 AS ESSERV, ESEXENTO, COSTO, 999999 AS EXISTEN, DESCRIP3 FROM SASERV WHERE CODSERV='" & rs4.Fields("CODPROD").Value & "'"
        End If
        ActualizarRs5
        If rs5.BOF = True And rs5.EOF = True Then
            V_Vista = "SELECT CODPROD, DESCRIP, PRECIOI3, 0 AS ESSERV, ESEXENTO, DOLAR AS COSTO, (SELECT EXISTEN FROM SAEXIS WHERE CODUBIC='" & FRMFAC.Text2(2).text & "' AND SAEXIS.CODPROD=SAPROD.CODPROD) AS EXISTEN, DESCRIP3 FROM SAPROD WHERE CODPROD in(select codprod from sacodbar where codalte='" & rs4.Fields("CODPROD").Value & "'  )"
            ActualizarRs5
        End If
        If rs5.BOF = True And rs5.EOF = True Then
            
        Else
            FRMFAC.Spreadsheet1.Cells(i, 1) = "'" & rs5.Fields("CODPROD").Value
            If rs4.Fields("cantemp").Value > 0 Then
                FRMFAC.Spreadsheet1.Cells(i, 2) = "'" & rs4.Fields("DESCRIP").Value
            Else
                FRMFAC.Spreadsheet1.Cells(i, 2) = "'" & rs5.Fields("DESCRIP").Value
            End If
            If rs4.Fields("CANTIDAD").Value > 0 Then
                FRMFAC.Spreadsheet1.Cells(i, 3) = rs4.Fields("CANTIDAD").Value
                If rs4.Fields("cantemp").Value > 0 Then
                    FRMFAC.Spreadsheet1.Cells(i, 5) = rs4.Fields("PRECIOI3").Value
                Else
                    FRMFAC.Spreadsheet1.Cells(i, 5) = rs5.Fields("PRECIOI3").Value
                End If
                vTipPrecioWeb = "Precio3"
                FRMFAC.Spreadsheet1.Cells(i, 24) = rs5.Fields("PRECIOI3").Value
                If vBoquearAdvertirExistencia = "SI" Then
                    'FRMFAC.Spreadsheet1.Cells(i, 15) = rs4.Fields("CANTIDAD").Value
                    'FRMFAC.Spreadsheet1.Cells(i, 15) = 1
                End If
            ElseIf rs4.Fields("CANTIDAD2").Value > 0 Then
                FRMFAC.Spreadsheet1.Cells(i, 3) = rs4.Fields("CANTIDAD2").Value
                If vBoquearAdvertirExistencia = "SI" Then
                    'FRMFAC.Spreadsheet1.Cells(i, 15) = rs4.Fields("CANTIDAD2").Value
                    'FRMFAC.Spreadsheet1.Cells(i, 15) = 1
                End If
                If rs4.Fields("cantemp").Value > 0 Then
                    If rs5.Fields("PRECIOI2").Value > 0 Then
                        FRMFAC.Spreadsheet1.Cells(i, 5) = rs4.Fields("PRECIOI2").Value
                        vTipPrecioWeb = "Precio2"
                        FRMFAC.Spreadsheet1.Cells(i, 24) = rs5.Fields("PRECIOI2").Value
                    Else
                        FRMFAC.Spreadsheet1.Cells(i, 5) = rs4.Fields("PRECIOI3").Value
                        vTipPrecioWeb = "Precio3"
                        FRMFAC.Spreadsheet1.Cells(i, 24) = rs5.Fields("PRECIOI3").Value
                    End If
                Else
                    If rs5.Fields("PRECIOI2").Value > 0 Then
                        FRMFAC.Spreadsheet1.Cells(i, 5) = rs5.Fields("PRECIOI2").Value
                        vTipPrecioWeb = "Precio2"
                        FRMFAC.Spreadsheet1.Cells(i, 24) = rs5.Fields("PRECIOI2").Value
                    Else
                        FRMFAC.Spreadsheet1.Cells(i, 5) = rs5.Fields("PRECIOI3").Value
                        vTipPrecioWeb = "Precio3"
                        FRMFAC.Spreadsheet1.Cells(i, 24) = rs5.Fields("PRECIOI3").Value
                    End If
                End If
            ElseIf rs4.Fields("CANTIDAD1").Value > 0 Then
                FRMFAC.Spreadsheet1.Cells(i, 3) = rs4.Fields("CANTIDAD1").Value
                If vBoquearAdvertirExistencia = "SI" Then
                    'FRMFAC.Spreadsheet1.Cells(i, 15) = rs4.Fields("CANTIDAD1").Value
                    'FRMFAC.Spreadsheet1.Cells(i, 15) = 1
                End If
                If rs4.Fields("cantemp").Value > 0 Then
                    If rs5.Fields("PRECIOI1").Value > 0 Then
                        FRMFAC.Spreadsheet1.Cells(i, 5) = rs4.Fields("PRECIOI1").Value
                        vTipPrecioWeb = "Precio1"
                        FRMFAC.Spreadsheet1.Cells(i, 24) = rs5.Fields("PRECIOI1").Value
                    Else
                        FRMFAC.Spreadsheet1.Cells(i, 5) = rs4.Fields("PRECIOI3").Value
                        vTipPrecioWeb = "Precio3"
                        FRMFAC.Spreadsheet1.Cells(i, 24) = rs5.Fields("PRECIOI1").Value
                    End If
                Else
                    If rs5.Fields("PRECIOI1").Value > 0 Then
                        FRMFAC.Spreadsheet1.Cells(i, 5) = rs5.Fields("PRECIOI1").Value
                        vTipPrecioWeb = "Precio1"
                        FRMFAC.Spreadsheet1.Cells(i, 24) = rs5.Fields("PRECIOI1").Value
                    Else
                        FRMFAC.Spreadsheet1.Cells(i, 5) = rs5.Fields("PRECIOI3").Value
                        vTipPrecioWeb = "Precio3"
                        FRMFAC.Spreadsheet1.Cells(i, 24) = rs5.Fields("PRECIOI3").Value
                    End If
                End If
            End If
            If rs4.Fields("cantemp").Value > 0 Then
                FRMFAC.Spreadsheet1.Cells(i, 15) = rs4.Fields("cantemp").Value    'cantemp
                FRMFAC.Spreadsheet1.Cells(i, 16) = rs5.Fields("descrip").Value    'descrip empaq
                FRMFAC.Spreadsheet1.Cells(i, 17) = FRMFAC.Spreadsheet1.Cells(i, 5) / rs4.Fields("cantemp").Value     'precio unitario precio/cantemp
                FRMFAC.Spreadsheet1.Cells(i, 18) = rs4.Fields("unidad").Value    'bulto (unidad)
                FRMFAC.Spreadsheet1.Cells(i, 20) = vTipPrecioWeb   'Precio3'

            End If
            FRMFAC.Spreadsheet1.Cells(i, 6) = "=C" & i & "*E" & i
                If FRMFAC.Check1.Value = 0 Then
                    FRMFAC.Spreadsheet1.Cells(i, 7) = "=F" & i & "/" & Replace(vFactor, ",", ".")
                Else
                    FRMFAC.Spreadsheet1.Cells(i, 7) = "=F" & i & "*" & Replace(vFactor, ",", ".")
                End If
            'FRMFAC.Spreadsheet1.Cells(i, 7) = FRMFAC.Spreadsheet1.Cells(i, 6) * vFactor
            FRMFAC.Spreadsheet1.Cells(i, 89) = rs5.Fields("CODPROD").Value
            FRMFAC.Spreadsheet1.Cells(i, 119) = rs5.Fields("DESCRIP3").Value
            FRMFAC.Spreadsheet1.Cells(i, 8) = rs5.Fields("ESEXENTO").Value
            FRMFAC.Spreadsheet1.Cells(i, 9) = rs5.Fields("ESSERV").Value
            FRMFAC.Spreadsheet1.Cells(i, 10) = rs5.Fields("COSTO").Value
            FRMFAC.Spreadsheet1.Cells(i, 11) = 0
            FRMFAC.Spreadsheet1.Cells(i, 12) = 0
            FRMFAC.Spreadsheet1.Cells(i, 13) = rs5.Fields("EXISTEN").Value
            FRMFAC.Spreadsheet1.Cells(i, 14) = "=M" & i & "-C" & i

            FRMFAC.Spreadsheet1.Cells(i, 20) = rs4.Fields("tipoprecio").Value
            FRMFAC.Spreadsheet1.Cells(i, 21) = 0
            
            'FRMFAC.Spreadsheet1.Cells(i, 90) = "VERIFICADO"
            FRMFAC.Spreadsheet1.Cells(i, 100) = "'" & rs5.Fields("CODPROD").Value
            FRMFAC.Spreadsheet1.Cells(i, 101) = "'" & rs5.Fields("DESCRIP").Value
            FRMFAC.Spreadsheet1.Cells(i, 300) = Right(rs4.Fields("tipoprecio").Value, 1)     'PRECIO 3 POR DEFECTO
            FRMFAC.Spreadsheet1.Cells(i, 400) = rs5.Fields("COSTO").Value * rs4.Fields("CANTEMP").Value
            FRMFAC.Spreadsheet1.Cells(i, 401) = 0
            vPendienteElimiarPedidoWeb = rs3.Fields("rif").Value
                    
                    Select Case rs4.Fields("TipoPrecio").Value
                        Case "Precio3"
                            If FRMFAC.Combo3.text = "NEGRO" Then
                                FRMFAC.Spreadsheet1.Cells(i, 5).Interior.Color = RGB(0, 0, 0)
                            Else
                                FRMFAC.Spreadsheet1.Cells(i, 5).Interior.ColorIndex = 0
                            End If
                            'Spreadsheet1.Cells(vROW, 300) = "3"
                        Case "Precio2"
                            FRMFAC.Spreadsheet1.Cells(i, 5).Interior.Color = RGB(69, 123, 156)
                            'Spreadsheet1.Cells(vROW, 300) = "2"
                        Case "Precio1"
                            FRMFAC.Spreadsheet1.Cells(i, 5).Interior.Color = RGB(255, 89, 94)
                            'Spreadsheet1.Cells(vROW, 300) = "1"
                        Case "Precio4"
                            FRMFAC.Spreadsheet1.Cells(i, 5).Interior.Color = RGB(94, 210, 100)
                            'Spreadsheet1.Cells(vROW, 300) = "4"
                        Case "Precio5"
                            FRMFAC.Spreadsheet1.Cells(i, 5).Interior.Color = RGB(103, 108, 153)
                            'Spreadsheet1.Cells(vROW, 300) = "5"
                        Case "Precio6"
                            FRMFAC.Spreadsheet1.Cells(i, 5).Interior.Color = RGB(38, 88, 168)
                            'Spreadsheet1.Cells(vROW, 300) = "6"

                    End Select
                    
            
            i = i + 1
        End If
        rs4.MoveNext
    Wend
    ActClient
    
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

    FrmWebPedidos.Visible = False
End If

End Sub

Sub ActClient()
V_Vista = "select codclie, ISNULL(descrip,'') AS DESCRIP, ISNULL(direc1,'') AS DIREC1, ISNULL(direc2,'') AS DIREC2, ISNULL(telef,'') AS TELEF, " & _
" esCredito, id3, DiasCred, LimiteCred, saldo, isnull(codvend,'" & vCodVendInicial & "') as codvend, Represent, PorcentajeServicio, ISNULL(CLAVE,'') AS CLAVE,isnull(CodZona,'') as CodZona from saclie where codclie='" & rs3.Fields("rif").Value & "' "
'MsgBox (V_Vista)
ActualizarRsClie
If rsClie.BOF = True And rsClie.EOF = True Then
    'Text2(0).Text = ""
    FRMFAC.Label1(0).Caption = ""
    FRMFAC.Label1(0).ToolTipText = ""
    FRMFAC.Text2(0).text = ""
    vSaldoAnticipo = 0
Else
    'MsgBox (rsClie.Fields("codvend").Value)
    FRMFAC.Text2(0).text = rsClie.Fields("codclie").Value
    If vUsuaVend = "CLIENTE" Then
        If rsClie.Fields("CODVEND").Value <> "" Then
            FRMFAC.Text2(1).text = rsClie.Fields("codvend").Value
            vCodVend = rsClie.Fields("CODVEND").Value
            BuscarVend
        End If
    End If
    FRMFAC.Label1(0).Caption = rsClie.Fields("descrip").Value
    FRMFAC.Label1(0).ToolTipText = rsClie.Fields("DESCRIP").Value
    vDiasCred = rsClie.Fields("DiasCred").Value
    vLimiteCred = rsClie.Fields("LimiteCred").Value
    'vSaldoClienteTemp = rsClie.Fields("saldo").Value
    If rsClie.Fields("codclie").Value <> "01" Then
        V_Vista = "select sum(saldo/factor) as SALDO, DATEDIFF (DAY, MIN(FECHAE) , GETDATE() ) AS DIAS from saacxc where factor>0.01 and codclie='" & rsClie.Fields("codclie").Value & "' and saldo>=0.01"
        ActualizarRs2
        If rs2.BOF = True Or rs2.EOF = True Then
            vSaldoClienteTemp = 0
        Else
            If IsNull(rs2.Fields("saldo").Value) = False Then
                vSaldoClienteTemp = rs2.Fields("SALDO").Value
                vTDiasCred = rs2.Fields("DIAS").Value
            Else
                vSaldoClienteTemp = 0
            End If
        End If
        V_Vista = "SELECT     SUM(SAFACT.Signo * SAIPAVTA.Impuesto) AS total  FROM SAIPAVTA INNER JOIN  SAFACT ON SAIPAVTA.TipoFac = SAFACT.TipoFac AND SAIPAVTA.NumeroD = SAFACT.NumeroD " & _
        " WHERE     (SAIPAVTA.CodPago = 'AD') AND (SAIPAVTA.TipoFac IN ('A', 'B','R')) AND (SAFACT.CodClie = '" & FRMFAC.Text2(0).text & "') "
        ActualizarRs2
        If IsNull(rs2.Fields("total").Value) = True Then
            vSaldoAnticipo = 0
        Else
            vSaldoAnticipo = rs2.Fields("total").Value
        End If
    Else
        vSaldoAnticipo = 0
    End If

End If
End Sub

Sub BuscarVend()
V_Vista = "Select codvend, descrip from savend where codvend ='" & FRMFAC.Text2(1).text & "'"
ActualizarRs2
If rs2.BOF = True Or rs2.EOF = True Then
    FRMFAC.Label1(1).Tag = "01"
    FRMFAC.Label1(1).Caption = ""
    FRMFAC.Text2(1).text = "01"
    vCodVend = "01"
Else
    FRMFAC.Label1(1).Caption = rs2.Fields("descrip").Value
    FRMFAC.Label1(1).Tag = rs2.Fields("codvend").Value
    vCodVend = rs2.Fields("codvend").Value
    FRMFAC.Text2(1).text = rs2.Fields("CODVEND").Value
End If
End Sub

Private Sub Command5_Click()
If rs3.BOF = True And rs3.EOF = True Then
Else
    MEN123 = MsgBox("Desea Eliminar", vbYesNo)
    If MEN123 = vbYes Then
        Me.Inet1.OpenURL "http://" & vServidorFTP & "/a_clientes/" & vRutaFTP & "/limpiar.php?rif=" & rs3.Fields("RIF").Value
        CN.Execute "delete from _PAGINAWEB_PEDIDO where rif='" & rs3.Fields("rif").Value & "'"
        CN.Execute "delete from _PAGINAWEB_PEDIDOE where rif='" & rs3.Fields("rif").Value & "'"
        Command2 = True
    End If
End If
End Sub

Private Sub Command6_Click()
'FrmWebConfig.Height = 1740
FrmWebConfig.Show 1
'FrmWebActualizar.Show 1
End Sub

Private Sub Command7_Click()
Dim strURL As String
Dim vDataUrl As String
Dim vFilaURL() As String
Dim vCampoUrl() As String
Command7.Enabled = False
    CN.Execute "truncate table _PAGINAWEB_PEDIDO"
    'Inet1.OpenURL "http://appsnac.com/a_clientes/" & vRutaFTP & "/pedidos.php"
    'MEN123 = MsgBox("BAJANDO PEDIDO", vbInformation)
    strURL = "http://" & vServidorFTP & "/pedidos.php"
    vDataUrl = Inet1.OpenURL(strURL)
    vFilaURL = Split(vDataUrl, "<br>")
    While x < UBound(vFilaURL)
        vCampoUrl = Split(vFilaURL(x), ";")
        On Error Resume Next
        CN.Execute "insert into _PAGINAWEB_PEDIDO (nombre, rif, codigo, cantidad, cantidad2, cantidad1) values ('" & vCampoUrl(1) & "','" & vCampoUrl(2) & "','" & vCampoUrl(3) & "',0" & vCampoUrl(4) & ",0" & vCampoUrl(4) & ",0" & vCampoUrl(4) & ") "
        x = x + 1
    Wend


Command2 = True
Command7.Enabled = True
End Sub

Private Sub Command8_Click()
Command8.Enabled = False
SubirUsuario
SubirProductos
SubirCierre
SubirVentas
SubirCxc
SubirCompras
SubirCxp
Command8.Enabled = True

End Sub
Sub SubirUsuario()
Dim vEncSql As String
Dim i As Long
Dim x As Long
vRif = Mid(vRutaFTP, 1, 20)

vMySqlQuery = "truncate table " & vRif & "usuario;"
SubirQueryHTTP

    V_Vista = "SELECT codusua, descrip, email as clave, movil as nivel from ssusrs"

'MEN123 = InputBox("", "", V_Vista)
ActualizarRs3
    
    vEncSql = " Insert into " & vRif & "usuario (   username, nombre, password, nivel) values "
    i = 1
    x = 1
    While rs3.EOF = False
        If i = 1 Then
            vQueryFTP = vEncSql
        End If
        vQueryFTP = vQueryFTP & "('" & rs3.Fields("codusua").Value & "', '" & rs3.Fields("descrip").Value & "','" & rs3.Fields("clave").Value & "','" & rs3.Fields("nivel").Value & "')," & vbCrLf
        rs3.MoveNext
        If i = 100 Then
            vMySqlQuery = Mid(vQueryFTP, 1, Len(vQueryFTP) - 3) & ";"

            SubirQueryHTTP
            RichTextBox1.text = vMySqlQuery
            RichTextBox1.SaveFile App.Path & "\QUERY" & x & ".TXT", 1
            x = x + 1
            i = 0
        End If
        i = i + 1
    Wend
x = x + 1

If vQueryFTP <> "" Then
    vQueryFTP = Mid(vQueryFTP, 1, Len(vQueryFTP) - 3)
    vMySqlQuery = vQueryFTP
    RichTextBox1.text = vQueryFTP
    RichTextBox1.SaveFile App.Path & "\" & vRif & "usuario.txt", 1
    SubirQueryHTTP
End If
End Sub


Sub SubirProductos()
Dim vEncSql As String
Dim i As Long
Dim x As Long
vRif = Mid(vRutaFTP, 1, 20)
V_Vista = "select codinst, descrip from sainsta where codinst in(select codinst from saprod)"
ActualizarRs3
    vQueryFTP = "truncate table " & vRif & "cat; Insert into " & vRif & "cat (id, descrip) values "
    While rs3.EOF = False
        vQueryFTP = vQueryFTP & "('" & rs3.Fields("codinst").Value & "', '" & rs3.Fields("descrip").Value & "'),"
        rs3.MoveNext
    Wend

        vQueryFTP = Mid(vQueryFTP, 1, Len(vQueryFTP) - 1)
RichTextBox1.text = vQueryFTP
RichTextBox1.SaveFile App.Path & "\" & vRif & "cat.txt", 1
vMySqlQuery = vQueryFTP
RichTextBox1.text = vQueryFTP
RichTextBox1.SaveFile App.Path & "\" & vRif & "p.txt", 1
SubirQueryHTTP
'MsgBox (vQueryFTP)
'V_Vista = "SELECT id, codprod as codigo, descrip, precioi3 as precio, precioi2, precioi1, refere as  barra, codinst, c01+c02+c03+c04+c05+c06+c07+c08+c09+c10 as meta, existen, isnull((select descrip from sainsta where sainsta.codinst=saprod.codinst),'') as departamento,descrip2 as categoria FROM saprod where precioi3>0 and existen>0 order by codinst, descrip2 "
V_Vista = "SELECT SAPROD.id, SAPROD.CodProd as codigo, SAPROD.Descrip, SAPROD.Existen, SAPROD.DOLAR, SAPROD.PRECIOI3 as precio, SAPROD.PRECIOI2, SAPROD.PRECIOI1, SAPROD.Refere AS BARRA, SAPROD.CodInst, SAINSTA.Descrip AS DEPARTAMENTO, SAPROD.CATEGORIA, '' AS META, ISNULL(_MOVDIARIOV.VTADIA, 0) AS VTADIA, _MOVDIARIOV.PERIODOD, _MOVDIARIOV.FALLA, saprod.esexento " & _
" FROM SAPROD LEFT OUTER JOIN SAINSTA ON SAPROD.CodInst = SAINSTA.CodInst LEFT OUTER JOIN _MOVDIARIOV ON SAPROD.CodProd = _MOVDIARIOV.CODPROD where saprod.existen>0 and precio3>0"

'MEN123 = InputBox("", "", V_Vista)
vMySqlQuery = "truncate table " & vRif & "p; "
SubirQueryHTTP
ActualizarRs3
    vEncSql = "Insert into " & vRif & "p (codigo, descrip, precio, barra, codinst, meta, existencia, costo, precio2, precio1, departamento, categoria, esexento) values "
    i = 1
    x = 1
    While rs3.EOF = False
        If i = 1 Then
            vQueryFTP = vEncSql
        End If
        vQueryFTP = vQueryFTP & "('" & rs3.Fields("codigo").Value & "', '" & rs3.Fields("descrip").Value & "'," & Replace(rs3.Fields("precio").Value, ",", ".") & ",'" & rs3.Fields("barra").Value & "'," & rs3.Fields("codinst").Value & ",'" & rs3.Fields("meta").Value & "',0" & Replace(rs3.Fields("existen").Value, ",", ".") & ",0" & Replace(rs3.Fields("dolar").Value, ",", ".") & ",0" & Replace(rs3.Fields("precioi2").Value, ",", ".") & ",0" & Replace(rs3.Fields("precioi1").Value, ",", ".") & ",'" & rs3.Fields("Departamento").Value & "','" & rs3.Fields("Categoria").Value & "'," & rs3.Fields("esexento").Value & ")," & vbCrLf
        rs3.MoveNext
        If i = 100 Then
            vMySqlQuery = Mid(vQueryFTP, 1, Len(vQueryFTP) - 3) & ";"

            SubirQueryHTTP
            RichTextBox1.text = vMySqlQuery
            RichTextBox1.SaveFile App.Path & "\QUERY" & x & ".TXT", 1
            x = x + 1
            i = 0
            vQueryFTP = ""
        End If
        i = i + 1
    Wend
x = x + 1
        vQueryFTP = Mid(vQueryFTP, 1, Len(vQueryFTP) - 3)
vMySqlQuery = vQueryFTP & ";"
RichTextBox1.text = vQueryFTP
RichTextBox1.SaveFile App.Path & "\" & vRif & "p.txt", 1
SubirQueryHTTP
End Sub
Sub SubirQueryHTTP()
Dim http As Object
Set http = CreateObject("WinHttp.WinHttpRequest.5.1")
Dim Url As String
Dim params As String

http.Option(6) = False

Url = "http://" & vServidorFTP & "/servicio/subirsql.php"
params = "linea=" & vMySqlQuery

http.Open "POST", Url, False
http.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
http.Send params
'MsgBox (vMySqlQuery)
End Sub
Sub SubirCompras()
Dim vEncSql As String
Dim i As Long
Dim x As Long
vRif = Mid(vRutaFTP, 1, 20)

vMySqlQuery = "truncate table " & vRif & "vcom;"
SubirQueryHTTP

    V_Vista = "SELECT CONVERT(varchar, FechaE, 112) AS periodo, Signo, NumeroD, codesta, codusua, CodProv, Descrip, TEXENTO/FACTOR * Signo AS EXENTO, TGRAVABLE/FACTOR * Signo AS BASE, mtotax/FACTOR * Signo AS iva, RETENIVA/FACTOR * Signo AS RETENCION, (TEXENTO+TGRAVABLE+MTOTAX)/FACTOR * Signo AS TOTAL  " & _
    " FROM dbo.SACOMP WHERE (TipoCOM IN ('H', 'I')) and  fechae >= DATEADD(MONTH, -3, GETDATE()) "

'MEN123 = InputBox("", "", V_Vista)
ActualizarRs3
    
    vEncSql = " Insert into " & vRif & "vcom (   periodo, signo, numerod, codesta, codusua, codprov, descrip, exento, base, iva, retencion, total) values "
    i = 1
    x = 1
    While rs3.EOF = False
        If i = 1 Then
            vQueryFTP = vEncSql
        End If
        vQueryFTP = vQueryFTP & "('" & rs3.Fields("periodo").Value & "', " & rs3.Fields("signo").Value & ",'" & rs3.Fields("numerod").Value & "','" & rs3.Fields("codesta").Value & "','" & rs3.Fields("codusua").Value & "','" & rs3.Fields("codprov").Value & "','" & rs3.Fields("descrip").Value & "',0" & Replace(rs3.Fields("exento").Value, ",", ".") & ",0" & Replace(rs3.Fields("base").Value, ",", ".") & ",0" & Replace(rs3.Fields("iva").Value, ",", ".") & ",0" & Replace(rs3.Fields("retencion").Value, ",", ".") & ",0" & Replace(rs3.Fields("total").Value, ",", ".") & ")," & vbCrLf
        rs3.MoveNext
        If i = 100 Then
            vMySqlQuery = Mid(vQueryFTP, 1, Len(vQueryFTP) - 3) & ";"

            SubirQueryHTTP
            RichTextBox1.text = vMySqlQuery
            RichTextBox1.SaveFile App.Path & "\QUERY" & x & ".TXT", 1
            x = x + 1
            i = 0
        End If
        i = i + 1
    Wend
x = x + 1

If vQueryFTP <> "" Then
    vQueryFTP = Mid(vQueryFTP, 1, Len(vQueryFTP) - 3)
    vMySqlQuery = vQueryFTP
    RichTextBox1.text = vQueryFTP
    RichTextBox1.SaveFile App.Path & "\" & vRif & "vcom.txt", 1
    SubirQueryHTTP
End If
End Sub
Sub SubirCxp()
Dim vEncSql As String
Dim i As Long
Dim x As Long
vRif = Mid(vRutaFTP, 1, 20)

vMySqlQuery = "truncate table " & vRif & "vcxp; "
SubirQueryHTTP

V_Vista = "SELECT nrounico, CONVERT(varchar, dbo.SAACXP.Fechav, 112) AS periodo, dbo.SAACXP.NumeroD, dbo.SAACXP.CodEsta, dbo.SAACXP.CodProv, dbo.SAACXP.CodUsua, dbo.SAPROV.Descrip, dbo.SAACXP.Monto/factor AS MontoFac, dbo.SAACXP.Saldo/factor AS monto FROM dbo.SAACXP INNER JOIN dbo.SAPROV ON dbo.SAACXP.CodPROV = dbo.SAPROV.CodPROV " & _
" WHERE     (dbo.SAACXP.TipoCxp IN ('10', '21')) AND (dbo.SAACXP.Saldo > 1)"
'MEN123 = InputBox("", "", V_Vista)
ActualizarRs3
    
    vEncSql = "Insert into " & vRif & "vcxp (id, periodo, NumeroD, CodEsta, CodUsua, CodPROV, Descrip, MontoFac, monto) values "
    i = 1
    x = 1
    While rs3.EOF = False
        If i = 1 Then
            vQueryFTP = vEncSql
        End If
        vQueryFTP = vQueryFTP & "(" & rs3.Fields("nrounico").Value & ",'" & rs3.Fields("periodo").Value & "', '" & rs3.Fields("numerod").Value & "','" & rs3.Fields("codesta").Value & "','" & rs3.Fields("codusua").Value & "','" & rs3.Fields("codprov").Value & "','" & rs3.Fields("descrip").Value & "',0" & Replace(rs3.Fields("montofac").Value, ",", ".") & ",0" & Replace(rs3.Fields("monto").Value, ",", ".") & ")," & vbCrLf
        rs3.MoveNext
        If i = 100 Then
            vMySqlQuery = Mid(vQueryFTP, 1, Len(vQueryFTP) - 3) & ";"

            SubirQueryHTTP
            RichTextBox1.text = vMySqlQuery
            RichTextBox1.SaveFile App.Path & "\QUERY" & x & ".TXT", 1
            x = x + 1
            i = 0
        End If
        i = i + 1
    Wend
x = x + 1

If vQueryFTP <> "" Then
    vQueryFTP = Mid(vQueryFTP, 1, Len(vQueryFTP) - 3)
    vMySqlQuery = vQueryFTP
    RichTextBox1.text = vQueryFTP
    RichTextBox1.SaveFile App.Path & "\" & vRif & "vcxp.txt", 1
    SubirQueryHTTP
End If
End Sub



Sub SubirCxc()
Dim vEncSql As String
Dim i As Long
Dim x As Long
vRif = Mid(vRutaFTP, 1, 20)

vMySqlQuery = "truncate table " & vRif & "vcxc; "
SubirQueryHTTP

V_Vista = "SELECT CONVERT(varchar, dbo.SAACXC.Fechav, 112) AS periodo, dbo.SAACXC.NumeroD, dbo.SAACXC.CodEsta, dbo.SAACXC.CodVend, dbo.SAACXC.CodClie, dbo.SAACXC.CodUsua, dbo.SACLIE.Descrip, dbo.SAACXC.Monto/factor AS MontoFac, dbo.SAACXC.Saldo/factor AS monto FROM dbo.SAACXC INNER JOIN dbo.SACLIE ON dbo.SAACXC.CodClie = dbo.SACLIE.CodClie " & _
" WHERE     (dbo.SAACXC.TipoCxc IN ('10', '21')) AND (dbo.SAACXC.Saldo > 1)"
'MEN123 = InputBox("", "", V_Vista)
ActualizarRs3
    
    vEncSql = "Insert into " & vRif & "vcxc ( periodo, NumeroD, CodEsta, CodVend, CodUsua, CodClie, Descrip, MontoFac, monto) values "
    i = 1
    x = 1
    While rs3.EOF = False
        If i = 1 Then
            vQueryFTP = vEncSql
        End If
        vQueryFTP = vQueryFTP & "('" & rs3.Fields("periodo").Value & "', '" & rs3.Fields("numerod").Value & "','" & rs3.Fields("codesta").Value & "','" & rs3.Fields("codusua").Value & "','" & rs3.Fields("codvend").Value & "','" & rs3.Fields("codclie").Value & "','" & rs3.Fields("descrip").Value & "',0" & Replace(rs3.Fields("montofac").Value, ",", ".") & ",0" & Replace(rs3.Fields("monto").Value, ",", ".") & ")," & vbCrLf
        rs3.MoveNext
        If i = 100 Then
            vMySqlQuery = Mid(vQueryFTP, 1, Len(vQueryFTP) - 3) & ";"

            SubirQueryHTTP
            RichTextBox1.text = vMySqlQuery
            RichTextBox1.SaveFile App.Path & "\QUERY" & x & ".TXT", 1
            x = x + 1
            i = 0
        End If
        i = i + 1
    Wend
x = x + 1

If vQueryFTP <> "" Then
    vQueryFTP = Mid(vQueryFTP, 1, Len(vQueryFTP) - 3)
    vMySqlQuery = vQueryFTP
    RichTextBox1.text = vQueryFTP
    RichTextBox1.SaveFile App.Path & "\" & vRif & "vcxc.txt", 1
    SubirQueryHTTP
End If
End Sub
Sub SubirVentas()
Dim vEncSql As String
Dim i As Long
Dim x As Long
vRif = Mid(vRutaFTP, 1, 20)

vMySqlQuery = "truncate table " & vRif & "vf;"
SubirQueryHTTP
If vVentasDetalladas = "SI" Then
    V_Vista = "SELECT CONVERT(varchar, FechaE, 112) AS periodo, Signo, NumeroD, codvend, codesta, codusua, CodClie, Descrip, Contado/(CASE WHEN ORDENC='SOLO DOLAR' THEN 1 ELSE (CASE WHEN FACTOR=0 THEN 1 ELSE FACTOR END) END) * Signo AS contado, Credito/(CASE WHEN ORDENC='SOLO DOLAR' THEN 1 ELSE (CASE WHEN FACTOR=0 THEN 1 ELSE FACTOR END) END) * Signo AS Credito, (CONTADO+Credito)/(CASE WHEN ORDENC='SOLO DOLAR' THEN 1 ELSE (CASE WHEN FACTOR=0 THEN 1 ELSE FACTOR END) END) * Signo AS TOTAL " & _
    " FROM dbo.SAFACT WHERE (TipoFac IN ('A', 'B')) and  fechae >= DATEADD(MONTH, -3, GETDATE()) "
Else
    V_Vista = "SELECT CONVERT(varchar, FechaE, 112) AS periodo, Signo, MAX(NumeroD) AS NumeroD, CodVend, CodEsta, CodUsua, '' AS CodClie, '' AS Descrip, SUM(Contado / (CASE WHEN ORDENC = 'SOLO DOLAR' THEN 1 ELSE (CASE WHEN FACTOR=0 THEN 1 ELSE FACTOR END) END) * Signo) AS contado, SUM(Credito / (CASE WHEN ORDENC = 'SOLO DOLAR' THEN 1 ELSE (CASE WHEN FACTOR=0 THEN 1 ELSE FACTOR END) END) * Signo) AS Credito, SUM((Contado + Credito) / (CASE WHEN ORDENC = 'SOLO DOLAR' THEN 1 ELSE (CASE WHEN FACTOR=0 THEN 1 ELSE FACTOR END) END) * Signo) AS TOTAL  FROM SAFACT WHERE (TipoFac IN ('A', 'B')) AND (FechaE >= DATEADD(MONTH, - 3, GETDATE())) GROUP BY CONVERT(varchar, FechaE, 112), Signo, CodVend, CodEsta, CodUsua "

End If
'MEN123 = InputBox("", "", V_Vista)
ActualizarRs3
    
    vEncSql = " Insert into " & vRif & "vf (   periodo, signo, numerod, codesta, codusua, codvend, codclie, descrip, contado, credito, total) values "
    i = 1
    x = 1
    While rs3.EOF = False
        If i = 1 Then
            vQueryFTP = vEncSql
        End If
        vQueryFTP = vQueryFTP & "('" & rs3.Fields("periodo").Value & "', " & rs3.Fields("signo").Value & ",'" & rs3.Fields("numerod").Value & "','" & rs3.Fields("codesta").Value & "','" & rs3.Fields("codusua").Value & "','" & rs3.Fields("codvend").Value & "','" & rs3.Fields("codclie").Value & "','" & rs3.Fields("descrip").Value & "',0" & Replace(rs3.Fields("contado").Value, ",", ".") & ",0" & Replace(rs3.Fields("credito").Value, ",", ".") & ",0" & Replace(rs3.Fields("total").Value, ",", ".") & ")," & vbCrLf
        rs3.MoveNext
        If i = 100 Then
            vMySqlQuery = Mid(vQueryFTP, 1, Len(vQueryFTP) - 3) & ";"

            SubirQueryHTTP
            RichTextBox1.text = vMySqlQuery
            RichTextBox1.SaveFile App.Path & "\QUERY" & x & ".TXT", 1
            x = x + 1
            i = 0
        End If
        i = i + 1
    Wend
x = x + 1

If vQueryFTP <> "" Then
    vQueryFTP = Mid(vQueryFTP, 1, Len(vQueryFTP) - 3)
    vMySqlQuery = vQueryFTP
    RichTextBox1.text = vQueryFTP
    RichTextBox1.SaveFile App.Path & "\" & vRif & "vf.txt", 1
    SubirQueryHTTP
End If
End Sub
Sub SubirCierre()
Dim vEncSql As String
Dim i As Long
Dim x As Long
vRif = Mid(vRutaFTP, 1, 20)

vMySqlQuery = "truncate table " & vRif & "vc;"
SubirQueryHTTP

V_Vista = "SELECT (SELECT TOP (1) RIF + ' ' + Descrip AS Expr1 FROM SACONF) AS EMPRESA, (SELECT     TOP (1) ISNULL(Email, '') AS Expr1 FROM SACONF AS SACONF_1) AS EMAIL, SAFACT.CodEsta, SAFACT.CodUsua, CONVERT(varchar, SAFACT.FechaE, 112) AS PERIODO, SAIPAVTA.CodPago, max(safact.factor) as factor, SUM(SAIPAVTA.Monto*SAFACT.SIGNO/(CASE WHEN SAFACT.FACTOR=0 THEN 1 ELSE SAFACT.FACTOR END) ) AS MtoRef, SUM(SAIPAVTA.Monto*SAFACT.SIGNO* (CASE WHEN SAIPAVTA.IMPUESTO+SAIPAVTA.RETENCT=0 THEN 1 ELSE 0 END)) AS MtoBs, SUM(SAIPAVTA.Impuesto*SAFACT.SIGNO) AS MtoDivisa, sum(saipavta.monto/(CASE WHEN SAFACT.FACTOR=0 THEN 1 ELSE SAFACT.FACTOR END)*saipavta.retenct*SAFACT.SIGNO) as MtoOtro " & _
" FROM  SAFACT INNER JOIN SAIPAVTA ON SAFACT.TipoFac = SAIPAVTA.TipoFac AND SAFACT.NumeroD = SAIPAVTA.NumeroD WHERE (SAFACT.TipoFac IN ('A', 'B')) AND safact.fechae >= DATEADD(MONTH, -1, GETDATE()) GROUP BY SAFACT.CodEsta, SAFACT.CodUsua, CONVERT(varchar, SAFACT.FechaE, 112), SAIPAVTA.CodPago ORDER BY periodo, SAFACT.CodUsua, SAIPAVTA.CodPago "

'MEN123 = InputBox("", "", V_Vista)
ActualizarRs3
    
    vEncSql = " Insert into " & vRif & "vc ( empresa, email, codesta, codusua, periodo, codpago, mtoref, mtobs, mtodivisa, mtootro) values "
    i = 1
    x = 1
    While rs3.EOF = False
        If i = 1 Then
            vQueryFTP = vEncSql
        End If
        vQueryFTP = vQueryFTP & "('" & rs3.Fields("empresa").Value & "', '" & rs3.Fields("email").Value & "','" & rs3.Fields("codesta").Value & "','" & rs3.Fields("codusua").Value & "','" & rs3.Fields("periodo").Value & "','" & rs3.Fields("codpago").Value & "',0" & Replace(rs3.Fields("mtoref").Value, ",", ".") & ",0" & Replace(rs3.Fields("mtobs").Value, ",", ".") & ",0" & Replace(rs3.Fields("mtodivisa").Value, ",", ".") & ",0" & Replace(rs3.Fields("mtootro").Value, ",", ".") & ")," & vbCrLf
        rs3.MoveNext
        If i = 100 Then
            vMySqlQuery = Mid(vQueryFTP, 1, Len(vQueryFTP) - 3) & ";"

            SubirQueryHTTP
            RichTextBox1.text = vMySqlQuery
            RichTextBox1.SaveFile App.Path & "\QUERY" & x & ".TXT", 1
            x = x + 1
            i = 0
        End If
        i = i + 1
    Wend
x = x + 1

If vQueryFTP <> "" Then
    vQueryFTP = Mid(vQueryFTP, 1, Len(vQueryFTP) - 3)
    vMySqlQuery = vQueryFTP
    RichTextBox1.text = vQueryFTP
    RichTextBox1.SaveFile App.Path & "\" & vRif & "vc.txt", 1
    SubirQueryHTTP
End If
End Sub


Sub Ftp_Vendedor()
Dim vSaldoFTP As String
Dim vQueryFTP As String


Dim et  As ADODB.Stream
Dim objStream   As ADODB.Stream
 
 Set objStreamUTF8 = CreateObject("ADODB.Stream")
 Set objStreamUTF8NoBOM = CreateObject("ADODB.Stream")

Const adSaveCreateNotExist = 1
Const adSaveCreateOverWrite = 2
Const adTypeBinary = 1
Const adTypeText = 2
''''''''''''''''''''''''
Dim http As Object
Set http = CreateObject("WinHttp.WinHttpRequest.5.1")
Dim Url As String
Dim params As String

Command8.Enabled = False

vJsonRuta = vRutaFTP
vJsonArchivo = "sql.txt"
'Dim objStreamUTF8: Set objStreamUTF8 = CreateObject("ADODB.Stream")
'Dim objStreamUTF8NoBOM: Set objStreamUTF8NoBOM = CreateObject("ADODB.Stream")

'Command17.Enabled = False
'Label1.Caption = "Actualizando Factor"
'V_Vista = "select codprod, descrip, precioi3, refere, codinst, descrip+codprod+isnull(refere,'')+isnull(c01,'')+isnull(c02,'')+isnull(c03,'')+isnull(c04,'')+isnull(c05,'')+isnull(c06,'')+isnull(c07,'')+isnull(c08,'')+isnull(c09,'')+isnull(c10,'') as meta from saprod where existen>0 and precioi3>0"
V_Vista = "SELECT     CodVend, descrip, clase  FROM SAVEND where len(clase)>=3 "
'MEN123 = InputBox("", "", V_Vista)
ActualizarRs3
    vQueryFTP = "truncate table a_" & vRutaFTP & "ven; Insert into a_" & vRutaFTP & "ven (codvend, nombre, clave, condiciones) values "
    While rs3.EOF = False
        vQueryFTP = vQueryFTP & "('" & rs3.Fields("codvend").Value & "', '" & rs3.Fields("descrip").Value & "','" & rs3.Fields("clase").Value & "', ''),"
        rs3.MoveNext
    Wend
        vQueryFTP = Mid(vQueryFTP, 1, Len(vQueryFTP) - 1)
'        RichTextBox1.Text = vQueryFTP & ";"
'        'RichTextBox1.SaveFile App.Path & "\ACTUALIZAR.SQL", 1
'        RichTextBox1.SaveFile App.Path & "\J309849697.txt", 1
            On Error Resume Next
            Kill App.Path & "\sql.txt"
            
            On Error Resume Next
            Kill App.Path & "\toto.php"

With objStreamUTF8
  .charset = "UTF-8"
  .Open
  .WriteText vQueryFTP
  .Position = 0
  .SaveToFile "toto.php", adSaveCreateOverWrite
  .Type = adTypeText
  .Position = 3
End With

With objStreamUTF8NoBOM
  .Type = adTypeBinary
  .Open
  objStreamUTF8.CopyTo objStreamUTF8NoBOM
  .SaveToFile App.Path & "\sql.txt", adSaveCreateOverWrite
End With

objStreamUTF8.Close
objStreamUTF8NoBOM.Close
'vServFTP
'vUseFTP
'vClaveFtp
            
Url = "http://" & vServidorFTP & "/" & vJsonRuta & "/subir4.php"
params = "linea=" & vQueryFTP & "&archivo=" & vJsonArchivo

http.Open "POST", Url, False
http.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
http.Send params
'        FormFTP.txtServidor.text = vServidorFTP
'        FormFTP.txtUsuario.text = vUsuarioFTP
'        FormFTP.txtPassword.text = vClaveFTP
        

'FormFTP.Show 1
Me.Inet1.OpenURL "http://" & vServidorFTP & "/" & vRutaFTP & "/subir.php"

Label9.Caption = "Actualizado"
Command8.Enabled = True

Command8.Caption = "Subir Nube Json"
End Sub
Sub Ftp_Catalogo()
Dim vSaldoFTP As String
Dim vQueryFTP As String
Dim et  As ADODB.Stream
Dim objStream   As ADODB.Stream
Dim vCatWeb As String
Dim vPrecDet As Double
Dim vPrecBul As Double
Dim vDetMarca As String
''''''''''''''''''''''''
Dim http As Object
Set http = CreateObject("WinHttp.WinHttpRequest.5.1")
Dim Url As String
Dim params As String

vJson = True
'vJsonRuta = "/data"
vJsonRuta = "data"
vJsonArchivo = "data.js"
 
 Set objStreamUTF8 = CreateObject("ADODB.Stream")
 Set objStreamUTF8NoBOM = CreateObject("ADODB.Stream")

Const adSaveCreateNotExist = 1
Const adSaveCreateOverWrite = 2
Const adTypeBinary = 1
Const adTypeText = 2
Command8.Caption = "Subiendo.."
Command8.Enabled = False
'Dim objStreamUTF8: Set objStreamUTF8 = CreateObject("ADODB.Stream")
'Dim objStreamUTF8NoBOM: Set objStreamUTF8NoBOM = CreateObject("ADODB.Stream")

'Command17.Enabled = False
'Label1.Caption = "Actualizando Factor"
'V_Vista = "select codprod, descrip, precioi3, refere, codinst, descrip+codprod+isnull(refere,'')+isnull(c01,'')+isnull(c02,'')+isnull(c03,'')+isnull(c04,'')+isnull(c05,'')+isnull(c06,'')+isnull(c07,'')+isnull(c08,'')+isnull(c09,'')+isnull(c10,'') as meta from saprod where existen>0 and precioi3>0"
V_Vista = "SELECT (Case SAPROD.C10 when 'SI' then '00 - ARTICULOS NUEVOS' ELSE SAINSTA.Descrip End)  AS category, SAPROD.CodProd, SAPROD.C05, SAPROD.C07, SAPROD.C08,SAPROD.Descrip, ISNULL(SAPROD.Unidad, '') AS descrip2, SAPROD.Existen, SAPROD.Preciou3 AS precio_detal,  SAPROD.Preciou2 AS precio_mayor, '' AS marca, 'https://" & vServidorFTP & "/img/productos/' + SAPROD.CodProd + '.jpg' AS imagen, SAPROD.C09 FROM SAPROD INNER JOIN SAINSTA ON SAPROD.CodInst = SAINSTA.CodInst " & _
" WHERE   (SAPROD.precioi3>0 AND  (SAPROD.C09 <> 'NO') AND (SAPROD.Activo = 1)) or SAPROD.C09='SI' ORDER BY SAPROD.C10 DESC, SAINSTA.DESCRIP, SAPROD.Descrip"
'MEN123 = InputBox("", "", V_Vista)
ActualizarRs3
    vQueryFTP = "window.globalData = [" & vbCrLf

    
    
    While rs3.EOF = False
        vDetMarca = ""
        If vCatWeb <> rs3.Fields("category").Value Then
            vCatWeb = rs3.Fields("category").Value
            vQueryFTP = vQueryFTP & "    {" & vbCrLf
            vQueryFTP = vQueryFTP & "        category: '" & Replace(rs3.Fields("category").Value, Chr(34), "") & "'," & vbCrLf
            vQueryFTP = vQueryFTP & "        products: [" & vbCrLf
        End If
        vQueryFTP = vQueryFTP & "    {" & vbCrLf
        vQueryFTP = vQueryFTP & "        descrip1: '" & Replace(rs3.Fields("descrip").Value, Chr(34), "") & "'," & vbCrLf
        'vQueryFTP = vQueryFTP & "        'descrip2': '" & Replace(rs3.Fields("descrip2").Value, Chr(34), "") & "'," & vbCrLf

            If rs3.Fields("EXISTEN").Value >= NumeroValor(rs3.Fields("C05").Value) Then
                vQueryFTP = vQueryFTP & "        existencia: " & Replace(rs3.Fields("EXISTEN").Value, ",", ".") & "," & vbCrLf
            Else
                If rs3.Fields("C09").Value = "SI" Then
                    vQueryFTP = vQueryFTP & "        existencia: 1," & vbCrLf
                Else
                    vQueryFTP = vQueryFTP & "        existencia: 0," & vbCrLf
                End If
            End If

        vQueryFTP = vQueryFTP & "        precio_detal: " & Replace(Round(rs3.Fields("precio_detal").Value, 2), ",", ".") & "," & vbCrLf
        vQueryFTP = vQueryFTP & "        precio_mayor: " & Replace(Round(rs3.Fields("precio_mayor").Value, 2), ",", ".") & "," & vbCrLf
        If IsNumeric(rs3.Fields("C07").Value) = True Then
            vPrecDet = NumeroValor(rs3.Fields("c07"))
            If vPrecDet > 0 Then
                vPrecDet = rs3.Fields("precio_detal").Value / vPrecDet
                vDetMarca = "Precio1 Det: " & Format(Round(vPrecDet, 2), "#,##0.00") & vMostrarMonedaEspecifica & "<br>"
                If rs3.Fields("precio_mayor").Value <> rs3.Fields("precio_detal").Value Then
                    vPrecDet = NumeroValor(rs3.Fields("c07"))
                    vPrecDet = rs3.Fields("precio_mayor").Value / vPrecDet
                    vDetMarca = vDetMarca & "Precio2 Det: " & Format(Round(vPrecDet, 2), "#,##0.00") & vMostrarMonedaEspecifica & "<br>"
                End If
            End If
            
        End If
        If IsNumeric(rs3.Fields("C08").Value) = True Then
            vPrecDet = NumeroValor(rs3.Fields("c08"))
            If vPrecDet > 0 Then
                vPrecDet = rs3.Fields("precio_mayor").Value * vPrecDet
                vDetMarca = vDetMarca & "Precio Bult: " & Format(Round(vPrecDet, 2), "#,##0.00") & vMostrarMonedaEspecifica & ""
            End If
            
        End If
        vQueryFTP = vQueryFTP & "        descrip2: '" & vDetMarca & "'," & vbCrLf
        vQueryFTP = vQueryFTP & "        marca: ''," & vbCrLf
        vQueryFTP = vQueryFTP & "        codigo: '" & rs3.Fields("codprod").Value & "'," & vbCrLf
        vQueryFTP = vQueryFTP & "        imagen: '" & rs3.Fields("imagen").Value & "'" & vbCrLf
        vQueryFTP = vQueryFTP & "    }," & vbCrLf


        rs3.MoveNext
        If rs3.EOF = True Then
                'vQueryFTP = Mid(vQueryFTP, 1, Len(vQueryFTP) - 3)
                vQueryFTP = vQueryFTP & "        ]," & vbCrLf
                vQueryFTP = vQueryFTP & "    }," & vbCrLf
        Else
            If vCatWeb <> rs3.Fields("category").Value Then
                'vQueryFTP = Mid(vQueryFTP, 1, Len(vQueryFTP) - 3)
                vQueryFTP = vQueryFTP & "        ]," & vbCrLf
                vQueryFTP = vQueryFTP & "    }," & vbCrLf
            End If
        End If
    Wend
        'vQueryFTP = Mid(vQueryFTP, 1, Len(vQueryFTP) - 3)
        vQueryFTP = vbCrLf & vQueryFTP & vbCrLf & "]" & vbCrLf
        'vQueryFTP = Replace(vQueryFTP, "'", Chr(34))
        'vQueryFTP = vQueryFTP & vbCrLf & "}"
'        RichTextBox1.Text = vQueryFTP & ";"
'        'RichTextBox1.SaveFile App.Path & "\ACTUALIZAR.SQL", 1
'        RichTextBox1.SaveFile App.Path & "\J309849697.txt", 1
            On Error Resume Next
            Kill App.Path & "\" & vJsonArchivo
            
            On Error Resume Next
            Kill App.Path & "\toto.php"

With objStreamUTF8
  .charset = "UTF-8"
  .Open
  .WriteText vQueryFTP
  .Position = 0
  .SaveToFile "toto.php", adSaveCreateOverWrite
  .Type = adTypeText
  .Position = 3
End With

With objStreamUTF8NoBOM
  .Type = adTypeBinary
  .Open
  objStreamUTF8.CopyTo objStreamUTF8NoBOM
  .SaveToFile App.Path & "\" & vJsonArchivo, adSaveCreateOverWrite
End With

objStreamUTF8.Close
objStreamUTF8NoBOM.Close

Url = "http://" & vServidorFTP & "/" & vJsonRuta & "/subir4.php"
params = "linea=" & Replace(vQueryFTP, "&", "") & "&archivo=" & vJsonArchivo

http.Open "POST", Url, False
http.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
http.Send params

'MsgBox http.ResponseText ' Muestra la respuesta

Command8.Enabled = True
Command8.Caption = "Subir Nube Json"

End Sub

Sub Ftp_Productos()
Dim vSaldoFTP As String
Dim vQueryFTP As String
Dim et  As ADODB.Stream
Dim objStream   As ADODB.Stream
Dim vCatWeb As String
Dim vPrecDet As Double
Dim vPrecBul As Double
Dim vDetMarca As String
''''''''''''''''''''''''
Dim http As Object
Set http = CreateObject("WinHttp.WinHttpRequest.5.1")
Dim Url As String
Dim params As String

vJson = True
'vJsonRuta = "/data"
vJsonRuta = "publico/data"
vJsonArchivo = "data.js"
 
 Set objStreamUTF8 = CreateObject("ADODB.Stream")
 Set objStreamUTF8NoBOM = CreateObject("ADODB.Stream")

Const adSaveCreateNotExist = 1
Const adSaveCreateOverWrite = 2
Const adTypeBinary = 1
Const adTypeText = 2
Command8.Caption = "Subiendo.."
Command8.Enabled = False
'Dim objStreamUTF8: Set objStreamUTF8 = CreateObject("ADODB.Stream")
'Dim objStreamUTF8NoBOM: Set objStreamUTF8NoBOM = CreateObject("ADODB.Stream")

'Command17.Enabled = False
'Label1.Caption = "Actualizando Factor"
'V_Vista = "select codprod, descrip, precioi3, refere, codinst, descrip+codprod+isnull(refere,'')+isnull(c01,'')+isnull(c02,'')+isnull(c03,'')+isnull(c04,'')+isnull(c05,'')+isnull(c06,'')+isnull(c07,'')+isnull(c08,'')+isnull(c09,'')+isnull(c10,'') as meta from saprod where existen>0 and precioi3>0"
V_Vista = "SELECT (Case SAPROD.C10 when 'SI' then '00 - ARTICULOS NUEVOS' ELSE SAINSTA.Descrip End)  AS category, SAPROD.CodProd, SAPROD.C05, SAPROD.C07, SAPROD.C08,SAPROD.Descrip, ISNULL(SAPROD.Unidad, '') AS descrip2, SAPROD.Existen, SAPROD.Precioi3*(select factor from saconf) AS precio_detal,  SAPROD.Precioi3*(select factor from saconf) AS precio_mayor, '' AS marca, 'https://" & vServidorFTP & "/img/productos/' + SAPROD.CodProd + '.jpg' AS imagen, SAPROD.C09 FROM SAPROD INNER JOIN SAINSTA ON SAPROD.CodInst = SAINSTA.CodInst " & _
" WHERE   (SAPROD.precioi3>0 AND  (SAPROD.C09 <> 'NO') AND (SAPROD.Activo = 1)) or SAPROD.C09='SI' ORDER BY SAPROD.C10 DESC, SAINSTA.DESCRIP, SAPROD.Descrip"
'MEN123 = InputBox("", "", V_Vista)
ActualizarRs3
    vQueryFTP = "window.globalData = [" & vbCrLf

    
    
    While rs3.EOF = False
        vDetMarca = ""
        If vCatWeb <> rs3.Fields("category").Value Then
            vCatWeb = rs3.Fields("category").Value
            vQueryFTP = vQueryFTP & "    {" & vbCrLf
            vQueryFTP = vQueryFTP & "        category: '" & Replace(rs3.Fields("category").Value, Chr(34), "") & "'," & vbCrLf
            vQueryFTP = vQueryFTP & "        products: [" & vbCrLf
        End If
        vQueryFTP = vQueryFTP & "    {" & vbCrLf
        vQueryFTP = vQueryFTP & "        descrip1: '" & Replace(rs3.Fields("descrip").Value, Chr(34), "") & "'," & vbCrLf
        'vQueryFTP = vQueryFTP & "        'descrip2': '" & Replace(rs3.Fields("descrip2").Value, Chr(34), "") & "'," & vbCrLf

            If rs3.Fields("EXISTEN").Value >= NumeroValor(rs3.Fields("C05").Value) Then
                vQueryFTP = vQueryFTP & "        existencia: " & Replace(rs3.Fields("EXISTEN").Value, ",", ".") & "," & vbCrLf
            Else
                If rs3.Fields("C09").Value = "SI" Then
                    vQueryFTP = vQueryFTP & "        existencia: 1," & vbCrLf
                Else
                    vQueryFTP = vQueryFTP & "        existencia: 0," & vbCrLf
                End If
            End If

        vQueryFTP = vQueryFTP & "        precio_detal: " & Replace(Round(rs3.Fields("precio_detal").Value, 2), ",", ".") & "," & vbCrLf
        vQueryFTP = vQueryFTP & "        precio_mayor: " & Replace(Round(rs3.Fields("precio_mayor").Value, 2), ",", ".") & "," & vbCrLf
        If IsNumeric(rs3.Fields("C07").Value) = True Then
            vPrecDet = NumeroValor(rs3.Fields("c07"))
            If vPrecDet > 0 Then
                vPrecDet = rs3.Fields("precio_detal").Value / vPrecDet
                vDetMarca = "Precio1 Det: " & Format(Round(vPrecDet, 2), "#,##0.00") & "Bs" & "<br>"
                If rs3.Fields("precio_mayor").Value <> rs3.Fields("precio_detal").Value Then
                    vPrecDet = NumeroValor(rs3.Fields("c07"))
                    vPrecDet = rs3.Fields("precio_mayor").Value / vPrecDet
                    vDetMarca = vDetMarca & "Precio2 Det: " & Format(Round(vPrecDet, 2), "#,##0.00") & "Bs" & "<br>"
                End If
            End If
            
        End If
        If IsNumeric(rs3.Fields("C08").Value) = True Then
            vPrecDet = NumeroValor(rs3.Fields("c08"))
            If vPrecDet > 0 Then
                vPrecDet = rs3.Fields("precio_mayor").Value * vPrecDet
                vDetMarca = vDetMarca & "Precio Bult: " & Round(Format(vPrecDet, "#,##0.00"), 2) & "Bs" & ""
            End If
            
        End If
        vQueryFTP = vQueryFTP & "        descrip2: '" & vDetMarca & "'," & vbCrLf
        vQueryFTP = vQueryFTP & "        marca: ''," & vbCrLf
        vQueryFTP = vQueryFTP & "        codigo: '" & rs3.Fields("codprod").Value & "'," & vbCrLf
        vQueryFTP = vQueryFTP & "        imagen: '" & rs3.Fields("imagen").Value & "'" & vbCrLf
        vQueryFTP = vQueryFTP & "    }," & vbCrLf


        rs3.MoveNext
        If rs3.EOF = True Then
                'vQueryFTP = Mid(vQueryFTP, 1, Len(vQueryFTP) - 3)
                vQueryFTP = vQueryFTP & "        ]," & vbCrLf
                vQueryFTP = vQueryFTP & "    }," & vbCrLf
        Else
            If vCatWeb <> rs3.Fields("category").Value Then
                'vQueryFTP = Mid(vQueryFTP, 1, Len(vQueryFTP) - 3)
                vQueryFTP = vQueryFTP & "        ]," & vbCrLf
                vQueryFTP = vQueryFTP & "    }," & vbCrLf
            End If
        End If
    Wend
        'vQueryFTP = Mid(vQueryFTP, 1, Len(vQueryFTP) - 3)
        vQueryFTP = vbCrLf & vQueryFTP & vbCrLf & "]" & vbCrLf
        'vQueryFTP = Replace(vQueryFTP, "'", Chr(34))
        'vQueryFTP = vQueryFTP & vbCrLf & "}"
'        RichTextBox1.Text = vQueryFTP & ";"
'        'RichTextBox1.SaveFile App.Path & "\ACTUALIZAR.SQL", 1
'        RichTextBox1.SaveFile App.Path & "\J309849697.txt", 1
            On Error Resume Next
            Kill App.Path & "\" & vJsonArchivo
            
            On Error Resume Next
            Kill App.Path & "\toto.php"

With objStreamUTF8
  .charset = "UTF-8"
  .Open
  .WriteText vQueryFTP
  .Position = 0
  .SaveToFile "toto.php", adSaveCreateOverWrite
  .Type = adTypeText
  .Position = 3
End With

With objStreamUTF8NoBOM
  .Type = adTypeBinary
  .Open
  objStreamUTF8.CopyTo objStreamUTF8NoBOM
  .SaveToFile App.Path & "\" & vJsonArchivo, adSaveCreateOverWrite
End With

objStreamUTF8.Close
objStreamUTF8NoBOM.Close

            
Url = "http://" & vServidorFTP & "/" & vJsonRuta & "/subir4.php"
params = "linea=" & Replace(vQueryFTP, "&", "") & "&archivo=" & vJsonArchivo

http.Open "POST", Url, False
http.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
http.Send params

'MsgBox http.ResponseText ' Muestra la respuesta


'Me.Inet1.OpenURL "http://" & vServidorFTP & "/datos/subir.php"
Command8.Enabled = True
Command8.Caption = "Subir Nube Json"
End Sub



Private Sub DataGrid1_ColResize(Index As Integer, ByVal ColIndex As Integer, Cancel As Integer)
dbindEx = Index
GuardarFormatoGrid
End Sub
Private Sub DataGrid1_KeyDown(Index As Integer, KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyReturn Then
    Command2 = True
End If

End Sub

Private Sub DataGrid1_KeyUp(Index As Integer, KeyCode As Integer, Shift As Integer)
If Index = 0 Then
    ActualizarDetalleWeb
End If
End Sub

Private Sub DataGrid1_RowColChange(Index As Integer, LastRow As Variant, ByVal LastCol As Integer)
If Index = 0 Then
    ActualizarDetalleWeb
End If
End Sub

Private Sub Form_Load()
CONEXION_D = "FILE NAME=" & App.Path & "\CONEXION.dsn"
CN.Open CONEXION_D
V_Vista = "select TOP 1 * from _PAGINAWEB"
Set rs3 = New ADODB.Recordset
rs3.CursorLocation = adUseClient
rs3.CursorType = adOpenStatic
rs3.LockType = adLockBatchOptimistic
On Error Resume Next
rs3.Open V_Vista, CN, , , 1
If Err.Description <> "" Then
    CN.Execute " CREATE TABLE dbo._PAGINAWEB ( id int NOT NULL IDENTITY (1, 1),SERVIDOR nvarchar(250) NULL, USUARIO nvarchar(250) NULL, CLAVE nvarchar(250) NULL, RUTA nvarchar(250) NULL, [PUERTO] [int] NULL, Condiciones2 nvarchar(250) NULL )  ON [PRIMARY]"
    CN.Execute "INSERT INTO _PAGINAWEB (SERVIDOR, USUARIO, CLAVE, RUTA,PUERTO) VALUES ('','','','',21)"
Else

    vServidorFTP = rs3.Fields("SERVIDOR").Value
    vUsuarioFTP = rs3.Fields("USUARIO").Value
    vClaveFtp = rs3.Fields("CLAVE").Value
    vRutaFTP = rs3.Fields("RUTA").Value
    INTERNET_DEFAULT_FTP_PORT = rs3.Fields("PUERTO").Value
    vCondicionesWeb = rs3.Fields("Condiciones").Value
    vCondicionesWeb2 = rs3.Fields("Condiciones2").Value
End If
''''''''''''''''''''MONEDA REFERENCIA
On Error Resume Next
RichTextBox1.LoadFile App.Path & "\vVentasDetalladas.INI", 1
If Err.Description <> "" Then
    RichTextBox1.text = "NO"
    RichTextBox1.SaveFile App.Path & "\vVentasDetalladas.INI", 1
End If
vVentasDetalladas = RichTextBox1.text
 
End Sub



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

Private Sub Text1_KeyUp(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyDown Then
    If FrmWebPedidos.Visible = True Then
        Me.DataGrid1(0).SetFocus
    End If
End If
End Sub

Sub SubirVentasDia()
Dim vEncSql As String
Dim i As Long
Dim x As Long
vRif = Mid(vRutaFTP, 1, 20)

vMySqlQuery = "delete from " & vRif & "vf where DATE(periodo) = '" & Format(Date, "YYYY-MM-DD") & "' ;"
SubirQueryHTTP
If vVentasDetalladas = "SI" Then
    V_Vista = "SELECT CONVERT(varchar, FechaE, 112) AS periodo, Signo, NumeroD, codvend, codesta, codusua, CodClie, Descrip, Contado/(CASE WHEN ORDENC='SOLO DOLAR' THEN 1 ELSE (CASE WHEN FACTOR=0 THEN 1 ELSE FACTOR END) END) * Signo AS contado, Credito/(CASE WHEN ORDENC='SOLO DOLAR' THEN 1 ELSE (CASE WHEN FACTOR=0 THEN 1 ELSE FACTOR END) END) * Signo AS Credito, (CONTADO+Credito)/(CASE WHEN ORDENC='SOLO DOLAR' THEN 1 ELSE (CASE WHEN FACTOR=0 THEN 1 ELSE FACTOR END) END) * Signo AS TOTAL " & _
    " FROM dbo.SAFACT WHERE (TipoFac IN ('A', 'B')) and  convert(varchar,fechae,112) = convert(varchar,GETDATE(),112) "
Else
    V_Vista = "SELECT CONVERT(varchar, FechaE, 112) AS periodo, Signo, MAX(NumeroD) AS NumeroD, CodVend, CodEsta, CodUsua, '' AS CodClie, '' AS Descrip, SUM(Contado / (CASE WHEN ORDENC = 'SOLO DOLAR' THEN 1 ELSE (CASE WHEN FACTOR=0 THEN 1 ELSE FACTOR END) END) * Signo) AS contado, SUM(Credito / (CASE WHEN ORDENC = 'SOLO DOLAR' THEN 1 ELSE (CASE WHEN FACTOR=0 THEN 1 ELSE FACTOR END) END) * Signo) AS Credito, SUM((Contado + Credito) / (CASE WHEN ORDENC = 'SOLO DOLAR' THEN 1 ELSE FACTOR END) * Signo) AS TOTAL " & _
    " FROM SAFACT WHERE (TipoFac IN ('A', 'B')) AND (convert(varchar,fechae,112) = convert(varchar,GETDATE(),112)) GROUP BY CONVERT(varchar, FechaE, 112), Signo, CodVend, CodEsta, CodUsua"

End If
'MEN123 = InputBox("", "", V_Vista)
ActualizarRs3
    
    vEncSql = " Insert into " & vRif & "vf (   periodo, signo, numerod, codesta, codusua, codvend, codclie, descrip, contado, credito, total) values "
    i = 1
    x = 1
    While rs3.EOF = False
        If i = 1 Then
            vQueryFTP = vEncSql
        End If
        vQueryFTP = vQueryFTP & "('" & rs3.Fields("periodo").Value & "', " & rs3.Fields("signo").Value & ",'" & rs3.Fields("numerod").Value & "','" & rs3.Fields("codesta").Value & "','" & rs3.Fields("codusua").Value & "','" & rs3.Fields("codvend").Value & "','" & rs3.Fields("codclie").Value & "','" & rs3.Fields("descrip").Value & "',0" & Replace(rs3.Fields("contado").Value, ",", ".") & ",0" & Replace(rs3.Fields("credito").Value, ",", ".") & ",0" & Replace(rs3.Fields("total").Value, ",", ".") & ")," & vbCrLf
        rs3.MoveNext
        If i = 100 Then
            vMySqlQuery = Mid(vQueryFTP, 1, Len(vQueryFTP) - 3) & ";"

            SubirQueryHTTP
            RichTextBox1.text = vMySqlQuery
            RichTextBox1.SaveFile App.Path & "\QUERY" & x & ".TXT", 1
            x = x + 1
            i = 0
        End If
        i = i + 1
    Wend
x = x + 1

If vQueryFTP <> "" Then
    vQueryFTP = Mid(vQueryFTP, 1, Len(vQueryFTP) - 3)
    vMySqlQuery = vQueryFTP
    RichTextBox1.text = vQueryFTP
    RichTextBox1.SaveFile App.Path & "\" & vRif & "vf.txt", 1
    SubirQueryHTTP
End If
End Sub
Sub SubirCierreDia()
Dim vEncSql As String
Dim i As Long
Dim x As Long
vRif = Mid(vRutaFTP, 1, 20)

'vMySqlQuery = "truncate table " & vRif & "vc;"
vMySqlQuery = "delete from " & vRif & "vc where DATE(periodo) = '" & Format(Date, "YYYY-MM-DD") & "' ;"
SubirQueryHTTP

V_Vista = "SELECT (SELECT TOP (1) RIF + ' ' + Descrip AS Expr1 FROM SACONF) AS EMPRESA, (SELECT     TOP (1) ISNULL(Email, '') AS Expr1 FROM SACONF AS SACONF_1) AS EMAIL, SAFACT.CodEsta, SAFACT.CodUsua, CONVERT(varchar, SAFACT.FechaE, 112) AS PERIODO, SAIPAVTA.CodPago, max(safact.factor) as factor, SUM(SAIPAVTA.Monto*SAFACT.SIGNO/(CASE WHEN SAFACT.FACTOR=0 THEN 1 ELSE SAFACT.FACTOR END)) AS MtoRef, SUM(SAIPAVTA.Monto*SAFACT.SIGNO* (CASE WHEN SAIPAVTA.IMPUESTO+SAIPAVTA.RETENCT=0 THEN 1 ELSE 0 END)) AS MtoBs, SUM(SAIPAVTA.Impuesto*SAFACT.SIGNO) AS MtoDivisa, sum(saipavta.monto/(CASE WHEN SAFACT.FACTOR=0 THEN 1 ELSE SAFACT.FACTOR END)*saipavta.retenct*SAFACT.SIGNO) as MtoOtro " & _
" FROM  SAFACT INNER JOIN SAIPAVTA ON SAFACT.TipoFac = SAIPAVTA.TipoFac AND SAFACT.NumeroD = SAIPAVTA.NumeroD WHERE (SAFACT.TipoFac IN ('A', 'B')) AND (convert(varchar,safact.fechae,112) = convert(varchar,GETDATE(),112)) GROUP BY SAFACT.CodEsta, SAFACT.CodUsua, CONVERT(varchar, SAFACT.FechaE, 112), SAIPAVTA.CodPago ORDER BY periodo, SAFACT.CodUsua, SAIPAVTA.CodPago "

'MEN123 = InputBox("", "", V_Vista)
ActualizarRs3
    
    vEncSql = " Insert into " & vRif & "vc ( empresa, email, codesta, codusua, periodo, codpago, mtoref, mtobs, mtodivisa, mtootro) values "
    i = 1
    x = 1
    While rs3.EOF = False
        If i = 1 Then
            vQueryFTP = vEncSql
        End If
        vQueryFTP = vQueryFTP & "('" & rs3.Fields("empresa").Value & "', '" & rs3.Fields("email").Value & "','" & rs3.Fields("codesta").Value & "','" & rs3.Fields("codusua").Value & "','" & rs3.Fields("periodo").Value & "','" & rs3.Fields("codpago").Value & "',0" & Replace(rs3.Fields("mtoref").Value, ",", ".") & ",0" & Replace(rs3.Fields("mtobs").Value, ",", ".") & ",0" & Replace(rs3.Fields("mtodivisa").Value, ",", ".") & ",0" & Replace(rs3.Fields("mtootro").Value, ",", ".") & ")," & vbCrLf
        rs3.MoveNext
        If i = 100 Then
            vMySqlQuery = Mid(vQueryFTP, 1, Len(vQueryFTP) - 3) & ";"

            SubirQueryHTTP
            RichTextBox1.text = vMySqlQuery
            RichTextBox1.SaveFile App.Path & "\QUERY" & x & ".TXT", 1
            x = x + 1
            i = 0
        End If
        i = i + 1
    Wend
x = x + 1

If vQueryFTP <> "" Then
    vQueryFTP = Mid(vQueryFTP, 1, Len(vQueryFTP) - 3)
    vMySqlQuery = vQueryFTP
    RichTextBox1.text = vQueryFTP
    RichTextBox1.SaveFile App.Path & "\" & vRif & "vc.txt", 1
    SubirQueryHTTP
End If
End Sub





Private Sub Timer2_Timer()
Timer2.Enabled = False
    Timer1.Interval = 60000  ' Configuramos el Timer para que se active cada minuto
    Contador = 0 ' Inicializamos el contador
    Timer1.Enabled = True
    EjecutarFuncion
End Sub
