;** ExpMiche *******************************************************************
;*  Exportacao de Clientes para Michelle Cosmeticos em Arq.Texto               *
;*******************************************************************************
;Autor           : Vise Informatica (VISERP)
;Desenvolvido    : 24/02/2005 por Jose Luis Ameno da Silva
;Ultima Alteracao: 14/08/2007 por Anivel Augusto Rodrigues
;Motivo Alteracao: Ajustes Deleta_Arquivo
;Ultima Alteracao: 01/10/2016 por Anivel Augusto Rodrigues
;Motivo Alteracao: Ajustes showdialog.
;Ultima Alteracao: 24/03/2021 por Bruno Carvalho
;Motivo Alteracao: Alterado mensagem de confirma‡Æo ao gerar o arquivo p/ MensagemMP()                   

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

ClearAll
{Ask} {\\Sistemas\\Cadastro\\Destinat} Check
Moveto [Codigo                   ] Example "cd"
Moveto [Tipo                     ] TypeIn "C"
Moveto [Codigo Atividade         ] TypeIn "not 0100"

{Ask} {\\Sistemas\\Cadastro\\Fichacli} Enter
Moveto [Codigo                   ] Example "cd"
Moveto [Valor da Primeira Compra ] Check
Do_It!
ClearAll
Existe=True
If IsEmpty("Answer")
   Then Existe=False
        Return
EndIf

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

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

EndProc

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

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

NR=NRecords("\\Sistemas\\Exporta\\Answer")

Scan
   Barra(NR, RecNo())
   AGUARDE("Aguarde, Exportando Dados Saidas "+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 Completa([Razao Social          ],50," ")+";"
   Print File ARQ Completa([Nome Fantasia EmitDest],50," ")+";"
   Print File ARQ Completa([Contato               ],20," ")+";"
   Print File ARQ Completa([CGC                   ],18," ")+";"
   Print File ARQ Completa([IE                    ],20," ")+";"
   Print File ARQ Completa(Endereco                ,50," ")+";"
   Print File ARQ Completa(Numero                  ,09," ")+";"
   Print File ARQ Completa(Complemento             ,30," ")+";"
   Print File ARQ Completa([Bairro]                ,25," ")+";"
   Print File ARQ Completa([Cidade                ],35," ")+";"
   Print File ARQ Completa([UF                    ],02," ")+";"
   Print File ARQ Completa(CEP,08," ")+";"
   Print File ARQ Fill(" ",3)+";"
   Print File ARQ Completa([Telefone              ],13," ")+";"
   Print File ARQ Fill(" ",3)+";"
   Print File ARQ Completa([Fax                   ],13," ")+";"
   Print File ARQ Fill(" ",3)+";"
   Print File ARQ Fill(" ",13)+";"
   Print File ARQ Completa([Endereco_Eletronico   ],80," ")+";"
   Print File ARQ Completa(DATACAD,10," ")+";"
   Print File ARQ VALOR2STR_S([Valor da Primeira Compra    ],20)+";"
   Print File ARQ Completa(CODATIV,2," ")

   Print FIle ARQ "\n"

EndScan

FileWrite Append ARQ From "</correioslog>"+"\n"

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
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="YES": Campo="NO"
            Case Campo="NO" : Campo="YES"
          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:
          Checa_Tag()
          Temp=False
          RefreshDialog
   EndSwitch
EndIf
TECLA=""
MSG_CAMPO=""
Monta_Prompt(MSG_CAMPO,TECLA)
Return Temp
EndProc

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

While True
 ShowDialog "Gera Clientes TXT - Michelle Cosm‚ticos" Proc "SD_Dialog"
   Trigger "OPEN","ARRIVE"
    Key "TAB","ReverseTab","ENTER","ESC",
      -104, -105, -106, -107, -108, -109, -110, -111, -112, -113, -139, -140    
     @ 05,12 Height 05 Width 72

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

  EndDialog
  If Not RetVal Then QuitLoop EndIf

  AGUARDE("Aguarde, Exportando Clientes ")
  
  GERA()
  If Not Existe
     Then MENSAGEM("N„o Existem Registros a Processar !")
          Loop  
  EndIf
  GERAARQ()

  LimpAguarde()
  MensagemMP("// Gerado o arquivo: //["+SubStr(Directory(),1,2)+ARQ+"] ! //",SubStr(Directory(),1,2)+ARQ) 
  
EndWhile
ClearAll
Release Vars All
Release Procs All
