;** Rm_0220 ********************************************************************
;*  Curva ABC por Representante/Cliente                                        *
;*******************************************************************************
;Autor           : Vise Informatica (VISERP)
;Desenvolvido    : 06/11/1996 por Anivel Augusto Rodrigues
;Ultima Alteracao: 08/09/2004 por Jos‚ Luˇs Ameno da Silva
;Motivo Alteracao: PrivDir

;*******************************************************************************
;* Rotina para Selecionar Registros                                            *
;*******************************************************************************
Proc Seleciona()
ClearAll

If IsTable("\\Sistemas\\Vendas\\"+Dir_Trab+"NotaC")
   Then {Ask} TypeIn "\\Sistemas\\Vendas\\"+Dir_Trab+"NotaC" Enter
        Moveto [Data de Emissao        ] TypeIn ">=~VSDGDTINI, <=~VSDGDTFIM"
        Moveto [Codigo da Empresa      ] TypeIn "~VSDGEMPRESA.."
        Moveto [Codigo                 ] Check
        Moveto [Nome Fantasia EmitDest ] Check
        Moveto [Codigo do Representante] Check TypeIn "~VSDGREPRESEN.."
        Moveto [Nome Fantasia Represent] Check
        Moveto [Descricao da Destinacao] TypeIn "..VEND.. OR ..SAIDA.."
        Moveto [Tipo Movimento Estoque ] TypeIn "S"
        Moveto [Situacao               ] TypeIn "Not C"
        Moveto [Valor Nota Fiscal      ] TypeIn "Calc Sum as Valor"
        Do_It!
        ClearAll

        Add "Answer" Tabela_A
        ClearAll
EndIf

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        ] TypeIn " >=~VSDGDTINI,<=~VSDGDTFIM  "
        Moveto [Codigo                 ] Check
        Moveto [Nome Fantasia EmitDest ] Check
        Moveto [Codigo do Representante] Check TypeIn "~VSDGREPRESEN.."
        Moveto [Nome Fantasia Represent] Check
        Moveto [Tipo de Operacao       ] TypeIn "D"
        Moveto [Valor Nota Fiscal      ] TypeIn "Calc Sum as Valor"
        Do_It!
        ClearAll

        {Ask} {Answer} TypeIn "Fast"
        Moveto [Valor] Example "VrE" TypeIn " , Changeto " Example "VrE" TypeIn "*-1"
        Do_It!
        ClearAll

        Add "Answer" Tabela_A
        ClearAll
EndIf

If IsTable("\\Sistemas\\Vendas\\"+Dir_Trab+"NotaC")
   Then {Ask} TypeIn "\\Sistemas\\Vendas\\"+Dir_Trab+"NotaC" Enter
        Moveto [Codigo da Empresa      ] TypeIn "~VSDGEMPRESA.."
        Moveto [Data de Emissao        ] TypeIn " >=~VSDGDTINI,<=~VSDGDTFIM  "
        Moveto [Codigo                 ] Check
        Moveto [Nome Fantasia EmitDest ] Check
        Moveto [Codigo do Representante] Check TypeIn "~VSDGREPRESEN.."
        Moveto [Nome Fantasia Represent] Check
        Moveto [Descricao da Destinacao] TypeIn "..DEVOL.."
        Moveto [Tipo Movimento Estoque ] TypeIn "E"
        Moveto [Situacao               ] TypeIn "Not C"
        Moveto [Valor Nota Fiscal      ] TypeIn "Calc Sum as Valor"
        Do_It!
        ClearAll

        {Ask} {Answer} TypeIn "Fast"
        Moveto [Valor] Example "VrE" TypeIn " , Changeto " Example "VrE" TypeIn "*-1"
        Do_It!
        ClearAll

        Add "Answer" Tabela_A
        ClearAll
EndIf
EndProc  ; (Seleciona)

;*******************************************************************************
;* Rotina p/ processar Db temporaria                                           *
;*******************************************************************************
Proc Processa()

{Ask} TypeIn Tabela_A enter
Moveto [Codigo                 ] Check
Moveto [Nome Fantasia EmitDest ] Check
Moveto [Codigo do Representante] Check
Moveto [Nome Fantasia Represent] Check
Moveto [Valor                  ] TypeIn "Calc Sum as Valor"
Do_It!
ClearAll

{Ask} {Answer}
Moveto [Codigo                 ] Example "CODIGO"
Moveto [Nome Fantasia EmitDest ] Example "NOME"
Moveto [Codigo do Representante] Example "CODIGOR"
Moveto [Nome Fantasia Represent] Example "NOMER"
Moveto [Valor                  ] Example "VALOR"

{Ask} TypeIn Tabela Enter TypeIn "Fast Insert"
Moveto [Codigo                 ] Example "CODIGO"
Moveto [Nome Fantasia EmitDest ] Example "NOME"
Moveto [Codigo do Representante] Example "CODIGOR"
Moveto [Nome Fantasia Represent] Example "NOMER"
Moveto [Valor                  ] Example "VALOR"
Do_It!
ClearAll

{Ask} TypeIn Tabela Enter TypeIn "Fast"
Moveto [Valor ] TypeIn ">=~VSDGVALORA"
Moveto [Grupo ] TypeIn "Changeto A"
Ctrlhome Down
TypeIn "Fast"
Moveto [Valor ] TypeIn ">=~VSDGVALORB, <~VSDGVALORA"
Moveto [Grupo ] TypeIn "Changeto B"
Ctrlhome Down
TypeIn "Fast"
Moveto [Valor ] TypeIn "<~VSDGVALORB"
Moveto [Grupo ] TypeIn "Changeto C"
Do_It!
ClearAll

{Ask} TypeIn Tabela Enter
Moveto [Codigo do Representante] Check
Moveto [Nome Fantasia Represent] Check
Moveto [Valor                  ] TypeIn "Calc Sum As Valor"
Do_It!
ClearAll

Add "Answer" Tabela_B
ClearAll

Sort Tabela On "Codigo do Representante", "Grupo", "Valor" D

Coedit Tabela_B,Tabela
CODR="****"
Scan
   If [Codigo do Representante]<>CODR
      Then VRA =0
           CODR=[Codigo do Representante]
           Moveto Tabela_B
           Moveto [Codigo do Representante]
           Locate CODR
           If RetVal
              Then VRR=[Valor]
              Else VRR=0
           EndIf
           Moveto Tabela
   EndIf
   VRA=VRA+[Valor]
   [Indice          ]=NumVal((([Valor]*100)/VRR))
   [Indice Acumulado]=NumVal(((VRA*100)/VRR))
EndScan
Do_It!
ClearAll
EndProc    ; Processa

;*******************************************************************************
;* 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_DTINI"         : Campo="TG_REPRESEN"
            Case Campo="TG_DTFIM"         : Campo="TG_DTINI"
            Case Campo="TG_VALORA"        : Campo="TG_DTFIM"
            Case Campo="TG_VALORB"        : Campo="TG_VALORA"
            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_DTINI"
                    Case Campo="TG_DTINI"   :Campo="TG_DTFIM"
                    Case Campo="TG_DTFIM"   :Campo="TG_VALORA"
                    Case Campo="TG_VALORA":
                         VSDGVALORA=ControlValue("TG_VALORA")
                         If Not Isblank(VSDGVALORA)
                            Then Campo="TG_VALORB"
                         EndIf
                    Case Campo="TG_VALORB":
                         VSDGVALORB=ControlValue("TG_VALORB")
                         If Not Isblank(VSDGVALORB)
                            Then If VSDGVALORB > VSDGVALORA
                                    Then Mensagem("Valor B maior que Valor A !")
                                    Else Campo="YES"
                                 EndIf
                         EndIf
                  EndSwitch
          EndIf
          RefreshDialog
   EndSwitch
EndIf
Switch
  Case Campo="TG_DTFIM":
     MSG_CAMPO="Informe a Data Final"
  Case Campo     = "TG_VALORA":
       MSG_CAMPO = "Entre com o Valor inicial o Grupo [A]"
  Case Campo     = "TG_VALORB":
       MSG_CAMPO = "Entre com o Valor inicial o Grupo [B]"
Endswitch
Monta_Prompt(MSG_CAMPO,TECLA)
Return Temp
EndProc


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

VSDGEMPRESA =""
VSDGREPRESEN=""
VSDGVALORA  =BlankNum()
VSDGVALORB  =BlankNum()
VSDGDTINI   =BlankDate()
VSDGDTFIM   =BlankDate()

While True

  ShowDialog "Curva ABC por Representante/Cliente" Proc "SD_Dialog"
  Trigger "OPEN","ARRIVE"
  Key "F1","ENTER","ESC","TAB",
      -104, -105, -106, -107, -108, -109, -110, -111, -112, -113, -139, -140  
   @ 05,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 "Representante"  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 "Perˇodo Referente de" For "TG_DTINI"
    Accept @ 05,03 Width 13 "D"  Picture "#[#];.##;.##[##]" Tag "TG_DTINI" To VSDGDTINI
    Label  @ 05,16 "at‚" For "TG_DTFIM"
    Accept @ 05,21 Width 13 "D"  Picture "#[#];.##;.##[##]" Tag "TG_DTFIM" To VSDGDTFIM


           @ 00,36 ?? "Intervalo de Valores para"    
    Label  @ 01,35 "Grupo [A]>="                  For "TG_VALORA"
    Accept @ 01,47 Width 14 "$" Tag "TG_VALORA"   To VSDGVALORA

    Label  @ 02,35 "Grupo [B]>="                  For "TG_VALORB"
    Accept @ 02,47 Width 14 "$" Tag "TG_VALORB"   To VSDGVALORB
           @ 02,61 ?? "<[A]"

           @ 03,36 ?? "Grupo [C]< [B]"

    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 Usuario_Vendedor() Then Loop EndIf  

  If IsBlank(VSDGDTFIM)
     Then Mensagem("Data Final Invalida !")
          Loop
  EndIf

  If Isblank(VSDGVALORA)
     Then Mensagem("Valor Grupo A Invalido !")
          Loop
  EndIf

  If Isblank(VSDGVALORB)
     Then Mensagem("Valor Grupo B Invalido !")
          Loop
  EndIf

  DISPOSITIVO()
  If OK="" Then Loop EndIf

  AGUARDE("Aguarde, Gerando o Relat˘rio")
  Tabela=Cria_Temp(PrivDir())
  Empty "\\Base\\Sistemas\\Vendas\\Rm_0220"
  Copy "\\Base\\Sistemas\\Vendas\\Rm_0220" Tabela
  Tabela_A=Cria_Temp(PrivDir())
  Empty "\\Base\\Sistemas\\Vendas\\Rm_0220A"
  Copy "\\Base\\Sistemas\\Vendas\\Rm_0220A" Tabela_A
  Tabela_B=Cria_Temp(PrivDir())
  Empty "\\Base\\Sistemas\\Vendas\\Rm_0220B"
  Copy "\\Base\\Sistemas\\Vendas\\Rm_0220B" Tabela_B

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

  If IsEmpty(Tabela_A)
     Then MENSAGEM("N„o existem registros a processar")
          Delete Tabela_A
          Delete Tabela_B
          Delete Tabela
          Loop
  EndIf
  Processa()

  IMPRIREL(Tabela,"R")

  Delete Tabela_A
  Delete Tabela_B
  Delete Tabela

EndWhile
ClearAll
Release Vars All
Release Procs All

