;** Rc_0020 *******************************************************************
;*  Relatorio de Cliente                                                       *
;*******************************************************************************
;Autor: Visao/Vise
;Desenvolvido    : 05/12/1995 por Anivel Augusto Rodrigues
;Ultima Alteracao: 25/01/2005 por Anivel Augusto Rodrigues
;Motivo Alteracao: Novo Modelo Relatorio
;Ultima Alteracao: 27/03/2008 por Anivel Augusto Rodrigues
;Motivo Alteracao: Ajustes e sele‡Æo Contratante/Eventual
;Ultima Alteracao: 25/08/2009 por Humberto/Antonio marcelo.
;Motivo Alteracao: Implementacao do campo ORDEM numero 4 e ajustes.
;Ultima Alteracao: 11/09/2009 por Cleber Alves.
;Motivo Alteracao: Ajustes Filtro Data da Primeira Compra
;Ultima Alteracao: 31/05/2008 por Anivel Augusto Rodrigues
;Motivo Alteracao: Ajustes Ask !                           
;Ultima Alteracao: 14/12/2011 por Bruno Carvalho
;Motivo Alteracao: Adicionado op‡Æo de relatorio "Ordem 5"  
;Ultima Alteracao: 19/12/2011 por Anivel Augusto Rodrigues
;Motivo Alteracao: Ajustes novas funcionaliddes             

;*******************************************************************************
;* Rotina para Selecionar Registros                                            *
;*******************************************************************************
Proc Seleciona1()
ClearAll

{Ask} {Destinat} Check
Moveto [Codigo                 ] Example "Codc" TypeIn "!,>=~VSDGDESTINATINI, <=~VSDGDESTINATFIM"
Moveto [Tipo                   ] TypeIn "C"
Moveto [Codigo do Representante] TypeIn "~VSDGREPRESEN.."
Moveto [Tipo Pessoa            ] TypeIn "~VSDGTIPO.."
If Not IsBlank(VSDGSITUACAO)
   Then Moveto [Situacao do Cliente] TypeIn "~VSDGNOMSITUACAO.."
EndIf
Switch
  Case VSDGSTATUS="1": Moveto [Inativo] TypeIn "N"
  Case VSDGSTATUS="2": Moveto [Inativo] TypeIn "S"
EndSwitch
Switch
  Case VSDGTPASSIST="E": Moveto [Tipo Assistencia   ] TypeIn "EVENTUAL"
  Case VSDGTPASSIST="C": Moveto [Tipo Assistencia   ] TypeIn "CONTRATANTE"
EndSwitch
If VSDGOPDT=2
	 Then Moveto [Data_Cadastro] TypeIn ">=~VSDGDTINI,<=~VSDGDTFIM"
EndIf
{Ask} {Fichacli} check
Moveto [Codigo                 ] Example "Codc"
Do_It!
ClearAll

If VSDGOPRELLISTCAD = "5"
   Then {Ask} {Answer} Check
        Moveto [Codigo            ] Example "Cod!"
        Moveto [Data da Manutencao] Check
        {Ask} {DestInfo}
        Moveto [Codigo            ] Example "Cod"
        Moveto [Data de Nascimento] Check TypeIn "as Data da Manutencao"
        Do_It!
        ClearAll
EndIf   

EndProc    ; (Seleciona)

;*******************************************************************************
;* Rotina para Selecionar Registros                                            *
;*******************************************************************************
Proc Seleciona2()
ClearAll

Seleciona1()

View "DestInfo"
CoEdit "Answer"
Scan
  cliok=false
  Moveto "DestInfo"
  Moveto [Codigo]
  Locate [Answer->Codigo]
  If RetVal
     Then If Not IsBlank([Data de Nascimento])
             Then datai=DateVal(StrVal(Month([Data de Nascimento]))+"/"+StrVal(Day([Data de Nascimento]))+"/"+StrVal(Year(VSDGDTINI)))
                  dataf=DateVal(StrVal(Month([Data de Nascimento]))+"/"+StrVal(Day([Data de Nascimento]))+"/"+StrVal(Year(VSDGDTFIM)))
                  If Datai>=VSDGDTINI And Dataf<=VSDGDTFIM
                     Then cliok=true
                  EndIf
          EndIf
  EndIf
  Moveto "Answer"     
  If not cliok
     Then [Codigo]=""
  EndIf   
EndScan
Do_It!
ClearAll

{Ask}{Answer} Check
Moveto [Codigo] TypeIn "Not Blank"
Do_It!
ClearAll

EndProc    ; (Seleciona)

;*******************************************************************************
;* Rotina para Selecionar Registros                                            *
;*******************************************************************************
Proc Seleciona3()
ClearAll

SetDir "\\Sistemas\\Vendas"
Tabela2=Cria_Temp(PrivDir())
Monta_Dir_Trab(VSDGDTINI,VSDGDTFIM)
While Novo_Dir_Trab()
  If IsTable("Answer")     Then Delete "Answer" EndIf
  If IsTable("\\Sistemas\\Vendas\\"+Dir_Trab+"NotaC")
    Then {Ask} TypeIn "\\Sistemas\\Vendas\\"+Dir_Trab+"NotaC" Enter
          Moveto [Codigo                 ] Check
          Moveto [Data de Emissao        ] TypeIn ">=~VSDGDTINI,<=~VSDGDTFIM"
          Moveto [Descricao da Destinacao] TypeIn "..VEND.. or ..SERV.. or ..SAIDA.."
          Moveto [Tipo Movimento Estoque ] TypeIn "S"
          Moveto [Situacao               ] TypeIn "Not C"
          Moveto [Codigo do Representante] TypeIn "~VSDGREPRESEN.."
          Do_It!
          ClearAll
  EndIf
  If Not IsTable("Answer") Then Loop EndIf
  If IsTable(Tabela2)
     Then Add "Answer" Tabela2
     Else Rename "Answer" Tabela2
  EndIf
EndWhile
ClearAll

SetDir "\\Sistemas\\Cadastro"
Seleciona1()

{Ask} {Answer} Check
Moveto [Codigo                 ] Example "cli"
{Ask} TypeIn Tabela2 Enter
Moveto [Codigo                 ] Example "cli"
Do_It!
ClearAll

Delete Tabela2

EndProc    ; (Seleciona)

;*******************************************************************************
;* Rotina para Selecionar Registros                                            *
;*******************************************************************************
Proc Seleciona4()
ClearAll

SetDir "\\Sistemas\\Vendas"
Tabela2=Cria_Temp(PrivDir())
Monta_Dir_Trab(VSDGDTINI,VSDGDTFIM)
While Novo_Dir_Trab()
  If IsTable("Answer")     Then Delete "Answer" EndIf
  If IsTable("\\Sistemas\\Vendas\\"+Dir_Trab+"NotaC")
    Then {Ask} TypeIn "\\Sistemas\\Vendas\\"+Dir_Trab+"NotaC" Enter
          Moveto [Codigo                 ] Check
          Moveto [Data de Emissao        ] TypeIn ">=~VSDGDTINI,<=~VSDGDTFIM"
          Moveto [Descricao da Destinacao] TypeIn "..VEND.. or ..SERV.. or ..SAIDA.."
          Moveto [Tipo Movimento Estoque ] TypeIn "S"
          Moveto [Situacao               ] TypeIn "Not C"
          Moveto [Codigo do Representante] TypeIn "~VSDGREPRESEN.."
          Do_It!
          ClearAll
  EndIf
  If Not IsTable("Answer") Then Loop EndIf
  If IsTable(Tabela2)
     Then Add "Answer" Tabela2
     Else Rename "Answer" Tabela2
  EndIf
EndWhile
ClearAll

SetDir "\\Sistemas\\Cadastro"
Seleciona1()

{Ask} TypeIn Tabela2 Enter
Moveto [Codigo                 ] Example "cli"
{Ask} {Answer} TypeIn "Fast Delete"
Moveto [Codigo                 ] Example "cli"
Do_It!
ClearAll

Delete Tabela2

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_DESTINATINI"    : Campo="NO"
            Case Campo="TG_DESTINATFIM"    : Campo="TG_DESTINATINI"
            Case Campo="TG_STATUS"         : Campo="TG_DESTINATFIM"
            Case Campo="TG_SITUACAO"       : Campo="TG_STATUS"
            Case Campo="TG_TIPO"           : Campo="TG_SITUACAO"
            Case Campo="TG_TPASSIST"       : Campo="TG_TIPO"
            Case Campo="TG_OPRELLISTCAD"   : Campo="TG_TPASSIST"
            Case Campo="TG_OPRELCODALF"    : Campo="TG_OPRELLISTCAD"
            Case Campo="TG_REPRESEN"       : Campo="TG_OPRELCODALF"
            Case Campo="TG_OPDT"           : Campo="TG_REPRESEN"
            Case Campo="TG_DTINI"          : Campo="TG_OPDT"
            Case Campo="TG_DTFIM"          : Campo="TG_DTINI"
            Case Campo="TG_SN_CABECALHO"   : Campo="TG_OPDT" 
            Case Campo="TG_SN_LOCAL"       : Campo="TG_SN_CABECALHO"
            Case Campo="YES" Or Campo="NO" : Campo="TG_DESTINATINI"
          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_DESTINATINI" :Campo="TG_DESTINATFIM"
                    Case Campo="TG_DESTINATFIM" :
                         If VSDGDESTINATFIM < VSDGDESTINATINI
                            Then Mensagem("Codigo final menor que codigo inicial !")
                            Else Campo="TG_STATUS"
                         EndIf
                    Case Campo="TG_STATUS"      : Campo="TG_SITUACAO"
                    Case Campo="TG_SITUACAO"    : Campo="TG_TIPO"
                        VSDGSITUACAO=ControlValue("TG_SITUACAO")
                        Switch
                          Case VSDGSITUACAO="B": VSDGNOMSITUACAO="BOM"
                          Case VSDGSITUACAO="R": VSDGNOMSITUACAO="REGULAR"
                          Case VSDGSITUACAO="P": VSDGNOMSITUACAO="PESSIMO"
                          Case VSDGSITUACAO="E": VSDGNOMSITUACAO="RECADASTRO"
                          Case VSDGSITUACAO="" : VSDGNOMSITUACAO="TODOS"
                        EndSwitch
                    Case Campo="TG_TIPO"    : Campo="TG_TPASSIST"
                         VSDGTIPO=ControlValue("TG_TIPO")
                         Switch
                           Case VSDGTIPO="F": VSDGNOMTIPO="FISICA"
                           Case VSDGTIPO="J": VSDGNOMTIPO="JURIDICA"
                           OtherWise        : VSDGNOMTIPO="TODOS"
                         EndSwitch                     
                    Case Campo="TG_TPASSIST"  : Campo="TG_OPRELLISTCAD"
                        VSDGTPASSIST=ControlValue("TG_TPASSIST")
                        Switch 
                          Case VSDGTPASSIST="E": VSDGNOMTPASSIST="EVENTUAL"
                          Case VSDGTPASSIST="C": VSDGNOMTPASSIST="CONTRATANTE"
                          Case VSDGTPASSIST="" : VSDGNOMTPASSIST="TODOS"
                        EndSwitch
                    Case Campo="TG_OPRELLISTCAD": Campo="TG_OPRELCODALF"
                         Switch
                           Case VSDGOPRELLISTCAD="2": VSDGNOMOPRELLISTCAD="Cadastro de Clientes"
                           Case VSDGOPRELLISTCAD="3": VSDGNOMOPRELLISTCAD="Listagem de Clientes II"
                           Case VSDGOPRELLISTCAD="4": VSDGNOMOPRELLISTCAD="Listagem por Cadastros de Clientes I"
                           Case VSDGOPRELLISTCAD="5": VSDGNOMOPRELLISTCAD="Listagem por Cadastros de Clientes II"
                           OtherWise:                 VSDGOPRELLISTCAD="1"
                                                      VSDGNOMOPRELLISTCAD="Listagem de Clientes"
                         EndSwitch
                    Case Campo="TG_OPRELCODALF" : Campo="TG_REPRESEN"
                    Case Campo="TG_REPRESEN"    : Campo="TG_OPDT"
                    Case Campo="TG_OPDT"        : Campo="TG_DTINI"
                         VSDGOPDT=ControlValue("TG_OPDT")

                         If VSDGOPDT=5 
                            Then If VSDGOPRELLISTCAD="2"
                                    Then Campo="TG_SN_CABECALHO" 
                                    Else Campo="YES"
                                 EndIf                        
                         EndIf
           
                    Case Campo="TG_DTINI"       : Campo="TG_DTFIM"
                    Case Campo="TG_DTFIM"       : Campo="TG_SN_CABECALHO"
                         If VSDGOPRELLISTCAD<>"2"
                            Then Campo="YES"
                         EndIf                        
                    
                    Case Campo="TG_SN_CABECALHO"  :
                         VSDGCABECALHO=ControlValue("TG_SN_CABECALHO")
                         If Not IsFieldNull(VSDGCABECALHO)
                            Then If VSDGCABECALHO="NAO"
                                    Then Campo="TG_SN_LOCAL"
                                    Else Campo="YES"
                                 EndIf
                         EndIf

                    Case Campo="TG_SN_LOCAL"        :
                         VSDGLOCAL=ControlValue("TG_SN_LOCAL")
                         If Not IsFieldNull(VSDGLOCAL)
                            Then Campo="YES"
                         EndIf
                         
                  EndSwitch
          EndIf
          RefreshDialog
   EndSwitch
EndIf
Switch
  Case Campo="TG_TIPO":
     MSG_CAMPO="Informe (F=Fisica  J=Juridica  Em Branco=Todos)"
  Case Campo="TG_SITUACAO":
     MSG_CAMPO="Informe(B=Bom  R=Regular  P=Pessimo  E=Recadastro  Em Branco=Todos)"
  Case Campo="TG_TPASSIST":
     MSG_CAMPO="Informe (C=Contratante  E=Eventual  Em Branco=Todos)"
  Case Campo="TG_OPRELLISTCAD":
     MSG_CAMPO="Op‡”es: 1-Listagem  2-Cadastro  3-Listagem II 4-Listagem III 5-Listagem IV"
  Case Campo="TG_DTINI":
     MSG_CAMPO="Informe a Data Inicial"   
  Case Campo="TG_DTFIM":
     MSG_CAMPO="Informe a Data Final"
  Case Campo="TG_DTCOMPINI":
     MSG_CAMPO="Informe a Data Inicial"
  Case Campo="TG_DTCOMPFIM":  
     MSG_CAMPO="Informe a Data Final"   
Endswitch
Monta_Prompt(MSG_CAMPO,TECLA)
Return Temp
EndProc                 

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

VSDGDESTINATINI  =""
VSDGDESTINATFIM  =""
VSDGOPRELCODALF  =""
VSDGOPRELLISTCAD =""
VSDGTIPO         =""
VSDGSITUACAO     =""
VSDGCABECALHO    =""
VSDGLOCAL        =""
VSDGSTATUS       =""
DESCTP           ="Clientes"
VSDGDTINI        =BlankDate()
VSDGDTFIM        =BlankDate()
VSDGDTCOMPINI    =BlankDate()
VSDGDCOMPFIM     =BlankDate()

While True

  ShowDialog "Relat¢rio de Clientes" Proc "SD_Dialog1"
  Trigger "OPEN","ARRIVE"
  Key "F1","ENTER","ESC","TAB",
      -104, -105, -106, -107, -108, -109, -110, -111, -112, -113, -139, -140  
   @ 01,01 Height 20 Width 78

    Label  @ 00,00 "Cliente"  For "TG_DESTINATINI"
    Label  @ 01,01 "de"  For "TG_DESTINATINI"
    Accept @ 01,05 Width 07 "A4"  Picture "[&,#][#*#]" Tag "TG_DESTINATINI" To VSDGDESTINATINI
    Label  @ 01,12 "at‚"  For "TG_DESTINATFIM"
    Accept @ 01,17 Width 07 "A4"  Picture "[&,#][#*#]" Tag "TG_DESTINATFIM" To VSDGDESTINATFIM

    Label  @ 02,00 "Status do Cliente" For "TG_STATUS"
    Accept @ 03,01 Width 04 "A1"  Picture "1,2,3" Tag "TG_STATUS" To VSDGSTATUS
    Accept @ 03,06 Width 23 "A12" Picture "!*!" Tag "TG_NOMSTATUS" To VSDGNOMSTATUS

    Label  @ 02,30 "Situacao do Cliente" For "TG_SITUACAO"
    Accept @ 03,31 Width 04 "A1"  Picture "B,R,P" Tag "TG_SITUACAO" To VSDGSITUACAO
    Accept @ 03,36 Width 23 "A12" Picture "!*!" Tag "TG_NOMSITUACAO" To VSDGNOMSITUACAO

    Label  @ 04,00 "Tipo do Pessoa" For "TG_TIPO"
    Accept @ 05,01 Width 04 "A1"  Picture "F,J" Tag "TG_TIPO" To VSDGTIPO
    Accept @ 05,06 Width 23 "A9" Picture "!*!" Tag "TG_NOMTIPO" To VSDGNOMTIPO

    Label  @ 04,30 "Tipo Assistˆncia" For "TG_TPASSIST"
    Accept @ 05,31 Width 04 "A1"  Picture "C,E" Tag "TG_TPASSIST" To VSDGTPASSIST
    Accept @ 05,36 Width 23 "A12" Picture "!*!" Tag "TG_NOMTPASSIST" To VSDGNOMTPASSIST

    Label  @ 06,00 "Modelo" For "TG_OPRELLISTCAD"
    Accept @ 07,01 Width 04 "A1"  Picture "1,2,3,4,5" Tag "TG_OPRELLISTCAD" To VSDGOPRELLISTCAD
    Accept @ 07,06 Width 23 "A20" Picture "!*!" Tag "TG_NOMOPRELLISTCAD" To VSDGNOMOPRELLISTCAD
    
    Label  @ 06,30 "Op‡„o" For "TG_OPRELCODALF"
    Accept @ 07,31 Width 04 "A1"  Picture "1,2" Tag "TG_OPRELCODALF" To VSDGOPRELCODALF
    Accept @ 07,36 Width 23 "A12" Picture "!*!" Tag "TG_NOMOPRELCODALF" To VSDGNOMOPRELCODALF
    
    Label  @ 08,00 "Representante"  For "TG_REPRESEN"
    Accept @ 09,01 Width 07 "A4"  Picture "[&,#][#*#]" Tag "TG_REPRESEN" To VSDGREPRESEN
    Accept @ 09,09 Width 23 "A15" Picture "!*!" Tag "TG_NOMREPR" To VSDGNOMREPR  
       
    Label  @ 10,00 "Op‡”es de Datas: "  For "TG_OPDT"
    RadioButtons @ 11,01 Height 2 Width 65
     "~A~nivers rio",
     "Ca~d~astro",
     "~C~om Compras",
     "~S~em Compras",
     "~I~gnorar"
     Tag "TG_OPDT"
     To VSDGOPDT
  
    Label  @ 13,00 "Per¡odo Referente" For "TG_DTINI"
    Label  @ 14,00 "de" For "TG_DTINI"
    Accept @ 14,04 Width 13 "D"  Picture "#[#];.##;.##[##]" Tag "TG_DTINI" To VSDGDTINI
    Label  @ 14,17 "at‚" For "TG_DTFIM"
    Accept @ 14,22 Width 13 "D"  Picture "#[#];.##;.##[##]" Tag "TG_DTFIM" To VSDGDTFIM
  
    Label  @ 13,37 "Cabe‡alho" For "TG_SN_CABECALHO"
    Accept @ 14,38 Width 06 "A3"  Picture "SIM,NAO" Tag "TG_SN_CABECALHO" To VSDGCABECALHO

    Label  @ 13,50 "Local/Cobran‡a" For "TG_SN_LOCAL"
    Accept @ 14,51 Width 06 "A3"  Picture "SIM,NAO" Tag "TG_SN_LOCAL" To VSDGLOCAL

    Pushbutton @ 16,17 width 11 "~O~k"      OK     Default Value "Ok" Tag "YES" To BottonValue
    Pushbutton @ 16,31 width 11 "~C~ancela" CANCEL                    Tag "NO"  To BottonValue

  EndDialog
  If Not RetVal Then QuitLoop EndIf
  If Not Usuario_Vendedor() Then Loop EndIf  

  DISPOSITIVO()
  If OK="" Then Loop EndIf

  AGUARDE("Aguarde, Gerando Relat¢rio")
  Switch
    Case VSDGOPDT=1: Seleciona2()
    Case VSDGOPDT=3: Seleciona3()
    Case VSDGOPDT=4: Seleciona4()
    OtherWise      : Seleciona1()
  EndSwitch
  
  If IsEmpty("Answer")
     Then MENSAGEM("N„o Existem Registros a Processar")
          Loop
  EndIf

  If VSDGOPRELCODALF = "2"
     Then Sort "Answer" On "Razao Social"
          DORDEM="Alfabetica"
     Else Sort "Answer" On "Codigo"
          DORDEM="de Codigo"
  EndIf
  ClearAll

  Switch
    Case VSDGOPRELLISTCAD="1":
         CopyReport "Destinat" "1"  "Answer" "R"
    Case VSDGOPRELLISTCAD="2":
         If VSDGCABECALHO="SIM"
            Then CopyReport "Destinat" "3"  "Answer" "R"
            Else If VSDGLOCAL="NAO"
                    Then CopyReport "\\Base\\Sistemas\\Cadastro\\Rc_0040" "1" "Answer" "R"
                    Else CopyReport "\\Base\\Sistemas\\Cadastro\\Rc_0040" "8" "Answer" "R"
                 EndIf
         EndIf
    Case VSDGOPRELLISTCAD="3":
         CopyReport "\\Base\\Sistemas\\Cadastro\\Rc_0040" "12" "Answer" "R"
  
    Case VSDGOPRELLISTCAD="4":
         CopyReport "\\Base\\Sistemas\\Cadastro\\Rc_0040b" "3" "Answer" "R" 
         
    Case VSDGOPRELLISTCAD="5":
         CopyReport "\\Base\\Sistemas\\Cadastro\\Rc_0040b" "9" "Answer" "R"   
  EndSwitch

  IMPRIREL("Answer","R")

  Delete "Answer"

EndWhile
ClearAll
Release Vars All
Release Procs All
