;** RM_1002 ********************************************************************
;*  Rela‡„o de Pedidos - VisePedidos (Importa‡Ćo)                              *
;*******************************************************************************
;Autor: Visao/Vise
;Desenvolvido    : 08/06/2011 por Bruno Carvalho 
;Ultima Alteracao: 14/12/2016 por Anivel Augusto Rodrigues
;Motivo Alteracao: Ajustes showdialog                   
;Ultima Alteracao: 18/05/2020 por Bruno Carvalho
;Motivo Alteracao: Alterado CopyReport "...\\Vendas\\PedidoC" p/ "...\\Vendas\\RM_0041"

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

{Ask} {PedidoC} Check
Moveto [Codigo da Empresa      ] TypeIn "~VSDGEMPRESA.."
Moveto [Codigo do Representante] TypeIn "~VSDGREPRESEN.."
Moveto [Data_Inclusao          ] TypeIn ">=~VSDGDTINI, <=~VSDGDTFIM"
Do_It!

For i From 1 To 2
  {Ask} {Answer} TypeIn "Fast Delete"
  If i=1
     Then Moveto [Data_Inclusao          ] TypeIn "~VSDGDTINI"
          Moveto [Hora_Inclusao          ] TypeIn "<~VSDGHRINI"
     Else Moveto [Data_Inclusao          ] TypeIn "~VSDGDTFIM"
          Moveto [Hora_Inclusao          ] TypeIn ">~VSDGHRFIM"
  EndIf
  Do_It!
  ClearAll
EndFor

If Not IsEmpty("Answer")
   Then Coedit "Answer"
        While True
          If [Numero do Pedido]="" And [Numero Pedido Origem]="" 
             Then QuitLoop 
          EndIF
          If Substr([Numero Pedido Origem],5,1)<>"."
             Then Del
                  Up
                  Loop      
          EndIF
          Skip
          If EOT() Then QuitLoop EndIf
        EndWhile 
        Do_It!
        ClearAll

        {Tools} {Copy} {JustFamily} {PedidoC} {Answer} {Replace}
        Sort "Answer" On "Numero do Pedido"
EndIf 

EndProc

;*******************************************************************************
;* Seleciona Registros para Carta de Cobranca                                  *
;*******************************************************************************
Proc Processa()

Edit "Answer"
SetHighLight Current Record 23 Field 31
{Image} {KeepSet}
Do_It!

CoEdit "Answer"
PickForm "5"
While True
  CHAVE()
  If CHAVE=9
     Then QuitLoop
  EndIf
EndWhile
Do_It!
ClearAll
EndProc

;*******************************************************************************
;* Seleciona Titulos para Baixa
;*******************************************************************************
Proc CHAVE()
While True
  End
  Down
  Moveto [Numero do Pedido]
  While True

    Prompt " Esc=Sair  F6=Imprimir  F7=Paginar  "

    Wait Field
         Until "ESC","ENTER","F7","HOME","UP","PGUP","F6"

    Switch
    Case RetVal="F6":
         Del
         IMPRIME()
         QuitLoop
    Case RetVal="F7" Or RetVal="UP" Or RetVal="PGUP" Or RetVal="HOME": 
         CHAVE=0
         While True
           PAGINA("Answer","MR")
           If EDITAR="S"
              Then Mensagem("Altera‡„o n„o permitida !")
                   UnLockRecord
              Else If EXCLUIR="S"
                      Then CONFIRMA()
                           If OK="S"
                              Then DEL
                              Else UnLockRecord
                           EndIf
                      Else QuitLoop
                   EndIf
           EndIf
         EndWhile
         QuitLoop
    Case Retval="ESC":
         CHAVE=9
         Return    
    EndSwitch
  EndWhile
EndWhile
EndProc

;*******************************************************************************
;* Rotina De Impressao                                                         *
;*******************************************************************************
Proc IMPRIME()
Private OPCAOC,OPCAON

MenuVar="Relacao"

Echo Normal
Echo Off
ShowPopup " Relatorio " CENTERED
   "~R~ela‡„o" : " Imprime a rela‡„o dos pedidos importados do VisePedidos" :"Relacao",
   "~P~edidos" : " Imprime os pedidos em modo Grade Gerencial/Expedi‡„o"    :"Pedidos"
EndMenu
To MenuVar

Echo off

If Not RetVal or MenuVar="Esc"
    Then Return
EndIf

If MenuVar="Pedidos" 
   Then Opcaoc="Gerencial"

        Echo Normal
        Echo Off
        ShowPopup " Relatorio " CENTERED
           "~G~erencial" : " Imprime Modelo Gerencial"    :"Gerencial",
           "~E~xpedi‡„o" : " Imprime Modelo p/ Expedi‡„o" :"Expedi‡„o",
           "~A~mbos"     : " Imprime Ambos os Modelos"    :"Ambos"
        EndMenu
        To Opcaoc
        Echo off

        If Not RetVal or Opcaoc="Esc"
            Then Return
        EndIf
        
        Dispositivo()

        If OK="" 
           Then Return
        EndIf

        UnLockRecord
        Do_It!

        Tabela=Cria_Temp(PrivDir())
        Copy "Answer" Tabela

        If IsTable("Ped_Temp")
           Then Delete "Ped_Temp"
        EndIf

        Rename "Answer" "Ped_Temp"

        View Tabela

        While True
          PEDIDO = [Numero do Pedido]
              
          For L From 1 To NUMCOPIA
            If Opcaoc="Ambos" Or Opcaoc="Gerencial"
               Then Play "PDW0222G"
            EndIf
            If Opcaoc="Ambos" Or Opcaoc="Expedi‡„o"
               Then Play "PDW0222S"
            EndIf
          EndFor

          Moveto Tabela
          Skip
          If Eot() Then Quitloop EndIf
        EndWhile
        Do_It!
        ClearAll

        Delete Tabela   
        Rename "Ped_Temp" "Answer"

   Else Dispositivo()
        If OK="" 
           Then Return 
        EndIf

        UnLockRecord
        Do_It!
                
        If IsTable("Ped_Temp")
           Then Delete "Ped_Temp"
        EndIf

        Rename "Answer" "Ped_Temp"
        
        {Ask} TypeIn "Ped_Temp" Enter Check
        Moveto [Numero do Pedido         ] Example "NPed" 
        Moveto [Comissao do Representante] Check
        Moveto [Taxa de Desconto         ] Check
        {Ask} {PedidoG}
        Moveto [Numero do Pedido         ] Example "NPed"
        Moveto [Quantidade               ] TypeIn "Calc Sum as \"Comissao do Representante\""
        Moveto [Quantidade Faturada      ] TypeIn "Calc Sum as \"Taxa de Desconto\""
        Do_It!                                                                        
        ClearAll
        
        View "Ped_Temp"
        Coedit "Answer"
        Scan
           Moveto "Ped_Temp"
           Moveto [Numero do Pedido]
           Locate [Answer->Numero do Pedido]
           If RetVal
              Then [Comissao do Representante]=[Answer->Comissao do Representante]    ;Quantidade
                   [Taxa de Desconto         ]=[Answer->Taxa de Desconto         ]    ;Quantidade Faturada
           EndIf
           Moveto "Answer"
        EndScan
        Do_It!
        ClearAll

        Delete "Answer"
        Rename "Ped_Temp" "Answer"
        
        CopyReport "\\Base\\Sistemas\\Vendas\\RM_0041" "2" "Answer" "R" 
        IMPRIREL("Answer","R")
EndIf

CoEdit "Answer"
PickForm "5"
LIMPAGUARDE()
EndProc

;*******************************************************************************
;* Rotina                                                                      *
;*******************************************************************************
Proc SD_Dialog(Disparo,Campo,Evento,Elemento)
Temp=True
TECLA    =""
MSG_CAMPO=""                                     
If Disparo="OPEN"  Then AoCriarShowDialog() EndIf
If Disparo="EVENT" And Evento["Type"]="KEY" Then
   Switch
     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_HRINI"          : Campo="TG_DTINI"
            Case Campo="TG_DTFIM"          : Campo="TG_HRINI"
            Case Campo="TG_HRFIM"          : 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_HRINI"
                    Case Campo="TG_HRINI"          : Campo="TG_DTFIM"
                    Case Campo="TG_DTFIM"          : Campo="TG_HRFIM"
                    Case Campo="TG_HRFIM"          : Campo="YES"
                  EndSwitch
          EndIf
          RefreshDialog
   EndSwitch
EndIf
Switch
  Case Campo="":
       MSG_CAMPO=""
EndSwitch
Monta_Prompt(MSG_CAMPO,TECLA)
Return Temp
EndProc

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

SYS_VALOR()

If Not IsAssigned(SYS_CODIGO_CLIENTE)  Then SYS_CODIGO_CLIENTE="0000"  EndIf
If Not IsAssigned(SYS_CHAPA_USUARIO)   Then SYS_CHAPA_USUARIO=""       EndIf

VSDGEMPRESA =""
VSDGNOMEMP  =""
VSDGREPRESEN=""
VSDGNOMREPR =""
VSDGDTINI   = BlankDate()
VSDGDTFIM   = BlankDate()
VSDGHRINI   =""
VSDGHRFIM   =""

While True

  ShowDialog "ImpressĆo dos Pedidos Importados" 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 12 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 "A30" Picture "!*!" Tag "TG_NOMREPR" To VSDGNOMREPR

    Label  @ 04,00 "Perˇodo Referente" For "TG_DTINI"
    Label  @ 05,04 " de" For "TG_DTINI"
    Accept @ 05,09 Width 13 "D"  Picture "#[#];.##;.##[##]" Tag "TG_DTINI" To VSDGDTINI
    Label  @ 05,22 "as" For "TG_DTINI"
    Accept @ 05,26 Width 08 "A5"  Picture "##:##" Tag "TG_HRINI" To VSDGHRINI
    Label  @ 06,04 "at‚" For "TG_DTFIM"
    Accept @ 06,09 Width 13 "D"  Picture "#[#];.##;.##[##]" Tag "TG_DTFIM" To VSDGDTFIM
    Label  @ 06,22 "as" For "TG_DTFIM"
    Accept @ 06,26 Width 08 "A5"  Picture "##:##" Tag "TG_HRFIM" To VSDGHRFIM

    Pushbutton @ 08,22 width 11 "~O~k"      OK     Default Value "Ok" Tag "YES" To BottonValue
    Pushbutton @ 08,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 VSDGDTFIM=BlankDate()
     Then Mensagem("Data Final Invalida !")
          Loop
  EndIf

  AGUARDE("Aguarde, Selecionando registros")
  SELECIONA()

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

  Processa()

  Echo Normal
  Echo Off

  ClearAll
  Delete "Answer"

EndWhile
ClearAll
Release Vars All
Release Procs All
