;** Rm_0130B *******************************************************************
;*  AnalĄtico II p/ Centro de Custo                                            *
;*******************************************************************************
;Autor           : Vise Informatica (VISERP)
;Desenvolvido    : 26/04/2018 por Anivel Augusto Rodrigues
;Ultima Alteracao:   /  /     por .

;*******************************************************************************
;* Rotina p/ Selecionar e Order Registros                                      *
;*******************************************************************************
Proc SelecionaRM_0130B()
ClearAll

If IsTable("\\Sistemas\\Ctacor\\"+Dir_Trab+"MovCont")
  Then {Ask} TypeIn "\\Sistemas\\Ctacor\\"+Dir_Trab+"MovCont" Enter Check
       Moveto [Codigo da Empresa     ] TypeIn "~VSDGEMPRESA2.."
       If VSDGOPDTEMISCOMP="1"
          Then Moveto [Data Competencia] Check
               Moveto [Data            ] TypeIn ">=~VSDGDTINI, <=~VSDGDTFIM"
          Else Moveto [Data            ] Check
               Moveto [Data Competencia] TypeIn ">=~VSDGDTINI, <=~VSDGDTFIM, as Data"
       EndIf
       If CheckTG_OPCCUSTO[1]
          Then Moveto [Codigo CCusto          ] TypeIn "Not Blank, ~VSDGCCUSTO.."
          Else Moveto [Codigo CCusto          ] TypeIn "~VSDGCCUSTO.."
       Endif
       Moveto [Tipo Movimento          ] TypeIn "~VSDGOPERACAO.."
       Do_It!
       ClearAll
       
       Adiciona_DadosRM_0130B()
EndIf

EndProc  

;************************************************************************
;* Rotina p/ adicionar dados a partir da answer                         *
;************************************************************************
Proc Adiciona_DadosRM_0130B()

If IsEmpty("Answer")
   Then Return
EndIf

View Tabela
CoEdit "Answer"
Scan
  Moveto Tabela
  End
  Down

  [Numero do Lancamento   ]=[Answer->Numero do Lancamento   ]
  [Codigo da Empresa      ]=[Answer->Codigo da Empresa      ]
  [Nome Fantasia Empresa  ]=[Answer->Nome Fantasia Empresa  ]
  [Data                   ]=[Answer->Data                   ]
  [Codigo CCusto          ]=[Answer->Codigo CCusto          ]
  [Descricao CCusto       ]=[Answer->Descricao CCusto       ]
  if isblank([Codigo CCusto])
     then
          [Codigo CCusto          ]="  x.001"
          [Descricao CCusto       ]="NAO INFORMADO"
  endif
  [Codigo do EmitDest     ]=[Answer->Codigo do EmitDest     ]
  [Nome Fantasia EmitDest ]=[Answer->Nome Fantasia EmitDest ]
  If [Answer->Tipo Movimento]="S"
     Then [Tipo Movimento]="SAIDAS"
          [Valor Saida      ]=[Answer->Valor            ]
     Else [Tipo Movimento]="ENTRADAS"
          [Valor Entrada    ]=[Answer->Valor            ]
  EndIf
  [Descricao do Historico ]=[Answer->Descricao do Historico ]
  [Codigo Plano_Conta     ]=[Answer->Codigo Plano_Conta     ]
  [Descricao Plano_Conta  ]=[Answer->Descricao Plano_Conta  ]

  Moveto "Answer"
EndScan
Do_IT!
ClearAll

EndProc


;*******************************************************************************
;* Rotina p/ processar Db temporaria                                           *
;*******************************************************************************
Proc AjustaRM_0130B()
private CodCCusto

View "CCusto"
CoEdit Tabela
Scan For Not IsBlank([Codigo CCusto])
   [Codigo CCusto1]=SubStr([Codigo CCusto],1,4)+"000"

   CodCCusto = [Codigo CCusto1]
   MoveTo "CCusto"
   Moveto [Codigo CCusto]
   Locate CodCCusto
   If RetVal
      Then DescCCusto = [Descricao CCusto]
      Else DescCCusto = ""
           if CodCCusto="  x.000"
              then DescCCusto ="NAO INFORMADO"
           endif
   Endif

   MoveTo Tabela
   [Descricao CCusto1]=DescCCusto
EndScan
Do_It!
ClearAll

;***** Ajusta Dados ************************************
If VSDGEMPRESA="0000"
   Then {Ask} TypeIn Tabela Enter TypeIn "Fast"
        Moveto [Codigo da Empresa      ] TypeIn "changeto 0000"
        Moveto [Nome Fantasia Empresa  ] TypeIn "changeto TOTALIZA EMPRESAS"
        Do_It!
        ClearAll
EndIf

EndProc

;*******************************************************************************
;* Rotina Principal                                                            *
;*******************************************************************************
Proc ProcessaRM_0130B()

Tabela=Cria_Temp(PrivDir())
Copy "\\Base\\Sistemas\\Ctacor\\Rm_0130" Tabela
empty(Tabela)

Monta_Dir_Trab(VSDGDTINI,VSDGDTFIM)
While Novo_Dir_Trab()
  SelecionaRM_0130B()
EndWhile

If IsEmpty(Tabela)
   Then 
        MENSAGEM("No existem registros a processar")
   else
        AjustaRM_0130B()
        
        IMPRIREL(Tabela,"5")
EndIf
Delete Tabela

EndProc

;-- Chamada principal ----------------------------------------------------------
ProcessaRM_0130B()

Release Vars Tabela
Release Procs ProcessaRM_0130B, SelecionaRM_0130B, Adiciona_DadosRM_0130B, AjustaRM_0130B
