;** Rm_3061 ********************************************************************
;* Resumo de Vendas por Representante/Rateio (Cortador)                        *
;*******************************************************************************
;Autor           : Vise Informatica (VISERP)
;Desenvolvido    : 10/11/2005 por Anivel Augusto Rodrigues
;Ultima Alteracao: 09/02/2012 por Bruno Carvalho
;Motivo Alteracao: Add novo modelo de report para SYS_CODIGO_CLIENTE
;Ultima Alteracao: 10/07/2012 por Bruno Carvalho
;Motivo Alteracao: Adicionado filtro na ask do NotaI "Situacao_Item"
;Ultima Alteracao: 01/08/2012 por Bruno Carvalho
;Motivo Alteracao: Adicionado filtro para op‡Æo de relat¢rio "com rateio" e "sem rateio"
;Ultima Alteracao: 23/09/2013 por Andrey Fillipe
;Motivo Alteracao: Adicionado Representante Principal
;Ultima Alteracao: 19/03/2016 por Kamila Gomes
;Motivo Alteracao: Adicionado Filtro por Forma de Pagamento
;Ultima Alteracao: 12/12/2016 por Anivel Augusto Rodrigues
;Motivo Alteracao: Ajustes showdialog                   

;*******************************************************************************
;* Rotina p/ Selecionar Registros                                              *
;*******************************************************************************
Proc Seleciona()
ClearAll

Tabela2=Cria_Temp(PrivDir())
Copy "\\Sistemas\\Cadastro\\Represen" Tabela2
        
{Ask} TypeIn Tabela2 Enter
Moveto [Codigo do Representante] Example "codr"
Moveto [Nome Fantasia Represent] Check    

If VSDGOPRELANALSINT2="1"
   Then  {Ask} TypeIn "\\Sistemas\\Cadastro\\Represen" Enter
         Moveto [Codigo do Representante] Example "cdr"
         Moveto [Nome Fantasia Represent] Check TypeIn "as Nome Usuario"
         
        {Ask} TypeIn "\\Sistemas\\Vendas\\"+Dir_Trab+"NotaIC" Enter
        Moveto [Codigo da Empresa      ] Example "cemp"
        Moveto [Numero do Documento    ] Example "ndoc"
        Moveto [Serie do Documento     ] Example "sdoc"
        Moveto [Codigo do Representante] Check Example "cdr" TypeIn "!,~VSDGREPRESEN.. ,as Codigo Usuario"
EndIf

{Ask} TypeIn "\\Sistemas\\Vendas\\"+Dir_Trab+"NotaC" Enter
Moveto [Codigo da Empresa      ] Check Example "cemp" TypeIn ", ~VSDGEMPRESA.."
Moveto [Numero do Documento    ] Check Example "ndoc"
Moveto [Serie do Documento     ] Check Example "sdoc"
Moveto [Nome Fantasia Empresa  ] Check
Moveto [Tipo do Documento      ] Check
Moveto [Data de Emissao        ] Check TypeIn ">=~VSDGDTINI,<=~VSDGDTFIM"
Moveto [Codigo                 ] Check
Moveto [Razao Social           ] Check
Moveto [Descricao da Destinacao] TypeIn "..VEND.. or ..SERV.. or ..SAIDA.."
Moveto [Tipo Movimento Estoque ] TypeIn "S"
Moveto [Situacao               ] TypeIn "Not C" 
Moveto [Forma de Pagamento     ] TypeIn "~VSDGTPPAGTO.."

If VSDGOPRELANALSINT2="1"
   Then Moveto [Codigo do Representante] Check Example "codr"
   Else Moveto [Codigo do Representante] Check Example "codr" TypeIn ",~VSDGREPRESEN.."  
EndIf
        
{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 [Item do Documento      ] Check
Moveto [Codigo do Item         ] Check
Moveto [Descricao do Item      ] Check
Moveto [Unidade Fiscal         ] Check
Moveto [Quantidade             ] Check
Moveto [Preco Total            ] Check
Moveto [Valor Desconto Total   ] Check
Moveto [Situacao_Item          ] TypeIn "Not C"
Do_It!
ClearAll

Adiciona_Dados()

EndProc   ; (Seleciona)

;*******************************************************************************
;* Rotina adicionar dados na Db temporaria                                     *
;*******************************************************************************
Proc Adiciona_Dados()

If IsEmpty("Answer")
   Then Return
EndIf

{Ask} {Answer}
Moveto [Codigo da Empresa      ] Example "cemp"
Moveto [Nome Fantasia Empresa  ] Example "nemp"
If VSDGOPRELANALSINT2="1"
   Then Moveto [Codigo Usuario ] Example "crep"
        Moveto [Nome Usuario   ] Example "nrep"
EndIf  
Moveto [Numero do Documento    ] Example "nof"
Moveto [Tipo do Documento      ] Example "tpd"
Moveto [Data de Emissao        ] Example "dat"
Moveto [Codigo                 ] Example "ccli"
Moveto [Razao Social           ] Example "ncli"
Moveto [Codigo do Item         ] Example "cite"
Moveto [Descricao do Item      ] Example "dite"
Moveto [Unidade Fiscal         ] Example "uite"
Moveto [Quantidade             ] Example "qtde"
Moveto [Preco Total            ] Example "ptot"
Moveto [Valor Desconto Total   ] Example "vdes"
Moveto [Codigo do Representante] Example "codrep"
Moveto [Nome Fantasia Represent] Example "nomrep"

{Ask} TypeIn Tabela Enter TypeIn "Fast Insert"
Moveto [Codigo da Empresa      ] Example "cemp"
Moveto [Nome Fantasia Empresa  ] Example "nemp"
If VSDGOPRELANALSINT2="1"
   Then Moveto [Codigo_Usuario ] Example "crep"
        Moveto [Nome_Usuario   ] Example "nrep"
EndIf
Moveto [Numero do Documento    ] Example "nof"
Moveto [Tipo do Documento      ] Example "tpd"
Moveto [Data de Emissao        ] Example "dat"
Moveto [Codigo                 ] Example "ccli"
Moveto [Razao Social           ] Example "ncli"
Moveto [Codigo do Item         ] Example "cite"
Moveto [Descricao do Item      ] Example "dite"
Moveto [Unidade Fiscal         ] Example "uite"
Moveto [Vendas_Quantidade      ] Example "qtde"
Moveto [Vendas_Preco Total     ] Example "ptot" TypeIn"-" Example "vdes"
Moveto [Codigo do Representante] Example "codrep"
Moveto [Nome Fantasia Represent] Example "nomrep"
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_REPRESEN"      : Campo="TG_EMPRESA"
            Case Campo="TG_TPPAGTO"       : Campo="TG_REPRESEN"
            Case Campo="TG_DTINI"         : Campo="TG_TPPAGTO"
            Case Campo="TG_DTFIM"         : Campo="TG_DTINI"
            Case Campo="TG_OPRELANALSINT2": Campo="TG_DTFIM"
            Case Campo="TG_OPRELCODALF"   : Campo="TG_OPRELANALSINT2"
            Case Campo="TG_OPRELANALSINT" : Campo="TG_OPRELCODALF"
            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_TPPAGTO"
                    Case Campo="TG_TPPAGTO"       : Campo="TG_DTINI"
                    Case Campo="TG_DTINI"         : Campo="TG_DTFIM"
                    Case Campo="TG_DTFIM"         : Campo="TG_OPRELANALSINT2"
                    Case Campo="TG_OPRELANALSINT2" : 
                         VSDGOPRELANALSINT2=ControlValue("TG_OPRELANALSINT2")                       
                         If IsBlank(VSDGOPRELANALSINT2)
                            Then VSDGOPRELANALSINT2="1"
                         EndIf
                         Switch
                           Case VSDGOPRELANALSINT2="1" : VSDGNOMOPRELANALSINT2 = "Com Rateio"
                           Case VSDGOPRELANALSINT2="2" : VSDGNOMOPRELANALSINT2 = "Sem Rateio"
                         EndSwitch 
                         Campo="TG_OPRELCODALF"
                    Case Campo="TG_OPRELCODALF"   : Campo="TG_OPRELANALSINT"
                    Case Campo="TG_OPRELANALSINT" : Campo="YES"
                  EndSwitch
          EndIf
          RefreshDialog
   EndSwitch
EndIf
Switch
  Case Campo="TG_DTFIM":
     MSG_CAMPO="Informe a Data Final"
  Case Campo="TG_OPRELANALSINT2":
     MSG_CAMPO="Op‡„o: 1=Com Rateio  2=Sem Rateio"      
Endswitch
Monta_Prompt(MSG_CAMPO,TECLA)
Return Temp
EndProc

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

VSDGEMPRESA          = ""
VSDGDTINI            = BlankDate()
VSDGDTFIM            = BlankDate()
VSDGOPRELCODALF      = ""
VSDGREPRESEN         = ""
VSDGTPPAGTO          = ""
VSDGOPRELANALSINT    = ""
VSDGNOMOPRELANALSINT = ""
VSDGOPRELANALSINT2   = "" 
VSDGNOMOPRELANALSINT2= ""

While True

  ShowDialog "Vendas por Rateio" 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 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 "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 "Forma de Pagamento"  For "TG_TPPAGTO"
    Accept @ 05,03 Width 05 "A2"  Picture "!*!" Tag "TG_TPPAGTO"    To VSDGTPPAGTO
    Accept @ 05,09 Width 25 "A20" Picture "!*!" Tag "TG_NOMTPPG"    To VSDGNOMTPPAGTO

    Label  @ 06,00 "Per¡odo Referente de" For "TG_DTINI"
    Accept @ 07,03 Width 13 "D"  Picture "#[#];.##;.##[##]" Tag "TG_DTINI" To VSDGDTINI
    Label  @ 07,16 "at‚" For "TG_DTFIM"
    Accept @ 07,21 Width 13 "D"  Picture "#[#];.##;.##[##]" Tag "TG_DTFIM" To VSDGDTFIM

    Label  @ 00,35 "Op‡„o de Rateio" For "TG_OPRELANALSINT2"
    Accept @ 01,39 Width 04 "A1"  Picture "1,2" Tag "TG_OPRELANALSINT2" To VSDGOPRELANALSINT2
    Accept @ 01,44 Width 25 "A25"  Picture "!*!" Tag "TG_NOMOPRELANASINT2" To VSDGNOMOPRELANALSINT2

    Label  @ 02,35 "Op‡„o de Ordem" For "TG_OPRELCODALF"
    Accept @ 03,39 Width 04 "A1"  Picture "1,2" Tag "TG_OPRELCODALF" To VSDGOPRELCODALF
    Accept @ 03,44 Width 25 "A25"  Picture "!*!" Tag "TG_NOMOPRELCODALF" To VSDGNOMOPRELCODALF

    Label  @ 04,35 "Op‡„o do Relat¢rio" For "TG_OPRELANALSINT"
    Accept @ 05,39 Width 04 "A1"  Picture "1,2" Tag "TG_OPRELANALSINT" To VSDGOPRELANALSINT
    Accept @ 05,44 Width 25 "A25"  Picture "!*!" Tag "TG_NOMOPRELANASINT" To VSDGNOMOPRELANALSINT

    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 IsBlank(VSDGDTFIM)
     Then Mensagem("Data Final Invalida !")
          Loop
  EndIf
  
  DISPOSITIVO()
  If OK=""  Then Loop EndIf

  AGUARDE("Aguarde, gerando o relat¢rio")
  Tabela=Cria_Temp(PrivDir())
  Empty "\\Base\\Sistemas\\Vendas\\RM_3107"
  Copy "\\Base\\Sistemas\\Vendas\\RM_3107" Tabela

  Monta_Dir_Trab(VSDGDTINI,VSDGDTFIM)
  While Novo_Dir_Trab()
    Seleciona()
  EndWhile
  
  If IsEmpty(Tabela)
     Then MENSAGEM("N„o existem registros a processar")
          Delete Tabela
          Loop
  EndIf

  If SYS_CODIGO_CLIENTE="0298"
     Then REP1="12"
          REP2="13" 
     Else REP1="8"
          REP2="9"
  EndIf   

  If VSDGOPRELCODALF="1"
    Then If VSDGOPRELANALSINT="1"
            Then IMPRIREL(Tabela,REP1)
            Else IMPRIREL(Tabela,"10")
         EndIf
    Else If VSDGOPRELANALSINT="1"
            Then IMPRIREL(Tabela,REP2)
            Else IMPRIREL(Tabela,"11")
         EndIf
  EndIf

  Delete Tabela

EndWhile
ClearAll
Release Vars All
Release Procs All
