;** Rm_0020 ********************************************************************
;*  Emissao Formulacao                                                         *
;*******************************************************************************
;Autor           : Vise Informatica (VISERP)
;Desenvolvido    : 02/09/2002 por Anivel Augusto Rodrigues
;Ultima Alteracao: 07/11/2002 por Anivel Augusto Rodrigues
;Motivo Alteracao: Inclusao Opcao de Custo
;Ultima Alteracao: 20/05/2020 por Bruno Carvalho
;Motivo Alteracao: Ajustes para o Base no CopyReport do "Rm_0010" para "\\Base\\Sistemas\\Curtume\\Rm_0010"

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

{Ask} {FormulaC}
Moveto [Codigo do Produto] Example "ITEM"
Moveto [Codigo Cor_Tecido] Example "COR"
Moveto [Quantidade Produzida   ] Check
Moveto [Quantidade_Produz_Meios] Check
Moveto [Quantidade_Produz_Kg   ] Check
{Ask} {FormulaI} Check
Moveto [Codigo do Produto] Example "ITEM" TypeIn ", ~VSDGITEM.."
Moveto [Codigo Cor_Tecido] Example "COR"  TypeIn ", ~VSDGCORTECIDO.."
Moveto [Codigo da Etapa  ] TypeIn "~VSDGETAPA.."
Do_It!
ClearAll

If Not IsEmpty("Answer")
   Then
        View "\\Sistemas\\Estoque\\SDEstq"
        CoEdit "Answer"
        Scan
          [Quantidade Produzida   ]=VSDGQTDMTS
          [Quantidade_Produz_Meios]=VSDGQTDMEIOS
          [Quantidade_Produz_Kg   ]=VSDGQTDKGS
          If [Sequencia]>0
             Then [Quantidade]=NDecimal(([Porcentagem]*[Quantidade_Produz_Kg])/100,6)
          EndIf
          If VSDGOPREL="2"
             Then If VSDGOPCUSTO<>"F"
                     Then
                          Moveto "\\Sistemas\\Estoque\\SDEstq"
                          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
                  EndIf
          EndIf
        EndScan
        Do_It!
        ClearAll

        If VSDGOPREL="1" Then CopyReport "\\base\\sistemas\\curtume\\Rm_0010" "2" "Answer" "R" EndIf
        If VSDGOPREL="2" Then CopyReport "\\base\\sistemas\\curtume\\Rm_0010" "3" "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_ITEM"           : Campo="TG_EMPRESA"
            Case Campo="TG_COR_TECIDO"     : Campo="TG_ITEM"
            Case Campo="TG_ESPESSURA"      : Campo="TG_COR_TECIDO"
            Case Campo="TG_ETAPA"          : Campo="TG_ESPESSURA"
            Case Campo="TG_QTDMEIOS"       : Campo="TG_ETAPA"
            Case Campo="TG_QTDKGS"         : Campo="TG_QTDMEIOS"
            Case Campo="TG_QTDMTS"         : Campo="TG_QTDKGS"
            Case Campo="TG_OPREL"          : Campo="TG_QTDMTS"
            Case Campo="TG_MESANO"         : Campo="TG_OPREL"
            Case Campo="TG_OPCUSTO"        : Campo="TG_MESANO"
            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_ITEM"
                    Case Campo="TG_ITEM"       : Campo="TG_COR_TECIDO"
                    Case Campo="TG_COR_TECIDO" : Campo="TG_ESPESSURA"
                    Case Campo="TG_ESPESSURA"  : Campo="TG_ETAPA"
                         VSDGESPESSURA=ControlValue("TG_ESPESSURA")
                    Case Campo="TG_ETAPA"      : Campo="TG_QTDMEIOS"
                    Case Campo="TG_QTDMEIOS"   : Campo="TG_QTDKGS"
                         VSDGQTDMEIOS=ControlValue("TG_QTDMEIOS")
                    Case Campo="TG_QTDKGS"     : Campo="TG_QTDMTS"
                         VSDGQTDKGS=ControlValue("TG_QTDKGS")
                    Case Campo="TG_QTDMTS"     : Campo="TG_OPREL"
                         VSDGQTDMTS=ControlValue("TG_QTDMTS")
                    Case Campo="TG_OPREL"      :
                         VSDGOPREL=ControlValue("TG_OPREL")
                         If VSDGOPREL="2"
                            Then VSDGNOMOPREL="Com Custo"
                                 Campo="TG_MESANO"
                            Else VSDGNOMOPREL="Sem Custo"
                                 VSDGOPREL="1"
                                 VSDGOPCUSTO=""
                                 VSDGNOMOPCUSTO=""
                                 Campo="YES"
                         EndIf
                    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!")
                            Else VSDGDTINI = DateVal("01."+VSDGMES+"."+VSDGANO)
                            VSDGDTINI = DateVal("01."+VSDGMES+"."+VSDGANO)
                                 VSDGDTREF=Ultimo_Dia(VSDGMES+VSDGANO)
                                 If Month(VSDGDTREF)=Month(Today())
                                    Then VSDGDTREF=Today()
                                 EndIf
                                 Campo="TG_OPCUSTO"
                         EndIf
                    Case Campo="TG_OPCUSTO" :
                         VSDGOPCUSTO=ControlValue("TG_OPCUSTO")
                         Switch
                           Case VSDGOPCUSTO="U": VSDGNOMOPCUSTO="ULTIMO CUSTO"
                           Case VSDGOPCUSTO="M": VSDGNOMOPCUSTO="CUSTO MEDIO"
                           OtherWise:
                                VSDGOPCUSTO="F"
                                VSDGNOMOPCUSTO="CUSTO DA FORMULA"
                         EndSwitch
                         Campo="YES"
                  EndSwitch
          EndIf
          RefreshDialog
   EndSwitch
EndIf
Switch
  Case Campo="TG_EMPRESA":
       MSG_CAMPO="Informe uma Empresa"
  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 PRINCIPAL                                                            *
;*******************************************************************************
AutoLib="\\Sistemas\\Cadastro\\Bib_Show,\\Sistemas\\Cadastro\\Bibliot"
LimpAguarde()

VSDGEMPRESA     = ""
VSDGPRODUTO  = ""
VSDGCORTECIDO= ""
VSDGETAPA    = ""
VSDGESPESSURA= ""
VSDGQTDMEIOS = BlankNum()
VSDGQTDKGS   = BlankNum()
VSDGQTDMTS   = BlankNum()
VSDGOPCUSTO     = ""
VSDGNOMOPCUSTO  = ""
VSDGMESANO   = AlinhM(StrVal(Month(Today())),2)+"."+AlinhM(StrVal(Year(Today())),4)

While True

  ShowDialog "Emiss„o da Formula‡„o" 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 19 Width 65

    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 "Item"  For "TG_ITEM"
    Accept @ 03,01 Width 13 "A10" Picture "#*#" Tag "TG_ITEM"     To VSDGITEM
    Accept @ 03,15 Width 37 "A35" Picture "!*!" Tag "TG_NOMITEM" To VSDGNOMITEM

    Label  @ 04,00 "Cor" For "TG_COR_TECIDO"
    Accept @ 05,01 Width 07 "A4"  Picture "#*#" Tag "TG_COR_TECIDO"    To VSDGCORTECIDO
    Accept @ 05,09 Width 28 "A25" Picture "!*!" Tag "TG_NOMCOR_TECIDO" To VSDGNOMCORTECIDO

    Label  @ 04,38 "Espessura" For "TG_ESPESSURA"
    Accept @ 05,39 Width 23 "A20" Picture "!*!" Tag "TG_ESPESSURA" To VSDGESPESSURA

    Label  @ 06,00 "Etapa" For "TG_ETAPA"
    Accept @ 07,01 Width 07 "A4"  Picture "#*#" Tag "TG_ETAPA"    To VSDGETAPA
    Accept @ 07,09 Width 28 "A25" Picture "!*!" Tag "TG_NOMETAPA" To VSDGNOMETAPA

    Label  @ 08,00 "Qtd Meios" For "TG_QTDMEIOS"
    Accept @ 09,01 Width 11 "N" Tag "TG_QTDMEIOS" To VSDGQTDMEIOS

    Label  @ 08,14 "Qtd Kgs" For "TG_QTDKGS"
    Accept @ 09,15 Width 11 "N" Tag "TG_QTDKGS" To VSDGQTDKGS

    Label  @ 08,28 "Qtd Mts" For "TG_QTDMTS"
    Accept @ 09,29 Width 11 "N" Tag "TG_QTDMTS" To VSDGQTDMTS

    Label  @ 10,00 "Op‡„o Relatorio" For "TG_OPREL"
    Accept @ 11,01 Width 04 "A1"  Picture "1,2" Tag "TG_OPREL" To VSDGOPREL
    Accept @ 11,06 Width 31 "A25"  Picture "!*!" Tag "TG_NOMOPREL" To VSDGNOMOPREL

    Label  @ 12,00 "Mˆs/Ano Ficha Estoque"  For "TG_MESANO"
    Accept @ 13,01  Width 10 "A07"  Picture "##.####" Tag "TG_MESANO" To VSDGMESANO

    Label  @ 12,15 "Op‡„o de Custo" For "TG_OPCUSTO"
    Accept @ 13,16 Width 04 "A1"  Picture "M,U,F" Tag "TG_OPCUSTO" To VSDGOPCUSTO
    Accept @ 13,21 Width 18 "A25"  Picture "!*!" Tag "TG_NOMOPCUSTO" To VSDGNOMOPCUSTO

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

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

  If IsBlank(VSDGEMPRESA)
     Then MENSAGEM("Empresa Inv lida !")
          Loop
  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

  IMPRIREL("Answer","R")

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