;** RO_0304 ********************************************************************
;** Imprime a Digitacao do Romaneio                               Marron Caf‚  *
;*******************************************************************************
;Autor: Visao/Vise
;Desenvolvido  em: 24/01/2012 por Bruno Carvalho
;Alterado em     :   /  /     por 
;Motivo Alteracao: 

;*******************************************************************************
;* Rotina para Impressao do Relatorio . Cabe‡alho                              *
;*******************************************************************************
Proc Cabecalho()

;************************** IMPRESSAO DO CABECALHO *****************************
If Not IsAssigned(NOMEMPRESA)
   Then NOMEMPRESA=""
EndIf
Moveto "Faturac"
Print File ARQIMP CI_6LPP
Print File ARQIMP CI_10CPP
Print File ARQIMP "\015"

Print File ARQIMP "-----------------------------------------------------------------------------------------------------------------------------------" + "\n"
Print File ARQIMP "Marron Cafe"+Spaces(91)+"Emissao: "+Format("D13",Today())+"-"+Time()+"\n"
Print File ARQIMP "----------------------------------------------------------------------------------------------------------------------------------" + "\n\n"

Print File ARQIMP Spaces(02)+"Operacao......: "+Format("W25,AL",[Descricao da Destinacao])+Spaces(24)+"\027E"+"Romaneio: "+
                                                Format("W06.3,AL",NOTAFISC)+"\027F"+"\n"
Print File ARQIMP Spaces(02)+"Pedido........: "+Format("W06.3,AL",[Numero do Pedido])+"\n"
Print File ARQIMP Spaces(02)+"Carregamento..: "+Format("W06.3,AL",[Numero_Carregamento])+"\n"
Print File ARQIMP Spaces(02)+"Cliente.......: "+Format("W05,AL",[Codigo])+Format("W46,AL",[Razao Social])+"\n"
Print File ARQIMP Spaces(02)+"Local Fatura..: "+Format("W33,AL",[Endereco])+Spaces(02)+Format("W20,AL",[Bairro])+"\n"
Print File ARQIMP Spaces(18)+Format("W10,AL",[CEP])+Spaces(02)+Rtrim(Format("W20,AL",[Cidade]))+" - "+Format("W04,AL",[UF])+"\n"
Print File ARQIMP Spaces(18)+Format("W18,AL",[CGC])+Spaces(02)+Rtrim(Format("W16,AL",[IE]))+Spaces(02)+"TEL:"+Format("W15,AL",[Telefone])+"\n\n"

Print File ARQIMP Spaces(02)+"Dt Emissao Dt Saida   Volumes  Representante "+ "\n"
Print File ARQIMP Spaces(02)+"---------- --------   -------  ------------------------"+"\n"

Print File ARQIMP Spaces(01)+Format("D13",[Data de Emissao])
Print File ARQIMP Spaces(01)+Format("D13",[Data de Saida])
Print File ARQIMP Spaces(01)+Format("W07,AR",[Numero de Volumes])+Spaces(02)
Print File ARQIMP Spaces(01)+Format("W04,AL",[Codigo do Representante])
Print File ARQIMP Spaces(01)+Format("W25,AL",[Nome Fantasia Represent])+CI_15CPP+"\n\n"

Print File ARQIMP "\015"         ;Imprime 17 Caracteres por polegada

Moveto "FaturaC"
;********************* IMPRESSAO DOS ITENS ********************************
Print File ARQIMP "----------------------------------------------------------------------------------------------------------------------------------"+"\n"
Print File ARQIMP "Item  Codigo      Ref  Descricao                                                       Un      Quantidade   Unitario         Total"+"\n"
Print File ARQIMP "----------------------------------------------------------------------------------------------------------------------------------"+"\n"
                                  
EndProc ;(Cabecalho)
                
;*******************************************************************************
;* Rotina para Impressao do Relatorio . Itens                                  *
;*******************************************************************************
Proc Itens()
Private REFERENCIA

Print File ARQIMP CI_6LPP
Moveto "FaturaI"
Moveto [Numero da OF]
Locate [FaturaC->Numero da OF]
QUANT    = 0
TOT      = 0
CONTADOR = 0

While [Numero da OF]=[FaturaC->Numero da OF]
  Checa_Linha(1)
  
 ; MoveTo [Codigo do Item]
 ; DESC_COMP=""
 ; If busca_Lookup("Codigo do Item",[FaturaI->Codigo do Item],"SM")
 ;    Then DESC_COMP = REG_BUSCA_LOOKUP["Descricao Complementar"]
 ; EndIf
 ; KeyPress "Esc"
 
  MoveTo [Codigo do Item]
  REFERENCIA=""
  If busca_Lookup("Codigo do Item",[FaturaI->Codigo do Item],"SM")
     Then REFERENCIA = REG_BUSCA_LOOKUP["Referencia"]
  EndIf
  KeyPress "Esc" 
                        
  LINHA_ITEM = Spaces(02)+ Format("W03,AR",StrVal([Item da OF]))
  LINHA_ITEM = LINHA_ITEM + Spaces(01) + Format("W11,AL",[Codigo do Item])
  LINHA_ITEM = LINHA_ITEM + Spaces(01) + Format("W04,AL",REFERENCIA)
 ; LINHA_ITEM = LINHA_ITEM + Spaces(01) + Format("W62,AL",DESC_COMP)
  LINHA_ITEM = LINHA_ITEM + Spaces(01) + Format("W62,AL",[Descricao do Item])
  LINHA_ITEM = LINHA_ITEM + Spaces(02) + Format("W04,Al",[Unidade Fiscal])
  LINHA_ITEM = LINHA_ITEM + Spaces(01) + Format("W13.3,EIC",[Quantidade])
  LINHA_ITEM = LINHA_ITEM + Spaces(01) + Format("W10.2,EIC",[Preco Unitario])
  LINHA_ITEM = LINHA_ITEM + Spaces(01) + Format("W13.2,EIC",[Preco Total])
  
  Print File ARQIMP LINHA_ITEM + "\n"
  
  QUANT = QUANT + [Quantidade ]
  TOT   = TOT   + [Preco Total]
  Skip
  If EOT() Then QuitLoop EndIf
EndWhile                          
              
For ITEM From CONTADOR To PARAM_NITENS
    Print File ARQIMP "\n"
EndFor

EndProc ;(Itens)

;*******************************************************************************
;* Rotina para Impressao do Relatorio . Rodap‚                                 *
;*******************************************************************************
Proc Rodape(QUEBRAFORM)
Private QUEBRAFORM
Moveto "FaturaC"

Print File ARQIMP "----------------------------------------------------------------------------------------------------------------------------------"+"\n"

If QUEBRAFORM                    
   Then Print File ARQIMP Spaces(95)+"**********"+Spaces(15)+"**********"+"\n" 
   Else Print File ARQIMP Spaces(92)+Format("W13.3,EIC",QUANT)+Spaces(12)+Format("W13.2,EIC",[Valor Mercadoria])+"\n"
EndIf

;********************** VALORES *************************

If QUEBRAFORM
   Then Print File ARQIMP Spaces(95)+"Desconto..: "+"  ****  "+"% "+"*************"+"\n"
        Print File ARQIMP Spaces(95)+"Frete.............: "+Spaces(2)+"*************"+"\n"
        Print File ARQIMP Spaces(95)+"Total Liquido.....: "+Spaces(2)+"*************"+"\n"
   Else Print File ARQIMP Spaces(95)+"Desconto..: "+Format("W8.2,EIC",[Taxa de Desconto])+"% "+Format("W13.2,EIC",[Valor Desconto])+"\n"
        Print File ARQIMP Spaces(95)+"Frete.............: "+Spaces(2)+Format("W13.2,EIC",[Valor do Frete])+"\n"
        Print File ARQIMP Spaces(95)+"Total Liquido.....: "+Spaces(2)+Format("W13.2,EIC",[Valor Nota Fiscal])+"\n"
EndIf

;******************** OBSERVACAO ************************
Moveto "FaturaC"                    
If SubStr([Observacao na Nota Fiscal],001,115)<>""
   Then Print File ARQIMP "Observacao: "+Format("W115,AL",SubStr([Observacao na Nota Fiscal],001,115))+"\n"
   Else Print File ARQIMP "\n"
EndIf

;******************* INFORMACOES ***********************
Print File ARQIMP "CondPago..: "+Format("W04,AR",[FaturaC->Condicao de Pagamento])+" - "+Format("W20,AL",[FaturaC->Descricao CondPagto])+"\n\n"
;Print File ARQIMP "Volumes...: "+Format("W13",[Numero de Volumes])+"\n"
;Print File ARQIMP "Peso......: "+Format("W13.3,EIC",[Peso Liquido])+"\n\n"

;********************* IMPRESSAO DOS VENCIMENTOS **************************
Print File ARQIMP "\018"
Print File ARQIMP "\027E"
Print File ARQIMP "Vencimentos: "
 
Array DATA[5]
Array VALOR[5]
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])
  If K=5
     Then VENC()
          K=0
  EndIf
  Skip
  If EOT() Then QuitLoop EndIf
EndWhile
If K>0
   Then VENC()
EndIf
;Print File ARQIMP "\n"
Print File ARQIMP "\027F"

;******************* INFORMACOES ***********************
Print File ARQIMP "\015"
;Print File ARQIMP Spaces(28)+"FA€A O PAGAMENTO NOMINAL E CRUZADO E ENVIE PARA O ENDERECO DA NOTA"

;***************** IMPRESSAO DO RODAPE ******************
If QUEBRAFORM
   Then Print File ARQIMP "\n\n\n" 
   Else Print File ARQIMP Spaces(40)+"   /   /     -                                   " + "\n"
        Print File ARQIMP Spaces(40)+"-------------------------------------------------" + "\n"
        Print File ARQIMP Spaces(40)+"              Data e Assinatura"+"\n" 
EndIf        
Print File ARQIMP CI_6LPP
Print File ARQIMP CI_10CPP
Print File ARQIMP "\f"
                   
EndProc ;(Rodape)

;*******************************************************************************
;* Rotina de Impressao dos Vencimentos                                         *
;*******************************************************************************
Proc VENC()
If KV>0
   Then Print File ARQIMP Spaces(13)
EndIf
For I From 1 to K
  Print File ARQIMP DATA[I]+Spaces(02)
EndFor
Print File ARQIMP "\n"
Print File ARQIMP Spaces(13)
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

If (CONTADOR+nl)>(PARAM_NITENS)
   Then RodaPe(True)
        Cabecalho()
        CONTADOR=nl
        Moveto "FaturaI"
   Else CONTADOR=CONTADOR+nl
EndIf
EndProc

;*******************************************************************************
;* Rotina Principal                                                            *
;*******************************************************************************
ArqImp=PrivDir()+"Ro_0304.Txt"
If IsFile(ArqImp)
   Then Deleta_Arquivo(ARQIMP)
EndIf

PARAM_NITENS=28            
Moveto "Faturac"

;If Not IsBlank([Endereco de Entrega]) And [Endereco de Entrega]<>"O MESMO"
;   Then PARAM_NITENS=PARAM_NITENS-3
;EndIf        

AGUARDE("Imprimindo Romaneio -> "+NOTAFISC)
Cabecalho()
Itens()
Rodape(False) 

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

Release Vars VALOR,DATA
Release Procs IMPFAT,VENC
LIMPAGUARDE()