;** ApSUF   ********************************************************************
;*  Emissao da Apuracao de ICMS das Saidas por Estado                          *
;*******************************************************************************
;Autor           : Vise Informatica (VISERP)
;Desenvolvido    : 04/05/2008 por Anivel Augusto Rodrigues
;Ultima Alteracao: 17/12/2009 por Anivel Augusto Rodrigues
;Motivo Alteracao: Ajustes voltar diretorio ICMS_IPI
;Ultima Alteracao: 11/01/2011 por Anivel Augusto Rodrigues
;Motivo Alteracao: Ajustes ignorar obs FRETE ALIQ
;Ultima Alteracao: 22/02/2011 por Bruno Carvalho                           
;Motivo Alteracao: Add Variavel ATIVIDADE    

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

{Ask} {\\Sistemas\\Vendas\\Estados}
Moveto [UF                       ] Example "UF"
Moveto [Descricao                ] Check
{Ask} {\\Sistemas\\Vendas\\RegSaida}
Moveto [Estado Cliente    ] Check Example "UF" TypeIn "!, as UF"
Moveto [Valor Contabil    ] TypeIn "Calc Sum as Valor Contabil"
Moveto [Base Calculo ICMS ] TypeIn "Calc Sum as Base"
Moveto [ICMS Debitado     ] TypeIn "Calc Sum as Debito"
Moveto [ICMS Isento       ] TypeIn "Calc Sum as Isento"
Moveto [ICMS Outros       ] TypeIn "Calc Sum as Outros"
Moveto [Cancelada         ] TypeIn "Not S"
Moveto [Observacao        ] TypeIn "Not FRETE ALI.."
Do_It!
ClearAll

Sort "Answer" on "UF"
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
TOTVLRCT=0

TOTBASEICMST = 0

TOTCREDICMST = 0

TOTISENICMST = 0

TOTOUTRICMST = 0

TOTSUBSICMST = 0

UF="****"

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

   UF  = [UF               ]
   NAT = [Descricao        ]
   VRC = [valor contabil   ]
   BAS = [base             ]
   CRE = [debito           ]
   ISE = [isento           ]
   OUT = [outros           ]

   DETALHE1()

   TOTVLRCT=TOTVLRCT+VRC

   TOTBASEICMST = TOTBASEICMST + BAS

   TOTCREDICMST = TOTCREDICMST + CRE

   TOTISENICMST = TOTISENICMST + ISE

   TOTOUTRICMST = TOTOUTRICMST + OUT

EndScan

If LINHA>56
  Then While LINHA<63
         Tit(" ")
       EndWhile
       RODAPE()
       TOPOREL()
       SOMAT()
  Else 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+"APURACAO DO I.C.M.S. POR ESTADO DAS SAIDAS"+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+"                                                              |             |                I.C.M.S.  -  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+"UF DESCRICAO                                                  |  CONTABEIS  |BASE CALCULO |IMP.DEBITADO | ISEN/N.TRIB |    OUTRAS   "+ASP2+"\n"
 Print File ARQIMP ASP1+"------------------------------------------------------------------------------------------------------------------------------------"+ASP2+"\n"
                   ;1234|9.999 123456789012345678901234567890123456789012 a 99,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 Completa(UF,3," ")
    Else Print File ARQIMP Spaces(03)
 EndIf
 Print File ARQIMP Format("W59",nat)
 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 Total                                                        *
;*******************************************************************************
proc SOMAT()
 WT=1
 NAT = "    TOTAL:"+Fill(".",37)
 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="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="YES"
                  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"
EndSwitch
Monta_Prompt(MSG_CAMPO,TECLA)
Return Temp
EndProc

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

While True
  ShowDialog "Apura‡„o do ICMS / Saidas por Estado" 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 09 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

    Pushbutton @ 05,14 width 11 "~O~k"      OK     Default Value "Ok" Tag "YES" To BottonValue
    Pushbutton @ 05,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=0
  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 OK="" Then Loop EndIf
  If PROCESSO_OK
     Then ASP1=""
          ASP2=""
          ARQIMP = DIREPORT+"ApsIcms.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)
             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
