;** DestMail **********************************************************************
;*  Rotina p/ Alterar/Incluir/Remover emails                                   *
;*******************************************************************************
;Autor           : Vise Informatica (VISERP)
;Desenvolvido    : 17/08/2017 por Bruno Carvalho          
;Ultima Alteracao: 24/10/2017 por Bruno Carvalho 
;Motivo Alteracao: Ajustes ao gravar o EMAIL, ajustando para letras min£sculas
;Ultima Alteracao: 06/04/2018 por Anivel Augusto Rodrigues
;Motivo Alteracao: Ajustes Echo off
;Ultima Alteracao: 07/06/2018 por Bruno Carvalho
;Motivo Alteracao: Adicionado/ajustado Grava_LogEx() p/ gravar log de erros ao excluir registros         
;Ultima Alteracao: 13/08/2018 por Anivel Augusto Rodrigues
;Motivo Alteracao: Ajustes email nfe informar usar separdor virgula
;Ultima Alteracao: 16/08/2018 por Bruno Carvalho
;Motivo Alteracao: Permitido a inclusÆo de mais de um e-mail, separado por ";", quando tipo for NF-e

;*******************************************************************************
;* Rotina Tipo de Email                                                        *
;*******************************************************************************
Proc Tipo_Email(Tipo)
Private Tipo, Result

Result=True
Switch
  Case Tipo="F1":
       ;** M ximo, 14 POSICOES
       ShowPopup " Tipos " CENTERED
        "~C~ DEP.COMPRAS    " : " DEPARTAMENTO DE COMPRAS " : "C",
        "~V~ DEP.VENDAS     " : " DEPARTAMENTO DE VENDAS  " : "V",
        "~F~ DEP.FINANCEIRO " : " DEPARTAMENTO FINANCEIRO " : "F",
        "~N~ NF-e           " : " NOTA FISCAL ELETRONICA  " : "N",
        "~P~ PRINCIPAL      " : " EMAIL PRINCIPAL         " : "P",
        Separator,
        "~C~ancelar         " : " Cancelar a Operao     "  : "CANCELAR"
       EndMenu
       To MenuVar
       Echo Off
       
       If MenuVar<>"CANCELAR" And MenuVar<>"Esc"
          Then VSDGTIPOEMAIL=MenuVar
               Switch
                 Case VSDGTIPOEMAIL = "C" : VSDGNOMTIPOEMAIL = "DEP.COMPRAS"
                 Case VSDGTIPOEMAIL = "V" : VSDGNOMTIPOEMAIL = "DEP.VENDAS"
                 Case VSDGTIPOEMAIL = "F" : VSDGNOMTIPOEMAIL = "DEP.FINANCEIRO"
                 Case VSDGTIPOEMAIL = "N" : VSDGNOMTIPOEMAIL = "NF-e"
                 Case VSDGTIPOEMAIL = "P" : VSDGNOMTIPOEMAIL = "PRINCIPAL"
               EndSwitch
          Else VSDGNOMTIPOEMAIL = ""
               Return False
       EndIf
  Case Tipo="Seta_Valor" :
       Switch
         Case VSDGTIPOEMAIL = "C" : VSDGNOMTIPOEMAIL = "DEP.COMPRAS"
         Case VSDGTIPOEMAIL = "V" : VSDGNOMTIPOEMAIL = "DEP.VENDAS"
         Case VSDGTIPOEMAIL = "F" : VSDGNOMTIPOEMAIL = "DEP.FINANCEIRO"
         Case VSDGTIPOEMAIL = "N" : VSDGNOMTIPOEMAIL = "NF-e"
         Case VSDGTIPOEMAIL = "P" : VSDGNOMTIPOEMAIL = "PRINCIPAL"
         OtherWise                : VSDGNOMTIPOEMAIL = ""
                                    Result=False
       EndSwitch
  Case Tipo="TipoEmail":   
       Switch
         Case VSDGTIPOEMAIL=""  Or
              VSDGTIPOEMAIL="C" Or
              VSDGTIPOEMAIL="V" Or
              VSDGTIPOEMAIL="F" Or
              VSDGTIPOEMAIL="N" Or
              VSDGTIPOEMAIL="P"  : Result=True
                                   Switch
                                     Case VSDGTIPOEMAIL = "C" : VSDGNOMTIPOEMAIL = "DEP.COMPRAS"
                                     Case VSDGTIPOEMAIL = "V" : VSDGNOMTIPOEMAIL = "DEP.VENDAS"
                                     Case VSDGTIPOEMAIL = "F" : VSDGNOMTIPOEMAIL = "DEP.FINANCEIRO"
                                     Case VSDGTIPOEMAIL = "N" : VSDGNOMTIPOEMAIL = "NF-e"
                                     Case VSDGTIPOEMAIL = "P" : VSDGNOMTIPOEMAIL = "PRINCIPAL"
                                     OtherWise                : VSDGNOMTIPOEMAIL = ""
                                   EndSwitch
         OtherWise               : Mensagem("Tipo de email inv lido !")
                                   Result=False
       EndSwitch     
EndSwitch

Return Result   
       
EndProc ;Tipo_Email()       

;*******************************************************************************
;* Rotina                                                                      *
;*******************************************************************************
Proc EMAIL_ADD_SDDialog(Disparo,Campo,Evento,Elemento)
Private Disparo,Campo,Evento,Elemento
            
Temp=True
MSG_CAMPO=""                      
TECLA=""

If INICIO_DIALOG2
   Then Campo = "TG_IDENTIFICACAO"
        INICIO_DIALOG2=False
EndIf     

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: If Campo="TG_TIPOEMAIL"
                                      Then Tipo_Email("F1")
                                   EndIf     
     Case Evento["KeyCode"] =  27:
          Temp=False
          Switch
            Case Campo="TG_TIPOEMAIL"     : CancelDialog
            Case Campo="TG_IDENTIFICACAO" : CancelDialog
            Case Campo="TG_EMAIL"         : Campo="TG_IDENTIFICACAO"
            Case Campo="TG_FLAG2"         : Campo="TG_EMAIL"
          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
          Switch
             Case Campo="TG_TIPOEMAIL" :
                  VSDGTIPOEMAIL=ControlValue("TG_TIPOEMAIL")
                  If IsBlank(VSDGTIPOEMAIL)
                     Then Campo="TG_TIPOEMAIL"
                     Else GetRecord "\\Sistemas\\Cadastro\\DestMail" VSDGPESSOA,VSDGTIPOEMAIL To REG_PESSOA
                          If RetVal
                             Then Tipo_Email("Seta_Valor")
                                  Mensagem("Tipo de Email "+VSDGNOMTIPOEMAIL+" j  foi incluido para esta pessoa!")
                                  Campo="TG_TIPOEMAIL"
                             Else If Tipo_Email("TipoEmail")
                                     Then Campo="TG_EMAIL"   
                                  EndIf
                          EndIF                                                               
                  EndIf   
             Case Campo="TG_IDENTIFICACAO" : Campo="TG_EMAIL" 
                  VSDGIDENTIFICACAO=ControlValue("TG_IDENTIFICACAO")     
             Case Campo="TG_EMAIL" :  Campo="TG_FLAG2"
                  VSDGEMAIL=ControlValue("TG_EMAIL")
                  Switch
                    Case Search("@",VSDGEMAIL)=0 And VSDGEMAIL<>""        : Campo="TG_EMAIL" 
                                                                            FastMsg("E-mail inv lido!")
                    Case VSDGTIPOEMAIL <> "N" And Search(";",VSDGEMAIL)>0 : Campo="TG_EMAIL"
                         Mensagem(" permitido a inserÆo de mais de um e-mail apenas no tipo NF-e!")
                    Case Search(",",VSDGEMAIL)>0                          : Campo="TG_EMAIL"
                                                                            FastMsg("A inclusÆo de mais de um email  feito por \";\" !")
                  EndSwitch 
                  ;Switch
                  ;  Case IsBlank(VSDGTIPOEMAIL) ANd Not IsBlank(VSDGEMAIL) : 
                  ;       FastMsg("Informe o Tipo de Email!")
                  ;  Case Not IsBlank(VSDGTIPOEMAIL) And IsBlank(VSDGEMAIL) : 
                  ;       FastMsg("Informe o Email!") 
                  ;  Case IsBlank(VSDGTIPOEMAIL) And IsBlank(VSDGEMAIL)   :                                                       
                  ;EndSwitch
                  If Campo="TG_FLAG2" Then AcceptDialog EndIf
             Case Campo="TG_FLAG2" :
                  VSDGFLAG2=ControlValue("TG_FLAG2")
                  AcceptDialog     
          EndSwitch
          RefreshDialog
   EndSwitch
EndIf

Switch
  Case Campo="TG_TIPOEMAIL" : MSG_CAMPO = "C-DEP.COMPRAS  V-DEP.VENDAS  F-DEP.FINANCEIRO  N-NFe  P-PRINCIPAL"
  Case Campo="TG_EMAIL"     : 
    switch
      case VSDGTIPOEMAIL="N" : MSG_CAMPO = "Email, para mais de um email use \";\" como separador "
      otherwise              : MSG_CAMPO = "Email"
    endswitch
  Case Campo="TG_FLAG2"     : MSG_CAMPO = "Pausa para Prosseguir"                                                
Endswitch

Monta_Prompt(MSG_CAMPO,TECLA)
Return Temp

EndProc ;EMAIL_ADD_SDDialog()

;*******************************************************************************
;* Rotina Principal De Reajuste de Preco                                       *
;*******************************************************************************
Proc Adiciona_Email()                
Private POSI, Titulo, indice

INICIO_DIALOG2    = True
Indice            = ControlValue("TG_EMAIL")
VSDGFLAG2         = ""     

While True
  Result = False
  ShowDialog "Cadastro de Email" Proc "EMAIL_ADD_SDDialog"
    Trigger "OPEN","ARRIVE"                                                   
    Key "F1","ENTER","ESC",
      -104, -105, -106, -107, -108, -109, -110, -111, -112, -113, -139, -140  
      @ 00,01 Height 07 Width 78
  
     Label  @ 00,00 "Tipo"  For "TG_TIPOEMAIL"
     Accept @ 01,01 Width 4 "A1" Picture "C,V,F,N,P" Tag "TG_TIPOEMAIL" To VSDGTIPOEMAIL
     Accept @ 01,03 Width 17 "A14"  Picture "!*!" Tag "TG_NOMTIPOEMAIL" To VSDGNOMTIPOEMAIL
     
     Label  @ 00,21 "Identificao"  For "TG_IDENTIFICACAO"
     Accept @ 01,22 Width 38 "A20" Tag "TG_IDENTIFICACAO" To VSDGIDENTIFICACAO
        
     Label  @ 02,00 "Email"  For "TG_EMAIL"
     Accept @ 03,01 Width 72 "A100" Tag "TG_EMAIL" To VSDGEMAIL
          
     Label  @ 02,72 ""  For "TG_FLAG2"
     Accept @ 03,72 Width 1 "A3" Tag "TG_FLAG2" To VSDGFLAG2
     
  EndDialog
  If Not RetVal Then QuitLoop EndIf
  Result = True
  QuitLoop
EndWhile          

Release Vars REG_BARRA  
Return Result

EndProc ;Adiciona_Email()

;*******************************************************************************
;* Controle do ShowDialog                                                      *
;*******************************************************************************
Proc EMAIL_SDDialog(Disparo,Campo,Evento,Elemento)         
Private Disparo,Campo,Evento,Elemento

Temp             = True
MSG_CAMPO        = ""                                               
TECLA            = ""
ANT_VSDGPESSOA  = VSDGPESSOA
ANT_VSDGEMAIL   = VSDGEMAIL
                                     
If INICIO_DIALOG
   Then Campo = "TG_EMAIL"
        INICIO_DIALOG=False
EndIf     

If Disparo="OPEN" Then AoCriarShowDialog() EndIf
If Disparo="EVENT" And Evento["Type"]="KEY" Then
   Switch
     Case Evento["KeyCode"] =   9 : Temp=False
     Case Evento["KeyCode"] =  27 :
          Temp=False
          Switch
            Case Campo="TG_PESSOA"     : CancelDialog
            Case Campo="TG_FLAG"       : CancelDialog
            Case Campo="TG_EMAIL"      : CancelDialog
          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"] = -83 And Campo="TG_EMAIL" :
          ;ExclusÆo de Registro
          If EMAIL_PARAM_ROTINA="XXXXX" And DynArraySize(ARRAY_EMAIL)>0
             Then Indice    = ControlValue("TG_EMAIL")
                  TIPOEMAIL = Rtrim(SubStr(ARRAY_EMAIL[Indice],1,14))
                  Switch
                    Case TIPOEMAIL = "DEP.COMPRAS"     : VSDGTIPOEMAIL = "C"  
                    Case TIPOEMAIL = "DEP.VENDAS"      : VSDGTIPOEMAIL = "V"  
                    Case TIPOEMAIL = "DEP.FINANCEIRO"  : VSDGTIPOEMAIL = "F"  
                    Case TIPOEMAIL = "NF-e"            : VSDGTIPOEMAIL = "N"  
                    Case TIPOEMAIL = "PRINCIPAL"       : VSDGTIPOEMAIL = "P"  
                  EndSwitch
                  Release Vars TIPOEMAIL
                  
                  EMAIL     = Rtrim(SubStr(Lower(ARRAY_EMAIL[Indice]),16,35))
                  If Search("DESTE CADASTRO",ARRAY_EMAIL[Indice])>0
                     Then  Mensagem(Completa("No   poss¡vel  excluir  um  email que esta sendo utilizado.",62," ")+"////"+
                                    Completa("Se  necess rio, remover  antes  o  email inserido no cadastro",62," ")+"//"+
                                    Completa("desta pessoa para posteriormente exclu¡-lo.                  ",62," "))
                     Else ConfGeral(Completa("PESSOA: "+VSDGPESSOA+" "+VSDGNOMDEST,62," ")+"//"+
                                    Completa("Deseja EXCLUIR o email "+EMAIL+"?",62," "))
                            If Ok="S"  
                               Then {TableAdd} {\\Sistemas\\Cadastro\\DestMail}
                                    Moveto [Email]
                                    Locate EMAIL
                                    If RetVal
                                       Then While True
                                              If [Codigo] = VSDGPESSOA And [EmailTipo ] = VSDGTIPOEMAIL 
                                                 Then Grava_LogEx("Email")
                                                      Del
                                                      QuitLoop
                                              EndIf   
                                              Skip
                                              If Eot() Then QuitLoop EndIf
                                              Locate Next EMAIL
                                              If Not RetVal
                                                 Then QuitLoop
                                              EndIf   
                                            EndWhile
                                    EndIf
                                    Do_It!
                                    ClearImage
                                    
                                    Moveto TAB_ANT
                                    CoeditKey
                                    KeyPress "F7"
                                    
                                    If DynArraySize(ARRAY_EMAIL)=1
                                       Then Release Vars ARRAY_EMAIL
                                       Else If DynArraySize(ARRAY_EMAIL)>0
                                               Then DynArray AUX_ARRAY_EMAIL[]
                                                    CONTADOR = 0
                                                    For i From 1 To DynArraySize(ARRAY_EMAIL)
                                                       If ARRAY_EMAIL[i] <> ARRAY_EMAIL[Indice]
                                                          Then CONTADOR = CONTADOR + 1
                                                               AUX_ARRAY_EMAIL[CONTADOR] = ARRAY_EMAIL[i]
                                                       EndIf        
                                                    Endfor
                                                    Release Vars ARRAY_EMAIL
                                                    DynArray ARRAY_EMAIL[] 
                                                    For i From 1 To DynArraySize(AUX_ARRAY_EMAIL)
                                                      ARRAY_EMAIL[i] = AUX_ARRAY_EMAIL[i]
                                                    EndFor
                                            EndIf        
                                    EndIf   
                            EndIf
                            Release Vars AUX_ARRAY_EMAIL,CONTADOR,TM,i,Indice,ITEM              
                    EnDIf
          EndIf
     Case Evento["KeyCode"] = 13:
          Temp=False
          If Checa_TAG()
             Then Switch
                    Case Campo="TG_PESSOA"       : Campo="TG_EMAIL"
                         VSDGPESSOA=ControlValue("TG_PESSOA")
                         If Not IsBlank(ANT_VSDGPESSOA) And ANT_VSDGPESSOA<>VSDGPESSOA
                            Then VSDGPESSOA = ANT_VSDGPESSOA
                                 GetRecord "\\Sistemas\\Cadastro\\Destinat" VSDGPESSOA To REG_CODIGO
                                 If Not RetVal
                                    Then VSDGNOMDEST=""
                                         FastMsg("Pessoa "+VSDGPESSOA+" no localizado!")
                                    Else VSDGNOMDEST = REG_CODIGO["Nome Fantasia EmitDest"]
                                 EndIf   
                         EndIf
                    Case Campo="TG_FLAG"   : Campo="TG_EMAIL"    
                    Case Campo="TG_EMAIL"  : If EMAIL_PARAM_ROTINA="DESTINAT"
                                                Then Indice = ControlValue("TG_EMAIL")
                                                     VSDGNOMTIPOEMAIL  = Rtrim(SubStr(ARRAY_EMAIL[Indice],1,14)) 
                                                     VSDGEMAIL         = ""
                                                     VSDGIDENTIFICACAO = ""
                                                     Switch
                                                       Case VSDGNOMTIPOEMAIL = "DEP.COMPRAS"     : VSDGTIPOEMAIL = "C"  
                                                       Case VSDGNOMTIPOEMAIL = "DEP.VENDAS"      : VSDGTIPOEMAIL = "V"  
                                                       Case VSDGNOMTIPOEMAIL = "DEP.FINANCEIRO"  : VSDGTIPOEMAIL = "F"  
                                                       Case VSDGNOMTIPOEMAIL = "NF-e"            : VSDGTIPOEMAIL = "N"  
                                                       Case VSDGNOMTIPOEMAIL = "PRINCIPAL"       : VSDGTIPOEMAIL = "P"  
                                                     EndSwitch
                                                     GetRecord "\\Sistemas\\Cadastro\\DestMail" CODIGO,VSDGTIPOEMAIL To REG_EMAIL
                                                     If RetVal
                                                        Then VSDGEMAIL         = REG_EMAIL["Email"]
                                                             VSDGIDENTIFICACAO = REG_EMAIL["EmailIdentificacao"]
                                                     EndIf
                                                     If Adiciona_Email()
                                                        Then UnLockRecord
                                                             FormKey
                                                             Moveto "DestMail"
                                                             
                                                             ;If VSDGEMAIL <> ANT_VSDGEMAIL And VSDGEMAIL="" And ANT_VSDGEMAIL<>""
                                                             ;   Then ConfGeral("Deseja remover o email "+VSDGNOMTIPOEMAIL+"?")
                                                             ;        If Ok<>"S"
                                                             ;           Then VSDGEMAIL = ANT_VSDGEMAIL
                                                             ;        EndIf
                                                             ;EndIf
                                                             
                                                             Moveto [Codigo]
                                                             Locate VSDGPESSOA,VSDGTIPOEMAIL
                                                             If RetVal
                                                                Then Del
                                                             EndIf
                                                             If VSDGEMAIL<>""
                                                                Then End Down
                                                                     [Codigo            ] = VSDGPESSOA
                                                                     [EmailTipo         ] = VSDGTIPOEMAIL
                                                                     [Email             ] = Lower(VSDGEMAIL)
                                                                     [EmailIdentificacao] = VSDGIDENTIFICACAO
                                                             EndIf                 
                                                             UnLockRecord
                                                             SaveTables
                                                                                                           
                                                             Moveto "Destinat"
                                                             KeyPress "F7"
                                                             
                                                             Moveto "Destinat"
                                                             Moveto [Codigo]
                                                             Locate VSDGPESSOA
                                                             IsLockRecord()
                                                             
                                                             If VSDGTIPOEMAIL = "P" 
                                                                Then [Endereco_Eletronico] = Lower(VSDGEMAIL)
                                                             EnDIf   
                                                              
                                                             Moveto Field FIE_ANT 
                                                             ARRAY_EMAIL[Indice] = Format("W14",VSDGNOMTIPOEMAIL)+Spaces(1)+
                                                                                   Format("W35",Lower(VSDGEMAIL))+Spaces(1)+
                                                                                   Format("W20",VSDGIDENTIFICACAO)
                                                     EndIf
                                             EndIf                   
                  EndSwitch
          EndIf
          RefreshDialog
   EndSwitch
EndIf
        
MSG_CAMPO = "ESC=Retornar  "

Monta_Prompt(MSG_CAMPO,TECLA)
Return Temp

EndProc ;EMAIL_SDDialog()

;*******************************************************************************
;* EdiÆo do recebimento da Ordem de Compra                                    *
;*******************************************************************************
Proc EMAIL()

While True
  Result = False
  VSDGFLAG = ""
  Echo Normal
  Echo Off
  ShowDialog "Email" Proc "EMAIL_SDDialog"
  Trigger "OPEN","ARRIVE","CLOSE","SELECT"
  Key "F1","ENTER","ESC","F9",-67,-83,
      -104, -105, -106, -107, -108, -109, -110, -111, -112, -113, -139, -140  
       @ 06,10   Height 12 Width 79
  
      ;Label  @ 00,00 "Codigo" For "TG_PESSOA"
      ;Accept @ 01,01 Width 14 "A10"  Picture "[&,#][#*#]" Tag "TG_PESSOA" To VSDGPESSOA
      ;Accept @ 01,12 Width 45 "A30"  Picture "!*!" Tag "TG_PESSOA" To VSDGNOMDEST
      
      ;Label  @ 00,56 "" For "TG_FLAG"
      ;Accept @ 01,57 Width 03 "A1" Picture "!*!" Tag "TG_FLAG" To VSDGFLAG

      Label @ 00,00 ""  For "TG_EMAIL"                                    
      Label @ 01,00 " Tipo           Email                               Identificao  "  For "TG_EMAIL"
                   ;" 12345678901234 12345678901234567890123456789012345 123456789012345"
  
      PickDynArray @02,01 Height 7 width 75
        Columns 1
         ARRAY_EMAIL
          Tag "TG_EMAIL"
           To ARRAYELEMENT
           
      ;Label @ 10,00 "F9=Inserir"  For "TG_EMAIL"     
                
  EndDialog
  If Not RetVal 
     Then Result=False
     Else Result = True     
  EndIf
  QuitLoop
EndWhile

Release Vars ARRAYELEMENT,ARRAY_EMAIL,VSDGFLAG,VSDGFLAG2,indice
             
Return Result             
  
EndProc ;EMAIL()

;*******************************************************************************
;* Rotina Principal                                                            *
;*******************************************************************************
INICIO_DIALOG=True

If SysMode()="Main"
   Then AutoLib="\\Sistemas\\Cadastro\\Bibliot,\\Sistemas\\Cadastro\\Bib_Show"
        Mensagem("Processo executado no script DestCli!")
        Release Vars All
        Release Procs All
        Return
EndIf

If Not IsAssigned(EMAIL_PARAM_ROTINA)     Then EMAIL_PARAM_ROTINA=""        EndIf
If Not IsAssigned(EMAIL_PARAM_AUTOMATICO) Then EMAIL_PARAM_AUTOMATICO=False EndIf

TAB_ANT = Table()
FIE_ANT = Field()

Release Vars JanelaEmail,ARRAY_EMAIL,CONT_ARRAY,ty

CONT_ARRAY=0
DynArray ARRAY_EMAIL[]

VSDGPESSOA = [Codigo]

GetRecord "\\Sistemas\\Cadastro\\Destinat" VSDGPESSOA To REG_DESTINAT
If Not RetVal
   Then Release Vars EMAIL_PARAM_ROTINA,EMAIL_PARAM_AUTOMATICO,TAB_ANT,FIE_ANT,ARRAY_EMAIL,CONT_ARRAY
        Mensagem("Pessoa "+VSDGPESSOA+" no localizada no cadastro!")
        Return
   Else VSDGNOMDEST   = REG_DESTINAT["Nome Fantasia EmitDest"]
        VSDGEMAIL     = Lower(REG_DESTINAT["Endereco_Eletronico"])
        ANT_VSDGEMAIL = VSDGEMAIL
        Release Vars REG_DESTINAT 
EndIf

;---- Se email Principal j  adicionado, o processo  retornado -----------------------------------------
If EMAIL_PARAM_AUTOMATICO
   Then GetRecord "\\Sistemas\\Cadastro\\DestMail" VSDGPESSOA,"P" To REG_DESTMAIL
        If RetVal
           Then If Lower(REG_DESTMAIL["Email"])=Lower([Endereco_Eletronico])
                   Then Release Vars VSDGPESSOA,VSDGNOMDEST,VSDGEMAIL,EMAIL_PARAM_ROTINA,EMAIL_PARAM_AUTOMATICO,
                                     TAB_ANT,FIE_ANT,ARRAY_EMAIL,CONT_ARRAY
                        Return
                   Else COR_MENSAGEM="ALERTA"
                        MSG = Completa("H  um email PRINCIPAL cadastrado para esta pessoa.",62," ")+"////"+
                              Completa("Email cadastrado   : "+Lower(REG_DESTMAIL["Email"]),62," ")+"//"+
                              Completa("Email ser  alterado: "+Lower([Endereco_Eletronico]),62," ")+"//"+
                              Completa("Deseja realizar a alterao do email PRINCIPAL?",62," ")
                        ConfGeral(MSG)
                        If Ok<>"S" 
                           Then [Endereco_Eletronico] = Lower(REG_DESTMAIL["Email"])
                                Release Vars COR_MENSAGEM,VSDGPESSOA,VSDGNOMDEST,VSDGEMAIL,EMAIL_PARAM_ROTINA,EMAIL_PARAM_AUTOMATICO,
                                             TAB_ANT,FIE_ANT,ARRAY_EMAIL,CONT_ARRAY
                                Return
                        EndIf
                 EndIf       
        EndIf     
EndIf
VSDGNOMTIPOEMAIL     = ""                                                   

UnLockRecord
JanelaEmail = AbrirTabela("\\Sistemas\\Cadastro\\DestMail","AddRO",1,0,1,15)

If EMAIL_PARAM_AUTOMATICO
   Then UnLockRecord
        Moveto "DestMail"
        Moveto [Codigo]
        Locate VSDGPESSOA,"P"
        If RetVal
           Then Del
        EndIf                         
        End Down
        [Codigo            ] = VSDGPESSOA
        [EmailTipo         ] = "P"
        [Email             ] = Lower([Destinat->Endereco_Eletronico])
        UnLockRecord
        SaveTables
       
        Moveto "Destinat"
        KeyPress "F7"
       
        Moveto "Destinat"
        Moveto [Codigo]
        Locate CODIGO              ;Vari vel Constrole da Destinat
        IsLockRecord()
        
        Moveto Field FIE_ANT
        Release Vars Achou,JanelaEmail,EMAIL_PARAM_ROTINA,EMAIL_PARAM_AUTOMATICO,TAB_ANT,FIE_ANT,
                     ARRAY_EMAIL, CONT_ARRAY,VSDGNOMTIPOEMAIL,
                     VSDGNOMDEST,VSDGEMAIL,VSDGPESSOA,VSDGTIPOEMAIL,VSDGNOMTIPOEMAIL
        Return
EndIf

Moveto TAB_ANT
IsLockRecord()

Window Select JanelaEmail
Moveto [Codigo]
Locate VSDGPESSOA
If Retval
   Then While [Codigo] = VSDGPESSOA
          VSDGTIPOEMAIL = [EmailTipo]
          Tipo_Email("Seta_Valor")
          CONT_ARRAY    = CONT_ARRAY + 1
          ARRAY_EMAIL[CONT_ARRAY] = Format("W14",VSDGNOMTIPOEMAIL) + Spaces(1) + 
                                    Format("W35",Lower([Email])) + Spaces(1) + 
                                    Format("W20",[EmailIdentificacao])         
          Skip
          If Eot() Then QuitLoop EndIf
        EndWhile 
EndIf   
For ty From 1 To 5
  Switch
    Case ty = 1 : VSDGNOMTIPOEMAIL = "DEP.COMPRAS"
    Case ty = 2 : VSDGNOMTIPOEMAIL = "DEP.VENDAS"
    Case ty = 3 : VSDGNOMTIPOEMAIL = "DEP.FINANCEIRO"
    Case ty = 4 : VSDGNOMTIPOEMAIL = "NF-e"
    Case ty = 5 : VSDGNOMTIPOEMAIL = "PRINCIPAL"
  EndSwitch
  Achou=False
  For ty2 From 1 To DynArraySize(ARRAY_EMAIL)
    If Rtrim(SubStr(ARRAY_EMAIL[ty2],1,14))=VSDGNOMTIPOEMAIL
       Then Achou=True
            QuitLoop
    EndIf   
  EndFor
  If Not Achou
     Then ARRAY_EMAIL[DynArraySize(ARRAY_EMAIL)+1] = VSDGNOMTIPOEMAIL  
  EndIf    
Endfor
           
UnLockRecord
Do_It!
ClearImage
CoeditKey

Moveto TAB_ANT
KeyPress "F7"

EMAIL()  
If EMAIL_PARAM_AUTOMATICO
   Then GetRecord "\\Sistemas\\Cadastro\\DestMail" VSDGPESSOA,"P" To REG_DESTMAIL
        If RetVal
           Then [Endereco_Eletronico] = Lower(REG_DESTMAIL["Email"])
        EndIf
EndIf  

Moveto TAB_ANT
UnLockRecord
KeyPress "F7"
IsLockRecord()
Do_it!
;Echo Normal
;Echo Off
                       
Moveto TAB_ANT
CoEditKey
Switch
  Case EMAIL_PARAM_ROTINA="DESTINAT" : 
       Moveto [Codigo]
       Locate CODIGO              
EndSwitch
KeyPress "F7"
IsLockRecord()
Moveto Field FIE_ANT

Release Vars AUX_LANCTO,AUX_CAMPO,EMAIL_PARAM_ROTINA,EMAIL_PARAM_AUTOMATICO,JanelaEmail,ARRAY_EMAIL,CONT_ARRAY,ty
Release Procs Tipo_Email,EMAIL_ADD_SDDialog,Adiciona_Email,EMAIL_SDDialog,EMAIL
