;** RR_0222 ********************************************************************
;** Imprime a Producao                                            ** Lupus **  *
;*******************************************************************************
;Autor           : Vise Informatica (VISERP)
;Desenvolvido  em: 03/03/2009 por Anivel Augusto Rodrigues
;Alterado em     : 05/09/2013 por Bruno Carvalho
;Motivo Alteracao: Ajustes p/ quebrar por grade
;Alterado em     : 12/09/2013 por Bruno Carvalho
;Motivo Alteracao: Ajustes p/ quebrar por grade               
;Alterado em     : 30/11/2015 por Bruno Carvalho
;Motivo Alteracao: Ajustado p/ atribuir valor p/ pre‡o total e unit rio caso a empresa seja diferente de #0222#

;*******************************************************************************
;* Checa linha na area de impressao dos itens                                  *
;*******************************************************************************
Proc Checa_Linha(nl,tp)
Private nl,tp

If (nlin+nl) >= PARAM_NLINHA
   Then Print File ARQIMP "\n\n\n"
        Print File ARQIMP Format("W131,AC","*** Continua proxima pagina ==> ")    

        Print File ARQIMP CI_10CPP
        Print File ARQIMP "\f"
       
        nlin=14+Cont_Obs
        Cabecalho_Tit()
        If tp="Item"
           Then Moveto PrivDir()+"RfaCrig"
        EndIf
   Else nlin=nlin+nl    
EndIf

EndProc ;Checa_Linha()

;*******************************************************************************
;* Verifica as Grades no Pedido                                                *
;*******************************************************************************
Proc Verifica_Grades()
Private wc,gok

DynArray Array_Grade[]
                             
Array array_tittm[12]
Moveto "\\Sistemas\\Estoque\\TpGrade"
Moveto [Codigo da Grade]
Locate Grad
If RetVal
   Then array_tittm[1] =[Titulo Grade1 ]
        array_tittm[2] =[Titulo Grade2 ]
        array_tittm[3] =[Titulo Grade3 ]
        array_tittm[4] =[Titulo Grade4 ]
        array_tittm[5] =[Titulo Grade5 ]
        array_tittm[6] =[Titulo Grade6 ]
        array_tittm[7] =[Titulo Grade7 ]
        array_tittm[8] =[Titulo Grade8 ]
        array_tittm[9] =[Titulo Grade9 ]
        array_tittm[10]=[Titulo Grade10]
        array_tittm[11]=[Titulo Grade11]
        array_tittm[12]=[Titulo Grade12]
   Else For i From 1 to 12
          array_tittm[i]=""
        EndFor
EndIf

If EXIBE_GRADE 
   Then Emite_Grade=Grad                                                        
   Else Emite_Grade=" "
EndIf

Grade=Emite_Grade+ESPACO+
      Format("W4,AR",array_tittm[01])+ESPACO+
      Format("W4,AR",array_tittm[02])+ESPACO+
      Format("W4,AR",array_tittm[03])+ESPACO+
      Format("W4,AR",array_tittm[04])+ESPACO+
      Format("W4,AR",array_tittm[05])+ESPACO+
      Format("W4,AR",array_tittm[06])+ESPACO+
      Format("W4,AR",array_tittm[07])+ESPACO+
      Format("W4,AR",array_tittm[08])+ESPACO+
      Format("W4,AR",array_tittm[09])+ESPACO+
      Format("W4,AR",array_tittm[10])+ESPACO+
      Format("W4,AR",array_tittm[11])+ESPACO+
      Format("W4,AR",array_tittm[12])+ESPACO

Release Vars wc,wi,gok,array_tittm
EndProc

;*******************************************************************************
;* Rotina para Impressao do Cabecalho titulo                                   *
;*******************************************************************************
Proc Cabecalho_Tit()

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

Moveto "RFACC"

Print File ARQIMP CI_6LPP+CI_15CPP
Print File ARQIMP "\n"
Print File ARQIMP Spaces(1)+"--------------------------------------------------------------------------------------------------------------------------------------" + "\n"
Print File ARQIMP Spaces(1)+Format("W42,AL",NOMEMPRESA)+Format("W62,AC","ROMANEIO DE ENTRADA DE PRODUCAO "+[Destino_Producao])+
                        "Impressao: "+Format("D13",Today())+"-"+Time()+"\n"
Print File ARQIMP Spaces(1)+"--------------------------------------------------------------------------------------------------------------------------------------" + "\n\n"

Print File ARQIMP Spaces(1)+"Executor: "
Print File ARQIMP Format("W05,AL",[Codigo])
Print File ARQIMP Format("W76,AL",[Razao Social])
Print File ARQIMP CI_INEG+CI_IEXP+"N§ Producao: "+Format("W06.3,AL",[Numero_Producao])+CI_FNEG+CI_FEXP+"\n"

If [RFACC->Destino_Producao]<>"INTERNO"
   Then Print File ARQIMP Spaces(16)+Format("W40,AL",Reg_Busca_Lookup["Endereco"])
        Print File ARQIMP Format("W25,AL",Reg_Busca_Lookup["CGC"])
        Print File ARQIMP Format("W25,AL",Reg_Busca_Lookup["Contato"])+"\n"

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

        Print File ARQIMP Spaces(16)+Format("W20,AL",Reg_Busca_Lookup["Cidade"])
        Print File ARQIMP Format("W20,AL",Reg_Busca_Lookup["UF"])+"\n\n"
   Else Print File ARQIMP "\n"
EndIf

Print File ARQIMP Spaces(1)+"Entrada: "
Print File ARQIMP Format("D13",[Data_Producao])+"   "
Print File ARQIMP "Programacao: "+[Numero_Programacao]+"   "
Print File ARQIMP "Tabela: "+[Codigo da Tabela]+"   "
Print File ARQIMP "Empresa: "+Format("W05,AL",[Codigo da Empresa])+Format("W20,AL",[Nome Fantasia Empresa])+"\n"

;---- Imprime Observacoes ------------------------------------------------------
For i From 1 To Cont_Obs
  Print File ARQIMP SubStr(Observacao[i],4,Len(Observacao[i]))+"\n" 
EndFor 

Print File ARQIMP "\n"
Cabecalho_Item()

EndProc ;Cabecalho_Tit()

;*******************************************************************************
;* Rotina para Impressao do Cabecalho do Item                                  *
;*******************************************************************************
Proc Cabecalho_Item()
Private wc,linha

Print File ARQIMP Spaces(1)+"--------------------------------------------------------------------------------------------------------------------------------------" +"\n"
Print File ARQIMP Spaces(1)+"Seq"+ESPACO+"Ref   "+ESPACO+"Descricao                   "+Grade+" Total"+ESPACO+"UN"+ESPACO+"    PrUnit"+ESPACO+"     PrTotal"+"\n"            
Print File ARQIMP Spaces(1)+"---"+ESPACO_S+"------"+ESPACO_S+"-----------------------------"+ESPACO_S+"----"+ESPACO_S+"----"+ESPACO_S+"----"+ESPACO_S+"----"+ESPACO_S
Print File ARQIMP "----"+ESPACO_S+"----"+ESPACO_S+"----"+ESPACO_S+"----"+ESPACO_S+"----"+ESPACO_S+"----"+ESPACO_S+"----"+ESPACO_S+"----"+ESPACO_S+"------"+ESPACO_S+"--"
Print File ARQIMP ESPACO_S+"----------"+ESPACO_S+"------------"+"\n"
    
Moveto PrivDir()+"Rfacrig"
                            
EndProc ;Cabecalho_Item()

;*******************************************************************************
;* Imprime Totalizador                                                         *
;*******************************************************************************
Proc Totalizador()

For wi From 1 to 12
  If array_tqtd[wi]=0 then array_tqtd[wi]=BlankNum() EndIf
  array_tqtd[13]=array_tqtd[13]+array_tqtd[wi]
EndFor                                                                                                                                                                                                                                       
                                                                                                                                                                                                                                                                          
Print File ARQIMP Spaces(1)+Barramento+"----"+ESPACO_S+"----"+ESPACO_S+"----"+ESPACO_S+"----"+ESPACO_S+"----"+ESPACO_S+"----"+ESPACO_S
Print File ARQIMP "----"+ESPACO_S+"----"+ESPACO_S+"----"+ESPACO_S+"----"+ESPACO_S+"----"+ESPACO_S+"----"+ESPACO_S+"------"+BarramentoF+"------------"+"\n"
                  
                               
Print File ARQIMP Spaces(1)+"TOTAIS => "+Spaces(30)+ESPACO
Print File ARQIMP Format("W04,EIC",array_tqtd[01])+ESPACO
Print File ARQIMP Format("W04,EIC",array_tqtd[02])+ESPACO
Print File ARQIMP Format("W04,EIC",array_tqtd[03])+ESPACO
Print File ARQIMP Format("W04,EIC",array_tqtd[04])+ESPACO
Print File ARQIMP Format("W04,EIC",array_tqtd[05])+ESPACO
Print File ARQIMP Format("W04,EIC",array_tqtd[06])+ESPACO
Print File ARQIMP Format("W04,EIC",array_tqtd[07])+ESPACO
Print File ARQIMP Format("W04,EIC",array_tqtd[08])+ESPACO
Print File ARQIMP Format("W04,EIC",array_tqtd[09])+ESPACO
Print File ARQIMP Format("W04,EIC",array_tqtd[10])+ESPACO
Print File ARQIMP Format("W04,EIC",array_tqtd[11])+ESPACO
Print File ARQIMP Format("W04,EIC",array_tqtd[12])+ESPACO
Print File ARQIMP Format("W06,EIC",array_tqtd[13])+ESPACO+Spaces(13)+ESPACO
Print File ARQIMP Format("W12.2,EIC",TOT)+"\n"
Print File ARQIMP Spaces(1)+"--------------------------------------------------------------------------------------------------------------------------------------" +"\n"
Print File ARQIMP "\n\n"

TOT = 0

NLIN = NLIN + 3

EndProc ;Totalizador()

;*******************************************************************************
;* Rotina para Impressao do Relatorio                                          *
;*******************************************************************************
Proc IMPPROD()

Array array_tqtd[14] 

For wi From 1 to 14
  array_tqtd[wi]=0
EndFor
           
SEQ  = 0
NPAG = 0
TOT  = 0
TOTQ = 0
TOTP = 0           

MUDOU_GRADE = False

Moveto PrivDir()+"RFACrig"
Moveto [Numero_Producao]
Locate [RFACC->Numero_Producao]
Grad=[Codigo da Grade]
Verifica_Grades()
Cabecalho_Tit()
Moveto PrivDir()+"RFACrig"
While [Numero_Producao]=[RFACC->Numero_Producao]
   If Grad<>[Codigo da Grade]
      Then MUDOU_GRADE = True
           Checa_Linha(3,"Item")
           Totalizador()
           SEQ  = 0
           Grad=[Codigo da Grade]
           Verifica_Grades()
           Cabecalho_Item()
           
           For wi From 1 to 14
             array_tqtd[wi]=0
           EndFor   
   EndIf                                                  
   
   If SYS_CODIGO_CLIENTE="0222"
      Then vrunit=BlankNum()
           vrtot =BlankNum() 
      Else vrunit=[Preco Unitario]
           vrtot =[Preco Total]
   EndIf
   
   Checa_Linha(1,"Item")
   Moveto PrivDir()+"RFACrig"
   
   SEQ  = SEQ + 1  
   Print File ARQIMP Spaces(1)+Format("W03,AR",StrVal(SEQ))+ESPACO
   Print File ARQIMP Format("W06,AL",[Referencia])+ESPACO
   Print File ARQIMP Format("W29,AL",[Descricao do Item])+ESPACO
   Print File ARQIMP Format("W04,EIC",[Qtd1])+ESPACO
   Print File ARQIMP Format("W04,EIC",[Qtd2])+ESPACO
   Print File ARQIMP Format("W04,EIC",[Qtd3])+ESPACO
   Print File ARQIMP Format("W04,EIC",[Qtd4])+ESPACO
   Print File ARQIMP Format("W04,EIC",[Qtd5])+ESPACO
   Print File ARQIMP Format("W04,EIC",[Qtd6])+ESPACO
   Print File ARQIMP Format("W04,EIC",[Qtd7])+ESPACO
   Print File ARQIMP Format("W04,EIC",[Qtd8])+ESPACO
   Print File ARQIMP Format("W04,EIC",[Qtd9])+ESPACO
   Print File ARQIMP Format("W04,EIC",[Qtd10])+ESPACO
   Print File ARQIMP Format("W04,EIC",[Qtd11])+ESPACO
   Print File ARQIMP Format("W04,EIC",[Qtd12])+ESPACO
   Print File ARQIMP Format("W06,EIC",[Qtd1]+[Qtd2 ]+[Qtd3 ]+[Qtd4 ]+
                                      [Qtd5]+[Qtd6 ]+[Qtd7 ]+[Qtd8 ]+
                                      [Qtd9]+[Qtd10]+[Qtd11]+[Qtd12])+ESPACO
   Print File ARQIMP Format("W02,AL",[Unidade Interna])+ESPACO
   Print File ARQIMP Format("W10.2,EIC",vrunit)+ESPACO
   Print File ARQIMP Format("W12.2,EIC",vrtot)+"\n"

   TOT=TOT+vrtot

   TOTQ = TOTQ + ([Qtd1]+[Qtd2 ]+[Qtd3 ]+[Qtd4 ]+
                  [Qtd5]+[Qtd6 ]+[Qtd7 ]+[Qtd8 ]+
                  [Qtd9]+[Qtd10]+[Qtd11]+[Qtd12])
   TOTP = TOTP + [Preco Total] 

   array_tqtd[01]=array_tqtd[01]+[Qtd1 ]
   array_tqtd[02]=array_tqtd[02]+[Qtd2 ]
   array_tqtd[03]=array_tqtd[03]+[Qtd3 ]
   array_tqtd[04]=array_tqtd[04]+[Qtd4 ]
   array_tqtd[05]=array_tqtd[05]+[Qtd5 ]
   array_tqtd[06]=array_tqtd[06]+[Qtd6 ]
   array_tqtd[07]=array_tqtd[07]+[Qtd7 ]
   array_tqtd[08]=array_tqtd[08]+[Qtd8 ]
   array_tqtd[09]=array_tqtd[09]+[Qtd9 ]
   array_tqtd[10]=array_tqtd[10]+[Qtd10]
   array_tqtd[11]=array_tqtd[11]+[Qtd11]
   array_tqtd[12]=array_tqtd[12]+[Qtd12]

   array_tqtd[14]=array_tqtd[14]+[Preco Total]

   Skip
   If EOT() Then QuitLoop EndIf
EndWhile

Checa_Linha(4,"Item")
Totalizador()  

EndProc

;*******************************************************************************
;* Observa‡Æo                                                                  *
;*******************************************************************************
Proc Obs()     
Private OBS,QTD,LABEL_OBS,LINHA,Pos_Inicio,OBS_AUX,QTD_AUX,QTD_LINHA,wia

OBS        = [RFACC->Observacoes]
LABEL_OBS  = "  Observacao : "          
LINHA      = 0               
     
Pos_Inicio=SearchFrom("\n",OBS,1) 

While True
 If Pos_Inicio > 0
    Then OBS_AUX   = SubStr(OBS,1,Pos_Inicio-1)
    Else OBS_AUX   = OBS
 EndIf   
 OBS       = SubStr(OBS,Pos_Inicio+1,Len(OBS)) 
 QTD_AUX   = Len(OBS_AUX)
 QTD_LINHA = Round(QTD_AUX/NUM_CARACTERES_OBS,0)                     
 If ((QTD_AUX/NUM_CARACTERES_OBS) - QTD_LINHA) > 0
    Then QTD_LINHA = QTD_LINHA + 1
 EndIf   
 For wia From 1 To QTD_LINHA 
   Cont_Obs = Cont_Obs + 1
   
   If LINHA = 0 
      Then LINHA = LABEL_OBS
      Else LINHA = Spaces(Len(LABEL_OBS))
   EndIf
   Observacao[Cont_Obs] = AlinhM(StrVal(Cont_Obs),3)+LINHA
   
   LINHA = Substr(OBS_AUX,((wia-1)*NUM_CARACTERES_OBS)+1,NUM_CARACTERES_OBS)
   Observacao[Cont_Obs] = Observacao[Cont_Obs]+"\27E"+LINHA+"\27F"   
 EndFor
 If Pos_Inicio = 0 Then Quitloop EndIf
 Pos_Inicio=SearchFrom("\n",OBS,1)                
EndWhile  
LINHA = ""  
        
EndProc ;Obs()

;*******************************************************************************
;* Rotina Principal                                                            *
;*******************************************************************************
ArqImp=PrivDir()+"RR_0222.Txt"
                                                        
PARAM_NLINHA       = 63
EXIBE_GRADE        = False

ESPACO             = "Ý"
ESPACO_S           = "+"
Barramento="----------------------------------------"+ESPACO_S  
BarramentoF = ESPACO+Spaces(13)+ESPACO

;ESPACO             = " "
;ESPACO_S           = " "
;Barramento=Spaces(41)
;BarramentoF = Spaces(15)

NLIN               = 16
NUM_CARACTERES_OBS = 117
LINHAS_OBS         = False
Cont_Obs           = 0

Moveto "RFACC"
FormKey
CoEditKey
Moveto [Codigo]
If Not Busca_Lookup("Codigo",[Codigo],"SM")
   Then Reg_Busca_Lookup=""
EndIf
UnLockRecord
Do_It!

View NoWindow "Rfacrig"

{Ask} {Rfacrig} Check
Moveto [Numero_Producao] TypeIn "~NPROD.."
Do_It!
ClearAll

{Modify} {Sort} {Answer} 
If MenuChoice()="Same"
   Then {Same}
EndIf   
Down Down Down Down "2"                                  ;Referencia
Down Down Down Down Down Down Down Down Down Right "1"   ;Codigo da Tabela
Do_It! 
ClearAll
Copy "Answer" PrivDir()+"Rfacrig"

View "Rfacc"
Moveto [Numero_Producao]
Locate NPROD

DynArray Observacao[]
Obs()

NLIN = NLIN + CONT_OBS

View "\\Sistemas\\Estoque\\TpGrade"
Coedit PrivDir()+"Rfacrig"  

IMPPROD()
ImpriArq(ARQIMP)

Do_It!
ClearAll

;*******************************************************************************
LimpAguarde()

Release Vars QUANT, QUANT1, QUANT2, QUANT3, QUANT4, QUANT5, QUANT6,
             QUANT7, QUANT8, QUANT9, QUANT10, QUANT11, QUANT12,
             TOT, SeqItPed, Vrunit, vrtot

Release Procs IMPPROG,
              Seta_FilePrint, Imprime_FilePrint