;** Rm_0650  *******************************************************************
;* Emissao de Tabela de Precos                                                 *
;*******************************************************************************
;Autor           : Vise Informatica (VISERP)
;Desenvolvido    : 28/02/1997 por Anivel Augusto Rodrigues
;Ultima Alteracao: 06/10/2003 por Karen Lauar Nery
;Motivo Alteracao: Inclusao Proc Preco
;Ultima Alteracao: 23/06/2016 por Bruno Carvalho
;Motivo Alteracao: Removido Link nos Report "RM_3000" e utilizado fun‡Æo Ajusta_Answer() e Adiciona_Dados_Tabela()
;Ultima Alteracao: 03/02/2018 por Anivel Augusto Rodrigues
;Motivo Alteracao: Ajustes na estrutura tabela RM_3000 e reports, logica + Seta_Ordem_Report()                  

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

{Ask} {\\Sistemas\\Estoque\\Itens}
Moveto [Codigo do Item           ] Example "CODIGO"
Moveto [Codigo da Familia        ] Check TypeIn ">=~VSDGFAMIINI ,<=~VSDGFAMIFIM"
Moveto [Descricao da Familia     ] Check
Moveto [Codigo da Marca          ] Check TypeIn ">=~VSDGMARCAINI,<=~VSDGMARCAFIM"
Moveto [Descricao da Marca       ] Check
Moveto [Codigo do Grupo          ] Check TypeIn ">=~VSDGGRPINI  ,<=~VSDGGRPFIM"
Moveto [Descricao do Grupo       ] Check
Moveto [Codigo da Cor_Tecido     ] Check TypeIn ">=~VSDGCORINI  ,<=~VSDGCORFIM"
Moveto [Descricao da Cor_Tecido  ] Check
Moveto [Unidade Embalagem        ] Check
Moveto [Fator Conversao Embalagem] Check
Moveto [Inativo                  ] TypeIn "Not S"
{Ask} {TbPrCab}             
Moveto [Codigo da Tabela         ] TypeIn "_cdtab"
Moveto [Descricao da Tabela      ] Check
{Ask} {TbPrite} Check
Moveto [Codigo da Tabela         ] TypeIn "_cdtab, ~VSDGTABELA.."
Moveto [Codigo da Tabela] TypeIn "~VSDGTABELA.."
Moveto [Codigo do Item  ] Example "CODIGO"
Moveto [Preco Unitario  ] TypeIn ">0"
Do_It!
ClearAll
EndProc  ; (Seleciona)

;*******************************************************************************
;* Ajusta Dados na Answer                                                      *  
;*******************************************************************************
Proc Ajusta_Answer()

;Processo p/ ajustar tabela conforme remo‡Æo do link da tabela "ITENS" nos reports

{Ask} {Answer} Check
Moveto [Codigo do Item           ] Example "_Ite"
{Ask} {\\Sistemas\\Estoque\\Itens}
Moveto [Codigo do Item           ] Example "_Ite"
Moveto [Tributacao de ICMS       ] Check     
Moveto [Aliquota de ICMS         ] Check
Moveto [Referencia               ] Check
Moveto [Descricao complementar   ] Check
Moveto [Observacao               ] Check
Moveto [Localizacao              ] Check
Moveto [Codigo Fornecedor        ] Check
Do_It!
ClearAll

If IsAssigned(TABELA) And IsTable(TABELA) Then Delete TABELA EndIf
TABELA = Cria_Temp(PrivDir())
Copy "\\Base\\Sistemas\\Vendas\\RM_3000" TABELA
Adiciona_Dados_Tabela(TABELA)

{Ask} TypeIn TABELA Enter Check
Do_It!
ClearAll
Delete TABELA  

EndProc ;Ajusta_Answer()

;*******************************************************************************
;* Rotina p/ processar Db temporaria                                           *
;*******************************************************************************
Proc Processa()

If VSDGVRBRANCO="S"
   Then {Ask} {Answer} TypeIn "Fast"
        Moveto [Preco Unitario] TypeIn "0, Changeto Blank"
        Do_It!
        ClearAll
Endif

{Modify} {Restructure} {Answer}
Moveto [Field Name]
Locate "Preco Unitario_Formatado"
       If Not RetVal
          Then End Down
              "Preco Unitario_Formatado" Enter "A13"  Enter
       EndIf
       Do_It! ClearImage

EndProc   ; (Processa)

;************************************
;* ROTINA PARA FORMATAR O PRECO     *
;************************************
Proc Preco()

Coedit "Answer"

Scan for Not IsBlank([Preco Unitario])

 PRECO=Format("W11.3,EIC",[Preco Unitario])
 If SubStr(PRECO,Len(PRECO),1)="0"
    Then PRECO=Format("W10.2,EIC",[Preco Unitario])+" "
 EndIf
 [Preco Unitario_Formatado]=PRECO

EndScan
Do_It!

EndProc ;(Preco)

;*******************************************************************************
;* 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_TABELA"         : Campo="NO"
            Case Campo="TG_FAMIINI"        : Campo="TG_TABELA"
            Case Campo="TG_FAMIFIM"        : Campo="TG_FAMIINI"
            Case Campo="TG_MARCAINI"       : Campo="TG_FAMIFIM"
            Case Campo="TG_MARCAFIM"       : Campo="TG_MARCAINI"
            Case Campo="TG_GRPINI"         : Campo="TG_MARCAFIM"
            Case Campo="TG_GRPFIM"         : Campo="TG_GRPINI"
            Case Campo="TG_CORINI"         : Campo="TG_GRPFIM"
            Case Campo="TG_CORFIM"         : Campo="TG_CORINI"
            Case Campo="TG_DTINI"          : Campo="TG_CORFIM"
            Case Campo="TG_DTFIM"          : Campo="TG_DTINI"
            Case Campo="TG_OPRELCODALF"    : Campo="TG_DTFIM"
            Case Campo="TG_TAMAN"          : Campo="TG_OPRELCODALF"
            Case Campo="TG_VRBRANCO"       : Campo="TG_TAMAN"
            Case Campo="YES" Or Campo="NO" : Campo="TG_TABELA"
          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_TABELA"     :
                         If Not Isblank(VSDGTABELA)
                            Then Campo="TG_FAMIINI"
                            Else VSDGNOMTAB=""
                         EndIf
                    Case Campo="TG_FAMIINI"    :Campo="TG_FAMIFIM"
                    Case Campo="TG_FAMIFIM"    :Campo="TG_MARCAINI"
                    Case Campo="TG_MARCAINI"   :Campo="TG_MARCAFIM"
                    Case Campo="TG_MARCAFIM"   :Campo="TG_GRPINI"
                    Case Campo="TG_GRPINI"     :Campo="TG_GRPFIM"
                    Case Campo="TG_GRPFIM"     :Campo="TG_CORINI"
                    Case Campo="TG_CORINI"     :Campo="TG_CORFIM"
                    Case Campo="TG_CORFIM"     :Campo="TG_DTINI"
                    Case Campo="TG_DTINI"      :
                         If Not VSDGDTINI = BlankDate()
                            Then Campo="TG_DTFIM"
                         EndIf
                    Case Campo="TG_DTFIM"      :Campo="TG_OPRELCODALF"
                    Case Campo="TG_OPRELCODALF":Campo="TG_TAMAN"
                    Case Campo="TG_TAMAN"      :
                         VSDGTAMAN=ControlValue("TG_TAMAN")
                         If Not IsBlank(VSDGTAMAN)
                            Then Switch
                                   Case VSDGTAMAN="1":
                                        VSDGOPTAMAN="REDUZIDO"
                                   Case VSDGTAMAN="2":
                                        VSDGOPTAMAN="NORMAL"
                                   OtherWise:
                                        VSDGTAMAN=""
                                        VSDGOPTAMAN=""
                                 EndSwitch
                                 If Not IsBlank(VSDGTAMAN)
                                    Then Campo="TG_VRBRANCO"
                                 EndIf
                            Else VSDGOPTAMAN=""
                         EndIf
                    Case Campo="TG_VRBRANCO"   :
                         VSDGVRBRANCO=ControlValue("TG_VRBRANCO")
                         If Not Isblank(VSDGVRBRANCO)
                            Then Switch
                                   Case VSDGVRBRANCO="S":
                                        VSDGOPVRBRANCO="SIM"
                                   Case VSDGVRBRANCO="N":
                                        VSDGOPVRBRANCO="NAO"
                                   OtherWise:
                                        VSDGVRBRANCO=""
                                        VSDGOPVRBRANCO=""
                               EndSwitch
                               If Not IsBlank(VSDGVRBRANCO)
                                  Then Campo="YES"
                               EndIf
                         EndIf
                    Case Campo="TG_OPRELCODALF":Campo="YES"
                  EndSwitch
          EndIf
          RefreshDialog
   EndSwitch
EndIf
Switch
  Case Campo="TG_TABELA":
     MSG_CAMPO="Informe uma Tabela !"
  Case Campo="TG_DTINI":
     MSG_CAMPO="Informe a Data Inicial"
  Case Campo="TG_DTFIM":
     MSG_CAMPO="Informe a Data Final"
  Case Campo="TG_TAMAN":
     MSG_CAMPO="Informe o tamanho 1=Reduzido 2=Normal"
  Case Campo="TG_VRBRANCO":
     MSG_CAMPO="Deseja que imprime os valores em branco ? (Sim ou N„o)"
Endswitch
Monta_Prompt(MSG_CAMPO,TECLA)
Return Temp
EndProc

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

VSDGTABELA  =""
VSDGFAMIINI =""
VSDGFAMIFIM =""
VSDGMARCAINI=""
VSDGMARCAFIM=""
VSDGGRP     =""
VSDGMARCA   =""
VSDGCORTECIDO=""
VSDGTAMAN   =""
VSDGOPTAMAN =""
VSDGVRBRANCO=""
VSDGDTINI   =BlankDate()
VSDGDTFIM   =BlankDate()

While True

  ShowDialog "Emiss„o da Tabela de Pre‡o IV" Proc "SD_Dialog"
  Trigger "OPEN","ARRIVE"
  Key "F1","ENTER","ESC","TAB",
      -104, -105, -106, -107, -108, -109, -110, -111, -112, -113, -139, -140  
   @ 02,12   Height 20 Width 50

    Label  @ 00,00 "Tabela"  For "TG_TABELA"
    Accept @ 01,01 Width 08 "A5"  Picture "#*#" Tag "TG_TABELA" To VSDGTABELA
    Accept @ 01,10 Width 33 "A30" Picture "!*!" Tag "TG_NOMTAB" To VSDGNOMTAB

    Label  @ 03,00 "Familia.."  For "TG_FAMIINI"
    Accept @ 03,10 Width 07 "A3"  Picture "#*#" Tag "TG_FAMIINI" To VSDGFAMIINI
    Label  @ 03,17 "at‚"  For "TG_FAMIFIM"
    Accept @ 03,22 Width 07 "A3"  Picture "#*#" Tag "TG_FAMIFIM" To VSDGFAMIFIM

    Label  @ 04,00 "Marca...."  For "TG_MARCAINI"
    Accept @ 04,10 Width 07 "A3"  Picture "#*#" Tag "TG_MARCAINI" To VSDGMARCAINI
    Label  @ 04,17 "at‚"  For "TG_MARCAFIM"
    Accept @ 04,22 Width 07 "A3"  Picture "#*#" Tag "TG_MARCAFIM" To VSDGMARCAFIM

    Label  @ 05,00 "Grupo...."  For "TG_GRPINI"
    Accept @ 05,10 Width 07 "A3"  Picture "#*#" Tag "TG_GRPINI" To VSDGGRPINI
    Label  @ 05,17 "at‚"  For "TG_GRPFIM"
    Accept @ 05,22 Width 07 "A3"  Picture "#*#" Tag "TG_GRPFIM" To VSDGGRPFIM

    Label  @ 06,00 "Cor......"  For "TG_CORINI"
    Accept @ 06,10 Width 07 "A4"  Picture "#*#" Tag "TG_CORINI" To VSDGCORINI
    Label  @ 06,17 "at‚"  For "TG_CORFIM"
    Accept @ 06,22 Width 07 "A4"  Picture "#*#" Tag "TG_CORFIM" To VSDGCORFIM

    Label  @ 07,00 "Prazo Validade" For "TG_DTINI"
    Label  @ 08,00 "de" For "TG_DTINI"
    Accept @ 08,04 Width 13 "D"  Picture "#[#];.##;.##[##]" Tag "TG_DTINI" To VSDGDTINI
    Label  @ 08,17 "at‚" For "TG_DTFIM"
    Accept @ 08,22 Width 13 "D"  Picture "#[#];.##;.##[##]" Tag "TG_DTFIM" To VSDGDTFIM

    Label  @ 09,00 "Ordem" For "TG_OPRELCODALF"
    Accept @ 10,01 Width 04 "A1"  Picture "1,2" Tag "TG_OPRELCODALF" To VSDGOPRELCODALF
    Accept @ 10,06 Width 15 "A12" Picture "!*!" Tag "TG_NOMOPRELCODALF" To VSDGNOMOPRELCODALF

    Label  @ 11,00 "Op‡„o de Tamanho" For "TG_TAMAN"
    Accept @ 12,01 Width 04 "A1"  Picture "1,2" Tag "TG_TAMAN" To VSDGTAMAN
    Accept @ 12,06 Width 15 "A8"  Picture "REDUZIDO,NORMAL" Tag "TG_OPTAMAN" To VSDGOPTAMAN

    Label  @ 13,00 "Valor em Branco" For "TG_VRBRANCO"
    Accept @ 14,01 Width 04 "A1"  Picture "S,N" Tag "TG_VRBRANCO" To VSDGVRBRANCO
    Accept @ 14,06 Width 15 "A3"  Picture "SIM,NAO" Tag "TG_OPVRBRANCO" To VSDGOPVRBRANCO

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

  EndDialog
  If Not RetVal Then QuitLoop EndIf

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

  If IsBlank(VSDGDTINI)
     Then Mensagem("Data Inicial Invalida !")
          Loop
  EndIf

  DISPOSITIVO()
  If OK=""  Then Loop EndIf

  AGUARDE("Aguarde, Gerando a Tabela de Pre‡os")
  Seleciona()

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

  If VSDGTAMAN = "1"
     Then CopyReport "\\Base\\Sistemas\\Vendas\\RM_3000" "11" "Answer" "R"
     Else CopyReport "\\Base\\Sistemas\\Vendas\\RM_3000" "5"  "Answer" "R"
  Endif
  If VSDGOPRELCODALF="1"
     Then Seta_Ordem_Report("Answer","R","Codigo do Item","Codigo do Item"   ,"")
     Else Seta_Ordem_Report("Answer","R","Codigo do Item","Descricao do Item","")
  EndIf
  Preco()
  IMPRIREL("Answer","R")

  Delete "Answer"

EndWhile
ClearAll
Release Vars All
Release Procs All

