;** Transfdp  ******************************************************************
;*  Transferencia P/ Drives  (Duplictaas)                                     *
;*******************************************************************************
;Autor           : Vise Informatica (VISERP)
;Desenvolvido    : 04/09/2018 por Anivel Augusto Rodrigues
;Ultima Alteracao: 03/06/2019 por Bruno Carvalho
;Motivo Alteracao: Adicionado UNIDADE_ORIGEM no nome do pacote compactado PT_EDP_XXXX_XXXXXXXX -> PT_?DP_????_???????? (De EDP p/ XDP ou VDP)

;*******************************************************************************
;* ROTINA PROCESSA  -> realiza o processamento da Transferencia                *
;*******************************************************************************
Proc PROCESSA()
private Arquivo, wi

AGUARDE("Aguarde, Transferindo Duplicatas")
ClearAll

Array Arquivo[01]
Arquivo[1] ="Duplic"

for wi from 1 to 1

  AGUARDE("Aguarde, Transferindo "+Arquivo[wi]+" ["+Format("W2,AR",wi)+"/1]")

  If IsTable(Arquivo[wi])
     Then 
          If Arquivo[wi]<>"Duplic"
             Then 

             Else {Ask} {Duplic} Check
                  Moveto [Codigo da Empresa  ] TypeIn "~VSDGEMPRESA.."
                  Moveto [Data de Emissao    ] TypeIn ">=~VSDGDTINI, <=~VSDGDTFIM"
                  Moveto [Tipo do Documento  ] TypeIn "Not ROM.."
                  Moveto [Codigo do Documento] TypeIn "~VSDGTPDOCTO.."
                  Moveto [Valor do Documento ] TypeIn "_vr"
                  Moveto [Saldo do Documento ] TypeIn "= _vr"
          Endif
          Do_It!
          ClearAll

          If IsTable(PASTA+Arquivo[wi])
             Then Delete PASTA+Arquivo[wi]
          Endif
          Create PASTA+Arquivo[wi] Like Arquivo[wi]
          Add "Answer" PASTA+Arquivo[wi]
          ClearAll

          If Arquivo[wi]="Duplic" And Not IsEmpty("Answer")
             Then Gerou_Dados=True
          EndIf   
  EndIf
  
EndFor

ClearAll
Limpaguarde()
EndProc

;*******************************************************************************
;* ROTINA PARA DELETAR ARQUIVOS                                                *
;*******************************************************************************
Proc Deleta_Arquivos()

Deleta_Arquivo(PASTA+"DUPLIC.*")
Sleep 1000

Deleta_Arquivo(VSDGDRIVE+"\\DUPLIC.*")
Sleep 1000

EndProc ;Deleta_Arquivos() 

;*******************************************************************************
;* Rotina para gerar o log de registro por usu rio                             *
;*******************************************************************************
Proc Gera_Log()          
;---- Gravando LOG ---------------------------------------------------
If VSDGDTINI<>BlankDate()
   Then AUX_DATA = ", periodo de "+StrVal(VSDGDTINI)+" ate "+StrVal(VSDGDTFIM)
   Else AUX_DATA = ", periodo ate "+StrVal(VSDGDTFIM)
EndIf   

If VSDGDTREF<>BlankDate()
   Then AUX_DTREF = ", transacao em "+StrVal(VSDGDTREF)
   Else AUX_DTREF = ""
EndIf

AUX_OPCAO=""
If VSDGTPDOCTO<>"" Then AUX_OPCAO = ", Tipo Docto "+VSDGTPDOCTO EndIf

Grava_LogEx("TRANSFDP")  

EndProc ;Gera_Log()

;*******************************************************************************
;* Rotina                                                                      *
;*******************************************************************************
Proc SD_Dialog(Disparo,Campo,Evento,Elemento)
Temp=True
MSG_CAMPO=""
TECLA=""
If Disparo="OPEN"  Then AoCriarShowDialog() EndIf
If Disparo="EVENT" And Evento["Type"]="KEY" Then
   Switch
     Case Evento["KeyCode"] =   9: Temp=False
     Case Evento["KeyCode"] = -59: Dialog_F1()
     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"] =  27:
          Temp=False
          Switch
            Case Campo="TG_EMPRESA"       : Campo="NO"
            Case Campo="TG_TPDOCTO"       : Campo="TG_EMPRESA"
            Case Campo="TG_DTINI"         : Campo="TG_TPDOCTO"
            Case Campo="TG_DTFIM"         : Campo="TG_DTINI"
            Case Campo="TG_DTREF"         : Campo="TG_DTFIM"
            Case Campo="TG_DRIVES"        : Campo="TG_DTREF"
            Case Campo="YES" Or Campo="NO": Campo="TG_EMPRESA"
          EndSwitch
     Case Evento["KeyCode"] = 13:
          Temp=False
          If Checa_TAG()
             Then Switch
                    Case Campo="TG_EMPRESA" : Campo="TG_TPDOCTO"
                    Case Campo="TG_TPDOCTO" : Campo="TG_DTINI"
                    Case Campo="TG_DTINI"   : Campo="TG_DTFIM"
                    Case Campo="TG_DTFIM"   : Campo="TG_DTREF"
                    Case Campo="TG_DTREF"   : Campo="TG_DRIVES"
                    Case Campo="TG_DRIVES"   :
                         VSDGDRIVE=ControlValue("TG_DRIVES")
                         Campo="YES"
                  EndSwitch
          EndIf
   EndSwitch
EndIf

Switch
  Case Campo="TG_DTREF" :
       MSG_CAMPO="Informe a data da Transa‡„o"
  Case Campo="TG_DRIVES":
       MSG_CAMPO="Selecione o drive desejado !"
       TECLA=Chr(27)+""+Chr(26)+"=Selecionar  "
EndSwitch

Monta_Prompt(MSG_CAMPO,TECLA)
Return Temp
EndProc

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

VSDGEMPRESA    = ""
VSDGDTREF      = BlankDate()
VSDGTPDOCTO    = ""
VSDGDTINI      = BlankDate()
VSDGDTFIM      = BlankDate()
UNIDADE_ORIGEM = SubStr(Upper(Directory()),1,1)

DynArray Array_Drives[]
SysInfo Drives To Array_Drives
If IsFile("OEXPNFS.sc")
    Then Play "OEXPDPS"
    Else VSDGDRIVE=SubStr(Directory(),1,2)
EndIf

While True
  ShowDialog "Exporta‡„o Duplicatas" Proc "SD_Dialog"
  Trigger "OPEN","ARRIVE"
  Key "F1","ENTER","ESC",
      -104, -105, -106, -107, -108, -109, -110, -111, -112, -113, -139, -140  
   @ 05,12   Height 13 Width 72

    Label  @ 00,00 "Empresa"  For "TG_EMPRESA"
    Accept @ 01,01 Width 07 "A4"  Picture "[&,#][#*#]" Tag "TG_EMPRESA" To VSDGEMPRESA
    Accept @ 01,09 Width 25 "A20" Picture "!*!" Tag "TG_NOMEMP"  To VSDGNOMEMP

    Label  @ 02,00 "Tipo do Documento"  For "TG_TPDOCTO"
    Accept @ 03,01 Width 07 "A4"  Picture "!*!" Tag "TG_TPDOCTO" To VSDGTPDOCTO
    Accept @ 03,09 Width 25 "A20" Picture "!*!" Tag "TG_NOMTPDOCTO"  To VSDGNOMTPDOCTO

    Label  @ 04,00 "Per¡odo EmissÆo de" For "TG_DTINI"
    Accept @ 05,03 Width 13 "D"  Picture "#[#];.##;.##[##]" Tag "TG_DTINI" To VSDGDTINI
    Label  @ 05,16 "at‚" For "TG_DTFIM"
    Accept @ 05,21 Width 13 "D"  Picture "#[#];.##;.##[##]" Tag "TG_DTFIM" To VSDGDTFIM

    Label  @ 06,00 "Data Transa‡„o" For "TG_DTREF"
    Accept @ 07,03 Width 13 "D"  Picture "#[#];.##;.##[##]" Tag "TG_DTREF" To VSDGDTREF

    Label  @ 00,35 "Drives:" For "TG_DRIVES"
    PickDynArrayIndex @ 01,36 Height 4 width 33
     Columns 7
      Array_Drives
       Tag "TG_DRIVES"
        To VSDGDRIVE

    Pushbutton @ 09,22 width 11 "~O~k"      OK     Default Value "Ok" Tag "YES" To BottonValue
    Pushbutton @ 09,36 width 11 "~C~ancela" CANCEL                    Tag "NO"  To BottonValue

  EndDialog
  If Not RetVal Then QuitLoop EndIf

  If IsBlank(VSDGEMPRESA) Or
     IsBlank(VSDGDTREF)
     Then Mensagem("Informe os dados corretamente !")
          Loop
  EndIf

  DATAREF=AlinhM(StrVal(Day(VSDGDTREF)),2)+AlinhM(StrVal(Month(VSDGDTREF)),2)+SubStr(AlinhM(StrVal(Year(VSDGDTREF)),4),3,2)

  Aguarde("Aguarde, verificando diret¢rio")
  PASTA = PrivDir()+"TMP"
  If DirExists(PASTA)=0
     Then Cria_pasta(PASTA)
  EndIf
  PASTA = PASTA + "\\"
         
  If VSDGEMPRESA="" 
     Then CODEMP="0000" 
     Else CODEMP=VSDGEMPRESA
  EndIf

  ARQ_ARJ="PT_"+UNIDADE_ORIGEM+"DP"+"_"+CODEMP+"_"+
          AlinhM(StrVal(Year(VSDGDTREF)),4) +
          AlinhM(StrVal(Month(VSDGDTREF)),2)+
          AlinhM(StrVal(Day(VSDGDTREF)),2)  +
          ".ARJ"

  Deleta_Arquivos()
  
  FileWrite        PrivDir()+"fundel.txt"  From  "DELT|"+VSDGDRIVE+"\\"+ARQ_ARJ+"\n"
  FileWrite Append PrivDir()+"fundel.txt"  From  "DELT|"+PASTA+ARQ_ARJ+"\n"
  ExecVisecf(PrivDir()+"fundel.txt","")
  Sleep 1000

  Gerou_Dados=False
  Processa()

  If Not Gerou_Dados
     Then Deleta_Arquivos()
          Mensagem("NÆo existe(m) duplicatas(s) conforme sele‡Æo !// //Verificar Empresa, TpDocto, Data Referente !//Pacote nÆo foi gerado !")
     Else Gera_Log()


          Aguarde("Aguarde, compactando pacote de transa‡„o p/ "+ARQ_ARJ)
           
          FileWrite PrivDir()+"funcmd.txt" From "CMD|"+"c:\\vise\\Varj.exe a "+PASTA+ARQ_ARJ+" "+PASTA+"DUPLIC*.*"+"\n"
          ExecVisecf(PrivDir()+"funcmd.txt","")
          Sleep 1000
          
          Aguarde("Aguarde, copiando arquivo para a unidade "+VSDGDRIVE)
          FileWrite PrivDir()+"funcop.txt" From "COP|"+PASTA+ARQ_ARJ+"|"+VSDGDRIVE+"\\"+ARQ_ARJ+"\n" 
          ExecVisecf(PrivDir()+"funcop.txt","")
          Sleep 1000
              
          ;---- Excluindo Arquivo Temporario -----------------------------------
          Aguarde("Aguarde, excluindo arquivos temporarios")
          FileWrite PrivDir()+"fundel.txt" From "DELT|"+PASTA+ARQ_ARJ+"\n"
          ExecVisecf(PrivDir()+"fundel.txt","")
          Sleep 1000    
              
          Deleta_Arquivos()
          
          ;---- Excluindo Arquivo de Retorno -----------------------------------
          Aguarde("Aguarde, finalizando o processo")
          FileWrite        PrivDir()+"funcao.txt"  From  "DELT|"+PrivDir()+"fundel.ret"+"\n"
          FileWrite Append PrivDir()+"funcao.txt"  From  "DELT|"+PrivDir()+"funcmd.ret"+"\n"
          FileWrite Append PrivDir()+"funcao.txt"  From  "DELT|"+PrivDir()+"funcop.ret"+"\n"
          FileWrite Append PrivDir()+"funcao.txt"  From  "DELT|"+PrivDir()+"funren.ret"+"\n"
          ExecVisecf(PrivDir()+"funcao.txt","")
          
          MSG="Pacote de transa‡„o gerado !// //"
          MSG=MSG+"Arquivo em "+VSDGDRIVE+"\\"+ARQ_ARJ+"// //"
          MSG=MSG+"Verifique !"
          Mensagem(MSG)
  EndIf

  FileWrite        "OEXPDPS.sc" From ";****** Variaveis de Configura‡„o Drive Padrao Vise Informatica"+"\n"
  FileWrite Append "OEXPDPS.sc" From "VSDGDRIVE="+CHR(34)+VSDGDRIVE+Chr(34)+"\n"

EndWhile
ClearAll
Release Vars All
Release Procs All
