;** RP_RFACR *******************************************************************
;** Imprime a Producao                                            ** PadrÆo ** *
;*******************************************************************************
;Autor           : Vise Informatica (VISERP)
;Desenvolvido  em: 30/11/2015 por Bruno Carvalho
;Alterado em     :   /  /     por 
;Motivo Alteracao:              
                              
;*******************************************************************************
;* Rotina para Impressao do Cabecalho titulo                                   *
;*******************************************************************************
Proc Cabecalho_Tit()

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

Npag=Npag+1

Print File ARQIMP CI_8LPP+CI_15CPP
                                                                          
Print File ARQIMP CI_6LPP+CI_15CPP
Print File ARQIMP "\n"
Print File ARQIMP Spaces(2)+"--------------------------------------------------------------------------------------------------------------------------------------" +"\n"
Print File ARQIMP Spaces(2)+Format("W41,AL",NOMEMPRESA)+Format("W52,AC","ROMANEIO DE ENTRADA DE PRODUCAO "+REG_RFACC["Destino_Producao"])+
                        "Impressao: "+Format("D13",Today())+"-"+Time()+ "  Pag: "+Strval(NPAG)+"\n"
Print File ARQIMP Spaces(2)+"--------------------------------------------------------------------------------------------------------------------------------------" +"\n\n"

Print File ARQIMP Spaces(02)+"Executor: "+Format("W05,AL",REG_RFACC["Codigo"])+Format("W75,AL",REG_RFACC["Razao Social"])
Print File ARQIMP CI_INEG+CI_IEXP+"N§ Producao: "+Format("W6,AL",REG_RFACC["Numero_Producao"])+CI_FNEG+CI_FEXP+"\n"

Nlin = Nlin + 6
If REG_RFACC["Destino_Producao"]<>"INTERNO"
   Then Print File ARQIMP Spaces(17)+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(17)+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(17)+Format("W20,AL",Reg_Busca_Lookup["Cidade"])
        Print File ARQIMP Format("W20,AL",Reg_Busca_Lookup["UF"])+"\n\n"
        
        Nlin = Nlin + 4
   Else Print File ARQIMP "\n"
        Nlin = Nlin + 1
EndIf

DATA_PROG = BlankDate()
If PROGRAMACAO
   Then DATA_PROG = REG_PFACC["Data_Programacao"]
EndIf

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

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

Nlin = Nlin +1
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(2)+"                                                 ____ Quantidade _____  _________ Preco _________"+"\n"
Print File ARQIMP Spaces(2)+"Seq Item                                      UN Programada  Produzida      Unitario        Total"+"\n"
Print File ARQIMP Spaces(2)+"--- ----------------------------------------- -- ---------- ----------  ------------ ------------"+"\n"

Nlin = Nlin + 2
                           
EndProc ;Cabecalho_Item()   
                                                                                                                          
;*******************************************************************************
;* Rotina para Impressao do Relatorio                                          *
;*******************************************************************************
Proc IMP()                                                 
  
TOTL = 0  
SEQ  = 0
NPAG = 0
TOTP = 0
NLIN = 0

Cabecalho_Tit()
Moveto PrivDir()+"Rfacrir"
Scan
  SEQ  = SEQ + 1    
   
  Print File ARQIMP Spaces(2)+Format("W03,AR",StrVal(SEQ))+Spaces(1)
  Print File ARQIMP Format("W10,AL",[Referencia])+Spaces(1)
  Print File ARQIMP Format("W30,AL",[Descricao do Item])+Spaces(1)
  Print File ARQIMP Format("W02,AL",[Unidade Fiscal])+Spaces(1)
  Print File ARQIMP Format("W10,AR",[Quantidade Programada])+Spaces(1)
  Print File ARQIMP Format("W10,AR",[Quantidade Produzida ])+Spaces(2)
  Print File ARQIMP Format("W12.2,EIC",[Preco Unitario])+Spaces(1)
  Print File ARQIMP Format("W12.2,EIC",[Preco Unitario]*[Quantidade Produzida])+"\n"

  TOTL = TOTL + 1
  TOTP = TOTP + ([Preco Unitario]*[Quantidade Produzida])
  
  If TOTL>=PARAM_NLINHA
     Then TOTL = 0 
          NLIN = 0
          Print File ARQIMP "\f"
          Cabecalho_Tit()
          Moveto PrivDir()+"Rfacrir"
  EndIf
EndScan
Print File ARQIMP "\n"
Print File ARQIMP Spaces(80) +"TOTAL: "+ Format("W12.2,EIC",TOTP)+"\n"

EndProc

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

OBS        = REG_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()+"RP_RFACR.Txt"
Deleta_Arquivo(ARQIMP)         
                                                        
PARAM_NLINHA       = 63
NUM_CARACTERES_OBS = 117
LINHAS_OBS         = False
Cont_Obs           = 0
NLIN               = 0

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

PROGRAMACAO = False
If NPROG<>""
   Then GetRecord "\\Sistemas\\Faccao\\Pfacc" NPROG TO REG_PFACC
        If RetVal
           Then PROGRAMACAO = True
        EndIf   
EndIf

GetRecord "\\Sistemas\\Faccao\\Rfacc" NPROD TO REG_RFACC
If Not RetVal
   Then Mensagem("Produ‡„o n„o localizada!")
        Do_It!
        ClearAll 
        Release Vars QUANT, QUANT1, QUANT2, QUANT3, QUANT4, QUANT5, QUANT6,
                     QUANT7, QUANT8, QUANT9, QUANT10, QUANT11, QUANT12,
                     TOT, SeqItPed, Vrunit, vrtot,
                     npag, nlin
        Return 
EndIf   

{Ask} {Rfacc}
Moveto [Numero_Producao] TypeIn "_Np"
If NPROG<>""
   Then Moveto [Numero_Programacao] TypeIn "~NPROG.."
   Else Moveto [Numero_Corte      ] TypeIn "~NCORT.."
EndIf
{Ask} {Rfacrir} Check
Moveto [Numero_Producao] TypeIn "_Np, ~NPROD.."
Do_It!
ClearAll

Sort "Answer" On "Numero_Producao","Sequencia do Lancamento","Referencia","Codigo do Item"
Copy "Answer" PrivDir()+"Rfacrir"
ClearAll

View PrivDir()+"Rfacrir" 
View "RFACC"
Moveto [Numero_Programacao]
Locate NPROG

DynArray Observacao[]
Obs()

NLIN = NLIN + CONT_OBS

IMP()
ImpriArq(ARQIMP)

Do_It!
ClearAll

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

Release Procs IMPPROG, Cabecalho, Cabecalho_Itens, Rodape,
              Seta_FilePrint, Imprime_FilePrint
