;** Emp_Aut ********************************************************************
;*  Rela‡Æo de Autorizados para obter xmls                                     *
;*******************************************************************************
;Autor           : Vise Informatica (VISERP)
;Desenvolvido    : 20/01/2018 por Kamila Gomes
;Ultima Alteracao:   /  /     por                         
;Motivo Alteracao:                           

;*******************************************************************************
Proc SetaIndex(c)
Private c
 IndexEmpAut=NumVal(c)
EndProc

Proc SetaSeq(c)
Private c
 VSD_I_SEQ=AlinhM(StrVal(c),2)
EndProc

Proc PegaSeqIndex(c)
Private c
 SetaSeq(SubStr(Array_EmpAut[c],1,2))
EndProc

Proc Localiza_Registro()
Switch
  Case TabWork="Destinat":
    Moveto "EmpAut"
    Moveto [Codigo]
    Locate [Destinat->Codigo],NumVal(VSD_I_SEQ)
EndSwitch
If RetVal
   Then Return True
   Else Return False
EndIf   
EndProc

;*******************************************************************************
;*  Rotina para carregar variaveis                                            *
;*******************************************************************************
Proc CarregaVars()
If Localiza_Registro()
   Then
        VSDGCNPJ   =[CNPJ_CPF           ]
        VSDGNOMEAUT=[Nome_Autorizado_XML]
        Return True
EndIf
   
EndProc

;*******************************************************************************
;*  Rotina Atualiza Array                                                      *
;*******************************************************************************
Proc AtuaArray(c)
Private c

Array_EmpAut[c]= VSD_I_SEQ+"   "+
                 Format("W18,AL",[CNPJ_CPF])+"    "+
                 Format("W40,AL",[Nome_Autorizado_XML])

ArrayElement=c
EndProc

;*******************************************************************************
;*  Rotina para salvar dados                                                  *
;*******************************************************************************
Proc SalvaDados()
Private i,c

[CNPJ_CPF           ]=VSDGCNPJ
[Nome_Autorizado_XML]=VSDGNOMEAUT

UnLockRecord

AtuaArray(IndexEmpAut)

EndProc

;*******************************************************************************
;*  Rotina para excluir array e registro                                       *
;*******************************************************************************
Proc ExcluiArray()
Private i,n

If QuerExcluir("deste autorizado")
   Then 
        If Localiza_Registro()
           Then Del
        EndIf   

        Array_EmpAut[IndexEmpAut]=""

        DynArray Array_EmpAutI[]
        n=0
        For i From 1 To DynArraySize(Array_EmpAut)
          If IsBlank(Array_EmpAut[i]) Then Loop EndIf
          n=n+1
          Array_EmpAutI[n]=Array_EmpAut[i]
        EndFor

        Release Vars Array_EmpAut
        DynArray Array_EmpAut[]
        For i From 1 To DynArraySize(Array_EmpAutI)
          Array_EmpAut[i]=Array_EmpAutI[i]
        EndFor
        Release Vars Array_EmpAutI                  
        
EndIf

EndProc

;*******************************************************************************
;*  Rotina Atualiza Array                                                      *
;*******************************************************************************
Proc ChecaDados()
Private Ok,c,MSG

MSG="" 
Ok  =True

If VSDGCNPJ="00.000.000/0000-00" Or
   VSDGCNPJ="000.000.000-00" Or
   Not Checa_CGC_CPF_Formatado(VSDGCNPJ) 
   Then ;MSG=MSG+"CNPJ/CPF inv lido !//"
EndIf
If Not IsBlank(MSG)
   Then Mensagem(MSG)
        Ok=False                               
EndIf    

If Ok
   Then SalvaDados()
EndIf    

Return Ok
EndProc

;*******************************************************************************
;* Rotina                                                                      *
;*******************************************************************************
Proc SD_Dialog(Disparo,Campo,Evento,Elemento)
Private Disparo,Campo,Evento,Elemento

TECLA=""
MSG_CAMPO=""
Temp=True

If INICIO_DIALOG
   Then INICIO_DIALOG=False
        Switch
          Case SD=1: Campo="TG_EMPAUT"
            Dynarray WinArray5[]
            Window Handle Dialog to JANELA
            Window GetAttributes JANELA To WinArray5

          Case SD=2: Campo="TG_CNPJ" 
            Dynarray WinArray52[]
            Window Handle Dialog to JANELA2
            Window GetAttributes JANELA2 To WinArray52

        EndSwitch
EndIf

;;;If Disparo="OPEN" Then AoCriarShowDialog() EndIf

If Disparo="CLOSE"
   Then Switch
          Case Campo="YES": AcceptDialog
          Case Campo="NO" : CancelDialog
        EndSwitch
        Return Temp
EndIf

If Disparo="SELECT" And Campo="TG_EMPAUT" And
   DynArraySize(Array_EmpAut)>0 
   Then Temp=True
        CHAMA=True
        EmpAut()
EndIf

If Disparo="UPDATE"
   Then  Switch
          Case Campo="YES": AcceptDialog
          Case Campo="NO" : CancelDialog
          OtherWise       : Return Temp
        EndSwitch
EndIf

If Disparo="EVENT" And Evento["Type"]="KEY" Then
   Switch
     Case Evento["KeyCode"] =   9: Temp=False

     Case Evento["KeyCode"] =  27:
          Temp=False
          Switch
            Case SD=1:
              Switch 
						    Case Campo="TG_EMPAUT"         : Campo="YES"
                Case Campo="YES" Or Campo="NO" : Campo="TG_EMPAUT"
              EndSwitch

            Case SD=2: 
                Switch 
                  Case Campo="TG_CNPJ"           : CancelDialog
    			        Case Campo="TG_NOMEAUT"        : Campo="TG_CNPJ"
                  Case Campo="TG_FLAG"           : Campo="TG_NOMEAUT"
                EndSwitch 
          EndSwitch

       Case Evento["KeyCode"]=-60 And DynArraySize(Array_EmpAut)>0:
            Campo="TG_EMPAUT"

       Case Evento["KeyCode"] = -83 And Campo="TG_EMPAUT":
            If DynArraySize(Array_EmpAut)>0
               Then SetaIndex(ArrayElement)
                    PegaSeqIndex(IndexEmpAut)
                    ExcluiArray()
            EndIf
       Case Evento["KeyCode"] =-82 And Campo="TG_EMPAUT":
            Temp=False
            CHAMA=False
            EMPAUT()

       Case Evento["KeyCode"] = 13:
          Temp=False

          If Checa_TAG()
             Then Switch
                    Case Campo="TG_CNPJ"        : 
                         VSDGCNPJ=ControlValue("TG_CNPJ")
                         Campo="TG_NOMEAUT"                                        
             
                    Case Campo="TG_NOMEAUT"       : 
                         VSDGNOMEAUT=ControlValue("TG_NOMEAUT")
                         Campo="TG_FLAG"
                                                              
                    Case Campo="TG_FLAG"        : 
                         If ChecaDados()
                            Then AcceptDialog
                            Else Campo="TG_CNPJ"
                         EndIf
                    Case Campo="TG_EMPAUT" And
                         DynArraySize(Array_EmpAut)>0:
                         CHAMA=True
                         EMPAUT()

                  EndSwitch
          EndIf
   EndSwitch
EndIf

SelectControl Campo
TECLA=" Esc=Retornar  "

Switch
  Case Campo="TG_NOMEAUT"         : MSGCAMPO="Informe o nome do autorizado"

  Case Campo="TG_FLAG"         : MSGCAMPO="."

  Case Campo="TG_EMPAUT"  : MSGCAMPO="Autorizados "
       TECLA=TECLA+"Ins=Inserir  "
       If DynArraySize(Array_EmpAut)>0
          Then TECLA=TECLA+"Del=Excluir  Enter=Editar  "+Chr(25)+Chr(24)+"=Selecionar  "
       EndIf

  Case Campo="YES"             : MSGCAMPO="Fechar janela  [Enter]"
EndSwitch

MontaRodapeSD(MSGCAMPO, TECLA)

RefreshDialog

Return TEMP
EndProc  ; (Sd_Dialog)

;*******************************************************************************
;*  Rotina para dados do Autorizado                                            *
;*******************************************************************************
Proc EmpAut()
Private Titdocto, nl2, nl3, nl4, c, tmj

While True
  If CHAMA
     Then Registro="Existe"
          SetaIndex(ArrayElement)
          PegaSeqIndex(IndexEmpAut)
     Else Registro="Novo" 
          
          SetaIndex(0)
          SetaSeq(0)
          For c From 1 To DynArraySize(Array_EmpAut)
            SetaIndex(c)
            PegaSeqIndex(c)
          EndFor
          SetaIndex(IndexEmpAut+1)
          SetaSeq(NumVal(VSD_I_SEQ)+1)

          If Not Localiza_Registro()
             Then End Down
                  Switch
                    Case TabWork="Destinat":
                      [Codigo             ]=[Destinat->Codigo           ]
                  EndSwitch
                  [Sequencia       ]=NumVal(VSD_I_SEQ)
                  UnLockRecord
          EndIf
  EndIf
  If Not CarregaVars()
     Then QuitLoop
  EndIf
  
  SD=2
  INICIO_DIALOG=True
  Mouse Hide 
              
  ShowDialog "Autorizado XML [    ]" Proc "SD_Dialog"
  Trigger "OPEN","ARRIVE"
  Key "ENTER","ESC","F1"
   @ 07,05   Height 08 Width 65
  
    Accept @ -01,38 Width 4 "A2" Tag "I_Seq" To VSD_I_SEQ
    
    Label  @ 01   ,00 "CNPJ/CPF"  For "TG_CNPJ"
    Accept @ 02   ,01 Width 23 "A20"  Picture "##[.]###.###/####-##,###.###.###-##" Tag "TG_CNPJ" To VSDGCNPJ
    
    Label  @ 03  ,00 "Nome Autorizado"  For "TG_NOMEAUT"
    Accept @ 04  ,01 Width 45 "A40"  Picture "!*!" Tag "TG_NOMEAUT" To VSDGNOMEAUT
  
    Accept @ 04  ,60  Width 3  "A1" picture "!*!" Tag "TG_FLAG" to VSDGFLAG 
  
  EndDialog
  If Not RetVal
     Then Switch
            Case Registro="Existe" : Undo
                                     UnLockRecord
            Case Registro="Novo"   : Del
          EndSwitch
             
  EndIf   
  Mouse Show

  QuitLoop
EndWhile  
SD=1
Campo="TG_EMPAUT"
Mouse Show

EndProc    ; (Vencimento)


;*******************************************************************************
;*  Rotina para a Tela Principal                                                *
;*******************************************************************************
Proc Inicial()
Private c

Message("")
Echo Off
Mouse Show

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

;**** Carrega Autorizados xml da Empresa ****************************

IndexEmpAut=0

Switch
  Case TabWork="Destinat":
    Moveto "EmpAut"
    Moveto [Codigo]
    Locate [Destinat->Codigo]
EndSwitch

While True
  Switch
    Case TabWork="Destinat":
      If [Codigo]<>[Destinat->Codigo]
         Then QuitLoop
      EndIf   
  EndSwitch
  If [Sequencia   ]>0
     Then SetaSeq([Sequencia])
          IndexEmpAut=IndexEmpAut+1
          AtuaArray(IndexEmpAut)
  EndIf
  
  Skip
  If EOT() Then QuitLoop EndIf
EndWhile

SD=1
INICIO_DIALOG=True
ArrayElement=1
ShowDialog "Rela‡Æo de Autorizados a obter XML" Proc "SD_Dialog"
  Trigger "OPEN","ARRIVE","UPDATE","CLOSE","SELECT"
     Key "ENTER","ESC","F2","DEL","INS"
   @ 06,03 Height 12 Width 71

    PickDynArray @ 01,01 Height 06 width 66
     Columns 1
      Array_EmpAut
       Tag "TG_EMPAUT"
        To ArrayElement

    Label @ 00,01 "Seq  CNPJ/CPF              Nome Autorizado                    "  For "TG_EMPAUT"
                  ;99123499123
    Pushbutton @ 08,57 width 11 "~F~echar"       OK     Default Value "Ok" Tag "YES"          To BottonValue

  EndDialog
  Mouse Show

EndProc  ;(Inicial)

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

TabWork=Table()

ncampo=Field()
UnLockRecord
FormKey
SaveTables
Moveto TabWork

Inicial()

Moveto TabWork
FormKey
Moveto Field ncampo

Release vars Array_EmpAut,SD,CHAMA,INICIO_DIALOG,INICIO_DIALOG2,
             VSD_I_SEQ,VSDGCNPJ,VSDGNOMEAUT,
             ncampo,Registro,IndexEmpAut,
             Janela, WinArray5,  
             Janela2, WinArray52,TabWork  

Release procs Inical, SD_Dialog, EmpAut, SalvaDados, ExcluiArray, CarregaVars, AtuaArray, ChecaDados,
              SetaIndex, SetaSeq, PegaSeqIndex, Localiza_Registro
