;** Rm_0283  *******************************************************************
;*  Emissao dos Titulos a Recebidos por Representante/Pagto/Plcontas/CtCusto   *
;*******************************************************************************
;Autor           : Vise Informatica (VISERP)
;Desenvolvido    : 08/11/2012 por Anivel Augusto Rodrigues
;Ultima Alteracao:   /  /     por 

;*******************************************************************************
;* Rotina p/ Selecionar e Ordernar Registros                                   *
;*******************************************************************************
Proc Seleciona()
ClearAll
If IsTable("\\Sistemas\\CtaRec\\"+Dir_Trab+"Duplic") And
   IsTable("\\Sistemas\\CtaRec\\"+Dir_Trab+"DupBaixa")

   Then {Ask} TypeIn "\\Sistemas\\Ctarec\\"+Dir_Trab+"DupliC" Enter Check
        Moveto [Numero do Lancamento   ] Example "lanc"
        Moveto [Codigo da Empresa      ] TypeIn "~VSDGEMPRESA2.."
        Moveto [Codigo do Representante] TypeIn "~VSDGREPRESEN.."

        {Ask} TypeIn "\\Sistemas\\Ctarec\\"+Dir_Trab+"DupBaixa" Enter Check
        Moveto [Numero do Lancamento] Example "lanc"
        Moveto [Tipo da Baixa       ] TypeIn "Blank or NORMAL"
        Moveto [Data do Pagamento   ] TypeIn ">=~VSDGDTINI,<=~VSDGDTFIM"
        Moveto [Codigo Plano_Conta  ] TypeIn PLCONTAS
        Moveto [Codigo CCusto       ] TypeIn "~VSDGCCUSTO.."
        Do_It!
        ClearAll
EndIf
EndProc   ; (Seleciona)

;*******************************************************************************
;* Rotina p/ processar Db temporaria                                           *
;*******************************************************************************
Proc Processa()

;***** Ajusta Dados ************************************
If VSDGEMPRESA="0000"
   Then {Ask} TypeIn Tabela Enter TypeIn "Fast"
        Moveto [Codigo da Empresa      ] TypeIn "changeto 0000"
        Moveto [Nome Fantasia Empresa  ] TypeIn "changeto TOTALIZA EMPRESAS"
        Do_It!
        ClearAll
EndIf

Coedit Tabela
Scan
  [Valor do Desconto-1]=([Valor do Desconto-1]*-1)
  If IsBlank([Valor do Desconto-1]) Then [Valor do Desconto-1]=0 EndIf
EndScan
Do_It!
ClearAll
EndProc  ; (Processa)

;*******************************************************************************
;* 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_REPRESEN"      : Campo="TG_EMPRESA"
            Case Campo="TG_PLCONTAS"      : Campo="TG_REPRESEN"
            Case Campo="TG_CCUSTO"        : Campo="TG_PLCONTAS"
            Case Campo="TG_DTINI"         : Campo="TG_CCUSTO"
            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" : Campo="TG_REPRESEN"
                    Case Campo="TG_REPRESEN": Campo="TG_PLCONTAS"
                    Case Campo="TG_PLCONTAS": Campo="TG_CCUSTO"
                    Case Campo="TG_CCUSTO"  : Campo="TG_DTINI"
                    Case Campo="TG_DTINI"   : Campo="TG_DTFIM"
                    Case Campo="TG_DTFIM"   : 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 =""
VSDGREPRESEN=""
VSDGCONTA   =""
VSDGDTINI   =BlankDate()
VSDGDTFIM   =BlankDate()

While True

  ShowDialog "T¡tulos Recebidos por Repres/PlContas/CtCusto/DtPgto" 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 15 Width 65
   
    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 "Representante"  For "TG_REPRESEN"
    Accept @ 03,01 Width 07 "A4"  Picture "[&,#][#*#]" Tag "TG_REPRESEN" To VSDGREPRESEN
    Accept @ 03,09 Width 33 "A30" Picture "!*!" Tag "TG_NOMREPR" To VSDGNOMREPR

    Label  @ 04,00 "Plano de Contas" For "TG_PLCONTAS"
    Accept @ 5,01 Width 16 "A13"  Picture "!*!" Tag "TG_PLCONTAS" To VSDGPLCONTAS
    Accept @ 5,18 Width 43 "A40" Picture "!*!" Tag "TG_NOMPLCONTAS"  To VSDGNOMPLCONTAS

    Label  @ 06,00 "Centro de Custo" For "TG_CCUSTO"
    Accept @ 7,01 Width 10 "A7"  Picture "###.###" Tag "TG_CCUSTO" To VSDGCCUSTO
    Accept @ 7,12 Width 23 "A20" Picture "!*!" Tag "TG_NOMCUST"  To VSDGNOMCUST

    Label  @ 08,00 "Per¡odo Referente" For "TG_DTINI"
    Label  @ 09,00 "de" For "TG_DTINI"
    Accept @ 09,04 Width 13 "D"  Picture "#[#];.##;.##[##]" Tag "TG_DTINI" To VSDGDTINI
    Label  @ 09,17 "at‚" For "TG_DTFIM"
    Accept @ 09,22 Width 13 "D"  Picture "#[#];.##;.##[##]" Tag "TG_DTFIM" To VSDGDTFIM

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

  EndDialog
  If Not RetVal Then QuitLoop EndIf
  If Not Usuario_Vendedor() Then Loop EndIf  

  If IsBlank(VSDGDTFIM)
     Then Mensagem("Data Final Invalida !")
          Loop
  EndIf

  If VSDGEMPRESA="0000"
     Then VSDGEMPRESA2=""
     Else VSDGEMPRESA2=VSDGEMPRESA
  EndIf

  If Len(VSDGPLCONTAS)<13
     Then PLCONTAS=VSDGPLCONTAS + ".."
     Else PLCONTAS=VSDGPLCONTAS
  EndIf

  DISPOSITIVO()
  If OK="" Then Loop EndIf

  AGUARDE("Aguarde, Processando Dados")
  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

  If IsEmpty(Tabela)
     Then MENSAGEM("N„o existem registros a processar")
          Delete Tabela
          Loop
  EndIf
  Processa()

  CopyReport "\\Base\\Sistemas\\CtaRec\\Rm_1010a" "3" Tabela "R"

  IMPRIREL(Tabela,"R")

  Delete Tabela

EndWhile
ClearAll
Release Vars All
Release Procs All
