Last active
August 8, 2017 14:04
-
-
Save leogreal/3577aa4f7137890f0f7b9e6114023dff to your computer and use it in GitHub Desktop.
Protheus - Trocar de empresa em tempo de execução
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#INCLUDE 'PROTHEUS.CH' | |
#INCLUDE 'TOPCONN.CH' | |
#INCLUDE 'RWMAKE.CH' | |
#INCLUDE "TBICONN.CH" | |
#INCLUDE "TBICODE.CH" | |
User Function TESTE() | |
Local _aAux := {} | |
Local i := 0 | |
Local _aAreaSM0 := {} | |
Local _oAppBk := oApp //Guardo a variavel resposavel por componentes visuais | |
dbSelectArea("SM0") | |
_aAreaSM0 := SM0->(GetArea()) | |
_cEmpBkp := SM0->M0_CODIGO //Guardo a empresa atual | |
_cFilBkp := SM0->M0_CODFIL //Guardo a filial atual | |
//Lista Empresas que usuario tem Acesso | |
//Retorno - array | |
//1 - Codigo da empresa | |
//2 - Nome da empresa | |
//3 - Codigo da filial | |
//4 - Nome da filials | |
_aAux := FWEmpLoad(.F.) | |
//Percorro a lista de empresas/filiais que o usuário tem acesso | |
For i := 1 to Len(_aAux) | |
//troco de empresa | |
dbCloseAll() //Fecho todos os arquivos abertos | |
OpenSM0() //Abrir Tabela SM0 (Empresa/Filial) | |
dbSelectArea("SM0") //Abro a SM0 | |
SM0->(dbSetOrder(1)) | |
SM0->(dbSeek(_aAux[i][1] + _aAux[i][3],.T.)) //Posiciona Empresa | |
cEmpAnt := SM0->M0_CODIGO //Seto as variaveis de ambiente | |
cFilAnt := SM0->M0_CODFIL | |
OpenFile(cEmpAnt + cFilAnt) //Abro a empresa que eu desejo trabalhar | |
//Realizo o meu processo aqui | |
//funções como RetSqlName e xfilial, funcionam! | |
Next i | |
//Para finalizar volto as variaveis de sistema para seus valores antes da execução | |
dbCloseAll() //Fecho todos os arquivos abertos | |
OpenSM0() //Abrir Tabela SM0 (Empresa/Filial) | |
dbSelectArea("SM0") | |
SM0->(dbSetOrder(1)) | |
SM0->(RestArea(_aAreaSM0)) //Restaura Tabela | |
cFilAnt := SM0->M0_CODFIL //Restaura variaveis de ambiente | |
cEmpAnt := SM0->M0_CODIGO | |
OpenFile(cEmpAnt + cFilAnt) //Abertura das tabelas | |
oApp := _oAppBk //Backup do componente visual | |
Return |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment