;** GR_0108 ********************************************************************
;*  Grafico de Pedidos Mensal por Representante                                *
;*******************************************************************************
;Autor           : Vise Informatica (VISERP)
;Desenvolvido    : 19/05/2017 por Rafael Matos
;Ultima Alteracao: 27/11/2018 por Anivel Augusto Rodrigues
;Motivo Alteracao: Ajustes filtro gerencia                   

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

If IsTable("\\Sistemas\\Vendas\\"+Dir_Trab+"PedidoC") And
   IsTable("\\Sistemas\\Vendas\\"+Dir_Trab+"PedidoI")
   Then
       {Ask} TypeIn "\\Sistemas\\Cadastro\\Represen" Enter
        Moveto [Codigo do Representante] Example "crep"
        Moveto [Codigo da Gerencia     ] TypeIn "~VSDGGERENCIA.."

       {Ask} TypeIn "\\Sistemas\\Vendas\\"+Dir_Trab+"PedidoC" Enter
       Moveto [Numero do Pedido       ] Example "nped"
       Moveto [Codigo da Empresa      ] TypeIn "~VSDGEMPRESA2.."  
       Moveto [UF                     ] TypeIn "~VSDGESTADO.."
       Moveto [Data de Emissao        ] check TypeIn ">=~VSDGDTINI, <=~VSDGDTFIM"
       Moveto [Codigo do Representante] Example "crep"
       Moveto [Descricao da Destinacao] TypeIn "..VEND.. Or ..SAIDA.. Or ..SERV.."
       Moveto [Situacao do Pedido     ] 
       Switch
        Case VSDGSITUACAOPEDIDO="JUNTAR ABERTO+SUSPENSO"     : TypeIn "ABERTO or SUSPENSO"
        Case VSDGSITUACAOPEDIDO="IGNORAR CANCELADO+SUSPENSO" : TypeIn "not CANCELADO, not SUSPENSO"
        Case VSDGSITUACAOPEDIDO="IGNORAR CANCELADO"          : TypeIn "not CANCELADO"
        Case VSDGSITUACAOPEDIDO<>"TODAS"                     : TypeIn "~VSDGSITUACAOPEDIDO.."
       EndSwitch
       {Ask} TypeIn "\\Sistemas\\Vendas\\"+Dir_Trab+"PedidoI" Enter
       Moveto [Numero do Pedido       ] Example "nped"
       If VSDGOPANALISE = "2"
          Then Moveto [Quantidade           ] TypeIn "Calc Sum as Quant Total"
          Else Moveto [Preco Total          ] TypeIn "Calc Sum as Valor Total"
               Moveto [Valor Desconto Total ] TypeIn "Calc Sum as Valor Desconto Total"              
       EndIf
       Do_It!
       ClearAll
EndIf

EndProc

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

EndProc

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

ClearAll

{Ask} TypeIn Tabela Enter Check
Do_It!
ClearAll

VRTOTAL=0
View PrivDir()+"GR_0010"
CoEdit "Answer"
Scan
   If IsBlank([Data de Emissao]) Then Loop EndIf

   Mes=AlinhM(StrVal(Year ([Answer->Data de Emissao])),4)+"/"+Substr(mesext(Month([Answer->Data de Emissao])),1,3)

   Moveto PrivDir()+"GR_0010"
   Moveto [Mes]
   Locate mes
   If Not RetVal
      Then End Down
           [Mes        ]=mes
   EndIf
   If VSDGOPANALISE = "2"
      Then [Valor 1]=[Valor 1]+[Answer->Quant Total]
           VRTOTAL  = VRTOTAL +[Answer->Quant Total]
      Else [Valor 1    ]=[Valor 1    ]+[Answer->Valor Total]-[Answer->Valor Desconto Total]
           VRTOTAL      =VRTOTAL      +[Answer->Valor Total]-[Answer->Valor Desconto Total]
   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_GERENCIA"      : Campo="TG_EMPRESA"
            Case Campo="TG_ESTADO"        : Campo="TG_GERENCIA"
            Case Campo="TG_MESANO"        : Campo="TG_ESTADO"
            Case Campo="TG_SITUACAOPEDIDO": Campo="TG_MESANO"
            Case Campo="TG_OPANALISE"     : Campo="TG_SITUACAOPEDIDO"
            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"    : Campo="TG_GERENCIA"
                    Case Campo="TG_GERENCIA"   : Campo="TG_ESTADO"
                    Case Campo="TG_ESTADO" : Campo="TG_MESANO"    
                    Case Campo="TG_MESANO" : Campo="TG_SITUACAOPEDIDO"
                    Case Campo="TG_SITUACAOPEDIDO" : Campo="TG_OPANALISE"
                    Case Campo="TG_OPANALISE"      : Campo="YES"
                  EndSwitch
          EndIf
          RefreshDialog
   EndSwitch
EndIf
Switch
  Case Campo="":
     MSG_CAMPO=""
  Case Campo="TG_REPRESEN":
     MSG_CAMPO="Informe um Representante"
Endswitch
Monta_Prompt(MSG_CAMPO,TECLA)
Return Temp
EndProc

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

VSDGEMPRESA  = ""
VSDGGERENCIA = ""
VSDGREPRESEN = ""
VSDGESTADO   = ""
VSDGMESANO = AlinhM(StrVal(Month(Today())),2)+"."+AlinhM(StrVal(Year(Today())),4)

While True
  ShowDialog "Evolu‡„o dos pedidos 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 13 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 "Gerˆncia " For "TG_GERENCIA"
    Accept @ 03,01 Width 07 "A4"  Picture "[&,#][#*#]" Tag "TG_GERENCIA" To VSDGGERENCIA
    Accept @ 03,09 Width 25 "A10"  Picture "!*!" Tag "TG_DESCGERENCIA" To VSDGDESCGERENCIA

    Label  @ 04,00 "Estado"  For "TG_ESTADO"
    Accept @ 05,03 Width 05 "A2"  Picture "!*!" Tag "TG_ESTADO" To VSDGESTADO
    Accept @ 05,09 Width 25 "A20" Picture "!*!" Tag "TG_NOMESTADO"  To VSDGNOMESTADO

    Label  @ 06,00 "At‚ Mes/Ano" For "TG_MESANO"
    Accept @ 07,01 Width 10 "A7"  Picture "##;.####" Tag "TG_MESANO" To VSDGMESANO
    
    Label  @ 00,35 "Situacao do Pedido"  For "TG_SITUACAOPEDIDO"
    Accept @ 01,36 Width 33 "A30"  Picture "ABERTO,SUSPENSO,CANCELADO,FATURADO,TODAS,!*!" Tag "TG_SITUACAOPEDIDO"  To VSDGSITUACAOPEDIDO
    
    Label  @ 02,35 "Op‡„o p/ Analise" For "TG_OPANALISE"
    Accept @ 03,39 Width 04 "A1"  Picture "1,2" Tag "TG_OPANALISE" To VSDGOPANALISE
    Accept @ 03,44 Width 25 "A25" Picture "!*!" Tag "TG_NOMOPANALISE" To VSDGNOMOPANALISE

    Pushbutton @ 09,22 width 11 "~O~k"      OK     Default Value "Ok" Tag "YES" To BottonValue
    Pushbutton @ 09,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_0010" PrivDir()+"GR_0010"
  Empty(PrivDir()+"GR_0010")

  AGUARDE("Aguarde, Gerando Dados")
  
  Cria_Registros()
  
  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()

  GRAF_TAB= PrivDir()+"GR_0010"
  GRAF_TIPO= "3"
  If VSDGESTADO="" 
     Then GRAF_TIT1= "Evolu‡„o dos Pedidos éltimos 13 Meses at‚ "+rTrim(mesext(NumVal(Substr(VSDGMESANO,1,2))))+"/"+Substr(VSDGMESANO,4,4)
     Else GRAF_TIT1= "Evolu‡„o dos Pedidos em "+VSDGNOMESTADO+" dos éltimos 13 Meses at‚ "
          +rTrim(mesext(NumVal(Substr(VSDGMESANO,1,2))))+"/"+Substr(VSDGMESANO,4,4)
  EndIf
  GRAF_TIT2= "Empresa: "+VSDGEMPRESA+"-"+VSDGNOMEMP+" [ Total: "
  If VSDGOPANALISE="1" 
     Then GRAF_TIT2 = GRAF_TIT2 + LTrim(Format("W13.2,EIC",VRTOTAL))+" ]"
     Else GRAF_TIT2 = GRAF_TIT2 + LTrim(Format("W13,EIC",VRTOTAL))+" ]"
  EndIF   
              
  If VSDGOPANALISE = "2"
     Then GRAF_LINX= "Quantidade"
     Else GRAF_LINX= "Valor"
  EndIf
  
  GRAF_LINY= "Mes"

  MONTAGRAFICO(GRAF_TAB)

  Delete PrivDir()+"GR_0010"
  Delete "Answer"
  Delete Tabela

EndWhile
ClearAll
Release Vars All
Release Procs All
