;** Rc_0025 *******************************************************************
;*  Relatorio com Clientes/Fornecedores CGC/CPF Invalido                       *
;*******************************************************************************
;Autor           : Vise Informatica (VISERP)
;Desenvolvido    : 08/04/2003 por Anivel Augusto Rodrigues
;Ultima Alteracao: 25/04/2005 por Jose Luis Ameno da Silva
;Motivo Alteracao: Pega os Cgc e Cpf  duplicados
;Ultima Alteracao: 03/04/2007 por Anivel Augusto Rodrigues
;Motivo Alteracao: Imprimir CPFCNPG em Branco Sim/Nao
;Ultima Alteracao: 08/01/2009 por Anivel Augusto Rodrigues
;Motivo Alteracao: Ajustes Checa_CGC_CPF_Format
;Ultima Alteracao: 09/09/2016 por Anivel Augusto Rodrigues
;Motivo Alteracao: Ajustes showdialog 
;Ultima Alteracao: 23/12/2016 por Rafael Matos
;Motivo Alteracao: Ajustes showdialog
;Ultima Alteracao: 20/04/2021 por Bruno Carvalho             
;Motivo Alteracao: Ajustes no processo do filtro "TG_STATUS" na "Bib_Show"                  
;Ultima Alteracao: 16/07/2021 por Anivel Augusto Rodrigues
;Motivo Alteracao: Ajustes VSDGSTATUS

;******************************************************************************
; Checa Cgc e Cpf formatados                                                  *
;******************************************************************************
Proc Checa_CGC_CPF_Format(Campo_CGC_CPF)
Private Campo_CGC_CPF, Validar
;validar cgc/cpf
;123456789012345678
;##.###.###/####-##  CGC
;12345678901234
;###.###.###-##      CPF

Validar=False
If SubStr(Campo_CGC_CPF,03,1)="." and
   SubStr(Campo_CGC_CPF,07,1)="." and
   SubStr(Campo_CGC_CPF,11,1)="/" and
   SubStr(Campo_CGC_CPF,16,1)="-"
   Then WVR=""               ;CGC
        WVR=SubStr(Campo_CGC_CPF,01,2)+
            SubStr(Campo_CGC_CPF,04,3)+
            SubStr(Campo_CGC_CPF,08,3)+
            SubStr(Campo_CGC_CPF,12,4)+
            SubStr(Campo_CGC_CPF,17,2)
        If E_Numerico(WVR)
           Then CHECACGC(WVR)
                If WB=0
                   Then Validar=True
                EndIf
        EndIf
   Else If SubStr(Campo_CGC_CPF,04,1)="." and
           SubStr(Campo_CGC_CPF,08,1)="." and
           SubStr(Campo_CGC_CPF,12,1)="-"
           Then WVR=""               ;CPF
                WVR=SubStr(Campo_CGC_CPF,01,3)+
                    SubStr(Campo_CGC_CPF,05,3)+
                    SubStr(Campo_CGC_CPF,09,3)+
                    SubStr(Campo_CGC_CPF,13,2)
                If E_Numerico(WVR)
                   Then CHECACPF(WVR)
                        If WB=0
                           Then Validar=True
                        EndIf
                EndIf
        EndIf
EndIf
Return Validar
EndProc

;*******************************************************************************
;* Rotina p/ Selecionar e Order Registros                                      *
;*******************************************************************************
Proc ORDEM()
ClearAll

{Ask} {Destinat} Check
Moveto [Tipo   ]
Switch
  Case VSDGTIPO="C": TypeIn "C"
  Case VSDGTIPO="F": TypeIn "F"
EndSwitch
Switch
  Case VSDGSTATUS="INATIVO" : Moveto [Inativo] TypeIn "S"
  Case VSDGSTATUS="ATIVO"   : Moveto [Inativo] TypeIn "Not S"
EndSwitch      
If VSDGBRANCOS="NAO"
   Then Moveto [CGC] TypeIn "Not Blank"
EndIf

Do_It!
ClearAll

Coedit "Answer"
Moveto [CGC]
Scan
  CGC=[CGC]
  If IsBlank(CGC) Or CGC="000.000.000-00" Or
     CGC="111.111.111-11" Or CGC="00.000.000/0000-00" Or
     CGC="999.999.999-99"
     Then [Telex]="INVALIDOS"
     Else If Not Checa_CGC_CPF_Format(CGC)
             Then [Telex]="INVALIDOS"
          EndIf
  EndIf
EndScan
Do_It!
ClearAll

{Ask} {Answer} Check
Moveto [Telex] TypeIn "INVALIDOS"
Do_It!
ClearAll

TabelaCC=Cria_Temp(PrivDir())
ClearAll
Rename "Answer" TabelaCC

;; Ate aqui ja tem os invalidos (brancos, zeros ou fora do formato)

{Ask} {Destinat}
Moveto [CGC    ] Check
Moveto [Codigo ] TypeIn "Calc Count"
Moveto [Tipo   ]
Switch
  Case VSDGTIPO="C": TypeIn "C"
  Case VSDGTIPO="F": TypeIn "F"
EndSwitch
Switch
  Case VSDGSTATUS="INATIVO" : Moveto [Inativo] TypeIn "S"
  Case VSDGSTATUS="ATIVO"   : Moveto [Inativo] TypeIn "Not S"
EndSwitch      
Do_It!
ClearAll

{Ask} {Answer} TypeIn "Delete"
Moveto [CGC   ] TypeIn "Blank Or \"00.000.000/0000-00\" Or \"000.000.000-00\""+
                       " Or \"111.111.111-11\" Or \"999.999.999-99\""
Do_It!
ClearAll

{Ask} {Answer} TypeIn "Delete"
Moveto [Count Of Codigo ] TypeIn "<= 1"
Do_It!
ClearAll

{Ask} {Answer}
Moveto [CGC ] Example "CGC"
{Ask} {Destinat} Check
Moveto [CGC ] Example "CGC"
Do_It!
ClearAll

{Ask} {Answer}
Moveto [Telex ] TypeIn "Changeto DUPLICADOS"
Do_It!
ClearAll

Add "Answer" TabelaCC ClearImage

Delete "Answer"

ClearImage

EndProc  ; (Seleciona)

;*******************************************************************************
;* Rotina                                                                      *
;*******************************************************************************
Proc SD_Dialog1(Disparo,Campo,Evento,Elemento)
Temp=True
MSG_CAMPO=""
TECLA=""
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: Dialog_F1()
     Case Evento["KeyCode"] =  27:
          Temp=False
          Switch
            Case Campo="TG_TIPO"           : Campo="NO"
            Case Campo="TG_STATUS"         : Campo="TG_TIPO"
            Case Campo="TG_OPRELCODALF"    : Campo="TG_STATUS"
            Case Campo="TG_BRANCOS"        : Campo="TG_OPRELCODALF"
            Case Campo="YES" Or Campo="NO" : Campo="TG_TIPO"
          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_TIPO" : 
                         VSDGTIPO=ControlValue("TG_TIPO")
                         Switch
                           Case VSDGTIPO="C": VSDGNOMTIPO="CLIENTES"
                           Case VSDGTIPO="F": VSDGNOMTIPO="FORNECEDORES"
                           Case VSDGTIPO="" : VSDGNOMTIPO="TODOS"
                         EndSwitch
                         Campo="TG_STATUS"
                    Case Campo="TG_STATUS"      : Campo="TG_OPRELCODALF"
                    Case Campo="TG_OPRELCODALF" : Campo="TG_BRANCOS"
                    Case Campo="TG_BRANCOS"     : Campo="YES"
                         VSDGBRANCOS=ControlValue("TG_BRANCOS")
                         If IsBlank(VSDGBRANCOS)
                            Then VSDGBRANCOS="SIM"
                         EndIf
                  EndSwitch
          EndIf
          RefreshDialog
   EndSwitch
EndIf
Switch
  Case Campo="TG_TIPO":
     MSG_CAMPO="Informe C -> Cliente, F -> Fornecedor  Em Branco=Todos"
  Case Campo="":
     MSG_CAMPO=""
  Case Campo="TG_BRANCOS":
     MSG_CAMPO="Imprime CPF/CNPJ em brancos Sim/No"
Endswitch
Monta_Prompt(MSG_CAMPO,TECLA)
Return Temp
EndProc

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

VSDGTIPO     =""
VSDGNOMTIPO  =""
VSDGOPRELCODALF  =""
VSDGSTATUS       =""

While True

  ShowDialog "RelatĒrio de CGC/CPF Invalido" Proc "SD_Dialog1"
  Trigger "OPEN","ARRIVE"
  Key "F1","ENTER","ESC","TAB",
      -104, -105, -106, -107, -108, -109, -110, -111, -112, -113, -139, -140  
   @ 05,12   Height 13 Width 72

    Label  @ 00,00 "Tipo" For "TG_TIPO"
    Accept @ 01,04 Width 04 "A1"  Picture "C,F, " Tag "TG_TIPO" To VSDGTIPO
    Accept @ 01,09 Width 25 "A13" Picture "!*!" Tag "TG_NOMTIPO" To VSDGNOMTIPO

    Label  @ 02,00 "Status" For "TG_STATUS"
    Accept @ 03,01  Width 33 "A12" Picture "ATIVO,INATIVO,TODOS" Tag "TG_STATUS" To VSDGSTATUS

    Label  @ 04,00 "Ordem" For "TG_OPRELCODALF"
    Accept @ 05,04 Width 04 "A1"  Picture "1,2" Tag "TG_OPRELCODALF" To VSDGOPRELCODALF
    Accept @ 05,09 Width 25 "A12" Picture "!*!" Tag "TG_NOMOPRELCODALF" To VSDGNOMOPRELCODALF

    Label  @ 06,00 "Imprime CPF/CNPJ em branco" For "TG_BRANCOS"
    Accept @ 07,02 Width 06 "A3"  Picture "SIM,NAO" Tag "TG_BRANCOS" To VSDGBRANCOS

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

  EndDialog
  If Not RetVal Then QuitLoop EndIf

  DISPOSITIVO()
  If OK="" Then Loop EndIf

  AGUARDE("Aguarde, Gerando RelatĒrio")
  ORDEM()

  If IsEmpty(TabelaCC)
     Then MENSAGEM("No Existem Registros a Processar")
          Loop
  EndIf

  If VSDGOPRELCODALF = "2"
     Then CopyReport "\\Base\\Sistemas\\Cadastro\\RC_0040"  "13"  TabelaCC "R"
          DORDEM="Alfabetica"
     Else CopyReport "\\Base\\Sistemas\\Cadastro\\RC_0040"  "2"  TabelaCC "R"
          DORDEM="de Codigo"
  EndIf

  IMPRIREL(TabelaCC,"R")

  Delete TabelaCC

EndWhile
ClearAll
Release Vars All
Release Procs All
