;** RO_0397 ********************************************************************
;** Imprime a Digitacao do Romaneio                             ** TECIPAM **  *
;*******************************************************************************
;Autor           : Vise Informatica (VISERP)
;Desenvolvido  em: 05/09/2017 por Bruno Carvalho           
;Ultima Altera‡Æo: 11/09/2017 por Bruno Carvalho 
;Motivo Altera‡Æo: Ajustes
;Ultima Altera‡Æo: 28/11/2017 por Bruno Carvalho 
;Motivo Altera‡Æo: Adicionado Data de EmissÆo
;Ultima Altera‡Æo: 08/03/2018 por Bruno Carvalho 
;Motivo Altera‡Æo: Ajustes obs
;Ultima Altera‡Æo: 04/04/2018 por Anivel Augusto Rodrigues
;Motivo Altera‡Æo: Ajustes usando fonte 10cpp
;Ultima Altera‡Æo: 10/04/2018 por Anivel Augusto Rodrigues
;Motivo Altera‡Æo: Ajustes nova fonte 10cpp e caracteres


;*******************************************************************************
;* ImpressÆo do Cabe‡alho                                                      *
;*******************************************************************************
Proc Cabecalho()

Moveto "Faturac"

Print File ARQIMP chr(27)+"k1"

Print File ARQIMP CI_10CPP+CI_6LPP

Print File ARQIMP Spaces(2)+Format("W47,AL","CONTROLE INTERNO")+"Impressao: "+Format("D10",Today())+"-"+Time()+ "\n\n"

Print File ARQIMP CI_IEXP+CI_INEG+Spaces(20)+"Orcamento N§ "+Format("W06.3,AL",NOTAFISC)+CI_FEXP+CI_FNEG+"\n\n"

Print File ARQIMP Spaces(2)+"Cliente : "+CI_INEG+Format("W05,AL",[Codigo])+Format("W34,AL",[Razao Social])+CI_FNEG+
                  Spaces(1)+Format("D13",[Data de Emissao])+"\n"

Print File ARQIMP Spaces(2)+Spaces(10)+Format("W39,AL",[Endereco])+Spaces(1)
Print File ARQIMP Format("W18,AL",[CGC])+"\n"

Print File ARQIMP Spaces(2)+Spaces(10)+Format("W20,AL",[Bairro])
Print File ARQIMP Format("W20,AL",[CEP])
Print File ARQIMP Format("W25,AL",[IE])+"\n"

Print File ARQIMP Spaces(2)+Spaces(10)+Format("W20,AL",[Cidade])
Print File ARQIMP Format("W20,AL",[UF])
Print File ARQIMP Format("W25,AL",[Telefone])+"\n"

Print File ARQIMP Spaces(2)+"Operacao: "              
Print File ARQIMP CI_INEG+Format("W38",[Codigo da Destinacao]+" "+[Descricao da Destinacao])+CI_FNEG
Print File ARQIMP Spaces(2)+"SEMANA: "+Format("W9",AREA_DESCRICAO)+"\n"

Print File ARQIMP Spaces(2)+"Vendedor: "+Format("W05,AL",[Codigo do Representante])
Print File ARQIMP Format("W25,AL",[Nome Fantasia Represent])+"\n"

;********************* IMPRESSAO DOS ITENS ********************************
Print File ARQIMP Spaces(2)+Fill(Chr(196),78)+"\n"
Print File ARQIMP Spaces(2)+"   Item                                      Un  Quantidade Unitario     Total"+"\n"
Print File ARQIMP Spaces(2)+Fill(Chr(196),78)+"\n"

CONTADOR   =13
endproc

;*******************************************************************************
;* Rotina para Impressao do Relatorio                                          *
;*******************************************************************************
Proc IMPFAT()

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

AREA_DESCRICAO = ""
GetRecord "\\Sistemas\\Cadastro\\Destinat" [FaturaC->Codigo] To REG_DESTINAT
If RetVal
   Then AREA_DESCRICAO = REG_DESTINAT["Descricao da Area"]
EnDIf   

Cabecalho()

TM_DESCRICAO_ITEM = 30

Moveto "FaturaI"
Moveto [Numero da OF]
Locate [FaturaC->Numero da OF]
While [Numero da OF]=[FaturaC->Numero da OF]
  desccompl = ""
  GetRecord "\\Sistemas\\Estoque\\Itens" [Codigo do Item] To REG_ITEM
  If Retval
     Then desccompl = REG_ITEM["Descricao Complementar"]
  EndIf
  If Rtrim(desccompl) = "" Then desccompl = [Descricao do Item] EndIf     
  
  nl=len(desccompl)/TM_DESCRICAO_ITEM
  if (len(desccompl)/TM_DESCRICAO_ITEM) > int(nl) or nl=0
     then nl=int(nl)+1
  endif    

  posi=1
  s=Format("W04,AR",[Item da OF])+
    Spaces(01)+Format("W11,AL",[Codigo do Item])

  Checa_Linha(nl)
  for ii from 1 to nl
    s=s+Format("W30,AL",Substr(desccompl,posi,TM_DESCRICAO_ITEM))
    
    posi=posi+TM_DESCRICAO_ITEM
    if Substr(desccompl,posi,TM_DESCRICAO_ITEM)<>""
     then Print File ARQIMP s+"\n"
          s=Spaces(16)
    endif      
     
  endfor
  Print File ARQIMP s
  Print File ARQIMP Spaces(01)+Format("W03,Al",[Unidade Fiscal])
  If [Quantidade]-Int([Quantidade])>0
     Then Print File ARQIMP Format("W11.3,EIC",[Quantidade])
     Else Print File ARQIMP Format("W07,EIC",[Quantidade])+spaces(04)     
  EndIf
  Print File ARQIMP Format("W09.2,EIC",[Preco Unitario])
  Print File ARQIMP Format("W10.2,EIC",[Preco Total])+"\n"                  

  Skip
  If EOT() Then QuitLoop EndIf
EndWhile

Rodape()
EndProc

;*******************************************************************************
;* ImpressÆo do Rodap‚                                                         *
;*******************************************************************************
Proc Rodape()
Moveto "FaturaC"

Checa_Linha(8)
Print File ARQIMP Spaces(2)+Fill(Chr(196),78)+"\n"

Print File ARQIMP Spaces(2)+Spaces(51)+"Total............"+Format("W10.2,EIC",[Valor Mercadoria])+"\n"

Print File ARQIMP Spaces(2)+"** FAVOR MANTER ENDERECO/TELEFONE ATUALIZADOS **"
Print File ARQIMP Spaces(03)+"Desconto"+Format("W06.2,EIC",[Taxa de Desconto]) + "% ."
Print File ARQIMP Format("W10.2,EIC",[Valor Desconto])+"\n"

Print File ARQIMP Spaces(2)+Spaces(51)+"DespAces........."+Format("W10.2,EIC",[Valor Despesas Acessorias])+"\n"
Print File ARQIMP Spaces(2)+Spaces(51)+"Total Liquido...."+Format("W10.2,EIC",[Valor nota Fiscal])+"\n\n"

Print File ARQIMP Spaces(2)+"Cond. Pagto: "+[Descricao CondPagto]
Print File ARQIMP "   NP: "+StrVal([Numero de Parcelas])
Print File ARQIMP "   CD: "+[Composicao de Dias]
Print File ARQIMP "  VOL: "+StrVal([Numero de Volumes])+"\n\n"

imprimeobs()

;********************* IMPRESSAO DOS VENCIMENTOS **************************
Print File ARQIMP "\020"
Print File ARQIMP "\027E"
Print File ARQIMP Spaces(2)+"Vencimentos: "

Array DATA [9]
Array VALOR[9]
Array FPGT [9]
K=0
KV=0
Moveto "FaturaV"
Moveto [Numero da OF]
Locate [FaturaC->Numero da OF]
While [Numero da OF]=[FaturaC->Numero da OF]
  K=K+1
  DATA[K]  = Format("D13",[Data])
  VALOR[K] = Format("W10.2,AR,EIC",[Valor])
  FPGT[K]  = Format("W10","FP: "+[Forma de Pagamento])
  If K=5
     Then VENC()
          K=0
  EndIf
  Skip
  If EOT() Then QuitLoop EndIf
EndWhile
If K>0
   Then VENC()
EndIf
Print File ARQIMP "\027F"

;************************* IMPRESSAO DO RODAPE *********************************
Checa_Linha(5)

Print File ARQIMP "\n\n"
Print File ARQIMP Format("W80,AC","   /   /     -                                   ")+ "\n"
Print File ARQIMP Format("W80,AC",Fill(Chr(196),50))+ "\n"
Print File ARQIMP Format("W80,AC","Data e Assinatura")
Print File ARQIMP CI_6LPP
Print File ARQIMP CI_10CPP
Print File ARQIMP "\f"

endproc


;*******************************************************************************
;* Rotina de Impressao de Observa‡äes                                          *
;*******************************************************************************
proc imprimeobs()
private TamObs, nlobs, posobs, i, s

TamObs=78

s="Observacoes: "+Tira_QuebraLinha([Observacao na Nota Fiscal])

nlobs=Len(s)/tamobs
If nlobs-Int(nlobs)>0
   Then nlobs=Int(nlobs)+1
   Else nlobs=Int(nlobs)
EndIf

Checa_Linha(nlobs+1)

posobs=1
For i from 1 to nlobs
  Print File ARQIMP CI_INEG+spaces(2)+SubStr(s,posobs,tamobs)+CI_FNEG+"\n"
  posobs=posobs+tamobs
EndFor
Print File ARQIMP "\n"

endproc

;*******************************************************************************
;* Rotina de Impressao dos Vencimentos                                         *
;*******************************************************************************
Proc VENC()
Checa_Linha(4)

If KV>0
   Then Print File ARQIMP Spaces(15)
EndIf
Print File ARQIMP Spaces(6)
For I From 1 to K
  Print File ARQIMP FPGT[I]+Spaces(2)
EndFor
Print File ARQIMP "\n"

Print File ARQIMP Spaces(17)
For I From 1 to K
  Print File ARQIMP DATA[I]+Spaces(02)
EndFor
Print File ARQIMP "\n"

Print File ARQIMP Spaces(17)
For I From 1 to K
  Print File ARQIMP VALOR[I]+Spaces(02)
EndFor
Print File ARQIMP "\n\n"

KV=KV+1
EndProc

;*******************************************************************************
;* Checa linha na area de impressao dos itens                                  *
;*******************************************************************************
Proc Checa_Linha(nl)
private nl, ntab

ntab=table()

If (CONTADOR+nl)>64
   Then 
        Print File ARQIMP Format("W80,AR","*** CONTINUA NA PROXIMA FOLHA ***")+"\f"
        Cabecalho()
EndIf
CONTADOR=CONTADOR+nl

moveto ntab
EndProc

;*******************************************************************************
;* Rotina Principal                                                            *
;*******************************************************************************

ArqImp=PrivDir()+"RO_0397.Txt"
If IsFile(ArqImp)
   Then Deleta_Arquivo(ARQIMP)
EndIf

AGUARDE("Imprimindo Romaneio -> "+NOTAFISC)
IMPFAT()

;---- Imprime Arquivo Impressao ------------------------------------------------
ImpriArq(ARQIMP)

Release Vars VALOR,DATA,CONTADOR
Release Procs IMPFAT,VENC,imprimeobs,Checa_Linha,Cabecalho,Rodape 
LIMPAGUARDE()


