;** GR_0004 ********************************************************************
;*  Grafico de Compras Diario                                                  *
;*******************************************************************************
;Autor           : Vise Informatica (VISERP)
;Desenvolvido    : 06/05/2003 por Rodrigo Francisco Bento
;Ultima Alteracao: 02/09/2004 por Jos‚ Lu¡s Ameno da Silva
;Motivo Alteracao: PrivDir
;Ultima Alteracao: 19/09/2016 por Anivel Augusto Rodrigues
;Motivo Alteracao: Ajustes showdialog                   

;*******************************************************************************
;* 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"
        Do_It!
        ClearAll
EndIf
EndProc

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

DIA=DAY(VSDGDTFIM)
CoEdit PrivDir()+"GR_0004a"
For wi From 1 to DIA
    End Down
    [Dia   ]=AlinhM(StrVal(wi),2)
    [Valor ]=0
EndFor
Do_It!
ClearAll

EndProc
;*******************************************************************************
;* Rotina p/ Processar Dados                                                   *
;*******************************************************************************
Proc PROCESSA()
ClearAll

{Ask} TypeIn Tabela Enter
Moveto [Data de Entrada        ] Check
Moveto [Valor Total            ] TypeIn "Calc Sum as Valor Total"
Do_It!
ClearAll

View PrivDir()+"GR_0004a"
CoEdit "Answer"
Scan
    If IsBlank([Data de Entrada]) Then Loop EndIf
    Moveto PrivDir()+"GR_0004a"
    Moveto [Dia]
    Locate AlinhM(StrVal(Day([Answer->Data de Entrada])),2)
    If RetVal
        Then 
            [Valor ]= [Valor]+([Answer->Valor Total])
        Else End Down
             [Dia   ]=AlinhM(StrVal(Day([Answer->Data de Entrada])),2)
             [Valor ]=0  
    EndIf
    Moveto "Answer"
EndScan
Do_It!
ClearAll

EndProc

;*******************************************************************************
;* Rotina p/ Processar Dados                                                   *
;*******************************************************************************
Proc PROCESSA2()
ClearAll
VRTOTAL=0
{Ask} TypeIn PrivDir()+"GR_0004a" Enter Check
Do_It!
ClearAll

View PrivDir()+"GR_0004"
CoEdit "Answer"
Scan
    DIA =AlinhM(StrVal([Answer->Dia]),2)
    dtref=DateVal(MES+"/"+DIA+"/"+ANO)
    Switch
     Case Dow(dtref)="Sun": DIA=DIA+"Dm"
     Case Dow(dtref)="Mon": DIA=DIA+"Sg"
     Case Dow(dtref)="Tue": DIA=DIA+"Tr"
     Case Dow(dtref)="Wed": DIA=DIA+"Qa"
     Case Dow(dtref)="Thu": DIA=DIA+"Qi"
     Case Dow(dtref)="Fri": DIA=DIA+"Sx"
     Case Dow(dtref)="Sat": DIA=DIA+"Sb"
    EndSwitch
    Moveto PrivDir()+"GR_0004"
    End Down
    [Dia  ]=DIA
    [Valor]=[Answer->Valor]
    VRTOTAL=VRTOTAL+[Valor]
    Moveto "Answer"
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_MESANO"        : Campo="TG_EMPRESA"
            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_MESANO"
                            Else VSDGNOMEMP=""
                         Endif
                    Case Campo="TG_MESANO" : Campo="YES"
                  EndSwitch
          EndIf
          RefreshDialog
   EndSwitch
EndIf
Switch
  Case Campo="TG_EMPRESA":
     MSG_CAMPO="Informe uma Empresa"
Endswitch
Monta_Prompt(MSG_CAMPO,TECLA)
Return Temp
EndProc

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

While True
  ShowDialog "Evolu‡„o das Compras Di rias" Proc "SD_Dialog"
  Trigger "OPEN","ARRIVE"
  Key "F1","ENTER","ESC",
      -104, -105, -106, -107, -108, -109, -110, -111, -112, -113, -139, -140  
   @ 06,12   Height 09 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 "Mˆs/Ano Referˆncia" 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 Not Valida_ShowDialog("") Then Loop EndIf

  If VSDGEMPRESA=""
     Then Mensagem("Empresa Invalida !")
          Loop
  EndIf

  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

  VSDGDTINI=DateVal(MES+"/"+"01"+"/"+ANO)

  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_0004"  PrivDir()+"GR_0004"
  Copy "\\Base\\Sistemas\\Estoque\\GR_0004"  PrivDir()+"GR_0004a"
  Empty(PrivDir()+"GR_0004")
  Empty(PrivDir()+"GR_0004a")

  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_0004"
  GRAF_TIPO= "L"
  GRAF_TIT1= "Evolu‡„o das Compras Di rias do Mes de "+rTrim(mesext(NumVal(Substr(VSDGMESANO,1,2))))+"/"+Substr(VSDGMESANO,4,4)
  GRAF_TIT2= "Empresa: "+VSDGEMPRESA+"-"+VSDGNOMEMP+" [ Total: "+LTrim(Format("W13.2,EIC",VRTOTAL))+" ]"
  GRAF_LINX= "Valor"
  GRAF_LINY= "Dias"

  MONTAGRAFICO(GRAF_TAB)

  Delete PrivDir()+"GR_0004"
  Delete PrivDir()+"GR_0004a"
  Delete Tabela

EndWhile
ClearAll
Release Vars All
Release Procs All

