;** GR_0001 ********************************************************************
;*  Grafico de movimentos mensais por conta                                                     *
;*******************************************************************************
;Autor           : Vise Informatica (VISERP)
;Desenvolvido    : 26/12/1996 por Anivel Augusto Rodrigues
;Ultima Alteracao: 14/08/2006 por Davi Carrano
;Motivo Alteracao: Criar Meses em branco para o grafico ficar completo
;Ultima Alteracao: 17/08/2006 por Davi Carrano
;Motivo Alteracao: Pegar tabela do base
;Ultima Alteracao: 05/10/2012 por Kamila Gomes 
;Motivo Alteracao: Ajustes Processa() e titulos do grafico  
;Ultima Alteracao: 06/09/2016 por Anivel Augusto Rodrigues
;Motivo Alteracao: Ajustes showdialog                   
;Ultima Alteracao: 22/12/2016 por Anivel Augusto Rodrigues
;Motivo Alteracao: Ajustes showdialog                   

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

ClearAll

If IsTable("\\Sistemas\\Ctacor\\"+Dir_Trab+"SaldCont");BUSCA MES FECHADO
   Then {Ask} TypeIn "\\Sistemas\\Ctacor\\"+Dir_Trab+"SaldCont" Enter 
        Moveto [Codigo da Conta      ] TypeIn "~VSDGCONTA.."
        Moveto [Ano Referencia       ] Check TypeIn ">=~ANOINI, <=~ANO"
        Moveto [Mes Referencia       ] Check
        Moveto [Entradas             ] Check
        Moveto [Saidas               ] Check
        Do_It!
        ClearAll
EndIf
EndProc

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

{Ask} TypeIn Tabela Enter
Moveto [Entradas] TypeIn "Blank, changeto 0"
Do_It!
ClearAll
  
{Ask} TypeIn Tabela Enter
Moveto [Saidas] TypeIn "Blank, changeto 0"
Do_It!
ClearAll

{Ask} TypeIn Tabela Enter Check
Do_It!
ClearAll

Mes_Aux=NumVal(MESINI)
Ano_Aux=NumVal(ANOINI)
CoEdit PrivDir()+"GR_0001a"
For wi From 1 to 13
    End Down
    [Ano        ]=Ano_Aux
    [Mes        ]=Mes_Aux
    [Valor 1    ]=0
    [Valor 2    ]=0
    If Mes_Aux = 12 
       Then Mes_Aux=0
            Ano_Aux=Ano_Aux+1
    EndIf
    Mes_Aux=Mes_Aux+1
EndFor
Do_It!
ClearAll

View PrivDir()+"GR_0001a"
CoEdit "Answer"
Scan
   If IsBlank([Ano Referencia]) Then Loop EndIf
   If ([Ano Referencia]=ANOINI And [Mes Referencia]>=MESINI) Or
      ([Ano Referencia]=ANO    And [Mes Referencia]<=MES)
      Then
           Moveto PrivDir()+"GR_0001a"
           Moveto [Ano]
           Locate NumVal([Answer->Ano Referencia]),NumVal([Answer->Mes Referencia])
           If RetVal
              Then 
                  [Valor 1 ]=[Answer->Entradas] 
                  [Valor 2 ]=[Answer->Saidas] 
              Else End Down
                   [Ano   ]=Year ([Answer->Data de Emissao])
                   [Mes   ]=Month([Answer->Data de Emissao]) 
                   [Valor 1 ]=0
                   [Valor 2 ]=0    
           EndIf
   EndIf
   Moveto "Answer"
EndScan
Do_It!
ClearAll

VRTOTAL=0
View PrivDir()+"GR_0001"
CoEdit PrivDir()+"GR_0001a"
Scan
   CopytoArray Reg
   
   Moveto PrivDir()+"GR_0001"
   End Down
   [Mes        ]=StrVal(Reg["Ano"])+"/"+Substr(mesext(NumVal(Reg["Mes"])),1,3)
   [Valor 1    ]=Reg["Valor 1"]
   [Valor 2    ]=Reg["Valor 2"]
   
   VRTOTAL=VRTOTAL+Reg["Valor 1"]-Reg["Valor 2"]
   
   Moveto PrivDir()+"GR_0001a"
EndScan

Do_it!
ClearAll

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_CONTA"         : Campo="NO"
            Case Campo="TG_MESANO"        : Campo="TG_CONTA"
            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_MESANO"
                         Endif
                    Case Campo="TG_MESANO" : Campo="YES"
                  EndSwitch
          EndIf
          RefreshDialog
   EndSwitch
EndIf
Switch
  Case Campo="TG_CONTA":
     MSG_CAMPO="Informe uma Conta"
Endswitch
Monta_Prompt(MSG_CAMPO,TECLA)
Return Temp
EndProc

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

VSDGCONTA  =""
VSDGMESANO =AlinhM(StrVal(Month(Today())),2)+"."+AlinhM(StrVal(Year(Today())),4)

While True
  ShowDialog "Movimentos mensais por conta dos £ltimos 12 meses" 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 09 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 "At‚ Mˆs/Ano" For "TG_MESANO"
    Accept @ 03,01 Width 13 "A7"  Picture "##;.####" Tag "TG_MESANO" To VSDGMESANO

    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

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

  MES=Substr(VSDGMESANO,1,2)
  ANO=Substr(VSDGMESANO,4,4)
  If NumVal(MES) > 12 Or NumVal(MES) <= 0
     Then Loop
  EndIf
  
  MESINI=AlinhM(StrVal((NumVal(MES))),2)
  ANOINI=StrVal((NumVal(ANO)-1))
  VSDGDTINI=DateVal(MESINI+"/"+"01"+"/"+ANOINI)

  MESFIM=AlinhM(StrVal((NumVal(MES)+1)),2)
  If NumVal(MESFIM)=13
     Then MESFIM="01"
          ANOFIM=StrVal((NumVal(ANO)+1))
     Else ANOFIM=ANO
  EndIf
  VSDGDTFIM=DateVal(MESFIM+"/"+"01"+"/"+ANOFIM)-1

  DISPOSITIVOG()
  If OK="" Then Loop EndIf
  
  Copy "\\Base\\Sistemas\\Ctacor\\GR_0001" PrivDir()+"GR_0001"
  Copy "\\Base\\Sistemas\\Ctacor\\GR_0001a" PrivDir()+"GR_0001a"
  Empty(PrivDir()+"GR_0001")
  Empty(PrivDir()+"GR_0001a")

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

  GRAF_TAB = PrivDir()+"GR_0001"
  GRAF_TIPO= "3"
  GRAF_TIT1= "Movimentos Mensais dos éltimos 13 Meses at‚ "+rTrim(mesext(NumVal(Substr(VSDGMESANO,1,2))))+"/"+Substr(VSDGMESANO,4,4)
  GRAF_TIT2= "Conta: "+VSDGCONTA+" - "+VSDGNOMCONTA+" [ Total: "+LTrim(Format("W13.2,EIC",VRTOTAL))+" ]"
  GRAF_LINX= "Valor"
  GRAF_LINY= "Meses"
  GRAF_LENG= "Y"
  GRAF_LENG1= "Entradas"
  GRAF_LENG2= "Saidas"
  GRAF_CAMPO= 2

  MONTAGRAFICO(GRAF_TAB)

  Delete PrivDir()+"GR_0001"
  Delete PrivDir()+"GR_0001a"

EndWhile
ClearAll
Release Vars All
Release Procs All

