;** GR_0201 ********************************************************************
;*  Grafico de Venas/Metas                                                     *
;*******************************************************************************
;Autor           : Vise Informatica (VISERP)
;Desenvolvido    : 25/05/2018 por Anivel Augusto Rodrigues
;Ultima Alteracao:   /  /     por 
;Motivo Alteracao: 

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

If IsTable("\\Sistemas\\Vendas\\"+Dir_Trab+"NotaC") And
   IsTable("\\Sistemas\\Vendas\\"+Dir_Trab+"NotaI")
   Then
        If VSDGOPAG=1 Or VSDGOPAG=2 Then
           {Ask} TypeIn "\\Sistemas\\Vendas\\"+Dir_Trab+"NotaC" Enter
           Moveto [Codigo da Empresa      ] Example "cemp" TypeIn ",~VSDGEMPRESA2.."
           Moveto [Numero do Documento    ] Example "ndoc"
           Moveto [Serie do Documento     ] Example "sdoc"
           Moveto [Data de Emissao        ] check TypeIn ">=~VSDGDTINI, <=~VSDGDTFIM"
           Moveto [Descricao da Destinacao] TypeIn "..VEND.. Or ..SAIDA.. Or ..SERV.."
           Moveto [Situacao               ] TypeIn "Not C"
           Moveto [Tipo Movimento Estoque ] Check TypeIn "S"
           Switch
               Case VSDGOPANALISE="1" :
                  Moveto [Valor Nota Fiscal      ] TypeIn "Calc Sum as Valor Total"
               Case VSDGOPANALISE="2" :
                  {Ask} TypeIn "\\Sistemas\\Vendas\\"+Dir_Trab+"NotaI" Enter
                  Moveto [Codigo da Empresa      ] Example "cemp"
                  Moveto [Numero do Documento    ] Example "ndoc"
                  Moveto [Serie do Documento     ] Example "sdoc"
                  Moveto [Preco Total            ] TypeIn "Calc Sum as Valor Total"
                  Moveto [Valor Desconto Total   ] TypeIn "Calc Sum as Valor Desconto Total"
                  Moveto [Situacao_Item          ] TypeIn "Not C"
           EndSwitch
           Do_It!
           ClearAll

           Processa()
        EndIf

        If VSDGOPAG=2                             Then
           {Ask} TypeIn "\\Sistemas\\Vendas\\"+Dir_Trab+"NotaC" Enter
           Moveto [Codigo da Empresa      ] Example "cemp" TypeIn ",~VSDGEMPRESA2.."
           Moveto [Numero do Documento    ] Example "ndoc"
           Moveto [Serie do Documento     ] Example "sdoc"
           Moveto [Data de Emissao        ] check TypeIn ">=~VSDGDTINI, <=~VSDGDTFIM"
           Moveto [Descricao da Destinacao] TypeIn "..DEVOL.."
           Moveto [Situacao               ] TypeIn "Not C"
           Moveto [Tipo Movimento Estoque ] Check TypeIn "E"
           Switch
               Case VSDGOPANALISE="1" :
                  Moveto [Valor Nota Fiscal      ] TypeIn "Calc Sum as Valor Total"
               Case VSDGOPANALISE="2" :
                  {Ask} TypeIn "\\Sistemas\\Vendas\\"+Dir_Trab+"NotaI" Enter
                  Moveto [Codigo da Empresa      ] Example "cemp"
                  Moveto [Numero do Documento    ] Example "ndoc"
                  Moveto [Serie do Documento     ] Example "sdoc"
                  Moveto [Preco Total            ] TypeIn "Calc Sum as Valor Total"
                  Moveto [Valor Desconto Total   ] TypeIn "Calc Sum as Valor Desconto Total"
                  Moveto [Situacao_Item          ] TypeIn "Not C"
           EndSwitch
           Do_It!
           ClearAll

           Processa()
        EndIf
EndIf
EndProc

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

If IsTable("\\Sistemas\\Estoque\\"+Dir_Trab+"NotaEC") And
   IsTable("\\Sistemas\\Estoque\\"+Dir_Trab+"NotaEI")
   Then
        If VSDGOPAG=2                             Then
           {Ask} TypeIn "\\Sistemas\\Estoque\\"+Dir_Trab+"NotaEC" Enter
           Moveto [Numero do Lancamento   ] Example "nlan" 
           Moveto [Codigo da Empresa      ] TypeIn "~VSDGEMPRESA2.."
           Moveto [Data de Entrada        ] check TypeIn ">=~VSDGDTINI, <=~VSDGDTFIM, as Data de Emissao"
           Moveto [Tipo de Operacao       ] Check TypeIn "D, as Tipo Movimento Estoque"
           Switch
               Case VSDGOPANALISE="1" :
                  Moveto [Valor Nota Fiscal      ] TypeIn "Calc Sum as Valor Total"
               Case VSDGOPANALISE="2" :
                  {Ask} TypeIn "\\Sistemas\\Estoque\\"+Dir_Trab+"NotaEI" Enter
                  Moveto [Numero do Lancamento   ] Example "nlan"
                  Moveto [Preco Total            ] TypeIn "Calc Sum as Valor Total"
                  Moveto [Valor Desconto Total   ] TypeIn "Calc Sum as Valor Desconto Total"
           EndSwitch
           Do_It!
           ClearAll

           Processa()
        EndIf
EndIf
EndProc

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

View PrivDir()+"GR_0010a"
CoEdit "Answer"
Scan
   If IsBlank([Data de Emissao]) Then Loop EndIf
   Moveto PrivDir()+"GR_0010a"
   Moveto [Ano]
   Locate Year([Answer->Data de Emissao]),Month([Answer->Data de Emissao])
   If Not RetVal
      Then End Down
           [Ano   ]=Year ([Answer->Data de Emissao])
           [Mes   ]=Month([Answer->Data de Emissao])
   EndIf
   Switch 
       Case VSDGOPANALISE="1" :
           If [Answer->Tipo Movimento Estoque]="S"
              Then [Valor 1]=[Valor 1]+[Answer->Valor Total]
              Else [Valor 2]=[Valor 2]+[Answer->Valor Total]
           EndIf
       Case VSDGOPANALISE="2" :
           If [Answer->Tipo Movimento Estoque]="S"
              Then [Valor 1]=[Valor 1]+[Answer->Valor Total]-[Answer->Valor Desconto Total]
              Else [Valor 2]=[Valor 2]+[Answer->Valor Total]-[Answer->Valor Desconto Total]
           EndIf
   EndSwitch
   Moveto "Answer"
EndScan
Do_It!
ClearAll

EndProc

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

If VSDGOPAG=2 
   Then
        CoEdit PrivDir()+"GR_0010a"
        Scan
          [Valor 1]=[Valor 1]-[Valor 2]
          [Valor 2]=blanknum()
        EndScan
        Do_It!
        ClearAll
endif

{Ask} {\\Sistemas\\Vendas\\Rankvc}
Moveto [Numero da Programacao  ] TypeIn "_Prog"
Moveto [Codigo da Empresa      ] TypeIn "~VSDGEMPRESA2.."
Moveto [Data Inicial           ] Check TypeIn ">=~VSDGDTINI, <=~VSDGDTFIM"
{Ask} {\\Sistemas\\Vendas\\Rankvi}
Moveto [Numero da Programacao  ] TypeIn "_Prog"
Moveto [Meta_Faturamento       ] typein "Calc Sum As Valor Total"
Do_it!
ClearAll

View PrivDir()+"GR_0010a"
CoEdit "Answer"
Scan
   If IsBlank([Data Inicial]) Then Loop EndIf
   Moveto PrivDir()+"GR_0010a"
   Moveto [Ano]
   Locate Year([Answer->Data Inicial]),Month([Answer->Data Inicial])
   If Not RetVal
      Then End Down
           [Ano   ]=Year ([Answer->Data Inicial])
           [Mes   ]=Month([Answer->Data Inicial])
   EndIf
   [Valor 2]=[Valor 2]+[Answer->Valor Total]

   Moveto "Answer"
EndScan
Do_It!
ClearAll

endproc

;*******************************************************************************
;* Rotina p/ Selecionar e Ordernar Registros                                   *
;*******************************************************************************
Proc Cria_Registros()
Private mes,ano
Mes_Aux=NumVal(MESINI)
Ano_Aux=NumVal(ANOINI)
CoEdit PrivDir()+"GR_0010a"
For wi From 1 to 13
    End Down
    [Ano        ]=Ano_Aux
    [Mes        ]=Mes_Aux
    [MesExt     ]=mesext(NumVal(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

EndProc

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

{Ask} TypeIn PrivDir()+"GR_0010a" Enter Check
Do_It!
ClearAll

VRTOTAL    =0
VRTOTALMETA=0
Mes=0

View PrivDir()+"GR_0011"
CoEdit "Answer"
Scan
   Moveto PrivDir()+"GR_0011"
   End Down
   [Mes        ]=SubStr(AlinhM(StrVal([Answer->Ano]),4),1,4)+"/"+Substr(mesext(NumVal([Answer->Mes])),1,3)

   [Valor 1    ]=[Answer->Valor 1]
   [Valor 2    ]=[Answer->Valor 2]

   VRTOTAL      =VRTOTAL    +[Valor 1]
   VRTOTALMETA  =VRTOTALMETA+[Valor 2]

   If Mes <> 0
      Then While True
             If Mes = 12 Then Mes=0 EndIf
             If NumVal([Answer->Mes]) <> Mes+1
                Then Ins
                     [Mes         ]=Substr(mesext(NumVal(Mes+1)),1,3)
                     [Valor 1     ]=0
                     [Valor 2     ]=0
                     Mes=Mes+1
                     Down
                Else Mes=NumVal([Answer->Mes])
                     QuitLoop
             EndIf
           EndWhile
      Else Mes=NumVal([Answer->Mes])
   EndIf
   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_OPANALISE"     : Campo="TG_EMPRESA"
            Case Campo="TG_MESANO"        : Campo="TG_OPANALISE"
            Case Campo="TG_OPAG"          : Campo="TG_MESANO"
            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_OPANALISE"
                            Else VSDGNOMEMP=""
                         Endif
                    Case Campo="TG_OPANALISE"   :Campo="TG_MESANO"
                         VSDGOPANALISE=ControlValue("TG_OPANALISE")
                         Switch
                           Case VSDGOPANALISE="2":
                                VSDGNOMOPANALISE="Valor Total Produtos"
                           OtherWise:
                                VSDGOPANALISE="1"
                                VSDGNOMOPANALISE="Valor Total Nota"
                         EndSwitch
                    Case Campo="TG_MESANO" : Campo="TG_OPAG"
                    Case Campo="TG_OPAG"   : Campo="YES"
                      VSDGOPAG=ControlValue("TG_OPAG")
                  EndSwitch
          EndIf
          RefreshDialog
   EndSwitch
EndIf
Switch
  Case Campo="":
     MSG_CAMPO=""
  Case Campo="TG_OPAG":
       MensagemRadioButtons()
    Case Campo="TG_OPANALISE":
     MSG_CAMPO="Op‡”es: 1=Valor Total Nota  2=Valor Total Produtos"
Endswitch
Monta_Prompt(MSG_CAMPO,TECLA)
Return Temp
EndProc

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

VSDGEMPRESA=""
VSDGOPAG   =1
VSDGMESANO = AlinhM(StrVal(Month(Today())),2)+"."+AlinhM(StrVal(Year(Today())),4)

While True
  ShowDialog "Evolu‡„o das Vendas/Metas dos éltimos 13 Meses" 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 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 "Op‡„o p/ Analise" For "TG_OPANALISE"
    Accept @ 03,04 Width 04 "A1"  Picture "1,2" Tag "TG_OPANALISE" To VSDGOPANALISE
    Accept @ 03,09 Width 25 "A25" Picture "!*!" Tag "TG_NOMOPANALISE" To VSDGNOMOPANALISE

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

    Label  @ 00,35 "Op‡Æo de Visualiza‡Æo: "  For "TG_OPAG"
    RadioButtons @ 01,35 Height 5 Width 33
     "~V~endas",
     "~L~¡quido [Venda-Devolu‡Æo]"
     Tag "TG_OPAG"
     To VSDGOPAG

    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 VSDGEMPRESA="0000"
     Then VSDGEMPRESA2=""
     Else VSDGEMPRESA2=VSDGEMPRESA
  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\\Vendas\\GR_0010a" PrivDir()+"GR_0010a"
  Copy "\\Base\\Sistemas\\Vendas\\GR_0011"  PrivDir()+"GR_0011"
  Empty(PrivDir()+"GR_0010a")
  Empty(PrivDir()+"GR_0011")

  AGUARDE("Aguarde, Gerando Dados")

  Cria_Registros()
  
  Monta_Dir_Trab(VSDGDTINI,VSDGDTFIM)
  While Novo_Dir_Trab()
    Seleciona()
    Seleciona2()
  EndWhile
  SELECIONAMETAS()

  If IsEmpty(PrivDir()+"GR_0010a")
     Then MENSAGEM("N„o existem registros a processar")
          Loop
  EndIf

  Processa2()
  
  GRAF_TIPO= "3"
  Switch
    Case VSDGOPAG=1 : GRAF_TIT1 = "Evolu‡„o das Vendas/Metas dos éltimos 13 Meses at‚ "+rTrim(mesext(NumVal(Substr(VSDGMESANO,1,2))))+"/"+Substr(VSDGMESANO,4,4)
                      GRAF_TAB  = PrivDir()+"GR_0011"
                      GRAF_LENG = "Y"
                      GRAF_LENG1= "Vendas"
                      GRAF_LENG2= "Metas"
                      
    Case VSDGOPAG=2 : GRAF_TIT1 = "Evolu‡„o das Vendas L¡quidas/Metas dos éltimos 13 Meses at‚ "+rTrim(mesext(NumVal(Substr(VSDGMESANO,1,2))))+"/"+Substr(VSDGMESANO,4,4)
                      GRAF_TAB  = PrivDir()+"GR_0011"
                      GRAF_LENG = "Y"
                      GRAF_LENG1= "Vendas"
                      GRAF_LENG2= "Metas"

  EndSwitch
  GRAF_TIT2= "Empresa: "+VSDGEMPRESA+"-"+VSDGNOMEMP+" [ Total: Venda Liquida "+LTrim(Format("W14.2,EIC",VRTOTAL))+"     Meta "+LTrim(Format("W14.2,EIC",VRTOTALMETA))+" ]"
  GRAF_LINX= "Valor"
  GRAF_LINY= "Mes"
  
  MONTAGRAFICO(GRAF_TAB)
  Delete PrivDir()+"GR_0010a"
  Delete PrivDir()+"GR_0011"
  Delete "Answer"

EndWhile
ClearAll
Release Vars All
Release Procs All
