;** RO_0022a *******************************************************************
;** Imprime a Digitacao do Romaneio                     ** Divin¢polis Botäes **
;*******************************************************************************
;Autor: Visao/Vise
;Desenvolvido  em: 12/08/2010 por Bruno Carvalho
;Alterado em     : 23/09/2010 por Bruno Carvalho 
;Motivo Alteracao: Ajustes "Agrupa Itens" e Quebra de Vencimentos
;Alterado em     : 31/05/2017 por Rafael Matos
;Motivo Alteracao: Retirado tabela "Fatura TG"
                                                                                
;*******************************************************************************
;* Rotina para Impressao do Relatorio                                          *
;*******************************************************************************
Proc IMPFAT()

;************************** IMPRESSAO DO CABECALHO *****************************
If Not IsAssigned(NOMEMPRESA)
   Then NOMEMPRESA=""
EndIf
Moveto "Faturac"
Print File ARQIMP chr(27)+chr(67)+chr(33) ;Imprime 33 Linhas por pagina
Print File ARQIMP CI_10CPP
Print File ARQIMP CI_6LPP

;Print File ARQIMP "\n"
Print File ARQIMP Format("W50,AL","CONTROLE INTERNO")+"Impressao: "+Format("D10",Today())+"-"+Time()+ "\n\n"

Print File ARQIMP "\014"
Print File ARQIMP "\027E"
Print File ARQIMP Spaces(20)+"Orcamento N§ "+Format("W06.3,AL",NOTAFISC)+"\n\n"
Print File ARQIMP "\020"

Print File ARQIMP "Cliente : "+Format("W05,AL",[Codigo])+Format("W46,AL",[Razao Social])+"\n"
Print File ARQIMP "\027F"

Print File ARQIMP CI_8LPP
Print File ARQIMP "\027P"         ;Imprime 10 Caracteres por polegada

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

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

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

Print File ARQIMP "Operacao: "
Print File ARQIMP Chr(27)+Chr(71)
Print File ARQIMP ""+[Codigo da Destinacao]+" "+[Descricao da Destinacao]+"\n"
Print File ARQIMP Chr(27)+Chr(72)

Print File ARQIMP "Vendedor: "+Format("W05,AL",[Codigo do Representante])
Print File ARQIMP Format("W25,AL",[Nome Fantasia Represent])+"\n"
Print File ARQIMP "\015"         ;Imprime 17 Caracteres por polegada

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

Moveto TABELA_TEMP
Moveto [Numero da OF]
Locate [FaturaC->Numero da OF]
TOT=0
SeqItPed=0

While [Numero da OF]=[FaturaC->Numero da OF]
   SeqItPed=SeqItPed+1

  Print File ARQIMP Spaces(02)+Format("W03,AR",StrVal(SeqItPed))
  Print File ARQIMP Spaces(01)+Format("W11,AL",[Codigo do Item])
  Print File ARQIMP Spaces(01)+Format("W76,AL",[Descricao do Item])
  Print File ARQIMP Spaces(01)+Format("W03,Al",[Unidade Fiscal])

  If [Qtd1]-Int([Qtd1])>0
     Then Print File ARQIMP Format("W011.3,EIC",[Qtd1])
     Else Print File ARQIMP Format("W011,EIC",[Qtd1])
  EndIf

  Print File ARQIMP Format("W10.2,EIC",[Preco Unitario])
  Print File ARQIMP Format("W13.2,EIC",[Preco Total])+"\n"
  TOT   = TOT + [Preco Total]
  Skip
  If EOT() Then QuitLoop EndIf
EndWhile

Moveto "FaturaC"

Print File ARQIMP "------------------------------------------------------------------------------------------------------------------------------------"+"\n"

;********************* IMPRESSAO DOS TOTAIS **************************
Moveto "FaturaC"

Print File ARQIMP Spaces(102)+"\027E"+"Total.............."+"\027F"+Format("W11.2,EIC",TOT)+"\n"

Print File ARQIMP "          *** FAVOR MANTER O ENDERECO E O TELEFONE ATUALIZADOS ***"
Print File ARQIMP Spaces(36)+"\027E"+"Desconto"+Format("W06.2,EIC",[Taxa de Desconto]) + "%..."+"\027F"
Print File ARQIMP Format("W12.2,EIC",[Valor Desconto])+"\n"

Print File ARQIMP Spaces(102)+"\027E"+"Total Liquido......"+"\027F"+Format("W11.2,EIC",[Valor Mercadoria Liquido])+"\n\n"

Print File ARQIMP "\027E"+"Cond. Pagto: "+"\027F"+[Descricao CondPagto]
Print File ARQIMP "\027E"+"   NP: "+"\027F"+StrVal([Numero de Parcelas])
Print File ARQIMP "\027E"+"   CD: "+"\027F"+[Composicao de Dias]+"\n"

Print File ARQIMP "\027E"+"OBS.: "+"\027F"
Print File ARQIMP Format("W75,AL",SubStr([Observacao na Nota Fiscal],001,73))+"\n\n"


;************************ IMPRESSAO DOS VENCIMENTOS ****************************
Print File ARQIMP "\027E"
Print File ARQIMP "Vencimentos: "
Print File ARQIMP "\027F"
Array DATA[9]
Array VALOR[9]
K     = 0
KV    = 0

Moveto "FaturaV"
Moveto [Numero da OF]
Locate [FaturaC->Numero da OF]
While [Numero da OF]=[FaturaC->Numero da OF]
  K = K+1
  KK= K+1
  DATA[K]  = Format("D13",[Data])
  VALOR[K] = Format("W11.2,AL,EIC",[Valor])
  If K=4
     Then PARCELAS = K
          VENC()
          K=0
  EndIf
  Skip
  If EOT() Then QuitLoop EndIf
EndWhile
If K>0
   Then PARCELAS = K
        VENC()
EndIf
Print File ARQIMP "\n\n"


;************************* IMPRESSAO DO RODAPE *********************************
Print File ARQIMP Spaces(42)+"   /   /     -                                   " + "\n"
Print File ARQIMP Spaces(42)+"-------------------------------------------------" + "\n"
Print File ARQIMP Spaces(42)+"\027E"+"                 Data e Assinatura"+"\027F"+"\n"
Print File ARQIMP CI_6LPP
Print File ARQIMP CI_10CPP
Print File ARQIMP "\f"
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(12)
For I From 1 to K
  Print File ARQIMP VALOR[I]+Spaces(01)
EndFor
Print File ARQIMP "\n"

KV=KV+1

EndProc
;*******************************************************************************
;* Rotina para Verificar Referˆncia                                            *
;*******************************************************************************
Proc Verifica()

WSREF =[Codigo do Item]
WSDES ="" 
WSUNI =""

Moveto [Codigo do Item]
If Not BUSCA_LOOKUP("Codigo do Item",[Codigo do Item],"Item n„o Cadastrado !")
   Then Return
EndIf

WSREF=REG_BUSCA_LOOKUP["Referencia"]

If Not IsBlank(WSREF)
   Then WSREF=SubStr([Codigo do Item],1,6)+"0000"
        If Not BUSCA_LOOKUP("Codigo do Item",WSREF,"SM")
            Then Return
        EndIf 
   Else WSREF=[Codigo do Item]           
EndIf

WSDES=REG_BUSCA_LOOKUP["Descricao do Item"]
WSUNI=REG_BUSCA_LOOKUP["Unidade Fiscal"]   

EndProc

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

AGUARDE("Imprimindo Romaneio -> "+NOTAFISC)

Do_It!

TABELA_TEMP=Cria_Tabela("","Numero da OF(A6*),Sequencia(S*),Referencia(A20*),Codigo do Item(A10),Codigo da Cor_Tecido(A4),"+
"Preco Unitario($),Descricao do Item(A100),Descricao da Cor_Tecido(A20),Qtd1(S),Qtd2(S),Qtd3(S),Qtd4(S),Qtd5(S),Qtd6(S),Qtd7(S),Qtd8(S),Qtd9(S),"+
"Qtd10(S),Qtd11(S),Qtd12(S),Quantidade(N),Unidade Fiscal(A4),Preco Total($),Aliquota de IPI(N),Aliquota de ICMS(N),Peso Liquido(N),Peso Embalagem(N),Volumes(N)")

Adiciona_Indice_Secundario(TABELA_TEMP,"Indftgrd","Codigo do Item,Codigo da Cor_Tecido,Preco Unitario,Numero da OF,Sequencia")

CoEditKey

{TableAdd} TypeIn TABELA_TEMP Enter

SEQG=1
Moveto "FaturaI"
Moveto [Numero da OF]
Locate [FaturaC->Numero da OF]
While [Numero da OF]=[FaturaC->Numero da OF]    

Verifica()   
Moveto "FaturaI"

  WSPRU = [Preco Unitario]
  WSQTD = [Quantidade    ]

  Moveto TABELA_TEMP
  Moveto [Codigo do Item]
  Locate IndexOrder by "Indftgrd" WSREF,"",WSPRU
  If Not RetVal
     Then End
          Down
          [Numero da OF           ]=[FaturaI->Numero da OF]
          [Sequencia              ]=SEQG
          [Codigo do Item         ]=WSREF
          [Descricao do Item      ]=WSDES
          [Unidade Fiscal         ]=WSUNI
          [Qtd1                   ]=WSQTD
          [Referencia             ]=WSREF
          [Preco Unitario         ]=WSPRU
          SEQG=SEQG+1
     Else [Qtd1       ]=[Qtd1]+WSQTD     
  EndIf
  [Preco Total]=[Qtd1]*[Preco Unitario]

  UnLockRecord

  Moveto "FaturaI"
  Skip
  If EOT() Then QuitLoop EndIf
EndWhile

Print File ARQIMP CI_10CPP
Print File ARQIMP CI_6LPP

IMPFAT()

;---- Imprime Arquivo Impressao ------------------------------------------------
ImpriArq(ARQIMP)

Do_It!
Moveto TABELA_TEMP
ClearImage
Delete TABELA_TEMP

Moveto "FaturaC"
CoEditKey
Release Vars VALOR,DATA,ARRAY_TM,WSREF,COLUNA
Release Procs IMPFAT,VENC,Verifica_Array_Grade
LIMPAGUARDE()
