;** Rm_0200  *******************************************************************
;*  Emissao de Cheques/Prazo Medio                                             *
;*******************************************************************************
;Autor           : Vise Informatica (VISERP)
;Desenvolvido    : 06/05/2003 por Anivel Augusto Rodrigues
;Ultima Alteracao: 11/06/2003 por Anivel Augusto Rodrigues
;Motivo Alteracao: Ajustes
;Ultima Alteracao: 09/09/2016 por Anivel Augusto Rodrigues
;Motivo Alteracao: Ajustes showdialog                   
;Ultima Alteracao: 26/12/2016 por Anivel Augusto Rodrigues
;Motivo Alteracao: Ajustes showdialog                   

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

{Ask} {ChequeC} Check

if VSDGNCHT>0
   then Moveto [Numero do Lancamento] Example "NUM" TypeIn ", ~VSDGNCHT"
   else Moveto [Numero do Lancamento] Example "NUM" 
endif
Moveto [Codigo da Empresa   ] TypeIn  "~VSDGEMPRESA.."

{Ask} {ChequeI} Check
Moveto [Numero do Lancamento] Example "NUM"
Do_It!
ClearAll

EndProc   ; (Seleciona)

;*******************************************************************************
;* Calculo da Data Media                                                       *
;*******************************************************************************
Proc CALC_MEDIA()
M_VALORM   =0
VALOR_TOTAL=0

CoEdit "Answer"
Scan
  MDIA=[Data Vencimento]-VSDGDTREF
  If MDIA=0
     Then M_VALORM   =M_VALORM   + [Valor]
     Else M_VALORM   =M_VALORM   +([Valor]*MDIA)
  EndIf
  [Situacao]=StrVal(MDIA)

  VALOR_TOTAL=VALOR_TOTAL+[Valor]
EndScan
Do_It!
ClearAll
M_DIAREF     = Round( ( M_VALORM / VALOR_TOTAL ), 3 )
M_DIAREFI    = Int( M_DIAREF )
M_DIAREFD    = M_DIAREF - M_DIAREFI
If M_DIAREFD > 0.51
   Then M_DIAREFI = M_DIAREFI + 1
EndIf

DTMEDIA=VSDGDTREF+M_DIAREFI
NDIAS  =M_DIAREFI
EndProc  ; (Calcula)

;*******************************************************************************
;* Rotina                                                                      *
;*******************************************************************************
Proc SD_Dialog(Disparo,Campo,Evento,Elemento)
Temp=True
TECLA=""
MSG_CAMPO=""
If Disparo="OPEN"  Then AoCriarShowDialog() EndIf
If Disparo="EVENT" And Evento["Type"]="KEY" Then
   Switch
     Case Evento["KeyCode"] = -59: Dialog_F1()
     Case Evento["KeyCode"] =  27:
          Temp=False
          Switch
            Case Campo="TG_EMPRESA"       : Campo="NO"
            Case Campo="TG_NCHT"          : Campo="TG_EMPRESA"
            Case Campo="TG_DTREF"         : Campo="TG_NCHT"
            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_NCHT"
                    Case Campo="TG_NCHT"       : Campo="TG_DTREF"
                    Case Campo="TG_DTREF"      : Campo="YES"
                  EndSwitch
          EndIf
          RefreshDialog
   EndSwitch
EndIf
Switch
  Case Campo="":
       MSG_CAMPO=""
       
Endswitch
Monta_Prompt(MSG_CAMPO,TECLA)
Return Temp
EndProc   ; (SD_Dialog)

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

VSDGEMPRESA=""
VSDGNCHT   =BlankNum()
VSDGDTREF  =BlankDate()

While True
  ShowDialog "Rela‡„o de Cheques/Lan‡to/Prazo Medio" Proc "SD_Dialog"
  Trigger "OPEN","ARRIVE"
  Key "F1","ENTER","ESC",
      -104, -105, -106, -107, -108, -109, -110, -111, -112, -113, -139, -140  
   @ 05,12   Height 11 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 "Lan‡amento" For "TG_NCHT"
    Accept @ 03,01 Width 13 "N"  Picture "#[#####]" Tag "TG_NCHT" To VSDGNCHT
    Accept @ 03,09 Width 25 "A22" Picture "!*!" Tag "TG_NOMNCHT"  To VSDGNOMNCHT

    Label  @ 04,00 "Data Base" For "TG_DTREF"
    Accept @ 05,01 Width 13 "D"  Picture "#[#];.##;.##[##]" Tag "TG_DTREF" To VSDGDTREF

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

  EndDialog
  If Not RetVal Then QuitLoop EndIf
  If Not Valida_ShowDialog("") Then Loop EndIf

  DISPOSITIVO()
  If OK="" Then Loop EndIf
  AGUARDE("Aguarde, Gerando o Relat¢rio")
  SELECIONA()

  If IsEmpty("Answer")
     Then MENSAGEM("N„o existem registros a processar")
          Delete "Answer"
          Loop
  EndIf
  CALC_MEDIA()
  CopyReport "\\Base\\Sistemas\\CheqRec\\Rm_0010" "14" "Answer" "R"

  IMPRIREL("Answer","R")

  Delete "Answer"

EndWhile
ClearAll
Release Vars All
Release Procs All
