feat: adiciona filtro de cobrança e converte para query builder no deb.repository
This commit is contained in:
67
src/orders/repositories/deb.repository.ts
Normal file
67
src/orders/repositories/deb.repository.ts
Normal file
@@ -0,0 +1,67 @@
|
||||
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { DataSource } from 'typeorm';
|
||||
import { InjectDataSource } from '@nestjs/typeorm';
|
||||
|
||||
@Injectable()
|
||||
export class DebRepository {
|
||||
constructor(
|
||||
@InjectDataSource("oracle") private readonly oracleDataSource: DataSource,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Busca débitos por CPF/CGCENT
|
||||
* @param cpfCgcent - CPF ou CGCENT do cliente
|
||||
* @param matricula - Matrícula do funcionário (opcional)
|
||||
* @param cobranca - Código de cobrança (opcional)
|
||||
* @returns Lista de débitos do cliente
|
||||
*/
|
||||
async findByCpfCgcent(cpfCgcent: string, matricula?: number, cobranca?: string) {
|
||||
const queryRunner = this.oracleDataSource.createQueryRunner();
|
||||
await queryRunner.connect();
|
||||
try {
|
||||
const queryBuilder = queryRunner.manager
|
||||
.createQueryBuilder()
|
||||
.select([
|
||||
'p.dtemissao AS "dtemissao"',
|
||||
'p.codfilial AS "codfilial"',
|
||||
'p.duplic AS "duplic"',
|
||||
'p.prest AS "prest"',
|
||||
'p.codcli AS "codcli"',
|
||||
'c.cliente AS "cliente"',
|
||||
'p.codcob AS "codcob"',
|
||||
'cb.cobranca AS "cobranca"',
|
||||
'p.dtvenc AS "dtvenc"',
|
||||
'p.dtpag AS "dtpag"',
|
||||
'p.valor AS "valor"',
|
||||
`CASE
|
||||
WHEN p.dtpag IS NOT NULL THEN 'PAGO'
|
||||
WHEN p.dtvenc < TRUNC(SYSDATE) THEN 'EM ATRASO'
|
||||
WHEN p.dtvenc >= TRUNC(SYSDATE) THEN 'A VENCER'
|
||||
ELSE 'NENHUM'
|
||||
END AS "situacao"`,
|
||||
])
|
||||
.from('pcprest', 'p')
|
||||
.innerJoin('pcclient', 'c', 'p.codcli = c.codcli')
|
||||
.innerJoin('pccob', 'cb', 'p.codcob = cb.codcob')
|
||||
.innerJoin('pcempr', 'e', 'c.cgcent = e.cpf')
|
||||
.where('p.codcob NOT IN (:...excludedCob)', { excludedCob: ['DESD', 'CANC'] })
|
||||
.andWhere('c.cgcent = :cpfCgcent', { cpfCgcent });
|
||||
|
||||
if (matricula) {
|
||||
queryBuilder.andWhere('e.matricula = :matricula', { matricula });
|
||||
}
|
||||
|
||||
if (cobranca) {
|
||||
queryBuilder.andWhere('p.codcob = :cobranca', { cobranca });
|
||||
}
|
||||
|
||||
queryBuilder.orderBy('p.dtvenc', 'ASC');
|
||||
|
||||
const result = await queryBuilder.getRawMany();
|
||||
return result;
|
||||
} finally {
|
||||
await queryRunner.release();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user