;** Rm_0022  *******************************************************************
;* Relacao de Of Por Vendedor                                                  *
;*******************************************************************************
;Autor           : Vise Informatica (VISERP)
;Desenvolvido    : 12/11/2012 por Kamila Gomes            
;Ultima Alteracao: 27/10/2016 por Rafael Matos
;Motivo Alteracao: Ajustes Layout para padroniza‡Æo                              
;Ultima Alteracao: 10/12/2016 por Anivel Augusto Rodrigues
;Motivo Alteracao: Ajustes showdialog                   
;Ultima Alteracao: 27/12/2016 por Anivel Augusto Rodrigues
;Motivo Alteracao: Ajustes showdialog                   
;Ultima Alteracao: 27/12/2019 por Bruno Carvalho
;Motivo Alteracao: Removido o caracter "!" ao linkar no campo [Numero da OF] pela tabela "FaturaI", onde gerava v rios registros em branco 

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

{Ask} {FaturaC} Check
Moveto [Numero da OF           ] Example "Docto!"
Moveto [Codigo da Empresa      ] TypeIn "~VSDGEMPRESA.."
Moveto [Codigo do Representante] TypeIn "~VSDGREPRESEN.."
Moveto [Codigo da Destinacao   ] TypeIn "~VSDGDESTIN.."
If VSDGTPDATA="1"
  Then Moveto [Data de Emissao        ] TypeIn ">=~VSDGDTINI,<=~VSDGDTFIM"
  Else Moveto [Data_Inclusao          ] TypeIn ">=~VSDGDTINI,<=~VSDGDTFIM"
EndIf  
Moveto [Numero_Carregamento    ] TypeIn "~VSDGNCARREG.."

If VSDGCSITENS="C"
   Then {Ask} {FaturaI} Check
        Moveto [Numero da OF           ] Example "Docto"
EndIf
Do_It!
ClearAll

EndProc   ; (Seleciona)

;*******************************************************************************
;* Rotina Ajusta Dados                                                         *
;*******************************************************************************
Proc Ajusta_Dados()
ClearAll

If VSDGCSITENS="C"
   Then Sort "Answer" On "Codigo da Empresa", "Numero da OF","Item da OF"
   Else Sort "Answer" On "Codigo da Empresa", "Numero da OF"
EndIf

NDOCTO="****"
CoEdit "Answer"
Scan
 If [Numero da OF]<>NDOCTO 
    Then NDOCTO=[Numero da OF       ]
    Else [Valor Nota Fiscal]=BlankNum()
 EndIf
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_REPRESEN"      : Campo="TG_EMPRESA"
            Case Campo="TG_DESTIN"        : Campo="TG_REPRESEN"
            Case Campo="TG_DTINI"         : Campo="TG_DESTIN"
            Case Campo="TG_DTFIM"         : Campo="TG_DTINI"
            Case Campo="TG_TPDATA"        : Campo="TG_DTFIM"    
            Case Campo="TG_CSITENS"       : Campo="TG_TPDATA"
            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_DESTIN"
                    Case Campo="TG_DESTIN"   : Campo="TG_DTINI"
                    Case Campo="TG_DTINI"    : Campo="TG_DTFIM"
                    Case Campo="TG_DTFIM"    : Campo="TG_TPDATA"
                    Case Campo="TG_TPDATA"   :
                                VSDGTPDATA=ControlValue("TG_TPDATA")
                                If IsBlank(VSDGTPDATA)
                                     Then VSDGTPDATA="1"
                                EndIf

                                If VSDGTPDATA="1"
                                   Then VSDGNOMTPDATA="DATA DE EMISSAO"
                                   Else VSDGNOMTPDATA="DATA DE CRIACAO"
                                EndIf
                                Campo="TG_CSITENS"
                    Case Campo="TG_CSITENS" : Campo="YES"
                  EndSwitch
          EndIf
          RefreshDialog
   EndSwitch
EndIf
Switch
  Case Campo="TG_TPDATA":
     MSG_CAMPO="1 = EMISSAO  2 = CRIACAO"
Endswitch
Monta_Prompt(MSG_CAMPO,TECLA)
Return Temp
EndProc

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

VSDGEMPRESA =""
VSDGCSITENS =""
VSDGDTINI   =BlankDate()
VSDGDTFIM   =BlankDate()
VSDGNCARREG =""

While True

  ShowDialog "Rela‡„o Ordem de Fatuamento emitidas" 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 "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 "Destinacao"  For "TG_DESTIN"
    Accept @ 05,01 Width 07 "A3"  Picture "#*#" Tag "TG_DESTIN"    To VSDGDESTIN
    Accept @ 05,09 Width 25 "A30" Picture "!*!" Tag "TG_NOMDESTIN" To VSDGNOMDESTIN

    Label  @ 00,35 "Op‡äes de Datas" For "TG_TPDATA"
    Accept @ 01,39 Width 04 "A1"  Picture "1,2"  Tag "TG_TPDATA"     To VSDGTPDATA
    Accept @ 01,44 Width 25 "A18" Picture "!*!"  Tag "TG_NOMTPDATA" To VSDGNOMTPDATA

    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  @ 02,35 "Op‡„o p/ Itens" For "TG_CSITENS"
    Accept @ 03,39 Width 04 "A1"  Picture "S,C" Tag "TG_CSITENS" To VSDGCSITENS
    Accept @ 03,44 Width 25 "A20"  Picture "!*!" Tag "TG_NOMCSITENS" To VSDGNOMCSITENS

    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")
  Seleciona()
  If IsEmpty("Answer")
     Then MENSAGEM("N„o existem registros a processar")
          Delete "Answer"
          Loop
  EndIf
  Ajusta_Dados()

  If VSDGCSITENS="C"
     Then CopyReport "\\Base\\Sistemas\\Vendas\\Rm_2002" "2" "Answer" "R"
     Else CopyReport "\\Base\\Sistemas\\Vendas\\Rm_2002" "3" "Answer" "R"
  EndIf

  IMPRIREL("Answer","R")

  Delete "Answer"

EndWhile
ClearAll
Release Vars All
Release Procs All

