;** RO_0372 ********************************************************************
;** Imprime a Digitacao do Romaneio                      ** Bela Vista Tintas **
;*******************************************************************************
;Autor           : Vise Informatica (VISERP)
;Desenvolvido  em: 19/08/2015 por Andrey Fillipe
;Alterado em     :   /  /     por 
;Motivo Alteracao: 

;*******************************************************************************
;* Rotina para Impressao do Relatorio                                          *
;*******************************************************************************
PARAM_NITENS=10

Proc Cabecalho()

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

Print File ARQIMP CI_10CPP
Print File ARQIMP CI_8LPP

Moveto "FaturaC"
Print File ARQIMP "\n"
Print File ARQIMP Format("W50,AL","CONTROLE INTERNO")+"Impressao: "+Format("D10",[Data de Emissao])+"-"+Time()+ "\n\n"

Print File ARQIMP "\014"
Print File ARQIMP "\027E"
Print File ARQIMP Spaces(20)+"Orcamento N§ "+Format("W06.3,AL",NOTAFISC)+"\n\n"
Print File ARQIMP "\020"

Print File ARQIMP "Cliente : "+Format("W05,AL",[Codigo])+Format("W46,AL",[Razao Social])+"\n"
Print File ARQIMP "\027F"

Print File ARQIMP CI_10CPP
Print File ARQIMP CI_8LPP

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

Print File ARQIMP 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(10)+Format("W20,AL",[Cidade])
Print File ARQIMP Format("W20,AL",[UF])
Print File ARQIMP Format("W25,AL",[Telefone])+"\n"

Print File ARQIMP "Operacao: "
Print File ARQIMP Chr(27)+Chr(71)
Print File ARQIMP ""+[Codigo da Destinacao]+" "+[Descricao da Destinacao]+"\n"
Print File ARQIMP Chr(27)+Chr(72)

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

Moveto "FaturaIC"
Moveto[Numero da OF]
Locate [FaturaC->Numero da OF]
If RetVal
   Then Print File ARQIMP "Pintor: "+Format("W05,AL",[Codigo do Representante])
        Print File ARQIMP Format("W25,AL",[Nome Fantasia Represent])+"\n"
   Else Print File ARQIMP "\n"     
EndIf        
Print File ARQIMP CI_15CPP

;********************* IMPRESSAO DOS ITENS ********************************
Print File ARQIMP "----------------------------------------------------------------------------------------------------------------------------------------"+"\n"
Print File ARQIMP Spaces(02)+"Item                                                                         Un.Emb  Quant.Emb  Un  Quantidade  Unitario        Total"+"\n"
Print File ARQIMP "----------------------------------------------------------------------------------------------------------------------------------------"+"\n"

EndProc ;(Cabecalho)

;********************* IMPRESSAO DOS ITENS ********************************
Proc Itens()

Moveto "FaturaI"
Moveto [Numero da OF]
Locate [FaturaC->Numero da OF]

PARAM_TAMANHO_DESCR = 59
QUANT               = 0
TOT                 = 0
CONTADOR            = 0

While [Numero da OF]=[FaturaC->Numero da OF]
  
  DescItem=[Descricao do Item]
  Moveto [Codigo do Item]
  Ativa_Lookup()
  Moveto [Codigo do Item]
  Locate [FaturaI->Codigo do Item]
  If RetVal
     Then UNEMB   =[Unidade Embalagem]
          If [Descricao Complementar]<>""
             Then DescItem=[Descricao Complementar]
          EndIf   
          If IsBlank([Fator Conversao Embalagem])
             Then FATCON=1
             Else FATCON=[Fator Conversao Embalagem]
          EndIf
     Else UNEMB =""
          FATCON=BlankNum()
  EndIf
  KeyPress "ESC"
  UnlockRecord
  
  Checa_Linha(1)                
  
  Print File ARQIMP Spaces(02)+Format("W04,AL",[Item da OF])
  Print File ARQIMP Spaces(01)+Format("W12,AL",[Codigo do Item])
     
  LINHA=""
  Numero_de_Linhas=Int(Len(DescItem)/PARAM_TAMANHO_DESCR)
  If (Len(DescItem)/PARAM_TAMANHO_DESCR)>Numero_de_Linhas
     Then Numero_de_Linhas=Numero_de_Linhas+1
  EndIf
  
  If Numero_de_Linhas=0
     Then Numero_de_Linhas=1
  EndIf
  
  pos_inicial=1
  For nl From 1 To Numero_de_Linhas
     LINHA = LINHA + Format("W59,AL",SubStr(DescItem,pos_inicial,PARAM_TAMANHO_DESCR))
     If nl<=Numero_de_Linhas
        Then Print File ARQIMP LINHA 
             If nl<>Numero_de_Linhas
                Then Print File ARQIMP "\n"
             EndIf   
             ;CONTADOR = CONTADOR + 1
             LINHA = Spaces(19)
             pos_inicial=pos_inicial+PARAM_TAMANHO_DESCR
     EndIf
  EndFor
  ;If Len(descItem)>59
  ;   Then Print File ARQIMP Spaces(01)+ Substr(descItem,60,120)+"\n"
  ;EndIf
  
  If Not IsBlank(UNEMB)
     Then Print File ARQIMP Spaces(05)+Format("W03,Al",UNEMB)
          Print File ARQIMP Format("W10.2,EIC",([Quantidade]/FATCON))
     Else Print File ARQIMP Spaces(18)     
  EndIf
  Print File ARQIMP Spaces(02)+Format("W03,Al",[Unidade Fiscal])
  Print File ARQIMP Format("W11.2,EIC",[Quantidade])
  Print File ARQIMP Format("W10.2,EIC",[Preco Unitario])
  Print File ARQIMP Format("W13.2,EIC",[Preco Total])+"\n"


  TOT   = TOT + [Preco Total]
  Skip
  If EOT() Then QuitLoop EndIf
EndWhile

For ITEM From CONTADOR To PARAM_NITENS
    Print File ARQIMP "\n"
    QUEBRA_FOLHA = QUEBRA_FOLHA+1
EndFor

EndProc ;(Itens)

;********************* IMPRESSAO DOS TOTAIS **************************

Proc Rodape(QUEBRAFORM)
Private QUEBRAFORM

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

Moveto "FaturaC"

If SubStr([FaturaC->Observacao na Nota Fiscal],1,90)<>""
   Then Print File ARQIMP "Obs.: "+Format("W90,AL",SubStr([FaturaC->Observacao na Nota Fiscal],1,90))
   Else Print File ARQIMP Spaces(96)
EndIf

If QUEBRAFORM
   Then Print File ARQIMP Spaces(7)+"Total................ ********* "+"\n"
        If SubStr([FaturaC->Observacao na Nota Fiscal],91,90)<>""
           Then Print File ARQIMP Spaces(6)+Format("W90,AL",SubStr([FaturaC->Observacao na Nota Fiscal],91,90))
           Else Print File ARQIMP Spaces(96)
        EndIf        
        Print File ARQIMP Spaces(7)+"Desconto   ***   %... ********* "
   Else Print File ARQIMP Spaces(7)+"Total................"+Format("W11.2,EIC",TOT)+"\n"        
        If SubStr([FaturaC->Observacao na Nota Fiscal],91,90)<>""
           Then Print File ARQIMP Spaces(6)+Format("W90,AL",SubStr([FaturaC->Observacao na Nota Fiscal],91,90))
           Else Print File ARQIMP Spaces(96)
        EndIf        
        Print File ARQIMP Spaces(7)+"Desconto "+Format("W06.2,EIC",[Taxa de Desconto]) + "  %..."
        Print File ARQIMP Format("W11.2,EIC",[Valor Desconto])+"\n"
EndIf  

If [Frete Incluso na Nota]="+"
   Then If QUEBRAFORM
           Then Print File ARQIMP Spaces(103)+"Valor Frete.......... ********* "+Format("W11.2,EIC",[Valor do Frete])+"\n"
           Else Print File ARQIMP Spaces(103)+"Valor Frete.........."+Format("W11.2,EIC",[Valor do Frete])+"\n"
        EndIf
EndIf

If QUEBRAFORM
   Then Print File ARQIMP Spaces(103)+"Valor Total.......... ********* "+"\n"
   Else Print File ARQIMP Spaces(103)+"Valor Total.........."+Format("W11.2,EIC",[Valor Nota Fiscal])+"\n"
EndIf  

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

;********************* IMPRESSAO DOS VENCIMENTOS **************************

Print File ARQIMP "\027E"
Print File ARQIMP "Vencimentos: "

Array DATA[9]
Array VALOR[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])
  If K=9
     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"

;************************* IMPRESSAO DO RODAPE *********************************

If QUEBRAFORM
   Then Print File ARQIMP "\n\n\n" 
   Else Print File ARQIMP Spaces(38)+"   /   /     -                                   " + "\n"
        Print File ARQIMP Spaces(38)+"-------------------------------------------------" + "\n"
        Print File ARQIMP Spaces(38)+"              Data e Assinatura"+"\n"
EndIf        
Print File ARQIMP CI_6LPP
Print File ARQIMP CI_10CPP

If QUEBRA_FOLHA = (PARAM_NITENS*2)+1
   Then QUEBRA       = True
        QUEBRA_FOLHA = 1
EndIf                             

If QUEBRA
   Then QUEBRA = False
        Print File ARQIMP "\f"
   Else Print File ARQIMP "\n\n\n\n" 
EndIf

EndProc ;(Rodape)

;*******************************************************************************
;* Checa linha na area de impressao dos itens                                  *
;*******************************************************************************
Proc Checa_Linha(Nl)
private nl
QUEBRA = False

If QUEBRA_FOLHA <= (PARAM_NITENS*2)
   Then QUEBRA_FOLHA = QUEBRA_FOLHA+nl
EndIf

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

;*******************************************************************************
;* 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

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

QUEBRA_FOLHA = 0

AGUARDE("Imprimindo Romaneio -> "+NOTAFISC)

For Copias From 1 to 2
    Cabecalho()
    Itens()
    Rodape(False) 
EndFor

;---- Imprime Arquivo Impressao ------------------------------------------------
ImpriArq(ARQIMP)
Print File ARQIMP CI_10CPP
Print File ARQIMP CI_6LPP

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