;** ExpWPNS   ******************************************************************
;* Exportacao de Pedidos Note->Server Vise Vendas                              *
;*******************************************************************************
;Autor           : Vise Informatica (VISERP)
;Desenvolvido    : 30/05/2011 por Bruno Carvalho
;Ultima Alteracao: 31/05/2011 por Bruno Carvalho
;Motivo Alteracao: Ajustado rotina para cria‡Æo de pastas pr¢prias pra exporta‡Æo

;*******************************************************************************
;* Exporta‡Æo . Cadastro                                                       *
;*******************************************************************************
Proc Clientes()

SetDir "\\Sistemas\\Cadastro"

{Ask} {\\Sistemas\\Cadastro\\Destinat} Check
Moveto [Tipo                   ] TypeIn "E or C"
Moveto [Codigo do Representante] TypeIn "~CODREP.."
Do_It!
ClearAll
Gera_Txt("Destinat")

{Ask} {\\Sistemas\\Cadastro\\Destinat}
Moveto [Codigo                 ] Example "clie"
Moveto [Tipo                   ] TypeIn "E or C"
Moveto [Codigo do Representante] TypeIn "~CODREP.."
{Ask} {\\Sistemas\\Cadastro\\Destinfo} Check
Moveto [Codigo                 ] Example "clie"
Do_It!
ClearAll
Gera_Txt("Destinfo")

{Ask} {\\Sistemas\\Cadastro\\Destinat}
Moveto [Codigo                 ] Example "clie"
Moveto [Tipo                   ] TypeIn "E or C"
Moveto [Codigo do Representante] TypeIn "~CODREP.."
{Ask} {\\Sistemas\\Cadastro\\Destobs} Check
Moveto [Codigo                 ] Example "clie"
Do_It!
ClearAll
Gera_Txt("Destobs")

{Ask} {\\Sistemas\\Cadastro\\Destinat}
Moveto [Codigo                 ] Example "clie"
Moveto [Tipo                   ] TypeIn "E or C"
Moveto [Codigo do Representante] TypeIn "~CODREP.."
{Ask} {\\Sistemas\\Cadastro\\DestAut} Check
Moveto [Codigo                 ] Example "clie"
Do_It!
ClearAll
Gera_Txt("DestAut")

{Ask} {\\Sistemas\\Cadastro\\Destinat}
Moveto [Codigo                 ] Example "clie"
Moveto [Tipo                   ] TypeIn "E or C"
Moveto [Codigo do Representante] TypeIn "~CODREP.."
{Ask} {\\Sistemas\\Cadastro\\Destmsg} Check
Moveto [Codigo                 ] Example "clie"
Do_It!
ClearAll
Gera_Txt("Destmsg")

{Ask} {\\Sistemas\\Cadastro\\Destinat}
Moveto [Codigo                 ] Example "clie"
Moveto [Tipo                   ] TypeIn "E or C"
Moveto [Codigo do Representante] TypeIn "~CODREP.."
{Ask} {\\Sistemas\\Cadastro\\End_Cob} Check
Moveto [Codigo                 ] Example "clie"
Do_It!
ClearAll
Gera_Txt("End_Cob")

{Ask} {\\Sistemas\\Cadastro\\Destinat}
Moveto [Codigo                 ] Example "clie"
Moveto [Tipo                   ] TypeIn "E or C"
Moveto [Codigo do Representante] TypeIn "~CODREP.."
{Ask} {\\Sistemas\\Cadastro\\FichaCli} Check
Moveto [Codigo                 ] Example "clie"
Do_It!
ClearAll
Gera_Txt("Fichacli")

{Ask} {\\Sistemas\\Cadastro\\Represen} Check
Moveto [Codigo do Representante] TypeIn "~CODREP.."
Do_It!
ClearAll
Gera_Txt("Represen")

{Ask} {\\Sistemas\\Cadastro\\Ativid} Check
Do_It!
ClearAll
Gera_Txt("Ativid")

{Ask} {\\Sistemas\\Cadastro\\Area} Check
Do_It!
ClearAll
Gera_Txt("Area")

{Ask} {\\Sistemas\\Cadastro\\Rota} Check
Do_It!
ClearAll
Gera_Txt("Rota")

{Ask} {\\Sistemas\\Cadastro\\Cidades} Check
Do_It!
ClearAll
Gera_Txt("Cidades")

{Ask} {\\Sistemas\\Cadastro\\Cobrador} Check
Do_It!
ClearAll
Gera_Txt("Cobrador")

{Ask} {\\Sistemas\\Cadastro\\Cartoes} Check
Do_It!
ClearAll
Gera_Txt("Cartoes")

{Ask} {\\Sistemas\\Cadastro\\Cfo} Check
Do_It!
ClearAll
Gera_Txt("Cfo")

{Ask} {\\Sistemas\\Cadastro\\Paises} Check
Do_It!
ClearAll
Gera_Txt("Paises")

{Ask} {\\Sistemas\\Cadastro\\Empresas} Check
Do_It!
ClearAll
Gera_Txt("Empresas")

{Ask} {\\Sistemas\\Cadastro\\P_Dest} Check
Do_It!
ClearAll
Gera_Txt("P_Dest")

{Ask} {\\Sistemas\\Cadastro\\Tppagto} Check
Do_It!
ClearAll
Gera_Txt("Tppagto")

{Ask} {\\Sistemas\\Cadastro\\Usuarios} Check
Do_It!
ClearAll
Gera_Txt("Usuarios")

EndProc

;*******************************************************************************
;* Exporta‡Æo . Estoque                                                        *
;*******************************************************************************
Proc Itens()

SetDir "\\Sistemas\\Estoque"

{Ask} {\\Sistemas\\Vendas\\TbPrCab}
Moveto [Codigo da Tabela       ] Example "Tab" TypeIn ", ~VSDGTABELA.."
Moveto [Usa_TLMK               ] TypeIn "S"
{Ask} {\\Sistemas\\Vendas\\TbPrIte} Check
Moveto [Codigo da Tabela       ] Example "Tab"
Do_It!
ClearAll

{Ask} {Answer}
Moveto [Codigo do Item         ] Example "Itens"
{Ask} {\\Sistemas\\Estoque\\Itens} Check
Moveto [Tipo do Item           ] TypeIn "P Or T Or D"
Moveto [Codigo do Item         ] Example "Itens"
Do_It!
ClearAll

Tabela=Cria_Temp(PrivDir())
Copy "Answer" Tabela
Gera_Txt("Itens")

{Ask} TypeIn Tabela Enter
Moveto [Codigo da Grade        ] Example "CodG"
{Ask} {\\Sistemas\\Estoque\\Tpgrade} Check
Moveto [Codigo da Grade        ] Example "CodG"
Do_It!
ClearAll

Delete Tabela
Gera_Txt("Tpgrade")

{Ask} {\\Sistemas\\Estoque\\Familia} Check  
Do_It!
ClearAll
Gera_Txt("Familia")

{Ask} {\\Sistemas\\Estoque\\Grupos} Check
Do_It!
ClearAll
Gera_Txt("Grupos")

{Ask} {\\Sistemas\\Estoque\\Marcas} Check
Do_It!
ClearAll
Gera_Txt("Marcas")

{Ask} {\\Sistemas\\Estoque\\Cores} Check
Do_It!
ClearAll
Gera_Txt("Cores")

{Ask} {\\Sistemas\\Estoque\\Iten_Cor} Check
Do_It!
ClearAll
Gera_Txt("Iten_Cor")

{Ask} {\\Sistemas\\Estoque\\Tppagto} Check
Do_It!
ClearAll
Gera_Txt("Tppagto")

EndProc

;*******************************************************************************
;* Exporta‡Æo . Tabela de Pre‡o                                                *
;*******************************************************************************
Proc TabPreco()

SetDir "\\Sistemas\\Vendas"

{Ask} {\\Sistemas\\Vendas\\TbPrCab} Check
Moveto [Codigo da Tabela       ] TypeIn "~VSDGTABELA.."
Moveto [Usa_TLMK               ] TypeIn "S"
Do_It!
ClearAll
Gera_Txt("Tbprcab")

{Ask} {\\Sistemas\\Vendas\\TbPrIte} Check
Moveto [Codigo da Tabela       ] TypeIn "~VSDGTABELA.."
Do_It!
ClearAll
Gera_Txt("Tbprite")

{Ask} {\\Sistemas\\Vendas\\TbPrCpg} Check
Moveto [Codigo da Tabela       ] TypeIn "~VSDGTABELA.."
Do_It!
ClearAll
Gera_Txt("Tbprcpg")

{Ask} {\\Sistemas\\Cadastro\\Destinat}
Moveto [Codigo                 ] Example "clie"
Moveto [Codigo do Representante] TypeIn "~CODREP.."
{Ask} {\\Sistemas\\Vendas\\TbPrCli} Check
Moveto [Codigo da Tabela       ] TypeIn "~VSDGTABELA.."
Moveto [Codigo                 ] Example "clie"
Do_It!
ClearAll
Gera_Txt("Tbprcli")

{Ask} {\\Sistemas\\Cadastro\\Destinat}
Moveto [Codigo                 ] Example "clie"
Moveto [Codigo do Representante] TypeIn "~CODREP.."
{Ask} {\\Sistemas\\Vendas\\TbPrCIt} Check
Moveto [Codigo da Tabela       ] TypeIn "~VSDGTABELA.."
Moveto [Codigo                 ] Example "clie"
Do_It!
ClearAll
Gera_Txt("Tbprcit")

{Ask} {\\Sistemas\\Vendas\\TbPrRep} Check
Moveto [Codigo da Tabela       ] TypeIn "~VSDGTABELA.."
Moveto [Codigo do Representante] TypeIn "~CODREP.."
Do_It!
ClearAll
Gera_Txt("Tbprrep")

{Ask} {\\Sistemas\\Vendas\\TbPrRIt} Check
Moveto [Codigo da Tabela       ] TypeIn "~VSDGTABELA.."
Moveto [Codigo do Representante] TypeIn "~CODREP.."
Do_It!
ClearAll
Gera_Txt("Tbprrit")

EndProc       

;*******************************************************************************
;* Exporta‡Æo . Vendas                                                         *
;*******************************************************************************
Proc TabVenda()

SetDir "\\Sistemas\\Vendas"  

{Ask} {\\Sistemas\\Vendas\\Condpgto} Check
Do_It!
ClearAll
Gera_Txt("Condpgto")

{Ask} {\\Sistemas\\Vendas\\Destin} Check
Moveto [Utiliza_Balcao] TypeIn "S"
Do_It!
ClearAll
Gera_Txt("Destin")

{Ask} {\\Sistemas\\Vendas\\Transpor} Check
Do_It!
ClearAll
Gera_Txt("Transpor")

{Ask} {\\Sistemas\\Vendas\\Estados} Check
Do_It!
ClearAll
Gera_Txt("Estados")
       
{Ask} {\\Sistemas\\Vendas\\P_Pedwin} Check
Do_It!
ClearAll
Gera_Txt("P_Pedwin")

{Ask} {\\Sistemas\\Vendas\\TabFrete} Check
Do_It!
ClearAll
Gera_Txt("TabFrete")

{Ask} {\\Sistemas\\Vendas\\TRDescto} Check
Do_It!
ClearAll
Gera_Txt("TRDescto")

EndProc

;*******************************************************************************
;* Exporta‡Æo . Finan‡as                                                       *
;*******************************************************************************
Proc Financa()

SetDir "\\Sistemas\\CtaRec"

{Ask} {\\Sistemas\\Cadastro\\Destinat}
Moveto [Codigo                 ] Example "clie"
Moveto [Codigo do Representante] TypeIn "~CODREP.."
{Ask} {\\Sistemas\\Ctarec\\Duplic}
Moveto [Numero do Lancamento   ] Example "nlan"
Moveto [Codigo do Destinatario ] Example "clie"
{Ask} {\\Sistemas\\Ctarec\\Dupbaixa} Check
Moveto [Numero do Lancamento   ] Example "nlan"
Do_It!
ClearAll
Gera_Txt("DupBaixa")

{Ask} {\\Sistemas\\Cadastro\\Destinat}
Moveto [Codigo                 ] Example "clie"
Moveto [Codigo do Representante] TypeIn "~CODREP.."
{Ask} {\\Sistemas\\Ctarec\\Duplic} Check
Moveto [Codigo do Destinatario ] Example "clie"
Do_It!
ClearAll
Gera_Txt("Duplic")

SetDir "\\Sistemas\\CheqRec"

{Ask} {\\Sistemas\\Cadastro\\Destinat}
Moveto [Codigo                 ] Example "clie"
Moveto [Codigo do Representante] TypeIn "~CODREP.."
{Ask} {\\Sistemas\\Cheqrec\\Chequec}
Moveto [Numero do Lancamento   ] Example "nlan"
Moveto [Codigo                 ] Example "clie"
{Ask} {\\Sistemas\\Cheqrec\\Chequei} Check
Moveto [Numero do Lancamento   ] Example "nlan"
Do_It!
ClearAll
Gera_Txt("Chequei")

{Ask} {\\Sistemas\\Cadastro\\Destinat}
Moveto [Codigo                 ] Example "clie"
Moveto [Codigo do Representante] TypeIn "~CODREP.."
{Ask} {\\Sistemas\\Cheqrec\\Chequec} Check
Moveto [Codigo                 ] Example "clie"
Do_It!
ClearAll
Gera_Txt("Chequec")

{Ask} {\\Sistemas\\Cheqrec\\Banco} Check
Do_It!
ClearAll
Gera_Txt("Banco")

{Ask} {\\Sistemas\\Cheqrec\\CDPessoa} Check
Do_It!
ClearAll
Gera_Txt("CDPessoa")

SetDir "\\Sistemas\\CtaCor"

{Ask} {\\Sistemas\\CtaCor\\CCusto} Check
Do_It!
ClearAll
Gera_Txt("CCusto")

EndProc

;******************************************************************************
; CONVERTE DATA DE GRAVACAO PARA STRING                                       *
;******************************************************************************
PROC DATA2STR_S(DATA)
DTGRAV=AlinhM(LTrim(Format("D13",DATA)),10)
;;;DTGRAV=SUBSTR(DTGRAV,1,2)+SUBSTR(DTGRAV,4,2)+SUBSTR(DTGRAV,9,2)
RETURN DTGRAV
ENDPROC

;******************************************************************************
;*Procedimento responsavel pela montagem do cgc/cpf
;******************************************************************************
Proc MONTA_STRING(MSTRING,NUMALF)
M_NOVO = ""                                                                                                      
Switch
   Case NUMALF = "N":
        For I From 1 to Len(MSTRING)
            If (Asc(SubStr(MSTRING,I,1)) >= 48) And
               (Asc(SubStr(MSTRING,I,1)) <= 57)
               Then M_NOVO = M_NOVO + SubStr(MSTRING,I,1)                                                         ?
            EndIf                                                                                            
        EndFor
   Case NUMALF = "A":
        For I From 1 to Len(MSTRING)
            If (Asc(SubStr(MSTRING,I,1)) >= 065) And
               (Asc(SubStr(MSTRING,I,1)) <= 122)
               Then M_NOVO = M_NOVO + SubStr(MSTRING,I,1)
            EndIf
        EndFor
   Case NUMALF = "AN":
        For I From 1 to Len(MSTRING)
            If (Asc(SubStr(MSTRING,I,1)) >= 065) And
               (Asc(SubStr(MSTRING,I,1)) <= 122) Or
               (Asc(SubStr(MSTRING,I,1)) >= 48) And
               (Asc(SubStr(MSTRING,I,1)) <= 57)
               Then M_NOVO = M_NOVO + SubStr(MSTRING,I,1)
            EndIf
        EndFor                                  
EndSwitch
Return(M_NOVO)
EndProc ; (Monta_String)

;*******************************************************************************
;* Rotina Converte caracter especial e passa para maiusculo                    *
;*******************************************************************************
Proc Tira_CharEspecial(s)
private s, sn, i, ch

sn=""
For i from 1 to Len(s)
  ch=SubStr(s,i,1)
  Switch
   Case Asc(ch)=8   : ch="" ; ?
   Case Asc(ch)=9   : ch="" ; ?
   Case Asc(ch)=10  : ch="" ; ?
   Case Asc(ch)=11  : ch="" ; ?
   Case Asc(ch)=12  : ch="" ; ?
   Case Asc(ch)=13  : ch="" ; ?
   Case Asc(ch)=14  : ch="" ; ? 
   Case Asc(ch)=59  : ch="," ; ;   
   Case Asc(ch)=233 And ch="é" : ch="E" ; é
   Case Asc(ch)=233 And ch="Ý" : ch="E" ; Ý
   Case Asc(ch)=233 And ch="é" : ch="E" ; é
   Case Asc(ch)=234 And ch="ê" : ch="E" ; ê
   Case Asc(ch)=235 : ch="U" ; Ý
   Case Asc(ch)=237 : ch="I" ; Ý  
   Case Asc(ch)=243 : ch="O" ; ·
   Case Asc(ch)=196 : ch=""  ; -
   OtherWise:
     Switch
       Case Asc(ch)>0 and Asc(ch)<32 : ch="#"
       Case Asc(ch)>125              : ch="#"
     EndSwitch
  EndSwitch
  sn=sn+ch
EndFor
Return Upper(sn)
EndProc

;********************************************************************************
;* CONVERTE VALOR PARA STRING                                                  *
;********************************************************************************
PROC VALOR2STR_S(VALOR,TAM)

VRTIT=STRVAL(VALOR)
PART1=LEN(VRTIT)-3
PART2=LEN(VRTIT)-1
VRTIT=SUBSTR(VRTIT,1,PART1)+SUBSTR(VRTIT,PART2,2)
VRTIT=FILL("0",TAM-LEN(VRTIT))+VRTIT
RETURN VRTIT
ENDPROC

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

VrTit=StrVal(Valor)
VrTit=Fill("0",Tam-Len(VrTit))+VrTit

Return VrTit
EndProc  ; (Valor_Str)

;*******************************************************************************
;* Rotina para gerar registros em texto                                        *
;*******************************************************************************
Proc Gera_Txt(Tabela)
Private Valores, ARQ, Arquivos

If IsTable(VSDGDRIVE+VSDGPASTA+"\\Tppagto")
   Then Delete(VSDGDRIVE+VSDGPASTA+"\\Tppagto")
EndIf

{Tools} {Copy} {Table} {Answer} TypeIn VSDGDRIVE+VSDGPASTA+"\\"+Tabela Enter
ClearAll

NR = NRecords(VSDGDRIVE+VSDGPASTA+"\\"+Tabela)
Arquivos=""

Valores = 11  ; Valor para campo monet rio!

Switch
  Case Substr(Directory(),13,3)="CAD" : DIR="CAD_"
  Case Substr(Directory(),13,3)="COR" : DIR="COR_"
  Case Substr(Directory(),13,3)="REC" : DIR="REC_"
  Case Substr(Directory(),13,3)="EST" : DIR="EST_"
  Case Substr(Directory(),13,3)="CHE" : DIR="CHE_"
  Case Substr(Directory(),13,3)="VEN" : DIR="VEN_"        
EndSwitch

{Tools} {Info} {Structure} TypeIn Tabela Enter ClearAll
ARQ = VSDGDRIVE+VSDGPASTA+"\\"+Tabela+".txt"
Deleta_Arquivo(ARQ)

Arquivos=Arquivos+Completa(ARQ,50," ")+"//"

If Not IsEmpty(VSDGDRIVE+VSDGPASTA+"\\"+Tabela)
   Then Gerou_Arquivos=True
        View "Struct"
        View VSDGDRIVE+VSDGPASTA+"\\"+Tabela
             Scan  
               Barra(NR, RecNo())
               AGUARDE("Aguarde, Transferindo "+Tabela+" "+BARRA_STRING)
             
               Moveto "Struct"
               Scan
                 Camp = [Field Name]
                 Tipo = [Field Type]
                         
                 If Search("Lookup",Camp)>0
                    Then Loop
                 EndIf
             
                 Moveto VSDGDRIVE+VSDGPASTA+"\\"+Tabela
                 Moveto Field Camp
                 Campo = []
                           
                 Switch  
                   Case Search("CGC",Campo)>0 Or Search("CPF",Campo)>0 Or Search("CNPJ",Campo)>0 :
                        ;Print File ARQ Completa(MONTA_STRING(Campo,"N"),18," ") 
                        Print File ARQ MONTA_STRING(Campo,"N")+";"     
                   Case SubStr(Tipo,1,1) = "N" Or SubStr(Tipo,1,1) = "$" :
                        ;Print File ARQ VALOR2STR_S(Campo,Valores)
                        If Campo > 0 And Not IsBlank(Campo)
                           Then Print File ARQ VALOR2STR_S(Campo,Valores)+";"
                           Else Print File ARQ ";" 
                        EndIf   
                   Case SubStr(Tipo,1,1) = "D" :                                                      
                        ;Print File ARQ DATA2STR_S(Campo)  
                        If IsBlank(Campo)
                           Then Print File ARQ Rtrim(Campo)+";"
                           Else Print File ARQ DATA2STR_S(Campo)+";"
                        EndIf
                   Case SubStr(Tipo,1,1) = "S" :
                        ;Print File ARQ VALOR1STR_S(Campo,Numericos)
                        If Campo > 0 And Campo <> ""
                           Then Print File ARQ Rtrim(Campo)+";"
                           Else Print File ARQ ";"
                        EndIf
                   Case SubStr(Tipo,1,1) = "A" Or SubStr(Tipo,1,1) = "M" :
                        ;If Search("*",Tipo)>0
                        ;   Then pos=SearchFrom("*",Tipo,1)
                        ;   Else pos=7
                        ;EndIf
                        ;Print File ARQ Completa(Campo,Substr([Field Type],2,pos-2)," ") 
                        If Len (Campo) > 254
                           Then Campo=SubStr(Campo,1,254)
                        EndIf                     
                        Print File ARQ Tira_CharEspecial(Rtrim(Campo))+";"
                   Otherwise :
                        Print File ARQ ";"
                 EndSwitch  
                 Moveto "Struct"
               Endscan  
               Print File ARQ "\n"
               Moveto VSDGDRIVE+VSDGPASTA+"\\"+Tabela
            EndScan
EndIf
    
DIRETORIO=Directory()
SetDir "\\Note\\Windows\\Exporta\\"
cmd="C:\\Execvise Renomeia "+VSDGDRIVE+VSDGPASTA+"\\"+Tabela+".txt \""+VSDGDRIVE+VSDGPASTA+"\\"+DIR+Upper(Tabela)+".txt"+"\""
Executa_Programa(cmd)
SetDir DIRETORIO

LimpAguarde()

ClearAll

Release Vars Valores, Tabela, ARQ, Arquivos

EndProc  ; Gera_Txt()

;*******************************************************************************
;* 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"] =  27:
          Temp=False
          Switch
            Case Campo="TG_TABELA"        : Campo="NO"
            Case Campo="TG_GERENCIA"      : Campo="TG_TABELA"
            Case Campo="TG_REPRESEN"      : Campo="TG_GERENCIA"
            Case Campo="TG_OPGERENCIA"    : Campo="TG_REPRESEN"
            Case Campo="YES" Or Campo="NO": Campo="TG_TABELA"
          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_TABELA"     : Campo="TG_GERENCIA"
                    Case Campo="TG_GERENCIA"   : Campo="TG_REPRESEN"
                         If IsBlank(VSDGGERENCIA) 
                            Then VSDGGERENCIA = ".."
                         EndIf 
                    Case Campo="TG_REPRESEN"   : Campo="TG_OPGERENCIA"
                    Case Campo="TG_OPGERENCIA" : Campo="YES"
                  EndSwitch
          EndIf
          RefreshDialog
   EndSwitch
EndIf
Switch
  Case Campo="":
     MSG_CAMPO=""
  Case Campo="TG_TABELA":
     MSG_CAMPO="Informe uma tabela"
  Case Campo="TG_REPRESEN":
     MSG_CAMPO="Informe um representante"
  Case Campo="TG_OPGERENCIA":
     MSG_CAMPO="Defina a op‡„o"
     TECLA="Barra de Espa‡o=Marca/Desmarca  "
Endswitch
Monta_Prompt(MSG_CAMPO,TECLA)
Return Temp
EndProc  
;*******************************************************************************
;* ROTINA PRINCIPAL                                                            *
;*******************************************************************************
AutoLib="\\Sistemas\\Cadastro\\Bib_Show,\\Sistemas\\Cadastro\\Bibliot"
LimpAguarde()

VSDGTABELA  =""
VSDGREPRESEN=""
VSDGGERENCIA=""
VSDGDRIVE=SubStr(Directory(),1,2)

Array TG_OPGERENCIA[1]
TG_OPGERENCIA[1]="Agrupa todos vendedores da mesma gerˆncia"

Array CheckTG_OPGERENCIA[ArraySize(TG_OPGERENCIA)]
CheckTG_OPGERENCIA[1]=False

While True

  ShowDialog "Exporta‡„o Server->Pedidos Windows" 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 56

    Label  @ 00,00 "Tabela"  For "TG_TABELA"
    Accept @ 01,01 Width 08 "A5"  Picture "#*#" Tag "TG_TABELA" To VSDGTABELA
    Accept @ 01,10 Width 33 "A30" Picture "!*!" Tag "TG_NOMTAB" To VSDGNOMTAB

    Label  @ 02,00 "Gerˆncia" For "TG_GERENCIA"
    Accept @ 03,01 Width 13 "A10"  Picture "!*!" Tag "TG_GERENCIA" To VSDGGERENCIA

    Label  @ 04,00 "Representante"  For "TG_REPRESEN"
    Accept @ 05,01 Width 08 "A4"  Picture "[&,#][#*#]" Tag "TG_REPRESEN"   To VSDGREPRESEN
    Accept @ 05,10 Width 33 "A20" Picture "!*!" Tag "TG_NOMREPR"    To VSDGNOMREPR

    CheckBoxes @ 07,00 Height 1 Width 50 Tag "TG_OPGERENCIA"
    TG_OPGERENCIA[1] to CheckTG_OPGERENCIA[1]

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

  EndDialog
  If Not RetVal Then QuitLoop EndIf
  
  If Not IsBlank(VSDGTABELA) Then
     TabOk=False
     View "\\Sistemas\\Vendas\\TbPrCab"
     Moveto [Codigo da Tabela]
     Locate VSDGTABELA
     If RetVal
        Then If [\Sistemas\Vendas\TbPrCab->Ativa]<>"N"
                Then TabOk=True
             EndIf
     EndIf
     ClearAll
     If Not TabOk
        Then Mensagem("Tabela informada esta inativa !")
             Loop
     EndIf
  EndIf
  
  If CheckTG_OPGERENCIA[1]=True And Not IsBlank(VSDGREPRESEN)
     Then Mensagem("Agrupar gerˆncia apenas para todos representante !")
          Loop
  EndIf

  If RTrim(VSDGGERENCIA)=""
     Then Mensagem("Gerencia inv lida !")
          Loop
  EndIf
  
  VSDGPASTA="\\Note"
  If DirExists(VSDGPASTA)=0
     Then Cria_Pasta(VSDGPASTA)
          Sleep 2000
  EndIf
  
  VSDGPASTA="\\Note\\Windows"
  If DirExists(VSDGPASTA)=0
     Then Cria_Pasta(VSDGPASTA)
          Sleep 2000
  EndIf

  VSDGPASTA="\\Note\\Windows\\Exporta"
  If DirExists(VSDGPASTA)=0
     Then Cria_Pasta(VSDGPASTA)
          Sleep 2000
  EndIf


  If IsAssigned(Array_Registro)  Then Release Vars Array_Registro EndIf
  DynArray Array_Registro[]


  {Ask} {\\Sistemas\\Cadastro\\Represen} Check
  Do_It!
  ClearAll

  Sort "Answer" On "Descricao Gerencia","Codigo do Representante"
  gerant="***"
  wc=0
  Scan

    Switch

      Case CheckTG_OPGERENCIA[1]=False:
        Switch
          Case Not IsBlank(VSDGREPRESEN) And
               [Codigo do Representante]<>VSDGREPRESEN : Loop
          Case IsBlank(VSDGREPRESEN) And
               IsBlank([Conta Contabil Represent])     : Loop
        EndSwitch

        wc=wc+1
        Array_Registro[wc]=Completa([Nome Fantasia Represent],20," ")+"|"+
                           Completa([Descricao Gerencia     ],10," ")+"|"+
                           [Codigo do Representante]+"|"+
                           [Codigo do Representante]


      Case CheckTG_OPGERENCIA[1]=True :
        Switch
          Case Not IsBlank(VSDGGERENCIA) And
               VSDGGERENCIA<>".." And
               [Descricao Gerencia]<>VSDGGERENCIA      : Loop
          Case VSDGGERENCIA=".." And
               (IsBlank([Conta Contabil Represent]) Or
                IsBlank([Descricao Gerencia      ]))    : Loop
        EndSwitch

        If [Descricao Gerencia]<>gerant
           Then
                If wc>0
                   Then Array_Registro[wc]=Completa(gerant,20," ")+"|"+
                                           Completa(gerant,10," ")+"|"+
                                           Completa(FIDREP,04," ")+"|"+
                                           CODREP
                EndIf
                wc=wc+1

                gerant=[Descricao Gerencia]
                FIDREP=RTrim(SubStr([Descricao Gerencia],1,4))
                CODREP=""

        EndIf

        If IsBlank(CODREP)
           Then CODREP=[Codigo do Representante]
           Else CODREP=CODREP+" Or "+[Codigo do Representante]
        EndIf

    EndSwitch

  EndScan
  ClearAll

  For wc from 1 to DynArraySize(Array_Registro)
    NOMREP=SubStr(Array_Registro[wc],01,20)
    FIDREP=RTrim(SubStr(Array_Registro[wc],33,004))
    CODREP=RTrim(SubStr(Array_Registro[wc],38,500))
  
    Aguarde("Aguarde, Exportando Dados "+VSDGPASTA)
  
    Deleta_Arquivo(VSDGDRIVE+VSDGPASTA+"\\*.DB")
    Deleta_Arquivo(VSDGDRIVE+VSDGPASTA+"\\*.MB")
    Deleta_Arquivo(VSDGDRIVE+VSDGPASTA+"\\*.PX")
    Deleta_Arquivo(VSDGDRIVE+VSDGPASTA+"\\*.TXT")
    Sleep 2000
    
    cmd="C:\\Execvise Deltree "+VSDGDRIVE+VSDGPASTA+"\\"+FIDREP
    Executa_Programa(cmd)

    VSDGPASTA=VSDGPASTA+"\\"+FIDREP
    If DirExists(VSDGPASTA)=0
       Then Cria_Pasta(VSDGPASTA)
            Sleep 2000
    EndIf    
    
    Gerou_Arquivos=False
    
    Clientes()
    Itens()
    TabPreco()
    TabVenda()
    Financa()
    
    SetDir VSDGDRIVE+"\\Note\\Windows\\Exporta"

    Deleta_Arquivo(VSDGDRIVE+VSDGPASTA+"\\*.DB")
    Deleta_Arquivo(VSDGDRIVE+VSDGPASTA+"\\*.MB")
    Deleta_Arquivo(VSDGDRIVE+VSDGPASTA+"\\*.PX")

    Cmd="arj a PCT"+FIDREP+" "+FIDREP
    Executa_Programa(cmd)

    DIA  =AlinhM(StrVal(Day(Today())),2)+StrVal(Month(Today()))+StrVal(Year(Today()))
    HORA = Substr(Time(),1,2)+Substr(Time(),4,2)+Substr(Time(),7,2)

    VSDGPASTA="\\Note\\Windows\\Exporta"
    cmd="C:\\Execvise Renomeia "+VSDGDRIVE+VSDGPASTA+"\\PCT"+FIDREP+".arj \""+VSDGDRIVE+VSDGPASTA+"\\PCTSN"+FIDREP+"."+DIA+"."+HORA+".arj"+"\""
    Executa_Programa(cmd)
    SetDir "\\Sistemas\\Vendas"
                                    
    cmd="C:\\Execvise Deltree "+VSDGDRIVE+VSDGPASTA+"\\"+FIDREP
    Executa_Programa(cmd)
    
  	LimpAguarde()

    If Gerou_Arquivos
       Then Mensagem("Arquivo Gerado!// "+VSDGDRIVE+VSDGPASTA+"\\PCTSN"+FIDREP+"."+DIA+"."+HORA+"// "+
                      Substr(DIA,1,2)+"/"+Substr(DIA,3,2)+"/"+Substr(DIA,5,4)+" …s "+
                      Substr(HORA,1,2)+":"+Substr(HORA,3,2)+":"+Substr(HORA,5,2))
 	  EndIf
  EndFor 
EndWhile
ClearAll
Release Vars All
Release Procs All
