;** Expp1p2  *******************************************************************
;* Exporta arquivos texto Registros P1 e P2 para a SEF-MG                      *
;*******************************************************************************
;Autor           : Vise Informatica (VISERP)
;Desenvolvido    : 08/07/2005 por Jose luis Ameno da Silva
;Ultima Alteracao: 14/08/2007 por Anivel Augusto Rodrigues
;Motivo Alteracao: Ajustes Deleta_Arquivo
;Ultima Alteracao: 01/10/2016 por Anivel Augusto Rodrigues
;Motivo Alteracao: Ajustes showdialog.                  

;*******************************************************************************
;* Rotina p/ Selecionar e Ordernar Registros                                   *
;*******************************************************************************
Proc SELECIONA()
ClearAll

{Ask} {\\Sistemas\\Estoque\\Itens}
Moveto [Codigo do Item      ] Example "CODIGO"
Moveto [Inativo             ] TypeIn "Not S"
Moveto [Tributacao de ICMS  ] Check
Moveto [Aliquota de ICMS    ] Check
Moveto [Tributacao de ISSQN ] Check
Moveto [Aliquota de ISSQN   ] Check

{Ask} {\\Sistemas\\Vendas\\TbPrite}
Moveto [Codigo da Tabela    ] TypeIn "~VSDGTABELA.."
Moveto [Codigo do Item      ] Check Example "CODIGO"
Moveto [Descricao do Item   ] Check
Moveto [Unidade Fiscal      ] Check
Moveto [Preco Unitario      ] Check
Do_It!
ClearAll
If IsEmpty("Answer")
   Then Return
EndIf

EndProc

;********************************************************************************
;* CONVERTE VALOR de 2 casas decimais PARA STRING                                                  *
;********************************************************************************
PROC VALOR2STR2(VALOR,TAM)

VRTIT=STRVAL(VALOR)
PART1=LEN(VRTIT)-3
PART2=LEN(VRTIT)-1
VRTIT=SUBSTR(VRTIT,1,PART1)+SUBSTR(VRTIT,PART2,2)
VRTIT=FILL("0",TAM-LEN(VRTIT))+VRTIT
RETURN VRTIT
ENDPROC

;*******************************************************************************
;* Gera Arquivo Texto p/                                                       *
;*******************************************************************************
Proc EXPORTA()
ClearAll

c=0
View "\\Sistemas\\Cadastro\\Empresas"
Moveto [Codigo da Empresa]
Locate VSDGEMPRESA
If Not Retval
   Then Mensagem("Empresa n„o cadastrada !")
        Return
Endif

CGCCPF=""
TIPOP =""
If SubStr([CGC Empresa],03,1)="." and
      SubStr([CGC Empresa],07,1)="." and
      SubStr([CGC Empresa],11,1)="/" and
      SubStr([CGC Empresa],16,1)="-"
   Then TIPOP  ="J"
           CGCCPF=SubStr([CGC Empresa],01,2)+
                  SubStr([CGC Empresa],04,3)+
                  SubStr([CGC Empresa],08,3)+
                  SubStr([CGC Empresa],12,4)+
                  SubStr([CGC Empresa],17,2)
   Else If SubStr([CGC Empresa],04,1)="." and
              SubStr([CGC Empresa],08,1)="." and
              SubStr([CGC Empresa],12,1)="-"
              Then TIPOP  ="F"
                   CGCCPF=SubStr([CGC Empresa],01,3)+
                          SubStr([CGC Empresa],05,3)+
                          SubStr([CGC Empresa],09,3)+
                          SubStr([CGC Empresa],13,2)
        EndIf
EndIf

IE=""
If SubStr([IE Empresa],04,1)="." and
      SubStr([IE Empresa],11,1)="."
   Then IE=SubStr([IE Empresa],01,3)+SubStr([IE Empresa],05,6)+SubStr([IE Empresa],12,4)
   Else IE=Completa(Substr([IE Empresa],1,14),14, " ")
EndIf

RAZAO=Completa(Substr([Razao Social Empresa],1,50),50," ")
ClearImage

View "\\Sistemas\\Icms_Ipi\\Receitae"
Moveto [Codigo da Empresa]
Locate VSDGEMPRESA
If Not Retval
   Then Mensagem("Empresa n„o cadastrada !")
        Return
Endif
IM=""
If SubStr([Inscricao Municipal],04,1)="." and
      SubStr([Inscricao Municipal],11,1)="."
   Then IM=SubStr([Inscricao Municipal],01,3)+SubStr([Inscricao Municipal],05,6)+SubStr([Inscricao Municipal],12,4)
   Else IM=Completa(Substr([Inscricao Municipal],1,14),14, " ")
EndIf
ClearImage
NR=NRecords("\\Sistemas\\Exporta\\Answer")

If DirExists("\\Sistemas\\Exporta\\Dados\\Sef")=0
   Then Cria_Pasta("\\Sistemas\\Exporta\\Dados\\Sef")
EndIf

ARQ="\\Sistemas\\Exporta\\Dados\\Sef\\Mgp1p2.TXT"

Deleta_Arquivo(ARQ)

;;REGISTRO TIPO P1
Print File ARQ "P1"
Print File ARQ Completa(Substr(CGCCPF,1,14),14," ")
Print File ARQ Completa(Substr(IE,1,14),14," ")
Print File ARQ Completa(Substr(IM,1,14),14," ")
Print File ARQ Completa(Substr(RAZAO,1,50),50," ")
Print File ARQ Fill(" ",32)
Print FIle ARQ "\n"

View "\\Sistemas\\Exporta\\Answer"
Contador= 0
Scan
   Contador = Contador + 1
   Barra(NR, RecNo())
   Aguarde("Aguarde, Exportando Dados "+BARRA_STRING)

;;REGISTRO TIPO P2
   Print File ARQ "P2"
   Print File ARQ Completa(Substr(CGCCPF,1,14),14," ")
   Print File ARQ Completa(Substr([Codigo do Item],1,14),14," ")
   Print File ARQ Completa(Substr([Descricao do Item],1,50),50," ")
   Print File ARQ Completa(Substr([Unidade Fiscal],1,06),06," ")
   ALIQUOTA=[Aliquota de ICMS]
   SITUACAO=[Tributacao de ICMS]
   If [Tributacao de ICMS]="S"
      Then SITUACAO="F"
      Else If [Tributacao de ICMS]="D"
              Then SITUACAO="T"
              Else If [Tributacao de ICMS]="O" Or [Tributacao de ICMS]=" "
                      Then SITUACAO="S"
                           ALIQUOTA=[Aliquota de ISSQN]
                   Endif
           Endif
   Endif
   Print File ARQ Completa(Substr(SITUACAO,1,01),01," ")
   Print File ARQ VALOR2STR2(ALIQUOTA,04)
   Print File ARQ VALOR2STR2([Preco Unitario],12)
   Print File ARQ Fill(" ",23)
   Print FIle ARQ "\n"
   c=1

EndScan
   
   Print File ARQ "P9"
   Print File ARQ Completa(Substr(CGCCPF,1,14),14," ")
   Print File ARQ Completa(Substr(IE,1,14),14," ")
   Print File ARQ VALOR2STR2(Contador,6)
   


EndProc

;*******************************************************************************
;* Rotina                                                                      *
;*******************************************************************************
Proc SD_Dialog(Disparo,Campo,Evento,Elemento)
Temp=True
MSG_CAMPO=""
TECLA=""
If Disparo="OPEN"  Then AoCriarShowDialog() EndIf
If Disparo="EVENT" And Evento["Type"]="KEY" Then
   Switch
     Case Evento["KeyCode"] = 9: Temp=False
     Case Evento["KeyCode"] = -59: Dialog_F1()
     Case Evento["KeyCode"] =  27:
          Temp=False
          Switch
            Case Campo="TG_EMPRESA"        : Campo="NO"
            Case Campo="TG_TABELA"         : Campo="TG_EMPRESA"
            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 IsBlank(VSDGEMPRESA)
                            Then VSDGNOMEMP=""
                            Else Campo="TG_TABELA"
                         EndIf
                    Case Campo="TG_TABELA"     :Campo="YES"
                  EndSwitch
          EndIf
          RefreshDialog
   EndSwitch
EndIf
Monta_Prompt(MSG_CAMPO,TECLA)
Return Temp
EndProc

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

VSDGEMPRESA =""
VSDGTABELA  =""
c=0

While True

  ShowDialog "Exporta‡„o de Tabela de Pre‡os para a SEF-MG" Proc "SD_Dialog"
  Trigger "OPEN","ARRIVE"
  Key "F1","ENTER","ESC","TAB",
      -104, -105, -106, -107, -108, -109, -110, -111, -112, -113, -139, -140  
   @ 04,12   Height 09 Width 72

    Label  @ 00,00 "Empresa"  For "TG_EMPRESA"
    Accept @ 01,01 Width 07 "A4"  Picture "[&,#][#*#]" Tag "TG_EMPRESA"    To VSDGEMPRESA
    Accept @ 01,09 Width 25 "A20" Picture "!*!" Tag "TG_NOMEMPRESA" To VSDGNOMEMP

    Label  @ 02,00 "Tabela"  For "TG_TABELA"
    Accept @ 03,01 Width 08 "A5"  Picture "#*#" Tag "TG_TABELA" To VSDGTABELA
    Accept @ 03,10 Width 24 "A30" Picture "!*!" Tag "TG_NOMTAB" To VSDGNOMTAB

    Pushbutton @ 05,22 width 11 "~O~k"      OK     Default Value "Ok" Tag "YES" To BottonValue
    Pushbutton @ 05,36 width 11 "~C~ancela" CANCEL                    Tag "NO"  To BottonValue

  EndDialog
  If Not RetVal Then QuitLoop EndIf

  AGUARDE("Aguarde, exportando arquivo Tabela de Pre‡os")

  Seleciona()

  If IsEmpty("Answer")
     Then MENSAGEM("N„o existem registros a processar")
          Delete "Answer"
          Loop
  EndIf
  Exporta()
  If c = 0
     Then MENSAGEM("Arquivo n„o foi gerado !")
     Else Mensagem("// Gerado o arquivo: // \\Sistemas\\Exporta\\Dados\\Sef\\MGp1p2 //")
  EndIf

  Delete "Answer"

EndWhile

ClearAll
Release Vars All
Release Procs All
