;** Pd_0268 ********************************************************************
;** Imprime a Digitacao do Pedido                        ** RIG FUNDIDOS **    *
;*******************************************************************************
;Autor           : Vise Informatica (VISERP)
;Desenvolvido  em: 02/09/2010 por Bruno Carvalho
;Ultima Altera‡Æo: 10/09/2010 por Bruno Carvalho
;Motivo Altera‡Æo: Add Observa‡Æo do Item
;Ultima Altera‡Æo: 06/10/2010 por Bruno Carvalho
;Motivo Altera‡Æo: Add Observa‡Æo do Pedido
;Ultima Altera‡Æo: 23/03/2012 por Bruno Carvalho
;Motivo Altera‡Æo: Ajustes
;Ultima Altera‡Æo: 08/03/2017 por Kamila Gomes
;Motivo Altera‡Æo: Ajustes para imprimir 44 linhas por p gina
;Ultima Altera‡Æo: 21/12/2017 por Anivel Augusto Rodrigues
;Motivo Altera‡Æo: Ajustes observacoes
;Ultima Altera‡Æo: 04/05/2018 por Anivel Augusto Rodrigues
;Motivo Altera‡Æo: Ajustes regime tributario

;*******************************************************************************
;* Rotina para Impressao do Relatorio                                          *
;*******************************************************************************
 
;*************************** IMPRESSAO DO CABECALHO ****************************
Proc Cabecalho()
private REG_DESTINAT, COD_REDESPACHO, NOME_REDESPACHO, REGTRIB

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

GetRecord "\\Sistemas\\Cadastro\\Destinat" [Codigo] To REG_DESTINAT
If Retval 
   Then COD_REDESPACHO =REG_DESTINAT["Codigo Transp_Redespacho"]
        NOME_REDESPACHO=REG_DESTINAT["Nome Fantasia Redespacho"]
        REGTRIB        =REG_DESTINAT["Regime_Tributario"       ]
   Else COD_REDESPACHO =""
        NOME_REDESPACHO=""
        REGTRIB        =""
EndIf

Moveto "PedidoC"
Print File ARQIMP chr(27)+chr(67)+chr(44)                                       ;Imprime 44 Linhas por pagina
Print File ARQIMP CI_15CPP+CI_8LPP
Print File ARQIMP Spaces(50)+"Impressao: "+Format("D10",Today())+"-"+Time()+ "\n"
Print File ARQIMP "\014"
Print File ARQIMP "\027E"
Print File ARQIMP Spaces(10)+"Pedido N§ "+Format("W06,AR",[Numero do Pedido])+"\027F"+"\n"
Print File ARQIMP "\020"
Print File ARQIMP "\027P"         ;Imprime 10 Caracteres por polegada
Print File ARQIMP Spaces(01)+"\027E"+"Nome Fantasia: "+"\027F"+Format("W25,AL",[Nome Fantasia EmitDest])+"\n"

Print File ARQIMP Spaces(01)+"\027E"+"     Cliente : "+"\027F"+Format("W05,AL",[Codigo])+Format("W64,AL",[Razao Social])
Print File ARQIMP "\027E"+"CGC: "+"\027F"+Format("W20,AL",[CGC])
Print File ARQIMP "\027E"+"RT: "+"\027F"+Format("W20,AL",REGTRIB)+"\n"

Print File ARQIMP Spaces(21)+Format("W44,AL",[Endereco])
Print File ARQIMP Format("W21,AL",[CEP])
Print File ARQIMP "\027E"+"IE: "+"\027F"+Format("W15,AL",[IE])+"\n"

Print File ARQIMP Spaces(21)+Format("W23,AL",[Bairro])
Print File ARQIMP Format("W03,AL",[UF])+"/"
Print File ARQIMP Format("W36,AL",[Cidade])
Print File ARQIMP "\027E"+"Fone: "+"\027F"+Format("W20,AL",[Telefone])+"\n"

Print File ARQIMP Spaces(01)+"\027E"+"Representante: "+"\027F"+Format("W05,AL",[Codigo do Representante])
Print File ARQIMP Format("W31,AL",[Nome Fantasia Represent])
Print File ARQIMP Spaces(04)+"\027E"+"Emissao: "+"\027F"+Format("W10,AL,D13",[Data de Emissao])
Print File ARQIMP Spaces(05)+"\027E"+"TabPre‡o: "+"\027F"+Format("W06,AL",[Codigo da Tabela])+"\n"

Print File ARQIMP Spaces(01)+"\027E"+"   Destina‡„o: "+"\027F"+Format("W04,AL",[Codigo da Destinacao])
Print File ARQIMP Format("W29,AL",[Descricao da Destinacao])
Print File ARQIMP Spaces(01)+"\027E"+"Transportador: "+"\027F"+Format("W05,AL",[Codigo da Transportadora])
Print File ARQIMP Format("W25,AL",[Nome Fantasia Transport])+"\n"

Print File ARQIMP Spaces(02)+"Sq Item                                           Cor           Grupo          "
Print File ARQIMP " Quantidade Un PrUnitario %Desc PrComDecsto    PrTotal"+"\n"
Print File ARQIMP Spaces(02)+"-- ---------------------------------------------- ------------- -------------- "
Print File ARQIMP "----------- -- ---------- ----- ----------- ----------"+"\n"

EndProc ;(Cabecalho)

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

Moveto "PedidoI" 

QUANT    = 0
TOT      = 0
CONTADOR = 0

Scan 
   Checa_Linha(1)
   GetRecord "\\Sistemas\\Estoque\\Itens" [Codigo do Item] To REG_ITEMGRUPO
   If Retval
      Then Descgrupo=REG_ITEMGRUPO["Descricao do Grupo"]
      Else Descgrupo=""
   EndIf

   Print File ARQIMP Spaces(01)+Format("W03,AR",[Item do Pedido])
   Print File ARQIMP Spaces(01)+Format("W11,AL",[Codigo do Item])
   Print File ARQIMP Format("W36,AL",[Descricao do Item])
   Print File ARQIMP Format("W14,AL",[Descricao da Cor_Tecido])
   Print File ARQIMP Format("W15,AL",Descgrupo)
   Print File ARQIMP Format("W11.3,EIC",[Quantidade])
   Print File ARQIMP Format("W03,AR",[Unidade Fiscal])
   Print File ARQIMP Format("W11.2,EIC",[Preco Unitario])
   Print File ARQIMP Format("W06.2,EIC",[Taxa Desconto Unitario])
   Print File ARQIMP Format("W12.2,EIC",[Preco Unitario]-[Valor Desconto Unitario])
   Print File ARQIMP Format("W11.2,EIC",[Preco Total])+"\n"
   If [Observacao_Item]<>""
       Then Print File ARQIMP "                "+Format("W41,AL",[Observacao_Item])+"\n"
            CONTADOR=CONTADOR+1
   EndIf
   
   TOT   = TOT + [Preco Total]
   ;CONTADOR = CONTADOR + 1
EndScan

For ITEM from CONTADOR to 18
  Print File ARQIMP "\n"
EndFor

EndProc ;(Itens)

;********************* IMPRESSAO DOS TOTAIS **************************
Proc Rodape(QUEBRAFORM)
Private QUEBRAFORM

Moveto "PedidoC"   
If QUEBRAFORM
  Then 
    While CONTADOR < 17
     CONTADOR=CONTADOR + 1
     Print File ARQIMP "\n"
    EndWhile
    
    Print File ARQIMP "\n"
    Print File ARQIMP Format("W86,AR","   * * * *  CONTINUA NO PROXIMO FORMULARIO * * * *      SUBTOTAL =>")
    Print File ARQIMP Format("W49.2,EIC",TOT)+"\n"
    
    Print File ARQIMP "  Mecadorias         Desconto     Liquido      Frete    Seguro   Desp.Aces         IPI       Total     BC.ICMS        ICMS"+"\n"
    ;;9.999.999,99888.88%777.777,776.666.666,6644.444,44111.111,112.222.222,223.333/333,334.444.444,445.555.555,551.111.111,11
    Print File ARQIMP Spaces(04)+Format("W12.2,AC","*****")
    Print File ARQIMP Spaces(08)+Format("W06.2,AC","*****")+"%"
    Print File ARQIMP Spaces(03)+Format("W10.2,AC","*****")
    Print File ARQIMP Format("W11.2,AC","*****")
    Print File ARQIMP Format("W09.2,AC","*****")
    Print File ARQIMP Spaces(03)+Format("W10.2,AC","*****")
    Print File ARQIMP Spaces(01)+Format("W012.2,AC","*****")
    Print File ARQIMP Format("W012.2,AC","*****")
    Print File ARQIMP Format("W012.2,AC","*****")
    Print File ARQIMP Format("W012.2,AC","*****")+"\n\n"
    
    Print File ARQIMP Spaces(02)+"   Cond. Pagamento.: "+[Descricao CondPagto]
    Print File ARQIMP "   NP: "+StrVal([Numero de Parcelas])
    Print File ARQIMP "   CD: "+[Composicao de Dias]
    Print File ARQIMP "   FP: "+[Forma de Pagamento]
    Print File ARQIMP "   N.Volumes.: "+LTrim(Format("W06,AC","*****"))
    Print File ARQIMP "   Peso Liquido: "+LTrim(Format("W11.3,AC","*****"))
    Print File ARQIMP "   Peso Bruto: "+LTrim(Format("W11.3,AC","*****"))+"\n"
    
    ;Print File ARQIMP Spaces(02)+"Forma de Pagamento.: "+[Forma de Pagamento]+"\n"
    
    Print File ARQIMP Spaces(02)+"Obs. do Pedido.....: "+Format("W107,AL",SubStr([Observacao do Pedido],001,107))+"\n"
    If Substr([Observacao do Pedido],108,107)<>""
       Then Print File ARQIMP Spaces(23)+Format("W107,AL",SubStr([Observacao do Pedido],108,107))+"\n\n"
    EndIf
    
    Print File ARQIMP "\n"
    Print File ARQIMP "\027F"
    
    Print File ARQIMP "\f"
  Else
    Print File ARQIMP "\n"
    Print File ARQIMP "  Mecadorias         Desconto     Liquido    Frete    Seguro   Desp.Aces         IPI       Total     BC.ICMS        ICMS"+"\n"
    ;;9.999.999,99888.88%777.777,776.666.666,6644.444,44111.111,112.222.222,223.333/333,334.444.444,445.555.555,551.111.111,11
    
    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])
    Print File ARQIMP Format("W012.2,EIC",[Valor IPI])
    Print File ARQIMP Format("W012.2,EIC",[Valor Nota Fiscal])
    Print File ARQIMP Format("W012.2,EIC",[Valor BCICMS])
    Print File ARQIMP Format("W012.2,EIC",[Valor ICMS])+"\n\n"
    
    Print File ARQIMP Spaces(02)+"   Cond. Pagamento.: "+[Descricao CondPagto]
    Print File ARQIMP "   NP: "+StrVal([Numero de Parcelas])
    Print File ARQIMP "   CD: "+[Composicao de Dias]
    Print File ARQIMP "   FP: "+[Forma de Pagamento]
    Print File ARQIMP "   N.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"
    
    ;Print File ARQIMP Spaces(02)+"Forma de Pagamento.: "+[Forma de Pagamento]+"\n"
    
    imprimeobs()
    
    ;********************* IMPRESSAO DOS VENCIMENTOS **************************
    
    Print File ARQIMP "\027E"+"  Vencimentos: "
    
    Array DATA[9]
    Array VALOR[9]
    K=0
    KV=0
    Moveto "PedidoV"
    Scan
      K=K+1
      DATA[K]  = Format("D13",[Data])
      VALOR[K] = Format("W10.2,AR,EIC",[Valor])
      If K=9
         Then VENC()
              K=0
      EndIf
    EndScan
    If K>0
       Then VENC()
    EndIf
  ;Print File ARQIMP "\n"
  Print File ARQIMP "\027F"
  
  ;************************* IMPRESSAO DO RODAPE *********************************
  Print File ARQIMP Spaces(42)+"   /   /     -                                   " + "\n"
  Print File ARQIMP Spaces(42)+"-------------------------------------------------" + "\n"
  Print File ARQIMP Spaces(42)+"              Data e Assinatura"
  Print File ARQIMP "\f"
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"

KV=KV+1
EndProc

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

If (CONTADOR+nl)>18
   Then RodaPe(True)
        Cabecalho() 
        CONTADOR=nl    
        Moveto "PedidoI"
   Else CONTADOR=CONTADOR+nl
EndIf
EndProc ;Checa_Linha() 

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

TamObs=64

s="Observacäes: "+Tira_QuebraLinha([Observacao do Pedido])

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 CI_IEXP+CI_INEG+Spaces(02)+SubStr(s,posobs,tamobs)+CI_FEXP+CI_FNEG+"\n"
  posobs=posobs+tamobs
EndFor

Moveto "PedidoC"
endproc

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

AGUARDE("Imprimindo Pedido Gerencial -> "+PEDIDO)
Cabecalho()
Itens()
Rodape(false)

ImpriArq(ARQIMP)

Release Vars VALOR,DATA, CONTADOR, ARQIMP
Release Procs Itens, Rodape, Cabecalho,VENC,imprimeobs
