;** GR_0200 ********************************************************************
;*  Grafico de Faturamento Mensal II                                           *
;*******************************************************************************
;Autor           : Vise Informatica (VISERP)
;Desenvolvido    : 05/12/2008 por Anivel Augusto Rodrigues
;Ultima Alteracao: 19/09/2011 por Anivel Augusto Rodrigues
;Motivo Alteracao: Ajustes 0000 TOTAL EMPRESAS

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

If IsTable("\\Sistemas\\Vendas\\"+Dir_Trab+"NotaC")
   Then
        If VSDGOPAG=1 Or VSDGOPAG=3 Or VSDGOPAG=4 Then
           {Ask} TypeIn "\\Sistemas\\Vendas\\"+Dir_Trab+"NotaC" Enter
           Moveto [Codigo da Empresa      ] TypeIn "~VSDGEMPRESA2.."
           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"
           Moveto [Valor Nota Fiscal      ] TypeIn "Calc Sum as Valor Total"
           Do_It!
           ClearAll

           Processa()
        EndIf

        If VSDGOPAG=2 Or VSDGOPAG=3 Or VSDGOPAG=4 Then
           {Ask} TypeIn "\\Sistemas\\Vendas\\"+Dir_Trab+"NotaC" Enter
           Moveto [Codigo da Empresa      ] TypeIn "~VSDGEMPRESA2.."
           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"
           Moveto [Valor Nota Fiscal      ] TypeIn "Calc Sum as Valor Total"
           Do_It!
           ClearAll

           Processa()
        EndIf
EndIf
EndProc

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

If IsTable("\\Sistemas\\Estoque\\"+Dir_Trab+"NotaEC")
   Then
        If VSDGOPAG=2 Or VSDGOPAG=3 Or VSDGOPAG=4 Then
           {Ask} TypeIn "\\Sistemas\\Estoque\\"+Dir_Trab+"NotaEC" Enter
           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"
           Moveto [Valor Nota Fiscal      ] TypeIn "Calc Sum as Valor Total"
           Do_It!
           ClearAll

           Processa()
        EndIf
EndIf
EndProc


;*******************************************************************************
;* Rotina p/ Selecionar e Ordernar Registros                                   *
;*******************************************************************************
Proc Cria_Registros()
Private mes,ano

mes=NumVal(MESINI)
ano=NumVal(ANOINI)

ClearAll

Edit PrivDir()+"GR_0010a"
While True
  End Down
  [Ano   ]=ano
  [Mes   ]=mes

  If mes=Month(VSDGDTFIM) And ano=Year(VSDGDTFIM)
     Then QuitLoop
  EndIf   
  mes=mes+1
  If mes=13
     Then mes=1
          ano=ANO+1
  EndIf
EndWhile
Do_It!
ClearAll

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
   If [Answer->Tipo Movimento Estoque]="S"
      Then [Valor 1]=[Valor 1]+[Answer->Valor Total]
      Else [Valor 2]=[Valor 2]+[Answer->Valor Total]
   EndIf
   Moveto "Answer"
EndScan
Do_It!
ClearAll

EndProc

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

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

Mes=0
View PrivDir()+"GR_0011"
CoEdit "Answer"
Scan
   Moveto PrivDir()+"GR_0011"
   End Down
   [Mes        ]=Substr(mesext(NumVal([Answer->Mes])),1,3)
;  [Mes        ]=Substr(mesext(NumVal([Answer->Mes])),1,3)+"/"+SubStr(AlinhM(StrVal([Answer->Ano]),4),3,2)
;  [Mes        ]=Substr(mesext(NumVal([Answer->Mes])),1,3)+"/"+StrVal([Answer->Ano])
   Switch
     Case VSDGOPAG=1: [Valor 1    ]=[Answer->Valor 1]
     Case VSDGOPAG=2: [Valor 1    ]=[Answer->Valor 2]
     Case VSDGOPAG=3: [Valor 1    ]=[Answer->Valor 1]
                      [Valor 2    ]=[Answer->Valor 2]
     Case VSDGOPAG=4: [Valor 1    ]=[Answer->Valor 1]-[Answer->valor 2]
   EndSwitch
   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


If VSDGOPAG<>3 Then
   {Ask} TypeIn PrivDir()+"GR_0011" Enter
   Moveto [Mes    ] Example "mm"
   Moveto [Valor 1] Example "vr"
   {Ask} TypeIn PrivDir()+"GR_0010" Enter TypeIn "Fast Insert"
   Moveto [Mes    ] Example "mm"
   Moveto [Valor 1] Example "vr"
   Do_it!
   ClearAll
EndIf


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="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_MESANO"
                            Else VSDGNOMEMP=""
                         Endif
                    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":
     MSG_CAMPO="Selecione o op‡„o desejada !"
     TECLA    ="<- ->=Selecionar  "
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 mensais" 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 70

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

    Label  @ 02,00 "Ate Mes/Ano" For "TG_MESANO"
    Accept @ 03,01 Width 13 "A7"  Picture "##;.####" Tag "TG_MESANO" To VSDGMESANO

    Label  @ 04,00 "Op‡”es de Datas: "  For "TG_OPAG"
    RadioButtons @ 05,01 Height 1 Width 65
     "~V~endas",
     "~D~evolu‡”es",
     "~A~mbos [V/D]",
     "~L~ˇquido [V-D]"
     Tag "TG_OPAG"
     To VSDGOPAG

    Pushbutton @ 07,21 width 11 "~O~k"      OK     Default Value "Ok" Tag "YES" To BottonValue
    Pushbutton @ 07,35 width 11 "~C~ancela" CANCEL                    Tag "NO"  To BottonValue

  EndDialog
  If Not RetVal Then QuitLoop 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_0010"  PrivDir()+"GR_0010"
  Copy "\\Base\\Sistemas\\Vendas\\GR_0010a" PrivDir()+"GR_0010a"
  Copy "\\Base\\Sistemas\\Vendas\\GR_0011"  PrivDir()+"GR_0011"
  Empty(PrivDir()+"GR_0010")
  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

  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"
                      GRAF_TAB  = PrivDir()+"GR_0010"
    Case VSDGOPAG=2 : GRAF_TIT1 = "Evolu‡„o das devolu‡”es"
                      GRAF_TAB  = PrivDir()+"GR_0010"
    Case VSDGOPAG=3 : GRAF_TIT1 = "Evolu‡„o das Vendas/Devolu‡”es"
                      GRAF_TAB  = PrivDir()+"GR_0011"
                      GRAF_LENG = "Y"
                      GRAF_LENG1= "Vendas"
                      GRAF_LENG2= "Devolucoes"
    Case VSDGOPAG=4 : GRAF_TIT1 = "Evolu‡„o das vendas lˇquidas [V-D]"
                      GRAF_TAB  = PrivDir()+"GR_0010"
  EndSwitch
  GRAF_TIT2= VSDGNOMEMP+" at‚ "+VSDGMESANO
  GRAF_LINX= "Valor"
  GRAF_LINY= "Mes"

  MONTAGRAFICO(GRAF_TAB)
  Delete PrivDir()+"GR_0010"
  Delete PrivDir()+"GR_0010a"
  Delete PrivDir()+"GR_0011"
  Delete "Answer"

EndWhile
ClearAll
Release Vars All
Release Procs All
