;** Imp_Inv2 *******************************************************************
;*  Emiss„o do Inventario para AF com NBM                                      *
;*******************************************************************************
;Autor           : Vise Informatica (VISERP)
;Desenvolvido    : 15/05/2012 por Bruno Carvalho
;Ultima Alteracao:   /  /     por 
;Motivo Alteracao: 

;*******************************************************************************
;* Rotina para gerar subtotal                                                  *
;*******************************************************************************
Proc SubTotal(QUANT,VALOR)

LINHA=LINHA+2
Print File ARQIMP ASP1+"| "+Spaces(12)+" |"+Spaces(46)+"|"+Spaces(7)+"SubTotal... "
Switch
  Case PNC=0 : Print File ARQIMP Format("W16,EIC",QUANT)
  Case PNC=1 : Print File ARQIMP Format("W16.1,EIC",QUANT)
  Case PNC=2 : Print File ARQIMP Format("W16.2,EIC",QUANT)
  Case PNC=3 : Print File ARQIMP Format("W16.3,EIC",QUANT)    
EndSwitch
Print File ARQIMP "|"+Spaces(16)+"|"+Format("W15.2,EIC",VALOR)+"|"+ASP2+"\n" 
Print File ARQIMP ASP1+"| "+Spaces(13)+"|"+Spaces(46)+"|"+Spaces(8)+"|"+Spaces(9)+"|"+Spaces(16)+"|"+Spaces(16)+"|"+Spaces(15)+"|"+ASP2+"\n"

QTD_TRIB =0
VRT_TRIB =0
VR_TRIB  =0

QTD_ALIQ =0
VRT_ALIQ =0
VR_ALIQ  =0
  
QTD_TIPO =0
VRT_TIPO =0
VR_TIPO  =0

EndProc ;SubTotal()

;*******************************************************************************
;* Rotina para gerar o titulo                                                  *
;*******************************************************************************
Proc Titulo(Indice)
Private Titulo, Indice

Switch
  Case Indice=1 : TITULO="Tipo de Tributa‡„o: "+[Tributacao de ICMS]+Spaces(39)
                  TRIB_ICMS = [Tributacao de ICMS]
                  ALIQ_ICMS = "****"
                  TIPO_ITEM = "****"
  Case Indice=2 : TITULO="  Al¡quota de ICMS: "+Format("W3,AR",StrVal([Aliquota de ICMS]))+" %"+Spaces(35)
                  ALIQ_ICMS = [Aliquota de ICMS]
  Case Indice=3 : TITULO="    Tipo: "+Format("W30,AL",[Descricao Tipo do Item])+Spaces(20)
                  TIPO_ITEM = [Tipo do Item]
Endswitch
Print File ARQIMP ASP1+"| "+TITULO+"|"+Spaces(8)+"|"+Spaces(9)+"|"+Spaces(16)+"|"+Spaces(16)+"|"+Spaces(15)+"|"+ASP2+"\n"

LINHA=LINHA+1
EndProc ;Titulo()

;*******************************************************************************
;* Rotina de Impressao                                                         *
;*******************************************************************************
Proc IMPRIME()

Print File ARQIMP ASP1+CI_15CPP+ASP2+"\n"
LINHA=0
SOMAP=0
SOMAT=0
SOMAF=0
FOLHA=2
QUBRA_FOLHA=FOLHA

{Ask} {Itens} 
Moveto [Codigo do Item        ] Example "CodI"
Moveto [Descricao Tipo do Item] Check
Moveto [Tributacao de ICMS    ] Check
Moveto [Aliquota de ICMS      ] Check 
{Ask} TypeIn TabelaI Enter Check Enter
Moveto [Codigo do Item        ]  Example "CodI" TypeIn "!" 
Moveto [Aliquota de ICMS      ] Check 
Do_It!
ClearAll

Rename "Answer" TabelaI

If VSDGOPRELCODALF="2"
   Then Sort TabelaI On "Codigo da Empresa","Tributacao de ICMS","Aliquota de ICMS","Tipo do Item","Descricao do Item"
   Else Sort TabelaI On "Codigo da Empresa","Tributacao de ICMS","Aliquota de ICMS","Tipo do Item","Codigo do Item"
EndIf   

View TabelaI
View "\\Sistemas\\Cadastro\\Empresas"
Moveto TabelaI
Moveto [Codigo da Empresa]
CODEMP=[]
Moveto "\\Sistemas\\Cadastro\\Empresas"
Moveto [Codigo da Empresa]
Locate CODEMP
If RetVal
   Then IE =[\Sistemas\Cadastro\Empresas->IE Empresa ]
        CGC=[\Sistemas\Cadastro\Empresas->CGC Empresa]
        Razao=[\Sistemas\Cadastro\Empresas->Razao Social Empresa]
EndIf
TOPOREL()
               
TRIB_ICMS="****"
ALIQ_ICMS="****"
TIPO_ITEM="****"

QTD_TRIB =0
VRT_TRIB =0
VR_TRIB  =0

QTD_ALIQ =0
VRT_ALIQ =0
VR_ALIQ  =0
  
QTD_TIPO =0
VRT_TIPO =0
VR_TIPO  =0

Moveto TabelaI
While True
  If CODEMP<>[Codigo da Empresa]
     Then If Not SOMA_PARCIAL
             Then SubTotal(QTD_TRIB,VRT_TRIB)
                  IMPRIME_TRANSF=False
                  SOMAPAR()
          EndIf 
          SOMATOT()
          Rodape()
          Print File ARQIMP ASP1+"\f"+ASP2+"\n"         
          TOPOREL()

          Moveto [Codigo da Empresa]
          CODEMP=[]
          Moveto "\\Sistemas\\Cadastro\\Empresas"
          Moveto [Codigo da Empresa]
          Locate CODEMP
          If RetVal                                        
             Then IE =[\Sistemas\Cadastro\Empresas->IE Empresa ]
                  CGC=[\Sistemas\Cadastro\Empresas->CGC Empresa]
          EndIf
          Moveto TabelaI
  EndIf

  If QUBRA_FOLHA<>FOLHA
     Then TRIB_ICMS = "****"
          ALIQ_ICMS = "****"
          TIPO_ITEM = "****"
          QUBRA_FOLHA=FOLHA
  EndIf

  Switch
    Case TRIB_ICMS<>[Tributacao de ICMS] And TRIB_ICMS<>"****"  : SubTotal(QTD_TRIB,VRT_TRIB)
    Case ALIQ_ICMS<>[Aliquota de ICMS  ] And ALIQ_ICMS<>"****"  : SubTotal(QTD_ALIQ,VRT_ALIQ)
    Case TIPO_ITEM<>[Tipo do Item      ] And TIPO_ITEM<>"****"  : SubTotal(QTD_TIPO,VRT_TIPO)
  EndSwitch
 
  If TRIB_ICMS<>[Tributacao de ICMS] 
     Then Titulo(1)
  EndIf
     
  If ALIQ_ICMS<>[Aliquota de ICMS] 
     Then Titulo(2)
  EndIf
     
  If TIPO_ITEM<>[Tipo do Item] 
     Then Titulo(3)
  EndIf     

  ;ImpressÆo dos itens
  Print File ARQIMP ASP1+"| "+Format("W12,AL",[Codigo do Item])+" |"+Format("W46,AL",[Descricao do Item])
  Print File ARQIMP "|"+Format("W08,AL",[NCM])+"|  "+Format("W07,AL",[Unidade Interna])+"|"
  Switch
    Case PNC=0 : Print File ARQIMP Format("W16,EIC",[Saldo Atual])
    Case PNC=1 : Print File ARQIMP Format("W16.1,EIC",[Saldo Atual])
    Case PNC=2 : Print File ARQIMP Format("W16.2,EIC",[Saldo Atual])
    Case PNC=3 : Print File ARQIMP Format("W16.3,EIC",[Saldo Atual])
  EndSwitch
  Print File ARQIMP "|"+Format("W16.3,EIC",([Custo Total Atual]/[Saldo Atual]))+"|"+Format("W15.2,EIC",[Custo Total Atual])+"|"+ASP2+"\n"
  LINHA=LINHA+1

  QTD_TRIB =QTD_TRIB+[Saldo Atual]
  VRT_TRIB =VRT_TRIB+[Custo Total Atual]
  VR_TRIB  =VR_TRIB +(([Custo Total Atual]*[Aliquota de ICMS])/100) 

  QTD_ALIQ =QTD_ALIQ+[Saldo Atual]
  VRT_ALIQ =VRT_ALIQ+[Custo Total Atual]
  VR_ALIQ  =VR_ALIQ +(([Custo Total Atual]*[Aliquota de ICMS])/100) 
  
  QTD_TIPO =QTD_TIPO+[Saldo Atual]
  VRT_TIPO =VRT_TIPO+[Custo Total Atual]
  VR_TIPO  =VR_TIPO +(([Custo Total Atual]*[Aliquota de ICMS])/100) 
  
  SOMAF=SOMAF+[Custo Total Atual]
  SOMAP=SOMAP+[Custo Total Atual]
  SOMAT=SOMAT+[Custo Total Atual]
  
  Skip
  If EOT()
    Then SubTotal(QTD_TRIB,VRT_TRIB)
         IMPRIME_TRANSF=False
         SOMAPAR()
         SOMATOT()
         RODAPE()
         QuitLoop
  EndIf

  If FOLHA=3
     Then IMPRIME_TRANSF=False
     Else IMPRIME_TRANSF=True
  EndIf   

  SOMA_PARCIAL = False
  Switch
    Case (LINHA>42) And (CI_15CPP="\027x0\015")  :               ;Matricial
         If CODEMP<>[Codigo da Empresa] Then SubTotal(QTD_TRIB,VRT_TRIB) EndIf
         SOMAPAR()
         RODAPE()
         TOPOREL()
         SOMA_PARCIAL = True
    Case (LINHA>40) And (CI_15CPP<>"\027x0\015") :               ;Jato Tinta ou Laser
         If CODEMP<>[Codigo da Empresa] Then SubTotal(QTD_TRIB,VRT_TRIB) EndIf
         SOMAPAR()
         RODAPE()
         TOPOREL()
         SOMA_PARCIAL = True
  EndSwitch

EndWhile
Print File ARQIMP ASP1+CI_10CPP+ASP2+"\n"
Print File ARQIMP ASP1+CI_6LPP+ASP2+"\n"

EndProc
;*******************************************************************************
;* Rotina de Soma Parcial                                                      *
;*******************************************************************************
Proc SomaPar()

Print File ARQIMP ASP1+"|"+Fill("=",130)+"|"+ASP2+"\n"     
Print File ARQIMP ASP1+"|"+Spaces(101)+"TOTAL PAGINA:"+Format("W16.2,EIC",SOMAP)+"|"+ASP2+"\n"
If IMPRIME_TRANSF
   Then Print File ARQIMP ASP1+"|"+Spaces(101)+" TRANSFERIDO:"+Format("W16.2,EIC",SOMAF)+"|"+ASP2+"\n"
        IMPRIME_TRANSF=False
EndIf   
LINHA=0                               
SOMAP=0

EndProc
;*******************************************************************************
;* Rotina de Soma Total                                                        *
;*******************************************************************************
Proc SomaTot()

Print File ARQIMP ASP1+"|"+Fill(" ",130)+"|"+ASP2+"\n"
Print File ARQIMP ASP1+"|"+Fill("=",130)+"|"+ASP2+"\n"
Print File ARQIMP ASP1+"|"+Spaces(101)+"SOMA TOTAL:  "+Format("W16.2,EIC",SOMAT)+"|"+ASP2+"\n"

EndProc
;*******************************************************************************
;* Rotina Topo do Relatorio                                                    *
;*******************************************************************************
Proc TOPOREL()

Print File ARQIMP ASP1+" \n"
Print File ARQIMP ASP1+"+==================================================================================================================================+"+ASP2+"\n"
Print File ARQIMP ASP1+"|                                             R E G I S T R O  D E  I N V E N T A R I O                                            | "+ASP2+"\n"
Print File ARQIMP ASP1+"|                                                                                                                                  | "+ASP2+"\n"
Print File ARQIMP ASP1+"|  FIRMA: "+Format("W20,AL",Razao)+Spaces(101)+"| "+ASP2+"\n"
Print File ARQIMP ASP1+"|  INSCRICAO ESTADUAL: "+Format("W17,AL",IE)+" CNPJ(MF): "+Format("W20,AL",CGC)+Spaces(60)+"| "+ASP2+"\n"
Print File ARQIMP ASP1+"|  FOLHA:"+Format("W10,AL",FOLHA)+"              ESTOQUE EXISTENTES EM: "+Format("D13",VSDGDTREF)+Spaces(65)+"| "+ASP2+" \n"
Print File ARQIMP ASP1+"|                                                                                                                                  | "+ASP2+" \n"
Print File ARQIMP ASP1+"|==================================================================================================================================|"+ASP2+" \n"
Print File ARQIMP ASP1+"|CLASSIFICACAO |                DISCRIMINACAO                 |  NCM   | UNIDADE |   QUANTIDADE   |             VALORES            |"+ASP2+" \n"
Print File ARQIMP ASP1+"|              |                                              |        |         |                |--------------------------------| "+ASP2+" \n"
Print File ARQIMP ASP1+"|   FISCAL     |                                              |        |         |                |                |               | "+ASP2+" \n"
Print File ARQIMP ASP1+"|              |                                              |        |         |                |   UNITARIO     |    TOTAL      | "+ASP2+" \n"
Print File ARQIMP ASP1+"|==================================================================================================================================|" +ASP2+" \n"
FOLHA=FOLHA+1
EndProc
;*******************************************************************************
;* Rotina de Rodape do Relatorio                                               *
;*******************************************************************************
Proc Rodape()

Print File ARQIMP ASP1+"+"+Fill("=",130)+"+"+ASP2+"\n"
Print File ARQIMP ASP1+"\f"+ASP2+"\n"

EndProc
;*******************************************************************************
;* Rotina Principal                                                            *
;*******************************************************************************
ASP1=""
ASP2=""
Switch
  Case OK="I": ARQIMP = DIREPORT+"RELPROD.TXT"
  Case OK="A": ARQIMP = DIREPORT+ARQUIVO+".TXT"
  Case OK="V": DIREPORT="\\Sistemas\\Reports\\"
               ARQIMP = DIREPORT+"TEMP_TXT.TXT"
               ASP1="\""
               ASP2="\""
EndSwitch                       
Deleta_Arquivo(ARQIMP)
If IsTable("Answer") Then Delete "Answer" EndIf

AGUARDE("Imprimindo")
Imprime()
If OK="V"
   Then {Tools} {Copy} {Table} {\\Base\\Sistemas\\Estoque\\Rm_tx160} {Answer}
        {Tools} {ExportImport} {Import} {ASCII} {AppendDelimited} TypeIn ARQIMP Enter
        {Answer} ClearImage
        IMPRIREL("Answer","1")
        Deleta_Arquivo(ARQIMP)
   Else Mensagem("Impress„o Concluida !")
        If OK="I"
           Then
;---- Imprime Arquivo Impressao ------------------------------------------------
            ImpriArq(ARQIMP)
        EndIf
EndIf

Release Vars LINHA, SOMAP, SOMAT, FOLHA, CODEMP, IE, CGC, Razao, PNC
Release Procs TOPOREL, SOMAPAR, SOMATOT, RODAPE, Imprime