;** Rm_0011  *******************************************************************
;*  Emiss„o Diario Auxiliar de Caixa/Banco                                     *
;*******************************************************************************
;Autor           : Vise Informatica (VISERP)
;Desenvolvido    : 10/12/2001 por Anivel Augusto Rodrigues
;Ultima Alteracao: 18/07/2005 por Davi Pereira Carrano
;Motivo Alteracao: Corre‡Æo nome de variavel
;Ultima Alteracao: 03/09/2014 por Andrey Fillipe
;Motivo Alteracao: Adicionado filtro Op‡ao de data
;Ultima Alteracao: 06/09/2016 por Anivel Augusto Rodrigues
;Motivo Alteracao: Ajustes showdialog                   
;Ultima Alteracao: 26/12/2016 por Anivel Augusto Rodrigues
;Motivo Alteracao: Ajustes showdialog                   
;Ultima Alteracao: 27/03/2018 por Anivel Augusto Rodrigues
;Motivo Alteracao: Inclusao campos valor do desconto, valor do juros         

;*******************************************************************************
;* Rotina p/ Selecionar e Ordernar Registros                                   *
;*******************************************************************************
Proc Seleciona()
ClearAll
If IsTable("\\Sistemas\\Ctacor\\"+Dir_Trab+"MovCont")
   Then {Ask} TypeIn "\\Sistemas\\Ctacor\\"+Dir_Trab+"MovCont" Enter Check
        Moveto [Codigo da Conta        ] TypeIn "~VSDGCONTA"
        If VSDGOPDTEMISCOMP = "1"
          Then Moveto [Data Competencia] Check
               Moveto [Data            ] TypeIn ">=~VSDGDTINI, <=~VSDGDTFIM"
          Else Moveto [Data            ] Check
               Moveto [Data Competencia] TypeIn ">=~VSDGDTINI, <=~VSDGDTFIM, as Data"
        EndIf
        Do_It!
        ClearAll

        {Ask} {Answer}
        Moveto [Numero do Lancamento  ] Example "LANCTOE" Down
                                        Example "LANCTOS" Up
        Moveto [Codigo da Conta       ] Example "CONTAE" Down
                                        Example "CONTAS"
        Moveto [Numero do Cheque      ] Example "CHEQUE" Up
        Moveto [Tipo Movimento        ] TypeIn "E" Down
                                        TypeIn "S" Up
        Moveto [Data                  ] Example "DATAE" Down
                                        Example "DATAS" Up
        Moveto [Valor                 ] Example "VALORE" Down
                                        Example "VALORS" Up
        Moveto [Descricao do Historico] Example "HISTORICOE" Down
                                        Example "HISTORICOS" Up
        Moveto [Nome Fantasia EmitDest] Example "EMITDESTE" Down
                                        Example "EMITDESTS" Up
        moveto [Valor do Desconto     ] example "_vrdesctoe" Down
                                        example "_vrdesctos" Up         
        moveto [Valor do Juros        ] example "_vrjurose" Down
                                        example "_vrjuross" Up

        {Ask} TypeIn Tabela Enter TypeIn "Fast Insert" Down
                                  TypeIn "Fast Insert" Up
        Moveto [Numero do Lancamento  ] Example "LANCTOE" Down
                                        Example "LANCTOS" Up
        Moveto [Codigo da Conta       ] Example "CONTAE" Down
                                        Example "CONTAS"
        Moveto [Numero do Cheque      ] Example "CHEQUE" Up
        Moveto [Data Movimento        ] Example "DATAE" Down
                                        Example "DATAS" Up
        Moveto [Valor                 ] Example "VALORE" Down
                                        Example "VALORS" TypeIn "*-1" Up
        Moveto [Historico             ] Example "HISTORICOE" Down
                                        Example "HISTORICOS" Up
        Moveto [Nome Fantasia EmitDest] Example "EMITDESTE" Down
                                        Example "EMITDESTS" Up
        moveto [Valor do Desconto     ] example "_vrdesctoe" Down
                                        example "_vrdesctos" Up         
        moveto [Valor do Juros        ] example "_vrjurose" Down
                                        example "_vrjuross" Up
        Do_It!
        ClearAll
EndIf

EndProc   ; (Seleciona)

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

Coedit Tabela
Scan

  While [Data Movimento]<VSDGDTINI and not IsBlank([Data Movimento]) And [Situacao]<>"A"
     SALDOINI=SALDOINI+[Valor]
     Del
  EndWhile

  If IsBlank([Data Movimento])
     Then QuitLoop
  EndIf

EndScan

If Not IsBlank([Data Movimento])
   Then Home
        Ins
        [Codigo da Conta       ]=VSDGCONTA
        [Data Movimento        ]=VSDGDTINI-1
        [Nome Fantasia EmitDest]="SALDO ANTERIOR"
        [Valor                 ]=SALDOINI
EndIf
Do_It!
ClearAll

EndProc    ; (Processa)

;*******************************************************************************
;* 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_CONTA"         : Campo="NO"
            Case Campo="TG_OPDTEMISCOMP"  : Campo="TG_CONTA"
            Case Campo="TG_DTINI"         : Campo="TG_OPDTEMISCOMP"
            Case Campo="TG_DTFIM"         : Campo="TG_DTINI"
            Case Campo="TG_PGINI"         : Campo="TG_DTFIM"
            Case Campo="YES" Or Campo="NO": Campo="TG_CONTA"
          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_CONTA" :
                         If IsBlank(VSDGCONTA)
                            Then VSDGNOMCONTA=""
                            Else Campo="TG_OPDTEMISCOMP"
                         Endif
                    Case Campo="TG_OPDTEMISCOMP" : Campo="TG_DTINI"
                    Case Campo="TG_DTINI" :
                         If Not IsBlank(VSDGDTINI)
                            Then Campo="TG_DTFIM"
                         Endif
                    Case Campo="TG_DTFIM" : Campo="TG_PGINI"
                    Case Campo="TG_PGINI":
                         VSDGPGINI=ControlValue("TG_PGINI")
                         Campo="YES"
                  EndSwitch
          EndIf
          RefreshDialog
   EndSwitch
EndIf
Switch
  Case Campo="TG_CONTA":
     MSG_CAMPO="Somente uma Conta disponivel"
  Case Campo="TG_DTINI":
     MSG_CAMPO="Informe a Data Inicial"
  Case Campo="TG_PGINI":
     MSG_CAMPO="Informe a Pagina Inicial"
Endswitch
Monta_Prompt(MSG_CAMPO,TECLA)
Return Temp
EndProc

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

VSDGTPCONTA   = ""
VSDGCONTA     = ""
VSDGDTINI     = BlankDate()
VSDGDTFIM     = BlankDate()
VSDGPGINI     = BlankNum()
VSDGOPDTEMISCOMP   =""
VSDGNOMOPDTEMISCOMP=""

While True
  ShowDialog "Emiss„o Diario Auxiliar de Caixa/Banco" Proc "SD_Dialog"
  Trigger "OPEN","ARRIVE"
  Key "F1","ENTER","ESC","TAB",
      -104, -105, -106, -107, -108, -109, -110, -111, -112, -113, -139, -140  
   @ 05,12   Height 13 Width 72

    Label  @ 00,00 "Conta"  For "TG_CONTA"
    Accept @ 01,01 Width 07 "A4"  Picture "#*#" Tag "TG_CONTA"    To VSDGCONTA
    Accept @ 01,09 Width 25 "A20" Picture "!*!" Tag "TG_NOMCONTA" To VSDGNOMCONTA

    Label  @ 02,00 "Op‡äes de Datas" For "TG_OPDTEMISCOMP"
    Accept @ 03,04 Width 04 "A1"  Picture "1,2" Tag "TG_OPDTEMISCOMP" To VSDGOPDTEMISCOMP
    Accept @ 03,09 Width 25 "A20"  Picture "!*!" Tag "TG_NOMOPDTEMISCOMP" To VSDGNOMOPDTEMISCOMP

    Label  @ 04,00 "Per¡odo Referente de" For "TG_DTINI"
    Accept @ 05,03 Width 13 "D"  Picture "#[#];.##;.##[##]" Tag "TG_DTINI" To VSDGDTINI
    Label  @ 05,16 "at‚" For "TG_DTFIM"
    Accept @ 05,21 Width 13 "D"  Picture "#[#];.##;.##[##]" Tag "TG_DTFIM" To VSDGDTFIM

    Label  @ 06,00 "Pagina Inicial"  For "TG_PGINI"
    Accept @ 07,01 Width 07 "S"  Picture "#[####]" Tag "TG_PGINI" To VSDGPGINI

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

  EndDialog
  If Not RetVal Then QuitLoop EndIf

  If VSDGCONTA=""
     Then Mensagem("Conta Invalida !")
          Loop
  EndIf

  If VSDGDTINI=BlankDate()
     Then Mensagem("Data Inicial Invalida !")
          Loop
  EndIf

  ANOREF=StrVal(Year(VSDGDTINI))
  MESREF=StrVal(Month(VSDGDTINI))
  ALINHM(MESREF, 2)
  MESREF=NVR

  View "SaldCont"
  Moveto [Codigo da Conta]
  Locate VSDGCONTA,ANOREF,MESREF
  If Not RetVal
     Then Mensagem("Conta sem Saldo Inicial Atualizado")
          ClearAll
          Loop
  EndIf

  SALDOINI=[Saldo Inicial]
  DATREF=DateVal("01."+MESREF+"."+ANOREF)

  DISPOSITIVO()
  If OK="" Then Loop EndIf

  AGUARDE("Aguarde, preparando o extrato")
  Tabela=Cria_Temp(PrivDir())
  Copy "\\Base\\Sistemas\\CtaCor\\Rm_0010" Tabela
  Empty Tabela

  Monta_Dir_Trab(VSDGDTINI,VSDGDTFIM)
  While Novo_Dir_Trab()
    Seleciona()
  EndWhile

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

  {Report} {Change} TypeIn Tabela Enter {R} {Extrato de Contas}
  Home Ins Enter Ins Enter Ins Enter Ins Enter Enter Ins Home
  "** Pagina de Abertura **" Enter
  "** Pagina Inicial     =>" TypeIn VSDGPGINI Enter
  "** Iniciando na proxima pagina **" Enter
  "PAGEBREAK " TypeIn VSDGPGINI Do_It!

  IMPRIREL(Tabela,"1")

  Delete Tabela

EndWhile
ClearAll
Release Vars All
Release Procs All
