diff --git a/src/cnab/cnab.service.ts b/src/cnab/cnab.service.ts index aee26e02..e1a66ae6 100644 --- a/src/cnab/cnab.service.ts +++ b/src/cnab/cnab.service.ts @@ -45,7 +45,7 @@ import { PagadorService } from './service/pagamento/pagador.service'; import { RemessaRetornoService } from './service/pagamento/remessa-retorno.service'; import { TransacaoAgrupadoService } from './service/pagamento/transacao-agrupado.service'; import { TransacaoService } from './service/pagamento/transacao.service'; -import { +import { parseCnab240Extrato, parseCnab240Pagamento, stringifyCnab104File, @@ -87,10 +87,10 @@ export class CnabService { private transacaoAgService: TransacaoAgrupadoService, private itemTransacaoAgService: ItemTransacaoAgrupadoService, private readonly lancamentoService: LancamentoService, - private arquivoPublicacaoService: ArquivoPublicacaoService, + private arquivoPublicacaoService: ArquivoPublicacaoService, private headerArquivoService: HeaderArquivoService, private headerLoteService: HeaderLoteService, - private detalheAService: DetalheAService, + private detalheAService: DetalheAService, ) {} // #region saveTransacoesJae @@ -104,39 +104,72 @@ export class CnabService { * * Requirement: **Salvar novas transações Jaé** - {@link https://github.com/RJ-SMTR/api-cct/issues/207#issuecomment-1984421700 #207, items 3} */ - public async saveTransacoesJae(dataOrdemIncial,dataOrdemFinal,daysBefore=0,consorcio:string) { - + public async saveTransacoesJae( + dataOrdemIncial, + dataOrdemFinal, + daysBefore = 0, + consorcio: string, + ) { // 1. Update cliente favorecido - await this.updateAllFavorecidosFromUsers(); + await this.updateAllFavorecidosFromUsers(); // 2. Update TransacaoView - await this.updateTransacaoViewBigquery(dataOrdemIncial,dataOrdemFinal,daysBefore,consorcio); + await this.updateTransacaoViewBigquery( + dataOrdemIncial, + dataOrdemFinal, + daysBefore, + consorcio, + ); // 3. Update ordens - const ordens = await this.bigqueryOrdemPagamentoService.getFromWeek(dataOrdemIncial,dataOrdemFinal,daysBefore); - await this.saveOrdens(ordens,consorcio); + const ordens = await this.bigqueryOrdemPagamentoService.getFromWeek( + dataOrdemIncial, + dataOrdemFinal, + daysBefore, + ); + await this.saveOrdens(ordens, consorcio); } /** * Atualiza a tabela TransacaoView */ - async updateTransacaoViewBigquery(dataOrdemIncial,dataOrdemFinal,daysBack=0,consorcio:string) { - const transacoesBq = await this.bigqueryTransacaoService.getFromWeek(dataOrdemIncial,dataOrdemFinal,daysBack) - - let trs = transacoesBq; - if(consorcio==='Van'){ - trs = transacoesBq.filter(tr => tr.modo===consorcio); - }else if(consorcio=='Empresa'){ - trs = transacoesBq.filter(tr => tr.modo!=='Van'); - }else if(consorcio!='Todos' && consorcio!='Empresa' && consorcio!='Todos'){ - trs = transacoesBq.filter(tr => tr.consorcio == consorcio); + async updateTransacaoViewBigquery( + dataOrdemIncial, + dataOrdemFinal, + daysBack = 0, + consorcio: string, + ) { + const transacoesBq = await this.bigqueryTransacaoService.getFromWeek( + dataOrdemIncial, + dataOrdemFinal, + daysBack, + ); + + let trs = transacoesBq; + if (consorcio === 'Van') { + trs = transacoesBq.filter((tr) => tr.modo === consorcio); + } else if (consorcio == 'Empresa') { + trs = transacoesBq.filter((tr) => tr.modo !== 'Van'); + } else if ( + consorcio != 'Todos' && + consorcio != 'Empresa' && + consorcio != 'Todos' + ) { + trs = transacoesBq.filter((tr) => tr.consorcio == consorcio); } - - forChunk(trs, 1000, async (chunk) => { - const transacoes = chunk.map((i) =>TransacaoView.fromBigqueryTransacao(i)); - for(const tr of transacoes) { - if((tr.modo == undefined || tr.modo == null) || tr.nomeOperadora == undefined || tr.nomeOperadora == null){ + + forChunk(trs, 1000, async (chunk) => { + const transacoes = chunk.map((i) => + TransacaoView.fromBigqueryTransacao(i), + ); + for (const tr of transacoes) { + if ( + tr.modo == undefined || + tr.modo == null || + tr.nomeOperadora == undefined || + tr.nomeOperadora == null + ) { continue; } - await this.transacaoViewService.save(tr); + await this.transacaoViewService.save(tr); } }); } @@ -145,8 +178,8 @@ export class CnabService { * Salvar Transacao / ItemTransacao e agrupados */ - async saveOrdens(ordens: BigqueryOrdemPagamentoDTO[],consorcio="Todos") { - const pagador = (await this.pagadorService.getAllPagador()).contaBilhetagem; + async saveOrdens(ordens: BigqueryOrdemPagamentoDTO[], consorcio = 'Todos') { + const pagador = (await this.pagadorService.getAllPagador()).contaBilhetagem; for (const ordem of ordens) { const cpfCnpj = ordem.consorcioCnpj || ordem.operadoraCpfCnpj; @@ -154,22 +187,23 @@ export class CnabService { continue; } const favorecido = await this.clienteFavorecidoService.findOne({ - where: { cpfCnpj: completeCPFCharacter(cpfCnpj,0) }}); + where: { cpfCnpj: completeCPFCharacter(cpfCnpj, 0) }, + }); - if (!favorecido) { + if (!favorecido) { continue; - } - - if (consorcio =='Todos' || consorcio == ordem.consorcio){ - await this.saveAgrupamentos(ordem, pagador, favorecido); - }else if(consorcio =='Van'){ - if(ordem.consorcio =='STPC' || ordem.consorcio == 'STPL'){ - await this.saveAgrupamentos(ordem, pagador, favorecido); - } - }else if(consorcio =='Empresa'){ - if(ordem.consorcio !='STPC' && ordem.consorcio != 'STPL'){ - await this.saveAgrupamentos(ordem, pagador, favorecido); - } + } + + if (consorcio == 'Todos' || consorcio == ordem.consorcio) { + await this.saveAgrupamentos(ordem, pagador, favorecido); + } else if (consorcio == 'Van') { + if (ordem.consorcio == 'STPC' || ordem.consorcio == 'STPL') { + await this.saveAgrupamentos(ordem, pagador, favorecido); + } + } else if (consorcio == 'Empresa') { + if (ordem.consorcio != 'STPC' && ordem.consorcio != 'STPL') { + await this.saveAgrupamentos(ordem, pagador, favorecido); + } } } } @@ -185,12 +219,18 @@ export class CnabService { async compareTransacaoViewPublicacao(daysBefore = 0) { const transacoesView = await this.getTransacoesViewWeek(daysBefore); - const publicacoes = this.getUniqueUpdatePublicacoes(await this.getPublicacoesWeek(daysBefore)); + const publicacoes = this.getUniqueUpdatePublicacoes( + await this.getPublicacoesWeek(daysBefore), + ); for (const publicacao of publicacoes) { const transacoes = transacoesView.filter( - (transacaoView) => transacaoView.idOperadora === publicacao.itemTransacao.idOperadora && + (transacaoView) => + transacaoView.idOperadora === publicacao.itemTransacao.idOperadora && transacaoView.idConsorcio === publicacao.itemTransacao.idConsorcio && - isSameDay(transacaoView.datetimeProcessamento, subDays(publicacao.itemTransacao.dataOrdem, 1)) + isSameDay( + transacaoView.datetimeProcessamento, + subDays(publicacao.itemTransacao.dataProcessamento, 1), + ), ); const toUpdate: DeepPartial[] = transacoes.map((i) => ({ ...i, @@ -204,13 +244,22 @@ export class CnabService { const unique: ArquivoPublicacao[] = []; publicacoes.forEach((publicacao) => { const existing = ArquivoPublicacao.filterUnique(unique, publicacao)[0] as - | ArquivoPublicacao | undefined; - const ocourences = ArquivoPublicacao.filterUnique(publicacoes,publicacao) - .sort((a, b) => b.itemTransacao.dataOrdem.getTime() - a.itemTransacao.dataOrdem.getTime()); + | ArquivoPublicacao + | undefined; + const ocourences = ArquivoPublicacao.filterUnique( + publicacoes, + publicacao, + ).sort( + (a, b) => + b.itemTransacao.dataOrdem.getTime() - + a.itemTransacao.dataOrdem.getTime(), + ); const paid = ocourences.filter((i) => i.isPago)[0] as - | ArquivoPublicacao | undefined; + | ArquivoPublicacao + | undefined; const noErrors = ocourences.filter((i) => !i.getIsError())[0] as - | ArquivoPublicacao | undefined; + | ArquivoPublicacao + | undefined; const recent = ocourences[0] as ArquivoPublicacao; if (!existing) { @@ -232,8 +281,8 @@ export class CnabService { const sex = startOfDay(subDays(friday, 7 + daysBefore)); const qui = endOfDay(subDays(friday, 1)); const result = await this.arqPublicacaoService.findMany({ - where: { itemTransacao: { dataOrdem: Between(sex, qui)} }, - order: { itemTransacao: { dataOrdem: 'ASC' } } + where: { itemTransacao: { dataOrdem: Between(sex, qui) } }, + order: { itemTransacao: { dataOrdem: 'ASC' } }, }); return result; } @@ -242,7 +291,7 @@ export class CnabService { * Salvar: * - TransacaoAgrupado (CNAB) * - ItemTransacaoAgrupado () - * - Transacao + * - Transacao */ async saveAgrupamentos( ordem: BigqueryOrdemPagamentoDTO, @@ -271,56 +320,96 @@ export class CnabService { // Cria ou atualiza itemTransacao (somar o valor a ser pago na sexta de pagamento) itemAg = await this.itemTransacaoAgService.findOne({ where: { - transacaoAgrupado: { id: transacaoAg.id, status: { id: TransacaoStatusEnum.created }}, - ...(ordem.consorcio === 'STPC' || ordem.consorcio === 'STPL' ? - { idOperadora: ordem.idOperadora }: { idConsorcio: ordem.idConsorcio }) + transacaoAgrupado: { + id: transacaoAg.id, + status: { id: TransacaoStatusEnum.created }, + }, + ...(ordem.consorcio === 'STPC' || ordem.consorcio === 'STPL' + ? { idOperadora: ordem.idOperadora } + : { idConsorcio: ordem.idConsorcio }), }, }); if (itemAg) { itemAg.valor += asNumber(ordem.valorTotalTransacaoLiquido); } else { - itemAg = this.convertItemTransacaoAgrupadoDTO(ordem, favorecido, transacaoAg); + itemAg = this.convertItemTransacaoAgrupadoDTO( + ordem, + favorecido, + transacaoAg, + ); } await this.itemTransacaoAgService.save(itemAg); - }else { // Se não existir, cria Transacao e Item - transacaoAg = await this.saveTransacaoAgrupado(ordem,pagador); - itemAg = await this.saveItemTransacaoAgrupado(ordem, favorecido, transacaoAg); + } else { + // Se não existir, cria Transacao e Item + transacaoAg = await this.saveTransacaoAgrupado(ordem, pagador); + itemAg = await this.saveItemTransacaoAgrupado( + ordem, + favorecido, + transacaoAg, + ); } const transacao = await this.saveTransacao(ordem, pagador, transacaoAg.id); - await this.saveItemTransacaoPublicacao(ordem, favorecido, transacao, itemAg ); + await this.saveItemTransacaoPublicacao( + ordem, + favorecido, + transacao, + itemAg, + ); } - private async saveTransacaoAgrupado(ordem: BigqueryOrdemPagamentoDTO,pagador:Pagador){ + private async saveTransacaoAgrupado( + ordem: BigqueryOrdemPagamentoDTO, + pagador: Pagador, + ) { const transacaoAg = this.convertTransacaoAgrupadoDTO(ordem, pagador); - return await this.transacaoAgService.save(transacaoAg); + return await this.transacaoAgService.save(transacaoAg); } - private async saveItemTransacaoAgrupado(ordem: BigqueryOrdemPagamentoDTO,favorecido:ClienteFavorecido,transacaoAg:TransacaoAgrupado){ - const itemAg = this.convertItemTransacaoAgrupadoDTO(ordem, favorecido,transacaoAg); + private async saveItemTransacaoAgrupado( + ordem: BigqueryOrdemPagamentoDTO, + favorecido: ClienteFavorecido, + transacaoAg: TransacaoAgrupado, + ) { + const itemAg = this.convertItemTransacaoAgrupadoDTO( + ordem, + favorecido, + transacaoAg, + ); return await this.itemTransacaoAgService.save(itemAg); - } + } /** * Save or update Transacao. * * Unique id: `idOrdemPagamento` */ - async saveTransacao(ordem: BigqueryOrdemPagamentoDTO,pagador: Pagador, transacaoAgId: number): Promise { - const transacao = this.convertTransacao(ordem,pagador,transacaoAgId); + async saveTransacao( + ordem: BigqueryOrdemPagamentoDTO, + pagador: Pagador, + transacaoAgId: number, + ): Promise { + const transacao = this.convertTransacao(ordem, pagador, transacaoAgId); return await this.transacaoService.save(transacao); } - private convertTransacao(ordem: BigqueryOrdemPagamentoDTO,pagador: Pagador,transacaoAgId: number){ + private convertTransacao( + ordem: BigqueryOrdemPagamentoDTO, + pagador: Pagador, + transacaoAgId: number, + ) { return new Transacao({ dataOrdem: ordem.dataOrdem, dataPagamento: ordem.dataPagamento, pagador: pagador, idOrdemPagamento: ordem.idOrdemPagamento, - transacaoAgrupado: { id: transacaoAgId } - }); + transacaoAgrupado: { id: transacaoAgId }, + }); } - convertTransacaoAgrupadoDTO(ordem: BigqueryOrdemPagamentoDTO, pagador: Pagador) { + convertTransacaoAgrupadoDTO( + ordem: BigqueryOrdemPagamentoDTO, + pagador: Pagador, + ) { const dataOrdem = yearMonthDayToDate(ordem.dataOrdem); /** semana de pagamento: sex-qui */ const fridayOrdem = nextFriday(startOfDay(dataOrdem)); @@ -334,11 +423,14 @@ export class CnabService { return transacao; } - convertItemTransacaoAgrupadoDTO(ordem: BigqueryOrdemPagamentoDTO, - favorecido: ClienteFavorecido,transacaoAg: TransacaoAgrupado) { + convertItemTransacaoAgrupadoDTO( + ordem: BigqueryOrdemPagamentoDTO, + favorecido: ClienteFavorecido, + transacaoAg: TransacaoAgrupado, + ) { const dataOrdem = yearMonthDayToDate(ordem.dataOrdem); const fridayOrdem = nextFriday(nextThursday(startOfDay(dataOrdem))); - const item = new ItemTransacaoAgrupado({ + const item = new ItemTransacaoAgrupado({ dataCaptura: ordem.dataOrdem, dataOrdem: fridayOrdem, idConsorcio: ordem.idConsorcio, @@ -353,32 +445,41 @@ export class CnabService { } async saveItemTransacaoPublicacao( - ordem: BigqueryOrdemPagamentoDTO, favorecido: ClienteFavorecido, transacao: Transacao, - itemTransacaoAg: ItemTransacaoAgrupado + ordem: BigqueryOrdemPagamentoDTO, + favorecido: ClienteFavorecido, + transacao: Transacao, + itemTransacaoAg: ItemTransacaoAgrupado, ) { - const item = this.convertItemTransacao(ordem,favorecido,transacao,itemTransacaoAg); - await this.itemTransacaoService.save(item); + const item = this.convertItemTransacao( + ordem, + favorecido, + transacao, + itemTransacaoAg, + ); + await this.itemTransacaoService.save(item); await this.arquivoPublicacaoService.save(item); } - private convertItemTransacao(ordem: BigqueryOrdemPagamentoDTO, + private convertItemTransacao( + ordem: BigqueryOrdemPagamentoDTO, favorecido: ClienteFavorecido, transacao: Transacao, - itemTransacaoAg: ItemTransacaoAgrupado){ - return new ItemTransacao({ - clienteFavorecido: favorecido, - dataCaptura: ordem.dataOrdem, - dataOrdem: startOfDay(new Date(ordem.dataOrdem)), - idConsorcio: ordem.idConsorcio, - idOperadora: ordem.idOperadora, - idOrdemPagamento: ordem.idOrdemPagamento, - nomeConsorcio: ordem.consorcio, - nomeOperadora: ordem.operadora, - valor: ordem.valorTotalTransacaoLiquido, - transacao: transacao, - itemTransacaoAgrupado: { id: itemTransacaoAg.id } - }); - } + itemTransacaoAg: ItemTransacaoAgrupado, + ) { + return new ItemTransacao({ + clienteFavorecido: favorecido, + dataCaptura: ordem.dataOrdem, + dataOrdem: startOfDay(new Date(ordem.dataOrdem)), + idConsorcio: ordem.idConsorcio, + idOperadora: ordem.idOperadora, + idOrdemPagamento: ordem.idOrdemPagamento, + nomeConsorcio: ordem.consorcio, + nomeOperadora: ordem.operadora, + valor: ordem.valorTotalTransacaoLiquido, + transacao: transacao, + itemTransacaoAgrupado: { id: itemTransacaoAg.id }, + }); + } async getTransacoesViewWeek(daysBefore = 0) { let friday = new Date(); @@ -387,7 +488,10 @@ export class CnabService { } const startDate = startOfDay(subDays(friday, 8 + daysBefore)); const endDate = endOfDay(subDays(friday, 2)); - return await this.transacaoViewService.find({datetimeProcessamento: Between(startDate,endDate)},false); + return await this.transacaoViewService.find( + { datetimeProcessamento: Between(startDate, endDate) }, + false, + ); } // #endregion @@ -406,18 +510,27 @@ export class CnabService { // 1. Update cliente favorecido await this.updateAllFavorecidosFromUsers(); // 2. Find new Lancamento from this week - const newLancamentos = await this.lancamentoService.findToPayWeek(); + const newLancamentos = await this.lancamentoService.findToPayWeek(); // 3. Save new Transacao / ItemTransacao const favorecidos = newLancamentos.map((i) => i.id_cliente_favorecido); const pagador = (await this.pagadorService.getAllPagador()).contaBilhetagem; // It will automatically update Lancamentos via OneToMany - const transacaoDTO = this.transacaoService.generateDTOForLancamento(pagador, newLancamentos); - const savedTransacao = await this.transacaoService.saveForLancamento(transacaoDTO); + const transacaoDTO = this.transacaoService.generateDTOForLancamento( + pagador, + newLancamentos, + ); + const savedTransacao = await this.transacaoService.saveForLancamento( + transacaoDTO, + ); const updatedLancamentos = savedTransacao.lancamentos as LancamentoEntity[]; // .findByLancamentos(savedTransacao.lancamentos as LancamentoEntity[]) - const itemTransacaoDTOs = this.itemTransacaoService.generateDTOsFromLancamentos(updatedLancamentos, favorecidos); - await this.itemTransacaoService.saveMany(itemTransacaoDTOs); + const itemTransacaoDTOs = + this.itemTransacaoService.generateDTOsFromLancamentos( + updatedLancamentos, + favorecidos, + ); + await this.itemTransacaoService.saveMany(itemTransacaoDTOs); } private async updateAllFavorecidosFromUsers() { @@ -436,95 +549,171 @@ export class CnabService { * * @throws `Error` if any subtask throws */ - public async generateRemessa(tipo: PagadorContaEnum, dataPgto: Date | undefined, isConference: boolean, - isCancelamento: boolean, nsaInicial: number, nsaFinal: number,dataCancelamento=new Date()):Promise{ - const METHOD = this.sendRemessa.name; - - const listCnab:string[] = []; - if(!isCancelamento){ - const transacoesAg = await this.transacaoAgService.findAllNewTransacao(tipo); + public async generateRemessa( + tipo: PagadorContaEnum, + dataPgto: Date | undefined, + isConference: boolean, + isCancelamento: boolean, + nsaInicial: number, + nsaFinal: number, + dataCancelamento = new Date(), + ): Promise { + const METHOD = this.sendRemessa.name; + + const listCnab: string[] = []; + if (!isCancelamento) { + const transacoesAg = await this.transacaoAgService.findAllNewTransacao( + tipo, + ); if (!transacoesAg.length) { - this.logger.log(`Não há transações novas para gerar remessa, nada a fazer...`,METHOD); + this.logger.log( + `Não há transações novas para gerar remessa, nada a fazer...`, + METHOD, + ); return []; } // Generate Remessas and send SFTP - for (const transacaoAg of transacoesAg){ - const headerArquivoDTO = await this.remessaRetornoService.saveHeaderArquivoDTO(transacaoAg,isConference); - const lotes = await this.remessaRetornoService.getLotes(transacaoAg.pagador,headerArquivoDTO,dataPgto,isConference); - const cnab104 = this.remessaRetornoService.generateFile(headerArquivoDTO,lotes); - if (headerArquivoDTO && cnab104){ - const [cnabStr, processedCnab104] = stringifyCnab104File(cnab104,true,'CnabPgtoRem'); - for (const processedLote of processedCnab104.lotes){ - const savedLote = lotes.filter(i => i.formaLancamento === processedLote.headerLote.formaLancamento.value)[0]; - await this.remessaRetornoService.updateHeaderLoteDTOFrom104(savedLote, processedLote.headerLote,isConference); - } - if(!isConference){ //conferencia não atualiza status remessa - await this.updateStatusRemessa(headerArquivoDTO,processedCnab104.headerArquivo,transacaoAg.id) + for (const transacaoAg of transacoesAg) { + const headerArquivoDTO = + await this.remessaRetornoService.saveHeaderArquivoDTO( + transacaoAg, + isConference, + ); + const lotes = await this.remessaRetornoService.getLotes( + transacaoAg.pagador, + headerArquivoDTO, + dataPgto, + isConference, + ); + const cnab104 = this.remessaRetornoService.generateFile( + headerArquivoDTO, + lotes, + ); + if (headerArquivoDTO && cnab104) { + const [cnabStr, processedCnab104] = stringifyCnab104File( + cnab104, + true, + 'CnabPgtoRem', + ); + for (const processedLote of processedCnab104.lotes) { + const savedLote = lotes.filter( + (i) => + i.formaLancamento === + processedLote.headerLote.formaLancamento.value, + )[0]; + await this.remessaRetornoService.updateHeaderLoteDTOFrom104( + savedLote, + processedLote.headerLote, + isConference, + ); } - if(cnabStr){ - listCnab.push(cnabStr); - } - } + if (!isConference) { + //conferencia não atualiza status remessa + await this.updateStatusRemessa( + headerArquivoDTO, + processedCnab104.headerArquivo, + transacaoAg.id, + ); + } + if (cnabStr) { + listCnab.push(cnabStr); + } + } } - }else{ //Se for cancelamento - if(this.validateCancel(nsaInicial,nsaFinal)){ + } else { + //Se for cancelamento + if (this.validateCancel(nsaInicial, nsaFinal)) { return []; } - - if(nsaFinal == undefined || nsaFinal == 0){ + + if (nsaFinal == undefined || nsaFinal == 0) { nsaFinal = nsaInicial; } - for (let index = nsaInicial; nsaInicial < nsaFinal+1; nsaInicial++) { + for (let index = nsaInicial; nsaInicial < nsaFinal + 1; nsaInicial++) { const headerArquivoDTO = await this.getHeaderArquivoCancelar(index); headerArquivoDTO.nsa = await this.headerArquivoService.getNextNSA(); - const lotes = await this.getLotesCancelar(index); + const lotes = await this.getLotesCancelar(index); const lotesDto: HeaderLoteDTO[] = []; let detalhes: CnabRegistros104Pgto[] = []; - for(const lote of lotes){ - const headerLoteDTO = this.headerLoteService.convertHeaderLoteDTO(headerArquivoDTO,lote.pagador,lote.formaLancamento == '41'? - Cnab104FormaLancamento.TED:Cnab104FormaLancamento.CreditoContaCorrente); - const detalhesA = (await (this.detalheAService.findMany({ headerLote:{ id: lote.id } }))) - .sort((a,b) => a.nsr - b.nsr); - for(const detalheA of detalhesA){ - const detalhe = await this.remessaRetornoService.saveDetalhes104(detalheA.numeroDocumentoEmpresa,headerLoteDTO, - detalheA.itemTransacaoAgrupado, detalheA.nsr, detalheA.dataVencimento,false,true,detalheA); - if(detalhe){ + for (const lote of lotes) { + const headerLoteDTO = this.headerLoteService.convertHeaderLoteDTO( + headerArquivoDTO, + lote.pagador, + lote.formaLancamento == '41' + ? Cnab104FormaLancamento.TED + : Cnab104FormaLancamento.CreditoContaCorrente, + ); + const detalhesA = ( + await this.detalheAService.findMany({ headerLote: { id: lote.id } }) + ).sort((a, b) => a.nsr - b.nsr); + for (const detalheA of detalhesA) { + const detalhe = await this.remessaRetornoService.saveDetalhes104( + detalheA.numeroDocumentoEmpresa, + headerLoteDTO, + detalheA.itemTransacaoAgrupado, + detalheA.nsr, + detalheA.dataVencimento, + false, + true, + detalheA, + ); + if (detalhe) { detalhes.push(detalhe); } } - headerLoteDTO.registros104 = detalhes; - lotesDto.push(headerLoteDTO); - detalhes = []; - } - const cnab104 = this.remessaRetornoService.generateFile(headerArquivoDTO,lotesDto,true,dataCancelamento); - if(headerArquivoDTO && cnab104){ - const [cnabStr, ] = stringifyCnab104File(cnab104,true,'CnabPgtoRem'); - if (!cnabStr) { + headerLoteDTO.registros104 = detalhes; + lotesDto.push(headerLoteDTO); + detalhes = []; + } + const cnab104 = this.remessaRetornoService.generateFile( + headerArquivoDTO, + lotesDto, + true, + dataCancelamento, + ); + if (headerArquivoDTO && cnab104) { + const [cnabStr] = stringifyCnab104File(cnab104, true, 'CnabPgtoRem'); + if (!cnabStr) { continue; - } + } listCnab.push(cnabStr); - } + } } } return listCnab; - } + } - private async updateStatusRemessa(headerArquivoDTO:HeaderArquivoDTO,cnabHeaderArquivo:CnabHeaderArquivo104,transacaoAgId:number){ - await this.remessaRetornoService.updateHeaderArquivoDTOFrom104(headerArquivoDTO,cnabHeaderArquivo); - await this.transacaoAgService.save({ id: transacaoAgId, status: new TransacaoStatus(TransacaoStatusEnum.remessa) }); + private async updateStatusRemessa( + headerArquivoDTO: HeaderArquivoDTO, + cnabHeaderArquivo: CnabHeaderArquivo104, + transacaoAgId: number, + ) { + await this.remessaRetornoService.updateHeaderArquivoDTOFrom104( + headerArquivoDTO, + cnabHeaderArquivo, + ); + await this.transacaoAgService.save({ + id: transacaoAgId, + status: new TransacaoStatus(TransacaoStatusEnum.remessa), + }); + } + + private validateCancel(nsaInicial: number, nsaFinal: number) { + return ( + (nsaInicial == undefined && nsaFinal == undefined) || + (nsaFinal != 0 && nsaFinal < nsaInicial) + ); } - private validateCancel(nsaInicial:number,nsaFinal:number){ - return (nsaInicial == undefined && nsaFinal ==undefined || (nsaFinal!=0 && nsaFinal < nsaInicial)); - } - private async getLotesCancelar(nsa: number) { - return (await (this.headerLoteService.findMany({ headerArquivo:{ nsa: nsa }}))).sort((a,b) => a.loteServico - b.loteServico); + return ( + await this.headerLoteService.findMany({ headerArquivo: { nsa: nsa } }) + ).sort((a, b) => a.loteServico - b.loteServico); } - + private async getHeaderArquivoCancelar(nsa: number) { - return await this.headerArquivoService.getHeaderArquivoNsa(nsa); + return await this.headerArquivoService.getHeaderArquivoNsa(nsa); } public async sendRemessa(listCnab: string[]) { @@ -554,17 +743,26 @@ export class CnabService { // Save Retorno, ArquivoPublicacao, move SFTP to backup try { - const retorno104 = parseCnab240Pagamento(cnabString); + const retorno104 = parseCnab240Pagamento(cnabString); await this.remessaRetornoService.saveRetorno(retorno104); - await this.sftpService.moveToBackup(cnabName,SftpBackupFolder.RetornoSuccess); + await this.sftpService.moveToBackup( + cnabName, + SftpBackupFolder.RetornoSuccess, + ); } catch (error) { this.logger.error( `Erro ao processar CNAB retorno, movendo para backup de erros e finalizando... - ${error}`, - error.stack, METHOD); - await this.sftpService.moveToBackup( cnabName,SftpBackupFolder.RetornoFailure); + error.stack, + METHOD, + ); + await this.sftpService.moveToBackup( + cnabName, + SftpBackupFolder.RetornoFailure, + ); return; } } + /** * This task will: * 1. Get extrato from SFTP @@ -592,8 +790,10 @@ export class CnabService { } catch (error) { this.logger.error( 'Erro ao processar CNAB extrato, movendo para backup de erros e finalizando...', - error,METHOD); - await this.sftpService.moveToBackup( + error, + METHOD, + ); + await this.sftpService.moveToBackup( cnab.name, SftpBackupFolder.RetornoFailure, ); diff --git a/src/cnab/entity/pagamento/ocorrencia.entity.ts b/src/cnab/entity/pagamento/ocorrencia.entity.ts index 4452cc57..60ea4b51 100644 --- a/src/cnab/entity/pagamento/ocorrencia.entity.ts +++ b/src/cnab/entity/pagamento/ocorrencia.entity.ts @@ -24,6 +24,7 @@ const userErrors = [ 'DB', 'ZA', 'ZY', + '02', ]; @Entity() diff --git a/src/cnab/service/pagamento/detalhe-a.service.ts b/src/cnab/service/pagamento/detalhe-a.service.ts index a0be7556..637ed90b 100644 --- a/src/cnab/service/pagamento/detalhe-a.service.ts +++ b/src/cnab/service/pagamento/detalhe-a.service.ts @@ -25,13 +25,13 @@ export class DetalheAService { private detalheARepository: DetalheARepository, private clienteFavorecidoService: ClienteFavorecidoService, private transacaoAgrupadoService: TransacaoAgrupadoService, - private pagamentosPendentesService: PagamentosPendentesService + private pagamentosPendentesService: PagamentosPendentesService, ) {} /** * Assumimos que todos os detalheA do retorno têm dataVencimento na mesma semana. */ - async updateDetalheAStatus(detalheA:DetalheA){ + async updateDetalheAStatus(detalheA: DetalheA) { // Update Transacao status const transacaoAgrupada = detalheA.itemTransacaoAgrupado.transacaoAgrupado; const status = new TransacaoStatus(); @@ -52,33 +52,43 @@ export class DetalheAService { return this.detalheARepository.saveManyIfNotExists(dtos); } - public async saveRetornoFrom104(headerArq: CnabHeaderArquivo104, headerLotePgto: CnabHeaderLote104Pgto, - r: CnabRegistros104Pgto, dataEfetivacao: Date): Promise{ - + public async saveRetornoFrom104( + headerArq: CnabHeaderArquivo104, + headerLotePgto: CnabHeaderLote104Pgto, + r: CnabRegistros104Pgto, + dataEfetivacao: Date, + ): Promise { const favorecido = await this.clienteFavorecidoService.findOne({ where: { nome: ILike(`%${r.detalheA.nomeTerceiro.stringValue.trim()}%`) }, }); - - if (!favorecido) { + + if (!favorecido) { return null; } const dataVencimento = startOfDay(r.detalheA.dataVencimento.convertedValue); let detalheARem; - try{ - detalheARem = await this.detalheARepository.getOne({ - dataVencimento: dataVencimento, - numeroDocumentoEmpresa: r.detalheA.numeroDocumentoEmpresa.convertedValue, - valorLancamento: r.detalheA.valorLancamento.convertedValue + try { + detalheARem = await this.detalheARepository.getOne({ + dataVencimento: dataVencimento, + numeroDocumentoEmpresa: + r.detalheA.numeroDocumentoEmpresa.convertedValue, + valorLancamento: r.detalheA.valorLancamento.convertedValue, }); - if((detalheARem.ocorrenciasCnab===undefined || detalheARem.ocorrenciasCnab==='') - || (detalheARem.ocorrenciasCnab !== r.detalheA.ocorrencias.value.trim() - && detalheARem.ocorrenciasCnab !== '00' && detalheARem.ocorrenciasCnab !== 'BD')){ + if ( + detalheARem.ocorrenciasCnab === undefined || + detalheARem.ocorrenciasCnab === '' || + (detalheARem.ocorrenciasCnab !== r.detalheA.ocorrencias.value.trim() && + detalheARem.ocorrenciasCnab !== '00' && + detalheARem.ocorrenciasCnab !== 'BD') + ) { const detalheA = new DetalheADTO({ id: detalheARem.id, - // headerLote: { id: detalheARem.headerLote.id }, - loteServico: Number(r.detalheA.loteServico.value), + // headerLote: { id: detalheARem.headerLote.id }, + loteServico: Number(r.detalheA.loteServico.value), finalidadeDOC: r.detalheA.finalidadeDOC.value, - numeroDocumentoEmpresa: Number(r.detalheA.numeroDocumentoEmpresa.value), + numeroDocumentoEmpresa: Number( + r.detalheA.numeroDocumentoEmpresa.value, + ), dataVencimento: startOfDay(r.detalheA.dataVencimento.convertedValue), dataEfetivacao: dataEfetivacao, tipoMoeda: r.detalheA.tipoMoeda.value, @@ -91,36 +101,50 @@ export class DetalheAService { indicadorBloqueio: r.detalheA.indicadorBloqueio.value, indicadorFormaParcelamento: r.detalheA.indicadorFormaParcelamento.stringValue, - periodoVencimento: startOfDay(r.detalheA.dataVencimento.convertedValue), + periodoVencimento: startOfDay( + r.detalheA.dataVencimento.convertedValue, + ), numeroParcela: r.detalheA.numeroParcela.convertedValue, valorRealEfetivado: r.detalheA.valorRealEfetivado.convertedValue, nsr: Number(r.detalheA.nsr.value), ocorrenciasCnab: - r.detalheA.ocorrencias.value.trim()|| + r.detalheA.ocorrencias.value.trim() || headerLotePgto.ocorrencias.value.trim() || - headerArq.ocorrenciaCobrancaSemPapel.value.trim() + headerArq.ocorrenciaCobrancaSemPapel.value.trim(), }); return await this.detalheARepository.save(detalheA); } - }catch (err) { - this.logger.error(err); - this.logger.debug(`Detalhe não encontrado para o favorecido: `,favorecido?.nome); + } catch (err) { + this.logger.error(err); + this.logger.debug( + `Detalhe não encontrado para o favorecido: `, + favorecido?.nome, + ); } - if(r.detalheA.ocorrencias!==undefined && r.detalheA.ocorrencias.value.trim()!=='' && - r.detalheA.ocorrencias.value.trim()!=='BD' && r.detalheA.ocorrencias.value.trim()!=='00'){ - - const pg = await this.pagamentosPendentesService.findOne({ numeroDocumento : r.detalheA.numeroDocumentoEmpresa.value.trim(), - valorLancamento: r.detalheA.valorLancamento.convertedValue, - nomeFavorecido : r.detalheA.nomeTerceiro.convertedValue - }) + if ( + r.detalheA.ocorrencias !== undefined && + r.detalheA.ocorrencias.value.trim() !== '' && + r.detalheA.ocorrencias.value.trim() !== 'BD' && + r.detalheA.ocorrencias.value.trim() !== '00' + ) { + const pg = await this.pagamentosPendentesService.findOne({ + numeroDocumento: r.detalheA.numeroDocumentoEmpresa.value.trim(), + valorLancamento: r.detalheA.valorLancamento.convertedValue, + nomeFavorecido: r.detalheA.nomeTerceiro.convertedValue, + }); - if(!pg){ - const pagamentosPendentes = new PagamentosPendentes(); - pagamentosPendentes.nomeFavorecido = r.detalheA.nomeTerceiro.stringValue.trim(); - pagamentosPendentes.dataVencimento = r.detalheA.dataVencimento.convertedValue; - pagamentosPendentes.valorLancamento = r.detalheA.valorLancamento.convertedValue; - pagamentosPendentes.numeroDocumento = r.detalheA.numeroDocumentoEmpresa.value.trim(); - pagamentosPendentes.ocorrenciaErro = r.detalheA.ocorrencias.value.trim(); + if (!pg) { + const pagamentosPendentes = new PagamentosPendentes(); + pagamentosPendentes.nomeFavorecido = + r.detalheA.nomeTerceiro.stringValue.trim(); + pagamentosPendentes.dataVencimento = + r.detalheA.dataVencimento.convertedValue; + pagamentosPendentes.valorLancamento = + r.detalheA.valorLancamento.convertedValue; + pagamentosPendentes.numeroDocumento = + r.detalheA.numeroDocumentoEmpresa.value.trim(); + pagamentosPendentes.ocorrenciaErro = + r.detalheA.ocorrencias.value.trim(); await this.pagamentosPendentesService.save(pagamentosPendentes); } } diff --git a/src/cnab/service/pagamento/remessa-retorno.service.ts b/src/cnab/service/pagamento/remessa-retorno.service.ts index 1fb6a53f..04efb616 100644 --- a/src/cnab/service/pagamento/remessa-retorno.service.ts +++ b/src/cnab/service/pagamento/remessa-retorno.service.ts @@ -48,30 +48,43 @@ export class RemessaRetornoService { timestamp: true, }); - constructor( + constructor( private arqPublicacaoService: ArquivoPublicacaoService, private itemTransacaoService: ItemTransacaoService, private itemTransacaoAgService: ItemTransacaoAgrupadoService, private headerArquivoService: HeaderArquivoService, - private headerLoteService: HeaderLoteService, + private headerLoteService: HeaderLoteService, private detalheAService: DetalheAService, private detalheBService: DetalheBService, private headerArquivoConfService: HeaderArquivoConfService, - private headerLoteConfService: HeaderLoteConfService, + private headerLoteConfService: HeaderLoteConfService, private detalheAConfService: DetalheAConfService, - private detalheBConfService: DetalheBConfService + private detalheBConfService: DetalheBConfService, ) {} - public async saveHeaderArquivoDTO(transacaoAg: TransacaoAgrupado,isConference): Promise{ + public async saveHeaderArquivoDTO( + transacaoAg: TransacaoAgrupado, + isConference, + ): Promise { let headerArquivoDTO; - if(!isConference){ - headerArquivoDTO = await this.headerArquivoService.getDTO(HeaderArquivoTipoArquivo.Remessa,transacaoAg); - const headerArquivo = await this.headerArquivoService.save(headerArquivoDTO); - headerArquivoDTO.id = headerArquivo.id; - }else{ - headerArquivoDTO = await this.headerArquivoConfService.getDTO(HeaderArquivoTipoArquivo.Remessa,transacaoAg); - const headerArquivo = await this.headerArquivoConfService.save(headerArquivoDTO); - headerArquivoDTO.id = headerArquivo.id; + if (!isConference) { + headerArquivoDTO = await this.headerArquivoService.getDTO( + HeaderArquivoTipoArquivo.Remessa, + transacaoAg, + ); + const headerArquivo = await this.headerArquivoService.save( + headerArquivoDTO, + ); + headerArquivoDTO.id = headerArquivo.id; + } else { + headerArquivoDTO = await this.headerArquivoConfService.getDTO( + HeaderArquivoTipoArquivo.Remessa, + transacaoAg, + ); + const headerArquivo = await this.headerArquivoConfService.save( + headerArquivoDTO, + ); + headerArquivoDTO.id = headerArquivo.id; } return headerArquivoDTO; } @@ -89,8 +102,11 @@ export class RemessaRetornoService { * (se banco do favorecido = banco do pagador - pagador é sempre Caixa) * - senão, TED (41) */ - public async getLotes(pagador: Pagador,headerArquivoDTO: HeaderArquivoDTO, - dataPgto: Date | undefined,isConference:boolean + public async getLotes( + pagador: Pagador, + headerArquivoDTO: HeaderArquivoDTO, + dataPgto: Date | undefined, + isConference: boolean, ) { const transacaoAg = headerArquivoDTO.transacaoAgrupado as TransacaoAgrupado; const itemTransacaoAgs = @@ -100,48 +116,80 @@ export class RemessaRetornoService { /** Agrupa por: formaLancamento */ const lotes: HeaderLoteDTO[] = []; let nsrTed = 0; - let nsrCC = 0; + let nsrCC = 0; let loteTed; let loteCC; - for (const itemTransacaoAgrupado of itemTransacaoAgs) { - const itemTransacao = await this.itemTransacaoService.findOne({ where: { - itemTransacaoAgrupado: { id: itemTransacaoAgrupado.id}} + for (const itemTransacaoAgrupado of itemTransacaoAgs) { + const itemTransacao = await this.itemTransacaoService.findOne({ + where: { + itemTransacaoAgrupado: { id: itemTransacaoAgrupado.id }, + }, }); - if(itemTransacao){ - if(itemTransacao.clienteFavorecido.codigoBanco !== '104'){ //TED - nsrTed ++; - if(loteTed == undefined){ - if(!isConference){ - loteTed = this.headerLoteService.convertHeaderLoteDTO(headerArquivoDTO,pagador,Cnab104FormaLancamento.TED) - loteTed = await this.headerLoteService.saveDto(loteTed); - }else{ - loteTed = this.headerLoteConfService.convertHeaderLoteDTO(headerArquivoDTO,pagador,Cnab104FormaLancamento.TED) - loteTed = await this.headerLoteConfService.saveDto(loteTed); + if (itemTransacao) { + if (itemTransacao.clienteFavorecido.codigoBanco !== '104') { + //TED + nsrTed++; + if (loteTed == undefined) { + if (!isConference) { + loteTed = this.headerLoteService.convertHeaderLoteDTO( + headerArquivoDTO, + pagador, + Cnab104FormaLancamento.TED, + ); + loteTed = await this.headerLoteService.saveDto(loteTed); + } else { + loteTed = this.headerLoteConfService.convertHeaderLoteDTO( + headerArquivoDTO, + pagador, + Cnab104FormaLancamento.TED, + ); + loteTed = await this.headerLoteConfService.saveDto(loteTed); } - } - const detalhes104 = await this.saveListDetalhes(loteTed,[itemTransacaoAgrupado],nsrTed,dataPgto,isConference); - nsrTed ++; + } + const detalhes104 = await this.saveListDetalhes( + loteTed, + [itemTransacaoAgrupado], + nsrTed, + dataPgto, + isConference, + ); + nsrTed++; loteTed.registros104.push(...detalhes104); - }else{ //Credito em Conta - nsrCC++; - if(loteCC == undefined){ - if(!isConference){ - loteCC = this.headerLoteService.convertHeaderLoteDTO(headerArquivoDTO,pagador,Cnab104FormaLancamento.CreditoContaCorrente) - loteCC = await this.headerLoteService.saveDto(loteCC); - }else{ - loteCC = this.headerLoteConfService.convertHeaderLoteDTO(headerArquivoDTO,pagador,Cnab104FormaLancamento.CreditoContaCorrente) - loteCC = await this.headerLoteConfService.saveDto(loteCC); + } else { + //Credito em Conta + nsrCC++; + if (loteCC == undefined) { + if (!isConference) { + loteCC = this.headerLoteService.convertHeaderLoteDTO( + headerArquivoDTO, + pagador, + Cnab104FormaLancamento.CreditoContaCorrente, + ); + loteCC = await this.headerLoteService.saveDto(loteCC); + } else { + loteCC = this.headerLoteConfService.convertHeaderLoteDTO( + headerArquivoDTO, + pagador, + Cnab104FormaLancamento.CreditoContaCorrente, + ); + loteCC = await this.headerLoteConfService.saveDto(loteCC); } - } - const detalhes104 = await this.saveListDetalhes(loteCC,[itemTransacaoAgrupado],nsrCC,dataPgto,isConference); + } + const detalhes104 = await this.saveListDetalhes( + loteCC, + [itemTransacaoAgrupado], + nsrCC, + dataPgto, + isConference, + ); nsrCC++; - loteCC.registros104.push(...detalhes104); - } + loteCC.registros104.push(...detalhes104); + } } - if(loteTed != undefined){ + if (loteTed != undefined) { lotes.push(loteTed); } - if(loteCC != undefined){ + if (loteCC != undefined) { lotes.push(loteCC); } } @@ -152,17 +200,17 @@ export class RemessaRetornoService { detalheA: CnabDetalheA_104, headerLoteId: number, itemTransacaoAg: ItemTransacaoAgrupado, - isConference:boolean + isConference: boolean, ) { let existing; - if(!isConference){ + if (!isConference) { existing = await this.detalheAService.findOne({ where: { nsr: Number(detalheA.nsr.value), itemTransacaoAgrupado: { id: itemTransacaoAg?.id }, }, }); - }else{ + } else { existing = await this.detalheAConfService.findOne({ where: { nsr: Number(detalheA.nsr.value), @@ -195,7 +243,7 @@ export class RemessaRetornoService { numeroParcela: getCnabFieldConverted(detalheA.numeroParcela), dataEfetivacao: getCnabFieldConverted(detalheA.dataEfetivacao), headerLote: { id: headerLoteId }, - itemTransacaoAgrupado: itemTransacaoAg + itemTransacaoAgrupado: itemTransacaoAg, }); } @@ -219,10 +267,21 @@ export class RemessaRetornoService { /** * Montar Cnab104 a partir dos DTOs de tabelas */ - public generateFile(headerArquivo: HeaderArquivoDTO, headerLoteDTOs: HeaderLoteDTO[],isCancelamento = false,dataCancelamento=new Date()) { + public generateFile( + headerArquivo: HeaderArquivoDTO, + headerLoteDTOs: HeaderLoteDTO[], + isCancelamento = false, + dataCancelamento = new Date(), + ) { const headerArquivo104 = this.getHeaderArquivo104FromDTO(headerArquivo); const trailerArquivo104 = sc(PgtoRegistros.trailerArquivo); - return this.getCnabFilePgto(headerArquivo104,headerLoteDTOs,trailerArquivo104,isCancelamento,dataCancelamento); + return this.getCnabFilePgto( + headerArquivo104, + headerLoteDTOs, + trailerArquivo104, + isCancelamento, + dataCancelamento, + ); } /** @@ -234,15 +293,29 @@ export class RemessaRetornoService { * * @returns Detalhes104 gerados a partir dos ItemTransacaoAg */ - async saveListDetalhes(headerLoteDto: HeaderLoteDTO, itemTransacoes: ItemTransacaoAgrupado[], - nsr: number, dataPgto: Date | undefined, isConference: boolean): Promise { - let numeroDocumento = await this.detalheAService.getNextNumeroDocumento(new Date()); + async saveListDetalhes( + headerLoteDto: HeaderLoteDTO, + itemTransacoes: ItemTransacaoAgrupado[], + nsr: number, + dataPgto: Date | undefined, + isConference: boolean, + ): Promise { + let numeroDocumento = await this.detalheAService.getNextNumeroDocumento( + new Date(), + ); // Para cada itemTransacao, cria detalhe const detalhes: CnabRegistros104Pgto[] = []; - let itemTransacaoAgAux: ItemTransacaoAgrupado | undefined; - for (const itemTransacao of itemTransacoes) { - itemTransacaoAgAux = itemTransacao as ItemTransacaoAgrupado; - const detalhe = await this.saveDetalhes104(numeroDocumento,headerLoteDto, itemTransacaoAgAux,nsr,dataPgto,isConference); + let itemTransacaoAgAux: ItemTransacaoAgrupado | undefined; + for (const itemTransacao of itemTransacoes) { + itemTransacaoAgAux = itemTransacao as ItemTransacaoAgrupado; + const detalhe = await this.saveDetalhes104( + numeroDocumento, + headerLoteDto, + itemTransacaoAgAux, + nsr, + dataPgto, + isConference, + ); if (detalhe) { detalhes.push(detalhe); } @@ -250,13 +323,13 @@ export class RemessaRetornoService { } return detalhes; } - + getCnabFilePgto( headerArquivo104: CnabHeaderArquivo104, headerLoteDTOs: HeaderLoteDTO[], trailerArquivo104: CnabTrailerArquivo104, - isCancelamento:boolean, - dataCancelamento = new Date() + isCancelamento: boolean, + dataCancelamento = new Date(), ) { const cnab104: CnabFile104Pgto = { headerArquivo: headerArquivo104, @@ -271,20 +344,20 @@ export class RemessaRetornoService { if (!cnab104.lotes.length) { return null; } - if(isCancelamento){ + if (isCancelamento) { cnab104.lotes.forEach((l) => { - l.registros.forEach( r =>{ - r.detalheA.tipoMovimento.value = Cnab104TipoMovimento.Exclusao; - r.detalheA.dataVencimento.value = dataCancelamento - r.detalheB.dataVencimento.value = dataCancelamento - }) + l.registros.forEach((r) => { + r.detalheA.tipoMovimento.value = Cnab104TipoMovimento.Exclusao; + r.detalheA.dataVencimento.value = dataCancelamento; + r.detalheB.dataVencimento.value = dataCancelamento; + }); }); } return cnab104; } private getHeaderArquivo104FromDTO( - headerArquivoDTO: HeaderArquivoDTO + headerArquivoDTO: HeaderArquivoDTO, ): CnabHeaderArquivo104 { const headerArquivo104: CnabHeaderArquivo104 = sc( PgtoRegistros.headerArquivo, @@ -319,15 +392,15 @@ export class RemessaRetornoService { public async updateHeaderLoteDTOFrom104( headerLoteDTO: HeaderLoteDTO, headerLote104: CnabHeaderLote104Pgto, - isConference: boolean - ) { + isConference: boolean, + ) { headerLoteDTO.loteServico = Number(headerLote104.loteServico.value); - if(!isConference){ + if (!isConference) { await this.headerLoteService.save(headerLoteDTO); - }else{ + } else { await this.headerLoteConfService.save(headerLoteDTO); } - } + } private getHeaderLoteFrom104( headerLoteDTO: HeaderLoteDTO, @@ -369,39 +442,53 @@ export class RemessaRetornoService { * * @param numeroDocumento Managed by company. It must be a new number. * @returns null if failed ItemTransacao to CNAB */ - public async saveDetalhes104(numeroDocumento: number, headerLote: HeaderLoteDTO, - itemTransacaoAg: ItemTransacaoAgrupado, nsr: number,dataPgto: Date | undefined, - isConference: boolean,isCancelamento=false,detalheAC = new DetalheA): Promise { + public async saveDetalhes104( + numeroDocumento: number, + headerLote: HeaderLoteDTO, + itemTransacaoAg: ItemTransacaoAgrupado, + nsr: number, + dataPgto: Date | undefined, + isConference: boolean, + isCancelamento = false, + detalheAC = new DetalheA(), + ): Promise { const METHOD = 'getDetalhes104()'; let favorecido; - if(itemTransacaoAg != undefined){ - const itemTransacao = await this.itemTransacaoService.findOne({ - where:{ itemTransacaoAgrupado: { id: itemTransacaoAg.id } } }); + if (itemTransacaoAg != undefined) { + const itemTransacao = await this.itemTransacaoService.findOne({ + where: { itemTransacaoAgrupado: { id: itemTransacaoAg.id } }, + }); favorecido = itemTransacao?.clienteFavorecido; - }else{ - const itemTransacaoAg = detalheAC.headerLote.headerArquivo.transacaoAgrupado?.itemTransacoesAgrupado[0]; - const itemTransacao = await this.itemTransacaoService.findOne({where: { - itemTransacaoAgrupado: { id: itemTransacaoAg?.id } - }}); + } else { + const itemTransacaoAg = + detalheAC.headerLote.headerArquivo.transacaoAgrupado + ?.itemTransacoesAgrupado[0]; + const itemTransacao = await this.itemTransacaoService.findOne({ + where: { + itemTransacaoAgrupado: { id: itemTransacaoAg?.id }, + }, + }); favorecido = itemTransacao?.clienteFavorecido; } // Failure if no favorecido if (!favorecido && !isCancelamento) { - await this.itemTransacaoService.save({id: itemTransacaoAg.id }); - this.logger.debug(`Falha ao usar ItemTransacao: favorecido ausente.`, - METHOD); + await this.itemTransacaoService.save({ id: itemTransacaoAg.id }); + this.logger.debug( + `Falha ao usar ItemTransacao: favorecido ausente.`, + METHOD, + ); return null; } - if(dataPgto){ - if(dataPgto < new Date()){ + if (dataPgto) { + if (dataPgto < new Date()) { dataPgto = new Date(); } } - // Save detalheA + // Save detalheA const detalheA: CnabDetalheA_104 = sc(PgtoRegistros.detalheA); detalheA.codigoBancoDestino.value = favorecido.codigoBanco; detalheA.codigoAgenciaDestino.value = favorecido.agencia; @@ -409,33 +496,33 @@ export class RemessaRetornoService { detalheA.contaCorrenteDestino.value = favorecido.contaCorrente; detalheA.dvContaDestino.value = favorecido.dvContaCorrente; detalheA.nomeTerceiro.value = favorecido.nome; - detalheA.numeroDocumentoEmpresa.value = numeroDocumento; + detalheA.numeroDocumentoEmpresa.value = numeroDocumento; const fridayOrdem = itemTransacaoAg.dataOrdem; detalheA.dataVencimento.value = fridayOrdem; - if(dataPgto === undefined){ + if (dataPgto === undefined) { detalheA.dataVencimento.value = detalheA.dataVencimento.value; - }else{ + } else { detalheA.dataVencimento.value = dataPgto; - } + } - if(!isCancelamento){ - detalheA.valorLancamento.value = itemTransacaoAg.valor; - }else{ - detalheA.valorLancamento.value = detalheAC.valorLancamento; + if (!isCancelamento) { + detalheA.valorLancamento.value = itemTransacaoAg.valor; + } else { + detalheA.valorLancamento.value = detalheAC.valorLancamento; } - - detalheA.nsr.value = nsr; - - // DetalheB + + detalheA.nsr.value = nsr; + + // DetalheB const detalheB: CnabDetalheB_104 = sc(PgtoRegistros.detalheB); detalheB.tipoInscricao.value = getTipoInscricao( asString(favorecido.cpfCnpj), ); detalheB.numeroInscricao.value = asString(favorecido.cpfCnpj); - if(dataPgto == undefined){ + if (dataPgto == undefined) { detalheB.dataVencimento.value = detalheA.dataVencimento.value; - }else{ + } else { detalheB.dataVencimento.value = dataPgto; } // Favorecido address @@ -449,9 +536,14 @@ export class RemessaRetornoService { detalheB.siglaEstado.value = favorecido.uf; detalheB.nsr.value = nsr + 1; - if(!isCancelamento){ - const savedDetalheA = await this.saveDetalheA(detalheA, asNumber(headerLote.id),itemTransacaoAg,isConference); - await this.saveDetalheB(detalheB,savedDetalheA.id,isConference); + if (!isCancelamento) { + const savedDetalheA = await this.saveDetalheA( + detalheA, + asNumber(headerLote.id), + itemTransacaoAg, + isConference, + ); + await this.saveDetalheB(detalheB, savedDetalheA.id, isConference); } return { @@ -461,53 +553,70 @@ export class RemessaRetornoService { } async saveDetalheA( -detalheA104: CnabDetalheA_104, savedHeaderLoteId: number, itemTransacaoAg: ItemTransacaoAgrupado, isConference: boolean, + detalheA104: CnabDetalheA_104, + savedHeaderLoteId: number, + itemTransacaoAg: ItemTransacaoAgrupado, + isConference: boolean, ) { const detalheADTO = await this.convertCnabDetalheAToDTO( detalheA104, savedHeaderLoteId, itemTransacaoAg, - isConference + isConference, ); - if(!isConference){ + if (!isConference) { const saved = await this.detalheAService.save(detalheADTO); return await this.detalheAService.getOne({ id: saved.id }); - }else{ + } else { const saved = await this.detalheAConfService.save(detalheADTO); return await this.detalheAConfService.getOne({ id: saved.id }); } } - async saveDetalheB(detalheB104: CnabDetalheB_104, savedDetalheAId: number, isConference: boolean) { + async saveDetalheB( + detalheB104: CnabDetalheB_104, + savedDetalheAId: number, + isConference: boolean, + ) { const detalheBDTO = await this.convertCnabDetalheBToDTO( detalheB104, savedDetalheAId, ); - if(!isConference){ + if (!isConference) { await this.detalheBService.save(detalheBDTO); - }else{ + } else { await this.detalheBConfService.save(detalheBDTO); } } public async saveRetorno(cnab: CnabFile104Pgto) { const dataEfetivacao = new Date(); - let detalheAUpdated; + let detalheAUpdated: DetalheA | null = null; for (const cnabLote of cnab.lotes) { - for (const registro of cnabLote.registros){ - this. logger.debug(`Header Arquivo NSA: `+cnab.headerArquivo.nsa.value); - this.logger.debug(`Header lote : `+cnabLote.headerLote.codigoRegistro.value) + for (const registro of cnabLote.registros) { + this.logger.debug( + `Header Arquivo NSA: ` + cnab.headerArquivo.nsa.value, + ); + this.logger.debug( + `Header lote : ` + cnabLote.headerLote.codigoRegistro.value, + ); // Save Detalhes detalheAUpdated = await this.detalheAService.saveRetornoFrom104( - cnab.headerArquivo, cnabLote.headerLote,registro, dataEfetivacao); + cnab.headerArquivo, + cnabLote.headerLote, + registro, + dataEfetivacao, + ); if (!detalheAUpdated) { continue; } - this.logger.debug(`Detalhe A : `+ detalheAUpdated.id); - await this.detalheBService.saveFrom104(registro, detalheAUpdated); - await this.arqPublicacaoService.compareRemessaToRetorno(detalheAUpdated); - await this.detalheAService.updateDetalheAStatus(detalheAUpdated); + this.logger.debug(`Detalhe A : ` + detalheAUpdated.id); + await this.detalheBService.saveFrom104(registro, detalheAUpdated); + await this.arqPublicacaoService.compareRemessaToRetorno( + detalheAUpdated, + ); + await this.detalheAService.updateDetalheAStatus(detalheAUpdated); } } } -} \ No newline at end of file +} diff --git a/src/transacao-bq/transacao-view.entity.ts b/src/transacao-bq/transacao-view.entity.ts index 6fb9b217..b42487fc 100644 --- a/src/transacao-bq/transacao-view.entity.ts +++ b/src/transacao-bq/transacao-view.entity.ts @@ -83,7 +83,7 @@ export class TransacaoView { @Column({ type: String, nullable: true }) consorcioCnpj: string | null; - @ManyToOne(() => ArquivoPublicacao, { eager: false, nullable: true }) + @ManyToOne(() => ArquivoPublicacao, { eager: true, nullable: true }) @JoinColumn({ foreignKeyConstraintName: 'FK_TransacaoView_arquivoPublicacao_ManyToOne', })