;**Exp_reg  ********************************************************************
;*  Exportacao de dados p/ Registradora                                        *
;*******************************************************************************
;Autor           : Vise Informatica (VISERP)
;Desenvolvido    : 10/08/2004 por Davi Pereira Carrano
;Ultima Alteracao: 13/08/2007 por Anivel Augusto Rodrigues
;Motivo Alteracao: Ajustes Deleta_Arquivo
;Ultima Alteracao: 15/08/2007 por Anivel Augusto Rodrigues
;Motivo Alteracao: Ajustes Run
;Ultima Alteracao: 19/09/2012 por Anivel Augusto Rodrigues
;Motivo Alteracao: Ajustes Substitui‡Ćo Editor novo arquivo por FileWrite

;*******************************************************************************
;* Gera Arquivo Texto p/ Produtos                                              *
;*******************************************************************************
Proc EXPORTA()
ClearAll
CC=0
CD=0
For I from 1 to 4
   If VSDGPORTA[I]= False
      Then Loop
   EndIf

   PORTA=StrVal(I)

   ;===================================
   ;=============PRODUTOS==============
   ;===================================


   ;============================================================================
   ;Criar o arquivo de Configura‡Ćo para ler os dados da registradora
   ;============================================================================
   SetDir "C:\\Vise\\G930"

   Deleta_Arquivo("PLUR.CFG")
   Deleta_Arquivo("G91PLU.TXT")

    Arq_Config="PLUR.CFG"
    Print File Arq_Config PORTA+"\n"
    Print File Arq_Config "038400"+"\n"
    Print File Arq_Config " M   0    Dando Sequencia a Comunicacao"+"\n"
    Print File Arq_Config " M   0    Lendo as Plu's Programadas"+"\n"
    Print File Arq_Config " L   S    G91PLU.TXT"+"\n"
    Executa_Programa("g930 PLUR")

    SetDir Driver +"\\Sistemas\\Exporta"

   ;============================================================================
   ;Importar os dados lidos d um arquivo TXT lido da registradora para uma tabela do paradox
   ;============================================================================
   If IsTable("Exp_Pad2")
      Then Delete "Exp_Pad2"
   EndIf


   {Tools} {ExportImport} {Import} {ASCII} {Text} TypeIn "C:\\Vise\\G930\\G91PLU.txt" Enter {Exp_Pad2}
   ClearAll

   SetDir "\\Sistemas\\Estoque"
   If Not IsEmpty("\\Sistemas\\Exporta\\Exp_Pad2")
      Then
           NR=NRecords("\\Sistemas\\Exporta\\Exp_Pad2")

           View   "\\Sistemas\\Exporta\\Exp_Pad2"
           View   "\\Sistemas\\Vendas\\Tbprite"
           View   "\\Sistemas\\Vendas\\Tbprcab"
           Coedit "Itens"
           Moveto "\\Sistemas\\Exporta\\Exp_Pad2"
           Scan
             Barra(NR, RecNo())
             AGUARDE("Aguarde, Importando Produtos para Vise"+BARRA_STRING)

             CC=CC+1
             Moveto "Itens"
             End Down

             [Codigo de Barras      ]=SubStr([\Sistemas\Exporta\Exp_Pad2->Text],1,12)
             [Codigo do Item        ]=AlinhM(SubStr([\Sistemas\Exporta\Exp_Pad2->Text],15,4),10)
             [Descricao do Item     ]=SubStr([\Sistemas\Exporta\Exp_Pad2->Text],21,14)
             [Codigo do Departamento]=SubStr([\Sistemas\Exporta\Exp_Pad2->Text],47,2)
             [Tributacao de IPI     ]="N"
             [Tributacao de ISSQN   ]="N"
             [Tributacao de IR      ]="N"
             [Tributacao de INSS    ]="N"
             [Tipo do Item          ]="P"
             If SubStr([\Sistemas\Exporta\Exp_Pad2->Text],50,2)="01"
                Then [Inativo]="N"
                Else [Inativo]="S"
             EndIf


             If CC=1
                Then Moveto "\\Sistemas\\Vendas\\Tbprcab"
                     End Down
                     [Codigo da Tabela   ]=VSDGTABELA
                     [Descricao da Tabela]=VSDGNOMTABELA
                     [Ativa              ]="S"
             EndIf


             Moveto "\\Sistemas\\Vendas\\Tbprite"
             End Down
             [Codigo da Tabela ]=VSDGTABELA
             [Codigo do Item   ]=AlinhM(SubStr([\Sistemas\Exporta\Exp_Pad2->Text],15,4),10)
             [Tipo do Item     ]="P"
             [Descricao do Item]=SubStr([\Sistemas\Exporta\Exp_Pad2->Text],21,14)
             [Preco Unitario   ]=NumVal(SubStr([\Sistemas\Exporta\Exp_Pad2->Text],36,9))



             Moveto "\\Sistemas\\Exporta\\Exp_Pad2"
           EndScan
           Do_It!
           ClearAll

   EndIf
   SetDir Driver + "\\Sistemas\\Exporta"
EndFor
EndProc

Proc Departamento()
   ;===================================
   ;=========DEPARTAMENTOS=============
   ;===================================

   ;============================================================================
   ;Criar o arquivo de Configura‡Ćo para ler os dados dos departamentos da registradora
   ;============================================================================
   SetDir "C:\\Vise\\G930"

   Deleta_Arquivo("DLUR.CFG")
   Deleta_Arquivo("G91DLU.TXT")

    Arq_Config="DLUR.CFG"
    Print File Arq_Config PORTA+"\n"
    Print File Arq_Config "038400"+"\n"
    Print File Arq_Config " M   0    Dando Sequencia a Comunicacao"+"\n"
    Print File Arq_Config " M   0    Lendo os Departamentos Programados"+"\n"
    Print File Arq_Config " L   D    G91DLU.TXT"+"\n"
    Executa_Programa("g930 DLUR")

    SetDir Driver +"\\Sistemas\\Exporta"


   ;============================================================================
   ;Importar os dados lidos da impressora de um arquivo TXT para uma tabela do paradox
   ;============================================================================

   If IsTable("Exp_Pad6")
      Then Delete "Exp_Pad6"
   EndIf


   {Tools} {ExportImport} {Import} {ASCII} {Text} TypeIn "C:\\Vise\\G930\\G91DLU.txt" Enter {Exp_Pad6}
   ClearAll



   If Not IsEmpty("Exp_Pad6")
      Then
           View   "\\Sistemas\\Estoque\\Depart"
           CoEdit "Exp_Pad6"
           Scan
             CD=CD+1
             Moveto "\\Sistemas\\Estoque\\Depart"
             End Down
             [Codigo do Departamento   ]=SubStr([\Sistemas\Exporta\Exp_Pad6->Text],1,2)
             [Descricao do Departamento]=SubStr([\Sistemas\Exporta\Exp_Pad6->Text],4,18)

             Moveto "Exp_Pad6"
           EndScan
           Do_It!
           ClearAll

   EndIf

   SetDir Driver + "\\Sistemas\\Exporta"




EndProc ;(Exporta)


;********************************************************************************
;* CONVERTE VALOR PARA STRING                                                  *
;********************************************************************************
Proc Valor_Str(Valor,Tam,Dec)
Private VrTit,Valor,Tam,Dec,Part1,Part2

VrTit=StrVal(Valor)
If SearchFrom(".",VrTit,1)>0
   Then Part1=SubStr(VrTit,1,SearchFrom(".",VrTit,1)-1)
        Part2=SubStr(VRTit,SearchFrom(".",VrTit,1)+1,Dec)
        Part2=PART2+Fill(0,DEC-Len(Part2))
   Else Part1=VrTit
        Part2=Fill(0,Dec)
EndIf
Vrtit=Part1+Part2
If Len(Vrtit)>=6
   Then Vrtit=SubStr(Vrtit,1,5)
   Else VrTit=Fill("0",Tam-Len(VrTit))+VrTit
EndIf

Return VrTit
EndProc  ; (Valor_Str)

;*******************************************************************************
;* Rotina                                                                      *
;*******************************************************************************
Proc SD_Dialog(Disparo,Campo,Evento,Elemento)
Temp=True
TECLA=""
MSG_CAMPO=""
If Disparo="OPEN"  Then AoCriarShowDialog() EndIf
If Disparo="EVENT" And Evento["Type"]="KEY" Then
   Switch
     Case Evento["KeyCode"] = -59: Dialog_F1()
     Case Evento["KeyCode"] =  27:
          Temp=False
          Switch
            Case Campo="TG_PORTA"         : Campo="NO"
            Case Campo="TG_TAB"        : Campo="TG_PORTA"
            Case Campo="YES" Or Campo="NO": Campo="TG_PORTA"
          EndSwitch
     Case (Evento["KeyCode"] <= -104 And Evento["KeyCode"] >= -113) Or 
           Evento["KeyCode"]  = -139 Or  Evento["KeyCode"]  = -140:
           SYS_TECLA_ACIONADA=Evento["KeyCode"]
           Seta_Tecla_Acionada()
           CancelDialog
     Case Evento["KeyCode"] = 13:
          Temp=False
          If Checa_TAG()
             Then Switch
                    Case Campo="TG_PORTA":
                         If (VSDGPORTA[1]=False) And
                            (VSDGPORTA[2]=False) And
                            (VSDGPORTA[3]=False) And
                            (VSDGPORTA[4]=False)
                            Then Campo="TG_PORTA"
                            Else Campo="TG_TAB"
                         EndIf

                    Case Campo="TG_TAB"     :
                        VSDGNOMTABELA=ControlValue("TG_TAB")
                        If Not IsBlank(VSDGNOMTABELA)
                           Then Campo="YES"
                        EndIf

                  EndSwitch
          EndIf
          RefreshDialog
   EndSwitch
EndIf
Switch
 Case Campo="TG_PORTA"   : MSG_CAMPO="Porta(s) de Comunica‡Ćo(”es) que se encontra a(s) registradora(s)"
 Case Campo="TG_TAB"  : MSG_CAMPO="Tabela de Pre‡o"
EndSwitch
Monta_Prompt(MSG_CAMPO,TECLA)
Return Temp

EndProc


;************************************
;* ROTINA PARA Ler/Gravacao Opcao   *
;************************************
Proc ConfOpcoes(Opcao)
Private Opcao
Switch
  Case Opcao="Ler":
       If IsFile("EX_0001.sc")
           Then Play "EX_0001"
           Else VSDGPORTA[1]=False
                VSDGPORTA[2]=False
                VSDGPORTA[3]=False
                VSDGPORTA[4]=False
       EndIf
  Case Opcao="Gravar":
       FileWrite        "EX_0001.sc" From ";****** Variaveis de Configura‡„o Portas de Comunica‡Ćo"+"\n"
       FileWrite Append "EX_0001.sc" From "VSDGARQUIVO= "+StrVal(VSDGARQUIVO)+"\n"
       If VSDGPORTA[1]=True
          Then FileWrite Append "EX_0001.sc" From "VSDGPORTA[1]=True" +"\n"
          Else FileWrite Append "EX_0001.sc" From "VSDGPORTA[1]=False"+"\n"
       EndIf
       If VSDGPORTA[2]=True
          Then FileWrite Append "EX_0001.sc" From "VSDGPORTA[2]=True" +"\n"
          Else FileWrite Append "EX_0001.sc" From "VSDGPORTA[2]=False"+"\n"
       EndIf
       If VSDGPORTA[3]=True
          Then FileWrite Append "EX_0001.sc" From "VSDGPORTA[3]=True" +"\n"
          Else FileWrite Append "EX_0001.sc" From "VSDGPORTA[3]=False"+"\n"
       EndIf
       If VSDGPORTA[4]=True
          Then FileWrite Append "EX_0001.sc" From "VSDGPORTA[4]=True" +"\n"
          Else FileWrite Append "EX_0001.sc" From "VSDGPORTA[4]=False"+"\n"
       EndIf
EndSwitch
EndProc ;

;*******************************************************************************
;* ROTINA PRINCIPAL                                                            *
;*******************************************************************************
AutoLib="\\Sistemas\\Cadastro\\Bib_Show,\\Sistemas\\Cadastro\\Bibliot"
LimpAguarde()


Array VSDGPORTA[4]
VSDGPORTA[1]=False
VSDGPORTA[2]=False
VSDGPORTA[3]=False
VSDGPORTA[4]=False
VSDGTABELA="00001"
VSDGNOMTABELA=""
ConfOpcoes("Ler")


While True
 ShowDialog "Exporta‡„o de dados p/ Vise" Proc "SD_Dialog"
   Trigger "OPEN","ARRIVE"
    Key "TAB","ReverseTab","ENTER","ESC","F1",
      -104, -105, -106, -107, -108, -109, -110, -111, -112, -113, -139, -140    
     @ 05,12 Height 12 Width 46

    Label  @ 01,01 "Porta  " For "TG_PORTA"
    CheckBoxes @2,01 Height 2 Width 20 Tag "TG_PORTA"
    "COM1"       to VSDGPORTA[1],
    "COM2"       to VSDGPORTA[2],
    "COM3"       to VSDGPORTA[3],
    "COM4"       to VSDGPORTA[4]


    Label  @ 05,00 "Tabela"  For "TG_TAB"
;    Accept @ 06,01 Width 08 "A5"  Picture "#*#" Tag "TG_TABELA" To VSDGTABELA
    Accept @ 06,01 Width 33 "A30" Picture "!*!" Tag "TG_TAB" To VSDGNOMTABELA

    Pushbutton @ 08,08 width 13 "~O~k"      OK     Default Value "Ok" Tag "YES" To BottonValue
    Pushbutton @ 08,24 width 13 "~C~ancela" CANCEL                    Tag "NO"  To BottonValue

  EndDialog
  If Not RetVal Then QuitLoop EndIf

  Driver=SubStr(Directory(),1,2)

  Exporta()
  If Not CC>0
     Then MENSAGEM("N„o Ocorreu Exporta‡„o [Produtos] !")
  EndIf
  


  ConfOpcoes("Gravar")
  Mensagem("Exporta‡„o Concluida !")

EndWhile
ClearAll
Release Vars All
Release Procs All
