;** ApSIPI *********************************************************************
;*  Emissao da Apuracao de IPI das Saidas                                      *
;*******************************************************************************
;Autor           : Vise Informatica (VISERP)
;Desenvolvido    : 20/08/99 por Anivel Augusto Rodrigues
;Ultima Alteracao: 03/09/2004 por Jos‚ Lu¡s Ameno da Silva
;Motivo Alteracao: PrivDir
;Alterado em     : 29/06/2006 por Antonio Marcelo Alves
;Motivo Alteracao: Inclusao ImpriArq()
;Ultima Alteracao: 14/08/2007 por Anivel Augusto Rodrigues
;Motivo Alteracao: Ajustes Deleta_Arquivo
;Ultima Alteracao: 17/12/2009 por Anivel Augusto Rodrigues
;Motivo Alteracao: Ajustes voltar diretorio ICMS_IPI
;Ultima Alteracao: 22/02/2011 por Bruno Carvalho                           
;Motivo Alteracao: Add Variavel ATIVIDADE 

;*******************************************************************************
;* Rotina para Selecionar Registros                                            *
;*******************************************************************************
Proc SELECIONA()
ClearAll

{Ask} {\\Sistemas\\Cadastro\\CFO}
Moveto [Codigo Fiscal de Operacao] Example "cfo"
Moveto [Descricao do CFO         ] Check
{Ask} {\\Sistemas\\Vendas\\RegSaida}
Moveto [CFO              ] Check Example "cfo" TypeIn "!"
Moveto [Aliquota de IPI  ] Check TypeIn "as Aliquota"
Moveto [Valor Contabil   ] TypeIn "Calc Sum as Valor Contabil"
Moveto [Base Calculo IPI ] TypeIn "Calc Sum as Base"
Moveto [IPI Debitado     ] TypeIn "Calc Sum as Debito"
Moveto [IPI Isento       ] TypeIn "Calc Sum as Isento"
Moveto [IPI Outros       ] TypeIn "Calc Sum as Outros"
Moveto [Cancelada         ] TypeIn "Not S"
Do_It!
ClearAll

Sort "Answer" on "CFO","Aliquota"
PROCESSO_OK=True

ClearAll
EndProc


;*******************************************************************************
;* Rotina de Impressao                                                         *
;*******************************************************************************
Proc IMPRIME()
Open Printer
Printer On

Print File ARQIMP ASP1+CI_15CPP+ASP2+"\n"

LINHA=62
   WX4=0
WT=0
TOTVLRCP=0
TOTVLRCT=0

TOTBASEICMSP = 0
TOTBASEICMST = 0

TOTCREDICMSP = 0
TOTCREDICMST = 0

TOTISENICMSP = 0
TOTISENICMST = 0

TOTOUTRICMSP = 0
TOTOUTRICMST = 0

TOTSUBSICMSP = 0
TOTSUBSICMST = 0

CFO1=""

View "Answer"
Scan
   If LINHA=62
     Then If WX4=0
             Then TOPOREL()
                  WX4=1
             Else RODAPE()
                  TOPOREL()
          EndIf
   EndIf

   If Substr([CFO],1,1)<>CFO1
      Then If CFO1<>""
              Then SOMAP()
           EndIf
           CFO1=Substr([CFO],1,1)
           If Substr([CFO],1,1)="5"
              Then Tit(" SAIDAS PARA O ESTADO")
           EndIf
           If Substr([CFO],1,1)="6"
              Then Tit(" SAIDAS PARA OUTROS ESTADOS")
           EndIf
           If Substr([CFO],1,1)="7"
              Then Tit(" SAIDAS PARA OUTROS PAISES")
           EndIf
   EndIf

   CFO = [CFO              ]
   NAT = [Descricao do CFO ]
   ALI = [aliquota         ]
   VRC = [valor contabil   ]
   BAS = [base             ]
   CRE = [debito           ]
   ISE = [isento           ]
   OUT = [outros           ]

   DETALHE1()

   TOTVLRCP=TOTVLRCP+VRC
   TOTVLRCT=TOTVLRCT+VRC

   TOTBASEICMSP = TOTBASEICMSP + BAS
   TOTBASEICMST = TOTBASEICMST + BAS

   TOTCREDICMSP = TOTCREDICMSP + CRE
   TOTCREDICMST = TOTCREDICMST + CRE

   TOTISENICMSP = TOTISENICMSP + ISE
   TOTISENICMST = TOTISENICMST + ISE

   TOTOUTRICMSP = TOTOUTRICMSP + OUT
   TOTOUTRICMST = TOTOUTRICMST + OUT

EndScan

If LINHA>56
  Then While LINHA<63
         Tit(" ")
       EndWhile
       SOMAP()
       RODAPE()
       TOPOREL()
       SOMAT()
  Else SOMAP()
       SOMAT()
EndIf
Tit(" ")
RODAPE()

Print File ARQIMP ASP1+CI_10CPP+ASP2+"\n"
Printer Off
Close Printer
EndProc

;*******************************************************************************
;* Rotina Topo de Formulario                                                   *
;*******************************************************************************
Proc TOPOREL()

FOLHA=FOLHA+1

While True
 If OPCAOPAG<>"Todas"
    Then If FOLHA<OPCAOPAGINI Or
            FOLHA>OPCAOPAGFIM
            Then QuitLoop
         EndIf
 EndIf

 Print File ARQIMP ASP1+ASP2+" \n"
 Print File ARQIMP ASP1+Spaces(35)+"R E G I S T R O   D E   A P U R A C A O   D O   I. P. I. "+ASP2+"\n"
 Print File ARQIMP ASP1+ASP2+" \n"
 Print File ARQIMP ASP1+"   EMPRESA: "+Format("W40",RS)+" - Cod.: "+ce+ASP2+" \n"
 Print File ARQIMP ASP1+"Insc. Est.: "+Format("W14",IE)+Spaces(27)+"C.G.C.: "+Format("W18",CGC)+ASP2+"\n"
 Print File ARQIMP ASP1+"     FOLHA: "+AlinhM(StrVal(FOLHA),3)+Spaces(37)+"Periodo: "+StrVal(VSDGDTINI)+" ate "+StrVal(VSDGDTFIM)+ASP2+" \n"
 Print File ARQIMP ASP1+ASP2+" \n"
 Print File ARQIMP ASP1+"------------------------------------------------------------------------------ SAIDAS ----------------------------------------------"+ASP2+"\n"
 Print File ARQIMP ASP1+"  CODIF.  |                                                   |             |                I.P.I.    -  VALORES FISCAIS           "+ASP2+"\n"
 Print File ARQIMP ASP1+"          |                                                   |             |-------------------------------------------------------"+ASP2+"\n"
 Print File ARQIMP ASP1+"----------|                                                   |   VALORES   | OP. COM DEBITO DO IMPOSTO | OP. SEM DEBITO DO IMPOSTO "+ASP2+"\n"
 Print File ARQIMP ASP1+"    |     |                                                   |             |-------------------------------------------------------"+ASP2+"\n"
 Print File ARQIMP ASP1+"CONT| FISC|                                                   |  CONTABEIS  |BASE CALCULO |IMP.DEBITADO | ISEN/N.TRIB |    OUTRAS   "+ASP2+"\n"
 Print File ARQIMP ASP1+"------------------------------------------------------------------------------------------------------------------------------------"+ASP2+"\n"
                   ;1234|9.999  123456789012345678901234567890123456789012 a99,99%|88.888.888,88|77.777.777,77|66.666.666,66|55.555.555,55|44.444.444,44
 QuitLoop
EndWhile
LINHA=14
Tit(" ")
EndProc

;*******************************************************************************
;* Rotina de Detalhe 1                                                         *
;*******************************************************************************
proc DETALHE1()

While True
 If OPCAOPAG<>"Todas"
    Then If FOLHA<OPCAOPAGINI Or
            FOLHA>OPCAOPAGFIM
            Then QuitLoop
         EndIf
 EndIf

 Print File ARQIMP ASP1+"    |"
 If WT=0
    Then Print File ARQIMP Substr(CFO,1,1)+"."+Substr(CFO,2,3)
    Else Print File ARQIMP Spaces(05)
 EndIf
 If Len(CFO)=3
   Then Print File ARQIMP Spaces(2)+Format("W42",nat)
   Else Print File ARQIMP Spaces(1)+Format("W42",nat)
 EndIf
 If WT=0
    Then Print File ARQIMP " /"+Format("W6.2, ei",ali)+"%"
    Else Print File ARQIMP Spaces(9)
 EndIf
 Print File ARQIMP "|"+Format("W13.2, eic",VRC)
 Print File ARQIMP "|"+Format("W13.2, eic",bas)
 Print File ARQIMP "|"+Format("W13.2, eic",cre)
 Print File ARQIMP "|"+Format("W13.2, eic",ise)
 Print File ARQIMP "|"+Format("W13.2, eic",out)
 Print File ARQIMP ASP2+"\n"
 QuitLoop
EndWhile
LINHA=LINHA+1
Endproc

;*******************************************************************************
;* Rotina de Tiulo                                                             *
;*******************************************************************************
proc Tit(ntit)
Private ntit
While True
 If OPCAOPAG<>"Todas"
    Then If FOLHA<OPCAOPAGINI Or
            FOLHA>OPCAOPAGFIM
            Then QuitLoop
         EndIf
 EndIf

 Print File ARQIMP ASP1+"    |"
 Print File ARQIMP COMPLETA(ntit,57," ")
 Print File ARQIMP "|             |             |             |             |             "+ASP2+"\n"
 QuitLoop
EndWhile
LINHA=LINHA+1
EndProc

;*******************************************************************************
;* Rotina de Soma Parcial                                                      *
;*******************************************************************************
proc SOMAP()
 WT=1
   NAT = " SUBTOTAL:"+Fill(".",38)

 VRC = TOTVLRCP
 BAS = TOTBASEICMSP
 CRE = TOTCREDICMSP
 ISE = TOTISENICMSP
 OUT = TOTOUTRICMSP
 DETALHE1()
 Tit(" ")

 TOTVLRCP=0
 totbaseicmsp=0
 TOTCREDICMSP=0
 TOTISENICMSP=0
 TOTOUTRICMSP=0
 WT=0

Endproc

;*******************************************************************************
;* Rotina de Soma Total                                                        *
;*******************************************************************************
proc SOMAT()
 WT=1
 NAT = "    TOTAL:"+Fill(".",38)
 VRC = TOTVLRCT
 BAS = TOTBASEICMST
 CRE = TOTCREDICMST
 ISE = TOTISENICMST
 OUT = TOTOUTRICMST
 DETALHE1()

 LINHA=LINHA+1
Endproc

;*******************************************************************************
;* Rotina Topo de Rodape do Formulario                                         *
;*******************************************************************************
proc RODAPE()
While True
 If OPCAOPAG<>"Todas"
    Then If FOLHA<OPCAOPAGINI Or
            FOLHA>OPCAOPAGFIM
            Then QuitLoop
         EndIf
 EndIf

 Print File ARQIMP ASP1+Fill("-",132)
 Print File ARQIMP Chr(12)+ASP2+"\n"
 QuitLoop
EndWhile
Endproc

;*******************************************************************************
;* Rotina                                                                      *
;*******************************************************************************
Proc SD_Dialog(Disparo,Campo,Evento,Elemento)
Temp=True
TECLA    =""
MSG_CAMPO=""
If Disparo="OPEN"  Then AoCriarShowDialog() EndIf
If Disparo="EVENT" And Evento["Type"]="KEY" Then
   Switch
     Case Evento["KeyCode"] = -59: Dialog_F1()
     Case Evento["KeyCode"] =  27:
          Temp=False
          Switch
            Case Campo="TG_EMPRESA"       : Campo="NO"
            Case Campo="TG_DTINI"         : Campo="TG_EMPRESA"
            Case Campo="TG_DTFIM"         : Campo="TG_DTINI"
            Case Campo="TG_FOLHA"         : Campo="TG_DTFIM"
            Case Campo="YES" Or Campo="NO": Campo="TG_EMPRESA"
          EndSwitch
     Case (Evento["KeyCode"] <= -104 And Evento["KeyCode"] >= -113) Or 
           Evento["KeyCode"]  = -139 Or  Evento["KeyCode"]  = -140:
           SYS_TECLA_ACIONADA=Evento["KeyCode"]
           Seta_Tecla_Acionada()
           CancelDialog
     Case Evento["KeyCode"] = 13:
          Temp=False
          If Checa_TAG()
             Then Switch
                    Case Campo="TG_EMPRESA"    :
                         If Not Isblank(VSDGEMPRESA)
                            Then Campo="TG_DTINI"
                            Else VSDGNOMEMP=""
                         EndIf
                    Case Campo="TG_DTINI"      : Campo="TG_DTFIM"
                    Case Campo="TG_DTFIM"      : Campo="TG_FOLHA"
                    Case Campo="TG_FOLHA"      : Campo="YES"
                         VSDGFOLHA=ControlValue("TG_FOLHA")
                  EndSwitch
          EndIf
          RefreshDialog
   EndSwitch
EndIf
Switch
  Case Campo="TG_EMPRESA":
       MSG_CAMPO="Informe uma empresa"
  Case Campo="TG_DTINI":
       MSG_CAMPO="Informe a Data Inicial"
  Case Campo="TG_FOLHA":
       MSG_CAMPO="Informe o n£mero da folha inicial"
EndSwitch
Monta_Prompt(MSG_CAMPO,TECLA)
Return Temp
EndProc

;*******************************************************************************
;* ROTINA PRINCIPAL                                                            *
;*******************************************************************************
AutoLib="\\Sistemas\\Cadastro\\Bib_Show,\\Sistemas\\Cadastro\\Bibliot"
LimpAguarde()
VSDGEMPRESA=""
VSDGFOLHA  =""
VSDGDTINI  =BlankDate()
VSDGDTFIM  =BlankDate()

While True
  ShowDialog "Registro de Apura‡„o do IPI / Saidas" Proc "SD_Dialog"
  Trigger "OPEN","ARRIVE"
  Key "F1","ENTER","ESC",
      -104, -105, -106, -107, -108, -109, -110, -111, -112, -113, -139, -140  
   @ 05,12   Height 11 Width 56
    Label  @ 00,00 "Empresa"  For "TG_EMPRESA"
    Accept @ 1,01 Width 07 "A4"  Picture "[&,#][#*#]" Tag "TG_EMPRESA" To VSDGEMPRESA
    Accept @ 1,09 Width 23 "A20" Picture "!*!" Tag "TG_NOMEMP" To VSDGNOMEMP

    Label  @ 02,00 "Per¡odo Referente" For "TG_DTINI"
    Label  @ 03,00 "de" For "TG_DTINI"
    Accept @ 03,04 Width 13 "D"  Picture "#[#];.##;.##[##]" Tag "TG_DTINI" To VSDGDTINI
    Label  @ 03,17 "at‚" For "TG_DTFIM"
    Accept @ 03,22 Width 13 "D"  Picture "#[#];.##;.##[##]" Tag "TG_DTFIM" To VSDGDTFIM

    Label  @ 04,00 "No Folha"  For "TG_FOLHA"
    Accept @ 5,01 Width 07 "A4"  Picture "#*#" Tag "TG_FOLHA" To VSDGFOLHA

    Pushbutton @ 07,14 width 11 "~O~k"      OK     Default Value "Ok" Tag "YES" To BottonValue
    Pushbutton @ 07,28 width 11 "~C~ancela" CANCEL                    Tag "NO"  To BottonValue
  EndDialog

  If Not RetVal Then QuitLoop EndIf

  If IsBlank(VSDGEMPRESA)
     Then Mensagem("Empresa Invalida !")
          Loop
  EndIf

  If VSDGDTFIM=BlankDate() or VSDGDTINI=BlankDate()
     Then Mensagem("Data Invalida !")
          Loop
  EndIf

  DISPOSITIVO()
  If OK="" Then Loop EndIf

  FOLHA=NumVal(VSDGFOLHA)-1
  View "\\Sistemas\\Cadastro\\Empresas"
  Moveto [Codigo da Empresa]
  Locate VSDGEMPRESA
  If RetVal
     Then CE        = [Codigo da Empresa    ]
          NF        = [Nome Fantasia Empresa]
          RS        = [Razao Social Empresa ]
          CGC       = [CGC Empresa          ]
          IE        = [IE Empresa           ]
          ATIVIDADE = [Atividade            ]
  EndIf
  ClearAll

  AGUARDE("Aguarde, processando")
  Play "\\Sistemas\\Exporta\\RegSaida"
  SetDir "\\sistemas\\icms_ipi"

  SELECIONA()
  If PROCESSO_OK
     Then ASP1=""
          ASP2=""
          ARQIMP = DIREPORT+"ApSIPI.TXT"
          Switch
            Case OK="A": ARQIMP = DIREPORT+ARQUIVO+".TXT"
            Case OK="V": ASP1="\""
                         ASP2="\""
          EndSwitch
          Deleta_Arquivo(ARQIMP)

          AGUARDE("Imprimindo")
          IMPRIME()
          If OK="V"
             Then Tabela=Cria_Temp(PrivDir())
                  {Tools} {Copy} {Table} {\\Base\\Sistemas\\Icms_Ipi\\Rm_tx250} TypeIn Tabela Enter ClearAll
                  {Tools} {ExportImport} {Import} {ASCII} {AppendDelimited} TypeIn ARQIMP Enter
                  TypeIn Tabela Enter ClearAll
                  OPCAOPAG="Todas"
                  IMPRIREL(Tabela,"1")
                  Deleta_Arquivo(ARQIMP)
                  Delete Tabela
             Else Mensagem("Impress„o Concluida !")
                  If OK="I"
                     Then
;---- Imprime Arquivo Impressao ------------------------------------------------
                      ImpriArq(ARQIMP)
                  EndIf
          EndIf
  EndIf

  Empty "\\Sistemas\\Vendas\\RegSaida"
  Delete "Answer"
  LimpAguarde()

EndWhile
SetDir "\\sistemas\\icms_ipi"

ClearAll
Release Vars All
Release Procs All

