;** EmitRSG ********************************************************************
;*  Emissao do Registro de Saida                 * Filtrar grupo de itens      *
;*******************************************************************************
;Autor           : Vise Informatica (VISERP)
;Desenvolvido    : 07/04/2016 por Anivel Augusto Rodrigues
;Ultima Alteracao:   /  /     por 
;Motivo Alteracao: 

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

If IsTable("TEMP")
   Then Delete "TEMP"
EndIf

Sort "\\Sistemas\\Vendas\\RegSaida"
     On "Codigo da Empresa", "Data de Emissao", "Especie da Nota Fiscal", "Numero da Nota Fiscal",
        "Serie da Nota Fiscal", "Sequencia da Nota Fiscal"
     To "Temp"
ClearAll

If Not IsBlank(VSDGCFOP)
   Then {Ask} {Temp} TypeIn "Fast Delete"
        Moveto [CFO                   ] TypeIn "Not ~VSDGCFOP"
        Do_It!
        ClearAll
EndIf

If Not IsBlank(VSDGTPDOC)
   Then {Ask} {Temp} TypeIn "Fast Delete"
        Moveto [Especie da Nota Fiscal] TypeIn "Not ~VSDGTPDOC"
        Do_It!
        ClearAll
EndIf

Open Printer
Printer On

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

TOTBASEIPIP = 0
TOTBASEIPIT = 0

TOTCREDIPIP = 0
TOTCREDIPIT = 0

TOTISENIPIP = 0
TOTISENIPIT = 0

TOTOUTRIPIP = 0
TOTOUTRIPIT = 0

DTEMIS=BlankDate()
NNF=""
ENF=""
OBS=""
DOCTO_CANC_INUT=""

If IsFile(PrivDir()+"Cfg_Imp")
   Then Play PrivDir()+"Cfg_Imp"
   Else CI_15CPP=Chr(15)
        CI_10CPP=Chr(18)
EndIf

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

primeiravez_nf=True

TOPOREL()
View "Temp"
Scan
   If [Especie da Nota Fiscal]<>ENF or
      [Numero da nota fiscal]<>NNF
      Then
           If DOCTO_CANC_INUT<>"S" And DOCTO_CANC_INUT<>"4" And DOCTO_CANC_INUT<>"2"
              Then If Not IsBlank(NNF)
                      Then IMPOBS()
                   EndIf
                   If ENF="C/F"
                      Then OBS="Numero Reducao Z : "+
                               Completa(NRZ,6," ")+Spaces(30)+
                               " - Valor Grande Total: "+Format("W14.2, eic",GT)
                           IMPOBS()
                   EndIf
           EndIf

           NNF   =[numero da nota fiscal ]
           ENF   =[especie da nota fiscal]

           ESP=[especie da nota fiscal]
           SER=[serie da nota fiscal]

           Switch
             Case SER="M-1": SER=""
             Case SER="M1A": SER=""
             Case SER="S-1": SER="1"
           EndSwitch

           NUM =[numero da nota fiscal]
           NUMF=[numero varias notas  ]
           DTE =[data de emissao]
           UF  =[estado cliente]
           CP  =[condicao de pagamento]
          ;;;OBS =[Observacao]
           NRZ =[Numero Cupom Reducao]
           GT  =[Totalizador Final      ]
           WY  =0

           DOCTO_CANC_INUT=[Cancelada] 
           primeiravez_nf =True
           
   EndIf
   OBS =[Observacao]

   For wwwi From 1 to 2
     If LINHA>55
       Then SOMAP()
            RODAPE()
            TOPOREL()

            ESP=[especie da nota fiscal]
            SER=[serie da nota fiscal]

           Switch
             Case SER="M-1": SER=""
             Case SER="M1A": SER=""
             Case SER="S-1": SER="1"
           EndSwitch

            NUM =[numero da nota fiscal]
            NUMF=[numero varias notas  ]
            DTE =[data de emissao]
            UF  =[estado cliente]
            CP  =[condicao de pagamento]
            OBS =[Observacao]
            NRZ =[Numero Cupom Reducao]
            GT  =[Totalizador Final      ]

            WY  =0
     EndIf

     If DOCTO_CANC_INUT="S" Or DOCTO_CANC_INUT="4" 
        Or DOCTO_CANC_INUT="2" 
        Then If primeiravez_nf
                Then 
                     primeiravez_nf =False
                     While True
                       If OPCAOPAG<>"Todas"
                           Then If FOLHA<OPCAOPAGINI Or
                                   FOLHA>OPCAOPAGFIM
                                   Then QuitLoop
                                EndIf
                       EndIf
                       Print File ARQIMP ASP1+Format("W4",ESP)
                       Print File ARQIMP Format("W3",SER)
                       Print File ARQIMP Format("W7",NUM)+Spaces(09)
                       Print File ARQIMP Format("W4",AlinhM(StrVal(Day(DTE)),2))
                       Print File ARQIMP OBS+ASP2+"\n"    
                       QuitLoop
                     EndWhile
                     LINHA=LINHA+1
             EndIf
             QuitLoop
     EndIf

     If Len([CFO])<=3
        Then CFO=AlinhM([CFO],3)
        Else CFO=AlinhM([CFO],4)
     EndIf

     VRC=[valor contabil   ]

     If wwwi=1
        Then
             IMP="ICMS"
             BAS=[Base Calculo ICMS]
             ALI=[Aliquota de ICMS ]
             CRE=[ICMS Debitado    ]
             ISE=[ICMS Isento      ]
             OUT=[ICMS Outros      ]
             SUB=[Substituicao ICMS]

             If [Frete Destacado na Nota]<>"S"
             ;SubStr([Observacao],1,10)<>"FRETE ALIQ"
                Then
                     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

                     TOTSUBSICMSP=TOTSUBSICMSP+SUB
                     TOTSUBSICMST=TOTSUBSICMST+SUB

             EndIf
        Else IMP=" IPI"
             BAS=[Base Calculo IPI]
             ALI=[Aliquota de IPI ]
             CRE=[IPI Debitado    ]
             ISE=[IPI Isento      ]
             OUT=[IPI Outros      ]

             TOTBASEIPIP=TOTBASEIPIP+BAS
             TOTBASEIPIT=TOTBASEIPIT+BAS

             TOTCREDIPIP=TOTCREDIPIP+CRE
             TOTCREDIPIT=TOTCREDIPIT+CRE

             TOTISENIPIP=TOTISENIPIP+ISE
             TOTISENIPIT=TOTISENIPIT+ISE

             TOTOUTRIPIP=TOTOUTRIPIP+OUT
             TOTOUTRIPIT=TOTOUTRIPIT+OUT
     EndIf

     If WY=0
        Then DETALHE1()
             WY=1
        Else MSGSOMA=""
             DETALHE2()
     EndIf
     primeiravez_nf =False

   EndFor

EndScan

If DOCTO_CANC_INUT<>"S" And DOCTO_CANC_INUT<>"4" And DOCTO_CANC_INUT<>"2"
   Then If Not IsBlank(NNF)
          Then IMPOBS()
        EndIf
        If ENF="C/F"
          Then OBS="Numero Reducao Z : "+
                   Completa(NRZ,6," ")+Spaces(30)+
                   " - Valor Grande Total: "+Format("W14.2, eic",GT)
               IMPOBS()
        EndIf
EndIf

If LINHA>55
  Then SOMAP()
       RODAPE()
       TOPOREL()
       SOMAT()
  Else SOMAP()
       SOMAT()
EndIf

While LINHA<56
  While True
    If OPCAOPAG<>"Todas"
       Then If FOLHA<OPCAOPAGINI Or
               FOLHA>OPCAOPAGFIM
               Then QuitLoop
            EndIf
    EndIf
    Print File ARQIMP ASP1+ASP2+" \n"
    QuitLoop
  EndWhile
  LINHA=LINHA+1
EndWhile
RODAPE()

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

Printer Off
Close Printer
EndProc  ; (Imprime)

;*******************************************************************************
;* Rotina Observacao                                                           *
;*******************************************************************************
Proc ImpObs()
If IsBlank(OBS) Or
   Search("CANCELAD",OBS)>0 Or
   Search("INUTILIZ",OBS)>0
   Then Return
EndIf

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

 Print File ARQIMP ASP1+ Spaces(39)
 Print File ARQIMP "* "+OBS+" * "+ASP2+"\n"
 QuitLoop
EndWhile
LINHA=LINHA+1
EndProc   ; (ImpObs)

;*******************************************************************************
;* 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+CI_15CPP+ASP2+"\n"
 Print File ARQIMP ASP1+ Format("W138,AC","LIVRO REGISTRO DE SAIDAS - RS - MODELO P2 - GRUPO: "+VSDGNOMGRP)+ASP2+"\n"
 Print File ARQIMP ASP1+ASP2+" \n"
 Print File ARQIMP ASP1+"   Empresa: "+Format("W40",RS)+" - Cod.: "+ce+Spaces(45)+"CONDICOES: 0 - OUTROS "+ASP2+"\n"
 Print File ARQIMP ASP1+"Insc. Est.: "+Format("W14",IE)+Spaces(27)+"C.G.C.: "+Format("W18",CGC)+Spaces(42)+"1 - A VISTA"+ASP2+"\n"
 Print File ARQIMP ASP1+"     Folha: "+Format("W4",FOLHA)+Spaces(62)+"Periodo: "+Format("D13",VSDGDTINI)+" Ate "+Format("D13",VSDGDTFIM)
 Print File ARQIMP Spaces(09)+"2 - A PRAZO"+ASP2+"\n"
 Print File ARQIMP ASP1+"-----------------------------------------------------------------------------------------------------------------------------------------"+ASP2+"\n"
 Print File ARQIMP ASP1+"     DOCUMENTOS FISCAIS     |            |CODIFICACAO |                        V A L O R E S   F I S C A I S                             "+ASP2+"\n"
 Print File ARQIMP ASP1+"----------------------------|            |------------|----------------------------------------------------------------------------------"+ASP2+"\n"
 Print File ARQIMP ASP1+"   |   |             |   |  |   VALOR    |      |     |ICMS|OPERACOES COM DEBITO DO IMPOSTO|OPERACOES SEM DEBITO DO IMP| DEBITO ICMS  |C "+ASP2+"\n"
 Print File ARQIMP ASP1+"ESP|SER|NUMERO       |DIA|UF|  CONTABIL  |CONTAB|FISC | IPI|BASE CALCULO |ALIQ|IMP.DEBITADO|ISENT/N. TRIB|    OUTRAS   |SUBS. TRIBUT. |P "+ASP2+"\n"
 Print File ARQIMP ASP1+"-----------------------------------------------------------------------------------------------------------------------------------------"+ASP2+"\n"
                        ;eee|sss|999999       |99 |XX|9.999.999,99|CONTAB|9.999|xxxx|99.999.999,99|88,8|9.999.999,99|88.888.888,88|99.999.999,99|777.777.777,77|9
                        ;12345678901234567890123456789012341234567890123
 QuitLoop
EndWhile
LINHA=12
EndProc  ;  (TopoRel)

;*******************************************************************************
;* Rotina de Detalhe 1                                                         *
;*******************************************************************************
Proc DETALHE1()
If IMP=" IPI" And SYS_CODIGO_CLIENTE="0167"
   Then Return
EndIf
If IMP=" IPI" And 
   (ATIVIDADE="COMERCIO" Or ATIVIDADE="COM/SERV") And 
   (CRE+ISE+OUT)=0
   Then Return
EndIf

While True

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

 Print File ARQIMP ASP1+Format("W4",ESP)
 Print File ARQIMP Format("W3",SER)
 Print File ARQIMP Format("W7",NUM)
 If Not IsBlank(NUMF) And NUMF<>"0000000"
    Then Print File ARQIMP "/"+Format("W8",NUMF)
    Else Print File ARQIMP "         "
 EndIf
 Print File ARQIMP Format("W3",AlinhM(StrVal(Day(DTE)),2))
 Print File ARQIMP Format("W2",UF)
 Print File ARQIMP Format("W13.2, eic",VRC)
 Print File ARQIMP Spaces(8)
 Print File ARQIMP Substr(CFO,1,1)+"."+Completa(Substr(CFO,2,3),4," ")
 Print File ARQIMP Format("W4",IMP)
 Print File ARQIMP Format("W14.2, eic",BAS)
 Print File ARQIMP Format("W5.1, ei",ALI)
 Print File ARQIMP Format("W13.2, eic",CRE)
 Print File ARQIMP Format("W14.2, eic",ISE)
 Print File ARQIMP Format("W14.2, eic",OUT)
 If IMP="ICMS"
    Then  Print File ARQIMP Format("W14.2, eic",SUB)
    Else  Print File ARQIMP Spaces(15)
 EndIf
 Print File ARQIMP Spaces(02)
 Print File ARQIMP Format("W1",CP)
 Print File ARQIMP ASP2+"\n"
 QuitLoop
EndWhile

LINHA=LINHA+1
EndProc

;*******************************************************************************
;* Rotina de Detalhe 2                                                         *
;*******************************************************************************
Proc DETALHE2()
If IMP=" IPI" And SYS_CODIGO_CLIENTE="0167"
   Then Return
EndIf
If IMP=" IPI" And
   (ATIVIDADE="COMERCIO" Or ATIVIDADE="COM/SERV") And
   (CRE+ISE+OUT)=0 
   Then Return
EndIf

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

 If WT=1
    Then Print File ARQIMP ASP1+MSGSOMA
         Print File ARQIMP Format("W19.2, eic",VRC)
         Print File ARQIMP Spaces(14)
    Else Print File ARQIMP ASP1+Spaces(26)
         If IMP="ICMS"
            Then ;;Print File ARQIMP Format("W2",UF)
                 Print File ARQIMP Spaces(2)
                 Print File ARQIMP Format("W13.2, eic",VRC)
                 Print File ARQIMP Spaces(8)
                 Print File ARQIMP substr(CFO,1,1)+"."+Completa(substr(CFO,2,3),4," ")
            Else Print File ARQIMP Spaces(29)
         EndIf
 EndIf

 Print File ARQIMP Format("W4",IMP)

 Print File ARQIMP Format("W14.2, eic",BAS)
 If WT=0
    Then Print File ARQIMP Format("W5.1, ei",ALI)
    Else Print File ARQIMP Spaces(5)
 EndIf
 Print File ARQIMP Format("W13.2, eic",CRE)
 Print File ARQIMP Format("W14.2, eic",ISE)
 Print File ARQIMP Format("W14.2, eic",OUT)
 If IMP="ICMS"
    Then  Print File ARQIMP  Format("W14.2, eic",SUB)
 EndIf
 Print File ARQIMP ASP2+" \n"
 QuitLoop
EndWhile
LINHA=LINHA+1
EndProc

;*******************************************************************************
;* Rotina de Soma Parcial                                                      *
;*******************************************************************************
Proc SOMAP()
While True
 If OPCAOPAG<>"Todas"
    Then If FOLHA<OPCAOPAGINI Or
            FOLHA>OPCAOPAGFIM
            Then QuitLoop
         EndIf
 EndIf

 Print File ARQIMP ASP1+ASP2+"\n"
 Quitloop
EndWhile

 If TOTVLRCT=0 Then
Print File ARQIMP ASP1+
"                                                                                                                                         "+ASP2+"\n"
Print File ARQIMP ASP1+
"  ** NAO HOUVE MOVIMENTACAO NO PERIDODO **                                                                                               "+ASP2+"\n"
Print File ARQIMP ASP1+
"                                                                                                                                         "+ASP2+"\n"
EndIf

 WT=1
 MSGSOMA=Spaces(08)+"SOMA PARCIAL: "
 IMP="ICMS"
 VRC=TOTVLRCP
 BAS=TOTBASEICMSP
 CRE=TOTCREDICMSP
 ISE=TOTISENICMSP
 OUT=TOTOUTRICMSP
 SUB=TOTSUBSICMSP
 Detalhe2()

 WT=2
 IMP=" IPI"
 BAS=TOTBASEIPIP
 CRE=TOTCREDIPIP
 ISE=TOTISENIPIP
 OUT=TOTOUTRIPIP
 Detalhe2()

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

 Print File ARQIMP ASP1+ASP2+"\n"
 QuitLoop
EndWhile

 LINHA=LINHA+4

 TOTVLRCP=0
 TOTBASEICMSP=0
 TOTCREDICMSP=0
 TOTISENICMSP=0
 TOTOUTRICMSP=0
 TOTSUBSICMSP=0
 TOTBASEIPIP=0
 TOTCREDIPIP=0
 TOTISENIPIP=0
 TOTOUTRIPIP=0
 WT=0

EndProc

;*******************************************************************************
;* Rotina de Soma Total                                                        *
;*******************************************************************************
Proc SOMAT()
 WT=1
 MSGSOMA=Spaces(08)+"SOMA TOTAL..: "
 IMP="ICMS"
 VRC=TOTVLRCT
 BAS=TOTBASEICMST
 CRE=TOTCREDICMST

 ISE=TOTISENICMST
 OUT=TOTOUTRICMST
 SUB=TOTSUBSICMST
 Detalhe2()

 WT=2
 IMP=" IPI"
 BAS=TOTBASEIPIT
 CRE=TOTCREDIPIT
 ISE=TOTISENIPIT
 OUT=TOTOUTRIPIT
 Detalhe2()

 LINHA=LINHA+2
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("-",135)
 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="TG_CFOP"          : Campo="TG_FOLHA"
            Case Campo="TG_TPDOC"         : Campo="TG_CFOP"
            Case Campo="TG_GRP"           : Campo="TG_TPDOC"
            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"      :
                         VSDGFOLHA=ControlValue("TG_FOLHA")
                         If Not IsBlank(VSDGFOLHA)
                            Then Campo="TG_CFOP"
                         EndIf
                    Case Campo="TG_CFOP"      : Campo="TG_TPDOC"
                         VSDGCFOP=ControlValue("TG_CFOP")
                    Case Campo="TG_TPDOC"      : Campo="TG_GRP"
                         VSDGTPDOC=ControlValue("TG_TPDOC")
                         
                    Case Campo="TG_GRP"         :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"
  Case Campo="TG_FOLHA":
       MSG_CAMPO="Informe o n£mero da folha inicial"
  Case Campo="TG_CFOP" Or
       Campo="TG_TPDOC":
       MSG_CAMPO="????=Filtrar  Branco=Todos"
  Case Campo="TG_GRP":
       MSG_CAMPO="Informe o grupo especial de produtos"
EndSwitch
Monta_Prompt(MSG_CAMPO,TECLA)
Return Temp
EndProc

;*******************************************************************************
;* ROTINA PRINCIPAL                                                            *
;*******************************************************************************
AutoLib="\\Sistemas\\Cadastro\\Bib_Show,\\Sistemas\\Cadastro\\Bibliot"

If Not IsAssigned(SYS_CODIGO_CLIENTE)  Then SYS_CODIGO_CLIENTE=""  EndIf
If Not IsAssigned(SYS_CLIENTE_CONFEC)  Then SYS_CLIENTE_CONFEC=""  EndIf

LimpAguarde()
PROCESSO_OK=True
VSDGEMPRESA=""
VSDGFOLHA  =""
VSDGDTINI  =BlankDate()
VSDGDTFIM  =BlankDate()
VSDGGRUP    =""
VSDGNOMGRP  =""

While True
  ShowDialog "Emiss„o do Registro de Saidas/Grupo" 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 13 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 "N§Folha"  For "TG_FOLHA"
    Accept @ 5,01 Width 07 "A4"  Picture "#*#" Tag "TG_FOLHA" To VSDGFOLHA

    Label  @ 04,09 "CFOP"  For "TG_CFOP"
    Accept @ 5,10 Width 07 "A4"  Picture "#*#" Tag "TG_CFOP" To VSDGCFOP

    Label  @ 04,18 "Esp.Docto"  For "TG_TPDOC"
    Accept @ 5,19 Width 07 "A4"  Picture "!*!" Tag "TG_TPDOC" To VSDGTPDOC

    Label  @ 06,00 "Grupo"  For "TG_GRP"
    Accept @ 07,02 Width 06 "A3"  Picture "#*#" Tag "TG_GRP"    To VSDGGRP
    Accept @ 07,09 Width 33 "A30" Picture "!*!" Tag "TG_NOMGRP" To VSDGNOMGRP

    Pushbutton @ 09,14 width 11 "~O~k"      OK     Default Value "Ok" Tag "YES" To BottonValue
    Pushbutton @ 09,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

  If IsBlank(VSDGGRP)
     Then Mensagem("Grupo Invalido !")
          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"

  If PROCESSO_OK
     Then ASP1=""
          ASP2=""
          ARQIMP = DIREPORT+"REGSAIDA.TXT"
          Switch
            Case OK="A": ARQIMP = DIREPORT+ARQUIVO+".TXT"
            Case OK="V": ASP1="\""
                         ASP2="\""
          EndSwitch
          Deleta_Arquivo(ARQIMP)
          If IsTable("Answer") Then Delete "Answer" EndIf
          IMPRIME()

          If OK="V"
             Then {Tools} {Copy} {Table} {Rm_tx250} {Answer} ClearAll
                  {Tools} {ExportImport} {Import} {ASCII} {AppendDelimited} TypeIn ARQIMP Enter
                  {Answer} ClearAll
                  OPCAOPAG="Todas"
                  IMPRIREL("Answer","1")
                  Deleta_Arquivo(ARQIMP)
             Else If OK="I"
                     Then ImpriArq(ARQIMP)
                  EndIf
                  Mensagem("Impress„o Concluida !")
          EndIf
  EndIf

  Delete "Temp"
  LimpAguarde()

EndWhile
ClearAll
Release Vars All
Release Procs All
