EXEMPLO_1212310
Database
Tables
(current)
Columns
Constraints
Relationships
Orphan Tables
Anomalies
Routines
ATUALIZARESUMO
Parameters
Name
Type
Mode
@NCONTA
char
IN
@SCONTA
int
IN
@TIPO
char
IN
@CODCOLIGADA
int
IN
@SEQPARCIAL
int
IN
Definition
CREATE PROCEDURE ATUALIZARESUMO @NCONTA CHAR(14), @SCONTA INTEGER, @TIPO CHAR(1), @CODCOLIGADA INTEGER, @SEQPARCIAL INTEGER AS DECLARE @TOTAL_HONO FLOAT, @TOTAL_DIARIAS FLOAT, @SADT_RESUMO FLOAT, @SADT_HOSPITAL FLOAT, @SADT_TERCEIROS FLOAT, @TAXAS FLOAT, @SERVICOS FLOAT, @MATL FLOAT, @MATB FLOAT, @MATU FLOAT, @MATRESUMO FLOAT, @MEDL FLOAT, @MEDB FLOAT, @MEDU FLOAT, @MEDRESUMO FLOAT, @MATORTESE FLOAT, @MATPROTESE FLOAT, @TOTAL_HONO_NCRED FLOAT, @PARTICULAR FLOAT, @CUSTOHM FLOAT, @CUSTODIARIAS FLOAT, @CUSTOTAXAS FLOAT, @CUSTOSERVICO FLOAT, @CUSTOMED FLOAT, @CUSTOMAT FLOAT, @CUSTOSADT FLOAT, @CUSTOORT FLOAT, @CUSTOPRO FLOAT, @TOTPACOTE FLOAT, @AUXIPACOTE FLOAT, @CPP INTEGER, @MEDBER FLOAT, @MATBER FLOAT, @NAOCALCTXEXTP VARCHAR(1), @STCTA VARCHAR(1), @TPPAC VARCHAR(1), @TIPOPGRUPOC INTEGER, @CODCOMPRADOR INTEGER, @CODPRESTADOR INTEGER, @TOTAL_HONOPCTE FLOAT, @TOTAL_HONO_NCREDPCTE FLOAT, @TOTAL_DIARIASPCTE FLOAT, @SADT_HOSPITALPCTE FLOAT, @SADT_TERCEIROSPCTE FLOAT, @SADT_RESUMOPCTE FLOAT, @TAXASPCTE FLOAT, @SERVICOSPCTE FLOAT, @MATRESUMOPCTE FLOAT, @MEDRESUMOPCTE FLOAT, @MATORTESEPCTE FLOAT, @MATPROTESEPCTE FLOAT, @PARTICULARPCTE FLOAT, @CODSERVTAXAADM VARCHAR(8), @IDUNIDFAT INTEGER, @UTILIZA4CASAS VARCHAR(1), @TOTPCTEIPSEMG FLOAT, @PADRAOFATURAMENTO INTEGER, @PADRAOESPECIAL INTEGER, @IDUNIDATEND INTEGER, @DATAINI DATETIME, @PARTICULARPEND FLOAT SELECT @NAOCALCTXEXTP = NAOCALCTAXADMPACEXTP FROM SZPARAMGERAL WHERE CODCOLIGADA = @CODCOLIGADA IF @NAOCALCTXEXTP IS NULL SELECT @NAOCALCTXEXTP = 'F' IF (SELECT COUNT(*) FROM SZATENDRESUMO WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL) <= 0 BEGIN INSERT INTO SZATENDRESUMO (CODCOLIGADA, NUMEROCONTA, SEQUENCIALCONTA, SEQPARCIAL) VALUES ( @CODCOLIGADA, @NCONTA, @SCONTA, @SEQPARCIAL ) END SELECT @CPP = B.CODGERAL, @CODPRESTADOR = A.CODPRESTADOR, @CODCOMPRADOR = A.CODCONVENIO, @STCTA = A.STATUS, @IDUNIDFAT = A.IDUNIDFAT, @IDUNIDATEND = A.IDUNIDATEND, @DATAINI = A.DATAINI 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 SELECT @UTILIZA4CASAS = TEXTO FROM SZPARAMCONV A, SZCONDCONTRAT B, SZPARCIALATEND C WHERE C.CODCOLIGADA = @CODCOLIGADA AND C.NUMEROCONTA = @NCONTA AND C.SEQUENCIALCONTA = @SCONTA AND C.SEQPARCIAL = @SEQPARCIAL AND A.IDPARAM = 3 AND A.CODCOLIGADA = B.CODCOLIGADA AND A.CODTIPOCONTRATO = B.CODTIPOCONTRATO AND B.CODCONVENIO = C.CODCONVENIO AND B.IDUNIDATEND = C.IDUNIDFAT AND B.DATAVIGENCIA = (SELECT MAX(DATAVIGENCIA) FROM SZCONDCONTRAT I WHERE I.CODCOLIGADA = @CODCOLIGADA AND I.CODCONVENIO = @CODCOMPRADOR AND I.IDUNIDATEND = @IDUNIDFAT AND I.DATAVIGENCIA <= @DATAINI) IF ( @UTILIZA4CASAS IS NULL ) OR ( @UTILIZA4CASAS = '' ) SET @UTILIZA4CASAS = 'F' IF @TIPO = 'N' SELECT @TPPAC = TIPOPACIENTE FROM SZATENDIMENTO WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA ELSE SELECT @TPPAC = TIPOPACIENTE FROM SZATENDIMENTOREC WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA SELECT @TOTPACOTE = (SELECT SUM(VALORTOTALTAXAS) FROM SZATENDTAXASERV WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL AND CODGGASTO = '10' AND ( TAXAFORA IS NULL OR SZATENDTAXASERV.TAXAFORA = '' OR SZATENDTAXASERV.TAXAFORA = 'F' ) AND ESTORNADO IS NULL) IF @TOTPACOTE IS NULL SELECT @TOTPACOTE = 0.00 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 SELECT @TOTAL_HONO = (SELECT SUM(VALORTOTALHM) 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 ( ( CODPRESTADOR IN (SELECT A.CODPRESTADOR FROM SZCREDENCIAMENTODETALHE A WHERE A.CODCOLIGADA = @CODCOLIGADA AND A.CODCOMPRADOR = @CODCOMPRADOR AND ( ( A.IDUNIDATEND = @IDUNIDATEND ) OR ( A.IDUNIDATEND IS NULL ) ) AND A.DATAINCLUSAO <= SZATENDHONORARIO.DATAREALIZACAOHM AND COALESCE(A.DATEXCLUSAO, GETDATE()) > SZATENDHONORARIO.DATAREALIZACAOHM AND A.CREDDIRETO = 'T' AND SZATENDHONORARIO.CODPRESTADOR <> @CPP) ) OR ( SZATENDHONORARIO.CODPRESTADOR IN (SELECT A.CODPRESTADOR FROM SZCREDENCIAMENTODETALHE A WHERE A.CODCOLIGADA = @CODCOLIGADA AND A.CODCOMPRADOR = @CODCOMPRADOR AND ( ( A.IDUNIDATEND = @IDUNIDATEND ) OR ( A.IDUNIDATEND IS NULL ) ) AND A.DATAINCLUSAO <= SZATENDHONORARIO.DATAREALIZACAOHM AND COALESCE(A.DATEXCLUSAO, GETDATE()) > SZATENDHONORARIO.DATAREALIZACAOHM AND A.CREDDIRETO = 'F') ) )) IF @TOTAL_HONO IS NULL SELECT @TOTAL_HONO = 0.00 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 ) SELECT @AUXIPACOTE = (SELECT SUM(VALORTOTALHM) FROM SZATENDHONORARIO WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL AND INCLUIDOPACOTE = 'T' 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 <= 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') ) )) IF @AUXIPACOTE IS NULL SELECT @AUXIPACOTE = 0.00 SELECT @TOTPACOTE = @TOTPACOTE + @AUXIPACOTE SELECT @TOTAL_HONOPCTE = @AUXIPACOTE END ELSE SELECT @TOTAL_HONOPCTE = 0 SELECT @AUXIPACOTE = 0.00 SELECT @TOTAL_HONO_NCRED = (SELECT SUM(VALORTOTALHM) 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 ( 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) ) )) IF @TOTAL_HONO_NCRED IS NULL SELECT @TOTAL_HONO_NCRED = 0.00 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 ) SELECT @AUXIPACOTE = (SELECT SUM(VALORTOTALHM) FROM SZATENDHONORARIO WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL AND INCLUIDOPACOTE = 'T' 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 <= 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) ) )) IF @AUXIPACOTE IS NULL SELECT @AUXIPACOTE = 0.00 SELECT @TOTPACOTE = @TOTPACOTE + @AUXIPACOTE SELECT @TOTAL_HONO_NCREDPCTE = @AUXIPACOTE END ELSE SELECT @TOTAL_HONO_NCREDPCTE = 0 SELECT @AUXIPACOTE = 0.00 SELECT @TOTAL_DIARIAS = (SELECT SUM(VALORTOTALDIARIAS) FROM SZATENDDIARIAS WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL AND ESTORNADO IS NULL) IF @TOTAL_DIARIAS IS NULL SELECT @TOTAL_DIARIAS = 0.00 IF ( @TOTPACOTE > 0.0 ) AND ( @TOTAL_DIARIAS > 0.0 ) BEGIN SELECT @AUXIPACOTE = (SELECT SUM(VALORTOTALDIARIAS) FROM SZATENDDIARIAS WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL AND INCLUIDOPACOTE = 'T' AND ESTORNADO IS NULL) IF @AUXIPACOTE IS NULL SELECT @AUXIPACOTE = 0.00 SELECT @TOTPACOTE = @TOTPACOTE + @AUXIPACOTE SELECT @TOTAL_DIARIASPCTE = @AUXIPACOTE END ELSE SELECT @TOTAL_DIARIASPCTE = 0 SELECT @AUXIPACOTE = 0.00 UPDATE SZATENDRESUMO SET SADTRESUMO = 0.00, SADTHOSPITAL = 0.00, SADTTERCEIROS = 0.00 WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL SELECT @SADT_HOSPITAL = (SELECT SUM(VALORTOTALSADT) FROM SZATENDSADT WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL AND ( EXAMEFORA IS NULL OR EXAMEFORA = '' OR EXAMEFORA = 'F' ) AND CODPRESTADOR = @CODPRESTADOR AND ESTORNADO IS NULL) IF @SADT_HOSPITAL IS NULL SELECT @SADT_HOSPITAL = 0.00 IF ( @TOTPACOTE > 0.0 ) AND ( @SADT_HOSPITAL > 0.0 ) BEGIN SELECT @AUXIPACOTE = (SELECT SUM(VALORTOTALSADT) FROM SZATENDSADT WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL AND INCLUIDOPACOTE = 'T' AND ( EXAMEFORA IS NULL OR EXAMEFORA = '' OR EXAMEFORA = 'F' ) AND CODPRESTADOR = @CODPRESTADOR AND ESTORNADO IS NULL) IF @AUXIPACOTE IS NULL SELECT @AUXIPACOTE = 0.00 SELECT @TOTPACOTE = @TOTPACOTE + @AUXIPACOTE SELECT @SADT_HOSPITALPCTE = @AUXIPACOTE END ELSE SELECT @SADT_HOSPITALPCTE = 0 SELECT @AUXIPACOTE = 0.00 SELECT @SADT_TERCEIROS = (SELECT SUM(VALORTOTALSADT) 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 ( ( CODPRESTADOR <> @CODPRESTADOR ) OR ( CODPRESTADOR IS NULL ) )) IF @SADT_TERCEIROS IS NULL SELECT @SADT_TERCEIROS = 0.00 IF ( @TOTPACOTE > 0.0 ) AND ( @SADT_TERCEIROS > 0.0 ) BEGIN SELECT @AUXIPACOTE = (SELECT SUM(VALORTOTALSADT) FROM SZATENDSADT WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL AND INCLUIDOPACOTE = 'T' AND ( EXAMEFORA IS NULL OR EXAMEFORA = '' OR EXAMEFORA = 'F' ) AND ESTORNADO IS NULL AND ( ( CODPRESTADOR <> @CODPRESTADOR ) OR ( CODPRESTADOR IS NULL ) )) IF @AUXIPACOTE IS NULL SELECT @AUXIPACOTE = 0.00 SELECT @TOTPACOTE = @TOTPACOTE + @AUXIPACOTE SELECT @SADT_TERCEIROSPCTE = @AUXIPACOTE END ELSE SELECT @SADT_TERCEIROSPCTE = 0 SELECT @AUXIPACOTE = 0.00 SELECT @SADT_RESUMO = @SADT_HOSPITAL + @SADT_TERCEIROS SELECT @SADT_RESUMOPCTE = @SADT_HOSPITALPCTE + @SADT_TERCEIROSPCTE SELECT @TAXAS = (SELECT SUM(VALORTOTALTAXAS) FROM SZATENDTAXASERV WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL AND CODGGASTO = '03' AND ( TAXAFORA IS NULL OR TAXAFORA = '' OR TAXAFORA = 'F' ) AND ESTORNADO IS NULL) IF @TAXAS IS NULL SELECT @TAXAS = 0.00 IF ( @TOTPACOTE > 0.0 ) AND ( @TAXAS > 0.0 ) BEGIN SELECT @AUXIPACOTE = (SELECT SUM(VALORTOTALTAXAS) FROM SZATENDTAXASERV WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL AND CODGGASTO = '03' AND INCLUIDOPACOTE = 'T' AND ( TAXAFORA IS NULL OR TAXAFORA = '' OR TAXAFORA = 'F' ) AND ESTORNADO IS NULL) IF @AUXIPACOTE IS NULL SELECT @AUXIPACOTE = 0.00 SELECT @TOTPACOTE = @TOTPACOTE + @AUXIPACOTE SELECT @TAXASPCTE = @AUXIPACOTE END ELSE SELECT @TAXASPCTE = 0 SELECT @AUXIPACOTE = 0.00 SELECT @SERVICOS = (SELECT SUM(VALORTOTALTAXAS) FROM SZATENDTAXASERV WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL AND CODGGASTO = '04' AND ( TAXAFORA IS NULL OR TAXAFORA = '' OR TAXAFORA = 'F' ) AND ESTORNADO IS NULL) IF @SERVICOS IS NULL SELECT @SERVICOS = 0.00 IF ( @TOTPACOTE > 0.0 ) AND ( @SERVICOS > 0.0 ) BEGIN SELECT @AUXIPACOTE = (SELECT SUM(VALORTOTALTAXAS) FROM SZATENDTAXASERV WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL AND CODGGASTO = '04' AND INCLUIDOPACOTE = 'T' AND ( TAXAFORA IS NULL OR TAXAFORA = '' OR TAXAFORA = 'F' ) AND ESTORNADO IS NULL) IF @AUXIPACOTE IS NULL SELECT @AUXIPACOTE = 0.00 SELECT @TOTPACOTE = @TOTPACOTE + @AUXIPACOTE SELECT @SERVICOSPCTE = @AUXIPACOTE END ELSE SELECT @SERVICOSPCTE = 0 SELECT @AUXIPACOTE = 0.00 IF @UTILIZA4CASAS = 'T' SELECT @MATL = (SELECT SUM(CONVERT(NUMERIC(18, 4), ( ( QUANTIDADEMATMED * VALORMATMED ) - ( QTDDEVOLVIDA * VALORMATMED ) ))) FROM SZATENDMATMED WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL AND CODGGASTO = '06' AND LOCALUSO IN ( 'L', 'E', 'A' ) AND ESTORNADO IS NULL AND ( ( TIPOMOVIMENTO <> 2 ) OR ( TIPOMOVIMENTO IS NULL ) )) ELSE SELECT @MATL = (SELECT SUM(CONVERT(NUMERIC(18, 2), ( ( QUANTIDADEMATMED * VALORMATMED ) - ( QTDDEVOLVIDA * VALORMATMED ) ))) FROM SZATENDMATMED WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL AND CODGGASTO = '06' AND LOCALUSO IN ( 'L', 'E', 'A' ) AND ESTORNADO IS NULL AND ( ( TIPOMOVIMENTO <> 2 ) OR ( TIPOMOVIMENTO IS NULL ) )) IF @MATL IS NULL SELECT @MATL = 0.00 IF @UTILIZA4CASAS = 'T' SELECT @MATB = (SELECT SUM(CONVERT(NUMERIC(18, 4), ( ( QUANTIDADEMATMED * VALORMATMED ) - ( QTDDEVOLVIDA * VALORMATMED ) ))) FROM SZATENDMATMED WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL AND CODGGASTO = '06' AND LOCALUSO = 'C' AND ESTORNADO IS NULL AND ( ( TIPOMOVIMENTO <> 2 ) OR ( TIPOMOVIMENTO IS NULL ) )) ELSE SELECT @MATB = (SELECT SUM(CONVERT(NUMERIC(18, 2), ( ( QUANTIDADEMATMED * VALORMATMED ) - ( QTDDEVOLVIDA * VALORMATMED ) ))) FROM SZATENDMATMED WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL AND CODGGASTO = '06' AND LOCALUSO = 'C' AND ESTORNADO IS NULL AND ( ( TIPOMOVIMENTO <> 2 ) OR ( TIPOMOVIMENTO IS NULL ) )) IF @MATB IS NULL SELECT @MATB = 0.00 IF @UTILIZA4CASAS = 'T' SELECT @MATU = (SELECT SUM(CONVERT(NUMERIC(18, 4), ( ( QUANTIDADEMATMED * VALORMATMED ) - ( QTDDEVOLVIDA * VALORMATMED ) ))) FROM SZATENDMATMED WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL AND CODGGASTO = '06' AND ESTORNADO IS NULL AND LOCALUSO IN ( 'U', 'N' ) AND ( ( TIPOMOVIMENTO <> 2 ) OR ( TIPOMOVIMENTO IS NULL ) )) ELSE SELECT @MATU = (SELECT SUM(CONVERT(NUMERIC(18, 2), ( ( QUANTIDADEMATMED * VALORMATMED ) - ( QTDDEVOLVIDA * VALORMATMED ) ))) FROM SZATENDMATMED WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL AND CODGGASTO = '06' AND ESTORNADO IS NULL AND LOCALUSO IN ( 'U', 'N' ) AND ( ( TIPOMOVIMENTO <> 2 ) OR ( TIPOMOVIMENTO IS NULL ) )) IF @MATU IS NULL SELECT @MATU = 0.00 IF @UTILIZA4CASAS = 'T' SELECT @MATBER = (SELECT SUM(CONVERT(NUMERIC(18, 4), ( ( QUANTIDADEMATMED * VALORMATMED ) - ( QTDDEVOLVIDA * VALORMATMED ) ))) FROM SZATENDMATMED WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL AND CODGGASTO = '06' AND LOCALUSO = 'B' AND ESTORNADO IS NULL AND ( ( TIPOMOVIMENTO <> 2 ) OR ( TIPOMOVIMENTO IS NULL ) )) ELSE SELECT @MATBER = (SELECT SUM(CONVERT(NUMERIC(18, 2), ( ( QUANTIDADEMATMED * VALORMATMED ) - ( QTDDEVOLVIDA * VALORMATMED ) ))) FROM SZATENDMATMED WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL AND CODGGASTO = '06' AND LOCALUSO = 'B' AND ESTORNADO IS NULL AND ( ( TIPOMOVIMENTO <> 2 ) OR ( TIPOMOVIMENTO IS NULL ) )) IF @MATBER IS NULL SELECT @MATBER = 0.00 IF @UTILIZA4CASAS = 'T' SELECT @MATRESUMO = (SELECT SUM(CONVERT(NUMERIC(18, 4), ( ( QUANTIDADEMATMED * VALORMATMED ) - ( QTDDEVOLVIDA * VALORMATMED ) ))) FROM SZATENDMATMED WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL AND CODGGASTO = '06' AND ESTORNADO IS NULL AND ( ( TIPOMOVIMENTO <> 2 ) OR ( TIPOMOVIMENTO IS NULL ) )) ELSE SELECT @MATRESUMO = (SELECT SUM(CONVERT(NUMERIC(18, 2), ( ( QUANTIDADEMATMED * VALORMATMED ) - ( QTDDEVOLVIDA * VALORMATMED ) ))) FROM SZATENDMATMED WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL AND CODGGASTO = '06' AND ESTORNADO IS NULL AND ( ( TIPOMOVIMENTO <> 2 ) OR ( TIPOMOVIMENTO IS NULL ) )) IF @MATRESUMO IS NULL SELECT @MATRESUMO = 0.00 IF ( @TOTPACOTE > 0.0 ) AND ( @MATRESUMO > 0.0 ) BEGIN IF @UTILIZA4CASAS = 'T' SELECT @AUXIPACOTE = (SELECT SUM(CONVERT(NUMERIC(18, 4), ( ( QUANTIDADEMATMED * VALORMATMED ) - ( QTDDEVOLVIDA * VALORMATMED ) ))) FROM SZATENDMATMED WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL AND INCLUIDOPACOTE = 'T' AND CODGGASTO = '06' AND ESTORNADO IS NULL AND ( ( TIPOMOVIMENTO <> 2 ) OR ( TIPOMOVIMENTO IS NULL ) )) ELSE SELECT @AUXIPACOTE = (SELECT SUM(CONVERT(NUMERIC(18, 2), ( ( QUANTIDADEMATMED * VALORMATMED ) - ( QTDDEVOLVIDA * VALORMATMED ) ))) FROM SZATENDMATMED WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL AND INCLUIDOPACOTE = 'T' AND CODGGASTO = '06' AND ESTORNADO IS NULL AND ( ( TIPOMOVIMENTO <> 2 ) OR ( TIPOMOVIMENTO IS NULL ) )) IF @AUXIPACOTE IS NULL SELECT @AUXIPACOTE = 0.00 SELECT @TOTPACOTE = @TOTPACOTE + @AUXIPACOTE SELECT @MATRESUMOPCTE = @AUXIPACOTE END ELSE SELECT @MATRESUMOPCTE = 0 SELECT @AUXIPACOTE = 0.00 IF @UTILIZA4CASAS = 'T' SELECT @MEDL = (SELECT SUM(CONVERT(NUMERIC(18, 4), ( ( QUANTIDADEMATMED * VALORMATMED ) - ( QTDDEVOLVIDA * VALORMATMED ) ))) FROM SZATENDMATMED WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL AND CODGGASTO = '05' AND LOCALUSO IN ( 'L', 'E', 'A' ) AND ESTORNADO IS NULL AND ( ( TIPOMOVIMENTO <> 2 ) OR ( TIPOMOVIMENTO IS NULL ) )) ELSE SELECT @MEDL = (SELECT SUM(CONVERT(NUMERIC(18, 2), ( ( QUANTIDADEMATMED * VALORMATMED ) - ( QTDDEVOLVIDA * VALORMATMED ) ))) FROM SZATENDMATMED WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL AND CODGGASTO = '05' AND LOCALUSO IN ( 'L', 'E', 'A' ) AND ESTORNADO IS NULL AND ( ( TIPOMOVIMENTO <> 2 ) OR ( TIPOMOVIMENTO IS NULL ) )) IF @MEDL IS NULL SELECT @MEDL = 0.00 IF @UTILIZA4CASAS = 'T' SELECT @MEDB = (SELECT SUM(CONVERT(NUMERIC(18, 4), ( ( QUANTIDADEMATMED * VALORMATMED ) - ( QTDDEVOLVIDA * VALORMATMED ) ))) FROM SZATENDMATMED WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL AND CODGGASTO = '05' AND LOCALUSO = 'C' AND ESTORNADO IS NULL AND ( ( TIPOMOVIMENTO <> 2 ) OR ( TIPOMOVIMENTO IS NULL ) )) ELSE SELECT @MEDB = (SELECT SUM(CONVERT(NUMERIC(18, 2), ( ( QUANTIDADEMATMED * VALORMATMED ) - ( QTDDEVOLVIDA * VALORMATMED ) ))) FROM SZATENDMATMED WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL AND CODGGASTO = '05' AND LOCALUSO = 'C' AND ESTORNADO IS NULL AND ( ( TIPOMOVIMENTO <> 2 ) OR ( TIPOMOVIMENTO IS NULL ) )) IF @MEDB IS NULL SELECT @MEDB = 0.00 IF @UTILIZA4CASAS = 'T' SELECT @MEDU = (SELECT SUM(CONVERT(NUMERIC(18, 4), ( ( QUANTIDADEMATMED * VALORMATMED ) - ( QTDDEVOLVIDA * VALORMATMED ) ))) FROM SZATENDMATMED WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL AND CODGGASTO = '05' AND LOCALUSO IN ( 'U', 'N' ) AND ESTORNADO IS NULL AND ( ( TIPOMOVIMENTO <> 2 ) OR ( TIPOMOVIMENTO IS NULL ) )) ELSE SELECT @MEDU = (SELECT SUM(CONVERT(NUMERIC(18, 2), ( ( QUANTIDADEMATMED * VALORMATMED ) - ( QTDDEVOLVIDA * VALORMATMED ) ))) FROM SZATENDMATMED WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL AND CODGGASTO = '05' AND LOCALUSO IN ( 'U', 'N' ) AND ESTORNADO IS NULL AND ( ( TIPOMOVIMENTO <> 2 ) OR ( TIPOMOVIMENTO IS NULL ) )) IF @MEDU IS NULL SELECT @MEDU = 0.00 IF @UTILIZA4CASAS = 'T' SELECT @MEDBER = (SELECT SUM(CONVERT(NUMERIC(18, 4), ( ( QUANTIDADEMATMED * VALORMATMED ) - ( QTDDEVOLVIDA * VALORMATMED ) ))) FROM SZATENDMATMED WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL AND CODGGASTO = '05' AND LOCALUSO = 'B' AND ESTORNADO IS NULL AND ( ( TIPOMOVIMENTO <> 2 ) OR ( TIPOMOVIMENTO IS NULL ) )) ELSE SELECT @MEDBER = (SELECT SUM(CONVERT(NUMERIC(18, 2), ( ( QUANTIDADEMATMED * VALORMATMED ) - ( QTDDEVOLVIDA * VALORMATMED ) ))) FROM SZATENDMATMED WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL AND CODGGASTO = '05' AND LOCALUSO = 'B' AND ESTORNADO IS NULL AND ( ( TIPOMOVIMENTO <> 2 ) OR ( TIPOMOVIMENTO IS NULL ) )) IF @MEDBER IS NULL SELECT @MEDBER = 0.00 IF @UTILIZA4CASAS = 'T' SELECT @MEDRESUMO = (SELECT SUM(CONVERT(NUMERIC(18, 4), ( ( QUANTIDADEMATMED * VALORMATMED ) - ( QTDDEVOLVIDA * VALORMATMED ) ))) FROM SZATENDMATMED WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL AND CODGGASTO = '05' AND ESTORNADO IS NULL AND ( ( TIPOMOVIMENTO <> 2 ) OR ( TIPOMOVIMENTO IS NULL ) )) ELSE SELECT @MEDRESUMO = (SELECT SUM(CONVERT(NUMERIC(18, 2), ( ( QUANTIDADEMATMED * VALORMATMED ) - ( QTDDEVOLVIDA * VALORMATMED ) ))) FROM SZATENDMATMED WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL AND CODGGASTO = '05' AND ESTORNADO IS NULL AND ( ( TIPOMOVIMENTO <> 2 ) OR ( TIPOMOVIMENTO IS NULL ) )) IF @MEDRESUMO IS NULL SELECT @MEDRESUMO = 0.00 IF ( @TOTPACOTE > 0.0 ) AND ( @MEDRESUMO > 0.0 ) BEGIN IF @UTILIZA4CASAS = 'T' SELECT @AUXIPACOTE = (SELECT SUM(CONVERT(NUMERIC(18, 4), ( ( QUANTIDADEMATMED * VALORMATMED ) - ( QTDDEVOLVIDA * VALORMATMED ) ))) FROM SZATENDMATMED WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL AND INCLUIDOPACOTE = 'T' AND CODGGASTO = '05' AND ESTORNADO IS NULL AND ( ( TIPOMOVIMENTO <> 2 ) OR ( TIPOMOVIMENTO IS NULL ) )) ELSE SELECT @AUXIPACOTE = (SELECT SUM(CONVERT(NUMERIC(18, 2), ( ( QUANTIDADEMATMED * VALORMATMED ) - ( QTDDEVOLVIDA * VALORMATMED ) ))) FROM SZATENDMATMED WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL AND INCLUIDOPACOTE = 'T' AND CODGGASTO = '05' AND ESTORNADO IS NULL AND ( ( TIPOMOVIMENTO <> 2 ) OR ( TIPOMOVIMENTO IS NULL ) )) IF @AUXIPACOTE IS NULL SELECT @AUXIPACOTE = 0.00 SELECT @TOTPACOTE = @TOTPACOTE + @AUXIPACOTE SELECT @MEDRESUMOPCTE = @AUXIPACOTE END ELSE SELECT @MEDRESUMOPCTE = 0 SELECT @AUXIPACOTE = 0.00 IF @UTILIZA4CASAS = 'T' SELECT @MATORTESE = (SELECT SUM(CONVERT(NUMERIC(18, 4), ( ( QUANTIDADEMATMED * VALORMATMED ) - ( QTDDEVOLVIDA * VALORMATMED ) ))) FROM SZATENDMATMED WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL AND CODGGASTO = '07' AND ESTORNADO IS NULL AND ( ( TIPOMOVIMENTO <> 2 ) OR ( TIPOMOVIMENTO IS NULL ) )) ELSE SELECT @MATORTESE = (SELECT SUM(CONVERT(NUMERIC(18, 2), ( ( QUANTIDADEMATMED * VALORMATMED ) - ( QTDDEVOLVIDA * VALORMATMED ) ))) FROM SZATENDMATMED WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL AND CODGGASTO = '07' AND ESTORNADO IS NULL AND ( ( TIPOMOVIMENTO <> 2 ) OR ( TIPOMOVIMENTO IS NULL ) )) IF @MATORTESE IS NULL SELECT @MATORTESE = 0.00 IF ( @TOTPACOTE > 0.0 ) AND ( @MATORTESE > 0.0 ) BEGIN IF @UTILIZA4CASAS = 'T' SELECT @AUXIPACOTE = (SELECT SUM(CONVERT(NUMERIC(18, 4), ABS(QUANTIDADEMATMED - QTDDEVOLVIDA) * VALORMATMED)) FROM SZATENDMATMED WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL AND INCLUIDOPACOTE = 'T' AND CODGGASTO = '07' AND ESTORNADO IS NULL AND ( ( TIPOMOVIMENTO <> 2 ) OR ( TIPOMOVIMENTO IS NULL ) )) ELSE SELECT @AUXIPACOTE = (SELECT SUM(CONVERT(NUMERIC(18, 2), ABS(QUANTIDADEMATMED - QTDDEVOLVIDA) * VALORMATMED)) FROM SZATENDMATMED WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL AND INCLUIDOPACOTE = 'T' AND CODGGASTO = '07' AND ESTORNADO IS NULL AND ( ( TIPOMOVIMENTO <> 2 ) OR ( TIPOMOVIMENTO IS NULL ) )) IF @AUXIPACOTE IS NULL SELECT @AUXIPACOTE = 0.00 SELECT @TOTPACOTE = @TOTPACOTE + @AUXIPACOTE SELECT @MATORTESEPCTE = @AUXIPACOTE END ELSE SELECT @MATORTESEPCTE = 0 SELECT @AUXIPACOTE = 0.00 IF @UTILIZA4CASAS = 'T' SELECT @MATPROTESE = (SELECT SUM(CONVERT(NUMERIC(18, 4), ( ( QUANTIDADEMATMED * VALORMATMED ) - ( QTDDEVOLVIDA * VALORMATMED ) ))) FROM SZATENDMATMED WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL AND CODGGASTO = '08' AND ESTORNADO IS NULL AND ( ( TIPOMOVIMENTO <> 2 ) OR ( TIPOMOVIMENTO IS NULL ) )) ELSE SELECT @MATPROTESE = (SELECT SUM(CONVERT(NUMERIC(18, 2), ( ( QUANTIDADEMATMED * VALORMATMED ) - ( QTDDEVOLVIDA * VALORMATMED ) ))) FROM SZATENDMATMED WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL AND CODGGASTO = '08' AND ESTORNADO IS NULL AND ( ( TIPOMOVIMENTO <> 2 ) OR ( TIPOMOVIMENTO IS NULL ) )) IF @MATPROTESE IS NULL SELECT @MATPROTESE = 0.00 IF ( @TOTPACOTE > 0.0 ) AND ( @MATPROTESE > 0.0 ) BEGIN IF @UTILIZA4CASAS = 'T' SELECT @AUXIPACOTE = (SELECT SUM(CONVERT(NUMERIC(18, 4), ABS(QUANTIDADEMATMED - QTDDEVOLVIDA) * VALORMATMED)) FROM SZATENDMATMED WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL AND INCLUIDOPACOTE = 'T' AND CODGGASTO = '08' AND ESTORNADO IS NULL AND ( ( TIPOMOVIMENTO <> 2 ) OR ( TIPOMOVIMENTO IS NULL ) )) ELSE SELECT @AUXIPACOTE = (SELECT SUM(CONVERT(NUMERIC(18, 2), ABS(QUANTIDADEMATMED - QTDDEVOLVIDA) * VALORMATMED)) FROM SZATENDMATMED WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL AND INCLUIDOPACOTE = 'T' AND CODGGASTO = '08' AND ESTORNADO IS NULL AND ( ( TIPOMOVIMENTO <> 2 ) OR ( TIPOMOVIMENTO IS NULL ) )) IF @AUXIPACOTE IS NULL SELECT @AUXIPACOTE = 0.00 SELECT @TOTPACOTE = @TOTPACOTE + @AUXIPACOTE SELECT @MATPROTESEPCTE = @AUXIPACOTE END ELSE SELECT @MATPROTESEPCTE = @AUXIPACOTE SELECT @AUXIPACOTE = 0.00 SELECT @PARTICULAR = (SELECT SUM(CONVERT(NUMERIC(18, 2), ABS(QUANTIDADE - ISNULL(QTDDEVOLVIDA, 0)) * VALOR)) FROM SZATENDGASTOPARTIC WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL AND ESTORNADO IS NULL AND ( ( TIPOMOVIMENTO <> 2 ) OR ( TIPOMOVIMENTO IS NULL ) )) IF @PARTICULAR IS NULL SELECT @PARTICULAR = 0.00 IF ( @TOTPACOTE > 0.0 ) AND ( @PARTICULAR > 0.0 ) BEGIN SELECT @AUXIPACOTE = (SELECT SUM(CONVERT(NUMERIC(18, 2), ABS(QUANTIDADE - ISNULL(QTDDEVOLVIDA, 0)) * VALOR)) FROM SZATENDGASTOPARTIC WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL AND INCLUIDOPACOTE = 'T' AND ESTORNADO IS NULL) IF @AUXIPACOTE IS NULL SELECT @AUXIPACOTE = 0.00 SELECT @TOTPACOTE = @TOTPACOTE + @AUXIPACOTE SELECT @PARTICULARPCTE = @AUXIPACOTE END ELSE SELECT @PARTICULARPCTE = @AUXIPACOTE SELECT @PARTICULARPEND = (SELECT SUM(CONVERT(NUMERIC(18, 2), ABS(SZATENDGASTOPARTIC.QUANTIDADE - ISNULL(SZATENDGASTOPARTIC.QTDDEVOLVIDA, 0)) * SZATENDGASTOPARTIC.VALOR)) FROM SZATENDGASTOPARTIC WHERE SZATENDGASTOPARTIC.CODCOLIGADA = @CODCOLIGADA AND SZATENDGASTOPARTIC.NUMEROCONTA = @NCONTA AND SZATENDGASTOPARTIC.SEQUENCIALCONTA = @SCONTA AND SZATENDGASTOPARTIC.SEQPARCIAL = @SEQPARCIAL AND SZATENDGASTOPARTIC.ESTORNADO IS NULL AND (( SZATENDGASTOPARTIC.TIPOMOVIMENTO <> 2 ) OR ( SZATENDGASTOPARTIC.TIPOMOVIMENTO IS NULL )) AND NOT EXISTS (SELECT SZPEDIDOATEND.* FROM SZPEDIDOATEND JOIN SZITENSNFGERADA ON SZITENSNFGERADA.CODCOLIGADA = SZPEDIDOATEND.CODCOLIGADA AND SZITENSNFGERADA.NUMEROCONTA = SZPEDIDOATEND.NUMEROCONTA AND SZITENSNFGERADA.SEQUENCIALCONTA = SZPEDIDOATEND.SEQUENCIALCONTA AND SZITENSNFGERADA.SEQPARCIAL = SZPEDIDOATEND.SEQPARCIAL AND SZITENSNFGERADA.IDPEDIDOATEND = SZPEDIDOATEND.IDPEDIDOATEND WHERE SZPEDIDOATEND.CODCOLIGADA = SZATENDGASTOPARTIC.CODCOLIGADA AND SZPEDIDOATEND.NUMEROCONTA = SZATENDGASTOPARTIC.NUMEROCONTA AND SZPEDIDOATEND.SEQUENCIALCONTA = SZATENDGASTOPARTIC.SEQUENCIALCONTA AND SZPEDIDOATEND.SEQPARCIAL = SZATENDGASTOPARTIC.SEQPARCIAL AND SZITENSNFGERADA.SEQLANCAMENTO = SZATENDGASTOPARTIC.SEQLANCAMENTO)) IF @PARTICULARPEND IS NULL SELECT @PARTICULARPEND = 0.00 SELECT @AUXIPACOTE = 0.00 SELECT @CUSTOHM = (SELECT SUM(CONVERT(NUMERIC(18, 2), QUANTIDADEHM * VALORCUSTOUNITARIO)) FROM SZATENDHONORARIO WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL AND ESTORNADO IS NULL) IF @CUSTOHM IS NULL SELECT @CUSTOHM = 0.00 SELECT @CUSTODIARIAS = (SELECT SUM(CONVERT(NUMERIC(18, 2), QUANTIDADEDIARIAS * VALORCUSTOUNITARIO)) FROM SZATENDDIARIAS WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL AND ESTORNADO IS NULL) IF @CUSTODIARIAS IS NULL SELECT @CUSTODIARIAS = 0.00 SELECT @CUSTOSADT = (SELECT SUM(CONVERT(NUMERIC(18, 2), QUANTIDADESADT * VALORCUSTOUNITARIO)) FROM SZATENDSADT WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL AND ESTORNADO IS NULL) IF @CUSTOSADT IS NULL SELECT @CUSTOSADT = 0.00 SELECT @CUSTOTAXAS = (SELECT SUM(CONVERT(NUMERIC(18, 2), QUANTIDADETAXAS * VALORCUSTOUNITARIO)) FROM SZATENDTAXASERV WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL AND CODGGASTO = '03' AND ESTORNADO IS NULL AND ( TAXAFORA IS NULL OR TAXAFORA = '' OR TAXAFORA = 'F' )) IF @CUSTOTAXAS IS NULL SELECT @CUSTOTAXAS = 0.00 SELECT @CUSTOSERVICO = (SELECT SUM(CONVERT(NUMERIC(18, 2), QUANTIDADETAXAS * VALORCUSTOUNITARIO)) FROM SZATENDTAXASERV WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL AND CODGGASTO = '04' AND ( TAXAFORA IS NULL OR TAXAFORA = '' OR TAXAFORA = 'F' ) AND ESTORNADO IS NULL) IF @CUSTOSERVICO IS NULL SELECT @CUSTOSERVICO = 0.00 SELECT @CUSTOMAT = (SELECT SUM(CONVERT(NUMERIC(18, 2), ( ( QUANTIDADEMATMED * VALORCUSTOUNITARIO ) - ( QTDDEVOLVIDA * VALORCUSTOUNITARIO ) ))) FROM SZATENDMATMED WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL AND CODGGASTO = '06' AND ESTORNADO IS NULL AND ( ( TIPOMOVIMENTO <> 2 ) OR ( TIPOMOVIMENTO IS NULL ) )) IF @CUSTOMAT IS NULL SELECT @CUSTOMAT = 0.00 SELECT @CUSTOORT = (SELECT SUM(CONVERT(NUMERIC(18, 2), ( ( QUANTIDADEMATMED * VALORCUSTOUNITARIO ) - ( QTDDEVOLVIDA * VALORCUSTOUNITARIO ) ))) FROM SZATENDMATMED WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL AND CODGGASTO = '07' AND ESTORNADO IS NULL AND ( ( TIPOMOVIMENTO <> 2 ) OR ( TIPOMOVIMENTO IS NULL ) )) IF @CUSTOORT IS NULL SELECT @CUSTOORT = 0.00 SELECT @CUSTOPRO = (SELECT SUM(CONVERT(NUMERIC(18, 2), ( ( QUANTIDADEMATMED * VALORCUSTOUNITARIO ) - ( QTDDEVOLVIDA * VALORCUSTOUNITARIO ) ))) FROM SZATENDMATMED WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL AND CODGGASTO = '08' AND ESTORNADO IS NULL AND ( ( TIPOMOVIMENTO <> 2 ) OR ( TIPOMOVIMENTO IS NULL ) )) IF @CUSTOPRO IS NULL SELECT @CUSTOPRO = 0.00 SELECT @CUSTOMED = (SELECT SUM(CONVERT(NUMERIC(18, 2), ( ( QUANTIDADEMATMED * VALORCUSTOUNITARIO ) - ( QTDDEVOLVIDA * VALORCUSTOUNITARIO ) ))) FROM SZATENDMATMED WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL AND CODGGASTO = '05' AND ESTORNADO IS NULL AND ( ( TIPOMOVIMENTO <> 2 ) OR ( TIPOMOVIMENTO IS NULL ) )) IF @CUSTOMED IS NULL SELECT @CUSTOMED = 0.00 UPDATE SZATENDRESUMO SET HMRESUMO = @TOTAL_HONO, HMRESUMONCRED = @TOTAL_HONO_NCRED, DIARIASRESUMO = @TOTAL_DIARIAS, SADTHOSPITAL = @SADT_HOSPITAL, SADTTERCEIROS = @SADT_TERCEIROS, SADTRESUMO = @SADT_RESUMO, TAXASRESUMO = @TAXAS, SERVICORESUMO = @SERVICOS, MATLEITORESUMO = @MATL, MATBLOCORESUMO = @MATB, MATUTIRESUMO = @MATU, MATRESUMO = @MATRESUMO, MEDLEITORESUMO = @MEDL, MEDBLOCORESUMO = @MEDB, MEDUTIRESUMO = @MEDU, MEDRESUMO = @MEDRESUMO, MATORTESE = @MATORTESE, MATPROTESE = @MATPROTESE, GASTOPARTICULAR = @PARTICULAR, PACOTERESUMO = @TOTPACOTE - @TOTPCTEIPSEMG, CUSTOHM = @CUSTOHM, CUSTODIARIAS = @CUSTODIARIAS, CUSTOTAXAS = @CUSTOTAXAS, CUSTOSERVICO = @CUSTOSERVICO, CUSTOMED = @CUSTOMED, CUSTOMAT = @CUSTOMAT, CUSTOSADT = @CUSTOSADT, CUSTOORT = @CUSTOORT, CUSTOPRO = @CUSTOPRO, MEDBERCARIORESUMO = @MEDBER, MATBERCARIORESUMO = @MATBER, GASTOSPARTICULARPENDENTE = @PARTICULARPEND WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL IF @TOTPACOTE > 0 BEGIN IF (SELECT COUNT(*) FROM SZRESUMOCONTAPCTE WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL) <= 0 BEGIN INSERT INTO SZRESUMOCONTAPCTE (CODCOLIGADA, NUMEROCONTA, SEQUENCIALCONTA, SEQPARCIAL) VALUES ( @CODCOLIGADA, @NCONTA, @SCONTA, @SEQPARCIAL ) END UPDATE SZRESUMOCONTAPCTE SET HMRESUMO = @TOTAL_HONOPCTE, HMRESUMONCRED = @TOTAL_HONO_NCREDPCTE, DIARIASRESUMO = @TOTAL_DIARIASPCTE, SADTHOSPITAL = @SADT_HOSPITALPCTE, SADTTERCEIROS = @SADT_TERCEIROSPCTE, SADTRESUMO = @SADT_RESUMOPCTE, TAXASRESUMO = @TAXASPCTE, SERVICORESUMO = @SERVICOSPCTE, MATRESUMO = @MATRESUMOPCTE, MEDRESUMO = @MEDRESUMOPCTE, MATORTESE = @MATORTESEPCTE, MATPROTESE = @MATPROTESEPCTE, GASTOPARTICULAR = @PARTICULARPCTE, PACOTERESUMO = @TOTPACOTE - @TOTPCTEIPSEMG WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL END IF @TOTPACOTE > 0 BEGIN UPDATE SZATENDRESUMO SET TAXAADMRESUMO = 0, SUBTOTALHOSPITAL = HMRESUMONCRED + DIARIASRESUMO + TAXASRESUMO + SERVICORESUMO + MEDRESUMO + MATRESUMO + MATORTESE + MATPROTESE + SADTHOSPITAL, VALORTOTALRESUMO = HMRESUMO + HMRESUMONCRED + DIARIASRESUMO + TAXASRESUMO + SERVICORESUMO + SADTRESUMO + MEDRESUMO + MATORTESE + MATPROTESE + MATRESUMO, TOTALATENDIMENTO = PACOTERESUMO WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL UPDATE SZRESUMOCONTAPCTE SET TAXAADMRESUMO = 0, SUBTOTALHOSPITAL = HMRESUMONCRED + DIARIASRESUMO + TAXASRESUMO + SERVICORESUMO + MEDRESUMO + MATRESUMO + MATORTESE + MATPROTESE + SADTHOSPITAL, VALORTOTALRESUMO = HMRESUMO + HMRESUMONCRED + DIARIASRESUMO + TAXASRESUMO + SERVICORESUMO + SADTRESUMO + MEDRESUMO + MATORTESE + MATPROTESE + MATRESUMO, TOTALATENDIMENTO = PACOTERESUMO WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL SELECT @TIPOPGRUPOC = TIPOPGRUPOC FROM SZCADGERAL WHERE CODCOLIGADA = @CODCOLIGADA AND CODGERAL = @CODCOMPRADOR IF ( @TPPAC = 'I' ) OR ( ( @TPPAC = 'E' AND @NAOCALCTXEXTP = 'F' ) OR ( @TPPAC = 'E' AND @NAOCALCTXEXTP = 'T' AND @TIPOPGRUPOC <> 999 ) ) BEGIN SELECT @CODSERVTAXAADM = CODSERVTAXAADM FROM SZCONDCONTRAT WHERE CODCOLIGADA = @CODCOLIGADA AND IDUNIDATEND = @IDUNIDFAT AND CODCONVENIO = @CODCOMPRADOR AND DATAVIGENCIA = (SELECT MAX(DATAVIGENCIA) FROM SZCONDCONTRAT I WHERE I.CODCOLIGADA = @CODCOLIGADA AND I.CODCONVENIO = @CODCOMPRADOR AND I.IDUNIDATEND = @IDUNIDFAT AND I.DATAVIGENCIA <= @DATAINI) IF ( @CODSERVTAXAADM IS NULL ) OR ( @CODSERVTAXAADM = '' ) BEGIN EXEC CALCTAXAADM @NCONTA, @SCONTA, @CODCOLIGADA, @SEQPARCIAL, @TIPO END UPDATE SZATENDRESUMO SET VALORTOTALRESUMO = VALORTOTALRESUMO + ISNULL(TAXAADMRESUMO, 0), TOTALATENDIMENTO = TOTALATENDIMENTO + ISNULL(TAXAADMRESUMO, 0), PACOTERESUMO = PACOTERESUMO + ISNULL(TAXAADMRESUMO, 0) WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL UPDATE SZRESUMOCONTAPCTE SET VALORTOTALRESUMO = VALORTOTALRESUMO + ISNULL(TAXAADMRESUMO, 0), TOTALATENDIMENTO = TOTALATENDIMENTO + ISNULL(TAXAADMRESUMO, 0), PACOTERESUMO = PACOTERESUMO + ISNULL(TAXAADMRESUMO, 0) WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL END END ELSE BEGIN DELETE FROM SZRESUMOCONTAPCTE WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL IF @STCTA = 'F' BEGIN UPDATE SZATENDRESUMO SET SUBTOTALHOSPITAL = HMRESUMONCRED + DIARIASRESUMO + TAXASRESUMO + SERVICORESUMO + MEDRESUMO + MATRESUMO + MATORTESE + MATPROTESE + SADTHOSPITAL, VALORTOTALRESUMO = HMRESUMO + HMRESUMONCRED + DIARIASRESUMO + TAXASRESUMO + SERVICORESUMO + SADTRESUMO + MEDRESUMO + MATORTESE + MATPROTESE + MATRESUMO + ISNULL(TAXAADMRESUMO, 0), TOTALATENDIMENTO = 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 END ELSE BEGIN UPDATE SZATENDRESUMO SET TAXAADMRESUMO = 0, SUBTOTALHOSPITAL = HMRESUMONCRED + DIARIASRESUMO + TAXASRESUMO + SERVICORESUMO + MEDRESUMO + MATRESUMO + MATORTESE + MATPROTESE + SADTHOSPITAL, VALORTOTALRESUMO = HMRESUMO + HMRESUMONCRED + DIARIASRESUMO + TAXASRESUMO + SERVICORESUMO + SADTRESUMO + MEDRESUMO + MATORTESE + MATPROTESE + MATRESUMO, TOTALATENDIMENTO = HMRESUMO + HMRESUMONCRED + DIARIASRESUMO + TAXASRESUMO + SERVICORESUMO + SADTRESUMO + MEDRESUMO + MATORTESE + MATPROTESE + MATRESUMO WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL SELECT @TIPOPGRUPOC = TIPOPGRUPOC FROM SZCADGERAL WHERE CODCOLIGADA = @CODCOLIGADA AND CODGERAL = @CODCOMPRADOR IF ( @TPPAC = 'I' ) OR ( ( @TPPAC = 'E' AND @NAOCALCTXEXTP = 'F' ) OR ( @TPPAC = 'E' AND @NAOCALCTXEXTP = 'T' AND @TIPOPGRUPOC <> 999 ) ) BEGIN SELECT @CODSERVTAXAADM = CODSERVTAXAADM FROM SZCONDCONTRAT WHERE CODCOLIGADA = @CODCOLIGADA AND IDUNIDATEND = @IDUNIDFAT AND CODCONVENIO = @CODCOMPRADOR AND DATAVIGENCIA = (SELECT MAX(DATAVIGENCIA) FROM SZCONDCONTRAT I WHERE I.CODCOLIGADA = @CODCOLIGADA AND I.CODCONVENIO = @CODCOMPRADOR AND I.IDUNIDATEND = @IDUNIDFAT AND I.DATAVIGENCIA <= @DATAINI) IF ( @CODSERVTAXAADM IS NULL ) OR ( @CODSERVTAXAADM = '' ) BEGIN EXEC CALCTAXAADM @NCONTA, @SCONTA, @CODCOLIGADA, @SEQPARCIAL, @TIPO END UPDATE SZATENDRESUMO SET VALORTOTALRESUMO = VALORTOTALRESUMO + ISNULL(TAXAADMRESUMO, 0), TOTALATENDIMENTO = TOTALATENDIMENTO + ISNULL(TAXAADMRESUMO, 0) WHERE CODCOLIGADA = @CODCOLIGADA AND NUMEROCONTA = @NCONTA AND SEQUENCIALCONTA = @SCONTA AND SEQPARCIAL = @SEQPARCIAL END END END