diff --git a/src/relatorio/dtos/relatorio-analitico.dto.ts b/src/relatorio/dtos/relatorio-analitico.dto.ts index 3b3fe1a0..805582ca 100644 --- a/src/relatorio/dtos/relatorio-analitico.dto.ts +++ b/src/relatorio/dtos/relatorio-analitico.dto.ts @@ -6,12 +6,12 @@ export class RelatorioAnaliticoDto { Object.assign(this, analitico); } } - + dataEfetivacao: Date; dataVencimento: Date; favorecido: string; consorcio: string; - valorTransacao: number = 0; + valor: number = 0; status: string; - ocorrencia: string; -} \ No newline at end of file + ocorrencia: string; +} diff --git a/src/relatorio/relatorio-analitico.repository.ts b/src/relatorio/relatorio-analitico.repository.ts index cbd6012c..c381e5f9 100644 --- a/src/relatorio/relatorio-analitico.repository.ts +++ b/src/relatorio/relatorio-analitico.repository.ts @@ -1,354 +1,301 @@ import { Injectable } from '@nestjs/common'; import { InjectDataSource } from '@nestjs/typeorm'; -import { DataSource } from 'typeorm'; -import { IFindPublicacaoRelatorio } from './interfaces/find-publicacao-relatorio.interface'; import { CustomLogger } from 'src/utils/custom-logger'; -import { RelatorioSinteticoDto } from './dtos/relatorio-sintetico.dto'; +import { DataSource } from 'typeorm'; import { RelatorioAnaliticoDto } from './dtos/relatorio-analitico.dto'; +import { IFindPublicacaoRelatorio } from './interfaces/find-publicacao-relatorio.interface'; +import { compactQuery } from 'src/utils/console-utils'; @Injectable() -export class RelatorioAnaliticoRepository { - - constructor(@InjectDataSource() - private readonly dataSource: DataSource) {} - - private logger = new CustomLogger(RelatorioAnaliticoRepository.name, { timestamp: true }); - - private getQueryAPagarConsorcio(dataInicio:string,dataFim:string, - valorMin?:number,valorMax?:number,nomeConsorcio?:string[]){ - let query = ` - select * from ( - select cs."consorcio" nomeFavorecido,sum(cs."valor_agrupado")::float valor - from ( select distinct ita.id AS id, - ita."nomeConsorcio" AS consorcio, - cf.nome AS favorecido, - cf."cpfCnpj" AS favorecido_cpfcnpj, - ita."valor" AS valor_agrupado - from item_transacao_agrupado ita - inner join item_transacao it on ita.id = it."itemTransacaoAgrupadoId" - inner join arquivo_publicacao ap on ap."itemTransacaoId"=it.id - inner join cliente_favorecido cf on cf.id=it."clienteFavorecidoId" - WHERE (1=1) `; - - if(dataInicio!==undefined && dataFim!==undefined && - (dataFim === dataInicio || new Date(dataFim)>new Date(dataInicio))) - query = query + ` and ita."dataOrdem" between '${dataInicio}' and '${dataFim}'`; - - if((nomeConsorcio!==undefined) && !(['Todos'].some(i=>nomeConsorcio?.includes(i)))) - query = query +` and ita."nomeConsorcio" in('${nomeConsorcio?.join("','")}')`; - - query = query +` and (ap."isPago"=false or ap."isPago" is null) `; - - query = query + `) as cs `; - - query = query + ` group by cs."consorcio"` - - query = query + ` order by cs."consorcio" `; - - query = query + `) as resul where (1=1) `; - - if(valorMin!==undefined) - query = query +` and resul."valor">=${valorMin}`; - - if(valorMax!==undefined) - query = query + ` and resul."valor"<=${valorMax}`; - - query = query + " union All "; - - query = query +` - select * from ( select cs."consorcio" nomeFavorecido,sum(cs."valor_agrupado")::float valor - from ( - select distinct ita.id AS id, - ita."nomeConsorcio" AS consorcio, - cf.nome AS favorecido, - cf."cpfCnpj" AS favorecido_cpfcnpj, - da."valorLancamento" AS valor_agrupado - from item_transacao_agrupado ita - inner join detalhe_a da on da."itemTransacaoAgrupadoId"= ita.id - inner join item_transacao it on ita.id = it."itemTransacaoAgrupadoId" - inner join arquivo_publicacao ap on ap."itemTransacaoId"=it.id - inner join cliente_favorecido cf on cf.id=it."clienteFavorecidoId" - WHERE (1=1) `; - - if(dataInicio!==undefined && dataFim!==undefined && - (dataFim === dataInicio || new Date(dataFim)>new Date(dataInicio))) - query = query + ` and da."dataVencimento" between '${dataInicio}' and '${dataFim}'`; - - if((nomeConsorcio!==undefined) && !(['Todos'].some(i=>nomeConsorcio?.includes(i)))) - query = query +` and ita."nomeConsorcio" in('${nomeConsorcio?.join("','")}')`; - - query = query + ` and ap."isPago"=false `; - - query = query + ` and da."ocorrenciasCnab" is null `; - - query = query + `) as cs `; - - query = query + ` group by cs."consorcio"` - - query = query + ` order by cs."consorcio" `; - - query = query + `) as resul where (1=1) `; - - if(valorMin!==undefined) - query = query +` and resul."valor">=${valorMin}`; - - if(valorMax!==undefined) - query = query + ` and resul."valor"<=${valorMax}`; - - this.logger.debug(query); - return query; - } - - private getQueryConsorcio(dataInicio:string,dataFim:string,pago?:boolean, - valorMin?:number,valorMax?:number,nomeConsorcio?:string[]){ - let query = ` - select * from ( select cs."consorcio" nomeFavorecido,sum(cs."valor_agrupado")::float valor - from ( - select distinct ita.id AS id, - ita."nomeConsorcio" AS consorcio, - cf.nome AS favorecido, - cf."cpfCnpj" AS favorecido_cpfcnpj, - da."valorLancamento" AS valor_agrupado - from item_transacao_agrupado ita - inner join detalhe_a da on da."itemTransacaoAgrupadoId"= ita.id - inner join item_transacao it on ita.id = it."itemTransacaoAgrupadoId" - inner join arquivo_publicacao ap on ap."itemTransacaoId"=it.id - inner join cliente_favorecido cf on cf.id=it."clienteFavorecidoId" - WHERE (1=1) `; - - if(dataInicio!==undefined && dataFim!==undefined && - (dataFim === dataInicio || new Date(dataFim)>new Date(dataInicio))) - query = query + ` and da."dataVencimento" between '${dataInicio}' and '${dataFim}'`; - - if((nomeConsorcio!==undefined) && !(['Todos'].some(i=>nomeConsorcio?.includes(i)))) - query = query +` and ita."nomeConsorcio" in('${nomeConsorcio?.join("','")}')`; - - if(pago!==undefined) - query = query + ` and ap."isPago"=${pago}`; - - if(pago ===false) - query = query + ` and da."ocorrenciasCnab" is not null - and da."ocorrenciasCnab" not in('00','BD') `; - - query = query + `) as cs `; - - query = query + ` group by cs."consorcio"` - - query = query + ` order by cs."consorcio" `; - - query = query + `) as resul where (1=1) `; - - if(valorMin!==undefined) - query = query +` and resul."valor">=${valorMin}`; - - if(valorMax!==undefined) - query = query + ` and resul."valor"<=${valorMax}`; - - return query; +export class RelatorioAnaliticoRepository { + constructor( + @InjectDataSource() + private readonly dataSource: DataSource, + ) {} + + private logger = new CustomLogger(RelatorioAnaliticoRepository.name, { timestamp: true }); + + private getQuery(args: IFindPublicacaoRelatorio) { + if (args.aPagar === true) { + return this.getQueryApagar(args); + } else if (args.aPagar === false || args.aPagar === undefined) { + return this.getQueryNaoApagar(args); + } else { + return this.getQueryApagar(args) + ` union all ` + this.getQueryNaoApagar(args); + } } - - private getQueryAPagarOperadores(dataInicio:string,dataFim:string,valorMin?:number, - valorMax?:number,favorecidoNome?:string[]){ - let query = ` - select * from (select cs."favorecido" nomeFavorecido,sum(cs."valor_agrupado")::float valor - from ( - select distinct ita.id AS id, - ita."nomeConsorcio" AS consorcio, - cf.nome AS favorecido, - cf."cpfCnpj" AS favorecido_cpfcnpj, - ita."valor" AS valor_agrupado - from item_transacao_agrupado ita - inner join item_transacao it on ita.id = it."itemTransacaoAgrupadoId" - inner join arquivo_publicacao ap on ap."itemTransacaoId"=it.id - inner join cliente_favorecido cf on cf.id=it."clienteFavorecidoId" - WHERE ita."nomeConsorcio" in('STPC','STPL') `; - if(dataInicio!==undefined && dataFim!==undefined && - (dataFim === dataInicio || new Date(dataFim)>new Date(dataInicio))) - query = query + ` and ita."dataOrdem" between '${dataInicio}' and '${dataFim}'`; - - query = query +` and (ap."isPago"=false or ap."isPago" is null) `; - - if(favorecidoNome!==undefined && !(['Todos'].some(i=>favorecidoNome?.includes(i)))) - query = query +` and cf.nome in('${favorecidoNome?.join("','")}')`; - - query = query +`) as cs `; - - query = query + ` group by cs."consorcio", cs."favorecido" ` - - query = query + ` order by cs."favorecido" `; - - query = query + `) as resul where (1=1) `; - - if(valorMin!==undefined) - query = query +` and resul."valor">=${valorMin}`; - - if(valorMax!==undefined) - query = query + ` and resul."valor"<=${valorMax}`; - - query = query + ` union all `; - - query = query + ` select * from (select cs."favorecido" nomeFavorecido,sum(cs."valor_agrupado")::float valor - from ( - select distinct ita.id AS id, - ita."nomeConsorcio" AS consorcio, - cf.nome AS favorecido, - cf."cpfCnpj" AS favorecido_cpfcnpj, - da."valorLancamento" AS valor_agrupado - from transacao_view tv - inner join item_transacao_agrupado ita on tv."itemTransacaoAgrupadoId"=ita.id - inner join detalhe_a da on da."itemTransacaoAgrupadoId"= ita.id - inner join item_transacao it on ita.id = it."itemTransacaoAgrupadoId" - inner join arquivo_publicacao ap on ap."itemTransacaoId"=it.id - inner join cliente_favorecido cf on cf.id=it."clienteFavorecidoId" - WHERE ita."nomeConsorcio" in('STPC','STPL') `; - if(dataInicio!==undefined && dataFim!==undefined && - (dataFim === dataInicio || new Date(dataFim)>new Date(dataInicio))) - query = query +` and da."dataVencimento" between '${dataInicio}' and '${dataFim}'`; - query = query + ` and ap."isPago"=false `; - - query = query + ` and da."ocorrenciasCnab" is null `; - - if(favorecidoNome!==undefined && !(['Todos'].some(i=>favorecidoNome?.includes(i)))) - query = query +` and cf.nome in('${favorecidoNome?.join("','")}')`; - - query = query +`) as cs `; - - query = query + ` group by cs."consorcio", cs."favorecido" `; - - query = query + ` order by cs."favorecido" `; - - query = query + `) as resul where (1=1) `; - - if(valorMin!==undefined) - query = query +` and resul."valor">=${valorMin}`; - - if(valorMax!==undefined) - query = query + ` and resul."valor"<=${valorMax}`; - - query = query + ` union all `; - - query = query + `select * from ( select cs."favorecido" nomeFavorecido,sum(cs."valor_agrupado")::float valor - from ( - select distinct tv.id AS id, - tv."nomeConsorcio" AS consorcio, - cf.nome AS favorecido, - cf."cpfCnpj" AS favorecido_cpfcnpj, - tv."valorTransacao" AS valor_agrupado - from transacao_view tv - inner join cliente_favorecido cf on cf."cpfCnpj" =tv."operadoraCpfCnpj" - WHERE tv."nomeConsorcio" in('STPC','STPL') `; - - query = query +` and tv."datetimeTransacao" between '${dataInicio+' 00:00:00'}' and '${dataFim+' 23:59:59'}' `; - - if(favorecidoNome!==undefined && !(['Todos'].some(i=>favorecidoNome?.includes(i)))) - query = query +` and cf.nome in('${favorecidoNome?.join("','")}')`; - - query = query + ` ) as cs `; - - query = query + ` group by cs."consorcio", cs."favorecido" `; + public async findAnalitico(args: IFindPublicacaoRelatorio): Promise { + const query = this.getQuery(args); + this.logger.debug(compactQuery(query)); + const queryRunner = this.dataSource.createQueryRunner(); + await queryRunner.connect(); + let result: any[] = await queryRunner.query(compactQuery(query)); + queryRunner.release(); + const analiticos = result.map((r) => new RelatorioAnaliticoDto(r)); + return analiticos; + } - query = query + ` order by cs."favorecido" `; + public getQueryApagar(args: IFindPublicacaoRelatorio) { + const dataInicio = args.dataInicio.toISOString().slice(0, 10); + const dataFim = args.dataFim.toISOString().slice(0, 10); + let query = `WITH subtotal_data AS ( `; + query = query + ` SELECT `; + query = query + ` tv."nomeConsorcio", `; + query = query + ` SUM(tv."valorPago") AS subTotal `; + query = query + ` FROM transacao_view tv `; + query = query + ` WHERE tv."valorPago" > 0 `; + if (dataInicio !== undefined && dataFim !== undefined && (dataFim === dataInicio || new Date(dataFim) > new Date(dataInicio))) { + query = query + ` AND tv."datetimeTransacao" BETWEEN '${dataInicio + ' 00:00:00'}' and '${dataFim + ' 23:59:59'}' `; + } + query = query + ` AND tv."itemTransacaoAgrupadoId" IS NULL `; + query = query + ` GROUP BY tv."nomeConsorcio" ), `; + query = query + `total_data AS ( `; + query = query + ` SELECT `; + query = query + ` SUM(tv."valorPago") AS Total `; + query = query + ` FROM transacao_view tv `; + query = query + ` WHERE tv."valorPago" > 0 `; + if (dataInicio !== undefined && dataFim !== undefined && (dataFim === dataInicio || new Date(dataFim) > new Date(dataInicio))) { + query = query + ` AND tv."datetimeTransacao" BETWEEN '${dataInicio + ' 00:00:00'}' and '${dataFim + ' 23:59:59'}' `; + } - query = query + `) as resul where (1=1) `; - - if(valorMin!==undefined) - query = query +` and resul."valor">=${valorMin}`; + if (args.consorcioNome !== undefined && !['Todos'].some((i) => args.consorcioNome?.includes(i))) { + query = query + ` AND tv."nomeConsorcio" in('${args.consorcioNome?.join("','")}')`; + } else if ((['Todos'].some((i) => args.consorcioNome?.includes(i)) && ['Todos'].some((i) => args.favorecidoNome?.includes(i))) || (args.consorcioNome !== undefined && args.favorecidoNome !== undefined)) { + query = + query + + ` AND tv."nomeConsorcio" in ('STPC','STPL','VLT','Santa Cruz', + 'Internorte','Intersul','Transcarioca','MobiRio','TEC') `; + } else if (['Todos'].some((i) => args.favorecidoNome?.includes(i))) { + query = query + ` AND tv."nomeConsorcio" in('STPC','STPL') `; + } + query = query + ` AND tv."itemTransacaoAgrupadoId" IS NULL) `; + + query = query + ` SELECT DISTINCT `; + query = query + ` res.*, `; + query = query + ` COALESCE(sub.subTotal, 0) AS subTotal,`; + query = query + ` total_data.Total`; + query = query + ` FROM (`; + + let body = ` SELECT DISTINCT + tv.id, + (tv."datetimeTransacao":: DATE)::VARCHAR AS datatransacao, + CASE + WHEN tv."nomeConsorcio" = 'VLT' THEN (tv."datetimeTransacao":: DATE + INTERVAL '2 day')::VARCHAR + WHEN tv."nomeConsorcio" <> 'VLT' AND EXTRACT(DOW FROM tv."datetimeTransacao") = 0 THEN + (tv."datetimeTransacao":: DATE + INTERVAL '3 day')::VARCHAR + WHEN tv."nomeConsorcio" <> 'VLT' AND EXTRACT(DOW FROM tv."datetimeTransacao") = 1 THEN + (tv."datetimeTransacao":: DATE + INTERVAL '2 day')::VARCHAR + WHEN tv."nomeConsorcio" <> 'VLT' AND EXTRACT(DOW FROM tv."datetimeTransacao") = 2 THEN + (tv."datetimeTransacao":: DATE + INTERVAL '8 day')::VARCHAR + WHEN tv."nomeConsorcio" <> 'VLT' AND EXTRACT(DOW FROM tv."datetimeTransacao") = 3 THEN + (tv."datetimeTransacao":: DATE + INTERVAL '7 day')::VARCHAR + WHEN tv."nomeConsorcio" <> 'VLT' AND EXTRACT(DOW FROM tv."datetimeTransacao") = 4 THEN + (tv."datetimeTransacao":: DATE + INTERVAL '6 day')::VARCHAR + WHEN tv."nomeConsorcio" <> 'VLT' AND EXTRACT(DOW FROM tv."datetimeTransacao") = 5 THEN + (tv."datetimeTransacao":: DATE + INTERVAL '5 day')::VARCHAR + WHEN tv."nomeConsorcio" <> 'VLT' AND EXTRACT(DOW FROM tv."datetimeTransacao") = 6 THEN + (tv."datetimeTransacao":: DATE + INTERVAL '4 day')::VARCHAR + END AS datapagamento, + tv."nomeConsorcio" AS consorcio, + COALESCE(cf.nome,uu."fullName") AS favorecido, + tv."operadoraCpfCnpj" cpfCnpj, + ROUND(tv."valorPago", 2)::FLOAT AS valor, + 'a pagar' AS status, + '' AS mensagem_status `; + + body = body + `FROM transacao_view tv `; + body = body + `LEFT JOIN cliente_favorecido cf ON tv."operadoraCpfCnpj" = cf."cpfCnpj" `; + body = body + `LEFT JOIN public.user uu on uu."cpfCnpj"=tv."operadoraCpfCnpj" `; + let conditions = `where tv."valorPago" >0 and`; + + if (dataInicio !== undefined && dataFim !== undefined && (dataFim === dataInicio || new Date(dataFim) > new Date(dataInicio))) conditions = conditions + ` tv."datetimeTransacao" between '${dataInicio + ' 00:00:00'}' and '${dataFim + ' 23:59:59'}' `; + + if (args.consorcioNome !== undefined && !['Todos'].some((i) => args.consorcioNome?.includes(i))) { + conditions = conditions + ` and tv."nomeConsorcio" in('${args.consorcioNome?.join("','")}')`; + } else if (args.favorecidoNome !== undefined && !['Todos'].some((i) => args.favorecidoNome?.includes(i))) { + conditions = conditions + ` and cf."nome" in('${args.favorecidoNome?.join("','")}')`; + } else if ((['Todos'].some((i) => args.consorcioNome?.includes(i)) && ['Todos'].some((i) => args.favorecidoNome?.includes(i))) || (args.consorcioNome !== undefined && args.favorecidoNome !== undefined)) { + conditions = + conditions + + ` and tv."nomeConsorcio" + in ('STPC','STPL','VLT','Santa Cruz','Internorte','Intersul','Transcarioca','MobiRio','TEC') `; + } else if (['Todos'].some((i) => args.favorecidoNome?.includes(i)) && args.consorcioNome !== undefined) { + conditions = conditions + ` and tv."nomeConsorcio" in('STPC','STPL',${args.consorcioNome?.join("','")}) `; + } else if (['Todos'].some((i) => args.favorecidoNome?.includes(i))) { + conditions = conditions + ` and tv."nomeConsorcio" in('STPC','STPL') `; + } - if(valorMax!==undefined) - query = query + ` and resul."valor"<=${valorMax}`; + let footer = `) AS res + LEFT JOIN subtotal_data sub + ON res."consorcio" = sub."nomeConsorcio" + CROSS JOIN total_data + ORDER BY res."consorcio", res."favorecido", res."datapagamento"`; - this.logger.debug(query); - return query; + let result = ` select * from ( ` + query + body + conditions + footer + ` ) as tt where (1=1)`; + return result; } - - private getQueryOperadores(dataInicio:string,dataFim:string,pago?:boolean,valorMin?:number, - valorMax?:number,favorecidoNome?:string[]){ - let query = - ` select * from ( - select cs."favorecido" nomeFavorecido,sum(cs."valor_agrupado")::float valor - from ( - select distinct ita.id AS id, - ita."nomeConsorcio" AS consorcio, - cf.nome AS favorecido, - cf."cpfCnpj" AS favorecido_cpfcnpj, - da."valorLancamento" AS valor_agrupado - from transacao_view tv - inner join item_transacao_agrupado ita on tv."itemTransacaoAgrupadoId"=ita.id - inner join detalhe_a da on da."itemTransacaoAgrupadoId"= ita.id - inner join item_transacao it on ita.id = it."itemTransacaoAgrupadoId" - inner join arquivo_publicacao ap on ap."itemTransacaoId"=it.id - inner join cliente_favorecido cf on cf.id=it."clienteFavorecidoId" - WHERE ita."nomeConsorcio" in('STPC','STPL') `; - if(dataInicio!==undefined && dataFim!==undefined && - (dataFim === dataInicio || new Date(dataFim)>new Date(dataInicio))) - query = query +` and da."dataVencimento" between '${dataInicio}' and '${dataFim}'`; - if(pago!==undefined) - query = query + ` and ap."isPago"=${pago}`; + public getQueryNaoApagar(args: IFindPublicacaoRelatorio) { + const dataInicio = args.dataInicio.toISOString().slice(0, 10); + const dataFim = args.dataFim.toISOString().slice(0, 10); + let query = ` select distinct res.*, `; + query = query + `(select sum(ss."valorLancamento")::float from `; + query = + query + + ` (select distinct dta.id,dta."valorLancamento" + from detalhe_a dta + inner join item_transacao_agrupado tt on dta."itemTransacaoAgrupadoId"=tt.id + inner join transacao_agrupado tta on tta."id"=tt."transacaoAgrupadoId" and tta."statusId"<>'5' + left join item_transacao itt on itt."itemTransacaoAgrupadoId" = tt."id" + left join arquivo_publicacao app on app."itemTransacaoId"=itt.id + LEFT JOIN cliente_favorecido cf ON itt."clienteFavorecidoId" = cf.id + WHERE `; + if (dataInicio !== undefined && dataFim !== undefined && (dataFim === dataInicio || new Date(dataFim) > new Date(dataInicio))) query = query + ` dta."dataVencimento" between '${dataInicio}' and '${dataFim}'`; + if (args.emProcessamento !== undefined && args.emProcessamento === true) { + query = query + ` and app."isPago"=false and TRIM(dta."ocorrenciasCnab")='' `; + } else if (args.pago !== undefined) { + query = query + ` and app."isPago"=${args.pago} and TRIM(dta."ocorrenciasCnab")<>'' `; + } + query = query + ` and tt."nomeConsorcio"=res.consorcio `; + if (args.favorecidoNome !== undefined && !['Todos'].some((i) => args.favorecidoNome?.includes(i))) { + query = query + ` and cf."nome" in('${args.favorecidoNome?.join("','")}')`; + } + query = query + ` )as ss) as subTotal, `; + + query = + query + + `(select sum(tt."valorLancamento")::float from + (select distinct dta.id,dta."valorLancamento" + from detalhe_a dta + inner join item_transacao_agrupado tt on dta."itemTransacaoAgrupadoId"=tt.id + inner join transacao_agrupado tta on tta."id"=tt."transacaoAgrupadoId" and tta."statusId"<>'5' + left join item_transacao itt on itt."itemTransacaoAgrupadoId" = tt."id" + left join arquivo_publicacao app on app."itemTransacaoId"=itt.id + LEFT JOIN cliente_favorecido cf ON itt."clienteFavorecidoId" = cf.id + WHERE `; + if (dataInicio !== undefined && dataFim !== undefined && (dataFim === dataInicio || new Date(dataFim) > new Date(dataInicio))) query = query + ` dta."dataVencimento" between '${dataInicio}' and '${dataFim}'`; + if (args.emProcessamento !== undefined && args.emProcessamento === true) { + query = query + ` and app."isPago"=false and TRIM(dta."ocorrenciasCnab")='' `; + } else if (args.pago !== undefined) { + query = query + ` and app."isPago"=${args.pago} and TRIM(dta."ocorrenciasCnab")<>'' `; + } - if(pago ===false) - query = query + ` and da."ocorrenciasCnab" is not null - and da."ocorrenciasCnab" not in('00','BD') `; - - if(favorecidoNome!==undefined && !(['Todos'].some(i=>favorecidoNome?.includes(i)))) - query = query +` and cf.nome in('${favorecidoNome?.join("','")}')`; + if (args.consorcioNome !== undefined && !['Todos'].some((i) => args.consorcioNome?.includes(i))) { + query = query + ` and tt."nomeConsorcio" in('${args.consorcioNome?.join("','")}')`; + } else if (args.favorecidoNome !== undefined && !['Todos'].some((i) => args.favorecidoNome?.includes(i))) { + query = query + ` and cf."nome" in('${args.favorecidoNome?.join("','")}')`; + } else if ((['Todos'].some((i) => args.consorcioNome?.includes(i)) && ['Todos'].some((i) => args.favorecidoNome?.includes(i))) || (args.consorcioNome !== undefined && args.favorecidoNome !== undefined)) { + query = + query + + ` and tt."nomeConsorcio" in ('STPC','STPL','VLT','Santa Cruz', + 'Internorte','Intersul','Transcarioca','MobiRio','TEC') `; + } else if (['Todos'].some((i) => args.favorecidoNome?.includes(i))) { + query = query + ` and tt."nomeConsorcio" in('STPC','STPL','TEC') `; + } + query = query + ` )as tt )as total `; + + query = query + `from ( `; + query += ` + SELECT DISTINCT ON (q.tv_id) * + FROM + ( + SELECT + it.*, + tv.id AS tv_id, + (it."dataOrdem"::DATE - tv."datetimeProcessamento"::DATE) AS date_priority, + it.id AS it_id, + tv."datetimeProcessamento" AS dataprocessamento, + tv."valorPago"::FLOAT AS valor, + COALESCE(it.datatransacao_vlt, tv."datetimeTransacao"::VARCHAR) AS datatransacao + FROM + ( + `; + + query = + query + + ` + select distinct + it.id, + case + when (it."nomeConsorcio" = 'VLT') and EXTRACT( DOW FROM da."dataVencimento")=1 THEN --segunda + (da."dataVencimento":: Date - INTERVAL '4 day')::varchar + when (it."nomeConsorcio" = 'VLT') and EXTRACT( DOW FROM da."dataVencimento")=2 THEN --terça + (da."dataVencimento":: Date - INTERVAL '4 day')::varchar + when (it."nomeConsorcio" = 'VLT') and EXTRACT( DOW FROM da."dataVencimento")=3 THEN --quarta + (da."dataVencimento":: Date - INTERVAL '2 day')::varchar + when (it."nomeConsorcio" = 'VLT') and EXTRACT( DOW FROM da."dataVencimento")=4 THEN --quinta + (da."dataVencimento":: Date - INTERVAL '2 day')::varchar + when (it."nomeConsorcio" = 'VLT') and EXTRACT( DOW FROM da."dataVencimento")=5 THEN --Sexta + (da."dataVencimento":: Date - INTERVAL '2 day')::varchar + end as datatransacao_vlt, + da."dataVencimento"::date::Varchar As datapagamento, + cf."cpfCnpj", + da."dataEfetivacao", -- dto + da."dataVencimento", -- dto + it."nomeConsorcio" AS consorcio, -- dto + cf.nome AS favorecido, -- dto + it."valor"::float AS valor_ordem, + it."itemTransacaoAgrupadoId", + it."dataOrdem", + case + when(not(ap."isPago") and TRIM(da."ocorrenciasCnab")='')then 'Aguardando Pagamento' + when (ap."isPago") then 'pago' + when (not (ap."isPago")) then 'naopago' + else 'apagar' end AS status, + case when (not (ap."isPago")) then oc."message" + else '' end As mensagem_status `; + + query = + query + + ` from item_transacao_agrupado ita + inner join detalhe_a da on da."itemTransacaoAgrupadoId"= ita.id + inner join item_transacao it on ita.id = it."itemTransacaoAgrupadoId" + inner join transacao_agrupado ta on ta."id"=ita."transacaoAgrupadoId" and ta."statusId"<>'5' + inner join arquivo_publicacao ap on ap."itemTransacaoId"=it.id + inner join cliente_favorecido cf on cf.id=it."clienteFavorecidoId" + left join ocorrencia oc on oc."detalheAId"=da.id + where `; + if (dataInicio !== undefined && dataFim !== undefined && (dataFim === dataInicio || new Date(dataFim) > new Date(dataInicio))) query = query + ` da."dataVencimento" between '${dataInicio}' and '${dataFim}'`; + + if (args.consorcioNome !== undefined && !['Todos'].some((i) => args.consorcioNome?.includes(i))) { + query = query + ` and it."nomeConsorcio" in('${args.consorcioNome?.join("','")}')`; + } else if (args.favorecidoNome !== undefined && !['Todos'].some((i) => args.favorecidoNome?.includes(i))) { + query = query + ` and cf."nome" in('${args.favorecidoNome?.join("','")}')`; + } else if ((['Todos'].some((i) => args.consorcioNome?.includes(i)) && ['Todos'].some((i) => args.favorecidoNome?.includes(i))) || (args.consorcioNome !== undefined && args.favorecidoNome !== undefined)) { + query = + query + + ` and it."nomeConsorcio" + in ('STPC','STPL','VLT','Santa Cruz','Internorte','Intersul','Transcarioca','MobiRio','TEC') `; + } else if (['Todos'].some((i) => args.favorecidoNome?.includes(i))) { + query = query + ` and it."nomeConsorcio" in('STPC','STPL','TEC') `; + } - query = query + `) as cs `; + if (args.emProcessamento !== undefined && args.emProcessamento === true) { + query = query + ` and ap."isPago"=false and TRIM(da."ocorrenciasCnab")='' `; + } else if (args.pago !== undefined) { + query = query + ` and ap."isPago"=${args.pago} and TRIM(da."ocorrenciasCnab")<>'' `; + } - query = query + ` group by cs."consorcio", cs."favorecido" `; + if (args.valorMin !== undefined) query = query + ` and it."valor">=${args.valorMin}`; - query = query + ` order by cs."favorecido" `; + if (args.valorMax !== undefined) query = query + ` and it."valor"<=${args.valorMax}`; - query = query + `) as resul where (1=1) `; - - if(valorMin!==undefined) - query = query +` and resul."valor">=${valorMin}`; + query += ` + ) it + LEFT JOIN transacao_view tv ON tv."itemTransacaoAgrupadoId" = it."itemTransacaoAgrupadoId" + AND tv."datetimeProcessamento"::DATE BETWEEN (it."dataOrdem"::DATE) - INTERVAL '6 DAYS' + AND it."dataOrdem"::DATE - INTERVAL '1 DAY' + ) q + ORDER BY q.tv_id, q.date_priority, q.it_id DESC + `; + query = + query + + ` ) as res + order by "consorcio", "favorecido","datapagamento", "dataprocessamento" `; - if(valorMax!==undefined) - query = query + ` and resul."valor"<=${valorMax}`; + this.logger.debug(compactQuery(query)); return query; } - - public async findAnalitico(args: IFindPublicacaoRelatorio): Promise { - let queryConsorcio = ''; - if(args.aPagar === true && args.favorecidoNome ===undefined){ - queryConsorcio = this.getQueryAPagarConsorcio(args.dataInicio.toISOString().slice(0,10), - args.dataFim.toISOString().slice(0,10),args.valorMin, - args.valorMax,args.consorcioNome); - } - - if((args.aPagar === undefined || args.aPagar === false) && - (args.consorcioNome!==undefined || args.favorecidoNome === undefined)){ - queryConsorcio = this.getQueryConsorcio(args.dataInicio.toISOString().slice(0,10), - args.dataFim.toISOString().slice(0,10),args.pago,args.valorMin, - args.valorMax,args.consorcioNome); - } - - let queryOperadores =''; - if(args.aPagar === true && args.consorcioNome===undefined){ - queryOperadores = this.getQueryAPagarOperadores(args.dataInicio.toISOString().slice(0,10), - args.dataFim.toISOString().slice(0,10),args.valorMin, - args.valorMax,args.favorecidoNome); - } - - if((args.aPagar === undefined || args.aPagar === false) && (args.consorcioNome===undefined || args.favorecidoNome !==undefined)) { - queryOperadores = this.getQueryOperadores(args.dataInicio.toISOString().slice(0,10), - args.dataFim.toISOString().slice(0,10),args.pago,args.valorMin,args.valorMax,args.favorecidoNome); - } - - if(queryConsorcio !=='' && queryOperadores!==''){ - queryOperadores = ` union all `+queryOperadores; - } - - const query = queryConsorcio + queryOperadores; - this.logger.debug(query); - const queryRunner = this.dataSource.createQueryRunner(); - await queryRunner.connect(); - let result: any[] = await queryRunner.query(query); - queryRunner.release(); - const analiticos = result.map((r) => new RelatorioAnaliticoDto(r)); - return analiticos; - } -} \ No newline at end of file +} diff --git a/src/relatorio/relatorio-consolidado.repository.ts b/src/relatorio/relatorio-consolidado.repository.ts index 09de8278..30958904 100644 --- a/src/relatorio/relatorio-consolidado.repository.ts +++ b/src/relatorio/relatorio-consolidado.repository.ts @@ -5,20 +5,21 @@ import { RelatorioConsolidadoDto } from './dtos/relatorio-consolidado.dto'; import { IFindPublicacaoRelatorio } from './interfaces/find-publicacao-relatorio.interface'; import { CustomLogger } from 'src/utils/custom-logger'; - @Injectable() -export class RelatorioConsolidadoRepository { - - constructor(@InjectDataSource() - private readonly dataSource: DataSource) {} - - private logger = new CustomLogger(RelatorioConsolidadoRepository.name, { timestamp: true }); - - private getQueryAPagarConsorcio(dataInicio:string,dataFim:string, - valorMin?:number,valorMax?:number,nomeConsorcio?:string[]){ +export class RelatorioConsolidadoRepository { + constructor( + @InjectDataSource() + private readonly dataSource: DataSource, + ) {} + + private logger = new CustomLogger(RelatorioConsolidadoRepository.name, { timestamp: true }); + + private getQueryAPagarConsorcio(dataInicio: string, dataFim: string, valorMin?: number, valorMax?: number, nomeConsorcio?: string[]) { let query = ` select * from ( `; query = query + `select cs."consorcio" nomeFavorecido,sum(cs."valor_agrupado")::float valor from ( `; - query = query + ` select distinct tv.id AS id, + query = + query + + ` select distinct tv.id AS id, tv."nomeConsorcio" AS consorcio, tv."valorPago" AS valor_agrupado from transacao_view tv @@ -26,39 +27,37 @@ export class RelatorioConsolidadoRepository { and tv."valorPago" is not null and tv."valorPago" >0 `; - if(dataInicio!==undefined && dataFim!==undefined && - (dataFim === dataInicio || new Date(dataFim)>new Date(dataInicio))) - query = query +` and tv."datetimeTransacao" between '${dataInicio+' 00:00:00'}' and '${dataFim+' 23:59:59'}' `; + if (dataInicio !== undefined && dataFim !== undefined && (dataFim === dataInicio || new Date(dataFim) > new Date(dataInicio))) query = query + ` and tv."datetimeTransacao" between '${dataInicio + ' 00:00:00'}' and '${dataFim + ' 23:59:59'}' `; - if(['Todos'].some(i=>nomeConsorcio?.includes(i))) { - query = query +` AND tv."nomeConsorcio" in ('STPC','STPL','VLT','Santa Cruz', + if (['Todos'].some((i) => nomeConsorcio?.includes(i))) { + query = + query + + ` AND tv."nomeConsorcio" in ('STPC','STPL','VLT','Santa Cruz', 'Internorte','Intersul','Transcarioca','MobiRio','TEC') `; - }else if((nomeConsorcio!==undefined) && !(['Todos'].some(i=>nomeConsorcio?.includes(i)))) - query = query +` and tv."nomeConsorcio" in('${nomeConsorcio?.join("','")}')`; - - query = query + `) as cs `; + } else if (nomeConsorcio !== undefined && !['Todos'].some((i) => nomeConsorcio?.includes(i))) query = query + ` and tv."nomeConsorcio" in('${nomeConsorcio?.join("','")}')`; - query = query + ` group by cs."consorcio"` + query = query + `) as cs `; + + query = query + ` group by cs."consorcio"`; query = query + ` order by cs."consorcio" `; - query = query + `) as resul where (1=1) `; - - if(valorMin!==undefined) - query = query +` and resul."valor">=${valorMin}`; + query = query + `) as resul where (1=1) `; + + if (valorMin !== undefined) query = query + ` and resul."valor">=${valorMin}`; - if(valorMax!==undefined) - query = query + ` and resul."valor"<=${valorMax}`; + if (valorMax !== undefined) query = query + ` and resul."valor"<=${valorMax}`; - this.logger.debug(query); - return query; - } - - private getQueryConsorcio(dataInicio:string,dataFim:string,pago?:boolean, - valorMin?:number,valorMax?:number,nomeConsorcio?:string[],emProcessamento?:boolean){ + this.logger.debug(query); + return query; + } + + private getQueryConsorcio(dataInicio: string, dataFim: string, pago?: boolean, valorMin?: number, valorMax?: number, nomeConsorcio?: string[], emProcessamento?: boolean) { let query = ` select * from ( `; - query = query +` select cs."consorcio" nomeFavorecido,sum(cs."valor_agrupado")::float valor from ( `; - query = query +` select distinct ita.id AS id, + query = query + ` select cs."consorcio" nomeFavorecido,sum(cs."valor_agrupado")::float valor from ( `; + query = + query + + ` select distinct ita.id AS id, ita."nomeConsorcio" AS consorcio, da."valorLancamento" AS valor_agrupado from transacao_agrupado ta @@ -68,43 +67,39 @@ export class RelatorioConsolidadoRepository { inner join arquivo_publicacao ap on ap."itemTransacaoId"=it.id where ta."statusId"<>5 `; - if(dataInicio!==undefined && dataFim!==undefined && - (dataFim === dataInicio || new Date(dataFim)>new Date(dataInicio))) - query = query + ` and da."dataVencimento" between '${dataInicio}' and '${dataFim}'`; + if (dataInicio !== undefined && dataFim !== undefined && (dataFim === dataInicio || new Date(dataFim) > new Date(dataInicio))) query = query + ` and da."dataVencimento" between '${dataInicio}' and '${dataFim}'`; - if((nomeConsorcio!==undefined) && !(['Todos'].some(i=>nomeConsorcio?.includes(i)))) - query = query +` and ita."nomeConsorcio" in('${nomeConsorcio?.join("','")}')`; - - if(emProcessamento==true){ - query = query + ` and ap."isPago"=false and TRIM(da."ocorrenciasCnab")='' ` - }else if(pago!==undefined){ + if (nomeConsorcio !== undefined && !['Todos'].some((i) => nomeConsorcio?.includes(i))) query = query + ` and ita."nomeConsorcio" in('${nomeConsorcio?.join("','")}')`; + + if (emProcessamento == true) { + query = query + ` and ap."isPago"=false and TRIM(da."ocorrenciasCnab")='' `; + } else if (pago !== undefined) { query = query + ` and ap."isPago"=${pago} and TRIM(da."ocorrenciasCnab")<>'' `; - } + } - query = query + `) as cs `; + query = query + `) as cs `; - query = query + ` group by cs."consorcio"` + query = query + ` group by cs."consorcio"`; query = query + ` order by cs."consorcio" `; - query = query + `) as resul where (1=1) `; - - if(valorMin!==undefined) - query = query +` and resul."valor">=${valorMin}`; + query = query + `) as resul where (1=1) `; - if(valorMax!==undefined) - query = query + ` and resul."valor"<=${valorMax}`; - - return query; + if (valorMin !== undefined) query = query + ` and resul."valor">=${valorMin}`; + + if (valorMax !== undefined) query = query + ` and resul."valor"<=${valorMax}`; + + return query; } - - private getQueryAPagarOperadores(dataInicio:string,dataFim:string,valorMin?:number, - valorMax?:number,favorecidoNome?:string[]){ + + private getQueryAPagarOperadores(dataInicio: string, dataFim: string, valorMin?: number, valorMax?: number, favorecidoNome?: string[]) { let query = `select * from ( `; query = query + ` select cs."favorecido" nomeFavorecido,sum(cs."valor_agrupado")::float valor from ( `; - query = query + ` select distinct tv.id AS id, + query = + query + + ` select distinct tv.id AS id, tv."nomeConsorcio" AS consorcio, cf.nome AS favorecido, cf."cpfCnpj" AS favorecido_cpfcnpj, @@ -114,35 +109,33 @@ export class RelatorioConsolidadoRepository { where tv."itemTransacaoAgrupadoId" is null and tv."valorPago" is not null and tv."valorPago" >0 `; - - query = query +` and tv."datetimeTransacao" between '${dataInicio+' 00:00:00'}' and '${dataFim+' 23:59:59'}' `; - - if(favorecidoNome!==undefined && !(['Todos'].some(i=>favorecidoNome?.includes(i)))) - query = query +` and cf.nome in('${favorecidoNome?.join("','")}')`; - query = query + ` ) as cs `; + query = query + ` and tv."datetimeTransacao" between '${dataInicio + ' 00:00:00'}' and '${dataFim + ' 23:59:59'}' `; + + if (favorecidoNome !== undefined && !['Todos'].some((i) => favorecidoNome?.includes(i))) query = query + ` and cf.nome in('${favorecidoNome?.join("','")}')`; + + query = query + ` ) as cs `; query = query + ` group by cs."consorcio", cs."favorecido" `; query = query + ` order by cs."favorecido" `; - query = query + `) as resul where (1=1) `; - - if(valorMin!==undefined) - query = query +` and resul."valor">=${valorMin}`; + query = query + `) as resul where (1=1) `; + + if (valorMin !== undefined) query = query + ` and resul."valor">=${valorMin}`; - if(valorMax!==undefined) - query = query + ` and resul."valor"<=${valorMax}`; + if (valorMax !== undefined) query = query + ` and resul."valor"<=${valorMax}`; this.logger.debug(query); return query; } - - private getQueryOperadores(dataInicio:string,dataFim:string,pago?:boolean,valorMin?:number, - valorMax?:number,favorecidoNome?:string[],emProcessamento?:boolean){ - let query = ` select * from ( `; - query = query + ` select cs."favorecido" nomeFavorecido,sum(cs."valor_agrupado")::float valor from ( `; - query = query + ` select distinct ita.id AS id, + + private getQueryOperadores(dataInicio: string, dataFim: string, pago?: boolean, valorMin?: number, valorMax?: number, favorecidoNome?: string[], emProcessamento?: boolean) { + let query = ` select * from ( `; + query = query + ` select cs."favorecido" nomeFavorecido,sum(cs."valor_agrupado")::float valor from ( `; + query = + query + + ` select distinct ita.id AS id, ita."nomeConsorcio" AS consorcio, cf.nome AS favorecido, cf."cpfCnpj" AS favorecido_cpfcnpj, @@ -154,66 +147,52 @@ export class RelatorioConsolidadoRepository { inner join arquivo_publicacao ap on ap."itemTransacaoId"=it.id inner join cliente_favorecido cf on cf.id=it."clienteFavorecidoId" where ta."statusId"<>5 and ita."nomeConsorcio" in('STPC','STPL','TEC') `; - if(dataInicio!==undefined && dataFim!==undefined && - (dataFim === dataInicio || new Date(dataFim)>new Date(dataInicio))) - query = query +` and da."dataVencimento" between '${dataInicio}' and '${dataFim}'`; - if(emProcessamento==true){ - query = query + ` and ap."isPago"=false and TRIM(da."ocorrenciasCnab")='' ` - }else if(pago!==undefined){ - query = query + ` and ap."isPago"=${pago} and TRIM(da."ocorrenciasCnab")<>'' `; - } - - - if(favorecidoNome!==undefined && !(['Todos'].some(i=>favorecidoNome?.includes(i)))) - query = query +` and cf.nome in('${favorecidoNome?.join("','")}')`; - - query = query + `) as cs `; - - query = query + ` group by cs."consorcio", cs."favorecido" `; - - query = query + ` order by cs."favorecido" `; - - query = query + `) as resul where (1=1) `; - - if(valorMin!==undefined) - query = query +` and resul."valor">=${valorMin}`; - - if(valorMax!==undefined) - query = query + ` and resul."valor"<=${valorMax}`; + if (dataInicio !== undefined && dataFim !== undefined && (dataFim === dataInicio || new Date(dataFim) > new Date(dataInicio))) query = query + ` and da."dataVencimento" between '${dataInicio}' and '${dataFim}'`; + if (emProcessamento == true) { + query = query + ` and ap."isPago"=false and TRIM(da."ocorrenciasCnab")='' `; + } else if (pago !== undefined) { + query = query + ` and ap."isPago"=${pago} and TRIM(da."ocorrenciasCnab")<>'' `; + } + + if (favorecidoNome !== undefined && !['Todos'].some((i) => favorecidoNome?.includes(i))) query = query + ` and cf.nome in('${favorecidoNome?.join("','")}')`; + + query = query + `) as cs `; + + query = query + ` group by cs."consorcio", cs."favorecido" `; + + query = query + ` order by cs."favorecido" `; + + query = query + `) as resul where (1=1) `; + + if (valorMin !== undefined) query = query + ` and resul."valor">=${valorMin}`; + + if (valorMax !== undefined) query = query + ` and resul."valor"<=${valorMax}`; return query; } - public async findConsolidado(args: IFindPublicacaoRelatorio): Promise { + public async findConsolidado(args: IFindPublicacaoRelatorio): Promise { let queryConsorcio = ''; - if(args.aPagar === true && args.favorecidoNome ===undefined){ - queryConsorcio = this.getQueryAPagarConsorcio(args.dataInicio.toISOString().slice(0,10), - args.dataFim.toISOString().slice(0,10),args.valorMin, - args.valorMax,args.consorcioNome); - } - - if((args.aPagar === undefined || args.aPagar === false) && - (args.consorcioNome!==undefined || args.favorecidoNome === undefined)){ - queryConsorcio = this.getQueryConsorcio(args.dataInicio.toISOString().slice(0,10), - args.dataFim.toISOString().slice(0,10),args.pago,args.valorMin, - args.valorMax,args.consorcioNome,args.emProcessamento); - } - - let queryOperadores =''; - if(args.aPagar === true && args.consorcioNome===undefined){ - queryOperadores = this.getQueryAPagarOperadores(args.dataInicio.toISOString().slice(0,10), - args.dataFim.toISOString().slice(0,10),args.valorMin, - args.valorMax,args.favorecidoNome); - } - - if((args.aPagar === undefined || args.aPagar === false) && (args.consorcioNome===undefined || args.favorecidoNome !==undefined)) { - queryOperadores = this.getQueryOperadores(args.dataInicio.toISOString().slice(0,10), - args.dataFim.toISOString().slice(0,10),args.pago,args.valorMin,args.valorMax,args.favorecidoNome,args.emProcessamento); + if (args.aPagar === true && args.favorecidoNome === undefined) { + queryConsorcio = this.getQueryAPagarConsorcio(args.dataInicio.toISOString().slice(0, 10), args.dataFim.toISOString().slice(0, 10), args.valorMin, args.valorMax, args.consorcioNome); } - - if(queryConsorcio !=='' && queryOperadores!==''){ - queryOperadores = ` union all `+queryOperadores; - } - + + if ((args.aPagar === undefined || args.aPagar === false) && (args.consorcioNome !== undefined || args.favorecidoNome === undefined)) { + queryConsorcio = this.getQueryConsorcio(args.dataInicio.toISOString().slice(0, 10), args.dataFim.toISOString().slice(0, 10), args.pago, args.valorMin, args.valorMax, args.consorcioNome, args.emProcessamento); + } + + let queryOperadores = ''; + if (args.aPagar === true && args.consorcioNome === undefined) { + queryOperadores = this.getQueryAPagarOperadores(args.dataInicio.toISOString().slice(0, 10), args.dataFim.toISOString().slice(0, 10), args.valorMin, args.valorMax, args.favorecidoNome); + } + + if ((args.aPagar === undefined || args.aPagar === false) && (args.consorcioNome === undefined || args.favorecidoNome !== undefined)) { + queryOperadores = this.getQueryOperadores(args.dataInicio.toISOString().slice(0, 10), args.dataFim.toISOString().slice(0, 10), args.pago, args.valorMin, args.valorMax, args.favorecidoNome, args.emProcessamento); + } + + if (queryConsorcio !== '' && queryOperadores !== '') { + queryOperadores = ` union all ` + queryOperadores; + } + const query = queryConsorcio + queryOperadores; this.logger.debug(query); const queryRunner = this.dataSource.createQueryRunner(); @@ -222,5 +201,5 @@ export class RelatorioConsolidadoRepository { queryRunner.release(); const consolidados = result.map((r) => new RelatorioConsolidadoDto(r)); return consolidados; - } -} \ No newline at end of file + } +} diff --git a/src/relatorio/relatorio-sintetico.repository.ts b/src/relatorio/relatorio-sintetico.repository.ts index f4c1759a..886d8ce7 100644 --- a/src/relatorio/relatorio-sintetico.repository.ts +++ b/src/relatorio/relatorio-sintetico.repository.ts @@ -1,84 +1,83 @@ import { Injectable } from '@nestjs/common'; import { InjectDataSource } from '@nestjs/typeorm'; -import { DataSource } from 'typeorm'; -import { IFindPublicacaoRelatorio } from './interfaces/find-publicacao-relatorio.interface'; import { CustomLogger } from 'src/utils/custom-logger'; +import { DataSource } from 'typeorm'; import { RelatorioSinteticoDto } from './dtos/relatorio-sintetico.dto'; -import { query } from 'express'; +import { IFindPublicacaoRelatorio } from './interfaces/find-publicacao-relatorio.interface'; +import { compactQuery } from 'src/utils/console-utils'; @Injectable() -export class RelatorioSinteticoRepository { - - constructor(@InjectDataSource() - private readonly dataSource: DataSource) {} +export class RelatorioSinteticoRepository { + constructor( + @InjectDataSource() + private readonly dataSource: DataSource, + ) {} + + private logger = new CustomLogger(RelatorioSinteticoRepository.name, { timestamp: true }); - private logger = new CustomLogger(RelatorioSinteticoRepository.name, { timestamp: true }); - - private getQuery(args:IFindPublicacaoRelatorio){ - if(args.aPagar === true){ + private getQuery(args: IFindPublicacaoRelatorio) { + if (args.aPagar === true) { return this.getQueryApagar(args); - }else if (args.aPagar === false || args.aPagar === undefined){ + } else if (args.aPagar === false || args.aPagar === undefined) { return this.getQueryNaoApagar(args); - }else{ - return this.getQueryApagar(args) + ` union all ` +this.getQueryNaoApagar(args); + } else { + return this.getQueryApagar(args) + ` union all ` + this.getQueryNaoApagar(args); } - } - + } + public async findSintetico(args: IFindPublicacaoRelatorio): Promise { const query = this.getQuery(args); - this.logger.debug(query); + this.logger.debug(compactQuery(query)); const queryRunner = this.dataSource.createQueryRunner(); await queryRunner.connect(); - let result: any[] = await queryRunner.query(query); + let result: any[] = await queryRunner.query(compactQuery(query)); queryRunner.release(); const sinteticos = result.map((r) => new RelatorioSinteticoDto(r)); return sinteticos; - } + } - public getQueryApagar(args:IFindPublicacaoRelatorio){ - const dataInicio = args.dataInicio.toISOString().slice(0,10) - const dataFim = args.dataFim.toISOString().slice(0,10) + public getQueryApagar(args: IFindPublicacaoRelatorio) { + const dataInicio = args.dataInicio.toISOString().slice(0, 10); + const dataFim = args.dataFim.toISOString().slice(0, 10); let query = `WITH subtotal_data AS ( `; - query = query + ` SELECT `; - query = query + ` tv."nomeConsorcio", `; - query = query + ` SUM(tv."valorPago") AS subTotal `; - query = query + ` FROM transacao_view tv `; - query = query + ` WHERE tv."valorPago" > 0 `; - if(dataInicio!==undefined && dataFim!==undefined && - (dataFim === dataInicio || new Date(dataFim)>new Date(dataInicio))) { - query = query + ` AND tv."datetimeTransacao" BETWEEN '${dataInicio+' 00:00:00'}' and '${dataFim+' 23:59:59'}' `; - } - query = query + ` AND tv."itemTransacaoAgrupadoId" IS NULL ` - query = query + ` GROUP BY tv."nomeConsorcio" ), `; - query = query + `total_data AS ( `; - query = query + ` SELECT `; - query = query + ` SUM(tv."valorPago") AS Total `; - query = query + ` FROM transacao_view tv `; - query = query + ` WHERE tv."valorPago" > 0 `; - if(dataInicio!==undefined && dataFim!==undefined && - (dataFim === dataInicio || new Date(dataFim)>new Date(dataInicio))) { - query = query + ` AND tv."datetimeTransacao" BETWEEN '${dataInicio+' 00:00:00'}' and '${dataFim+' 23:59:59'}' `; - } - - if((args.consorcioNome!==undefined) && !(['Todos'].some(i=>args.consorcioNome?.includes(i)))){ - query = query +` AND tv."nomeConsorcio" in('${args.consorcioNome?.join("','")}')`; - }else if((['Todos'].some(i=>args.consorcioNome?.includes(i))) - && (['Todos'].some(i=>args.favorecidoNome?.includes(i))) || - ((args.consorcioNome!==undefined) && (args.favorecidoNome!==undefined))){ - query = query +` AND tv."nomeConsorcio" in ('STPC','STPL','VLT','Santa Cruz', + query = query + ` SELECT `; + query = query + ` tv."nomeConsorcio", `; + query = query + ` SUM(tv."valorPago") AS subTotal `; + query = query + ` FROM transacao_view tv `; + query = query + ` WHERE tv."valorPago" > 0 `; + if (dataInicio !== undefined && dataFim !== undefined && (dataFim === dataInicio || new Date(dataFim) > new Date(dataInicio))) { + query = query + ` AND tv."datetimeTransacao" BETWEEN '${dataInicio + ' 00:00:00'}' and '${dataFim + ' 23:59:59'}' `; + } + query = query + ` AND tv."itemTransacaoAgrupadoId" IS NULL `; + query = query + ` GROUP BY tv."nomeConsorcio" ), `; + query = query + `total_data AS ( `; + query = query + ` SELECT `; + query = query + ` SUM(tv."valorPago") AS Total `; + query = query + ` FROM transacao_view tv `; + query = query + ` WHERE tv."valorPago" > 0 `; + if (dataInicio !== undefined && dataFim !== undefined && (dataFim === dataInicio || new Date(dataFim) > new Date(dataInicio))) { + query = query + ` AND tv."datetimeTransacao" BETWEEN '${dataInicio + ' 00:00:00'}' and '${dataFim + ' 23:59:59'}' `; + } + + if (args.consorcioNome !== undefined && !['Todos'].some((i) => args.consorcioNome?.includes(i))) { + query = query + ` AND tv."nomeConsorcio" in('${args.consorcioNome?.join("','")}')`; + } else if ((['Todos'].some((i) => args.consorcioNome?.includes(i)) && ['Todos'].some((i) => args.favorecidoNome?.includes(i))) || (args.consorcioNome !== undefined && args.favorecidoNome !== undefined)) { + query = + query + + ` AND tv."nomeConsorcio" in ('STPC','STPL','VLT','Santa Cruz', 'Internorte','Intersul','Transcarioca','MobiRio','TEC') `; - }else if((['Todos'].some(i=>args.favorecidoNome?.includes(i)))){ - query = query +` AND tv."nomeConsorcio" in('STPC','STPL') `; - } - query = query + ` AND tv."itemTransacaoAgrupadoId" IS NULL) `; + } else if (['Todos'].some((i) => args.favorecidoNome?.includes(i))) { + query = query + ` AND tv."nomeConsorcio" in('STPC','STPL') `; + } + query = query + ` AND tv."itemTransacaoAgrupadoId" IS NULL) `; - query = query +` SELECT DISTINCT `; - query = query +` res.*, `; - query = query +` COALESCE(sub.subTotal, 0) AS subTotal,`; - query = query +` total_data.Total`; - query = query +` FROM (`; + query = query + ` SELECT DISTINCT `; + query = query + ` res.*, `; + query = query + ` COALESCE(sub.subTotal, 0) AS subTotal,`; + query = query + ` total_data.Total`; + query = query + ` FROM (`; - let body = ` SELECT DISTINCT + let body = ` SELECT DISTINCT tv.id, (tv."datetimeTransacao":: DATE)::VARCHAR AS datatransacao, CASE @@ -105,63 +104,64 @@ export class RelatorioSinteticoRepository { 'a pagar' AS status, '' AS mensagem_status `; - body = body + `FROM transacao_view tv `; - body = body + `LEFT JOIN cliente_favorecido cf ON tv."operadoraCpfCnpj" = cf."cpfCnpj" `; - body = body +`LEFT JOIN public.user uu on uu."cpfCnpj"=tv."operadoraCpfCnpj" `; - let conditions = `where tv."valorPago" >0 and`; - - if(dataInicio!==undefined && dataFim!==undefined && - (dataFim === dataInicio || new Date(dataFim)>new Date(dataInicio))) - conditions = conditions + ` tv."datetimeTransacao" between '${dataInicio+' 00:00:00'}' and '${dataFim+' 23:59:59'}' `; + body = body + `FROM transacao_view tv `; + body = body + `LEFT JOIN cliente_favorecido cf ON tv."operadoraCpfCnpj" = cf."cpfCnpj" `; + body = body + `LEFT JOIN public.user uu on uu."cpfCnpj"=tv."operadoraCpfCnpj" `; + let conditions = `where tv."valorPago" >0 and`; - if((args.consorcioNome!==undefined) && !(['Todos'].some(i=>args.consorcioNome?.includes(i)))){ - conditions = conditions +` and tv."nomeConsorcio" in('${args.consorcioNome?.join("','")}')`; - }else if((args.favorecidoNome!==undefined) && !(['Todos'].some(i=>args.favorecidoNome?.includes(i)))){ - conditions = conditions +` and cf."nome" in('${args.favorecidoNome?.join("','")}')`; - }else if((['Todos'].some(i=>args.consorcioNome?.includes(i))) && (['Todos'].some(i=>args.favorecidoNome?.includes(i))) - || ((args.consorcioNome!==undefined) && (args.favorecidoNome!==undefined))){ - conditions = conditions +` and tv."nomeConsorcio" + if (dataInicio !== undefined && dataFim !== undefined && (dataFim === dataInicio || new Date(dataFim) > new Date(dataInicio))) conditions = conditions + ` tv."datetimeTransacao" between '${dataInicio + ' 00:00:00'}' and '${dataFim + ' 23:59:59'}' `; + + if (args.consorcioNome !== undefined && !['Todos'].some((i) => args.consorcioNome?.includes(i))) { + conditions = conditions + ` and tv."nomeConsorcio" in('${args.consorcioNome?.join("','")}')`; + } else if (args.favorecidoNome !== undefined && !['Todos'].some((i) => args.favorecidoNome?.includes(i))) { + conditions = conditions + ` and cf."nome" in('${args.favorecidoNome?.join("','")}')`; + } else if ((['Todos'].some((i) => args.consorcioNome?.includes(i)) && ['Todos'].some((i) => args.favorecidoNome?.includes(i))) || (args.consorcioNome !== undefined && args.favorecidoNome !== undefined)) { + conditions = + conditions + + ` and tv."nomeConsorcio" in ('STPC','STPL','VLT','Santa Cruz','Internorte','Intersul','Transcarioca','MobiRio','TEC') `; - }else if((['Todos'].some(i=>args.favorecidoNome?.includes(i))) && (args.consorcioNome!==undefined)){ - conditions = conditions +` and tv."nomeConsorcio" in('STPC','STPL',${args.consorcioNome?.join("','")}) `; - }else if((['Todos'].some(i=>args.favorecidoNome?.includes(i)))){ - conditions = conditions +` and tv."nomeConsorcio" in('STPC','STPL') `; - } + } else if (['Todos'].some((i) => args.favorecidoNome?.includes(i)) && args.consorcioNome !== undefined) { + conditions = conditions + ` and tv."nomeConsorcio" in('STPC','STPL',${args.consorcioNome?.join("','")}) `; + } else if (['Todos'].some((i) => args.favorecidoNome?.includes(i))) { + conditions = conditions + ` and tv."nomeConsorcio" in('STPC','STPL') `; + } - let footer = `) AS res + let footer = `) AS res LEFT JOIN subtotal_data sub ON res."consorcio" = sub."nomeConsorcio" CROSS JOIN total_data - ORDER BY res."consorcio", res."favorecido", res."datapagamento"` + ORDER BY res."consorcio", res."favorecido", res."datapagamento"`; - let result = ` select * from ( `+ query + body + conditions + footer +` ) as tt where (1=1)`; + let result = ` select * from ( ` + query + body + conditions + footer + ` ) as tt where (1=1)`; return result; } - public getQueryNaoApagar(args:IFindPublicacaoRelatorio){ - const dataInicio = args.dataInicio.toISOString().slice(0,10) - const dataFim = args.dataFim.toISOString().slice(0,10) + public getQueryNaoApagar(args: IFindPublicacaoRelatorio) { + const dataInicio = args.dataInicio.toISOString().slice(0, 10); + const dataFim = args.dataFim.toISOString().slice(0, 10); let query = ` select distinct res.*, `; - query = query + `(select sum(ss."valorLancamento")::float from `; - query = query + ` (select distinct dta.id,dta."valorLancamento" + query = query + `(select sum(ss."valorLancamento")::float from `; + query = + query + + ` (select distinct dta.id,dta."valorLancamento" from detalhe_a dta inner join item_transacao_agrupado tt on dta."itemTransacaoAgrupadoId"=tt.id inner join transacao_agrupado tta on tta."id"=tt."transacaoAgrupadoId" and tta."statusId"<>'5' left join item_transacao itt on itt."itemTransacaoAgrupadoId" = tt."id" left join arquivo_publicacao app on app."itemTransacaoId"=itt.id WHERE `; - if(dataInicio!==undefined && dataFim!==undefined && - (dataFim === dataInicio || new Date(dataFim)>new Date(dataInicio))) - query = query + ` dta."dataVencimento" between '${dataInicio}' and '${dataFim}'`; - if(args.emProcessamento!==undefined && args.emProcessamento===true ){ - query = query +` and app."isPago"=false and TRIM(dta."ocorrenciasCnab")='' ` - }else if(args.pago !==undefined){ - query = query +` and app."isPago"=${args.pago} and TRIM(dta."ocorrenciasCnab")<>'' `; - } - query = query + ` and tt."nomeConsorcio"=res.consorcio `; - query = query + ` )as ss) as subTotal, `; + if (dataInicio !== undefined && dataFim !== undefined && (dataFim === dataInicio || new Date(dataFim) > new Date(dataInicio))) query = query + ` dta."dataVencimento" between '${dataInicio}' and '${dataFim}'`; + if (args.emProcessamento !== undefined && args.emProcessamento === true) { + query = query + ` and app."isPago"=false and TRIM(dta."ocorrenciasCnab")='' `; + } else if (args.pago !== undefined) { + query = query + ` and app."isPago"=${args.pago} and TRIM(dta."ocorrenciasCnab")<>'' `; + } + query = query + ` and tt."nomeConsorcio"=res.consorcio `; + query = query + ` )as ss) as subTotal, `; - query = query + `(select sum(tt."valorLancamento")::float from + query = + query + + `(select sum(tt."valorLancamento")::float from (select distinct dta.id,dta."valorLancamento" from detalhe_a dta inner join item_transacao_agrupado tt on dta."itemTransacaoAgrupadoId"=tt.id @@ -169,34 +169,31 @@ export class RelatorioSinteticoRepository { left join item_transacao itt on itt."itemTransacaoAgrupadoId" = tt."id" left join arquivo_publicacao app on app."itemTransacaoId"=itt.id WHERE `; - if(dataInicio!==undefined && dataFim!==undefined && - (dataFim === dataInicio || new Date(dataFim)>new Date(dataInicio))) - query = query + ` dta."dataVencimento" between '${dataInicio}' and '${dataFim}'`; - if(args.emProcessamento!==undefined && args.emProcessamento===true ){ - query = query +` and app."isPago"=false and TRIM(dta."ocorrenciasCnab")='' ` - }else if(args.pago !==undefined){ - query = query +` and app."isPago"=${args.pago} and TRIM(dta."ocorrenciasCnab")<>'' `; - } + if (dataInicio !== undefined && dataFim !== undefined && (dataFim === dataInicio || new Date(dataFim) > new Date(dataInicio))) query = query + ` dta."dataVencimento" between '${dataInicio}' and '${dataFim}'`; + if (args.emProcessamento !== undefined && args.emProcessamento === true) { + query = query + ` and app."isPago"=false and TRIM(dta."ocorrenciasCnab")='' `; + } else if (args.pago !== undefined) { + query = query + ` and app."isPago"=${args.pago} and TRIM(dta."ocorrenciasCnab")<>'' `; + } - if((args.consorcioNome!==undefined) && !(['Todos'].some(i=>args.consorcioNome?.includes(i)))){ - query = query +` and tt."nomeConsorcio" in('${args.consorcioNome?.join("','")}')`; - }else - if((args.favorecidoNome!==undefined) && !(['Todos'].some(i=>args.favorecidoNome?.includes(i)))) - query = query +` and tt."nomeConsorcio" in(res.consorcio) `; - else if((['Todos'].some(i=>args.consorcioNome?.includes(i))) - && (['Todos'].some(i=>args.favorecidoNome?.includes(i))) || - ((args.consorcioNome!==undefined) && (args.favorecidoNome!==undefined))){ - query = query +` and tt."nomeConsorcio" in ('STPC','STPL','VLT','Santa Cruz', + if (args.consorcioNome !== undefined && !['Todos'].some((i) => args.consorcioNome?.includes(i))) { + query = query + ` and tt."nomeConsorcio" in('${args.consorcioNome?.join("','")}')`; + } else if (args.favorecidoNome !== undefined && !['Todos'].some((i) => args.favorecidoNome?.includes(i))) query = query + ` and tt."nomeConsorcio" in(res.consorcio) `; + else if ((['Todos'].some((i) => args.consorcioNome?.includes(i)) && ['Todos'].some((i) => args.favorecidoNome?.includes(i))) || (args.consorcioNome !== undefined && args.favorecidoNome !== undefined)) { + query = + query + + ` and tt."nomeConsorcio" in ('STPC','STPL','VLT','Santa Cruz', 'Internorte','Intersul','Transcarioca','MobiRio','TEC') `; - } - else if((['Todos'].some(i=>args.favorecidoNome?.includes(i)))){ - query = query +` and tt."nomeConsorcio" in('STPC','STPL','TEC') `; - } - query = query + ` )as tt )as total `; - + } else if (['Todos'].some((i) => args.favorecidoNome?.includes(i))) { + query = query + ` and tt."nomeConsorcio" in('STPC','STPL','TEC') `; + } + query = query + ` )as tt )as total `; + query = query + `from ( `; - query = query +` + query = + query + + ` select distinct it.id, case @@ -222,9 +219,11 @@ export class RelatorioSinteticoRepository { when (not (ap."isPago")) then 'naopago' else 'apagar' end AS status, case when (not (ap."isPago")) then oc."message" - else '' end As mensagem_status `; - - query = query + ` from item_transacao_agrupado ita + else '' end As mensagem_status `; + + query = + query + + ` from item_transacao_agrupado ita inner join detalhe_a da on da."itemTransacaoAgrupadoId"= ita.id inner join item_transacao it on ita.id = it."itemTransacaoAgrupadoId" inner join transacao_agrupado ta on ta."id"=ita."transacaoAgrupadoId" and ta."statusId"<>'5' @@ -232,39 +231,38 @@ export class RelatorioSinteticoRepository { inner join cliente_favorecido cf on cf.id=it."clienteFavorecidoId" left join ocorrencia oc on oc."detalheAId"=da.id where `; - if(dataInicio!==undefined && dataFim!==undefined && - (dataFim === dataInicio || new Date(dataFim)>new Date(dataInicio))) - query = query + ` da."dataVencimento" between '${dataInicio}' and '${dataFim}'`; + if (dataInicio !== undefined && dataFim !== undefined && (dataFim === dataInicio || new Date(dataFim) > new Date(dataInicio))) query = query + ` da."dataVencimento" between '${dataInicio}' and '${dataFim}'`; - if((args.consorcioNome!==undefined) && !(['Todos'].some(i=>args.consorcioNome?.includes(i)))){ - query = query +` and it."nomeConsorcio" in('${args.consorcioNome?.join("','")}')`; - }else if((args.favorecidoNome!==undefined) && !(['Todos'].some(i=>args.favorecidoNome?.includes(i)))){ - query = query +` and cf."nome" in('${args.favorecidoNome?.join("','")}')`; - }else if( (['Todos'].some(i=>args.consorcioNome?.includes(i))) && (['Todos'].some(i=>args.favorecidoNome?.includes(i))) - || ((args.consorcioNome!==undefined) && (args.favorecidoNome!==undefined))){ - query = query +` and it."nomeConsorcio" + if (args.consorcioNome !== undefined && !['Todos'].some((i) => args.consorcioNome?.includes(i))) { + query = query + ` and it."nomeConsorcio" in('${args.consorcioNome?.join("','")}')`; + } else if (args.favorecidoNome !== undefined && !['Todos'].some((i) => args.favorecidoNome?.includes(i))) { + query = query + ` and cf."nome" in('${args.favorecidoNome?.join("','")}')`; + } else if ((['Todos'].some((i) => args.consorcioNome?.includes(i)) && ['Todos'].some((i) => args.favorecidoNome?.includes(i))) || (args.consorcioNome !== undefined && args.favorecidoNome !== undefined)) { + query = + query + + ` and it."nomeConsorcio" in ('STPC','STPL','VLT','Santa Cruz','Internorte','Intersul','Transcarioca','MobiRio','TEC') `; - }else if((['Todos'].some(i=>args.favorecidoNome?.includes(i)))){ - query = query +` and it."nomeConsorcio" in('STPC','STPL','TEC') `; + } else if (['Todos'].some((i) => args.favorecidoNome?.includes(i))) { + query = query + ` and it."nomeConsorcio" in('STPC','STPL','TEC') `; } - if(args.emProcessamento!==undefined && args.emProcessamento===true){ - query = query +` and ap."isPago"=false and TRIM(da."ocorrenciasCnab")='' ` - }else if(args.pago !==undefined){ + if (args.emProcessamento !== undefined && args.emProcessamento === true) { + query = query + ` and ap."isPago"=false and TRIM(da."ocorrenciasCnab")='' `; + } else if (args.pago !== undefined) { query = query + ` and ap."isPago"=${args.pago} and TRIM(da."ocorrenciasCnab")<>'' `; - } - - if(args.valorMin!==undefined) - query = query +` and it."valor">=${args.valorMin}`; + } + + if (args.valorMin !== undefined) query = query + ` and it."valor">=${args.valorMin}`; + + if (args.valorMax !== undefined) query = query + ` and it."valor"<=${args.valorMax}`; + + query = + query + + ` ) as res + order by "consorcio", "favorecido","datapagamento" `; + + this.logger.debug(compactQuery(query)); - if(args.valorMax!==undefined) - query = query + ` and it."valor"<=${args.valorMax}`; - - query = query + ` ) as res - order by "consorcio", "favorecido","datapagamento" `; - - this.logger.debug(query); - return query; } -} \ No newline at end of file +} diff --git a/src/relatorio/relatorio.service.ts b/src/relatorio/relatorio.service.ts index 26fca219..b2bed4c0 100644 --- a/src/relatorio/relatorio.service.ts +++ b/src/relatorio/relatorio.service.ts @@ -9,92 +9,86 @@ import { RelatorioSinteticoRepository } from './relatorio-sintetico.repository'; @Injectable() export class RelatorioService { - constructor(private relatorioConsolidadoRepository: RelatorioConsolidadoRepository, - private relatorioSinteticoRepository: RelatorioSinteticoRepository, - private relatorioAnaliticoRepository: RelatorioAnaliticoRepository - ) {} + constructor(private relatorioConsolidadoRepository: RelatorioConsolidadoRepository, private relatorioSinteticoRepository: RelatorioSinteticoRepository, private relatorioAnaliticoRepository: RelatorioAnaliticoRepository) {} /** * Gerar relatórios consolidados - agrupados por Favorecido. */ - async findConsolidado(args: IFindPublicacaoRelatorio) { - let result: RelatorioConsolidadoResultDto[]=[]; - - if(args.dataInicio ===undefined || args.dataFim === undefined || - new Date(args.dataFim) < new Date(args.dataInicio)){ + async findConsolidado(args: IFindPublicacaoRelatorio) { + let result: RelatorioConsolidadoResultDto[] = []; + + if (args.dataInicio === undefined || args.dataFim === undefined || new Date(args.dataFim) < new Date(args.dataInicio)) { throw new Error('Parametro de data inválido'); } - if(args.pago === undefined && args.aPagar === undefined && (args.favorecidoNome ===undefined) - && (args.consorcioNome === undefined)){ + if (args.pago === undefined && args.aPagar === undefined && args.favorecidoNome === undefined && args.consorcioNome === undefined) { result.push(await this.resultConsolidado(args)); result.push(await this.resultPago(args)); result.push(await this.resultErros(args)); result.push(await this.resultApagar(args)); - }else if(args.pago === true && args.aPagar === true){ + } else if (args.pago === true && args.aPagar === true) { result.push(await this.resultPago(args)); result.push(await this.resultApagar(args)); - }else if(args.pago ===true) { + } else if (args.pago === true) { result.push(await this.resultPago(args)); - }else if(args.pago ===false) { + } else if (args.pago === false) { result.push(await this.resultErros(args)); - }else if(args.aPagar === true){ + } else if (args.aPagar === true) { result.push(await this.resultApagar(args)); - }else{ + } else { result.push(await this.resultConsolidado(args)); - } + } return result; - } + } - private async resultConsolidado(args: IFindPublicacaoRelatorio){ - return this.instanceDataConsolidado(args,'todos'); + private async resultConsolidado(args: IFindPublicacaoRelatorio) { + return this.instanceDataConsolidado(args, 'todos'); } - private async resultPago(args: IFindPublicacaoRelatorio){ - return this.instanceDataConsolidado(args,'pago'); + private async resultPago(args: IFindPublicacaoRelatorio) { + return this.instanceDataConsolidado(args, 'pago'); } - private async resultErros(args: IFindPublicacaoRelatorio){ - return this.instanceDataConsolidado(args,'erros'); + private async resultErros(args: IFindPublicacaoRelatorio) { + return this.instanceDataConsolidado(args, 'erros'); } - private async resultApagar(args: IFindPublicacaoRelatorio){ - return this.instanceDataConsolidado(args,'aPagar'); - } + private async resultApagar(args: IFindPublicacaoRelatorio) { + return this.instanceDataConsolidado(args, 'aPagar'); + } - private async instanceDataConsolidado(args: IFindPublicacaoRelatorio,status:string){ - const consolidado = await this.relatorioConsolidadoRepository.findConsolidado(args); + private async instanceDataConsolidado(args: IFindPublicacaoRelatorio, status: string) { + const consolidado = await this.relatorioConsolidadoRepository.findConsolidado(args); const consolidadosData = new RelatorioConsolidadoResultDto(); consolidadosData.count = consolidado.length; consolidadosData.data = consolidado; - consolidadosData.valor = +consolidado.reduce((s, i) => s + i.valor, 0).toFixed(2); - consolidadosData.status = status + consolidadosData.valor = +consolidado.reduce((s, i) => s + i.valor, 0).toFixed(2); + consolidadosData.status = status; return consolidadosData; } - + ///////SINTETICO ////// - async findSintetico(args: IFindPublicacaoRelatorio){ - if(args.dataInicio ===undefined || args.dataFim === undefined || - new Date(args.dataFim) < new Date(args.dataInicio)){ + async findSintetico(args: IFindPublicacaoRelatorio) { + if (args.dataInicio === undefined || args.dataFim === undefined || new Date(args.dataFim) < new Date(args.dataInicio)) { throw new Error('Parametro de data inválido'); } - let result: RelatorioSinteticoResultDto[]=[]; - result.push(await this.resultSintetico(args)); + let result: RelatorioSinteticoResultDto[] = []; + result.push(await this.resultSintetico(args)); return result; } - private async resultSintetico(args: IFindPublicacaoRelatorio){ - return this.instanceDataSintetico(args,'todos'); - } + private async resultSintetico(args: IFindPublicacaoRelatorio) { + return this.instanceDataSintetico(args, 'todos'); + } - private async instanceDataSintetico(args: IFindPublicacaoRelatorio,status:string){ - const sintetico = await this.relatorioSinteticoRepository.findSintetico(args); + private async instanceDataSintetico(args: IFindPublicacaoRelatorio, status: string) { + const sintetico = await this.relatorioSinteticoRepository.findSintetico(args); const sintenticosData = new RelatorioSinteticoResultDto(); sintenticosData.count = sintetico.length; sintenticosData.data = sintetico; - sintenticosData.valor = (sintetico!==undefined && sintetico[0]!==undefined)?sintetico[0].total:0; + sintenticosData.valor = sintetico !== undefined && sintetico[0] !== undefined ? sintetico[0].total : 0; sintenticosData.status = status; return sintenticosData; } @@ -102,58 +96,56 @@ export class RelatorioService { /////////////////////// ///////ANALITICO ////// - async findAnalitico(args: IFindPublicacaoRelatorio){ - if(args.dataInicio ===undefined || args.dataFim === undefined || - new Date(args.dataFim) < new Date(args.dataInicio)){ + async findAnalitico(args: IFindPublicacaoRelatorio) { + if (args.dataInicio === undefined || args.dataFim === undefined || new Date(args.dataFim) < new Date(args.dataInicio)) { throw new Error('Parametro de data inválido'); } - let result: RelatorioAnaliticoResultDto[]=[]; - - if(args.pago === undefined && args.aPagar === undefined && (args.favorecidoNome ===undefined) - && (args.consorcioNome === undefined)){ + let result: RelatorioAnaliticoResultDto[] = []; + + if (args.pago === undefined && args.aPagar === undefined && args.favorecidoNome === undefined && args.consorcioNome === undefined) { result.push(await this.resultAnalitico(args)); result.push(await this.resultPagoAnalitico(args)); result.push(await this.resultErrosAnalitico(args)); result.push(await this.resultApagarAnalitico(args)); - }else if(args.pago === true && args.aPagar === true){ + } else if (args.pago === true && args.aPagar === true) { result.push(await this.resultPagoAnalitico(args)); result.push(await this.resultApagarAnalitico(args)); - }else if(args.pago ===true) { + } else if (args.pago === true) { result.push(await this.resultPagoAnalitico(args)); - }else if(args.pago ===false) { + } else if (args.pago === false) { result.push(await this.resultErrosAnalitico(args)); - }else if(args.aPagar === true){ + } else if (args.aPagar === true) { result.push(await this.resultApagarAnalitico(args)); - }else{ + } else { result.push(await this.resultAnalitico(args)); } return result; } - private async resultAnalitico(args: IFindPublicacaoRelatorio){ - return this.instanceDataAnalitico(args,'todos'); + private async resultAnalitico(args: IFindPublicacaoRelatorio) { + return this.instanceDataAnalitico(args, 'todos'); } - private async resultPagoAnalitico(args: IFindPublicacaoRelatorio){ - return this.instanceDataAnalitico(args,'pago'); + private async resultPagoAnalitico(args: IFindPublicacaoRelatorio) { + return this.instanceDataAnalitico(args, 'pago'); } - private async resultErrosAnalitico(args: IFindPublicacaoRelatorio){ - return this.instanceDataAnalitico(args,'erros'); + private async resultErrosAnalitico(args: IFindPublicacaoRelatorio) { + return this.instanceDataAnalitico(args, 'erros'); } - private async resultApagarAnalitico(args: IFindPublicacaoRelatorio){ - return this.instanceDataAnalitico(args,'aPagar'); - } + private async resultApagarAnalitico(args: IFindPublicacaoRelatorio) { + return this.instanceDataAnalitico(args, 'aPagar'); + } - private async instanceDataAnalitico(args: IFindPublicacaoRelatorio,status:string){ - const analitico = await this.relatorioAnaliticoRepository.findAnalitico(args); + private async instanceDataAnalitico(args: IFindPublicacaoRelatorio, status: string) { + const analitico = await this.relatorioAnaliticoRepository.findAnalitico(args); const analiticosData = new RelatorioAnaliticoResultDto(); analiticosData.count = analitico.length; analiticosData.data = analitico; - analiticosData.valor = +analitico.reduce((s, i) => s + i.valorTransacao, 0).toFixed(2); - analiticosData.status = status + analiticosData.valor = +analitico.reduce((s, i) => s + i.valor, 0).toFixed(2); + analiticosData.status = status; return analiticosData; } }