;** TransfRQ  ******************************************************************
;*  Transferencia  de Requisi‡äes                                              *
;*******************************************************************************
;Autor           : Vise Informatica (VISERP)
;Desenvolvido    : 08/05/2020 por Bruno Carvalho                  
;Ultima Alteracao:   /  /     por                             
;Motivo Alteracao: 

;*******************************************************************************
;* ROTINA PROCESSA  -> realiza o processamento da Transferencia                *
;*******************************************************************************
Proc PROCESSA()

AGUARDE("Aguarde, transferindo requisi‡äes...")
ClearAll

Array Arquivo[5]
Arquivo[1]="Requisit"
Arquivo[2]="RqCabeca"
Arquivo[3]="RqItem"
Arquivo[4]="RqItemG"
Arquivo[5]="RqMatPri" 

Cont = 1
SetDir "\\Sistemas\\Estoque"
For Cont From 1 To 5
    AGUARDE("Aguarde, Transferindo "+Upper(Arquivo[Cont]))
    If IsTable(Arquivo[Cont])
       Then Switch
              Case Arquivo[Cont]="Requisit" : 
                   {Ask} {Requisit} Check
              Case Arquivo[Cont]="RqCabeca" :
                   If VSDGTIPOEST=""
                      Then {Ask} {RqCabeca} Check
                           Moveto [Numero do Lancamento] Typein "~VSDGREQUISICAO.."
                           Moveto [Codigo da Empresa   ] TypeIn "~VSDGEMPRESA.."
                      Else {Ask} {RqCabeca} Check
                           Moveto [Numero do Lancamento] Typein "_nlanc, ~VSDGREQUISICAO.."
                           Moveto [Codigo da Empresa   ] TypeIn "~VSDGEMPRESA.."
                           Moveto [Data de Ocorrencia  ] TypeIn ">=~VSDGDTINI, <=~VSDGDTFIM"
                           {Ask} {RqItem} 
                           Moveto [Numero do Lancamento] Typein "_nlanc, ~VSDGREQUISICAO.."
                           Moveto [Tipo do Item        ] Typein "~VSDGTIPOEST.."
                   EndIf        
              OtherWise :                                                               
                   {Ask} TypeIn PASTA+"RqCabeca" Enter                          ;RqCabe‡a j  criada no processo 2
                   Moveto [Numero do Lancamento] Example "nlanc"
                   {Ask} TypeIn Arquivo[Cont] Enter Check
                   Moveto [Numero do Lancamento] Example "nlanc"
            EndSwitch
            Do_It!
            ClearAll
            
            If Arquivo[Cont]="RqCabeca" And IsEmpty("Answer")
               Then QuitLoop
            EndIf   
            
            If IsTable(PASTA+Arquivo[Cont])
               Then Delete PASTA+Arquivo[Cont]
            Endif
            
            Create PASTA+Arquivo[Cont] Like Arquivo[Cont]
            Add "Answer" PASTA+Arquivo[Cont]
            ClearAll
            
            If Arquivo[Cont]="RqCabeca"
               Then Gerou_Dados=True
            EndIf        
    EndIf
EndFor

ClearAll
Limpaguarde()
EndProc

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

Deleta_Arquivo(PASTA+"Requisit.*")
Deleta_Arquivo(PASTA+"RqCabeca.*")
Deleta_Arquivo(PASTA+"RqItem.*")
Deleta_Arquivo(PASTA+"RqItemG.*")
Deleta_Arquivo(PASTA+"RqMatPri.*")
Sleep 1000

Deleta_Arquivo(VSDGDRIVE+"\\Requisit.*")
Deleta_Arquivo(VSDGDRIVE+"\\RqCabeca.*")
Deleta_Arquivo(VSDGDRIVE+"\\RqItem.*")
Deleta_Arquivo(VSDGDRIVE+"\\RqItemG.*")
Deleta_Arquivo(VSDGDRIVE+"\\RqMatPri.*")
Sleep 1000 

EndProc

;*******************************************************************************
;* 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_REQUISICAO"    : Campo="TG_EMPRESA"
            Case Campo="TG_REQUISIT"      : Campo="TG_REQUISICAO"   
            Case Campo="TG_TPMOV"         : Campo="TG_REQUISIT"
            Case Campo="TG_TPOPE"         : Campo="TG_TPMOV"   
            Case Campo="TG_TIPOEST"       : If Not IsBlank(VSDGTPMOV)
                                               Then Campo="TG_TPOPE"
                                               Else Campo="TG_TPMOV"
                                            EndIf    
            Case Campo="TG_DTINI"         : Campo="TG_TIPOEST"
            Case Campo="TG_DTFIM"         : Campo="TG_DTINI"
            Case Campo="TG_DTREF"         : If Not IsBlank(VSDGREQUISICAO)
                                               Then Campo="TG_REQUISICAO"
                                               Else Campo="TG_DTFIM"
                                            EndIf   
            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"    :
                         If Isblank(VSDGEMPRESA)
                            Then VSDGNOMEMP=""
                            Else Campo="TG_REQUISICAO"
                         EndIf
                    Case Campo="TG_REQUISICAO" : 
                         If Not IsBlank(VSDGREQUISICAO) 
                            Then VSDGREQUISIT    = ""
                                 VSDGNOMREQUI    = ""
                                 VSDGTPMOV       = ""
                                 VSDGNOMTPMOV    = ""
                                 VSDGTPOPE       = ""
                                 VSDGNOMTPOPE    = ""
                                 VSDGTIPOEST     = ""
                                 VSDGDESCTIPOEST = ""
                                 VSDGDTINI       = BlankDate()
                                 VSDGDTFIM       = BlankDate()
                                 If VSDGREQUISICAO<>""
                                    Then GetRecord "\\Sistemas\\Estoque\\RqCabeca" VSDGREQUISICAO To Reg_Req
                                         If RetVal
                                            Then VSDGEMPRESA = Reg_Req["Codigo da Empresa"]
                                                 VSDGNOMEMP  = Reg_Req["Nome Fantasia Empresa"]
                                         EndIf     
                                 EndIf   
                                 Campo="TG_DTREF"                  
                            Else Campo="TG_REQUISIT"
                         EndIf   
                    Case Campo="TG_REQUISIT"   : Campo="TG_TPMOV"
                    Case Campo="TG_TPMOV"      :
                         VSDGTPMOV=ControlVAlue("TG_TPMOV")
                         If Not IsBlank(VSDGTPMOV)
                            Then If VSDGTPMOV="E"
                                    Then VSDGNOMTPMOV = "ENTRADA"
                                    Else VSDGNOMTPMOV = "SAIDA"
                                 EndIf
                                 Campo="TG_TPOPE"
                            Else VSDGNOMTPMOV = "TODAS"
                                 VSDGTPOPE    = ""
                                 VSDGNOMTPOPE = "TODAS"
                                 Campo        = "TG_TIPOEST"
                         Endif
                    Case Campo="TG_TPOPE"    :
                         VSDGTPOPE = ControlValue("TG_TPOPE")
                         Switch
                            Case VSDGTPOPE = "C":
                                 VSDGNOMTPOPE = "CONSUMO"
                            Case VSDGTPOPE = "T":
                                 VSDGNOMTPOPE = "TRANSFERENCIA"
                            Case VSDGTPOPE = "P":
                                 VSDGNOMTPOPE = "PRODUCAO"
                            Case VSDGTPOPE = "I":
                                 VSDGNOMTPOPE = "INDUSTRIALIZACAO"     
                            Case VSDGTPOPE = "O":
                                 VSDGNOMTPOPE = "OUTROS"
                            Case VSDGTPOPE = "D":
                                 VSDGNOMTPOPE = "DEVOLUCAO"
                            Otherwise           :
                                 VSDGNOMTPOPE = "TODAS"
                         EndSwitch
                         Campo = "TG_TIPOEST"
                    Case Campo="TG_TIPOEST" : Campo="TG_DTINI"     
                    Case Campo="TG_DTINI"   :
                         If Isblank(VSDGDTINI)
                            Then Mensagem("Data inicial invalida !")
                                 Campo="TG_DTINI"
                            Else Campo="TG_DTFIM"
                         EndIf
                    Case Campo="TG_DTFIM"   :
                         If VSDGDTFIM<VSDGDTINI
                            Then Mensagem("Data final invalida !")
                            Else Campo="TG_DTREF"
                         EndIf
                    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_TPMOV" :
       MSG_CAMPO = "Branco=Todas  E=Entrada  S=Sa¡da"
  Case Campo = "TG_TPOPE" :
       MSG_CAMPO = "Branco=Todas  C=Consumo  P=Producao  I=Indust. T=Transf.  D=Devol.  O=Outros"
  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  "
  Case Campo="TG_EMPRESA":
       MSG_CAMPO="Informe uma empresa"
  Case Campo="TG_DTINI":
       MSG_CAMPO="Informe a data inicial"
EndSwitch
Monta_Prompt(MSG_CAMPO,TECLA)
Return Temp
EndProc

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

UNIDADE_ORIGEM    = SubStr(Upper(Directory()),1,1)
VSDGEMPRESA       = ""
VSDGREQUISICAO    = ""
VSDGNOMREQUISICAO = ""
VSDGREQUISIT      = ""
VSDGNOMREQUI      = ""
VSDGTPMOV         = ""
VSDGNOMTPMOV      = ""
VSDGTPOPE         = ""
VSDGNOMTPOPE      = ""
VSDGTIPOEST       = ""
VSDGDESCTIPOEST   = ""
VSDGDTINI         = BlankDate()
VSDGDTFIM         = Today()-1
VSDGDTREF         = BlankDate()

DynArray Array_Drives[]
SysInfo Drives To Array_Drives
VSDGDRIVE=SubStr(Directory(),1,2)

While True
  ShowDialog "Exporta‡„o de Requisi‡äes [Estoque]" Proc "SD_Dialog"
  Trigger "OPEN","ARRIVE"
  Key "F1","ENTER","ESC","TAB",
      -104, -105, -106, -107, -108, -109, -110, -111, -112, -113, -139, -140  
   @ 05,12   Height 17 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 "N§ Requisi‡Æo"  For "TG_REQUISICAO"
    Accept @ 03,01 Width 09 "A6"  Picture "#*#" Tag "TG_REQUISICAO" To VSDGREQUISICAO
    Accept @ 03,09 Width 25 "A22" Picture "#*#" Tag "TG_NOMREQUISICAO" To VSDGNOMREQUISICAO
                                                                                                                        
    Label  @ 04,00 "Requisitante"  For "TG_REQUISIT"
    Accept @ 05,01 Width 09 "A6" Picture "#*#" Tag "TG_REQUISIT"  To VSDGREQUISIT
    Accept @ 05,09 Width 25 "A35" Picture "!*!" Tag "TG_NOMREQUI" To VSDGNOMREQUI
    
    Label  @ 06,00 "Tipo de Movimento" For "TG_TPMOV"
    Accept @ 07,04 Width 04 "A1"   Picture "E,S" Tag "TG_TPMOV" To VSDGTPMOV
    Accept @ 07,09 Width 25 "A30"  Picture "!*!" Tag "TG_NOMTPMOV" To VSDGNOMTPMOV

    Label  @ 08,00 "Tipo de Opera‡„o" For "TG_TPOPE"
    Accept @ 09,04 Width 04 "A1"   Picture "C,P,T,O,D,I" Tag "TG_TPOPE" To VSDGTPOPE
    Accept @ 09,09 Width 25 "A30"  Picture "!*!" Tag "TG_NOMTPOPE" To VSDGNOMTPOPE

    Label  @ 10,00 "Tipo do Estoque"  For "TG_TIPOEST"
    Accept @ 11,04 Width 04 "A1"  Picture "!" Tag "TG_TIPOEST" To VSDGTIPOEST
    Accept @ 11,09 Width 25 "A30"  Picture "!*!" Tag "TG_DESCTIPOEST" To VSDGDESCTIPOEST

    Label  @ 00,35 "Per¡odo Referente de" For "TG_DTINI"
    Accept @ 01,38 Width 13 "D"  Picture "#[#];.##;.##[##]" Tag "TG_DTINI" To VSDGDTINI
    Label  @ 01,51 "at‚" For "TG_DTFIM"
    Accept @ 01,56 Width 13 "D"  Picture "#[#];.##;.##[##]" Tag "TG_DTFIM" To VSDGDTFIM

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

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

    Pushbutton @ 13,22 width 11 "~O~k"      OK     Default Value "Ok" Tag "YES" To BottonValue
    Pushbutton @ 13,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+"RQ"+"_"+CODEMP+"_"+
          AlinhM(StrVal(Year(VSDGDTREF)),4) +
          AlinhM(StrVal(Month(VSDGDTREF)),2)+
          AlinhM(StrVal(Day(VSDGDTREF)),2)  +
          ".ARJ"

  Aguarde("Aguarde, excluindo arquivos temporarios")
  Deleta_Arquivos()
  
  FileWrite PrivDir()+"fundel.txt"  From  "DELT|"+VSDGDRIVE+"\\"+ARQ_ARJ+"\n"
  ExecVisecf(PrivDir()+"fundel.txt","")
  Sleep 2000
  
  Gerou_Dados=False  
  Processa()           

  If Not Gerou_Dados
     Then Deleta_Arquivos()
          Mensagem("NÆo h  requisi‡Æo conforme sele‡Æo!// //Verificar Empresa, Data Referente !//Pacote nÆo foi gerado !")
     Else 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+"R*.*"+"\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
 EndWhile
ClearAll
Release Vars All
Release Procs All