;** RPedidos *******************************************************************
;*  Alteracao e Inclusao de Pedidos p/ Producao                                *
;*******************************************************************************
;Autor           : Vise Informatica (VISERP)
;Desenvolvido    : 26/05/2004 por Anivel Augusto Rodrigues
;Alterado em		 : 22/03/2006 por Anivel Augusto Rodrigues
;Motivo Alteracao: Inclusao TG_NCARREG
;Alterado em		 : 15/12/2009 por Bruno Carvalho
;Motivo Alteracao: Inclusao no TG_OPDT a op‡Æo ENTREGA

;*******************************************************************************
;* Rotina para Localizar Registro                                              *
;*******************************************************************************
Proc CHAVE()
While True
  Moveto [Sequencia]
  End
  CODIGO=[Sequencia]
  Down
  CODIGO=CODIGO+1
  [Sequencia]=CODIGO

  While true

      If Field()="Sequencia"
         Then Prompt " Esc=Sair  F3=Limpar  F4=Impress„o  F6=Selecao  F7=Paginar"
         Else Prompt " Esc=Retornar  F1=Tabelas"
      EndIf

      Wait Field
           Until "ESC","ENTER","F7","HOME","UP","PGUP","F3","F4","F1","F6",
            -104, -105, -106, -107, -108, -109, -110, -111, -112, -113, -139, -140
            
      If SubStr(RetVal,1,2)="-1"      
         Then SYS_TECLA_ACIONADA=RetVal 
              Seta_Tecla_Acionada()     
              RetVal="ESC"            
      EndIf                             
                                                                        

      Switch
      Case RetVal="F1"And Field()="Numero do Pedido":
           CONSULTAF1()
      Case RetVal="F3"And Field()="Sequencia":
           CONFIRMA()
           If OK="S"
              Then EXCLUSAO()
                   QuitLoop
           EndIf
      Case RetVal="F4":
           If Field()="Sequencia"
              Then ShowPopup " Impress„o " @04 ,28
                      "~1~- Resumo Itens   " :" Resumo dos itens  ":"CARREG1",
                      "~2~- Rela‡„o Pedidos ":" Rela‡„o de Pedidos":"CARREG2",
                      "~C~ancelar"           :" Cancelar Opera‡„o"                   :"CANCELAR"
                   EndMenu
                    To MenuVar
                    If Not Retval or (MenuVar="Cancelar") or (MenuVar="Esc")
                       Then Loop
                    EndIf

              Switch
                    Case MenuVar="CARREG1"  :
                         Del
                         OPREL=1
                         IMPRIME()
                         QuitLoop
                    Case MenuVar="CARREG2"  :
                         Del
                         OPREL=2
                         IMPRIME()
                         QuitLoop
                   Case MenuVar="CANCELAR" :Loop
              EndSwitch
           EndIf
       Case RetVal="F6":
            Del
            Selecao()
            QuitLoop
      Case RetVal="F7" Or RetVal="UP" Or RetVal="PGUP" Or RetVal="HOME" And Field()="Sequencia":
           PAGINA("RPedidos","MR")
           If EDITAR="S"
              Then QuitLoop
              Else If EXCLUIR="S"
                      Then CONFIRMA()
                           If OK="S"
                              Then DEL
                           EndIf
                   EndIf
           EndIf
           QuitLoop
      Case Retval="ESC":
           If Field()="Numero do Pedido"
              Then If CHAVE=1
                      Then Del
                   EndIf
                   QuitLoop
           EndIf
           CHAVE=9
           Del
           Return
      Case Retval="ENTER":
           If Field()="Sequencia"
              Then If IsBlank([])
                      Then Loop
                   EndIf
                   CODIGO=[]
                   Del
                   Locate CODIGO
                   If Retval
                      Then CHAVE=0
                           Moveto [Numero do Pedido]
                           Loop
                      Else CHAVE=1
                           End Down
                           [Sequencia]=CODIGO
                   EndIf
                   If IsLockRecord()
                      Then End Down
                           Moveto [Sequencia]
                           Loop
                   EndIf
             Else Switch
                    Case Field()="Numero do Pedido" :
                         If IsBlank([])
                             Then Loop
                         Endif
                         QuitLoop
                  EndSwitch
        EndIf
        Right
      EndSwitch
  EndWhile
EndWhile
EndProc

;*******************************************************************************
;* Rotina De Exclusao                                                          *
;***************************************************+***************************
Proc EXCLUSAO()
Home
While RecordStatus("New")=False
  Del
EndWhile
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_CIDADE"        : Campo="TG_EMPRESA"
            Case Campo="TG_NCARREG"       : Campo="TG_CIDADE"
						Case Campo="TG_OPDT"          : Campo="TG_CIDADE"
						Case Campo="TG_DTINI"         : Campo="TG_OPDT"
						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_CIDADE"
                    Case Campo="TG_CIDADE"  :Campo="TG_NCARREG"
                    Case Campo="TG_NCARREG" : Campo="TG_OPDT"
                    Case Campo="TG_OPDT"    : Campo="TG_DTINI"
                         VSDGOPDT=ControlValue("TG_OPDT")
                         Switch
                           Case VSDGOPDT="E": VSDGDESCOPDT="DATA DE EMISSAO"
                           Case VSDGOPDT="I": VSDGDESCOPDT="DATA DE INCLUSAO"
                           Case VSDGOPDT="N": VSDGDESCOPDT="DATA DE ENTREGA"
                           OtherWise: Campo="TG_OPDT"
                         EndSwitch
                    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=""
  Case Campo="TG_OPDT":
     MSG_CAMPO="Op‡„o: Emiss„o/Inclus„o/eNtrega "
Endswitch
Monta_Prompt(MSG_CAMPO,TECLA)
Return Temp
EndProc

;************************************
;* ROTINA PARA LIMPAR AGUARDE       *
;************************************
Proc LimpAguarde2L()
@ 23,0 ?? Fill(" ",80)
PaintCanvas Attribute 31 23,0,23,79
@ 24,0 ?? Fill(" ",80)
PaintCanvas Attribute 31 24,0,24,79
EndProc ; (LimpAguarde2L)

;*******************************************************************************
;* Rotina Filtrar Registros                                                    *
;*******************************************************************************
Proc Selecao()
LimpAguarde()
Do_It!
ClearAll
Echo Normal
Echo Off

OP_SHOW="S"
  ShowDialog "Relacao de Pedidos p/ Produ‡„o" 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 15 Width 56

    Label  @ 00,00 "Empresa"  For "TG_EMPRESA"
    Accept @ 01,01 Width 07 "A4"  Picture "[&,#][#*#]" Tag "TG_EMPRESA"    To VSDGEMPRESA
    Accept @ 01,09 Width 23 "A20" Picture "!*!" Tag "TG_NOMEMPRESA" To VSDGNOMEMP

    Label  @ 02,00 "Cidade"  For "TG_CIDADE"
    Accept @ 03,01 Width 23 "A20" Picture "!*!" Tag "TG_CIDADE"  To VSDGCIDADE

		Label  @ 04,00 "N§ Carregamento" For "TG_NCARREG"
		Accept @ 05,01 Width 9	"A6"  Picture "#*#" Tag "TG_NCARREG"    To VSDGNCARREG
		Accept @ 05,11 Width 23 "A20" Picture "!*!" Tag "TG_NOMNCARREG" To VSDGNOMNCARREG

		Label  @ 06,00 "Op‡„o de Datas" For "TG_OPDT"
		Accept @ 07,01 Width 04 "A1"  Picture "E,I,N" Tag "TG_OPDT" To VSDGOPDT
		Accept @ 07,06 Width 31 "A20"  Picture "!*!" Tag "TG_DESCOPDT" To VSDGDESCOPDT

		Label  @ 08,00 "Per¡odo Referente" For "TG_DTINI"
		Label  @ 09,00 "de" For "TG_DTINI"
		Accept @ 09,04 Width 13 "D"  Picture "#[#];.##;.##[##]" Tag "TG_DTINI" To VSDGDTINI
		Accept @ 09,18 Width 08 "A5"  Picture "##:##" Tag "TG_HRINI" To VSDGHRINI

		Label  @ 09,26 "at‚" For "TG_DTFIM"
		Accept @ 09,31 Width 13 "D"  Picture "#[#];.##;.##[##]" Tag "TG_DTFIM" To VSDGDTFIM
		Accept @ 09,45 Width 08 "A5"  Picture "##:##" Tag "TG_HRFIM" To VSDGHRFIM

		Pushbutton @ 11,15 width 11 "~O~k"      OK     Default Value "Ok" Tag "YES" To BottonValue
		Pushbutton @ 11,28 width 11 "~C~ancela" CANCEL                    Tag "NO"  To BottonValue

  EndDialog

If RetVal
   Then LimpaRodape(0)
        Aguarde("Aguarde, Selecionando registros")
        ClearAll
        Seleciona2()

        If IsEmpty("Answer")
           Then Mensagem("N„o existem registros conforme sele‡„o !")
           Else View "RPedidos"
                Coedit "Answer"
                Scan
                Moveto "RPedidos"
                End
                SEQ=[Sequencia]
                Down
                [Sequencia        ]=SEQ+1
                [Codigo da Empresa]=[Answer->Codigo da Empresa]
                [Numero do Pedido ]=[Answer->Numero do Pedido ]
                [Data de Emissao  ]=[Answer->Data de Emissao  ]
                [Codigo           ]=[Answer->Codigo           ]
                SEQ                =[Sequencia                ]
                UnLockRecord
                Moveto "Answer"
                EndScan
                Do_It!
                ClearAll
        EndIf
EndIf
ClearAll
If IsTable("Answer")
   Then Delete "Answer"
EndIf
CoEdit "RPedidos"
PickForm "F"

EndProc

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

If Upper(VSDGCIDADE)="TODAS"
   Then VSDGCIDADE2=".."
   Else VSDGCIDADE2=VSDGCIDADE
EndIf

{Ask} {\\Sistemas\\Vendas\\PedidoC} Check
Moveto [Codigo da Empresa  ] TypeIn "~VSDGEMPRESA.."
Switch
  Case VSDGOPDT="E": Moveto [Data de Emissao    ] TypeIn ">=~VSDGDTINI,<=~VSDGDTFIM"
  Case VSDGOPDT="I": Moveto [Data_Inclusao      ] TypeIn ">=~VSDGDTINI,<=~VSDGDTFIM"
  Case VSDGOPDT="N": Moveto [Data de Entrega    ] TypeIn ">=~VSDGDTINI,<=~VSDGDTFIM"
EndSwitch
Moveto [Cidade             ] TypeIn VSDGCIDADE2
Moveto [Numero_Carregamento] TypeIn "~VSDGNCARREG.."
Do_It!
ClearAll

If VSDGOPDT="E"
   Then Return
EndIf

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

EndProc    ; (Seleciona)

;*******************************************************************************
;* Rotina De Impressao                                                         *
;*******************************************************************************
Proc IMPRIME()
Do_It!
ClearAll

DISPOSITIVO()
If OK=""
   Then CoEdit "RPedidos"
        PickForm "F"
        Return
EndIf
AGUARDE("Aguarde, Gerando o Relat¢rio")

Switch
  Case OPREL=2 : SELECIONA2()
  OTherWise    : SELECIONA()
EndSwitch

If IsEmpty("Answer")
   Then MENSAGEM("N„o existem registros a processar")
   Else
        If OPREL=1 Then CopyReport  "\\Base\\Sistemas\\Vendas\\RM_0300"   "4" "Answer" "R"
                        IMPRIREL("Answer","R")
        EndIf
        If OPREL=2 Then CopyReport  "\\Base\\Sistemas\\Vendas\\RM_1100"   "3" "Answer" "R"
                        IMPRIREL("Answer","R")
        EndIf

EndIf

Delete "Answer"

CoEdit "RPedidos"
PickForm "F"
EndProc

;*******************************************************************************
;* Seleciona Pedidos para impressao                                            *
;*******************************************************************************
Proc SELECIONA()
{Ask} {RPedidos}
Moveto [Sequencia       ] Check
Moveto [Numero do Pedido] Example "NUM"
{Ask} {\\Sistemas\\Vendas\\PedidoI} Check
Moveto [Numero do Pedido] Example "NUM"
Do_It!
ClearAll
EndProc

;*******************************************************************************
;* Seleciona Pedidos para impressao
;*******************************************************************************
Proc SELECIONA2()
{Ask} {RPedidos}
Moveto [Numero do Pedido] Example "NUM"
{Ask} {\\Sistemas\\Vendas\\PedidoC} Check
Moveto [Numero do Pedido] Example "NUM"
Do_It!
ClearAll
EndProc

;*******************************************************************************
;* Rotina Principal                                                            *
;*******************************************************************************
AutoLib="\\Sistemas\\Cadastro\\Bibliot,\\Sistemas\\Cadastro\\Bib_Show"

VSDGEMPRESA =""
VSDGCIDADE  =""
VSDGNCARREG =""
VSDGOPDT    =""
VSDGDTINI   =BlankDate()
VSDGDTFIM   =BlankDate()
VSDGHRINI  =""
VSDGHRFIM  =""

CoEdit "RPedidos"
PickForm "F"
While True
  CHAVE()
  If CHAVE=9
     Then QuitLoop
  EndIf
EndWhile
Do_It!
ClearAll
Release Vars All
Release Procs All

