;** Ro_0278T *******************************************************************
;** Imprime o Romaneio de Troca                                     ** Maciel **
;*******************************************************************************
;Autor           : Vise Informatica (VISERP)
;Desenvolvido  em: 01/12/2016 por Bruno Carvalho              
;Alterado em     : 20/04/2017 por Anivel Augusto Rodrigues 
;Motivo Alteracao: Ajustes marca no item
;Ultima Alteracao: 05/08/2017 por Bruno Carvalho
;Motivo Alteracao: Substituido [Valor IR] -> [Valor DebitoCredito]
;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
;Ultima Alteracao: 09/09/2020 por Bruno Carvalho                           
;Motivo Alteracao: Alterado as vari veis VSD_VrTroco e VrTroco p/ [Valor Recebimento] e [Valor Troco] respectivamente

;*******************************************************************************
;* ImpressÆo do Cabe‡alho                                                      *
;*******************************************************************************
;ImpressÆo em Folha A4, em duas vias!

Proc Cabecalho()
If Not IsAssigned(NOMEMPRESA)
   Then NOMEMPRESA=""
EndIf
Moveto "Faturac"
Print File ARQIMP chr(27)+chr(67)+chr(33) ;Imprime 33 Linhas por pagina
Print File ARQIMP CI_10CPP
Print File ARQIMP CI_8LPP

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

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

Print File ARQIMP "\027F"+"Emissao : "+Format("D10",[Data de Emissao])+"\n"
Print File ARQIMP "\027E"+"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("W7,AL",[UF])
Print File ARQIMP "Tel: "
Print File ARQIMP Format("W15,AL",[Telefone])

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

Print File ARQIMP Chr(27)+Chr(71)
Print File ARQIMP "Entrega : "
GetRecord"\\Sistemas\\Cadastro\\End_Cob" [Codigo] To REG_CLI
If RetVal And Not IsBlank(REG_CLI["Endereco de Entrega"])
   Then 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"])
EndIf   
Print File ARQIMP "\n"
Print File ARQIMP Chr(27)+Chr(72)
   
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 fill("-",132)+"\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

If (CONTADOR+nl)>(CONTA_ITENS)
   Then RodaPe(True)
        Print File ARQIMP "\f"
        Cabecalho()
        CONTADOR=nl
        Moveto "FaturaI"
   Else CONTADOR=CONTADOR+nl
EndIf
EndProc

;*******************************************************************************
;* ImpressÆo dos Itens                                                         *
;*******************************************************************************
Proc Itens()
private infadic, marca, regitens

Moveto "FaturaID"
Moveto [Numero da OF]
Locate [FaturaC->Numero da OF]
CONTA_ITENS = 0
While [Numero da OF]=[FaturaC->Numero da OF]
  CONTA_ITENS = CONTA_ITENS+1
  Skip
  If EOT() Then QuitLoop EndIf
EndWhile

If CONTA_ITENS  <> 58
   Then Moveto "FaturaI"
        Moveto [Numero da OF]
        Locate [FaturaC->Numero da OF]
        While [Numero da OF]=[FaturaC->Numero da OF]
          CONTA_ITENS = CONTA_ITENS+1
          Skip
          If EOT() Then QuitLoop EndIf
        EndWhile
        Moveto "FaturaI"
        Home
EndIf

Switch
  Case CONTA_ITENS <=11 : CONTA_ITENS  = 11
  Case CONTA_ITENS > 11 : CONTA_ITENS  = 58 
EndSwitch

Moveto "FaturaID"
Home
Moveto [Numero da OF]
Locate [FaturaC->Numero da OF]

CONTADOR= 0
TOTDV   =0
             
Moveto "FaturaID"
Moveto [Numero da OF]
Locate [FaturaC->Numero da OF]
If RetVal
   Then 
        Print File ARQIMP "-------------- ITEM(S) DEVOLVIDOS --------------------------------------------------------------------------------------------------"+"\n"
        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()
        
          TOTDV  =TOTDV+[Preco Total]
          Skip
          If EOT() Then QuitLoop EndIf
        EndWhile
EndIf        
       
Moveto "FaturaI"
Moveto [Numero da OF]
Locate [FaturaC->Numero da OF]


Checa_Linha(1)
Print File ARQIMP "-------------- ITEM(S) COMPRADOS ---------------------------------------------------------------------------------------------------"+"\n"
CONTADOR = CONTADOR + 1

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 ITEM From CONTADOR To CONTA_ITENS
    If CONTADOR = CONTA_ITENS 
       Then QuitLoop
    EndIf
    Print File ARQIMP "\n"
EndFor 

Print File ARQIMP CI_8LPP          

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

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

If QUEBRAFORM
   Then Print File ARQIMP "VR.DESCTO: "+Format("W9,AC","******")+      
                          Spaces(02)+"VR.SERVICO....: "+Format("W9,AC","******")+
                          Spaces(02)+"VR.DESCTO: "+Format("W9,AC","******")+
                          Spaces(02)+"VR.FRETE.: "+Format("W11,AC","******")+"\n"
        Print File ARQIMP "VR.LEVOU.: "+Format("W9,AC","******")+      
                          Spaces(02)+"VR.DEVOLVEU...: "+Format("W9,AC","******")+
                          Spaces(02)+"         : "+Format("W9,AC","")+
                          Spaces(02)+"DIFERENCA: "+Format("W11,AC","******")+"\n\n"                          
   Else Print File ARQIMP "VR.DESCTO: "+Format("W9.2,EIC",[Valor Desconto])+
                          Spaces(02)+"VR.SERVICO....: "+Format("W09.2,EIC",[Valor Servico])+
                          Spaces(02)+"VR.DESCTO: "+Format("W9.2,EIC",[Valor Desconto Servico])+
                          Spaces(02)+"VR.FRETE.: "+Format("W11.2,EIC",[Valor do Frete])+"\n"
                          
        Print File ARQIMP CI_INEG
        If [Valor DebitoCredito]>0
           Then DIF="Credito"
           Else If [Valor DebitoCredito]<0
                   Then DIF="Debito"
                   Else DIF="Zero"
                EndIf
        EndIf                                  
        Print File ARQIMP "VR.LEVOU.: "+Format("W09.2,EIC",[Valor Mercadoria])+      
                          Spaces(02)+"VR.DEVOLVEU...: "+Format("W09.2,EIC",TOTDV)+
                          Spaces(02)+"DIFERENCA: "+Format("W11.2",[Valor DebitoCredito])+"(" + DIF + ")"+"\n\n"
        Print File ARQIMP CI_FNEG+"\n"                     
                          
        Print File ARQIMP CI_IEXP+CI_INEG+"VR A PAGAR: "
        Print File ARQIMP LTrim(Format("W11.2,EIC",[Valor Nota Fiscal]))
        
        If [Valor Recebimento]>0
           Then Print File ARQIMP Spaces(03)+"VR RECEBIDO: "+Format("W11.2,EIC",[Valor Recebimento])
                Print File ARQIMP Spaces(03)+"VR TROCO: "+Format("W11.2,EIC",[Valor Troco])
        EndIf
        Print File ARQIMP CI_FEXP+CI_FNEG+"\n\n"
EndIf
                                
Print File ARQIMP "Observacao : "
Print File ARQIMP Format("W119,AL",SubStr([Observacao na Nota Fiscal],001,119))+"\n"

If SubStr([Observacao na Nota Fiscal],120,119)<>""
   Then Print File ARQIMP Spaces(13)+Format("W119,AL",SubStr([Observacao na Nota Fiscal],120,119))+"\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 "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"

If QUEBRAFORM
   Then Print File ARQIMP "\n\n\n\n"
        Print File ARQIMP fill("=",132)+"\n"
        
   Else 
        Print File ARQIMP Spaces(41)+"   /   /     -                                   " + "\n"
        Print File ARQIMP Spaces(41)+"-------------------------------------------------" + "\n"
        Print File ARQIMP Spaces(41)+"              Data e Assinatura"+"\n"
        Print File ARQIMP "\n"
        Print File ARQIMP fill("=",132)+"\n"
        Print File ARQIMP CI_6LPP
        Print File ARQIMP CI_10CPP

EndIf   

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

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

AGUARDE("Imprimindo Romaneio -> "+NOTAFISC)
Cont=0
for wromcopia from 1 to NUMCOPIA
  Cabecalho()
  Itens()
  Rodape(False)
  Cont=Cont+1
  If Cont=2 
     Then Cont=0
          Print File ARQIMP "\f"
  EndIf    
endfor 

NUMCOPIA=1
ImpriArq(ARQIMP)

Release Vars VALOR,DATA,wromcopia
Release Procs Cabecalho, Itens, Rodape, VENC, Checa_Linha
LIMPAGUARDE()
