;** Ro_0278C *******************************************************************
;** Imprime o Romaneio                                  Maciel                 *
;*******************************************************************************
;Autor           : Vise Informatica (VISERP)
;Desenvolvido  em: 05/05/2017 por Anivel Augusto Rodrigues
;Ultima Alteracao: 31/08/2017 por Anivel Augusto Rodrigues
;Motivo Alteracao: Ajustes desconto item
;Ultima Alteracao: 26/06/2018 por Anivel Augusto Rodrigues                           
;Motivo Alteracao: Ajustes itens mercadoria/servico
    
;*******************************************************************************
;* ImpressÆo do Cabe‡alho                                                      *
;*******************************************************************************
;ImpressÆo em formulario continua meia folha !

Proc Cabecalho()
private s1      

contador=14

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

Print File ARQIMP CI_10CPP
Print File ARQIMP CI_8LPP
Print File ARQIMP chr(27)+chr(67)+chr(44) ;Imprime 44 Linhas por pagina

Print File ARQIMP Format("W49,AL","    ")+"Impressao: "+Format("D10",Today())+"-"+Time()+ "\n\n"

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

Print File ARQIMP "Emissao : "+Format("D10",[Data de Emissao])+"\n"
Print File ARQIMP CI_INEG+"Cliente : "+Format("W05,AL",[Codigo])+Format("W46,AL",[Razao Social])+"\n"
If Rtrim([Razao Social])<>Rtrim([Nome Fantasia EmitDest])
   Then Print File ARQIMP "\027E"+Spaces(15)+Format("W46,AL",[Nome Fantasia EmitDest])+"\n"
        contador=contador+1
EndIf   

Print File ARQIMP CI_FNEG+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"

s1=Spaces(10)+Format("W20,AL",[Cidade])+
   Format("W7,AL",[UF])+
   "Tel: "+Format("W15,AL",[Telefone])

GetRecord "\\Sistemas\\Cadastro\\Destinat" [Codigo] To Cel
If RetVal And Not IsBlank(Cel["Celular"])
   Then s1=s1+Spaces(01)+"Cel: "+Cel["Celular"]
EndIf
Print File ARQIMP s1+"\n"

GetRecord"\\Sistemas\\Cadastro\\End_Cob" [Codigo] To REG_CLI
If RetVal And Not IsBlank(REG_CLI["Endereco de Entrega"])
   Then
        Print File ARQIMP "Entrega : "
        Print File ARQIMP Chr(27)+Chr(71)
        Print File ARQIMP Format("W41,AL",REG_CLI["Endereco de Entrega"])+"Bairro: "+Format("W20,AL",REG_CLI["Bairro de Entrega"])+"\n"
        Print File ARQIMP "          "+Format("W41,AL",REG_CLI["Cidade de Entrega"]+"-"+Format("W2,AL",REG_CLI["UF de Entrega"]))+
                          "CEP   : "+Format("W10,AL",REG_CLI["CEP de Entrega"])+Chr(27)+Chr(72)+"\n"
        contador=contador+2
EndIf   
   
Print File ARQIMP "Operacao: "
Print File ARQIMP ""+Format("W03,AL",[Codigo da Destinacao])+" "+Format("W30,AL",[Descricao da Destinacao])

Print File ARQIMP "Vendedor: "+Format("W05,AL",[Codigo do Representante])
Print File ARQIMP Format("W20,AL",[Nome Fantasia Represent])+"\n"
Print File ARQIMP "\015"         ;Imprime 17 Caracteres por polegada

;********************* IMPRESSAO DOS ITENS ********************************
Print File ARQIMP "\n"
Print File ARQIMP Spaces(02)+"Item                                           Marca                           Un  Quantidade  Unitario          Total"+"\n"
Print File ARQIMP fill("-",132)+"\n"

EndProc ;(Cabecalho)

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

ntabela=table()

If (CONTADOR+nl)>=41 
   Then CONTADOR=0
        RodaPe(True)
EndIf
CONTADOR=CONTADOR+nl

moveto ntabela
EndProc

;*******************************************************************************
;* ImpressÆo dos Itens                                                         *
;*******************************************************************************
Proc Itens()
private infadic, marca, rgitens, wii
       
wii      = 0

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

  getrecord "\\sistemas\\estoque\\itens" [Codigo do Item] to regitens
  if retval
     then marca=regitens["Descricao da Marca"]
     else marca=""
  endif   

  Checa_Linha(1)
  Print File ARQIMP Spaces(02)+Format("W03,AR",StrVal([Item da OF]))
  Print File ARQIMP Spaces(01)+Format("W11,AL",[Codigo do Item])
  Print File ARQIMP Spaces(01)+Format("W30,AL",[Descricao do Item]) ;62
  Print File ARQIMP Spaces(01)+Format("W31,AL",marca)
  Print File ARQIMP Spaces(01)+Format("W03,Al",[Unidade Fiscal])
  Print File ARQIMP Format("W11.3,EIC",[Quantidade])
  Print File ARQIMP Format("W10.2,EIC",[Preco Unitario])
  Print File ARQIMP Spaces(02)+Format("W13.2,EIC",[Preco Total])+"\n"
  
  imprimeobs1()
    
  Skip
  If EOT() Then QuitLoop EndIf
EndWhile


For wii From CONTADOR To 26
    Print File ARQIMP "\n"
EndFor 

EndProc ;(Itens)

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

TamObs=110

s=[Informacoes_Adicionais]

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

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

endproc


;*******************************************************************************
;* ImpressÆo do Rodap‚                                                         *
;*******************************************************************************
Proc Rodape(QUEBRAFORM)
Private QUEBRAFORM

If QUEBRAFORM
   Then 
        Print File ARQIMP "-->"+format("W128,AR","* * *  CONTINUA NA PROXIMA FOLHA * * * ")+"\f"

        Checa_Linha(1)
        Print File ARQIMP "-->"+format("W128,AR","* * *  CONTINUA  * * * "+"  Orcamento N§ "+Format("W06.3,AL",NOTAFISC))+"\n"
        
        return
endif        

;********************* IMPRESSAO DOS TOTAIS **************************
Moveto "FaturaC"
Checa_Linha(1)
Print File ARQIMP fill("-",132)+"\n"

Checa_Linha(2)
Print File ARQIMP "VR.MERCADORIAS: "+Format("W11.2,EIC",[Valor Mercadoria])+
                  Spaces(02)+"VR.DESCTO : "+Format("W8.2,EIC",[Valor Desconto])+
                  Spaces(02)+"VR.SERVICO: "+Format("W11.2,EIC",[Valor Servico])+
                  Spaces(02)+"VR.DESCTO : "+Format("W8.2,EIC",[Valor Desconto Servico])+
                  Spaces(02)+"VR.FRETE. : "+Format("W8.2,EIC",[Valor do Frete])+"\n\n"
                  
Checa_Linha(2)
Print File ARQIMP CI_IEXP+CI_INEG+"VR A PAGAR: "
Print File ARQIMP LTrim(Format("W11.2,EIC",[Valor Nota Fiscal]))

If IsAssigned(VSD_VrTroco)
   Then Print File ARQIMP Spaces(03)+"VR RECEBIDO: "+Format("W11.2,EIC",VSD_VrTroco)
        Print File ARQIMP Spaces(03)+"VR TROCO: "+Format("W11.2,EIC",VrTroco)
EndIf
Print File ARQIMP CI_FEXP+CI_FNEG+CI_15CPP+"\n\n"

imprimeobs()

Checa_Linha(1)
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 ****************************
s1="Vencimentos: "
s2="             "
s3="             "
KV=0
for K from 1 to 24

 if isblank(DATA[k])
    then quitloop
 endif    
 
 if kv=12
    then 
         Checa_Linha(3)
         Print File ARQIMP s1+"\n"
         Print File ARQIMP s2+"\n"
         Print File ARQIMP s3+"\n"

         kv=0
         s1="             "
         s2="             "
         s3="             "
 endif

 kv=kv+1
 s1=s1+spaces(08)+FPGT[K]          +Spaces(02)
 s2=s2+Format("D13",DATA[k])       +Spaces(02)
 s3=s3+Format("W10.2,EIC",VALOR[k])+Spaces(02)

endfor
if kv>0
  then 
       Checa_Linha(3)
       Print File ARQIMP s1+"\n"
       Print File ARQIMP s2+"\n"
       Print File ARQIMP s3+"\n"

       kv=0
       s1=""
       s2=""
       s3=""
endif

Checa_Linha(5)
Print File ARQIMP "\n"
Print File ARQIMP Spaces(80)+"   /   /     -                                   " + "\n"
Print File ARQIMP Spaces(80)+"-------------------------------------------------" + "\n"
Print File ARQIMP Spaces(80)+"              Data e Assinatura"+"\n"

Print File ARQIMP fill("=",132)
Print File ARQIMP CI_6LPP
Print File ARQIMP CI_10CPP+"\f"

EndProc ;Rodape

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

TamObs=132

s="Observacoes: "+[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)

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

endproc

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

AGUARDE("Imprimindo Romaneio -> "+NOTAFISC)


Array  DATA[24]
Array VALOR[24]
Array FPGT [24]

for K from 1 to 24
   DATA[k]=blankdate()
  VALOR[k]=blanknum()
   FPGT[K]=""
endfor

K=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]=[Data ]
  VALOR[K]=[Valor]
   FPGT[K]=[Forma de Pagamento]
  
  Skip
  If EOT() Then QuitLoop EndIf
EndWhile

Cabecalho()
Itens()
Rodape(False)
                                   
ImpriArq(ARQIMP)

Release Vars VALOR,DATA,FPGT,K,VSD_VrTroco,VrTroco, contador
Release Procs Cabecalho, Itens, Rodape, Checa_Linha, imprimeobs, imprimeobs1
LIMPAGUARDE()
