;** GR_0003 ********************************************************************
;*  Grafico de movimentos Mensais por Plano de Conta                           *
;*******************************************************************************
;Autor           : Vise Informatica (VISERP)
;Desenvolvido    : 02/02/2005 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: 19/01/2009 por Anivel Augusto Rodrigues
;Motivo Alteracao: TOTALIZA EMPRESAS
;Ultima Alteracao: 04/07/2011 por Nikolas
;Motivo Alteracao: Ajustes
;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: 17/12/2016 por Anivel Augusto Rodrigues
;Motivo Alteracao: Ajustes TG_EMPRESATE                 
;Ultima Alteracao: 22/12/2016 por Anivel Augusto Rodrigues
;Motivo Alteracao: Ajustes showdialog                   
;Ultima Alteracao: 16/01/2021 por Anivel Augusto Rodrigues
;Motivo Alteracao: Ajustes showdialog                   

;*******************************************************************************
;* Rotina p/ Selecionar e Order Registros                                      *
;*******************************************************************************
Proc Seleciona(i)
ClearAll

If IsTable("\\Sistemas\\Ctacor\\"+Dir_Trab+"MovCont")
   Then {Ask} TypeIn "\\Sistemas\\Ctacor\\"+Dir_Trab+"MovCont" Enter
        Moveto [Codigo da Empresa     ] TypeIn "~VSDGEMPRESA2.."
        If SubStr(PLCONTAS,1,1)="2" or SubStr(PLCONTAS,1,1)="3"
           Then Switch
                  Case i=1:
                    Moveto [Data                  ] Check TypeIn ">=~VSDGDTINI, <=~VSDGDTFIM"
                    Moveto [Cheque Pre-Datado     ] TypeIn "Not S"
                  Case i=2:
                    Moveto [Data Cheque Pre-Datado] Check TypeIn ">=~VSDGDTINI, <=~VSDGDTFIM, as Data"
                    Moveto [Cheque Pre-Datado     ] TypeIn "S"
                EndSwitch
                Moveto [Tipo Movimento        ] TypeIn "S"
           Else Moveto [Data                  ] Check TypeIn ">=~VSDGDTINI, <=~VSDGDTFIM"
                Moveto [Tipo Movimento        ] TypeIn "E"
        EndIf
        Moveto [Codigo Plano_Conta    ] TypeIn PLCONTAS
        Moveto [Valor                 ] TypeIn "Calc Sum as Valor Total"
        Do_It!
        ClearAll
EndIf
EndProc     ;  (Seleciona)

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

{Ask} TypeIn Tabela Enter Check
Do_It!
ClearAll

Mes_Aux=NumVal(MESINI)
Ano_Aux=NumVal(ANOINI)
CoEdit PrivDir()+"GR_0002a"
For wi From 1 to 13
    End Down
    [Ano        ]=Ano_Aux
    [Mes        ]=Mes_Aux
    [Valor 1]=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_0002a"
CoEdit "Answer"
Scan
   If IsBlank([Data]) Then Loop EndIf
   Moveto PrivDir()+"GR_0002a"
   Moveto [Ano]
   Locate Year([Answer->Data]),Month([Answer->Data])
   If Not RetVal
      Then End Down
           [Ano   ]=Year ([Answer->Data])
           [Mes   ]=Month([Answer->Data])
   EndIf
   [Valor 1]=[Valor 1]+[Answer->Valor Total]
   Moveto "Answer"
EndScan
Do_It!
ClearAll

VRTOTAL=0
View PrivDir()+"GR_0002"
CoEdit PrivDir()+"GR_0002a"
Scan
   CopytoArray Reg
   
   Moveto PrivDir()+"GR_0002"
   End Down
   [Mes        ]=StrVal(Reg["Ano"])+"/"+Substr(mesext(NumVal(Reg["Mes"])),1,3)
   [Valor 1]=Reg["Valor 1"]
   
   VRTOTAL=VRTOTAL+Reg["Valor 1"]
   
   Moveto PrivDir()+"GR_0002a"
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_EMPRESATE"       : Campo="NO"
            Case Campo="TG_PLCONTAS"      : Campo="TG_EMPRESATE"
            Case Campo="TG_MESANO"        : Campo="TG_PLCONTAS"
            Case Campo="YES" Or Campo="NO": Campo="TG_EMPRESATE"
          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_EMPRESATE" :
                         If Not IsBlank(VSDGEMPRESA)
                            Then Campo="TG_PLCONTAS"
                            Else VSDGNOMEMP=""
                         Endif
                    Case Campo="TG_PLCONTAS" :
                         If Not IsBlank(VSDGPLCONTAS)
                            Then Campo="TG_MESANO"
                            Else VSDGNOMPLCONTAS=""
                         Endif
                    Case Campo="TG_MESANO" : Campo="YES"
                  EndSwitch
          EndIf
          RefreshDialog
   EndSwitch
EndIf
Switch
  Case Campo="TG_EMPRESATE":
     MSG_CAMPO="Informe uma Empresa"
  Case Campo="TG_PLCONTAS":
     MSG_CAMPO="Informe um Plano de Conta"
Endswitch
Monta_Prompt(MSG_CAMPO,TECLA)
Return Temp
EndProc

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


VSDGEMPRESA =""
VSDGPLCONTAS=""
VSDGMESANO  =""

While True
  ShowDialog "Movimentos mensais por plano de conta" 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 12 Width 72

    Label  @ 00,00 "Empresa"  For "TG_EMPRESATE"
    Accept @ 01,01 Width 07 "A4"  Picture "[&,#][#*#]" Tag "TG_EMPRESATE"    To VSDGEMPRESA
    Accept @ 01,09 Width 25 "A20" Picture "!*!" Tag "TG_NOMEMPRESA" To VSDGNOMEMP

    Label  @ 02,00 "Plano de Contas" For "TG_PLCONTAS"
    Accept @ 03,01 Width 33 "A13"  Picture "!*!" Tag "TG_PLCONTAS" To VSDGPLCONTAS
    Accept @ 04,01 Width 33 "A40" Picture "!*!" Tag "TG_NOMPLCONTAS"  To VSDGNOMPLCONTAS

    Label  @ 05,00 "At‚ Mˆs/Ano" For "TG_MESANO"
    Accept @ 06,01 Width 13 "A7"  Picture "##;.####" Tag "TG_MESANO" To VSDGMESANO

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

  EndDialog
  If Not RetVal Then QuitLoop EndIf
  If Not Valida_ShowDialog("") Then Loop EndIf

  If VSDGEMPRESA=""
     Then Mensagem("Empresa Invalida !")
          Loop
  EndIf
  If VSDGPLCONTAS=""
     Then Mensagem("Plano de Conta Invalido !")
          Loop
  EndIf
  Switch
    Case VSDGTPPLCONTAS="A": PLCONTAS=VSDGPLCONTAS
    Case VSDGTPPLCONTAS="S": PLCONTAS=VSDGPLCONTAS+".."
    OtherWise              : PLCONTAS=VSDGPLCONTAS+".."
  EndSwitch

  If VSDGMESANO=""
     Then Mensagem("Mes/Ano Invalido !")
          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

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

  DISPOSITIVOG()
  If OK="" Then Loop EndIf

  Copy "\\Base\\Sistemas\\Ctacor\\GR_0002"  PrivDir()+"GR_0002"
  Copy "\\Base\\Sistemas\\Ctacor\\GR_0002a" PrivDir()+"GR_0002a"
  Empty(PrivDir()+"GR_0002")
  Empty(PrivDir()+"GR_0002a")

  AGUARDE("Aguarde, Gerando Dados")
  Tabela=Cria_Temp(PrivDir())

  Monta_Dir_Trab(VSDGDTINI,VSDGDTFIM)
  While Novo_Dir_Trab()
    For i From 1 To 2
      If IsTable("Answer")     Then Delete "Answer" EndIf
      Seleciona(i)
      If Not IsTable("Answer") Then Loop EndIf
      If IsTable(Tabela)
         Then Add "Answer" Tabela
         Else Rename "Answer" Tabela
      EndIf
      If SubStr(PLCONTAS,1,1)="1"
         Then QuitLoop
      EndIf
    EndFor
  EndWhile

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

  GRAF_TAB= PrivDir()+"GR_0002"
  GRAF_TIPO= "3"
  GRAF_TIT1= "Movimentos Mensais por Plano de Contas dos éltimos 13 Meses at‚ "+rTrim(mesext(NumVal(Substr(VSDGMESANO,1,2))))+"/"+Substr(VSDGMESANO,4,4)
  GRAF_TIT2= "Empresa: "+VSDGEMPRESA+" - "+VSDGNOMEMP+" Plano de Contas: "+VSDGPLCONTAS+" - "+VSDGNOMPLCONTAS
             +" [ Total: "+LTrim(Format("W13.2,EIC",VRTOTAL))+" ]"
  GRAF_LINX= "Valor"
  GRAF_LINY= "Mes"

  MONTAGRAFICO(GRAF_TAB)

  Delete PrivDir()+"GR_0002"
  Delete PrivDir()+"GR_0002a"
  Delete "Answer"
  Delete Tabela

EndWhile
ClearAll
Release Vars All
Release Procs All
