;** RP_0377 ********************************************************************
;*  Recibo de Recebimento Cliente / Contas a Receber                           *
;*******************************************************************************
;Autor           : Vise Informatica (VISERP)
;Desenvolvido    : 29/05/2017 por Rafael Matos
;Ultima Alteracao: 02/09/2017 por Bruno Carvalho 
;Motivo Alteracao: Controle PARAM_IMPRESSAO p/ impressÆo de processo individual ou quando ‚ chamado pelo RM_0500
;Ultima Alteracao: 03/06/2020 por Anivel Augusto Rodrigues
;Motivo Alteracao: Ajustes #0092 CGC         

;*******************************************************************************
;* Rotina para impressao do recibo de recebimento                           ****
;*****************+*************************************************************
Proc IMPRP()

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

If Not IsAssigned(Sys_Reset_Impressora)
   Then Sys_Reset_Impressora=""
EndIf

Obs()

Print File ARQIMP Sys_ReSet_Impressora
Print File ARQIMP CI_8LPP

If cliant<>[Duplic->Codigo do Destinatario]
   Then If cliant<>"****"
           Then Rodape()
        EndIf   
        cliant=[Duplic->Codigo do Destinatario]
        VR_RECIBO=0
        Cabecalho()
EndIf   
ITENS()

If ULTIMA_BAIXA 
   Then RODAPE()
EndIF

EndProc ;IMPRP()

;*******************************************************************************
;* Cabe‡alho                                                                   *
;*******************************************************************************
Proc CABECALHO()


Print File ARQIMP CI_FEXP
Print File ARQIMP CI_FNEG
Print File ARQIMP CI_10CPP
Print File ARQIMP CI_INEG
Print File ARQIMP Format("W4,AL",CODEMP) + Spaces(1) + Format("W55,AL",RAZAOSOCIALEMP) + Format("W19,AR",CNPJEMP)+"\n"
Print File ARQIMP Format ("W56,AL",ENDERECOEMP)+Format("W23,AR",CIDADEEMP)+"\n\n"
Print File ARQIMP CI_10CPP
Print File ARQIMP CI_IEXP
Print File ARQIMP CI_INEG

Print File ARQIMP "         RECIBO DE RECEBIMENTO"
Print File ARQIMP CI_15CPP
Print File ARQIMP CI_FEXP
Print File ARQIMP CI_FNEG+"\n"

Print File ARQIMP Spaces(108)+"EMISSAO: "+Format("D13",Today())+"-"+Time()+"\n"
Print File ARQIMP Fill(" ",136)+"\n"
Print File ARQIMP "NOME................: "+Format("W40,AL",[Duplic->Nome Fantasia EmitDest])

If IsAssigned(CGC)
   Then Print File ARQIMP spaces(05)+CGC
EndIf

If IsAssigned(RG)
   Then Print File ARQIMP spaces(05)+"RG: "+RG+"\n"
   Else Print File ARQIMP "\n" 
EndIf

Print File ARQIMP "ENDERE€O............: " + Format("W40,AL",ENDERECO)+Spaces(05)
Print File ARQIMP "BAIRRO..............: " + Format("W20,AL",BAIRRO)+Spaces(05)+"FONE: " + Format("W20,AL",TELEFONE)+Spaces(05)
Print File ARQIMP "CIDADE..............: " + Format("W20,AL",CIDADE)+"\n"
Print File ARQIMP "CENTRO DE CUSTO.....: " + [DupBaixa->Codigo CCusto]+Spaces(1)+Format("W30,AL",[DupBaixa->Descricao CCusto])+"\n\n"

ESCEXT([DupBaixa->Valor do Recebimento])
EXTENSO=" ("+SubStr(EXTENSO,2,97)+")"  
Print File ARQIMP "VALOR DO RECEBIMENTO: " + Format("W10.2,EIC",VR_RECEBIMENTO)+ Format("W100",SubStr(EXTENSO,1,100))+ "\n\n"

Print File ARQIMP "Referente...........: "+Format("W15,AL",[Duplic->Documento Referente])+"\n"

For i From 1 To Cont_Obs
  Print File ARQIMP SubStr(Observacao[i],4,Len(Observacao[i])) 
EndFor 
Print File ARQIMP "\n\n"

Print File ARQIMP Spaces(01)+"                                            -------------------------------------- Valores --------------------------------------\n"
Print File ARQIMP Spaces(01)+"          N§ Lancamento         Documento           Original            Juros/Multa               Desconto                  Total\n"
Print File ARQIMP Spaces(01)+"       ---------------- ----------------- ------------------ ---------------------- ---------------------- ----------------------\n"

EndProc ;CABECALHO()      xxxxxxxxx.xx
                                  
;*******************************************************************************
;* Impressao dos Itens                                                      *
;*******************************************************************************
Proc ITENS()

Print File ARQIMP CI_15CPP
Print File ARQIMP CI_FEXP
Print File ARQIMP CI_FNEG

Print File ARQIMP Spaces(18)+Format("W06,AL",[Duplic->Numero do Lancamento])
Print File ARQIMP Spaces(03)+Format("W04,AL",[Duplic->Tipo do Documento])
Print File ARQIMP Spaces(01)+Format("W10,AL",[Duplic->Numero do Documento])
Print File ARQIMP Spaces(06)+Format("W13.2,EIC",(VR_RECEBIMENTO+VR_DESCTO)-VR_JUROS)

Print File ARQIMP Spaces(11)+Format("W12.2,EIC",VR_JUROS)
Print File ARQIMP Spaces(11)+Format("W12.2,EIC",VR_DESCTO)
Print File ARQIMP Spaces(11)+Format("W12.2,EIC",VR_RECEBIMENTO)+"\n"

VR_RECIBO=VR_RECIBO+VR_RECEBIMENTO
        
EndProc ;ITENS()

;*******************************************************************************
;* Impressao dos Rodap‚                                                        *
;*******************************************************************************
Proc RODAPE()

Print File ARQIMP CI_15CPP
Print File ARQIMP CI_FEXP
Print File ARQIMP CI_FNEG

Print File ARQIMP Fill(" ",136)+"\n"

Print File ARQIMP Spaces(39)+Format("W19,AL","Data do Recebimento: ")
Print File ARQIMP Spaces(01)+Format("D13",DATA_PAGTO)
                                    
Print File ARQIMP Spaces(05)+"Valor Recebimento: "+LTrim(Format("W11.2,EIC",VR_RECIBO))+"\n\n\n\n"

Print File ARQIMP Format("W136,AC","   /   /     -                                                         ")+"\n"
Print File ARQIMP Format("W136,AC","-----------------------------------------------------------------------")+"\n"
Print File ARQIMP Format("W136,AC","Data e Assinatura")+"\n";Spaces(17)++"\n"

Print File ARQIMP Fill("=",136)
If Not ULTIMA_BAIXA
   THEN PRINT FILE ARQIMP "\N\N"
   ELSE If RECB_AUT
           Then If SALTA_PAGINA
                   Then Print File ARQIMP "\f"
                   Else Print File ARQIMP "\n\n"
                EndIf   
           Else PRINT FILE ARQIMP FILL("\N",20)
        EndIf
ENDIF

Print File ARQIMP CI_10CPP
Print File ARQIMP CI_6LPP

EndProc ;RODAPE()

;*******************************************************************************
;* Observa‡Æo                                                                  *
;*******************************************************************************
Proc Obs()  
Private OBS,QTD,LABEL_OBS,LINHA,Pos_Inicio,OBS_AUX,QTD_AUX,QTD_LINHA,wia

OBS        = [Duplic->Observacoes]
LABEL_OBS  = "Observacoes.......: "
LINHA      = 0   
     
Pos_Inicio=SearchFrom("\n",OBS,1) 
While True
 If Pos_Inicio > 0
    Then OBS_AUX   = SubStr(OBS,1,Pos_Inicio-1)
    Else OBS_AUX   = OBS
 EndIf   
 OBS       = SubStr(OBS,Pos_Inicio+1,Len(OBS)) 
 QTD_AUX   = Len(OBS_AUX)
 QTD_LINHA = Round(QTD_AUX/NUM_CARACTERES_OBS,0)                     
 If ((QTD_AUX/NUM_CARACTERES_OBS) - QTD_LINHA) > 0
    Then QTD_LINHA = QTD_LINHA + 1
 EndIf   
 For wia From 1 To QTD_LINHA 
   Cont_Obs = Cont_Obs + 1
   
   If LINHA = 0 
      Then LINHA = LABEL_OBS
      Else LINHA = Spaces(Len(LABEL_OBS))
   EndIf
   Observacao[Cont_Obs] = AlinhM(StrVal(Cont_Obs),3)+LINHA
   
   LINHA = Substr(OBS_AUX,((wia-1)*NUM_CARACTERES_OBS)+1,NUM_CARACTERES_OBS)
   Observacao[Cont_Obs] = Observacao[Cont_Obs]+"\27E"+LINHA+"\27F"+"\n"   
 EndFor
 If Pos_Inicio = 0 Then Quitloop EndIf
 Pos_Inicio=SearchFrom("\n",OBS,1)                
EndWhile  
LINHA = ""  
                               
EndProc ;Obs()
 
;*******************************************************************************
;* Rotina Principal                                                            *
;*****************+*************************************************************
If Not IsAssigned(RECB_AUT)         Then RECB_AUT=False       EndIf
If Not IsAssigned (SALTA_PAGINA)    Then SALTA_PAGINA = False EndIf
If Not IsAssigned (PARAM_IMPRESSAO) Then PARAM_IMPRESSAO = "" EndIf

Release Vars Observacao       
DynArray Observacao[]       
                                                          
NUM_CARACTERES_OBS = 111
LINHAS_OBS         = False
Cont_Obs           = 0

If PARAM_IMPRESSAO = ""
   Then cliant = "****"
        
        AGUARDE("Imprimindo o Recibo")
        ARQIMP=PrivDir()+"RP_0377.Txt"
        If IsFile(ARQIMP)
           Then Deleta_Arquivo(ARQIMP)
        EndIf
        
        CODEMP         = [Duplic->Codigo da Empresa]
        CODIGOEMP      = ""
        RAZAOSOCIALEMP = ""
        ENDERECOEMP    = ""
        CNPJEMP        = ""
                                
        GetRecord "\\Sistemas\\Cadastro\\Destinat" CODEMP to REG_EMP
        If Retval
           Then CODIGOEMP      = CODEMP
                RAZAOSOCIALEMP = REG_EMP["Razao Social"]
                ENDERECOEMP    = REG_EMP["Endereco"]+ ","+Spaces(1)+REG_EMP["Bairro"]
                CIDADEEMP      = REG_EMP["Cidade"] +"/"+REG_EMP["UF"]
                CNPJEMP        = REG_EMP["CGC"]                         
        EndIf         

        CODCLI = [Duplic->Codigo do Destinatario]
        ENDERECO=""
        BAIRRO  =""
        CIDADE  =""
        TELEFONE=""
        RG      =""
        
        GetRecord "\\Sistemas\\Cadastro\\Destinat" CODCLI to REG_CLI
        If RetVal
           Then ENDERECO = REG_CLI["Endereco" ]                
                BAIRRO   = REG_CLI["Bairro"   ]
                CIDADE   = REG_CLI["Cidade"   ]
                TELEFONE = REG_CLI["Telefone" ]
                RG       = REG_CLI["RG"       ]
        EndIf
        Release Vars REG_EMP,REG_CLI
        
        Moveto "DupBaixa"
        VR_RECEBIMENTO = [Valor do Recebimento]
        VR_DESCTO      = [Valor do Desconto   ]
        VR_JUROS       = [Valor do Juros      ]
        DATA_PAGTO     = [Data do Pagamento   ]     
     
        Moveto "Duplic"
        
   Else ;PARAM_IMPRESSAO = "RM_0500"
        AGUARDE("Imprimindo Recibo de Recebimento-> "+[Duplic->Numero do Lancamento])
        VR_RECEBIMENTO = [Valor do Recebimento]
        VR_DESCTO      = [Valor do Desconto   ]
        VR_JUROS       = [Valor do Juros      ]
        DATA_PAGTO     = [Data do Pagamento   ]        
EndIf
IMPRP()
LIMPAGUARDE()

Release Vars CODIGOEMP,CODEMP,RAZAOSOCIALEMP,ENDERECOEMP,CIDADEEMP,CNPJEMP,CODCLI,ENDERECO,
             BAIRRO,CIDADE,TELEFONE,RG,NUM_CARACTERES_OBS,LINHAS_OBS,Cont_Obs,PARAM_IMPRESSAO,
             VR_RECEBIMENTO,VR_DESCTO,VR_JUROS,DATA_PAGTO
Release Procs IMPRP, RODAPE, ITENS, CABECALHO, COMPLEMENTO 
