;** Rm_0111 ********************************************************************
;*  Relatorio de Clientes que nao compraram em um periodo                      *
;*******************************************************************************
;Autor           : Vise Informatica (VISERP)
;Desenvolvido    : 05/11/1997 por Anivel Augusto Rodrigues
;Ultima Alteracao: 10/05/2007 por Davi Carrano
;Motivo Alteracao: Inclusao da Opcao Endereco
;Ultima Alteracao: 10/02/2020 por Bruno Carvalho
;Motivo Alteracao: Ajustes de padroniza‡Æo nos campos [TG_CIDADE] e [TG_ESTADO]

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

{Ask} {\\Sistemas\\Cadastro\\FichaCli}
Moveto [Codigo                ] Example "COD"
Moveto [Data da Ultima Compra ] Check
Moveto [Valor da Ultima Compra] Check
{Ask} {\\Sistemas\\Cadastro\\Destinat}
Moveto [Codigo                 ] Check Example "COD" TypeIn "!"
Moveto [Tipo                   ] TypeIn "C"
Moveto [Razao Social           ] Check
Moveto [Endereco               ] Check
Moveto [Bairro                 ] Check
Moveto [Cep                    ] Check
Moveto [Cidade                 ] Check
If UPPER(VSDGCIDADE)<>"TODAS"
   Then TypeIn VSDGCIDADE
EndIf
Moveto [UF                     ] Check TypeIn "~VSDGESTADO.."
Moveto [Telefone               ] Check
Moveto [Contato                ] Check
Do_It!
ClearAll
EndProc   ; (Seleciona)

;*******************************************************************************
;* Rotina p/ Selecionar Vendas                                                 *
;*******************************************************************************
Proc Sel_Vendas()
ClearAll

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"
        Do_It!
        ClearAll
Endif
EndProc   ; (Sel_Vendas)

;*******************************************************************************
;* Rotina p/ Excluir registros                                               *
;*******************************************************************************
Proc Exclui_Registros()
ClearAll

{Ask} TypeIn tabela2 Enter
Moveto [Codigo] Example "cod"
{Ask} TypeIn tabela  Enter TypeIn "Fast Delete"
Moveto [Codigo] Example "cod"
Do_It!
ClearAll
EndProc   ; (Exclui_Registros)

;*******************************************************************************
;* Rotina                                                                      *
;*******************************************************************************
Proc SD_Dialog(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_DTINI"         : Campo="NO"
            Case Campo="TG_DTFIM"         : Campo="TG_DTINI"
            Case Campo="TG_CIDADE"        : Campo="TG_DTFIM"
            Case Campo="TG_ESTADO"        : Campo="TG_CIDADE"
            Case Campo="TG_OPRELCODALF"   : Campo="TG_ESTADO"
            Case Campo="YES" Or Campo="NO": Campo="TG_OPRELCODALF"
          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_DTINI"       : Campo="TG_DTFIM"
                    Case Campo="TG_DTFIM"       : Campo="TG_CIDADE"
                    Case Campo="TG_CIDADE"      : Campo="TG_ESTADO"
                    Case Campo="TG_ESTADO"      : Campo="TG_OPRELCODALF"
                    Case Campo="TG_OPRELCODALF" : Campo="YES"
                  EndSwitch
          EndIf
          RefreshDialog
   EndSwitch
EndIf
Switch
  Case Campo="TG_DTINI":
       MSG_CAMPO="Informe a Data Inicial do Periodo"
  Case Campo="TG_DTFIM":
       MSG_CAMPO="Informe a Data Final do Periodo"
EndSwitch
Monta_Prompt(MSG_CAMPO,TECLA)
Return Temp
EndProc

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

VSDGDTINI   =BlankDate()
VSDGDTFIM   =BlankDate()
VSDGCIDADE      =""
VSDGESTADO      ="" 
VSDGOPRELCODALF =""

While True

  ShowDialog "Rela‡„o de Clientes que n„o compram em um per¡odo" Proc "SD_Dialog"
  Trigger "OPEN","ARRIVE"
  Key "F1","ENTER","ESC","TAB",
      -104, -105, -106, -107, -108, -109, -110, -111, -112, -113, -139, -140  
   @ 05,09   Height 11 Width 60

    Label  @ 00,00 "Per¡odo Referente" For "TG_DTINI"
    Label  @ 01,00 "de" For "TG_DTINI"
    Accept @ 01,04 Width 13 "D"  Picture "#[#];.##;.##[##]" Tag "TG_DTINI" To VSDGDTINI
    Label  @ 01,17 "at‚" For "TG_DTFIM"
    Accept @ 01,22 Width 13 "D"  Picture "#[#];.##;.##[##]" Tag "TG_DTFIM" To VSDGDTFIM

    Label  @ 02,00 "Cidade"  For "TG_CIDADE"
    Accept @ 03,01 Width 28 "A20"  Picture "!*!" Tag "TG_CIDADE"  To VSDGCIDADE
                                   
    Label  @ 02,28 "Estado"  For "TG_ESTADO"
    Accept @ 03,29 Width 06 "A2"  Picture "!*!" Tag "TG_ESTADO" To VSDGESTADO

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

    Pushbutton @ 07,14 width 11 "~O~k"      OK     Default Value "Ok" Tag "YES" To BottonValue
    Pushbutton @ 07,28 width 11 "~C~ancela" CANCEL                    Tag "NO"  To BottonValue

  EndDialog
  If Not RetVal Then QuitLoop EndIf
  If IsBlank(VSDGDTFIM)
     Then Mensagem("Data Final Invalida !")
          Loop
  EndIf
  If VSDGDTFIM < VSDGDTINI
     Then Mensagem("Data Final deve ser maior que Data Inicial !")
          Loop
  EndIf

  DISPOSITIVO()
  If OK=""  Then Loop EndIf

  AGUARDE("Aguarde, gerando o relat¢rio")
  Tabela=Cria_Temp(PrivDir())
  Seleciona()
  Rename "Answer" Tabela

  Tabela2=Cria_Temp(PrivDir())

  Monta_Dir_Trab(VSDGDTINI,VSDGDTFIM)
  While Novo_Dir_Trab()
    If IsTable("Answer")     Then Delete "Answer" EndIf
    Sel_Vendas()
    If Not IsTable("Answer") Then Loop EndIf
    If IsTable(Tabela2)
       Then Add "Answer" Tabela2
       Else Rename "Answer" Tabela2
    EndIf
  EndWhile

  Exclui_Registros()
                 
  If IsEmpty(Tabela)
     Then MENSAGEM("N„o existem registros a processar")
          Delete Tabela
          Loop
  EndIf

  If VSDGOPRELCODALF="1"
     Then CopyReport "\\Base\\Sistemas\\Vendas\\Rm_0110" "6" Tabela "R"
     Else CopyReport "\\Base\\Sistemas\\Vendas\\Rm_0110" "7" Tabela "R"
  EndIf

  IMPRIREL(Tabela,"R")

  If IsTable("Answer")     Then Delete "Answer" EndIf
  If IsTable(Tabela)       Then Delete Tabela   EndIf
  If IsTable(Tabela2)      Then Delete Tabela2   EndIf

EndWhile
ClearAll
Release Vars All
Release Procs All
