diff --git a/src/cnab/dto/arquivo-publicacao.dto.ts b/src/cnab/dto/arquivo-publicacao.dto.ts new file mode 100644 index 00000000..62bcf9b3 --- /dev/null +++ b/src/cnab/dto/arquivo-publicacao.dto.ts @@ -0,0 +1,37 @@ +export class ArquivoPublicacaoDTO { + constructor(dto?: ArquivoPublicacaoDTO) { + if (dto) { + Object.assign(this, dto); + } + } + id_arquivo_publicacao: number; + + id_header_arquivo: number; + id_transacao: number; + id_header_lote: number; + dt_geracao_remessa: Date; + hr_geracao_remessa: Date; + dt_geracao_retorno: Date; + hr_geracao_retorno: Date; + + lote_servico: string; + nome_pagador: string; + agencia_pagador: string; + dv_agencia_pagador: string; + conta_pagador: string; + dv_conta_pagador: string; + + nome_cliente?: string; + cpf_cnpj_cliente?: string; + cod_banco_cliente?: string; + agencia_cliente?: string; + dv_agencia_cliente?: string; + conta_corrente_cliente?: string; + dv_conta_corrente_cliente?: string; + + dt_vencimento?: Date; + valor_lancamento?: number; + data_efetivacao?: Date; + valor_real_efetivado?: number; + ocorrencias: string; + } \ No newline at end of file diff --git a/src/cnab/dto/detalhe-a.dto.ts b/src/cnab/dto/detalhe-a.dto.ts index 0efb94f9..2adfbd44 100644 --- a/src/cnab/dto/detalhe-a.dto.ts +++ b/src/cnab/dto/detalhe-a.dto.ts @@ -70,4 +70,7 @@ export class DetalheADTO { @ValidateIf(isCreate) @IsNotEmpty() valor_real_efetivado?: number; + + ocorrencias?: string; + } diff --git a/src/cnab/entity/arquivo-publicacao.entity.ts b/src/cnab/entity/arquivo-publicacao.entity.ts new file mode 100644 index 00000000..9b6104d4 --- /dev/null +++ b/src/cnab/entity/arquivo-publicacao.entity.ts @@ -0,0 +1,90 @@ +import { EntityHelper } from "src/utils/entity-helper"; +import { Column, DeepPartial, PrimaryGeneratedColumn } from "typeorm"; + +export class ArquivoPublicacao extends EntityHelper { + constructor( + arquivoPublicacao?: ArquivoPublicacao | DeepPartial, + ) { + super(); + if (arquivoPublicacao !== undefined) { + Object.assign(this, arquivoPublicacao); + } + } + @PrimaryGeneratedColumn() + id_publicacao:number; + + @Column({ type: String, unique: false, nullable: false}) + id_header_arquivo: number; + + @Column({ type: String, unique: false, nullable: false}) + id_transacao: number; + + @Column({ type: String, unique: false, nullable: false}) + id_header_lote: number; + + @Column({ type: Date, unique: false, nullable: false}) + dt_geracao_remessa: Date; + + @Column({ type: Date, unique: false, nullable: false}) + hr_geracao_remessa: Date; + + @Column({ type: Date, unique: false, nullable: false}) + dt_geracao_retorno: Date; + + @Column({ type: Date, unique: false, nullable: false}) + hr_geracao_retorno: Date; + + @Column({ type: String, unique: false, nullable: false}) + lote_servico: string; + + @Column({ type: String, unique: false, nullable: false}) + nome_pagador: string; + + @Column({ type: String, unique: false, nullable: false}) + agencia_pagador: string; + + @Column({ type: String, unique: false, nullable: false}) + dv_agencia_pagador: string; + + @Column({ type: String, unique: false, nullable: false}) + conta_pagador: string; + + @Column({ type: String, unique: false, nullable: false}) + dv_conta_pagador: string; + + @Column({ type: String, unique: false, nullable: false}) + nome_cliente?: string; + + @Column({ type: String, unique: false, nullable: false}) + cpf_cnpj_cliente?: string; + + @Column({ type: String, unique: false, nullable: false}) + cod_banco_cliente?: string; + + @Column({ type: String, unique: false, nullable: false}) + agencia_cliente?: string; + + @Column({ type: String, unique: false, nullable: false}) + dv_agencia_cliente?: string; + + @Column({ type: String, unique: false, nullable: false}) + conta_corrente_cliente?: string; + + @Column({ type: String, unique: false, nullable: false}) + dv_conta_corrente_cliente?: string; + + @Column({ type: String, unique: false, nullable: false}) + dt_vencimento?: Date; + + @Column({ type: String, unique: false, nullable: false}) + valor_lancamento?: number; + + @Column({ type: String, unique: false, nullable: false}) + data_efetivacao?: Date; + + @Column({ type: String, unique: false, nullable: false}) + valor_real_efetivado?: number; + + @Column({ type: String, unique: false, nullable: false}) + ocorrencias: string; + } \ No newline at end of file diff --git a/src/cnab/entity/detalhe-a.entity.ts b/src/cnab/entity/detalhe-a.entity.ts index 65eeb848..f949a365 100644 --- a/src/cnab/entity/detalhe-a.entity.ts +++ b/src/cnab/entity/detalhe-a.entity.ts @@ -56,4 +56,8 @@ export class DetalheA extends EntityHelper { @Column({ type: Number, unique: false, nullable: false }) nsr: number; -} + + @Column({ type: String, unique: false, nullable: false }) + ocorrencias: string; + +} \ No newline at end of file diff --git a/src/cnab/repository/arquivo-publicacao.repository.ts b/src/cnab/repository/arquivo-publicacao.repository.ts new file mode 100644 index 00000000..7dc0fb19 --- /dev/null +++ b/src/cnab/repository/arquivo-publicacao.repository.ts @@ -0,0 +1,21 @@ +import { Injectable, Logger } from "@nestjs/common"; +import { InjectRepository } from "@nestjs/typeorm"; +import { Repository } from "typeorm"; +import { ArquivoPublicacao } from "../entity/arquivo-publicacao.entity"; +import { ArquivoPublicacaoDTO } from "../dto/arquivo-publicacao.dto"; + +@Injectable() +export class ArquivoPublicacaoRepository { + private logger: Logger = new Logger('ArquivoPublicacaoRepository', { + timestamp: true, + }); + + constructor( + @InjectRepository(ArquivoPublicacao) + private arquivoPublicacaoRepository: Repository, + ) { } + + public async save(dto: ArquivoPublicacaoDTO): Promise { + return this.arquivoPublicacaoRepository.save(dto); + } +} \ No newline at end of file diff --git a/src/cnab/repository/transacao.repository.ts b/src/cnab/repository/transacao.repository.ts index 632166e2..94ec82d0 100644 --- a/src/cnab/repository/transacao.repository.ts +++ b/src/cnab/repository/transacao.repository.ts @@ -19,7 +19,6 @@ export class TransacaoRepository { public async save(dto: TransacaoDTO): Promise { return this.transacaoRepository.save(dto); - } public async findOne( diff --git a/src/cnab/service/cliente-favorecido.service.ts b/src/cnab/service/cliente-favorecido.service.ts index 5d9b08c5..f1ec17b5 100644 --- a/src/cnab/service/cliente-favorecido.service.ts +++ b/src/cnab/service/cliente-favorecido.service.ts @@ -1,10 +1,11 @@ -import { Injectable, Logger } from '@nestjs/common'; +import { HttpStatus, Injectable, Logger } from '@nestjs/common'; import { User } from 'src/users/entities/user.entity'; import { UsersService } from 'src/users/users.service'; import { ClienteFavorecido } from '../entity/cliente-favorecido.entity'; import { ClienteFavorecidoRepository } from '../repository/cliente-favorecido.repository'; import { SaveClienteFavorecidoDTO } from '../dto/cliente-favorecido.dto'; import { validateDTO } from 'src/utils/validation-utils'; +import { CommonHttpException } from 'src/utils/http-exception/common-http-exception'; @Injectable() export class ClienteFavorecidoService { @@ -44,6 +45,22 @@ export class ClienteFavorecidoService { return await this.clienteFavorecidoRepository.findAll({}); } + public async getOneByIdClienteFavorecido( + id_cliente_favorecido: number, + ): Promise { + const cliente_favorecido = + await this.clienteFavorecidoRepository.getOne({ id_cliente_favorecido: id_cliente_favorecido }); + if (!cliente_favorecido) { + throw CommonHttpException.errorDetails( + 'cliente_favorecido.conta not found', + { pagadorConta: id_cliente_favorecido }, + HttpStatus.NOT_FOUND, + ); + } else { + return cliente_favorecido; + } + } + private async saveFavorecidoFromUser( user: User, existingId_facorecido?: number, diff --git a/src/cnab/service/cnab.service.ts b/src/cnab/service/cnab.service.ts index 76a27d20..4c7c0455 100644 --- a/src/cnab/service/cnab.service.ts +++ b/src/cnab/service/cnab.service.ts @@ -16,4 +16,5 @@ export class CnabService { public async sendNewCNABs() { await this.headerArquivoService.saveRemessa() } + } diff --git a/src/cnab/service/header-arquivo.service.ts b/src/cnab/service/header-arquivo.service.ts index c8d1ba96..a22a1906 100644 --- a/src/cnab/service/header-arquivo.service.ts +++ b/src/cnab/service/header-arquivo.service.ts @@ -1,3 +1,6 @@ +import { ArquivoPublicacaoRepository } from './../repository/arquivo-publicacao.repository'; +import { ClienteFavorecidoService } from './cliente-favorecido.service'; +import { ArquivoPublicacaoDTO } from './../dto/arquivo-publicacao.dto'; import { Injectable, Logger } from '@nestjs/common'; import { BanksService } from 'src/banks/banks.service'; import { EntityCondition } from 'src/utils/types/entity-condition.type'; @@ -44,15 +47,17 @@ export class HeaderArquivoService { constructor( private headerArquivoRepository: HeaderArquivoRepository, + private arquivoPublicacaoRepository: ArquivoPublicacaoRepository, private transacaoService: TransacaoService, private headerLoteService: HeaderLoteService, private itemTransacaoService: ItemTransacaoService, private pagadorService: PagadorService, private detalheAService: DetalheAService, private detalheBService: DetalheBService, + private clienteFavorecidoService: ClienteFavorecidoService, private cnab104Service: Cnab104Service, private banksService: BanksService, - private sftpService: SftpService, + private sftpService: SftpService ) { } public async saveRemessa(): Promise { @@ -331,4 +336,51 @@ export class HeaderArquivoService { } return true; } + + public async compareRemessaToRetorno():Promise{ + const arquivos = await this.headerArquivoRepository.findAll(); + + arquivos.forEach(headerArquivo => { + const arquivoPublicacao = new ArquivoPublicacaoDTO(); + arquivoPublicacao.id_header_arquivo = headerArquivo.id_header_arquivo + arquivoPublicacao.id_transacao = headerArquivo.id_transacao; + + const headersLote = + this.headerLoteService.findMany({id_header_arquivo: headerArquivo.id_header_arquivo}); + headersLote.forEach(async headerLote=> { + arquivoPublicacao.id_header_lote = headerLote.id_header_lote; + arquivoPublicacao.dt_geracao_remessa = headerLote.dt_geracao; + arquivoPublicacao.hr_geracao_remessa = headerLote.hr_geracao; + arquivoPublicacao.dt_geracao_retorno = headerLote.dt_geracao; + arquivoPublicacao.hr_geracao_retorno = headerLote.hr_geracao; + arquivoPublicacao.lote_servico = headerLote.lote_servico; + const pagador = await this.pagadorService.getOneByIdPagador(headerLote.id_pagador); + + arquivoPublicacao.nome_pagador = pagador.nome_empresa; + arquivoPublicacao.agencia_pagador = pagador.agencia; + arquivoPublicacao.dv_agencia_pagador = pagador.dv_agencia; + arquivoPublicacao.conta_pagador = pagador.conta; + arquivoPublicacao.dv_conta_pagador = pagador.dv_conta; + + const detalhesA = await this.detalheAService.findMany({ id_header_lote: headerLote.id_header_lote }); + detalhesA.forEach( async detalheA => { + arquivoPublicacao.dt_vencimento = detalheA.dt_vencimento; + arquivoPublicacao.valor_lancamento = detalheA.valor_lancamento; + arquivoPublicacao.data_efetivacao = detalheA.data_efetivacao; + arquivoPublicacao.valor_real_efetivado = detalheA.valor_real_efetivado; + arquivoPublicacao.ocorrencias = detalheA.ocorrencias; + const clienteFavorecido = + await this.clienteFavorecidoService.getOneByIdClienteFavorecido(detalheA.id_cliente_favorecido); + arquivoPublicacao.nome_cliente = clienteFavorecido.nome ; + arquivoPublicacao.cpf_cnpj_cliente = clienteFavorecido.cpf_cnpj; + arquivoPublicacao.cod_banco_cliente = clienteFavorecido.cod_banco ; + arquivoPublicacao.agencia_cliente = clienteFavorecido.agencia; + arquivoPublicacao.dv_agencia_cliente = clienteFavorecido.dv_agencia; + arquivoPublicacao.conta_corrente_cliente = clienteFavorecido.conta_corrente; + arquivoPublicacao.dv_conta_corrente_cliente = clienteFavorecido.dv_conta_corrente; + void this.arquivoPublicacaoRepository.save(arquivoPublicacao); + }); + }); + }); + } } diff --git a/src/cnab/service/transacao.service.ts b/src/cnab/service/transacao.service.ts index aeed12e7..92ce0f8c 100644 --- a/src/cnab/service/transacao.service.ts +++ b/src/cnab/service/transacao.service.ts @@ -8,8 +8,9 @@ import { PagadorContaEnum } from '../enums/pagador/pagador.enum'; import { TransacaoRepository } from '../repository/transacao.repository'; import { TransacaoDTO } from './../dto/transacao.dto'; import { ClienteFavorecidoService } from './cliente-favorecido.service'; -import { ItemTransacaoService } from './item-transacao.service'; + import { PagadorService } from './pagador.service'; +import { ItemTransacaoService } from './item-transacao.service'; @Injectable() export class TransacaoService {