;** RC_0425 ********************************************************************
;** Imprime a Programacao Corte                           ** Teodoro **        *
;*******************************************************************************
;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     : 25/01/2019 por Anivel Augusto Rodrigues
;Motivo Alteracao: Ajustes colunas com 1.000 unidades

;*******************************************************************************
;* 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=10+Cont_Obs
        Cabecalho_Tit()
        If tp="Item"
           Then Moveto PrivDir()+"CfaCrig"
        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("W5,AR",array_tittm[01])+ESPACO+
      Format("W5,AR",array_tittm[02])+ESPACO+
      Format("W5,AR",array_tittm[03])+ESPACO+
      Format("W5,AR",array_tittm[04])+ESPACO+
      Format("W5,AR",array_tittm[05])+ESPACO+
      Format("W5,AR",array_tittm[06])+ESPACO+
      Format("W5,AR",array_tittm[07])+ESPACO+
      Format("W5,AR",array_tittm[08])+ESPACO+
      Format("W5,AR",array_tittm[09])+ESPACO+
      Format("W5,AR",array_tittm[10])+ESPACO+
      Format("W5,AR",array_tittm[11])+ESPACO+
      Format("W5,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

Print File ARQIMP CI_6LPP+CI_15CPP
Print File ARQIMP "\n"
Print File ARQIMP "------------------------------------------------------------------------------------------------------------------------------------" + "\n"
Print File ARQIMP Format("W40,AL",NOMEMPRESA)+Format("W62,AC","ORDEM DE CORTE")+"Impressao: "+Format("D13",Today())+"-"+Time()+ "\n"
Print File ARQIMP "------------------------------------------------------------------------------------------------------------------------------------" + "\n\n"

Print File ARQIMP Spaces(02)+"Responsavel: ______________________________"+Spaces(50)
Print File ARQIMP CI_INEG+CI_IEXP+"N§ Corte: "+Format("W06.3,AL",[Cfacc->Numero_Corte])+CI_FNEG+CI_FEXP+"\n"

Print File ARQIMP Spaces(02)+"Programado : "
Print File ARQIMP Format("D13",[Cfacc->Data_Programacao])+"   "
Print File ARQIMP "Empresa: "+Format("W05,AL",[Cfacc->Codigo da Empresa])+Format("W20,AL",[Cfacc->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)+"Seq"+ESPACO+"Ref   "+ESPACO+"Descricao                  "+ESPACO+""+Grade+" Total"+ESPACO+"UN"+"\n"            
Print File ARQIMP Spaces(1)+"--- ------ ----------------------------- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ------- --"+"\n"

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)+"                                         ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- -------"+"\n"

Print File ARQIMP Spaces(41)+ESPACO
Print File ARQIMP Format("W05",array_tqtd[01])+ESPACO
Print File ARQIMP Format("W05",array_tqtd[02])+ESPACO
Print File ARQIMP Format("W05",array_tqtd[03])+ESPACO
Print File ARQIMP Format("W05",array_tqtd[04])+ESPACO
Print File ARQIMP Format("W05",array_tqtd[05])+ESPACO
Print File ARQIMP Format("W05",array_tqtd[06])+ESPACO
Print File ARQIMP Format("W05",array_tqtd[07])+ESPACO
Print File ARQIMP Format("W05",array_tqtd[08])+ESPACO
Print File ARQIMP Format("W05",array_tqtd[09])+ESPACO
Print File ARQIMP Format("W05",array_tqtd[10])+ESPACO
Print File ARQIMP Format("W05",array_tqtd[11])+ESPACO
Print File ARQIMP Format("W05",array_tqtd[12])+ESPACO
Print File ARQIMP Format("W07,EIC",array_tqtd[13])+ESPACO
Print File ARQIMP "\n\n"

NLIN = NLIN + 3

EndProc ;Totalizador()

;*******************************************************************************
;* Rotina para Impressao do Relatorio                                          *
;*******************************************************************************
Proc IMPPROG()
Private SEQ

Array array_tqtd[14] 
For wi From 1 to 14
  array_tqtd[wi]=0
EndFor

SEQ  = 0
NPAG = 0
TOTQ = 0

MUDOU_GRADE = False

Moveto PrivDir()+"CfaCrig"
Moveto [Numero_Corte]
Locate [CFACC->Numero_Corte]
Grad=[Codigo da Grade]
Verifica_Grades()
Cabecalho_Tit()
Moveto PrivDir()+"CfaCrig"
While [Numero_Corte]=[CFACC->Numero_Corte]

   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
   
   Checa_Linha(1,"Item")
   Moveto PrivDir()+"CfaCrig"
   
   SEQ  = SEQ + 1   

   Print File ARQIMP Format("W04,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("W05",[Qtd1])+ESPACO
   Print File ARQIMP Format("W05",[Qtd2])+ESPACO
   Print File ARQIMP Format("W05",[Qtd3])+ESPACO
   Print File ARQIMP Format("W05",[Qtd4])+ESPACO
   Print File ARQIMP Format("W05",[Qtd5])+ESPACO
   Print File ARQIMP Format("W05",[Qtd6])+ESPACO
   Print File ARQIMP Format("W05",[Qtd7])+ESPACO
   Print File ARQIMP Format("W05",[Qtd8])+ESPACO
   Print File ARQIMP Format("W05",[Qtd9])+ESPACO
   Print File ARQIMP Format("W05",[Qtd10])+ESPACO
   Print File ARQIMP Format("W05",[Qtd11])+ESPACO
   Print File ARQIMP Format("W05",[Qtd12])+ESPACO
   Print File ARQIMP Format("W07,EIC",[Qtd1]+[Qtd2 ]+[Qtd3 ]+[Qtd4 ]+
                                      [Qtd5]+[Qtd6 ]+[Qtd7 ]+[Qtd8 ]+
                                      [Qtd9]+[Qtd10]+[Qtd11]+[Qtd12])+ESPACO
   Print File ARQIMP Format("W02,AL",[Unidade Interna])+ESPACO+"\n"

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

   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]

   Skip
   If EOT() Then QuitLoop EndIf
EndWhile

Checa_Linha(4,"Item")
Totalizador()  
Print File ARQIMP Spaces(102)+"Total Geral "+Format("W7,EIC",TOTQ)+"\n"

;************************* IMPRESSAO DO RODAPE ********************************
Moveto "CFACC"

OBS="Pedidos a atender: "
obs_inicio=True
Moveto "CFACP"
Moveto [Numero_Corte]
Locate [CFACC->Numero_Corte]
While [Numero_Corte]=[CFACC->Numero_Corte]

   If (Len(OBS)+7)>132
      Then Print File ARQIMP OBS+"\n"
           OBS=""
   EndIf
   If obs_Inicio
      Then OBS=OBS+[Numero do Pedido]
           obs_inicio=false
      Else OBS=OBS+", "+[Numero do Pedido]
   EndIf

   Skip
   If EOT() Then QuitLoop EndIf
EndWhile

If Len(OBS)>0
   Then Print File ARQIMP OBS+"\n"
EndIf
Print File ARQIMP CI_10CPP+"\f"+"\n"

Moveto "CFACC"
EndProc

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

OBS        = [CFACC->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                                                            *
;*******************************************************************************
Moveto "CFACC"

ArqImp=PrivDir()+"RC_0425.Txt"

PARAM_NLINHA       = 63
EXIBE_GRADE        = False
ESPACO             = " "
NLIN               = 10
NUM_CARACTERES_OBS = 117
LINHAS_OBS         = False
Cont_Obs           = 0

FormKey 
Seta_FilePrint("RC_0204")

View NoWindow "Cfacrig"
CoEditKey
Do_It!

{Ask} {Cfacrig} Check
Moveto [Numero_Corte] TypeIn "~NCORT.."
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()+"Cfacrig"

View "Cfacc"
Moveto [Numero_Corte]
Locate NCORT

View "Cfacp"
Moveto [Numero_Corte]
Locate NCORT

DynArray Observacao[]
Obs()

NLIN = NLIN + CONT_OBS

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

IMPPROG()
ImpriArq(ARQIMP)

Do_It!
ClearAll
;*******************************************************************************
LimpAguarde()

Release Vars QUANT, QUANT1, QUANT2, QUANT3, QUANT4, QUANT5, QUANT6,
             QUANT7, QUANT8, QUANT9, QUANT10, QUANT11, QUANT12,
             SeqItPed,OBS,obs_inicio

Release Procs IMPPROG,
              Seta_FilePrint, Imprime_FilePrint
