;** PD_0407 ********************************************************************
;** Imprime a Digitacao do Pedido                                              *
;*******************************************************************************
;Autor           : Vise Informatica (VISERP)
;Desenvolvido  em: 12/01/2018 por Bruno Carvalho
;Ultima Altera‡Æo: 25/01/2018 por Bruno Carvalho 
;Motivo Altera‡Æo: Ajustes na ImpressÆo 
;Ultima Altera‡Æo: 26/02/2018 por Anivel Augusto Rodrigues
;Motivo Altera‡Æo: Ajustes folha inteira 
;Ultima Altera‡Æo: 28/05/2018 por Kamila Gomes
;Motivo Altera‡Æo: Ajuste nome da empresa 

;*******************************************************************************
;* Rotina para Impressao do Relatorio                                          *
;*******************************************************************************
 
;*************************** IMPRESSAO DO CABECALHO ****************************
Proc Cabecalho()

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

Moveto "PedidoC"
Print File ARQIMP CI_15CPP+CI_6LPP                          
Print File ARQIMP CI_INEG+CI_IEXP
Print File ARQIMP "QUALIMARMORES"
Print File ARQIMP CI_FNEG+CI_FEXP
Print File ARQIMP Spaces(47)+"Impressao: "+Format("D10",Today())+"-"+Time()+ "\n\n"

Print File ARQIMP CI_INEG+CI_IEXP
Print File ARQIMP Spaces(41)+"Pedido N§ "+Format("W06,AR",[Numero do Pedido])
Print File ARQIMP CI_FNEG+CI_FEXP+"\n\n"

Print File ARQIMP "\020"
Print File ARQIMP "\027P"         ;Imprime 10 Caracteres por polegada
Print File ARQIMP Spaces(01)+"\027E"+"     Cliente : "+"\027F"+Format("W05,AL",[Codigo])+Format("W45,AL",[Razao Social])
Print File ARQIMP Spaces(01)+"\027E"+"          CGC: "+"\027F"+Format("W18,AL",[CGC])+"\n"

Print File ARQIMP Spaces(16)+Format("W30,AL",[Endereco])
Print File ARQIMP Format("W20,AC",[Bairro])
Print File ARQIMP Spaces(01)+"\027E"+"           IE: "+"\027F"+Format("W15,AL",[IE])+"\n"

Print File ARQIMP Spaces(16)+Format("W10,AL",[CEP])+Spaces(01)+Format("W17",[Cidade]+"/"+[UF])+Format("W20,AL",[Telefone])
Print File ARQIMP Spaces(04)+"\027E"+"     Emissao: "+"\027F"+Format("W10,AL,D13",[Data de Emissao])+"\n"

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

Print File ARQIMP "  Sq Item                                                                Un    PrTotal"+"\n"
Print File ARQIMP "  -- ------------------------------------------------------------------- -- ----------"+"\n"

CONTADOR=11                                                                                                       
If IsAssigned(GRUPO) And GRUPO<>"" 
   Then Print File ARQIMP CI_INEG+Spaces(2)+Upper(GRUPO)+CI_FNEG+"\n"
        CONTADOR=CONTADOR+1
EndIF   

EndProc ;(Cabecalho)

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

CONT_AUX_ITEM=0
DynArray AUX_ITEM[]

Moveto "PedidoI" 
Scan 
   Descricao = [Descricao do Item]
   GetRecord "\\Sistemas\\Estoque\\Itens" [Codigo do Item] To REG_ITEMGRUPO
   If Retval
      Then Descgrupo=REG_ITEMGRUPO["Descricao do Grupo"]
           If REG_ITEMGRUPO["Descricao Complementar"]<>""   
              Then Descricao = REG_ITEMGRUPO["Descricao Complementar"]
           EndIf         
      Else Descgrupo=""
   EndIf

   GRUPO = Rtrim([Observacao_Item])

   CONT_AUX_ITEM = CONT_AUX_ITEM + 1
   AUX_ITEM[CONT_AUX_ITEM] = Format("W40",GRUPO)+
                             Spaces(01)+Format("W03,AR",[Item do Pedido])+
                             Spaces(01)+Format("W11,AL",[Codigo do Item])+
                             Format("W55,AL",[Descricao do Item])+Spaces(01)+
                             Format("W03,AR",[Unidade Fiscal])+
                             Format("W11.2,EIC",[Preco Total])
EndScan

AUX_GRUPO = "**---***"
For ii From 1 To DynArraySize(AUX_ITEM)
  GRUPO = Ltrim(Rtrim(SubStr(AUX_ITEM[ii],1,40)))
  If AUX_GRUPO<>GRUPO
     Then AUX_GRUPO = GRUPO
        
          Checa_Linha(1)
          Print File ARQIMP CI_INEG+Spaces(2)+Upper(GRUPO)+CI_FNEG+"\n"                   
  EndIf      

  Checa_Linha(1)
  Print File ARQIMP SubStr(AUX_ITEM[ii],41,Len(AUX_ITEM[ii]))+"\n"
EndFor

EndProc ;(Itens)

;*******************************************************************************
;* Rotina para Impressao do Rodape                                             *
;*******************************************************************************
Proc Rodape()
Private ii

Moveto "PedidoC"   

Checa_Linha(DynArraySize(OBS)+13)

Print File ARQIMP "\n"
Print File ARQIMP Spaces(24)+"  Mecadorias         Desconto     Liquido    Frete       Total"+"\n"
                  ;9.999.999,99888.88%777.777,776.666.666,6644.444,442.222.222,22

Print File ARQIMP Spaces(24)
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("W12.2,EIC",[Valor Nota Fiscal])+"\n\n"

If DynArraySize(OBS) > 0
   Then For ii From 1 To DynArraySize(OBS)
          Print File ARQIMP SubStr(OBS[ii],6,Len(OBS[ii]))+"\n" 
        EndFor
EndIf
Print File ARQIMP "\n"

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 "\027F"

;************************* IMPRESSAO DO RODAPE *********************************
Print File ARQIMP "\n"
Print File ARQIMP Spaces(30) + "    /  /   -                                     " + "\n"
Print File ARQIMP Spaces(30) + "-------------------------------------------------" + "\n"
Print File ARQIMP Spaces(30) + "                Data e Assinatura                " + "\n"
Print File ARQIMP "\f"
  
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"

KV=KV+1
EndProc

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

tabela=table()

If (CONTADOR+nl)>PARAM_NITENS
   Then 
        Print File ARQIMP Format("W108,AC","   * * * *  CONTINUA NO PROXIMO FORMULARIO * * * *")
        Print File ARQIMP "\f"

        Cabecalho() 
        Moveto tabela          
EndIf
CONTADOR=CONTADOR+nl
EndProc ;Checa_Linha() 

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

TamObs=106

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

DynArray OBS[]

posobs=1
For i from 1 to nlobs
  OBS[i] = Format("W5",StrVal(i))+CI_INEG+Spaces(02)+SubStr(s,posobs,tamobs)+CI_FEXP+CI_FNEG
  posobs=posobs+tamobs
EndFor

endproc

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

imprimeobs()
PARAM_NITENS = 64    

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

ImpriArq(ARQIMP)

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