;** GR_0016 ********************************************************************
;*  Grafico das Compras Mensais por Fornecedor                                 *
;*******************************************************************************
;Autor           : Vise Informatica (VISERP)
;Desenvolvido    : 17/10/96 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: 06/10/2012 por Kamila Gomes          
;Motivo Alteracao: Ajustes Rotina Processa e Titulos dos Graficos 
;Ultima Alteracao: 19/09/2016 por Anivel Augusto Rodrigues
;Motivo Alteracao: Ajustes showdialog    
;Ultima Alteracao: 28/12/2016 por Rafael Matos
;Motivo Alteracao: Ajustes showdialog TG_DESTINAT            

;*******************************************************************************
;* Rotina p/ Selecionar e Ordernar Registros                                   *
;*******************************************************************************
Proc SELECIONA()
ClearAll
If IsTable("\\Sistemas\\Estoque\\"+Dir_Trab+"NotaEC")
   Then {Ask} TypeIn "\\Sistemas\\Estoque\\"+Dir_Trab+"NotaEC" Enter
        Moveto [Codigo da Empresa      ] TypeIn "~VSDGEMPRESA.."
        Moveto [Data de Entrada        ] check TypeIn ">=~VSDGDTINI, <=~VSDGDTFIM"
        Moveto [Tipo de Operacao       ] TypeIn "C"
        Moveto [Valor Nota Fiscal      ] TypeIn "Calc Sum as Valor Total"
        Moveto [Codigo                 ] TypeIn "~VSDGDESTINAT.."
        Do_It!
        ClearAll
EndIf
EndProc

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

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

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

{Ask} TypeIn Tabela Enter Check
Do_It!
ClearAll

View PrivDir()+"GR_0005a"
CoEdit "Answer"
Scan
   If IsBlank([Data de Entrada]) Then Loop EndIf
   Moveto PrivDir()+"GR_0005a"
   Moveto [Ano]
   Locate Year([Answer->Data de Entrada]),Month([Answer->Data de Entrada])
   If Not RetVal
      Then End Down
           [Ano   ]=Year ([Answer->Data de Entrada])
           [Mes   ]=Month([Answer->Data de Entrada])
   EndIf
   [Valor Total]=[Valor Total]+[Answer->Valor Total]
   Moveto "Answer"
EndScan
Do_It!
ClearAll

EndProc

;*******************************************************************************
;* Rotina p/ Processar Dados                                                   *
;*******************************************************************************
Proc PROCESSA2()
ClearAll

VRTOTAL=0
View PrivDir()+"GR_0005"
CoEdit PrivDir()+"GR_0005a"
Scan
   CopytoArray Reg
   
   Moveto PrivDir()+"GR_0005"
   End Down
   [Mes        ]=StrVal(Reg["Ano"])+"/"+Substr(mesext(NumVal(Reg["Mes"])),1,3)
   [Valor Total]=Reg["Valor Total"]
   
   VRTOTAL=VRTOTAL+Reg["Valor Total"]
   
   Moveto PrivDir()+"GR_0005a"
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_EMPRESA"       : Campo="NO"
            Case Campo="TG_DESTINAT"      : Campo="TG_EMPRESA"
            Case Campo="TG_MESANO"        : Campo="TG_DESTINAT"
            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" :
                         If Not IsBlank(VSDGEMPRESA)
                            Then Campo="TG_DESTINAT"
                            Else VSDGNOMEMP=""
                         Endif
                    Case Campo="TG_DESTINAT" :
                         If Not IsBlank(VSDGDESTINAT)
                            Then Campo="TG_MESANO"
                            Else VSDGNOMDEST=""
                         Endif
                    Case Campo="TG_MESANO" : Campo="YES"
                  EndSwitch
          EndIf
          RefreshDialog
   EndSwitch
EndIf
Switch
  Case Campo="TG_EMPRESA":
     MSG_CAMPO="Informe uma Empresa"
  Case Campo="TG_DESTINAT":
     MSG_CAMPO="Informe um Cliente"
Endswitch
Monta_Prompt(MSG_CAMPO,TECLA)
Return Temp
EndProc

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

VSDGEMPRESA =""
VSDGDESTINAT=""
VSDGMESANO  =""

While True
  ShowDialog "Evolu‡„o das Compras Mensais por Fornecedor" 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 11 Width 72

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

    Label  @ 02,00 "Pessoa"  For "TG_DESTINAT"
    Accept @ 03,01 Width 07 "A4"  Picture "[&,#][#*#]" Tag "TG_DESTINAT"   To VSDGDESTINAT
    Accept @ 03,09 Width 25 "A20" Picture "!*!" Tag "TG_NOMDEST"    To VSDGNOMDEST

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

    Pushbutton @ 07,22 width 11 "~O~k"      OK     Default Value "Ok" Tag "YES" To BottonValue
    Pushbutton @ 07,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 VSDGDESTINAT=""
     Then Mensagem("Cliente 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


  DISPOSITIVOG()
  If OK="" Then Loop EndIf

  Copy "\\Base\\Sistemas\\Estoque\\GR_0005"  PrivDir()+"GR_0005"
  Copy "\\Base\\Sistemas\\Estoque\\GR_0005a" PrivDir()+"GR_0005a"
  Empty(PrivDir()+"GR_0005")
  Empty(PrivDir()+"GR_0005a")

  Cria_Registros()
  
  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()
  PROCESSA2()

  GRAF_TAB= PrivDir()+"GR_0005"
  GRAF_TIPO= "3"
  GRAF_TIT1= "Evolu‡„o das Compras por Fornecedor dos éltimos 13 Meses at‚ "+rTrim(mesext(NumVal(Substr(VSDGMESANO,1,2))))+"/"+Substr(VSDGMESANO,4,4)
  GRAF_TIT2= "Empresa: "+VSDGEMPRESA+"-"+VSDGNOMEMP+"  Fornecedor: "+VSDGDESTINAT+"-"+VSDGNOMDEST+" [ Total: "+LTrim(Format("W13.2,EIC",VRTOTAL))+" ]"
  GRAF_LINX= "Valor"
  GRAF_LINY= "Mes"

  MONTAGRAFICO(GRAF_TAB)

  Delete PrivDir()+"GR_0005"
  Delete PrivDir()+"GR_0005a"
  Delete "Answer"
  Delete Tabela

EndWhile
ClearAll
Release Vars All
Release Procs All

