;** RP_0000 ********************************************************************
;** Imprime a Controle Pagamento                          ** PADRAO **         *
;*******************************************************************************
;Autor           : Vise Informatica (VISERP)
;Desenvolvido  em: 02/12/2008 por Anivel Augusto Rodrigues
;Ultima Alteracao: 05/12/2008 por Anivel Augusto Rodrigues
;Motivo Alteracao: Ajustes CH NAO CADASTRADO
;Ultima Alteracao: 11/12/2008 por Anivel Augusto Rodrigues
;Motivo Alteracao: Ajustes TitCheq
;Ultima Alteracao: 01/12/2011 por Bruno Carvalho
;Motivo Alteracao: Altera‡Æo de "ExecVise -> Vexe","Dosprinter -> Vdpr"
;Ultima Alteracao: 28/06/2012 por Bruno Carvalho
;Motivo Alteracao: Adicionado Data da M‚dia dos Cheques
;Ultima Alteracao: 07/10/2019 por Bruno Carvalho
;Motivo Alteracao: Ajustes p/ nÆo verificar/impimir "TitCheq" quando #0388#   

;*******************************************************************************
;* Calculo da Data Media                                                       *
;*******************************************************************************
Proc CALC_MEDIA()
Private Tabela

Tabela=Table()

M_VALORM   =0
VALOR_TOTAL=0

VSDGDTREF=Today()

Moveto "TitCheq" 
Scan
  MDIA=[Data Vencimento]-VSDGDTREF
  If MDIA=0
     Then M_VALORM   =M_VALORM   + [Valor]
     Else M_VALORM   =M_VALORM   +([Valor]*MDIA)
  EndIf

  VALOR_TOTAL=VALOR_TOTAL+[Valor]
EndScan         

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

Moveto Tabela

EndProc  ; (Calcula)

;*******************************************************************************
;* Rotina para Impressao do Comprovante 																			 *
;*******************************************************************************
Proc IMPPAG()
Private TOTCH, NUMCH

;************************* IMPRESSAO DO CABECALHO *****************************
If Not IsAssigned(NOMEMPRESA)
   Then NOMEMPRESA=""
EndIf

Moveto "Titulos"
Print File ARQIMP CI_15CPP+"\n"
Print File ARQIMP "------------------------------------------------------------------------------------------------------------------------------------" + "\n"
Print File ARQIMP Format("W40,AL",NOMEMPRESA)+Spaces(15)+"Controle de Pagamento"+Spaces(26)+"Impressao: "+Format("D13",Today())+"-"+Time()+ "\n"
Print File ARQIMP "------------------------------------------------------------------------------------------------------------------------------------" + "\n\n"

Print File ARQIMP "     Pagto para: "
Print File ARQIMP Format("W55,AL",[Razao Social]+" - "+[Codigo do Emitente])
Print File ARQIMP "Lancamento : "+Format("W06,AL",[Numero do Lancamento])+"\n"

Print File ARQIMP "Referente Docto: "
Print File ARQIMP Format("W04,AL",[Tipo do Documento])+"/"
Print File ARQIMP Format("W14,AL",[Numero do Documento])
Print File ARQIMP "                "+Spaces(10)
Print File ARQIMP "              Empresa: "+[Codigo da Empresa]+" "+[Nome Fantasia Empresa]+"\n"

Print File ARQIMP "        Emissao: "
Print File ARQIMP Format("D13",[Data de Emissao])+Spaces(09)
Print File ARQIMP "Valor Original: "+Format("W10.2,EIC",[Valor do Documento])
Print File ARQIMP "       Plano de Conta: "+[Codigo Plano_Conta]+" "+[Descricao Plano_Conta]+"\n"

Print File ARQIMP "         Vencto: "
Print File ARQIMP Format("D13",[Data de Vencimento])+Spaces(09)
Print File ARQIMP "         Saldo: "+Format("W10.2,EIC",[Saldo do Documento])
Print File ARQIMP "      Centro de Custo: "+[Codigo CCusto]+" "+[Descricao CCusto]+"\n\n"

Print File ARQIMP "    Observacoes: "+[Observacoes]+"\n"

Moveto "TitBaixa"
Print File ARQIMP "\n"
Print File ARQIMP "** Baixas **"+"\n"
Print File ARQIMP "Seq  Dt Pagto  Tipo          Vr Pago  Vr Descto   Vr Juros Conta                                                                    "+"\n"
Print File ARQIMP "---- --------- ---------- ---------- ---------- ---------- -------------------------------------------------------------------------"+"\n"
Scan
  Print File ARQIMP Format("W04,AC",[Sequencia da Baixa])
  Print File ARQIMP Format("D13",[Data do Pagamento])+" "
  Print File ARQIMP Format("W11,AL",[Tipo da Baixa])
  Print File ARQIMP Format("W10.2,EIC",[Valor do Pagamento])
  Print File ARQIMP Format("W11.2,EIC",[Valor do Desconto ])
  Print File ARQIMP Format("W11.2,EIC",[Valor do Juros    ])+" "
  Print File ARQIMP Format("W05,AL",[Codigo da Conta   ])
  Print File ARQIMP Format("W31,AL",[Descricao da Conta   ])
  If Not IsBlank([Numero do Cheque])
     Then Print File ARQIMP "CH "+Format("W11,AL",[Numero do Cheque     ])
          If [Cheque Pre-Datado]="S"
             Then Print File ARQIMP "Pre-Datado "+Format("D13",[Data Cheque Pre-Datado])
          EndIf
  EndIf
  Print File ARQIMP "\n"
EndScan

If SYS_CODIGO_CLIENTE<>"0388" 
   Then Moveto "TitCheq"
        If RecordStatus("New")=False Then
           Print File ARQIMP "\n"
           Print File ARQIMP "** Cheques de Terceiros **"+"\n"
           Print File ARQIMP "Seq  Banco Cheque            Valor   Vencto   Titular                                                              Lancto CH/Terc   "+"\n"
           Print File ARQIMP "---- ----- ---------- ------------ ---------- -------------------------------------------------------------------- -----------------"+"\n"
           TOTCH=0
           NUMCH=0
           Scan
             TOTCH=TOTCH+[Valor]
             NUMCH=NUMCH+1
        
             Print File ARQIMP Format("W04,AR",[Sequencia])+" "
             Print File ARQIMP Format("W06,AL",[Codigo_Banco    ])
             Print File ARQIMP Format("W10,AL",[Numero_Cheque])
             Print File ARQIMP Format("W13.2,EIC",[Valor])+" "
             Print File ARQIMP Format("D13",[Data Vencimento])+" "
             Print File ARQIMP Format("W31,AL",SubStr([Titular_Cheque],1,30))
             Print File ARQIMP Format("W19,AL",[CPF])
             Print File ARQIMP Format("W19,AL",[UF]+"/"+SubStr([Cidade],1,15))
             If Not IsBlank([Numero Lancamento Cheque])
               Then Print File ARQIMP StrVal([Numero Lancamento Cheque])+"/"+StrVal([Sequencia do Cheque])
               Else Print File ARQIMP "CH NAO CADASTRADO"
             EndIF
             Print File ARQIMP "\n"
           EndScan
        
           Print File ARQIMP "\n"
           
           Print File ARQIMP "                       ------------"+"\n"
           Print File ARQIMP "            Total... "+Format("W13.2,EIC",TOTCH)+Format("W13,EIC",NDIAS)+
                             " Dias      Dt Media: "+Format("D13",DTMEDIA)+"\n"
           ;Print File ARQIMP "  "+Format("W04,AR",NUMCH)+" Cheque(s)"+"\n"
        EndIf
EndIf
Print File ARQIMP "\f"

Moveto "Titulos"
EndProc

;*******************************************************************************
;* Rotina Principal                                                            *
;*******************************************************************************
AGUARDE("Imprimindo Controle -> "+[Titulos->Numero do Lancamento])
ArqImp=PrivDir()+"RP_0000.Txt"
If IsFile(ArqImp)
   Then Deleta_Arquivo(ARQIMP)
EndIf
CI_15CPP="\15"

If SYS_CODIGO_CLIENTE<>"0388" 
   Then Moveto "TitCheq"
        If RecordStatus("New")=False
           Then CALC_MEDIA()
        EndIf   
EndIf     

Moveto "Titulos"

IMPPAG()
ExecVise("EXE c:\\vise\\Vdpr.exe /PDF /DEL "+ARQIMP)

Release Procs IMPPAG, Seta_FilePrint, Imprime_FilePrint
