;** ExpSSrv ********************************************************************
;*  Exportacao de Notas de Servicos/Saidas     p/ Arquivo Texto                *
;*******************************************************************************
;Autor           : Vise Informatica (VISERP)
;Desenvolvido    : 04/01/2011 por Anivel Augusto Rodrigues
;Ultima Alteracao: 07/01/2011 por Anivel Augusto Rodrigues 
;Motivo Alteracao: Ajustes
;Ultima Alteracao: 20/08/2012 por Anivel Augusto Rodrigues
;Motivo Alteracao: Ajustes desprezar item notai cancalado

;*******************************************************************************
;* Rotina para Selecionar Registros e Imprimir em Video ou Arquivo             *
;*******************************************************************************
Proc Seleciona()
ClearAll
If IsTable("\\Sistemas\\Vendas\\"+Dir_Trab+"NotaC") 
   Then {Ask} TypeIn "\\Sistemas\\Vendas\\"+Dir_Trab+"NotaC" Enter Check
         Moveto [Codigo da Empresa      ] Example "cemp" TypeIn ",~VSDGEMPRESA.."
         Moveto [Numero do Documento    ] Example "ndoc"                           
         Moveto [Serie do Documento     ] Example "sdoc"                           
         Moveto [Data de Emissao        ] TypeIn ">=~VSDGDTINI,<=~VSDGDTFIM"
         Moveto [Tipo Movimento Estoque ] TypeIn "S"
         Moveto [Valor Servico          ] TypeIn ">0"
        {Ask} TypeIn "\\Sistemas\\Vendas\\"+Dir_Trab+"NotaI" Enter             
         Moveto [Codigo da Empresa      ] Example "cemp"
         Moveto [Numero do Documento    ] Example "ndoc"                           
         Moveto [Serie do Documento     ] Example "sdoc"
         Moveto [Tipo do Item           ] TypeIn "C"
         Moveto [Aliquota de ISSQN      ] Check                         
         Moveto [Preco Total            ] TypeIn "Calc Sum as Valor Soma Item"
         Moveto [Situacao_Item          ] TypeIn "Not C"
         Do_It!
         ClearAll
EndIf

EndProc  ;  (Seleciona)

;*******************************************************************************
;* Gera Arquivo Texto                                                          *
;*******************************************************************************
Proc EXPORTA_SS()
ClearAll

c=0
NR=NRecords(Tabela)

ARQ="\\Sistemas\\Exporta\\Dados\\Asplan\\MV"+"0"+PREFIXO+MES+".TXT"
arquivos=arquivos+Completa(ARQ,62," ")+"//"
gerou_arquivos=True

Deleta_Arquivo(ARQ)

View Tabela
Scan
   Barra(NR, RecNo())
   Aguarde("Aguarde, Exportando Notas de Servi‡o/Saida "+BARRA_STRING)

   C=C+1

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

   Print File ARQ ALINHM(PREFIXO,4)
   Print File ARQ Fill(" ",4)
   Print File ARQ AlinhM([Numero do Documento],6)
   Print File ARQ Fill(" ",12)
   Switch
     Case SYS_CODIGO_CLIENTE="0092" : Print File ARQ "0105"  ; Codigo Servi‡o
     OtherWise                      : Print File ARQ "0000"
   EndSwitch
   Print File ARQ Fill(" ",3)
   Print File ARQ Completa([Serie do Documento],3," ")
   Print File ARQ Fill(" ",4)
   Print File ARQ SubStr(DATA2STR_S([Data de Emissao]),1,2)
   Print File ARQ Fill(" ",1)
   Print File ARQ SubStr(DATA2STR_S([Data de Emissao]),3,2)
   Print File ARQ Fill(" ",1)
   Print File ARQ SubStr(DATA2STR_S([Data de Emissao]),5,2)
   Print File ARQ Fill(" ",6)
   Switch
     Case [Situacao]="C" : Print File ARQ "9"
     OtherWise           : Print File ARQ "0"
   EndSwitch
   Print File ARQ Fill(" ",5)
   Print File ARQ SubStr(VALOR2STR_S([Valor Servico    ],12),01,10)
   Print File ARQ Fill(" ",1)
   Print File ARQ SubStr(VALOR2STR_S([Valor Servico    ],12),11,02)
   Print File ARQ Fill(" ",59)
   Switch
     Case [Valor ISSQN]>0    : Print File ARQ SubStr(VALOR2STR_S([Valor Servico],12),01,10)
                               Print File ARQ Fill(" ",1)
                               Print File ARQ SubStr(VALOR2STR_S([Valor Servico],12),11,02)
     OtherWise               : Print File ARQ "0000000000 00"
   EndSwitch
   Print File ARQ Fill(" ",4)
   Print File ARQ SubStr(VALOR2STR_S([Aliquota de ISSQN],12),01,03)
   Print File ARQ Fill(" ",1)
   Print File ARQ SubStr(VALOR2STR_S([Aliquota de ISSQN],12),04,02)
   Print File ARQ Fill(" ",5)
   Print File ARQ SubStr(VALOR2STR_S([Valor ISSQN      ],12),01,10)
   Print File ARQ Fill(" ",1)
   Print File ARQ SubStr(VALOR2STR_S([Valor ISSQN      ],12),11,02)
   Print File ARQ Fill(" ",5)
   Print File ARQ "0000000000 00"
   Print File ARQ Fill(" ",1)
   Switch
     OtherWise               : Print File ARQ "0"   ; Tipo Isen‡Ćo
   EndSwitch
   Print File ARQ Fill(" ",5)
   Switch
     Case [Valor ISSQN]=0    : Print File ARQ SubStr(VALOR2STR_S([Valor Servico],12),01,10)
                               Print File ARQ Fill(" ",1)
                               Print File ARQ SubStr(VALOR2STR_S([Valor Servico],12),11,02)
     OtherWise               : Print File ARQ "0000000000 00"
   EndSwitch
   Print File ARQ Fill(" ",3)
   Print File ARQ Completa(Substr([Cidade],1,15),15," ")
   Print File ARQ Fill(" ",5)
   Print File ARQ "0000"
   Print File ARQ Fill(" ",3)
   Print File ARQ Completa(CGCCPF,14," ")
   Print File ARQ "0000000000000"
   Print File ARQ "0000000000000"
   Print File ARQ "0000000000000"
   Print File ARQ "00000"
   Switch
     OtherWise                      : Print File ARQ "N"  ;Profissional Liberal
   EndSwitch
   Switch
     Case SYS_CODIGO_CLIENTE="0092" : Print File ARQ "41   "  ; Modelo nota fiscal
     OtherWise                      : Print File ARQ "     "
   EndSwitch
   Print File ARQ "0000000000000"
   Print File ARQ "0000000000000"
   Print File ARQ "0000000000000"
   Print File ARQ "        "
   Print File ARQ "000000000000"
   Switch
     Case [Tipo de Pagamento]="V" : Print File ARQ "01" ; A Vista
     Case [Tipo de Pagamento]="P" : Print File ARQ "02" ; A Prazo
     OtherWise                    : Print File ARQ "00"
   EndSwitch
   Print File ARQ Completa([Razao Social],40," ")
   Print File ARQ Completa([UF          ],02," ")
   Print File ARQ "000000"
   Print File ARQ "00000"
   Print File ARQ "00000"
   Print File ARQ "0000000000000"
   Print File ARQ "0000000000000"
   Print File ARQ "0000000000000"
   Print File ARQ "0000000000000"

   Print FIle ARQ "\n"

EndScan

EndProc

;***************************************
; CONVERTE DATA DE GRAVACAO PARA STRING
;***************************************
PROC DATA2STR_S(DATA)
DTGRAV=AlinhM(LTrim(Format("D13",DATA)),10)
DTGRAV=SUBSTR(DTGRAV,1,2)+SUBSTR(DTGRAV,4,2)+SUBSTR(DTGRAV,9,2)
RETURN DTGRAV
ENDPROC

;*****************************************
;* CONVERTE VALOR PARA STRING            *
;*****************************************
PROC VALOR2STR_S(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

;*******************************************************************************
;* ROTINA PRINCIPAL                                                            *
;*******************************************************************************

AGUARDE("Aguarde, Processando Notas de Servi‡o/Saida")
Tabela=Cria_Temp(PrivDir())

Monta_Dir_Trab(VSDGDTINI,VSDGDTFIM)
While Novo_Dir_Trab()
  If IsTable("Answer")     Then Delete "Answer" EndIf
  Seleciona()
  If Not IsTable("Answer") Then Loop EndIf
  If IsTable(Tabela)
     Then Add "Answer" Tabela
     Else Rename "Answer" Tabela
  EndIf
EndWhile

EXPORTA_SS()
If (c=0)
   Then MENSAGEM("N„o Ocorreu Exporta‡„o de Notas de Servi‡o/Saida")
        Return
EndIf
Delete Tabela

ClearAll
LimpAguarde()
Release Procs SELECIONA, EXPORTA_SS, DATA2STR_S,VALOR2STR_S
