;** RO_0243T *******************************************************************
;** Imprime a Digitacao do Romaneio                              ** Fluoritta **
;*******************************************************************************
;Autor: Visao/Vise
;Desenvolvido  em: 29/04/2013 por Bruno Carvalho
;Ultima Alteracao: 05/07/2017 por Rafael Matos
;Motivo Alteracao: Retirado "Fatura TG", alterado para tabela tempor ria
;Ultima Alteracao: 05/08/2017 por Bruno Carvalho
;Motivo Alteracao: Substituido [Valor IR] -> [Valor DebitoCredito]

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

Proc Cabecalho()

Print File ARQIMP CI_10CPP+CI_6LPP
;Print File ARQIMP CI_10CPP+CI_6LPP+"\n"
NPAG=NPAG+1

Moveto "FaturaC"
Print File ARQIMP  "--------------------------------------------------------------------------------" + "\n"
Print File ARQIMP Spaces(28)+"Emissao: "+Format("D13",Today())+"-"+Time()+"              Pagina: "+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(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("W25,AL",[IE])+"\n"

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

Print File ARQIMP Spaces(02)+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])
Print File ARQIMP Spaces(01)+Format("W25,AL",[Nome Fantasia Represent])+CI_15CPP+"\n\n"

;********************* IMPRESSAO DOS ITENS ********************************
Print File ARQIMP "Seq Item                                         P    M    G   GG     "+
"                                     Total UN   PrUnit    PrTotal"+"\n"
Print File ARQIMP "--- ----------------------------------------- ---- ---- ---- ---- ----"+
" ---- ---- ---- ---- ---- ---- ---- ------ -- -------- ----------"+"\n"
                                  ;999 999.9 123456789012345678901234567890 12345678901234567890 99.999 99.999 99.999 99.999 99.999 XX 9.999,99 999.999,99
NLINHA=15
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()

If IsAssigned(FATURA_TROCA)
   Then ; *** ENTRADAS *********************************************************
        Print File ARQIMP "* Entradas\n"
        
        Moveto TABELA_TEMP2
        Moveto [Numero da OF]
        Locate [FaturaC->Numero da OF]
        
        QUANTDEV = 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
        TOTDEV   = 0
        SeqItPed = 0
        
        While [Numero da OF]=[FaturaC->Numero da OF]
           If NLINHA>60
              Then Rodape()
                   Cabecalho()
                   Moveto TABELA_TEMP2
           EndIf
        
           SeqItPed=SeqItPed+1
           Print File ARQIMP Format("W03,AR",StrVal(SeqItPed))
           Print File ARQIMP Spaces(01)+Format("W10,AL",[Codigo do Item])
           Print File ARQIMP Spaces(01)+Format("W30,AL",[Descricao do Item])
           Print File ARQIMP Format("W05,EIC",[Qtd1    ])
           Print File ARQIMP Format("W05,EIC",[Qtd2    ])
           Print File ARQIMP Format("W05,EIC",[Qtd3    ])
           Print File ARQIMP Format("W05,EIC",[Qtd4    ])
           Print File ARQIMP Format("W05,EIC",[Qtd5    ])
           Print File ARQIMP Format("W05,EIC",[Qtd6    ])
           Print File ARQIMP Format("W05,EIC",[Qtd7    ])
           Print File ARQIMP Format("W05,EIC",[Qtd8    ])
           Print File ARQIMP Format("W05,EIC",[Qtd9    ])
           Print File ARQIMP Format("W05,EIC",[Qtd10   ])
           Print File ARQIMP Format("W05,EIC",[Qtd11   ])
           Print File ARQIMP Format("W05,EIC",[Qtd12   ])
           Print File ARQIMP Format("W07,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("W09.2,EIC",[Preco Unitario])
           Print File ARQIMP Format("W11.2,EIC",[Preco Total])+"\n"
           NLINHA=NLINHA+1
           
           QUANTDEV = QUANTDEV  +[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   ]
           TOTDEV   = TOTDEV   +[Preco Total]
           Skip
           If EOT() Then QuitLoop EndIf
        EndWhile
            
        Moveto "FaturaC"                                                           
        Print File ARQIMP          "                                              ---- ---- ---- ---- ----"+
        " ---- ---- ---- ---- ---- ---- ---- ------             ----------"+"\n"
        
        Print File ARQIMP Spaces(27)
        Print File ARQIMP "* Total Entradas  "
        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("W07,EIC",QUANTDEV )
        Print File ARQIMP Format("W23.2,EIC",TOTDEV )+"\n"

        ; *** SAIDAS ***********************************************************
        Print File ARQIMP "* Saidas\n"
EndIf

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>60
      Then Rodape()
           Cabecalho()
           Moveto TABELA_TEMP
   EndIf

   SeqItPed=SeqItPed+1
   Print File ARQIMP Format("W03,AR",StrVal(SeqItPed))
   Print File ARQIMP Spaces(01)+Format("W10,AL",[Codigo do Item])
   Print File ARQIMP Spaces(01)+Format("W30,AL",[Descricao do Item])
   Print File ARQIMP Format("W05,EIC",[Qtd1    ])
   Print File ARQIMP Format("W05,EIC",[Qtd2    ])
   Print File ARQIMP Format("W05,EIC",[Qtd3    ])
   Print File ARQIMP Format("W05,EIC",[Qtd4    ])
   Print File ARQIMP Format("W05,EIC",[Qtd5    ])
   Print File ARQIMP Format("W05,EIC",[Qtd6    ])
   Print File ARQIMP Format("W05,EIC",[Qtd7    ])
   Print File ARQIMP Format("W05,EIC",[Qtd8    ])
   Print File ARQIMP Format("W05,EIC",[Qtd9    ])
   Print File ARQIMP Format("W05,EIC",[Qtd10   ])
   Print File ARQIMP Format("W05,EIC",[Qtd11   ])
   Print File ARQIMP Format("W05,EIC",[Qtd12   ])
   Print File ARQIMP Format("W07,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("W09.2,EIC",[Preco Unitario])
   Print File ARQIMP Format("W11.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"

If IsAssigned(FATURA_TROCA)
   Then Print File ARQIMP Spaces(29)
        Print File ARQIMP "* Total Saidas  "
   Else Print File ARQIMP Spaces(45)      
EndIf

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("W07,EIC",QUANT)
Print File ARQIMP Format("W23.2,EIC",TOT)+"\n"
Print File ARQIMP CI_10CPP+CI_6LPP+"\n"

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

;********************* IMPRESSAO DOS VENCIMENTOS **************************
If [Valor DebitoCredito]>0
   Then DIF="Credito"
   Else If [Valor DebitoCredito]<0                                         
           Then DIF="Debito"                                     
           Else DIF="Zero"              
        EndIf                               
EndIf

If IsAssigned(FATURA_TROCA)
   Then Print File ARQIMP "Quantidade ->  Vendida(s): "+Format("W8,AL",QUANT)+"  Devolvida(s): "+Format("W8,AL",QUANTDEV)+
                                      "   Liquida(s): "+Format("W8,AL",QUANT-QUANTDEV)+"\n\n" 
EndIf
     
If [Valor Desconto]>0
   Then Print File ARQIMP Spaces(56)+"Desconto: "+Format("W12.2,EIC",[Valor Desconto])+Spaces(3)+"\n"
EndIf

If IsAssigned(FATURA_TROCA)
   Then Print File ARQIMP Spaces(59)+"Saida: "+Format("W12.2,EIC",TOT)+"\n"                    
        Print File ARQIMP Spaces(57)+"Entrada: "+Format("W12.2,EIC",TOTDEV)+"\n"
        
        AUX_DIF=Format("W12","\("+DIF+"\)")+Spaces(3)
        
        Print File ARQIMP Spaces(42)+AUX_DIF
        Print File ARQIMP            "Liquido: "+Format("W12.2,EIC",[Valor DebitoCredito])+"\n\n"
   Else Print File ARQIMP Spaces(57)+"Liquido: "+Format("W12.2,EIC",[Valor Nota Fiscal])+"\n\n"
EndIf                                       

Print File ARQIMP Spaces(02)+"Condicao de Pagamento: "+[Descricao CondPagto]+"\n"


;********************* IMPRESSAO DOS VENCIMENTOS *******************************
Print File ARQIMP "\018"
Print File ARQIMP "\027E"

Print File ARQIMP "            Vencimentos: "

Array DATA[5]
Array VALOR[5]
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("W10.2,AR,EIC",[Valor])
  If K=5
     Then VENC()
          K=0
  EndIf
  Skip
  If EOT() Then QuitLoop EndIf
EndWhile
If K>0
   Then VENC()
EndIf
Print File ARQIMP "\n"
Print File ARQIMP "\027F"

Moveto"Faturac"
Print File ARQIMP "Observacoes  : "
Print File ARQIMP CI_INEG
If Substr([Observacao na Nota Fiscal],01,65)<>""
   Then Print File ARQIMP Spaces(15)+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(15)+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(15)+Format("W65,AL",SubStr([Observacao na Nota Fiscal],131,65))+"\n"
EndIf
Print File ARQIMP "SO TROCAMOS NO PRAZO DE 30 DIAS - NAO TROCAMOS PECAS BRANCAS\n"
Print File ARQIMP "TROCAMOS SOMENTE PECA POR PECA - NAO RETIRAR A ETIQUETA\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(01)
EndFor
Print File ARQIMP "\n"
Print File ARQIMP Spaces(25)
For I From 1 to K
  Print File ARQIMP VALOR[I]+Spaces(01)
EndFor
Print File ARQIMP "\n\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 Principal                                                            *
;*******************************************************************************
ArqImp=PrivDir()+"RO_0243.Txt"
If IsFile(ArqImp)
   Then Run NoRefresh "Del "+ArqImp
EndIf

Array Array_TM[12]

AGUARDE("Imprimindo Romaneio -> "+NOTAFISC)
Do_It!

Copy "\\Base\\Sistemas\\Vendas\\FaturaI"  PrivDir()+"FaturaI"
If IsAssigned(FATURA_TROCA)
   Then Copy "\\Base\\Sistemas\\Vendas\\FaturaId" PrivDir()+"FaturaId"
EndIf

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

TABELA_TEMP2=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_TEMP2,"Indftgrd","Codigo do Item,Codigo da Cor_Tecido,Preco Unitario,Numero da OF,Sequencia")

CoEditKey

{TableAdd} TypeIn TABELA_TEMP Enter
{TableAdd} TypeIn PrivDir()+"FaturaI"  Enter
{TableAdd} "\\Sistemas\\Estoque\\TpGrade" Enter

If IsAssigned(FATURA_TROCA)
   Then {TableAdd} TypeIn TABELA_TEMP2 Enter
        {TableAdd} TypeIn PrivDir()+"FaturaId" Enter
                     
        SEQG=1
        Moveto "FaturaId"
        Locate [FaturaC->Numero da OF]
        While [Numero da OF]=[FaturaC->Numero da OF]

        Verifica_Array_Grade()
        Moveto "FaturaId"

        WSCOR =[Codigo da Cor_Tecido]
        WSPRU =[Preco Unitario      ]

        Moveto TABELA_TEMP2
        Moveto [Codigo do Item]
        Locate IndexOrder by "Indftgrd" WSREF
        If Not RetVal
           Then End
                Down
                [Numero da OF           ]=[FaturaId->Numero da OF           ]
                [Sequencia              ]=BlankNum()
                [Referencia             ]=WSREF
                [Codigo do Item         ]=WSREF
                [Codigo da Cor_Tecido   ]=[FaturaId->Codigo da Cor_Tecido   ]
                [Preco Unitario         ]=[FaturaId->Preco Unitario         ]
                [Descricao do Item      ]=[FaturaId->Descricao do Item      ]
      
                [Descricao do Item      ]=Substr([FaturaId->Descricao do Item],1,23)
      
                [Descricao da Cor_Tecido]=[FaturaId->Descricao da Cor_Tecido]
                [Unidade Fiscal         ]=[FaturaId->Unidade Fiscal         ]
                SEQG=SEQG+1
        EndIf

        Switch
          Case Coluna=0 : [Qtd1  ]=[Qtd1  ]+[FaturaId->Quantidade]
          Case Coluna=1 : [Qtd1  ]=[Qtd1  ]+[FaturaId->Quantidade]
          Case Coluna=2 : [Qtd2  ]=[Qtd2  ]+[FaturaId->Quantidade]
          Case Coluna=3 : [Qtd3  ]=[Qtd3  ]+[FaturaId->Quantidade]
          Case Coluna=4 : [Qtd4  ]=[Qtd4  ]+[FaturaId->Quantidade]
          Case Coluna=5 : [Qtd5  ]=[Qtd5  ]+[FaturaId->Quantidade]
          Case Coluna=6 : [Qtd6  ]=[Qtd6  ]+[FaturaId->Quantidade]
          Case Coluna=7 : [Qtd7  ]=[Qtd7  ]+[FaturaId->Quantidade]
          Case Coluna=8 : [Qtd8  ]=[Qtd8  ]+[FaturaId->Quantidade]
          Case Coluna=9 : [Qtd9  ]=[Qtd9  ]+[FaturaId->Quantidade]
          Case Coluna=10: [Qtd10 ]=[Qtd10 ]+[FaturaId->Quantidade]
          Case Coluna=11: [Qtd11 ]=[Qtd11 ]+[FaturaId->Quantidade]
          Case Coluna=12: [Qtd12 ]=[Qtd12 ]+[FaturaId->Quantidade]
        EndSwitch
        
        [Preco Total]=[Preco Unitario]*([Qtd1]+[Qtd2]+[Qtd3]+[Qtd4]+[Qtd5]+[Qtd6]+
                                        [Qtd7]+[Qtd8]+[Qtd9]+[Qtd10]+[Qtd11]+[Qtd12])
        UnLockRecord
      
        Moveto "FaturaId"
        Skip
        If EOT() Then QuitLoop EndIf
      EndWhile
EndIf      
      
SEQG=1
Moveto "FaturaI"
Locate [FaturaC->Numero da OF]
While [Numero da OF]=[FaturaC->Numero da OF]

  Verifica_Array_Grade()
  Moveto "FaturaI"

  WSCOR =[Codigo da Cor_Tecido]
  WSPRU =[Preco Unitario      ]

  Moveto TABELA_TEMP
  Moveto [Codigo do Item]
  Locate IndexOrder by "Indftgrd" WSREF
  If Not RetVal
     Then End
          Down
          [Numero da OF           ]=[FaturaI->Numero da OF           ]
          [Sequencia              ]=BlankNum()
          [Referencia             ]=WSREF
          [Codigo do Item         ]=WSREF
          [Codigo da Cor_Tecido   ]=[FaturaI->Codigo da Cor_Tecido   ]
          [Preco Unitario         ]=[FaturaI->Preco Unitario         ]
          [Descricao do Item      ]=[FaturaI->Descricao do Item      ]

          [Descricao do Item]=Substr([FaturaI->Descricao do Item],1,23)

          [Descricao da Cor_Tecido]=[FaturaI->Descricao da Cor_Tecido]
          [Unidade Fiscal         ]=[FaturaI->Unidade Fiscal         ]
          SEQG=SEQG+1
  EndIf

  Switch
    Case Coluna=0 : [Qtd1  ]=[Qtd1  ]+[FaturaI->Quantidade]
    Case Coluna=1 : [Qtd1  ]=[Qtd1  ]+[FaturaI->Quantidade]
    Case Coluna=2 : [Qtd2  ]=[Qtd2  ]+[FaturaI->Quantidade]
    Case Coluna=3 : [Qtd3  ]=[Qtd3  ]+[FaturaI->Quantidade]
    Case Coluna=4 : [Qtd4  ]=[Qtd4  ]+[FaturaI->Quantidade]
    Case Coluna=5 : [Qtd5  ]=[Qtd5  ]+[FaturaI->Quantidade]
    Case Coluna=6 : [Qtd6  ]=[Qtd6  ]+[FaturaI->Quantidade]
    Case Coluna=7 : [Qtd7  ]=[Qtd7  ]+[FaturaI->Quantidade]
    Case Coluna=8 : [Qtd8  ]=[Qtd8  ]+[FaturaI->Quantidade]
    Case Coluna=9 : [Qtd9  ]=[Qtd9  ]+[FaturaI->Quantidade]
    Case Coluna=10: [Qtd10 ]=[Qtd10 ]+[FaturaI->Quantidade]
    Case Coluna=11: [Qtd11 ]=[Qtd11 ]+[FaturaI->Quantidade]
    Case Coluna=12: [Qtd12 ]=[Qtd12 ]+[FaturaI->Quantidade]
  EndSwitch
  [Preco Total]=[Preco Unitario]*([Qtd1]+[Qtd2]+[Qtd3]+[Qtd4]+[Qtd5]+[Qtd6]+
                                  [Qtd7]+[Qtd8]+[Qtd9]+[Qtd10]+[Qtd11]+[Qtd12])
  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
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()
