;** RO_0246 ********************************************************************
;** Imprime a Digitacao do Romaneio                   ** RED BLUE ***
;*******************************************************************************
;Autor: Visao/Vise
;Desenvolvido  em: 12/03/2010 por Humberto F.Santos.
;Alterado em     : 19/04/2010 por Cleber Alves
;Motivo Alteracao: Agrupamento do Item
;Ultima Alteracao: 05/07/2017 por Rafael Matos
;Motivo Alteracao: Retirado "Fatura TG", alterado para tabela tempor ria

;*******************************************************************************
;* Rotina para Impressao do Cabecalho                                          *
;*******************************************************************************
Proc Cabecalho()
Print File ARQIMP CI_10CPP+CI_6LPP+"\n"
NPAG=NPAG+1

Moveto "FaturaC"
Print File ARQIMP  "--------------------------------------------------------------------------------" + "\n"
Print File ARQIMP "Red Blue - (37)3212-5458    " + "Emissao:"+Format("D13",Today())+"-"+Time()+" Pag: "+StrVAl(NPAG)+"\n"
Print File ARQIMP  "--------------------------------------------------------------------------------" + "\n\n"

Print File ARQIMP Spaces(02)+"Operacao: "+Format("W25,AL",[Descricao da Destinacao])
Print File ARQIMP Spaces(24)+"Romaneio No "+Format("W06.3,AL",NOTAFISC)+"\n"
Print File ARQIMP Spaces(61)+"Pedido No "+Format("W06.3,AL",[Numero do Pedido])+"\n"
Print File ARQIMP Spaces(58)+"Carregamento No "+Format("W06.3,AL",[Numero_Carregamento])+"\n"

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

Print File ARQIMP Spaces(02)+"Local Fatura..: "
Print File ARQIMP Format("W33,AL",[Endereco])
Print File ARQIMP Spaces(02)+Format("W20,AL",[Bairro])+"\n"
Print File ARQIMP Spaces(18)+Format("W10,AL",[CEP])
Print File ARQIMP Spaces(02)+Format("W20,AL",[Cidade])+" -"
Print File ARQIMP Spaces(01)+Format("W04,AL",[UF])+"\n"
Print File ARQIMP Spaces(18)+Format("W18,AL",[CGC])
Print File ARQIMP Spaces(02)+Format("W16,AL",[IE])+ Spaces(02)+"TEL:"+Format("W15,AL",[Telefone])+"\n"

Print File ARQIMP Spaces(02)+"Dt Emissao Dt Saida   Volumes  Representante "+ "\n"
Print File ARQIMP Spaces(02)+"---------- --------   -------  ------------------------"+"\n"

Print File ARQIMP Spaces(01)+Format("D13",[Data de Emissao])
Print File ARQIMP Spaces(01)+Format("D13",[Data de Saida])
Print File ARQIMP Spaces(01)+Format("W07,AR",[Numero de Volumes])+Spaces(02)
Print File ARQIMP Spaces(01)+Format("W04,AL",[Codigo do Representante])
Print File ARQIMP Spaces(01)+Format("W25,AL",[Nome Fantasia Represent])+CI_15CPP+"\n\n"


;Print File ARQIMP Spaces(02)+"Separador:........................................" + Spaces(03)+"Conferente:................................"+"\n"

;********************* IMPRESSAO DOS ITENS ********************************
Print File ARQIMP "-------------------------------------------------------------------"+

                       "---------------------------------------------------------------------"+"\n"
;Print File ARQIMP "|   |     |                                  | PP |  P |  M ;|  G | GG "+
 ;                      "| EG |    |    |    |    |    |    |      |  |        |          |"+"\n"
Print File ARQIMP "|Seq|Ref  |Descricao                         | 02 | 04 | 06 | 08 | 10 "+
                       "| 12 | 14 | 16 |    |    |    |    | Total|UN |Vr.Unit.| Vr.Total |"+"\n"
Print File ARQIMP "---------------------------------------------------------------------------"+
                  "-------------------------------------------------------------"+"\n"


NLINHA=19
EndProc

;*******************************************************************************
;* Rotina para Impressao do Rodape                                             *
;*******************************************************************************
Proc Rodape()
Print File ARQIMP Spaces(110)+"** Continua =>"+"\n"
Print File ARQIMP
"----------------------------------------------------------------------"+
"-----------------------------------------------------------------"+"\n\f"
EndProc

;*******************************************************************************
;* Rotina para Impressao do Relatorio                                          *
;*******************************************************************************
Proc IMPFAT()
;************************* IMPRESSAO DO CABECALHO *****************************
If Not IsAssigned(NOMEMPRESA)
   Then NOMEMPRESA=""
EndIf
NPAG=0
Cabecalho()

Moveto TABELA_TEMP
Moveto [Numero da OF]
Locate [FaturaC->Numero da OF]
QUANT  =0
QUANT1 =0
QUANT2 =0
QUANT3 =0
QUANT4 =0
QUANT5 =0
QUANT6 =0
QUANT7 =0
QUANT8 =0
QUANT9 =0
QUANT10=0
QUANT11=0
QUANT12=0
TOT=0
SeqItPed=0
While [Numero da OF]=[FaturaC->Numero da OF]
   If NLINHA>57
      Then Rodape()
           Cabecalho()
           Moveto TABELA_TEMP
   EndIf

   SeqItPed=SeqItPed+1
   Print File ARQIMP Format("W04,AR",StrVal(SeqItPed))+"|"
   Print File ARQIMP Format("W05,AL",[Codigo do Item])+"|"
   Print File ARQIMP Spaces(01)+Format("W30,AL",[Descricao do Item])+"   |"
   Print File ARQIMP Format("W04,EIC",[Qtd1    ])+"|"
   Print File ARQIMP Format("W04,EIC",[Qtd2    ])+"|"
   Print File ARQIMP Format("W04,EIC",[Qtd3    ])+"|"
   Print File ARQIMP Format("W04,EIC",[Qtd4    ])+"|"
   Print File ARQIMP Format("W04,EIC",[Qtd5    ])+"|"
   Print File ARQIMP Format("W04,EIC",[Qtd6    ])+"|"
   Print File ARQIMP Format("W04,EIC",[Qtd7    ])+"|"
   Print File ARQIMP Format("W04,EIC",[Qtd8    ])+"|"
   Print File ARQIMP Format("W04,EIC",[Qtd9    ])+"|"
   Print File ARQIMP Format("W04,EIC",[Qtd10   ])+"|"
   Print File ARQIMP Format("W04,EIC",[Qtd11   ])+"|"
   Print File ARQIMP Format("W04,EIC",[Qtd12   ])+"|"
   Print File ARQIMP Format("W06,EIC",[Qtd1   ]+[Qtd2   ]+[Qtd3   ]+[Qtd4    ]+
                                      [Qtd5   ]+[Qtd6   ]+[Qtd7   ]+[Qtd8    ]+
                                      [Qtd9   ]+[Qtd10  ]+[Qtd11  ]+[Qtd12   ])+"|"
   Print File ARQIMP Spaces(01)+Format("W02,AL",[Unidade Fiscal])+"|"
   Print File ARQIMP Format("W08.2,EIC",[Preco Unitario])+"|"
   Print File ARQIMP Format("W10.2,EIC",[Preco Total])+"|"+"\n"
   NLINHA=NLINHA+1

   QUANT  =QUANT  +[Qtd1    ]+[Qtd2    ]+[Qtd3    ]+[Qtd4    ]+
                   [Qtd5    ]+[Qtd6    ]+[Qtd7    ]+[Qtd8    ]+
                   [Qtd9    ]+[Qtd10   ]+[Qtd11   ]+[Qtd12   ]
   QUANT1   =QUANT1   +[Qtd1    ]
   QUANT2   =QUANT2   +[Qtd2    ]
   QUANT3   =QUANT3   +[Qtd3    ]
   QUANT4   =QUANT4   +[Qtd4    ]
   QUANT5   =QUANT5   +[Qtd5    ]
   QUANT6   =QUANT6   +[Qtd6    ]
   QUANT7   =QUANT7   +[Qtd7    ]
   QUANT8   =QUANT8   +[Qtd8    ]
   QUANT9   =QUANT9   +[Qtd9    ]
   QUANT10  =QUANT10  +[Qtd10   ]
   QUANT11  =QUANT11  +[Qtd11   ]
   QUANT12  =QUANT12  +[Qtd12   ]
   TOT=TOT+[Preco Total]
   Skip
   If EOT() Then QuitLoop EndIf
EndWhile

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

Print File ARQIMP Spaces(45)
Print File ARQIMP Format("W05,EIC",QUANT1   )
Print File ARQIMP Format("W05,EIC",QUANT2   )
Print File ARQIMP Format("W05,EIC",QUANT3   )
Print File ARQIMP Format("W05,EIC",QUANT4   )
Print File ARQIMP Format("W05,EIC",QUANT5   )
Print File ARQIMP Format("W05,EIC",QUANT6   )
Print File ARQIMP Format("W05,EIC",QUANT7   )
Print File ARQIMP Format("W05,EIC",QUANT8   )
Print File ARQIMP Format("W05,EIC",QUANT9   )
Print File ARQIMP Format("W05,EIC",QUANT10  )
Print File ARQIMP Format("W05,EIC",QUANT11  )
Print File ARQIMP Format("W05,EIC",QUANT12  )
Print File ARQIMP Format("W08,EIC",QUANT)
Print File ARQIMP Format("W23.2,EIC",TOT)+"\n"
Print File ARQIMP CI_10CPP+CI_6LPP+"\n"

Print File ARQIMP Spaces(48)+ "Desconto"+Format("W06.2,EIC",[Taxa de Desconto])+"%..."
Print File ARQIMP Spaces(03)+ Format("W10.2,EIC",[Valor Desconto])+"\n"
Print File ARQIMP Spaces(48)+ "Frete............."+Spaces(05)+Format("W08.2,EIC",[Valor do Frete])+""+"\n"
Print File ARQIMP Spaces(48)+"Total Liquido......"+Format("W12.2,EIC",[Valor Mercadoria Liquido])+"\n\n"

If NLINHA>57
   Then Rodape()
        Cabecalho()
EndIf

;********************* IMPRESSAO DOS VENCIMENTOS **************************
Print File ARQIMP Spaces(02)+"Condicao de Pagamento: "+[Descricao CondPagto]+"\n"

Print File ARQIMP Spaces(12)+"Vencimentos: "

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
  DATA[K]  = Format("D13",[Data])
  VALOR[K] = Format("W11.2,AL,EIC",[Valor])
  If K=4
     Then VENC()
          K=0
  EndIf
  Skip
  If EOT() Then QuitLoop EndIf
EndWhile
If K>0
   Then VENC()
EndIf
Print File ARQIMP "\n"

Moveto"Faturac"
Print File ARQIMP "Observacoes  : "
Print File ARQIMP CI_INEG
If Substr([Observacao na Nota Fiscal],01,65)<>""
   Then Print File ARQIMP Spaces(03)+Format("W65,AL",SubStr([Observacao na Nota Fiscal],001,65))+"\n"
   Else Print File ARQIMP "\n"
EndIf
If Substr([Observacao na Nota Fiscal],66,65)<>""
   Then Print File ARQIMP Spaces(03)+Format("W65,AL",SubStr([Observacao na Nota Fiscal],66,65))+"\n"
EndIf
If Substr([Observacao na Nota Fiscal],132,197)<>""
   Then Print File ARQIMP Spaces(03)+Format("W65,AL",SubStr([Observacao na Nota Fiscal],131,65))+"\n"
EndIf
;Print File ARQIMP "NA OPORTUNIDADE PEDIMOS QUE NOS ENVIE OS CHEQUES REFERENTE A ESTE PEDIDO,\n"
;Print File ARQIMP "CONFORME A RELACAO ACIMA AO NOSSO ENDERECO, VIA SEDEX OU CARTA COM A.R.\n"
;Print File ARQIMP CI_FNEG

;************************* IMPRESSAO DO RODAPE ********************************

Print File ARQIMP CI_10CPP+CI_6LPP+"\f"

EndProc

;*******************************************************************************
;* Rotina de Impressao dos Vencimentos                                         *
;*******************************************************************************
Proc VENC()
If KV>0
   Then Print File ARQIMP Spaces(25)
EndIf
For I From 1 to K
  Print File ARQIMP  DATA[I]+Spaces(02)
EndFor
Print File ARQIMP "\n"
Print File ARQIMP Spaces(25)
For I From 1 to K
  Print File ARQIMP VALOR[I]+Spaces(02)
EndFor
Print File ARQIMP "\n"

KV=KV+1
EndProc


;*******************************************************************************
;* Rotina Monta Aray Grade                                                     *
;*******************************************************************************
Proc Verifica_Array_Grade()
Private i,wsite,CODANTERIOR

WSREF =[Codigo do Item]
COLUNA=0

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"]

Moveto "\\Sistemas\\Estoque\\TpGrade"
Moveto [Codigo da Grade]
Locate REG_BUSCA_LOOKUP["Codigo da Grade"]
If Not RetVal
   Then Mensagem("Item "+[FaturaI->Codigo do Item]+" sem grade definida !")
        Return
   Else CopyToArray REG_BUSCA_LOOKUP
EndIf

Moveto "FaturaI"
Array_TM[1] =LTrim(RTrim(REG_BUSCA_LOOKUP["Codigo Item1"]))
Array_TM[2] =LTrim(RTrim(REG_BUSCA_LOOKUP["Codigo Item2"]))
Array_TM[3] =LTrim(RTrim(REG_BUSCA_LOOKUP["Codigo Item3"]))
Array_TM[4] =LTrim(RTrim(REG_BUSCA_LOOKUP["Codigo Item4"]))
Array_TM[5] =LTrim(RTrim(REG_BUSCA_LOOKUP["Codigo Item5"]))
Array_TM[6] =LTrim(RTrim(REG_BUSCA_LOOKUP["Codigo Item6"]))
Array_TM[7] =LTrim(RTrim(REG_BUSCA_LOOKUP["Codigo Item7"]))
Array_TM[8] =LTrim(RTrim(REG_BUSCA_LOOKUP["Codigo Item8"]))
Array_TM[9] =LTrim(RTrim(REG_BUSCA_LOOKUP["Codigo Item9"]))
Array_TM[10]=LTrim(RTrim(REG_BUSCA_LOOKUP["Codigo Item10"]))
Array_TM[11]=LTrim(RTrim(REG_BUSCA_LOOKUP["Codigo Item11"]))
Array_TM[12]=LTrim(RTrim(REG_BUSCA_LOOKUP["Codigo Item12"]))

For i from 1 to 12
  If Not IsBlank(WSREF)
     Then QuitLoop
  EndIf
  If LTrim(RTrim(SubStr(Array_TM[i],1,2)))<>""
     Then If Len(Array_TM[i])=1
             Then CODANTERIOR=SubStr([Codigo do Item],1,7)+"00"+Array_TM[i]
             Else CODANTERIOR=SubStr([Codigo do Item],1,6)+"00"+Array_TM[i]
          EndIf
          If BUSCA_LOOKUP("Codigo do Item",CODANTERIOR,"SM")
             Then WSREF=REG_BUSCA_LOOKUP["Referencia"]
          EndIf
  EndIf
EndFor

For i from 1 to 12
  If Len(RTrim(SubStr(Array_TM[i],1,2)))=1
     Then If SubStr([Codigo do Item],10,1)=SubStr(Array_TM[i],1,1)
             Then Coluna=i
                  QuitLoop
          EndIf
     Else If SubStr([Codigo do Item],9,2)=SubStr(Array_TM[i],1,2)
             Then Coluna=i
                  QuitLoop
          EndIf
  EndIf
EndFor

EndProc
;*******************************************************************************
;* Rotina Monta Aray Grade                                                     *
;*******************************************************************************
Proc Grava()
Moveto TABELA_TEMP
   End Down
   [Numero da OF           ]=item["Numero da OF"        ]
   [Sequencia              ]=SEQG
   [Referencia             ]=REFANT
   [Codigo do Item         ]=REFANT
   [Codigo da Cor_Tecido   ]=item["Codigo da Cor_Tecido"]
   [Preco Unitario         ]=item["Preco Unitario"      ]   
   [Descricao do Item]=Substr(item["Descricao do Item"],1,23)
   [Descricao da Cor_Tecido]=item["Descricao da Cor_Tecido"]
   [Unidade Fiscal         ]=item["Unidade Fiscal"         ]
   [Qtd1  ]=QUANT1
   [Qtd2  ]=QUANT2
   [Qtd3  ]=QUANT3
   [Qtd4  ]=QUANT4
   [Qtd5  ]=QUANT5
   [Qtd6  ]=QUANT6
   [Qtd7  ]=QUANT7
   [Qtd8  ]=QUANT8
   [Qtd9  ]=QUANT9
   [Qtd10 ]=QUANT10
   [Qtd11 ]=QUANT11
   [Qtd12 ]=QUANT12
   [Preco Total]=[Preco Unitario]*([Qtd1]+[Qtd2]+[Qtd3]+[Qtd4]+[Qtd5]+[Qtd6]+
                                   [Qtd7]+[Qtd8]+[Qtd9]+[Qtd10]+[Qtd11]+[Qtd12])
UnLockRecord
SEQG=SEQG+1          
    
QUANT1 =0
QUANT2 =0
QUANT3 =0
QUANT4 =0
QUANT5 =0
QUANT6 =0
QUANT7 =0
QUANT8 =0
QUANT9 =0
QUANT10=0
QUANT11=0
QUANT12=0
                                 
EndProc
;*******************************************************************************
;* Rotina Principal                                                            *
;*******************************************************************************
ArqImp=PrivDir()+"Ro_0246.Txt"
If IsFile(ArqImp)
   Then Run NoRefresh "Del "+ArqImp
EndIf

Array Array_TM[12]
QUANT1 =0
QUANT2 =0
QUANT3 =0
QUANT4 =0
QUANT5 =0
QUANT6 =0
QUANT7 =0
QUANT8 =0
QUANT9 =0
QUANT10=0
QUANT11=0
QUANT12=0
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
{TableAdd} "\\Sistemas\\Estoque\\TpGrade" Enter

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

  Verifica_Array_Grade()  
  Moveto "FaturaI"
  If WSREF<>REFANT 
     Then
        If REFANT <> "***"
           Then Grava()             
        EndIf     
        Moveto "FaturaI"   
        CopytoArray item       
  EndIf 
  Switch    
    Case Coluna=0 : QUANT1 =QUANT1+[FaturaI->Quantidade]
    Case Coluna=1 : QUANT1 =QUANT1+[FaturaI->Quantidade]
    Case Coluna=2 : QUANT2 =QUANT2+[FaturaI->Quantidade]
    Case Coluna=3 : QUANT3 =QUANT3+[FaturaI->Quantidade]
    Case Coluna=4 : QUANT4 =QUANT4+[FaturaI->Quantidade]
    Case Coluna=5 : QUANT5 =QUANT5+[FaturaI->Quantidade]
    Case Coluna=6 : QUANT6 =QUANT6+[FaturaI->Quantidade]
    Case Coluna=7 : QUANT7 =QUANT7+[FaturaI->Quantidade]
    Case Coluna=8 : QUANT8 =QUANT8+[FaturaI->Quantidade]
    Case Coluna=9 : QUANT9 =QUANT9+[FaturaI->Quantidade]
    Case Coluna=10: QUANT10=QUANT10+[FaturaI->Quantidade]
    Case Coluna=11: QUANT11=QUANT11+[FaturaI->Quantidade]
    Case Coluna=12: QUANT12=QUANT12+[FaturaI->Quantidade]    
  EndSwitch  
  Moveto "FaturaI"
  Skip 
  REFANT = WSREF
  If EOT() Then QuitLoop EndIf
EndWhile
Grava()
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 "\\Sistemas\\Estoque\\TpGrade"
ClearImage

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