;** Rm_0076 ********************************************************************
;*  Relacao de Notas Canceladas Rel N§ 87                                      *
;*******************************************************************************
;Autor           : Vise Informatica (VISERP)
;Desenvolvido    : 22/07/2004 por Nikolas M. Mateus
;Ultima Alteracao: 23/01/2006 por Nikolas M. Mateus
;Motivo Alteracao: Filtrar Somentes Notas Canceladas
;Ultima Alteracao: 03/02/2008 por Nikolas M. Mateus
;Motivo Alteracao: Ajustes para pegar retorno de manifesto
;Ultima Alteracao: 12/12/2016 por Anivel Augusto Rodrigues
;Motivo Alteracao: Ajustes showdialog                   
;Ultima Alteracao: 25/05/2018 por Bruno Carvalho
;Motivo Alteracao: Adicionado Justificativa

;*******************************************************************************
;* Rotina para Selecionar Registros                                            *
;*******************************************************************************

Proc Seleciona()
If IsTable("\\Sistemas\\Vendas\\"+Dir_Trab+"NotaC")
   Then {Ask} TypeIn "\\Sistemas\\Vendas\\"+Dir_Trab+"NotaC" Enter Check
        Moveto [Codigo da Empresa        ] TypeIn "~VSDGEMPRESA.."
        Moveto [Codigo do Representante  ] TypeIn "~VSDGREPRESEN.."
        Moveto [Data de Emissao          ] TypeIn ">=~VSDGDTINI,<=~VSDGDTFIM"
        ;Moveto [Descricao da Destinacao ] TypeIn "..VEND.."
        ;Moveto [Tipo Movimento Estoque  ] TypeIn "S"
        Moveto [Situacao                 ] TypeIn "C"
        ;Moveto [Tipo de Pagamento       ] TypeIn "V or P"
        Do_It!
        ClearAll
EndIf
EndProc   ;  (Seleciona)

;*******************************************************************************
;* Rotina p/ Ajustar a Justificativa                                           *
;*******************************************************************************
Proc Ajusta_Justificativa()

CRIAR_TABELA = False
Adiciona_Campo(Tabela,"Justificativa(A255)")
ClearAll

Coedit Tabela
Scan For Search("[**",[Observacao na Nota Fiscal])>0
  Barra(NRecords(Tabela), RecNo())
  Aguarde("Aguarde, Ajustando Dados "+BARRA_STRING)
  PosI = Search("[**",[Observacao na Nota Fiscal])
  PosF = Search("**]",[Observacao na Nota Fiscal])
  If PosI>0 And PosF>0
     Then ;Exemplo: CANCELADO POR: 0007 GERENTE ADMINISTRATIVO 11.12.2017 16:24 DADOS INCORRETOS**]
          ;NÆo esta sendo exibido o CANCELADO POR, DATA E HORA, apenas o descritivo
          
          JUSTIFICATIVA = SubStr([Observacao na Nota Fiscal],PosI+3,PosF-2)
          PosI = Search(":",JUSTIFICATIVA)
          JUSTIFICATIVA = SubStr(JUSTIFICATIVA,PosI+1,Len(JUSTIFICATIVA))
          PosI = Search(":",JUSTIFICATIVA)
          JUSTIFICATIVA = SubStr(JUSTIFICATIVA,PosI+4,Len(JUSTIFICATIVA))
          PosF = Search("**]",JUSTIFICATIVA)
          If PosF>0
             Then JUSTIFICATIVA = SubStr(JUSTIFICATIVA,1,PosF-1)
          EndIf   
          [Justificativa] = Ltrim(Rtrim(JUSTIFICATIVA))
  EndIf     
EndScan
Do_it!
ClearAll

EndProc ;Ajusta_Justificativa()

;*******************************************************************************
;* 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_OPRELANALSINT" : Campo="TG_DTFIM"
            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_OPRELANALSINT"
                    Case Campo="TG_OPRELANALSINT" :Campo="YES"
                  EndSwitch
          EndIf
          RefreshDialog
   EndSwitch
EndIf
Switch
  Case Campo="TG_DTFIM":
     MSG_CAMPO="Informe a Data Final"
Endswitch
Monta_Prompt(MSG_CAMPO,TECLA)
Return Temp
EndProc

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

VSDGEMPRESA =""
VSDGREPRESEN=""
VSDGDTINI   =BlankDate()
VSDGDTFIM   =BlankDate()

While True

  ShowDialog "Relacao de Vendas a Vista e a Prazo" 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

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

    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

  DISPOSITIVO()
  If OK="" Then Loop EndIf

  AGUARDE("Aguarde, gerando o relat¢rio")
  Tabela=Cria_Temp("\\Sistemas\\Vendas\\")

  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
  Ajusta_Justificativa()

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

  If VSDGOPRELANALSINT="1"
     Then CopyReport "\\Base\\Sistemas\\Vendas\\Rm_1001" "5" Tabela "R"
     Else CopyReport "\\Base\\Sistemas\\Vendas\\Rm_1001" "4" Tabela "R"
  EndIf

  IMPRIREL(Tabela,"R")

  Delete Tabela

EndWhile
ClearAll
Release Vars All
Release Procs All
