;** ExpMiche *******************************************************************
;*  Exportacao de Clientes para Sigep em Arq.Texto                             *
;*******************************************************************************
;Autor           : Vise Informatica (VISERP)
;Desenvolvido    : 03/08/2007 por Antonio Marcelo Alves
;Ultima Alteracao:   /  /     por
;Motivo Alteracao:

;*******************************************************************************
;* Rotina p/ Selecionar Registros                                              *
;*******************************************************************************
Proc GERA()

ClearAll
{Ask} {\\Sistemas\\Cadastro\\Destinat} Check
Moveto [Codigo                   ] Example "cd"
Moveto [Tipo                     ] TypeIn "C"
Moveto [Data_Cadastro            ] TypeIn ">=~VSDGDTINI, <=~VSDGDTFIM"

Do_It!
ClearAll
Existe=True
If IsEmpty("Answer")
   Then Existe=False
        Return
EndIf

If DirExists("\\Sistemas\\Exporta\\Dados\\Sigep")=0
   Then Cria_Pasta("\\Sistemas\\Exporta\\Dados\\Sigep")
EndIf

ARQ ="\\Sistemas\\Exporta\\Dados\\SIGEP\\SIGEP.TXT"
Deleta_Arquivo(ARQ)

EndProc

;*******************************************************************************
;* Gera Arquivo Texto p/ Registro de Saida                                     *
;*******************************************************************************
Proc GERAARQ()
ClearAll


View "\\Sistemas\\Exporta\\Answer"

NR=NRecords("\\Sistemas\\Exporta\\Answer")
Print File ARQ "1SIGEP DESTINATARIO NACIONAL \n"
Scan
   Barra(NR, RecNo())
   AGUARDE("Aguarde, Exportando Dados SIGEP "+BARRA_STRING)

;   CEP =""
;   CEP=SubStr([CEP],01,5)+SubStr([CEP],06,3)
;   If [Data_Cadastro]<>BlankDate()
;      Then DATACAD=AlinhM(StrVal(Year([Data_Cadastro])),4)+"-"+AlinhM(StrVal(Month([Data_Cadastro])),2)+
;           "-"+AlinhM(StrVal(Day([Data_Cadastro])),2)
;      Else DATACAD="    -  "
;   Endif
   CODATIV=SubStr([Codigo Atividade],3,2)

   posvirgu   =SearchFrom(",",[Endereco],1)
   postraco   =SearchFrom("-",[Endereco],1)
   If postraco <> 0
      Then Numero=Ltrim(Rtrim(SubStr([Endereco],posvirgu+1,postraco-posvirgu-1)))
      Else Numero=Ltrim(Rtrim(SubStr([Endereco],posvirgu+1,Len([Endereco]))))
   Endif
   Complemento=" "
   If postraco<>0
      Then Complemento=Ltrim(Rtrim(Substr([Endereco],postraco+1,Len([Endereco])-postraco)))
   Endif
   Endereco   =Ltrim(Rtrim(Substr([Endereco],1,posvirgu-1)))

   Print File ARQ "2"
   Print File ARQ Completa(Tira_CharNumL([CGC]),14," ")
   Print File ARQ Completa([Nome Fantasia EmitDest],50," ")
   Print File ARQ Completa([Endereco_Eletronico   ],50," ")
   Print File ARQ Completa(" ",50," ") ;aos cuidados
   Print File ARQ Completa([Contato               ],50," ")
   Print File ARQ Completa(Tira_CharNumL([CEP]),08," ")
   Print File ARQ Completa([Endereco]                ,50," ") ;Logradouro
   Print File ARQ Completa(" " ,06," ") ;numero
   Print File ARQ Completa(Complemento,30," ")
   Print File ARQ Completa([Bairro]                ,50," ")
   Print File ARQ Completa([Cidade                ],50," ")
   Print File ARQ Completa(Tira_CharNumL([Telefone]),18," ")
   Print File ARQ Completa(" " ,10," ") ;Celular
   Print File ARQ Completa(Tira_CharNumL([Fax]),10," ")
   Print FIle ARQ "\n"

EndScan
   Print File ARQ "9"
   Print File ARQ Completa(StrVal(NR),6," ")
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


;*******************************************************************************
;* 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_DTINI"         : Campo="NO"
            Case Campo="TG_DTFIM"         : Campo="TG_DTINI"
            Case Campo="YES" Or Campo="NO": Campo="TG_DTFIM"
          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_DTINI"      : Campo="TG_DTFIM"
                    Case Campo="TG_DTFIM"      :
                         If VSDGDTFIM=BlankDate()
                            Then Mensagem("Data Final Invalida !")
                            Else Campo="YES"
                         EndIf
                  EndSwitch
          EndIf
          RefreshDialog
   EndSwitch
EndIf
Switch
  Case Campo="TG_DTFIM":
       MSG_CAMPO="Informe a Data Final"

EndSwitch
Monta_Prompt(MSG_CAMPO,TECLA)
Return Temp
EndProc


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

sn=""
For i from 1 to Len(s)
  ch=SubStr(s,i,1)
    Switch
       Case Asc(ch)=40  : ch="" ; (
       Case Asc(ch)=41  : ch="" ; )
       Case Asc(ch)=42  : ch="" ; *
       Case Asc(ch)=43  : ch="" ; +
       Case Asc(ch)=44  : ch="" ; ,
       Case Asc(ch)=45  : ch="" ; -
       Case Asc(ch)=46  : ch="" ; .
       Case Asc(ch)=47  : ch="" ; /
       Case Asc(ch)=32  : ch="" ; " "
       Case Asc(ch)=88  : ch="" ; X

    EndSwitch
  sn=sn+ch
EndFor
Return Upper(sn)
EndProc



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

LimpAguarde()
VSDGEMPRESA=""
VSDGDTINI  =BlankDate()
VSDGDTFIM  =BlankDate()

ARQ  =""
ARQJ ="JURIDICO.TXT"
ARQF ="FISICO.TXT"

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

While True
  ShowDialog "Exporta‡Ćo de dados p/ SIGEP" Proc "SD_Dialog"
  Trigger "OPEN","ARRIVE"
  Key "F1","ENTER","ESC",
      -104, -105, -106, -107, -108, -109, -110, -111, -112, -113, -139, -140
   @ 03,18   Height 09 Width 41
    Label  @ 01,00 "Perˇodo de Cadastramento dos Clientes" For "TG_DTINI"
    Label  @ 02,00 "de" For "TG_DTINI"
    Accept @ 02,04 Width 13 "D"  Picture "#[#];.##;.##[##]" Tag "TG_DTINI" To VSDGDTINI
    Label  @ 02,17 "at‚" For "TG_DTFIM"
    Accept @ 02,22 Width 13 "D"  Picture "#[#];.##;.##[##]" Tag "TG_DTFIM" To VSDGDTFIM


    Pushbutton @ 04,05 width 11 "~O~k"      OK     Default Value "Ok" Tag "YES" To BottonValue
    Pushbutton @ 04,18 width 11 "~C~ancela" CANCEL                    Tag "NO"  To BottonValue

  EndDialog
  If Not RetVal Then QuitLoop EndIf
  If VSDGDTFIM=BlankDate(); Or VSDGDTINI=BlankDate()
     Then Mensagem("Data Final Invalida !")
  Loop
  EndIf
  
AGUARDE("Aguarde, Exportando Clientes ")

GERA()
If Not Existe
   Then MENSAGEM("N„o Existem Registros a Processar !")
        Return
EndIf
GERAARQ()

Delete "\\Sistemas\\Exporta\\Answer"
LimpAguarde()
Mensagem("// Gerado o arquivo: //["+SubStr(Directory(),1,2)+ARQ+"] ! //")
EndWhile
ClearAll
LimpAguarde()
Release Procs Gera, GeraArq

