;** RO2v0217   *****************************************************************
;** Imprime a Digitacao do Romaneio 2a Via                 ** Auditoria **     *
;*******************************************************************************
;Autor           : Vise Informatica (VISERP)
;Desenvolvido  em: 28/01/2012 por Kamila Gomes            
;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: 07/11/2018 por Anivel Augusto Rodrigues                           
;Motivo Alteracao: Ajustes formata‡Æo peso EIC com 3 casas decimais

;*******************************************************************************
;* Rotina para Impressao do Relatorio                                          *
;*******************************************************************************
;RO2v0217
; Scrip onde agrupa se o codigo do item e o pre‡o unitario forem iguais, isto ‚, 
; a quantidade e o pre‡o total irÆo ajustar-se de acordo com os itens que estiverem iguais!


Proc CABECALHO()
Linha = 1
;************************* IMPRESSAO DO CABECALHO *****************************

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

Print File ARQIMP "\n"
Print File ARQIMP Format("W40,AL",NOMEMPRESA)+Spaces(04)+"Impressao: "+Format("D10",Today())+"-"+Time()+ "\n\n"
;**********************
Linha = Linha + 2

Print File ARQIMP Chr(27)+Chr(14)
Print File ARQIMP "\027E"
Print File ARQIMP Spaces(14)+"2a Via "+[Tipo do Documento]+" N§ "+Format("W06.3,AL",[Numero do Documento])+"\n"
Print File ARQIMP Chr(27)+Chr(20)
Print File ARQIMP "\027F"
;**********************
Linha = Linha + 1

Print File ARQIMP Chr(27)+Chr(48)
Print File ARQIMP Chr(27)+Chr(18)

Print File ARQIMP "Cliente:  "+Format("W05,AL",[Codigo])+Format("W46,AL",[Razao Social])+"\n"
Print File ARQIMP "Endere‡o: "+Format("W46,AL",[Endereco])+"\n"
Print File ARQIMP "Bairro:   "+Format("W20,AL",[Bairro])+"\n"
Print File ARQIMP "Telefone: "+Format("W20,AL",[Telefone])+"\n"
Print File ARQIMP "CEP:      "+Format("W10,AL",[CEP])

;**********************
Linha = Linha + 5

Print File ARQIMP Spaces(09)+Format("W10,AL","")
Print File ARQIMP "IE:  "+Format("W15,AL",[IE])+"\n"
Print File ARQIMP Spaces(09)+Format("W40,AL","")
Print File ARQIMP "CGC: "+Format("W18,AL",[CGC])+"\n"
;**********************
Linha = Linha + 1

Print File ARQIMP Spaces(09)+Format("W20,AL","")
Print File ARQIMP Format("W20,AL","")
Print File ARQIMP Format("W25,AL","")+"\n"
;**********************
Linha = Linha + 1

Print  File ARQIMP Spaces(09)+Format("W20,AL",[Cidade])
Print  File ARQIMP Format("W20,AL",[UF])
Print  File ARQIMP Format("W25,AL","")+"\n\n"
;**********************
Linha = Linha + 1

Print  File ARQIMP "Operacao: "+Format("W05,AL",[Codigo da Destinacao   ])
Print  File ARQIMP Format("W25,AL",[Descricao da Destinacao])
Print  File ARQIMP "Emissao..: "+Format("D13",[Data de Emissao    ])+"\n"
;**********************
Linha = Linha + 2

Print  File ARQIMP "Vendedor: "+Format("W05,AL",[Codigo do Representante])
Print  File ARQIMP Format("W25,AL",[Nome Fantasia Represent])
;NOME_USUARIO=""
;View "\\Sistemas\\Cadastro\\Usuarios"
;Moveto [Chapa]
;Locate [Answer->Codigo_Usuario]
;If RetVal
;   Then NOME_USUARIO=[Nome]
;EndIf
;ClearImage
;Moveto "Answer"
;Print  File ARQIMP "Usuario: "+Format("W03,AL",[Codigo_Usuario])
;Print  File ARQIMP Format("W17,AL",NOME_USUARIO)
Print  File ARQIMP " OP: "+[Origem do Pedido]+"\n\n"
Print  File ARQIMP Chr(27)+Chr(15)
;**********************
Linha = Linha + 2

EndProc ;(Cabecalho)

;*******************************************************************************
;* ImpressÆo dos Itens                                                         *
;*******************************************************************************
Proc Itens()  

Print File ARQIMP "Pedido Sq Item                                             Qtde Un   PrUni   PrTab  Descontos %Com   PrTotal Tb.Pr    VrCom"+"\n"
Print File ARQIMP "------ -- ------------------------------------------ ---------- -- ------- ------- ---------- ---- --------- -----  -------"+"\n"
;**********************
Linha = Linha + 2
;;999999 12345678901234567890123456789099.999,999 XX 99.999,99 999.999,99 99/99/99
TOTCOMIS=0
SEQ=1
Moveto PrivDir()+"NotaI"   
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 ]   

       Print File ARQIMP Format("W07,AL",[Numero Pedido_Orcamento])
       ;Print File ARQIMP Format("W03,AL",[Item do Documento])
       Print File ARQIMP Format("W03,AL",SEQ)
       Print File ARQIMP Format("W10,AR",[Codigo do Item])+" "
       Print File ARQIMP Format("W32,AL",[Descricao do Item])
       Print File ARQIMP Format("W10.2,EIC",[Quantidade])
       Print File ARQIMP Format("W03,AR",[Unidade Fiscal])
       Print File ARQIMP Format("W8.2,EIC",[Preco Unitario])
       Print File ARQIMP Format("W8.2,EIC",[Preco Unitario Tabela])
       Print File ARQIMP Format("W10.2,EIC",[Valor Desconto Item])
       Print File ARQIMP Format("W5.2,EIC",[Comissao do Item])
       Print File ARQIMP Format("W10.2,EIC",[Preco Total])+" "
       Print File ARQIMP Format("W06",[Codigo da Tabela])
       ITEMCOMIS=(([Preco Total]-[Valor Desconto Total])*[Comissao do Item])/100
       Print File ARQIMP Format("W8.2,EIC",ITEMCOMIS)+"\n"
    
       TOTCOMIS=TOTCOMIS+ITEMCOMIS
       ;**********************
       Linha = Linha + 1
       SEQ = SEQ+1
       Skip
       If EOT() Then QuitLoop EndIf

EndWhile
Print File ARQIMP Chr(27)+Chr(48)
Print File ARQIMP Chr(27)+Chr(18)

EndProc ;Itens()


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

;********************* IMPRESSAO DOS TOTAIS **************************
Moveto "Answer"
Print File ARQIMP "\n"
;**********************
Linha = Linha + 1

Print File ARQIMP Spaces(27)+"  Mecadorias         Desconto     Liquido       Total    Comissao"+"\n"
                ;;9.999.999,99888.88%777.777,776.666.666,665.555.555,558.888.888,88
;**********************
Linha = Linha + 1

Print File ARQIMP Spaces(27)+Format("W12.2,EIC",[Valor Mercadoria])
Print File ARQIMP Format("W06.2,EIC",[Taxa de Desconto])+"%"
Print File ARQIMP Format("W10.2,EIC",[Valor Desconto])
Print File ARQIMP Format("W12.2,EIC",[Valor Mercadoria Liquido])
Print File ARQIMP Format("W12.2,EIC",[Valor Nota Fiscal])
Print File ARQIMP Format("W12.2,EIC",TOTCOMIS)+"\n\n"
;**********************
Linha = Linha + 2

Print File ARQIMP "Cond. Pagto: "+[Descricao CondPagto]
Print File ARQIMP "   NP: "+StrVal([Numero de Parcelas])
Print File ARQIMP "   CD: "+[Composicao de Dias]+"   FP: "+[Forma de Pagamento]+"   FR:"+Format("W10.2,EIC",[Valor do Frete])+"\n"
;********************* IMPRESSAO DOS VENCIMENTOS **************************
Print File ARQIMP "Vencimentos: "
;**********************
Linha = Linha + 1

Array DATA[9]
Array VALOR[9]
Array VALORC[9]
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])
  VALORC[K] = Format("W10.2,AR,EIC",[Valor da Comissao])
  If K=9
     Then VENC()
          K=0
  EndIf
  Skip
  If EOT() Then QuitLoop EndIf
EndWhile
If K>0
   Then VENC()
EndIf

Moveto"Answer"
Print File ARQIMP "Transport..: "+Format("W41,AL",[Nome Fantasia Transport])
Print File ARQIMP "Volumes:"+Format("W3,AL",[Numero de Volumes])
Print File ARQIMP "  Peso:"+Format("W09.3,EIC,AL",[Peso Liquido])+"\n"
;**********************
Linha = Linha + 2

Print File ARQIMP "Observacoes: "
Print File ARQIMP "\27E"
Print File ARQIMP Format("W60,AL",SubStr([Observacao na Nota Fiscal],001,60))+"\n"
If Substr([Observacao na Nota Fiscal],61,120)<>""
   Then Print File ARQIMP Spaces(13)+Format("W60,AL",SubStr([Observacao na Nota Fiscal],61,120))+"\n"
EndIf
If Substr([Observacao na Nota Fiscal],121,180)<>""
   Then Print File ARQIMP Spaces(13)+Format("W60,AL",SubStr([Observacao na Nota Fiscal],121,180))+"\n"
EndIf
Print File ARQIMP "\27F"
Print File ARQIMP "\n"

;**********************
Linha = Linha + 1

;************************* IMPRESSAO DO RODAPE ********************************
Print File ARQIMP Spaces(12)+"            VENDA                                E N T R E G A"+"\n"
Print File ARQIMP Spaces(12)+"   /   /   -                          /   /   -"+"\n"
Print File ARQIMP Spaces(12)+"--------------------------------   --------------------------------"+"\n"
Print File ARQIMP Spaces(12)+"   Data e Assinatura do Emitente      Data e Assinatura do Cliente"
;**********************
Linha = Linha + 3

If Numero_Pagina = 1
   Then For ContLinha From Linha To 40
           Print File ARQIMP  "\n"
        EndFor
   Else Print File ARQIMP "\f"
EndIf
Print File ARQIMP Chr(27)+Chr(50)
Print File ARQIMP Chr(27)+Chr(18)


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"
Print File ARQIMP Spaces(13)
For I From 1 to K
  Print File ARQIMP VALORC[I]+Spaces(02)
EndFor
Print File ARQIMP "\n"

KV=KV+1
EndProc

;*******************************************************************************
;* Rotina Principal                                                            *
;*******************************************************************************
CONTINUA_IMPRESSAO=True
Numero_Pagina=0
N_COPIAS=1

ArqImp=PrivDir()+"RO2aVia.Txt"
If IsFile(ArqImp)
   Then Deleta_Arquivo(ARQIMP)
EndIf
AGUARDE("Imprimindo 2a Via Romaneio -> "+[Answer->Numero do Documento])
Do_It!
Copy "\\Base\\Sistemas\\Vendas\\NotaI"  PrivDir()+"NotaI"
CoEditKey
{TableAdd} TypeIn PrivDir()+"NotaI"  Enter

Moveto "Notai"
Moveto [Numero do Documento]
Locate [Answer->Numero do Documento]
While [Numero do Documento]=[Answer->Numero do Documento]
  CopyToArray REG_NOTAI
          
  Moveto PrivDir()+"Notai"
  Moveto [Codigo do Item]
  Locate REG_NOTAI["Codigo do Item"]
  If RetVal
     Then If [Preco Unitario]=REG_NOTAI["Preco Unitario"]
             Then INCLUI_NOVO=False
                  [Quantidade ]=[Quantidade] +REG_NOTAI["Quantidade"]
                  [Preco Total]=[Preco Total]+REG_NOTAI["Preco Total"]
             Else INCLUI_NOVO=True
          EndIf
     Else INCLUI_NOVO=True
  EndIf
          
  If INCLUI_NOVO
     Then End Down
          REG_NOTAI["NCM"]=""
          CopyFromArray REG_NOTAI
  EndIf        
         
  Moveto "Notai"
  Skip
  If EOT() Then QuitLoop EndIf
EndWhile  

Moveto PrivDir()+"Notai"
Home
While True
  AUX_REF=""
  Moveto [Codigo do Item]
  If Busca_Lookup("Codigo do Item",[Codigo do Item],"")
     Then AUX_REF=REG_BUSCA_LOOKUP["Referencia"]
  EndIf
  KeyPress "Esc"
  Moveto [NCM]
  []=AUX_REF
  Skip
  If EOT() Then QuitLoop EndIf
EndWhile

Print File ARQIMP Chr(27)+Chr(48)
Print File ARQIMP Chr(27)+Chr(18)

Cabecalho()
Itens()
Rodape() 

Do_It!
Moveto PrivDir()+"Notai"
ClearImage

;---- Imprime Arquivo Impressao ------------------------------------------------
          ImpriArq(ARQIMP)
Moveto "Answer"
CoEditKey
Release Vars VALOR,DATA,ARRAY_TM,WSREF,COLUNA
Release Procs IMPFAT,VENC,Verifica_Array_Grade

          LIMPAGUARDE()

