;** Rm_0016 ********************************************************************
;*  Demonstracao do Resultado por Decendio                                     *
;*******************************************************************************
;Autor           : Vise Informatica (VISERP)
;Desenvolvido    : 25/07/1996 por Anivel Augusto Rodrigues
;Ultima Alteracao: 18/12/2002 por Karen Lauar Nery
;Motivo Alteracao: Ajustes CFO

;*******************************************************************************
;* Localiza Nota                                                               *
;*******************************************************************************
Proc Localiza_NotaC()
EMPRESA = [Codigo da Empresa  ]
NDOCTO  = [Numero do Documento]
SDOCTO  = [Serie do Documento ]
CopyToArray REGTB

CMV  =BlankNum()
VENDV=BlankNum()
VENDP=BlankNum()

Moveto "Destin"
Moveto [Codigo da Destinacao]
Locate [Temp_Ven->Codigo da Destinacao]
If Retval
   Then Atua_Estoque=[Atualiza Estoque]
   Else Atua_Estoque="N"
EndIf

Moveto "Temp_Ven"
EndProc

;*******************************************************************************
;* Gravar Nota                                                                 *
;*******************************************************************************
Proc Grava_NotaC()
If SearchFrom("VEND",REGTB["Descricao da Destinacao"],1)>=1 or (REGTB["CFO"]="5949" or REGTB["CFO"]="599")
   Then If REGTB["Tipo de Pagamento"]="V" Then VENDV=REGTB["Valor Nota Fiscal"] EndIf
        If REGTB["Tipo de Pagamento"]="P" Then VENDP=REGTB["Valor Nota Fiscal"] EndIf
EndIf

If REGTB["Codigo da Empresa"]<>[Answer->Codigo da Empresa]
   Then Moveto "Answer"
        Moveto [Codigo da Empresa]
        Locate REGTB["Codigo da Empresa"]
        If Not RetVal
           Then End
                If Not IsBlank([Codigo da Empresa])
                   Then Down
                EndIf
                [Codigo da Empresa]=REGTB["Codigo da Empresa"]
        EndIf
        Moveto "Temp_Ven"
EndIf

If REGTB["Data de Emissao"]>=VSDGDTINI And REGTB["Data de Emissao"]<=DATA10
   Then [Answer->VENDV1]=[Answer->VENDV1]+VENDV
        [Answer->VENDP1]=[Answer->VENDP1]+VENDP
        [Answer->CMV1  ]=[Answer->CMV1  ]+CMV
EndIf
If REGTB["Data de Emissao"]>DATA10   And REGTB["Data de Emissao"]<=DATA20
   Then [Answer->VENDV2]=[Answer->VENDV2]+VENDV
        [Answer->VENDP2]=[Answer->VENDP2]+VENDP
        [Answer->CMV2  ]=[Answer->CMV2  ]+CMV
EndIf
If REGTB["Data de Emissao"]>DATA20   And REGTB["Data de Emissao"]<=VSDGDTFIM
   Then [Answer->VENDV3]=[Answer->VENDV3]+VENDV
        [Answer->VENDP3]=[Answer->VENDP3]+VENDP
        [Answer->CMV3  ]=[Answer->CMV3  ]+CMV
EndIf
If REGTB["Data de Emissao"]=VSDGDTREF
   Then [Answer->VENDV5]=[Answer->VENDV5]+VENDV
        [Answer->VENDP5]=[Answer->VENDP5]+VENDP
        [Answer->CMV5  ]=[Answer->CMV5  ]+CMV
EndIf
EndProc

;*******************************************************************************
;* Rotina p/ Selecionar e Order Registros                                      *
;*******************************************************************************
Proc GERA_MOVIM()
If IsAssigned(REGTB)
   Then Release vars REGTB
EndIf
DynArray REGTB[]

ClearAll
Play "\\Sistemas\\Exporta\\RegSaida"
Play "\\Sistemas\\Exporta\\RegEntra"
ClearAll

For wxa From 1 To 2
  Switch
    Case wxa=1:
         If IsTable("\\Sistemas\\Vendas\\Temp_Des") Then Delete("\\Sistemas\\Vendas\\Temp_Des") EndIf
         SetDir "\\Sistemas\\Ctacor"
    Case wxa=2:
         If IsTable("\\Sistemas\\Vendas\\Temp_Ven") Then Delete("\\Sistemas\\Vendas\\Temp_Ven") EndIf
         SetDir "\\Sistemas\\Vendas"
  EndSwitch

  Monta_Dir_Trab(VSDGDTINI,VSDGDTFIM)
  While Novo_Dir_Trab()
    Switch
      Case wxa=1:
           Dir_Buscag  = "\\Sistemas\\Ctacor\\"+Dir_Trab
           If IsTable("\\Sistemas\\Ctacor\\"+Dir_Trab+"MovCont")
              Then {Ask} TypeIn "\\Sistemas\\Ctacor\\"+Dir_Trab+"MovCont" Enter Check
                   Moveto [Codigo da Empresa      ] TypeIn "~VSDGEMPRESA.."
                   Moveto [Data                   ] TypeIn ">=~VSDGDTINI, <=~VSDGDTFIM"
                   Moveto [Tipo Movimento         ] TypeIn "S"
                   Moveto [Descricao do Lancamento] TypeIn "..DESPESA.."
                   Do_It!
                   ClearAll
                   If IsTable("\\Sistemas\\Vendas\\Temp_Des")
                      Then Add "Answer" "\\Sistemas\\Vendas\\Temp_Des"
                      Else Rename "Answer" "\\Sistemas\\Vendas\\Temp_Des"
                   EndIf
           EndIf
      Case wxa=2:
           Dir_Buscag  = "\\Sistemas\\Vendas\\"+Dir_Trab
           If IsTable("\\Sistemas\\Vendas\\"+Dir_Trab+"NotaC")
              Then {Ask} TypeIn "\\Sistemas\\Vendas\\"+Dir_Trab+"NotaC" Enter Check
                   Moveto [Codigo da Empresa      ] Example "emp" TypeIn ", ~VSDGEMPRESA.."
                   Moveto [Numero do Documento    ] Example "doc"
                   Moveto [Serie do Documento     ] Example "ser"
                   Moveto [Data de Emissao        ] TypeIn ">=~VSDGDTINI, <=~VSDGDTFIM"
                   Moveto [Tipo Movimento Estoque ] TypeIn "S"
                   Moveto [Situacao               ] TypeIn "Not C"
                   {Ask} TypeIn "\\Sistemas\\Vendas\\"+Dir_Trab+"NotaI" Enter
                   Moveto [Codigo da Empresa      ] Example "emp"
                   Moveto [Numero do Documento    ] Example "doc"
                   Moveto [Serie do Documento     ] Example "ser"
                   Moveto [Item do Documento      ] Check
                   Moveto [Tipo do Item           ] Check
                   Moveto [Quantidade             ] Check
                   Moveto [Custo Medio Unitario   ] Check
                   Do_It!
                   ClearAll
                   If IsTable("\\Sistemas\\Vendas\\Temp_Ven")
                      Then Add "Answer" "\\Sistemas\\Vendas\\Temp_Ven"
                      Else Rename "Answer" "\\Sistemas\\Vendas\\Temp_Ven"
                   EndIf
           EndIf
    EndSwitch
  EndWhile
EndFor
ClearAll
SetDir "\\Sistemas\\Vendas"

Sort "Temp_Ven" on "Codigo da Empresa","Serie do Documento","Numero do Documento","Item do Documento"
ClearAll

DATA10=VSDGDTINI+9
DATA20=VSDGDTINI+19

{Ask} {RM_0016} Check
Do_It!
ClearAll
Empty("Answer")

View "Answer"
View "\\Sistemas\\Estoque\\RegEntra"
View "\\Sistemas\\Vendas\\RegSaida"
View "\\Sistemas\\Icms_Ipi\\ImpostoI"
View "Destin"
View "Temp_Des"
View "Temp_Ven"
EditKey

;**** Faturamento ****
If Not IsEmpty("Temp_Ven")
   Then Moveto "Temp_Ven"
        Localiza_NotaC()
        Scan

           If [Codigo da Empresa  ] <> EMPRESA Or
              [Serie do Documento ] <> SDOCTO  Or
              [Numero do Documento] <> NDOCTO
              Then Grava_NotaC()
                   Localiza_NotaC()
           EndIf

           If SearchFrom("VEND",[Descricao da Destinacao],1)>=1 or ([CFO]="5949" or [CFO]="599")
              Then
                   If ([Tipo do Item]="P" or [Tipo do Item]="S" or [Tipo do Item]="T") And
                      Atua_Estoque="S"
                      Then CMV=CMV+([Quantidade]*[Custo Medio Unitario])
                   EndIf
           EndIf
        EndScan
        Grava_NotaC()
EndIf

;**** Despesas Operacionais ****
If Not IsEmpty("Temp_Des")
   Then Moveto "Temp_Des"
        Scan

          If [Codigo da Empresa]<>[Answer->Codigo da Empresa]
             Then Moveto "Answer"
                  Moveto [Codigo da Empresa]
                  Locate [Temp_Des->Codigo da Empresa]
                  If Not RetVal
                     Then End
                          If Not IsBlank([Codigo da Empresa])
                             Then Down
                          EndIf
                          [Codigo da Empresa]=[temp_Des->Codigo da Empresa]
                  EndIf
                  Moveto "Temp_Des"
          EndIf

          If [Data]>=VSDGDTINI And [Data]<=DATA10
             Then [Answer->DESPO1]=[Answer->DESPO1]+[Valor]
          EndIf
          If [Data]>DATA10   And [Data]<=DATA20
             Then [Answer->DESPO2]=[Answer->DESPO2]+[Valor]
          EndIf
          If [Data]>DATA20   And [Data]<=VSDGDTFIM
             Then [Answer->DESPO3]=[Answer->DESPO3]+[Valor]
          EndIf
          If [Data]=VSDGDTREF
             Then [Answer->DESPO5]=[Answer->DESPO5]+[Valor]
          EndIf

        EndScan
EndIf

;**** ICMS/IPI RegEntra ********
If Not IsEmpty("\\Sistemas\\Estoque\\RegEntra")
   Then Moveto "\\Sistemas\\Estoque\\RegEntra"
        Scan
          If [Codigo da Empresa]<>[Answer->Codigo da Empresa]
             Then Moveto "Answer"
                  Moveto [Codigo da Empresa]
                  Locate [\Sistemas\Estoque\RegEntra->Codigo da Empresa]
                  If Not RetVal
                     Then End
                          If Not IsBlank([Codigo da Empresa])
                             Then Down
                          EndIf
                          [Codigo da Empresa]=[\Sistemas\Estoque\RegEntra->Codigo da Empresa]
                  EndIf
                  Moveto "\\Sistemas\\Estoque\\RegEntra"
          EndIf

          If [Data de Entrada]>=VSDGDTINI And [Data de Entrada]<=DATA10
             Then [Answer->ICMSE1]=[Answer->ICMSE1]+[ICMS Debitado]
                  [Answer->IPIE1 ]=[Answer->IPIE1 ]+[IPI Debitado]
          EndIf
          If [Data de Entrada]>DATA10   And [Data de Entrada]<=DATA20
             Then [Answer->ICMSE2]=[Answer->ICMSE2]+[ICMS Debitado]
                  [Answer->IPIE2 ]=[Answer->IPIE2 ]+[IPI Debitado]
          EndIf
          If [Data de Entrada]>DATA20   And [Data de Entrada]<=VSDGDTFIM
             Then [Answer->ICMSE3]=[Answer->ICMSE3]+[ICMS Debitado]
                  [Answer->IPIE3 ]=[Answer->IPIE3 ]+[IPI Debitado]
          EndIf
          If [Data de Entrada]=VSDGDTREF
             Then [Answer->ICMSE5]=[Answer->ICMSE5]+[ICMS Debitado]
                  [Answer->IPIE5 ]=[Answer->IPIE5 ]+[IPI Debitado]
          EndIf
        EndScan
EndIf

;**** ICMS/IPI RegSaida ********
If Not IsEmpty("\\Sistemas\\Vendas\\RegSaida")
   Then Moveto "\\Sistemas\\Vendas\\RegSaida"
        Scan

          If [Cancelada        ]="S" Or
             [Cancelada        ]="4"   
             Then Loop
          EndIf

          If [Codigo da Empresa]<>[Answer->Codigo da Empresa]
             Then Moveto "Answer"
                  Moveto [Codigo da Empresa]
                  Locate [\Sistemas\Vendas\RegSaida->Codigo da Empresa]
                  If Not RetVal
                     Then End
                          If Not IsBlank([Codigo da Empresa])
                             Then Down
                          EndIf
                          [Codigo da Empresa]=[\Sistemas\Vendas\RegSaida->Codigo da Empresa]
                  EndIf
                  Moveto "\\Sistemas\\Vendas\\RegSaida"
          EndIf

          If [Data de Emissao]>=VSDGDTINI And [Data de Emissao]<=DATA10
             Then [Answer->ICMSS1]=[Answer->ICMSS1]+[ICMS Debitado]
                  [Answer->IPIS1 ]=[Answer->IPIS1 ]+[IPI Debitado]
                  [Answer->SICMS1]=[Answer->SICMS1]+[Substituicao ICMS]
          EndIf
          If [Data de Emissao]>DATA10   And [Data de Emissao]<=DATA20
             Then [Answer->ICMSS2]=[Answer->ICMSS2]+[ICMS Debitado]
                  [Answer->IPIS2 ]=[Answer->IPIS2 ]+[IPI Debitado]
                  [Answer->SICMS2]=[Answer->SICMS2]+[Substituicao ICMS]
          EndIf
          If [Data de Emissao]>DATA20   And [Data de Emissao]<=VSDGDTFIM
             Then [Answer->ICMSS3]=[Answer->ICMSS3]+[ICMS Debitado]
                  [Answer->IPIS3 ]=[Answer->IPIS3 ]+[IPI Debitado]
                  [Answer->SICMS3]=[Answer->SICMS3]+[Substituicao ICMS]
          EndIf
          If [Data de Emissao]=VSDGDTREF
             Then [Answer->ICMSS5]=[Answer->ICMSS5]+[ICMS Debitado]
                  [Answer->IPIS5 ]=[Answer->IPIS5 ]+[IPI Debitado]
                  [Answer->SICMS5]=[Answer->SICMS5]+[Substituicao ICMS]
          EndIf
        EndScan
EndIf

;;;;;;;**** Saldo Inicial ICMS/IPI **************
DIA1=NumVal(Day(VSDGDTINI))
MES =StrVal(Month(VSDGDTINI))
MES =ALINHM(MES,2)
ANO =StrVal(Year(VSDGDTINI))

If Not IsEmpty("\\Sistemas\\Icms_Ipi\\ImpostoI")
   Then Moveto "\\Sistemas\\Icms_Ipi\\ImpostoI"
        Scan For [Ano Referencia   ]=ANO And
                 [Mes Referencia   ]=MES And
                 [Dia Referencia   ]=DIA1

          If Not IsBlank(EMPRESA) And [Codigo da Empresa]<>EMPRESA
             Then Loop
          EndIf

          If [Codigo da Empresa]<>[Answer->Codigo da Empresa]
             Then Moveto "Answer"
                  Moveto [Codigo da Empresa]
                  Locate [\Sistemas\Icms_Ipi\ImpostoI->Codigo da Empresa]
                  If Not RetVal
                     Then End
                          If Not IsBlank([Codigo da Empresa])
                             Then Down
                          EndIf
                          [Codigo da Empresa]=[\Sistemas\Icms_Ipi\ImpostoI->Codigo da Empresa]
                  EndIf
                  Moveto "\\Sistemas\\Icms_Ipi\\ImpostoI"
          EndIf

          If [Codigo do Imposto]="ICMS" Then [Answer->ICMSC1]=[Saldo Inicial] EndIf
          If [Codigo do Imposto]="IPI"  Then [Answer->IPIC1 ]=[Saldo Inicial] EndIf
        EndScan
EndIf

;;;;******* Calculos **********
Moveto "Answer"
Scan
  Moveto [Codigo da Empresa]

  [VENDV4  ]=[VENDV1]+[VENDV2]+[VENDV3]
  [VENDP4  ]=[VENDP1]+[VENDP2]+[VENDP3]

  [TOTVEND1]=[VENDV1]+[VENDP1]
  [TOTVEND2]=[VENDV2]+[VENDP2]
  [TOTVEND3]=[VENDV3]+[VENDP3]
  [TOTVEND4]=[VENDV4]+[VENDP4]
  [TOTVEND5]=[VENDV5]+[VENDP5]

  [CMV4    ]=[CMV1  ]+[CMV2  ]+[CMV3  ]
  [DESPO4  ]=[DESPO1]+[DESPO2]+[DESPO3]
  [ICMSE4  ]=[ICMSE1]+[ICMSE2]+[ICMSE3]
  [ICMSS4  ]=[ICMSS1]+[ICMSS2]+[ICMSS3]
  [SICMS4  ]=[SICMS1]+[SICMS2]+[SICMS3]
  [IPIE4   ]=[IPIE1 ]+[IPIE2 ]+[IPIE3 ]
  [IPIS4   ]=[IPIS1 ]+[IPIS2 ]+[IPIS3 ]

  [ICMSC2]=([ICMSC1]+[ICMSE1])-[ICMSS1]
  If [ICMSC2]<=0 Then [ICMSC2]=BlankNum() EndIf

  [ICMSC3]=([ICMSC2]+[ICMSE2])-[ICMSS2]
  If [ICMSC3]<=0 Then [ICMSC3]=BlankNum() EndIf

  [ICMSC4]=[ICMSC1]+([ICMSE1]+[ICMSE2]+[ICMSE3])
  [ICMSC4]=[ICMSC4]-([ICMSS1]+[ICMSS2]+[ICMSS3])
  If [ICMSC4]<=0 Then [ICMSC4]=BlankNum() EndIf

  [IPIC2]=([IPIC1]+[IPIE1])-[IPIS1]
  If [IPIC2]<=0 Then [IPIC2]=BlankNum() EndIf

  [IPIC3]=([IPIC2]+[IPIE2])-[IPIS2]
  If [IPIC3]<=0 Then [IPIC3]=BlankNum() EndIf

  [IPIC4]=[IPIC1]+([IPIE1]+[IPIE2]+[IPIE3])
  [IPIC4]=[IPIC4]-([IPIS1]+[IPIS2]+[IPIS3])
  If [IPIC4]<=0 Then [IPIC4]=BlankNum() EndIf

  [ICMSC5]=[ICMSE5]-[ICMSS5]
  If [ICMSC5]<=0 Then [ICMSC5]=BlankNum() EndIf

  [IPIC5]=[IPIE5]-[IPIS5]
  If [IPIC5]<=0 Then [IPIC5]=BlankNum() EndIf

  [ICMSAPG1]=([ICMSC1]+[ICMSE1])-[ICMSS1]
  If [ICMSAPG1]>=0 Then [ICMSAPG1]=BlankNum() EndIf
  If [ICMSAPG1]<0 Then [ICMSAPG1]=[ICMSAPG1]*-1 EndIf
  [IPIAPG1]=([IPIC1]+[IPIE1])-[IPIS1]
  If [IPIAPG1]>=0 Then [IPIAPG1]=BlankNum() EndIf
  If [IPIAPG1]<0 Then [IPIAPG1]=[IPIAPG1]*-1 EndIf

  [ICMSAPG2]=([ICMSC2]+[ICMSE2])-[ICMSS2]
  If [ICMSAPG2]>=0 Then [ICMSAPG2]=BlankNum() EndIf
  If [ICMSAPG2]<0 Then [ICMSAPG2]=[ICMSAPG2]*-1 EndIf
  [IPIAPG2]=([IPIC2]+[IPIE2])-[IPIS2]
  If [IPIAPG2]>=0 Then [IPIAPG2]=BlankNum() EndIf
  If [IPIAPG2]<0 Then [IPIAPG2]=[IPIAPG2]*-1 EndIf

  [ICMSAPG3]=([ICMSC3]+[ICMSE3])-[ICMSS3]
  If [ICMSAPG3]>=0 Then [ICMSAPG3]=BlankNum() EndIf
  If [ICMSAPG3]<0 Then [ICMSAPG3]=[ICMSAPG3]*-1 EndIf
  [IPIAPG3]=([IPIC3]+[IPIE3])-[IPIS3]
  If [IPIAPG3]>=0 Then [IPIAPG3]=BlankNum() EndIf
  If [IPIAPG3]<0 Then [IPIAPG3]=[IPIAPG3]*-1 EndIf

  [LMV1   ]=[TOTVEND1]-[CMV1  ]
  [LP1    ]=[LMV1    ]-[DESPO1]
  [PIS1   ]=Round(([TOTVEND1]*0.65)/100, 2)
  [COFINS1]=Round(([TOTVEND1]*3)/100, 2)
  [IMPAPG1]=[ICMSAPG1]+[IPIAPG1]+[SICMS1]+[PIS1]+[COFINS1]

  [LMV2   ]=[TOTVEND2]-[CMV2  ]
  [LP2    ]=[LMV2    ]-[DESPO2]
  [PIS2   ]=Round(([TOTVEND2]*0.65)/100, 2)
  [COFINS2]=Round(([TOTVEND2]*3)/100, 2)
  [IMPAPG2]=[ICMSAPG2]+[IPIAPG2]+[SICMS2]+[PIS2]+[COFINS2]

  [LMV3   ]=[TOTVEND3]-[CMV3  ]
  [LP3    ]=[LMV3    ]-[DESPO3]
  [PIS3   ]=Round(([TOTVEND3]*0.65)/100, 2)
  [COFINS3]=Round(([TOTVEND3]*3)/100, 2)
  [IMPAPG3]=[ICMSAPG3]+[IPIAPG3]+[SICMS3]+[PIS3]+[COFINS3]

  [LMV4   ]=[TOTVEND4]-[CMV4  ]
  [LP4    ]=[LMV4    ]-[DESPO4]
  [PIS4   ]=Round(([TOTVEND4]*0.65)/100, 2)
  [COFINS4]=Round(([TOTVEND4]*3)/100, 2)
  [IMPAPG4]=[ICMSAPG4]+[IPIAPG4]+[SICMS4]+[PIS4]+[COFINS4]

  [LMV5   ]=[TOTVEND5]-[CMV5  ]
  [LP5    ]=[LMV5    ]-[DESPO5]
  [PIS5   ]=Round(([TOTVEND5]*0.65)/100, 2)
  [COFINS5]=Round(([TOTVEND5]*3)/100, 2)
  [IMPAPG5]=[ICMSAPG5]+[IPIAPG5]+[SICMS5]+[PIS5]+[COFINS5]

  [ICMSAPG4]=[ICMSAPG1]+[ICMSAPG2]+[ICMSAPG3]
  [IPIAPG4 ]=[IPIAPG1 ]+[IPIAPG2 ]+[IPIAPG3 ]

  [ICMSAPG5]=[ICMSE5]-[ICMSS5]
  If [ICMSAPG5]>=0 Then [ICMSAPG5]=BlankNum() EndIf
  If [ICMSAPG5]<0  Then [ICMSAPG5]=[ICMSAPG5]*-1 EndIf

  [IPIAPG5]=[IPIE5]-[IPIS5]
  If [IPIAPG5]>=0 Then [IPIAPG5]=BlankNum() EndIf
  If [IPIAPG5]<0  Then [IPIAPG5]=[IPIAPG5]*-1 EndIf

EndScan
Do_It!
ClearAll

CopyReport "RM_0016" "R" "Answer" "R"

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_DTREF"         : 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": Campo="TG_DTREF"
                    Case Campo="TG_DTREF"  :
                      If VSDGDTREF=BlankDate()
                         Then Campo="TG_DTREF"
                         Else Campo="YES"
                      EndIf
                  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=""
VSDGDTREF  =Today()-1
MESANO     =BlankDate()
While True

  ShowDialog "Demonstra‡„o do Resultado"  Proc "SD_Dialog"
  Trigger "OPEN","ARRIVE"
  Key "F1","ENTER","ESC",
      -104, -105, -106, -107, -108, -109, -110, -111, -112, -113, -139, -140  
   @ 26,26   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 "Emitidos at‚" For "TG_DTREF"
    Label  @ 03,00 "de" For "TG_DTREF"
    Accept @ 03,04 Width 13 "D"  Picture "#[#];.##;.##[##]" Tag "TG_DTREF" To VSDGDTREF

    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 VSDGDTREF=BlankDate()
     Then Mensagem("Data referˆncia inv lida !")
          Loop
  EndIf

  MES=AlinhM(StrVal(Month(VSDGDTREF)),2)
  ANO=StrVal(Year(VSDGDTREF))
  If NumVal(ANO) < 2000
     Then MESANO=MES+"."+SubStr(ANO,3,2)
     Else MESANO=MES+"."+ANO
  EndIf

  VSDGDTINI=DateVal("1"+"."+StrVal(NumVal(MES))+"."+StrVal(NumVal(ANO)))
  VSDGDTFIM=VSDGDTREF

  Dispositivo()
  If OK=""  Then Loop EndIf

  Aguarde("Aguarde, Gerando o Relat¢rio")
  Gera_Movim()

  Imprirel("Answer","R")
  Delete "Answer"
  If IsTable("\\Sistemas\\Vendas\\Temp_Des") Then Delete("\\Sistemas\\Vendas\\Temp_Des") EndIf
  If IsTable("\\Sistemas\\Vendas\\Temp_Ven") Then Delete("\\Sistemas\\Vendas\\Temp_Ven") EndIf

  Loop
EndWhile
ClearAll
Release Vars All
Release Procs All

