EXEMPLO_1212310
Database
Tables
(current)
Columns
Constraints
Relationships
Orphan Tables
Anomalies
Routines
ATUALIZARESUMOG
Parameters
Name
Type
Mode
@NCONTA
char
IN
@SCONTA
int
IN
@TIPO
char
IN
@CODCOLIGADA
int
IN
@SEQPARCIAL
int
IN
Definition
CREATE PROCEDURE ATUALIZARESUMOG @NCONTA CHAR(14), @SCONTA INTEGER, @TIPO CHAR(1), @CODCOLIGADA INTEGER, @SEQPARCIAL INTEGER AS DECLARE @TOTAL_HONO NUMERIC(15, 2), @TOTAL_DIARIAS NUMERIC(15, 2), @SADT_RESUMO NUMERIC(15, 2), @SADT_HOSPITAL NUMERIC(15, 2), @SADT_TERCEIROS NUMERIC(15, 2), @TOTAL_HONO_NCRED NUMERIC(15, 2), @TOTPACOTE NUMERIC(15, 2), @TOTPACOTESUM NUMERIC(15, 2), @AUXIPACOTE NUMERIC(15, 2), @CONTROLEESTORNOGLOSAGG INTEGER, @CODCOMPRADOR INTEGER, @CODPRESTADOR INTEGER, @STATUSAUDIT VARCHAR(1), @STATUSAUDIT2 VARCHAR(1), @TOTAL_HONOPCTE NUMERIC(15, 2), @TOTAL_HONO_NCREDPCTE NUMERIC(15, 2), @TOTAL_DIARIASPCTE NUMERIC(15, 2), @SADT_HOSPITALPCTE NUMERIC(15, 2), @SADT_TERCEIROSPCTE NUMERIC(15, 2), @SADT_RESUMOPCTE NUMERIC(15, 2), @TOTALID NUMERIC(15, 2), @TOTALIDPCTE NUMERIC(15, 2), @AUX NUMERIC(15, 2), @GLOSAGG INTEGER, @CPP INTEGER, @IDUNIDFAT INTEGER, @TOTPCTEIPSEMG FLOAT, @PADRAOFATURAMENTO INTEGER, @PADRAOESPECIAL INTEGER, @SEMINFOPAGTO INTEGER, @IDUNIDATEND INTEGER, @DATAINI DATETIME SET @CONTROLEESTORNOGLOSAGG = -1 SET @TOTALID = 0 SET @TOTALIDPCTE = 0 IF (SELECT COUNT(*) FROM SZATENDRESUMOGLOSA WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL) <= 0 BEGIN SELECT @CONTROLEESTORNOGLOSAGG = 1 INSERT INTO SZATENDRESUMOGLOSA SELECT * FROM SZATENDRESUMO WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL END SELECT @CPP = B.CODGERAL, @CODPRESTADOR = A.CODPRESTADOR, @CODCOMPRADOR = A.CODCONVENIO, @STATUSAUDIT = A.STATUSAUDITORIA, @STATUSAUDIT2 = A.STATUSAUDITORIA2, @IDUNIDFAT = A.IDUNIDFAT, @DATAINI = A.DATAINI, @IDUNIDATEND = A.IDUNIDATEND FROM SZPARCIALATEND A, SZUNIDATEND B WHERE A.CODCOLIGADA = @CODCOLIGADA AND A.NUMEROCONTA = @NCONTA AND A.SEQUENCIALCONTA = @SCONTA AND A.SEQPARCIAL = @SEQPARCIAL AND B.CODCOLIGADA = A.CODCOLIGADA AND B.IDUNIDATEND = A.IDUNIDFAT IF @STATUSAUDIT <> 'A' BEGIN DELETE FROM SZATENDMATMEDGLOSA FROM SZATENDMATMED A, SZATENDMATMEDGLOSA B WHERE ( A.NUMEROCONTA = @NCONTA ) AND ( A.SEQUENCIALCONTA = @SCONTA ) AND ( B.SEQPARCIAL = @SEQPARCIAL ) AND ( A.SEQPARCIAL = B.SEQPARCIAL ) AND ( A.NUMEROCONTA = B.NUMEROCONTA ) AND ( A.SEQUENCIALCONTA = B.SEQUENCIALCONTA ) AND ( A.SEQDEBITOMATMED = B.SEQDEBITOMATMED ) AND ( ( A.STATUS <> 'P' ) OR ( A.STATUS IS NULL ) ) AND COALESCE(A.SEMINFOPGTO, 'F') = 'F' AND ( A.CODCOLIGADA = B.CODCOLIGADA AND A.CODCOLIGADA = @CODCOLIGADA ) DELETE FROM SZATENDTAXASERVGLOSA FROM SZATENDTAXASERV A, SZATENDTAXASERVGLOSA B WHERE ( A.NUMEROCONTA = @NCONTA ) AND ( A.SEQUENCIALCONTA = @SCONTA ) AND ( B.SEQPARCIAL = A.SEQPARCIAL ) AND ( B.SEQPARCIAL = @SEQPARCIAL ) AND ( A.NUMEROCONTA = B.NUMEROCONTA ) AND ( A.SEQUENCIALCONTA = B.SEQUENCIALCONTA ) AND ( A.SEQDEBITOTAXAS = B.SEQDEBITOTAXAS ) AND ( ( A.STATUS <> 'P' ) OR ( A.STATUS IS NULL ) ) AND COALESCE(A.SEMINFOPGTO, 'F') = 'F' AND ( A.CODCOLIGADA = B.CODCOLIGADA AND A.CODCOLIGADA = @CODCOLIGADA ) DELETE FROM SZATENDSADTGLOSA FROM SZATENDSADT A, SZATENDSADTGLOSA B WHERE ( A.NUMEROCONTA = @NCONTA ) AND ( A.SEQUENCIALCONTA = @SCONTA ) AND ( A.SEQPARCIAL = B.SEQPARCIAL ) AND ( B.SEQPARCIAL = @SEQPARCIAL ) AND ( A.NUMEROCONTA = B.NUMEROCONTA ) AND ( A.SEQUENCIALCONTA = B.SEQUENCIALCONTA ) AND ( A.SEQDEBITOSADT = B.SEQDEBITOSADT ) AND ( ( A.STATUS <> 'P' ) OR ( A.STATUS IS NULL ) ) AND COALESCE(A.SEMINFOPGTO, 'F') = 'F' AND ( A.CODCOLIGADA = B.CODCOLIGADA AND A.CODCOLIGADA = @CODCOLIGADA ) DELETE FROM SZATENDDIARIASGLOSA FROM SZATENDDIARIAS A, SZATENDDIARIASGLOSA B WHERE ( A.NUMEROCONTA = @NCONTA ) AND ( A.SEQUENCIALCONTA = @SCONTA ) AND ( A.NUMEROCONTA = B.NUMEROCONTA ) AND ( A.SEQPARCIAL = B.SEQPARCIAL ) AND ( B.SEQPARCIAL = @SEQPARCIAL ) AND ( A.SEQUENCIALCONTA = B.SEQUENCIALCONTA ) AND ( A.SEQDEBITODIARIA = B.SEQDEBITODIARIA ) AND ( ( A.STATUS <> 'P' ) OR ( A.STATUS IS NULL ) ) AND COALESCE(A.SEMINFOPGTO, 'F') = 'F' AND ( A.CODCOLIGADA = B.CODCOLIGADA AND A.CODCOLIGADA = @CODCOLIGADA ) DELETE FROM SZATENDHONORARIOGLOSA FROM SZATENDHONORARIO A, SZATENDHONORARIOGLOSA B WHERE ( A.NUMEROCONTA = @NCONTA ) AND ( A.SEQUENCIALCONTA = @SCONTA ) AND ( A.NUMEROCONTA = B.NUMEROCONTA ) AND ( A.SEQPARCIAL = B.SEQPARCIAL ) AND ( B.SEQPARCIAL = @SEQPARCIAL ) AND ( A.SEQUENCIALCONTA = B.SEQUENCIALCONTA ) AND ( A.SEQDEBITOHM = B.SEQDEBITOHM ) AND ( ( A.STATUS <> 'P' ) OR ( A.STATUS IS NULL ) ) AND COALESCE(A.SEMINFOPGTO, 'F') = 'F' AND ( A.CODCOLIGADA = B.CODCOLIGADA AND A.CODCOLIGADA = @CODCOLIGADA ) END SELECT @TOTPACOTE = ISNULL(SUM(VALORTOTALTAXAS), 0) FROM SZATENDTAXASERV WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL AND CODGGASTO = '10' AND ( TAXAFORA IS NULL OR TAXAFORA = '' OR TAXAFORA = 'F' ) AND ESTORNADO IS NULL SELECT @TOTPACOTESUM = ISNULL(SUM(VALORTOTALTAXAS), 0), @TOTALIDPCTE = ISNULL(SUM(CASE WHEN IDENTIFICADO = 'T' THEN VALORTOTALTAXAS END), 0) FROM SZATENDTAXASERV WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL AND CODGGASTO = '10' AND ( ( TAXAFORA IS NULL ) OR ( TAXAFORA = '' ) OR ( TAXAFORA = 'F' ) ) AND ESTORNADO IS NULL AND COALESCE(SEMINFOPGTO, 'F') = 'F' AND SEQDEBITOTAXAS NOT IN (SELECT SEQDEBITOTAXAS FROM SZATENDTAXASERVGLOSA WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL AND CODGGASTO = '10' AND ( TAXAFORA IS NULL OR TAXAFORA = '' OR TAXAFORA = 'F' ) AND ESTORNADO IS NULL) SELECT @TOTPACOTESUM = @TOTPACOTESUM + ISNULL(SUM(VALORTOTALTAXAS), 0), @TOTALIDPCTE = @TOTALIDPCTE + ISNULL(SUM(VALORTOTALTAXAS), 0) FROM SZATENDTAXASERVGLOSA WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL AND CODGGASTO = '10' AND ( TAXAFORA IS NULL OR TAXAFORA = '' OR TAXAFORA = 'F' ) AND ESTORNADO IS NULL AND COALESCE(SEMINFOPGTO, 'F') = 'F' SELECT @PADRAOESPECIAL = C.INTEIRO, @PADRAOFATURAMENTO = B.INFOPROPRIACOMPRADOR FROM SZCONDCONTRAT A LEFT OUTER JOIN SZPARAMCONV C ON C.CODCOLIGADA = A.CODCOLIGADA AND C.CODTIPOCONTRATO = A.CODTIPOCONTRATO AND C.IDPARAM = 32, SZCADGERAL B WHERE A.CODCOLIGADA = @CODCOLIGADA AND A.CODCONVENIO = @CODCOMPRADOR AND A.IDUNIDATEND = @IDUNIDFAT AND B.CODCOLIGADA = A.CODCOLIGADA AND B.CODGERAL = A.CODCONVENIO AND A.DATAVIGENCIA = (SELECT MAX(DATAVIGENCIA) FROM SZCONDCONTRAT I WHERE I.CODCOLIGADA = @CODCOLIGADA AND I.CODCONVENIO = @CODCOMPRADOR AND I.IDUNIDATEND = @IDUNIDFAT AND I.DATAVIGENCIA <= @DATAINI) SELECT @TOTPCTEIPSEMG = 0.00 IF ( ( @PADRAOFATURAMENTO = 36 ) OR ( @PADRAOFATURAMENTO = 39 ) OR ( @PADRAOFATURAMENTO = 33 AND @PADRAOESPECIAL = 5 ) ) SELECT @TOTPCTEIPSEMG = 0.01 SELECT @TOTPACOTE = @TOTPACOTE + @TOTPCTEIPSEMG IF @TOTPACOTE > 0.0 BEGIN IF (SELECT COUNT(*) FROM SZRESUMOCONTAPCTE WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL) <= 0 BEGIN EXECUTE ATUALIZARESUMO @NCONTA, @SCONTA, @TIPO, @CODCOLIGADA, @SEQPARCIAL END IF (SELECT COUNT(*) FROM SZRESUMOCONTAPCTEG WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL) <= 0 BEGIN SELECT @CONTROLEESTORNOGLOSAGG = 1 INSERT INTO SZRESUMOCONTAPCTEG SELECT * FROM SZRESUMOCONTAPCTE WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL END END SELECT @TOTAL_HONO = -2 SELECT @TOTAL_HONOPCTE = -2 SELECT @SEMINFOPAGTO = COUNT(*) FROM SZATENDHONORARIOGLOSA WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL AND ESTORNADO IS NULL AND SEMINFOPGTO = 'T' AND ( ( HONORARIOFORA IS NULL ) OR ( HONORARIOFORA = '' ) OR ( HONORARIOFORA = 'F' ) ) AND ( ( CODPRESTADOR IN (SELECT CODPRESTADOR FROM SZCREDENCIAMENTO WHERE CODCOLIGADA = @CODCOLIGADA AND CODCOMPRADOR = @CODCOMPRADOR AND CREDDIRETO = 'T' AND CODPRESTADOR <> @CPP) ) OR ( CODPRESTADOR IN (SELECT CODPRESTADOR FROM SZCREDENCIAMENTO WHERE CODCOLIGADA = @CODCOLIGADA AND CODCOMPRADOR = @CODCOMPRADOR AND CREDDIRETO = 'F') ) ) IF (SELECT COUNT(*) FROM SZATENDHONORARIOGLOSA WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL AND ESTORNADO IS NULL AND ( ( HONORARIOFORA IS NULL ) OR ( HONORARIOFORA = '' ) OR ( HONORARIOFORA = 'F' ) ) AND ( ( CODPRESTADOR IN (SELECT CODPRESTADOR FROM SZCREDENCIAMENTODETALHE WHERE CODCOLIGADA = @CODCOLIGADA AND CODCOMPRADOR = @CODCOMPRADOR AND ( ( IDUNIDATEND = @IDUNIDATEND ) OR ( IDUNIDATEND IS NULL ) ) AND DATAINCLUSAO <= SZATENDHONORARIOGLOSA.DATAREALIZACAOHM AND COALESCE(DATEXCLUSAO, GETDATE()) > SZATENDHONORARIOGLOSA.DATAREALIZACAOHM AND CREDDIRETO = 'T' AND CODPRESTADOR <> @CPP) ) OR ( CODPRESTADOR IN (SELECT CODPRESTADOR FROM SZCREDENCIAMENTODETALHE WHERE CODCOLIGADA = @CODCOLIGADA AND CODCOMPRADOR = @CODCOMPRADOR AND ( ( IDUNIDATEND = @IDUNIDATEND ) OR ( IDUNIDATEND IS NULL ) ) AND DATAINCLUSAO <= SZATENDHONORARIOGLOSA.DATAREALIZACAOHM AND COALESCE(DATEXCLUSAO, GETDATE()) > SZATENDHONORARIOGLOSA.DATAREALIZACAOHM AND CREDDIRETO = 'F') ) )) = 0 BEGIN SELECT @TOTAL_HONO = (SELECT HMRESUMO FROM SZATENDRESUMOGLOSA WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL) SELECT @AUX = HMRESUMO FROM SZATENDRESUMO WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL IF ( @TOTAL_HONO IS NULL ) OR ( @CONTROLEESTORNOGLOSAGG > 0 ) SET @TOTAL_HONO = @AUX SET @GLOSAGG = 0 IF ( @AUX - @TOTAL_HONO ) <> 0 SET @GLOSAGG = 1 IF (@SEMINFOPAGTO > 0) SET @GLOSAGG = 0 IF @GLOSAGG <> 0 SET @TOTALID = @TOTALID + @TOTAL_HONO END IF ( @TOTAL_HONO < 0 ) OR ( @GLOSAGG = 0 ) BEGIN SELECT @TOTAL_HONO = ISNULL(SUM(VALORTOTALHM), 0), @TOTALID = @TOTALID + ISNULL(SUM(CASE WHEN IDENTIFICADO = 'T' THEN VALORTOTALHM END), 0) FROM SZATENDHONORARIO WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL AND ESTORNADO IS NULL AND COALESCE(SEMINFOPGTO, 'F') = 'F' AND ( ( HONORARIOFORA IS NULL ) OR ( HONORARIOFORA = '' ) OR ( HONORARIOFORA = 'F' ) ) AND ( ( CODPRESTADOR IN (SELECT CODPRESTADOR FROM SZCREDENCIAMENTODETALHE WHERE CODCOLIGADA = @CODCOLIGADA AND CODCOMPRADOR = @CODCOMPRADOR AND ( ( IDUNIDATEND = @IDUNIDATEND ) OR ( IDUNIDATEND IS NULL ) ) AND DATAINCLUSAO <= SZATENDHONORARIO.DATAREALIZACAOHM AND COALESCE(DATEXCLUSAO, GETDATE()) > SZATENDHONORARIO.DATAREALIZACAOHM AND CREDDIRETO = 'T' AND CODPRESTADOR <> @CPP) ) OR ( CODPRESTADOR IN (SELECT CODPRESTADOR FROM SZCREDENCIAMENTODETALHE WHERE CODCOLIGADA = @CODCOLIGADA AND CODCOMPRADOR = @CODCOMPRADOR AND ( ( IDUNIDATEND = @IDUNIDATEND ) OR ( IDUNIDATEND IS NULL ) ) AND DATAINCLUSAO <= SZATENDHONORARIO.DATAREALIZACAOHM AND COALESCE(DATEXCLUSAO, GETDATE()) > SZATENDHONORARIO.DATAREALIZACAOHM AND CREDDIRETO = 'F') ) ) AND SEQDEBITOHM NOT IN (SELECT SEQDEBITOHM FROM SZATENDHONORARIOGLOSA WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL AND ESTORNADO IS NULL AND ( ( HONORARIOFORA IS NULL ) OR ( HONORARIOFORA = '' ) OR ( HONORARIOFORA = 'F' ) ) AND ( ( CODPRESTADOR IN (SELECT CODPRESTADOR FROM SZCREDENCIAMENTODETALHE WHERE CODCOLIGADA = @CODCOLIGADA AND CODCOMPRADOR = @CODCOMPRADOR AND ( ( IDUNIDATEND = @IDUNIDATEND ) OR ( IDUNIDATEND IS NULL ) ) AND DATAINCLUSAO <= SZATENDHONORARIOGLOSA.DATAREALIZACAOHM AND COALESCE(DATEXCLUSAO, GETDATE()) > SZATENDHONORARIOGLOSA.DATAREALIZACAOHM AND CREDDIRETO = 'T' AND CODPRESTADOR <> @CPP) ) OR ( CODPRESTADOR IN (SELECT CODPRESTADOR FROM SZCREDENCIAMENTODETALHE WHERE CODCOLIGADA = @CODCOLIGADA AND CODCOMPRADOR = @CODCOMPRADOR AND ( ( IDUNIDATEND = @IDUNIDATEND ) OR ( IDUNIDATEND IS NULL ) ) AND DATAINCLUSAO <= SZATENDHONORARIOGLOSA.DATAREALIZACAOHM AND COALESCE(DATEXCLUSAO, GETDATE()) > SZATENDHONORARIOGLOSA.DATAREALIZACAOHM AND CREDDIRETO = 'F') ) )) SELECT @TOTAL_HONO = @TOTAL_HONO + ISNULL(SUM(VALORTOTALHM), 0), @TOTALID = @TOTALID + ISNULL(SUM(VALORTOTALHM), 0) FROM SZATENDHONORARIOGLOSA WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL AND ESTORNADO IS NULL AND COALESCE(SEMINFOPGTO, 'F') = 'F' AND ( ( HONORARIOFORA IS NULL ) OR ( HONORARIOFORA = '' ) OR ( HONORARIOFORA = 'F' ) ) AND ( ( CODPRESTADOR IN (SELECT CODPRESTADOR FROM SZCREDENCIAMENTODETALHE WHERE CODCOLIGADA = @CODCOLIGADA AND CODCOMPRADOR = @CODCOMPRADOR AND ( ( IDUNIDATEND = @IDUNIDATEND ) OR ( IDUNIDATEND IS NULL ) ) AND DATAINCLUSAO <= SZATENDHONORARIOGLOSA.DATAREALIZACAOHM AND COALESCE(DATEXCLUSAO, GETDATE()) > SZATENDHONORARIOGLOSA.DATAREALIZACAOHM AND CREDDIRETO = 'T' AND CODPRESTADOR <> @CPP) ) OR ( CODPRESTADOR IN (SELECT CODPRESTADOR FROM SZCREDENCIAMENTODETALHE WHERE CODCOLIGADA = @CODCOLIGADA AND CODCOMPRADOR = @CODCOMPRADOR AND ( ( IDUNIDATEND = @IDUNIDATEND ) OR ( IDUNIDATEND IS NULL ) ) AND DATAINCLUSAO <= SZATENDHONORARIOGLOSA.DATAREALIZACAOHM AND COALESCE(DATEXCLUSAO, GETDATE()) > SZATENDHONORARIOGLOSA.DATAREALIZACAOHM AND CREDDIRETO = 'F') ) ) END IF (SELECT COUNT(*) FROM SZATENDHONORARIOGLOSA WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL AND ESTORNADO IS NULL AND INCLUIDOPACOTE = 'T' AND ( ( HONORARIOFORA IS NULL ) OR ( HONORARIOFORA = '' ) OR ( HONORARIOFORA = 'F' ) ) AND ( ( CODPRESTADOR IN (SELECT CODPRESTADOR FROM SZCREDENCIAMENTODETALHE WHERE CODCOLIGADA = @CODCOLIGADA AND CODCOMPRADOR = @CODCOMPRADOR AND ( ( IDUNIDATEND = @IDUNIDATEND ) OR ( IDUNIDATEND IS NULL ) ) AND DATAINCLUSAO <= SZATENDHONORARIOGLOSA.DATAREALIZACAOHM AND COALESCE(DATEXCLUSAO, GETDATE()) > SZATENDHONORARIOGLOSA.DATAREALIZACAOHM AND CREDDIRETO = 'T' AND CODPRESTADOR <> @CPP) ) OR ( CODPRESTADOR IN (SELECT CODPRESTADOR FROM SZCREDENCIAMENTODETALHE WHERE CODCOLIGADA = @CODCOLIGADA AND CODCOMPRADOR = @CODCOMPRADOR AND ( ( IDUNIDATEND = @IDUNIDATEND ) OR ( IDUNIDATEND IS NULL ) ) AND DATAINCLUSAO <= SZATENDHONORARIOGLOSA.DATAREALIZACAOHM AND COALESCE(DATEXCLUSAO, GETDATE()) > SZATENDHONORARIOGLOSA.DATAREALIZACAOHM AND CREDDIRETO = 'F') ) )) = 0 BEGIN SELECT @TOTAL_HONOPCTE = (SELECT HMRESUMO FROM SZRESUMOCONTAPCTEG WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL) SELECT @AUX = HMRESUMO FROM SZRESUMOCONTAPCTE WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL IF ( @TOTAL_HONOPCTE IS NULL ) OR ( @CONTROLEESTORNOGLOSAGG > 0 ) SET @TOTAL_HONOPCTE = @AUX IF ( @AUX - @TOTAL_HONOPCTE ) <> 0 SET @TOTALIDPCTE = @TOTALIDPCTE + @TOTAL_HONOPCTE END IF ( @TOTPACOTE > 0.0 ) AND ( @TOTAL_HONO > 0.0 ) BEGIN SELECT @AUXIPACOTE = (SELECT COUNT(*) FROM SZATENDHONORARIO WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL AND ESTORNADO IS NULL AND ( ( HONORARIOFORA IS NULL ) OR ( HONORARIOFORA = '' ) OR ( HONORARIOFORA = 'F' ) ) AND INCLUIDOPACOTE = 'T') IF @AUXIPACOTE IS NULL SELECT @AUXIPACOTE = 0.00 IF ( @AUXIPACOTE > 0.0 ) BEGIN SELECT @AUXIPACOTE = ISNULL(SUM(VALORTOTALHM), 0), @TOTALIDPCTE = @TOTALIDPCTE + ISNULL(SUM(CASE WHEN IDENTIFICADO = 'T' THEN VALORTOTALHM END), 0 ) FROM SZATENDHONORARIO WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL AND ESTORNADO IS NULL AND COALESCE(SEMINFOPGTO, 'F') = 'F' AND INCLUIDOPACOTE = 'T' AND ( ( HONORARIOFORA IS NULL ) OR ( HONORARIOFORA = '' ) OR ( HONORARIOFORA = 'F' ) ) AND ( ( CODPRESTADOR IN (SELECT CODPRESTADOR FROM SZCREDENCIAMENTODETALHE WHERE CODCOLIGADA = @CODCOLIGADA AND CODCOMPRADOR = @CODCOMPRADOR AND ( ( IDUNIDATEND = @IDUNIDATEND ) OR ( IDUNIDATEND IS NULL ) ) AND DATAINCLUSAO <= SZATENDHONORARIO.DATAREALIZACAOHM AND COALESCE(DATEXCLUSAO, GETDATE()) > SZATENDHONORARIO.DATAREALIZACAOHM AND CREDDIRETO = 'T' AND CODPRESTADOR <> @CPP) ) OR ( CODPRESTADOR IN (SELECT CODPRESTADOR FROM SZCREDENCIAMENTODETALHE WHERE CODCOLIGADA = @CODCOLIGADA AND CODCOMPRADOR = @CODCOMPRADOR AND ( ( IDUNIDATEND = @IDUNIDATEND ) OR ( IDUNIDATEND IS NULL ) ) AND DATAINCLUSAO <= SZATENDHONORARIO.DATAREALIZACAOHM AND COALESCE(DATEXCLUSAO, GETDATE()) > SZATENDHONORARIO.DATAREALIZACAOHM AND CREDDIRETO = 'F') ) ) AND SEQDEBITOHM NOT IN (SELECT SEQDEBITOHM FROM SZATENDHONORARIOGLOSA WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL AND ESTORNADO IS NULL AND INCLUIDOPACOTE = 'T' AND ( ( HONORARIOFORA IS NULL ) OR ( HONORARIOFORA = '' ) OR ( HONORARIOFORA = 'F' ) ) AND ( ( CODPRESTADOR IN (SELECT CODPRESTADOR FROM SZCREDENCIAMENTODETALHE WHERE CODCOLIGADA = @CODCOLIGADA AND CODCOMPRADOR = @CODCOMPRADOR AND ( ( IDUNIDATEND = @IDUNIDATEND ) OR ( IDUNIDATEND IS NULL ) ) AND DATAINCLUSAO <= SZATENDHONORARIOGLOSA.DATAREALIZACAOHM AND COALESCE(DATEXCLUSAO, GETDATE()) > SZATENDHONORARIOGLOSA.DATAREALIZACAOHM AND CREDDIRETO = 'T' AND CODPRESTADOR <> @CPP) ) OR ( CODPRESTADOR IN (SELECT CODPRESTADOR FROM SZCREDENCIAMENTODETALHE WHERE CODCOLIGADA = @CODCOLIGADA AND CODCOMPRADOR = @CODCOMPRADOR AND ( ( IDUNIDATEND = @IDUNIDATEND ) OR ( IDUNIDATEND IS NULL ) ) AND DATAINCLUSAO <= SZATENDHONORARIOGLOSA.DATAREALIZACAOHM AND COALESCE(DATEXCLUSAO, GETDATE()) > SZATENDHONORARIOGLOSA.DATAREALIZACAOHM AND CREDDIRETO = 'F') ) )) SELECT @AUXIPACOTE = @AUXIPACOTE + ISNULL(SUM(VALORTOTALHM), 0), @TOTALIDPCTE = @TOTALIDPCTE + ISNULL(SUM(VALORTOTALHM), 0) FROM SZATENDHONORARIOGLOSA WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL AND ESTORNADO IS NULL AND COALESCE(SEMINFOPGTO, 'F') = 'F' AND INCLUIDOPACOTE = 'T' AND ( ( HONORARIOFORA IS NULL ) OR ( HONORARIOFORA = '' ) OR ( HONORARIOFORA = 'F' ) ) AND ( ( CODPRESTADOR IN (SELECT CODPRESTADOR FROM SZCREDENCIAMENTODETALHE WHERE CODCOLIGADA = @CODCOLIGADA AND CODCOMPRADOR = @CODCOMPRADOR AND ( ( IDUNIDATEND = @IDUNIDATEND ) OR ( IDUNIDATEND IS NULL ) ) AND DATAINCLUSAO <= SZATENDHONORARIOGLOSA.DATAREALIZACAOHM AND COALESCE(DATEXCLUSAO, GETDATE()) > SZATENDHONORARIOGLOSA.DATAREALIZACAOHM AND CREDDIRETO = 'T' AND CODPRESTADOR <> @CPP) ) OR ( CODPRESTADOR IN (SELECT CODPRESTADOR FROM SZCREDENCIAMENTODETALHE WHERE CODCOLIGADA = @CODCOLIGADA AND CODCOMPRADOR = @CODCOMPRADOR AND ( ( IDUNIDATEND = @IDUNIDATEND ) OR ( IDUNIDATEND IS NULL ) ) AND DATAINCLUSAO <= SZATENDHONORARIOGLOSA.DATAREALIZACAOHM AND COALESCE(DATEXCLUSAO, GETDATE()) > SZATENDHONORARIOGLOSA.DATAREALIZACAOHM AND CREDDIRETO = 'F') ) ) END SELECT @TOTPACOTESUM = @TOTPACOTESUM + @AUXIPACOTE SELECT @TOTAL_HONOPCTE = @AUXIPACOTE END ELSE SELECT @TOTAL_HONOPCTE = 0 SELECT @TOTAL_HONO_NCRED = -2 SELECT @TOTAL_HONO_NCREDPCTE = -2 SELECT @SEMINFOPAGTO = COUNT(*) FROM SZATENDHONORARIOGLOSA WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL AND ESTORNADO IS NULL AND SEMINFOPGTO = 'T' AND ( ( HONORARIOFORA IS NULL ) OR ( HONORARIOFORA = '' ) OR ( HONORARIOFORA = 'F' ) ) AND ( CODPRESTADOR NOT IN (SELECT CODPRESTADOR FROM SZCREDENCIAMENTO WHERE CODCOLIGADA = @CODCOLIGADA AND CODCOMPRADOR = @CODCOMPRADOR) OR ( CODPRESTADOR IN (SELECT CODPRESTADOR FROM SZCREDENCIAMENTO WHERE CODCOLIGADA = @CODCOLIGADA AND CODCOMPRADOR = @CODCOMPRADOR AND CREDDIRETO = 'T' AND CODPRESTADOR = @CPP) ) ) IF (SELECT COUNT(*) FROM SZATENDHONORARIOGLOSA WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL AND ESTORNADO IS NULL AND ( ( HONORARIOFORA IS NULL ) OR ( HONORARIOFORA = '' ) OR ( HONORARIOFORA = 'F' ) ) AND ( CODPRESTADOR NOT IN (SELECT CODPRESTADOR FROM SZCREDENCIAMENTODETALHE WHERE CODCOLIGADA = @CODCOLIGADA AND CODCOMPRADOR = @CODCOMPRADOR AND ( ( IDUNIDATEND = @IDUNIDATEND ) OR ( IDUNIDATEND IS NULL ) ) AND DATAINCLUSAO <= SZATENDHONORARIOGLOSA.DATAREALIZACAOHM AND COALESCE(DATEXCLUSAO, GETDATE()) > SZATENDHONORARIOGLOSA.DATAREALIZACAOHM) OR ( CODPRESTADOR IN (SELECT CODPRESTADOR FROM SZCREDENCIAMENTODETALHE WHERE CODCOLIGADA = @CODCOLIGADA AND CODCOMPRADOR = @CODCOMPRADOR AND ( ( IDUNIDATEND = @IDUNIDATEND ) OR ( IDUNIDATEND IS NULL ) ) AND DATAINCLUSAO <= SZATENDHONORARIOGLOSA.DATAREALIZACAOHM AND COALESCE(DATEXCLUSAO, GETDATE()) > SZATENDHONORARIOGLOSA.DATAREALIZACAOHM AND CREDDIRETO = 'T' AND CODPRESTADOR = @CPP) ) )) = 0 BEGIN SELECT @TOTAL_HONO_NCRED = (SELECT HMRESUMONCRED FROM SZATENDRESUMOGLOSA WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL) SELECT @AUX = HMRESUMONCRED FROM SZATENDRESUMO WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL IF ( @TOTAL_HONO_NCRED IS NULL ) OR ( @CONTROLEESTORNOGLOSAGG > 0 ) SET @TOTAL_HONO_NCRED = @AUX SET @GLOSAGG = 0 IF ( @TOTAL_HONO_NCRED - @AUX ) <> 0 SET @GLOSAGG = 1 IF (@SEMINFOPAGTO > 0) SET @GLOSAGG = 0 IF @GLOSAGG <> 0 SET @TOTALID = @TOTALID + @TOTAL_HONO_NCRED END IF ( @TOTAL_HONO_NCRED < 0 ) OR ( @GLOSAGG = 0 ) BEGIN SELECT @TOTAL_HONO_NCRED = ISNULL(SUM(VALORTOTALHM), 0), @TOTALID = @TOTALID + ISNULL(SUM(CASE WHEN IDENTIFICADO = 'T' THEN VALORTOTALHM END), 0) FROM SZATENDHONORARIO WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL AND ESTORNADO IS NULL AND COALESCE(SEMINFOPGTO, 'F') = 'F' AND ( ( HONORARIOFORA IS NULL ) OR ( HONORARIOFORA = '' ) OR ( HONORARIOFORA = 'F' ) ) AND ( CODPRESTADOR NOT IN (SELECT CODPRESTADOR FROM SZCREDENCIAMENTODETALHE WHERE CODCOLIGADA = @CODCOLIGADA AND CODCOMPRADOR = @CODCOMPRADOR AND ( ( IDUNIDATEND = @IDUNIDATEND ) OR ( IDUNIDATEND IS NULL ) ) AND DATAINCLUSAO <= SZATENDHONORARIO.DATAREALIZACAOHM AND COALESCE(DATEXCLUSAO, GETDATE()) > SZATENDHONORARIO.DATAREALIZACAOHM) OR ( CODPRESTADOR IN (SELECT CODPRESTADOR FROM SZCREDENCIAMENTODETALHE WHERE CODCOLIGADA = @CODCOLIGADA AND CODCOMPRADOR = @CODCOMPRADOR AND ( ( IDUNIDATEND = @IDUNIDATEND ) OR ( IDUNIDATEND IS NULL ) ) AND DATAINCLUSAO <= SZATENDHONORARIO.DATAREALIZACAOHM AND COALESCE(DATEXCLUSAO, GETDATE()) > SZATENDHONORARIO.DATAREALIZACAOHM AND CREDDIRETO = 'T' AND CODPRESTADOR = @CPP) ) ) AND SEQDEBITOHM NOT IN (SELECT SEQDEBITOHM FROM SZATENDHONORARIOGLOSA WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL AND ESTORNADO IS NULL AND ( ( HONORARIOFORA IS NULL ) OR ( HONORARIOFORA = '' ) OR ( HONORARIOFORA = 'F' ) ) AND ( CODPRESTADOR NOT IN (SELECT CODPRESTADOR FROM SZCREDENCIAMENTODETALHE WHERE CODCOLIGADA = @CODCOLIGADA AND CODCOMPRADOR = @CODCOMPRADOR AND ( ( IDUNIDATEND = @IDUNIDATEND ) OR ( IDUNIDATEND IS NULL ) ) AND DATAINCLUSAO <= SZATENDHONORARIOGLOSA.DATAREALIZACAOHM AND COALESCE(DATEXCLUSAO, GETDATE()) > SZATENDHONORARIOGLOSA.DATAREALIZACAOHM) OR ( CODPRESTADOR IN (SELECT CODPRESTADOR FROM SZCREDENCIAMENTODETALHE WHERE CODCOLIGADA = @CODCOLIGADA AND CODCOMPRADOR = @CODCOMPRADOR AND ( ( IDUNIDATEND = @IDUNIDATEND ) OR ( IDUNIDATEND IS NULL ) ) AND DATAINCLUSAO <= SZATENDHONORARIOGLOSA.DATAREALIZACAOHM AND COALESCE(DATEXCLUSAO, GETDATE()) > SZATENDHONORARIOGLOSA.DATAREALIZACAOHM AND CREDDIRETO = 'T' AND CODPRESTADOR = @CPP) ) )) SELECT @TOTAL_HONO_NCRED = @TOTAL_HONO_NCRED + ISNULL(SUM(VALORTOTALHM), 0), @TOTALID = @TOTALID + ISNULL(SUM(VALORTOTALHM), 0) FROM SZATENDHONORARIOGLOSA WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL AND ESTORNADO IS NULL AND COALESCE(SEMINFOPGTO, 'F') = 'F' AND ( ( HONORARIOFORA IS NULL ) OR ( HONORARIOFORA = '' ) OR ( HONORARIOFORA = 'F' ) ) AND ( CODPRESTADOR NOT IN (SELECT CODPRESTADOR FROM SZCREDENCIAMENTODETALHE WHERE CODCOLIGADA = @CODCOLIGADA AND CODCOMPRADOR = @CODCOMPRADOR AND ( ( IDUNIDATEND = @IDUNIDATEND ) OR ( IDUNIDATEND IS NULL ) ) AND DATAINCLUSAO <= SZATENDHONORARIOGLOSA.DATAREALIZACAOHM AND COALESCE(DATEXCLUSAO, GETDATE()) > SZATENDHONORARIOGLOSA.DATAREALIZACAOHM) OR ( CODPRESTADOR IN (SELECT CODPRESTADOR FROM SZCREDENCIAMENTODETALHE WHERE CODCOLIGADA = @CODCOLIGADA AND CODCOMPRADOR = @CODCOMPRADOR AND ( ( IDUNIDATEND = @IDUNIDATEND ) OR ( IDUNIDATEND IS NULL ) ) AND DATAINCLUSAO <= SZATENDHONORARIOGLOSA.DATAREALIZACAOHM AND COALESCE(DATEXCLUSAO, GETDATE()) > SZATENDHONORARIOGLOSA.DATAREALIZACAOHM AND CREDDIRETO = 'T' AND CODPRESTADOR = @CPP) ) ) END IF (SELECT COUNT(*) FROM SZATENDHONORARIOGLOSA WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL AND ESTORNADO IS NULL AND INCLUIDOPACOTE = 'T' AND ( ( HONORARIOFORA IS NULL ) OR ( HONORARIOFORA = '' ) OR ( HONORARIOFORA = 'F' ) ) AND ( NOT CODPRESTADOR IN (SELECT CODPRESTADOR FROM SZCREDENCIAMENTODETALHE WHERE CODCOLIGADA = @CODCOLIGADA AND CODCOMPRADOR = @CODCOMPRADOR AND ( ( IDUNIDATEND = @IDUNIDATEND ) OR ( IDUNIDATEND IS NULL ) ) AND DATAINCLUSAO <= SZATENDHONORARIOGLOSA.DATAREALIZACAOHM AND COALESCE(DATEXCLUSAO, GETDATE()) > SZATENDHONORARIOGLOSA.DATAREALIZACAOHM) OR ( CODPRESTADOR IN (SELECT CODPRESTADOR FROM SZCREDENCIAMENTODETALHE WHERE CODCOLIGADA = @CODCOLIGADA AND CODCOMPRADOR = @CODCOMPRADOR AND ( ( IDUNIDATEND = @IDUNIDATEND ) OR ( IDUNIDATEND IS NULL ) ) AND DATAINCLUSAO <= SZATENDHONORARIOGLOSA.DATAREALIZACAOHM AND COALESCE(DATEXCLUSAO, GETDATE()) > SZATENDHONORARIOGLOSA.DATAREALIZACAOHM AND CREDDIRETO = 'T' AND CODPRESTADOR = @CPP) ) )) = 0 BEGIN SELECT @TOTAL_HONO_NCREDPCTE = (SELECT HMRESUMONCRED FROM SZRESUMOCONTAPCTEG WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL) SELECT @AUX = HMRESUMONCRED FROM SZRESUMOCONTAPCTE WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL IF ( @TOTAL_HONO_NCREDPCTE IS NULL ) OR ( @CONTROLEESTORNOGLOSAGG > 0 ) SET @TOTAL_HONO_NCREDPCTE = @AUX IF ( @TOTAL_HONO_NCREDPCTE - @AUX ) <> 0 SET @TOTALIDPCTE = @TOTALIDPCTE + @TOTAL_HONO_NCREDPCTE END IF ( @TOTPACOTE > 0.0 ) AND ( @TOTAL_HONO_NCRED > 0.0 ) BEGIN SELECT @AUXIPACOTE = (SELECT COUNT(*) FROM SZATENDHONORARIO WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL AND ESTORNADO IS NULL AND ( ( HONORARIOFORA IS NULL ) OR ( HONORARIOFORA = '' ) OR ( HONORARIOFORA = 'F' ) ) AND INCLUIDOPACOTE = 'T') IF @AUXIPACOTE IS NULL SELECT @AUXIPACOTE = 0.00 IF ( @AUXIPACOTE > 0.0 ) BEGIN SELECT @AUXIPACOTE = ISNULL(SUM(VALORTOTALHM), 0), @TOTALIDPCTE = @TOTALIDPCTE + ISNULL(SUM(CASE WHEN IDENTIFICADO = 'T' THEN VALORTOTALHM END), 0 ) FROM SZATENDHONORARIO WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL AND ESTORNADO IS NULL AND COALESCE(SEMINFOPGTO, 'F') = 'F' AND INCLUIDOPACOTE = 'T' AND ( ( HONORARIOFORA IS NULL ) OR ( HONORARIOFORA = '' ) OR ( HONORARIOFORA = 'F' ) ) AND ( CODPRESTADOR NOT IN (SELECT CODPRESTADOR FROM SZCREDENCIAMENTODETALHE WHERE CODCOLIGADA = @CODCOLIGADA AND CODCOMPRADOR = @CODCOMPRADOR AND ( ( IDUNIDATEND = @IDUNIDATEND ) OR ( IDUNIDATEND IS NULL ) ) AND DATAINCLUSAO <= SZATENDHONORARIO.DATAREALIZACAOHM AND COALESCE(DATEXCLUSAO, GETDATE()) > SZATENDHONORARIO.DATAREALIZACAOHM) OR ( CODPRESTADOR IN (SELECT CODPRESTADOR FROM SZCREDENCIAMENTODETALHE WHERE CODCOLIGADA = @CODCOLIGADA AND CODCOMPRADOR = @CODCOMPRADOR AND ( ( IDUNIDATEND = @IDUNIDATEND ) OR ( IDUNIDATEND IS NULL ) ) AND DATAINCLUSAO <= SZATENDHONORARIO.DATAREALIZACAOHM AND COALESCE(DATEXCLUSAO, GETDATE()) > SZATENDHONORARIO.DATAREALIZACAOHM AND CREDDIRETO = 'T' AND SZATENDHONORARIO.CODPRESTADOR = @CPP) ) ) AND SEQDEBITOHM NOT IN (SELECT SEQDEBITOHM FROM SZATENDHONORARIOGLOSA WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL AND ESTORNADO IS NULL AND INCLUIDOPACOTE = 'T' AND ( ( HONORARIOFORA IS NULL ) OR ( HONORARIOFORA = '' ) OR ( HONORARIOFORA = 'F' ) ) AND ( CODPRESTADOR NOT IN (SELECT CODPRESTADOR FROM SZCREDENCIAMENTODETALHE WHERE CODCOMPRADOR = @CODCOMPRADOR AND CODCOLIGADA = @CODCOLIGADA AND ( ( IDUNIDATEND = @IDUNIDATEND ) OR ( IDUNIDATEND IS NULL ) ) AND DATAINCLUSAO <= SZATENDHONORARIOGLOSA.DATAREALIZACAOHM AND COALESCE(DATEXCLUSAO, GETDATE()) > SZATENDHONORARIOGLOSA.DATAREALIZACAOHM) OR ( CODPRESTADOR IN (SELECT CODPRESTADOR FROM SZCREDENCIAMENTODETALHE WHERE CODCOMPRADOR = @CODCOMPRADOR AND CODCOLIGADA = @CODCOLIGADA AND ( ( IDUNIDATEND = @IDUNIDATEND ) OR ( IDUNIDATEND IS NULL ) ) AND DATAINCLUSAO <= SZATENDHONORARIOGLOSA.DATAREALIZACAOHM AND COALESCE(DATEXCLUSAO, GETDATE()) > SZATENDHONORARIOGLOSA.DATAREALIZACAOHM AND CREDDIRETO = 'T' AND CODPRESTADOR = @CPP) ) )) SELECT @AUXIPACOTE = @AUXIPACOTE + ISNULL(SUM(VALORTOTALHM), 0), @TOTALIDPCTE = @TOTALIDPCTE + ISNULL(SUM(VALORTOTALHM), 0 ) FROM SZATENDHONORARIOGLOSA WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL AND ESTORNADO IS NULL AND COALESCE(SEMINFOPGTO, 'F') = 'F' AND INCLUIDOPACOTE = 'T' AND ( ( HONORARIOFORA IS NULL ) OR ( HONORARIOFORA = '' ) OR ( HONORARIOFORA = 'F' ) ) AND ( CODPRESTADOR NOT IN (SELECT CODPRESTADOR FROM SZCREDENCIAMENTODETALHE WHERE CODCOMPRADOR = @CODCOMPRADOR AND CODCOLIGADA = @CODCOLIGADA AND ( ( IDUNIDATEND = @IDUNIDATEND ) OR ( IDUNIDATEND IS NULL ) ) AND DATAINCLUSAO <= SZATENDHONORARIOGLOSA.DATAREALIZACAOHM AND COALESCE(DATEXCLUSAO, GETDATE()) > SZATENDHONORARIOGLOSA.DATAREALIZACAOHM) OR ( CODPRESTADOR IN (SELECT CODPRESTADOR FROM SZCREDENCIAMENTODETALHE WHERE CODCOMPRADOR = @CODCOMPRADOR AND CODCOLIGADA = @CODCOLIGADA AND ( ( IDUNIDATEND = @IDUNIDATEND ) OR ( IDUNIDATEND IS NULL ) ) AND DATAINCLUSAO <= SZATENDHONORARIOGLOSA.DATAREALIZACAOHM AND COALESCE(DATEXCLUSAO, GETDATE()) > SZATENDHONORARIOGLOSA.DATAREALIZACAOHM AND CREDDIRETO = 'T' AND CODPRESTADOR = @CPP) ) ) END SELECT @TOTPACOTESUM = @TOTPACOTESUM + @AUXIPACOTE SELECT @TOTAL_HONO_NCREDPCTE = @AUXIPACOTE END ELSE SELECT @TOTAL_HONO_NCREDPCTE = 0 SELECT @TOTAL_DIARIAS = -2 SELECT @TOTAL_DIARIASPCTE = -2 SELECT @SEMINFOPAGTO = COUNT(*) FROM SZATENDDIARIASGLOSA WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL AND ESTORNADO IS NULL AND SEMINFOPGTO = 'T' AND CODGGASTO = '02' IF (SELECT COUNT(*) FROM SZATENDDIARIASGLOSA WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL AND ESTORNADO IS NULL AND CODGGASTO = '02') = 0 BEGIN SELECT @TOTAL_DIARIAS = (SELECT DIARIASRESUMO FROM SZATENDRESUMOGLOSA WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL) SELECT @AUX = DIARIASRESUMO FROM SZATENDRESUMO WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL IF ( @TOTAL_DIARIAS IS NULL ) OR ( @CONTROLEESTORNOGLOSAGG > 0 ) SET @TOTAL_DIARIAS = @AUX SET @GLOSAGG = 0 IF ( @TOTAL_DIARIAS - @AUX ) <> 0 SET @GLOSAGG = 1 IF (@SEMINFOPAGTO > 0) SET @GLOSAGG = 0 IF @GLOSAGG <> 0 SET @TOTALID = @TOTALID + @TOTAL_DIARIAS END IF ( @TOTAL_DIARIAS < 0 ) OR ( @GLOSAGG = 0 ) BEGIN SELECT @TOTAL_DIARIAS = ISNULL(SUM(VALORTOTALDIARIAS), 0), @TOTALID = @TOTALID + ISNULL(SUM(CASE WHEN IDENTIFICADO = 'T' THEN VALORTOTALDIARIAS END), 0) FROM SZATENDDIARIAS WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL AND ESTORNADO IS NULL AND COALESCE(SEMINFOPGTO, 'F') = 'F' AND SEQDEBITODIARIA NOT IN (SELECT SEQDEBITODIARIA FROM SZATENDDIARIASGLOSA WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL AND ESTORNADO IS NULL) SELECT @TOTAL_DIARIAS = @TOTAL_DIARIAS + ISNULL(SUM(VALORTOTALDIARIAS), 0), @TOTALID = @TOTALID + ISNULL(SUM(VALORTOTALDIARIAS), 0) FROM SZATENDDIARIASGLOSA WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL AND ESTORNADO IS NULL AND COALESCE(SEMINFOPGTO, 'F') = 'F' END IF (SELECT COUNT(*) FROM SZATENDDIARIASGLOSA WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL AND ESTORNADO IS NULL AND CODGGASTO = '02' AND INCLUIDOPACOTE = 'T') = 0 BEGIN SELECT @TOTAL_DIARIASPCTE = (SELECT DIARIASRESUMO FROM SZRESUMOCONTAPCTEG WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL) SELECT @AUX = DIARIASRESUMO FROM SZRESUMOCONTAPCTE WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL IF ( @TOTAL_DIARIASPCTE IS NULL ) OR ( @CONTROLEESTORNOGLOSAGG > 0 ) SET @TOTAL_DIARIASPCTE = @AUX IF ( @TOTAL_DIARIASPCTE - @AUX ) <> 0 SET @TOTALIDPCTE = @TOTALIDPCTE + @TOTAL_DIARIASPCTE END IF ( @TOTPACOTE > 0.0 ) AND ( @TOTAL_DIARIAS > 0.0 ) BEGIN SELECT @AUXIPACOTE = (SELECT COUNT(*) FROM SZATENDDIARIAS WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL AND ESTORNADO IS NULL AND INCLUIDOPACOTE = 'T') IF @AUXIPACOTE IS NULL SELECT @AUXIPACOTE = 0.00 IF ( @AUXIPACOTE > 0.0 ) BEGIN SELECT @AUXIPACOTE = ISNULL(SUM(VALORTOTALDIARIAS), 0), @TOTALIDPCTE = @TOTALIDPCTE + ISNULL(SUM(CASE WHEN IDENTIFICADO = 'T' THEN VALORTOTALDIARIAS END), 0) FROM SZATENDDIARIAS WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL AND ESTORNADO IS NULL AND COALESCE(SEMINFOPGTO, 'F') = 'F' AND INCLUIDOPACOTE = 'T' AND SEQDEBITODIARIA NOT IN (SELECT SEQDEBITODIARIA FROM SZATENDDIARIASGLOSA WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL AND ESTORNADO IS NULL AND INCLUIDOPACOTE = 'T') SELECT @AUXIPACOTE = @AUXIPACOTE + ISNULL(SUM(VALORTOTALDIARIAS), 0), @TOTALIDPCTE = @TOTALIDPCTE + ISNULL(SUM(VALORTOTALDIARIAS), 0) FROM SZATENDDIARIASGLOSA WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL AND ESTORNADO IS NULL AND COALESCE(SEMINFOPGTO, 'F') = 'F' AND INCLUIDOPACOTE = 'T' END SELECT @TOTPACOTESUM = @TOTPACOTESUM + @AUXIPACOTE SELECT @TOTAL_DIARIASPCTE = @AUXIPACOTE END ELSE SELECT @TOTAL_DIARIASPCTE = 0 UPDATE SZATENDRESUMOGLOSA SET SADTRESUMO = 0.00 WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL SELECT @SADT_HOSPITAL = -2 SELECT @SADT_HOSPITALPCTE = -2 SELECT @SEMINFOPAGTO = COUNT(*) FROM SZATENDSADTGLOSA WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL AND ESTORNADO IS NULL AND SEMINFOPGTO = 'T' AND ( ( EXAMEFORA IS NULL ) OR ( EXAMEFORA = '' ) OR ( EXAMEFORA = 'F' ) ) AND CODPRESTADOR = @CODPRESTADOR IF (SELECT COUNT(*) FROM SZATENDSADTGLOSA WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL AND ESTORNADO IS NULL AND ( ( EXAMEFORA IS NULL ) OR ( EXAMEFORA = '' ) OR ( EXAMEFORA = 'F' ) ) AND CODPRESTADOR = @CODPRESTADOR) = 0 BEGIN SELECT @SADT_HOSPITAL = (SELECT SADTHOSPITAL FROM SZATENDRESUMOGLOSA WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL) SELECT @AUX = SADTHOSPITAL FROM SZATENDRESUMO WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL IF ( @SADT_HOSPITAL IS NULL ) OR ( @CONTROLEESTORNOGLOSAGG > 0 ) SET @SADT_HOSPITAL = @AUX SET @GLOSAGG = 0 IF ( @SADT_HOSPITAL - @AUX ) <> 0 SET @GLOSAGG = 1 IF (@SEMINFOPAGTO > 0) SET @GLOSAGG = 0 IF @GLOSAGG <> 0 SET @TOTALID = @TOTALID + @SADT_HOSPITAL END IF ( @SADT_HOSPITAL < 0 ) OR ( @GLOSAGG = 0 ) BEGIN SELECT @SADT_HOSPITAL = ISNULL(SUM(VALORTOTALSADT), 0), @TOTALID = @TOTALID + ISNULL(SUM(CASE WHEN IDENTIFICADO = 'T' THEN VALORTOTALSADT END), 0) FROM SZATENDSADT WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL AND ( ( EXAMEFORA IS NULL ) OR ( EXAMEFORA = '' ) OR ( EXAMEFORA = 'F' ) ) AND ESTORNADO IS NULL AND COALESCE(SEMINFOPGTO, 'F') = 'F' AND CODPRESTADOR = @CODPRESTADOR AND SEQDEBITOSADT NOT IN (SELECT SEQDEBITOSADT FROM SZATENDSADTGLOSA WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL AND ( ( EXAMEFORA IS NULL ) OR ( EXAMEFORA = '' ) OR ( EXAMEFORA = 'F' ) ) AND ESTORNADO IS NULL AND CODPRESTADOR = @CODPRESTADOR) SELECT @SADT_HOSPITAL = @SADT_HOSPITAL + ISNULL(SUM(VALORTOTALSADT), 0), @TOTALID = @TOTALID + ISNULL(SUM(VALORTOTALSADT), 0) FROM SZATENDSADTGLOSA WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL AND ESTORNADO IS NULL AND COALESCE(SEMINFOPGTO, 'F') = 'F' AND ( ( EXAMEFORA IS NULL ) OR ( EXAMEFORA = '' ) OR ( EXAMEFORA = 'F' ) ) AND CODPRESTADOR = @CODPRESTADOR END IF (SELECT COUNT(*) FROM SZATENDSADTGLOSA WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL AND ESTORNADO IS NULL AND INCLUIDOPACOTE = 'T' AND ( ( EXAMEFORA IS NULL ) OR ( EXAMEFORA = '' ) OR ( EXAMEFORA = 'F' ) ) AND CODPRESTADOR = @CODPRESTADOR) = 0 BEGIN SELECT @SADT_HOSPITALPCTE = (SELECT SADTHOSPITAL FROM SZRESUMOCONTAPCTEG WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL) SELECT @AUX = SADTHOSPITAL FROM SZRESUMOCONTAPCTE WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL IF ( @SADT_HOSPITALPCTE IS NULL ) OR ( @CONTROLEESTORNOGLOSAGG > 0 ) SET @SADT_HOSPITALPCTE = @AUX IF ( @SADT_HOSPITALPCTE - @AUX ) <> 0 SET @TOTALIDPCTE = @TOTALIDPCTE + @SADT_HOSPITALPCTE END IF ( @TOTPACOTE > 0.0 ) AND ( @SADT_HOSPITAL > 0.0 ) BEGIN SELECT @AUXIPACOTE = (SELECT COUNT(*) FROM SZATENDSADT WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL AND ESTORNADO IS NULL AND INCLUIDOPACOTE = 'T' AND CODPRESTADOR = @CODPRESTADOR) IF @AUXIPACOTE IS NULL SELECT @AUXIPACOTE = 0.00 IF ( @AUXIPACOTE > 0.0 ) BEGIN SELECT @AUXIPACOTE = ISNULL(SUM(VALORTOTALSADT), 0), @TOTALIDPCTE = @TOTALIDPCTE + ISNULL(SUM(CASE WHEN IDENTIFICADO = 'T' THEN VALORTOTALSADT END), 0) FROM SZATENDSADT WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL AND ( ( EXAMEFORA IS NULL ) OR ( EXAMEFORA = '' ) OR ( EXAMEFORA = 'F' ) ) AND ESTORNADO IS NULL AND COALESCE(SEMINFOPGTO, 'F') = 'F' AND INCLUIDOPACOTE = 'T' AND CODPRESTADOR = @CODPRESTADOR AND SEQDEBITOSADT NOT IN (SELECT SEQDEBITOSADT FROM SZATENDSADTGLOSA WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL AND ( ( EXAMEFORA IS NULL ) OR ( EXAMEFORA = '' ) OR ( EXAMEFORA = 'F' ) ) AND ESTORNADO IS NULL AND INCLUIDOPACOTE = 'T' AND CODPRESTADOR = @CODPRESTADOR) SELECT @AUXIPACOTE = @AUXIPACOTE + ISNULL(SUM(VALORTOTALSADT), 0 ), @TOTALIDPCTE = @TOTALIDPCTE + ISNULL(SUM(VALORTOTALSADT), 0) FROM SZATENDSADTGLOSA WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL AND ESTORNADO IS NULL AND COALESCE(SEMINFOPGTO, 'F') = 'F' AND INCLUIDOPACOTE = 'T' AND ( ( EXAMEFORA IS NULL ) OR ( EXAMEFORA = '' ) OR ( EXAMEFORA = 'F' ) ) AND CODPRESTADOR = @CODPRESTADOR END SELECT @TOTPACOTESUM = @TOTPACOTESUM + @AUXIPACOTE SELECT @SADT_HOSPITALPCTE = @AUXIPACOTE END ELSE SELECT @SADT_HOSPITALPCTE = 0 SELECT @SADT_TERCEIROS = -2 SELECT @SADT_TERCEIROSPCTE = -2 SELECT @SEMINFOPAGTO = COUNT(*) FROM SZATENDSADTGLOSA WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL AND ESTORNADO IS NULL AND SEMINFOPGTO = 'T' AND ( ( EXAMEFORA IS NULL ) OR ( EXAMEFORA = '' ) OR ( EXAMEFORA = 'F' ) ) AND ( ( CODPRESTADOR <> @CODPRESTADOR ) OR ( CODPRESTADOR IS NULL ) ) IF (SELECT COUNT(*) FROM SZATENDSADTGLOSA WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL AND ESTORNADO IS NULL AND ( ( EXAMEFORA IS NULL ) OR ( EXAMEFORA = '' ) OR ( EXAMEFORA = 'F' ) ) AND ( ( CODPRESTADOR <> @CODPRESTADOR ) OR ( CODPRESTADOR IS NULL ) )) = 0 BEGIN SELECT @SADT_TERCEIROS = (SELECT SADTTERCEIROS FROM SZATENDRESUMOGLOSA WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL) SELECT @AUX = SADTTERCEIROS FROM SZATENDRESUMO WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL IF ( @SADT_TERCEIROS IS NULL ) OR ( @CONTROLEESTORNOGLOSAGG > 0 ) SET @SADT_TERCEIROS = @AUX SET @GLOSAGG = 0 IF ( @SADT_TERCEIROS - @AUX ) <> 0 SET @GLOSAGG = 1 IF (@SEMINFOPAGTO > 0) SET @GLOSAGG = 0 IF @GLOSAGG <> 0 SET @TOTALID = @TOTALID + @SADT_TERCEIROS END IF ( @SADT_TERCEIROS < 0 ) OR ( @GLOSAGG = 0 ) BEGIN SELECT @SADT_TERCEIROS = ISNULL(SUM(VALORTOTALSADT), 0), @TOTALID = @TOTALID + ISNULL(SUM(CASE WHEN IDENTIFICADO = 'T' THEN VALORTOTALSADT END), 0) FROM SZATENDSADT WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL AND ESTORNADO IS NULL AND COALESCE(SEMINFOPGTO, 'F') = 'F' AND ( ( EXAMEFORA IS NULL ) OR ( EXAMEFORA = '' ) OR ( EXAMEFORA = 'F' ) ) AND ( ( CODPRESTADOR <> @CODPRESTADOR ) OR ( CODPRESTADOR IS NULL ) ) AND SEQDEBITOSADT NOT IN (SELECT SEQDEBITOSADT FROM SZATENDSADTGLOSA WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL AND ESTORNADO IS NULL AND ( ( EXAMEFORA IS NULL ) OR ( EXAMEFORA = '' ) OR ( EXAMEFORA = 'F' ) ) AND ( ( CODPRESTADOR <> @CODPRESTADOR ) OR ( CODPRESTADOR IS NULL ) ) ) SELECT @SADT_TERCEIROS = @SADT_TERCEIROS + ISNULL(SUM(VALORTOTALSADT), 0), @TOTALID = @TOTALID + ISNULL(SUM(VALORTOTALSADT), 0) FROM SZATENDSADTGLOSA WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL AND ESTORNADO IS NULL AND COALESCE(SEMINFOPGTO, 'F') = 'F' AND ( ( EXAMEFORA IS NULL ) OR ( EXAMEFORA = '' ) OR ( EXAMEFORA = 'F' ) ) AND ( ( CODPRESTADOR <> @CODPRESTADOR ) OR ( CODPRESTADOR IS NULL ) ) END IF (SELECT COUNT(*) FROM SZATENDSADTGLOSA WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL AND ESTORNADO IS NULL AND INCLUIDOPACOTE = 'T' AND ( ( EXAMEFORA IS NULL ) OR ( EXAMEFORA = '' ) OR ( EXAMEFORA = 'F' ) ) AND ( ( CODPRESTADOR <> @CODPRESTADOR ) OR ( CODPRESTADOR IS NULL ) )) = 0 BEGIN SELECT @SADT_TERCEIROSPCTE = (SELECT SADTTERCEIROS FROM SZRESUMOCONTAPCTEG WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL) SELECT @AUX = SADTTERCEIROS FROM SZRESUMOCONTAPCTE WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL IF ( @SADT_TERCEIROSPCTE IS NULL ) OR ( @CONTROLEESTORNOGLOSAGG > 0 ) SET @SADT_TERCEIROSPCTE = @AUX IF ( @SADT_TERCEIROSPCTE - @AUX ) <> 0 SET @TOTALIDPCTE = @TOTALIDPCTE + @SADT_TERCEIROSPCTE END IF ( @TOTPACOTE > 0.0 ) AND ( @SADT_TERCEIROS > 0.0 ) BEGIN SELECT @AUXIPACOTE = (SELECT COUNT(*) FROM SZATENDSADT WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL AND ESTORNADO IS NULL AND INCLUIDOPACOTE = 'T' AND ( ( CODPRESTADOR <> @CODPRESTADOR ) OR ( CODPRESTADOR IS NULL ) )) IF @AUXIPACOTE IS NULL SELECT @AUXIPACOTE = 0.00 IF ( @AUXIPACOTE > 0.0 ) BEGIN SELECT @AUXIPACOTE = ISNULL(SUM(VALORTOTALSADT), 0), @TOTALIDPCTE = @TOTALIDPCTE + ISNULL(SUM(CASE WHEN IDENTIFICADO = 'T' THEN VALORTOTALSADT END), 0) FROM SZATENDSADT WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL AND ESTORNADO IS NULL AND COALESCE(SEMINFOPGTO, 'F') = 'F' AND INCLUIDOPACOTE = 'T' AND ( ( EXAMEFORA IS NULL ) OR ( EXAMEFORA = '' ) OR ( EXAMEFORA = 'F' ) ) AND ( ( CODPRESTADOR <> @CODPRESTADOR ) OR ( CODPRESTADOR IS NULL ) ) AND SEQDEBITOSADT NOT IN (SELECT SEQDEBITOSADT FROM SZATENDSADTGLOSA WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL AND ESTORNADO IS NULL AND INCLUIDOPACOTE = 'T' AND ( ( EXAMEFORA IS NULL ) OR ( EXAMEFORA = '' ) OR ( EXAMEFORA = 'F' ) ) AND ( ( CODPRESTADOR <> @CODPRESTADOR ) OR ( CODPRESTADOR IS NULL ) ) ) SELECT @AUXIPACOTE = @AUXIPACOTE + ISNULL(SUM(VALORTOTALSADT), 0 ), @TOTALIDPCTE = @TOTALIDPCTE + ISNULL(SUM(VALORTOTALSADT), 0) FROM SZATENDSADTGLOSA WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL AND ESTORNADO IS NULL AND COALESCE(SEMINFOPGTO, 'F') = 'F' AND INCLUIDOPACOTE = 'T' AND ( ( EXAMEFORA IS NULL ) OR ( EXAMEFORA = '' ) OR ( EXAMEFORA = 'F' ) ) AND ( ( CODPRESTADOR <> @CODPRESTADOR ) OR ( CODPRESTADOR IS NULL ) ) END SELECT @TOTPACOTESUM = @TOTPACOTESUM + @AUXIPACOTE SELECT @SADT_TERCEIROSPCTE = @AUXIPACOTE END ELSE SELECT @SADT_TERCEIROSPCTE = 0 SELECT @SADT_RESUMO = @SADT_HOSPITAL + @SADT_TERCEIROS SELECT @SADT_RESUMOPCTE = @SADT_HOSPITALPCTE + @SADT_TERCEIROSPCTE UPDATE SZATENDRESUMOGLOSA SET HMRESUMO = ISNULL(@TOTAL_HONO, 0), HMRESUMONCRED = ISNULL(@TOTAL_HONO_NCRED, 0), DIARIASRESUMO = ISNULL(@TOTAL_DIARIAS, 0), SADTHOSPITAL = ISNULL(@SADT_HOSPITAL, 0), SADTTERCEIROS = ISNULL(@SADT_TERCEIROS, 0), SADTRESUMO = ISNULL(@SADT_RESUMO, 0), GASTOPARTICULAR = 0 WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL UPDATE SZRESUMOCONTAPCTEG SET HMRESUMO = ISNULL(@TOTAL_HONOPCTE, 0), HMRESUMONCRED = ISNULL(@TOTAL_HONO_NCREDPCTE, 0), DIARIASRESUMO = ISNULL(@TOTAL_DIARIASPCTE, 0), SADTHOSPITAL = ISNULL(@SADT_HOSPITALPCTE, 0), SADTTERCEIROS = ISNULL(@SADT_TERCEIROSPCTE, 0), SADTRESUMO = ISNULL(@SADT_RESUMOPCTE, 0) WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL EXEC ATUALIZARESUMOGCOMP @CODCOLIGADA, @NCONTA, @SCONTA, @SEQPARCIAL, @CONTROLEESTORNOGLOSAGG, @TOTPACOTE, @TOTPACOTESUM, @TOTALID, @TOTALIDPCTE UPDATE SZATENDRESUMOGLOSA SET SUBTOTALHOSPITAL = HMRESUMONCRED + DIARIASRESUMO + TAXASRESUMO + SERVICORESUMO + MEDRESUMO + MATRESUMO + MATORTESE + MATPROTESE + SADTHOSPITAL WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL UPDATE SZRESUMOCONTAPCTEG SET SUBTOTALHOSPITAL = HMRESUMONCRED + DIARIASRESUMO + TAXASRESUMO + SERVICORESUMO + MEDRESUMO + MATRESUMO + MATORTESE + MATPROTESE + SADTHOSPITAL WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL UPDATE SZATENDRESUMOGLOSA SET VALORTOTALRESUMO = HMRESUMO + HMRESUMONCRED + DIARIASRESUMO + TAXASRESUMO + SERVICORESUMO + SADTRESUMO + MEDRESUMO + MATORTESE + MATPROTESE + MATRESUMO + ISNULL(TAXAADMRESUMO, 0) WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL AND ( ( GLOSATOTCONTA = 'F' ) OR ( GLOSATOTCONTA = '' ) OR ( GLOSATOTCONTA IS NULL ) ) UPDATE SZRESUMOCONTAPCTEG SET VALORTOTALRESUMO = HMRESUMO + HMRESUMONCRED + DIARIASRESUMO + TAXASRESUMO + SERVICORESUMO + SADTRESUMO + MEDRESUMO + MATORTESE + MATPROTESE + MATRESUMO + ISNULL(TAXAADMRESUMO, 0) WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL AND ( ( GLOSATOTCONTA = 'F' ) OR ( GLOSATOTCONTA = '' ) OR ( GLOSATOTCONTA IS NULL ) ) IF @TOTPACOTE > 0.00 BEGIN UPDATE SZATENDRESUMOGLOSA SET TOTALATENDIMENTO = PACOTERESUMO WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL END ELSE BEGIN UPDATE SZATENDRESUMOGLOSA SET TOTALATENDIMENTO = VALORTOTALRESUMO WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL END IF @STATUSAUDIT2 = 'G' BEGIN UPDATE SZATENDRESUMOGLOSA SET VALORTOTALRESUMO = 0.0, PACOTERESUMO = 0.0, TOTALATENDIMENTO = 0.0 WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL END IF @TOTPACOTE > 0.00 BEGIN UPDATE SZRESUMOCONTAPCTEG SET TOTALATENDIMENTO = PACOTERESUMO WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL END ELSE BEGIN UPDATE SZRESUMOCONTAPCTEG SET TOTALATENDIMENTO = VALORTOTALRESUMO WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL END IF @STATUSAUDIT2 = 'G' BEGIN UPDATE SZRESUMOCONTAPCTEG SET VALORTOTALRESUMO = 0.0, PACOTERESUMO = 0.0, TOTALATENDIMENTO = 0.0 WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL END