;** GR_0024 ********************************************************************
;*  Grafico de Atendimentos Mensais                                            *
;*******************************************************************************
;Autor           : Vise Informatica (VISERP)
;Desenvolvido    : 12/11/2012 por Kamila Gomes           
;Ultima Alteracao: 14/12/2016 por Anivel Augusto Rodrigues
;Motivo Alteracao: Ajustes showdialog     
;Ultima Alteracao: 03/01/2017 por Rafael Matos
;Motivo Alteracao: Ajustes showdialog MensagemRadioButtons()               

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

If IsTable("\\Sistemas\\Vendas\\"+Dir_Trab+"NotaC") And
   IsTable("\\Sistemas\\Vendas\\"+Dir_Trab+"NotaCAvl")
  Then 
       {Ask} TypeIn "\\Sistemas\\Vendas\\"+Dir_Trab+"NotaC" Enter 
       Moveto [Codigo da Empresa      ] Check TypeIn "_cemp!, ~VSDGEMPRESA.."
       Moveto [Numero do Documento    ] Check TypeIn "_ndoc!"
       Moveto [Serie do Documento     ] Check TypeIn "_sdoc!"                                
       Moveto [Data de Emissao        ] Check TypeIn ">=~VSDGDTINI,<=~VSDGDTFIM"
       Moveto [Codigo do Representante] TypeIn "~VSDGREPRESEN.."
       Moveto [Descricao da Destinacao] TypeIn "..VEND.."
       Moveto [Tipo Movimento Estoque ] TypeIn "S"
       Moveto [Situacao               ] TypeIn "Not C"
       {Ask} TypeIn "\\Sistemas\\Vendas\\"+Dir_Trab+"NotaCavl" Enter 
       Moveto [Codigo da Empresa      ] TypeIn "_cemp!" 
       Moveto [Numero do Documento    ] TypeIn "_ndoc!"
       Moveto [Serie do Documento     ] TypeIn "_sdoc!"
       Moveto [Nota                   ] Check 
       Do_It!
       ClearAll
       
       If VSDGOPAG=1 Or VSDGOPAG=2 Or VSDGOPAG=3 Or VSDGOPAG=4
          Then Processa()
          Else Processa2()
       EndIf   
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_0013a"
For wi From 1 to 13
    End Down
    [Ano        ]=Ano_Aux
    [Mes        ]=Mes_Aux
    [MesExt     ]=mesext(NumVal(Mes_Aux))
    [Total 1]=0
    [Total 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 PROCESSA()
ClearAll

View PrivDir()+"GR_0013a"
CoEdit "Answer"
Scan
   If IsBlank([Data de Emissao]) Then Loop EndIf
   Moveto PrivDir()+"GR_0013a"
   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->Nota]=Strval(VSDGOPAG) Then [Total 1]=[Total 1]+1 EndIf       
   [Total 2]=[Total 2]+1
   Moveto "Answer"
EndScan
Do_It!
ClearAll

EndProc

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

View PrivDir()+"GR_0013a"
CoEdit "Answer"
Scan
   If IsBlank([Data de Emissao]) Then Loop EndIf
   Moveto PrivDir()+"GR_0013a"
   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 VSDGOPAG=5 : If [Answer->Nota]="5" Or [Answer->Nota]="" Then [Total 1]=[Total 1]+1 EndIf  
      Case VSDGOPAG=6 : If [Answer->Nota]="1" Or [Answer->Nota]="2" Then [Total 1]=[Total 1]+1 EndIf   
      Case VSDGOPAG=7 : If [Answer->Nota]="3" Or [Answer->Nota]="4" Then [Total 1]=[Total 1]+1 EndIf  
      OtherWise       : [Total 1]=0
   EndSwitch         
   [Total 2]=[Total 2]+1
   Moveto "Answer"
EndScan
Do_It!
ClearAll

EndProc

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

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

VRTOTAL=0
VRTIPO=0
Mes=0

View PrivDir()+"GR_0013"
CoEdit "Answer"
Scan
   Moveto PrivDir()+"GR_0013"
   End Down
   [Mes    ]=SubStr(AlinhM(StrVal([Answer->Ano]),4),1,4)+"/"+Substr(mesext(NumVal([Answer->Mes])),1,3)
   [Total 1]=[Answer->Total 1]
   [Total 2]=[Answer->Total 2]
   VRTIPO   =VRTIPO +[Total 1]
   VRTOTAL  =VRTOTAL+[Total 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)
                     [Total 1     ]=0
                     [Total 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=8 Then
   {Ask} TypeIn PrivDir()+"GR_0013" Enter
   Moveto [Mes    ] Example "mm"
   Moveto [Total 2] Example "vr"
   {Ask} TypeIn PrivDir()+"GR_0014" Enter TypeIn "Fast Insert"
   Moveto [Mes    ] Example "mm"
   Moveto [Total 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_REPRESEN"      : Campo="TG_EMPRESA"
            Case Campo="TG_MESANO"        : Campo="TG_REPRESEN"
            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"  : Campo="TG_REPRESEN"
                    Case Campo="TG_REPRESEN" : Campo="TG_MESANO"
                    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()
Endswitch
Monta_Prompt(MSG_CAMPO,TECLA)
Return Temp
EndProc

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

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

While True
  ShowDialog "Evolu‡„o dos atendimentos 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 14 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 "Vendedor"  For "TG_REPRESEN"
    Accept @ 03,01 Width 07 "A4"  Picture "#*#" Tag "TG_REPRESEN"   To VSDGREPRESEN
    Accept @ 03,09 Width 25 "A20" Picture "!*!" Tag "TG_NOMREPR"    To VSDGNOMREPR

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

    Label  @ 00,35 "Op‡”es de Tipo: "  For "TG_OPAG"
    RadioButtons @ 01,35 Height 8 Width 25
     "~O~timo",
     "~B~om",
     "~R~egular",
     "~P~essimo",
     "~N~Ćo Avaliado",
     "~1~=Otimo/Bom",
     "~2~=Regular/Pessimo",
     "~A~tendimentos"
     Tag "TG_OPAG"
     To VSDGOPAG

    Pushbutton @ 10,22 width 11 "~O~k"      OK     Default Value "Ok" Tag "YES" To BottonValue
    Pushbutton @ 10,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 VSDGEMPRESA2="0000"
     Else VSDGEMPRESA2=VSDGEMPRESA
  EndIf
  
  If VSDGREPRESEN=""
     Then VSDGREPRESEN2="0000"
     Else VSDGREPRESEN2=VSDGREPRESEN
  EndIf
  
  Switch
    Case VSDGOPAG=1 :  VSDGNOMOPAG="OTIMO"
    Case VSDGOPAG=2 :  VSDGNOMOPAG="BOM"
    Case VSDGOPAG=3 :  VSDGNOMOPAG="REGULAR"
    Case VSDGOPAG=4 :  VSDGNOMOPAG="PESSIMO"
    Case VSDGOPAG=5 :  VSDGNOMOPAG="NAO AVALIADO"
    Case VSDGOPAG=6 :  VSDGNOMOPAG="OTIMO/BOM"
    Case VSDGOPAG=7 :  VSDGNOMOPAG="REGULAR/PESSIMO"
  EndSwitch

  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_0014"  PrivDir()+"GR_0014"
  Copy "\\Base\\Sistemas\\Vendas\\GR_0013a" PrivDir()+"GR_0013a"
  Copy "\\Base\\Sistemas\\Vendas\\GR_0013"  PrivDir()+"GR_0013"
  Empty(PrivDir()+"GR_0014")
  Empty(PrivDir()+"GR_0013a")
  Empty(PrivDir()+"GR_0013")

  AGUARDE("Aguarde, Gerando Dados")

  Cria_Registros()

  Monta_Dir_Trab(VSDGDTINI,VSDGDTFIM)
  While Novo_Dir_Trab()
      Seleciona() 
  EndWhile

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

  Processa3()

  GRAF_TIPO= "3"
  GRAF_TIT1 = "Atendimentos dos éltimos 13 Meses at‚ "+rTrim(mesext(NumVal(Substr(VSDGMESANO,1,2))))+"/"+Substr(VSDGMESANO,4,4)

  If VSDGOPAG=8 
      Then GRAF_TIT2= "Empresa: "+VSDGEMPRESA2+"-"+VSDGNOMEMP+",  Repr.: "+VSDGEMPRESA2+"-"+VSDGNOMREPR
           GRAF_TAB  = PrivDir()+"GR_0014"
      Else GRAF_TIT2= "Empresa: "+VSDGEMPRESA2+"-"+VSDGNOMEMP+",  Repr.: "+VSDGEMPRESA2+"-"+VSDGNOMREPR+ ", Tipo: "+VSDGNOMOPAG
           GRAF_TAB  = PrivDir()+"GR_0013"
           GRAF_LENG = "Y"
           GRAF_LENG1= VSDGNOMOPAG+" - "+StrVal(VRTIPO)
           GRAF_LENG2= "Atendimentos"+" - "+StrVal(VRTOTAL)
  EndIf  
  GRAF_LINX= "Valor"
  GRAF_LINY= "Mes"

  MONTAGRAFICO(GRAF_TAB)
  Delete PrivDir()+"GR_0014"
  Delete PrivDir()+"GR_0013a"
  Delete PrivDir()+"GR_0013"
  Delete "Answer"

EndWhile
ClearAll
Release Vars All
Release Procs All
