;** PD_0291 ********************************************************************
;** Pedido                                                                     *
;*******************************************************************************
;Autor           : Vise Informatica (VISERP)
;Desenvolvido  em: 11/07/2019 por Anivel Augusto Rodrigues
;Ultima Alteracao:   /  /     por 
;Motivo Alteracao:                                                         

;Imprime 2 vias em folha A4, por‚m se quantidade de itens maior que 14, ir  imprimir 2 vias em folha inteira
 
;*******************************************************************************
;* ImpressÆo do Cabe‡alho                                                      *
;*******************************************************************************
;ImpressÆo em Folha A4, em duas vias!
Proc Cabecalho();
If Not IsAssigned(NOMEMPRESA)
   Then NOMEMPRESA=""
EndIf

Moveto "Pedidoc"
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 "\n"
Print File ARQIMP "  ------------------------------------------------------------------------------"+"\n"
Print File ARQIMP Spaces(02)+Format("W30,AL",NOMEMPRESA)+"Pedido de Venda"+Spaces(3)+"Impressao: "+Format("D13",Today())+"-"+Time()+"\n"
Print File ARQIMP "  ------------------------------------------------------------------------------"+"\n\n"

Print File ARQIMP Spaces(59)
Print File ARQIMP CI_IEXP
Print File ARQIMP CI_INEG
Print File ARQIMP "N§: "+Format("W06.3,AL",[Numero do Pedido])+"\n"
Print File ARQIMP CI_FEXP
Print File ARQIMP CI_FNEG

Print File ARQIMP Spaces(02)+Format("W05,AL",[Codigo])
Print File ARQIMP Format("W39,AL",[Razao Social])+"\n"

Print File ARQIMP Spaces(07)+Format("W33,AL",[Endereco])
Print File ARQIMP Format("W18,AL",[CGC])+Spaces(02)+"Emissao: "
Print File ARQIMP Format("D13",[Data de Emissao])+"\n"

Print File ARQIMP Spaces(07)+Format("W20,AL",[Bairro])
Print File ARQIMP Format("W12,AL",[CEP])
Print File ARQIMP Spaces(01)+Format("W16,AL",[IE])
Print File ARQIMP Spaces(04)+"Entrega: "+Format("D13",[Data de Entrega])+"\n"

Print File ARQIMP Spaces(07)+Format("W20,AL",[Cidade])
Print File ARQIMP Format("W13,AL",[UF])
Print File ARQIMP Format("W20,AL",[Telefone])+"\n"

Print File ARQIMP Spaces(02)+"Operacao: "+Format("W04,AL",[Codigo da Destinacao])
Print File ARQIMP Format("W26,AL",[Descricao da Destinacao])
Print File ARQIMP "CFO: "+Format("W12,AL",[CFO])
Print File ARQIMP "TabPreco:"+Format("W06,AL",[Codigo da Tabela])+"\n\n"

;********************* IMPRESSAO DOS ITENS ********************************
Print File ARQIMP Spaces(02)+"Seq Item                                      Quantidade Un   PrUnit   PrTotal"+"\n"
Print File ARQIMP Spaces(02)+"--- ----------------------------------------- ---------- -- -------- ---------"+"\n"

CONTADOR = 14

EndProc ;(Cabecalho)

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

ntab=table()

If (CONTADOR+nl)>86             
   Then
        Pedidomaisdeumafolha=true
        Print File ARQIMP "*** CONTINUA PROXIMA FOLHA ***\f"
        Cabecalho()
EndIf
CONTADOR=CONTADOR+nl

moveto ntab
EndProc

;*******************************************************************************
;* ImpressÆo dos Itens                                                         *
;*******************************************************************************
Proc Itens()
private wc
      
Moveto "Pedidoi"
Scan
  Checa_Linha(1)
  
  Print File ARQIMP Spaces(02)+Format("W04,AL",[Item do Pedido])
  Print File ARQIMP Format("W11",[Codigo do Item])
  Print File ARQIMP Format("W31,AL",[Descricao do Item])
  Print File ARQIMP Format("W10.3,EIC",[Quantidade])
  Print File ARQIMP Format("W03,AR",[Unidade Fiscal])
  Print File ARQIMP Format("W9.2,EIC",[Preco Unitario])
  Print File ARQIMP Format("W10.2,EIC",[Preco Total])+"\n"

  If not isblank([Informacoes_Adicionais])
     Then Checa_Linha(1)
          Print File ARQIMP Spaces(17)+Format("W80,AL",SubStr([Informacoes_Adicionais],1,60))+"\n"
  EndIf

EndScan

wc=CONTADOR
For ITEM From wc To 23              
    Print File ARQIMP "\n"
    CONTADOR=CONTADOR+1  
EndFor 
Print File ARQIMP "\027F"
Print File ARQIMP CI_8LPP

EndProc ;(Itens)

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

Moveto "PedidoC"
Checa_Linha(9)

Print File ARQIMP "\n"
Print File ARQIMP "  Mecadorias         Desconto     Liquido    Frete    Seguro   Desp.Aces"+"\n"
Print File ARQIMP Format("W12.2,EIC",[Valor Mercadoria])
Print File ARQIMP Format("W06.2,EIC",[Taxa de Desconto])+"%"
Print File ARQIMP Format("W10.2,EIC",[Valor do Desconto])
Print File ARQIMP Format("W12.2,EIC",[Valor Mercadoria Liquido])
Print File ARQIMP Format("W09.2,EIC",[Valor do Frete])
Print File ARQIMP Format("W10.2,EIC",[Valor do Seguro])
Print File ARQIMP Format("W012.2,EIC",[Valor Despesas Acessorias])+"\n\n"

Print File ARQIMP "         IPI       Subst.Icms     BC.ICMS     ICMS     Total"+"\n"
Print File ARQIMP Format("W12.2,EIC",[Valor IPI])
Print File ARQIMP Spaces(05)+Format("W12.2,EIC",[Valor Substituicao ICMS])
Print File ARQIMP Format("W12.2,EIC",[Valor BCICMS])
Print File ARQIMP Format("W09.2,EIC",[Valor ICMS])
Print File ARQIMP Format("W10.2,EIC",[Valor Nota Fiscal])+"\n\n"

Print File ARQIMP Spaces(02)+"Cond.Pagto.: "+[Descricao CondPagto]
Print File ARQIMP "   NP: "+StrVal([Numero de Parcelas])
Print File ARQIMP "   CD: "+[Composicao de Dias]+"\n"
Print File ARQIMP "  Volumes....: "+LTrim(Format("W06,EIC",[PedidoC->Numero de Volumes]))
Print File ARQIMP "      Peso Liquido: "+LTrim(Format("W11.3,EIC",[PedidoC->Peso Liquido]))
Print File ARQIMP "      Peso Bruto: "+LTrim(Format("W11.3,EIC",[PedidoC->Peso Bruto]))+"\n"

;********************* IMPRESSAO DOS VENCIMENTOS **************************
Print File ARQIMP Spaces(02)+"Vencimentos: "

Array DATA[9]
Array VALOR[9]
K=0
Moveto "PedidoV"
Scan
  K=K+1
  DATA[K]  = Format("D13",[Data])
  VALOR[K] = Format("W11.2,AL,EIC",[Valor])
  If K=9
     Then VENC()
          K=0
  EndIf
EndScan
If K>0
   Then VENC()
EndIf
Print File ARQIMP "\n"


;************************* IMPRESSAO DO RODAPE ********************************
Moveto "PedidoC"
Checa_Linha(9)

Print File ARQIMP Spaces(02)+"Obs. Pedido: "+Format("W110,AL",SubStr([Observacao do Pedido],001,110))+"\n"
If Substr([Observacao do Pedido],111,110)<>""
   Then Print File ARQIMP Spaces(20)+Format("W110,AL",SubStr([Observacao do Pedido],111,110))+"\n"
EndIf

Print File ARQIMP Spaces(02)+"Obs. Nota  : "+Format("W100,AL",SubStr([Observacao na Nota Fiscal],001,100))+"\n"

Print File ARQIMP Spaces(02)+"Entrega....: "+Format("W20,AL",[Endereco de Entrega])+"\n"
Print File ARQIMP Spaces(17)+Format("W15,AL",[Bairro de Entrega])+Format("W11,AL",[CEP de Entrega])
Print File ARQIMP Spaces(1)+"_________________________________"+"\n\n"
Print File ARQIMP Spaces(17)+Format("W15,AL",[Cidade de Entrega])+Format("W03,AL",[UF de Entrega])
Print File ARQIMP Spaces(10)+"Data/Assinatura Responsavel"+"\n\n"
Print File ARQIMP "  ------------------------------------------------------------------------------"
Print File ARQIMP CI_10CPP
Print File ARQIMP CI_6LPP
Print File ARQIMP "\n"

EndProc

;*******************************************************************************
;* Rotina de Impressao dos Vencimentos                                         *
;*******************************************************************************
Proc VENC()
If RecNo()>9
   Then Print File ARQIMP Spaces(20)
EndIf
For I From 1 to K
  Print File ARQIMP DATA[I]+Spaces(03)
EndFor
Print File ARQIMP "\n"
Print File ARQIMP Spaces(16)
For I From 1 to K
  Print File ARQIMP VALOR[I]
EndFor
Print File ARQIMP "\n"

Checa_Linha(2)

EndProc


;*******************************************************************************
;* Rotina Principal                                                            *
;*******************************************************************************
AGUARDE("Imprimindo Pedido -> "+PEDIDO)

ArqImp=PrivDir()+"PD_0291.Txt"
If IsFile(ArqImp)
   Then Deleta_Arquivo(ARQIMP)
EndIf

Pedidomaisdeumafolha=False
VIA=0   
For Wc From 1 To NUMCOPIA
  VIA = VIA + 1        

  Cabecalho()
  Itens()                                 
  Rodape()

  If Mod(VIA,2)>0 and CONTADOR<44 and NUMCOPIA>1 and not Pedidomaisdeumafolha
     then
          For ITEM From CONTADOR To 44              
            Print File ARQIMP "\n"
          EndFor 
     else
          Print File ARQIMP "\f"
  endif
EndFor
NUMCOPIA=1
ImpriArq(ARQIMP)

LIMPAGUARDE()

Release Vars VIA,ITEM, Pedidomaisdeumafolha
Release Procs Cabecalho,Checa_Linha,Itens,Rodape
