;** Rm_0080A *******************************************************************
;*  Emissao do Ranking de Pedidos I                                            *
;*******************************************************************************
;Autor           : Vise Informatica (VISERP)
;Desenvolvido    : 02/10/2019 por Anivel Augusto Rodrigues
;Ultima Alteracao: 02/10/2019 por Anivel Augusto Rodrigues 
;Motivo Alteracao: Rotina impressÆo foi separada do RM_0080

;*******************************************************************************
;* Rotina p/ Cabecalho                                                         *
;*******************************************************************************
Proc RM_0080A_Cabecalho()

pag=pag+1

Print File ARQIMP CI_15CPP
Print File ARQIMP CI_8LPP
Print File ARQIMP "\n\n"

Print File ARQIMP "  "+Fill("-",193)+"\n"
Print File ARQIMP "  "+Completa(NOMEMPRESA,37," ")

Print File ARQIMP Format("W156,AR","Emissao.: "+AlinhM(LTrim(Format("D13",Today())),10)+"-"+AlinhM(LTrim(Time()),8)+"  Pag.: "+Format("W4,EIC",pag))+"\n"
Print File ARQIMP "  "+Fill("-",193)+"\n\n"

Print File ARQIMP Format("W193,AC","Ranking de Pedidos I de "+StrVal(VSDGDTINI)+" ate "+StrVal(VSDGDTFIM))+"\n\n"

For i From 1 To DynArraySize(Grade)
  Print File ARQIMP SubStr(Grade[i],2,Len(Grade[i]))
EndFor

Linha =DynArraySize(Grade)+11 

Print File ARQIMP "  ----- ---------------------------------------------- ------ ------ ------ ------ ------ ------ ------ ------"+
                  " ------ ------ ------ ------ ------- --------------  ----------"+"\n"
              
Print File ARQIMP "\027E"+"       Empresa: "+"\027F"+[Codigo da Empresa]+" "+[Nome Fantasia Empresa]+"\027F"+"\n"
Print File ARQIMP "\n"

EndProc   ; RM_0080A_Cabecalho
                    
;*******************************************************************************
;* Rotina para ImpressÆo Sintetica                                             *
;*******************************************************************************
Proc RM_0080A_Corpo()
private NR, CODEMP, Contador, NUM_LINHA_PAG

ArqImp=PrivDir()+"RM_0080.TXT"
If IsFile(ArqImp)
  Then Deleta_Arquivo(ARQIMP)
EndIf

Impressao_Orientacao="Paisagem"

Sort Tabela On "Quantidade" D
ClearAll

View Tabela

NUM_LINHA_PAG=60

CODEMP   = "*****"   
pag      = 0
Contador = 0

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

RM_0080A_Cabecalho()

NR=NRecords(Tabela)

SEQ     = 0
TOT_QTD = 0
TOT_PRT = 0
TOT_EMP = 0

QTD1  = 0
QTD2  = 0
QTD3  = 0
QTD4  = 0
QTD5  = 0
QTD6  = 0
QTD7  = 0
QTD8  = 0
QTD9  = 0
QTD10 = 0
QTD11 = 0
QTD12 = 0

Moveto Tabela
Scan 
  SEQ = SEQ + 1 
  Contador = Contador + 1
  ;*** Se futuramente liberar para mais de uma empresa ************************* 
  If CODEMP="*****"
     Then CODEMP=[Codigo da Empresa]
  EndIf   
  
  If (CODEMP<>[Codigo da Empresa] And CODEMP<>"*****") 
     Then Linha = Linha + 1
          RM_0080A_Totalizador("EMPRESA")

          TOT_QTD = 0      
          TOT_PRT = 0      
          TOT_EMP = 0
                           
          QTD1  = 0        
          QTD2  = 0        
          QTD3  = 0        
          QTD4  = 0        
          QTD5  = 0        
          QTD6  = 0        
          QTD7  = 0        
          QTD8  = 0        
          QTD9  = 0        
          QTD10 = 0        
          QTD11 = 0        
          QTD12 = 0
          
          Print File ARQIMP "\f"
          RM_0080A_Cabecalho()        
  EndIf
  
  Print File ARQIMP Spaces(2)
  Print File ARQIMP Format("W5,AR",StrVal(SEQ))+Spaces(1)
  Print File ARQIMP Format("W11",[Referencia])
  Print File ARQIMP Format("W31",[Descricao do Item])
  Print File ARQIMP Completa("("+Rtrim([Codigo da Grade])+")",4," ")+Spaces(1)
  Print File ARQIMP Format("W6",[Qtd1])+Spaces(1)
  Print File ARQIMP Format("W6",[Qtd2])+Spaces(1)
  Print File ARQIMP Format("W6",[Qtd3])+Spaces(1)
  Print File ARQIMP Format("W6",[Qtd4])+Spaces(1)
  Print File ARQIMP Format("W6",[Qtd5])+Spaces(1)
  Print File ARQIMP Format("W6",[Qtd6])+Spaces(1)
  Print File ARQIMP Format("W6",[Qtd7])+Spaces(1)
  Print File ARQIMP Format("W6",[Qtd8])+Spaces(1)
  Print File ARQIMP Format("W6",[Qtd9])+Spaces(1)
  Print File ARQIMP Format("W6",[Qtd10])+Spaces(1)
  Print File ARQIMP Format("W6",[Qtd11])+Spaces(1)
  Print File ARQIMP Format("W6",[Qtd12])+Spaces(1)
  Print File ARQIMP Format("W7",[Quantidade])+Spaces(1)
  Print File ARQIMP Format("W14.2,EIC",[Preco Total])+Spaces(2) 
  Print File ARQIMP Format("W9.2,EIC",([Quantidade]/[Total_Empresa])*100)+"%"+"\n"
      
  Linha = Linha + 1 
          
  TOT_QTD = TOT_QTD + [Quantidade]
  TOT_PRT = TOT_PRT + [Preco Total]
  TOT_EMP = TOT_EMP + (([Quantidade]*100)/[Total_Empresa])
  
  QTD1  = QTD1  + [Qtd1]
  QTD2  = QTD2  + [Qtd2]
  QTD3  = QTD3  + [Qtd3]
  QTD4  = QTD4  + [Qtd4]
  QTD5  = QTD5  + [Qtd5]
  QTD6  = QTD6  + [Qtd6]
  QTD7  = QTD7  + [Qtd7]
  QTD8  = QTD8  + [Qtd8]
  QTD9  = QTD9  + [Qtd9]
  QTD10 = QTD10 + [Qtd10]
  QTD11 = QTD11 + [Qtd11]
  QTD12 = QTD12 + [Qtd12]  

  If Linha >= NUM_LINHA_PAG     
     Then Print File ARQIMP "\f"
          RM_0080A_Cabecalho()
  EndIf    
EndScan
ClearAll

RM_0080A_Totalizador("EMPRESA")

EndProc   ;RM_0080A_Corpo() 
            
;*******************************************************************************
;* Rotina                                                                      *
;*******************************************************************************
Proc RM_0080A_Totalizador(PARAM)
private PARAM, QTD, AUX_QTD

Switch
  case PARAM="EMPRESA" :  
       QTD = Spaces(34)+"Total por Empresa... "
       For i From 1 To 12
         Switch
           Case i=1  : AUX_QTD=QTD1
           Case i=2  : AUX_QTD=QTD2
           Case i=3  : AUX_QTD=QTD3
           Case i=4  : AUX_QTD=QTD4
           Case i=5  : AUX_QTD=QTD5
           Case i=6  : AUX_QTD=QTD6
           Case i=7  : AUX_QTD=QTD7
           Case i=8  : AUX_QTD=QTD8
           Case i=9  : AUX_QTD=QTD9
           Case i=10 : AUX_QTD=QTD10
           Case i=11 : AUX_QTD=QTD11
           Case i=12 : AUX_QTD=QTD12  
         EndSwitch 
          
         If AUX_QTD <> "" And AUX_QTD <> BlankNum() And AUX_QTD<>0 
            Then QTD = QTD + Format("W6,AR",AUX_QTD)+Spaces(1)
            Else QTD = QTD + Spaces(6)+Spaces(1)
        EndIf
                               
       Endfor
       
       Print File ARQIMP "\n"
       Print File ARQIMP Spaces(55)+"------ ------ ------ ------ ------ ------ ------ ------"+
                                    " ------ ------ ------ ------ ------- --------------  ----------"+"\n"
       Print File ARQIMP QTD+Format("W7",TOT_QTD)+" "+Format("W14.2,EIC",TOT_PRT)+"  "+Format("W9",TOT_EMP)+"%"+"\n"                             
       
EndSwitch

EndProc ;RM_0080A_Totalizador()


;*******************************************************************************
;* ROTINA PRINCIPAL                                                            *
;*******************************************************************************
RM_0080A_Corpo()
If DISPOSITIVO_OPCAO="A"
  Then IMPRIREL(Tabela,"R") 
  Else ImpriArq(ARQIMP)
EndIf

ClearAll
Release Vars ARQIMP, pag, Linha,   
             QTD1,QTD2,QTD3,QTD4,QTD5,QTD6,QTD7,QTD8,QTD9,QTD10,QTD11,QTD12,
             TOT_QTD, TOT_PRT, TOT_EMP
             
Release Procs RM_0080A_Cabecalho, RM_0080A_Corpo, RM_0080A_Totalizador
