;** RO2V0345 *******************************************************************
;** Imprime a Digitacao do Romaneio 2 via                   ** Vitoria Textil **
;*******************************************************************************
;Autor           : Vise Informatica (VISERP)
;Desenvolvido  em: 09/08/2016 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                                                      *
;*******************************************************************************
Proc Cabecalho();
If Not IsAssigned(NOMEMPRESA)
   Then NOMEMPRESA=""
EndIf
Moveto "Answer"
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 spaces(39)+CI_IEXP+CI_INEG+"Orcamento N§ "+Format("W06.3,AL",NOTAFISC)+CI_FNEG+CI_FEXP+"\n"

Print File ARQIMP "Emissao : "+Format("D10",[Data de Emissao])+ "     Impressao: "+Format("D10",Today())+"-"+Time()
If IsAssigned(SOIMPRIMIR) And SOIMPRIMIR
   Then Print File ARQIMP CI_IEXP+CI_INEG+"       2a Via"+CI_FNEG+CI_FEXP+"\n"
   Else Print File ARQIMP "\n"
EndIf

Print File ARQIMP CI_INEG+"Cliente : "+Format("W05,AL",[Codigo])+Format("W44,AL",[Razao Social])+CI_FNEG+"\n"

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

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

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

Print File ARQIMP Spaces(10)+regdestinat["Endereco_Eletronico"]+"\n"

Print File ARQIMP "Operacao: "+CI_INEG+Format("W05,AL",[Codigo da Destinacao   ])+Format("W25,AL",[Descricao da Destinacao])+CI_FNEG
Print File ARQIMP "Vendedor: "+Format("W05,AL",[Codigo do Representante])+Format("W25,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                                                                           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 "NotaI"
   Else CONTADOR=CONTADOR+nl
EndIf
EndProc

;*******************************************************************************
;* ImpressÆo dos Itens                                                         *
;*******************************************************************************
Proc Itens()
       
Moveto "NotaI"
Moveto [Codigo da Empresa]
Locate [Answer->Codigo da Empresa  ],
       [Answer->Numero do Documento],
       [Answer->Serie do Documento ]

CONTA_ITENS = 0

while [Codigo da Empresa  ]=[Answer->Codigo da Empresa  ] And
      [Numero do Documento]=[Answer->Numero do Documento] And
      [Serie do Documento ]=[Answer->Serie do Documento ]

  CONTA_ITENS = CONTA_ITENS+1
  Skip
  If EOT() Then QuitLoop EndIf
EndWhile

Switch
  Case CONTA_ITENS <=18 : CONTA_ITENS  = 18
  Case CONTA_ITENS > 18 : CONTA_ITENS  = 59 
EndSwitch

Home
Moveto [Codigo da Empresa]
Locate [Answer->Codigo da Empresa  ],
       [Answer->Numero do Documento],
       [Answer->Serie do Documento ]

CONTADOR = 0
while [Codigo da Empresa  ]=[Answer->Codigo da Empresa  ] And
      [Numero do Documento]=[Answer->Numero do Documento] And
      [Serie do Documento ]=[Answer->Serie do Documento ]

  Checa_Linha(1)
  Print File ARQIMP Spaces(02)+Format("W03,AR",StrVal([Item do Documento]))
  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",[Informacoes_Adicionais])
  Print File ARQIMP Spaces(01)+Format("W03,Al",[Unidade Fiscal])
  Print File ARQIMP Format("W09.3,EIC",[Quantidade])
  Print File ARQIMP Format("W10.2,EIC",[Preco Unitario])
  Print File ARQIMP Spaces(02)+Format("W13.2,EIC",[Preco Total])+"\n"

  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)

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

Print File ARQIMP Fill("-",132)+"\n"

;********************* IMPRESSAO DOS TOTAIS **************************
Moveto "Answer"

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

If QUEBRAFORM
   Then Print File ARQIMP Spaces(88)+"Desconto  *****  ..    *******  "+"\n"
        Print File ARQIMP Spaces(88)+"Total Liquido......    *******  "+"\n"
   Else Print File ARQIMP Spaces(88)+"Desconto "+Format("W08.2,EIC",[Taxa de Desconto])+"%."+Format("W11.2,EIC",[Valor Desconto])+"\n"
        Print File ARQIMP Spaces(88)+"Total Liquido......"+Format("W11.2,EIC",[Valor Nota Fiscal])+"\n"
EndIf

If Rtrim([Observacao na Nota Fiscal])<>""
   Then Print File ARQIMP "         OBS.: "+Format("W117,AL",SubStr([Observacao na Nota Fiscal],01,117))+"\n"
EndIf   
Print File ARQIMP "Transportador: "+Format("W30",[Answer->Codigo da Transportadora]+" "+[Answer->Nome Fantasia Transport])
Print File ARQIMP "    Volumes: "+Format("W08,EIC,AL",[Answer->Numero de Volumes])+" Peso Bruto: "+Format("W12.3,EIC,AL",[Answer->Peso Bruto])+"\n"
         
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 "\018"
Print File ARQIMP "\027E"
Print File ARQIMP "Vencimentos: "

Array DATA[5]
Array VALOR[5]
Array FP[5]
K=0
KV=0

Moveto "NotaV"
Moveto [Codigo da Empresa]
Locate [Answer->Codigo da Empresa  ],
       [Answer->Numero do Documento],
       [Answer->Serie do Documento ]
while [Codigo da Empresa  ]=[Answer->Codigo da Empresa  ] And
      [Numero do Documento]=[Answer->Numero do Documento] And
      [Serie do Documento ]=[Answer->Serie do Documento ]
  K=K+1
  DATA[K]  = Format("D13",[Data])
  VALOR[K] = Format("W10.2,AR,EIC",[Valor])
  FP[K]= Format("W10,AL",[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 "\n"
Print File ARQIMP "\027F"

If QUEBRAFORM
   Then Print File ARQIMP "\n\n"
   Else If CONTA_ITENS <=18
          Then Print File ARQIMP "\n\n"
          Else Print File ARQIMP "\n\n\n\n\n\n"
        EndIf  
        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

If IMPRIMINDO_PARCELAS
   Then Print File ARQIMP FP[NP]+Spaces(02)+"\n"
        Print File ARQIMP DATA[NP]+Spaces(02)+"\n"
        Print File ARQIMP Spaces(13)+VALOR[NP]+Spaces(02)+"\n\n"
        return
EndIF

For I From 1 to K
  Print File ARQIMP FP[I]+Spaces(02)
EndFor
Print File ARQIMP "\n"
Print File ARQIMP Spaces(13)
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()+"Ro2V0345.Txt"
If IsFile(ArqImp)
   Then Deleta_Arquivo(ARQIMP)
EndIf

AGUARDE("Imprimindo 2§ Via Romaneio -> "+NOTAFISC)

Moveto "Answer"
getrecord "\\sistemas\\cadastro\\destinat" [Answer->Codigo] to regdestinat

For Wc From 1 To 1

  IMPRIMINDO_PARCELAS=FALSE
  NParc=[Answer->Numero de Parcelas]
  If NParc<1 Then NParc=1 EndIf

  For NP From 1 to NParc
    If [NotaV->Forma de Pagamento]="NP"
       Then IMPRIMINDO_PARCELAS=TRUE
    EndIf
    Cabecalho()
    Itens()
    Rodape(False) 
    If IMPRIMINDO_PARCELAS=False Then QuitLoop EndIf
  EndFor

  If CONTA_ITENS = 18 And Wc = 2
     Then Print File ARQIMP "\f"
     Else If CONTA_ITENS = 59 And Wc = 1
             Then Print File ARQIMP "\f"
          EndIf
  EndIf
  
EndFor
NUMCOPIA=1
ImpriArq(ARQIMP)

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

