;** Rm_0130C *******************************************************************
;*  Sintetico I p/ Centro de Custo                                             *
;*******************************************************************************
;Autor           : Vise Informatica (VISERP)
;Desenvolvido    : 26/04/2018 por Anivel Augusto Rodrigues
;Ultima Alteracao:   /  /     por .

;*******************************************************************************
;* Rotina p/ Selecionar Registros                                              *
;*******************************************************************************
Proc SelecionaRM_0130C()
ClearAll

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

       Adiciona_DadosRM_0130C()
EndIf

EndProc       ;(Seleciona)

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

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          ]
  if isblank([Codigo CCusto])
     then
          [Codigo CCusto          ]="  x.001"
          [Descricao CCusto       ]="NAO INFORMADO"
  endif
  [Descricao CCusto       ]=[Answer->Descricao CCusto       ]
  [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

  Moveto "Answer"
EndScan
Do_IT!
ClearAll

EndProc

;*******************************************************************************
;* Rotina p/ processar Db temporaria                                           *
;*******************************************************************************
Proc AjustaRM_0130C()
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_0130C()

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

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

If IsEmpty(Tabela)
   Then 
        MENSAGEM("N„o existem registros a processar")
   else
        AjustaRM_0130C()
        
        IMPRIREL(Tabela,"1")
EndIf
Delete Tabela

EndProc

;-- Chamada principal ----------------------------------------------------------
ProcessaRM_0130C()

Release Vars Tabela
Release Procs ProcessaRM_0130C, SelecionaRM_0130C, Adiciona_DadosRM_0130C, AjustaRM_0130C
