;** 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
;Ultima Alteracao: 12/07/2020 por Anivel Augusto Rodrigues
;Motivo Alteracao: Ajustes email nova logica                          
;Ultima Alteracao: 31/07/2020 por Anivel Augusto Rodrigues
;Motivo Alteracao: Ajustes rodape informativo para mais de um email utilzar separador ";" 
;Ultima Alteracao: 04/09/2020 por Anivel Augusto Rodrigues
;Motivo Alteracao: Ajustes aceitar mais de um amil com ";" 

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

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

If Disparo="CLOSE"
   then CancelDialog
        Return Temp
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: Temp=False
     Case Evento["KeyCode"] =  27:
          Temp=False
          Switch
            Case Campo="TG_IDENTIFICACAO" : CancelDialog
            Case Campo="TG_EMAIL"         : Campo="TG_IDENTIFICACAO"
            Case Campo="TG_FLAG2"         : Campo="TG_EMAIL"
          EndSwitch                            
     Case Evento["KeyCode"] = 13:    
          Temp=False
          Switch
             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 EMAIL_TIPO <> "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 

             Case Campo="TG_FLAG2" :
                  VSDGFLAG2=ControlValue("TG_FLAG2")

                  EMAIL_MontaArray("GRAVAR",EMAIL_Indice,VSDGEMAIL,VSDGIDENTIFICACAO)
                  AcceptDialog     
          EndSwitch
   EndSwitch
EndIf

Switch
  Case Campo="TG_EMAIL"     : MSG_CAMPO = "Email, para mais de um email use \";\" como separador "
  Case Campo="TG_FLAG2"     : MSG_CAMPO = "Pausa para Prosseguir"                                                
Endswitch
TECLA    = "//Esc=Retornar  Enter=Avanar/Gravar"

RefreshDialog
Monta_Prompt(MSG_CAMPO,TECLA)
Return Temp

EndProc ;EMAIL_ADD_SDDialog()

;*******************************************************************************
;* Rotina Principal De Reajuste de Preco                                       *
;*******************************************************************************
Proc EMAIL_Adiciona()                

EMAIL_INICIO_DIALOG2= True

EMAIL_NOME        =EMAIL_MontaArray("PEGAREMAIL"  ,EMAIL_Indice,"","")
EMAIL_TIPO        =EMAIL_MontaArray("PEGARTIPO"   ,EMAIL_Indice,"","")
if EMAIL_TIPO="P"
   then EMAIL_NOME="£nico do cadastro ! [PRINCIAL]"
endif   
 
VSDGEMAIL         =EMAIL_MontaArray("PEGARVREMAIL",EMAIL_Indice,"","")
VSDGIDENTIFICACAO =EMAIL_MontaArray("PEGARVRIDENT",EMAIL_Indice,"","")
VSDGFLAG2         = ""     

ShowDialog "Informe o valor" Proc "EMAIL_ADD_SDDialog"
Trigger "OPEN","CLOSE","ARRIVE"                                            
  Key "F1","ENTER","ESC"
    @ 00,01 Height 06 Width 79

   Label  @ 00,00 "Identificao para o email "+EMAIL_NOME  For "TG_IDENTIFICACAO"
   Accept @ 01,01 Width 23 "A20" Tag "TG_IDENTIFICACAO" To VSDGIDENTIFICACAO
      
   Label  @ 02,00 "Email "+EMAIL_NOME  For "TG_EMAIL"
   Accept @ 03,01 Width 73 "A100" Tag "TG_EMAIL" To VSDGEMAIL
        
   Label  @ 02,73 ""  For "TG_FLAG2"
   Accept @ 03,73 Width 1 "A3" Tag "TG_FLAG2" To VSDGFLAG2
   
EndDialog
EndProc ;EMAIL_Adiciona()

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

Temp             = True
MSG_CAMPO        = ""                                               
TECLA            = ""
                                     
If EMAIL_INICIO_DIALOG
   Then Campo = "TG_EMAILS"
        EMAIL_INICIO_DIALOG=False
EndIf     

switch
  case Disparo="OPEN"      :
    AoCriarShowDialog()
  
  case Disparo="CLOSE"     :
    CancelDialog

  case Disparo="SELECT"    :  
    Temp=True
    EMAIL_Indice = ControlValue("TG_EMAILS")
    EMAIL_Adiciona()
  
  case Disparo="EVENT" And Evento["Type"]="KEY" :
   Switch
     Case Evento["KeyCode"] =   9 : Temp=False
     Case Evento["KeyCode"] =  27 :
          Temp=False
          Switch
            Case Campo="TG_EMAILS"     : CancelDialog
          EndSwitch
     Case Evento["KeyCode"] = 13:
          Temp=False
          Switch
            Case Campo="TG_EMAILS" :
              EMAIL_Indice = ControlValue("TG_EMAILS")
              EMAIL_Adiciona()
          EndSwitch
   EndSwitch
endswitch

MSG_CAMPO="Selecione o item desejado"
TECLA    = "//Esc=Retornar  Enter=Incluir/Alterar"

RefreshDialog
Monta_Prompt(MSG_CAMPO,TECLA)
Return Temp

EndProc ;EMAIL_SDDialog()

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

LimpaRodape(0)

Echo Normal
Echo Off
ShowDialog "Outros Emailïs -> "+EMAILTitulo Proc "EMAIL_SDDialog"
Trigger "OPEN","ARRIVE","CLOSE","SELECT"
Key "F1","ENTER","ESC"
     @ 06,10   Height 08 Width 79

    Label @ 00,00 "    Tipo           IdentificaÆo        Email                         "  For "TG_EMAILS"
                   ;12345678901234567890123456789012345678901234567890123456789012345678901234567890
                   ;12 12345678901234 12345678901234567890 1234567890123456789012345678901234567890

    PickDynArray @01,01 Height 5 width 75
      Columns 1
       EMAIL_ARRAY
        Tag "TG_EMAILS"
         To ARRAYELEMENT
         
EndDialog
EndProc ;EMAIL()

;*******************************************************************************
;* Funäes email                                                               *
;*******************************************************************************
Proc EMAIL_MontaArray(oper,indice,s,s2)
private oper,indice,s

Result=""
switch
  case oper="GRAVAR"       : EMAIL_ARRAY[indice] = Format("W017",EMAIL_ARRAY[indice])+Spaces(1)+   ; tipo/nome
                                                   Format("W020",rtrim(s2))          +Spaces(1)+   ; identificaÆo
                                                   Lower(rtrim(s))                                 ; email

  case oper="PEGARTIPO"    : Result=rtrim(substr(EMAIL_ARRAY[indice],02,01))         ; nome

  case oper="PEGAREMAIL"   : Result=rtrim(substr(EMAIL_ARRAY[indice],04,14))         ; nome

  case oper="PEGARVRIDENT" : Result=rtrim(substr(EMAIL_ARRAY[indice],19,20))         ; identificaÆo

  case oper="PEGARVREMAIL" : Result=Lower(rtrim(substr(EMAIL_ARRAY[indice],40,100))) ; email
  
endswitch
;12345678901234567890123456789012345678901234567890123456789012345678901234567890
;12 12345678901234 12345678901234567890 1234567890123456789012345678901234567890

Return Result
endproc

;*******************************************************************************
;* Rotina Principal                                                            *
;*******************************************************************************

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

Release Vars JanelaEMAIL,EMAIL_ARRAY

EMAIL_TabelaOrigem = Table()
EMAIL_CampoOrigem = Field()

VSDGPESSOA = [Codigo]
VSDGEMAILP = Lower(rtrim([Endereco_Eletronico]))
EMAILTitulo= [Codigo]+"-"+[Nome Fantasia EmitDest]

DynArray EMAIL_ARRAY[]
EMAIL_ARRAY[01] = "1P PRINCIPAL"        
EMAIL_ARRAY[02] = "2N NF-e"
EMAIL_ARRAY[03] = "3V DEP.VENDAS"
EMAIL_ARRAY[04] = "4C DEP.COMPRAS"
EMAIL_ARRAY[05] = "5F DEP.FINANCEIRO"

UnLockRecord
Moveto EMAIL_TabelaOrigem
IsLockRecord()

JanelaEMAIL = AbrirTabela("\\Sistemas\\Cadastro\\DestMail","Add",1,0,1,15)
Window Select JanelaEMAIL
Moveto [Codigo]
Locate VSDGPESSOA
If Retval
   Then While [Codigo] = VSDGPESSOA
          For EMAIL_wi From 1 To 5
            if EMAIL_MontaArray("PEGARTIPO",EMAIL_wi,"","")=[EmailTipo]
               then EMAIL_MontaArray("GRAVAR",EMAIL_wi,[Email],[EmailIdentificacao])
            endif
          endfor
          Skip
          If Eot() Then QuitLoop EndIf
        EndWhile 
EndIf   
if EMAIL_MontaArray("PEGARVREMAIL",1,"","")="" or          ; Principal
   EMAIL_MontaArray("PEGARVREMAIL",1,"","")<>VSDGEMAILP
   then EMAIL_MontaArray("GRAVAR",1,VSDGEMAILP,"")
endif   

Moveto EMAIL_TabelaOrigem
KeyPress "F7"

EMAIL()  

Moveto EMAIL_TabelaOrigem
KeyPress "F7"

Window Select JanelaEMAIL
Moveto [Codigo]
For EMAIL_wi From 1 To 5
  EMAIL_TIPO=EMAIL_MontaArray("PEGARTIPO",EMAIL_wi,"","")
  Locate VSDGPESSOA,EMAIL_TIPO
  If Retval
     Then 
          switch
            case EMAIL_MontaArray("PEGARVREMAIL",EMAIL_wi,"","")="" And        
                 EMAIL_MontaArray("PEGARVRIDENT",EMAIL_wi,"","")=""     :
              Del

            case EMAIL_MontaArray("PEGARVREMAIL",EMAIL_wi,"","")<>[Email             ] Or        
                 EMAIL_MontaArray("PEGARVRIDENT",EMAIL_wi,"","")<>[EmailIdentificacao]     : 
               [Email             ]=EMAIL_MontaArray("PEGARVREMAIL",EMAIL_wi,"","")  
               [EmailIdentificacao]=EMAIL_MontaArray("PEGARVRIDENT",EMAIL_wi,"","") unlockrecord
          endswitch
     Else     
          switch
            case EMAIL_MontaArray("PEGARVREMAIL",EMAIL_wi,"","")<>"" Or        
                 EMAIL_MontaArray("PEGARVRIDENT",EMAIL_wi,"","")<>""    :   
              end down
              [Codigo            ]=VSDGPESSOA
              [EmailTipo         ]=EMAIL_TIPO
              [Email             ]=EMAIL_MontaArray("PEGARVREMAIL",EMAIL_wi,"","")
              [EmailIdentificacao]=EMAIL_MontaArray("PEGARVRIDENT",EMAIL_wi,"","") unlockrecord
          endswitch
  endif        
endfor
Do_It!
ClearImage

Moveto EMAIL_TabelaOrigem
CoeditKey
KeyPress "F7"
Moveto Field EMAIL_CampoOrigem

if EMAIL_MontaArray("PEGARVREMAIL",1,"","")<>"" and  ; Principal
   EMAIL_MontaArray("PEGARVREMAIL",1,"","")<>[Endereco_Eletronico]
   then [Endereco_Eletronico]=EMAIL_MontaArray("PEGARVREMAIL",1,"","") 
endif   

Release Vars EMAIL_PARAM_ROTINA,JanelaEMAIL,
             EMAIL_ARRAY,EMAIL_wi,
             EMAIL_INICIO_DIALOG, EMAIL_INICIO_DIALOG2, EMAILTitulo,EMAIL_Indice,
             EMAIL_TIPO,
             EMAIL_TabelaOrigem,EMAIL_CampoOrigem
             
Release Procs EMAIL_ADD_SDDialog,EMAIL_Adiciona,EMAIL_SDDialog,EMAIL,EMAIL_MontaArray
