;** Rm_0001*********************************************************************
;* Rela‡Æo de Contratos                                                        *
;*******************************************************************************
;Autor           : Vise Informatica (VISERP)
;Desenvolvido  em: 07/11/2016 por Rafael Matos 
;Ultima Alteracao: 28/12/2016 por Rafael Matos
;Motivo Alteracao: Ajustes ShowDialog TG_DESTINAT   
;Ultima Alteracao: 29/05/2018 por Anivel Augusto Rodrigues
;Motivo Alteracao: Adicionado ValidarModelosRelatorios()

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


{Ask} TypeIn "\\Sistemas\\Geec\\Contrato" Enter Check
 Moveto [Codigo da Empresa      ] TypeIn "~VSDGEMPRESA.."
 Moveto [Codigo                 ] TypeIn "~VSDGDESTINAT.."
 Moveto [Codigo_TpContrato      ] TypeIn "~VSDGTPCONTRATO.."
 Moveto [Codigo do Representante] TypeIn "~VSDGREPRESEN.."
 Switch
   Case VSDGTPDATA="1"    : Moveto [Data_Inclusao          ] TypeIn ">=~VSDGDTINI,<=~VSDGDTFIM"
   Case VSDGTPDATA="2"    : Moveto [VigenciaContratoInicial] TypeIn ">=~VSDGDTINI,<=~VSDGDTFIM"
   Case VSDGTPDATA="3"    : Moveto [PeriodoContratoInicial ] TypeIn ">=~VSDGDTINI,<=~VSDGDTFIM"
   Case VSDGTPDATA="4"    : Moveto [PeriodoContratoFinal   ] TypeIn ">=~VSDGDTINI,<=~VSDGDTFIM"
   Case VSDGTPDATA="5"    : Moveto [DataProspeccaoContrato ] TypeIn ">=~VSDGDTINI,<=~VSDGDTFIM"
 EndSwitch
 Moveto [Codigo_GrupoVencimento ] TypeIn "~VSDGGRPVENC.."
 Moveto [ContratoInativo] 
 Switch
    Case VSDGSTATUS="1" : TypeIn "N"  ;Somente Ativos
    Case VSDGSTATUS="2" : TypeIn "S"  ;Somente Inativos
 EndSwitch
 Do_It!          
 ClearAll
         
EndProc   ; (Seleciona)

;*******************************************************************************
;* Rotina 2 p/ Selecionar e Ordernar Registros                                   *
;*******************************************************************************
Proc Seleciona2()

ClearAll
If IsTable("\\Sistemas\\Geec\\"+Dir_Trab+"Contrato")
   Then
        {Ask} TypeIn "\\Sistemas\\Geec\\"+Dir_Trab+"Contrato" Enter Check
         Moveto [Codigo da Empresa      ] TypeIn "~VSDGEMPRESA"
         Moveto [Codigo                 ] Example "clie" TypeIn ", ~VSDGDESTINAT.." 
         Moveto [Codigo_TpContrato      ] TypeIn "~VSDGTPCONTRATO.."
         Moveto [Codigo do Representante] TypeIn "~VSDGREPRESEN.."
         Switch
           Case VSDGTPDATA="1"    : Moveto [Data_Inclusao          ] TypeIn ">=~VSDGDTINI,<=~VSDGDTFIM"
           Case VSDGTPDATA="2"    : Moveto [VigenciaContratoInicial] TypeIn ">=~VSDGDTINI,<=~VSDGDTFIM"
           Case VSDGTPDATA="3"    : Moveto [PeriodoContratoInicial ] TypeIn ">=~VSDGDTINI,<=~VSDGDTFIM"
           Case VSDGTPDATA="4"    : Moveto [PeriodoContratoFinal   ] TypeIn ">=~VSDGDTINI,<=~VSDGDTFIM"
           Case VSDGTPDATA="5"    : Moveto [DataProspeccaoContrato ] TypeIn ">=~VSDGDTINI,<=~VSDGDTFIM"
         EndSwitch
         Moveto [Codigo_GrupoVencimento ] TypeIn "~VSDGGRPVENC.."
         Moveto [ContratoInativo] 
         Switch
            Case VSDGSTATUS="1" : TypeIn "N"  ;Somente Ativos
            Case VSDGSTATUS="2" : TypeIn "S"  ;Somente Inativos
         EndSwitch 
         {Ask} {\\Sistemas\\Cadastro\\Destinat}
         Moveto [Codigo           ] Example "clie"
         Moveto [Bairro           ] Check
         Moveto [Cidade           ] Check
         Moveto [UF               ] Check
         Moveto [Codigo da Rota   ] Check
         Moveto [Descricao da Rota] Check 
         Do_It!          
         ClearAll
EndIf

EndProc   ; (Seleciona2)

;************************************
;* ROTINA PARA Ler/Gravacao Opcao   *
;************************************
Proc ModeloRelatoriosCarregaLista()

;ModeloRelatoriosAdd("XXX  XX maximo 61 XX")

ModeloRelatoriosAdd("Criar") ; linha obrigatoria inicial
ModeloRelatoriosAdd("01  Anal¡tico por Pessoa")
ModeloRelatoriosAdd("02  Anal¡tico por Tipo de Contrato")
ModeloRelatoriosAdd("03  Anal¡tico por Representante")
ModeloRelatoriosAdd("04  Anal¡tico por Grupo de Vencimento")
ModeloRelatoriosAdd("05  Anal¡tico por Rota")
ModeloRelatoriosAdd("Separator")
ModeloRelatoriosAdd("06  Sint‚tico por Grupo de Vencimento")
ModeloRelatoriosAdd("07  Sint‚tico por Tipo de Contrato")
ModeloRelatoriosAdd("Fim")
                           
EndProc ;ModeloRelatoriosCarregaLista

;*******************************************************************************
;* 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: 
          Switch
            Case Campo="TG_CODMODELO" : If ModeloRelatorios()
                                           Then VSDGCODMODELO=Rtrim(SubStr(Array_OpModeloRelatorios[VSDGOPMODELORELATORIOS],1,3))
                                                VSDGMODELO   =SubStr(Array_OpModeloRelatorios[VSDGOPMODELORELATORIOS],5,75)
                                        EndIf
          Otherwise : Dialog_F1()                              
          EndSwitch
     
     Case Evento["KeyCode"] =  27:
          Temp=False
          Switch
            Case Campo="TG_EMPRESA"           : Campo="NO"
            Case Campo="TG_DESTINAT"          : Campo="TG_EMPRESA"
            Case Campo="TG_TPCONTRATO"        : Campo="TG_DESTINAT"
            Case Campo="TG_REPRESEN"          : Campo="TG_TPCONTRATO"
            Case Campo="TG_GRPVENC"           : Campo="TG_REPRESEN"
            Case Campo="TG_TPDATA"            : Campo="TG_GRPVENC"
            Case Campo="TG_DTINI"             : Campo="TG_TPDATA"
            Case Campo="TG_DTFIM"             : Campo="TG_DTINI"
            Case Campo="TG_STATUS"            : Campo="TG_DTFIM"
            Case Campo="TG_CODMODELO"         : Campo="TG_STATUS"
            Case Campo="YES" Or Campo="NO": Campo="TG_EMPRESA"
          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_EMPRESA"     : Campo="TG_DESTINAT"
                    Case Campo="TG_DESTINAT"    : Campo="TG_TPCONTRATO"
                    Case Campo="TG_TPCONTRATO"  : Campo="TG_REPRESEN"   
                    Case Campo="TG_REPRESEN"    : Campo="TG_GRPVENC"
                    Case Campo="TG_GRPVENC"     : Campo="TG_TPDATA"
                    Case Campo="TG_TPDATA"      : Campo="TG_DTINI"
                       VSDGTPDATA=ControlValue("TG_TPDATA")
                       If IsBlank(VSDGTPDATA) Then VSDGTPDATA="1" EndIf
                       Switch
                         Case VSDGTPDATA="1"    : VSDGNOMTPDATA="INCLUSAO"
                         Case VSDGTPDATA="2"    : VSDGNOMTPDATA="VIGENCIA"
                         Case VSDGTPDATA="3"    : VSDGNOMTPDATA="INICIO CONTRATO"
                         Case VSDGTPDATA="4"    : VSDGNOMTPDATA="FIM CONTRATO"
                         Case VSDGTPDATA="5"    : VSDGNOMTPDATA="PROSPEC€ÇO"
                       EndSwitch                     
                    Case Campo="TG_DTINI"       : Campo="TG_DTFIM"                    
                    Case Campo="TG_DTFIM"       : Campo="TG_STATUS"
                    Case Campo="TG_STATUS"      : Campo="TG_CODMODELO"
                    Case Campo="TG_CODMODELO"   : Campo="YES"
              EndSwitch
          EndIf
          RefreshDialog
   EndSwitch
EndIf
Switch
  Case Campo="":
     MSG_CAMPO=""
  Case Campo="TG_TPDATA":
     MSG_CAMPO="1=InclusÆo  2=Vigˆncia  3=Inicio Contrato  4=Fim Contrato  5=Prospec‡Æo"   
Endswitch
Monta_Prompt(MSG_CAMPO,TECLA)
Return Temp
EndProc

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

VSDGDESTINAT =""
VSDGNOMDEST  =""
VSDGDTINI   =BlankDate()
VSDGDTFIM   =BlankDate()
VSDGTPCONTRATO =""
VSDGNOMTPCONTRATO =""
VSDGREPRESEN =""
VSDGNOMREPR =""
VSDGGRPVENC =""
VSDGNOMGRPVENC =""
VSDGSTATUS =""
VSDGNOMSTATUS =""
VSDGTPDATA =""
VSDGNOMTPDATA =""

ModeloRelatoriosCarregaLista()

While True

  ShowDialog "Rela‡Æo de Contratos" Proc "SD_Dialog"
  Trigger "OPEN","ARRIVE"
  Key "F1","ENTER","ESC","TAB",
      -104, -105, -106, -107, -108, -109, -110, -111, -112, -113, -139, -140  
   @ 05,12   Height 21 Width 72

    Label  @ 00,00 "Empresa"  For "TG_EMPRESA"
    Accept @ 01,01 Width 07 "A4"  Picture "[&,#][#*#]" Tag "TG_EMPRESA" To VSDGEMPRESA
    Accept @ 01,09 Width 25 "A20" Picture "!*!" Tag "TG_NOMEMP" To VSDGNOMEMP
    
    Label  @ 02,00 "Pessoa"  For "TG_DESTINAT"
    Accept @ 03,01 Width 07 "A4"  Picture "[&,#][#*#]" Tag "TG_DESTINAT" To VSDGDESTINAT
    Accept @ 03,09 Width 25 "A20" Picture "!*!" Tag "TG_NOMCLI" To VSDGNOMDEST

    Label  @ 04,00 "Tipo do Contrato"  For "TG_TPCONTRATO"
    Accept @ 05,01 Width 09 "A6"  Picture "[&,#][#*#]" Tag "TG_TPCONTRATO" To VSDGTPCONTRATO
    Accept @ 05,10 Width 24 "A20" Picture "!*!" Tag "TG_NOMTPCONTRATO"  To VSDGNOMTPCONTRATO
   
    Label  @ 06,00 "Representante"  For "TG_REPRESEN"
    Accept @ 07,01 Width 07 "A4"  Picture "[&,#][#*#]" Tag "TG_REPRESEN" To VSDGREPRESEN
    Accept @ 07,09 Width 25 "A20" Picture "!*!" Tag "TG_NOMREPR"  To VSDGNOMREPR
    
    Label  @ 08,00 "Grupo de Vencimento"  For "TG_GRPVENC"
    Accept @ 09,01 Width 07 "A4"  Picture "[&,#][#*#]" Tag "TG_GRPVENC" To VSDGGRPVENC
    Accept @ 09,09 Width 25 "A20" Picture "!*!" Tag "TG_NOMGRPVENC"  To VSDGNOMGRPVENC
    
    Label  @ 10,00 "Op‡äes de Datas" For "TG_TPDATA"
    Accept @ 11,04 Width 04 "A1"  Picture "1,2,3,4,5"  Tag "TG_TPDATA" To VSDGTPDATA
    Accept @ 11,09 Width 25 "A18" Picture "!*!"  Tag "TG_NOMTPDATA" To VSDGNOMTPDATA  
    
    Label  @ 12,00 "Per¡odo Referente de" For "TG_DTINI"
    Accept @ 13,03 Width 13 "D"  Picture "#[#];.##;.##[##]" Tag "TG_DTINI" To VSDGDTINI
    Label  @ 13,16 "at‚" For "TG_DTFIM"
    Accept @ 13,21 Width 13 "D"  Picture "#[#];.##;.##[##]" Tag "TG_DTFIM" To VSDGDTFIM
         
    
    Label  @ 00,35  "Status do Contrato" For "TG_STATUS"
    Accept @ 01,39 Width 04 "A1"  Picture "1,2,3" Tag "TG_STATUS" To VSDGSTATUS  
    Accept @ 01,44  Width 25 "A12" Picture "!*!" Tag "TG_NOMSTATUS" To VSDGNOMSTATUS


    Label  @ 14,00  "Modelo de ~R~elat¢rio"  For "TG_CODMODELO"
    Accept @ 15,01  Width 06 "A3"  Picture "!*!" Tag "TG_CODMODELO" To VSDGCODMODELO
    Accept @ 15,06  Width 63 "A64"  Picture "!*!" Tag "TG_MODELO" To VSDGMODELO  

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

  EndDialog
  If Not RetVal Then QuitLoop EndIf
  if not ValidarModelosRelatorios() then loop endif         
  If Not Usuario_Vendedor() Then Loop EndIf
  
  If IsBlank(VSDGDTFIM)
     Then Mensagem("Data Final Invalida !")
          Loop
  EndIf

  DISPOSITIVO()
  If OK=""  Then Loop EndIf

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

  Monta_Dir_Trab(VSDGDTINI,VSDGDTFIM)
  While Novo_Dir_Trab()
    If IsTable("Answer")     Then Delete "Answer" EndIf
    If VSDGCODMODELO="05"
      Then Seleciona2()
      Else Seleciona()
    EndIf
    If Not IsTable("Answer") Then Loop EndIf
    If IsTable(Tabela)
       Then Add "Answer" Tabela
       Else Rename "Answer" Tabela
    EndIf
  EndWhile

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

  Switch
    Case VSDGCODMODELO="01": Impressao_Orientacao="Paisagem" CopyReport "\\Base\\Sistemas\\Geec\\Rm_0001" "R" Tabela "R"
    Case VSDGCODMODELO="02": Impressao_Orientacao="Paisagem" CopyReport "\\Base\\Sistemas\\Geec\\Rm_0001" "1" Tabela "R"
    Case VSDGCODMODELO="03": Impressao_Orientacao="Paisagem" CopyReport "\\Base\\Sistemas\\Geec\\Rm_0001" "2" Tabela "R"
    Case VSDGCODMODELO="04": Impressao_Orientacao="Paisagem" CopyReport "\\Base\\Sistemas\\Geec\\Rm_0001" "3" Tabela "R"
    Case VSDGCODMODELO="05": Impressao_Orientacao="Paisagem" CopyReport "\\Base\\Sistemas\\Geec\\Rm_0002" "R" Tabela "R"
    Case VSDGCODMODELO="06": Impressao_Orientacao="Retrato"  CopyReport "\\Base\\Sistemas\\Geec\\Rm_0001" "4" Tabela "R"
    Case VSDGCODMODELO="07": Impressao_Orientacao="Retrato"  CopyReport "\\Base\\Sistemas\\Geec\\Rm_0001" "5" Tabela "R"
  EndSwitch

  IMPRIREL(Tabela,"R")

  Delete Tabela

EndWhile
ClearAll
Release Vars All
Release Procs All

