;** Rm_0150 ********************************************************************
;*  Emissao das programacoes por destinatario                                  *
;*******************************************************************************
;Autor           : Vise Informatica (VISERP)
;Desenvolvido    : 28/03/2007 por Anivel Augusto Rodrigues
;Ultima Alteracao: 11/04/2007 por Anivel Augusto Rodrigues
;Motivo Alteracao: Estamparia
;Ultima Alteracao: 24/03/2008 por Anivel Augusto Rodrigues
;Motivo Alteracao: Bordado
;Ultima Alteracao: 23/10/2013 por Bruno Carvalho
;Motivo Alteracao: Altera‡Ćo do filtro "Destinat rio" 
;Ultima Alteracao: 28/12/2016 por Rafael Matos
;Motivo Alteracao: Ajustes showdialog TG_DESTINAT

;*******************************************************************************
;* Rotina p/ Selecionar e Ordernar Registros                                   *
;*******************************************************************************
Proc Seleciona()
ClearAll
                                                       
{Ask} {Pfacc}
Moveto [Numero_Programacao    ] Example "num"
Moveto [Codigo da Empresa     ] TypeIn "~VSDGEMPRESA.."
Moveto [Data_Programacao      ]
If VSDGOPCAO="D"
   Then TypeIn ">=~VSDGDTINI,<=~VSDGDTFIM"
EndIf
Moveto [Codigo                ] TypeIn "~VSDGDESTINAT.."
Moveto [Situacao_Programacao  ] TypeIn "Not FINALIZADA"
Moveto [Destino_Producao      ]
If VSDGOPDEST="INTERNO + FACCAO"
   Then TypeIn "INTERNO Or FACCAO"
   Else TypeIn "~VSDGOPDEST" 
EndIf

{Ask} {PfacRI} Check
Moveto [Numero_Programacao    ] Example "num"
Do_It!
ClearAll

{Ask} {Answer}
Moveto [Quantidade Produzida] TypeIn "Changeto Blank"
Do_It!
ClearAll

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

{Ask} TypeIn Tabela Enter
Moveto [Numero_Programacao    ] Example "num"
{Ask} {Rfacc}
Moveto [Numero_Producao       ] Example "numprod"
Moveto [Numero_Programacao    ] Check Example "num"
{Ask} {RfacRI} Check
Moveto [Numero_Producao       ] Example "numprod"
Do_It!
ClearAll

CoEdit Tabela,"Answer"
Scan

  Moveto Tabela
  Moveto [Numero_Programacao]
  Locate [Answer->Numero_Programacao],[Answer->Codigo do Item]
  If RetVal
     Then [Quantidade Produzida  ]=[Quantidade Produzida  ]+[Answer->Quantidade Produzida  ]
  EndIf
  Moveto "Answer"

EndScan
Do_It!
ClearAll

{Ask} {Pfacc}
Moveto [Numero_Programacao    ] Check Example "num"
Moveto [Codigo da Empresa     ] Check
Moveto [Nome Fantasia Empresa ] Check
Moveto [Data_Programacao      ] Check
Moveto [Destino_Producao      ] Check
Moveto [Codigo                ] Check
Moveto [Nome Fantasia EmitDest] Check
Moveto [Razao Social          ] Check
Moveto [Situacao_Programacao  ] Check
{Ask} TypeIn Tabela Enter Check
Moveto [Numero_Programacao    ] Example "num"
Do_It!
ClearAll

EndProc   ; (Seleciona)

;*******************************************************************************
;* 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()
          If Campo="TG_OPDEST"
             Then Seleciona_Opcoes("Op‡”es de Desinat rio","7,9")
          EndIf
     Case Evento["KeyCode"] =  27:
          Temp=False
          Switch
            Case Campo="TG_EMPRESA"       : Campo="NO"
            Case Campo="TG_OPDEST"        : Campo="TG_EMPRESA"
            Case Campo="TG_DESTINAT"      : Campo="TG_OPDEST"
            Case Campo="TG_OPCAO          : Campo="TG_DESTINAT"
            Case Campo="TG_DTINI"         : Campo="TG_OPCAO"
            Case Campo="TG_DTFIM"         : Campo="TG_DTINI"
            Case Campo="TG_OPMODEL"       : Campo="TG_OPCAO"
            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_OPDEST"
                    Case Campo="TG_OPDEST"     :
                         VSDGOPDEST=ControlValue("TG_OPDEST")
                         If IsBlank(VSDGOPDEST) 
                            Then Campo="TG_OPDEST" 
                            Else If Not Checa_Opcoes("Op‡”es de Desinat rio",VSDGOPDEST)
                                   Then Mensagem("Destino inv lido !")
                                        Campo="TG_OPDEST"
                                   Else If Not Isblank(VSDGOPDEST) 
                                           Then If Not IsBlank(VSDGOPDEST)
                                                   Then Campo="TG_DESTINAT"
                                                EndIf           
                                        EndIf
                                EndIf
                         EndIf  
                    Case Campo="TG_DESTINAT"   : Campo="TG_OPCAO"
                    Case Campo="TG_OPCAO"       :
                         VSDGOPCAO=ControlValue("TG_OPCAO")
                         If IsBlank(VSDGOPCAO)
                            Then VSDGOPCAO="T"
                         EndIf
                         Switch
                           Case VSDGOPCAO="T": VSDGDESCOPDT="TODAS"
                                              VSDGDTINI  =BlankDate()
                                              VSDGDTFIM  =BlankDate()
                                              Campo="TG_OPMODEL"
                           Case VSDGOPCAO="D": VSDGDESCOPDT="DATA DE PROGRAMACAO"
                                              Campo="TG_DTINI"
                         EndSwitch
                    Case Campo="TG_DTINI"      : Campo="TG_DTFIM"
                    Case Campo="TG_DTFIM"      : Campo="TG_OPMODEL"
                    Case Campo="TG_OPMODEL"    :
                         VSDGOPMODEL=ControlValue("TG_OPMODEL")
                         If Not IsBlank(VSDGOPMODEL)
                            Then Switch
                                   Case VSDGOPMODEL="1": VSDGNOMOPMODEL="Analˇtico"
                                   Case VSDGOPMODEL="2": VSDGNOMOPMODEL="Sintetico I"
                                   Case VSDGOPMODEL="3": VSDGNOMOPMODEL="Sintetico II"
                                 EndSwitch
                                 Campo="YES"
                         EndIf
                  EndSwitch
          EndIf
          RefreshDialog
   EndSwitch
EndIf
Switch
  Case Campo="":
     MSG_CAMPO=""
  Case Campo="TG_OPCAO":
     MSG_CAMPO="Op‡„o: T=Todas  D=Data"
  Case Campo="TG_OPMODEL":
     MSG_CAMPO="Op‡„o: 1=Analˇtico  2=Sintetico I  3=Sintetico II"
  Case Campo="TG_OPDEST":
     TECLA="F1=Op‡”es  "
EndSwitch
Monta_Prompt(MSG_CAMPO,TECLA)
Return Temp
EndProc

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

VSDGEMPRESA =""
VSDGDESTINAT=""
VSDGDTINI   =BlankDate()
VSDGDTFIM   =BlankDate()
VSDGOPCAO    ="T"

Coedit "P_DESTI"
Monta_Opcoes("Op‡”es de Desinat rio","INTERNO + FACCAO") 
Do_It!
ClearAll   

While True
  ShowDialog "Programa‡”es em aberto Produ‡„o/Estamparia/Fac‡„o" Proc "SD_Dialog"
  Trigger "OPEN","ARRIVE"
  Key "F1","ENTER","ESC",
      -104, -105, -106, -107, -108, -109, -110, -111, -112, -113, -139, -140  
   @ 05,12   Height 17 Width 66

    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_NOMEMP" To VSDGNOMEMP

    Label  @ 02,00 "Op‡”es Destinat rio" For "TG_OPDEST"
    Accept @ 03,01 Width 31 "A28"  Picture "!*!" Tag "TG_OPDEST" To VSDGOPDEST

    Label  @ 04,00 "Pessoa"  For "TG_DESTINAT"
    Accept @ 05,01 Width 07 "A4"  Picture "[&,#][#*#]" Tag "TG_DESTINAT" To VSDGDESTINAT
    Accept @ 05,09 Width 23 "A20" Picture "!*!" Tag "TG_NOMCLI" To VSDGNOMDEST

    Label  @ 06,00 "Op‡„o em aberto" For "TG_OPCAO"
    Accept @ 07,01 Width 04 "A1"  Picture "T,D" Tag "TG_OPCAO" To VSDGOPCAO
    Accept @ 07,06 Width 28 "A25"  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
    Label  @ 09,17 "at‚" For "TG_DTFIM"
    Accept @ 09,22 Width 13 "D"  Picture "#[#];.##;.##[##]" Tag "TG_DTFIM" To VSDGDTFIM

    Label  @ 10,00 "Modelos de Relat˘rio" For "TG_OPMODEL"
    Accept @ 11,01 Width 04 "A1"  Picture "1,2,3" Tag "TG_OPMODEL" To VSDGOPMODEL
    Accept @ 11,06 Width 28 "A25"  Picture "!*!" Tag "TG_NOMOPMODEL" To VSDGNOMOPMODEL

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

  EndDialog
  If Not RetVal Then QuitLoop EndIf
  If Not Valida_ShowDialog("") Then Loop EndIf

  If VSDGOPCAO="D" And VSDGDTFIM=BlankDate()
     Then Mensagem("Data Final Invalida !")
          Loop
  EndIf

  DISPOSITIVO()
  If OK="" Then Loop EndIf

  AGUARDE("Aguarde, Processando Dados")

  Seleciona()

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

  Switch
    Case VSDGOPMODEL="1": CopyReport "\\Base\\Sistemas\\Faccao\\Rm_0100" "R" "Answer" "R"
    Case VSDGOPMODEL="2": CopyReport "\\Base\\Sistemas\\Faccao\\Rm_0100" "1" "Answer" "R"
    Case VSDGOPMODEL="3": CopyReport "\\Base\\Sistemas\\Faccao\\Rm_0100" "2" "Answer" "R"
  EndSwitch

  IMPRIREL("Answer","R")

  Delete Tabela
  Delete "Answer"

EndWhile
ClearAll
Release Vars All
Release Procs All
