;** Rm_0010 ********************************************************************
;* Ficha de Formulas                                                           *
;*******************************************************************************
;Autor           : Vise Informatica (VISERP)
;Desenvolvido    : 05/12/1995 por Anivel Augusto Rodrigues
;Ultima Alteracao: 04/04/2002 por Anivel Augusto Rodrigues
;Motivo Alteracao: Ajustes
;Ultima Alteracao: 12/05/2010 por Bruno Carvalho
;Motivo Alteracao: Adicionado em Rel. de F¢rmula "Venda a Praticar"  (Report 2)
;Ultima Alteracao: 12/12/2013 por Bruno Carvalho
;Motivo Alteracao: Adicionado no cabe‡alho o campo de Observa‡Æo nos Reports
;Ultima Alteracao: 19/12/2013 por Kamila Gomes
;Motivo Alteracao: Alterada exibi‡Æo do campo de Observa‡Æo nos Reports
;Ultima Alteracao: 19/09/2014 por Kamila Gomes
;Motivo Alteracao: Ajuste variaveis VSDGANO e VSDGMES
;Ultima Alteracao: 21/09/2016 por Anivel Augusto Rodrigues
;Motivo Alteracao: Ajustes showdialog.                  
;Ultima Alteracao: 04/05/2017 por Anivel Augusto Rodrigues
;Motivo Alteracao: Ajustes ordem dos itens da formula   
;Ultima Alteracao: 24/03/2020 por Anivel Augusto Rodrigues
;Motivo Alteracao: Ajustes showdialog   
;Ultima Alteracao: 06/04/2020 por Anivel Augusto Rodrigues
;Motivo Alteracao: Ajustes showdialog   
;Ultima Alteracao: 20/05/2020 por Bruno Carvalho
;Motivo Alteracao: Alterado CopyReport "...\\Producao\\Formulai" p/ "...\\Producao\\RM_0041"

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

If IsTable("\\FechAno\\"+VSDGANO+"\\Estoque\\Sdestq") 
   Then TAB_TEMP = "\\FechAno\\"+VSDGANO+"\\Estoque\\Sdestq"
   Else TAB_TEMP = "\\Sistemas\\Estoque\\SdEstq"
EndIf 

{Ask} {\\Sistemas\\Estoque\\Itens}
Moveto [Codigo do Item   ] Example "ITEM"
Moveto [Tipo do Item     ] TypeIn "~VSDGTPITEM.."
Moveto [Codigo da Familia] TypeIn "~VSDGFAMI.."
Moveto [Codigo do Grupo  ] TypeIn "~VSDGGRP.."
Moveto [Codigo da Marca  ] TypeIn "~VSDGMARCA.."

{Ask} {FormulaI} Check
Moveto [Codigo do Produto] Example "ITEM" TypeIn ", ~VSDGITEM.."
Do_It!
ClearAll
If Not IsEmpty("Answer")
  Then If VSDGOPREL="2"
         Then
              If VSDGOPCUSTO<>"F"
                 Then View TAB_TEMP
                      Coedit "Answer"
                      Scan
                        Moveto TAB_TEMP
                        Moveto [Codigo da Empresa]
                        Locate VSDGEMPRESA,[Answer->Codigo do Material],VSDGANO,VSDGMES
                        If RetVal
                           Then Switch
                                  Case VSDGOPCUSTO="U": VRUNIT=[Preco de Custo]
                                  Case VSDGOPCUSTO="M": VRUNIT=NDecimal([Custo Total Atual]/[Saldo Atual],5)
                                EndSwitch
                           Else VRUNIT=BlankNum()
                        EndIf
                        Moveto "Answer"
                        [Valor Unitario]=VRUNIT
                      EndScan
                      Do_It!
                      ClearAll
                      CopyReport "\\Base\\Sistemas\\Producao\\RM_0041" "R" "Answer" "R"
                 Else CopyReport "\\Base\\Sistemas\\Producao\\RM_0041" "2" "Answer" "R"
              EndIf
         Else CopyReport "\\Base\\Sistemas\\Producao\\RM_0041" "1" "Answer" "R"
       EndIf
EndIf

EndProc ; (Seleciona)
;*******************************************************************************
;* 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_TPITEM"         : Campo="TG_EMPRESA"
            Case Campo="TG_FAMI"           : Campo="TG_TPITEM"
            Case Campo="TG_GRP"            : Campo="TG_FAMI"
            Case Campo="TG_MARCA"          : Campo="TG_GRP"
            Case Campo="TG_ITEM"           : Campo="TG_MARCA"
            Case Campo="TG_OPRELCODALF"    : Campo="TG_ITEM"
            Case Campo="TG_OPREL"          : Campo="TG_OPRELCODALF"
            Case Campo="TG_OPCUSTO"        : Campo="TG_OPREL"
            Case Campo="TG_MESANO"         : Campo="TG_OPCUSTO"
            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_TPITEM"
                    Case Campo="TG_TPITEM"     : Campo="TG_FAMI"
                    Case Campo="TG_FAMI"       : Campo="TG_GRP"
                    Case Campo="TG_GRP"        : Campo="TG_MARCA" 
                    Case Campo="TG_MARCA"      : Campo="TG_ITEM"
                    Case Campo="TG_ITEM"       : Campo="TG_OPRELCODALF"
                    Case Campo="TG_OPRELCODALF": Campo="TG_OPREL"
                    Case Campo="TG_OPREL"      :
                         VSDGOPREL=ControlValue("TG_OPREL")
                         If VSDGOPREL="2"
                            Then VSDGNOMOPREL="Com Custo"
                                 Campo="TG_OPCUSTO"
                            Else VSDGNOMOPREL="Sem Custo"
                                 VSDGOPREL="1"
                                 VSDGOPCUSTO=""
                                 VSDGNOMOPCUSTO=""
                                 Campo="YES"
                         EndIf
                    Case Campo="TG_OPCUSTO"    :
                         VSDGOPCUSTO=ControlValue("TG_OPCUSTO")
                         CAMPO="TG_MESANO"
                         Switch
                           Case VSDGOPCUSTO="U": VSDGNOMOPCUSTO="ULTIMO CUSTO"
                           Case VSDGOPCUSTO="M": VSDGNOMOPCUSTO="CUSTO MEDIO"
                           OtherWise:
                                VSDGOPCUSTO="F"
                                VSDGNOMOPCUSTO="CUSTO DA FORMULA"
                                VSDGMESANO=""
                                CAMPO="YES"
                         EndSwitch
                    Case Campo="TG_MESANO"     :
                         VSDGMES=SubStr(VSDGMESANO,1,2)
                         VSDGANO=SubStr(VSDGMESANO,4,4)
                         If NumVal(VSDGANO) < 2000
                            Then VSDGANO=SubStr(VSDGANO,3,2)
                            Else VSDGANO=VSDGANO
                         EndIf
                         If NumVal(VSDGMES) > 12 Or NumVal(VSDGMES) = 0
                            Then MENSAGEM("Mˆs Inv lido!")
                                 Campo="TG_MESANO"
                            Else VSDGDTINI = DateVal("01."+VSDGMES+"."+VSDGANO)
                                 VSDGDTREF=Ultimo_Dia(VSDGMES+VSDGANO)
                                 If Month(VSDGDTREF)=Month(Today())
                                    Then VSDGDTREF=Today()
                                 EndIf
                                 Campo="YES"
                         EndIf
                  EndSwitch
          EndIf
          RefreshDialog
   EndSwitch
EndIf
Switch
;;Case Campo="TG_TAMANHO":
;;     MSG_CAMPO="Em Branco = Todos"
  Case Campo="TG_OPREL"  :
       MSG_CAMPO="Op‡”es: 1=Sem Custo  2=Com Custo"
  Case Campo="TG_OPCUSTO":
       MSG_CAMPO="M=Medio, U=Ultimo, F=Formula"
EndSwitch
Monta_Prompt(MSG_CAMPO,TECLA)
Return Temp
EndProc   ;(Rotina)

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

VSDGEMPRESA  = ""
VSDGTPITEM   = ""
VSDGFAMI     = ""
VSDGGRP      = ""
VSDGMARCA    = ""
VSDGTPITEM   = ""
VSDGOPRELCODALF = ""
VSDGOPREL    = ""
VSDGMESANO   = ""
VSDGOPCUSTO  = ""
VSDGNOMOPREL = ""
VSDGNOMOPCUSTO=""
VSDGMES       =""
VSDGANO       =""

While True

  ShowDialog "Ficha de F¢rmulas" Proc "SD_Dialog"
  Trigger "OPEN","ARRIVE"
  Key "F1","ENTER","ESC","TAB",
      -104, -105, -106, -107, -108, -109, -110, -111, -112, -113, -139, -140  
  @ 04,10   Height 18 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_NOMEMP" To VSDGNOMEMP

    Label  @ 02,00 "Tipo do Item"  For "TG_TPITEM"
    Accept @ 03,04 Width 04 "A1"  Picture "!" Tag "TG_TPITEM"      To VSDGTPITEM
    Accept @ 03,09 Width 25 "A20" Picture "!*!" Tag "TG_NOMTPITEM" To VSDGNOMTPITEM

    Label  @ 04,00 "Familia"  For "TG_FAMI"
    Accept @ 05,02 Width 06 "A3"  Picture "#*#" Tag "TG_FAMI"      To VSDGFAMI
    Accept @ 05,09 Width 25 "A25" Picture "!*!" Tag "TG_NOMFAMI"   To VSDGNOMFAMI

    Label  @ 06,00 "Grupo"  For "TG_GRP"
    Accept @ 07,02 Width 06 "A3"  Picture "#*#" Tag "TG_GRP"       To VSDGGRP
    Accept @ 07,09 Width 25 "A25" Picture "!*!" Tag "TG_NOMGRP"    To VSDGNOMGRP

    Label  @ 08,00 "Marca" For "TG_MARCA"
    Accept @ 09,02 Width 06 "A3"  Picture "#*#" Tag "TG_MARCA" To VSDGMARCA
    Accept @ 09,09 Width 25 "A20" Picture "!*!" Tag "TG_MARCA" To VSDGNOMMARCA

    Label  @ 10,00 "Item"  For "TG_ITEM"
    Accept @ 11,01 Width 33 "A10" Picture "#*#" Tag "TG_ITEM"     To VSDGITEM
    Accept @ 12,01 Width 33 "A35" Picture "!*!" Tag "TG_NOMITEM" To VSDGNOMITEM

    Label  @ 00,35 "Op‡„o de Ordem/Itens da Formula" For "TG_OPRELCODALF"
    Accept @ 01,39 Width 04 "A1"  Picture "1,2" Tag "TG_OPRELCODALF" To VSDGOPRELCODALF
    Accept @ 01,44 Width 25 "A25"  Picture "!*!" Tag "TG_NOMOPRELCODALF" To VSDGNOMOPRELCODALF

    Label  @ 02,35 "Exibir f¢rmula" For "TG_OPREL"
    Accept @ 03,39 Width 04 "A1"  Picture "1,2" Tag "TG_OPREL" To VSDGOPREL
    Accept @ 03,44 Width 25 "A25"  Picture "!*!" Tag "TG_NOMOPREL" To VSDGNOMOPREL

    Label  @ 04,35 "Op‡„o de Custo" For "TG_OPCUSTO"
    Accept @ 05,39 Width 04 "A1"  Picture "M,U,F" Tag "TG_OPCUSTO" To VSDGOPCUSTO
    Accept @ 05,44 Width 25 "A25"  Picture "!*!" Tag "TG_NOMOPCUSTO" To VSDGNOMOPCUSTO

    Label  @ 06,35 "Mˆs/Ano Estoque"  For "TG_MESANO"
    Accept @ 07,36  Width 10 "A07"  Picture "##.####" Tag "TG_MESANO" To VSDGMESANO

    Pushbutton @ 14,22 width 11 "~O~k"      OK     Default Value "Ok"  Tag "YES" To BottonValue
    Pushbutton @ 14,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 Not IsBlank(VSDGMESANO)
     Then If IsBlank(VSDGEMPRESA)
             Then Mensagem("Informar Empresa p/ Ficha de Estoque !")
                  Loop
          EndIf
  EndIf

  DISPOSITIVO()
  If OK="" Then Loop EndIf

  AGUARDE("Aguarde, gerando o relat¢rio")

  Seleciona()

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

  if VSDGOPRELCODALF="2"
     then Seta_Ordem_Report("Answer","R","Codigo do Material","Descricao do Item","C")
  endif   

  IMPRIREL("Answer","R")

  Delete "Answer"
EndWhile
ClearAll
Release Vars All
Release Procs All
